fwprofiles.c

Go to the documentation of this file.
00001 /* fwprofiles.c  --  eurephiadm fwprofiles command:
00002  *                   Manages firewall profiles
00003  *
00004  *  GPLv2 only - Copyright (C) 2009 - 2010
00005  *               David Sommerseth <dazo@users.sourceforge.net>
00006  *
00007  *  This program is free software; you can redistribute it and/or
00008  *  modify it under the terms of the GNU General Public License
00009  *  as published by the Free Software Foundation; version 2
00010  *  of the License.
00011  *
00012  *  This program is distributed in the hope that it will be useful,
00013  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  *  GNU General Public License for more details.
00016  *
00017  *  You should have received a copy of the GNU General Public License
00018  *  along with this program; if not, write to the Free Software
00019  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00020  *
00021  */
00022 
00032 #include <stdio.h>
00033 #include <string.h>
00034 #include <assert.h>
00035 
00036 #ifdef HAVE_LIBXML2
00037 #include <libxml/tree.h>
00038 #endif
00039 
00040 #define MODULE "eurephia::fwProfiles" 
00041 #include <eurephia_nullsafe.h>
00042 #include <eurephia_context.h>
00043 #include <eurephia_log.h>
00044 #include <eurephia_xml.h>
00045 #include <eurephia_values_struct.h>
00046 #include <eurephiadb_session_struct.h>
00047 #include <eurephiadb_mapping.h>
00048 #include <eurephiadb_driver.h>
00049 #include <eurephia_values.h>
00050 
00051 #include "../argparser.h"
00052 #include "../xsltparser.h"
00053 
00054 
00060 void display_fwprofiles_help(int page)
00061 {
00062         switch( page ) {
00063         case 'l':
00064                 printf("The fwprofiles list mode will show all registered firewall profiles.\n"
00065                        "\n"
00066                        "     -v | --verbose               Show more details\n"
00067                        "\n"
00068                        "Filters:\n"
00069                        "     -a | --accessprofile <id>    Numeric ID.\n"
00070                        "     -f | --fw-destination <name> Reference used by the firewall\n"
00071                        "     -i | --uid <user id>         Numeric user ID\n"
00072                        "     -n | --username <username>   User name\n"
00073                        "     -c | --certid <certid>       Numeric reference to a certificate\n"
00074                        "     -e | --email <email addr>    e-mail address in certificates\n"
00075                        "     -d | --digest <SHA1 digest>  Certificate SHA1 digest\n\n");
00076                 break;
00077 
00078         case 'A':
00079                 printf("The fwprofiles add mode will register a new firewall profile.\n"
00080                        "\n"
00081                        "     -d | --description    <text> Description of the firewall destination/rule\n"
00082                        "     -f | --fw-destination <name> The reference used by the firewall module\n"
00083                        "\n"
00084                        );
00085                 break;
00086 
00087         case 'D':
00088                 printf("The fwprofiles delete mode will delete a firewall profile.\n"
00089                        "\n"
00090                        "     -a | --accessprofile  <id>   Description of the firewall destination/rule\n"
00091                        "     -f | --fw-destination <name> The reference used by the firewall module\n"
00092                        "\n"
00093                        );
00094                 break;
00095 
00096         default:
00097                 printf("Available modes for the fwprofiles command are:\n\n"
00098                        "     -A | --add          Add a new firewall profile\n"
00099                        "     -D | --delete       Delete a firewall profile\n"
00100                        "     -l | --list         List available firewall profiles\n"
00101                        "     -h | --help <mode>  Show help\n\n");
00102                 break;
00103         }
00104 }
00105 
00106 
00110 void help_fwProfiles()
00111 {
00112         display_fwprofiles_help(0);
00113 }
00114 
00115 
00127 int help_fwProfiles2(eurephiaCTX *ctx, eurephiaSESSION *sess, eurephiaVALUES *cfg, int argc, char **argv)
00128 {
00129         e_options fwprofilesargs[] = {
00130                 {"--list", "-l", 0},
00131                 {"--add", "-A", 0},
00132                 {"--delete", "-D", 0},
00133                 {NULL, NULL, 0}
00134         };
00135         int i = 1;
00136         display_fwprofiles_help(eurephia_getopt(&i, argc, argv, fwprofilesargs));
00137         return 0;
00138 }
00139 
00140 
00152 int list_profiles(eurephiaCTX *ctx, eurephiaSESSION *sess, eurephiaVALUES *cfg, int argc, char **argv)
00153 {
00154         xmlDoc *profiles_xml = NULL, *srch_xml = NULL;
00155         xmlNode *fmap_n = NULL, *srch_n = NULL;
00156         char *xsltparams[] = {"view", "'list'", NULL};
00157         int i = 0;
00158 
00159         e_options fwprofilesargs[] = {
00160                 {"--verbose", "-v", 0},
00161                 {"--help", "-h", 0},
00162                 {"--accessprofile", "-a", 1},
00163                 {"--fw-destination", "-f", 1},
00164                 {"--uid", "-i", 1},
00165                 {"--username", "-u", 1},
00166                 {"--certid", "-c", 1},
00167                 {"--email", "-e", 1},
00168                 {"--digest", "-d", 1},
00169                 {NULL, NULL, 0}
00170         };
00171 
00172         eurephiaXML_CreateDoc(ctx, 1, "firewall_profiles", &srch_xml, &srch_n);
00173         xmlNewProp(srch_n, (xmlChar *) "mode", (xmlChar *) "search");
00174 
00175         fmap_n = xmlNewChild(srch_n, NULL, (xmlChar *) "fieldMapping", NULL);
00176         xmlNewProp(fmap_n, (xmlChar *) "table", (xmlChar *) "firewall_profiles");
00177 
00178         for( i = 1; i < argc; i++ ) {
00179                 switch( eurephia_getopt(&i, argc, argv, fwprofilesargs) ) {
00180                 case 'v':
00181                         xsltparams[1] = "'details'";
00182                         break;
00183 
00184                 case 'a':
00185                         xmlNewChild(fmap_n, NULL, (xmlChar *) "accessprofile", (xmlChar *) optargs[0]);
00186                         break;
00187 
00188                 case 'f':
00189                         xmlNewChild(fmap_n, NULL, (xmlChar *) "fwprofile", (xmlChar *) optargs[0]);
00190                         break;
00191 
00192                 case 'i':
00193                         xmlNewChild(fmap_n, NULL, (xmlChar *) "uid", (xmlChar *) optargs[0]);
00194                         break;
00195 
00196                 case 'u':
00197                         xmlNewChild(fmap_n, NULL, (xmlChar *) "username", (xmlChar *) optargs[0]);
00198                         break;
00199 
00200                 case 'c':
00201                         xmlNewChild(fmap_n, NULL, (xmlChar *) "certid", (xmlChar *) optargs[0]);
00202                         break;
00203 
00204                 case 'e':
00205                         xmlNewChild(fmap_n, NULL, (xmlChar *) "email", (xmlChar *) optargs[0]);
00206                         break;
00207 
00208                 case 'd':
00209                         xmlNewChild(fmap_n, NULL, (xmlChar *) "digest", (xmlChar *) optargs[0]);
00210                         break;
00211 
00212                 case 'h':
00213                         display_fwprofiles_help('l');
00214                         return 0;
00215 
00216                 default:
00217                         return 1;
00218                 }
00219         }
00220 
00221         profiles_xml = eDBadminFirewallProfiles(ctx, srch_xml);
00222         xmlFreeDoc(srch_xml);
00223         if( profiles_xml == NULL ) {
00224                 fprintf(stderr, "%s: Error retrieving firewall profiles\n", MODULE);
00225                 return 1;
00226         }
00227 
00228         xslt_print_xmldoc(stdout, cfg, profiles_xml, "fwadmin.xsl", (const char **) xsltparams);
00229 
00230         xmlFreeDoc(profiles_xml);
00231         return 0;
00232 }
00233 
00234 
00246 int addelete_profile(eurephiaCTX *ctx, eurephiaSESSION *sess, eurephiaVALUES *cfg, int argc, char **argv)
00247 {
00248         xmlDoc *result_xml = NULL, *srch_xml = NULL;
00249         xmlNode *fmap_n = NULL, *srch_n = NULL;
00250         eurephiaRESULT *res = NULL;
00251         int i = 0, rc = 1, mode = 0;
00252 
00253         e_options addargs[] = {
00254                         {"--help", "-h", 0},
00255                         {"--description", "-d", 1},
00256                         {"--fw-destination", "-f", 1},
00257                         {NULL, NULL, 0}
00258         };
00259 
00260         e_options deleteargs[] = {
00261                         {"--help", "-h", 0},
00262                         {"--accessprofile", "-a", 1},
00263                         {"--fw-destination", "-f", 1},
00264                         {NULL, NULL, 0}
00265         };
00266 
00267 
00268         e_options *fwprofilesargs = NULL;
00269 
00270         eurephiaXML_CreateDoc(ctx, 1, "firewall_profiles", &srch_xml, &srch_n);
00271         if( (strcmp(argv[0], "--add") == 0) || (strcmp(argv[0], "-A") == 0) ) {
00272                 xmlNewProp(srch_n, (xmlChar *) "mode", (xmlChar *) "add");
00273                 fwprofilesargs = addargs;
00274                 mode = 'A';
00275         } else if( (strcmp(argv[0], "--delete") == 0) || (strcmp(argv[0], "-D") == 0) ) {
00276                 xmlNewProp(srch_n, (xmlChar *) "mode", (xmlChar *) "delete");
00277                 fwprofilesargs = deleteargs;
00278                 mode = 'D';
00279         } else {
00280                 fprintf(stderr, "%s: Invalid mode\n", MODULE);
00281                 xmlFreeDoc(srch_xml);
00282                 return 1;
00283         }
00284 
00285         fmap_n = xmlNewChild(srch_n, NULL, (xmlChar *) "fieldMapping", NULL);
00286         xmlNewProp(fmap_n, (xmlChar *) "table", (xmlChar *) "firewall_profiles");
00287 
00288         for( i = 1; i < argc; i++ ) {
00289                 switch( eurephia_getopt(&i, argc, argv, fwprofilesargs) ) {
00290                 case 'a':
00291                         xmlNewChild(fmap_n, NULL, (xmlChar *) "accessprofile", (xmlChar *) optargs[0]);
00292                         break;
00293 
00294                 case 'd':
00295                         xmlNewChild(fmap_n, NULL, (xmlChar *) "description", (xmlChar *) optargs[0]);
00296                         break;
00297 
00298                 case 'f':
00299                         xmlNewChild(fmap_n, NULL, (xmlChar *) "fwprofile", (xmlChar *) optargs[0]);
00300                         break;
00301 
00302                 case 'h':
00303                         display_fwprofiles_help(mode);
00304                         return 0;
00305 
00306                 default:
00307                         return 1;
00308                 }
00309         }
00310 
00311         result_xml = eDBadminFirewallProfiles(ctx, srch_xml);
00312         xmlFreeDoc(srch_xml);
00313         if( result_xml == NULL ) {
00314                 fprintf(stderr, "%s: Error registering firewall profiles\n", MODULE);
00315                 return 1;
00316         }
00317 
00318         res = eurephiaXML_ParseResultMsg(ctx, result_xml);
00319         if( res == NULL ) {
00320                 fprintf(stderr, "%s: Error registering firewall profiles. No results returned\n", MODULE);
00321                 return 1;
00322 
00323         } else {
00324                 if( res->resultType == exmlERROR ) {
00325                         fprintf(stderr, "%s: %s\n", MODULE, res->message);
00326                         rc = 1;
00327                 } else {
00328                         fprintf(stdout, "%s: %s\n", MODULE, res->message);
00329                         rc = 0;
00330                 }
00331         }
00332         free_nullsafe(ctx, res);
00333         xmlFreeDoc(result_xml);
00334 
00335         return rc;
00336 }
00337 
00338 
00350 int cmd_fwProfiles(eurephiaCTX *ctx, eurephiaSESSION *sess, eurephiaVALUES *cfg, int argc, char **argv)
00351 {
00352         char **mode_argv;
00353         int rc = 0, i = 0, mode_argc = 0;
00354         e_options fwprofilesargs[] = {
00355                 {"--list", "-l", 0},
00356                 {"--add", "-A", 0},
00357                 {"--delete", "-D", 0},
00358                 {"--help", "-h", 0},
00359                 {NULL, NULL, 0}
00360         };
00361         int (*mode_fnc) (eurephiaCTX *ctx, eurephiaSESSION *sess, eurephiaVALUES *cfg, int argc, char **argv);
00362 
00363         assert((ctx != NULL) && (ctx->dbc != NULL));
00364 
00365         mode_fnc = NULL;
00366         for( i = 1; i < argc; i++ ) {
00367                 switch( eurephia_getopt(&i, argc, argv, fwprofilesargs) ) {
00368                 case 'l':
00369                         mode_fnc = list_profiles;
00370                         break;
00371 
00372                 case 'A':
00373                 case 'D':
00374                         mode_fnc = addelete_profile;
00375                         break;
00376 
00377                 case 'h':
00378                         mode_fnc = help_fwProfiles2;
00379 
00380                 default:
00381                         break;
00382                 }
00383                 if( mode_fnc != NULL ) {
00384                         break;
00385                 }
00386         }
00387 
00388         // If we do not have any known mode defined, exit with error
00389         if( mode_fnc == NULL )  {
00390                 fprintf(stderr, "%s: Unknown argument.  No mode given\n", MODULE);
00391                 return 1;
00392         }
00393 
00394         // Allocate memory for our arguments being sent to the mode function
00395         mode_argv = (char **) calloc(sizeof(char *), (argc - i)+2);
00396         assert(mode_argv != NULL);
00397 
00398         // Copy over only the arguments needed for the mode
00399         mode_argc = eurephia_arraycp(i, argc, argv, mode_argv, (argc - i));
00400 
00401         // Call the mode function
00402         rc = mode_fnc(ctx, sess, cfg, mode_argc, mode_argv);
00403         free_nullsafe(ctx, mode_argv);
00404 
00405         return rc;
00406 
00407 }
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines