Generated.
[gnutls.git] / src / crypt-gaa.c
blob8ec3b033ed3269625d93e1853dfa11cf17d0653f
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;
393 static char* gaa_getstr(char *arg)
395 return arg;
398 /* option structures */
400 struct GAAOPTION_create_conf
402 char* arg1;
403 int size1;
406 struct GAAOPTION_passwd_conf
408 char* arg1;
409 int size1;
412 struct GAAOPTION_salt
414 int arg1;
415 int size1;
418 struct GAAOPTION_index
420 int arg1;
421 int size1;
424 struct GAAOPTION_passwd
426 char* arg1;
427 int size1;
430 struct GAAOPTION_username
432 char* arg1;
433 int size1;
436 #line 349 "gaa.skel"
437 static int gaa_is_an_argument(char *str)
439 #ifdef GAA_WIN32
440 if(str[0] == '/' && str[1] != 0)
441 return GAA_MULTIPLE_OPTION;
442 #endif
443 if(str[0] != '-')
444 return GAA_NOT_AN_OPTION;
445 if(str[1] == 0)
446 return GAA_NOT_AN_OPTION;
447 if(str[1] == '-')
449 if(str[2] != 0)
450 return GAA_WORD_OPTION;
451 else
452 return GAA_NOT_AN_OPTION;
454 if(str[2] == 0)
455 return GAA_LETTER_OPTION;
456 else
457 return GAA_MULTIPLE_OPTION;
460 static int gaa_get_option_num(char *str, int status)
462 switch(status)
464 case GAA_LETTER_OPTION:
465 GAA_CHECK1STR("", GAAOPTID_create_conf);
466 GAA_CHECK1STR("c", GAAOPTID_passwd_conf);
467 GAA_CHECK1STR("s", GAAOPTID_salt);
468 GAA_CHECK1STR("i", GAAOPTID_index);
469 GAA_CHECK1STR("p", GAAOPTID_passwd);
470 GAA_CHECK1STR("u", GAAOPTID_username);
471 case GAA_MULTIPLE_OPTION:
472 #line 375 "gaa.skel"
473 GAA_CHECK1STR("h", GAAOPTID_help);
474 GAA_CHECK1STR("v", GAAOPTID_version);
475 GAA_CHECK1STR("", GAAOPTID_verify);
477 #line 277 "gaa.skel"
478 break;
479 case GAA_WORD_OPTION:
480 GAA_CHECKSTR("help", GAAOPTID_help);
481 GAA_CHECKSTR("version", GAAOPTID_version);
482 GAA_CHECKSTR("create-conf", GAAOPTID_create_conf);
483 GAA_CHECKSTR("passwd-conf", GAAOPTID_passwd_conf);
484 GAA_CHECKSTR("verify", GAAOPTID_verify);
485 GAA_CHECKSTR("salt", GAAOPTID_salt);
486 GAA_CHECKSTR("index", GAAOPTID_index);
487 GAA_CHECKSTR("passwd", GAAOPTID_passwd);
488 GAA_CHECKSTR("username", GAAOPTID_username);
490 #line 281 "gaa.skel"
491 break;
492 default: break;
494 return GAA_ERROR_NOMATCH;
497 static int gaa_try(int gaa_num, int gaa_index, gaainfo *gaaval, char *opt_list)
499 int OK = 0;
500 int gaa_last_non_option;
501 struct GAAOPTION_create_conf GAATMP_create_conf;
502 struct GAAOPTION_passwd_conf GAATMP_passwd_conf;
503 struct GAAOPTION_salt GAATMP_salt;
504 struct GAAOPTION_index GAATMP_index;
505 struct GAAOPTION_passwd GAATMP_passwd;
506 struct GAAOPTION_username GAATMP_username;
508 #line 393 "gaa.skel"
509 #ifdef GAA_REST_EXISTS
510 struct GAAREST GAAREST_tmp;
511 #endif
513 opt_list[gaa_num] = 1;
515 for(gaa_last_non_option = gaa_index;
516 (gaa_last_non_option != GAAargc) && (gaa_is_an_argument(GAAargv[gaa_last_non_option]) == GAA_NOT_AN_OPTION);
517 gaa_last_non_option++);
519 if(gaa_num == GAA_REST)
521 gaa_index = 1;
522 gaa_last_non_option = GAAargc;
525 switch(gaa_num)
527 case GAAOPTID_help:
528 OK = 0;
529 #line 38 "crypt.gaa"
530 { gaa_help(); exit(0); ;};
532 return GAA_OK;
533 break;
534 case GAAOPTID_version:
535 OK = 0;
536 #line 37 "crypt.gaa"
537 { srptool_version(); exit(0); ;};
539 return GAA_OK;
540 break;
541 case GAAOPTID_create_conf:
542 OK = 0;
543 GAA_TESTMOREARGS;
544 GAA_FILL(GAATMP_create_conf.arg1, gaa_getstr, GAATMP_create_conf.size1);
545 gaa_index++;
546 #line 35 "crypt.gaa"
547 { gaaval->create_conf = GAATMP_create_conf.arg1 ;};
549 return GAA_OK;
550 break;
551 case GAAOPTID_passwd_conf:
552 OK = 0;
553 GAA_TESTMOREARGS;
554 GAA_FILL(GAATMP_passwd_conf.arg1, gaa_getstr, GAATMP_passwd_conf.size1);
555 gaa_index++;
556 #line 32 "crypt.gaa"
557 { gaaval->passwd_conf = GAATMP_passwd_conf.arg1 ;};
559 return GAA_OK;
560 break;
561 case GAAOPTID_verify:
562 OK = 0;
563 #line 29 "crypt.gaa"
564 { gaaval->verify = 1 ;};
566 return GAA_OK;
567 break;
568 case GAAOPTID_salt:
569 OK = 0;
570 GAA_TESTMOREARGS;
571 GAA_FILL(GAATMP_salt.arg1, gaa_getint, GAATMP_salt.size1);
572 gaa_index++;
573 #line 26 "crypt.gaa"
574 { gaaval->salt = GAATMP_salt.arg1 ;};
576 return GAA_OK;
577 break;
578 case GAAOPTID_index:
579 OK = 0;
580 GAA_TESTMOREARGS;
581 GAA_FILL(GAATMP_index.arg1, gaa_getint, GAATMP_index.size1);
582 gaa_index++;
583 #line 23 "crypt.gaa"
584 { gaaval->index = GAATMP_index.arg1 ;};
586 return GAA_OK;
587 break;
588 case GAAOPTID_passwd:
589 OK = 0;
590 GAA_TESTMOREARGS;
591 GAA_FILL(GAATMP_passwd.arg1, gaa_getstr, GAATMP_passwd.size1);
592 gaa_index++;
593 #line 20 "crypt.gaa"
594 { gaaval->passwd = GAATMP_passwd.arg1 ;};
596 return GAA_OK;
597 break;
598 case GAAOPTID_username:
599 OK = 0;
600 GAA_TESTMOREARGS;
601 GAA_FILL(GAATMP_username.arg1, gaa_getstr, GAATMP_username.size1);
602 gaa_index++;
603 #line 17 "crypt.gaa"
604 { gaaval->username = GAATMP_username.arg1 ;};
606 return GAA_OK;
607 break;
609 #line 413 "gaa.skel"
610 default: break;
612 return GAA_ERROR_UNKNOWN;
615 int gaa(int argc, char **argv, gaainfo *gaaval)
617 int tmp1, tmp2;
618 int l;
619 size_t i, j;
620 char *opt_list;
622 i = 0;
624 GAAargv = argv;
625 GAAargc = argc;
627 opt_list = (char*) gaa_malloc(GAA_NB_OPTION + 1);
629 for(l = 0; l < GAA_NB_OPTION + 1; l++)
630 opt_list[l] = 0;
631 /* initialization */
632 if(inited == 0)
635 #line 40 "crypt.gaa"
636 { gaaval->username=NULL; gaaval->passwd=NULL; gaaval->salt=0;
637 gaaval->create_conf=NULL; gaaval->passwd_conf=NULL; gaaval->verify = 0;
638 gaaval->index = 1; ;};
641 inited = 1;
642 #line 438 "gaa.skel"
643 gaa_arg_used = NULL;
645 if (argc > 0) {
646 gaa_arg_used = gaa_malloc(argc * sizeof(char));
649 for(l = 1; l < argc; l++)
650 gaa_arg_used[l] = 0;
651 for(l = 1; l < argc; l++)
653 if(gaa_arg_used[l] == 0)
655 j = 0;
656 tmp1 = gaa_is_an_argument(GAAargv[l]);
657 switch(tmp1)
659 case GAA_WORD_OPTION:
660 j++;
661 case GAA_LETTER_OPTION:
662 j++;
663 tmp2 = gaa_get_option_num(argv[l]+j, tmp1);
664 if(tmp2 == GAA_ERROR_NOMATCH)
666 printf("Invalid option '%s'\n", argv[l]+j);
667 return 0;
669 switch(gaa_try(tmp2, l+1, gaaval, opt_list))
671 case GAA_ERROR_NOTENOUGH_ARGS:
672 printf("'%s': not enough arguments\n",gaa_current_option);
673 return 0;
674 case GAA_ERROR_INVALID_ARG:
675 printf("Invalid arguments\n");
676 return 0;
677 case GAA_OK:
678 break;
679 default:
680 printf("Unknown error\n");
682 gaa_arg_used[l] = 1;
683 break;
684 case GAA_MULTIPLE_OPTION:
685 for(j = 1; j < strlen(argv[l]); j++)
687 tmp2 = gaa_get_option_num(argv[l]+j, tmp1);
688 if(tmp2 == GAA_ERROR_NOMATCH)
690 printf("Invalid option '%c'\n", *(argv[l]+j));
691 return 0;
693 switch(gaa_try(tmp2, l+1, gaaval, opt_list))
695 case GAA_ERROR_NOTENOUGH_ARGS:
696 printf("'%s': not enough arguments\n",gaa_current_option);
697 return 0;
698 case GAA_ERROR_INVALID_ARG:
699 printf("Invalid arguments\n");
700 return 0;
701 case GAA_OK:
702 break;
703 default:
704 printf("Unknown error\n");
707 gaa_arg_used[l] = 1;
708 break;
709 default: break;
713 if(gaa_processing_file == 0)
716 #line 507 "gaa.skel"
717 #ifdef GAA_REST_EXISTS
718 switch(gaa_try(GAA_REST, 1, gaaval, opt_list))
720 case GAA_ERROR_NOTENOUGH_ARGS:
721 printf("Rest: not enough arguments\n");
722 return 0;
723 case GAA_ERROR_INVALID_ARG:
724 printf("Invalid arguments\n");
725 return 0;
726 case GAA_OK:
727 break;
728 default:
729 printf("Unknown error\n");
731 #endif
733 for(l = 1; l < argc; l++)
735 if(gaa_arg_used[l] == 0)
737 printf("Too many arguments\n");
738 return 0;
741 free(gaa_arg_used);
742 free(opt_list);
743 return -1;
746 struct gaastrnode
748 char *str;
749 struct gaastrnode *next;
752 typedef struct gaastrnode gaa_str_node;
754 static int gaa_internal_get_next_str(FILE *file, gaa_str_node *tmp_str, int argc)
756 int pos_ini;
757 int a;
758 int i = 0, len = 0, newline = 0;
760 if(argc == 1) {
761 newline = 1;
762 len = 2;
765 a = fgetc( file);
766 if (a == EOF) return 0;
768 while(a == ' ' || a == 9 || a == '\n')
770 if(a == '\n')
772 newline=1;
773 len = 2;
775 a = fgetc( file);
776 if (a == EOF) return 0;
779 pos_ini = ftell(file) - 1;
781 while(a != ' ' && a != 9 && a != '\n')
784 len++;
785 a = fgetc( file);
786 if(a==EOF) return 0; /* a = ' '; */
789 len += 1;
790 tmp_str->str = gaa_malloc((len) * sizeof(char));
792 if(newline == 1)
794 tmp_str->str[0] = '-';
795 tmp_str->str[1] = '-';
796 i = 2;
798 else
800 i = 0;
803 fseek(file,pos_ini, SEEK_SET);
806 a = fgetc( file);
808 if (a == EOF) {
809 i+=2;
810 break;
812 tmp_str->str[i] = a;
813 i++;
815 while(a != ' ' && a != 9 && a != '\n' && i < len);
817 tmp_str->str[i - 1] = 0;
819 fseek(file,- 1, SEEK_CUR);
820 /* printf("%d\n", ftell(file)); */
822 return -1;
825 int gaa_file(const char *name, gaainfo *gaaval)
827 gaa_str_node *first_str, **tmp_str, *tmp_str2;
828 int rval, i;
829 char **argv;
830 int argc = 0;
831 FILE *file;
833 gaa_processing_file = 1;
835 if((file = fopen(name, "r")) == NULL)
837 printf("Couldn't open '%s' configuration file for reading\n", name);
838 return 1;
841 tmp_str = &first_str;
844 argc++;
845 *tmp_str = gaa_malloc(sizeof(gaa_str_node));
847 (*tmp_str)->str = NULL;
848 (*tmp_str)->next = NULL;
850 rval = gaa_internal_get_next_str(file, *tmp_str, argc);
851 tmp_str = &((*tmp_str)->next);
853 while(rval == -1);
855 if(rval == 1)
856 return 0;
858 argv = gaa_malloc((1 + argc) * sizeof(char*));
860 tmp_str2 = first_str;
861 argv[0] = "cfg";
862 for(i = 1; i < argc; i++)
864 argv[i] = tmp_str2->str;
865 tmp_str2 = tmp_str2->next;
868 rval = gaa(argc, argv, gaaval);
869 gaa_processing_file = 0;
870 return rval;