source: trunk/src/sh_getopt.c@ 353

Last change on this file since 353 was 347, checked in by katerina, 13 years ago

Fix for ticket #255 (improve protection against 'intruder on server' scenario).

File size: 24.7 KB
Line 
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
42extern int sh_calls_set_bind_addr (const char *);
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 {
54 const char * longopt;
55 const char shortopt;
56 const char * usage;
57 int hasArg;
58 int (*func)(const char * opt);
59} opttable_t;
60
61/*@noreturn@*/
62static int sh_getopt_usage (const char * dummy);
63#if defined (SH_WITH_CLIENT) || defined (SH_STANDALONE)
64static int sh_getopt_forever (const char * dummy);
65#endif
66static int sh_getopt_copyright (const char * dummy);
67static int sh_getopt_version (const char * dummy);
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
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
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 },
122#if defined(SH_WITH_SERVER) || defined(SH_WITH_CLIENT)
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},
227 { N_("list-file"),
228 '-',
229 N_("Modify -d to list content of a single file"),
230 HAS_ARG_YES,
231 set_list_file},
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 },
273 { N_("version"),
274 'v',
275 N_("Show version and compiled-in options"),
276 HAS_ARG_NO,
277 sh_getopt_version },
278#if defined(HAVE_LIBPRELUDE)
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
306 /* last entry -- required !! -- */
307 { NULL,
308 '\0',
309 NULL,
310 HAS_ARG_NO,
311 NULL }
312};
313
314
315static void sh_getopt_print_log_facilities (void)
316{
317 int num = 0;
318
319 fputs (_("Compiled-in log facilities:\n"), stdout);
320
321#ifndef DEFAULT_CONSOLE
322 if (num > 0) fputc ('\n', stdout);
323 printf ("%s", _(" console (/dev/console)")); ++num;
324#else
325 if (num > 0) fputc ('\n', stdout);
326 if (0 == strcmp (DEFAULT_CONSOLE, _("NULL")))
327 { printf ("%s", _("console (/dev/console)")); ++num; }
328 else
329 { printf (_("console (%s)"), DEFAULT_CONSOLE); ++num; }
330#endif
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;
335
336#if defined(WITH_EXTERNAL)
337 if (num > 0) fputc ('\n', stdout);
338 fputs (_(" external program"), stdout); ++num;
339#endif
340
341#if defined(WITH_MESSAGE_QUEUE)
342 if (num > 0) fputc ('\n', stdout);
343 fputs (_(" message queue"), stdout); ++num;
344#endif
345
346#if defined(WITH_DATABASE)
347 if (num > 0) fputc ('\n', stdout);
348 fputs (_(" database"), stdout); ++num;
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)
364 if (num > 0) fputc ('\n', stdout);
365 fputs (_(" server"), stdout); ++num;
366#endif
367
368#if defined(SH_WITH_MAIL)
369 if (num > 0) fputc ('\n', stdout);
370 fputs (_(" email"), stdout); ++num;
371#endif
372
373#ifdef HAVE_LIBPRELUDE
374 if (num > 0) fputc ('\n', stdout); ++num;
375 fputs (_(" prelude (0.9.6+)"), stdout);
376#endif
377
378 if (num == 0)
379 fputs (_(" none"), stdout);
380 fputc ('\n', stdout);
381 return;
382}
383
384static void sh_getopt_print_options (void)
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#if defined(USE_IPVX)
404 fputs (_(", IPv6 supported"), stdout);
405#endif
406
407 fputs (_(", compiled-in options:"), stdout);
408
409#if defined(USE_SYSTEM_MALLOC)
410 if (num > 0) fputc ('\n', stdout);
411 fputs (_(" using system malloc"), stdout); ++num;
412#else
413 if (num > 0) fputc ('\n', stdout);
414 fputs (_(" using dnmalloc"), stdout); ++num;
415#endif
416
417#if defined(HAVE_EGD_RANDOM)
418 if (num > 0) fputc ('\n', stdout);
419 printf (_(" using entropy gathering daemon (%s)"), EGD_SOCKET_NAME); ++num;
420#endif
421#if defined(HAVE_UNIX_RANDOM)
422 if (num > 0) fputc ('\n', stdout);
423 fputs (_(" using unix entropy gatherer"), stdout); ++num;
424#endif
425#if defined(HAVE_URANDOM)
426 if (num > 0) fputc ('\n', stdout);
427 printf (_(" using entropy device (%s)"), NAME_OF_DEV_RANDOM); ++num;
428#endif
429
430#ifdef WITH_GPG
431 if (num > 0) fputc ('\n', stdout);
432 printf (_(" GnuPG signatures (%s)"), DEFAULT_GPG_PATH); ++num;
433#ifdef HAVE_GPG_CHECKSUM
434 if (num > 0) fputc ('\n', stdout);
435 printf (_(" -- GnuPG checksum: %s"), GPG_HASH); ++num;
436#endif
437#ifdef USE_FINGERPRINT
438 if (num > 0) fputc ('\n', stdout);
439 printf (_(" -- Key fingerprint: %s"), SH_GPG_FP); ++num;
440#endif
441#endif
442
443#if defined(SH_SHELL_EVAL)
444 if (num > 0) fputc ('\n', stdout);
445 fputs (_(" shell expansion in configuration file supported"), stdout); ++num;
446#endif
447
448#if defined(SL_DEBUG)
449 if (num > 0) fputc ('\n', stdout);
450 fputs (_(" debug build (do not use for production)"), stdout); ++num;
451#endif
452#if defined(SCREW_IT_UP)
453 if (num > 0) fputc ('\n', stdout);
454 fputs (_(" anti-debugger"), stdout); ++num;
455#endif
456#if defined(SH_USE_XML)
457 if (num > 0) fputc ('\n', stdout);
458 fputs (_(" xml log format"), stdout); ++num;
459#endif
460#if defined(HAVE_NTIME)
461 if (num > 0) fputc ('\n', stdout);
462 fputs (_(" using time server"), stdout); ++num;
463#endif
464#if defined(HAVE_REGEX_H)
465 if (num > 0) fputc ('\n', stdout);
466 fputs (_(" posix regex support"), stdout); ++num;
467#endif
468
469
470#if defined(SH_WITH_CLIENT) || defined(SH_STANDALONE)
471#if defined(HAVE_LIBZ)
472 if (num > 0) fputc ('\n', stdout);
473 fputs (_(" optionally store full text for files"), stdout); ++num;
474#endif
475#if !defined(SH_COMPILE_STATIC) && defined(__linux__) && defined(HAVE_AUPARSE_H) && defined(HAVE_AUPARSE_LIB)
476 if (num > 0) fputc ('\n', stdout);
477 fputs (_(" optionally report auditd record of changed file"), stdout); ++num;
478#endif
479#if defined(USE_XATTR)
480 if (num > 0) fputc ('\n', stdout);
481 fputs (_(" check SELinux attributes"), stdout); ++num;
482#endif
483#if defined(USE_ACL)
484 if (num > 0) fputc ('\n', stdout);
485 fputs (_(" check Posix ACLs"), stdout); ++num;
486#endif
487#if defined(RELOAD_DATABASE)
488 if (num > 0) fputc ('\n', stdout);
489 fputs (_(" fetch database on reload"), stdout); ++num;
490#endif
491#endif
492
493#if defined(SH_WITH_SERVER)
494
495#if !defined(HAVE_GETPEEREID) && !defined(SO_PEERCRED) && !defined(HAVE_STRUCT_CMSGCRED) && !defined(HAVE_STRUCT_FCRED) && !(defined(HAVE_STRUCT_SOCKCRED) && defined(LOCAL_CREDS))
496 if (num > 0) fputc ('\n', stdout);
497 fputs (_(" command socket authentication: use SetSocketPassword"), stdout);
498 ++num;
499#else
500 if (num > 0) fputc ('\n', stdout);
501 fputs (_(" command socket authentication: use SetSocketAllowUID"), stdout);
502 ++num;
503#endif
504
505#if defined(SH_USE_LIBWRAP)
506 if (num > 0) fputc ('\n', stdout);
507 fputs (_(" support tcp wrapper"), stdout); ++num;
508#endif
509#if defined(INET_SYSLOG)
510 if (num > 0) fputc ('\n', stdout);
511 fputs (_(" support listening on 514/udp (syslog)"), stdout); ++num;
512#endif
513#endif
514
515 if (num == 0)
516 fputs (_(" none"), stdout);
517 fputc ('\n', stdout);
518 return;
519}
520
521static void sh_getopt_print_modules (void)
522{
523#if defined (SH_WITH_CLIENT) || defined (SH_STANDALONE)
524 int num = 0;
525
526 fputs (_("Compiled-in modules:\n"), stdout);
527#ifdef SH_USE_UTMP
528 if (num > 0) fputc (',', stdout);
529 fputs (_(" login/logout"), stdout); ++num;
530#endif
531#ifdef SH_USE_MOUNTS
532 if (num > 0) fputc (',', stdout);
533 fputs (_(" mount options"), stdout); ++num;
534#endif
535#ifdef SH_USE_USERFILES
536 if (num > 0) fputc (',', stdout);
537 fputs (_(" userfiles"), stdout); ++num;
538#endif
539#ifdef SH_USE_KERN
540 if (num > 0) fputc (',', stdout);
541 fputs (_(" kernel"), stdout); ++num;
542#endif
543#ifdef SH_USE_SUIDCHK
544 if (num > 0) fputc (',', stdout);
545 fputs (_(" suid"), stdout); ++num;
546#endif
547#ifdef SH_USE_PROCESSCHECK
548 if (num > 0) fputc (',', stdout);
549 fputs (_(" processes"), stdout); ++num;
550#endif
551#ifdef SH_USE_PORTCHECK
552 if (num > 0) fputc (',', stdout);
553 fputs (_(" ports"), stdout); ++num;
554#endif
555#ifdef USE_LOGFILE_MONITOR
556 if (num > 0) fputc (',', stdout);
557 fputs (_(" logfile monitor"), stdout); ++num;
558#endif
559 if (num == 0)
560 fputs (_(" none"), stdout);
561 fputc ('\n', stdout);
562#endif
563 return;
564}
565
566static int sh_getopt_version (const char * dummy)
567{
568 (void) dummy;
569 fprintf (stdout,
570 _("This is samhain (%s), "\
571 "(c) 1999-2008 Rainer Wichmann (http://la-samhna.de).\n"),
572 VERSION);
573 fprintf (stdout, "%s",_("This software comes with ABSOLUTELY NO WARRANTY. "));
574 fprintf (stdout, "%s",_("Use at own risk.\n\n"));
575
576 sh_getopt_print_log_facilities ();
577 sh_getopt_print_modules ();
578 sh_getopt_print_options ();
579
580 _exit (EXIT_SUCCESS);
581 /*@notreached@*/
582 return 0; /* make compilers happy */
583}
584static int sh_getopt_copyright (const char * dummy)
585{
586 fprintf (stdout, "%s",
587 _("Copyright (C) 1999-2008 Rainer Wichmann"\
588 " (http://la-samhna.de).\n\n"));
589
590 fprintf (stdout, "%s",
591 _("This program is free software; "\
592 "you can redistribute it and/or modify\n"));
593 fprintf (stdout, "%s",_("it under the terms of the GNU General "\
594 "Public License as published by\n"));
595 fprintf (stdout, "%s",_("the Free Software Foundation; either version 2 "\
596 "of the License, or\n"));
597 fprintf (stdout, "%s",_("(at your option) any later version.\n\n"));
598
599 fprintf (stdout, "%s",_("This program is distributed in the hope "\
600 "that it will be useful,\n"));
601 fprintf (stdout, "%s",_("but WITHOUT ANY WARRANTY; "\
602 "without even the implied warranty of\n"));
603 fprintf (stdout, "%s",_("MERCHANTABILITY or FITNESS FOR A PARTICULAR "\
604 "PURPOSE. See the\n"));
605 fprintf (stdout, "%s",_("GNU General Public License for more details.\n\n"));
606
607 fprintf (stdout, "%s",_("You should have received a copy of the "\
608 "GNU General Public License\n"));
609 fprintf (stdout, "%s",_("along with this program; "\
610 "if not, write to the Free Software\n"));
611 fprintf (stdout, "%s",_("Foundation, Inc., 59 Temple Place - Suite 330, "\
612 "Boston, MA 02111-1307, USA.\n\n"));
613
614 fprintf (stdout, "%s",_("This product makes use of the reference "\
615 "implementation of the TIGER message\n"));
616 fprintf (stdout, "%s",_("digest algorithm. This code is copyright Eli Biham "\
617 "(biham@cs.technion.ac.il)\n"));
618 fprintf (stdout, "%s",_("and Ross Anderson (rja14@cl.cam.ac.uk). It can be used "\
619 "freely without any\n"));
620 fprintf (stdout, "%s",_("restrictions.\n"));
621#if defined(USE_SRP_PROTOCOL) && !defined(SH_STANDALONE)
622#if (!defined(HAVE_LIBGMP) || !defined(HAVE_GMP_H))
623 fprintf (stdout, "%s",_("This product makes use of the 'bignum' library by "\
624 "Henrik Johansson\n"));
625 fprintf (stdout, "%s",_("(Henrik.Johansson@Nexus.Comm.SE). If you are "\
626 "including this library in a\n"));
627 fprintf (stdout, "%s",_("commercial product, be sure to distribute ALL of"\
628 " it with the product.\n"));
629#endif
630 fprintf (stdout, "%s",_("This product uses the 'Secure Remote Password' "\
631 "cryptographic\n"));
632 fprintf (stdout, "%s",_("authentication system developed by Tom Wu "\
633 "(tjw@CS.Stanford.EDU).\n"));
634#endif
635 fprintf (stdout, "%s",_("\nPlease refer to the file COPYING in the source "\
636 "distribution for a"));
637 fprintf (stdout, "%s",_("\nfull list of incorporated code and associated "\
638 "licenses.\n"));
639
640 if (dummy)
641 _exit (EXIT_SUCCESS);
642 else
643 _exit (EXIT_SUCCESS);
644 /*@notreached@*/
645 return 0; /* make compilers happy */
646}
647
648/*@noreturn@*/
649static int sh_getopt_usage (const char * dummy)
650{
651 int i;
652 char fmt[64];
653
654 char opts[64];
655
656 for (i = 0; i < 64; ++i) /* splint does not grok char opts[64] = { '\0' }; */
657 opts[i] = '\0';
658
659 fprintf (stdout,
660 _("This is samhain (%s), "\
661 "(c) 1999-2006 Rainer Wichmann (http://la-samhna.de).\n"),
662 VERSION);
663 fprintf (stdout, "%s",_("This software comes with ABSOLUTELY NO WARRANTY. "));
664 fprintf (stdout, "%s",_("Use at own risk.\n"));
665
666 fprintf (stdout, "%s",_("Usage:\n\n"));
667
668 for (i = 0; op_table[i].longopt != NULL; ++i) {
669
670 if (i == 63)
671 break;
672
673 if (op_table[i].shortopt != '-' &&
674 strchr(opts, op_table[i].shortopt) != NULL)
675 fprintf (stdout, "%s",_("Short option char collision !\n"));
676 opts[i] = op_table[i].shortopt;
677
678
679 if (op_table[i].hasArg == HAS_ARG_NO) {
680 if (sl_strlen(op_table[i].longopt) < 10)
681 sl_strlcpy(fmt,_("%c%c%c --%-s,\t\t\t %s\n"), sizeof(fmt));
682 else if (sl_strlen(op_table[i].longopt) < 17)
683 sl_strlcpy(fmt, _("%c%c%c --%-s,\t\t %s\n"), sizeof(fmt));
684 else
685 sl_strlcpy(fmt, _("%c%c%c --%-s,\t %s\n"), sizeof(fmt));
686 /* flawfinder: ignore */
687 fprintf (stdout, fmt,
688 (op_table[i].shortopt == '-') ? ' ' : '-',
689 (op_table[i].shortopt == '-') ? ' ' : op_table[i].shortopt,
690 (op_table[i].shortopt == '-') ? ' ' : ',',
691 _(op_table[i].longopt),
692 _(op_table[i].usage));
693 } else {
694 if (sl_strlen(op_table[i].longopt) < 12)
695 sl_strlcpy(fmt, _("%c%c %s --%-s=<arg>,\t\t %s\n"), sizeof(fmt));
696 else
697 sl_strlcpy(fmt, _("%c%c %s --%-s=<arg>,\t %s\n"), sizeof(fmt));
698 /* flawfinder: ignore */
699 fprintf (stdout, fmt,
700 (op_table[i].shortopt == '-') ? ' ' : '-',
701 (op_table[i].shortopt == '-') ? ' ' : op_table[i].shortopt,
702 (op_table[i].shortopt == '-') ? _(" ") : _("<arg>,"),
703 _(op_table[i].longopt),
704 _(op_table[i].usage));
705 }
706 }
707
708 fprintf (stdout, "%s",
709 _("\nPlease report bugs to support@la-samhna.de.\n"));
710
711 (void) fflush(stdout);
712
713 if ( dummy != NULL)
714 {
715 if (sl_strcmp( dummy, _("fail")) == 0 )
716 _exit (EXIT_FAILURE);
717 }
718
719 _exit (EXIT_SUCCESS);
720 /*@notreached@*/
721 return 0; /* make compilers happy */
722}
723
724#if defined (SH_WITH_CLIENT) || defined (SH_STANDALONE)
725static int sh_getopt_forever (const char * dummy)
726{
727 (void) dummy;
728 SL_ENTER(_("sh_getopt_forever"));
729 sh.flag.loop = S_TRUE;
730 SL_RETURN(0, _("sh_getopt_forever"));
731}
732#endif
733
734int sh_getopt_get (int argc, char * argv[])
735{
736 int count = 0;
737 size_t len = 0;
738 int foundit = 0;
739 int i;
740 size_t k;
741 char * theequal;
742
743 SL_ENTER(_("sh_getopt_get"));
744
745 /* -- Return if no args. --
746 */
747 if (argc < 2)
748 SL_RETURN(0, _("sh_getopt_get"));
749
750 while (argc > 1 && argv[1][0] == '-')
751 {
752
753 /* Initialize
754 */
755 foundit = 0;
756 len = sl_strlen (argv[1]);
757
758 /* a '-' with no argument: error
759 */
760 if (len == 1)
761 (void) sh_getopt_usage(_("fail"));
762
763 /* a '--' with no argument: stop argument processing
764 */
765 if (len == 2 && argv[1][1] == '-')
766 SL_RETURN( count, _("sh_getopt_get"));
767
768 /* a short option: process it
769 */
770 if (len >= 2 && argv[1][1] != '-')
771 {
772 for (k = 1; k < len; ++k)
773 {
774 for (i = 0; op_table[i].shortopt != '\0'; ++i)
775 {
776
777 if ( op_table[i].shortopt == argv[1][k] )
778 {
779 foundit = 1;
780 if ( op_table[i].hasArg == HAS_ARG_YES )
781 {
782 if (k != (len - 1))
783 {
784 /* not last option
785 */
786 fprintf (stderr, "%s",
787 _("Error: short option with argument is not last in option string\n"));
788 (void) sh_getopt_usage(_("fail"));
789 }
790 if (argc < 3)
791 {
792 /* argument required, but no avail
793 */
794 fprintf (stderr, "%s",
795 _("Error: missing argument\n"));
796 (void) sh_getopt_usage(_("fail"));
797 }
798 else
799 {
800 /* call function with argument */
801 --argc; ++argv;
802 if (NULL != op_table[i].func &&
803 0 != (* op_table[i].func )(argv[1]))
804 fprintf (stderr,
805 _("Error processing option -%c\n"),
806 op_table[i].shortopt);
807 break;
808 }
809 }
810 else
811 {
812 if (NULL != op_table[i].func &&
813 0 != (* op_table[i].func )(NULL))
814 fprintf (stderr,
815 _("Error processing option -%c\n"),
816 op_table[i].shortopt);
817 break;
818 }
819 }
820 }
821 }
822
823 /* 'break' should get here
824 */
825 if (foundit == 1)
826 {
827 --argc; ++argv;
828 continue;
829 }
830 else
831 {
832 /* unrecognized short option */
833 fprintf (stderr, "%s",_("Error: unrecognized short option\n"));
834 (void) sh_getopt_usage(_("fail"));
835 }
836 }
837
838 /* a long option: process it
839 */
840 if (len > 2)
841 {
842
843 for (i = 0; op_table[i].longopt != NULL; ++i)
844 {
845
846 if (sl_strncmp(_(op_table[i].longopt),
847 &argv[1][2],
848 sl_strlen(op_table[i].longopt)) == 0 )
849 {
850 foundit = 1;
851 if ( op_table[i].hasArg == HAS_ARG_YES )
852 {
853 theequal = strchr(argv[1], '=');
854 if (theequal == NULL)
855 {
856 if (argc < 3)
857 {
858 /* argument required, but no avail
859 */
860 fprintf (stderr, "%s",
861 _("Error: missing argument\n"));
862 (void) sh_getopt_usage(_("fail"));
863 }
864 else
865 {
866 /* call function with argument */
867 --argc; ++argv;
868 if (NULL != op_table[i].func &&
869 0 != (* op_table[i].func )(argv[1]))
870 fprintf (stderr,
871 _("Error processing option -%s\n"),
872 op_table[i].longopt);
873 break;
874 }
875 }
876 else
877 {
878 if (sl_strlen (theequal) > 1)
879 {
880 ++theequal;
881 /* call function with argument */
882 if (NULL != op_table[i].func &&
883 0 != (* op_table[i].func )(theequal))
884 fprintf (stderr,
885 _("Error processing option -%s\n"),
886 op_table[i].longopt);
887 break;
888 }
889 else
890 {
891 fprintf (stderr, "%s",
892 _("Error: invalid argument\n"));
893 /* argument required, but no avail */
894 (void) sh_getopt_usage(_("fail"));
895 }
896 }
897 }
898 else
899 {
900 if (NULL != op_table[i].func &&
901 0 != (* op_table[i].func )(NULL))
902 fprintf (stderr,
903 _("Error processing option -%s\n"),
904 op_table[i].longopt);
905 break;
906 }
907 }
908 }
909
910 /* 'break' should get here */
911 if (foundit == 1)
912 {
913 ++count;
914 --argc;
915 ++argv;
916 continue;
917 }
918 else
919 {
920 /* unrecognized long option */
921 fprintf (stderr, "%s",_("Error: unrecognized long option\n"));
922 (void) sh_getopt_usage(_("fail"));
923 }
924 }
925 }
926
927 SL_RETURN( count, _("sh_getopt_get"));
928}
Note: See TracBrowser for help on using the repository browser.