56
56
#include <argp.h> /* struct argp_option, struct
57
57
argp_state, struct argp,
58
58
argp_parse(), ARGP_ERR_UNKNOWN,
59
ARGP_KEY_END, ARGP_KEY_ARG, error_t */
59
ARGP_KEY_END, ARGP_KEY_ARG,
60
61
#include <signal.h> /* struct sigaction, sigemptyset(),
61
62
sigaddset(), sigaction(),
62
63
sigprocmask(), SIG_BLOCK, SIGCHLD,
64
65
#include <errno.h> /* errno, EBADF */
66
67
#define BUFFER_SIZE 256
67
#define ARGFILE "/conf/conf.d/mandos/plugin-runner.conf"
69
#define PDIR "/lib/mandos/plugins.d"
70
#define AFILE "/conf/conf.d/mandos/plugin-runner.conf"
69
72
const char *argp_program_version = "plugin-runner 1.0";
70
73
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
114
125
new_plugin->argv = malloc(sizeof(char *) * 2);
115
126
if (new_plugin->argv == NULL){
116
128
free(new_plugin);
119
new_plugin->argv[0] = name;
131
new_plugin->argv[0] = copy_name;
120
132
new_plugin->argv[1] = NULL;
122
134
new_plugin->environ = malloc(sizeof(char *));
123
135
if(new_plugin->environ == NULL){
124
137
free(new_plugin->argv);
125
138
free(new_plugin);
190
203
process *process_list = NULL;
192
/* Mark a process as completed when it exits, and save its exit
205
/* Mark processes as completed when they exit, and save their exit
194
207
void handle_sigchld(__attribute__((unused)) int sig){
195
process *proc = process_list;
197
pid_t pid = wait(&status);
202
while(proc != NULL and proc->pid != pid){
206
/* Process not found in process list */
209
proc->status = status;
210
proc->completed = true;
209
process *proc = process_list;
211
pid_t pid = waitpid(-1, &status, WNOHANG);
213
/* Only still running child processes */
217
if (errno != ECHILD){
220
/* No child processes */
224
/* A child exited, find it in process_list */
225
while(proc != NULL and proc->pid != pid){
229
/* Process not found in process list */
232
proc->status = status;
233
proc->completed = true;
213
237
bool print_out_password(const char *buffer, size_t length){
228
char **add_to_argv(char **argv, int *argc, char *arg){
231
argv = malloc(sizeof(char*) * 2);
235
argv[0] = NULL; /* Will be set to argv[0] in main before parsing */
239
argv = realloc(argv, sizeof(char *)
240
* ((unsigned int) *argc + 1));
252
static void free_plugin_list(plugin *plugin_list){
253
for(plugin *next; plugin_list != NULL; plugin_list = next){
254
next = plugin_list->next;
255
for(char **arg = plugin_list->argv; *arg != NULL; arg++){
258
free(plugin_list->argv);
259
for(char **env = plugin_list->environ; *env != NULL; env++){
262
free(plugin_list->environ);
249
267
int main(int argc, char *argv[]){
250
const char *plugindir = "/lib/mandos/plugins.d";
251
const char *argfile = ARGFILE;
268
char *plugindir = NULL;
269
char *argfile = NULL;
253
271
size_t d_name_len;
269
287
/* Establish a signal handler */
270
288
sigemptyset(&sigchld_action.sa_mask);
271
289
ret = sigaddset(&sigchld_action.sa_mask, SIGCHLD);
273
291
perror("sigaddset");
292
exitstatus = EXIT_FAILURE;
276
295
ret = sigaction(SIGCHLD, &sigchld_action, &old_sigchld_action);
278
297
perror("sigaction");
298
exitstatus = EXIT_FAILURE;
282
302
/* The options we understand. */
299
319
{ .name = "plugin-dir", .key = 128,
300
320
.arg = "DIRECTORY",
301
321
.doc = "Specify a different plugin directory", .group = 2 },
302
{ .name = "userid", .key = 129,
303
.arg = "ID", .flags = 0,
304
.doc = "User ID the plugins will run as", .group = 2 },
305
{ .name = "groupid", .key = 130,
306
.arg = "ID", .flags = 0,
307
.doc = "Group ID the plugins will run as", .group = 2 },
308
{ .name = "debug", .key = 131,
309
.doc = "Debug mode", .group = 3 },
322
{ .name = "config-file", .key = 129,
324
.doc = "Specify a different configuration file", .group = 2 },
325
{ .name = "userid", .key = 130,
326
.arg = "ID", .flags = 0,
327
.doc = "User ID the plugins will run as", .group = 3 },
328
{ .name = "groupid", .key = 131,
329
.arg = "ID", .flags = 0,
330
.doc = "Group ID the plugins will run as", .group = 3 },
331
{ .name = "debug", .key = 132,
332
.doc = "Debug mode", .group = 4 },
428
460
if (ret == ARGP_ERR_UNKNOWN){
429
461
fprintf(stderr, "Unknown error while parsing arguments\n");
430
462
exitstatus = EXIT_FAILURE;
434
conffp = fopen(argfile, "r");
466
if (argfile == NULL){
467
conffp = fopen(AFILE, "r");
469
conffp = fopen(argfile, "r");
435
472
if(conffp != NULL){
436
473
char *org_line = NULL;
474
char *p, *arg, *new_arg, *line;
439
char *p, *arg, *new_arg, *line;
440
477
const char whitespace_delims[] = " \r\t\f\v\n";
441
478
const char comment_delim[] = "#";
481
custom_argv = malloc(sizeof(char*) * 2);
482
if(custom_argv == NULL){
484
exitstatus = EXIT_FAILURE;
487
custom_argv[0] = argv[0];
488
custom_argv[1] = NULL;
444
491
sret = getline(&org_line, &size, conffp);
455
502
new_arg = strdup(p);
456
custom_argv = add_to_argv(custom_argv, &custom_argc, new_arg);
457
if (custom_argv == NULL){
458
perror("add_to_argv");
459
exitstatus = EXIT_FAILURE;
505
exitstatus = EXIT_FAILURE;
511
custom_argv = realloc(custom_argv, sizeof(char *)
512
* ((unsigned int) custom_argc + 1));
513
if(custom_argv == NULL){
515
exitstatus = EXIT_FAILURE;
519
custom_argv[custom_argc-1] = new_arg;
520
custom_argv[custom_argc] = NULL;
468
527
if (errno == EMFILE or errno == ENFILE or errno == ENOMEM){
470
529
exitstatus = EXIT_FAILURE;
475
534
if(custom_argv != NULL){
476
custom_argv[0] = argv[0];
477
535
ret = argp_parse (&argp, custom_argc, custom_argv, 0, 0, &plugin_list);
478
536
if (ret == ARGP_ERR_UNKNOWN){
479
537
fprintf(stderr, "Unknown error while parsing arguments\n");
480
538
exitstatus = EXIT_FAILURE;
506
564
perror("setgid");
567
if (plugindir == NULL){
570
dir = opendir(plugindir);
509
dir = opendir(plugindir);
511
574
perror("Could not open plugin dir");
512
575
exitstatus = EXIT_FAILURE;
516
579
/* Set the FD_CLOEXEC flag on the directory, if possible */
661
724
ret = pipe(pipefd);
664
727
exitstatus = EXIT_FAILURE;
667
730
ret = set_cloexec_flag(pipefd[0]);
669
732
perror("set_cloexec_flag");
670
733
exitstatus = EXIT_FAILURE;
673
736
ret = set_cloexec_flag(pipefd[1]);
675
738
perror("set_cloexec_flag");
676
739
exitstatus = EXIT_FAILURE;
679
742
/* Block SIGCHLD until process is safely in process list */
680
743
ret = sigprocmask (SIG_BLOCK, &sigchld_action.sa_mask, NULL);
682
745
perror("sigprocmask");
683
746
exitstatus = EXIT_FAILURE;
686
749
// Starting a new process to be watched
687
750
pid_t pid = fork();
690
753
exitstatus = EXIT_FAILURE;
694
757
/* this is the child process */
766
/* Free the plugin list */
767
for(plugin *next; plugin_list != NULL; plugin_list = next){
768
next = plugin_list->next;
769
free(plugin_list->argv);
770
if(plugin_list->environ[0] != NULL){
771
for(char **e = plugin_list->environ; *e != NULL; e++){
829
free_plugin_list(plugin_list);
817
871
/* Remove the plugin */
818
872
FD_CLR(proc->fd, &rfds_all);
819
873
/* Block signal while modifying process_list */
820
ret = sigprocmask (SIG_BLOCK, &sigchld_action.sa_mask, NULL);
874
ret = sigprocmask(SIG_BLOCK, &sigchld_action.sa_mask, NULL);
822
876
perror("sigprocmask");
823
877
exitstatus = EXIT_FAILURE;
826
880
/* Delete this process entry from the list */
827
881
if(process_list == proc){
852
906
/* This process exited nicely, so print its buffer */
854
bool bret = print_out_password(proc->buffer, proc->buffer_length);
908
bool bret = print_out_password(proc->buffer,
909
proc->buffer_length);
856
911
perror("print_out_password");
857
912
exitstatus = EXIT_FAILURE;
861
916
/* This process has not completed. Does it have any output? */
862
917
if(proc->eof or not FD_ISSET(proc->fd, &rfds)){
896
951
if(process_list == NULL or exitstatus != EXIT_SUCCESS){
897
/* Fallback if all plugins failed, none are found or an error occured */
952
/* Fallback if all plugins failed, none are found or an error
899
955
fprintf(stderr, "Going to fallback mode using getpass(3)\n");
900
956
char *passwordbuffer = getpass("Password: ");
903
959
perror("print_out_password");
904
960
exitstatus = EXIT_FAILURE;
909
964
/* Restore old signal handler */
910
sigaction(SIGCHLD, &old_sigchld_action, NULL);
914
/* Free the plugin list */
915
for(plugin *next; plugin_list != NULL; plugin_list = next){
916
next = plugin_list->next;
917
free(plugin_list->argv);
918
if(plugin_list->environ[0] != NULL){
919
for(char **e = plugin_list->environ; *e != NULL; e++){
965
ret = sigaction(SIGCHLD, &old_sigchld_action, NULL);
968
exitstatus = EXIT_FAILURE;
971
if(custom_argv != NULL){
972
for(char **arg = custom_argv+1; *arg != NULL; arg++){
923
free(plugin_list->environ);
977
free_plugin_list(plugin_list);