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>
40 #include "safe_strncat.c"
43 static void *Realloc(void *p
, size_t size
)
47 if ((p2
= realloc(p
, size
)) == NULL
)
48 err(EXIT_FAILURE
, "%s", "realloc()");
53 /* This may be used in modules to keep a consistant time format with other
55 char *stamp(time_t epoch
, const char *format
)
57 static char buf
[TIMEBUFSIZE
];
60 t
= localtime(&epoch
);
61 strftime(buf
, sizeof(buf
), format
, t
);
66 * This may be used in modules to add a string to the buffer (ui_module_exec()).
68 void add_string(char ***buf
, const char *str
)
74 for (s
= *buf
; *s
; s
++)
79 s
= Realloc(s
, (i
+ 2) * sizeof(char *));
85 /* This is for the field separators (-F and -m). */
86 static int escapes(const char *str
)
126 /* Help text. Module help text is displayed after this. */
127 static void usage_header()
129 printf("Usage: %s [-vhVL] [-c <filename>] [-t fmt] [-m c] [-F c] [-d]\n"
130 "\t[[-xX] -O <module1> [options] [-- [-xX] -O <module2> [...]]]\n"
131 "\t[- | username | -f filename] [...]\n\n", __progname
);
134 /* Help text. Module help text is displayed before this. */
137 printf(" -d\tLoad the default modules (passwd.so, mail.so, and login.so).\n");
138 printf(" -c\tRead a configuration file. Can be used more than once.\n");
139 printf(" -O\tLoad a module. Can be used more than once.\n");
140 printf(" -x\tChain module1's output to module2's input.\n");
141 printf(" -X\tDon't output module1's info, only chain it.\n");
142 printf(" -F c\tSeparate output with the specified character "
143 "('%c').\n", delimchar
);
144 printf(" -m c\tSeparate multi-string values with the specified "
145 "character ('%c').\n", multichar
);
146 printf(" -t tf\tstrftime(3) time format ('%s').\n", DEFAULT_TIMEFORMAT
);
147 printf(" -f\tUsers are the owners of the specified files.\n");
148 printf(" -L\tFollow symbolic links.\n");
149 printf(" -v\tVerbose output when possible (twice for all modules).\n");
150 printf(" -h\tThis help text.\n");
151 printf(" -V\tVersion information.\n\n");
152 printf("Output key: %s=unknown/error, %s=none, %s=yes/on, "
153 "%s=no/off\n", UNKNOWN
, NONE
, ON
, OFF
);
157 * Add a module to the array of loaded modules. The index argument is the
158 * current item number being added stored in an integer. The module is also
159 * initialized here with ui_module_init().
161 static int open_module(char *filename
, struct module_s
**ptr
)
165 char *p
, s
[PATH_MAX
];
167 struct module_s
*mod
, **mods
, *last
;
170 strncpy(s
, filename
, sizeof(s
));
172 if ((p
= strrchr(s
, '/')) != NULL
)
175 strncpy(s
, filename
, sizeof(s
));
179 for (mods
= modules
; mods
&& *mods
; mods
++) {
180 if (strcmp(p
, (*mods
)->name
) == 0) {
186 if ((m
= dlopen(filename
, RTLD_NOW
)) == NULL
) {
187 warnx("%s", dlerror());
193 last
= modules
? modules
[module_total
-1] : NULL
;
194 modules
= Realloc(modules
, (module_total
+2) * sizeof(struct module_s
*));
195 mod
= calloc(1, sizeof(struct module_s
));
196 modules
[module_total
++] = mod
;
197 modules
[module_total
] = NULL
;
199 strncpy(mod
->name
, p
, sizeof(mod
->name
));
201 if ((init
= dlsym(mod
->m
, "ui_module_init")) == NULL
)
202 warnx("%s", dlerror());
204 (*init
) (&chainable
);
207 SET_FLAG(mod
->flags
, MODULE_CHAINABLE
);
209 if (last
&& TEST_FLAG(last
->flags
, MODULE_CHAINED
) &&
210 !TEST_FLAG(mod
->flags
, MODULE_CHAINABLE
)) {
211 warnx("%s: this module is not chainable", mod
->name
);
213 modules
[module_total
] = NULL
;
218 /* Module chaining. See junction() for more info. */
220 SET_FLAG(mod
->flags
, MODULE_CHAINED
);
223 SET_FLAG(mod
->flags
, MODULE_OUTPUT
);
226 SET_FLAG(mod
->flags
, MODULE_VERBOSE
);
229 SET_FLAG(mod
->flags
, MODULE_DUP
);
230 warnx("%s: a module by this name is already loaded", p
);
235 verbose
= (verbose
< 2) ? 0 : 2;
240 /* This just free's up the array of modules. The modules should clean up after
241 * themselves via the ui_module_exit() function. */
242 static void cleanup_modules()
244 struct module_s
**mod
;
249 for (mod
= modules
; *mod
; mod
++) {
253 if ((e
= dlsym((*mod
)->m
, "ui_module_exit")) == NULL
)
254 warnx("%s", dlerror());
261 for (p
= (*mod
)->argv
; *p
; p
++)
273 static void output(char **s
, const int sep
, int which
)
286 printf("%c", which
== OUTPUT_DONE
? '\n' : sep
);
289 /* Pass the argument to each loaded module. */
290 static int junction(const char *arg
)
294 int ret
= EXIT_SUCCESS
;
296 struct module_s
**mod
;
299 if ((STAT(arg
, &st
)) == -1) {
306 if ((pw
= getpwuid(st
.st_uid
)) == NULL
) {
308 warnx("%s: no such user", arg
);
310 if (errno
== 0 || errno
== ENOENT
|| errno
== EPERM
311 || errno
== EBADF
|| errno
== ESRCH
)
312 warnx("%s: no such uid %u", arg
, st
.st_uid
);
314 warn("%s", "getpwuid()");
323 if ((pw
= getpwnam(arg
)) == NULL
) {
325 warnx("%s: no such user", arg
);
327 if (errno
== 0 || errno
== ENOENT
|| errno
== EPERM
328 || errno
== EBADF
|| errno
== ESRCH
)
329 warnx("%s: no such user", arg
);
331 warn("%s", "getpwnam()");
338 for (mod
= modules
; *mod
; mod
++) {
341 if ((m_exec
= dlsym((*mod
)->m
, "ui_module_exec")) == NULL
) {
342 warnx("%s", dlerror());
346 ret
|= (*m_exec
) (&s
, pw
, multichar
,
347 TEST_FLAG((*mod
)->flags
, MODULE_VERBOSE
), tf
);
349 if (!TEST_FLAG((*mod
)->flags
, MODULE_CHAINED
) ||
350 (TEST_FLAG((*mod
)->flags
, MODULE_CHAINED
) &&
351 TEST_FLAG((*mod
)->flags
, MODULE_OUTPUT
))) {
353 (*(mod
+1)) ? OUTPUT_APPEND
: OUTPUT_DONE
);
355 if (!TEST_FLAG((*mod
)->flags
, MODULE_CHAINED
)) {
358 for (p
= s
; *p
; p
++) {
371 /* Copy options for each module into it's own argc and argv variables stopping
372 * at -- (getopt(3)). */
373 static int init_module_options(int the_argc
, char **the_argv
,
374 struct module_s
*mod
)
378 module_options_init
*o
;
379 int old_optind
= optind
;
381 int ret
= EXIT_SUCCESS
;
382 char *optstring
= NULL
;
383 char *defaults
= NULL
;
384 int have_an_argument
= 0;
386 if ((o
= dlsym(mod
->m
, "ui_module_options_init")) == NULL
) {
387 warnx("%s", dlerror());
391 if ((optstring
= (*o
) (&defaults
))) {
392 mod
->argv
= Realloc(mod
->argv
, (mod
->argc
+ 2) * sizeof(char *));
393 mod
->argv
[mod
->argc
++] = strdup(__progname
);
394 mod
->argv
[mod
->argc
] = NULL
;
396 /* Probably a default module. */
397 if (the_argv
== NULL
)
400 while ((opt
= getopt(the_argc
, the_argv
, optstring
)) != -1) {
403 warnx("%s: invalid option -- %c\n", mod
->name
,
410 mod
->argv
= Realloc(mod
->argv
, (mod
->argc
+ 2) * sizeof(char *));
411 snprintf(tmp
, sizeof(tmp
), "-%c%s", opt
, (optarg
) ? optarg
: "");
412 mod
->argv
[mod
->argc
++] = strdup(tmp
);
413 mod
->argv
[mod
->argc
] = NULL
;
414 have_an_argument
= 1;
418 goto skip_option_stuff
;
422 * No options were specified for this module. Set the modules default
423 * options (ui_module_options_init()) if any.
425 if (!have_an_argument
&& defaults
) {
426 mod
->argv
= Realloc(mod
->argv
, (mod
->argc
+ 2) * sizeof(char *));
427 snprintf(tmp
, sizeof(tmp
), "-%s", defaults
);
428 mod
->argv
[mod
->argc
++] = strdup(tmp
);
429 mod
->argv
[mod
->argc
] = NULL
;
433 opterr
= optind
= optopt
= 1;
435 if ((m
= dlsym(mod
->m
, "ui_module_options")) == NULL
) {
436 warnx("%s", dlerror());
440 ret
|= (*m
) (mod
->argc
, mod
->argv
);
450 * This will parse a line used as an argument list for the exec() line of
451 * functions returning a dynamically allocated array of character pointers so
452 * you should free() it afterwards. Both ' and " quoting is supported (with
453 * escapes) for multi-word arguments.
455 * This is my second attempt at it. Works alot better than the first. :)
458 * Ben Kibbey <bjk@luxsci.net>
461 * Modified to handle argv[0] and argc. (Ben Kibbey <bjk@luxsci.net>)
463 static char **parseargv(char *str
, const char *progname
, int *me_argc
)
476 if (!(pptr
= malloc(sizeof(char *))))
479 pptr
= Realloc(pptr
, (idx
+ 2) * sizeof(char *));
480 pptr
[idx
++] = strdup(progname
);
483 for (i
= 0, s
= str
; *s
; lastchar
= *s
++) {
484 if ((*s
== '\"' || *s
== '\'') && lastchar
!= '\\') {
485 quote
= (quote
) ? 0 : 1;
489 if (*s
== ' ' && !quote
) {
491 pptr
= Realloc(pptr
, (idx
+ 2) * sizeof(char *));
492 pptr
[idx
++] = strdup(arg
);
498 if ((i
+ 1) == sizeof(arg
))
507 pptr
= Realloc(pptr
, (idx
+ 2) * sizeof(char *));
508 pptr
[idx
++] = strdup(arg
);
517 static char *get_home_directory()
520 static char dir
[PATH_MAX
];
524 if ((pw
= getpwuid(getuid())) == NULL
) {
528 warnx("getpwuid(): no such uid");
533 strncpy(dir
, pw
->pw_dir
, sizeof(dir
));
537 /* Read in a configuration file adding modules to the module array and
538 * checking any module options. */
539 static int parse_rc_file(const char *filename
)
541 char line
[LINE_MAX
], *p
;
543 int old_optind
= optind
;
544 struct module_s
*mod
;
546 if ((fp
= fopen(filename
, "r")) == NULL
) {
547 warn("%s", filename
);
551 while ((p
= fgets(line
, sizeof(line
), fp
)) != NULL
) {
552 char name
[PATH_MAX
], options
[LINE_MAX
], tmp
[PATH_MAX
], *s
;
554 char **my_argv
, **ap
;
558 while (*p
&& isspace((unsigned char) *p
))
566 if (*p
== '>' || *p
== '-') {
575 while (*p
&& *p
!= ' ' && *p
!= '\t') {
591 while (*p
&& isspace((unsigned char) *p
))
597 if (*p
== '\n' || (*p
== '#' && lastchar
!= '\\'))
600 if (*p
== '#' && lastchar
== '\\') {
601 lastchar
= *--s
= *p
++;
606 lastchar
= *s
++ = *p
++;
613 s
= get_home_directory();
614 strncpy(tmp
, s
, sizeof(tmp
));
616 safe_strncat(tmp
, p
, sizeof(tmp
));
617 strncpy(name
, tmp
, sizeof(name
));
620 if (open_module(name
, &mod
))
623 if ((my_argv
= parseargv(options
, __progname
, &my_argc
)) == NULL
)
627 n
= init_module_options(my_argc
, my_argv
, mod
);
629 for (ap
= my_argv
; *ap
; ap
++)
646 int main(int argc
, char *argv
[])
648 int ret
= EXIT_SUCCESS
;
650 char line
[LINE_MAX
], *s
= NULL
;
653 #ifndef HAVE___PROGNAME
654 __progname
= argv
[0];
656 delimchar
= DEFAULT_DELIMINATING_CHAR
;
657 multichar
= DEFAULT_MULTI_CHAR
;
658 strncpy(tf
, DEFAULT_TIMEFORMAT
, sizeof(tf
));
661 while ((opt
= getopt(argc
, argv
, "+x:X:dm:c:hO:F:t:vVLf")) != -1) {
662 struct module_s
*mod
;
671 if (open_module("passwd.so", &mod
) == 0) {
672 if (init_module_options(1, NULL
, mod
))
680 if (open_module("mail.so", &mod
) == 0) {
681 if (init_module_options(1, NULL
, mod
))
689 if (open_module("login.so", &mod
) == 0) {
690 if (init_module_options(1, NULL
, mod
))
700 if ((optarg
[0] != '\\' && strlen(optarg
) > 1) ||
701 (optarg
[0] == '\\' && strlen(optarg
) != 2)) {
706 if ((multichar
= escapes(optarg
)) == 0)
711 if ((ret
= parse_rc_file(optarg
)) != 0) {
719 if ((optarg
[0] != '\\' && strlen(optarg
) > 1) ||
720 (optarg
[0] == '\\' && strlen(optarg
) != 2)) {
725 if ((delimchar
= escapes(optarg
)) == 0)
730 strncpy(tf
, optarg
, sizeof(tf
));
733 printf("%s\n%s\n", PACKAGE_STRING
, COPYRIGHT
);
750 if (open_module(optarg
, &mod
)) {
755 if (init_module_options(argc
, argv
, mod
))
759 * For modules which have no options at all (to keep getopt
760 * from interpreting the rest as arguments.
763 if (strcmp(argv
[optind
], "--") == 0)
775 /* The last module cannot be chained (syntax). */
776 if (!module_total
|| TEST_FLAG(modules
[module_total
-1]->flags
, MODULE_CHAINED
))
779 /* Cycle through the modules and output their help text. */
782 struct module_s
**mod
;
784 for (mod
= modules
; mod
&& *mod
; mod
++) {
787 if (TEST_FLAG((*mod
)->flags
, MODULE_DUP
))
790 if ((m_help
= dlsym((*mod
)->m
, "ui_module_help")) == NULL
) {
791 warnx("%s", dlerror());
795 printf("%s\n", (*mod
)->name
);
804 if (argc
== optind
|| strcmp(argv
[optind
], "-") == 0) {
805 while ((s
= fgets(line
, sizeof(line
), stdin
)) != NULL
) {
806 if (s
[strlen(s
) - 1] == '\n')
807 s
[strlen(s
) - 1] = '\0';
813 for (; optind
< argc
; optind
++)
814 ret
|= junction(argv
[optind
]);