Fix memory leak.
[gnutls.git] / src / crypt-gaa.c
blob3375cf998870e3ca77cfa10da00fb5f6e68fd8dd
1 /* File generated by GAA 1.6.6
2 */
3 #define GAA_NO_WIN32
4 #line 1 "crypt.gaa"
7 /* C declarations */
9 #include <config.h>
10 #ifdef _WIN32
11 # include <io.h>
12 #endif
14 void srptool_version(void);
16 #include <stdio.h>
17 #include <string.h>
18 #include <stdlib.h>
20 #ifndef GAA_NO_WIN32
21 #if defined(_WIN32) || defined(__WIN32__) || defined(WIN32) || defined(WINDOWS)
22 #define GAA_WIN32
23 #endif
24 #endif
26 static void* gaa_malloc( size_t size) {
27 void* ret;
28 ret = malloc(size);
29 if (ret==NULL) {
30 fprintf(stderr, "gaa: could not allocate memory");
31 exit(1);
33 return ret;
36 static void __gaa_helpsingle(char short_name, char *name,
37 char *arg_desc, char *opt_help)
39 int col1, col3, col4, tabsize = 3, curr;
40 int i;
42 col1 = 5; /* Default values */
43 col3 = 30;
44 col4 = 70;
46 curr = 0;
47 for(i = 0; i < col1; i++)
49 printf(" ");
50 curr++;
52 if(short_name)
54 if(name && *name)
56 printf("-%c, ", short_name);
57 curr += 4;
59 else
61 printf("-%c ", short_name);
62 curr += 3;
65 if(name && *name)
67 printf("--%s ", name);
68 curr += 3 + strlen(name);
70 if(arg_desc && *arg_desc)
72 printf("%s ", arg_desc);
73 curr += 1 + strlen(arg_desc);
75 if(curr >= col3)
77 printf("\n");
78 curr = 0;
80 if(opt_help) /* let's print the option's help body */
82 const char *str = opt_help;
83 while(*str)
85 while(curr < col3)
87 printf(" ");
88 curr++;
90 switch(*str)
92 case '\n':
93 printf("\n");
94 curr = 0;
95 break;
96 case '\t':
99 printf(" ");
100 curr++;
102 while((curr - col3) % tabsize != 0 && curr < col4);
103 case ' ':
104 if(*str == ' ')
106 curr++;
107 printf(" ");
109 for(i = 1; str[i] && str[i] != ' ' && str[i] != '\n'
110 && str[i] != '\t'; i++);
111 if(curr + i - 1 >= col4)
112 curr = col4;
113 break;
114 default:
115 printf("%c", *str);
116 curr++;
118 if(curr >= col4)
120 printf("\n");
121 curr = 0;
123 str++;
126 printf("\n");
129 void gaa_help(void)
131 printf("Srptool help\nUsage : srptool [options]\n");
132 __gaa_helpsingle('u', "username", "username ", "specify username.");
133 __gaa_helpsingle('p', "passwd", "FILE ", "specify a password file.");
134 __gaa_helpsingle('i', "index", "INDEX ", "specify the index of the group parameters in tpasswd.conf to use.");
135 __gaa_helpsingle('s', "salt", "SALT ", "specify salt size for crypt algorithm.");
136 __gaa_helpsingle(0, "verify", "", "just verify password.");
137 __gaa_helpsingle('c', "passwd-conf", "FILE ", "specify a password conf file.");
138 __gaa_helpsingle(0, "create-conf", "FILE ", "Generate a tpasswd.conf file.");
139 __gaa_helpsingle('v', "version", "", "prints the program's version number");
140 __gaa_helpsingle('h', "help", "", "shows this help text");
142 #line 100 "gaa.skel"
144 /* Copy of C area */
146 #line 104 "gaa.skel"
147 /* GAA HEADER */
148 #ifndef GAA_HEADER_POKY
149 #define GAA_HEADER_POKY
151 typedef struct _gaainfo gaainfo;
153 struct _gaainfo
155 #line 34 "crypt.gaa"
156 char *create_conf;
157 #line 31 "crypt.gaa"
158 char *passwd_conf;
159 #line 28 "crypt.gaa"
160 int verify;
161 #line 25 "crypt.gaa"
162 int salt;
163 #line 22 "crypt.gaa"
164 int index;
165 #line 19 "crypt.gaa"
166 char *passwd;
167 #line 16 "crypt.gaa"
168 char *username;
170 #line 114 "gaa.skel"
173 #ifdef __cplusplus
174 extern "C"
176 #endif
178 int gaa(int argc, char *argv[], gaainfo *gaaval);
180 void gaa_help(void);
182 int gaa_file(const char *name, gaainfo *gaaval);
184 #ifdef __cplusplus
186 #endif
189 #endif
191 #line 135 "gaa.skel"
193 /* C declarations */
195 #define GAAERROR(x) \
197 gaa_error = 1; \
198 return x; \
201 static char *gaa_current_option;
202 static int gaa_error = 0;
204 /* Generated by gaa */
206 #include <string.h>
207 #include <stdlib.h>
210 #define GAA_OK -1
212 #define GAA_ERROR_NOMATCH 0
213 #define GAA_ERROR_NOTENOUGH_ARGS 1
214 #define GAA_ERROR_INVALID_ARG 2
215 #define GAA_ERROR_UNKNOWN 3
217 #define GAA_NOT_AN_OPTION 0
218 #define GAA_WORD_OPTION 1
219 #define GAA_LETTER_OPTION 2
220 #define GAA_MULTIPLE_OPTION 3
222 #define GAA_REST 0
223 #define GAA_NB_OPTION 9
224 #define GAAOPTID_help 1
225 #define GAAOPTID_version 2
226 #define GAAOPTID_create_conf 3
227 #define GAAOPTID_passwd_conf 4
228 #define GAAOPTID_verify 5
229 #define GAAOPTID_salt 6
230 #define GAAOPTID_index 7
231 #define GAAOPTID_passwd 8
232 #define GAAOPTID_username 9
234 #line 168 "gaa.skel"
236 #define GAA_CHECK1STR(a,b) \
237 if(a[0] == str[0]) \
239 gaa_current_option = a; \
240 return b; \
243 #define GAA_CHECKSTR(a,b) \
244 if(strcmp(a,str) == 0) \
246 gaa_current_option = a; \
247 return b; \
250 #define GAA_TESTMOREARGS \
251 if(!OK) \
253 while((gaa_last_non_option != gaa_index) && (gaa_arg_used[gaa_index] == 1)) \
254 gaa_index++; \
255 if(gaa_last_non_option == gaa_index) \
256 return GAA_ERROR_NOTENOUGH_ARGS; \
259 #define GAA_TESTMOREOPTIONALARGS \
260 if(!OK) \
262 while((gaa_last_non_option != gaa_index) && (gaa_arg_used[gaa_index] == 1)) \
263 gaa_index++; \
264 if(gaa_last_non_option == gaa_index) \
265 OK = 1; \
268 #define GAA_FILL_2ARGS(target, func) \
269 target = func(GAAargv[gaa_index]); \
270 gaa_arg_used[gaa_index] = 1; \
271 if(gaa_error == 1) \
273 gaa_error = 0; \
274 return GAA_ERROR_INVALID_ARG; \
279 #define GAA_FILL(target, func, num) \
280 if(!OK) \
282 target = func(GAAargv[gaa_index]); \
283 gaa_arg_used[gaa_index] = 1; \
284 if(gaa_error == 1) \
286 gaa_error = 0; \
287 return GAA_ERROR_INVALID_ARG; \
289 num = 1; \
291 else \
293 num = 0; \
296 #define GAA_LIST_FILL(target, func, type ,num) \
297 if(!OK) \
299 num = 0; \
300 target = NULL; \
301 if ( gaa_last_non_option - gaa_index > 0) \
302 target = gaa_malloc((gaa_last_non_option - gaa_index) * sizeof(type));\
303 for(; gaa_index < gaa_last_non_option; gaa_index++) \
305 if(gaa_arg_used[gaa_index] == 0) \
307 GAA_FILL_2ARGS(target[num], func); \
308 num++; \
311 if(num == 0) \
312 return GAA_ERROR_NOTENOUGH_ARGS; \
315 #define GAA_OPTIONALLIST_FILL(target, func, type ,num) \
316 if(!OK) \
318 num = 0; \
319 target = NULL; \
320 if ( gaa_last_non_option - gaa_index > 0) \
321 target = gaa_malloc((gaa_last_non_option - gaa_index) * sizeof(type));\
322 for(; gaa_index < gaa_last_non_option; gaa_index++) \
324 if(gaa_arg_used[gaa_index] == 0) \
326 GAA_FILL_2ARGS(target[num], func); \
327 num++; \
332 #define GAA_OBLIGAT(str) \
333 k = 0; \
334 for(i = 0; i < strlen(str); i++) \
336 j = gaa_get_option_num(str + i, GAA_LETTER_OPTION); \
337 if(j == GAA_ERROR_NOMATCH) \
339 printf("Error: invalid 'obligat' set\n"); \
340 exit(-1); \
342 if(opt_list[j] == 1) \
343 k = 1; \
345 if(k == 0) \
347 if(strlen(str) == 1) \
348 printf("You must give the -%s option\n", str); \
349 else \
350 printf("You must give at least one option of '%s'\n", str); \
351 return 0; \
354 #define GAA_INCOMP(str) \
355 k = 0; \
356 for(i = 0; i < strlen(str); i++) \
358 j = gaa_get_option_num(str + i, GAA_LETTER_OPTION); \
359 if(j == GAA_ERROR_NOMATCH) \
361 printf("Error: invalid 'obligat' set\n"); \
362 exit(-1); \
364 if(opt_list[j] == 1) \
365 k++; \
367 if(k > 1) \
369 printf("The options '%s' are incompatible\n", str); \
370 return 0; \
374 static char **GAAargv;
375 static int GAAargc;
376 static char *gaa_arg_used;
377 static int gaa_processing_file = 0;
378 static int inited = 0;
380 static int gaa_getint(char *arg)
382 int tmp;
383 char a;
384 if(sscanf(arg, "%d%c", &tmp, &a) < 1)
386 printf("Option %s: '%s' isn't an integer\n", gaa_current_option, arg);
387 GAAERROR(-1);
389 return tmp;
392 static char gaa_getchar(char *arg)
394 if(strlen(arg) != 1)
396 printf("Option %s: '%s' isn't an character\n", gaa_current_option, arg);
397 GAAERROR(-1);
399 return arg[0];
402 static char* gaa_getstr(char *arg)
404 return arg;
406 static float gaa_getfloat(char *arg)
408 float tmp;
409 char a;
410 if(sscanf(arg, "%f%c", &tmp, &a) < 1)
412 printf("Option %s: '%s' isn't a float number\n", gaa_current_option, arg);
413 GAAERROR(-1);
415 return tmp;
417 /* option structures */
419 struct GAAOPTION_create_conf
421 char* arg1;
422 int size1;
425 struct GAAOPTION_passwd_conf
427 char* arg1;
428 int size1;
431 struct GAAOPTION_salt
433 int arg1;
434 int size1;
437 struct GAAOPTION_index
439 int arg1;
440 int size1;
443 struct GAAOPTION_passwd
445 char* arg1;
446 int size1;
449 struct GAAOPTION_username
451 char* arg1;
452 int size1;
455 #line 349 "gaa.skel"
456 static int gaa_is_an_argument(char *str)
458 #ifdef GAA_WIN32
459 if(str[0] == '/' && str[1] != 0)
460 return GAA_MULTIPLE_OPTION;
461 #endif
462 if(str[0] != '-')
463 return GAA_NOT_AN_OPTION;
464 if(str[1] == 0)
465 return GAA_NOT_AN_OPTION;
466 if(str[1] == '-')
468 if(str[2] != 0)
469 return GAA_WORD_OPTION;
470 else
471 return GAA_NOT_AN_OPTION;
473 if(str[2] == 0)
474 return GAA_LETTER_OPTION;
475 else
476 return GAA_MULTIPLE_OPTION;
479 static int gaa_get_option_num(char *str, int status)
481 switch(status)
483 case GAA_LETTER_OPTION:
484 GAA_CHECK1STR("", GAAOPTID_create_conf);
485 GAA_CHECK1STR("c", GAAOPTID_passwd_conf);
486 GAA_CHECK1STR("s", GAAOPTID_salt);
487 GAA_CHECK1STR("i", GAAOPTID_index);
488 GAA_CHECK1STR("p", GAAOPTID_passwd);
489 GAA_CHECK1STR("u", GAAOPTID_username);
490 case GAA_MULTIPLE_OPTION:
491 #line 375 "gaa.skel"
492 GAA_CHECK1STR("h", GAAOPTID_help);
493 GAA_CHECK1STR("v", GAAOPTID_version);
494 GAA_CHECK1STR("", GAAOPTID_verify);
496 #line 277 "gaa.skel"
497 break;
498 case GAA_WORD_OPTION:
499 GAA_CHECKSTR("help", GAAOPTID_help);
500 GAA_CHECKSTR("version", GAAOPTID_version);
501 GAA_CHECKSTR("create-conf", GAAOPTID_create_conf);
502 GAA_CHECKSTR("passwd-conf", GAAOPTID_passwd_conf);
503 GAA_CHECKSTR("verify", GAAOPTID_verify);
504 GAA_CHECKSTR("salt", GAAOPTID_salt);
505 GAA_CHECKSTR("index", GAAOPTID_index);
506 GAA_CHECKSTR("passwd", GAAOPTID_passwd);
507 GAA_CHECKSTR("username", GAAOPTID_username);
509 #line 281 "gaa.skel"
510 break;
511 default: break;
513 return GAA_ERROR_NOMATCH;
516 static int gaa_try(int gaa_num, int gaa_index, gaainfo *gaaval, char *opt_list)
518 int OK = 0;
519 int gaa_last_non_option;
520 struct GAAOPTION_create_conf GAATMP_create_conf;
521 struct GAAOPTION_passwd_conf GAATMP_passwd_conf;
522 struct GAAOPTION_salt GAATMP_salt;
523 struct GAAOPTION_index GAATMP_index;
524 struct GAAOPTION_passwd GAATMP_passwd;
525 struct GAAOPTION_username GAATMP_username;
527 #line 393 "gaa.skel"
528 #ifdef GAA_REST_EXISTS
529 struct GAAREST GAAREST_tmp;
530 #endif
532 opt_list[gaa_num] = 1;
534 for(gaa_last_non_option = gaa_index;
535 (gaa_last_non_option != GAAargc) && (gaa_is_an_argument(GAAargv[gaa_last_non_option]) == GAA_NOT_AN_OPTION);
536 gaa_last_non_option++);
538 if(gaa_num == GAA_REST)
540 gaa_index = 1;
541 gaa_last_non_option = GAAargc;
544 switch(gaa_num)
546 case GAAOPTID_help:
547 OK = 0;
548 #line 38 "crypt.gaa"
549 { gaa_help(); exit(0); ;};
551 return GAA_OK;
552 break;
553 case GAAOPTID_version:
554 OK = 0;
555 #line 37 "crypt.gaa"
556 { srptool_version(); exit(0); ;};
558 return GAA_OK;
559 break;
560 case GAAOPTID_create_conf:
561 OK = 0;
562 GAA_TESTMOREARGS;
563 GAA_FILL(GAATMP_create_conf.arg1, gaa_getstr, GAATMP_create_conf.size1);
564 gaa_index++;
565 #line 35 "crypt.gaa"
566 { gaaval->create_conf = GAATMP_create_conf.arg1 ;};
568 return GAA_OK;
569 break;
570 case GAAOPTID_passwd_conf:
571 OK = 0;
572 GAA_TESTMOREARGS;
573 GAA_FILL(GAATMP_passwd_conf.arg1, gaa_getstr, GAATMP_passwd_conf.size1);
574 gaa_index++;
575 #line 32 "crypt.gaa"
576 { gaaval->passwd_conf = GAATMP_passwd_conf.arg1 ;};
578 return GAA_OK;
579 break;
580 case GAAOPTID_verify:
581 OK = 0;
582 #line 29 "crypt.gaa"
583 { gaaval->verify = 1 ;};
585 return GAA_OK;
586 break;
587 case GAAOPTID_salt:
588 OK = 0;
589 GAA_TESTMOREARGS;
590 GAA_FILL(GAATMP_salt.arg1, gaa_getint, GAATMP_salt.size1);
591 gaa_index++;
592 #line 26 "crypt.gaa"
593 { gaaval->salt = GAATMP_salt.arg1 ;};
595 return GAA_OK;
596 break;
597 case GAAOPTID_index:
598 OK = 0;
599 GAA_TESTMOREARGS;
600 GAA_FILL(GAATMP_index.arg1, gaa_getint, GAATMP_index.size1);
601 gaa_index++;
602 #line 23 "crypt.gaa"
603 { gaaval->index = GAATMP_index.arg1 ;};
605 return GAA_OK;
606 break;
607 case GAAOPTID_passwd:
608 OK = 0;
609 GAA_TESTMOREARGS;
610 GAA_FILL(GAATMP_passwd.arg1, gaa_getstr, GAATMP_passwd.size1);
611 gaa_index++;
612 #line 20 "crypt.gaa"
613 { gaaval->passwd = GAATMP_passwd.arg1 ;};
615 return GAA_OK;
616 break;
617 case GAAOPTID_username:
618 OK = 0;
619 GAA_TESTMOREARGS;
620 GAA_FILL(GAATMP_username.arg1, gaa_getstr, GAATMP_username.size1);
621 gaa_index++;
622 #line 17 "crypt.gaa"
623 { gaaval->username = GAATMP_username.arg1 ;};
625 return GAA_OK;
626 break;
628 #line 413 "gaa.skel"
629 default: break;
631 return GAA_ERROR_UNKNOWN;
634 int gaa(int argc, char **argv, gaainfo *gaaval)
636 int tmp1, tmp2;
637 int i, j;
638 char *opt_list;
640 GAAargv = argv;
641 GAAargc = argc;
643 opt_list = (char*) gaa_malloc(GAA_NB_OPTION + 1);
645 for(i = 0; i < GAA_NB_OPTION + 1; i++)
646 opt_list[i] = 0;
647 /* initialization */
648 if(inited == 0)
651 #line 40 "crypt.gaa"
652 { gaaval->username=NULL; gaaval->passwd=NULL; gaaval->salt=0;
653 gaaval->create_conf=NULL; gaaval->passwd_conf=NULL; gaaval->verify = 0;
654 gaaval->index = 1; ;};
657 inited = 1;
658 #line 438 "gaa.skel"
659 gaa_arg_used = NULL;
661 if (argc > 0) {
662 gaa_arg_used = gaa_malloc(argc * sizeof(char));
665 for(i = 1; i < argc; i++)
666 gaa_arg_used[i] = 0;
667 for(i = 1; i < argc; i++)
669 if(gaa_arg_used[i] == 0)
671 j = 0;
672 tmp1 = gaa_is_an_argument(GAAargv[i]);
673 switch(tmp1)
675 case GAA_WORD_OPTION:
676 j++;
677 case GAA_LETTER_OPTION:
678 j++;
679 tmp2 = gaa_get_option_num(argv[i]+j, tmp1);
680 if(tmp2 == GAA_ERROR_NOMATCH)
682 printf("Invalid option '%s'\n", argv[i]+j);
683 return 0;
685 switch(gaa_try(tmp2, i+1, gaaval, opt_list))
687 case GAA_ERROR_NOTENOUGH_ARGS:
688 printf("'%s': not enough arguments\n",gaa_current_option);
689 return 0;
690 case GAA_ERROR_INVALID_ARG:
691 printf("Invalid arguments\n");
692 return 0;
693 case GAA_OK:
694 break;
695 default:
696 printf("Unknown error\n");
698 gaa_arg_used[i] = 1;
699 break;
700 case GAA_MULTIPLE_OPTION:
701 for(j = 1; j < strlen(argv[i]); j++)
703 tmp2 = gaa_get_option_num(argv[i]+j, tmp1);
704 if(tmp2 == GAA_ERROR_NOMATCH)
706 printf("Invalid option '%c'\n", *(argv[i]+j));
707 return 0;
709 switch(gaa_try(tmp2, i+1, gaaval, opt_list))
711 case GAA_ERROR_NOTENOUGH_ARGS:
712 printf("'%s': not enough arguments\n",gaa_current_option);
713 return 0;
714 case GAA_ERROR_INVALID_ARG:
715 printf("Invalid arguments\n");
716 return 0;
717 case GAA_OK:
718 break;
719 default:
720 printf("Unknown error\n");
723 gaa_arg_used[i] = 1;
724 break;
725 default: break;
729 if(gaa_processing_file == 0)
732 #line 507 "gaa.skel"
733 #ifdef GAA_REST_EXISTS
734 switch(gaa_try(GAA_REST, 1, gaaval, opt_list))
736 case GAA_ERROR_NOTENOUGH_ARGS:
737 printf("Rest: not enough arguments\n");
738 return 0;
739 case GAA_ERROR_INVALID_ARG:
740 printf("Invalid arguments\n");
741 return 0;
742 case GAA_OK:
743 break;
744 default:
745 printf("Unknown error\n");
747 #endif
749 for(i = 1; i < argc; i++)
751 if(gaa_arg_used[i] == 0)
753 printf("Too many arguments\n");
754 return 0;
757 free(gaa_arg_used);
758 free(opt_list);
759 return -1;
762 struct gaastrnode
764 char *str;
765 struct gaastrnode *next;
768 typedef struct gaastrnode gaa_str_node;
770 static int gaa_internal_get_next_str(FILE *file, gaa_str_node *tmp_str, int argc)
772 int pos_ini;
773 int a;
774 int i = 0, len = 0, newline = 0;
776 if(argc == 1) {
777 newline = 1;
778 len = 2;
781 a = fgetc( file);
782 if (a == EOF) return 0;
784 while(a == ' ' || a == 9 || a == '\n')
786 if(a == '\n')
788 newline=1;
789 len = 2;
791 a = fgetc( file);
792 if (a == EOF) return 0;
795 pos_ini = ftell(file) - 1;
797 while(a != ' ' && a != 9 && a != '\n')
800 len++;
801 a = fgetc( file);
802 if(a==EOF) return 0; //a = ' ';
805 len += 1;
806 tmp_str->str = gaa_malloc((len) * sizeof(char));
808 if(newline == 1)
810 tmp_str->str[0] = '-';
811 tmp_str->str[1] = '-';
812 i = 2;
814 else
816 i = 0;
819 fseek(file,pos_ini, SEEK_SET);
822 a = fgetc( file);
824 if (a == EOF) {
825 i+=2;
826 break;
828 tmp_str->str[i] = a;
829 i++;
831 while(a != ' ' && a != 9 && a != '\n' && i < len);
833 tmp_str->str[i - 1] = 0;
835 fseek(file,- 1, SEEK_CUR);
836 /* printf("%d\n", ftell(file)); */
838 return -1;
841 int gaa_file(const char *name, gaainfo *gaaval)
843 gaa_str_node *first_str, **tmp_str, *tmp_str2;
844 int rval, i;
845 char **argv;
846 int argc = 0;
847 FILE *file;
849 gaa_processing_file = 1;
851 if((file = fopen(name, "r")) == NULL)
853 printf("Couldn't open '%s' configuration file for reading\n", name);
854 return 1;
857 tmp_str = &first_str;
860 argc++;
861 *tmp_str = gaa_malloc(sizeof(gaa_str_node));
863 (*tmp_str)->str = NULL;
864 (*tmp_str)->next = NULL;
866 rval = gaa_internal_get_next_str(file, *tmp_str, argc);
867 tmp_str = &((*tmp_str)->next);
869 while(rval == -1);
871 if(rval == 1)
872 return 0;
874 argv = gaa_malloc((1 + argc) * sizeof(char*));
876 tmp_str2 = first_str;
877 argv[0] = "cfg";
878 for(i = 1; i < argc; i++)
880 argv[i] = tmp_str2->str;
881 tmp_str2 = tmp_str2->next;
884 rval = gaa(argc, argv, gaaval);
885 gaa_processing_file = 0;
886 return rval;