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>
42 static void *Realloc(void *p
, size_t size
)
46 if ((p2
= realloc(p
, size
)) == NULL
)
47 err(EXIT_FAILURE
, "%s", "realloc()");
52 /* This may be used in modules to keep a consistant time format with other
54 char *stamp(time_t epoch
, const char *format
)
56 static char buf
[TIMEBUFSIZE
];
59 t
= localtime(&epoch
);
60 strftime(buf
, sizeof(buf
), format
, t
);
65 * This may be used in modules to add a string to the buffer (ui_module_exec()).
67 void add_string(char ***buf
, const char *str
)
73 for (s
= *buf
; *s
; s
++)
78 s
= Realloc(s
, (i
+ 2) * sizeof(char *));
84 /* This is for the field separators (-F and -m). */
85 static int escapes(const char *str
)
125 /* Help text. Module help text is displayed after this. */
126 static void usage_header()
128 printf("Usage: %s [-vhVL] [-c <filename>] [-t fmt] [-m c] [-F c] [-d]\n"
129 "\t[[-xX] -O <module1> [options] [-- [-xX] -O <module2> [...]]]\n"
130 "\t[- | username | -f filename] [...]\n\n", __progname
);
133 /* Help text. Module help text is displayed before this. */
136 printf(" -d\tLoad the default modules (passwd.so, mail.so, and login.so).\n");
137 printf(" -c\tRead a configuration file. Can be used more than once.\n");
138 printf(" -O\tLoad a module. Can be used more than once.\n");
139 printf(" -x\tChain module1's output to module2's input.\n");
140 printf(" -X\tDon't output module1's info, only chain it.\n");
141 printf(" -F c\tSeparate output with the specified character "
142 "('%c').\n", delimchar
);
143 printf(" -m c\tSeparate multi-string values with the specified "
144 "character ('%c').\n", multichar
);
145 printf(" -t tf\tstrftime(3) time format ('%s').\n", DEFAULT_TIMEFORMAT
);
146 printf(" -f\tUsers are the owners of the specified files.\n");
147 printf(" -L\tFollow symbolic links.\n");
148 printf(" -v\tVerbose output when possible (twice for all modules).\n");
149 printf(" -h\tThis help text.\n");
150 printf(" -V\tVersion information.\n\n");
151 printf("Output key: %s=unknown/error, %s=none, %s=yes/on, "
152 "%s=no/off\n", UNKNOWN
, NONE
, ON
, OFF
);
156 * Add a module to the array of loaded modules. The index argument is the
157 * current item number being added stored in an integer. The module is also
158 * initialized here with ui_module_init().
160 static int open_module(char *filename
, int *idx
)
164 char *p
, s
[FILENAME_MAX
];
168 strncpy(s
, filename
, sizeof(s
));
170 if ((p
= strrchr(s
, '/')) != NULL
)
173 strncpy(s
, filename
, sizeof(s
));
177 for (i
= 0; i
< module_index
; i
++) {
178 if (strcmp(p
, modules
[i
].name
) == 0) {
179 if (TEST_FLAG(modules
[i
].flags
, MODULE_DUP
))
182 SET_FLAG(modules
[i
].flags
, MODULE_DUP
);
183 warnx("%s: a module by this name is already loaded", p
);
187 if ((m
= dlopen(filename
, RTLD_NOW
)) == NULL
) {
188 warnx("%s", dlerror());
194 modules
= Realloc(modules
, (module_index
+ 2) * sizeof(struct module
));
195 modules
[module_index
].m
= m
;
197 strncpy(modules
[module_index
].name
, p
, sizeof(modules
[module_index
].name
));
198 *idx
= module_index
++;
200 if ((init
= dlsym(modules
[*idx
].m
, "ui_module_init")) == NULL
)
201 warnx("%s", dlerror());
203 (*init
) (&chainable
);
206 SET_FLAG(modules
[*idx
].flags
, MODULE_CHAINABLE
);
208 if (*idx
- 1 >= 0 && TEST_FLAG(modules
[*idx
- 1].flags
, MODULE_CHAINED
) &&
209 !TEST_FLAG(modules
[*idx
].flags
, MODULE_CHAINABLE
)) {
210 warnx("%s: this module is not chainable", modules
[*idx
].name
);
214 /* Module chaining. See junction() for more info. */
216 SET_FLAG(modules
[*idx
].flags
, MODULE_CHAINED
);
219 SET_FLAG(modules
[*idx
].flags
, MODULE_OUTPUT
);
222 SET_FLAG(modules
[*idx
].flags
, MODULE_VERBOSE
);
226 verbose
= (verbose
< 2) ? 0 : 2;
230 /* This just free's up the array of modules. The modules should clean up after
231 * themselves via the ui_module_exit() function. */
232 static void cleanup_modules()
236 for (i
= 0; i
< module_index
; i
++) {
239 if ((e
= dlsym(modules
[i
].m
, "ui_module_exit")) == NULL
)
240 warnx("%s", dlerror());
244 dlclose(modules
[i
].m
);
250 static void output(char **s
, const int sep
, int which
)
263 printf("%c", which
== OUTPUT_DONE
? '\n' : sep
);
266 /* Pass the argument to each loaded module. */
267 static int junction(const char *arg
)
272 int ret
= EXIT_SUCCESS
;
276 if ((STAT(arg
, &st
)) == -1) {
283 if ((pw
= getpwuid(st
.st_uid
)) == NULL
) {
285 warnx("%s: no such user", arg
);
287 if (errno
== 0 || errno
== ENOENT
|| errno
== EPERM
288 || errno
== EBADF
|| errno
== ESRCH
)
289 warnx("%s: no such uid %u", arg
, st
.st_uid
);
291 warn("%s", "getpwuid()");
300 if ((pw
= getpwnam(arg
)) == NULL
) {
302 warnx("%s: no such user", arg
);
304 if (errno
== 0 || errno
== ENOENT
|| errno
== EPERM
305 || errno
== EBADF
|| errno
== ESRCH
)
306 warnx("%s: no such user", arg
);
308 warn("%s", "getpwnam()");
315 for (i
= 0; i
< module_index
; i
++) {
318 if ((m_exec
= dlsym(modules
[i
].m
, "ui_module_exec")) == NULL
) {
319 warnx("%s", dlerror());
323 ret
|= (*m_exec
) (&s
, pw
, multichar
,
324 TEST_FLAG(modules
[i
].flags
, MODULE_VERBOSE
), tf
);
326 if (!TEST_FLAG(modules
[i
].flags
, MODULE_CHAINED
) ||
327 (TEST_FLAG(modules
[i
].flags
, MODULE_CHAINED
) &&
328 TEST_FLAG(modules
[i
].flags
, MODULE_OUTPUT
))) {
330 ((i
+ 1) < module_index
) ? OUTPUT_APPEND
: OUTPUT_DONE
);
332 if (!TEST_FLAG(modules
[i
].flags
, MODULE_CHAINED
)) {
335 for (p
= s
; *p
; p
++) {
348 /* Copy options for each module into it's own argc and argv variables stopping
349 * at -- (getopt(3)). */
350 static int init_module_options(int the_argc
, char **the_argv
, struct module mod
)
354 module_options_init
*o
;
355 int old_optind
= optind
;
359 int ret
= EXIT_SUCCESS
;
360 char *optstring
= NULL
;
361 char *defaults
= NULL
;
362 int have_an_argument
= 0;
364 if ((o
= dlsym(mod
.m
, "ui_module_options_init")) == NULL
) {
365 warnx("%s", dlerror());
369 if ((optstring
= (*o
) (&defaults
))) {
370 argv
= Realloc(argv
, (argc
+ 2) * sizeof(char *));
371 argv
[argc
++] = strdup(__progname
);
374 /* Probably a default module. */
375 if (the_argv
== NULL
)
378 while ((opt
= getopt(the_argc
, the_argv
, optstring
)) != -1) {
381 warnx("%s: invalid option -- %c\n", mod
.name
,
388 argv
= Realloc(argv
, (argc
+ 2) * sizeof(char *));
389 snprintf(tmp
, sizeof(tmp
), "-%c%s", opt
, (optarg
) ? optarg
: "");
390 argv
[argc
++] = strdup(tmp
);
392 have_an_argument
= 1;
396 goto skip_option_stuff
;
400 * No options were specified for this module. Set the modules default
401 * options (ui_module_options_init()) if any.
403 if (!have_an_argument
&& defaults
) {
404 argv
= Realloc(argv
, (argc
+ 2) * sizeof(char *));
405 snprintf(tmp
, sizeof(tmp
), "-%s", defaults
);
406 argv
[argc
++] = strdup(tmp
);
411 opterr
= optind
= optopt
= 1;
413 if ((m
= dlsym(mod
.m
, "ui_module_options")) == NULL
) {
414 warnx("%s", dlerror());
418 ret
|= (*m
) (argc
, argv
);
428 * This will parse a line used as an argument list for the exec() line of
429 * functions returning a dynamically allocated array of character pointers so
430 * you should free() it afterwards. Both ' and " quoting is supported (with
431 * escapes) for multi-word arguments.
433 * This is my second attempt at it. Works alot better than the first. :)
436 * Ben Kibbey <bjk@luxsci.net>
439 * Modified to handle argv[0] and argc. (Ben Kibbey <bjk@luxsci.net>)
441 static char **parseargv(char *str
, const char *progname
, int *me_argc
)
454 if (!(pptr
= malloc(sizeof(char *))))
457 pptr
= Realloc(pptr
, (idx
+ 2) * sizeof(char *));
458 pptr
[idx
++] = strdup(progname
);
461 for (i
= 0, s
= str
; *s
; lastchar
= *s
++) {
462 if ((*s
== '\"' || *s
== '\'') && lastchar
!= '\\') {
463 quote
= (quote
) ? 0 : 1;
467 if (*s
== ' ' && !quote
) {
469 pptr
= Realloc(pptr
, (idx
+ 2) * sizeof(char *));
470 pptr
[idx
++] = strdup(arg
);
476 if ((i
+ 1) == sizeof(arg
))
485 pptr
= Realloc(pptr
, (idx
+ 2) * sizeof(char *));
486 pptr
[idx
++] = strdup(arg
);
495 static char *get_home_directory()
498 static char dir
[FILENAME_MAX
];
502 if ((pw
= getpwuid(getuid())) == NULL
) {
506 warnx("getpwuid(): no such uid");
511 strncpy(dir
, pw
->pw_dir
, sizeof(dir
));
515 /* Read in a configuration file adding modules to the module array and
516 * checking any module options. */
517 static int parse_rc_file(const char *filename
)
519 char line
[LINE_MAX
], *p
;
522 int old_optind
= optind
;
524 if ((fp
= fopen(filename
, "r")) == NULL
) {
525 warn("%s", filename
);
529 while ((p
= fgets(line
, sizeof(line
), fp
)) != NULL
) {
530 char name
[FILENAME_MAX
], options
[LINE_MAX
], tmp
[LINE_MAX
], *s
;
535 while (*p
&& isspace((unsigned char) *p
))
543 if (*p
== '>' || *p
== '-') {
552 while (*p
&& *p
!= ' ' && *p
!= '\t') {
568 while (*p
&& isspace((unsigned char) *p
))
574 if (*p
== '\n' || (*p
== '#' && lastchar
!= '\\'))
577 if (*p
== '#' && lastchar
== '\\') {
578 lastchar
= *--s
= *p
++;
583 lastchar
= *s
++ = *p
++;
590 s
= get_home_directory();
591 strncpy(tmp
, s
, sizeof(tmp
));
593 strncat(tmp
, p
, sizeof(tmp
)-strlen(tmp
)-1);
594 strncpy(name
, tmp
, sizeof(name
));
597 if (open_module(name
, &idx
))
600 if ((my_argv
= parseargv(options
, __progname
, &my_argc
)) == NULL
)
605 if (init_module_options(my_argc
, my_argv
, modules
[idx
])) {
617 int main(int argc
, char *argv
[])
620 int ret
= EXIT_SUCCESS
;
622 char line
[LINE_MAX
], *s
= NULL
;
625 #ifndef HAVE___PROGNAME
626 __progname
= argv
[0];
628 delimchar
= DEFAULT_DELIMINATING_CHAR
;
629 multichar
= DEFAULT_MULTI_CHAR
;
630 strncpy(tf
, DEFAULT_TIMEFORMAT
, sizeof(tf
));
633 while ((opt
= getopt(argc
, argv
, "+x:X:dm:c:hO:F:t:vVLf")) != -1) {
643 if (open_module("passwd.so", &i
) == 0) {
644 if (init_module_options(1, NULL
, modules
[i
]))
652 if (open_module("mail.so", &i
) == 0) {
653 if (init_module_options(1, NULL
, modules
[i
]))
661 if (open_module("login.so", &i
) == 0) {
662 if (init_module_options(1, NULL
, modules
[i
]))
672 if ((optarg
[0] != '\\' && strlen(optarg
) > 1) ||
673 (optarg
[0] == '\\' && strlen(optarg
) != 2)) {
678 if ((multichar
= escapes(optarg
)) == 0)
683 if ((ret
= parse_rc_file(optarg
)) != 0) {
691 if ((optarg
[0] != '\\' && strlen(optarg
) > 1) ||
692 (optarg
[0] == '\\' && strlen(optarg
) != 2)) {
697 if ((delimchar
= escapes(optarg
)) == 0)
702 strncpy(tf
, optarg
, sizeof(tf
));
705 printf("%s\n%s\n", PACKAGE_STRING
, COPYRIGHT
);
722 if (open_module(optarg
, &i
)) {
727 if (init_module_options(argc
, argv
, modules
[i
]))
731 * For modules which have no options at all (to keep getopt
732 * from interpreting the rest as arguments.
735 if (strcmp(argv
[optind
], "--") == 0)
747 /* The last module cannot be chained (syntax). */
748 if (!module_index
|| TEST_FLAG(modules
[module_index
- 1].flags
, MODULE_CHAINED
))
751 /* Cycle through the modules and output their help text. */
755 for (i
= 0; i
< module_index
; i
++) {
758 if (TEST_FLAG(modules
[i
].flags
, MODULE_DUP
))
761 if ((m_help
= dlsym(modules
[i
].m
, "ui_module_help")) == NULL
) {
762 warnx("%s", dlerror());
766 fprintf(stderr
, "%s\n", modules
[i
].name
);
775 if (argc
== optind
|| strcmp(argv
[optind
], "-") == 0) {
776 while ((s
= fgets(line
, sizeof(line
), stdin
)) != NULL
) {
777 if (s
[strlen(s
) - 1] == '\n')
778 s
[strlen(s
) - 1] = '\0';
784 for (; optind
< argc
; optind
++)
785 ret
|= junction(argv
[optind
]);