/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-15 02:52:02 UTC
  • Revision ID: teddy@fukt.bsnet.se-20090115025202-utyjssfivzumvw7m
* debian/watch: New file.

* debian/mandos-client.README.Debian (Emergency Escape): New section;
                                                         document the
                                                         "mandos=off"
                                                         kernel
                                                         parameter.
* initramfs-tools-script: Exit if kernel has parameter "mandos=off".

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