1
/* -*- coding: utf-8 -*- */
3
* Mandos plugin runner - Run Mandos plugins
5
* Copyright © 2007-2008 Teddy Hogeborn & Björn Påhlsson
7
* This program is free software: you can redistribute it and/or
8
* modify it under the terms of the GNU General Public License as
9
* published by the Free Software Foundation, either version 3 of the
10
* License, or (at your option) any later version.
12
* This program is distributed in the hope that it will be useful, but
13
* WITHOUT ANY WARRANTY; without even the implied warranty of
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15
* General Public License for more details.
17
* You should have received a copy of the GNU General Public License
18
* along with this program. If not, see
19
* <http://www.gnu.org/licenses/>.
21
* Contact the authors at <mandos@fukt.bsnet.se>.
24
#define _GNU_SOURCE /* TEMP_FAILURE_RETRY() */
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() */
42
#include <iso646.h> /* and, or, not */
43
#include <dirent.h> /* DIR, struct dirent, opendir(),
44
readdir(), closedir(), dirfd() */
45
#include <unistd.h> /* struct stat, stat(), S_ISREG(),
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> /* strtok, strlen(), strcpy(),
56
#include <errno.h> /* errno */
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 */
67
#define BUFFER_SIZE 256
69
const char *argp_program_version = "mandos-client 1.0";
70
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
74
typedef struct process{
86
typedef struct plugin{
87
char *name; /* can be NULL or any plugin name */
94
static plugin *getplugin(char *name, plugin **plugin_list){
95
for (plugin *p = *plugin_list; p != NULL; p = p->next){
97
or (p->name and name and (strcmp(p->name, name) == 0))){
101
/* Create a new plugin */
102
plugin *new_plugin = malloc(sizeof(plugin));
103
if (new_plugin == NULL){
107
new_plugin->name = name;
108
new_plugin->argv = malloc(sizeof(char *) * 2);
109
if (new_plugin->argv == NULL){
113
new_plugin->argv[0] = name;
114
new_plugin->argv[1] = NULL;
115
new_plugin->argc = 1;
116
new_plugin->disabled = false;
117
new_plugin->next = *plugin_list;
118
/* Append the new plugin to the list */
119
*plugin_list = new_plugin;
123
static void addargument(plugin *p, char *arg){
124
p->argv[p->argc] = arg;
125
p->argv = realloc(p->argv, sizeof(char *) * (size_t)(p->argc + 2));
126
if (p->argv == NULL){
131
p->argv[p->argc] = NULL;
135
* Based on the example in the GNU LibC manual chapter 13.13 "File
137
* *Note File Descriptor Flags:(libc)Descriptor Flags.
139
static int set_cloexec_flag(int fd)
141
int ret = fcntl(fd, F_GETFD, 0);
142
/* If reading the flags failed, return error indication now. */
146
/* Store modified flag word in the descriptor. */
147
return fcntl(fd, F_SETFD, ret | FD_CLOEXEC);
150
process *process_list = NULL;
152
/* Mark a process as completed when it exits, and save its exit
154
void handle_sigchld(__attribute__((unused)) int sig){
155
process *proc = process_list;
157
pid_t pid = wait(&status);
162
while(proc != NULL and proc->pid != pid){
166
/* Process not found in process list */
169
proc->status = status;
170
proc->completed = true;
173
bool print_out_password(const char *buffer, size_t length){
175
for(size_t written = 0; written < length; written += ret){
176
ret = TEMP_FAILURE_RETRY(write(STDOUT_FILENO, buffer + written,
185
int main(int argc, char *argv[]){
186
const char *plugindir = "/conf/conf.d/mandos/plugins.d";
189
struct dirent *dirst;
196
int exitstatus = EXIT_SUCCESS;
197
struct sigaction old_sigchld_action;
198
struct sigaction sigchld_action = { .sa_handler = handle_sigchld,
199
.sa_flags = SA_NOCLDSTOP };
200
char *plus_options = NULL;
201
char **plus_argv = NULL;
203
/* Establish a signal handler */
204
sigemptyset(&sigchld_action.sa_mask);
205
ret = sigaddset(&sigchld_action.sa_mask, SIGCHLD);
210
ret = sigaction(SIGCHLD, &sigchld_action, &old_sigchld_action);
216
/* The options we understand. */
217
struct argp_option options[] = {
218
{ .name = "global-options", .key = 'g',
219
.arg = "OPTION[,OPTION[,...]]",
220
.doc = "Options passed to all plugins" },
221
{ .name = "options-for", .key = 'o',
222
.arg = "PLUGIN:OPTION[,OPTION[,...]]",
223
.doc = "Options passed only to specified plugin" },
224
{ .name = "disable", .key = 'd',
226
.doc = "Disable a specific plugin", .group = 1 },
227
{ .name = "plugin-dir", .key = 128,
229
.doc = "Specify a different plugin directory", .group = 2 },
230
{ .name = "userid", .key = 129,
231
.arg = "ID", .flags = 0,
232
.doc = "User ID the plugins will run as", .group = 2 },
233
{ .name = "groupid", .key = 130,
234
.arg = "ID", .flags = 0,
235
.doc = "Group ID the plugins will run as", .group = 2 },
236
{ .name = "debug", .key = 131,
237
.doc = "Debug mode", .group = 3 },
241
error_t parse_opt (int key, char *arg, struct argp_state *state) {
242
/* Get the INPUT argument from `argp_parse', which we know is a
243
pointer to our plugin list pointer. */
244
plugin **plugins = state->input;
248
char *p = strtok(arg, ",");
250
addargument(getplugin(NULL, plugins), p);
251
p = strtok(NULL, ",");
257
char *name = strtok(arg, ":");
258
char *p = strtok(NULL, ":");
262
addargument(getplugin(name, plugins), p);
263
p = strtok(NULL, ",");
270
getplugin(arg, plugins)->disabled = true;
277
uid = (uid_t)strtol(arg, NULL, 10);
280
gid = (gid_t)strtol(arg, NULL, 10);
286
if(plus_options != NULL or arg == NULL or arg[0] != '+'){
294
return ARGP_ERR_UNKNOWN;
299
plugin *plugin_list = NULL;
301
struct argp argp = { .options = options, .parser = parse_opt,
302
.args_doc = "[+PLUS_SEPARATED_OPTIONS]",
303
.doc = "Mandos plugin runner -- Run plugins" };
305
ret = argp_parse (&argp, argc, argv, 0, 0, &plugin_list);
306
if (ret == ARGP_ERR_UNKNOWN){
307
fprintf(stderr, "Unkown error while parsing arguments\n");
308
exitstatus = EXIT_FAILURE;
313
/* This is a mangled argument in the form of
314
"+--option+--other-option=parameter+--yet-another-option", etc */
315
/* Make new argc and argv vars, and call argp_parse() again. */
316
plus_options++; /* skip the first '+' character */
317
const char delims[] = "+";
320
plus_argv = malloc(sizeof(char*) * 2);
321
if(plus_argv == NULL){
323
exitstatus = EXIT_FAILURE;
326
plus_argv[0] = argv[0];
328
arg = strtok(plus_options, delims); /* Get first argument */
331
plus_argv = realloc(plus_argv, sizeof(char *)
332
* ((unsigned int) new_argc + 1));
333
if(plus_argv == NULL){
335
exitstatus = EXIT_FAILURE;
338
plus_argv[new_argc-1] = arg;
339
plus_argv[new_argc] = NULL;
340
arg = strtok(NULL, delims); /* Get next argument */
342
ret = argp_parse (&argp, new_argc, plus_argv, 0, 0, &plugin_list);
343
if (ret == ARGP_ERR_UNKNOWN){
344
fprintf(stderr, "Unkown error while parsing arguments\n");
345
exitstatus = EXIT_FAILURE;
351
for(plugin *p = plugin_list; p != NULL; p=p->next){
352
fprintf(stderr, "Plugin: %s has %d arguments\n",
353
p->name ? p->name : "Global", p->argc - 1);
354
for(char **a = p->argv; *a != NULL; a++){
355
fprintf(stderr, "\tArg: %s\n", *a);
370
dir = opendir(plugindir);
372
perror("Could not open plugin dir");
373
exitstatus = EXIT_FAILURE;
377
/* Set the FD_CLOEXEC flag on the directory, if possible */
379
int dir_fd = dirfd(dir);
381
ret = set_cloexec_flag(dir_fd);
383
perror("set_cloexec_flag");
384
exitstatus = EXIT_FAILURE;
393
dirst = readdir(dir);
395
// All directory entries have been processed
399
exitstatus = EXIT_FAILURE;
405
d_name_len = strlen(dirst->d_name);
407
// Ignore dotfiles, backup files and other junk
409
bool bad_name = false;
411
const char const *bad_prefixes[] = { ".", "#", NULL };
413
const char const *bad_suffixes[] = { "~", "#", ".dpkg-new",
415
".dpkg-divert", NULL };
416
for(const char **pre = bad_prefixes; *pre != NULL; pre++){
417
size_t pre_len = strlen(*pre);
418
if((d_name_len >= pre_len)
419
and strncmp((dirst->d_name), *pre, pre_len) == 0){
421
fprintf(stderr, "Ignoring plugin dir entry \"%s\""
422
" with bad prefix %s\n", dirst->d_name, *pre);
433
for(const char **suf = bad_suffixes; *suf != NULL; suf++){
434
size_t suf_len = strlen(*suf);
435
if((d_name_len >= suf_len)
436
and (strcmp((dirst->d_name)+d_name_len-suf_len, *suf)
439
fprintf(stderr, "Ignoring plugin dir entry \"%s\""
440
" with bad suffix %s\n", dirst->d_name, *suf);
452
char *filename = malloc(d_name_len + strlen(plugindir) + 2);
453
if (filename == NULL){
455
exitstatus = EXIT_FAILURE;
458
strcpy(filename, plugindir); /* Spurious warning */
459
strcat(filename, "/"); /* Spurious warning */
460
strcat(filename, dirst->d_name); /* Spurious warning */
462
ret = stat(filename, &st);
465
exitstatus = EXIT_FAILURE;
469
if (not S_ISREG(st.st_mode) or (access(filename, X_OK) != 0)){
471
fprintf(stderr, "Ignoring plugin dir entry \"%s\""
472
" with bad type or mode\n", filename);
477
if(getplugin(dirst->d_name, &plugin_list)->disabled){
479
fprintf(stderr, "Ignoring disabled plugin \"%s\"\n",
485
plugin *p = getplugin(dirst->d_name, &plugin_list);
487
/* Add global arguments to argument list for this plugin */
488
plugin *g = getplugin(NULL, &plugin_list);
489
for(char **a = g->argv + 1; *a != NULL; a++){
497
exitstatus = EXIT_FAILURE;
500
ret = set_cloexec_flag(pipefd[0]);
502
perror("set_cloexec_flag");
503
exitstatus = EXIT_FAILURE;
506
ret = set_cloexec_flag(pipefd[1]);
508
perror("set_cloexec_flag");
509
exitstatus = EXIT_FAILURE;
512
/* Block SIGCHLD until process is safely in process list */
513
ret = sigprocmask (SIG_BLOCK, &sigchld_action.sa_mask, NULL);
515
perror("sigprocmask");
516
exitstatus = EXIT_FAILURE;
519
// Starting a new process to be watched
522
/* this is the child process */
523
ret = sigaction(SIGCHLD, &old_sigchld_action, NULL);
528
ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
530
perror("sigprocmask");
534
ret = dup2(pipefd[1], STDOUT_FILENO); /* replace our stdout */
541
/* If dir has no file descriptor, we could not set FD_CLOEXEC
542
above and must now close it manually here. */
545
if(execv(filename, p->argv) < 0){
553
close(pipefd[1]); /* close unused write end of pipe */
554
process *new_process = malloc(sizeof(process));
555
if (new_process == NULL){
557
ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
559
perror("sigprocmask");
561
exitstatus = EXIT_FAILURE;
565
*new_process = (struct process){ .pid = pid,
567
.next = process_list };
569
process_list = new_process;
570
/* Unblock SIGCHLD so signal handler can be run if this process
571
has already completed */
572
ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
574
perror("sigprocmask");
575
exitstatus = EXIT_FAILURE;
579
FD_SET(new_process->fd, &rfds_all);
581
if (maxfd < new_process->fd){
582
maxfd = new_process->fd;
587
/* Free the plugin list */
588
for(plugin *next; plugin_list != NULL; plugin_list = next){
589
next = plugin_list->next;
590
free(plugin_list->argv);
597
if (process_list == NULL){
598
fprintf(stderr, "No plugin processes started. Incorrect plugin"
603
fd_set rfds = rfds_all;
604
int select_ret = select(maxfd+1, &rfds, NULL, NULL, NULL);
605
if (select_ret == -1){
607
exitstatus = EXIT_FAILURE;
610
/* OK, now either a process completed, or something can be read
612
for(process *proc = process_list; proc ; proc = proc->next){
613
/* Is this process completely done? */
614
if(proc->eof and proc->completed){
615
/* Only accept the plugin output if it exited cleanly */
616
if(not WIFEXITED(proc->status)
617
or WEXITSTATUS(proc->status) != 0){
618
/* Bad exit by plugin */
620
if(WIFEXITED(proc->status)){
621
fprintf(stderr, "Plugin %u exited with status %d\n",
622
(unsigned int) (proc->pid),
623
WEXITSTATUS(proc->status));
624
} else if(WIFSIGNALED(proc->status)) {
625
fprintf(stderr, "Plugin %u killed by signal %d\n",
626
(unsigned int) (proc->pid),
627
WTERMSIG(proc->status));
628
} else if(WCOREDUMP(proc->status)){
629
fprintf(stderr, "Plugin %d dumped core\n",
630
(unsigned int) (proc->pid));
633
/* Remove the plugin */
634
FD_CLR(proc->fd, &rfds_all);
635
/* Block signal while modifying process_list */
636
ret = sigprocmask (SIG_BLOCK, &sigchld_action.sa_mask, NULL);
638
perror("sigprocmask");
639
exitstatus = EXIT_FAILURE;
642
/* Delete this process entry from the list */
643
if(process_list == proc){
644
/* First one - simple */
645
process_list = proc->next;
647
/* Second one or later */
648
for(process *p = process_list; p != NULL; p = p->next){
650
p->next = proc->next;
655
/* We are done modifying process list, so unblock signal */
656
ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask,
659
perror("sigprocmask");
663
/* We deleted this process from the list, so we can't go
664
proc->next. Therefore, start over from the beginning of
668
/* This process exited nicely, so print its buffer */
670
bool bret = print_out_password(proc->buffer, proc->buffer_length);
672
perror("print_out_password");
673
exitstatus = EXIT_FAILURE;
677
/* This process has not completed. Does it have any output? */
678
if(proc->eof or not FD_ISSET(proc->fd, &rfds)){
679
/* This process had nothing to say at this time */
682
/* Before reading, make the process' data buffer large enough */
683
if(proc->buffer_length + BUFFER_SIZE > proc->buffer_size){
684
proc->buffer = realloc(proc->buffer, proc->buffer_size
685
+ (size_t) BUFFER_SIZE);
686
if (proc->buffer == NULL){
688
exitstatus = EXIT_FAILURE;
691
proc->buffer_size += BUFFER_SIZE;
693
/* Read from the process */
694
ret = read(proc->fd, proc->buffer + proc->buffer_length,
697
/* Read error from this process; ignore the error */
704
proc->buffer_length += (size_t) ret;
709
if(process_list == NULL){
711
fprintf(stderr, "Going to fallback mode using getpass(3)\n");
712
char *passwordbuffer = getpass("Password: ");
713
bret = print_out_password(passwordbuffer, strlen(passwordbuffer));
715
perror("print_out_password");
716
exitstatus = EXIT_FAILURE;
719
bret = print_out_password("\n", 1);
721
perror("print_out_password");
722
exitstatus = EXIT_FAILURE;
728
/* Restore old signal handler */
729
sigaction(SIGCHLD, &old_sigchld_action, NULL);
733
/* Free the plugin list */
734
for(plugin *next; plugin_list != NULL; plugin_list = next){
735
next = plugin_list->next;
736
free(plugin_list->argv);
744
/* Free the process list and kill the processes */
745
for(process *next; process_list != NULL; process_list = next){
746
next = process_list->next;
747
close(process_list->fd);
748
ret = kill(process_list->pid, SIGTERM);
749
if(ret == -1 and errno != ESRCH){
750
/* set-uid proccesses migth not get closed */
753
free(process_list->buffer);
757
/* Wait for any remaining child processes to terminate */