attempts.c

Go to the documentation of this file.
00001 /* attempts.c  --  eurephiadm attempts command:
00002  *                 Show/edit records registered in the attempts table
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 
00031 #include <stdio.h>
00032 #include <string.h>
00033 #include <assert.h>
00034 
00035 #ifdef HAVE_LIBXML2
00036 #include <libxml/tree.h>
00037 #endif
00038 
00039 #define MODULE "eurephia::Attempts" 
00040 #include <eurephia_nullsafe.h>
00041 #include <eurephia_context.h>
00042 #include <eurephia_log.h>
00043 #include <eurephia_xml.h>
00044 #include <eurephia_values_struct.h>
00045 #include <eurephiadb_session_struct.h>
00046 #include <eurephiadb_mapping.h>
00047 #include <eurephiadb_driver.h>
00048 #include <eurephia_values.h>
00049 
00050 #include "../argparser.h"
00051 #include "../xsltparser.h"
00052 
00058 void display_attempts_help(int page)
00059 {
00060         switch( page ) {
00061         case 'l':
00062                 printf("The attempts list mode will show all registered login attempts.\n"
00063                        "\n"
00064                        "     -v | --verbose               Show more details\n"
00065                        "\n"
00066                        "Filters:\n"
00067                        "     -u | --username <username>   User name\n"
00068                        "     -d | --digest <SHA1 digest>  Certificate SHA1 digest\n"
00069                        "     -i | --ipaddr <IP address>   IP address\n\n");
00070                 break;
00071 
00072         case 'D':
00073                 printf("The attempts delete mode will remove a record from the attempts log.\n"
00074                        "\n"
00075                        "One of the following parameters must be given (only one):\n"
00076                        "     -u | --username <username>   User name\n"
00077                        "     -d | --digest <SHA1 digest>  Certificate SHA1 digest\n"
00078                        "     -i | --ipaddr <IP address>   IP address\n"
00079                        "     -a | --attemptid <ID>        Attempts record ID\n"
00080                        "\n"
00081                        );
00082                 break;
00083 
00084         case 'R':
00085                 printf("The attempts reset mode will reset the attempt registration.\n"
00086                        "\n"
00087                        "One of the following parameters must be given (only one):\n"
00088                        "     -u | --username <username>   User name\n"
00089                        "     -d | --digest <SHA1 digest>  Certificate SHA1 digest\n"
00090                        "     -i | --ipaddr <IP address>   IP address\n"
00091                        "     -a | --attemptid <ID>        Attempts record ID\n"
00092                        "\n"
00093                        );
00094                 break;
00095 
00096         default:
00097                 printf("Available modes for the attempts command are:\n\n"
00098                        "     -D | --delete       Delete a registered login attempt\n"
00099                        "     -R | --reset        Reset a registered login attempt\n"
00100                        "     -l | --list         List all registered login attempts\n"
00101                        "     -h | --help <mode>  Show help\n\n");
00102                 break;
00103         }
00104 }
00105 
00106 
00110 void help_Attempts()
00111 {
00112         display_attempts_help(0);
00113 }
00114 
00115 
00127 int help_Attempts2(eurephiaCTX *ctx, eurephiaSESSION *sess, eurephiaVALUES *cfg, int argc, char **argv)
00128 {
00129         e_options modeargs[] = {
00130                 {"--list", "-l", 0},
00131                 {"--reset", "-R", 0},
00132                 {"--delete", "-D", 0},
00133                 {NULL, NULL, 0}
00134         };
00135         int i = 1;
00136         display_attempts_help(eurephia_getopt(&i, argc, argv, modeargs));
00137         return 0;
00138 }
00139 
00140 
00152 int list_attempts(eurephiaCTX *ctx, eurephiaSESSION *sess, eurephiaVALUES *cfg, int argc, char **argv)
00153 {
00154         xmlDoc *log_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 modeargs[] = {
00160                 {"--verbose", "-v", 0},
00161                 {"--help", "-h", 0},
00162                 {"--username", "-u", 1},
00163                 {"--digest", "-d", 1},
00164                 {"--ipaddr", "-i", 1},
00165                 {NULL, NULL, 0}
00166         };
00167 
00168         eurephiaXML_CreateDoc(ctx, 1, "attemptslog", &srch_xml, &srch_n);
00169         xmlNewProp(srch_n, (xmlChar *) "mode", (xmlChar *) "list");
00170 
00171         fmap_n = xmlNewChild(srch_n, NULL, (xmlChar *) "fieldMapping", NULL);
00172         xmlNewProp(fmap_n, (xmlChar *) "table", (xmlChar *) "attemptslog");
00173 
00174         for( i = 1; i < argc; i++ ) {
00175                 switch( eurephia_getopt(&i, argc, argv, modeargs) ) {
00176                 case 'v':
00177                         xsltparams[1] = "'details'";
00178                         break;
00179 
00180                 case 'u':
00181                         xmlNewChild(fmap_n, NULL, (xmlChar *) "username", (xmlChar *) optargs[0]);
00182                         break;
00183 
00184                 case 'd':
00185                         xmlNewChild(fmap_n, NULL, (xmlChar *) "digest", (xmlChar *) optargs[0]);
00186                         break;
00187 
00188                 case 'i':
00189                         xmlNewChild(fmap_n, NULL, (xmlChar *) "ip", (xmlChar *) optargs[0]);
00190                         break;
00191 
00192                 case 'h':
00193                         display_attempts_help('l');
00194                         return 0;
00195 
00196                 default:
00197                         return 1;
00198                 }
00199         }
00200 
00201         log_xml = eDBadminAttemptsLog(ctx, srch_xml);
00202         xmlFreeDoc(srch_xml);
00203         if( log_xml == NULL ) {
00204                 fprintf(stderr, "%s: Error retrieving login attempts log.\n", MODULE);
00205                 return 1;
00206         }
00207 
00208         xslt_print_xmldoc(stdout, cfg, log_xml, "attempts.xsl", (const char **) xsltparams);
00209         xmlFreeDoc(log_xml);
00210         return 0;
00211 }
00212 
00213 
00225 int modify_attempts(eurephiaCTX *ctx, eurephiaSESSION *sess, eurephiaVALUES *cfg, int argc, char **argv)
00226 {
00227         xmlDoc *result_xml = NULL, *upd_xml = NULL;
00228         xmlNode *fmap_n = NULL, *upd_n = NULL;
00229         eurephiaRESULT *res = NULL;
00230         int i = 0, rc = 1, mode = 0;
00231 
00232         e_options modeargs[] = {
00233                 {"--help", "-h", 0},
00234                 {"--username", "-u", 1},
00235                 {"--digest", "-d", 1},
00236                 {"--ipaddr", "-i", 1},
00237                 {"--attemptid", "-a", 1},
00238                 {NULL, NULL, 0}
00239         };
00240 
00241         eurephiaXML_CreateDoc(ctx, 1, "attemptslog", &upd_xml, &upd_n);
00242         if( (strcmp(argv[0], "--reset") == 0) || (strcmp(argv[0], "-R") == 0) ) {
00243                 xmlNewProp(upd_n, (xmlChar *) "mode", (xmlChar *) "reset");
00244                 mode = 'R';
00245         } else if( (strcmp(argv[0], "--delete") == 0) || (strcmp(argv[0], "-D") == 0) ) {
00246                 xmlNewProp(upd_n, (xmlChar *) "mode", (xmlChar *) "delete");
00247                 mode = 'D';
00248         } else {
00249                 fprintf(stderr, "%s: Invalid mode\n", MODULE);
00250                 xmlFreeDoc(upd_xml);
00251                 return 1;
00252         }
00253 
00254         fmap_n = xmlNewChild(upd_n, NULL, (xmlChar *) "fieldMapping", NULL);
00255         xmlNewProp(fmap_n, (xmlChar *) "table", (xmlChar *) "attemptslog");
00256 
00257         for( i = 1; i < argc; i++ ) {
00258                 switch( eurephia_getopt(&i, argc, argv, modeargs) ) {
00259                 case 'u':
00260                         xmlNewChild(fmap_n, NULL, (xmlChar *) "username", (xmlChar *) optargs[0]);
00261                         break;
00262 
00263                 case 'd':
00264                         xmlNewChild(fmap_n, NULL, (xmlChar *) "digest", (xmlChar *) optargs[0]);
00265                         break;
00266 
00267                 case 'i':
00268                         xmlNewChild(fmap_n, NULL, (xmlChar *) "ip", (xmlChar *) optargs[0]);
00269                         break;
00270 
00271                 case 'a':
00272                         xmlNewChild(fmap_n, NULL, (xmlChar *) "id", (xmlChar *) optargs[0]);
00273                         break;
00274 
00275                 case 'h':
00276                         display_attempts_help(mode);
00277                         return 0;
00278 
00279                 default:
00280                         return 1;
00281                 }
00282         }
00283 
00284         result_xml = eDBadminAttemptsLog(ctx, upd_xml);
00285         xmlFreeDoc(upd_xml);
00286         if( result_xml == NULL ) {
00287                 fprintf(stderr, "%s: Error during modifying attempts register\n", MODULE);
00288                 return 1;
00289         }
00290 
00291         res = eurephiaXML_ParseResultMsg(ctx, result_xml);
00292         if( res == NULL ) {
00293                 fprintf(stderr, "%s: Error during modifying attempts register. No results returned.\n", MODULE);
00294                 return 1;
00295 
00296         }
00297 
00298         if( res->resultType == exmlERROR ) {
00299                 fprintf(stderr, "%s: %s\n", MODULE, res->message);
00300                 rc = 1;
00301         } else {
00302                 fprintf(stdout, "%s: %s\n", MODULE, res->message);
00303                 rc = 0;
00304         }
00305         free_nullsafe(ctx, res);
00306         xmlFreeDoc(result_xml);
00307 
00308         return rc;
00309 }
00310 
00311 
00323 int cmd_Attempts(eurephiaCTX *ctx, eurephiaSESSION *sess, eurephiaVALUES *cfg, int argc, char **argv)
00324 {
00325         char **mode_argv;
00326         int rc = 0, i = 0, mode_argc = 0;
00327         e_options cmdargs[] = {
00328                 {"--list", "-l", 0},
00329                 {"--reset", "-R", 0},
00330                 {"--delete", "-D", 0},
00331                 {"--help", "-h", 0},
00332                 {NULL, NULL, 0}
00333         };
00334         int (*mode_fnc) (eurephiaCTX *ctx, eurephiaSESSION *sess, eurephiaVALUES *cfg, int argc, char **argv);
00335 
00336         assert((ctx != NULL) && (ctx->dbc != NULL));
00337 
00338         mode_fnc = NULL;
00339         for( i = 1; i < argc; i++ ) {
00340                 switch( eurephia_getopt(&i, argc, argv, cmdargs) ) {
00341                 case 'l':
00342                         mode_fnc = list_attempts;
00343                         break;
00344 
00345                 case 'R':
00346                 case 'D':
00347                         mode_fnc = modify_attempts;
00348                         break;
00349 
00350                 case 'h':
00351                         mode_fnc = help_Attempts2;
00352 
00353                 default:
00354                         break;
00355                 }
00356                 if( mode_fnc != NULL ) {
00357                         break;
00358                 }
00359         }
00360 
00361         // If we do not have any known mode defined, exit with error
00362         if( mode_fnc == NULL )  {
00363                 fprintf(stderr, "%s: Unknown argument.  No mode given\n", MODULE);
00364                 return 1;
00365         }
00366 
00367         // Allocate memory for our arguments being sent to the mode function
00368         mode_argv = (char **) calloc(sizeof(char *), (argc - i)+2);
00369         assert(mode_argv != NULL);
00370 
00371         // Copy over only the arguments needed for the mode
00372         mode_argc = eurephia_arraycp(i, argc, argv, mode_argv, (argc - i));
00373 
00374         // Call the mode function
00375         rc = mode_fnc(ctx, sess, cfg, mode_argc, mode_argv);
00376         free_nullsafe(ctx, mode_argv);
00377 
00378         return rc;
00379 
00380 }
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines