From 4adc232cdc3f5aad1a2be38d8875494561f813cb Mon Sep 17 00:00:00 2001 From: Nikos Mavrogiannopoulos Date: Sun, 3 Apr 2011 10:56:36 +0200 Subject: crypt.* renamed to srptool.*. --- src/Makefile.am | 8 +- src/crypt-gaa.c | 910 ------------------------------------------------------ src/crypt-gaa.h | 47 --- src/crypt.c | 731 ------------------------------------------- src/crypt.gaa | 45 --- src/srptool-gaa.c | 910 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/srptool-gaa.h | 47 +++ src/srptool.c | 731 +++++++++++++++++++++++++++++++++++++++++++ src/srptool.gaa | 45 +++ 9 files changed, 1737 insertions(+), 1737 deletions(-) delete mode 100644 src/crypt-gaa.c delete mode 100644 src/crypt-gaa.h delete mode 100644 src/crypt.c delete mode 100644 src/crypt.gaa create mode 100644 src/srptool-gaa.c create mode 100644 src/srptool-gaa.h create mode 100644 src/srptool.c create mode 100644 src/srptool.gaa diff --git a/src/Makefile.am b/src/Makefile.am index 9460985dd0..e1d9b4cae1 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -53,11 +53,11 @@ libcmd_serv_la_CFLAGS = libcmd_serv_la_SOURCES = serv.gaa serv-gaa.h serv-gaa.c if ENABLE_SRP -srptool_SOURCES = crypt.c +srptool_SOURCES = srptool.c srptool_LDADD = ../lib/libgnutls.la libcmd-srp.la ../gl/libgnu.la noinst_LTLIBRARIES += libcmd-srp.la libcmd_srp_la_CFLAGS = -libcmd_srp_la_SOURCES = crypt.gaa crypt-gaa.h crypt-gaa.c +libcmd_srp_la_SOURCES = srptool.gaa srptool-gaa.h srptool-gaa.c endif psktool_SOURCES = psk.c @@ -133,8 +133,8 @@ libcmd_p11tool_la_LIBADD += ../gl/libgnu.la $(INET_PTON_LIB) psk-gaa.c: $(srcdir)/psk.gaa -$(GAA) $< -o psk-gaa.c -i psk-gaa.h -crypt-gaa.c: $(srcdir)/crypt.gaa - -$(GAA) $< -o crypt-gaa.c -i crypt-gaa.h +srptool-gaa.c: $(srcdir)/srptool.gaa + -$(GAA) $< -o srptool-gaa.c -i srptool-gaa.h p11tool-gaa.c: $(srcdir)/p11tool.gaa -$(GAA) $< -o p11tool-gaa.c -i p11tool-gaa.h certtool-gaa.c: $(srcdir)/certtool.gaa diff --git a/src/crypt-gaa.c b/src/crypt-gaa.c deleted file mode 100644 index f83cf9d274..0000000000 --- a/src/crypt-gaa.c +++ /dev/null @@ -1,910 +0,0 @@ -/* File generated by GAA 1.6.6 - */ -#define GAA_NO_WIN32 -#line 1 "crypt.gaa" - - -/* C declarations */ - -#include -#ifdef _WIN32 -# include -#endif - -void srptool_version(void); - -#include -#include -#include - -#ifndef GAA_NO_WIN32 -#if defined(_WIN32) || defined(__WIN32__) || defined(WIN32) || defined(WINDOWS) -#define GAA_WIN32 -#endif -#endif - -static void* gaa_malloc( size_t size) { -void* ret; - ret = malloc(size); - if (ret==NULL) { - fprintf(stderr, "gaa: could not allocate memory"); - exit(1); - } - return ret; -} - -static void __gaa_helpsingle(char short_name, char *name, - char *arg_desc, char *opt_help) -{ - int col1, col3, col4, tabsize = 3, curr; - int i; - - col1 = 5; /* Default values */ - col3 = 30; - col4 = 70; - - curr = 0; - for(i = 0; i < col1; i++) - { - printf(" "); - curr++; - } - if(short_name) - { - if(name && *name) - { - printf("-%c, ", short_name); - curr += 4; - } - else - { - printf("-%c ", short_name); - curr += 3; - } - } - if(name && *name) - { - printf("--%s ", name); - curr += 3 + strlen(name); - } - if(arg_desc && *arg_desc) - { - printf("%s ", arg_desc); - curr += 1 + strlen(arg_desc); - } - if(curr >= col3) - { - printf("\n"); - curr = 0; - } - if(opt_help) /* let's print the option's help body */ - { - const char *str = opt_help; - while(*str) - { - while(curr < col3) - { - printf(" "); - curr++; - } - switch(*str) - { - case '\n': - printf("\n"); - curr = 0; - break; - case '\t': - do - { - printf(" "); - curr++; - } - while((curr - col3) % tabsize != 0 && curr < col4); - case ' ': - if(*str == ' ') - { - curr++; - printf(" "); - } - for(i = 1; str[i] && str[i] != ' ' && str[i] != '\n' - && str[i] != '\t'; i++); - if(curr + i - 1 >= col4) - curr = col4; - break; - default: - printf("%c", *str); - curr++; - } - if(curr >= col4) - { - printf("\n"); - curr = 0; - } - str++; - } - } - printf("\n"); -} - -void gaa_help(void) -{ - printf("Srptool help\nUsage : srptool [options]\n"); - __gaa_helpsingle('d', "debug", "integer ", "Enable debugging"); - __gaa_helpsingle('u', "username", "username ", "specify username."); - __gaa_helpsingle('p', "passwd", "FILE ", "specify a password file."); - __gaa_helpsingle('i', "index", "INDEX ", "specify the index of the group parameters in tpasswd.conf to use."); - __gaa_helpsingle('s', "salt", "SALT ", "specify salt size for crypt algorithm."); - __gaa_helpsingle(0, "verify", "", "just verify password."); - __gaa_helpsingle('c', "passwd-conf", "FILE ", "specify a password conf file."); - __gaa_helpsingle(0, "create-conf", "FILE ", "Generate a tpasswd.conf file."); - __gaa_helpsingle('v', "version", "", "prints the program's version number"); - __gaa_helpsingle('h', "help", "", "shows this help text"); - -#line 100 "gaa.skel" -} -/* Copy of C area */ - -#line 104 "gaa.skel" -/* GAA HEADER */ -#ifndef GAA_HEADER_POKY -#define GAA_HEADER_POKY - -typedef struct _gaainfo gaainfo; - -struct _gaainfo -{ -#line 37 "crypt.gaa" - char *create_conf; -#line 34 "crypt.gaa" - char *passwd_conf; -#line 31 "crypt.gaa" - int verify; -#line 28 "crypt.gaa" - int salt; -#line 25 "crypt.gaa" - int index; -#line 22 "crypt.gaa" - char *passwd; -#line 19 "crypt.gaa" - char *username; -#line 16 "crypt.gaa" - int debug; - -#line 114 "gaa.skel" -}; - -#ifdef __cplusplus -extern "C" -{ -#endif - - int gaa(int argc, char *argv[], gaainfo *gaaval); - - void gaa_help(void); - - int gaa_file(const char *name, gaainfo *gaaval); - -#ifdef __cplusplus -} -#endif - - -#endif - -#line 135 "gaa.skel" - -/* C declarations */ - -#define GAAERROR(x) \ -{ \ -gaa_error = 1; \ -return x; \ -} - -static char *gaa_current_option; -static int gaa_error = 0; - -/* Generated by gaa */ - -#include -#include - - -#define GAA_OK -1 - -#define GAA_ERROR_NOMATCH 0 -#define GAA_ERROR_NOTENOUGH_ARGS 1 -#define GAA_ERROR_INVALID_ARG 2 -#define GAA_ERROR_UNKNOWN 3 - -#define GAA_NOT_AN_OPTION 0 -#define GAA_WORD_OPTION 1 -#define GAA_LETTER_OPTION 2 -#define GAA_MULTIPLE_OPTION 3 - -#define GAA_REST 0 -#define GAA_NB_OPTION 10 -#define GAAOPTID_help 1 -#define GAAOPTID_version 2 -#define GAAOPTID_create_conf 3 -#define GAAOPTID_passwd_conf 4 -#define GAAOPTID_verify 5 -#define GAAOPTID_salt 6 -#define GAAOPTID_index 7 -#define GAAOPTID_passwd 8 -#define GAAOPTID_username 9 -#define GAAOPTID_debug 10 - -#line 168 "gaa.skel" - -#define GAA_CHECK1STR(a,b) \ -if(a[0] == str[0]) \ -{ \ - gaa_current_option = a; \ - return b; \ -} - -#define GAA_CHECKSTR(a,b) \ -if(strcmp(a,str) == 0) \ -{ \ - gaa_current_option = a; \ - return b; \ -} - -#define GAA_TESTMOREARGS \ -if(!OK) \ -{ \ -while((gaa_last_non_option != gaa_index) && (gaa_arg_used[gaa_index] == 1)) \ - gaa_index++; \ -if(gaa_last_non_option == gaa_index) \ - return GAA_ERROR_NOTENOUGH_ARGS; \ -} - -#define GAA_TESTMOREOPTIONALARGS \ -if(!OK) \ -{ \ -while((gaa_last_non_option != gaa_index) && (gaa_arg_used[gaa_index] == 1)) \ - gaa_index++; \ -if(gaa_last_non_option == gaa_index) \ - OK = 1; \ -} - -#define GAA_FILL_2ARGS(target, func) \ -target = func(GAAargv[gaa_index]); \ -gaa_arg_used[gaa_index] = 1; \ -if(gaa_error == 1) \ -{ \ - gaa_error = 0; \ - return GAA_ERROR_INVALID_ARG; \ -} - - - -#define GAA_FILL(target, func, num) \ -if(!OK) \ -{ \ -target = func(GAAargv[gaa_index]); \ -gaa_arg_used[gaa_index] = 1; \ -if(gaa_error == 1) \ -{ \ - gaa_error = 0; \ - return GAA_ERROR_INVALID_ARG; \ -} \ -num = 1; \ -} \ -else \ -{ \ -num = 0; \ -} - -#define GAA_LIST_FILL(target, func, type ,num) \ -if(!OK) \ -{ \ -num = 0; \ -target = NULL; \ -if ( gaa_last_non_option - gaa_index > 0) \ - target = gaa_malloc((gaa_last_non_option - gaa_index) * sizeof(type));\ -for(; gaa_index < gaa_last_non_option; gaa_index++) \ -{ \ - if(gaa_arg_used[gaa_index] == 0) \ - { \ - GAA_FILL_2ARGS(target[num], func); \ - num++; \ - } \ -} \ -if(num == 0) \ - return GAA_ERROR_NOTENOUGH_ARGS; \ -} - -#define GAA_OPTIONALLIST_FILL(target, func, type ,num) \ -if(!OK) \ -{ \ -num = 0; \ -target = NULL; \ -if ( gaa_last_non_option - gaa_index > 0) \ - target = gaa_malloc((gaa_last_non_option - gaa_index) * sizeof(type));\ -for(; gaa_index < gaa_last_non_option; gaa_index++) \ -{ \ - if(gaa_arg_used[gaa_index] == 0) \ - { \ - GAA_FILL_2ARGS(target[num], func); \ - num++; \ - } \ -} \ -} - -#define GAA_OBLIGAT(str) \ -k = 0; \ -for(i = 0; i < strlen(str); i++) \ -{ \ - j = gaa_get_option_num(str + i, GAA_LETTER_OPTION); \ - if(j == GAA_ERROR_NOMATCH) \ - { \ - printf("Error: invalid 'obligat' set\n"); \ - exit(-1); \ - } \ - if(opt_list[j] == 1) \ - k = 1; \ -} \ -if(k == 0) \ -{ \ - if(strlen(str) == 1) \ - printf("You must give the -%s option\n", str); \ - else \ - printf("You must give at least one option of '%s'\n", str); \ - return 0; \ -} - -#define GAA_INCOMP(str) \ -k = 0; \ -for(i = 0; i < strlen(str); i++) \ -{ \ - j = gaa_get_option_num(str + i, GAA_LETTER_OPTION); \ - if(j == GAA_ERROR_NOMATCH) \ - { \ - printf("Error: invalid 'obligat' set\n"); \ - exit(-1); \ - } \ - if(opt_list[j] == 1) \ - k++; \ -} \ -if(k > 1) \ -{ \ - printf("The options '%s' are incompatible\n", str); \ - return 0; \ -} - - -static char **GAAargv; -static int GAAargc; -static char *gaa_arg_used; -static int gaa_processing_file = 0; -static int inited = 0; - -static int gaa_getint(char *arg) -{ - int tmp; - char a; - if(sscanf(arg, "%d%c", &tmp, &a) < 1) - { - printf("Option %s: '%s' isn't an integer\n", gaa_current_option, arg); - GAAERROR(-1); - } - return tmp; -} - -static char gaa_getchar(char *arg) -{ - if(strlen(arg) != 1) - { - printf("Option %s: '%s' isn't an character\n", gaa_current_option, arg); - GAAERROR(-1); - } - return arg[0]; -} - -static char* gaa_getstr(char *arg) -{ - return arg; -} -static float gaa_getfloat(char *arg) -{ - float tmp; - char a; - if(sscanf(arg, "%f%c", &tmp, &a) < 1) - { - printf("Option %s: '%s' isn't a float number\n", gaa_current_option, arg); - GAAERROR(-1); - } - return tmp; -} -/* option structures */ - -struct GAAOPTION_create_conf -{ - char* arg1; - int size1; -}; - -struct GAAOPTION_passwd_conf -{ - char* arg1; - int size1; -}; - -struct GAAOPTION_salt -{ - int arg1; - int size1; -}; - -struct GAAOPTION_index -{ - int arg1; - int size1; -}; - -struct GAAOPTION_passwd -{ - char* arg1; - int size1; -}; - -struct GAAOPTION_username -{ - char* arg1; - int size1; -}; - -struct GAAOPTION_debug -{ - int arg1; - int size1; -}; - -#line 349 "gaa.skel" -static int gaa_is_an_argument(char *str) -{ -#ifdef GAA_WIN32 - if(str[0] == '/' && str[1] != 0) - return GAA_MULTIPLE_OPTION; -#endif - if(str[0] != '-') - return GAA_NOT_AN_OPTION; - if(str[1] == 0) - return GAA_NOT_AN_OPTION; - if(str[1] == '-') - { - if(str[2] != 0) - return GAA_WORD_OPTION; - else - return GAA_NOT_AN_OPTION; - } - if(str[2] == 0) - return GAA_LETTER_OPTION; - else - return GAA_MULTIPLE_OPTION; -} - -static int gaa_get_option_num(char *str, int status) -{ - switch(status) - { - case GAA_LETTER_OPTION: - GAA_CHECK1STR("", GAAOPTID_create_conf); - GAA_CHECK1STR("c", GAAOPTID_passwd_conf); - GAA_CHECK1STR("s", GAAOPTID_salt); - GAA_CHECK1STR("i", GAAOPTID_index); - GAA_CHECK1STR("p", GAAOPTID_passwd); - GAA_CHECK1STR("u", GAAOPTID_username); - GAA_CHECK1STR("d", GAAOPTID_debug); - case GAA_MULTIPLE_OPTION: -#line 375 "gaa.skel" - GAA_CHECK1STR("h", GAAOPTID_help); - GAA_CHECK1STR("v", GAAOPTID_version); - GAA_CHECK1STR("", GAAOPTID_verify); - -#line 277 "gaa.skel" - break; - case GAA_WORD_OPTION: - GAA_CHECKSTR("help", GAAOPTID_help); - GAA_CHECKSTR("version", GAAOPTID_version); - GAA_CHECKSTR("create-conf", GAAOPTID_create_conf); - GAA_CHECKSTR("passwd-conf", GAAOPTID_passwd_conf); - GAA_CHECKSTR("verify", GAAOPTID_verify); - GAA_CHECKSTR("salt", GAAOPTID_salt); - GAA_CHECKSTR("index", GAAOPTID_index); - GAA_CHECKSTR("passwd", GAAOPTID_passwd); - GAA_CHECKSTR("username", GAAOPTID_username); - GAA_CHECKSTR("debug", GAAOPTID_debug); - -#line 281 "gaa.skel" - break; - default: break; - } - return GAA_ERROR_NOMATCH; -} - -static int gaa_try(int gaa_num, int gaa_index, gaainfo *gaaval, char *opt_list) -{ - int OK = 0; - int gaa_last_non_option; - struct GAAOPTION_create_conf GAATMP_create_conf; - struct GAAOPTION_passwd_conf GAATMP_passwd_conf; - struct GAAOPTION_salt GAATMP_salt; - struct GAAOPTION_index GAATMP_index; - struct GAAOPTION_passwd GAATMP_passwd; - struct GAAOPTION_username GAATMP_username; - struct GAAOPTION_debug GAATMP_debug; - -#line 393 "gaa.skel" -#ifdef GAA_REST_EXISTS - struct GAAREST GAAREST_tmp; -#endif - - opt_list[gaa_num] = 1; - - for(gaa_last_non_option = gaa_index; - (gaa_last_non_option != GAAargc) && (gaa_is_an_argument(GAAargv[gaa_last_non_option]) == GAA_NOT_AN_OPTION); - gaa_last_non_option++); - - if(gaa_num == GAA_REST) - { - gaa_index = 1; - gaa_last_non_option = GAAargc; - } - - switch(gaa_num) - { - case GAAOPTID_help: - OK = 0; -#line 41 "crypt.gaa" -{ gaa_help(); exit(0); ;}; - - return GAA_OK; - break; - case GAAOPTID_version: - OK = 0; -#line 40 "crypt.gaa" -{ srptool_version(); exit(0); ;}; - - return GAA_OK; - break; - case GAAOPTID_create_conf: - OK = 0; - GAA_TESTMOREARGS; - GAA_FILL(GAATMP_create_conf.arg1, gaa_getstr, GAATMP_create_conf.size1); - gaa_index++; -#line 38 "crypt.gaa" -{ gaaval->create_conf = GAATMP_create_conf.arg1 ;}; - - return GAA_OK; - break; - case GAAOPTID_passwd_conf: - OK = 0; - GAA_TESTMOREARGS; - GAA_FILL(GAATMP_passwd_conf.arg1, gaa_getstr, GAATMP_passwd_conf.size1); - gaa_index++; -#line 35 "crypt.gaa" -{ gaaval->passwd_conf = GAATMP_passwd_conf.arg1 ;}; - - return GAA_OK; - break; - case GAAOPTID_verify: - OK = 0; -#line 32 "crypt.gaa" -{ gaaval->verify = 1 ;}; - - return GAA_OK; - break; - case GAAOPTID_salt: - OK = 0; - GAA_TESTMOREARGS; - GAA_FILL(GAATMP_salt.arg1, gaa_getint, GAATMP_salt.size1); - gaa_index++; -#line 29 "crypt.gaa" -{ gaaval->salt = GAATMP_salt.arg1 ;}; - - return GAA_OK; - break; - case GAAOPTID_index: - OK = 0; - GAA_TESTMOREARGS; - GAA_FILL(GAATMP_index.arg1, gaa_getint, GAATMP_index.size1); - gaa_index++; -#line 26 "crypt.gaa" -{ gaaval->index = GAATMP_index.arg1 ;}; - - return GAA_OK; - break; - case GAAOPTID_passwd: - OK = 0; - GAA_TESTMOREARGS; - GAA_FILL(GAATMP_passwd.arg1, gaa_getstr, GAATMP_passwd.size1); - gaa_index++; -#line 23 "crypt.gaa" -{ gaaval->passwd = GAATMP_passwd.arg1 ;}; - - return GAA_OK; - break; - case GAAOPTID_username: - OK = 0; - GAA_TESTMOREARGS; - GAA_FILL(GAATMP_username.arg1, gaa_getstr, GAATMP_username.size1); - gaa_index++; -#line 20 "crypt.gaa" -{ gaaval->username = GAATMP_username.arg1 ;}; - - return GAA_OK; - break; - case GAAOPTID_debug: - OK = 0; - GAA_TESTMOREARGS; - GAA_FILL(GAATMP_debug.arg1, gaa_getint, GAATMP_debug.size1); - gaa_index++; -#line 17 "crypt.gaa" -{ gaaval->debug = GAATMP_debug.arg1 ;}; - - return GAA_OK; - break; - -#line 413 "gaa.skel" - default: break; - } - return GAA_ERROR_UNKNOWN; -} - -int gaa(int argc, char **argv, gaainfo *gaaval) -{ - int tmp1, tmp2; - int i, j; - char *opt_list; - - GAAargv = argv; - GAAargc = argc; - - opt_list = (char*) gaa_malloc(GAA_NB_OPTION + 1); - - for(i = 0; i < GAA_NB_OPTION + 1; i++) - opt_list[i] = 0; - /* initialization */ - if(inited == 0) - { - -#line 43 "crypt.gaa" -{ gaaval->username=NULL; gaaval->passwd=NULL; gaaval->salt=0; - gaaval->create_conf=NULL; gaaval->passwd_conf=NULL; gaaval->verify = 0; - gaaval->index = 1; gaaval->debug = 0; ;}; - - } - inited = 1; -#line 438 "gaa.skel" - gaa_arg_used = NULL; - - if (argc > 0) { - gaa_arg_used = gaa_malloc(argc * sizeof(char)); - } - - for(i = 1; i < argc; i++) - gaa_arg_used[i] = 0; - for(i = 1; i < argc; i++) - { - if(gaa_arg_used[i] == 0) - { - j = 0; - tmp1 = gaa_is_an_argument(GAAargv[i]); - switch(tmp1) - { - case GAA_WORD_OPTION: - j++; - case GAA_LETTER_OPTION: - j++; - tmp2 = gaa_get_option_num(argv[i]+j, tmp1); - if(tmp2 == GAA_ERROR_NOMATCH) - { - printf("Invalid option '%s'\n", argv[i]+j); - return 0; - } - switch(gaa_try(tmp2, i+1, gaaval, opt_list)) - { - case GAA_ERROR_NOTENOUGH_ARGS: - printf("'%s': not enough arguments\n",gaa_current_option); - return 0; - case GAA_ERROR_INVALID_ARG: - printf("Invalid arguments\n"); - return 0; - case GAA_OK: - break; - default: - printf("Unknown error\n"); - } - gaa_arg_used[i] = 1; - break; - case GAA_MULTIPLE_OPTION: - for(j = 1; j < strlen(argv[i]); j++) - { - tmp2 = gaa_get_option_num(argv[i]+j, tmp1); - if(tmp2 == GAA_ERROR_NOMATCH) - { - printf("Invalid option '%c'\n", *(argv[i]+j)); - return 0; - } - switch(gaa_try(tmp2, i+1, gaaval, opt_list)) - { - case GAA_ERROR_NOTENOUGH_ARGS: - printf("'%s': not enough arguments\n",gaa_current_option); - return 0; - case GAA_ERROR_INVALID_ARG: - printf("Invalid arguments\n"); - return 0; - case GAA_OK: - break; - default: - printf("Unknown error\n"); - } - } - gaa_arg_used[i] = 1; - break; - default: break; - } - } - } -if(gaa_processing_file == 0) -{ - -#line 507 "gaa.skel" -#ifdef GAA_REST_EXISTS - switch(gaa_try(GAA_REST, 1, gaaval, opt_list)) - { - case GAA_ERROR_NOTENOUGH_ARGS: - printf("Rest: not enough arguments\n"); - return 0; - case GAA_ERROR_INVALID_ARG: - printf("Invalid arguments\n"); - return 0; - case GAA_OK: - break; - default: - printf("Unknown error\n"); - } -#endif -} - for(i = 1; i < argc; i++) - { - if(gaa_arg_used[i] == 0) - { - printf("Too many arguments\n"); - return 0; - } - } - free(gaa_arg_used); - free(opt_list); - return -1; -} - -struct gaastrnode -{ - char *str; - struct gaastrnode *next; -}; - -typedef struct gaastrnode gaa_str_node; - -static int gaa_internal_get_next_str(FILE *file, gaa_str_node *tmp_str, int argc) -{ - int pos_ini; - int a; - int i = 0, len = 0, newline = 0; - - if(argc == 1) { - newline = 1; - len = 2; - } - - a = fgetc( file); - if (a == EOF) return 0; - - while(a == ' ' || a == 9 || a == '\n') - { - if(a == '\n') - { - newline=1; - len = 2; - } - a = fgetc( file); - if (a == EOF) return 0; - } - - pos_ini = ftell(file) - 1; - - while(a != ' ' && a != 9 && a != '\n') - { - - len++; - a = fgetc( file); - if(a==EOF) return 0; //a = ' '; - } - - len += 1; - tmp_str->str = gaa_malloc((len) * sizeof(char)); - - if(newline == 1) - { - tmp_str->str[0] = '-'; - tmp_str->str[1] = '-'; - i = 2; - } - else - { - i = 0; - } - - fseek(file,pos_ini, SEEK_SET); - do - { - a = fgetc( file); - - if (a == EOF) { - i+=2; - break; - } - tmp_str->str[i] = a; - i++; - } - while(a != ' ' && a != 9 && a != '\n' && i < len); - - tmp_str->str[i - 1] = 0; - - fseek(file,- 1, SEEK_CUR); -/* printf("%d\n", ftell(file)); */ - - return -1; -} - -int gaa_file(const char *name, gaainfo *gaaval) -{ - gaa_str_node *first_str, **tmp_str, *tmp_str2; - int rval, i; - char **argv; - int argc = 0; - FILE *file; - - gaa_processing_file = 1; - - if((file = fopen(name, "r")) == NULL) - { - printf("Couldn't open '%s' configuration file for reading\n", name); - return 1; - } - - tmp_str = &first_str; - do - { - argc++; - *tmp_str = gaa_malloc(sizeof(gaa_str_node)); - - (*tmp_str)->str = NULL; - (*tmp_str)->next = NULL; - - rval = gaa_internal_get_next_str(file, *tmp_str, argc); - tmp_str = &((*tmp_str)->next); - } - while(rval == -1); - - if(rval == 1) - return 0; - - argv = gaa_malloc((1 + argc) * sizeof(char*)); - - tmp_str2 = first_str; - argv[0] = "cfg"; - for(i = 1; i < argc; i++) - { - argv[i] = tmp_str2->str; - tmp_str2 = tmp_str2->next; - } - - rval = gaa(argc, argv, gaaval); - gaa_processing_file = 0; - return rval; -} diff --git a/src/crypt-gaa.h b/src/crypt-gaa.h deleted file mode 100644 index 18ea3bb6e9..0000000000 --- a/src/crypt-gaa.h +++ /dev/null @@ -1,47 +0,0 @@ - -#line 104 "gaa.skel" -/* GAA HEADER */ -#ifndef GAA_HEADER_POKY -#define GAA_HEADER_POKY - -typedef struct _gaainfo gaainfo; - -struct _gaainfo -{ -#line 37 "crypt.gaa" - char *create_conf; -#line 34 "crypt.gaa" - char *passwd_conf; -#line 31 "crypt.gaa" - int verify; -#line 28 "crypt.gaa" - int salt; -#line 25 "crypt.gaa" - int index; -#line 22 "crypt.gaa" - char *passwd; -#line 19 "crypt.gaa" - char *username; -#line 16 "crypt.gaa" - int debug; - -#line 114 "gaa.skel" -}; - -#ifdef __cplusplus -extern "C" -{ -#endif - - int gaa(int argc, char *argv[], gaainfo *gaaval); - - void gaa_help(void); - - int gaa_file(const char *name, gaainfo *gaaval); - -#ifdef __cplusplus -} -#endif - - -#endif diff --git a/src/crypt.c b/src/crypt.c deleted file mode 100644 index 187842450a..0000000000 --- a/src/crypt.c +++ /dev/null @@ -1,731 +0,0 @@ -/* - * Copyright (C) 2001, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010 - * Free Software Foundation, Inc. - * - * This file is part of GnuTLS. - * - * GnuTLS 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, either version 3 of the License, or - * (at your option) any later version. - * - * GnuTLS 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, see - * . - */ - -#include - -#include -#include -#include -#include -#include -#include -#include "../lib/random.h" /* for random */ - -#include -#include - -#ifndef _WIN32 -#include -#include -#else -#include -#endif - -/* Gnulib portability files. */ -#include -#include -#include -#include - -/* This may need some rewrite. A lot of stuff which should be here - * are in the library, which is not good. - */ - -int crypt_int (const char *username, const char *passwd, int salt, - char *tpasswd_conf, char *tpasswd, int uindex); -static int read_conf_values (gnutls_datum_t * g, gnutls_datum_t * n, - char *str); -static int _verify_passwd_int (const char *username, const char *passwd, - char *verifier, char *salt, - const gnutls_datum_t * g, - const gnutls_datum_t * n); - -static void -print_num (const char *msg, const gnutls_datum_t * num) -{ - unsigned int i; - - printf ("%s:\t", msg); - - for (i = 0; i < num->size; i++) - { - if (i != 0 && i % 12 == 0) - printf ("\n\t"); - else if (i != 0 && i != num->size) - printf (":"); - printf ("%.2x", num->data[i]); - } - printf ("\n\n"); - -} - -static int -generate_create_conf (char *tpasswd_conf) -{ - FILE *fd; - char line[5 * 1024]; - int index = 1; - gnutls_datum_t g, n; - gnutls_datum_t str_g, str_n; - - fd = fopen (tpasswd_conf, "w"); - if (fd == NULL) - { - fprintf (stderr, "Cannot open file '%s'\n", tpasswd_conf); - return -1; - } - - for (index = 1; index <= 3; index++) - { - - if (index == 1) - { - n = gnutls_srp_1024_group_prime; - g = gnutls_srp_1024_group_generator; - } - else if (index == 2) - { - n = gnutls_srp_1536_group_prime; - g = gnutls_srp_1536_group_generator; - } - else - { - n = gnutls_srp_2048_group_prime; - g = gnutls_srp_2048_group_generator; - } - - printf ("\nGroup %d, of %d bits:\n", index, n.size * 8); - print_num ("Generator", &g); - print_num ("Prime", &n); - - if (gnutls_srp_base64_encode_alloc (&n, &str_n) < 0) - { - fprintf (stderr, "Could not encode\n"); - return -1; - } - - if (gnutls_srp_base64_encode_alloc (&g, &str_g) < 0) - { - fprintf (stderr, "Could not encode\n"); - return -1; - } - - sprintf (line, "%d:%s:%s\n", index, str_n.data, str_g.data); - - gnutls_free (str_n.data); - gnutls_free (str_g.data); - - fwrite (line, 1, strlen (line), fd); - - } - - fclose (fd); - - return 0; - -} - -/* The format of a tpasswd file is: - * username:verifier:salt:index - * - * index is the index of the prime-generator pair in tpasswd.conf - */ -static int -_verify_passwd_int (const char *username, const char *passwd, - char *verifier, char *salt, - const gnutls_datum_t * g, const gnutls_datum_t * n) -{ - char _salt[1024]; - gnutls_datum_t tmp, raw_salt, new_verifier; - size_t salt_size; - char *pos; - - if (salt == NULL || verifier == NULL) - return -1; - - /* copy salt, and null terminate after the ':' */ - strcpy (_salt, salt); - pos = strchr (_salt, ':'); - if (pos != NULL) - *pos = 0; - - /* convert salt to binary. */ - tmp.data = _salt; - tmp.size = strlen (_salt); - - if (gnutls_srp_base64_decode_alloc (&tmp, &raw_salt) < 0) - { - fprintf (stderr, "Could not decode salt.\n"); - return -1; - } - - if (gnutls_srp_verifier - (username, passwd, &raw_salt, g, n, &new_verifier) < 0) - { - fprintf (stderr, "Could not make the verifier\n"); - return -1; - } - - free (raw_salt.data); - - /* encode the verifier into _salt */ - salt_size = sizeof (_salt); - memset (_salt, 0, salt_size); - if (gnutls_srp_base64_encode (&new_verifier, _salt, &salt_size) < 0) - { - fprintf (stderr, "Encoding error\n"); - return -1; - } - - free (new_verifier.data); - - if (strncmp (verifier, _salt, strlen (_salt)) == 0) - { - fprintf (stderr, "Password verified\n"); - return 0; - } - else - { - fprintf (stderr, "Password does NOT match\n"); - } - return -1; -} - -static int -filecopy (char *src, char *dst) -{ - FILE *fd, *fd2; - char line[5 * 1024]; - char *p; - - fd = fopen (dst, "w"); - if (fd == NULL) - { - fprintf (stderr, "Cannot open '%s' for write\n", dst); - return -1; - } - - fd2 = fopen (src, "r"); - if (fd2 == NULL) - { - /* empty file */ - fclose (fd); - return 0; - } - - line[sizeof (line) - 1] = 0; - do - { - p = fgets (line, sizeof (line) - 1, fd2); - if (p == NULL) - break; - - fputs (line, fd); - } - while (1); - - fclose (fd); - fclose (fd2); - - return 0; -} - -/* accepts password file */ -static int -find_strchr (char *username, char *file) -{ - FILE *fd; - char *pos; - char line[5 * 1024]; - unsigned int i; - - fd = fopen (file, "r"); - if (fd == NULL) - { - fprintf (stderr, "Cannot open file '%s'\n", file); - return -1; - } - - while (fgets (line, sizeof (line), fd) != NULL) - { - /* move to first ':' */ - i = 0; - while ((line[i] != ':') && (line[i] != '\0') && (i < sizeof (line))) - { - i++; - } - if (strncmp (username, line, MAX (i, strlen (username))) == 0) - { - /* find the index */ - pos = strrchr (line, ':'); - pos++; - fclose (fd); - return atoi (pos); - } - } - - fclose (fd); - return -1; -} - -/* Parses the tpasswd files, in order to verify the given - * username/password pair. - */ -static int -verify_passwd (char *conffile, char *tpasswd, char *username, - const char *passwd) -{ - FILE *fd; - char line[5 * 1024]; - unsigned int i; - gnutls_datum_t g, n; - int iindex; - char *p, *pos; - - iindex = find_strchr (username, tpasswd); - if (iindex == -1) - { - fprintf (stderr, "Cannot find '%s' in %s\n", username, tpasswd); - return -1; - } - - fd = fopen (conffile, "r"); - if (fd == NULL) - { - fprintf (stderr, "Cannot find %s\n", conffile); - return -1; - } - - do - { - p = fgets (line, sizeof (line) - 1, fd); - } - while (p != NULL && atoi (p) != iindex); - - if (p == NULL) - { - fprintf (stderr, "Cannot find entry in %s\n", conffile); - return -1; - } - line[sizeof (line) - 1] = 0; - - fclose (fd); - - if ((iindex = read_conf_values (&g, &n, line)) < 0) - { - fprintf (stderr, "Cannot parse conf file '%s'\n", conffile); - return -1; - } - - fd = fopen (tpasswd, "r"); - if (fd == NULL) - { - fprintf (stderr, "Cannot open file '%s'\n", tpasswd); - return -1; - } - - while (fgets (line, sizeof (line), fd) != NULL) - { - /* move to first ':' - * This is the actual verifier. - */ - i = 0; - while ((line[i] != ':') && (line[i] != '\0') && (i < sizeof (line))) - { - i++; - } - if (strncmp (username, line, MAX (i, strlen (username))) == 0) - { - char *verifier_pos, *salt_pos; - - pos = strchr (line, ':'); - fclose (fd); - if (pos == NULL) - { - fprintf (stderr, "Cannot parse conf file '%s'\n", conffile); - return -1; - } - pos++; - verifier_pos = pos; - - /* Move to the salt */ - pos = strchr (pos, ':'); - if (pos == NULL) - { - fprintf (stderr, "Cannot parse conf file '%s'\n", conffile); - return -1; - } - pos++; - salt_pos = pos; - - return _verify_passwd_int (username, passwd, - verifier_pos, salt_pos, &g, &n); - } - } - - fclose (fd); - return -1; - -} - -#define KPASSWD "/etc/tpasswd" -#define KPASSWD_CONF "/etc/tpasswd.conf" - -static void -tls_log_func (int level, const char *str) -{ - fprintf (stderr, "|<%d>| %s", level, str); -} - -int main (int argc, char **argv) -{ - gaainfo info; - const char *passwd; - int salt_size, ret; - struct passwd *pwd; - - set_program_name (argv[0]); - - if ((ret = gnutls_global_init ()) < 0) - { - fprintf (stderr, "global_init: %s\n", gnutls_strerror (ret)); - exit (1); - } - - umask (066); - - if (gaa (argc, argv, &info) != -1) - { - fprintf (stderr, "Error in the arguments.\n"); - return -1; - } - - gnutls_global_set_log_function (tls_log_func); - gnutls_global_set_log_level (info.debug); - - if (info.create_conf != NULL) - { - return generate_create_conf (info.create_conf); - } - - if (info.passwd == NULL) - info.passwd = (char *) KPASSWD; - if (info.passwd_conf == NULL) - info.passwd_conf = (char *) KPASSWD_CONF; - - if (info.username == NULL) - { -#ifndef _WIN32 - pwd = getpwuid (getuid ()); - - if (pwd == NULL) - { - fprintf (stderr, "No such user\n"); - return -1; - } - - info.username = pwd->pw_name; -#else - fprintf (stderr, "Please specify a user\n"); - return -1; -#endif - } - - salt_size = 16; - - passwd = getpass ("Enter password: "); - if (passwd == NULL) - { - fprintf (stderr, "Please specify a password\n"); - return -1; - } - -/* not ready yet */ - if (info.verify != 0) - { - return verify_passwd (info.passwd_conf, info.passwd, - info.username, passwd); - } - - - return crypt_int (info.username, passwd, salt_size, - info.passwd_conf, info.passwd, info.index); - -} - -static char * -_srp_crypt (const char *username, const char *passwd, int salt_size, - const gnutls_datum_t * g, const gnutls_datum_t * n) -{ - char salt[128]; - static char result[1024]; - gnutls_datum_t dat_salt, txt_salt; - gnutls_datum_t verifier, txt_verifier; - - if ((unsigned) salt_size > sizeof (salt)) - return NULL; - - /* generate the salt - */ - if (gnutls_rnd (GNUTLS_RND_NONCE, salt, salt_size) < 0) - { - fprintf (stderr, "Could not create nonce\n"); - return NULL; - } - - dat_salt.data = salt; - dat_salt.size = salt_size; - - if (gnutls_srp_verifier (username, passwd, &dat_salt, g, n, &verifier) < 0) - { - fprintf (stderr, "Error getting verifier\n"); - return NULL; - } - - /* base64 encode the verifier */ - if (gnutls_srp_base64_encode_alloc (&verifier, &txt_verifier) < 0) - { - fprintf (stderr, "Error encoding\n"); - free (verifier.data); - return NULL; - } - - free (verifier.data); - - if (gnutls_srp_base64_encode_alloc (&dat_salt, &txt_salt) < 0) - { - fprintf (stderr, "Error encoding\n"); - return NULL; - } - - sprintf (result, "%s:%s", txt_verifier.data, txt_salt.data); - free (txt_salt.data); - free (txt_verifier.data); - - return result; - -} - - -int -crypt_int (const char *username, const char *passwd, int salt_size, - char *tpasswd_conf, char *tpasswd, int uindex) -{ - FILE *fd; - char *cr; - gnutls_datum_t g, n; - char line[5 * 1024]; - char *p, *pp; - int iindex; - char tmpname[1024]; - - fd = fopen (tpasswd_conf, "r"); - if (fd == NULL) - { - fprintf (stderr, "Cannot find %s\n", tpasswd_conf); - return -1; - } - - do - { /* find the specified uindex in file */ - p = fgets (line, sizeof (line) - 1, fd); - iindex = atoi (p); - } - while (p != NULL && iindex != uindex); - - if (p == NULL) - { - fprintf (stderr, "Cannot find entry in %s\n", tpasswd_conf); - return -1; - } - line[sizeof (line) - 1] = 0; - - fclose (fd); - if ((iindex = read_conf_values (&g, &n, line)) < 0) - { - fprintf (stderr, "Cannot parse conf file '%s'\n", tpasswd_conf); - return -1; - } - - cr = _srp_crypt (username, passwd, salt_size, &g, &n); - if (cr == NULL) - { - fprintf (stderr, "Cannot _srp_crypt()...\n"); - return -1; - } - else - { - /* delete previous entry */ - struct stat st; - FILE *fd2; - int put; - - if (strlen (tpasswd) > sizeof (tmpname) + 5) - { - fprintf (stderr, "file '%s' is tooooo long\n", tpasswd); - return -1; - } - strcpy (tmpname, tpasswd); - strcat (tmpname, ".tmp"); - - if (stat (tmpname, &st) != -1) - { - fprintf (stderr, "file '%s' is locked\n", tpasswd); - return -1; - } - - if (filecopy (tpasswd, tmpname) != 0) - { - fprintf (stderr, "Cannot copy '%s' to '%s'\n", tpasswd, tmpname); - return -1; - } - - fd = fopen (tpasswd, "w"); - if (fd == NULL) - { - fprintf (stderr, "Cannot open '%s' for write\n", tpasswd); - remove (tmpname); - return -1; - } - - fd2 = fopen (tmpname, "r"); - if (fd2 == NULL) - { - fprintf (stderr, "Cannot open '%s' for read\n", tmpname); - remove (tmpname); - return -1; - } - - put = 0; - do - { - p = fgets (line, sizeof (line) - 1, fd2); - if (p == NULL) - break; - - pp = strchr (line, ':'); - if (pp == NULL) - continue; - - if (strncmp (p, username, - MAX (strlen (username), (unsigned int) (pp - p))) == 0) - { - put = 1; - fprintf (fd, "%s:%s:%u\n", username, cr, iindex); - } - else - { - fputs (line, fd); - } - } - while (1); - - if (put == 0) - { - fprintf (fd, "%s:%s:%u\n", username, cr, iindex); - } - - fclose (fd); - fclose (fd2); - - remove (tmpname); - - } - - - return 0; -} - - - -/* this function parses tpasswd.conf file. Format is: - * int(index):base64(n):base64(g) - */ -static int -read_conf_values (gnutls_datum_t * g, gnutls_datum_t * n, char *str) -{ - char *p; - int len; - int index, ret; - gnutls_datum_t dat; - - index = atoi (str); - - p = strrchr (str, ':'); /* we have g */ - if (p == NULL) - { - return -1; - } - - *p = '\0'; - p++; - - /* read the generator */ - len = strlen (p); - if (p[len - 1] == '\n') - len--; - - dat.data = p; - dat.size = len; - ret = gnutls_srp_base64_decode_alloc (&dat, g); - - if (ret < 0) - { - fprintf (stderr, "Decoding error\n"); - return -1; - } - - /* now go for n - modulo */ - p = strrchr (str, ':'); /* we have n */ - if (p == NULL) - { - return -1; - } - - *p = '\0'; - p++; - - dat.data = p; - dat.size = strlen (p); - - ret = gnutls_srp_base64_decode_alloc (&dat, n); - - if (ret < 0) - { - fprintf (stderr, "Decoding error\n"); - free (g->data); - return -1; - } - - return index; -} - -extern void srptool_version (void); - -void -srptool_version (void) -{ - const char *p = PACKAGE_NAME; - if (strcmp (gnutls_check_version (NULL), PACKAGE_VERSION) != 0) - p = PACKAGE_STRING; - version_etc (stdout, "srptool", p, gnutls_check_version (NULL), - "Nikos Mavrogiannopoulos", (char *) NULL); -} diff --git a/src/crypt.gaa b/src/crypt.gaa deleted file mode 100644 index 0026b0a887..0000000000 --- a/src/crypt.gaa +++ /dev/null @@ -1,45 +0,0 @@ -#{ - -/* C declarations */ - -#include -#ifdef _WIN32 -# include -#endif - -void srptool_version(void); - -#} - -helpnode "Srptool help\nUsage : srptool [options]" - -#int debug; -option (d, debug) INT "integer" { $debug = $1 } "Enable debugging" - -#char *username; -option (u,username) STR "username" { $username = $1 } "specify username." - -#char *passwd; -option (p, passwd) STR "FILE" { $passwd = $1 } "specify a password file." - -#int index; -option (i, index) INT "INDEX" { $index = $1 } "specify the index of the group parameters in tpasswd.conf to use." - -#int salt; -option (s, salt) INT "SALT" { $salt = $1 } "specify salt size for crypt algorithm." - -#int verify; -option (verify) { $verify = 1 } "just verify password." - -#char *passwd_conf; -option (c, passwd-conf) STR "FILE" { $passwd_conf = $1 } "specify a password conf file." - -#char *create_conf; -option ( create-conf) STR "FILE" { $create_conf = $1 } "Generate a tpasswd.conf file." - -option (v, version) { srptool_version(); exit(0); } "prints the program's version number" -option (h, help) { gaa_help(); exit(0); } "shows this help text" - -init { $username=NULL; $passwd=NULL; $salt=0; - $create_conf=NULL; $passwd_conf=NULL; $verify = 0; - $index = 1; $debug = 0; } diff --git a/src/srptool-gaa.c b/src/srptool-gaa.c new file mode 100644 index 0000000000..f396335b5a --- /dev/null +++ b/src/srptool-gaa.c @@ -0,0 +1,910 @@ +/* File generated by GAA 1.6.6 + */ +#define GAA_NO_WIN32 +#line 1 "srptool.gaa" + + +/* C declarations */ + +#include +#ifdef _WIN32 +# include +#endif + +void srptool_version(void); + +#include +#include +#include + +#ifndef GAA_NO_WIN32 +#if defined(_WIN32) || defined(__WIN32__) || defined(WIN32) || defined(WINDOWS) +#define GAA_WIN32 +#endif +#endif + +static void* gaa_malloc( size_t size) { +void* ret; + ret = malloc(size); + if (ret==NULL) { + fprintf(stderr, "gaa: could not allocate memory"); + exit(1); + } + return ret; +} + +static void __gaa_helpsingle(char short_name, char *name, + char *arg_desc, char *opt_help) +{ + int col1, col3, col4, tabsize = 3, curr; + int i; + + col1 = 5; /* Default values */ + col3 = 30; + col4 = 70; + + curr = 0; + for(i = 0; i < col1; i++) + { + printf(" "); + curr++; + } + if(short_name) + { + if(name && *name) + { + printf("-%c, ", short_name); + curr += 4; + } + else + { + printf("-%c ", short_name); + curr += 3; + } + } + if(name && *name) + { + printf("--%s ", name); + curr += 3 + strlen(name); + } + if(arg_desc && *arg_desc) + { + printf("%s ", arg_desc); + curr += 1 + strlen(arg_desc); + } + if(curr >= col3) + { + printf("\n"); + curr = 0; + } + if(opt_help) /* let's print the option's help body */ + { + const char *str = opt_help; + while(*str) + { + while(curr < col3) + { + printf(" "); + curr++; + } + switch(*str) + { + case '\n': + printf("\n"); + curr = 0; + break; + case '\t': + do + { + printf(" "); + curr++; + } + while((curr - col3) % tabsize != 0 && curr < col4); + case ' ': + if(*str == ' ') + { + curr++; + printf(" "); + } + for(i = 1; str[i] && str[i] != ' ' && str[i] != '\n' + && str[i] != '\t'; i++); + if(curr + i - 1 >= col4) + curr = col4; + break; + default: + printf("%c", *str); + curr++; + } + if(curr >= col4) + { + printf("\n"); + curr = 0; + } + str++; + } + } + printf("\n"); +} + +void gaa_help(void) +{ + printf("Srptool help\nUsage : srptool [options]\n"); + __gaa_helpsingle('d', "debug", "integer ", "Enable debugging"); + __gaa_helpsingle('u', "username", "username ", "specify username."); + __gaa_helpsingle('p', "passwd", "FILE ", "specify a password file."); + __gaa_helpsingle('i', "index", "INDEX ", "specify the index of the group parameters in tpasswd.conf to use."); + __gaa_helpsingle('s', "salt", "SALT ", "specify salt size for crypt algorithm."); + __gaa_helpsingle(0, "verify", "", "just verify password."); + __gaa_helpsingle('c', "passwd-conf", "FILE ", "specify a password conf file."); + __gaa_helpsingle(0, "create-conf", "FILE ", "Generate a tpasswd.conf file."); + __gaa_helpsingle('v', "version", "", "prints the program's version number"); + __gaa_helpsingle('h', "help", "", "shows this help text"); + +#line 100 "gaa.skel" +} +/* Copy of C area */ + +#line 104 "gaa.skel" +/* GAA HEADER */ +#ifndef GAA_HEADER_POKY +#define GAA_HEADER_POKY + +typedef struct _gaainfo gaainfo; + +struct _gaainfo +{ +#line 37 "srptool.gaa" + char *create_conf; +#line 34 "srptool.gaa" + char *passwd_conf; +#line 31 "srptool.gaa" + int verify; +#line 28 "srptool.gaa" + int salt; +#line 25 "srptool.gaa" + int index; +#line 22 "srptool.gaa" + char *passwd; +#line 19 "srptool.gaa" + char *username; +#line 16 "srptool.gaa" + int debug; + +#line 114 "gaa.skel" +}; + +#ifdef __cplusplus +extern "C" +{ +#endif + + int gaa(int argc, char *argv[], gaainfo *gaaval); + + void gaa_help(void); + + int gaa_file(const char *name, gaainfo *gaaval); + +#ifdef __cplusplus +} +#endif + + +#endif + +#line 135 "gaa.skel" + +/* C declarations */ + +#define GAAERROR(x) \ +{ \ +gaa_error = 1; \ +return x; \ +} + +static char *gaa_current_option; +static int gaa_error = 0; + +/* Generated by gaa */ + +#include +#include + + +#define GAA_OK -1 + +#define GAA_ERROR_NOMATCH 0 +#define GAA_ERROR_NOTENOUGH_ARGS 1 +#define GAA_ERROR_INVALID_ARG 2 +#define GAA_ERROR_UNKNOWN 3 + +#define GAA_NOT_AN_OPTION 0 +#define GAA_WORD_OPTION 1 +#define GAA_LETTER_OPTION 2 +#define GAA_MULTIPLE_OPTION 3 + +#define GAA_REST 0 +#define GAA_NB_OPTION 10 +#define GAAOPTID_help 1 +#define GAAOPTID_version 2 +#define GAAOPTID_create_conf 3 +#define GAAOPTID_passwd_conf 4 +#define GAAOPTID_verify 5 +#define GAAOPTID_salt 6 +#define GAAOPTID_index 7 +#define GAAOPTID_passwd 8 +#define GAAOPTID_username 9 +#define GAAOPTID_debug 10 + +#line 168 "gaa.skel" + +#define GAA_CHECK1STR(a,b) \ +if(a[0] == str[0]) \ +{ \ + gaa_current_option = a; \ + return b; \ +} + +#define GAA_CHECKSTR(a,b) \ +if(strcmp(a,str) == 0) \ +{ \ + gaa_current_option = a; \ + return b; \ +} + +#define GAA_TESTMOREARGS \ +if(!OK) \ +{ \ +while((gaa_last_non_option != gaa_index) && (gaa_arg_used[gaa_index] == 1)) \ + gaa_index++; \ +if(gaa_last_non_option == gaa_index) \ + return GAA_ERROR_NOTENOUGH_ARGS; \ +} + +#define GAA_TESTMOREOPTIONALARGS \ +if(!OK) \ +{ \ +while((gaa_last_non_option != gaa_index) && (gaa_arg_used[gaa_index] == 1)) \ + gaa_index++; \ +if(gaa_last_non_option == gaa_index) \ + OK = 1; \ +} + +#define GAA_FILL_2ARGS(target, func) \ +target = func(GAAargv[gaa_index]); \ +gaa_arg_used[gaa_index] = 1; \ +if(gaa_error == 1) \ +{ \ + gaa_error = 0; \ + return GAA_ERROR_INVALID_ARG; \ +} + + + +#define GAA_FILL(target, func, num) \ +if(!OK) \ +{ \ +target = func(GAAargv[gaa_index]); \ +gaa_arg_used[gaa_index] = 1; \ +if(gaa_error == 1) \ +{ \ + gaa_error = 0; \ + return GAA_ERROR_INVALID_ARG; \ +} \ +num = 1; \ +} \ +else \ +{ \ +num = 0; \ +} + +#define GAA_LIST_FILL(target, func, type ,num) \ +if(!OK) \ +{ \ +num = 0; \ +target = NULL; \ +if ( gaa_last_non_option - gaa_index > 0) \ + target = gaa_malloc((gaa_last_non_option - gaa_index) * sizeof(type));\ +for(; gaa_index < gaa_last_non_option; gaa_index++) \ +{ \ + if(gaa_arg_used[gaa_index] == 0) \ + { \ + GAA_FILL_2ARGS(target[num], func); \ + num++; \ + } \ +} \ +if(num == 0) \ + return GAA_ERROR_NOTENOUGH_ARGS; \ +} + +#define GAA_OPTIONALLIST_FILL(target, func, type ,num) \ +if(!OK) \ +{ \ +num = 0; \ +target = NULL; \ +if ( gaa_last_non_option - gaa_index > 0) \ + target = gaa_malloc((gaa_last_non_option - gaa_index) * sizeof(type));\ +for(; gaa_index < gaa_last_non_option; gaa_index++) \ +{ \ + if(gaa_arg_used[gaa_index] == 0) \ + { \ + GAA_FILL_2ARGS(target[num], func); \ + num++; \ + } \ +} \ +} + +#define GAA_OBLIGAT(str) \ +k = 0; \ +for(i = 0; i < strlen(str); i++) \ +{ \ + j = gaa_get_option_num(str + i, GAA_LETTER_OPTION); \ + if(j == GAA_ERROR_NOMATCH) \ + { \ + printf("Error: invalid 'obligat' set\n"); \ + exit(-1); \ + } \ + if(opt_list[j] == 1) \ + k = 1; \ +} \ +if(k == 0) \ +{ \ + if(strlen(str) == 1) \ + printf("You must give the -%s option\n", str); \ + else \ + printf("You must give at least one option of '%s'\n", str); \ + return 0; \ +} + +#define GAA_INCOMP(str) \ +k = 0; \ +for(i = 0; i < strlen(str); i++) \ +{ \ + j = gaa_get_option_num(str + i, GAA_LETTER_OPTION); \ + if(j == GAA_ERROR_NOMATCH) \ + { \ + printf("Error: invalid 'obligat' set\n"); \ + exit(-1); \ + } \ + if(opt_list[j] == 1) \ + k++; \ +} \ +if(k > 1) \ +{ \ + printf("The options '%s' are incompatible\n", str); \ + return 0; \ +} + + +static char **GAAargv; +static int GAAargc; +static char *gaa_arg_used; +static int gaa_processing_file = 0; +static int inited = 0; + +static int gaa_getint(char *arg) +{ + int tmp; + char a; + if(sscanf(arg, "%d%c", &tmp, &a) < 1) + { + printf("Option %s: '%s' isn't an integer\n", gaa_current_option, arg); + GAAERROR(-1); + } + return tmp; +} + +static char gaa_getchar(char *arg) +{ + if(strlen(arg) != 1) + { + printf("Option %s: '%s' isn't an character\n", gaa_current_option, arg); + GAAERROR(-1); + } + return arg[0]; +} + +static char* gaa_getstr(char *arg) +{ + return arg; +} +static float gaa_getfloat(char *arg) +{ + float tmp; + char a; + if(sscanf(arg, "%f%c", &tmp, &a) < 1) + { + printf("Option %s: '%s' isn't a float number\n", gaa_current_option, arg); + GAAERROR(-1); + } + return tmp; +} +/* option structures */ + +struct GAAOPTION_create_conf +{ + char* arg1; + int size1; +}; + +struct GAAOPTION_passwd_conf +{ + char* arg1; + int size1; +}; + +struct GAAOPTION_salt +{ + int arg1; + int size1; +}; + +struct GAAOPTION_index +{ + int arg1; + int size1; +}; + +struct GAAOPTION_passwd +{ + char* arg1; + int size1; +}; + +struct GAAOPTION_username +{ + char* arg1; + int size1; +}; + +struct GAAOPTION_debug +{ + int arg1; + int size1; +}; + +#line 349 "gaa.skel" +static int gaa_is_an_argument(char *str) +{ +#ifdef GAA_WIN32 + if(str[0] == '/' && str[1] != 0) + return GAA_MULTIPLE_OPTION; +#endif + if(str[0] != '-') + return GAA_NOT_AN_OPTION; + if(str[1] == 0) + return GAA_NOT_AN_OPTION; + if(str[1] == '-') + { + if(str[2] != 0) + return GAA_WORD_OPTION; + else + return GAA_NOT_AN_OPTION; + } + if(str[2] == 0) + return GAA_LETTER_OPTION; + else + return GAA_MULTIPLE_OPTION; +} + +static int gaa_get_option_num(char *str, int status) +{ + switch(status) + { + case GAA_LETTER_OPTION: + GAA_CHECK1STR("", GAAOPTID_create_conf); + GAA_CHECK1STR("c", GAAOPTID_passwd_conf); + GAA_CHECK1STR("s", GAAOPTID_salt); + GAA_CHECK1STR("i", GAAOPTID_index); + GAA_CHECK1STR("p", GAAOPTID_passwd); + GAA_CHECK1STR("u", GAAOPTID_username); + GAA_CHECK1STR("d", GAAOPTID_debug); + case GAA_MULTIPLE_OPTION: +#line 375 "gaa.skel" + GAA_CHECK1STR("h", GAAOPTID_help); + GAA_CHECK1STR("v", GAAOPTID_version); + GAA_CHECK1STR("", GAAOPTID_verify); + +#line 277 "gaa.skel" + break; + case GAA_WORD_OPTION: + GAA_CHECKSTR("help", GAAOPTID_help); + GAA_CHECKSTR("version", GAAOPTID_version); + GAA_CHECKSTR("create-conf", GAAOPTID_create_conf); + GAA_CHECKSTR("passwd-conf", GAAOPTID_passwd_conf); + GAA_CHECKSTR("verify", GAAOPTID_verify); + GAA_CHECKSTR("salt", GAAOPTID_salt); + GAA_CHECKSTR("index", GAAOPTID_index); + GAA_CHECKSTR("passwd", GAAOPTID_passwd); + GAA_CHECKSTR("username", GAAOPTID_username); + GAA_CHECKSTR("debug", GAAOPTID_debug); + +#line 281 "gaa.skel" + break; + default: break; + } + return GAA_ERROR_NOMATCH; +} + +static int gaa_try(int gaa_num, int gaa_index, gaainfo *gaaval, char *opt_list) +{ + int OK = 0; + int gaa_last_non_option; + struct GAAOPTION_create_conf GAATMP_create_conf; + struct GAAOPTION_passwd_conf GAATMP_passwd_conf; + struct GAAOPTION_salt GAATMP_salt; + struct GAAOPTION_index GAATMP_index; + struct GAAOPTION_passwd GAATMP_passwd; + struct GAAOPTION_username GAATMP_username; + struct GAAOPTION_debug GAATMP_debug; + +#line 393 "gaa.skel" +#ifdef GAA_REST_EXISTS + struct GAAREST GAAREST_tmp; +#endif + + opt_list[gaa_num] = 1; + + for(gaa_last_non_option = gaa_index; + (gaa_last_non_option != GAAargc) && (gaa_is_an_argument(GAAargv[gaa_last_non_option]) == GAA_NOT_AN_OPTION); + gaa_last_non_option++); + + if(gaa_num == GAA_REST) + { + gaa_index = 1; + gaa_last_non_option = GAAargc; + } + + switch(gaa_num) + { + case GAAOPTID_help: + OK = 0; +#line 41 "srptool.gaa" +{ gaa_help(); exit(0); ;}; + + return GAA_OK; + break; + case GAAOPTID_version: + OK = 0; +#line 40 "srptool.gaa" +{ srptool_version(); exit(0); ;}; + + return GAA_OK; + break; + case GAAOPTID_create_conf: + OK = 0; + GAA_TESTMOREARGS; + GAA_FILL(GAATMP_create_conf.arg1, gaa_getstr, GAATMP_create_conf.size1); + gaa_index++; +#line 38 "srptool.gaa" +{ gaaval->create_conf = GAATMP_create_conf.arg1 ;}; + + return GAA_OK; + break; + case GAAOPTID_passwd_conf: + OK = 0; + GAA_TESTMOREARGS; + GAA_FILL(GAATMP_passwd_conf.arg1, gaa_getstr, GAATMP_passwd_conf.size1); + gaa_index++; +#line 35 "srptool.gaa" +{ gaaval->passwd_conf = GAATMP_passwd_conf.arg1 ;}; + + return GAA_OK; + break; + case GAAOPTID_verify: + OK = 0; +#line 32 "srptool.gaa" +{ gaaval->verify = 1 ;}; + + return GAA_OK; + break; + case GAAOPTID_salt: + OK = 0; + GAA_TESTMOREARGS; + GAA_FILL(GAATMP_salt.arg1, gaa_getint, GAATMP_salt.size1); + gaa_index++; +#line 29 "srptool.gaa" +{ gaaval->salt = GAATMP_salt.arg1 ;}; + + return GAA_OK; + break; + case GAAOPTID_index: + OK = 0; + GAA_TESTMOREARGS; + GAA_FILL(GAATMP_index.arg1, gaa_getint, GAATMP_index.size1); + gaa_index++; +#line 26 "srptool.gaa" +{ gaaval->index = GAATMP_index.arg1 ;}; + + return GAA_OK; + break; + case GAAOPTID_passwd: + OK = 0; + GAA_TESTMOREARGS; + GAA_FILL(GAATMP_passwd.arg1, gaa_getstr, GAATMP_passwd.size1); + gaa_index++; +#line 23 "srptool.gaa" +{ gaaval->passwd = GAATMP_passwd.arg1 ;}; + + return GAA_OK; + break; + case GAAOPTID_username: + OK = 0; + GAA_TESTMOREARGS; + GAA_FILL(GAATMP_username.arg1, gaa_getstr, GAATMP_username.size1); + gaa_index++; +#line 20 "srptool.gaa" +{ gaaval->username = GAATMP_username.arg1 ;}; + + return GAA_OK; + break; + case GAAOPTID_debug: + OK = 0; + GAA_TESTMOREARGS; + GAA_FILL(GAATMP_debug.arg1, gaa_getint, GAATMP_debug.size1); + gaa_index++; +#line 17 "srptool.gaa" +{ gaaval->debug = GAATMP_debug.arg1 ;}; + + return GAA_OK; + break; + +#line 413 "gaa.skel" + default: break; + } + return GAA_ERROR_UNKNOWN; +} + +int gaa(int argc, char **argv, gaainfo *gaaval) +{ + int tmp1, tmp2; + int i, j; + char *opt_list; + + GAAargv = argv; + GAAargc = argc; + + opt_list = (char*) gaa_malloc(GAA_NB_OPTION + 1); + + for(i = 0; i < GAA_NB_OPTION + 1; i++) + opt_list[i] = 0; + /* initialization */ + if(inited == 0) + { + +#line 43 "srptool.gaa" +{ gaaval->username=NULL; gaaval->passwd=NULL; gaaval->salt=0; + gaaval->create_conf=NULL; gaaval->passwd_conf=NULL; gaaval->verify = 0; + gaaval->index = 1; gaaval->debug = 0; ;}; + + } + inited = 1; +#line 438 "gaa.skel" + gaa_arg_used = NULL; + + if (argc > 0) { + gaa_arg_used = gaa_malloc(argc * sizeof(char)); + } + + for(i = 1; i < argc; i++) + gaa_arg_used[i] = 0; + for(i = 1; i < argc; i++) + { + if(gaa_arg_used[i] == 0) + { + j = 0; + tmp1 = gaa_is_an_argument(GAAargv[i]); + switch(tmp1) + { + case GAA_WORD_OPTION: + j++; + case GAA_LETTER_OPTION: + j++; + tmp2 = gaa_get_option_num(argv[i]+j, tmp1); + if(tmp2 == GAA_ERROR_NOMATCH) + { + printf("Invalid option '%s'\n", argv[i]+j); + return 0; + } + switch(gaa_try(tmp2, i+1, gaaval, opt_list)) + { + case GAA_ERROR_NOTENOUGH_ARGS: + printf("'%s': not enough arguments\n",gaa_current_option); + return 0; + case GAA_ERROR_INVALID_ARG: + printf("Invalid arguments\n"); + return 0; + case GAA_OK: + break; + default: + printf("Unknown error\n"); + } + gaa_arg_used[i] = 1; + break; + case GAA_MULTIPLE_OPTION: + for(j = 1; j < strlen(argv[i]); j++) + { + tmp2 = gaa_get_option_num(argv[i]+j, tmp1); + if(tmp2 == GAA_ERROR_NOMATCH) + { + printf("Invalid option '%c'\n", *(argv[i]+j)); + return 0; + } + switch(gaa_try(tmp2, i+1, gaaval, opt_list)) + { + case GAA_ERROR_NOTENOUGH_ARGS: + printf("'%s': not enough arguments\n",gaa_current_option); + return 0; + case GAA_ERROR_INVALID_ARG: + printf("Invalid arguments\n"); + return 0; + case GAA_OK: + break; + default: + printf("Unknown error\n"); + } + } + gaa_arg_used[i] = 1; + break; + default: break; + } + } + } +if(gaa_processing_file == 0) +{ + +#line 507 "gaa.skel" +#ifdef GAA_REST_EXISTS + switch(gaa_try(GAA_REST, 1, gaaval, opt_list)) + { + case GAA_ERROR_NOTENOUGH_ARGS: + printf("Rest: not enough arguments\n"); + return 0; + case GAA_ERROR_INVALID_ARG: + printf("Invalid arguments\n"); + return 0; + case GAA_OK: + break; + default: + printf("Unknown error\n"); + } +#endif +} + for(i = 1; i < argc; i++) + { + if(gaa_arg_used[i] == 0) + { + printf("Too many arguments\n"); + return 0; + } + } + free(gaa_arg_used); + free(opt_list); + return -1; +} + +struct gaastrnode +{ + char *str; + struct gaastrnode *next; +}; + +typedef struct gaastrnode gaa_str_node; + +static int gaa_internal_get_next_str(FILE *file, gaa_str_node *tmp_str, int argc) +{ + int pos_ini; + int a; + int i = 0, len = 0, newline = 0; + + if(argc == 1) { + newline = 1; + len = 2; + } + + a = fgetc( file); + if (a == EOF) return 0; + + while(a == ' ' || a == 9 || a == '\n') + { + if(a == '\n') + { + newline=1; + len = 2; + } + a = fgetc( file); + if (a == EOF) return 0; + } + + pos_ini = ftell(file) - 1; + + while(a != ' ' && a != 9 && a != '\n') + { + + len++; + a = fgetc( file); + if(a==EOF) return 0; //a = ' '; + } + + len += 1; + tmp_str->str = gaa_malloc((len) * sizeof(char)); + + if(newline == 1) + { + tmp_str->str[0] = '-'; + tmp_str->str[1] = '-'; + i = 2; + } + else + { + i = 0; + } + + fseek(file,pos_ini, SEEK_SET); + do + { + a = fgetc( file); + + if (a == EOF) { + i+=2; + break; + } + tmp_str->str[i] = a; + i++; + } + while(a != ' ' && a != 9 && a != '\n' && i < len); + + tmp_str->str[i - 1] = 0; + + fseek(file,- 1, SEEK_CUR); +/* printf("%d\n", ftell(file)); */ + + return -1; +} + +int gaa_file(const char *name, gaainfo *gaaval) +{ + gaa_str_node *first_str, **tmp_str, *tmp_str2; + int rval, i; + char **argv; + int argc = 0; + FILE *file; + + gaa_processing_file = 1; + + if((file = fopen(name, "r")) == NULL) + { + printf("Couldn't open '%s' configuration file for reading\n", name); + return 1; + } + + tmp_str = &first_str; + do + { + argc++; + *tmp_str = gaa_malloc(sizeof(gaa_str_node)); + + (*tmp_str)->str = NULL; + (*tmp_str)->next = NULL; + + rval = gaa_internal_get_next_str(file, *tmp_str, argc); + tmp_str = &((*tmp_str)->next); + } + while(rval == -1); + + if(rval == 1) + return 0; + + argv = gaa_malloc((1 + argc) * sizeof(char*)); + + tmp_str2 = first_str; + argv[0] = "cfg"; + for(i = 1; i < argc; i++) + { + argv[i] = tmp_str2->str; + tmp_str2 = tmp_str2->next; + } + + rval = gaa(argc, argv, gaaval); + gaa_processing_file = 0; + return rval; +} diff --git a/src/srptool-gaa.h b/src/srptool-gaa.h new file mode 100644 index 0000000000..585238dd06 --- /dev/null +++ b/src/srptool-gaa.h @@ -0,0 +1,47 @@ + +#line 104 "gaa.skel" +/* GAA HEADER */ +#ifndef GAA_HEADER_POKY +#define GAA_HEADER_POKY + +typedef struct _gaainfo gaainfo; + +struct _gaainfo +{ +#line 37 "srptool.gaa" + char *create_conf; +#line 34 "srptool.gaa" + char *passwd_conf; +#line 31 "srptool.gaa" + int verify; +#line 28 "srptool.gaa" + int salt; +#line 25 "srptool.gaa" + int index; +#line 22 "srptool.gaa" + char *passwd; +#line 19 "srptool.gaa" + char *username; +#line 16 "srptool.gaa" + int debug; + +#line 114 "gaa.skel" +}; + +#ifdef __cplusplus +extern "C" +{ +#endif + + int gaa(int argc, char *argv[], gaainfo *gaaval); + + void gaa_help(void); + + int gaa_file(const char *name, gaainfo *gaaval); + +#ifdef __cplusplus +} +#endif + + +#endif diff --git a/src/srptool.c b/src/srptool.c new file mode 100644 index 0000000000..187842450a --- /dev/null +++ b/src/srptool.c @@ -0,0 +1,731 @@ +/* + * Copyright (C) 2001, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010 + * Free Software Foundation, Inc. + * + * This file is part of GnuTLS. + * + * GnuTLS 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, either version 3 of the License, or + * (at your option) any later version. + * + * GnuTLS 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, see + * . + */ + +#include + +#include +#include +#include +#include +#include +#include +#include "../lib/random.h" /* for random */ + +#include +#include + +#ifndef _WIN32 +#include +#include +#else +#include +#endif + +/* Gnulib portability files. */ +#include +#include +#include +#include + +/* This may need some rewrite. A lot of stuff which should be here + * are in the library, which is not good. + */ + +int crypt_int (const char *username, const char *passwd, int salt, + char *tpasswd_conf, char *tpasswd, int uindex); +static int read_conf_values (gnutls_datum_t * g, gnutls_datum_t * n, + char *str); +static int _verify_passwd_int (const char *username, const char *passwd, + char *verifier, char *salt, + const gnutls_datum_t * g, + const gnutls_datum_t * n); + +static void +print_num (const char *msg, const gnutls_datum_t * num) +{ + unsigned int i; + + printf ("%s:\t", msg); + + for (i = 0; i < num->size; i++) + { + if (i != 0 && i % 12 == 0) + printf ("\n\t"); + else if (i != 0 && i != num->size) + printf (":"); + printf ("%.2x", num->data[i]); + } + printf ("\n\n"); + +} + +static int +generate_create_conf (char *tpasswd_conf) +{ + FILE *fd; + char line[5 * 1024]; + int index = 1; + gnutls_datum_t g, n; + gnutls_datum_t str_g, str_n; + + fd = fopen (tpasswd_conf, "w"); + if (fd == NULL) + { + fprintf (stderr, "Cannot open file '%s'\n", tpasswd_conf); + return -1; + } + + for (index = 1; index <= 3; index++) + { + + if (index == 1) + { + n = gnutls_srp_1024_group_prime; + g = gnutls_srp_1024_group_generator; + } + else if (index == 2) + { + n = gnutls_srp_1536_group_prime; + g = gnutls_srp_1536_group_generator; + } + else + { + n = gnutls_srp_2048_group_prime; + g = gnutls_srp_2048_group_generator; + } + + printf ("\nGroup %d, of %d bits:\n", index, n.size * 8); + print_num ("Generator", &g); + print_num ("Prime", &n); + + if (gnutls_srp_base64_encode_alloc (&n, &str_n) < 0) + { + fprintf (stderr, "Could not encode\n"); + return -1; + } + + if (gnutls_srp_base64_encode_alloc (&g, &str_g) < 0) + { + fprintf (stderr, "Could not encode\n"); + return -1; + } + + sprintf (line, "%d:%s:%s\n", index, str_n.data, str_g.data); + + gnutls_free (str_n.data); + gnutls_free (str_g.data); + + fwrite (line, 1, strlen (line), fd); + + } + + fclose (fd); + + return 0; + +} + +/* The format of a tpasswd file is: + * username:verifier:salt:index + * + * index is the index of the prime-generator pair in tpasswd.conf + */ +static int +_verify_passwd_int (const char *username, const char *passwd, + char *verifier, char *salt, + const gnutls_datum_t * g, const gnutls_datum_t * n) +{ + char _salt[1024]; + gnutls_datum_t tmp, raw_salt, new_verifier; + size_t salt_size; + char *pos; + + if (salt == NULL || verifier == NULL) + return -1; + + /* copy salt, and null terminate after the ':' */ + strcpy (_salt, salt); + pos = strchr (_salt, ':'); + if (pos != NULL) + *pos = 0; + + /* convert salt to binary. */ + tmp.data = _salt; + tmp.size = strlen (_salt); + + if (gnutls_srp_base64_decode_alloc (&tmp, &raw_salt) < 0) + { + fprintf (stderr, "Could not decode salt.\n"); + return -1; + } + + if (gnutls_srp_verifier + (username, passwd, &raw_salt, g, n, &new_verifier) < 0) + { + fprintf (stderr, "Could not make the verifier\n"); + return -1; + } + + free (raw_salt.data); + + /* encode the verifier into _salt */ + salt_size = sizeof (_salt); + memset (_salt, 0, salt_size); + if (gnutls_srp_base64_encode (&new_verifier, _salt, &salt_size) < 0) + { + fprintf (stderr, "Encoding error\n"); + return -1; + } + + free (new_verifier.data); + + if (strncmp (verifier, _salt, strlen (_salt)) == 0) + { + fprintf (stderr, "Password verified\n"); + return 0; + } + else + { + fprintf (stderr, "Password does NOT match\n"); + } + return -1; +} + +static int +filecopy (char *src, char *dst) +{ + FILE *fd, *fd2; + char line[5 * 1024]; + char *p; + + fd = fopen (dst, "w"); + if (fd == NULL) + { + fprintf (stderr, "Cannot open '%s' for write\n", dst); + return -1; + } + + fd2 = fopen (src, "r"); + if (fd2 == NULL) + { + /* empty file */ + fclose (fd); + return 0; + } + + line[sizeof (line) - 1] = 0; + do + { + p = fgets (line, sizeof (line) - 1, fd2); + if (p == NULL) + break; + + fputs (line, fd); + } + while (1); + + fclose (fd); + fclose (fd2); + + return 0; +} + +/* accepts password file */ +static int +find_strchr (char *username, char *file) +{ + FILE *fd; + char *pos; + char line[5 * 1024]; + unsigned int i; + + fd = fopen (file, "r"); + if (fd == NULL) + { + fprintf (stderr, "Cannot open file '%s'\n", file); + return -1; + } + + while (fgets (line, sizeof (line), fd) != NULL) + { + /* move to first ':' */ + i = 0; + while ((line[i] != ':') && (line[i] != '\0') && (i < sizeof (line))) + { + i++; + } + if (strncmp (username, line, MAX (i, strlen (username))) == 0) + { + /* find the index */ + pos = strrchr (line, ':'); + pos++; + fclose (fd); + return atoi (pos); + } + } + + fclose (fd); + return -1; +} + +/* Parses the tpasswd files, in order to verify the given + * username/password pair. + */ +static int +verify_passwd (char *conffile, char *tpasswd, char *username, + const char *passwd) +{ + FILE *fd; + char line[5 * 1024]; + unsigned int i; + gnutls_datum_t g, n; + int iindex; + char *p, *pos; + + iindex = find_strchr (username, tpasswd); + if (iindex == -1) + { + fprintf (stderr, "Cannot find '%s' in %s\n", username, tpasswd); + return -1; + } + + fd = fopen (conffile, "r"); + if (fd == NULL) + { + fprintf (stderr, "Cannot find %s\n", conffile); + return -1; + } + + do + { + p = fgets (line, sizeof (line) - 1, fd); + } + while (p != NULL && atoi (p) != iindex); + + if (p == NULL) + { + fprintf (stderr, "Cannot find entry in %s\n", conffile); + return -1; + } + line[sizeof (line) - 1] = 0; + + fclose (fd); + + if ((iindex = read_conf_values (&g, &n, line)) < 0) + { + fprintf (stderr, "Cannot parse conf file '%s'\n", conffile); + return -1; + } + + fd = fopen (tpasswd, "r"); + if (fd == NULL) + { + fprintf (stderr, "Cannot open file '%s'\n", tpasswd); + return -1; + } + + while (fgets (line, sizeof (line), fd) != NULL) + { + /* move to first ':' + * This is the actual verifier. + */ + i = 0; + while ((line[i] != ':') && (line[i] != '\0') && (i < sizeof (line))) + { + i++; + } + if (strncmp (username, line, MAX (i, strlen (username))) == 0) + { + char *verifier_pos, *salt_pos; + + pos = strchr (line, ':'); + fclose (fd); + if (pos == NULL) + { + fprintf (stderr, "Cannot parse conf file '%s'\n", conffile); + return -1; + } + pos++; + verifier_pos = pos; + + /* Move to the salt */ + pos = strchr (pos, ':'); + if (pos == NULL) + { + fprintf (stderr, "Cannot parse conf file '%s'\n", conffile); + return -1; + } + pos++; + salt_pos = pos; + + return _verify_passwd_int (username, passwd, + verifier_pos, salt_pos, &g, &n); + } + } + + fclose (fd); + return -1; + +} + +#define KPASSWD "/etc/tpasswd" +#define KPASSWD_CONF "/etc/tpasswd.conf" + +static void +tls_log_func (int level, const char *str) +{ + fprintf (stderr, "|<%d>| %s", level, str); +} + +int main (int argc, char **argv) +{ + gaainfo info; + const char *passwd; + int salt_size, ret; + struct passwd *pwd; + + set_program_name (argv[0]); + + if ((ret = gnutls_global_init ()) < 0) + { + fprintf (stderr, "global_init: %s\n", gnutls_strerror (ret)); + exit (1); + } + + umask (066); + + if (gaa (argc, argv, &info) != -1) + { + fprintf (stderr, "Error in the arguments.\n"); + return -1; + } + + gnutls_global_set_log_function (tls_log_func); + gnutls_global_set_log_level (info.debug); + + if (info.create_conf != NULL) + { + return generate_create_conf (info.create_conf); + } + + if (info.passwd == NULL) + info.passwd = (char *) KPASSWD; + if (info.passwd_conf == NULL) + info.passwd_conf = (char *) KPASSWD_CONF; + + if (info.username == NULL) + { +#ifndef _WIN32 + pwd = getpwuid (getuid ()); + + if (pwd == NULL) + { + fprintf (stderr, "No such user\n"); + return -1; + } + + info.username = pwd->pw_name; +#else + fprintf (stderr, "Please specify a user\n"); + return -1; +#endif + } + + salt_size = 16; + + passwd = getpass ("Enter password: "); + if (passwd == NULL) + { + fprintf (stderr, "Please specify a password\n"); + return -1; + } + +/* not ready yet */ + if (info.verify != 0) + { + return verify_passwd (info.passwd_conf, info.passwd, + info.username, passwd); + } + + + return crypt_int (info.username, passwd, salt_size, + info.passwd_conf, info.passwd, info.index); + +} + +static char * +_srp_crypt (const char *username, const char *passwd, int salt_size, + const gnutls_datum_t * g, const gnutls_datum_t * n) +{ + char salt[128]; + static char result[1024]; + gnutls_datum_t dat_salt, txt_salt; + gnutls_datum_t verifier, txt_verifier; + + if ((unsigned) salt_size > sizeof (salt)) + return NULL; + + /* generate the salt + */ + if (gnutls_rnd (GNUTLS_RND_NONCE, salt, salt_size) < 0) + { + fprintf (stderr, "Could not create nonce\n"); + return NULL; + } + + dat_salt.data = salt; + dat_salt.size = salt_size; + + if (gnutls_srp_verifier (username, passwd, &dat_salt, g, n, &verifier) < 0) + { + fprintf (stderr, "Error getting verifier\n"); + return NULL; + } + + /* base64 encode the verifier */ + if (gnutls_srp_base64_encode_alloc (&verifier, &txt_verifier) < 0) + { + fprintf (stderr, "Error encoding\n"); + free (verifier.data); + return NULL; + } + + free (verifier.data); + + if (gnutls_srp_base64_encode_alloc (&dat_salt, &txt_salt) < 0) + { + fprintf (stderr, "Error encoding\n"); + return NULL; + } + + sprintf (result, "%s:%s", txt_verifier.data, txt_salt.data); + free (txt_salt.data); + free (txt_verifier.data); + + return result; + +} + + +int +crypt_int (const char *username, const char *passwd, int salt_size, + char *tpasswd_conf, char *tpasswd, int uindex) +{ + FILE *fd; + char *cr; + gnutls_datum_t g, n; + char line[5 * 1024]; + char *p, *pp; + int iindex; + char tmpname[1024]; + + fd = fopen (tpasswd_conf, "r"); + if (fd == NULL) + { + fprintf (stderr, "Cannot find %s\n", tpasswd_conf); + return -1; + } + + do + { /* find the specified uindex in file */ + p = fgets (line, sizeof (line) - 1, fd); + iindex = atoi (p); + } + while (p != NULL && iindex != uindex); + + if (p == NULL) + { + fprintf (stderr, "Cannot find entry in %s\n", tpasswd_conf); + return -1; + } + line[sizeof (line) - 1] = 0; + + fclose (fd); + if ((iindex = read_conf_values (&g, &n, line)) < 0) + { + fprintf (stderr, "Cannot parse conf file '%s'\n", tpasswd_conf); + return -1; + } + + cr = _srp_crypt (username, passwd, salt_size, &g, &n); + if (cr == NULL) + { + fprintf (stderr, "Cannot _srp_crypt()...\n"); + return -1; + } + else + { + /* delete previous entry */ + struct stat st; + FILE *fd2; + int put; + + if (strlen (tpasswd) > sizeof (tmpname) + 5) + { + fprintf (stderr, "file '%s' is tooooo long\n", tpasswd); + return -1; + } + strcpy (tmpname, tpasswd); + strcat (tmpname, ".tmp"); + + if (stat (tmpname, &st) != -1) + { + fprintf (stderr, "file '%s' is locked\n", tpasswd); + return -1; + } + + if (filecopy (tpasswd, tmpname) != 0) + { + fprintf (stderr, "Cannot copy '%s' to '%s'\n", tpasswd, tmpname); + return -1; + } + + fd = fopen (tpasswd, "w"); + if (fd == NULL) + { + fprintf (stderr, "Cannot open '%s' for write\n", tpasswd); + remove (tmpname); + return -1; + } + + fd2 = fopen (tmpname, "r"); + if (fd2 == NULL) + { + fprintf (stderr, "Cannot open '%s' for read\n", tmpname); + remove (tmpname); + return -1; + } + + put = 0; + do + { + p = fgets (line, sizeof (line) - 1, fd2); + if (p == NULL) + break; + + pp = strchr (line, ':'); + if (pp == NULL) + continue; + + if (strncmp (p, username, + MAX (strlen (username), (unsigned int) (pp - p))) == 0) + { + put = 1; + fprintf (fd, "%s:%s:%u\n", username, cr, iindex); + } + else + { + fputs (line, fd); + } + } + while (1); + + if (put == 0) + { + fprintf (fd, "%s:%s:%u\n", username, cr, iindex); + } + + fclose (fd); + fclose (fd2); + + remove (tmpname); + + } + + + return 0; +} + + + +/* this function parses tpasswd.conf file. Format is: + * int(index):base64(n):base64(g) + */ +static int +read_conf_values (gnutls_datum_t * g, gnutls_datum_t * n, char *str) +{ + char *p; + int len; + int index, ret; + gnutls_datum_t dat; + + index = atoi (str); + + p = strrchr (str, ':'); /* we have g */ + if (p == NULL) + { + return -1; + } + + *p = '\0'; + p++; + + /* read the generator */ + len = strlen (p); + if (p[len - 1] == '\n') + len--; + + dat.data = p; + dat.size = len; + ret = gnutls_srp_base64_decode_alloc (&dat, g); + + if (ret < 0) + { + fprintf (stderr, "Decoding error\n"); + return -1; + } + + /* now go for n - modulo */ + p = strrchr (str, ':'); /* we have n */ + if (p == NULL) + { + return -1; + } + + *p = '\0'; + p++; + + dat.data = p; + dat.size = strlen (p); + + ret = gnutls_srp_base64_decode_alloc (&dat, n); + + if (ret < 0) + { + fprintf (stderr, "Decoding error\n"); + free (g->data); + return -1; + } + + return index; +} + +extern void srptool_version (void); + +void +srptool_version (void) +{ + const char *p = PACKAGE_NAME; + if (strcmp (gnutls_check_version (NULL), PACKAGE_VERSION) != 0) + p = PACKAGE_STRING; + version_etc (stdout, "srptool", p, gnutls_check_version (NULL), + "Nikos Mavrogiannopoulos", (char *) NULL); +} diff --git a/src/srptool.gaa b/src/srptool.gaa new file mode 100644 index 0000000000..0026b0a887 --- /dev/null +++ b/src/srptool.gaa @@ -0,0 +1,45 @@ +#{ + +/* C declarations */ + +#include +#ifdef _WIN32 +# include +#endif + +void srptool_version(void); + +#} + +helpnode "Srptool help\nUsage : srptool [options]" + +#int debug; +option (d, debug) INT "integer" { $debug = $1 } "Enable debugging" + +#char *username; +option (u,username) STR "username" { $username = $1 } "specify username." + +#char *passwd; +option (p, passwd) STR "FILE" { $passwd = $1 } "specify a password file." + +#int index; +option (i, index) INT "INDEX" { $index = $1 } "specify the index of the group parameters in tpasswd.conf to use." + +#int salt; +option (s, salt) INT "SALT" { $salt = $1 } "specify salt size for crypt algorithm." + +#int verify; +option (verify) { $verify = 1 } "just verify password." + +#char *passwd_conf; +option (c, passwd-conf) STR "FILE" { $passwd_conf = $1 } "specify a password conf file." + +#char *create_conf; +option ( create-conf) STR "FILE" { $create_conf = $1 } "Generate a tpasswd.conf file." + +option (v, version) { srptool_version(); exit(0); } "prints the program's version number" +option (h, help) { gaa_help(); exit(0); } "shows this help text" + +init { $username=NULL; $passwd=NULL; $salt=0; + $create_conf=NULL; $passwd_conf=NULL; $verify = 0; + $index = 1; $debug = 0; } -- cgit v1.2.1