/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: 2009-01-23 20:09:55 UTC
  • Revision ID: teddy@fukt.bsnet.se-20090123200955-h9oy2hwyv8uly1op
* mandos (main): Bug fix: use "getint" on the "port" config file
                 option.
* plugins.d/mandos-client.c (main): Fixed spelling of
                                    "gnutls_initalized" and
                                    "gpgme_initalized".

Show diffs side-by-side

added added

removed removed

Lines of Context:
38
38
#include <sys/select.h>         /* fd_set, select(), FD_ZERO(),
39
39
                                   FD_SET(), FD_ISSET(), FD_CLR */
40
40
#include <sys/wait.h>           /* wait(), waitpid(), WIFEXITED(),
41
 
                                   WEXITSTATUS(), WTERMSIG(),
42
 
                                   WCOREDUMP() */
 
41
                                   WEXITSTATUS() */
43
42
#include <sys/stat.h>           /* struct stat, stat(), S_ISREG() */
44
43
#include <iso646.h>             /* and, or, not */
45
44
#include <dirent.h>             /* DIR, struct dirent, opendir(),
53
52
                                   close() */
54
53
#include <fcntl.h>              /* fcntl(), F_GETFD, F_SETFD,
55
54
                                   FD_CLOEXEC */
56
 
#include <string.h>             /* strsep, strlen(), asprintf(),
57
 
                                   strsignal() */
 
55
#include <string.h>             /* strsep, strlen(), asprintf() */
58
56
#include <errno.h>              /* errno */
59
57
#include <argp.h>               /* struct argp_option, struct
60
58
                                   argp_state, struct argp,
64
62
#include <signal.h>             /* struct sigaction, sigemptyset(),
65
63
                                   sigaddset(), sigaction(),
66
64
                                   sigprocmask(), SIG_BLOCK, SIGCHLD,
67
 
                                   SIG_UNBLOCK, kill(), sig_atomic_t
68
 
                                */
 
65
                                   SIG_UNBLOCK, kill() */
69
66
#include <errno.h>              /* errno, EBADF */
70
 
#include <inttypes.h>           /* intmax_t, PRIdMAX, strtoimax() */
 
67
#include <inttypes.h>           /* intmax_t, SCNdMAX, PRIdMAX,  */
71
68
 
72
69
#define BUFFER_SIZE 256
73
70
 
84
81
  char **environ;
85
82
  int envc;
86
83
  bool disabled;
87
 
  
 
84
 
88
85
  /* Variables used for running processes*/
89
86
  pid_t pid;
90
87
  int fd;
92
89
  size_t buffer_size;
93
90
  size_t buffer_length;
94
91
  bool eof;
95
 
  volatile sig_atomic_t completed;
96
 
  int status;
 
92
  volatile bool completed;
 
93
  volatile int status;
97
94
  struct plugin *next;
98
95
} plugin;
99
96
 
123
120
    }
124
121
  }
125
122
  
126
 
  *new_plugin = (plugin){ .name = copy_name,
127
 
                          .argc = 1,
128
 
                          .disabled = false,
129
 
                          .next = plugin_list };
 
123
  *new_plugin = (plugin) { .name = copy_name,
 
124
                           .argc = 1,
 
125
                           .disabled = false,
 
126
                           .next = plugin_list };
130
127
  
131
128
  new_plugin->argv = malloc(sizeof(char *) * 2);
132
129
  if(new_plugin->argv == NULL){
226
223
/* Mark processes as completed when they exit, and save their exit
227
224
   status. */
228
225
static void handle_sigchld(__attribute__((unused)) int sig){
229
 
  int old_errno = errno;
230
226
  while(true){
231
227
    plugin *proc = plugin_list;
232
228
    int status;
236
232
      break;
237
233
    }
238
234
    if(pid == -1){
239
 
      if(errno == ECHILD){
240
 
        /* No child processes */
241
 
        break;
 
235
      if(errno != ECHILD){
 
236
        perror("waitpid");
242
237
      }
243
 
      perror("waitpid");
 
238
      /* No child processes */
 
239
      break;
244
240
    }
245
241
    
246
242
    /* A child exited, find it in process_list */
252
248
      continue;
253
249
    }
254
250
    proc->status = status;
255
 
    proc->completed = 1;
 
251
    proc->completed = true;
256
252
  }
257
 
  errno = old_errno;
258
253
}
259
254
 
260
255
/* Prints out a password to stdout */
282
277
  }
283
278
  free(plugin_node->environ);
284
279
  free(plugin_node->buffer);
285
 
  
 
280
 
286
281
  /* Removes the plugin from the singly-linked list */
287
282
  if(plugin_node == plugin_list){
288
283
    /* First one - simple */
315
310
  struct dirent *dirst;
316
311
  struct stat st;
317
312
  fd_set rfds_all;
318
 
  int ret, maxfd = 0;
 
313
  int ret, numchars, maxfd = 0;
319
314
  ssize_t sret;
320
315
  intmax_t tmpmax;
321
316
  uid_t uid = 65534;
381
376
  };
382
377
  
383
378
  error_t parse_opt(int key, char *arg, __attribute__((unused))
384
 
                    struct argp_state *state){
385
 
    char *tmp;
386
 
    switch(key){
 
379
                    struct argp_state *state) {
 
380
    switch(key) {
387
381
    case 'g':                   /* --global-options */
388
382
      if(arg != NULL){
389
 
        char *plugin_option;
390
 
        while((plugin_option = strsep(&arg, ",")) != NULL){
391
 
          if(plugin_option[0] == '\0'){
 
383
        char *p;
 
384
        while((p = strsep(&arg, ",")) != NULL){
 
385
          if(p[0] == '\0'){
392
386
            continue;
393
387
          }
394
 
          if(not add_argument(getplugin(NULL), plugin_option)){
 
388
          if(not add_argument(getplugin(NULL), p)){
395
389
            perror("add_argument");
396
390
            return ARGP_ERR_UNKNOWN;
397
391
          }
408
402
      break;
409
403
    case 'o':                   /* --options-for */
410
404
      if(arg != NULL){
411
 
        char *plugin_name = strsep(&arg, ":");
412
 
        if(plugin_name[0] == '\0'){
413
 
          break;
414
 
        }
415
 
        char *plugin_option;
416
 
        while((plugin_option = strsep(&arg, ",")) != NULL){
417
 
          if(not add_argument(getplugin(plugin_name), plugin_option)){
 
405
        char *p_name = strsep(&arg, ":");
 
406
        if(p_name[0] == '\0' or arg == NULL){
 
407
          break;
 
408
        }
 
409
        char *opt = strsep(&arg, ":");
 
410
        if(opt[0] == '\0' or opt == NULL){
 
411
          break;
 
412
        }
 
413
        char *p;
 
414
        while((p = strsep(&opt, ",")) != NULL){
 
415
          if(p[0] == '\0'){
 
416
            continue;
 
417
          }
 
418
          if(not add_argument(getplugin(p_name), p)){
418
419
            perror("add_argument");
419
420
            return ARGP_ERR_UNKNOWN;
420
421
          }
459
460
      plugindir = strdup(arg);
460
461
      if(plugindir == NULL){
461
462
        perror("strdup");
462
 
      }
 
463
      }      
463
464
      break;
464
465
    case 129:                   /* --config-file */
465
466
      /* This is already done by parse_opt_config_file() */
466
467
      break;
467
468
    case 130:                   /* --userid */
468
 
      errno = 0;
469
 
      tmpmax = strtoimax(arg, &tmp, 10);
470
 
      if(errno != 0 or tmp == arg or *tmp != '\0'
471
 
         or tmpmax != (uid_t)tmpmax){
 
469
      ret = sscanf(arg, "%" SCNdMAX "%n", &tmpmax, &numchars);
 
470
      if(ret < 1 or tmpmax != (uid_t)tmpmax
 
471
         or arg[numchars] != '\0'){
472
472
        fprintf(stderr, "Bad user ID number: \"%s\", using %"
473
473
                PRIdMAX "\n", arg, (intmax_t)uid);
474
474
      } else {
476
476
      }
477
477
      break;
478
478
    case 131:                   /* --groupid */
479
 
      errno = 0;
480
 
      tmpmax = strtoimax(arg, &tmp, 10);
481
 
      if(errno != 0 or tmp == arg or *tmp != '\0'
482
 
         or tmpmax != (gid_t)tmpmax){
 
479
      ret = sscanf(arg, "%" SCNdMAX "%n", &tmpmax, &numchars);
 
480
      if(ret < 1 or tmpmax != (gid_t)tmpmax
 
481
         or arg[numchars] != '\0'){
483
482
        fprintf(stderr, "Bad group ID number: \"%s\", using %"
484
483
                PRIdMAX "\n", arg, (intmax_t)gid);
485
484
      } else {
513
512
     ignores everything but the --config-file option. */
514
513
  error_t parse_opt_config_file(int key, char *arg,
515
514
                                __attribute__((unused))
516
 
                                struct argp_state *state){
517
 
    switch(key){
 
515
                                struct argp_state *state) {
 
516
    switch(key) {
518
517
    case 'g':                   /* --global-options */
519
518
    case 'G':                   /* --global-env */
520
519
    case 'o':                   /* --options-for */
529
528
      if(argfile == NULL){
530
529
        perror("strdup");
531
530
      }
532
 
      break;
 
531
      break;      
533
532
    case 130:                   /* --userid */
534
533
    case 131:                   /* --groupid */
535
534
    case 132:                   /* --debug */
564
563
    conffp = fopen(AFILE, "r");
565
564
  } else {
566
565
    conffp = fopen(argfile, "r");
567
 
  }
 
566
  }  
568
567
  if(conffp != NULL){
569
568
    char *org_line = NULL;
570
569
    char *p, *arg, *new_arg, *line;
571
570
    size_t size = 0;
572
571
    const char whitespace_delims[] = " \r\t\f\v\n";
573
572
    const char comment_delim[] = "#";
574
 
    
 
573
 
575
574
    custom_argc = 1;
576
575
    custom_argv = malloc(sizeof(char*) * 2);
577
576
    if(custom_argv == NULL){
581
580
    }
582
581
    custom_argv[0] = argv[0];
583
582
    custom_argv[1] = NULL;
584
 
    
 
583
 
585
584
    /* for each line in the config file, strip whitespace and ignore
586
585
       commented text */
587
586
    while(true){
589
588
      if(sret == -1){
590
589
        break;
591
590
      }
592
 
      
 
591
 
593
592
      line = org_line;
594
593
      arg = strsep(&line, comment_delim);
595
594
      while((p = strsep(&arg, whitespace_delims)) != NULL){
614
613
          goto fallback;
615
614
        }
616
615
        custom_argv[custom_argc-1] = new_arg;
617
 
        custom_argv[custom_argc] = NULL;
 
616
        custom_argv[custom_argc] = NULL;        
618
617
      }
619
618
    }
620
619
    free(org_line);
663
662
  }
664
663
  
665
664
  /* Strip permissions down to nobody */
 
665
  ret = setuid(uid);
 
666
  if(ret == -1){
 
667
    perror("setuid");
 
668
  }  
666
669
  setgid(gid);
667
670
  if(ret == -1){
668
671
    perror("setgid");
669
672
  }
670
 
  ret = setuid(uid);
671
 
  if(ret == -1){
672
 
    perror("setuid");
673
 
  }
674
673
  
675
674
  if(plugindir == NULL){
676
675
    dir = opendir(PDIR);
758
757
        continue;
759
758
      }
760
759
    }
761
 
    
 
760
 
762
761
    char *filename;
763
762
    if(plugindir == NULL){
764
763
      ret = asprintf(&filename, PDIR "/%s", dirst->d_name);
776
775
      free(filename);
777
776
      continue;
778
777
    }
779
 
    
 
778
 
780
779
    /* Ignore non-executable files */
781
780
    if(not S_ISREG(st.st_mode) or (access(filename, X_OK) != 0)){
782
781
      if(debug){
935
934
  
936
935
  closedir(dir);
937
936
  dir = NULL;
938
 
  free_plugin(getplugin(NULL));
939
937
  
940
938
  for(plugin *p = plugin_list; p != NULL; p = p->next){
941
939
    if(p->pid != 0){
961
959
       from one of them */
962
960
    for(plugin *proc = plugin_list; proc != NULL;){
963
961
      /* Is this process completely done? */
964
 
      if(proc->completed and proc->eof){
 
962
      if(proc->eof and proc->completed){
965
963
        /* Only accept the plugin output if it exited cleanly */
966
964
        if(not WIFEXITED(proc->status)
967
965
           or WEXITSTATUS(proc->status) != 0){
968
966
          /* Bad exit by plugin */
969
 
          
 
967
 
970
968
          if(debug){
971
969
            if(WIFEXITED(proc->status)){
972
 
              fprintf(stderr, "Plugin %s [%" PRIdMAX "] exited with"
973
 
                      " status %d\n", proc->name,
974
 
                      (intmax_t) (proc->pid),
 
970
              fprintf(stderr, "Plugin %" PRIdMAX " exited with status"
 
971
                      " %d\n", (intmax_t) (proc->pid),
975
972
                      WEXITSTATUS(proc->status));
976
 
            } else if(WIFSIGNALED(proc->status)){
977
 
              fprintf(stderr, "Plugin %s [%" PRIdMAX "] killed by"
978
 
                      " signal %d: %s\n", proc->name,
979
 
                      (intmax_t) (proc->pid),
980
 
                      WTERMSIG(proc->status),
981
 
                      strsignal(WTERMSIG(proc->status)));
 
973
            } else if(WIFSIGNALED(proc->status)) {
 
974
              fprintf(stderr, "Plugin %" PRIdMAX " killed by signal"
 
975
                      " %d\n", (intmax_t) (proc->pid),
 
976
                      WTERMSIG(proc->status));
982
977
            } else if(WCOREDUMP(proc->status)){
983
 
              fprintf(stderr, "Plugin %s [%" PRIdMAX "] dumped"
984
 
                      " core\n", proc->name, (intmax_t) (proc->pid));
 
978
              fprintf(stderr, "Plugin %" PRIdMAX " dumped core\n",
 
979
                      (intmax_t) (proc->pid));
985
980
            }
986
981
          }
987
982
          
988
983
          /* Remove the plugin */
989
984
          FD_CLR(proc->fd, &rfds_all);
990
 
          
 
985
 
991
986
          /* Block signal while modifying process_list */
992
987
          ret = sigprocmask(SIG_BLOCK, &sigchld_action.sa_mask, NULL);
993
988
          if(ret < 0){
1060
1055
      }
1061
1056
    }
1062
1057
  }
1063
 
  
1064
 
  
 
1058
 
 
1059
 
1065
1060
 fallback:
1066
1061
  
1067
1062
  if(plugin_list == NULL or exitstatus != EXIT_SUCCESS){