24
24
#define _GNU_SOURCE /* TEMP_FAILURE_RETRY() */
26
#include <stdio.h> /* popen(), fileno(), fprintf(),
27
stderr, STDOUT_FILENO */
26
#include <stddef.h> /* size_t, NULL */
27
#include <stdlib.h> /* malloc(), exit(), EXIT_FAILURE,
28
EXIT_SUCCESS, realloc() */
29
#include <stdbool.h> /* bool, true, false */
30
#include <stdio.h> /* perror, popen(), fileno(),
31
fprintf(), stderr, STDOUT_FILENO */
32
#include <sys/types.h> /* DIR, opendir(), stat(), struct
33
stat, waitpid(), WIFEXITED(),
34
WEXITSTATUS(), wait(), pid_t,
35
uid_t, gid_t, getuid(), getgid(),
37
#include <sys/select.h> /* fd_set, select(), FD_ZERO(),
38
FD_SET(), FD_ISSET(), FD_CLR */
39
#include <sys/wait.h> /* wait(), waitpid(), WIFEXITED(),
41
#include <sys/stat.h> /* struct stat, stat(), S_ISREG() */
28
42
#include <iso646.h> /* and, or, not */
29
#include <sys/types.h> /* DIR, opendir(), stat(),
30
struct stat, waitpid(),
31
WIFEXITED(), WEXITSTATUS(),
33
#include <sys/wait.h> /* wait() */
34
43
#include <dirent.h> /* DIR, struct dirent, opendir(),
35
readdir(), closedir() */
36
#include <sys/stat.h> /* struct stat, stat(), S_ISREG() */
44
readdir(), closedir(), dirfd() */
37
45
#include <unistd.h> /* struct stat, stat(), S_ISREG(),
39
#include <fcntl.h> /* fcntl() */
40
#include <stddef.h> /* NULL */
41
#include <stdlib.h> /* EXIT_FAILURE */
42
#include <sys/select.h> /* fd_set, select(), FD_ZERO(),
43
FD_SET(), FD_ISSET() */
44
#include <string.h> /* strlen(), strcpy(), strcat() */
45
#include <stdbool.h> /* true */
46
#include <sys/wait.h> /* waitpid(), WIFEXITED(),
46
fcntl(), setuid(), setgid(),
47
F_GETFD, F_SETFD, FD_CLOEXEC,
48
access(), pipe(), fork(), close()
49
dup2, STDOUT_FILENO, _exit(),
50
execv(), write(), read(),
52
#include <fcntl.h> /* fcntl(), F_GETFD, F_SETFD,
54
#include <string.h> /* strsep, strlen(), strcpy(),
48
56
#include <errno.h> /* errno */
49
#include <argp.h> /* struct argp_option,
50
struct argp_state, struct argp,
57
#include <argp.h> /* struct argp_option, struct
58
argp_state, struct argp,
59
argp_parse(), ARGP_ERR_UNKNOWN,
60
ARGP_KEY_END, ARGP_KEY_ARG, error_t */
61
#include <signal.h> /* struct sigaction, sigemptyset(),
62
sigaddset(), sigaction(),
63
sigprocmask(), SIG_BLOCK, SIGCHLD,
64
SIG_UNBLOCK, kill() */
65
#include <errno.h> /* errno, EBADF */
53
67
#define BUFFER_SIZE 256
68
#define ARGFILE "/conf/conf.d/mandos/plugins.conf"
70
const char *argp_program_version = "plugin-runner 1.0";
71
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
152
171
proc->completed = true;
174
bool print_out_password(const char *buffer, size_t length){
176
if(length>0 and buffer[length-1] == '\n'){
179
for(size_t written = 0; written < length; written += (size_t)ret){
180
ret = TEMP_FAILURE_RETRY(write(STDOUT_FILENO, buffer + written,
189
char ** addcustomargument(char **argv, int *argc, char *arg){
193
argv = malloc(sizeof(char*) * 2);
197
argv[0] = NULL; /* Will be set to argv[0] in main before parsing */
201
argv = realloc(argv, sizeof(char *)
202
* ((unsigned int) *argc + 1));
155
211
int main(int argc, char *argv[]){
156
const char *plugindir = "/conf/conf.d/mandos/plugins.d";
212
const char *plugindir = "/lib/mandos/plugins.d";
213
const char *argfile = ARGFILE;
157
215
size_t d_name_len;
159
217
struct dirent *dirst;
272
337
.args_doc = "[+PLUS_SEPARATED_OPTIONS]",
273
338
.doc = "Mandos plugin runner -- Run plugins" };
275
argp_parse (&argp, argc, argv, 0, 0, &plugin_list);
278
/* This is a mangled argument in the form of
279
"+--option+--other-option=parameter+--yet-another-option", etc */
280
/* Make new argc and argv vars, and call argp_parse() again. */
281
plus_options++; /* skip the first '+' character */
282
const char delims[] = "+";
285
plus_argv = malloc(sizeof(char*) * 2);
286
if(plus_argv == NULL){
288
exitstatus = EXIT_FAILURE;
291
plus_argv[0] = argv[0];
293
arg = strtok(plus_options, delims); /* Get first argument */
296
plus_argv = realloc(plus_argv, sizeof(char *)
297
* ((unsigned int) new_argc + 1));
298
if(plus_argv == NULL){
300
exitstatus = EXIT_FAILURE;
303
plus_argv[new_argc-1] = arg;
304
plus_argv[new_argc] = NULL;
305
arg = strtok(NULL, delims); /* Get next argument */
307
argp_parse (&argp, new_argc, plus_argv, 0, 0, &plugin_list);
340
ret = argp_parse (&argp, argc, argv, 0, 0, &plugin_list);
341
if (ret == ARGP_ERR_UNKNOWN){
342
fprintf(stderr, "Unknown error while parsing arguments\n");
343
exitstatus = EXIT_FAILURE;
347
conffp = fopen(argfile, "r");
349
char *org_line = NULL;
352
char *p, *arg, *new_arg, *line;
353
const char whitespace_delims[] = " \r\t\f\v\n";
354
const char comment_delim[] = "#";
357
sret = getline(&org_line, &size, conffp);
363
arg = strsep(&line, comment_delim);
364
while((p = strsep(&arg, whitespace_delims)) != NULL){
369
custom_argv = addcustomargument(custom_argv, &custom_argc, new_arg);
370
if (custom_argv == NULL){
371
perror("addcustomargument");
372
exitstatus = EXIT_FAILURE;
379
/* Check for harmful errors and go to fallback. Other errors might
380
not affect opening plugins */
381
if (errno == EMFILE or errno == ENFILE or errno == ENOMEM){
383
exitstatus = EXIT_FAILURE;
388
if(custom_argv != NULL){
389
custom_argv[0] = argv[0];
390
ret = argp_parse (&argp, custom_argc, custom_argv, 0, 0, &plugin_list);
391
if (ret == ARGP_ERR_UNKNOWN){
392
fprintf(stderr, "Unknown error while parsing arguments\n");
393
exitstatus = EXIT_FAILURE;
563
670
/* Bad exit by plugin */
565
672
if(WIFEXITED(proc->status)){
566
fprintf(stderr, "Plugin %d exited with status %d\n",
567
proc->pid, WEXITSTATUS(proc->status));
673
fprintf(stderr, "Plugin %u exited with status %d\n",
674
(unsigned int) (proc->pid),
675
WEXITSTATUS(proc->status));
568
676
} else if(WIFSIGNALED(proc->status)) {
569
fprintf(stderr, "Plugin %d killed by signal %d\n",
570
proc->pid, WTERMSIG(proc->status));
677
fprintf(stderr, "Plugin %u killed by signal %d\n",
678
(unsigned int) (proc->pid),
679
WTERMSIG(proc->status));
571
680
} else if(WCOREDUMP(proc->status)){
572
fprintf(stderr, "Plugin %d dumped core\n", proc->pid);
681
fprintf(stderr, "Plugin %d dumped core\n",
682
(unsigned int) (proc->pid));
575
685
/* Remove the plugin */
656
if(process_list == NULL){
657
fprintf(stderr, "All plugin processes failed, exiting\n");
658
exitstatus = EXIT_FAILURE;
764
if(process_list == NULL or exitstatus != EXIT_SUCCESS){
765
/* Fallback if all plugins failed, none are found or an error occured */
767
fprintf(stderr, "Going to fallback mode using getpass(3)\n");
768
char *passwordbuffer = getpass("Password: ");
769
bret = print_out_password(passwordbuffer, strlen(passwordbuffer));
771
perror("print_out_password");
772
exitstatus = EXIT_FAILURE;
662
777
/* Restore old signal handler */
663
778
sigaction(SIGCHLD, &old_sigchld_action, NULL);
667
782
/* Free the plugin list */
668
783
for(plugin *next; plugin_list != NULL; plugin_list = next){