-
Notifications
You must be signed in to change notification settings - Fork 0
/
freeipa_manager.py
executable file
·387 lines (275 loc) · 14.7 KB
/
freeipa_manager.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
#!/usr/bin/env python3
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, version 3.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# Copyright (C) 2021 Unai Goikoetxeta
import os
from argparse import Namespace
from utils.utils import Utils
def app_option_check_cache(app_utils: Utils, quiet: bool) -> None:
status = app_utils.get_cache_handler().is_cache_outdated()
if status and not quiet:
print('Cache is outdated')
elif not status and not quiet:
print('Cache is up-to-date')
def app_option_check_servers(app_utils: Utils, quiet: bool) -> None:
ad_reachable, freeipa_reachable, smtp_reachable = app_utils.check_server_connectivity()
if ad_reachable == freeipa_reachable == smtp_reachable is True and not quiet:
print('FreeIPA, AD and SMTP servers are reachable')
elif not quiet:
if not ad_reachable:
print('AD server is not reachable')
if not freeipa_reachable:
print('FreeIPA server is not reachable')
if not smtp_reachable:
print('SMTP server is not reachable')
def app_option_delete_cache(app_utils: Utils, quiet: bool) -> None:
status = app_utils.get_cache_handler().delete_cache()
if status and not quiet:
print('Cache files have been deleted')
elif not quiet:
print('Cache files were not deleted because they do not exist')
def app_option_delete_user(user_id: str, app_utils: Utils, quiet: bool) -> None:
status = app_utils.get_freeipa_handler().delete_freeipa_user(user_id)
if status and not quiet:
print(f'User {user_id} deleted successfully')
elif not quiet:
print(f'User {user_id} could not be deleted')
def app_option_delete_user_otp_tokens(user_id: str, app_utils: Utils, quiet: bool) -> None:
status = app_utils.get_freeipa_handler().delete_freeipa_user_otp_tokens(user_id)
if status and not quiet:
print(f'OTP token(s) removed for user {user_id}')
elif not quiet:
print(f'OTP token(s) could not be removed for user {user_id}')
def app_option_disable_user(user_id: str, app_utils: Utils, quiet: bool) -> None:
status = app_utils.get_freeipa_handler().disable_freeipa_user(user_id)
if status and not quiet:
print(f'User {user_id} disabled successfully')
elif not quiet:
print(f'User {user_id} could not be disabled')
def app_option_enable_user(user_id: str, app_utils: Utils, quiet: bool) -> None:
status = app_utils.get_freeipa_handler().enable_freeipa_user(user_id)
if status and not quiet:
print(f'User {user_id} enabled successfully')
elif not quiet:
print(f'User {user_id} could not be enabled')
def app_option_export_file(export_file: str, app_utils: Utils, quiet: bool) -> None:
if export_file and export_file[:1] not in ['.', '/']:
export_file = os.getcwd() + '/' + export_file
elif not export_file:
export_file = os.getcwd() + '/' + app_utils.get_csv_files()['export_file']
status = app_utils.get_freeipa_handler().export_to_csv(export_file)
if status and not quiet:
print(f'FreeIPA users exported successfully to {export_file}')
elif not quiet:
print(f'FreeIPA users could not be exported to {export_file}')
def app_option_import_file(import_file: str, app_utils: Utils, quiet: bool) -> None:
if import_file and import_file[:1] not in ['.', '/']:
import_file = os.getcwd() + '/' + import_file
elif not import_file:
import_file = os.getcwd() + '/' + app_utils.get_csv_files()['import_file']
imported_users, updated_users, skipped_users, not_imported_users = app_utils.import_csv(import_file)
if imported_users and not quiet:
print('The following users were imported to FreeIPA:')
for user in imported_users:
print(f" - {user} with temporary password '{imported_users[user]}'")
if updated_users and not quiet:
print('The following users were updated in FreeIPA:')
for user in updated_users:
print(f' - {user}')
if skipped_users and not quiet:
print('The following users required no update and were skipped:')
for user in skipped_users:
print(f' - {user}')
if not_imported_users and not quiet:
print('The following users were not imported to FreeIPA due to errors:')
for user in not_imported_users:
print(f' - {user}')
if (imported_users or updated_users or skipped_users or not_imported_users) and not quiet:
print('FreeIPA user import completed.')
elif not quiet:
print('No users to import at this time.')
def app_option_list_expired_users(app_utils: Utils, quiet: bool) -> None:
expired_users, expired_users_disabled = app_utils.get_freeipa_handler().get_expired_users()
if expired_users and not quiet:
print('The passwords of the following users have expired within the last '
f'{app_utils.get_password_gracious_period()} days:')
for user_id in expired_users:
print(f' - {user_id}')
if expired_users_disabled and not quiet:
print('The following user accounts have been disabled for not changing their password within '
f'{app_utils.get_password_gracious_period()} days after expiration:')
for user_id in expired_users_disabled:
print(f' - {user_id}')
if not expired_users and not expired_users_disabled and not quiet:
print('There are no expired users at this time')
def app_option_list_users_no_password(app_utils: Utils, quiet: bool) -> None:
users_no_password = app_utils.get_freeipa_handler().get_users_no_password()
if users_no_password and not quiet:
print('The following users must change their password: ')
for user_id in users_no_password:
print(f' - {user_id}')
elif not quiet:
print('There are no users pending password change at this time')
def app_option_process_password_expirations(app_utils: Utils, quiet: bool) -> None:
notified, expired, disabled = app_utils.process_password_expirations()
if notified and not quiet:
print('The following users were notified for upcoming password expiration:')
for user in notified:
print(f' - {user}')
elif not quiet:
print('No user password expiration reminders to send at this time')
if expired and not quiet:
print('Passwords for the following users have expired:')
for user in expired:
print(f' - {user}')
if disabled and not quiet:
print('The following users were disabled for not changing their passwords within '
f'the gracious period of {app_utils.get_password_gracious_period()} days:')
for user in disabled:
print(f' - {user}')
if not notified and not expired and not disabled and not quiet:
print('No upcoming password expirations or expired users found.')
def app_option_process_terminated_users(app_utils: Utils, quiet: bool) -> None:
deleted_users, not_deleted_users = app_utils.delete_terminated_users()
if deleted_users and not quiet:
print(f'The following terminated users were deleted from FreeIPA:')
for user in deleted_users:
print(f' - {user}')
if not_deleted_users and not quiet:
print('The following terminated users could not be deleted from FreeIPA:')
for user in not_deleted_users:
print(f' - {user}')
if not deleted_users and not not_deleted_users and not quiet:
print('No terminated users identified at this time')
def add_option_remind_password_change(app_utils: Utils, quiet: bool) -> None:
status, users_no_password = app_utils.remind_password_change()
if status and not quiet:
print('The following users have been notified to change their passwords: ')
for user in users_no_password:
print(f' - {user}')
elif not quiet:
print('No user found to be notified for password change')
def app_option_reset_user_password(user_id: str, app_utils: Utils, quiet: bool) -> None:
success, password = app_utils.reset_user_password(user_id)
if success and not quiet:
print(f"Password for user {user_id} successfully changed to '{password}', user notified")
elif not quiet:
print(f'Password could not be changed for user {user_id}')
def app_option_template_file(template_file: str, app_utils: Utils, quiet: bool) -> None:
if template_file and template_file[:1] not in ['.', '/']:
template_file = os.getcwd() + '/' + template_file
elif not template_file:
template_file = os.getcwd() + '/' + app_utils.get_csv_files()['import_template']
status = app_utils.get_freeipa_handler().create_csv_template(template_file)
if status and not quiet:
print(f'Import CSV template successfully created at {template_file}')
elif not quiet:
print(f'Could not create the CSV template file at {template_file}')
def app_option_update_ad_cache(app_utils: Utils, quiet: bool) -> None:
ad_users = app_utils.get_ad_handler().get_ad_users(force_update_cache=True)
if ad_users and not quiet:
print('AD user cache has been updated')
elif not ad_users and not quiet:
print('AD user cache could not be updated')
def app_option_update_cache_files(app_utils: Utils, quiet: bool) -> None:
ad_users = app_utils.get_ad_handler().get_ad_users(force_update_cache=True)
freeipa_users = app_utils.get_freeipa_handler().get_freeipa_users(force_update_cache=True)
if ad_users and freeipa_users and not quiet:
print('FreeIPA and AD user caches have been updated')
elif ad_users and not quiet:
print('AD users cache has been updated but could not update the FreeIPA cache')
elif freeipa_users and not quiet:
print('FreeIPA users cache has been updated but could not update the AD cache')
elif not quiet:
print('FreeIPA and AD user cache files could not be updated')
def app_option_update_freeipa_cache(app_utils: Utils, quiet: bool) -> None:
freeipa_users = app_utils.get_freeipa_handler().get_freeipa_users(force_update_cache=True)
if freeipa_users and not quiet:
print('FreeIPA user cache has been updated')
elif not freeipa_users and not quiet:
print('FreeIPA user cache could not be updated')
def app_option_update_from_ad(app_utils: Utils, quiet: bool) -> None:
updates_success, updates_unsuccessful = app_utils.update_user_data_from_ad()
if updates_success and not quiet:
print('The following users were updated:')
for user in updates_success:
print(f' - {user}')
if updates_unsuccessful and not quiet:
print('The following users could not be updated: ')
for user in updates_unsuccessful:
print(f' - {user}')
if not updates_success and not updates_unsuccessful and not quiet:
print('No users to update at this time')
def check_for_logging_args(app_utils: Utils, args: Namespace) -> None:
logger = app_utils.get_logger()
if args.verbose:
logger.enable_verbose()
if args.info_log_level:
logger.set_level_info()
elif args.debug_log_level:
logger.set_level_debug()
if __name__ == "__main__":
valid_environment = False
config_file = os.path.dirname(os.path.realpath(__file__)) + '/config.yaml'
if os.path.isfile(config_file):
utils = Utils(config_file)
parser, cli_args = utils.get_menu().generate_menu()
if utils.validate_running_environment(cli_args.check_servers):
valid_environment = True
check_for_logging_args(utils, cli_args)
if cli_args.check_servers:
app_option_check_servers(utils, cli_args.quiet)
elif cli_args.check_cache:
app_option_check_cache(utils, cli_args.quiet)
elif cli_args.update_ad_cache:
app_option_update_ad_cache(utils, cli_args.quiet)
elif cli_args.update_freeipa_cache:
app_option_update_freeipa_cache(utils, cli_args.quiet)
elif cli_args.update_cache_files:
app_option_update_cache_files(utils, cli_args.quiet)
elif cli_args.delete_cache:
app_option_delete_cache(utils, cli_args.quiet)
elif cli_args.list_expired_users:
app_option_list_expired_users(utils, cli_args.quiet)
elif cli_args.list_users_no_password:
app_option_list_users_no_password(utils, cli_args.quiet)
elif cli_args.disable_user:
app_option_disable_user(cli_args.disable_user.lower().strip(), utils, cli_args.quiet)
elif cli_args.enable_user:
app_option_enable_user(cli_args.enable_user.lower().strip(), utils, cli_args.quiet)
elif cli_args.delete_user:
app_option_delete_user(cli_args.delete_user.lower().strip(), utils, cli_args.quiet)
elif cli_args.delete_user_otp_tokens:
app_option_delete_user_otp_tokens(cli_args.delete_user_otp_tokens.lower().strip(), utils,
cli_args.quiet)
elif cli_args.reset_user_password:
app_option_reset_user_password(cli_args.reset_user_password.lower().strip(), utils, cli_args.quiet)
elif cli_args.export_file is not None:
app_option_export_file(cli_args.export_file.lower().strip(), utils, cli_args.quiet)
elif cli_args.import_file is not None:
app_option_import_file(cli_args.import_file.lower().strip(), utils, cli_args.quiet)
elif cli_args.template_file is not None:
app_option_template_file(cli_args.template_file.lower().strip(), utils, cli_args.quiet)
elif cli_args.update_from_ad:
app_option_update_from_ad(utils, cli_args.quiet)
elif cli_args.process_password_expirations:
app_option_process_password_expirations(utils, cli_args.quiet)
elif cli_args.process_terminated_users:
app_option_process_terminated_users(utils, cli_args.quiet)
elif cli_args.remind_password_change:
add_option_remind_password_change(utils, cli_args.quiet)
else:
parser.print_help()
if not valid_environment:
print('The program is missing required files or server connectivity to run and cannot be executed')