3
3
* Mandos plugin runner - Run Mandos plugins
5
* Copyright © 2007-2008 Teddy Hogeborn & Björn Påhlsson
5
* Copyright © 2008 Teddy Hogeborn & Björn Påhlsson
7
7
* This program is free software: you can redistribute it and/or
8
8
* modify it under the terms of the GNU General Public License as
97
95
static plugin *plugin_list = NULL;
99
/* Gets a existing plugin based on name,
97
/* Gets an existing plugin based on name,
100
98
or if none is found, creates a new one */
101
99
static plugin *getplugin(char *name){
102
100
/* Check for exiting plugin with that name */
181
179
/* Add to a plugin's environment */
182
static bool add_environment(plugin *p, const char *def){
180
static bool add_environment(plugin *p, const char *def, bool replace){
184
/* namelen = length of name of environment variable */
185
size_t namelen = (size_t)(strchrnul(def, '=') - def);
186
/* Search for this environment variable */
187
for(char **e = p->environ; *e != NULL; e++){
188
if(strncmp(*e, def, namelen + 1) == 0){
189
/* It already exists */
191
char *new = realloc(*e, strlen(def) + 1);
186
201
return add_to_char_array(def, &(p->environ), &(p->envc));
191
206
* Descriptor Flags".
192
207
* *Note File Descriptor Flags:(libc)Descriptor Flags.
194
static int set_cloexec_flag(int fd)
209
static int set_cloexec_flag(int fd){
196
210
int ret = fcntl(fd, F_GETFD, 0);
197
211
/* If reading the flags failed, return error indication now. */
206
220
/* Mark processes as completed when they exit, and save their exit
208
void handle_sigchld(__attribute__((unused)) int sig){
222
static void handle_sigchld(__attribute__((unused)) int sig){
210
224
plugin *proc = plugin_list;
238
252
/* Prints out a password to stdout */
239
bool print_out_password(const char *buffer, size_t length){
253
static bool print_out_password(const char *buffer, size_t length){
241
if(length>0 and buffer[length-1] == '\n'){
244
255
for(size_t written = 0; written < length; written += (size_t)ret){
245
256
ret = TEMP_FAILURE_RETRY(write(STDOUT_FILENO, buffer + written,
246
257
length - written));
327
338
{ .name = "global-options", .key = 'g',
328
339
.arg = "OPTION[,OPTION[,...]]",
329
340
.doc = "Options passed to all plugins" },
330
{ .name = "global-envs", .key = 'e',
341
{ .name = "global-env", .key = 'G',
331
342
.arg = "VAR=value",
332
343
.doc = "Environment variable passed to all plugins" },
333
344
{ .name = "options-for", .key = 'o',
334
345
.arg = "PLUGIN:OPTION[,OPTION[,...]]",
335
346
.doc = "Options passed only to specified plugin" },
336
{ .name = "envs-for", .key = 'f',
347
{ .name = "env-for", .key = 'E',
337
348
.arg = "PLUGIN:ENV=value",
338
349
.doc = "Environment variable passed to specified plugin" },
339
350
{ .name = "disable", .key = 'd',
341
352
.doc = "Disable a specific plugin", .group = 1 },
353
{ .name = "enable", .key = 'e',
355
.doc = "Enable a specific plugin", .group = 1 },
342
356
{ .name = "plugin-dir", .key = 128,
343
357
.arg = "DIRECTORY",
344
358
.doc = "Specify a different plugin directory", .group = 2 },
359
373
error_t parse_opt (int key, char *arg, __attribute__((unused))
360
374
struct argp_state *state) {
361
/* Get the INPUT argument from `argp_parse', which we know is a
362
pointer to our plugin list pointer. */
364
376
case 'g': /* --global-options */
365
377
if (arg != NULL){
378
case 'e': /* --global-envs */
390
case 'G': /* --global-env */
383
char *envdef = strdup(arg);
387
if(not add_environment(getplugin(NULL), envdef)){
388
perror("add_environment");
394
if(not add_environment(getplugin(NULL), arg, true)){
395
perror("add_environment");
392
398
case 'o': /* --options-for */
393
399
if (arg != NULL){
394
400
char *p_name = strsep(&arg, ":");
395
if(p_name[0] == '\0'){
401
if(p_name[0] == '\0' or arg == NULL){
398
404
char *opt = strsep(&arg, ":");
405
if(opt[0] == '\0' or opt == NULL){
404
while((p = strsep(&opt, ",")) != NULL){
408
if(not add_argument(getplugin(p_name), p)){
409
perror("add_argument");
410
return ARGP_ERR_UNKNOWN;
409
while((p = strsep(&opt, ",")) != NULL){
413
if(not add_argument(getplugin(p_name), p)){
414
perror("add_argument");
415
return ARGP_ERR_UNKNOWN;
416
case 'f': /* --envs-for */
420
case 'E': /* --env-for */
422
426
if(envdef == NULL){
425
char *p_name = strndup(arg, (size_t) (envdef-arg));
430
if(not add_environment(getplugin(p_name), envdef)){
430
if(not add_environment(getplugin(arg), envdef+1, true)){
431
431
perror("add_environment");
441
441
p->disabled = true;
444
case 'e': /* --enable */
446
plugin *p = getplugin(arg);
448
return ARGP_ERR_UNKNOWN;
444
453
case 128: /* --plugin-dir */
445
455
plugindir = strdup(arg);
446
456
if(plugindir == NULL){
447
457
perror("strdup");
450
460
case 129: /* --config-file */
461
/* This is already done by parse_opt_config_file() */
463
case 130: /* --userid */
464
uid = (uid_t)strtol(arg, NULL, 10);
466
case 131: /* --groupid */
467
gid = (gid_t)strtol(arg, NULL, 10);
469
case 132: /* --debug */
473
/* Cryptsetup always passes an argument, which is an empty
474
string if "none" was specified in /etc/crypttab. So if
475
argument was empty, we ignore it silently. */
477
fprintf(stderr, "Ignoring unknown argument \"%s\"\n", arg);
483
return ARGP_ERR_UNKNOWN;
488
/* This option parser is the same as parse_opt() above, except it
489
ignores everything but the --config-file option. */
490
error_t parse_opt_config_file (int key, char *arg,
491
__attribute__((unused))
492
struct argp_state *state) {
494
case 'g': /* --global-options */
495
case 'G': /* --global-env */
496
case 'o': /* --options-for */
497
case 'E': /* --env-for */
498
case 'd': /* --disable */
499
case 'e': /* --enable */
500
case 128: /* --plugin-dir */
502
case 129: /* --config-file */
451
504
argfile = strdup(arg);
452
505
if(argfile == NULL){
453
506
perror("strdup");
456
509
case 130: /* --userid */
457
uid = (uid_t)strtol(arg, NULL, 10);
459
510
case 131: /* --groupid */
460
gid = (gid_t)strtol(arg, NULL, 10);
462
511
case 132: /* --debug */
465
512
case ARGP_KEY_ARG:
466
fprintf(stderr, "Ignoring unknown argument \"%s\"\n", arg);
468
513
case ARGP_KEY_END:
476
struct argp argp = { .options = options, .parser = parse_opt,
477
.args_doc = "[+PLUS_SEPARATED_OPTIONS]",
521
struct argp argp = { .options = options,
522
.parser = parse_opt_config_file,
478
524
.doc = "Mandos plugin runner -- Run plugins" };
480
ret = argp_parse (&argp, argc, argv, 0, 0, NULL);
526
/* Parse using the parse_opt_config_file in order to get the custom
527
config file location, if any. */
528
ret = argp_parse (&argp, argc, argv, ARGP_IN_ORDER, 0, NULL);
481
529
if (ret == ARGP_ERR_UNKNOWN){
482
530
fprintf(stderr, "Unknown error while parsing arguments\n");
483
531
exitstatus = EXIT_FAILURE;
487
/* Opens the configfile if aviable */
535
/* Reset to the normal argument parser */
536
argp.parser = parse_opt;
538
/* Open the configfile if available */
488
539
if (argfile == NULL){
489
540
conffp = fopen(AFILE, "r");
556
607
/* If there was any arguments from configuration file,
557
608
pass them to parser as command arguments */
558
609
if(custom_argv != NULL){
559
ret = argp_parse (&argp, custom_argc, custom_argv, 0, 0, NULL);
610
ret = argp_parse (&argp, custom_argc, custom_argv, ARGP_IN_ORDER,
560
612
if (ret == ARGP_ERR_UNKNOWN){
561
613
fprintf(stderr, "Unknown error while parsing arguments\n");
562
614
exitstatus = EXIT_FAILURE;
619
/* Parse actual command line arguments, to let them override the
621
ret = argp_parse (&argp, argc, argv, ARGP_IN_ORDER, 0, NULL);
622
if (ret == ARGP_ERR_UNKNOWN){
623
fprintf(stderr, "Unknown error while parsing arguments\n");
624
exitstatus = EXIT_FAILURE;
568
629
for(plugin *p = plugin_list; p != NULL; p=p->next){
569
630
fprintf(stderr, "Plugin: %s has %d arguments\n",
678
ret = asprintf(&filename, "%s/%s", plugindir, dirst->d_name);
739
if(plugindir == NULL){
740
ret = asprintf(&filename, PDIR "/%s", dirst->d_name);
742
ret = asprintf(&filename, "%s/%s", plugindir, dirst->d_name);
680
745
perror("asprintf");
724
789
/* Add global environment variables */
725
790
for(char **e = g->environ; *e != NULL; e++){
726
if(not add_environment(p, *e)){
791
if(not add_environment(p, *e, false)){
727
792
perror("add_environment");
735
800
if(p->environ[0] != NULL){
736
801
for(char **e = environ; *e != NULL; e++){
737
char *copy = strdup(*e);
742
if(not add_environment(p, copy)){
802
if(not add_environment(p, *e, false)){
743
803
perror("add_environment");
876
936
/* OK, now either a process completed, or something can be read
877
937
from one of them */
878
for(plugin *proc = plugin_list; proc != NULL; proc = proc->next){
938
for(plugin *proc = plugin_list; proc != NULL;){
879
939
/* Is this process completely done? */
880
940
if(proc->eof and proc->completed){
881
941
/* Only accept the plugin output if it exited cleanly */
908
968
exitstatus = EXIT_FAILURE;
912
972
/* We are done modifying process list, so unblock signal */
913
973
ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask,
938
1002
/* This process has not completed. Does it have any output? */
939
1003
if(proc->eof or not FD_ISSET(proc->fd, &rfds)){
940
1004
/* This process had nothing to say at this time */
943
1008
/* Before reading, make the process' data buffer large enough */
977
1043
fprintf(stderr, "Going to fallback mode using getpass(3)\n");
978
1044
char *passwordbuffer = getpass("Password: ");
979
bret = print_out_password(passwordbuffer, strlen(passwordbuffer));
1045
size_t len = strlen(passwordbuffer);
1046
/* Strip trailing newline */
1047
if(len > 0 and passwordbuffer[len-1] == '\n'){
1048
passwordbuffer[len-1] = '\0'; /* not strictly necessary */
1051
bret = print_out_password(passwordbuffer, len);
981
1053
perror("print_out_password");
982
1054
exitstatus = EXIT_FAILURE;