/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

  • Committer: Teddy Hogeborn
  • Date: 2008-09-21 04:22:50 UTC
  • Revision ID: teddy@fukt.bsnet.se-20080921042250-r0qqjsbz2ulfo5le
* Makefile: Bug fix: fix syntax error.

* debian/control: Depend on "po-debconf".
  (mandos, mandos-client): Add "${misc:Depends}", used by
                           dh_installdebconf.

* debian/mandos-client.config: New; show note.
* debian/mandos-client.template: New.

* debian/mandos.README.Debian: New.

* debian/mandos.config: New; show note.
* debian/mandos.prerm: New; stop daemon.
* debian/mandos.template: New.

* debian/po/POTFILES.in: New.
* debian/po/sv.po: New.

* debian/rules (clean): Added "debconf-updatepo" as suggested by
                        po-debconf(7).
  (binary-common): Added "dh_installdebconf" to use "debian/*.config"
                   and "debian/*.template" files.

Show diffs side-by-side

added added

removed removed

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