5 #if defined(_WIN32) || defined(__WIN32__) || defined(WIN32) || defined(WINDOWS)
10 void __gaa_helpsingle(char short_name
, char *name
,
11 char *arg_desc
, char *opt_help
)
13 int col1
, col3
, col4
, tabsize
= 3, curr
;
16 col1
= 5; /* Default values */
21 for(i
= 0; i
< col1
; i
++)
30 printf("-%c, ", short_name
);
35 printf("-%c ", short_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
);
54 if(opt_help
) /* let's print the option's help body */
56 const char *str
= opt_help
;
76 while((curr
- col3
) % tabsize
!= 0 && curr
< col4
);
83 for(i
= 1; str
[i
] && str
[i
] != ' ' && str
[i
] != '\n'
84 && str
[i
] != '\t'; i
++);
85 if(curr
+ i
- 1 >= col4
)
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");
123 #ifndef GAA_HEADER_POKY
124 #define GAA_HEADER_POKY
126 typedef struct _gaainfo gaainfo
;
157 int gaa(int argc
, char *argv
[], gaainfo
*gaaval
);
161 int gaa_file(char *name
, gaainfo
*gaaval
);
180 char *gaa_current_option
;
183 /* Generated by gaa */
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
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
216 #define GAA_CHECK1STR(a,b) \
219 gaa_current_option = a; \
223 #define GAA_CHECKSTR(a,b) \
224 if(strcmp(a,str) == 0) \
226 gaa_current_option = a; \
230 #define GAA_TESTMOREARGS \
233 while((gaa_last_non_option != gaa_index) && (gaa_arg_used[gaa_index] == 1)) \
235 if(gaa_last_non_option == gaa_index) \
236 return GAA_ERROR_NOTENOUGH_ARGS; \
239 #define GAA_TESTMOREOPTIONALARGS \
242 while((gaa_last_non_option != gaa_index) && (gaa_arg_used[gaa_index] == 1)) \
244 if(gaa_last_non_option == gaa_index) \
248 #define GAA_FILL_2ARGS(target, func) \
249 target = func(GAAargv[gaa_index]); \
250 gaa_arg_used[gaa_index] = 1; \
254 return GAA_ERROR_INVALID_ARG; \
259 #define GAA_FILL(target, func, num) \
262 target = func(GAAargv[gaa_index]); \
263 gaa_arg_used[gaa_index] = 1; \
267 return GAA_ERROR_INVALID_ARG; \
276 #define GAA_LIST_FILL(target, func, type ,num) \
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); \
290 return GAA_ERROR_NOTENOUGH_ARGS; \
293 #define GAA_OPTIONALLIST_FILL(target, func, type ,num) \
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); \
308 #define GAA_OBLIGAT(str) \
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"); \
318 if(opt_list[j] == 1) \
323 if(strlen(str) == 1) \
324 printf("You must give the -%s option\n", str); \
326 printf("You must give at least one option of '%s'\n", str); \
330 #define GAA_INCOMP(str) \
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"); \
340 if(opt_list[j] == 1) \
345 printf("The options '%s' are incompatible\n", str); \
353 int gaa_processing_file
= 0;
356 int gaa_getint(char *arg
)
360 if(sscanf(arg
, "%d%c", &tmp
, &a
) != 1)
362 printf("Option %s : '%s' isn't an integer\n", gaa_current_option
, arg
);
368 char gaa_getchar(char *arg
)
372 printf("Option %s : '%s' isn't an character\n", gaa_current_option
, arg
);
378 char* gaa_getstr(char *arg
)
382 float gaa_getfloat(char *arg
)
386 if(sscanf(arg
, "%f%c", &tmp
, &a
) != 1)
388 printf("Option %s : '%s' isn't a float number\n", gaa_current_option
, arg
);
393 /* option structures */
395 struct GAAOPTION_bits
401 struct GAAOPTION_create_conf
407 struct GAAOPTION_passwd_conf
413 struct GAAOPTION_salt
419 struct GAAOPTION_crypt
425 struct GAAOPTION_index
431 struct GAAOPTION_passwd
437 struct GAAOPTION_username
444 int gaa_is_an_argument(char *str
)
447 if(str
[0] == '/' && str
[1] != 0)
448 return GAA_MULTIPLE_OPTION
;
451 return GAA_NOT_AN_OPTION
;
453 return GAA_NOT_AN_OPTION
;
457 return GAA_WORD_OPTION
;
459 return GAA_NOT_AN_OPTION
;
462 return GAA_LETTER_OPTION
;
464 return GAA_MULTIPLE_OPTION
;
467 int gaa_get_option_num(char *str
, int 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
:
482 GAA_CHECK1STR("h", GAAOPTID_help
);
483 GAA_CHECK1STR("", GAAOPTID_verify
);
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
);
503 return GAA_ERROR_NOMATCH
;
506 int gaa_try(int gaa_num
, int gaa_index
, gaainfo
*gaaval
, char *opt_list
)
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
;
520 #ifdef GAA_REST_EXISTS
521 struct GAAREST GAAREST_tmp
;
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
)
533 gaa_last_non_option
= GAAargc
;
541 { gaa_help(); exit(0); ;};
548 GAA_FILL(GAATMP_bits
.arg1
, gaa_getint
, GAATMP_bits
.size1
);
551 { gaaval
->bits
= GAATMP_bits
.arg1
;};
555 case GAAOPTID_create_conf
:
558 GAA_FILL(GAATMP_create_conf
.arg1
, gaa_getstr
, GAATMP_create_conf
.size1
);
561 { gaaval
->create_conf
= GAATMP_create_conf
.arg1
;};
565 case GAAOPTID_passwd_conf
:
568 GAA_FILL(GAATMP_passwd_conf
.arg1
, gaa_getstr
, GAATMP_passwd_conf
.size1
);
571 { gaaval
->passwd_conf
= GAATMP_passwd_conf
.arg1
;};
575 case GAAOPTID_verify
:
578 { gaaval
->verify
= 1 ;};
585 GAA_FILL(GAATMP_salt
.arg1
, gaa_getint
, GAATMP_salt
.size1
);
588 { gaaval
->salt
= GAATMP_salt
.arg1
;};
595 GAA_FILL(GAATMP_crypt
.arg1
, gaa_getstr
, GAATMP_crypt
.size1
);
598 { gaaval
->crypt
= GAATMP_crypt
.arg1
;};
605 GAA_FILL(GAATMP_index
.arg1
, gaa_getint
, GAATMP_index
.size1
);
608 { gaaval
->index
= GAATMP_index
.arg1
;};
612 case GAAOPTID_passwd
:
615 GAA_FILL(GAATMP_passwd
.arg1
, gaa_getstr
, GAATMP_passwd
.size1
);
618 { gaaval
->passwd
= GAATMP_passwd
.arg1
;};
622 case GAAOPTID_username
:
625 GAA_FILL(GAATMP_username
.arg1
, gaa_getstr
, GAATMP_username
.size1
);
628 { gaaval
->username
= GAATMP_username
.arg1
;};
636 return GAA_ERROR_UNKNOWN
;
639 int gaa(int argc
, char **argv
, gaainfo
*gaaval
)
648 opt_list
= (char*) malloc(GAA_NB_OPTION
+ 1);
649 for(i
= 0; i
< GAA_NB_OPTION
+ 1; i
++)
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; ;};
663 gaa_arg_used
= malloc(argc
* sizeof(char));
664 for(i
= 1; i
< argc
; i
++)
666 for(i
= 1; i
< argc
; i
++)
668 if(gaa_arg_used
[i
] == 0)
671 tmp1
= gaa_is_an_argument(GAAargv
[i
]);
674 case GAA_WORD_OPTION
:
676 case GAA_LETTER_OPTION
:
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
);
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
);
689 case GAA_ERROR_INVALID_ARG
:
690 printf("Invalid arguments\n");
695 printf("Unknown error\n");
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
));
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
);
713 case GAA_ERROR_INVALID_ARG
:
714 printf("Invalid arguments\n");
719 printf("Unknown error\n");
728 if(gaa_processing_file
== 0)
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");
738 case GAA_ERROR_INVALID_ARG
:
739 printf("Invalid arguments\n");
744 printf("Unknown error\n");
748 for(i
= 1; i
< argc
; i
++)
750 if(gaa_arg_used
[i
] == 0)
752 printf("Too many arguments\n");
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
)
773 int i
= 0, len
= 0, newline
= 0;
778 if(fscanf(file
,"%c", &a
) != 1) return 0;
780 while(a
== ' ' || a
== 9 || a
== '\n')
787 if(fscanf(file
,"%c", &a
) != 1) return 0;
790 pos_ini
= ftell(file
) - 1;
792 while(a
!= ' ' && a
!= 9 && a
!= '\n')
796 if(fscanf(file
,"%c", &a
) != 1) a
= ' ';
799 tmp_str
->str
= malloc((len
+ 1) * sizeof(char));
802 tmp_str
->str
[0] = '-';
803 tmp_str
->str
[1] = '-';
811 fseek(file
,pos_ini
, SEEK_SET
);
814 if(fscanf(file
, "%c", &a
) != 1)
822 while(a
!= ' ' && a
!= 9 && a
!= '\n');
824 tmp_str
->str
[i
- 1] = 0;
826 fseek(file
,- 1, SEEK_CUR
);
831 int gaa_file(char *name
, gaainfo
*gaaval
)
833 gaa_str_node
*first_str
, **tmp_str
, *tmp_str2
;
839 gaa_processing_file
= 1;
841 if((file
= fopen(name
, "r")) == NULL
)
843 printf("Couldn't open '%s' configuration file for reading\n", name
);
847 tmp_str
= &first_str
;
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
);
863 argv
= malloc((1 + argc
) * sizeof(char*));
864 tmp_str2
= first_str
;
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;