summaryrefslogtreecommitdiffstats
path: root/eurephiadm/commands/adminaccess.c
blob: 1bcb65ae7f65eb4bbd8ba0e4a45495f300236e3d (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
/* useraccess.c  --  eurephiadm useraccess command:
 *                   Management of user access levels (eurephia_adminaccess table)
 *
 *  GPLv2 only - Copyright (C) 2009
 *               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.
 *
 */

/**
 * @file   adminaccess.c
 * @author David Sommerseth <dazo@users.sourceforge.net>
 * @date   2009-03-24
 *
 * @brief  eurephiadm command 'useraccess'.  Management of user access levels
 *
 */

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

#ifdef HAVE_LIBXML2
#include <libxml/tree.h>
#include <libxml/xpath.h>
#endif

#define MODULE "eurephia::UserAdmin" /**< Need to define the active module before including argparser.h */
#include <eurephia_nullsafe.h>
#include <eurephia_context.h>
#include <eurephia_log.h>
#include <eurephia_xml.h>
#include <eurephia_values_struct.h>
#include <eurephiadb_session_struct.h>
#include <eurephiadb_mapping.h>
#include <eurephiadb_driver.h>
#include <certinfo.h>

#include "../argparser.h"
#include "../field_print.h"
#include "../xsltparser.h"

/**
 * Help screens for the adminaccess command
 *
 * @param page which help screen to display
 */
void display_adminaccess_help(int page) {
        switch( page ) {
        case 'G':
                printf("The grant mode will grant a user access to a specified access level.\n"
                       "\n"
                       "     -i | --uid          User account ID\n"
                       "     -u | --username     User name\n"
                       "     -I | --interface    Grant access through which interface (default 'C')\n"
                       "     -a | --access-level Grant access which access level\n"
                       "\n"
                       );
                break;
        case 'R':
                printf("The revoke mode will remove the access from the desired user accounts.\n"
                       "\n"
                       "     -i | --uid          User account ID\n"
                       "     -I | --interface    Grant access through which interface (default 'C')\n"
                       "     -a | --access-level Grant access which access level\n"
                       "\n"
                       );
                break;
        case 'l':
                printf("The list mode will show the granted user access levels\n"
                       "\n"
                       "     -i | --uid          User account ID\n"
                       "     -u | --username     User name\n"
                       "     -I | --interface    Grant access through which interface (default 'C')\n"
                       "     -a | --access-level Grant access which access level\n"
                       "\n"
                       );
                break;
        default:
                printf("Available modes for the adminaccess command are:\n\n"
                       "     -G | --grant       Grant a specific access level to a user\n"
                       "     -R | --revoke      Revoke access levels\n"
                       "     -l | --list        List all granted access levels\n"
                       "     -h | --help <mode> Help about a specific mode\n\n");
                break;
        }
}

/**
 * Help screen wrapper.  Used by cmd_Help()
 */
void help_AdminAccess() {
        display_adminaccess_help(0);
}


/**
 * Help screen wrapper for the adminaccess help function.
 *
 * @param ctx   eurephiaCTX
 * @param sess  eurephiaSESSION of the current logged in user
 * @param cfg   eurephiaVALUES struct of the current configuration
 * @param argc  argument count for the eurephiadm command
 * @param argv  argument table for the eurephiadm command
 *
 * @return returns 0 on success, otherwise 1.
 */
int help_AdminAccess2(eurephiaCTX *ctx, eurephiaSESSION *sess, eurephiaVALUES *cfg, int argc, char **argv) {
        e_options helpargs[] = {
                {"--list", "-l", 0},
                {"--grant", "-G", 0},
                {"--revoke", "-R", 0},
                {NULL, NULL, 0}
        };

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

/**
 * adminaccess list mode, lists all users access levels
 *
 * @param ctx   eurephiaCTX
 * @param sess  eurephiaSESSION of the current logged in user
 * @param cfg   eurephiaVALUES struct of the current configuration
 * @param argc  argument count for the eurephiadm command
 * @param argv  argument table for the eurephiadm command
 *
 * @return returns 0 on success, otherwise 1.
 */
int list_adminaccess(eurephiaCTX *ctx, eurephiaSESSION *sess, eurephiaVALUES *cfg, int argc, char **argv) {
        xmlDoc *list_xml = NULL, *srch_xml = NULL;
        xmlNode *srch_n = NULL, *tmp_n = NULL;
        int i = 0;
        char *uid = NULL, *username = NULL, *intf = NULL, *acl = NULL;

        e_options listargs[] = {
                {"--uid", "-i", 1},
                {"--username", "-u", 1},
                {"--interface", "-I", 1},
                {"--access-level", "-a", 1},
                {"--help", "-h", 0},
                {NULL, NULL, 0}
        };

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

        // Parse arguments
        for( i = 1; i < argc; i++ ) {
                switch( eurephia_getopt(&i, argc, argv, listargs) ) {
                case 'i':
                        uid = optargs[0];
                        break;
                case 'u':
                        username = optargs[0];
                        break;
                case 'I':
                        intf = optargs[0];
                        break;
                case 'a':
                        acl = optargs[0];
                        break;

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

                default:
                        return 1;
                }
        }

        // Build up search XML
        eurephiaXML_CreateDoc(ctx, 1, "admin_access", &srch_xml, &srch_n);
        assert( (srch_xml != NULL) && (srch_n != NULL) );

        tmp_n = xmlNewChild(srch_n, NULL, (xmlChar *) "fieldMapping", NULL);
        xmlNewProp(tmp_n, (xmlChar *) "table", (xmlChar *) "eurephia_adminaccess");

        if( uid != NULL ) {
                xmlNewChild(tmp_n, NULL, (xmlChar *) "uid", (xmlChar *) uid);
        }
        if( username != NULL ) {
                xmlNewChild(tmp_n, NULL, (xmlChar *) "username", (xmlChar *) username);
        }
        if( intf != NULL ) {
                xmlNewChild(tmp_n, NULL, (xmlChar *) "interface", (xmlChar *) intf);
        }
        if( acl != NULL ) {
                xmlNewChild(tmp_n, NULL, (xmlChar *) "accesslevel", (xmlChar *) acl);
        }

        list_xml = eDBadminGetAdminAccess(ctx, srch_xml);
        xmlFreeDoc(srch_xml);
        if( list_xml == NULL ) {
                fprintf(stderr, "%s: Error retrieving user access list\n", MODULE);
                return 1;
        }

        xslt_print_xmldoc(stdout, cfg, list_xml, "adminaccess.xsl", NULL);
        xmlFreeDoc(list_xml);
        return 0;
}


/**
 * Function for granting and revoking user access levels.
 *
 * @param ctx   eurephiaCTX
 * @param sess  eurephiaSESSION of the current logged in user
 * @param cfg   eurephiaVALUES struct of the current configuration
 * @param argc  argument count for the eurephiadm command
 * @param argv  argument table for the eurephiadm command
 *
 * @return returns 0 on success, otherwise 1.
 */
int grant_revoke(eurephiaCTX *ctx, eurephiaSESSION *sess, eurephiaVALUES *cfg, int argc, char **argv) {
        xmlDoc *upd_xml = NULL;
        xmlNode *root_n = NULL, *fmap_n = NULL;
        char actmode = '-', *actmode_str = NULL;
        int i = 0, rc = 0;
        int f_uid = 0, f_acl = 0, f_intf = 0;

        e_options addargs[] = {
                {"--uid", "-i", 1},
                {"--access-level", "-a", 1},
                {"--interface", "-I", 1},
                {"--help", "-h", 0},
                {NULL, NULL, 0}
        };

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

        eurephiaXML_CreateDoc(ctx, 1, "edit_admin_access", &upd_xml, &root_n);
        fmap_n = xmlNewChild(root_n, NULL, (xmlChar *) "fieldMapping", NULL);
        xmlNewProp(fmap_n, (xmlChar *) "table", (xmlChar *) "eurephia_adminaccess");

        if( (strcmp(argv[0], "--grant") == 0) || (strcmp(argv[0], "-G") == 0) ) {
                actmode = 'G';
                actmode_str = "granted";
                xmlNewProp(root_n, (xmlChar *) "mode", (xmlChar *) "grant");
        } else if( (strcmp(argv[0], "--revoke") == 0) || (strcmp(argv[0], "-R") == 0) ) {
                actmode = 'R';
                actmode_str = "revoked";
                xmlNewProp(root_n, (xmlChar *) "mode", (xmlChar *) "revoke");
        }

        for( i = 1; i < argc; i++ ) {
                switch( eurephia_getopt(&i, argc, argv, addargs) ) {
                case 'i':
                        if( f_uid > 0 ) {
                                fprintf(stderr, "%s: User id can only be set once\n", MODULE);
                                return 1;
                        }
                        if( atoi_nullsafe(optargs[0]) < 1 ) {
                                fprintf(stderr, "%s: User ID must be a positive number (>0)\n", MODULE);
                                return 1;
                        }
                        f_uid++;
                        xmlNewChild(fmap_n, NULL, (xmlChar *) "uid", (xmlChar *) optargs[0]);
                        break;

                case 'a':
                        if( f_acl > 0 ) {
                                fprintf(stderr, "%s: Access level can only be set once\n", MODULE);
                                return 1;
                        }
                        f_acl++;
                        xmlNewChild(fmap_n, NULL, (xmlChar *) "accesslevel", (xmlChar *) optargs[0]);
                        break;

                case 'I':
                        if( f_intf > 0 ) {
                                fprintf(stderr, "%s: Access level can only be set once\n", MODULE);
                                return 1;
                        }
                        f_intf++;
                        xmlNewChild(fmap_n, NULL, (xmlChar *) "interface", (xmlChar *) optargs[0]);
                        break;

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

                default:
                        return 1;
               }
        }

        if( (f_uid != 1) || (f_acl != 1) ) {
                fprintf(stderr, "%s: You must provide both a user ID (--uid) "
                        "and an access level (--access-level)\n",
                        MODULE);
                return 1;
        }

        if( f_intf == 0 ) {
                xmlNewChild(fmap_n, NULL, (xmlChar *) "interface", (xmlChar *) "C");
        }

        if( eDBadminEditAdminAccess(ctx, upd_xml) < 1 ) {
                fprintf(stderr, "%s: Failed to update the access level\n", MODULE);
                rc = 1;
        } else {
                printf("%s: Access level %s\n", MODULE, actmode_str);
                printf("\n");
                rc = 0;
        }
        xmlFreeDoc(upd_xml);
        return rc;
}


/**
 * Main function for the the adminaccess command
 *
 * @param ctx   eurephiaCTX
 * @param sess  eurephiaSESSION of the current logged in user
 * @param cfg   eurephiaVALUES struct of the current configuration
 * @param argc  argument count for the eurephiadm command
 * @param argv  argument table for the eurephiadm command
 *
 * @return returns 0 on success, otherwise 1.
 */
int cmd_AdminAccess(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},
                {"--grant", "-G", 0},
                {"--revoke", "-R", 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_adminaccess;
                        break;

                case 'h':
                        mode_fnc = help_AdminAccess2;
                        break;

                case 'G':
                        mode_fnc = grant_revoke;
                        break;

                case 'R':
                        mode_fnc = grant_revoke;
                        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, "%s: Unknown argument.  No mode given\n", MODULE);
                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;
}