source: trunk/src/sh_getopt.c@ 181

Last change on this file since 181 was 181, checked in by katerina, 17 years ago

Remove support for obsolete version 0.8 of libprelude (ticket #120).

File size: 23.8 KB
RevLine 
[1]1/* SAMHAIN file system integrity testing */
2/* Copyright (C) 1999, 2000 Rainer Wichmann */
3/* */
4/* This program is free software; you can redistribute it */
5/* and/or modify */
6/* it under the terms of the GNU General Public License as */
7/* published by */
8/* the Free Software Foundation; either version 2 of the License, or */
9/* (at your option) any later version. */
10/* */
11/* This program is distributed in the hope that it will be useful, */
12/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
13/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
14/* GNU General Public License for more details. */
15/* */
16/* You should have received a copy of the GNU General Public License */
17/* along with this program; if not, write to the Free Software */
18/* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
19
20#include "config_xor.h"
21
22#include <stdlib.h>
23#include <stdio.h>
24#include <string.h>
25#include <limits.h>
26#include <errno.h>
27
28
29#include "samhain.h"
30#include "sh_error.h"
31#include "sh_getopt.h"
32#include "sh_files.h"
33#include "sh_utils.h"
34#include "sh_mail.h"
35#include "sh_forward.h"
36#include "sh_hash.h"
37
38#if defined(WITH_EXTERNAL)
39#include "sh_extern.h"
40#endif
41
[20]42extern int sh_calls_set_bind_addr (const char *);
[1]43
44#undef FIL__
45#define FIL__ _("sh_getopt.c")
46
47#define HAS_ARG_NO 0
48#define HAS_ARG_YES 1
49#define DROP_PRIV_NO 0
50#define DROP_PRIV_YES 1
51
52
53typedef struct options {
[170]54 const char * longopt;
[1]55 const char shortopt;
[170]56 const char * usage;
[1]57 int hasArg;
[20]58 int (*func)(const char * opt);
[1]59} opttable_t;
60
61/*@noreturn@*/
[20]62static int sh_getopt_usage (const char * dummy);
[1]63#if defined (SH_WITH_CLIENT) || defined (SH_STANDALONE)
[20]64static int sh_getopt_forever (const char * dummy);
[1]65#endif
[20]66static int sh_getopt_copyright (const char * dummy);
[76]67static int sh_getopt_version (const char * dummy);
[1]68
69static opttable_t op_table[] = {
70
71#if defined (SH_WITH_CLIENT) || defined (SH_STANDALONE)
72 { N_("set-checksum-test"),
73 't',
74 N_("Set checksum testing to 'init', 'update', or 'check'"),
75 HAS_ARG_YES,
76 sh_util_setchecksum },
77 { N_("interactive"),
78 'i',
79 N_("Run update in interactive mode"),
80 HAS_ARG_NO,
81 sh_util_set_interactive },
82#endif
[27]83#if defined(SH_WITH_SERVER) || defined(SH_WITH_CLIENT)
84 { N_("server-port"),
85 '-',
86 N_("Set the server port to connect to"),
87 HAS_ARG_YES,
88 sh_forward_server_port },
89#endif
[1]90#ifdef SH_WITH_SERVER
91 { N_("server"),
92 'S',
93 N_("Run as log server (obsolete)"),
94 HAS_ARG_NO,
95 sh_util_setserver },
96 { N_("qualified"),
97 'q',
98 N_("Log fully qualified name of client host"),
99 HAS_ARG_NO,
100 sh_forward_set_strip },
101 { N_("chroot"),
102 '-',
103 N_("Chroot to specified directory"),
104 HAS_ARG_YES,
105 sh_unix_set_chroot },
106#endif
107 { N_("daemon"),
108 'D',
109 N_("Run as daemon"),
110 HAS_ARG_NO,
111 sh_unix_setdeamon },
112 { N_("foreground"),
113 '-',
114 N_("Stay in the foreground"),
115 HAS_ARG_NO,
116 sh_unix_setnodeamon },
117 { N_("bind-address"),
118 '-',
119 N_("Bind to this address (interface) for outgoing connections"),
120 HAS_ARG_YES,
121 sh_calls_set_bind_addr },
[27]122#if defined(SH_WITH_SERVER) || defined(SH_WITH_CLIENT)
[1]123 { N_("set-export-severity"),
124 'e',
125 N_("Set severity threshold for export to remote log server"),
126 HAS_ARG_YES,
127 sh_error_setexport },
128#endif
129 { N_("set-syslog-severity"),
130 's',
131 N_("Set severity threshold for syslog"),
132 HAS_ARG_YES,
133 sh_error_set_syslog },
134#ifdef WITH_EXTERNAL
135 { N_("set-extern-severity"),
136 'x',
137 N_("Set severity threshold for logging by external program(s)"),
138 HAS_ARG_YES,
139 sh_error_set_external },
140#endif
141#ifdef HAVE_LIBPRELUDE
142 { N_("set-prelude-severity"),
143 '-',
144 N_("Set severity threshold for logging to prelude"),
145 HAS_ARG_YES,
146 sh_error_set_prelude },
147#endif
148#if defined(WITH_DATABASE)
149 { N_("set-database-severity"),
150 '-',
151 N_("Set severity threshold for logging to RDBMS"),
152 HAS_ARG_YES,
153 sh_error_set_database },
154#endif
155 { N_("set-log-severity"),
156 'l',
157 N_("Set severity threshold for logfile"),
158 HAS_ARG_YES,
159 sh_error_setlog },
160#if defined(SH_WITH_MAIL)
161 { N_("set-mail-severity"),
162 'm',
163 N_("Set severitythreshold for e-mail"),
164 HAS_ARG_YES,
165 sh_error_setseverity },
166#endif
167 { N_("set-print-severity"),
168 'p',
169 N_("Set the severity threshold for terminal/console log"),
170 HAS_ARG_YES,
171 sh_error_setprint },
172#if defined (SH_WITH_CLIENT) || defined (SH_STANDALONE)
173 { N_("recursion"),
174 'r',
175 N_("Set recursion level for directories"),
176 HAS_ARG_YES,
177 sh_files_setrecursion },
178#endif
179 { N_("verify-log"),
180 'L',
181 N_("Verify the audit trail"),
182 HAS_ARG_YES,
183 sh_error_logverify },
184 { N_("just-list"),
185 'j',
186 N_("Modify -L to just list the audit trail"),
187 HAS_ARG_NO,
188 sh_error_logverify_mod },
189#if defined(SH_WITH_MAIL)
190 { N_("verify-mail"),
191 'M',
192 N_("Verify the mailbox"),
193 HAS_ARG_YES,
194 sh_mail_sigverify
195 },
196#endif
197 { N_("add-key"),
198 'V',
199 N_("Add key for the mail/log signature"),
200 HAS_ARG_YES,
201 sh_util_set_newkey
202 },
203 { N_("hash-string"),
204 'H',
205 N_("Print the hash of a string"),
206 HAS_ARG_YES,
207 sh_error_verify },
208#if defined (SH_WITH_SERVER)
209 { N_("password"),
210 'P',
211 N_("Compute a client registry entry for password"),
212 HAS_ARG_YES,
213 sh_forward_make_client },
214 { N_("gen-password"),
215 'G',
216 N_("Generate a random password"),
217 HAS_ARG_NO,
218 sh_forward_create_password },
219#endif
220
221#if defined (SH_WITH_CLIENT) || defined (SH_STANDALONE)
222 { N_("forever"),
223 'f',
224 N_("Loop forever, even if not daemon"),
225 HAS_ARG_NO,
226 sh_getopt_forever},
[169]227 { N_("list-file"),
228 '-',
229 N_("Modify -d to list content of a single file"),
230 HAS_ARG_YES,
231 set_list_file},
[1]232 { N_("full-detail"),
233 'a',
234 N_("Modify -d to list full details"),
235 HAS_ARG_NO,
236 set_full_detail},
237 { N_("delimited"),
238 '-',
239 N_("Modify -d to list full details, comma delimited"),
240 HAS_ARG_NO,
241 set_list_delimited},
242 { N_("list-database"),
243 'd',
244 N_("List database content (like ls -l)"),
245 HAS_ARG_YES,
246 sh_hash_list_db},
247 { N_("init2stdout"),
248 '-',
249 N_("Write database to stdout on init"),
250 HAS_ARG_NO,
251 sh_hash_pushdata_stdout},
252#endif
253 { N_("trace-logfile"),
254 '-',
255 N_("Logfile for trace"),
256 HAS_ARG_YES,
257 sl_trace_file },
258 { N_("trace-enable"),
259 '-',
260 N_("Enable tracing"),
261 HAS_ARG_NO,
262 sl_trace_use },
263 { N_("copyright"),
264 'c',
265 N_("Print copyright information"),
266 HAS_ARG_NO,
267 sh_getopt_copyright },
268 { N_("help"),
269 'h',
270 N_("Print usage information"),
271 HAS_ARG_NO,
272 sh_getopt_usage },
[76]273 { N_("version"),
274 'v',
275 N_("Show version and compiled-in options"),
276 HAS_ARG_NO,
277 sh_getopt_version },
[181]278#if defined(HAVE_LIBPRELUDE)
[20]279 /* need to skip over these */
280 { N_("prelude"),
281 '-',
282 N_("Prelude generic options"),
283 HAS_ARG_NO,
284 NULL },
285 { N_("profile"),
286 '-',
287 N_("Profile to use for this analyzer"),
288 HAS_ARG_YES,
289 NULL },
290 { N_("heartbeat-interval"),
291 '-',
292 N_("Number of seconds between two heartbeats"),
293 HAS_ARG_YES,
294 NULL },
295 { N_("server-addr"),
296 '-',
297 N_("Address where this sensor should report to"),
298 HAS_ARG_YES,
299 NULL },
300 { N_("analyzer-name"),
301 '-',
302 N_("Name for this analyzer"),
303 HAS_ARG_YES,
304 NULL },
305#endif
[1]306 /* last entry -- required !! -- */
307 { NULL,
308 '\0',
309 NULL,
310 HAS_ARG_NO,
311 NULL }
312};
313
[76]314
[170]315static void sh_getopt_print_log_facilities (void)
[76]316{
[171]317 int num = 0;
[76]318
[171]319 fputs (_("Compiled-in log facilities:\n"), stdout);
320
[76]321#ifndef DEFAULT_CONSOLE
[171]322 if (num > 0) fputc ('\n', stdout);
323 printf (_(" console (/dev/console)")); ++num;
[76]324#else
[171]325 if (num > 0) fputc ('\n', stdout);
[76]326 if (0 == strcmp (DEFAULT_CONSOLE, _("NULL")))
[171]327 { printf (_("console (/dev/console)")); ++num; }
[76]328 else
[171]329 { printf (_("console (%s)"), DEFAULT_CONSOLE); ++num; }
[76]330#endif
[171]331 if (num > 0) fputc ('\n', stdout);
332 fputs (_(" syslog"), stdout); ++num;
333 if (num > 0) fputc ('\n', stdout);
334 printf (_(" logfile (%s)"), DEFAULT_ERRFILE); ++num;
[76]335
336#if defined(WITH_EXTERNAL)
[171]337 if (num > 0) fputc ('\n', stdout);
338 fputs (_(" external program"), stdout); ++num;
[76]339#endif
340
341#if defined(WITH_MESSAGE_QUEUE)
[171]342 if (num > 0) fputc ('\n', stdout);
343 fputs (_(" message queue"), stdout); ++num;
[76]344#endif
345
346#if defined(WITH_DATABASE)
[171]347 if (num > 0) fputc ('\n', stdout);
348 fputs (_(" database"), stdout); ++num;
[76]349#ifdef WITH_ODBC
350 fputs (_(" (odbc)"), stdout);
351#endif
352#ifdef WITH_ORACLE
353 fputs (_(" (Oracle)"), stdout);
354#endif
355#ifdef WITH_POSTGRES
356 fputs (_(" (PostgreSQL)"), stdout);
357#endif
358#ifdef WITH_MYSQL
359 fputs (_(" (MySQL)"), stdout);
360#endif
361#endif
362
363#if defined(SH_WITH_CLIENT) || defined(SH_WITH_SERVER)
[171]364 if (num > 0) fputc ('\n', stdout);
365 fputs (_(" server"), stdout); ++num;
[76]366#endif
367
368#if defined(SH_WITH_MAIL)
[171]369 if (num > 0) fputc ('\n', stdout);
370 fputs (_(" email"), stdout); ++num;
[76]371#endif
372
373#ifdef HAVE_LIBPRELUDE
[171]374 if (num > 0) fputc ('\n', stdout); ++num;
[181]375 fputs (_(" prelude (0.9.6+)"), stdout);
[76]376#endif
377
[171]378 if (num == 0)
379 fputs (_(" none"), stdout);
[76]380 fputc ('\n', stdout);
381 return;
382}
383
[170]384static void sh_getopt_print_options (void)
[76]385{
386 int num = 0;
387
388
389#if defined(SH_STANDALONE)
390 if (num > 0) fputc ('\n', stdout);
391 fputs (_("Standalone executable"), stdout); ++num;
392#endif
393#if defined(SH_WITH_CLIENT)
394 if (num > 0) fputc ('\n', stdout);
395 printf (_("Client executable (port %d)"), SH_DEFAULT_PORT); ++num;
396#endif
397#if defined(SH_WITH_CLIENT)
398 if (num > 0) fputc ('\n', stdout);
399 printf (_("Server executable (port %d, user %s)"),
400 SH_DEFAULT_PORT, DEFAULT_IDENT);
401 ++num;
402#endif
403
404 fputs (_(", compiled-in options:"), stdout);
405
[171]406#if defined(USE_DL_PREFIX)
407 if (num > 0) fputc ('\n', stdout);
408 printf (_(" using system malloc")); ++num;
409#else
410 if (num > 0) fputc ('\n', stdout);
411 printf (_(" using dnmalloc")); ++num;
412#endif
413
[76]414#if defined(HAVE_EGD_RANDOM)
415 if (num > 0) fputc ('\n', stdout);
[171]416 printf (_(" using entropy gathering daemon (%s)"), EGD_SOCKET_NAME); ++num;
[76]417#endif
418#if defined(HAVE_UNIX_RANDOM)
419 if (num > 0) fputc ('\n', stdout);
[171]420 fputs (_(" using unix entropy gatherer"), stdout); ++num;
[76]421#endif
422#if defined(HAVE_URANDOM)
423 if (num > 0) fputc ('\n', stdout);
[171]424 printf (_(" using entropy device (%s)"), NAME_OF_DEV_RANDOM); ++num;
[76]425#endif
426
427#ifdef WITH_GPG
428 if (num > 0) fputc ('\n', stdout);
429 printf (_(" GnuPG signatures (%s)"), DEFAULT_GPG_PATH); ++num;
430#ifdef HAVE_GPG_CHECKSUM
431 if (num > 0) fputc ('\n', stdout);
432 printf (_(" -- GnuPG checksum: %s"), GPG_HASH); ++num;
433#endif
434#ifdef USE_FINGERPRINT
435 if (num > 0) fputc ('\n', stdout);
436 printf (_(" -- Key fingerprint: %s"), SH_GPG_FP); ++num;
437#endif
438#endif
439
440#if defined(SL_DEBUG)
441 if (num > 0) fputc ('\n', stdout);
[171]442 fputs (_(" debug build (do not use for production)"), stdout); ++num;
[76]443#endif
444#if defined(SCREW_IT_UP)
445 if (num > 0) fputc ('\n', stdout);
446 fputs (_(" anti-debugger"), stdout); ++num;
447#endif
448#if defined(SH_USE_XML)
449 if (num > 0) fputc ('\n', stdout);
450 fputs (_(" xml log format"), stdout); ++num;
451#endif
452#if defined(HAVE_NTIME)
453 if (num > 0) fputc ('\n', stdout);
[171]454 fputs (_(" using time server"), stdout); ++num;
[76]455#endif
456
457#if defined(SH_WITH_CLIENT) || defined(SH_STANDALONE)
[169]458#if defined(HAVE_LIBZ)
459 if (num > 0) fputc ('\n', stdout);
460 fputs (_(" optionally store full text for files"), stdout); ++num;
461#endif
[76]462#if defined(USE_XATTR)
463 if (num > 0) fputc ('\n', stdout);
464 fputs (_(" check SELinux attributes"), stdout); ++num;
465#endif
466#if defined(USE_ACL)
467 if (num > 0) fputc ('\n', stdout);
468 fputs (_(" check Posix ACLs"), stdout); ++num;
469#endif
470#if defined(RELOAD_DATABASE)
471 if (num > 0) fputc ('\n', stdout);
472 fputs (_(" fetch database on reload"), stdout); ++num;
473#endif
474#endif
475
476#if defined(SH_WITH_SERVER)
477
478#if !defined(HAVE_GETPEEREID) && !defined(SO_PEERCRED) && !defined(HAVE_STRUCT_CMSGCRED) && !defined(HAVE_STRUCT_FCRED) && !(defined(HAVE_STRUCT_SOCKCRED) && defined(LOCAL_CREDS))
479 if (num > 0) fputc ('\n', stdout);
480 fputs (_(" command socket authentication: use SetSocketPassword"), stdout);
481 ++num;
482#else
483 if (num > 0) fputc ('\n', stdout);
484 fputs (_(" command socket authentication: use SetSocketAllowUID"), stdout);
485 ++num;
486#endif
487
488#if defined(SH_USE_LIBWRAP)
489 if (num > 0) fputc ('\n', stdout);
490 fputs (_(" support tcp wrapper"), stdout); ++num;
491#endif
492#if defined(INET_SYSLOG)
493 if (num > 0) fputc ('\n', stdout);
494 fputs (_(" support listening on 514/udp (syslog)"), stdout); ++num;
495#endif
496#endif
497
498 if (num == 0)
499 fputs (_(" none"), stdout);
500 fputc ('\n', stdout);
501 return;
502}
503
[170]504static void sh_getopt_print_modules (void)
[76]505{
506#if defined (SH_WITH_CLIENT) || defined (SH_STANDALONE)
507 int num = 0;
508
[171]509 fputs (_("Compiled-in modules:\n"), stdout);
[76]510#ifdef SH_USE_UTMP
511 if (num > 0) fputc (',', stdout);
512 fputs (_(" login/logout"), stdout); ++num;
513#endif
514#ifdef SH_USE_MOUNTS
515 if (num > 0) fputc (',', stdout);
516 fputs (_(" mount options"), stdout); ++num;
517#endif
518#ifdef SH_USE_USERFILES
519 if (num > 0) fputc (',', stdout);
520 fputs (_(" userfiles"), stdout); ++num;
521#endif
522#ifdef SH_USE_KERN
523 if (num > 0) fputc (',', stdout);
524 fputs (_(" kernel"), stdout); ++num;
525#endif
526#ifdef SH_USE_SUIDCHK
527 if (num > 0) fputc (',', stdout);
528 fputs (_(" suid"), stdout); ++num;
529#endif
530#ifdef SH_USE_PROCESSCHECK
531 if (num > 0) fputc (',', stdout);
532 fputs (_(" processes"), stdout); ++num;
533#endif
534#ifdef SH_USE_PORTCHECK
535 if (num > 0) fputc (',', stdout);
536 fputs (_(" ports"), stdout); ++num;
537#endif
538 if (num == 0)
539 fputs (_(" none"), stdout);
540 fputc ('\n', stdout);
541#endif
542 return;
543}
544
545static int sh_getopt_version (const char * dummy)
546{
547 (void) dummy;
548 fprintf (stdout,
549 _("This is samhain (%s), "\
[161]550 "(c) 1999-2008 Rainer Wichmann (http://la-samhna.de).\n"),
[76]551 VERSION);
552 fprintf (stdout, _("This software comes with ABSOLUTELY NO WARRANTY. "));
553 fprintf (stdout, _("Use at own risk.\n\n"));
554
555 sh_getopt_print_log_facilities ();
556 sh_getopt_print_modules ();
557 sh_getopt_print_options ();
558
559 _exit (EXIT_SUCCESS);
560 /*@notreached@*/
561 return 0; /* make compilers happy */
562}
[20]563static int sh_getopt_copyright (const char * dummy)
[1]564{
565 fprintf (stdout,
[161]566 _("Copyright (C) 1999-2008 Rainer Wichmann"\
[1]567 " (http://la-samhna.de).\n\n"));
568
569 fprintf (stdout,
570 _("This program is free software; "\
571 "you can redistribute it and/or modify\n"));
572 fprintf (stdout, _("it under the terms of the GNU General "\
573 "Public License as published by\n"));
574 fprintf (stdout, _("the Free Software Foundation; either version 2 "\
575 "of the License, or\n"));
576 fprintf (stdout, _("(at your option) any later version.\n\n"));
577
578 fprintf (stdout, _("This program is distributed in the hope "\
579 "that it will be useful,\n"));
580 fprintf (stdout, _("but WITHOUT ANY WARRANTY; "\
581 "without even the implied warranty of\n"));
582 fprintf (stdout, _("MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE."\
583 " See the\n"));
584 fprintf (stdout, _("GNU General Public License for more details.\n\n"));
585
586 fprintf (stdout, _("You should have received a copy of the "\
587 "GNU General Public License\n"));
588 fprintf (stdout, _("along with this program; "\
589 "if not, write to the Free Software\n"));
590 fprintf (stdout, _("Foundation, Inc., 59 Temple Place - Suite 330, "\
591 "Boston, MA 02111-1307, USA.\n\n"));
592
593 fprintf (stdout, _("This product makes use of the reference implementation "\
594 "of the TIGER message\n"));
595 fprintf (stdout, _("digest algorithm. This code is copyright Eli Biham "\
596 "(biham@cs.technion.ac.il)\n"));
597 fprintf (stdout, _("and Ross Anderson (rja14@cl.cam.ac.uk). It can be used "\
598 "freely without any\n"));
599 fprintf (stdout, _("restrictions.\n"));
600#if defined(USE_SRP_PROTOCOL) && !defined(SH_STANDALONE)
601#if (!defined(HAVE_LIBGMP) || !defined(HAVE_GMP_H))
602 fprintf (stdout, _("This product makes use of the 'bignum' library by "\
603 "Henrik Johansson\n"));
604 fprintf (stdout, _("(Henrik.Johansson@Nexus.Comm.SE). If you are including "\
605 "this library in a\n"));
606 fprintf (stdout, _("commercial product, be sure to distribute ALL of"\
607 " it with the product.\n"));
608#endif
609 fprintf (stdout, _("This product uses the 'Secure Remote Password' "\
610 "cryptographic\n"));
611 fprintf (stdout, _("authentication system developed by Tom Wu "\
612 "(tjw@CS.Stanford.EDU).\n"));
613#endif
614 fprintf (stdout, _("\nPlease refer to the file COPYING in the source "\
615 "distribution for a"));
616 fprintf (stdout, _("\nfull list of incorporated code and associated "\
617 "licenses.\n"));
618
619 if (dummy)
620 _exit (EXIT_SUCCESS);
621 else
622 _exit (EXIT_SUCCESS);
623 /*@notreached@*/
624 return 0; /* make compilers happy */
625}
626
627/*@noreturn@*/
[20]628static int sh_getopt_usage (const char * dummy)
[1]629{
630 int i;
631 char fmt[64];
632
633 char opts[64];
634
635 for (i = 0; i < 64; ++i) /* splint does not grok char opts[64] = { '\0' }; */
636 opts[i] = '\0';
637
638 fprintf (stdout,
639 _("This is samhain (%s), "\
[76]640 "(c) 1999-2006 Rainer Wichmann (http://la-samhna.de).\n"),
[1]641 VERSION);
642 fprintf (stdout, _("This software comes with ABSOLUTELY NO WARRANTY. "));
643 fprintf (stdout, _("Use at own risk.\n"));
644
645 fprintf (stdout, _("Usage:\n\n"));
646
647 for (i = 0; op_table[i].longopt != NULL; ++i) {
648
649 if (i == 63)
650 break;
651
652 if (op_table[i].shortopt != '-' &&
653 strchr(opts, op_table[i].shortopt) != NULL)
654 fprintf (stdout, _("Short option char collision !\n"));
655 opts[i] = op_table[i].shortopt;
656
657
658 if (op_table[i].hasArg == HAS_ARG_NO) {
659 if (sl_strlen(op_table[i].longopt) < 10)
[22]660 sl_strlcpy(fmt,_("%c%c%c --%-s,\t\t\t %s\n"), sizeof(fmt));
[1]661 else if (sl_strlen(op_table[i].longopt) < 17)
[22]662 sl_strlcpy(fmt, _("%c%c%c --%-s,\t\t %s\n"), sizeof(fmt));
[1]663 else
[22]664 sl_strlcpy(fmt, _("%c%c%c --%-s,\t %s\n"), sizeof(fmt));
665 /* flawfinder: ignore */
666 fprintf (stdout, fmt,
[1]667 (op_table[i].shortopt == '-') ? ' ' : '-',
668 (op_table[i].shortopt == '-') ? ' ' : op_table[i].shortopt,
669 (op_table[i].shortopt == '-') ? ' ' : ',',
670 _(op_table[i].longopt),
671 _(op_table[i].usage));
672 } else {
673 if (sl_strlen(op_table[i].longopt) < 12)
[22]674 sl_strlcpy(fmt, _("%c%c %s --%-s=<arg>,\t\t %s\n"), sizeof(fmt));
[1]675 else
[22]676 sl_strlcpy(fmt, _("%c%c %s --%-s=<arg>,\t %s\n"), sizeof(fmt));
677 /* flawfinder: ignore */
678 fprintf (stdout, fmt,
[1]679 (op_table[i].shortopt == '-') ? ' ' : '-',
680 (op_table[i].shortopt == '-') ? ' ' : op_table[i].shortopt,
681 (op_table[i].shortopt == '-') ? _(" ") : _("<arg>,"),
682 _(op_table[i].longopt),
683 _(op_table[i].usage));
684 }
685 }
686
687 fprintf (stdout,
688 _("\nPlease report bugs to support@la-samhna.de.\n"));
689
690 (void) fflush(stdout);
691
692 if ( dummy != NULL)
693 {
694 if (sl_strcmp( dummy, _("fail")) == 0 )
695 _exit (EXIT_FAILURE);
696 }
697
698 _exit (EXIT_SUCCESS);
699 /*@notreached@*/
700 return 0; /* make compilers happy */
701}
702
703#if defined (SH_WITH_CLIENT) || defined (SH_STANDALONE)
[20]704static int sh_getopt_forever (const char * dummy)
[1]705{
[170]706 (void) dummy;
[1]707 SL_ENTER(_("sh_getopt_forever"));
708 sh.flag.loop = S_TRUE;
709 SL_RETURN(0, _("sh_getopt_forever"));
710}
711#endif
712
713int sh_getopt_get (int argc, char * argv[])
714{
715 int count = 0;
716 size_t len = 0;
717 int foundit = 0;
718 int i;
719 size_t k;
720 char * theequal;
721
722 SL_ENTER(_("sh_getopt_get"));
723
724 /* -- Return if no args. --
725 */
726 if (argc < 2)
727 SL_RETURN(0, _("sh_getopt_get"));
728
729 while (argc > 1 && argv[1][0] == '-')
730 {
731
732 /* Initialize
733 */
734 foundit = 0;
735 len = sl_strlen (argv[1]);
736
737 /* a '-' with no argument: error
738 */
739 if (len == 1)
740 (void) sh_getopt_usage(_("fail"));
741
742 /* a '--' with no argument: stop argument processing
743 */
744 if (len == 2 && argv[1][1] == '-')
745 SL_RETURN( count, _("sh_getopt_get"));
746
747 /* a short option: process it
748 */
749 if (len >= 2 && argv[1][1] != '-')
750 {
751 for (k = 1; k < len; ++k)
752 {
753 for (i = 0; op_table[i].shortopt != '\0'; ++i)
754 {
755
756 if ( op_table[i].shortopt == argv[1][k] )
757 {
758 foundit = 1;
759 if ( op_table[i].hasArg == HAS_ARG_YES )
760 {
761 if (k != (len - 1))
762 {
763 /* not last option
764 */
765 fprintf (stderr,
766 _("Error: short option with argument is not last in option string\n"));
767 (void) sh_getopt_usage(_("fail"));
768 }
769 if (argc < 3)
770 {
771 /* argument required, but no avail
772 */
773 fprintf (stderr, _("Error: missing argument\n"));
774 (void) sh_getopt_usage(_("fail"));
775 }
776 else
777 {
778 /* call function with argument */
779 --argc; ++argv;
[20]780 if (NULL != op_table[i].func &&
781 0 != (* op_table[i].func )(argv[1]))
[1]782 fprintf (stderr,
[42]783 _("Error processing option -%c\n"),
[1]784 op_table[i].shortopt);
785 break;
786 }
787 }
788 else
789 {
[20]790 if (NULL != op_table[i].func &&
791 0 != (* op_table[i].func )(NULL))
[1]792 fprintf (stderr,
[42]793 _("Error processing option -%c\n"),
[1]794 op_table[i].shortopt);
795 break;
796 }
797 }
798 }
799 }
800
801 /* 'break' should get here
802 */
803 if (foundit == 1)
804 {
805 --argc; ++argv;
806 continue;
807 }
808 else
809 {
810 /* unrecognized short option */
811 fprintf (stderr, _("Error: unrecognized short option\n"));
812 (void) sh_getopt_usage(_("fail"));
813 }
814 }
815
816 /* a long option: process it
817 */
818 if (len > 2)
819 {
820
821 for (i = 0; op_table[i].longopt != NULL; ++i)
822 {
[27]823
[1]824 if (sl_strncmp(_(op_table[i].longopt),
825 &argv[1][2],
826 sl_strlen(op_table[i].longopt)) == 0 )
827 {
828 foundit = 1;
829 if ( op_table[i].hasArg == HAS_ARG_YES )
830 {
[27]831 theequal = strchr(argv[1], '=');
832 if (theequal == NULL)
[1]833 {
[20]834 if (argc < 3)
835 {
836 /* argument required, but no avail
837 */
838 fprintf (stderr, _("Error: missing argument\n"));
839 (void) sh_getopt_usage(_("fail"));
840 }
841 else
842 {
843 /* call function with argument */
844 --argc; ++argv;
845 if (NULL != op_table[i].func &&
846 0 != (* op_table[i].func )(argv[1]))
847 fprintf (stderr,
[42]848 _("Error processing option -%s\n"),
[20]849 op_table[i].longopt);
850 break;
851 }
[1]852 }
853 else
854 {
855 if (sl_strlen (theequal) > 1)
856 {
857 ++theequal;
858 /* call function with argument */
[20]859 if (NULL != op_table[i].func &&
860 0 != (* op_table[i].func )(theequal))
[1]861 fprintf (stderr,
[42]862 _("Error processing option -%s\n"),
[1]863 op_table[i].longopt);
864 break;
865 }
866 else
867 {
868 fprintf (stderr, _("Error: invalid argument\n"));
869 /* argument required, but no avail */
870 (void) sh_getopt_usage(_("fail"));
871 }
872 }
873 }
874 else
875 {
[20]876 if (NULL != op_table[i].func &&
877 0 != (* op_table[i].func )(NULL))
[1]878 fprintf (stderr,
[42]879 _("Error processing option -%s\n"),
[1]880 op_table[i].longopt);
881 break;
882 }
883 }
884 }
885
886 /* 'break' should get here */
887 if (foundit == 1)
888 {
889 ++count;
890 --argc;
891 ++argv;
892 continue;
893 }
894 else
895 {
896 /* unrecognized long option */
897 fprintf (stderr, _("Error: unrecognized long option\n"));
898 (void) sh_getopt_usage(_("fail"));
899 }
900 }
901 }
902
903 SL_RETURN( count, _("sh_getopt_get"));
904}
Note: See TracBrowser for help on using the repository browser.