/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-07 09:36:35 UTC
  • Revision ID: teddy@fukt.bsnet.se-20080907093635-yg1y272yv53dijhp
* Makefile (CONFDIR): Changed to be the same ("/etc/mandos") in both a
                      /usr/local install and a package install.
  (KEYDIR): Changed /usr/local install value to be "/etc/mandos/keys".
  (INITRAMFSTOOLS): Use $(DESTDIR) in /usr/local value too.

* initramfs-tools-hook: Look in "/etc/mandos/keys" too.

Show diffs side-by-side

added added

removed removed

Lines of Context:
96
96
 
97
97
static plugin *plugin_list = NULL;
98
98
 
99
 
/* Gets a existing plugin based on name,
 
99
/* Gets an existing plugin based on name,
100
100
   or if none is found, creates a new one */
101
101
static plugin *getplugin(char *name){
102
102
  /* Check for exiting plugin with that name */
118
118
      return NULL;
119
119
    }
120
120
  }
121
 
 
 
121
  
122
122
  *new_plugin = (plugin) { .name = copy_name,
123
123
                           .argc = 1,
124
124
                           .disabled = false,
132
132
  }
133
133
  new_plugin->argv[0] = copy_name;
134
134
  new_plugin->argv[1] = NULL;
135
 
 
 
135
  
136
136
  new_plugin->environ = malloc(sizeof(char *));
137
137
  if(new_plugin->environ == NULL){
138
138
    free(copy_name);
141
141
    return NULL;
142
142
  }
143
143
  new_plugin->environ[0] = NULL;
144
 
 
 
144
  
145
145
  /* Append the new plugin to the list */
146
146
  plugin_list = new_plugin;
147
147
  return new_plugin;
179
179
}
180
180
 
181
181
/* Add to a plugin's environment */
182
 
static bool add_environment(plugin *p, const char *def){
 
182
static bool add_environment(plugin *p, const char *def, bool replace){
183
183
  if(p == NULL){
184
184
    return false;
185
185
  }
 
186
  /* namelen = length of name of environment variable */
 
187
  size_t namelen = (size_t)(strchrnul(def, '=') - def);
 
188
  /* Search for this environment variable */
 
189
  for(char **e = p->environ; *e != NULL; e++){
 
190
    if(strncmp(*e, def, namelen + 1) == 0){
 
191
      /* It already exists */
 
192
      if(replace){
 
193
        char *new = realloc(*e, strlen(def) + 1);
 
194
        if(new == NULL){
 
195
          return false;
 
196
        }
 
197
        *e = new;
 
198
        strcpy(*e, def);
 
199
      }
 
200
      return true;
 
201
    }
 
202
  }
186
203
  return add_to_char_array(def, &(p->environ), &(p->envc));
187
204
}
188
205
 
221
238
      /* No child processes */
222
239
      break;
223
240
    }
224
 
 
 
241
    
225
242
    /* A child exited, find it in process_list */
226
243
    while(proc != NULL and proc->pid != pid){
227
244
      proc = proc->next;
238
255
/* Prints out a password to stdout */
239
256
bool print_out_password(const char *buffer, size_t length){
240
257
  ssize_t ret;
241
 
  if(length>0 and buffer[length-1] == '\n'){
242
 
    length--;
243
 
  }
244
258
  for(size_t written = 0; written < length; written += (size_t)ret){
245
259
    ret = TEMP_FAILURE_RETRY(write(STDOUT_FILENO, buffer + written,
246
260
                                   length - written));
327
341
    { .name = "global-options", .key = 'g',
328
342
      .arg = "OPTION[,OPTION[,...]]",
329
343
      .doc = "Options passed to all plugins" },
330
 
    { .name = "global-envs", .key = 'e',
 
344
    { .name = "global-env", .key = 'G',
331
345
      .arg = "VAR=value",
332
346
      .doc = "Environment variable passed to all plugins" },
333
347
    { .name = "options-for", .key = 'o',
334
348
      .arg = "PLUGIN:OPTION[,OPTION[,...]]",
335
349
      .doc = "Options passed only to specified plugin" },
336
 
    { .name = "envs-for", .key = 'f',
 
350
    { .name = "env-for", .key = 'E',
337
351
      .arg = "PLUGIN:ENV=value",
338
352
      .doc = "Environment variable passed to specified plugin" },
339
353
    { .name = "disable", .key = 'd',
340
354
      .arg = "PLUGIN",
341
355
      .doc = "Disable a specific plugin", .group = 1 },
 
356
    { .name = "enable", .key = 'e',
 
357
      .arg = "PLUGIN",
 
358
      .doc = "Enable a specific plugin", .group = 1 },
342
359
    { .name = "plugin-dir", .key = 128,
343
360
      .arg = "DIRECTORY",
344
361
      .doc = "Specify a different plugin directory", .group = 2 },
356
373
    { .name = NULL }
357
374
  };
358
375
  
359
 
  error_t parse_opt (int key, char *arg, __attribute__((unused)) struct argp_state *state) {
360
 
    /* Get the INPUT argument from `argp_parse', which we know is a
361
 
       pointer to our plugin list pointer. */
 
376
  error_t parse_opt (int key, char *arg, __attribute__((unused))
 
377
                     struct argp_state *state) {
362
378
    switch (key) {
363
379
    case 'g':                   /* --global-options */
364
380
      if (arg != NULL){
374
390
        }
375
391
      }
376
392
      break;
377
 
    case 'e':                   /* --global-envs */
 
393
    case 'G':                   /* --global-env */
378
394
      if(arg == NULL){
379
395
        break;
380
396
      }
381
 
      {
382
 
        char *envdef = strdup(arg);
383
 
        if(envdef == NULL){
384
 
          break;
385
 
        }
386
 
        if(not add_environment(getplugin(NULL), envdef)){
387
 
          perror("add_environment");
388
 
        }
 
397
      if(not add_environment(getplugin(NULL), arg, true)){
 
398
        perror("add_environment");
389
399
      }
390
400
      break;
391
401
    case 'o':                   /* --options-for */
392
402
      if (arg != NULL){
393
403
        char *p_name = strsep(&arg, ":");
394
 
        if(p_name[0] == '\0'){
 
404
        if(p_name[0] == '\0' or arg == NULL){
395
405
          break;
396
406
        }
397
407
        char *opt = strsep(&arg, ":");
398
 
        if(opt[0] == '\0'){
 
408
        if(opt[0] == '\0' or opt == NULL){
399
409
          break;
400
410
        }
401
 
        if(opt != NULL){
402
 
          char *p;
403
 
          while((p = strsep(&opt, ",")) != NULL){
404
 
            if(p[0] == '\0'){
405
 
              continue;
406
 
            }
407
 
            if(not add_argument(getplugin(p_name), p)){
408
 
              perror("add_argument");
409
 
              return ARGP_ERR_UNKNOWN;
410
 
            }
 
411
        char *p;
 
412
        while((p = strsep(&opt, ",")) != NULL){
 
413
          if(p[0] == '\0'){
 
414
            continue;
 
415
          }
 
416
          if(not add_argument(getplugin(p_name), p)){
 
417
            perror("add_argument");
 
418
            return ARGP_ERR_UNKNOWN;
411
419
          }
412
420
        }
413
421
      }
414
422
      break;
415
 
    case 'f':                   /* --envs-for */
 
423
    case 'E':                   /* --env-for */
416
424
      if(arg == NULL){
417
425
        break;
418
426
      }
421
429
        if(envdef == NULL){
422
430
          break;
423
431
        }
424
 
        char *p_name = strndup(arg, (size_t) (envdef-arg));
425
 
        if(p_name == NULL){
426
 
          break;
427
 
        }
428
 
        envdef++;
429
 
        if(not add_environment(getplugin(p_name), envdef)){
 
432
        *envdef = '\0';
 
433
        if(not add_environment(getplugin(arg), envdef+1, true)){
430
434
          perror("add_environment");
431
435
        }
432
436
      }
440
444
        p->disabled = true;
441
445
      }
442
446
      break;
 
447
    case 'e':                   /* --enable */
 
448
      if (arg != NULL){
 
449
        plugin *p = getplugin(arg);
 
450
        if(p == NULL){
 
451
          return ARGP_ERR_UNKNOWN;
 
452
        }
 
453
        p->disabled = false;
 
454
      }
 
455
      break;
443
456
    case 128:                   /* --plugin-dir */
 
457
      free(plugindir);
444
458
      plugindir = strdup(arg);
445
459
      if(plugindir == NULL){
446
460
        perror("strdup");
447
461
      }      
448
462
      break;
449
463
    case 129:                   /* --config-file */
450
 
      argfile = strdup(arg);
451
 
      if(argfile == NULL){
452
 
        perror("strdup");
453
 
      }
454
 
      break;      
 
464
      /* This is already done by parse_opt_config_file() */
 
465
      break;
455
466
    case 130:                   /* --userid */
456
467
      uid = (uid_t)strtol(arg, NULL, 10);
457
468
      break;
472
483
    return 0;
473
484
  }
474
485
  
475
 
  struct argp argp = { .options = options, .parser = parse_opt,
476
 
                       .args_doc = "[+PLUS_SEPARATED_OPTIONS]",
 
486
  /* This option parser is the same as parse_opt() above, except it
 
487
     ignores everything but the --config-file option. */
 
488
  error_t parse_opt_config_file (int key, char *arg,
 
489
                                 __attribute__((unused))
 
490
                                 struct argp_state *state) {
 
491
    switch (key) {
 
492
    case 'g':                   /* --global-options */
 
493
    case 'G':                   /* --global-env */
 
494
    case 'o':                   /* --options-for */
 
495
    case 'E':                   /* --env-for */
 
496
    case 'd':                   /* --disable */
 
497
    case 'e':                   /* --enable */
 
498
    case 128:                   /* --plugin-dir */
 
499
      break;
 
500
    case 129:                   /* --config-file */
 
501
      free(argfile);
 
502
      argfile = strdup(arg);
 
503
      if(argfile == NULL){
 
504
        perror("strdup");
 
505
      }
 
506
      break;      
 
507
    case 130:                   /* --userid */
 
508
    case 131:                   /* --groupid */
 
509
    case 132:                   /* --debug */
 
510
    case ARGP_KEY_ARG:
 
511
    case ARGP_KEY_END:
 
512
      break;
 
513
    default:
 
514
      return ARGP_ERR_UNKNOWN;
 
515
    }
 
516
    return 0;
 
517
  }
 
518
  
 
519
  struct argp argp = { .options = options,
 
520
                       .parser = parse_opt_config_file,
 
521
                       .args_doc = "",
477
522
                       .doc = "Mandos plugin runner -- Run plugins" };
478
523
  
479
 
  ret = argp_parse (&argp, argc, argv, 0, 0, NULL);
 
524
  /* Parse using the parse_opt_config_file in order to get the custom
 
525
     config file location, if any. */
 
526
  ret = argp_parse (&argp, argc, argv, ARGP_IN_ORDER, 0, NULL);
480
527
  if (ret == ARGP_ERR_UNKNOWN){
481
528
    fprintf(stderr, "Unknown error while parsing arguments\n");
482
529
    exitstatus = EXIT_FAILURE;
483
530
    goto fallback;
484
531
  }
485
 
 
486
 
  /* Opens the configfile if aviable */
 
532
  
 
533
  /* Reset to the normal argument parser */
 
534
  argp.parser = parse_opt;
 
535
  
 
536
  /* Open the configfile if available */
487
537
  if (argfile == NULL){
488
538
    conffp = fopen(AFILE, "r");
489
539
  } else {
507
557
    custom_argv[0] = argv[0];
508
558
    custom_argv[1] = NULL;
509
559
 
510
 
    /* for each line in the config file, strip whitespace and ignore commented text */
 
560
    /* for each line in the config file, strip whitespace and ignore
 
561
       commented text */
511
562
    while(true){
512
563
      sret = getline(&org_line, &size, conffp);
513
564
      if(sret == -1){
542
593
      }
543
594
    }
544
595
    free(org_line);
545
 
  } else{
 
596
  } else {
546
597
    /* Check for harmful errors and go to fallback. Other errors might
547
598
       not affect opening plugins */
548
599
    if (errno == EMFILE or errno == ENFILE or errno == ENOMEM){
554
605
  /* If there was any arguments from configuration file,
555
606
     pass them to parser as command arguments */
556
607
  if(custom_argv != NULL){
557
 
    ret = argp_parse (&argp, custom_argc, custom_argv, 0, 0, NULL);
 
608
    ret = argp_parse (&argp, custom_argc, custom_argv, ARGP_IN_ORDER,
 
609
                      0, NULL);
558
610
    if (ret == ARGP_ERR_UNKNOWN){
559
611
      fprintf(stderr, "Unknown error while parsing arguments\n");
560
612
      exitstatus = EXIT_FAILURE;
562
614
    }
563
615
  }
564
616
  
 
617
  /* Parse actual command line arguments, to let them override the
 
618
     config file */
 
619
  ret = argp_parse (&argp, argc, argv, ARGP_IN_ORDER, 0, NULL);
 
620
  if (ret == ARGP_ERR_UNKNOWN){
 
621
    fprintf(stderr, "Unknown error while parsing arguments\n");
 
622
    exitstatus = EXIT_FAILURE;
 
623
    goto fallback;
 
624
  }
 
625
  
565
626
  if(debug){
566
627
    for(plugin *p = plugin_list; p != NULL; p=p->next){
567
628
      fprintf(stderr, "Plugin: %s has %d arguments\n",
575
636
      }
576
637
    }
577
638
  }
578
 
 
 
639
  
579
640
  /* Strip permissions down to nobody */
580
641
  ret = setuid(uid);
581
642
  if (ret == -1){
585
646
  if (ret == -1){
586
647
    perror("setgid");
587
648
  }
588
 
 
 
649
  
589
650
  if (plugindir == NULL){
590
651
    dir = opendir(PDIR);
591
652
  } else {
612
673
  }
613
674
  
614
675
  FD_ZERO(&rfds_all);
615
 
 
 
676
  
616
677
  /* Read and execute any executable in the plugin directory*/
617
678
  while(true){
618
679
    dirst = readdir(dir);
619
680
    
620
 
    // All directory entries have been processed
 
681
    /* All directory entries have been processed */
621
682
    if(dirst == NULL){
622
683
      if (errno == EBADF){
623
684
        perror("readdir");
629
690
    
630
691
    d_name_len = strlen(dirst->d_name);
631
692
    
632
 
    // Ignore dotfiles, backup files and other junk
 
693
    /* Ignore dotfiles, backup files and other junk */
633
694
    {
634
695
      bool bad_name = false;
635
696
      
673
734
    }
674
735
 
675
736
    char *filename;
676
 
    ret = asprintf(&filename, "%s/%s", plugindir, dirst->d_name);
 
737
    if(plugindir == NULL){
 
738
      ret = asprintf(&filename, PDIR "/%s", dirst->d_name);
 
739
    } else {
 
740
      ret = asprintf(&filename, "%s/%s", plugindir, dirst->d_name);
 
741
    }
677
742
    if(ret < 0){
678
743
      perror("asprintf");
679
744
      continue;
721
786
        }
722
787
        /* Add global environment variables */
723
788
        for(char **e = g->environ; *e != NULL; e++){
724
 
          if(not add_environment(p, *e)){
 
789
          if(not add_environment(p, *e, false)){
725
790
            perror("add_environment");
726
791
          }
727
792
        }
732
797
       process, too. */
733
798
    if(p->environ[0] != NULL){
734
799
      for(char **e = environ; *e != NULL; e++){
735
 
        char *copy = strdup(*e);
736
 
        if(copy == NULL){
737
 
          perror("strdup");
738
 
          continue;
739
 
        }
740
 
        if(not add_environment(p, copy)){
 
800
        if(not add_environment(p, *e, false)){
741
801
          perror("add_environment");
742
802
        }
743
803
      }
770
830
      exitstatus = EXIT_FAILURE;
771
831
      goto fallback;
772
832
    }
773
 
    // Starting a new process to be watched
 
833
    /* Starting a new process to be watched */
774
834
    pid_t pid = fork();
775
835
    if(pid == -1){
776
836
      perror("fork");
873
933
    }
874
934
    /* OK, now either a process completed, or something can be read
875
935
       from one of them */
876
 
    for(plugin *proc = plugin_list; proc != NULL; proc = proc->next){
 
936
    for(plugin *proc = plugin_list; proc != NULL;){
877
937
      /* Is this process completely done? */
878
938
      if(proc->eof and proc->completed){
879
939
        /* Only accept the plugin output if it exited cleanly */
906
966
            exitstatus = EXIT_FAILURE;
907
967
            goto fallback;
908
968
          }
909
 
          free_plugin(proc);
 
969
          
910
970
          /* We are done modifying process list, so unblock signal */
911
971
          ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask,
912
972
                             NULL);
919
979
          if(plugin_list == NULL){
920
980
            break;
921
981
          }
 
982
          
 
983
          plugin *next_plugin = proc->next;
 
984
          free_plugin(proc);
 
985
          proc = next_plugin;
922
986
          continue;
923
987
        }
924
988
        
936
1000
      /* This process has not completed.  Does it have any output? */
937
1001
      if(proc->eof or not FD_ISSET(proc->fd, &rfds)){
938
1002
        /* This process had nothing to say at this time */
 
1003
        proc = proc->next;
939
1004
        continue;
940
1005
      }
941
1006
      /* Before reading, make the process' data buffer large enough */
954
1019
                 BUFFER_SIZE);
955
1020
      if(ret < 0){
956
1021
        /* Read error from this process; ignore the error */
 
1022
        proc = proc->next;
957
1023
        continue;
958
1024
      }
959
1025
      if(ret == 0){
974
1040
    bool bret;
975
1041
    fprintf(stderr, "Going to fallback mode using getpass(3)\n");
976
1042
    char *passwordbuffer = getpass("Password: ");
977
 
    bret = print_out_password(passwordbuffer, strlen(passwordbuffer));
 
1043
    size_t len = strlen(passwordbuffer);
 
1044
    /* Strip trailing newline */
 
1045
    if(len > 0 and passwordbuffer[len-1] == '\n'){
 
1046
      passwordbuffer[len-1] = '\0'; /* not strictly necessary */
 
1047
      len--;
 
1048
    }
 
1049
    bret = print_out_password(passwordbuffer, len);
978
1050
    if(not bret){
979
1051
      perror("print_out_password");
980
1052
      exitstatus = EXIT_FAILURE;