*** empty log message ***
[gnutls.git] / src / crypt-gaa.c
blob78e3a419e65675a587f092017ea1c27ee9cb4fe5
1 #include <stdio.h>
2 #include <string.h>
4 #ifndef GAA_NO_WIN32
5 #if defined(_WIN32) || defined(__WIN32__) || defined(WIN32) || defined(WINDOWS)
6 #define GAA_WIN32
7 #endif
8 #endif
10 void __gaa_helpsingle(char short_name, char *name,
11 char *arg_desc, char *opt_help)
13 int col1, col3, col4, tabsize = 3, curr;
14 int i;
16 col1 = 5; /* Default values */
17 col3 = 30;
18 col4 = 70;
20 curr = 0;
21 for(i = 0; i < col1; i++)
23 printf(" ");
24 curr++;
26 if(short_name)
28 if(name && *name)
30 printf("-%c, ", short_name);
31 curr += 4;
33 else
35 printf("-%c ", short_name);
36 curr += 3;
39 if(name && *name)
41 printf("--%s ", name);
42 curr += 3 + strlen(name);
44 if(arg_desc && *arg_desc)
46 printf("%s ", arg_desc);
47 curr += 1 + strlen(arg_desc);
49 if(curr >= col3)
51 printf("\n");
52 curr = 0;
54 if(opt_help) /* let's print the option's help body */
56 const char *str = opt_help;
57 while(*str)
59 while(curr < col3)
61 printf(" ");
62 curr++;
64 switch(*str)
66 case '\n':
67 printf("\n");
68 curr = 0;
69 break;
70 case '\t':
73 printf(" ");
74 curr++;
76 while((curr - col3) % tabsize != 0 && curr < col4);
77 case ' ':
78 if(*str == ' ')
80 curr++;
81 printf(" ");
83 for(i = 1; str[i] && str[i] != ' ' && str[i] != '\n'
84 && str[i] != '\t'; i++);
85 if(curr + i - 1 >= col4)
86 curr = col4;
87 break;
88 default:
89 printf("%c", *str);
90 curr++;
92 if(curr >= col4)
94 printf("\n");
95 curr = 0;
97 str++;
100 printf("\n");
103 void gaa_help()
105 printf("Srpcrypt help\nUsage : srpcrypt [options]""\n");
106 __gaa_helpsingle('u', "username", """username"" ", "specify username.");
107 __gaa_helpsingle('p', "passwd", """FILE"" ", "specify a password file.");
108 __gaa_helpsingle('i', "index", """INDEX"" ", "specify the index of the parameters in tpasswd.conf to use.");
109 __gaa_helpsingle('c', "crypt", """CRYPT"" ", "specify crypt algorithm (bcrypt/srpsha).");
110 __gaa_helpsingle('s', "salt", """SALT"" ", "specify salt/cost size for crypt algorithm.");
111 __gaa_helpsingle(0, "verify", "", "just verify password.");
112 __gaa_helpsingle(0, "passwd_conf", """FILE"" ", "specify a password conf file.");
113 __gaa_helpsingle(0, "create_conf", """FILE"" ", "Generate a tpasswd.conf file.");
114 __gaa_helpsingle(0, "bits", """BITS"" ", "specify the number of bits for prime numbers (used only when create_conf option is specified).");
115 __gaa_helpsingle('h', "help", "", "shows this help text");
117 #line 100 "gaa.skel"
119 /* Copy of C area */
121 #line 104 "gaa.skel"
122 /* GAA HEADER */
123 #ifndef GAA_HEADER_POKY
124 #define GAA_HEADER_POKY
126 typedef struct _gaainfo gaainfo;
128 struct _gaainfo
130 #line 27 "crypt.gaa"
131 int bits;
132 #line 24 "crypt.gaa"
133 char *create_conf;
134 #line 21 "crypt.gaa"
135 char *passwd_conf;
136 #line 18 "crypt.gaa"
137 int verify;
138 #line 15 "crypt.gaa"
139 int salt;
140 #line 12 "crypt.gaa"
141 char *crypt;
142 #line 9 "crypt.gaa"
143 int index;
144 #line 6 "crypt.gaa"
145 char *passwd;
146 #line 3 "crypt.gaa"
147 char *username;
149 #line 114 "gaa.skel"
152 #ifdef __cplusplus
153 extern "C"
155 #endif
157 int gaa(int argc, char *argv[], gaainfo *gaaval);
159 void gaa_help();
161 int gaa_file(char *name, gaainfo *gaaval);
163 #ifdef __cplusplus
165 #endif
168 #endif
170 #line 135 "gaa.skel"
172 /* C declarations */
174 #define GAAERROR \
176 gaa_error = 1; \
177 return; \
180 char *gaa_current_option;
181 int gaa_error = 0;
183 /* Generated by gaa */
185 #include <string.h>
186 #include <stdlib.h>
189 #define GAA_OK -1
191 #define GAA_ERROR_NOMATCH 0
192 #define GAA_ERROR_NOTENOUGH_ARGS 1
193 #define GAA_ERROR_INVALID_ARG 2
194 #define GAA_ERROR_UNKNOWN 3
196 #define GAA_NOT_AN_OPTION 0
197 #define GAA_WORD_OPTION 1
198 #define GAA_LETTER_OPTION 2
199 #define GAA_MULTIPLE_OPTION 3
201 #define GAA_REST 0
202 #define GAA_NB_OPTION 10
203 #define GAAOPTID_help 1
204 #define GAAOPTID_bits 2
205 #define GAAOPTID_create_conf 3
206 #define GAAOPTID_passwd_conf 4
207 #define GAAOPTID_verify 5
208 #define GAAOPTID_salt 6
209 #define GAAOPTID_crypt 7
210 #define GAAOPTID_index 8
211 #define GAAOPTID_passwd 9
212 #define GAAOPTID_username 10
214 #line 168 "gaa.skel"
216 #define GAA_CHECK1STR(a,b) \
217 if(a[0] == str[0]) \
219 gaa_current_option = a; \
220 return b; \
223 #define GAA_CHECKSTR(a,b) \
224 if(strcmp(a,str) == 0) \
226 gaa_current_option = a; \
227 return b; \
230 #define GAA_TESTMOREARGS \
231 if(!OK) \
233 while((gaa_last_non_option != gaa_index) && (gaa_arg_used[gaa_index] == 1)) \
234 gaa_index++; \
235 if(gaa_last_non_option == gaa_index) \
236 return GAA_ERROR_NOTENOUGH_ARGS; \
239 #define GAA_TESTMOREOPTIONALARGS \
240 if(!OK) \
242 while((gaa_last_non_option != gaa_index) && (gaa_arg_used[gaa_index] == 1)) \
243 gaa_index++; \
244 if(gaa_last_non_option == gaa_index) \
245 OK = 1; \
248 #define GAA_FILL_2ARGS(target, func) \
249 target = func(GAAargv[gaa_index]); \
250 gaa_arg_used[gaa_index] = 1; \
251 if(gaa_error == 1) \
253 gaa_error = 0; \
254 return GAA_ERROR_INVALID_ARG; \
259 #define GAA_FILL(target, func, num) \
260 if(!OK) \
262 target = func(GAAargv[gaa_index]); \
263 gaa_arg_used[gaa_index] = 1; \
264 if(gaa_error == 1) \
266 gaa_error = 0; \
267 return GAA_ERROR_INVALID_ARG; \
269 num = 1; \
271 else \
273 num = 0; \
276 #define GAA_LIST_FILL(target, func, type ,num) \
277 if(!OK) \
279 num = 0; \
280 target = malloc((gaa_last_non_option - gaa_index) * sizeof(type)); \
281 for(; gaa_index < gaa_last_non_option; gaa_index++) \
283 if(gaa_arg_used[gaa_index] == 0) \
285 GAA_FILL_2ARGS(target[num], func); \
286 num++; \
289 if(num == 0) \
290 return GAA_ERROR_NOTENOUGH_ARGS; \
293 #define GAA_OPTIONALLIST_FILL(target, func, type ,num) \
294 if(!OK) \
296 num = 0; \
297 target = malloc((gaa_last_non_option - gaa_index) * sizeof(type)); \
298 for(; gaa_index < gaa_last_non_option; gaa_index++) \
300 if(gaa_arg_used[gaa_index] == 0) \
302 GAA_FILL_2ARGS(target[num], func); \
303 num++; \
308 #define GAA_OBLIGAT(str) \
309 k = 0; \
310 for(i = 0; i < strlen(str); i++) \
312 j = gaa_get_option_num(str + i, GAA_LETTER_OPTION); \
313 if(j == GAA_ERROR_NOMATCH) \
315 printf("Error : invalid 'obligat' set\n"); \
316 exit(-1); \
318 if(opt_list[j] == 1) \
319 k = 1; \
321 if(k == 0) \
323 if(strlen(str) == 1) \
324 printf("You must give the -%s option\n", str); \
325 else \
326 printf("You must give at least one option of '%s'\n", str); \
327 return 0; \
330 #define GAA_INCOMP(str) \
331 k = 0; \
332 for(i = 0; i < strlen(str); i++) \
334 j = gaa_get_option_num(str + i, GAA_LETTER_OPTION); \
335 if(j == GAA_ERROR_NOMATCH) \
337 printf("Error : invalid 'obligat' set\n"); \
338 exit(-1); \
340 if(opt_list[j] == 1) \
341 k++; \
343 if(k > 1) \
345 printf("The options '%s' are incompatible\n", str); \
346 return 0; \
350 char **GAAargv;
351 int GAAargc;
352 char *gaa_arg_used;
353 int gaa_processing_file = 0;
354 int inited = 0;
356 int gaa_getint(char *arg)
358 int tmp;
359 char a;
360 if(sscanf(arg, "%d%c", &tmp, &a) != 1)
362 printf("Option %s : '%s' isn't an integer\n", gaa_current_option, arg);
363 GAAERROR;
365 return tmp;
368 char gaa_getchar(char *arg)
370 if(strlen(arg) != 1)
372 printf("Option %s : '%s' isn't an character\n", gaa_current_option, arg);
373 GAAERROR;
375 return arg[0];
378 char* gaa_getstr(char *arg)
380 return arg;
382 float gaa_getfloat(char *arg)
384 float tmp;
385 char a;
386 if(sscanf(arg, "%f%c", &tmp, &a) != 1)
388 printf("Option %s : '%s' isn't a float number\n", gaa_current_option, arg);
389 GAAERROR;
391 return tmp;
393 /* option structures */
395 struct GAAOPTION_bits
397 int arg1;
398 int size1;
401 struct GAAOPTION_create_conf
403 char* arg1;
404 int size1;
407 struct GAAOPTION_passwd_conf
409 char* arg1;
410 int size1;
413 struct GAAOPTION_salt
415 int arg1;
416 int size1;
419 struct GAAOPTION_crypt
421 char* arg1;
422 int size1;
425 struct GAAOPTION_index
427 int arg1;
428 int size1;
431 struct GAAOPTION_passwd
433 char* arg1;
434 int size1;
437 struct GAAOPTION_username
439 char* arg1;
440 int size1;
443 #line 349 "gaa.skel"
444 int gaa_is_an_argument(char *str)
446 #ifdef GAA_WIN32
447 if(str[0] == '/' && str[1] != 0)
448 return GAA_MULTIPLE_OPTION;
449 #endif
450 if(str[0] != '-')
451 return GAA_NOT_AN_OPTION;
452 if(str[1] == 0)
453 return GAA_NOT_AN_OPTION;
454 if(str[1] == '-')
456 if(str[2] != 0)
457 return GAA_WORD_OPTION;
458 else
459 return GAA_NOT_AN_OPTION;
461 if(str[2] == 0)
462 return GAA_LETTER_OPTION;
463 else
464 return GAA_MULTIPLE_OPTION;
467 int gaa_get_option_num(char *str, int status)
469 switch(status)
471 case GAA_LETTER_OPTION:
472 GAA_CHECK1STR("", GAAOPTID_bits);
473 GAA_CHECK1STR("", GAAOPTID_create_conf);
474 GAA_CHECK1STR("", GAAOPTID_passwd_conf);
475 GAA_CHECK1STR("s", GAAOPTID_salt);
476 GAA_CHECK1STR("c", GAAOPTID_crypt);
477 GAA_CHECK1STR("i", GAAOPTID_index);
478 GAA_CHECK1STR("p", GAAOPTID_passwd);
479 GAA_CHECK1STR("u", GAAOPTID_username);
480 case GAA_MULTIPLE_OPTION:
481 #line 375 "gaa.skel"
482 GAA_CHECK1STR("h", GAAOPTID_help);
483 GAA_CHECK1STR("", GAAOPTID_verify);
485 #line 277 "gaa.skel"
486 break;
487 case GAA_WORD_OPTION:
488 GAA_CHECKSTR("help", GAAOPTID_help);
489 GAA_CHECKSTR("bits", GAAOPTID_bits);
490 GAA_CHECKSTR("create_conf", GAAOPTID_create_conf);
491 GAA_CHECKSTR("passwd_conf", GAAOPTID_passwd_conf);
492 GAA_CHECKSTR("verify", GAAOPTID_verify);
493 GAA_CHECKSTR("salt", GAAOPTID_salt);
494 GAA_CHECKSTR("crypt", GAAOPTID_crypt);
495 GAA_CHECKSTR("index", GAAOPTID_index);
496 GAA_CHECKSTR("passwd", GAAOPTID_passwd);
497 GAA_CHECKSTR("username", GAAOPTID_username);
499 #line 281 "gaa.skel"
500 break;
501 default: break;
503 return GAA_ERROR_NOMATCH;
506 int gaa_try(int gaa_num, int gaa_index, gaainfo *gaaval, char *opt_list)
508 int OK = 0;
509 int gaa_last_non_option;
510 struct GAAOPTION_bits GAATMP_bits;
511 struct GAAOPTION_create_conf GAATMP_create_conf;
512 struct GAAOPTION_passwd_conf GAATMP_passwd_conf;
513 struct GAAOPTION_salt GAATMP_salt;
514 struct GAAOPTION_crypt GAATMP_crypt;
515 struct GAAOPTION_index GAATMP_index;
516 struct GAAOPTION_passwd GAATMP_passwd;
517 struct GAAOPTION_username GAATMP_username;
519 #line 393 "gaa.skel"
520 #ifdef GAA_REST_EXISTS
521 struct GAAREST GAAREST_tmp;
522 #endif
524 opt_list[gaa_num] = 1;
526 for(gaa_last_non_option = gaa_index;
527 (gaa_last_non_option != GAAargc) && (gaa_is_an_argument(GAAargv[gaa_last_non_option]) == GAA_NOT_AN_OPTION);
528 gaa_last_non_option++);
530 if(gaa_num == GAA_REST)
532 gaa_index = 1;
533 gaa_last_non_option = GAAargc;
536 switch(gaa_num)
538 case GAAOPTID_help:
539 OK = 0;
540 #line 30 "crypt.gaa"
541 { gaa_help(); exit(0); ;};
543 return GAA_OK;
544 break;
545 case GAAOPTID_bits:
546 OK = 0;
547 GAA_TESTMOREARGS;
548 GAA_FILL(GAATMP_bits.arg1, gaa_getint, GAATMP_bits.size1);
549 gaa_index++;
550 #line 28 "crypt.gaa"
551 { gaaval->bits = GAATMP_bits.arg1 ;};
553 return GAA_OK;
554 break;
555 case GAAOPTID_create_conf:
556 OK = 0;
557 GAA_TESTMOREARGS;
558 GAA_FILL(GAATMP_create_conf.arg1, gaa_getstr, GAATMP_create_conf.size1);
559 gaa_index++;
560 #line 25 "crypt.gaa"
561 { gaaval->create_conf = GAATMP_create_conf.arg1 ;};
563 return GAA_OK;
564 break;
565 case GAAOPTID_passwd_conf:
566 OK = 0;
567 GAA_TESTMOREARGS;
568 GAA_FILL(GAATMP_passwd_conf.arg1, gaa_getstr, GAATMP_passwd_conf.size1);
569 gaa_index++;
570 #line 22 "crypt.gaa"
571 { gaaval->passwd_conf = GAATMP_passwd_conf.arg1 ;};
573 return GAA_OK;
574 break;
575 case GAAOPTID_verify:
576 OK = 0;
577 #line 19 "crypt.gaa"
578 { gaaval->verify = 1 ;};
580 return GAA_OK;
581 break;
582 case GAAOPTID_salt:
583 OK = 0;
584 GAA_TESTMOREARGS;
585 GAA_FILL(GAATMP_salt.arg1, gaa_getint, GAATMP_salt.size1);
586 gaa_index++;
587 #line 16 "crypt.gaa"
588 { gaaval->salt = GAATMP_salt.arg1 ;};
590 return GAA_OK;
591 break;
592 case GAAOPTID_crypt:
593 OK = 0;
594 GAA_TESTMOREARGS;
595 GAA_FILL(GAATMP_crypt.arg1, gaa_getstr, GAATMP_crypt.size1);
596 gaa_index++;
597 #line 13 "crypt.gaa"
598 { gaaval->crypt = GAATMP_crypt.arg1 ;};
600 return GAA_OK;
601 break;
602 case GAAOPTID_index:
603 OK = 0;
604 GAA_TESTMOREARGS;
605 GAA_FILL(GAATMP_index.arg1, gaa_getint, GAATMP_index.size1);
606 gaa_index++;
607 #line 10 "crypt.gaa"
608 { gaaval->index = GAATMP_index.arg1 ;};
610 return GAA_OK;
611 break;
612 case GAAOPTID_passwd:
613 OK = 0;
614 GAA_TESTMOREARGS;
615 GAA_FILL(GAATMP_passwd.arg1, gaa_getstr, GAATMP_passwd.size1);
616 gaa_index++;
617 #line 7 "crypt.gaa"
618 { gaaval->passwd = GAATMP_passwd.arg1 ;};
620 return GAA_OK;
621 break;
622 case GAAOPTID_username:
623 OK = 0;
624 GAA_TESTMOREARGS;
625 GAA_FILL(GAATMP_username.arg1, gaa_getstr, GAATMP_username.size1);
626 gaa_index++;
627 #line 4 "crypt.gaa"
628 { gaaval->username = GAATMP_username.arg1 ;};
630 return GAA_OK;
631 break;
633 #line 413 "gaa.skel"
634 default: break;
636 return GAA_ERROR_UNKNOWN;
639 int gaa(int argc, char **argv, gaainfo *gaaval)
641 int tmp1, tmp2;
642 int i, j;
643 char *opt_list;
645 GAAargv = argv;
646 GAAargc = argc;
648 opt_list = (char*) malloc(GAA_NB_OPTION + 1);
649 for(i = 0; i < GAA_NB_OPTION + 1; i++)
650 opt_list[i] = 0;
651 /* initialization */
652 if(inited == 0)
655 #line 32 "crypt.gaa"
656 { gaaval->username=NULL; gaaval->passwd=NULL; gaaval->crypt=NULL; gaaval->salt=0;
657 gaaval->create_conf=NULL; gaaval->passwd_conf=NULL; gaaval->verify = 0; gaaval->bits=1040;
658 gaaval->index = 1; ;};
661 inited = 1;
662 #line 438 "gaa.skel"
663 gaa_arg_used = malloc(argc * sizeof(char));
664 for(i = 1; i < argc; i++)
665 gaa_arg_used[i] = 0;
666 for(i = 1; i < argc; i++)
668 if(gaa_arg_used[i] == 0)
670 j = 0;
671 tmp1 = gaa_is_an_argument(GAAargv[i]);
672 switch(tmp1)
674 case GAA_WORD_OPTION:
675 j++;
676 case GAA_LETTER_OPTION:
677 j++;
678 tmp2 = gaa_get_option_num(argv[i]+j, tmp1);
679 if(tmp2 == GAA_ERROR_NOMATCH)
681 printf("Invalid option '%s'\n", argv[i]+j);
682 return 0;
684 switch(gaa_try(tmp2, i+1, gaaval, opt_list))
686 case GAA_ERROR_NOTENOUGH_ARGS:
687 printf("'%s' : not enough arguments\n",gaa_current_option);
688 return 0;
689 case GAA_ERROR_INVALID_ARG:
690 printf("Invalid arguments\n");
691 return 0;
692 case GAA_OK:
693 break;
694 default:
695 printf("Unknown error\n");
697 gaa_arg_used[i] = 1;
698 break;
699 case GAA_MULTIPLE_OPTION:
700 for(j = 1; j < strlen(argv[i]); j++)
702 tmp2 = gaa_get_option_num(argv[i]+j, tmp1);
703 if(tmp2 == GAA_ERROR_NOMATCH)
705 printf("Invalid option '%c'\n", *(argv[i]+j));
706 return 0;
708 switch(gaa_try(tmp2, i+1, gaaval, opt_list))
710 case GAA_ERROR_NOTENOUGH_ARGS:
711 printf("'%s' : not enough arguments\n",gaa_current_option);
712 return 0;
713 case GAA_ERROR_INVALID_ARG:
714 printf("Invalid arguments\n");
715 return 0;
716 case GAA_OK:
717 break;
718 default:
719 printf("Unknown error\n");
722 gaa_arg_used[i] = 1;
723 break;
724 default: break;
728 if(gaa_processing_file == 0)
731 #line 507 "gaa.skel"
732 #ifdef GAA_REST_EXISTS
733 switch(gaa_try(GAA_REST, 1, gaaval, opt_list))
735 case GAA_ERROR_NOTENOUGH_ARGS:
736 printf("Rest : not enough arguments\n");
737 return 0;
738 case GAA_ERROR_INVALID_ARG:
739 printf("Invalid arguments\n");
740 return 0;
741 case GAA_OK:
742 break;
743 default:
744 printf("Unknown error\n");
746 #endif
748 for(i = 1; i < argc; i++)
750 if(gaa_arg_used[i] == 0)
752 printf("Too many arguments\n");
753 return 0;
756 free(gaa_arg_used);
757 free(opt_list);
758 return -1;
761 struct gaastrnode
763 char *str;
764 struct gaastrnode *next;
767 typedef struct gaastrnode gaa_str_node;
769 int gaa_internal_get_next_str(FILE *file, gaa_str_node *tmp_str, int argc)
771 int pos_ini;
772 char a;
773 int i = 0, len = 0, newline = 0;
775 if(argc == 1)
776 newline = 1;
778 if(fscanf(file,"%c", &a) != 1) return 0;
780 while(a == ' ' || a == 9 || a == '\n')
782 if(a == '\n')
784 newline=1;
785 len = 2;
787 if(fscanf(file,"%c", &a) != 1) return 0;
790 pos_ini = ftell(file) - 1;
792 while(a != ' ' && a != 9 && a != '\n')
795 len++;
796 if(fscanf(file,"%c", &a) != 1) a = ' ';
799 tmp_str->str = malloc((len + 1) * sizeof(char));
800 if(newline == 1)
802 tmp_str->str[0] = '-';
803 tmp_str->str[1] = '-';
804 i = 2;
806 else
808 i = 0;
811 fseek(file,pos_ini, SEEK_SET);
814 if(fscanf(file, "%c", &a) != 1)
816 i+=2;
817 break;
819 tmp_str->str[i] = a;
820 i++;
822 while(a != ' ' && a != 9 && a != '\n');
824 tmp_str->str[i - 1] = 0;
826 fseek(file,- 1, SEEK_CUR);
828 return -1;
831 int gaa_file(char *name, gaainfo *gaaval)
833 gaa_str_node *first_str, **tmp_str, *tmp_str2;
834 int rval, i;
835 char **argv;
836 int argc = 0;
837 FILE *file;
839 gaa_processing_file = 1;
841 if((file = fopen(name, "r")) == NULL)
843 printf("Couldn't open '%s' configuration file for reading\n", name);
844 return 0;
847 tmp_str = &first_str;
850 argc++;
851 *tmp_str = malloc(sizeof(gaa_str_node));
852 (*tmp_str)->str = NULL;
853 (*tmp_str)->next = NULL;
855 rval = gaa_internal_get_next_str(file, *tmp_str, argc);
856 tmp_str = &((*tmp_str)->next);
858 while(rval == -1);
860 if(rval == 1)
861 return 0;
863 argv = malloc((1 + argc) * sizeof(char*));
864 tmp_str2 = first_str;
865 argv[0] = "cfg";
866 for(i = 1; i < argc; i++)
868 argv[i] = tmp_str2->str;
869 tmp_str2 = tmp_str2->next;
872 rval = gaa(argc, argv, gaaval);
873 gaa_processing_file = 0;
874 return rval;