summaryrefslogtreecommitdiffstats
path: root/eurephiadm/commands/users.c
blob: aaeb666ef972e176fb66e25d483610a3b77f05f4 (plain)
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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
/* users.c  --  eurephiadm command - User Account Management
 *
 *  GPLv2 - Copyright (C) 2008  David Sommerseth <dazo@users.sourceforge.net>
 *
 *  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 2
 *  of the License.
 *
 *  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, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 *
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>

#define MODULE "eurephia::Users"
#include <eurephia_nullsafe.h>
#include <eurephia_context.h>
#include <eurephia_log.h>
#include <eurephia_values_struct.h>
#include <eurephiadb_session_struct.h>
#include <eurephia_admin_struct.h>
#include <eurephia_admin_common.h>
#include <eurephiadb_mapping.h>
#include <eurephiadb_driver.h>

#include "../argparser.h"

inline void field_print_int(char *label, int val) {
        printf("%25.25s: %i\n", label, val);
}

inline void field_print_str(char *label, char *val) {
        printf("%25.25s: %s\n", label, (val == NULL ? "-" : val));
}

void display_users_help(int page) {
        printf("\n%s -- Administer user accounts\n\n", MODULE);

        switch( page ) {
        case 'l':
                printf("The list mode can accept one parameter:\n\n"
                       "     -S | --sort <sort key>     Decide the sort order of the user list\n"
                       "\n"
                       "Available sort keys are: id, username, activated, deactivated and lastaccess\n\n");
                break;

        case 's':
                printf("The show mode shows more information about a user account.\n"
                       "The needed arguments are one of the following arguments:\n\n"
                       "     -i | --uid          Numeric user ID\n"
                       "     -u | --username     User name\n"
                       "     -l | --lastlog      Show users lastlog entries\n"
                       "     -a | --attempts     Show users failed attempts and blaclisting\n"
                       "     -b | --blacklist    Alias for --attempts\n"
                       "\n");
                break;

        case 'a':
                printf("The activate mode activates an account which not activated or deactivated.\n"
                       "The needed arguments are one of the following arguments:\n\n"
                       "     -i | --uid          Numeric user ID\n"
                       "     -u | --username     User name\n\n");
                break;

        case 'd':
                printf("The deactivate mode deactivates a user account.\n"
                       "The needed arguments are one of the following arguments:\n\n"
                       "     -i | --uid          Numeric user ID\n"
                       "     -u | --username     User name\n\n");
                break;

        case 'A':
                printf("Add user mode\n");
                break;

        case 'D':
                printf("Delete user mode\n");
                break;

        case 'p':
                printf("Change password mode\n");
                break;

        default:
                printf("Available modes:\n"
                       "     -l | --list         List all user accounts\n"
                       "     -s | --show         Show user account details\n"
                       "     -a | --activate     Activate a user account\n"
                       "     -d | --deactivate   Deactivate a user account\n"
                       "     -A | --add          Add a new user account\n"
                       "     -D | --delete       Delete a user account\n"
                       "     -p | --password     Change password on a users account\n"
                       "     -h | --help <mode>  Further help for these modes\n\n");
                break;
        }
}

int help_Users2(eurephiaCTX *ctx, eurephiaSESSION *sess, eurephiaVALUES *cfg, int argc, char **argv) {
        e_options helpargs[] = {
                {"--list", "-l", 0},
                {"--show", "-s", 0},
                {"--activate", "-a", 0},
                {"--deactivate", "-d", 0},
                {"--add", "-A", 0},
                {"--delete", "-D", 0},
                {"--password", "-p", 0},
                {NULL, NULL, 0}
        };

        int i = 1;
        display_users_help(eurephia_getopt(&i, argc, argv, helpargs));
        return 0;
}

void help_Users() {
        display_users_help(0);
}


int list_users(eurephiaCTX *ctx, eurephiaSESSION *sess, eurephiaVALUES *cfg, int argc, char **argv) {
        eurephiaUSERLIST *list = NULL;
        eurephiaUSERINFO *user = NULL;
        int i = 0;
        char *sortkeys = NULL;

        e_options listargs[] = {
                {"--sort", "-S", 1},
                {"--help", "-h", 0},
                {NULL, NULL, 0}
        };

        assert((ctx != NULL) && (ctx->dbc != NULL) && (ctx->dbc->config != NULL));

        for( i = 1; i < argc; i++ ) {
                switch( eurephia_getopt(&i, argc, argv, listargs) ) {
                case 'S':
                        sortkeys = optargs[0];
                        i++;
                        break;

                case 'h':
                        display_users_help('l');
                        return 0;

                default:
                        fprintf(stderr, "%s Unkown argument: %s\n", MODULE, argv[i]);
                        return 1;
                }
        }

        list = eDBadminGetUserList(ctx, sortkeys);
        if( list == NULL ) {
                fprintf(stderr, "Error retrieving user list\n");
                return 1;
        }


        fprintf(stdout, "%3s %-20.32s %-16.16s %-16.16s %-16.16s\n",
                "ID", "Username", "Activated","Deactivated","Last access");
        fprintf(stdout, "---------------------------------------------------------------------------\n");

        for( user = list->users; user != NULL; user = user->next ) {
                fprintf(stdout, "%3i %-20.32s %-16.16s %-16.16s %-16.16s\n",
                        user->uid,
                        user->username,
                        (user->activated == NULL ? "(Not active)": user->activated),
                        (user->deactivated == NULL ? "-": user->deactivated),
                        (user->last_accessed == NULL ? "-": user->last_accessed));
        }
        fprintf(stdout, "---------------------------------------------------------------------------\n");
        eAdminFreeUSERLIST(list);

        return 0;
}

// Show account information for a particular user
int show_user(eurephiaCTX *ctx, eurephiaSESSION *sess, eurephiaVALUES *cfg, int argc, char **argv) {
        eurephiaUSERINFO *user = NULL;
        char *uname = NULL;
        int i, uid = 0;
        long int show_info = USERINFO_user | USERINFO_certs;

        e_options activargs[] = {
                {"--uid", "-i", 1},
                {"--username", "-u", 1},
                {"--lastlog", "-l", 0},
                {"--attempts", "-a", 0},
                {"--blacklist", "-b", 0}, // Alias for -a | --attempts
                {"--help", "-h", 0},
                {NULL, NULL, 0}
        };

        assert((ctx != NULL) && (ctx->dbc != NULL) && (ctx->dbc->config != NULL));

        for( i = 1; i < argc; i++ ) {
                switch( eurephia_getopt(&i, argc, argv, activargs) ) {
                case 'i':
                        uid = atoi_nullsafe(optargs[0]);
                        if( uid == 0 ) {
                                fprintf(stderr, "%s: Invalid user id\n", MODULE);
                                return 1;
                        }
                        break;

                case 'u':
                        uname = strdup_nullsafe(optargs[0]);
                        if( strlen_nullsafe(uname) < 3 ) {
                                fprintf(stderr, "%s: User name too short\n", MODULE);
                                return 1;
                        }
                        break;

                case 'a':
                case 'b':
                        show_info = USERINFO_attempts | USERINFO_blacklist;
                        break;

                case 'l':
                        show_info = USERINFO_user | USERINFO_lastlog;
                        break;

                case 'h':
                        display_users_help('s');
                        return 0;

                default:
                        return 1;
                }
        }

        if( (uid == 0) && (uname == NULL) ){
                fprintf(stderr,
                        "%s:  Missing required parameter.  You must provide either user id or user name\n",
                        MODULE);
                return 1;
        }

        if( (uid > 0) && (uname != NULL) ) {
                fprintf(stderr,
                        "%s:  You cannot use both user id and user name, only one of them.\n",
                        MODULE);
                return 1;
        }

        user = eAdminPopulateUSERINFO(uid, uname, NULL, NULL, NULL, NULL);
        assert(user != NULL);
        if( eDBadminGetUserInfo(ctx, show_info, user) == 0 )  {
                fprintf(stderr, "%s: User not found\n", MODULE);
                return 1;
        }

        if( show_info & USERINFO_user ) {
                field_print_int("User id", user->uid);
                field_print_str("User name", user->username);
                field_print_str("Last accessed", user->last_accessed);
                field_print_str("Activated", user->activated);
                field_print_str("Dectivated", user->deactivated);
                printf("\n");
        }

        // Show associated certificates if we have that info
        if( (user->certlist != NULL) && (user->certlist->num_certs > 0) ) {
                eurephiaCERTINFO *crt = NULL;

                field_print_int("Associated certificates", user->certlist->num_certs);
                printf(" %3s (D) %-35.35s %33.33s\n         %-49.49s %19.19s\n",
                       "ID", "Common name", "Organisation", "e-mail", "Registered");
#ifdef FIREWALL
                printf("         %-44.44s %24.24s\n", "Firewall access profile", "FW Destination");
#endif
                printf(" --------------------------------------------------------------------"
                       "----------\n");
                for( crt = user->certlist->certs; crt != NULL; crt = crt->next) {
                        printf(" %3i (%1i) %-35.35s %33.33s\n         %-49.49s %19.19s\n",
                               crt->certid, crt->depth, crt->common_name, crt->organisation,
                               crt->email, crt->registered);
#ifdef FIREWALL
                        printf("         %-44.44s %24.24s\n",
                               ((crt->access != NULL) && (crt->access->access_descr != NULL)
                                ? crt->access->access_descr : "(No firewall profile setup)"),
                               ((crt->access != NULL) && (crt->access->fwprofile != NULL)
                                ? crt->access->fwprofile : "-")

                               );
#endif
                        if( crt->next != NULL ) {
                                printf("\n");
                        }
                }
                printf(" --------------------------------------------------------------------"
                       "----------\n");
        } else {
                // If we wanted to show associated certs, and didn't find any - inform about it
                if( (show_info & USERINFO_certs) == USERINFO_certs ) {
                        field_print_str("Associated certificates", "None");
                }
        }
        eAdminFreeUSERINFO(user);
        free_nullsafe(uname);

        return 0;
}


// This function handles activation and deactivation of an account
int account_activation(eurephiaCTX *ctx, eurephiaSESSION *sess, eurephiaVALUES *cfg, int argc, char **argv) {
        eurephiaUSERINFO *user = NULL;
        char *uname = NULL;
        int i, uid = 0, actmode = 0, rc = 0;

        e_options activargs[] = {
                {"--uid", "-i", 1},
                {"--username", "-u", 1},
                {"--help", "-h", 0},
                {NULL, NULL, 0}
        };

        assert((ctx != NULL) && (ctx->dbc != NULL) && (ctx->dbc->config != NULL));

        if( (strcmp(argv[0], "--activate") == 0) || (strcmp(argv[0], "-a") == 0) ) {
                actmode = 'a';
        } else if( (strcmp(argv[0], "--deactivate") == 0) || (strcmp(argv[0], "-d") == 0) ) {
                actmode = 'd';
        } else {
                fprintf(stderr, "%s: System error - illegal users mode'%s'\n", MODULE, argv[0]);
                return 1;
        }

        for( i = 1; i < argc; i++ ) {
                switch( eurephia_getopt(&i, argc, argv, activargs) ) {
                case 'i':
                        uid = atoi_nullsafe(optargs[0]);
                        if( uid == 0 ) {
                                fprintf(stderr, "%s: Invalid user id\n", MODULE);
                                return 1;
                        }
                        break;

                case 'u':
                        uname = strdup_nullsafe(optargs[0]);
                        if( strlen_nullsafe(uname) < 3 ) {
                                fprintf(stderr, "%s: User name too short\n", MODULE);
                                return 1;
                        }
                        break;

                case 'h':
                        display_users_help(actmode);
                        return 0;

                default:
                        return 1;
                }
        }
        if( (uid == 0) && (uname == NULL) ){
                fprintf(stderr,
                        "%s:  Missing required parameter.  You must provide either user id or user name\n",
                        MODULE);
                return 1;
        }

        if( (uid > 0) && (uname != NULL) ) {
                fprintf(stderr,
                        "%s:  You cannot use both user id and user name, only one of them.\n",
                        MODULE);
                return 1;
        }

        user = eAdminPopulateUSERINFO(uid, uname, NULL, NULL, NULL, NULL);
        assert(user != NULL);
        if( eDBadminGetUserInfo(ctx, USERINFO_user, user) == 0 )  {
                fprintf(stderr, "%s: User not found\n", MODULE);
                return 1;
        }

        switch( actmode ) {
        case 'a':  // Activate a user account
                if( (user->activated != NULL)  && (user->deactivated == NULL) ) {
                        printf("User account is already active\n");
                } else {
                        // Set activated field to now()
                        free_nullsafe(user->activated);
                        user->activated = strdup("CURRENT_TIMESTAMP");

                        // Remove deactivated flag
                        free_nullsafe(user->deactivated);
                        user->setnull_flags = FIELD_DEACTIVATED;

                        // Update the user record
                        rc = eDBadminUpdateUser(ctx, user->uid, user);
                        if( rc == 1 ) {
                                printf("User account is activated\n");
                        } else {
                                fprintf(stderr, "%s: Activating user account failed\n", MODULE);
                        }
                }
                break;

        case 'd':  // Deactivate a user account
                if( (user->activated != NULL)  && (user->deactivated != NULL) ) {
                        printf("User account is already deactived\n");
                } else if( (user->activated != NULL) ) {
                        // Set deactivated to now()
                        free_nullsafe(user->deactivated);
                        user->deactivated = strdup("CURRENT_TIMESTAMP");

                        // Update the user record
                        rc = eDBadminUpdateUser(ctx, user->uid, user);
                        if( rc == 1 ) {
                                printf("User account is deactivated\n");
                        } else {
                                fprintf(stderr, "%s: Deactivating the user account failed\n", MODULE);
                        }
                } else {
                        fprintf(stderr, "%s: User account is not activated yet\n", MODULE);
                }
                break;
        }

        eAdminFreeUSERINFO(user);
        free_nullsafe(uname);

        return 0;
}


int cmd_Users(eurephiaCTX *ctx, eurephiaSESSION *sess, eurephiaVALUES *cfg, int argc, char **argv) {
        char **mode_argv;
        int i, mode_argc = 0, rc = 0;
        int (*mode_fnc) (eurephiaCTX *ctx, eurephiaSESSION *sess, eurephiaVALUES *cfg, int argc, char **argv);

        e_options modeargs[] = {
                {"--list", "-l", 0},
                {"--show", "-s", 0},
                {"--activate", "-a", 0},
                {"--deactivate", "-d", 0},
                {"--add", "-A", 0},
                {"--delete", "-D", 0},
                {"--password", "-p", 0},
                {"--help", "-h", 0},
                {NULL, NULL, 0}
        };

        assert((ctx != NULL) && (ctx->dbc != NULL) && (ctx->dbc->config != NULL));
        mode_fnc = NULL;
        for( i = 1; i < argc; i++ ) {
                switch( eurephia_getopt(&i, argc, argv, modeargs) ) {
                case 'l':
                        mode_fnc = list_users;
                        break;

                case 's':
                        mode_fnc = show_user;
                        break;

                case 'a':
                        mode_fnc = account_activation;
                        break;

                case 'd':
                        mode_fnc = account_activation;
                        break;

                case 'h':
                        mode_fnc = help_Users2;
                        break;

                default:
                        break;
                }
                if( mode_fnc != NULL ) {
                        break;
                }
        }

        // If we do not have any known mode defined, exit with error
        if( mode_fnc == NULL )  {
                fprintf(stderr, "Unknown argument.  No mode given\n");
                return 1;
        }

        // Allocate memory for our arguments being sent to the mode function
        mode_argv = (char **) calloc(sizeof(char *), (argc - i)+2);
        assert(mode_argv != NULL);

        // Copy over only the arguments needed for the mode
        mode_argc = eurephia_arraycp(i, argc, argv, mode_argv, (argc - i));

        // Call the mode function
        rc = mode_fnc(ctx, sess, cfg, mode_argc, mode_argv);
        free_nullsafe(mode_argv);

        return rc;
}