1 /*
2   File autogenerated by gengetopt version 2.22.5
3   generated with the following command:
4   gengetopt --unamed-opts=CLIENT [SERVER] --no-handle-help --no-handle-error --no-handle-version --input ./shishi.ggo --file-name shishi_cmd
5 
6   The developers of gengetopt consider the fixed text that goes in all
7   gengetopt output files to be in the public domain:
8   we make no copyright claims on it.
9 */
10 
11 /* If we use autoconf.  */
12 #ifdef HAVE_CONFIG_H
13 #include "config.h"
14 #endif
15 
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <string.h>
19 
20 #ifndef FIX_UNUSED
21 #define FIX_UNUSED(X) (void) (X) /* avoid warnings for unused params */
22 #endif
23 
24 #include <getopt.h>
25 
26 #include "shishi_cmd.h"
27 
28 const char *gengetopt_args_info_purpose = "Shishi command line client.  If no command is given, Shishi\nacquire or list a valid ticket for you, identified by CLIENT (if\nempty, default username in default realm), for the service SERVER (if\nempty, ticket granting ticket for client realm).";
29 
30 const char *gengetopt_args_info_usage = "Usage: shishi [OPTIONS]... [CLIENT [SERVER]]...";
31 
32 const char *gengetopt_args_info_description = "";
33 
34 const char *gengetopt_args_info_help[] = {
35   "  -h, --help                    Print help and exit",
36   "  -V, --version                 Print version and exit",
37   "\nCommands:",
38   "  -d, --destroy                 Destroy tickets in local cache, limited by any \n                                  --client-name or --server-name.  \n                                  (default=off)",
39   "  -l, --list                    List tickets in local cache, limited by any \n                                  --client-name and --server-name.  \n                                  (default=off)",
40   "  -r, --renew                   Renew ticket.  Use --server-name to specify \n                                  ticket, default is the most recent renewable \n                                  ticket granting ticket for the default realm. \n                                   (default=off)",
41   "\nFlags:",
42   "      --forwardable             Get a forwardable ticket, i.e., one that can be \n                                  used to get forwarded tickets.  (default=off)",
43   "      --forwarded               Get a forwarded ticket.  (default=off)",
44   "      --proxiable               Get a proxiable ticket, i.e., one that can be \n                                  used to get proxy tickets.  (default=off)",
45   "      --proxy                   Get a proxy ticket.  (default=off)",
46   "      --renewable               Get a renewable ticket.  (default=off)",
47   "\nOptions:",
48   "      --client-name=NAME        Client name. Default is login username.",
49   "  -E, --encryption-type=ETYPE,[ETYPE...]\n                                Encryption types to use.  ETYPE is either \n                                  registered name or integer.  Valid values \n                                  include 'aes128', 'aes256', 'aes' (same as \n                                  'aes256'), '3des', 'des-md5', 'des-md4', \n                                  'des-crc', 'des' (same as 'des-md5'), and \n                                  'arcfour'.",
50   "  -e, --endtime=STRING          Specify when ticket validity should expire.  \n                                  The time syntax may be relative (to the start \n                                  time), such as '20 hours', or absolute, such \n                                  as '2001-02-03 04:05:06 CET'. The default is \n                                  8 hours after the start time.",
51   "      --realm=STRING            Set default realm.",
52   "      --renew-till=STRING       Specify renewable life of ticket.  Implies \n                                  --renewable.  Accepts same time syntax as \n                                  --endtime.  If --renewable is specified, the \n                                  default is 1 week after the start time.",
53   "      --server-name=NAME        Server name. Default is 'krbtgt/REALM' where \n                                  REALM is client realm.",
54   "  -s, --starttime=STRING        Specify when ticket should start to be valid.  \n                                  Accepts same time syntax as --endtime. The \n                                  default is to become valid immediately.",
55   "      --ticket-granter=NAME     Service name in ticket to use for \n                                  authenticating request. Only for TGS. \n                                  Defaults to 'krbtgt/REALM@REALM' where REALM \n                                  is client realm.",
56   "\nOther options:",
57   "      --configuration-file=FILE Read user configuration from FILE.",
58   "  -c, --ticket-file=FILE        Read tickets from FILE.",
59   "  -o, --library-options=STRING  Parse STRING as a configuration file statement.",
60   "  -q, --quiet                   Don't produce any diagnostic output.\n                                    (default=off)",
61   "      --system-configuration-file=FILE\n                                Read system configuration from FILE.",
62   "      --ticket-write-file=FILE  Write tickets from FILE.  Default is to write \n                                  them back to where they were read from.",
63   "  -v, --verbose                 Produce verbose output.\n                                  Use multiple times to increase amount of \n                                  information.",
64     0
65 };
66 
67 typedef enum {ARG_NO
68   , ARG_FLAG
69   , ARG_STRING
70 } cmdline_parser_arg_type;
71 
72 static
73 void clear_given (struct gengetopt_args_info *args_info);
74 static
75 void clear_args (struct gengetopt_args_info *args_info);
76 
77 static int
78 cmdline_parser_internal (int argc, char **argv, struct gengetopt_args_info *args_info,
79                         struct cmdline_parser_params *params, const char *additional_error);
80 
81 static int
82 cmdline_parser_required2 (struct gengetopt_args_info *args_info, const char *prog_name, const char *additional_error);
83 
84 static char *
85 gengetopt_strdup (const char *s);
86 
87 static
clear_given(struct gengetopt_args_info * args_info)88 void clear_given (struct gengetopt_args_info *args_info)
89 {
90   args_info->help_given = 0 ;
91   args_info->version_given = 0 ;
92   args_info->destroy_given = 0 ;
93   args_info->list_given = 0 ;
94   args_info->renew_given = 0 ;
95   args_info->forwardable_given = 0 ;
96   args_info->forwarded_given = 0 ;
97   args_info->proxiable_given = 0 ;
98   args_info->proxy_given = 0 ;
99   args_info->renewable_given = 0 ;
100   args_info->client_name_given = 0 ;
101   args_info->encryption_type_given = 0 ;
102   args_info->endtime_given = 0 ;
103   args_info->realm_given = 0 ;
104   args_info->renew_till_given = 0 ;
105   args_info->server_name_given = 0 ;
106   args_info->starttime_given = 0 ;
107   args_info->ticket_granter_given = 0 ;
108   args_info->configuration_file_given = 0 ;
109   args_info->ticket_file_given = 0 ;
110   args_info->library_options_given = 0 ;
111   args_info->quiet_given = 0 ;
112   args_info->system_configuration_file_given = 0 ;
113   args_info->ticket_write_file_given = 0 ;
114   args_info->verbose_given = 0 ;
115 }
116 
117 static
clear_args(struct gengetopt_args_info * args_info)118 void clear_args (struct gengetopt_args_info *args_info)
119 {
120   FIX_UNUSED (args_info);
121   args_info->destroy_flag = 0;
122   args_info->list_flag = 0;
123   args_info->renew_flag = 0;
124   args_info->forwardable_flag = 0;
125   args_info->forwarded_flag = 0;
126   args_info->proxiable_flag = 0;
127   args_info->proxy_flag = 0;
128   args_info->renewable_flag = 0;
129   args_info->client_name_arg = NULL;
130   args_info->client_name_orig = NULL;
131   args_info->encryption_type_arg = NULL;
132   args_info->encryption_type_orig = NULL;
133   args_info->endtime_arg = NULL;
134   args_info->endtime_orig = NULL;
135   args_info->realm_arg = NULL;
136   args_info->realm_orig = NULL;
137   args_info->renew_till_arg = NULL;
138   args_info->renew_till_orig = NULL;
139   args_info->server_name_arg = NULL;
140   args_info->server_name_orig = NULL;
141   args_info->starttime_arg = NULL;
142   args_info->starttime_orig = NULL;
143   args_info->ticket_granter_arg = NULL;
144   args_info->ticket_granter_orig = NULL;
145   args_info->configuration_file_arg = NULL;
146   args_info->configuration_file_orig = NULL;
147   args_info->ticket_file_arg = NULL;
148   args_info->ticket_file_orig = NULL;
149   args_info->library_options_arg = NULL;
150   args_info->library_options_orig = NULL;
151   args_info->quiet_flag = 0;
152   args_info->system_configuration_file_arg = NULL;
153   args_info->system_configuration_file_orig = NULL;
154   args_info->ticket_write_file_arg = NULL;
155   args_info->ticket_write_file_orig = NULL;
156 
157 }
158 
159 static
init_args_info(struct gengetopt_args_info * args_info)160 void init_args_info(struct gengetopt_args_info *args_info)
161 {
162 
163 
164   args_info->help_help = gengetopt_args_info_help[0] ;
165   args_info->version_help = gengetopt_args_info_help[1] ;
166   args_info->destroy_help = gengetopt_args_info_help[3] ;
167   args_info->list_help = gengetopt_args_info_help[4] ;
168   args_info->renew_help = gengetopt_args_info_help[5] ;
169   args_info->forwardable_help = gengetopt_args_info_help[7] ;
170   args_info->forwarded_help = gengetopt_args_info_help[8] ;
171   args_info->proxiable_help = gengetopt_args_info_help[9] ;
172   args_info->proxy_help = gengetopt_args_info_help[10] ;
173   args_info->renewable_help = gengetopt_args_info_help[11] ;
174   args_info->client_name_help = gengetopt_args_info_help[13] ;
175   args_info->encryption_type_help = gengetopt_args_info_help[14] ;
176   args_info->endtime_help = gengetopt_args_info_help[15] ;
177   args_info->realm_help = gengetopt_args_info_help[16] ;
178   args_info->renew_till_help = gengetopt_args_info_help[17] ;
179   args_info->server_name_help = gengetopt_args_info_help[18] ;
180   args_info->starttime_help = gengetopt_args_info_help[19] ;
181   args_info->ticket_granter_help = gengetopt_args_info_help[20] ;
182   args_info->configuration_file_help = gengetopt_args_info_help[22] ;
183   args_info->ticket_file_help = gengetopt_args_info_help[23] ;
184   args_info->library_options_help = gengetopt_args_info_help[24] ;
185   args_info->quiet_help = gengetopt_args_info_help[25] ;
186   args_info->system_configuration_file_help = gengetopt_args_info_help[26] ;
187   args_info->ticket_write_file_help = gengetopt_args_info_help[27] ;
188   args_info->verbose_help = gengetopt_args_info_help[28] ;
189   args_info->verbose_min = 0;
190   args_info->verbose_max = 0;
191 
192 }
193 
194 void
cmdline_parser_print_version(void)195 cmdline_parser_print_version (void)
196 {
197   printf ("%s %s\n",
198      (strlen(CMDLINE_PARSER_PACKAGE_NAME) ? CMDLINE_PARSER_PACKAGE_NAME : CMDLINE_PARSER_PACKAGE),
199      CMDLINE_PARSER_VERSION);
200 }
201 
print_help_common(void)202 static void print_help_common(void) {
203   cmdline_parser_print_version ();
204 
205   if (strlen(gengetopt_args_info_purpose) > 0)
206     printf("\n%s\n", gengetopt_args_info_purpose);
207 
208   if (strlen(gengetopt_args_info_usage) > 0)
209     printf("\n%s\n", gengetopt_args_info_usage);
210 
211   printf("\n");
212 
213   if (strlen(gengetopt_args_info_description) > 0)
214     printf("%s\n\n", gengetopt_args_info_description);
215 }
216 
217 void
cmdline_parser_print_help(void)218 cmdline_parser_print_help (void)
219 {
220   int i = 0;
221   print_help_common();
222   while (gengetopt_args_info_help[i])
223     printf("%s\n", gengetopt_args_info_help[i++]);
224 }
225 
226 void
cmdline_parser_init(struct gengetopt_args_info * args_info)227 cmdline_parser_init (struct gengetopt_args_info *args_info)
228 {
229   clear_given (args_info);
230   clear_args (args_info);
231   init_args_info (args_info);
232 
233   args_info->inputs = 0;
234   args_info->inputs_num = 0;
235 }
236 
237 void
cmdline_parser_params_init(struct cmdline_parser_params * params)238 cmdline_parser_params_init(struct cmdline_parser_params *params)
239 {
240   if (params)
241     {
242       params->override = 0;
243       params->initialize = 1;
244       params->check_required = 1;
245       params->check_ambiguity = 0;
246       params->print_errors = 1;
247     }
248 }
249 
250 struct cmdline_parser_params *
cmdline_parser_params_create(void)251 cmdline_parser_params_create(void)
252 {
253   struct cmdline_parser_params *params =
254     (struct cmdline_parser_params *)malloc(sizeof(struct cmdline_parser_params));
255   cmdline_parser_params_init(params);
256   return params;
257 }
258 
259 static void
free_string_field(char ** s)260 free_string_field (char **s)
261 {
262   if (*s)
263     {
264       free (*s);
265       *s = 0;
266     }
267 }
268 
269 
270 static void
cmdline_parser_release(struct gengetopt_args_info * args_info)271 cmdline_parser_release (struct gengetopt_args_info *args_info)
272 {
273   unsigned int i;
274   free_string_field (&(args_info->client_name_arg));
275   free_string_field (&(args_info->client_name_orig));
276   free_string_field (&(args_info->encryption_type_arg));
277   free_string_field (&(args_info->encryption_type_orig));
278   free_string_field (&(args_info->endtime_arg));
279   free_string_field (&(args_info->endtime_orig));
280   free_string_field (&(args_info->realm_arg));
281   free_string_field (&(args_info->realm_orig));
282   free_string_field (&(args_info->renew_till_arg));
283   free_string_field (&(args_info->renew_till_orig));
284   free_string_field (&(args_info->server_name_arg));
285   free_string_field (&(args_info->server_name_orig));
286   free_string_field (&(args_info->starttime_arg));
287   free_string_field (&(args_info->starttime_orig));
288   free_string_field (&(args_info->ticket_granter_arg));
289   free_string_field (&(args_info->ticket_granter_orig));
290   free_string_field (&(args_info->configuration_file_arg));
291   free_string_field (&(args_info->configuration_file_orig));
292   free_string_field (&(args_info->ticket_file_arg));
293   free_string_field (&(args_info->ticket_file_orig));
294   free_string_field (&(args_info->library_options_arg));
295   free_string_field (&(args_info->library_options_orig));
296   free_string_field (&(args_info->system_configuration_file_arg));
297   free_string_field (&(args_info->system_configuration_file_orig));
298   free_string_field (&(args_info->ticket_write_file_arg));
299   free_string_field (&(args_info->ticket_write_file_orig));
300 
301 
302   for (i = 0; i < args_info->inputs_num; ++i)
303     free (args_info->inputs [i]);
304 
305   if (args_info->inputs_num)
306     free (args_info->inputs);
307 
308   clear_given (args_info);
309 }
310 
311 
312 static void
write_into_file(FILE * outfile,const char * opt,const char * arg,const char * values[])313 write_into_file(FILE *outfile, const char *opt, const char *arg, const char *values[])
314 {
315   FIX_UNUSED (values);
316   if (arg) {
317     fprintf(outfile, "%s=\"%s\"\n", opt, arg);
318   } else {
319     fprintf(outfile, "%s\n", opt);
320   }
321 }
322 
323 static void
write_multiple_into_file(FILE * outfile,int len,const char * opt,char ** arg,const char * values[])324 write_multiple_into_file(FILE *outfile, int len, const char *opt, char **arg, const char *values[])
325 {
326   int i;
327 
328   for (i = 0; i < len; ++i)
329     write_into_file(outfile, opt, (arg ? arg[i] : 0), values);
330 }
331 
332 int
cmdline_parser_dump(FILE * outfile,struct gengetopt_args_info * args_info)333 cmdline_parser_dump(FILE *outfile, struct gengetopt_args_info *args_info)
334 {
335   int i = 0;
336 
337   if (!outfile)
338     {
339       fprintf (stderr, "%s: cannot dump options to stream\n", CMDLINE_PARSER_PACKAGE);
340       return EXIT_FAILURE;
341     }
342 
343   if (args_info->help_given)
344     write_into_file(outfile, "help", 0, 0 );
345   if (args_info->version_given)
346     write_into_file(outfile, "version", 0, 0 );
347   if (args_info->destroy_given)
348     write_into_file(outfile, "destroy", 0, 0 );
349   if (args_info->list_given)
350     write_into_file(outfile, "list", 0, 0 );
351   if (args_info->renew_given)
352     write_into_file(outfile, "renew", 0, 0 );
353   if (args_info->forwardable_given)
354     write_into_file(outfile, "forwardable", 0, 0 );
355   if (args_info->forwarded_given)
356     write_into_file(outfile, "forwarded", 0, 0 );
357   if (args_info->proxiable_given)
358     write_into_file(outfile, "proxiable", 0, 0 );
359   if (args_info->proxy_given)
360     write_into_file(outfile, "proxy", 0, 0 );
361   if (args_info->renewable_given)
362     write_into_file(outfile, "renewable", 0, 0 );
363   if (args_info->client_name_given)
364     write_into_file(outfile, "client-name", args_info->client_name_orig, 0);
365   if (args_info->encryption_type_given)
366     write_into_file(outfile, "encryption-type", args_info->encryption_type_orig, 0);
367   if (args_info->endtime_given)
368     write_into_file(outfile, "endtime", args_info->endtime_orig, 0);
369   if (args_info->realm_given)
370     write_into_file(outfile, "realm", args_info->realm_orig, 0);
371   if (args_info->renew_till_given)
372     write_into_file(outfile, "renew-till", args_info->renew_till_orig, 0);
373   if (args_info->server_name_given)
374     write_into_file(outfile, "server-name", args_info->server_name_orig, 0);
375   if (args_info->starttime_given)
376     write_into_file(outfile, "starttime", args_info->starttime_orig, 0);
377   if (args_info->ticket_granter_given)
378     write_into_file(outfile, "ticket-granter", args_info->ticket_granter_orig, 0);
379   if (args_info->configuration_file_given)
380     write_into_file(outfile, "configuration-file", args_info->configuration_file_orig, 0);
381   if (args_info->ticket_file_given)
382     write_into_file(outfile, "ticket-file", args_info->ticket_file_orig, 0);
383   if (args_info->library_options_given)
384     write_into_file(outfile, "library-options", args_info->library_options_orig, 0);
385   if (args_info->quiet_given)
386     write_into_file(outfile, "quiet", 0, 0 );
387   if (args_info->system_configuration_file_given)
388     write_into_file(outfile, "system-configuration-file", args_info->system_configuration_file_orig, 0);
389   if (args_info->ticket_write_file_given)
390     write_into_file(outfile, "ticket-write-file", args_info->ticket_write_file_orig, 0);
391   write_multiple_into_file(outfile, args_info->verbose_given, "verbose", 0, 0);
392 
393 
394   i = EXIT_SUCCESS;
395   return i;
396 }
397 
398 int
cmdline_parser_file_save(const char * filename,struct gengetopt_args_info * args_info)399 cmdline_parser_file_save(const char *filename, struct gengetopt_args_info *args_info)
400 {
401   FILE *outfile;
402   int i = 0;
403 
404   outfile = fopen(filename, "w");
405 
406   if (!outfile)
407     {
408       fprintf (stderr, "%s: cannot open file for writing: %s\n", CMDLINE_PARSER_PACKAGE, filename);
409       return EXIT_FAILURE;
410     }
411 
412   i = cmdline_parser_dump(outfile, args_info);
413   fclose (outfile);
414 
415   return i;
416 }
417 
418 void
cmdline_parser_free(struct gengetopt_args_info * args_info)419 cmdline_parser_free (struct gengetopt_args_info *args_info)
420 {
421   cmdline_parser_release (args_info);
422 }
423 
424 /** @brief replacement of strdup, which is not standard */
425 char *
gengetopt_strdup(const char * s)426 gengetopt_strdup (const char *s)
427 {
428   char *result = 0;
429   if (!s)
430     return result;
431 
432   result = (char*)malloc(strlen(s) + 1);
433   if (result == (char*)0)
434     return (char*)0;
435   strcpy(result, s);
436   return result;
437 }
438 
439 static int
440 check_multiple_option_occurrences(const char *prog_name, unsigned int option_given, unsigned int min, unsigned int max, const char *option_desc);
441 
442 int
check_multiple_option_occurrences(const char * prog_name,unsigned int option_given,unsigned int min,unsigned int max,const char * option_desc)443 check_multiple_option_occurrences(const char *prog_name, unsigned int option_given, unsigned int min, unsigned int max, const char *option_desc)
444 {
445   int error = 0;
446 
447   if (option_given && (min > 0 || max > 0))
448     {
449       if (min > 0 && max > 0)
450         {
451           if (min == max)
452             {
453               /* specific occurrences */
454               if (option_given != (unsigned int) min)
455                 {
456                   fprintf (stderr, "%s: %s option occurrences must be %d\n",
457                     prog_name, option_desc, min);
458                   error = 1;
459                 }
460             }
461           else if (option_given < (unsigned int) min
462                 || option_given > (unsigned int) max)
463             {
464               /* range occurrences */
465               fprintf (stderr, "%s: %s option occurrences must be between %d and %d\n",
466                 prog_name, option_desc, min, max);
467               error = 1;
468             }
469         }
470       else if (min > 0)
471         {
472           /* at least check */
473           if (option_given < min)
474             {
475               fprintf (stderr, "%s: %s option occurrences must be at least %d\n",
476                 prog_name, option_desc, min);
477               error = 1;
478             }
479         }
480       else if (max > 0)
481         {
482           /* at most check */
483           if (option_given > max)
484             {
485               fprintf (stderr, "%s: %s option occurrences must be at most %d\n",
486                 prog_name, option_desc, max);
487               error = 1;
488             }
489         }
490     }
491 
492   return error;
493 }
494 int
cmdline_parser(int argc,char ** argv,struct gengetopt_args_info * args_info)495 cmdline_parser (int argc, char **argv, struct gengetopt_args_info *args_info)
496 {
497   return cmdline_parser2 (argc, argv, args_info, 0, 1, 1);
498 }
499 
500 int
cmdline_parser_ext(int argc,char ** argv,struct gengetopt_args_info * args_info,struct cmdline_parser_params * params)501 cmdline_parser_ext (int argc, char **argv, struct gengetopt_args_info *args_info,
502                    struct cmdline_parser_params *params)
503 {
504   int result;
505   result = cmdline_parser_internal (argc, argv, args_info, params, 0);
506 
507   return result;
508 }
509 
510 int
cmdline_parser2(int argc,char ** argv,struct gengetopt_args_info * args_info,int override,int initialize,int check_required)511 cmdline_parser2 (int argc, char **argv, struct gengetopt_args_info *args_info, int override, int initialize, int check_required)
512 {
513   int result;
514   struct cmdline_parser_params params;
515 
516   params.override = override;
517   params.initialize = initialize;
518   params.check_required = check_required;
519   params.check_ambiguity = 0;
520   params.print_errors = 1;
521 
522   result = cmdline_parser_internal (argc, argv, args_info, &params, 0);
523 
524   return result;
525 }
526 
527 int
cmdline_parser_required(struct gengetopt_args_info * args_info,const char * prog_name)528 cmdline_parser_required (struct gengetopt_args_info *args_info, const char *prog_name)
529 {
530   int result = EXIT_SUCCESS;
531 
532   if (cmdline_parser_required2(args_info, prog_name, 0) > 0)
533     result = EXIT_FAILURE;
534 
535   return result;
536 }
537 
538 int
cmdline_parser_required2(struct gengetopt_args_info * args_info,const char * prog_name,const char * additional_error)539 cmdline_parser_required2 (struct gengetopt_args_info *args_info, const char *prog_name, const char *additional_error)
540 {
541   int error = 0;
542   FIX_UNUSED (additional_error);
543 
544   /* checks for required options */
545   if (check_multiple_option_occurrences(prog_name, args_info->verbose_given, args_info->verbose_min, args_info->verbose_max, "'--verbose' ('-v')"))
546      error = 1;
547 
548 
549   /* checks for dependences among options */
550 
551   return error;
552 }
553 
554 
555 static char *package_name = 0;
556 
557 /**
558  * @brief updates an option
559  * @param field the generic pointer to the field to update
560  * @param orig_field the pointer to the orig field
561  * @param field_given the pointer to the number of occurrence of this option
562  * @param prev_given the pointer to the number of occurrence already seen
563  * @param value the argument for this option (if null no arg was specified)
564  * @param possible_values the possible values for this option (if specified)
565  * @param default_value the default value (in case the option only accepts fixed values)
566  * @param arg_type the type of this option
567  * @param check_ambiguity @see cmdline_parser_params.check_ambiguity
568  * @param override @see cmdline_parser_params.override
569  * @param no_free whether to free a possible previous value
570  * @param multiple_option whether this is a multiple option
571  * @param long_opt the corresponding long option
572  * @param short_opt the corresponding short option (or '-' if none)
573  * @param additional_error possible further error specification
574  */
575 static
update_arg(void * field,char ** orig_field,unsigned int * field_given,unsigned int * prev_given,char * value,const char * possible_values[],const char * default_value,cmdline_parser_arg_type arg_type,int check_ambiguity,int override,int no_free,int multiple_option,const char * long_opt,char short_opt,const char * additional_error)576 int update_arg(void *field, char **orig_field,
577                unsigned int *field_given, unsigned int *prev_given,
578                char *value, const char *possible_values[],
579                const char *default_value,
580                cmdline_parser_arg_type arg_type,
581                int check_ambiguity, int override,
582                int no_free, int multiple_option,
583                const char *long_opt, char short_opt,
584                const char *additional_error)
585 {
586   char *stop_char = 0;
587   const char *val = value;
588   int found;
589   char **string_field;
590   FIX_UNUSED (field);
591 
592   stop_char = 0;
593   found = 0;
594 
595   if (!multiple_option && prev_given && (*prev_given || (check_ambiguity && *field_given)))
596     {
597       if (short_opt != '-')
598         fprintf (stderr, "%s: `--%s' (`-%c') option given more than once%s\n",
599                package_name, long_opt, short_opt,
600                (additional_error ? additional_error : ""));
601       else
602         fprintf (stderr, "%s: `--%s' option given more than once%s\n",
603                package_name, long_opt,
604                (additional_error ? additional_error : ""));
605       return 1; /* failure */
606     }
607 
608   FIX_UNUSED (default_value);
609 
610   if (field_given && *field_given && ! override)
611     return 0;
612   if (prev_given)
613     (*prev_given)++;
614   if (field_given)
615     (*field_given)++;
616   if (possible_values)
617     val = possible_values[found];
618 
619   switch(arg_type) {
620   case ARG_FLAG:
621     *((int *)field) = !*((int *)field);
622     break;
623   case ARG_STRING:
624     if (val) {
625       string_field = (char **)field;
626       if (!no_free && *string_field)
627         free (*string_field); /* free previous string */
628       *string_field = gengetopt_strdup (val);
629     }
630     break;
631   default:
632     break;
633   };
634 
635 
636   /* store the original value */
637   switch(arg_type) {
638   case ARG_NO:
639   case ARG_FLAG:
640     break;
641   default:
642     if (value && orig_field) {
643       if (no_free) {
644         *orig_field = value;
645       } else {
646         if (*orig_field)
647           free (*orig_field); /* free previous string */
648         *orig_field = gengetopt_strdup (value);
649       }
650     }
651   };
652 
653   return 0; /* OK */
654 }
655 
656 
657 int
cmdline_parser_internal(int argc,char ** argv,struct gengetopt_args_info * args_info,struct cmdline_parser_params * params,const char * additional_error)658 cmdline_parser_internal (
659   int argc, char **argv, struct gengetopt_args_info *args_info,
660                         struct cmdline_parser_params *params, const char *additional_error)
661 {
662   int c;	/* Character of the parsed option.  */
663 
664   int error = 0;
665   struct gengetopt_args_info local_args_info;
666 
667   int override;
668   int initialize;
669   int check_required;
670   int check_ambiguity;
671 
672   package_name = argv[0];
673 
674   override = params->override;
675   initialize = params->initialize;
676   check_required = params->check_required;
677   check_ambiguity = params->check_ambiguity;
678 
679   if (initialize)
680     cmdline_parser_init (args_info);
681 
682   cmdline_parser_init (&local_args_info);
683 
684   optarg = 0;
685   optind = 0;
686   opterr = params->print_errors;
687   optopt = '?';
688 
689   while (1)
690     {
691       int option_index = 0;
692 
693       static struct option long_options[] = {
694         { "help",	0, NULL, 'h' },
695         { "version",	0, NULL, 'V' },
696         { "destroy",	0, NULL, 'd' },
697         { "list",	0, NULL, 'l' },
698         { "renew",	0, NULL, 'r' },
699         { "forwardable",	0, NULL, 0 },
700         { "forwarded",	0, NULL, 0 },
701         { "proxiable",	0, NULL, 0 },
702         { "proxy",	0, NULL, 0 },
703         { "renewable",	0, NULL, 0 },
704         { "client-name",	1, NULL, 0 },
705         { "encryption-type",	1, NULL, 'E' },
706         { "endtime",	1, NULL, 'e' },
707         { "realm",	1, NULL, 0 },
708         { "renew-till",	1, NULL, 0 },
709         { "server-name",	1, NULL, 0 },
710         { "starttime",	1, NULL, 's' },
711         { "ticket-granter",	1, NULL, 0 },
712         { "configuration-file",	1, NULL, 0 },
713         { "ticket-file",	1, NULL, 'c' },
714         { "library-options",	1, NULL, 'o' },
715         { "quiet",	0, NULL, 'q' },
716         { "system-configuration-file",	1, NULL, 0 },
717         { "ticket-write-file",	1, NULL, 0 },
718         { "verbose",	0, NULL, 'v' },
719         { 0,  0, 0, 0 }
720       };
721 
722       c = getopt_long (argc, argv, "hVdlrE:e:s:c:o:qv", long_options, &option_index);
723 
724       if (c == -1) break;	/* Exit from `while (1)' loop.  */
725 
726       switch (c)
727         {
728         case 'h':	/* Print help and exit.  */
729 
730 
731           if (update_arg( 0 ,
732                0 , &(args_info->help_given),
733               &(local_args_info.help_given), optarg, 0, 0, ARG_NO,
734               check_ambiguity, override, 0, 0,
735               "help", 'h',
736               additional_error))
737             goto failure;
738           cmdline_parser_free (&local_args_info);
739           return 0;
740 
741           break;
742         case 'V':	/* Print version and exit.  */
743 
744 
745           if (update_arg( 0 ,
746                0 , &(args_info->version_given),
747               &(local_args_info.version_given), optarg, 0, 0, ARG_NO,
748               check_ambiguity, override, 0, 0,
749               "version", 'V',
750               additional_error))
751             goto failure;
752           cmdline_parser_free (&local_args_info);
753           return 0;
754 
755           break;
756         case 'd':	/* Destroy tickets in local cache, limited by any --client-name or --server-name..  */
757 
758 
759           if (update_arg((void *)&(args_info->destroy_flag), 0, &(args_info->destroy_given),
760               &(local_args_info.destroy_given), optarg, 0, 0, ARG_FLAG,
761               check_ambiguity, override, 1, 0, "destroy", 'd',
762               additional_error))
763             goto failure;
764 
765           break;
766         case 'l':	/* List tickets in local cache, limited by any --client-name and --server-name..  */
767 
768 
769           if (update_arg((void *)&(args_info->list_flag), 0, &(args_info->list_given),
770               &(local_args_info.list_given), optarg, 0, 0, ARG_FLAG,
771               check_ambiguity, override, 1, 0, "list", 'l',
772               additional_error))
773             goto failure;
774 
775           break;
776         case 'r':	/* Renew ticket.  Use --server-name to specify ticket, default is the most recent renewable ticket granting ticket for the default realm..  */
777 
778 
779           if (update_arg((void *)&(args_info->renew_flag), 0, &(args_info->renew_given),
780               &(local_args_info.renew_given), optarg, 0, 0, ARG_FLAG,
781               check_ambiguity, override, 1, 0, "renew", 'r',
782               additional_error))
783             goto failure;
784 
785           break;
786         case 'E':	/* Encryption types to use.  ETYPE is either registered name or integer.  Valid values include 'aes128', 'aes256', 'aes' (same as 'aes256'), '3des', 'des-md5', 'des-md4', 'des-crc', 'des' (same as 'des-md5'), and 'arcfour'..  */
787 
788 
789           if (update_arg( (void *)&(args_info->encryption_type_arg),
790                &(args_info->encryption_type_orig), &(args_info->encryption_type_given),
791               &(local_args_info.encryption_type_given), optarg, 0, 0, ARG_STRING,
792               check_ambiguity, override, 0, 0,
793               "encryption-type", 'E',
794               additional_error))
795             goto failure;
796 
797           break;
798         case 'e':	/* Specify when ticket validity should expire.  The time syntax may be relative (to the start time), such as '20 hours', or absolute, such as '2001-02-03 04:05:06 CET'. The default is 8 hours after the start time..  */
799 
800 
801           if (update_arg( (void *)&(args_info->endtime_arg),
802                &(args_info->endtime_orig), &(args_info->endtime_given),
803               &(local_args_info.endtime_given), optarg, 0, 0, ARG_STRING,
804               check_ambiguity, override, 0, 0,
805               "endtime", 'e',
806               additional_error))
807             goto failure;
808 
809           break;
810         case 's':	/* Specify when ticket should start to be valid.  Accepts same time syntax as --endtime. The default is to become valid immediately..  */
811 
812 
813           if (update_arg( (void *)&(args_info->starttime_arg),
814                &(args_info->starttime_orig), &(args_info->starttime_given),
815               &(local_args_info.starttime_given), optarg, 0, 0, ARG_STRING,
816               check_ambiguity, override, 0, 0,
817               "starttime", 's',
818               additional_error))
819             goto failure;
820 
821           break;
822         case 'c':	/* Read tickets from FILE..  */
823 
824 
825           if (update_arg( (void *)&(args_info->ticket_file_arg),
826                &(args_info->ticket_file_orig), &(args_info->ticket_file_given),
827               &(local_args_info.ticket_file_given), optarg, 0, 0, ARG_STRING,
828               check_ambiguity, override, 0, 0,
829               "ticket-file", 'c',
830               additional_error))
831             goto failure;
832 
833           break;
834         case 'o':	/* Parse STRING as a configuration file statement..  */
835 
836 
837           if (update_arg( (void *)&(args_info->library_options_arg),
838                &(args_info->library_options_orig), &(args_info->library_options_given),
839               &(local_args_info.library_options_given), optarg, 0, 0, ARG_STRING,
840               check_ambiguity, override, 0, 0,
841               "library-options", 'o',
842               additional_error))
843             goto failure;
844 
845           break;
846         case 'q':	/* Don't produce any diagnostic output.
847 .  */
848 
849 
850           if (update_arg((void *)&(args_info->quiet_flag), 0, &(args_info->quiet_given),
851               &(local_args_info.quiet_given), optarg, 0, 0, ARG_FLAG,
852               check_ambiguity, override, 1, 0, "quiet", 'q',
853               additional_error))
854             goto failure;
855 
856           break;
857         case 'v':	/* Produce verbose output.
858         Use multiple times to increase amount of information..  */
859 
860           local_args_info.verbose_given++;
861 
862           break;
863 
864         case 0:	/* Long option with no short option */
865           /* Get a forwardable ticket, i.e., one that can be used to get forwarded tickets..  */
866           if (strcmp (long_options[option_index].name, "forwardable") == 0)
867           {
868 
869 
870             if (update_arg((void *)&(args_info->forwardable_flag), 0, &(args_info->forwardable_given),
871                 &(local_args_info.forwardable_given), optarg, 0, 0, ARG_FLAG,
872                 check_ambiguity, override, 1, 0, "forwardable", '-',
873                 additional_error))
874               goto failure;
875 
876           }
877           /* Get a forwarded ticket..  */
878           else if (strcmp (long_options[option_index].name, "forwarded") == 0)
879           {
880 
881 
882             if (update_arg((void *)&(args_info->forwarded_flag), 0, &(args_info->forwarded_given),
883                 &(local_args_info.forwarded_given), optarg, 0, 0, ARG_FLAG,
884                 check_ambiguity, override, 1, 0, "forwarded", '-',
885                 additional_error))
886               goto failure;
887 
888           }
889           /* Get a proxiable ticket, i.e., one that can be used to get proxy tickets..  */
890           else if (strcmp (long_options[option_index].name, "proxiable") == 0)
891           {
892 
893 
894             if (update_arg((void *)&(args_info->proxiable_flag), 0, &(args_info->proxiable_given),
895                 &(local_args_info.proxiable_given), optarg, 0, 0, ARG_FLAG,
896                 check_ambiguity, override, 1, 0, "proxiable", '-',
897                 additional_error))
898               goto failure;
899 
900           }
901           /* Get a proxy ticket..  */
902           else if (strcmp (long_options[option_index].name, "proxy") == 0)
903           {
904 
905 
906             if (update_arg((void *)&(args_info->proxy_flag), 0, &(args_info->proxy_given),
907                 &(local_args_info.proxy_given), optarg, 0, 0, ARG_FLAG,
908                 check_ambiguity, override, 1, 0, "proxy", '-',
909                 additional_error))
910               goto failure;
911 
912           }
913           /* Get a renewable ticket..  */
914           else if (strcmp (long_options[option_index].name, "renewable") == 0)
915           {
916 
917 
918             if (update_arg((void *)&(args_info->renewable_flag), 0, &(args_info->renewable_given),
919                 &(local_args_info.renewable_given), optarg, 0, 0, ARG_FLAG,
920                 check_ambiguity, override, 1, 0, "renewable", '-',
921                 additional_error))
922               goto failure;
923 
924           }
925           /* Client name. Default is login username..  */
926           else if (strcmp (long_options[option_index].name, "client-name") == 0)
927           {
928 
929 
930             if (update_arg( (void *)&(args_info->client_name_arg),
931                  &(args_info->client_name_orig), &(args_info->client_name_given),
932                 &(local_args_info.client_name_given), optarg, 0, 0, ARG_STRING,
933                 check_ambiguity, override, 0, 0,
934                 "client-name", '-',
935                 additional_error))
936               goto failure;
937 
938           }
939           /* Set default realm..  */
940           else if (strcmp (long_options[option_index].name, "realm") == 0)
941           {
942 
943 
944             if (update_arg( (void *)&(args_info->realm_arg),
945                  &(args_info->realm_orig), &(args_info->realm_given),
946                 &(local_args_info.realm_given), optarg, 0, 0, ARG_STRING,
947                 check_ambiguity, override, 0, 0,
948                 "realm", '-',
949                 additional_error))
950               goto failure;
951 
952           }
953           /* Specify renewable life of ticket.  Implies --renewable.  Accepts same time syntax as --endtime.  If --renewable is specified, the default is 1 week after the start time..  */
954           else if (strcmp (long_options[option_index].name, "renew-till") == 0)
955           {
956 
957 
958             if (update_arg( (void *)&(args_info->renew_till_arg),
959                  &(args_info->renew_till_orig), &(args_info->renew_till_given),
960                 &(local_args_info.renew_till_given), optarg, 0, 0, ARG_STRING,
961                 check_ambiguity, override, 0, 0,
962                 "renew-till", '-',
963                 additional_error))
964               goto failure;
965 
966           }
967           /* Server name. Default is 'krbtgt/REALM' where REALM is client realm..  */
968           else if (strcmp (long_options[option_index].name, "server-name") == 0)
969           {
970 
971 
972             if (update_arg( (void *)&(args_info->server_name_arg),
973                  &(args_info->server_name_orig), &(args_info->server_name_given),
974                 &(local_args_info.server_name_given), optarg, 0, 0, ARG_STRING,
975                 check_ambiguity, override, 0, 0,
976                 "server-name", '-',
977                 additional_error))
978               goto failure;
979 
980           }
981           /* Service name in ticket to use for authenticating request. Only for TGS. Defaults to 'krbtgt/REALM@REALM' where REALM is client realm..  */
982           else if (strcmp (long_options[option_index].name, "ticket-granter") == 0)
983           {
984 
985 
986             if (update_arg( (void *)&(args_info->ticket_granter_arg),
987                  &(args_info->ticket_granter_orig), &(args_info->ticket_granter_given),
988                 &(local_args_info.ticket_granter_given), optarg, 0, 0, ARG_STRING,
989                 check_ambiguity, override, 0, 0,
990                 "ticket-granter", '-',
991                 additional_error))
992               goto failure;
993 
994           }
995           /* Read user configuration from FILE..  */
996           else if (strcmp (long_options[option_index].name, "configuration-file") == 0)
997           {
998 
999 
1000             if (update_arg( (void *)&(args_info->configuration_file_arg),
1001                  &(args_info->configuration_file_orig), &(args_info->configuration_file_given),
1002                 &(local_args_info.configuration_file_given), optarg, 0, 0, ARG_STRING,
1003                 check_ambiguity, override, 0, 0,
1004                 "configuration-file", '-',
1005                 additional_error))
1006               goto failure;
1007 
1008           }
1009           /* Read system configuration from FILE..  */
1010           else if (strcmp (long_options[option_index].name, "system-configuration-file") == 0)
1011           {
1012 
1013 
1014             if (update_arg( (void *)&(args_info->system_configuration_file_arg),
1015                  &(args_info->system_configuration_file_orig), &(args_info->system_configuration_file_given),
1016                 &(local_args_info.system_configuration_file_given), optarg, 0, 0, ARG_STRING,
1017                 check_ambiguity, override, 0, 0,
1018                 "system-configuration-file", '-',
1019                 additional_error))
1020               goto failure;
1021 
1022           }
1023           /* Write tickets from FILE.  Default is to write them back to where they were read from..  */
1024           else if (strcmp (long_options[option_index].name, "ticket-write-file") == 0)
1025           {
1026 
1027 
1028             if (update_arg( (void *)&(args_info->ticket_write_file_arg),
1029                  &(args_info->ticket_write_file_orig), &(args_info->ticket_write_file_given),
1030                 &(local_args_info.ticket_write_file_given), optarg, 0, 0, ARG_STRING,
1031                 check_ambiguity, override, 0, 0,
1032                 "ticket-write-file", '-',
1033                 additional_error))
1034               goto failure;
1035 
1036           }
1037 
1038           break;
1039         case '?':	/* Invalid option.  */
1040           /* `getopt_long' already printed an error message.  */
1041           goto failure;
1042 
1043         default:	/* bug: option not considered.  */
1044           fprintf (stderr, "%s: option unknown: %c%s\n", CMDLINE_PARSER_PACKAGE, c, (additional_error ? additional_error : ""));
1045           abort ();
1046         } /* switch */
1047     } /* while */
1048 
1049 
1050 
1051   args_info->verbose_given += local_args_info.verbose_given;
1052   local_args_info.verbose_given = 0;
1053 
1054   if (check_required)
1055     {
1056       error += cmdline_parser_required2 (args_info, argv[0], additional_error);
1057     }
1058 
1059   cmdline_parser_release (&local_args_info);
1060 
1061   if ( error )
1062     return (EXIT_FAILURE);
1063 
1064   if (optind < argc)
1065     {
1066       int i = 0 ;
1067       int found_prog_name = 0;
1068       /* whether program name, i.e., argv[0], is in the remaining args
1069          (this may happen with some implementations of getopt,
1070           but surely not with the one included by gengetopt) */
1071 
1072       i = optind;
1073       while (i < argc)
1074         if (argv[i++] == argv[0]) {
1075           found_prog_name = 1;
1076           break;
1077         }
1078       i = 0;
1079 
1080       args_info->inputs_num = argc - optind - found_prog_name;
1081       args_info->inputs =
1082         (char **)(malloc ((args_info->inputs_num)*sizeof(char *))) ;
1083       while (optind < argc)
1084         if (argv[optind++] != argv[0])
1085           args_info->inputs[ i++ ] = gengetopt_strdup (argv[optind-1]) ;
1086     }
1087 
1088   return 0;
1089 
1090 failure:
1091 
1092   cmdline_parser_release (&local_args_info);
1093   return (EXIT_FAILURE);
1094 }
1095