/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-03-27 13:33:17 UTC
  • mfrom: (288.1.2 mandos-pipe-ipc)
  • Revision ID: teddy@fukt.bsnet.se-20090327133317-riwt5s5orrisozuj
Merge from pipe IPC branch.

* mandos (AvahiService.rename, main): Include PID in log messages when
                                      using a different service name.
  (Client.ReceivedSecret, Client.Rejected): New D-Bus signals.
  (TCP_handler.handle): Send IPC to parent process.
  (ForkingMixInWithPipe): New mixin class.
  (IPv6_TCPServer): Inherit from ForkingMixInWithPipe instead of
                    SocketServer.ForkingMixIn.
  (IPv6_TCPServer.handle_ipc): New method.
  (main/mandos_server): Renamed to "mandos_dbus_service" and made
                        global.
  (main/MandosServer): Renamed to "MandosDBusService".
  (main/MandosDBusService.ClientNotFound): New D-Bus signal.

Show diffs side-by-side

added added

removed removed

Lines of Context:
62
62
#include <signal.h>             /* struct sigaction, sigemptyset(),
63
63
                                   sigaddset(), sigaction(),
64
64
                                   sigprocmask(), SIG_BLOCK, SIGCHLD,
65
 
                                   SIG_UNBLOCK, kill() */
 
65
                                   SIG_UNBLOCK, kill(), sig_atomic_t
 
66
                                */
66
67
#include <errno.h>              /* errno, EBADF */
 
68
#include <inttypes.h>           /* intmax_t, PRIdMAX, strtoimax() */
67
69
 
68
70
#define BUFFER_SIZE 256
69
71
 
80
82
  char **environ;
81
83
  int envc;
82
84
  bool disabled;
83
 
 
 
85
  
84
86
  /* Variables used for running processes*/
85
87
  pid_t pid;
86
88
  int fd;
88
90
  size_t buffer_size;
89
91
  size_t buffer_length;
90
92
  bool eof;
91
 
  volatile bool completed;
92
 
  volatile int status;
 
93
  volatile sig_atomic_t completed;
 
94
  int status;
93
95
  struct plugin *next;
94
96
} plugin;
95
97
 
99
101
   or if none is found, creates a new one */
100
102
static plugin *getplugin(char *name){
101
103
  /* Check for exiting plugin with that name */
102
 
  for (plugin *p = plugin_list; p != NULL; p = p->next){
103
 
    if ((p->name == name)
104
 
        or (p->name and name and (strcmp(p->name, name) == 0))){
 
104
  for(plugin *p = plugin_list; p != NULL; p = p->next){
 
105
    if((p->name == name)
 
106
       or (p->name and name and (strcmp(p->name, name) == 0))){
105
107
      return p;
106
108
    }
107
109
  }
108
110
  /* Create a new plugin */
109
111
  plugin *new_plugin = malloc(sizeof(plugin));
110
 
  if (new_plugin == NULL){
 
112
  if(new_plugin == NULL){
111
113
    return NULL;
112
114
  }
113
115
  char *copy_name = NULL;
114
116
  if(name != NULL){
115
117
    copy_name = strdup(name);
116
118
    if(copy_name == NULL){
 
119
      free(new_plugin);
117
120
      return NULL;
118
121
    }
119
122
  }
120
123
  
121
 
  *new_plugin = (plugin) { .name = copy_name,
122
 
                           .argc = 1,
123
 
                           .disabled = false,
124
 
                           .next = plugin_list };
 
124
  *new_plugin = (plugin){ .name = copy_name,
 
125
                          .argc = 1,
 
126
                          .disabled = false,
 
127
                          .next = plugin_list };
125
128
  
126
129
  new_plugin->argv = malloc(sizeof(char *) * 2);
127
 
  if (new_plugin->argv == NULL){
 
130
  if(new_plugin->argv == NULL){
128
131
    free(copy_name);
129
132
    free(new_plugin);
130
133
    return NULL;
221
224
/* Mark processes as completed when they exit, and save their exit
222
225
   status. */
223
226
static void handle_sigchld(__attribute__((unused)) int sig){
 
227
  int old_errno = errno;
224
228
  while(true){
225
229
    plugin *proc = plugin_list;
226
230
    int status;
230
234
      break;
231
235
    }
232
236
    if(pid == -1){
233
 
      if (errno != ECHILD){
234
 
        perror("waitpid");
 
237
      if(errno == ECHILD){
 
238
        /* No child processes */
 
239
        break;
235
240
      }
236
 
      /* No child processes */
237
 
      break;
 
241
      perror("waitpid");
238
242
    }
239
243
    
240
244
    /* A child exited, find it in process_list */
246
250
      continue;
247
251
    }
248
252
    proc->status = status;
249
 
    proc->completed = true;
 
253
    proc->completed = 1;
250
254
  }
 
255
  errno = old_errno;
251
256
}
252
257
 
253
258
/* Prints out a password to stdout */
275
280
  }
276
281
  free(plugin_node->environ);
277
282
  free(plugin_node->buffer);
278
 
 
 
283
  
279
284
  /* Removes the plugin from the singly-linked list */
280
285
  if(plugin_node == plugin_list){
281
286
    /* First one - simple */
310
315
  fd_set rfds_all;
311
316
  int ret, maxfd = 0;
312
317
  ssize_t sret;
 
318
  intmax_t tmpmax;
313
319
  uid_t uid = 65534;
314
320
  gid_t gid = 65534;
315
321
  bool debug = false;
372
378
    { .name = NULL }
373
379
  };
374
380
  
375
 
  error_t parse_opt (int key, char *arg, __attribute__((unused))
376
 
                     struct argp_state *state) {
377
 
    switch (key) {
 
381
  error_t parse_opt(int key, char *arg, __attribute__((unused))
 
382
                    struct argp_state *state){
 
383
    char *tmp;
 
384
    switch(key){
378
385
    case 'g':                   /* --global-options */
379
 
      if (arg != NULL){
380
 
        char *p;
381
 
        while((p = strsep(&arg, ",")) != NULL){
382
 
          if(p[0] == '\0'){
 
386
      if(arg != NULL){
 
387
        char *plugin_option;
 
388
        while((plugin_option = strsep(&arg, ",")) != NULL){
 
389
          if(plugin_option[0] == '\0'){
383
390
            continue;
384
391
          }
385
 
          if(not add_argument(getplugin(NULL), p)){
 
392
          if(not add_argument(getplugin(NULL), plugin_option)){
386
393
            perror("add_argument");
387
394
            return ARGP_ERR_UNKNOWN;
388
395
          }
398
405
      }
399
406
      break;
400
407
    case 'o':                   /* --options-for */
401
 
      if (arg != NULL){
402
 
        char *p_name = strsep(&arg, ":");
403
 
        if(p_name[0] == '\0' or arg == NULL){
404
 
          break;
405
 
        }
406
 
        char *opt = strsep(&arg, ":");
407
 
        if(opt[0] == '\0' or opt == NULL){
408
 
          break;
409
 
        }
410
 
        char *p;
411
 
        while((p = strsep(&opt, ",")) != NULL){
412
 
          if(p[0] == '\0'){
413
 
            continue;
414
 
          }
415
 
          if(not add_argument(getplugin(p_name), p)){
 
408
      if(arg != NULL){
 
409
        char *plugin_name = strsep(&arg, ":");
 
410
        if(plugin_name[0] == '\0'){
 
411
          break;
 
412
        }
 
413
        char *plugin_option;
 
414
        while((plugin_option = strsep(&arg, ",")) != NULL){
 
415
          if(not add_argument(getplugin(plugin_name), plugin_option)){
416
416
            perror("add_argument");
417
417
            return ARGP_ERR_UNKNOWN;
418
418
          }
435
435
      }
436
436
      break;
437
437
    case 'd':                   /* --disable */
438
 
      if (arg != NULL){
 
438
      if(arg != NULL){
439
439
        plugin *p = getplugin(arg);
440
440
        if(p == NULL){
441
441
          return ARGP_ERR_UNKNOWN;
444
444
      }
445
445
      break;
446
446
    case 'e':                   /* --enable */
447
 
      if (arg != NULL){
 
447
      if(arg != NULL){
448
448
        plugin *p = getplugin(arg);
449
449
        if(p == NULL){
450
450
          return ARGP_ERR_UNKNOWN;
463
463
      /* This is already done by parse_opt_config_file() */
464
464
      break;
465
465
    case 130:                   /* --userid */
466
 
      /* In the GNU C library, uid_t is always unsigned int */
467
 
      ret = sscanf(arg, "%ud", &uid);
468
 
      if(ret != 1){
469
 
        fprintf(stderr, "Bad user ID number: \"%s\", using %ud\n",
470
 
                arg, uid);
 
466
      errno = 0;
 
467
      tmpmax = strtoimax(arg, &tmp, 10);
 
468
      if(errno != 0 or tmp == arg or *tmp != '\0'
 
469
         or tmpmax != (uid_t)tmpmax){
 
470
        fprintf(stderr, "Bad user ID number: \"%s\", using %"
 
471
                PRIdMAX "\n", arg, (intmax_t)uid);
 
472
      } else {
 
473
        uid = (uid_t)tmpmax;
471
474
      }
472
475
      break;
473
476
    case 131:                   /* --groupid */
474
 
      /* In the GNU C library, gid_t is always unsigned int */
475
 
      ret = sscanf(arg, "%ud", &gid);
476
 
      if(ret != 1){
477
 
        fprintf(stderr, "Bad group ID number: \"%s\", using %ud\n",
478
 
                arg, gid);
 
477
      errno = 0;
 
478
      tmpmax = strtoimax(arg, &tmp, 10);
 
479
      if(errno != 0 or tmp == arg or *tmp != '\0'
 
480
         or tmpmax != (gid_t)tmpmax){
 
481
        fprintf(stderr, "Bad group ID number: \"%s\", using %"
 
482
                PRIdMAX "\n", arg, (intmax_t)gid);
 
483
      } else {
 
484
        gid = (gid_t)tmpmax;
479
485
      }
480
486
      break;
481
487
    case 132:                   /* --debug */
503
509
  
504
510
  /* This option parser is the same as parse_opt() above, except it
505
511
     ignores everything but the --config-file option. */
506
 
  error_t parse_opt_config_file (int key, char *arg,
507
 
                                 __attribute__((unused))
508
 
                                 struct argp_state *state) {
509
 
    switch (key) {
 
512
  error_t parse_opt_config_file(int key, char *arg,
 
513
                                __attribute__((unused))
 
514
                                struct argp_state *state){
 
515
    switch(key){
510
516
    case 'g':                   /* --global-options */
511
517
    case 'G':                   /* --global-env */
512
518
    case 'o':                   /* --options-for */
539
545
                       .args_doc = "",
540
546
                       .doc = "Mandos plugin runner -- Run plugins" };
541
547
  
542
 
  /* Parse using the parse_opt_config_file in order to get the custom
 
548
  /* Parse using parse_opt_config_file() in order to get the custom
543
549
     config file location, if any. */
544
 
  ret = argp_parse (&argp, argc, argv, ARGP_IN_ORDER, 0, NULL);
545
 
  if (ret == ARGP_ERR_UNKNOWN){
 
550
  ret = argp_parse(&argp, argc, argv, ARGP_IN_ORDER, 0, NULL);
 
551
  if(ret == ARGP_ERR_UNKNOWN){
546
552
    fprintf(stderr, "Unknown error while parsing arguments\n");
547
553
    exitstatus = EXIT_FAILURE;
548
554
    goto fallback;
552
558
  argp.parser = parse_opt;
553
559
  
554
560
  /* Open the configfile if available */
555
 
  if (argfile == NULL){
 
561
  if(argfile == NULL){
556
562
    conffp = fopen(AFILE, "r");
557
563
  } else {
558
564
    conffp = fopen(argfile, "r");
563
569
    size_t size = 0;
564
570
    const char whitespace_delims[] = " \r\t\f\v\n";
565
571
    const char comment_delim[] = "#";
566
 
 
 
572
    
567
573
    custom_argc = 1;
568
574
    custom_argv = malloc(sizeof(char*) * 2);
569
575
    if(custom_argv == NULL){
573
579
    }
574
580
    custom_argv[0] = argv[0];
575
581
    custom_argv[1] = NULL;
576
 
 
 
582
    
577
583
    /* for each line in the config file, strip whitespace and ignore
578
584
       commented text */
579
585
    while(true){
581
587
      if(sret == -1){
582
588
        break;
583
589
      }
584
 
 
 
590
      
585
591
      line = org_line;
586
592
      arg = strsep(&line, comment_delim);
587
593
      while((p = strsep(&arg, whitespace_delims)) != NULL){
613
619
  } else {
614
620
    /* Check for harmful errors and go to fallback. Other errors might
615
621
       not affect opening plugins */
616
 
    if (errno == EMFILE or errno == ENFILE or errno == ENOMEM){
 
622
    if(errno == EMFILE or errno == ENFILE or errno == ENOMEM){
617
623
      perror("fopen");
618
624
      exitstatus = EXIT_FAILURE;
619
625
      goto fallback;
622
628
  /* If there was any arguments from configuration file,
623
629
     pass them to parser as command arguments */
624
630
  if(custom_argv != NULL){
625
 
    ret = argp_parse (&argp, custom_argc, custom_argv, ARGP_IN_ORDER,
626
 
                      0, NULL);
627
 
    if (ret == ARGP_ERR_UNKNOWN){
 
631
    ret = argp_parse(&argp, custom_argc, custom_argv, ARGP_IN_ORDER,
 
632
                     0, NULL);
 
633
    if(ret == ARGP_ERR_UNKNOWN){
628
634
      fprintf(stderr, "Unknown error while parsing arguments\n");
629
635
      exitstatus = EXIT_FAILURE;
630
636
      goto fallback;
633
639
  
634
640
  /* Parse actual command line arguments, to let them override the
635
641
     config file */
636
 
  ret = argp_parse (&argp, argc, argv, ARGP_IN_ORDER, 0, NULL);
637
 
  if (ret == ARGP_ERR_UNKNOWN){
 
642
  ret = argp_parse(&argp, argc, argv, ARGP_IN_ORDER, 0, NULL);
 
643
  if(ret == ARGP_ERR_UNKNOWN){
638
644
    fprintf(stderr, "Unknown error while parsing arguments\n");
639
645
    exitstatus = EXIT_FAILURE;
640
646
    goto fallback;
647
653
      for(char **a = p->argv; *a != NULL; a++){
648
654
        fprintf(stderr, "\tArg: %s\n", *a);
649
655
      }
650
 
      fprintf(stderr, "...and %u environment variables\n", p->envc);
 
656
      fprintf(stderr, "...and %d environment variables\n", p->envc);
651
657
      for(char **a = p->environ; *a != NULL; a++){
652
658
        fprintf(stderr, "\t%s\n", *a);
653
659
      }
655
661
  }
656
662
  
657
663
  /* Strip permissions down to nobody */
 
664
  setgid(gid);
 
665
  if(ret == -1){
 
666
    perror("setgid");
 
667
  }
658
668
  ret = setuid(uid);
659
 
  if (ret == -1){
 
669
  if(ret == -1){
660
670
    perror("setuid");
661
 
  }  
662
 
  setgid(gid);
663
 
  if (ret == -1){
664
 
    perror("setgid");
665
671
  }
666
672
  
667
 
  if (plugindir == NULL){
 
673
  if(plugindir == NULL){
668
674
    dir = opendir(PDIR);
669
675
  } else {
670
676
    dir = opendir(plugindir);
697
703
    
698
704
    /* All directory entries have been processed */
699
705
    if(dirst == NULL){
700
 
      if (errno == EBADF){
 
706
      if(errno == EBADF){
701
707
        perror("readdir");
702
708
        exitstatus = EXIT_FAILURE;
703
709
        goto fallback;
750
756
        continue;
751
757
      }
752
758
    }
753
 
 
 
759
    
754
760
    char *filename;
755
761
    if(plugindir == NULL){
756
762
      ret = asprintf(&filename, PDIR "/%s", dirst->d_name);
763
769
    }
764
770
    
765
771
    ret = stat(filename, &st);
766
 
    if (ret == -1){
 
772
    if(ret == -1){
767
773
      perror("stat");
768
774
      free(filename);
769
775
      continue;
770
776
    }
771
 
 
 
777
    
772
778
    /* Ignore non-executable files */
773
 
    if (not S_ISREG(st.st_mode) or (access(filename, X_OK) != 0)){
 
779
    if(not S_ISREG(st.st_mode) or (access(filename, X_OK) != 0)){
774
780
      if(debug){
775
781
        fprintf(stderr, "Ignoring plugin dir entry \"%s\""
776
782
                " with bad type or mode\n", filename);
823
829
    
824
830
    int pipefd[2];
825
831
    ret = pipe(pipefd);
826
 
    if (ret == -1){
 
832
    if(ret == -1){
827
833
      perror("pipe");
828
834
      exitstatus = EXIT_FAILURE;
829
835
      goto fallback;
842
848
      goto fallback;
843
849
    }
844
850
    /* Block SIGCHLD until process is safely in process list */
845
 
    ret = sigprocmask (SIG_BLOCK, &sigchld_action.sa_mask, NULL);
 
851
    ret = sigprocmask(SIG_BLOCK, &sigchld_action.sa_mask, NULL);
846
852
    if(ret < 0){
847
853
      perror("sigprocmask");
848
854
      exitstatus = EXIT_FAILURE;
896
902
    close(pipefd[1]);           /* Close unused write end of pipe */
897
903
    free(filename);
898
904
    plugin *new_plugin = getplugin(dirst->d_name);
899
 
    if (new_plugin == NULL){
 
905
    if(new_plugin == NULL){
900
906
      perror("getplugin");
901
 
      ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
 
907
      ret = sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
902
908
      if(ret < 0){
903
909
        perror("sigprocmask");
904
910
      }
911
917
    
912
918
    /* Unblock SIGCHLD so signal handler can be run if this process
913
919
       has already completed */
914
 
    ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
 
920
    ret = sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
915
921
    if(ret < 0){
916
922
      perror("sigprocmask");
917
923
      exitstatus = EXIT_FAILURE;
920
926
    
921
927
    FD_SET(new_plugin->fd, &rfds_all);
922
928
    
923
 
    if (maxfd < new_plugin->fd){
 
929
    if(maxfd < new_plugin->fd){
924
930
      maxfd = new_plugin->fd;
925
931
    }
926
932
  }
927
933
  
928
934
  closedir(dir);
929
935
  dir = NULL;
 
936
  free_plugin(getplugin(NULL));
930
937
  
931
938
  for(plugin *p = plugin_list; p != NULL; p = p->next){
932
939
    if(p->pid != 0){
943
950
  while(plugin_list){
944
951
    fd_set rfds = rfds_all;
945
952
    int select_ret = select(maxfd+1, &rfds, NULL, NULL, NULL);
946
 
    if (select_ret == -1){
 
953
    if(select_ret == -1){
947
954
      perror("select");
948
955
      exitstatus = EXIT_FAILURE;
949
956
      goto fallback;
952
959
       from one of them */
953
960
    for(plugin *proc = plugin_list; proc != NULL;){
954
961
      /* Is this process completely done? */
955
 
      if(proc->eof and proc->completed){
 
962
      if(proc->completed and proc->eof){
956
963
        /* Only accept the plugin output if it exited cleanly */
957
964
        if(not WIFEXITED(proc->status)
958
965
           or WEXITSTATUS(proc->status) != 0){
959
966
          /* Bad exit by plugin */
960
 
 
 
967
          
961
968
          if(debug){
962
969
            if(WIFEXITED(proc->status)){
963
 
              fprintf(stderr, "Plugin %u exited with status %d\n",
964
 
                      (unsigned int) (proc->pid),
 
970
              fprintf(stderr, "Plugin %s [%" PRIdMAX "] exited with"
 
971
                      " status %d\n", proc->name,
 
972
                      (intmax_t) (proc->pid),
965
973
                      WEXITSTATUS(proc->status));
966
 
            } else if(WIFSIGNALED(proc->status)) {
967
 
              fprintf(stderr, "Plugin %u killed by signal %d\n",
968
 
                      (unsigned int) (proc->pid),
 
974
            } else if(WIFSIGNALED(proc->status)){
 
975
              fprintf(stderr, "Plugin %s [%" PRIdMAX "] killed by"
 
976
                      " signal %d\n", proc->name,
 
977
                      (intmax_t) (proc->pid),
969
978
                      WTERMSIG(proc->status));
970
979
            } else if(WCOREDUMP(proc->status)){
971
 
              fprintf(stderr, "Plugin %d dumped core\n",
972
 
                      (unsigned int) (proc->pid));
 
980
              fprintf(stderr, "Plugin %s [%" PRIdMAX "] dumped"
 
981
                      " core\n", proc->name, (intmax_t) (proc->pid));
973
982
            }
974
983
          }
975
984
          
976
985
          /* Remove the plugin */
977
986
          FD_CLR(proc->fd, &rfds_all);
978
 
 
 
987
          
979
988
          /* Block signal while modifying process_list */
980
989
          ret = sigprocmask(SIG_BLOCK, &sigchld_action.sa_mask, NULL);
981
990
          if(ret < 0){
989
998
          proc = next_plugin;
990
999
          
991
1000
          /* We are done modifying process list, so unblock signal */
992
 
          ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask,
993
 
                             NULL);
 
1001
          ret = sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask,
 
1002
                            NULL);
994
1003
          if(ret < 0){
995
1004
            perror("sigprocmask");
996
1005
            exitstatus = EXIT_FAILURE;
1025
1034
      if(proc->buffer_length + BUFFER_SIZE > proc->buffer_size){
1026
1035
        proc->buffer = realloc(proc->buffer, proc->buffer_size
1027
1036
                               + (size_t) BUFFER_SIZE);
1028
 
        if (proc->buffer == NULL){
 
1037
        if(proc->buffer == NULL){
1029
1038
          perror("malloc");
1030
1039
          exitstatus = EXIT_FAILURE;
1031
1040
          goto fallback;
1048
1057
      }
1049
1058
    }
1050
1059
  }
1051
 
 
1052
 
 
 
1060
  
 
1061
  
1053
1062
 fallback:
1054
1063
  
1055
1064
  if(plugin_list == NULL or exitstatus != EXIT_SUCCESS){