/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-02 06:03:08 UTC
  • Revision ID: teddy@fukt.bsnet.se-20080902060308-8uhgsfjiwixuvz6j
* plugin-runner.c (main/parse_opt): Removed code for "--config-file".
  (main/parse_opt_config_file): New function only for "--config-file".
  (main): Parse options first using "parse_opt_config_file", then from
          the config file, and lastly from the command line.

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
27
27
#include <stdlib.h>             /* malloc(), exit(), EXIT_FAILURE,
28
28
                                   EXIT_SUCCESS, realloc() */
29
29
#include <stdbool.h>            /* bool, true, false */
30
 
#include <stdio.h>              /* perror, fileno(), fprintf(),
31
 
                                   stderr, STDOUT_FILENO */
 
30
#include <stdio.h>              /* perror, popen(), fileno(),
 
31
                                   fprintf(), stderr, STDOUT_FILENO */
32
32
#include <sys/types.h>          /* DIR, opendir(), stat(), struct
33
33
                                   stat, waitpid(), WIFEXITED(),
34
34
                                   WEXITSTATUS(), wait(), pid_t,
46
46
                                   fcntl(), setuid(), setgid(),
47
47
                                   F_GETFD, F_SETFD, FD_CLOEXEC,
48
48
                                   access(), pipe(), fork(), close()
49
 
                                   dup2(), STDOUT_FILENO, _exit(),
 
49
                                   dup2, STDOUT_FILENO, _exit(),
50
50
                                   execv(), write(), read(),
51
51
                                   close() */
52
52
#include <fcntl.h>              /* fcntl(), F_GETFD, F_SETFD,
69
69
#define PDIR "/lib/mandos/plugins.d"
70
70
#define AFILE "/conf/conf.d/mandos/plugin-runner.conf"
71
71
 
72
 
const char *argp_program_version = "plugin-runner " VERSION;
 
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 },
387
390
        }
388
391
      }
389
392
      break;
390
 
    case 'G':                   /* --global-env */
 
393
    case 'e':                   /* --global-env */
391
394
      if(arg == NULL){
392
395
        break;
393
396
      }
394
 
      if(not add_environment(getplugin(NULL), arg, true)){
395
 
        perror("add_environment");
 
397
      {
 
398
        char *envdef = strdup(arg);
 
399
        if(envdef == NULL){
 
400
          break;
 
401
        }
 
402
        if(not add_environment(getplugin(NULL), envdef, true)){
 
403
          perror("add_environment");
 
404
        }
396
405
      }
397
406
      break;
398
407
    case 'o':                   /* --options-for */
399
408
      if (arg != NULL){
400
409
        char *p_name = strsep(&arg, ":");
401
 
        if(p_name[0] == '\0' or arg == NULL){
 
410
        if(p_name[0] == '\0'){
402
411
          break;
403
412
        }
404
413
        char *opt = strsep(&arg, ":");
405
 
        if(opt[0] == '\0' or opt == NULL){
 
414
        if(opt[0] == '\0'){
406
415
          break;
407
416
        }
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;
 
417
        if(opt != NULL){
 
418
          char *p;
 
419
          while((p = strsep(&opt, ",")) != NULL){
 
420
            if(p[0] == '\0'){
 
421
              continue;
 
422
            }
 
423
            if(not add_argument(getplugin(p_name), p)){
 
424
              perror("add_argument");
 
425
              return ARGP_ERR_UNKNOWN;
 
426
            }
416
427
          }
417
428
        }
418
429
      }
419
430
      break;
420
 
    case 'E':                   /* --env-for */
 
431
    case 'f':                   /* --env-for */
421
432
      if(arg == NULL){
422
433
        break;
423
434
      }
426
437
        if(envdef == NULL){
427
438
          break;
428
439
        }
429
 
        *envdef = '\0';
430
 
        if(not add_environment(getplugin(arg), envdef+1, true)){
 
440
        char *p_name = strndup(arg, (size_t) (envdef-arg));
 
441
        if(p_name == NULL){
 
442
          break;
 
443
        }
 
444
        envdef++;
 
445
        if(not add_environment(getplugin(p_name), envdef, true)){
431
446
          perror("add_environment");
432
447
        }
433
448
      }
441
456
        p->disabled = true;
442
457
      }
443
458
      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
459
    case 128:                   /* --plugin-dir */
454
 
      free(plugindir);
455
460
      plugindir = strdup(arg);
456
461
      if(plugindir == NULL){
457
462
        perror("strdup");
470
475
      debug = true;
471
476
      break;
472
477
    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
 
      }
 
478
      fprintf(stderr, "Ignoring unknown argument \"%s\"\n", arg);
479
479
      break;
480
480
    case ARGP_KEY_END:
481
481
      break;
492
492
                                 struct argp_state *state) {
493
493
    switch (key) {
494
494
    case 'g':                   /* --global-options */
495
 
    case 'G':                   /* --global-env */
 
495
    case 'e':                   /* --global-env */
496
496
    case 'o':                   /* --options-for */
497
 
    case 'E':                   /* --env-for */
 
497
    case 'f':                   /* --env-for */
498
498
    case 'd':                   /* --disable */
499
 
    case 'e':                   /* --enable */
500
499
    case 128:                   /* --plugin-dir */
501
500
      break;
502
501
    case 129:                   /* --config-file */
503
 
      free(argfile);
504
502
      argfile = strdup(arg);
505
503
      if(argfile == NULL){
506
504
        perror("strdup");
736
734
    }
737
735
 
738
736
    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
 
    }
 
737
    ret = asprintf(&filename, "%s/%s", plugindir, dirst->d_name);
744
738
    if(ret < 0){
745
739
      perror("asprintf");
746
740
      continue;
846
840
        perror("sigaction");
847
841
        _exit(EXIT_FAILURE);
848
842
      }
849
 
      ret = sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
 
843
      ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
850
844
      if(ret < 0){
851
845
        perror("sigprocmask");
852
846
        _exit(EXIT_FAILURE);
853
847
      }
854
 
      
 
848
 
855
849
      ret = dup2(pipefd[1], STDOUT_FILENO); /* replace our stdout */
856
850
      if(ret == -1){
857
851
        perror("dup2");
907
901
    if (maxfd < new_plugin->fd){
908
902
      maxfd = new_plugin->fd;
909
903
    }
 
904
    
910
905
  }
911
906
  
912
907
  closedir(dir);
913
908
  dir = NULL;
914
 
  
 
909
 
915
910
  for(plugin *p = plugin_list; p != NULL; p = p->next){
916
911
    if(p->pid != 0){
917
912
      break;
922
917
      free_plugin_list();
923
918
    }
924
919
  }
925
 
  
 
920
 
926
921
  /* Main loop while running plugins exist */
927
922
  while(plugin_list){
928
923
    fd_set rfds = rfds_all;
934
929
    }
935
930
    /* OK, now either a process completed, or something can be read
936
931
       from one of them */
937
 
    for(plugin *proc = plugin_list; proc != NULL;){
 
932
    for(plugin *proc = plugin_list; proc != NULL; proc = proc->next){
938
933
      /* Is this process completely done? */
939
934
      if(proc->eof and proc->completed){
940
935
        /* Only accept the plugin output if it exited cleanly */
967
962
            exitstatus = EXIT_FAILURE;
968
963
            goto fallback;
969
964
          }
970
 
          
971
 
          plugin *next_plugin = proc->next;
972
965
          free_plugin(proc);
973
 
          proc = next_plugin;
974
 
          
975
966
          /* We are done modifying process list, so unblock signal */
976
967
          ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask,
977
968
                             NULL);
984
975
          if(plugin_list == NULL){
985
976
            break;
986
977
          }
987
 
          
988
978
          continue;
989
979
        }
990
980
        
991
981
        /* This process exited nicely, so print its buffer */
992
 
        
 
982
 
993
983
        bool bret = print_out_password(proc->buffer,
994
984
                                       proc->buffer_length);
995
985
        if(not bret){
1002
992
      /* This process has not completed.  Does it have any output? */
1003
993
      if(proc->eof or not FD_ISSET(proc->fd, &rfds)){
1004
994
        /* This process had nothing to say at this time */
1005
 
        proc = proc->next;
1006
995
        continue;
1007
996
      }
1008
997
      /* Before reading, make the process' data buffer large enough */
1021
1010
                 BUFFER_SIZE);
1022
1011
      if(ret < 0){
1023
1012
        /* Read error from this process; ignore the error */
1024
 
        proc = proc->next;
1025
1013
        continue;
1026
1014
      }
1027
1015
      if(ret == 0){
1042
1030
    bool bret;
1043
1031
    fprintf(stderr, "Going to fallback mode using getpass(3)\n");
1044
1032
    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);
 
1033
    bret = print_out_password(passwordbuffer, strlen(passwordbuffer));
1052
1034
    if(not bret){
1053
1035
      perror("print_out_password");
1054
1036
      exitstatus = EXIT_FAILURE;
1061
1043
    perror("sigaction");
1062
1044
    exitstatus = EXIT_FAILURE;
1063
1045
  }
1064
 
  
 
1046
 
1065
1047
  if(custom_argv != NULL){
1066
1048
    for(char **arg = custom_argv+1; *arg != NULL; arg++){
1067
1049
      free(*arg);
1073
1055
    closedir(dir);
1074
1056
  }
1075
1057
  
1076
 
  /* Kill the processes */
 
1058
  /* Free the process list and kill the processes */
1077
1059
  for(plugin *p = plugin_list; p != NULL; p = p->next){
1078
1060
    if(p->pid != 0){
1079
1061
      close(p->fd);
1092
1074
  if(errno != ECHILD){
1093
1075
    perror("wait");
1094
1076
  }
1095
 
  
 
1077
 
1096
1078
  free_plugin_list();
1097
1079
  
1098
1080
  free(plugindir);