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
, int *idx
)
165 char *p
, s
[PATH_MAX
];
169 strncpy(s
, filename
, sizeof(s
));
171 if ((p
= strrchr(s
, '/')) != NULL
)
174 strncpy(s
, filename
, sizeof(s
));
178 for (i
= 0; i
< module_index
; i
++) {
179 if (strcmp(p
, modules
[i
].name
) == 0) {
180 if (TEST_FLAG(modules
[i
].flags
, MODULE_DUP
))
183 SET_FLAG(modules
[i
].flags
, MODULE_DUP
);
184 warnx("%s: a module by this name is already loaded", p
);
188 if ((m
= dlopen(filename
, RTLD_NOW
)) == NULL
) {
189 warnx("%s", dlerror());
195 modules
= Realloc(modules
, (module_index
+ 2) * sizeof(struct module
));
196 modules
[module_index
].m
= m
;
198 strncpy(modules
[module_index
].name
, p
, sizeof(modules
[module_index
].name
));
199 *idx
= module_index
++;
201 if ((init
= dlsym(modules
[*idx
].m
, "ui_module_init")) == NULL
)
202 warnx("%s", dlerror());
204 (*init
) (&chainable
);
207 SET_FLAG(modules
[*idx
].flags
, MODULE_CHAINABLE
);
209 if (*idx
- 1 >= 0 && TEST_FLAG(modules
[*idx
- 1].flags
, MODULE_CHAINED
) &&
210 !TEST_FLAG(modules
[*idx
].flags
, MODULE_CHAINABLE
)) {
211 warnx("%s: this module is not chainable", modules
[*idx
].name
);
215 /* Module chaining. See junction() for more info. */
217 SET_FLAG(modules
[*idx
].flags
, MODULE_CHAINED
);
220 SET_FLAG(modules
[*idx
].flags
, MODULE_OUTPUT
);
223 SET_FLAG(modules
[*idx
].flags
, MODULE_VERBOSE
);
227 verbose
= (verbose
< 2) ? 0 : 2;
231 /* This just free's up the array of modules. The modules should clean up after
232 * themselves via the ui_module_exit() function. */
233 static void cleanup_modules()
237 for (i
= 0; i
< module_index
; i
++) {
240 if ((e
= dlsym(modules
[i
].m
, "ui_module_exit")) == NULL
)
241 warnx("%s", dlerror());
245 dlclose(modules
[i
].m
);
251 static void output(char **s
, const int sep
, int which
)
264 printf("%c", which
== OUTPUT_DONE
? '\n' : sep
);
267 /* Pass the argument to each loaded module. */
268 static int junction(const char *arg
)
273 int ret
= EXIT_SUCCESS
;
277 if ((STAT(arg
, &st
)) == -1) {
284 if ((pw
= getpwuid(st
.st_uid
)) == NULL
) {
286 warnx("%s: no such user", arg
);
288 if (errno
== 0 || errno
== ENOENT
|| errno
== EPERM
289 || errno
== EBADF
|| errno
== ESRCH
)
290 warnx("%s: no such uid %u", arg
, st
.st_uid
);
292 warn("%s", "getpwuid()");
301 if ((pw
= getpwnam(arg
)) == NULL
) {
303 warnx("%s: no such user", arg
);
305 if (errno
== 0 || errno
== ENOENT
|| errno
== EPERM
306 || errno
== EBADF
|| errno
== ESRCH
)
307 warnx("%s: no such user", arg
);
309 warn("%s", "getpwnam()");
316 for (i
= 0; i
< module_index
; i
++) {
319 if ((m_exec
= dlsym(modules
[i
].m
, "ui_module_exec")) == NULL
) {
320 warnx("%s", dlerror());
324 ret
|= (*m_exec
) (&s
, pw
, multichar
,
325 TEST_FLAG(modules
[i
].flags
, MODULE_VERBOSE
), tf
);
327 if (!TEST_FLAG(modules
[i
].flags
, MODULE_CHAINED
) ||
328 (TEST_FLAG(modules
[i
].flags
, MODULE_CHAINED
) &&
329 TEST_FLAG(modules
[i
].flags
, MODULE_OUTPUT
))) {
331 ((i
+ 1) < module_index
) ? OUTPUT_APPEND
: OUTPUT_DONE
);
333 if (!TEST_FLAG(modules
[i
].flags
, MODULE_CHAINED
)) {
336 for (p
= s
; *p
; p
++) {
349 /* Copy options for each module into it's own argc and argv variables stopping
350 * at -- (getopt(3)). */
351 static int init_module_options(int the_argc
, char **the_argv
, struct module mod
)
355 module_options_init
*o
;
356 int old_optind
= optind
;
360 int ret
= EXIT_SUCCESS
;
361 char *optstring
= NULL
;
362 char *defaults
= NULL
;
363 int have_an_argument
= 0;
365 if ((o
= dlsym(mod
.m
, "ui_module_options_init")) == NULL
) {
366 warnx("%s", dlerror());
370 if ((optstring
= (*o
) (&defaults
))) {
371 argv
= Realloc(argv
, (argc
+ 2) * sizeof(char *));
372 argv
[argc
++] = strdup(__progname
);
375 /* Probably a default module. */
376 if (the_argv
== NULL
)
379 while ((opt
= getopt(the_argc
, the_argv
, optstring
)) != -1) {
382 warnx("%s: invalid option -- %c\n", mod
.name
,
389 argv
= Realloc(argv
, (argc
+ 2) * sizeof(char *));
390 snprintf(tmp
, sizeof(tmp
), "-%c%s", opt
, (optarg
) ? optarg
: "");
391 argv
[argc
++] = strdup(tmp
);
393 have_an_argument
= 1;
397 goto skip_option_stuff
;
401 * No options were specified for this module. Set the modules default
402 * options (ui_module_options_init()) if any.
404 if (!have_an_argument
&& defaults
) {
405 argv
= Realloc(argv
, (argc
+ 2) * sizeof(char *));
406 snprintf(tmp
, sizeof(tmp
), "-%s", defaults
);
407 argv
[argc
++] = strdup(tmp
);
412 opterr
= optind
= optopt
= 1;
414 if ((m
= dlsym(mod
.m
, "ui_module_options")) == NULL
) {
415 warnx("%s", dlerror());
419 ret
|= (*m
) (argc
, argv
);
429 * This will parse a line used as an argument list for the exec() line of
430 * functions returning a dynamically allocated array of character pointers so
431 * you should free() it afterwards. Both ' and " quoting is supported (with
432 * escapes) for multi-word arguments.
434 * This is my second attempt at it. Works alot better than the first. :)
437 * Ben Kibbey <bjk@luxsci.net>
440 * Modified to handle argv[0] and argc. (Ben Kibbey <bjk@luxsci.net>)
442 static char **parseargv(char *str
, const char *progname
, int *me_argc
)
455 if (!(pptr
= malloc(sizeof(char *))))
458 pptr
= Realloc(pptr
, (idx
+ 2) * sizeof(char *));
459 pptr
[idx
++] = strdup(progname
);
462 for (i
= 0, s
= str
; *s
; lastchar
= *s
++) {
463 if ((*s
== '\"' || *s
== '\'') && lastchar
!= '\\') {
464 quote
= (quote
) ? 0 : 1;
468 if (*s
== ' ' && !quote
) {
470 pptr
= Realloc(pptr
, (idx
+ 2) * sizeof(char *));
471 pptr
[idx
++] = strdup(arg
);
477 if ((i
+ 1) == sizeof(arg
))
486 pptr
= Realloc(pptr
, (idx
+ 2) * sizeof(char *));
487 pptr
[idx
++] = strdup(arg
);
496 static char *get_home_directory()
499 static char dir
[PATH_MAX
];
503 if ((pw
= getpwuid(getuid())) == NULL
) {
507 warnx("getpwuid(): no such uid");
512 strncpy(dir
, pw
->pw_dir
, sizeof(dir
));
516 /* Read in a configuration file adding modules to the module array and
517 * checking any module options. */
518 static int parse_rc_file(const char *filename
)
520 char line
[LINE_MAX
], *p
;
523 int old_optind
= optind
;
525 if ((fp
= fopen(filename
, "r")) == NULL
) {
526 warn("%s", filename
);
530 while ((p
= fgets(line
, sizeof(line
), fp
)) != NULL
) {
531 char name
[PATH_MAX
], options
[LINE_MAX
], tmp
[PATH_MAX
], *s
;
536 while (*p
&& isspace((unsigned char) *p
))
544 if (*p
== '>' || *p
== '-') {
553 while (*p
&& *p
!= ' ' && *p
!= '\t') {
569 while (*p
&& isspace((unsigned char) *p
))
575 if (*p
== '\n' || (*p
== '#' && lastchar
!= '\\'))
578 if (*p
== '#' && lastchar
== '\\') {
579 lastchar
= *--s
= *p
++;
584 lastchar
= *s
++ = *p
++;
591 s
= get_home_directory();
592 strncpy(tmp
, s
, sizeof(tmp
));
594 safe_strncat(tmp
, p
, sizeof(tmp
));
595 strncpy(name
, tmp
, sizeof(name
));
598 if (open_module(name
, &idx
))
601 if ((my_argv
= parseargv(options
, __progname
, &my_argc
)) == NULL
)
606 if (init_module_options(my_argc
, my_argv
, modules
[idx
])) {
618 int main(int argc
, char *argv
[])
621 int ret
= EXIT_SUCCESS
;
623 char line
[LINE_MAX
], *s
= NULL
;
626 #ifndef HAVE___PROGNAME
627 __progname
= argv
[0];
629 delimchar
= DEFAULT_DELIMINATING_CHAR
;
630 multichar
= DEFAULT_MULTI_CHAR
;
631 strncpy(tf
, DEFAULT_TIMEFORMAT
, sizeof(tf
));
634 while ((opt
= getopt(argc
, argv
, "+x:X:dm:c:hO:F:t:vVLf")) != -1) {
644 if (open_module("passwd.so", &i
) == 0) {
645 if (init_module_options(1, NULL
, modules
[i
]))
653 if (open_module("mail.so", &i
) == 0) {
654 if (init_module_options(1, NULL
, modules
[i
]))
662 if (open_module("login.so", &i
) == 0) {
663 if (init_module_options(1, NULL
, modules
[i
]))
673 if ((optarg
[0] != '\\' && strlen(optarg
) > 1) ||
674 (optarg
[0] == '\\' && strlen(optarg
) != 2)) {
679 if ((multichar
= escapes(optarg
)) == 0)
684 if ((ret
= parse_rc_file(optarg
)) != 0) {
692 if ((optarg
[0] != '\\' && strlen(optarg
) > 1) ||
693 (optarg
[0] == '\\' && strlen(optarg
) != 2)) {
698 if ((delimchar
= escapes(optarg
)) == 0)
703 strncpy(tf
, optarg
, sizeof(tf
));
706 printf("%s\n%s\n", PACKAGE_STRING
, COPYRIGHT
);
723 if (open_module(optarg
, &i
)) {
728 if (init_module_options(argc
, argv
, modules
[i
]))
732 * For modules which have no options at all (to keep getopt
733 * from interpreting the rest as arguments.
736 if (strcmp(argv
[optind
], "--") == 0)
748 /* The last module cannot be chained (syntax). */
749 if (!module_index
|| TEST_FLAG(modules
[module_index
- 1].flags
, MODULE_CHAINED
))
752 /* Cycle through the modules and output their help text. */
756 for (i
= 0; i
< module_index
; i
++) {
759 if (TEST_FLAG(modules
[i
].flags
, MODULE_DUP
))
762 if ((m_help
= dlsym(modules
[i
].m
, "ui_module_help")) == NULL
) {
763 warnx("%s", dlerror());
767 fprintf(stderr
, "%s\n", modules
[i
].name
);
776 if (argc
== optind
|| strcmp(argv
[optind
], "-") == 0) {
777 while ((s
= fgets(line
, sizeof(line
), stdin
)) != NULL
) {
778 if (s
[strlen(s
) - 1] == '\n')
779 s
[strlen(s
) - 1] = '\0';
785 for (; optind
< argc
; optind
++)
786 ret
|= junction(argv
[optind
]);