/mandos/trunk

To get this branch, use:
bzr branch http://bzr.recompile.se/loggerhead/mandos/trunk

« back to all changes in this revision

Viewing changes to plugin-runner.c

merge

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
/*
3
3
 * Mandos plugin runner - Run Mandos plugins
4
4
 *
5
 
 * Copyright © 2008 Teddy Hogeborn & Björn Påhlsson
 
5
 * Copyright © 2007-2008 Teddy Hogeborn & Björn Påhlsson
6
6
 * 
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
72
72
const char *argp_program_version = "plugin-runner 1.0";
73
73
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
74
74
 
 
75
struct plugin;
 
76
 
75
77
typedef struct plugin{
76
78
  char *name;                   /* can be NULL or any plugin name */
77
79
  char **argv;
94
96
 
95
97
static plugin *plugin_list = NULL;
96
98
 
97
 
/* Gets an existing plugin based on name,
 
99
/* Gets a existing plugin based on name,
98
100
   or if none is found, creates a new one */
99
101
static plugin *getplugin(char *name){
100
102
  /* Check for exiting plugin with that name */
116
118
      return NULL;
117
119
    }
118
120
  }
119
 
  
 
121
 
120
122
  *new_plugin = (plugin) { .name = copy_name,
121
123
                           .argc = 1,
122
124
                           .disabled = false,
185
187
  size_t namelen = (size_t)(strchrnul(def, '=') - def);
186
188
  /* Search for this environment variable */
187
189
  for(char **e = p->environ; *e != NULL; e++){
188
 
    if(strncmp(*e, def, namelen + 1) == 0){
 
190
    if(strncmp(*e, def, namelen+1) == 0){
189
191
      /* It already exists */
190
192
      if(replace){
191
 
        char *new = realloc(*e, strlen(def) + 1);
 
193
        char *new = realloc(*e, strlen(def));
192
194
        if(new == NULL){
193
195
          return false;
194
196
        }
206
208
 * Descriptor Flags".
207
209
 * *Note File Descriptor Flags:(libc)Descriptor Flags.
208
210
 */
209
 
static int set_cloexec_flag(int fd){
 
211
static int set_cloexec_flag(int fd)
 
212
{
210
213
  int ret = fcntl(fd, F_GETFD, 0);
211
214
  /* If reading the flags failed, return error indication now. */
212
215
  if(ret < 0){
219
222
 
220
223
/* Mark processes as completed when they exit, and save their exit
221
224
   status. */
222
 
static void handle_sigchld(__attribute__((unused)) int sig){
 
225
void handle_sigchld(__attribute__((unused)) int sig){
223
226
  while(true){
224
227
    plugin *proc = plugin_list;
225
228
    int status;
235
238
      /* No child processes */
236
239
      break;
237
240
    }
238
 
    
 
241
 
239
242
    /* A child exited, find it in process_list */
240
243
    while(proc != NULL and proc->pid != pid){
241
244
      proc = proc->next;
250
253
}
251
254
 
252
255
/* Prints out a password to stdout */
253
 
static bool print_out_password(const char *buffer, size_t length){
 
256
bool print_out_password(const char *buffer, size_t length){
254
257
  ssize_t ret;
 
258
  if(length>0 and buffer[length-1] == '\n'){
 
259
    length--;
 
260
  }
255
261
  for(size_t written = 0; written < length; written += (size_t)ret){
256
262
    ret = TEMP_FAILURE_RETRY(write(STDOUT_FILENO, buffer + written,
257
263
                                   length - written));
338
344
    { .name = "global-options", .key = 'g',
339
345
      .arg = "OPTION[,OPTION[,...]]",
340
346
      .doc = "Options passed to all plugins" },
341
 
    { .name = "global-env", .key = 'G',
 
347
    { .name = "global-env", .key = 'e',
342
348
      .arg = "VAR=value",
343
349
      .doc = "Environment variable passed to all plugins" },
344
350
    { .name = "options-for", .key = 'o',
345
351
      .arg = "PLUGIN:OPTION[,OPTION[,...]]",
346
352
      .doc = "Options passed only to specified plugin" },
347
 
    { .name = "env-for", .key = 'E',
 
353
    { .name = "env-for", .key = 'f',
348
354
      .arg = "PLUGIN:ENV=value",
349
355
      .doc = "Environment variable passed to specified plugin" },
350
356
    { .name = "disable", .key = 'd',
351
357
      .arg = "PLUGIN",
352
358
      .doc = "Disable a specific plugin", .group = 1 },
353
 
    { .name = "enable", .key = 'e',
354
 
      .arg = "PLUGIN",
355
 
      .doc = "Enable a specific plugin", .group = 1 },
356
359
    { .name = "plugin-dir", .key = 128,
357
360
      .arg = "DIRECTORY",
358
361
      .doc = "Specify a different plugin directory", .group = 2 },
372
375
  
373
376
  error_t parse_opt (int key, char *arg, __attribute__((unused))
374
377
                     struct argp_state *state) {
 
378
    /* Get the INPUT argument from `argp_parse', which we know is a
 
379
       pointer to our plugin list pointer. */
375
380
    switch (key) {
376
381
    case 'g':                   /* --global-options */
377
382
      if (arg != NULL){
387
392
        }
388
393
      }
389
394
      break;
390
 
    case 'G':                   /* --global-env */
 
395
    case 'e':                   /* --global-env */
391
396
      if(arg == NULL){
392
397
        break;
393
398
      }
394
 
      if(not add_environment(getplugin(NULL), arg, true)){
395
 
        perror("add_environment");
 
399
      {
 
400
        char *envdef = strdup(arg);
 
401
        if(envdef == NULL){
 
402
          break;
 
403
        }
 
404
        if(not add_environment(getplugin(NULL), envdef, true)){
 
405
          perror("add_environment");
 
406
        }
396
407
      }
397
408
      break;
398
409
    case 'o':                   /* --options-for */
399
410
      if (arg != NULL){
400
411
        char *p_name = strsep(&arg, ":");
401
 
        if(p_name[0] == '\0' or arg == NULL){
 
412
        if(p_name[0] == '\0'){
402
413
          break;
403
414
        }
404
415
        char *opt = strsep(&arg, ":");
405
 
        if(opt[0] == '\0' or opt == NULL){
 
416
        if(opt[0] == '\0'){
406
417
          break;
407
418
        }
408
 
        char *p;
409
 
        while((p = strsep(&opt, ",")) != NULL){
410
 
          if(p[0] == '\0'){
411
 
            continue;
412
 
          }
413
 
          if(not add_argument(getplugin(p_name), p)){
414
 
            perror("add_argument");
415
 
            return ARGP_ERR_UNKNOWN;
 
419
        if(opt != NULL){
 
420
          char *p;
 
421
          while((p = strsep(&opt, ",")) != NULL){
 
422
            if(p[0] == '\0'){
 
423
              continue;
 
424
            }
 
425
            if(not add_argument(getplugin(p_name), p)){
 
426
              perror("add_argument");
 
427
              return ARGP_ERR_UNKNOWN;
 
428
            }
416
429
          }
417
430
        }
418
431
      }
419
432
      break;
420
 
    case 'E':                   /* --env-for */
 
433
    case 'f':                   /* --env-for */
421
434
      if(arg == NULL){
422
435
        break;
423
436
      }
426
439
        if(envdef == NULL){
427
440
          break;
428
441
        }
429
 
        *envdef = '\0';
430
 
        if(not add_environment(getplugin(arg), envdef+1, true)){
 
442
        char *p_name = strndup(arg, (size_t) (envdef-arg));
 
443
        if(p_name == NULL){
 
444
          break;
 
445
        }
 
446
        envdef++;
 
447
        if(not add_environment(getplugin(p_name), envdef, true)){
431
448
          perror("add_environment");
432
449
        }
433
450
      }
441
458
        p->disabled = true;
442
459
      }
443
460
      break;
444
 
    case 'e':                   /* --enable */
445
 
      if (arg != NULL){
446
 
        plugin *p = getplugin(arg);
447
 
        if(p == NULL){
448
 
          return ARGP_ERR_UNKNOWN;
449
 
        }
450
 
        p->disabled = false;
451
 
      }
452
 
      break;
453
461
    case 128:                   /* --plugin-dir */
454
 
      free(plugindir);
455
462
      plugindir = strdup(arg);
456
463
      if(plugindir == NULL){
457
464
        perror("strdup");
458
465
      }      
459
466
      break;
460
467
    case 129:                   /* --config-file */
461
 
      /* This is already done by parse_opt_config_file() */
462
 
      break;
 
468
      argfile = strdup(arg);
 
469
      if(argfile == NULL){
 
470
        perror("strdup");
 
471
      }
 
472
      break;      
463
473
    case 130:                   /* --userid */
464
474
      uid = (uid_t)strtol(arg, NULL, 10);
465
475
      break;
470
480
      debug = true;
471
481
      break;
472
482
    case ARGP_KEY_ARG:
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. */
476
 
      if(arg[0] != '\0'){
477
 
        fprintf(stderr, "Ignoring unknown argument \"%s\"\n", arg);
478
 
      }
479
 
      break;
480
 
    case ARGP_KEY_END:
481
 
      break;
482
 
    default:
483
 
      return ARGP_ERR_UNKNOWN;
484
 
    }
485
 
    return 0;
486
 
  }
487
 
  
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) {
493
 
    switch (key) {
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 */
501
 
      break;
502
 
    case 129:                   /* --config-file */
503
 
      free(argfile);
504
 
      argfile = strdup(arg);
505
 
      if(argfile == NULL){
506
 
        perror("strdup");
507
 
      }
508
 
      break;      
509
 
    case 130:                   /* --userid */
510
 
    case 131:                   /* --groupid */
511
 
    case 132:                   /* --debug */
512
 
    case ARGP_KEY_ARG:
513
 
    case ARGP_KEY_END:
514
 
      break;
515
 
    default:
516
 
      return ARGP_ERR_UNKNOWN;
517
 
    }
518
 
    return 0;
519
 
  }
520
 
  
521
 
  struct argp argp = { .options = options,
522
 
                       .parser = parse_opt_config_file,
 
483
      fprintf(stderr, "Ignoring unknown argument \"%s\"\n", arg);
 
484
      break;
 
485
    case ARGP_KEY_END:
 
486
      break;
 
487
    default:
 
488
      return ARGP_ERR_UNKNOWN;
 
489
    }
 
490
    return 0;
 
491
  }
 
492
  
 
493
  struct argp argp = { .options = options, .parser = parse_opt,
523
494
                       .args_doc = "",
524
495
                       .doc = "Mandos plugin runner -- Run plugins" };
525
496
  
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);
529
 
  if (ret == ARGP_ERR_UNKNOWN){
530
 
    fprintf(stderr, "Unknown error while parsing arguments\n");
531
 
    exitstatus = EXIT_FAILURE;
532
 
    goto fallback;
533
 
  }
534
 
  
535
 
  /* Reset to the normal argument parser */
536
 
  argp.parser = parse_opt;
537
 
  
538
497
  /* Open the configfile if available */
539
498
  if (argfile == NULL){
540
499
    conffp = fopen(AFILE, "r");
736
695
    }
737
696
 
738
697
    char *filename;
739
 
    if(plugindir == NULL){
740
 
      ret = asprintf(&filename, PDIR "/%s", dirst->d_name);
741
 
    } else {
742
 
      ret = asprintf(&filename, "%s/%s", plugindir, dirst->d_name);
743
 
    }
 
698
    ret = asprintf(&filename, "%s/%s", plugindir, dirst->d_name);
744
699
    if(ret < 0){
745
700
      perror("asprintf");
746
701
      continue;
935
890
    }
936
891
    /* OK, now either a process completed, or something can be read
937
892
       from one of them */
938
 
    for(plugin *proc = plugin_list; proc != NULL;){
 
893
    for(plugin *proc = plugin_list; proc != NULL; proc = proc->next){
939
894
      /* Is this process completely done? */
940
895
      if(proc->eof and proc->completed){
941
896
        /* Only accept the plugin output if it exited cleanly */
968
923
            exitstatus = EXIT_FAILURE;
969
924
            goto fallback;
970
925
          }
971
 
          
 
926
          free_plugin(proc);
972
927
          /* We are done modifying process list, so unblock signal */
973
928
          ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask,
974
929
                             NULL);
981
936
          if(plugin_list == NULL){
982
937
            break;
983
938
          }
984
 
          
985
 
          plugin *next_plugin = proc->next;
986
 
          free_plugin(proc);
987
 
          proc = next_plugin;
988
939
          continue;
989
940
        }
990
941
        
1002
953
      /* This process has not completed.  Does it have any output? */
1003
954
      if(proc->eof or not FD_ISSET(proc->fd, &rfds)){
1004
955
        /* This process had nothing to say at this time */
1005
 
        proc = proc->next;
1006
956
        continue;
1007
957
      }
1008
958
      /* Before reading, make the process' data buffer large enough */
1021
971
                 BUFFER_SIZE);
1022
972
      if(ret < 0){
1023
973
        /* Read error from this process; ignore the error */
1024
 
        proc = proc->next;
1025
974
        continue;
1026
975
      }
1027
976
      if(ret == 0){
1042
991
    bool bret;
1043
992
    fprintf(stderr, "Going to fallback mode using getpass(3)\n");
1044
993
    char *passwordbuffer = getpass("Password: ");
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 */
1049
 
      len--;
1050
 
    }
1051
 
    bret = print_out_password(passwordbuffer, len);
 
994
    bret = print_out_password(passwordbuffer, strlen(passwordbuffer));
1052
995
    if(not bret){
1053
996
      perror("print_out_password");
1054
997
      exitstatus = EXIT_FAILURE;