2 Copyright (C) 2001-2011 Ben Kibbey <bjk@luxsci.net>
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02110-1301 USA
25 #include <sys/types.h>
35 #ifdef _POSIX2_LINE_MAX
36 #define LINE_MAX _POSIX2_LINE_MAX
51 #include "safe_strncat.c"
54 static void *Realloc(void *p
, size_t size
)
58 if ((p2
= realloc(p
, size
)) == NULL
)
59 err(EXIT_FAILURE
, "%s", "realloc()");
64 /* This may be used in modules to keep a consistant time format with other
66 char *stamp(time_t epoch
, const char *format
)
68 static char buf
[TIMEBUFSIZE
];
71 t
= localtime(&epoch
);
72 strftime(buf
, sizeof(buf
), format
, t
);
77 * This may be used in modules to add a string to the buffer (ui_module_exec()).
79 void add_string(char ***buf
, const char *str
)
85 for (s
= *buf
; *s
; s
++)
90 s
= Realloc(s
, (i
+ 2) * sizeof(char *));
96 /* This is for the field separators (-F and -m). */
97 static int escapes(const char *str
)
137 /* Help text. Module help text is displayed after this. */
138 static void usage_header()
140 printf("Usage: %s [-vhVL] [-c <filename>] [-t fmt] [-m c] [-F c] [-d]\n"
141 "\t[[-xX] -O <module1> [options] [-- [-xX] -O <module2> [...]]]\n"
142 "\t[- | username | -f filename] [...]\n\n", __progname
);
145 /* Help text. Module help text is displayed before this. */
148 printf(" -d\tLoad the default modules (passwd.so, mail.so, and login.so).\n");
149 printf(" -c\tRead a configuration file. Can be used more than once.\n");
150 printf(" -O\tLoad a module. Can be used more than once.\n");
151 printf(" -x\tChain module1's output to module2's input.\n");
152 printf(" -X\tDon't output module1's info, only chain it.\n");
153 printf(" -F c\tSeparate output with the specified character "
154 "('%c').\n", delimchar
);
155 printf(" -m c\tSeparate multi-string values with the specified "
156 "character ('%c').\n", multichar
);
157 printf(" -t tf\tstrftime(3) time format ('%s').\n", DEFAULT_TIMEFORMAT
);
158 printf(" -f\tUsers are the owners of the specified files.\n");
159 printf(" -L\tFollow symbolic links.\n");
160 printf(" -v\tVerbose output when possible (twice for all modules).\n");
161 printf(" -h\tThis help text.\n");
162 printf(" -V\tVersion information.\n\n");
163 printf("Output key: %s=unknown/error, %s=none, %s=yes/on, "
164 "%s=no/off\n", UNKNOWN
, NONE
, ON
, OFF
);
168 * Add a module to the array of loaded modules. The index argument is the
169 * current item number being added stored in an integer. The module is also
170 * initialized here with ui_module_init().
172 static int open_module(char *filename
, struct module_s
**ptr
)
176 char *p
, s
[PATH_MAX
];
178 struct module_s
*mod
, **mods
, *last
;
181 strncpy(s
, filename
, sizeof(s
));
183 if ((p
= strrchr(s
, '/')) != NULL
)
186 strncpy(s
, filename
, sizeof(s
));
190 for (mods
= modules
; mods
&& *mods
; mods
++) {
191 if (strcmp(p
, (*mods
)->name
) == 0) {
197 if ((m
= dlopen(filename
, RTLD_NOW
)) == NULL
) {
198 warnx("%s", dlerror());
204 last
= modules
? modules
[module_total
-1] : NULL
;
205 modules
= Realloc(modules
, (module_total
+2) * sizeof(struct module_s
*));
206 mod
= calloc(1, sizeof(struct module_s
));
207 modules
[module_total
++] = mod
;
208 modules
[module_total
] = NULL
;
210 strncpy(mod
->name
, p
, sizeof(mod
->name
));
212 if ((init
= dlsym(mod
->m
, "ui_module_init")) == NULL
)
213 warnx("%s", dlerror());
215 (*init
) (&chainable
);
218 SET_FLAG(mod
->flags
, MODULE_CHAINABLE
);
220 if (last
&& TEST_FLAG(last
->flags
, MODULE_CHAINED
) &&
221 !TEST_FLAG(mod
->flags
, MODULE_CHAINABLE
)) {
222 warnx("%s: this module is not chainable", mod
->name
);
224 modules
[module_total
] = NULL
;
229 /* Module chaining. See junction() for more info. */
231 SET_FLAG(mod
->flags
, MODULE_CHAINED
);
234 SET_FLAG(mod
->flags
, MODULE_OUTPUT
);
237 SET_FLAG(mod
->flags
, MODULE_VERBOSE
);
240 SET_FLAG(mod
->flags
, MODULE_DUP
);
241 warnx("%s: a module by this name is already loaded", p
);
246 verbose
= (verbose
< 2) ? 0 : 2;
251 /* This just free's up the array of modules. The modules should clean up after
252 * themselves via the ui_module_exit() function. */
253 static void cleanup_modules()
255 struct module_s
**mod
;
260 for (mod
= modules
; *mod
; mod
++) {
264 if ((e
= dlsym((*mod
)->m
, "ui_module_exit")) == NULL
)
265 warnx("%s", dlerror());
272 for (p
= (*mod
)->argv
; *p
; p
++)
284 static void output(char **s
, const int sep
, int which
)
297 printf("%c", which
== OUTPUT_DONE
? '\n' : sep
);
300 /* Pass the argument to each loaded module. */
301 static int junction(const char *arg
)
305 int ret
= EXIT_SUCCESS
;
307 struct module_s
**mod
;
310 if ((STAT(arg
, &st
)) == -1) {
317 if ((pw
= getpwuid(st
.st_uid
)) == NULL
) {
319 warnx("%s: no such user", arg
);
321 if (errno
== 0 || errno
== ENOENT
|| errno
== EPERM
322 || errno
== EBADF
|| errno
== ESRCH
)
323 warnx("%s: no such uid %u", arg
, st
.st_uid
);
325 warn("%s", "getpwuid()");
334 if ((pw
= getpwnam(arg
)) == NULL
) {
336 warnx("%s: no such user", arg
);
338 if (errno
== 0 || errno
== ENOENT
|| errno
== EPERM
339 || errno
== EBADF
|| errno
== ESRCH
)
340 warnx("%s: no such user", arg
);
342 warn("%s", "getpwnam()");
349 for (mod
= modules
; *mod
; mod
++) {
352 if ((m_exec
= dlsym((*mod
)->m
, "ui_module_exec")) == NULL
) {
353 warnx("%s", dlerror());
357 ret
|= (*m_exec
) (&s
, pw
, multichar
,
358 TEST_FLAG((*mod
)->flags
, MODULE_VERBOSE
), tf
);
360 if (!TEST_FLAG((*mod
)->flags
, MODULE_CHAINED
) ||
361 (TEST_FLAG((*mod
)->flags
, MODULE_CHAINED
) &&
362 TEST_FLAG((*mod
)->flags
, MODULE_OUTPUT
))) {
364 (*(mod
+1)) ? OUTPUT_APPEND
: OUTPUT_DONE
);
366 if (!TEST_FLAG((*mod
)->flags
, MODULE_CHAINED
)) {
369 for (p
= s
; *p
; p
++) {
382 /* Copy options for each module into it's own argc and argv variables stopping
383 * at -- (getopt(3)). */
384 static int init_module_options(int the_argc
, char **the_argv
,
385 struct module_s
*mod
)
389 module_options_init
*o
;
390 int old_optind
= optind
;
392 int ret
= EXIT_SUCCESS
;
393 char *optstring
= NULL
;
394 char *defaults
= NULL
;
395 int have_an_argument
= 0;
397 if ((o
= dlsym(mod
->m
, "ui_module_options_init")) == NULL
) {
398 warnx("%s", dlerror());
402 if ((optstring
= (*o
) (&defaults
))) {
403 mod
->argv
= Realloc(mod
->argv
, (mod
->argc
+ 2) * sizeof(char *));
404 mod
->argv
[mod
->argc
++] = strdup(__progname
);
405 mod
->argv
[mod
->argc
] = NULL
;
407 /* Probably a default module. */
408 if (the_argv
== NULL
)
411 while ((opt
= getopt(the_argc
, the_argv
, optstring
)) != -1) {
414 warnx("%s: invalid option -- %c\n", mod
->name
,
421 mod
->argv
= Realloc(mod
->argv
, (mod
->argc
+ 2) * sizeof(char *));
422 snprintf(tmp
, sizeof(tmp
), "-%c%s", opt
, (optarg
) ? optarg
: "");
423 mod
->argv
[mod
->argc
++] = strdup(tmp
);
424 mod
->argv
[mod
->argc
] = NULL
;
425 have_an_argument
= 1;
429 goto skip_option_stuff
;
433 * No options were specified for this module. Set the modules default
434 * options (ui_module_options_init()) if any.
436 if (!have_an_argument
&& defaults
) {
437 mod
->argv
= Realloc(mod
->argv
, (mod
->argc
+ 2) * sizeof(char *));
438 snprintf(tmp
, sizeof(tmp
), "-%s", defaults
);
439 mod
->argv
[mod
->argc
++] = strdup(tmp
);
440 mod
->argv
[mod
->argc
] = NULL
;
444 opterr
= optind
= optopt
= 1;
446 if ((m
= dlsym(mod
->m
, "ui_module_options")) == NULL
) {
447 warnx("%s", dlerror());
451 ret
|= (*m
) (mod
->argc
, mod
->argv
);
461 * This will parse a line used as an argument list for the exec() line of
462 * functions returning a dynamically allocated array of character pointers so
463 * you should free() it afterwards. Both ' and " quoting is supported (with
464 * escapes) for multi-word arguments.
466 * This is my second attempt at it. Works alot better than the first. :)
469 * Ben Kibbey <bjk@luxsci.net>
472 * Modified to handle argv[0] and argc. (Ben Kibbey <bjk@luxsci.net>)
474 static char **parseargv(char *str
, const char *progname
, int *me_argc
)
487 if (!(pptr
= malloc(sizeof(char *))))
490 pptr
= Realloc(pptr
, (idx
+ 2) * sizeof(char *));
491 pptr
[idx
++] = strdup(progname
);
494 for (i
= 0, s
= str
; *s
; lastchar
= *s
++) {
495 if ((*s
== '\"' || *s
== '\'') && lastchar
!= '\\') {
496 quote
= (quote
) ? 0 : 1;
500 if (*s
== ' ' && !quote
) {
502 pptr
= Realloc(pptr
, (idx
+ 2) * sizeof(char *));
503 pptr
[idx
++] = strdup(arg
);
509 if ((i
+ 1) == sizeof(arg
))
518 pptr
= Realloc(pptr
, (idx
+ 2) * sizeof(char *));
519 pptr
[idx
++] = strdup(arg
);
528 static char *get_home_directory()
531 static char dir
[PATH_MAX
];
535 if ((pw
= getpwuid(getuid())) == NULL
) {
539 warnx("getpwuid(): no such uid");
544 strncpy(dir
, pw
->pw_dir
, sizeof(dir
));
548 /* Read in a configuration file adding modules to the module array and
549 * checking any module options. */
550 static int parse_rc_file(const char *filename
)
552 char line
[LINE_MAX
], *p
;
554 int old_optind
= optind
;
555 struct module_s
*mod
;
557 if ((fp
= fopen(filename
, "r")) == NULL
) {
558 warn("%s", filename
);
562 while ((p
= fgets(line
, sizeof(line
), fp
)) != NULL
) {
563 char name
[PATH_MAX
], options
[LINE_MAX
], tmp
[PATH_MAX
], *s
;
565 char **my_argv
, **ap
;
569 while (*p
&& isspace((unsigned char) *p
))
577 if (*p
== '>' || *p
== '-') {
586 while (*p
&& *p
!= ' ' && *p
!= '\t') {
602 while (*p
&& isspace((unsigned char) *p
))
608 if (*p
== '\n' || (*p
== '#' && lastchar
!= '\\'))
611 if (*p
== '#' && lastchar
== '\\') {
612 lastchar
= *--s
= *p
++;
617 lastchar
= *s
++ = *p
++;
624 s
= get_home_directory();
625 strncpy(tmp
, s
, sizeof(tmp
));
627 safe_strncat(tmp
, p
, sizeof(tmp
));
628 strncpy(name
, tmp
, sizeof(name
));
631 if (open_module(name
, &mod
))
634 if ((my_argv
= parseargv(options
, __progname
, &my_argc
)) == NULL
)
638 n
= init_module_options(my_argc
, my_argv
, mod
);
640 for (ap
= my_argv
; *ap
; ap
++)
657 int main(int argc
, char *argv
[])
659 int ret
= EXIT_SUCCESS
;
661 char line
[LINE_MAX
], *s
= NULL
;
664 #ifndef HAVE___PROGNAME
665 __progname
= argv
[0];
667 delimchar
= DEFAULT_DELIMINATING_CHAR
;
668 multichar
= DEFAULT_MULTI_CHAR
;
669 strncpy(tf
, DEFAULT_TIMEFORMAT
, sizeof(tf
));
672 while ((opt
= getopt(argc
, argv
, "+x:X:dm:c:hO:F:t:vVLf")) != -1) {
673 struct module_s
*mod
;
682 if (open_module("passwd.so", &mod
) == 0) {
683 if (init_module_options(1, NULL
, mod
))
691 if (open_module("mail.so", &mod
) == 0) {
692 if (init_module_options(1, NULL
, mod
))
700 if (open_module("login.so", &mod
) == 0) {
701 if (init_module_options(1, NULL
, mod
))
711 if ((optarg
[0] != '\\' && strlen(optarg
) > 1) ||
712 (optarg
[0] == '\\' && strlen(optarg
) != 2)) {
717 if ((multichar
= escapes(optarg
)) == 0)
722 if ((ret
= parse_rc_file(optarg
)) != 0) {
730 if ((optarg
[0] != '\\' && strlen(optarg
) > 1) ||
731 (optarg
[0] == '\\' && strlen(optarg
) != 2)) {
736 if ((delimchar
= escapes(optarg
)) == 0)
741 strncpy(tf
, optarg
, sizeof(tf
));
744 printf("%s\n%s\n", PACKAGE_STRING
, COPYRIGHT
);
761 if (open_module(optarg
, &mod
)) {
766 if (init_module_options(argc
, argv
, mod
))
770 * For modules which have no options at all (to keep getopt
771 * from interpreting the rest as arguments.
774 if (strcmp(argv
[optind
], "--") == 0)
786 /* The last module cannot be chained (syntax). */
787 if (!module_total
|| TEST_FLAG(modules
[module_total
-1]->flags
, MODULE_CHAINED
))
790 /* Cycle through the modules and output their help text. */
793 struct module_s
**mod
;
795 for (mod
= modules
; mod
&& *mod
; mod
++) {
798 if (TEST_FLAG((*mod
)->flags
, MODULE_DUP
))
801 if ((m_help
= dlsym((*mod
)->m
, "ui_module_help")) == NULL
) {
802 warnx("%s", dlerror());
806 printf("%s\n", (*mod
)->name
);
815 if (argc
== optind
|| strcmp(argv
[optind
], "-") == 0) {
816 while ((s
= fgets(line
, sizeof(line
), stdin
)) != NULL
) {
817 if (s
[strlen(s
) - 1] == '\n')
818 s
[strlen(s
) - 1] = '\0';
824 for (; optind
< argc
; optind
++)
825 ret
|= junction(argv
[optind
]);