summaryrefslogtreecommitdiffstats
path: root/eurephiadm/commands/fwprofiles.c
blob: b62ee08f111003433eb3d869b8b05f8b0a7b3d83 (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
/* fwprofiles.c  --  eurephiadm fwprofiles command:
 *                   Manages firewall profiles
 *
 *  GPLv2 only - Copyright (C) 2009 - 2010
 *               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   fwprofiles.c
 * @author David Sommerseth <dazo@users.sourceforge.net>
 * @date   2009-03-29
 *
 * @brief  eurephia fwprofiles command, to manage firewall profiles.
 *
 */

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

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

#define MODULE "eurephia::fwProfiles" /**< Needed 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 <eurephia_values.h>

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


/**
 * Help screens for the fwprofiles command
 *
 * @param page which help screen to display
 */
void display_fwprofiles_help(int page)
{
        switch( page ) {
        case 'l':
                printf("The fwprofiles list mode will show all registered firewall profiles.\n"
                       "\n"
                       "     -v | --verbose               Show more details\n"
                       "\n"
                       "Filters:\n"
                       "     -a | --accessprofile <id>    Numeric ID.\n"
                       "     -f | --fw-destination <name> Reference used by the firewall\n"
                       "     -i | --uid <user id>         Numeric user ID\n"
                       "     -n | --username <username>   User name\n"
                       "     -c | --certid <certid>       Numeric reference to a certificate\n"
                       "     -e | --email <email addr>    e-mail address in certificates\n"
                       "     -d | --digest <SHA1 digest>  Certificate SHA1 digest\n\n");
                break;

        case 'A':
                printf("The fwprofiles add mode will register a new firewall profile.\n"
                       "\n"
                       "     -d | --description    <text> Description of the firewall destination/rule\n"
                       "     -f | --fw-destination <name> The reference used by the firewall module\n"
                       "\n"
                       );
                break;

        case 'D':
                printf("The fwprofiles delete mode will delete a firewall profile.\n"
                       "\n"
                       "     -a | --accessprofile  <id>   Description of the firewall destination/rule\n"
                       "     -f | --fw-destination <name> The reference used by the firewall module\n"
                       "\n"
                       );
                break;

        default:
                printf("Available modes for the fwprofiles command are:\n\n"
                       "     -A | --add          Add a new firewall profile\n"
                       "     -D | --delete       Delete a firewall profile\n"
                       "     -l | --list         List available firewall profiles\n"
                       "     -h | --help <mode>  Show help\n\n");
                break;
        }
}


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


/**
 * Help screen wrapper for the fwprofile 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_fwProfiles2(eurephiaCTX *ctx, eurephiaSESSION *sess, eurephiaVALUES *cfg, int argc, char **argv)
{
        e_options fwprofilesargs[] = {
                {"--list", "-l", 0},
                {"--add", "-A", 0},
                {"--delete", "-D", 0},
                {NULL, NULL, 0}
        };
        int i = 1;
        display_fwprofiles_help(eurephia_getopt(&i, argc, argv, fwprofilesargs));
        return 0;
}


/**
 * fwprofiles list mode.  Lists all registered firewall profiles.
 *
 * @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_profiles(eurephiaCTX *ctx, eurephiaSESSION *sess, eurephiaVALUES *cfg, int argc, char **argv)
{
        xmlDoc *profiles_xml = NULL, *srch_xml = NULL;
        xmlNode *fmap_n = NULL, *srch_n = NULL;
        char *xsltparams[] = {"view", "'list'", NULL};
        int i = 0;

        e_options fwprofilesargs[] = {
                {"--verbose", "-v", 0},
                {"--help", "-h", 0},
                {"--accessprofile", "-a", 1},
                {"--fw-destination", "-f", 1},
                {"--uid", "-i", 1},
                {"--username", "-u", 1},
                {"--certid", "-c", 1},
                {"--email", "-e", 1},
                {"--digest", "-d", 1},
                {NULL, NULL, 0}
        };

        eurephiaXML_CreateDoc(ctx, 1, "firewall_profiles", &srch_xml, &srch_n);
        xmlNewProp(srch_n, (xmlChar *) "mode", (xmlChar *) "search");

        fmap_n = xmlNewChild(srch_n, NULL, (xmlChar *) "fieldMapping", NULL);
        xmlNewProp(fmap_n, (xmlChar *) "table", (xmlChar *) "firewall_profiles");

        for( i = 1; i < argc; i++ ) {
                switch( eurephia_getopt(&i, argc, argv, fwprofilesargs) ) {
                case 'v':
                        xsltparams[1] = "'details'";
                        break;

                case 'a':
                        xmlNewChild(fmap_n, NULL, (xmlChar *) "accessprofile", (xmlChar *) optargs[0]);
                        break;

                case 'f':
                        xmlNewChild(fmap_n, NULL, (xmlChar *) "fwprofile", (xmlChar *) optargs[0]);
                        break;

                case 'i':
                        xmlNewChild(fmap_n, NULL, (xmlChar *) "uid", (xmlChar *) optargs[0]);
                        break;

                case 'u':
                        xmlNewChild(fmap_n, NULL, (xmlChar *) "username", (xmlChar *) optargs[0]);
                        break;

                case 'c':
                        xmlNewChild(fmap_n, NULL, (xmlChar *) "certid", (xmlChar *) optargs[0]);
                        break;

                case 'e':
                        xmlNewChild(fmap_n, NULL, (xmlChar *) "email", (xmlChar *) optargs[0]);
                        break;

                case 'd':
                        xmlNewChild(fmap_n, NULL, (xmlChar *) "digest", (xmlChar *) optargs[0]);
                        break;

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

                default:
                        return 1;
                }
        }

        profiles_xml = eDBadminFirewallProfiles(ctx, srch_xml);
        xmlFreeDoc(srch_xml);
        if( profiles_xml == NULL ) {
                fprintf(stderr, "%s: Error retrieving firewall profiles\n", MODULE);
                return 1;
        }

        xslt_print_xmldoc(stdout, cfg, profiles_xml, "fwadmin.xsl", (const char **) xsltparams);

        xmlFreeDoc(profiles_xml);
        return 0;
}


/**
 * fwprofile add/delete mode.  Registers or removes firewall profiles.
 *
 * @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 addelete_profile(eurephiaCTX *ctx, eurephiaSESSION *sess, eurephiaVALUES *cfg, int argc, char **argv)
{
        xmlDoc *result_xml = NULL, *srch_xml = NULL;
        xmlNode *fmap_n = NULL, *srch_n = NULL;
        eurephiaRESULT *res = NULL;
        int i = 0, rc = 1, mode = 0;

        e_options addargs[] = {
                        {"--help", "-h", 0},
                        {"--description", "-d", 1},
                        {"--fw-destination", "-f", 1},
                        {NULL, NULL, 0}
        };

        e_options deleteargs[] = {
                        {"--help", "-h", 0},
                        {"--accessprofile", "-a", 1},
                        {"--fw-destination", "-f", 1},
                        {NULL, NULL, 0}
        };


        e_options *fwprofilesargs = NULL;

        eurephiaXML_CreateDoc(ctx, 1, "firewall_profiles", &srch_xml, &srch_n);
        if( (strcmp(argv[0], "--add") == 0) || (strcmp(argv[0], "-A") == 0) ) {
                xmlNewProp(srch_n, (xmlChar *) "mode", (xmlChar *) "add");
                fwprofilesargs = addargs;
                mode = 'A';
        } else if( (strcmp(argv[0], "--delete") == 0) || (strcmp(argv[0], "-D") == 0) ) {
                xmlNewProp(srch_n, (xmlChar *) "mode", (xmlChar *) "delete");
                fwprofilesargs = deleteargs;
                mode = 'D';
        } else {
                fprintf(stderr, "%s: Invalid mode\n", MODULE);
                xmlFreeDoc(srch_xml);
                return 1;
        }

        fmap_n = xmlNewChild(srch_n, NULL, (xmlChar *) "fieldMapping", NULL);
        xmlNewProp(fmap_n, (xmlChar *) "table", (xmlChar *) "firewall_profiles");

        for( i = 1; i < argc; i++ ) {
                switch( eurephia_getopt(&i, argc, argv, fwprofilesargs) ) {
                case 'a':
                        xmlNewChild(fmap_n, NULL, (xmlChar *) "accessprofile", (xmlChar *) optargs[0]);
                        break;

                case 'd':
                        xmlNewChild(fmap_n, NULL, (xmlChar *) "description", (xmlChar *) optargs[0]);
                        break;

                case 'f':
                        xmlNewChild(fmap_n, NULL, (xmlChar *) "fwprofile", (xmlChar *) optargs[0]);
                        break;

                case 'h':
                        display_fwprofiles_help(mode);
                        return 0;

                default:
                        return 1;
                }
        }

        result_xml = eDBadminFirewallProfiles(ctx, srch_xml);
        xmlFreeDoc(srch_xml);
        if( result_xml == NULL ) {
                fprintf(stderr, "%s: Error registering firewall profiles\n", MODULE);
                return 1;
        }

        res = eurephiaXML_ParseResultMsg(ctx, result_xml);
        if( res == NULL ) {
                fprintf(stderr, "%s: Error registering firewall profiles. No results returned\n", MODULE);
                return 1;

        } else {
                if( res->resultType == exmlERROR ) {
                        fprintf(stderr, "%s: %s\n", MODULE, res->message);
                        rc = 1;
                } else {
                        fprintf(stdout, "%s: %s\n", MODULE, res->message);
                        rc = 0;
                }
        }
        free_nullsafe(ctx, res);
        xmlFreeDoc(result_xml);

        return rc;
}


/**
 * Main function for the fwprofiles 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_fwProfiles(eurephiaCTX *ctx, eurephiaSESSION *sess, eurephiaVALUES *cfg, int argc, char **argv)
{
        char **mode_argv;
        int rc = 0, i = 0, mode_argc = 0;
        e_options fwprofilesargs[] = {
                {"--list", "-l", 0},
                {"--add", "-A", 0},
                {"--delete", "-D", 0},
                {"--help", "-h", 0},
                {NULL, NULL, 0}
        };
        int (*mode_fnc) (eurephiaCTX *ctx, eurephiaSESSION *sess, eurephiaVALUES *cfg, int argc, char **argv);

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

        mode_fnc = NULL;
        for( i = 1; i < argc; i++ ) {
                switch( eurephia_getopt(&i, argc, argv, fwprofilesargs) ) {
                case 'l':
                        mode_fnc = list_profiles;
                        break;

                case 'A':
                case 'D':
                        mode_fnc = addelete_profile;
                        break;

                case 'h':
                        mode_fnc = help_fwProfiles2;

                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(ctx, mode_argv);

        return rc;

}