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
69
const char *argp_program_version = "plugin-runner 1.0";
70
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
57
74
typedef struct process{
152
170
proc->completed = true;
173
bool print_out_password(const char *buffer, size_t length){
175
if(length>0 and buffer[length-1] == '\n'){
178
for(size_t written = 0; written < length; written += (size_t)ret){
179
ret = TEMP_FAILURE_RETRY(write(STDOUT_FILENO, buffer + written,
155
188
int main(int argc, char *argv[]){
156
const char *plugindir = "/conf/conf.d/mandos/plugins.d";
189
const char *plugindir = "/lib/mandos/plugins.d";
157
190
size_t d_name_len;
159
192
struct dirent *dirst;
162
195
int ret, maxfd = 0;
163
198
bool debug = false;
164
199
int exitstatus = EXIT_SUCCESS;
200
struct sigaction old_sigchld_action;
201
struct sigaction sigchld_action = { .sa_handler = handle_sigchld,
202
.sa_flags = SA_NOCLDSTOP };
203
char *plus_options = NULL;
204
char **plus_argv = NULL;
166
206
/* Establish a signal handler */
167
struct sigaction old_sigchld_action,
168
sigchld_action = { .sa_handler = handle_sigchld,
169
.sa_flags = SA_NOCLDSTOP };
170
207
sigemptyset(&sigchld_action.sa_mask);
171
208
ret = sigaddset(&sigchld_action.sa_mask, SIGCHLD);
193
230
{ .name = "plugin-dir", .key = 128,
194
231
.arg = "DIRECTORY",
195
232
.doc = "Specify a different plugin directory", .group = 2 },
196
{ .name = "debug", .key = 129,
233
{ .name = "userid", .key = 129,
234
.arg = "ID", .flags = 0,
235
.doc = "User ID the plugins will run as", .group = 2 },
236
{ .name = "groupid", .key = 130,
237
.arg = "ID", .flags = 0,
238
.doc = "Group ID the plugins will run as", .group = 2 },
239
{ .name = "debug", .key = 131,
197
240
.doc = "Debug mode", .group = 3 },
207
250
if (arg != NULL){
208
char *p = strtok(arg, ",");
252
while((p = strsep(&arg, ",")) != NULL){
253
if(strcmp(p, "") == 0){
210
256
addargument(getplugin(NULL, plugins), p);
211
p = strtok(NULL, ",");
216
261
if (arg != NULL){
217
char *name = strtok(arg, ":");
218
char *p = strtok(NULL, ":");
262
char *name = strsep(&arg, ":");
263
if(strcmp(name, "") == 0){
266
char *opt = strsep(&arg, ":");
267
if(strcmp(opt, "") == 0){
272
while((p = strsep(&opt, ",")) != NULL){
273
if(strcmp(p, "") == 0){
222
276
addargument(getplugin(name, plugins), p);
223
p = strtok(NULL, ",");
250
312
plugin *plugin_list = NULL;
252
314
struct argp argp = { .options = options, .parser = parse_opt,
315
.args_doc = "[+PLUS_SEPARATED_OPTIONS]",
254
316
.doc = "Mandos plugin runner -- Run plugins" };
256
argp_parse (&argp, argc, argv, 0, 0, &plugin_list);
318
ret = argp_parse (&argp, argc, argv, 0, 0, &plugin_list);
319
if (ret == ARGP_ERR_UNKNOWN){
320
fprintf(stderr, "Unknown error while parsing arguments\n");
321
exitstatus = EXIT_FAILURE;
326
/* This is a mangled argument in the form of
327
"+--option+--other-option=parameter+--yet-another-option", etc */
328
/* Make new argc and argv vars, and call argp_parse() again. */
329
plus_options++; /* skip the first '+' character */
330
const char delims[] = "+";
333
plus_argv = malloc(sizeof(char*) * 2);
334
if(plus_argv == NULL){
336
exitstatus = EXIT_FAILURE;
339
plus_argv[0] = argv[0];
342
while((arg = strsep(&plus_options, delims)) != NULL){
344
plus_argv = realloc(plus_argv, sizeof(char *)
345
* ((unsigned int) new_argc + 1));
346
if(plus_argv == NULL){
348
exitstatus = EXIT_FAILURE;
351
plus_argv[new_argc-1] = arg;
352
plus_argv[new_argc] = NULL;
355
ret = argp_parse (&argp, new_argc, plus_argv, 0, 0, &plugin_list);
356
if (ret == ARGP_ERR_UNKNOWN){
357
fprintf(stderr, "Unknown error while parsing arguments\n");
358
exitstatus = EXIT_FAILURE;
259
364
for(plugin *p = plugin_list; p != NULL; p=p->next){
501
635
/* Bad exit by plugin */
503
637
if(WIFEXITED(proc->status)){
504
fprintf(stderr, "Plugin %d exited with status %d\n",
505
proc->pid, WEXITSTATUS(proc->status));
638
fprintf(stderr, "Plugin %u exited with status %d\n",
639
(unsigned int) (proc->pid),
640
WEXITSTATUS(proc->status));
506
641
} else if(WIFSIGNALED(proc->status)) {
507
fprintf(stderr, "Plugin %d killed by signal %d\n",
508
proc->pid, WTERMSIG(proc->status));
642
fprintf(stderr, "Plugin %u killed by signal %d\n",
643
(unsigned int) (proc->pid),
644
WTERMSIG(proc->status));
509
645
} else if(WCOREDUMP(proc->status)){
510
fprintf(stderr, "Plugin %d dumped core\n", proc->pid);
646
fprintf(stderr, "Plugin %d dumped core\n",
647
(unsigned int) (proc->pid));
513
650
/* Remove the plugin */
594
if(process_list == NULL){
595
fprintf(stderr, "All plugin processes failed, exiting\n");
596
exitstatus = EXIT_FAILURE;
729
if(process_list == NULL or exitstatus != EXIT_SUCCESS){
730
/* Fallback if all plugins failed, none are found or an error occured */
732
fprintf(stderr, "Going to fallback mode using getpass(3)\n");
733
char *passwordbuffer = getpass("Password: ");
734
bret = print_out_password(passwordbuffer, strlen(passwordbuffer));
736
perror("print_out_password");
737
exitstatus = EXIT_FAILURE;
600
742
/* Restore old signal handler */
601
743
sigaction(SIGCHLD, &old_sigchld_action, NULL);
603
747
/* Free the plugin list */
604
748
for(plugin *next; plugin_list != NULL; plugin_list = next){
605
749
next = plugin_list->next;