/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-10-01 15:29:01 UTC
  • Revision ID: teddy@fukt.bsnet.se-20081001152901-fh30whl3m4vb7m24
* debian/changelog: New Debian revision.

* debian/mandos-client.lintian-overrides: Added comments.
* debian/mandos.lintian-overrides: - '' -

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,2009 Teddy Hogeborn
6
 
 * Copyright © 2008,2009 Björn Påhlsson
 
5
 * Copyright © 2008 Teddy Hogeborn & Björn Påhlsson
7
6
 * 
8
7
 * This program is free software: you can redistribute it and/or
9
8
 * modify it under the terms of the GNU General Public License as
64
63
                                   sigprocmask(), SIG_BLOCK, SIGCHLD,
65
64
                                   SIG_UNBLOCK, kill() */
66
65
#include <errno.h>              /* errno, EBADF */
67
 
#include <inttypes.h>           /* intmax_t, SCNdMAX, PRIdMAX,  */
68
66
 
69
67
#define BUFFER_SIZE 256
70
68
 
89
87
  size_t buffer_size;
90
88
  size_t buffer_length;
91
89
  bool eof;
92
 
  volatile sig_atomic_t completed;
93
 
  int status;
 
90
  volatile bool completed;
 
91
  volatile int status;
94
92
  struct plugin *next;
95
93
} plugin;
96
94
 
100
98
   or if none is found, creates a new one */
101
99
static plugin *getplugin(char *name){
102
100
  /* Check for exiting plugin with that name */
103
 
  for(plugin *p = plugin_list; p != NULL; p = p->next){
104
 
    if((p->name == name)
105
 
       or (p->name and name and (strcmp(p->name, name) == 0))){
 
101
  for (plugin *p = plugin_list; p != NULL; p = p->next){
 
102
    if ((p->name == name)
 
103
        or (p->name and name and (strcmp(p->name, name) == 0))){
106
104
      return p;
107
105
    }
108
106
  }
109
107
  /* Create a new plugin */
110
108
  plugin *new_plugin = malloc(sizeof(plugin));
111
 
  if(new_plugin == NULL){
 
109
  if (new_plugin == NULL){
112
110
    return NULL;
113
111
  }
114
112
  char *copy_name = NULL;
115
113
  if(name != NULL){
116
114
    copy_name = strdup(name);
117
115
    if(copy_name == NULL){
118
 
      free(new_plugin);
119
116
      return NULL;
120
117
    }
121
118
  }
122
119
  
123
 
  *new_plugin = (plugin){ .name = copy_name,
124
 
                          .argc = 1,
125
 
                          .disabled = false,
126
 
                          .next = plugin_list };
 
120
  *new_plugin = (plugin) { .name = copy_name,
 
121
                           .argc = 1,
 
122
                           .disabled = false,
 
123
                           .next = plugin_list };
127
124
  
128
125
  new_plugin->argv = malloc(sizeof(char *) * 2);
129
 
  if(new_plugin->argv == NULL){
 
126
  if (new_plugin->argv == NULL){
130
127
    free(copy_name);
131
128
    free(new_plugin);
132
129
    return NULL;
223
220
/* Mark processes as completed when they exit, and save their exit
224
221
   status. */
225
222
static void handle_sigchld(__attribute__((unused)) int sig){
226
 
  int old_errno = errno;
227
223
  while(true){
228
224
    plugin *proc = plugin_list;
229
225
    int status;
233
229
      break;
234
230
    }
235
231
    if(pid == -1){
236
 
      if(errno == ECHILD){
237
 
        /* No child processes */
238
 
        break;
 
232
      if (errno != ECHILD){
 
233
        perror("waitpid");
239
234
      }
240
 
      perror("waitpid");
 
235
      /* No child processes */
 
236
      break;
241
237
    }
242
238
    
243
239
    /* A child exited, find it in process_list */
249
245
      continue;
250
246
    }
251
247
    proc->status = status;
252
 
    proc->completed = 1;
 
248
    proc->completed = true;
253
249
  }
254
 
  errno = old_errno;
255
250
}
256
251
 
257
252
/* Prints out a password to stdout */
312
307
  struct dirent *dirst;
313
308
  struct stat st;
314
309
  fd_set rfds_all;
315
 
  int ret, numchars, maxfd = 0;
316
 
  ssize_t sret;
317
 
  intmax_t tmpmax;
 
310
  int ret, maxfd = 0;
318
311
  uid_t uid = 65534;
319
312
  gid_t gid = 65534;
320
313
  bool debug = false;
377
370
    { .name = NULL }
378
371
  };
379
372
  
380
 
  error_t parse_opt(int key, char *arg, __attribute__((unused))
381
 
                    struct argp_state *state){
382
 
    switch(key){
 
373
  error_t parse_opt (int key, char *arg, __attribute__((unused))
 
374
                     struct argp_state *state) {
 
375
    switch (key) {
383
376
    case 'g':                   /* --global-options */
384
 
      if(arg != NULL){
 
377
      if (arg != NULL){
385
378
        char *p;
386
379
        while((p = strsep(&arg, ",")) != NULL){
387
380
          if(p[0] == '\0'){
403
396
      }
404
397
      break;
405
398
    case 'o':                   /* --options-for */
406
 
      if(arg != NULL){
 
399
      if (arg != NULL){
407
400
        char *p_name = strsep(&arg, ":");
408
401
        if(p_name[0] == '\0' or arg == NULL){
409
402
          break;
440
433
      }
441
434
      break;
442
435
    case 'd':                   /* --disable */
443
 
      if(arg != NULL){
 
436
      if (arg != NULL){
444
437
        plugin *p = getplugin(arg);
445
438
        if(p == NULL){
446
439
          return ARGP_ERR_UNKNOWN;
449
442
      }
450
443
      break;
451
444
    case 'e':                   /* --enable */
452
 
      if(arg != NULL){
 
445
      if (arg != NULL){
453
446
        plugin *p = getplugin(arg);
454
447
        if(p == NULL){
455
448
          return ARGP_ERR_UNKNOWN;
468
461
      /* This is already done by parse_opt_config_file() */
469
462
      break;
470
463
    case 130:                   /* --userid */
471
 
      ret = sscanf(arg, "%" SCNdMAX "%n", &tmpmax, &numchars);
472
 
      if(ret < 1 or tmpmax != (uid_t)tmpmax
473
 
         or arg[numchars] != '\0'){
474
 
        fprintf(stderr, "Bad user ID number: \"%s\", using %"
475
 
                PRIdMAX "\n", arg, (intmax_t)uid);
476
 
      } else {
477
 
        uid = (uid_t)tmpmax;
478
 
      }
 
464
      uid = (uid_t)strtol(arg, NULL, 10);
479
465
      break;
480
466
    case 131:                   /* --groupid */
481
 
      ret = sscanf(arg, "%" SCNdMAX "%n", &tmpmax, &numchars);
482
 
      if(ret < 1 or tmpmax != (gid_t)tmpmax
483
 
         or arg[numchars] != '\0'){
484
 
        fprintf(stderr, "Bad group ID number: \"%s\", using %"
485
 
                PRIdMAX "\n", arg, (intmax_t)gid);
486
 
      } else {
487
 
        gid = (gid_t)tmpmax;
488
 
      }
 
467
      gid = (gid_t)strtol(arg, NULL, 10);
489
468
      break;
490
469
    case 132:                   /* --debug */
491
470
      debug = true;
492
471
      break;
493
 
/*
494
 
 * When adding more options before this line, remember to also add a
495
 
 * "case" to the "parse_opt_config_file" function below.
496
 
 */
497
472
    case ARGP_KEY_ARG:
498
473
      /* Cryptsetup always passes an argument, which is an empty
499
474
         string if "none" was specified in /etc/crypttab.  So if
512
487
  
513
488
  /* This option parser is the same as parse_opt() above, except it
514
489
     ignores everything but the --config-file option. */
515
 
  error_t parse_opt_config_file(int key, char *arg,
516
 
                                __attribute__((unused))
517
 
                                struct argp_state *state){
518
 
    switch(key){
 
490
  error_t parse_opt_config_file (int key, char *arg,
 
491
                                 __attribute__((unused))
 
492
                                 struct argp_state *state) {
 
493
    switch (key) {
519
494
    case 'g':                   /* --global-options */
520
495
    case 'G':                   /* --global-env */
521
496
    case 'o':                   /* --options-for */
548
523
                       .args_doc = "",
549
524
                       .doc = "Mandos plugin runner -- Run plugins" };
550
525
  
551
 
  /* Parse using parse_opt_config_file() in order to get the custom
 
526
  /* Parse using the parse_opt_config_file in order to get the custom
552
527
     config file location, if any. */
553
 
  ret = argp_parse(&argp, argc, argv, ARGP_IN_ORDER, 0, NULL);
554
 
  if(ret == ARGP_ERR_UNKNOWN){
 
528
  ret = argp_parse (&argp, argc, argv, ARGP_IN_ORDER, 0, NULL);
 
529
  if (ret == ARGP_ERR_UNKNOWN){
555
530
    fprintf(stderr, "Unknown error while parsing arguments\n");
556
531
    exitstatus = EXIT_FAILURE;
557
532
    goto fallback;
561
536
  argp.parser = parse_opt;
562
537
  
563
538
  /* Open the configfile if available */
564
 
  if(argfile == NULL){
 
539
  if (argfile == NULL){
565
540
    conffp = fopen(AFILE, "r");
566
541
  } else {
567
542
    conffp = fopen(argfile, "r");
570
545
    char *org_line = NULL;
571
546
    char *p, *arg, *new_arg, *line;
572
547
    size_t size = 0;
 
548
    ssize_t sret;
573
549
    const char whitespace_delims[] = " \r\t\f\v\n";
574
550
    const char comment_delim[] = "#";
575
551
 
622
598
  } else {
623
599
    /* Check for harmful errors and go to fallback. Other errors might
624
600
       not affect opening plugins */
625
 
    if(errno == EMFILE or errno == ENFILE or errno == ENOMEM){
 
601
    if (errno == EMFILE or errno == ENFILE or errno == ENOMEM){
626
602
      perror("fopen");
627
603
      exitstatus = EXIT_FAILURE;
628
604
      goto fallback;
631
607
  /* If there was any arguments from configuration file,
632
608
     pass them to parser as command arguments */
633
609
  if(custom_argv != NULL){
634
 
    ret = argp_parse(&argp, custom_argc, custom_argv, ARGP_IN_ORDER,
635
 
                     0, NULL);
636
 
    if(ret == ARGP_ERR_UNKNOWN){
 
610
    ret = argp_parse (&argp, custom_argc, custom_argv, ARGP_IN_ORDER,
 
611
                      0, NULL);
 
612
    if (ret == ARGP_ERR_UNKNOWN){
637
613
      fprintf(stderr, "Unknown error while parsing arguments\n");
638
614
      exitstatus = EXIT_FAILURE;
639
615
      goto fallback;
642
618
  
643
619
  /* Parse actual command line arguments, to let them override the
644
620
     config file */
645
 
  ret = argp_parse(&argp, argc, argv, ARGP_IN_ORDER, 0, NULL);
646
 
  if(ret == ARGP_ERR_UNKNOWN){
 
621
  ret = argp_parse (&argp, argc, argv, ARGP_IN_ORDER, 0, NULL);
 
622
  if (ret == ARGP_ERR_UNKNOWN){
647
623
    fprintf(stderr, "Unknown error while parsing arguments\n");
648
624
    exitstatus = EXIT_FAILURE;
649
625
    goto fallback;
656
632
      for(char **a = p->argv; *a != NULL; a++){
657
633
        fprintf(stderr, "\tArg: %s\n", *a);
658
634
      }
659
 
      fprintf(stderr, "...and %d environment variables\n", p->envc);
 
635
      fprintf(stderr, "...and %u environment variables\n", p->envc);
660
636
      for(char **a = p->environ; *a != NULL; a++){
661
637
        fprintf(stderr, "\t%s\n", *a);
662
638
      }
664
640
  }
665
641
  
666
642
  /* Strip permissions down to nobody */
 
643
  ret = setuid(uid);
 
644
  if (ret == -1){
 
645
    perror("setuid");
 
646
  }  
667
647
  setgid(gid);
668
 
  if(ret == -1){
 
648
  if (ret == -1){
669
649
    perror("setgid");
670
650
  }
671
 
  ret = setuid(uid);
672
 
  if(ret == -1){
673
 
    perror("setuid");
674
 
  }
675
651
  
676
 
  if(plugindir == NULL){
 
652
  if (plugindir == NULL){
677
653
    dir = opendir(PDIR);
678
654
  } else {
679
655
    dir = opendir(plugindir);
706
682
    
707
683
    /* All directory entries have been processed */
708
684
    if(dirst == NULL){
709
 
      if(errno == EBADF){
 
685
      if (errno == EBADF){
710
686
        perror("readdir");
711
687
        exitstatus = EXIT_FAILURE;
712
688
        goto fallback;
724
700
      
725
701
      const char const *bad_suffixes[] = { "~", "#", ".dpkg-new",
726
702
                                           ".dpkg-old",
727
 
                                           ".dpkg-bak",
728
703
                                           ".dpkg-divert", NULL };
729
704
      for(const char **pre = bad_prefixes; *pre != NULL; pre++){
730
705
        size_t pre_len = strlen(*pre);
772
747
    }
773
748
    
774
749
    ret = stat(filename, &st);
775
 
    if(ret == -1){
 
750
    if (ret == -1){
776
751
      perror("stat");
777
752
      free(filename);
778
753
      continue;
779
754
    }
780
755
 
781
756
    /* Ignore non-executable files */
782
 
    if(not S_ISREG(st.st_mode) or (access(filename, X_OK) != 0)){
 
757
    if (not S_ISREG(st.st_mode) or (access(filename, X_OK) != 0)){
783
758
      if(debug){
784
759
        fprintf(stderr, "Ignoring plugin dir entry \"%s\""
785
760
                " with bad type or mode\n", filename);
832
807
    
833
808
    int pipefd[2];
834
809
    ret = pipe(pipefd);
835
 
    if(ret == -1){
 
810
    if (ret == -1){
836
811
      perror("pipe");
837
812
      exitstatus = EXIT_FAILURE;
838
813
      goto fallback;
851
826
      goto fallback;
852
827
    }
853
828
    /* Block SIGCHLD until process is safely in process list */
854
 
    ret = sigprocmask(SIG_BLOCK, &sigchld_action.sa_mask, NULL);
 
829
    ret = sigprocmask (SIG_BLOCK, &sigchld_action.sa_mask, NULL);
855
830
    if(ret < 0){
856
831
      perror("sigprocmask");
857
832
      exitstatus = EXIT_FAILURE;
905
880
    close(pipefd[1]);           /* Close unused write end of pipe */
906
881
    free(filename);
907
882
    plugin *new_plugin = getplugin(dirst->d_name);
908
 
    if(new_plugin == NULL){
 
883
    if (new_plugin == NULL){
909
884
      perror("getplugin");
910
 
      ret = sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
 
885
      ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
911
886
      if(ret < 0){
912
887
        perror("sigprocmask");
913
888
      }
920
895
    
921
896
    /* Unblock SIGCHLD so signal handler can be run if this process
922
897
       has already completed */
923
 
    ret = sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
 
898
    ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
924
899
    if(ret < 0){
925
900
      perror("sigprocmask");
926
901
      exitstatus = EXIT_FAILURE;
929
904
    
930
905
    FD_SET(new_plugin->fd, &rfds_all);
931
906
    
932
 
    if(maxfd < new_plugin->fd){
 
907
    if (maxfd < new_plugin->fd){
933
908
      maxfd = new_plugin->fd;
934
909
    }
935
910
  }
952
927
  while(plugin_list){
953
928
    fd_set rfds = rfds_all;
954
929
    int select_ret = select(maxfd+1, &rfds, NULL, NULL, NULL);
955
 
    if(select_ret == -1){
 
930
    if (select_ret == -1){
956
931
      perror("select");
957
932
      exitstatus = EXIT_FAILURE;
958
933
      goto fallback;
961
936
       from one of them */
962
937
    for(plugin *proc = plugin_list; proc != NULL;){
963
938
      /* Is this process completely done? */
964
 
      if(proc->completed and proc->eof){
 
939
      if(proc->eof and proc->completed){
965
940
        /* Only accept the plugin output if it exited cleanly */
966
941
        if(not WIFEXITED(proc->status)
967
942
           or WEXITSTATUS(proc->status) != 0){
969
944
 
970
945
          if(debug){
971
946
            if(WIFEXITED(proc->status)){
972
 
              fprintf(stderr, "Plugin %s [%" PRIdMAX "] exited with"
973
 
                      " status %d\n", proc->name,
974
 
                      (intmax_t) (proc->pid),
 
947
              fprintf(stderr, "Plugin %u exited with status %d\n",
 
948
                      (unsigned int) (proc->pid),
975
949
                      WEXITSTATUS(proc->status));
976
 
            } else if(WIFSIGNALED(proc->status)){
977
 
              fprintf(stderr, "Plugin %s [%" PRIdMAX "] killed by"
978
 
                      " signal %d\n", proc->name,
979
 
                      (intmax_t) (proc->pid),
 
950
            } else if(WIFSIGNALED(proc->status)) {
 
951
              fprintf(stderr, "Plugin %u killed by signal %d\n",
 
952
                      (unsigned int) (proc->pid),
980
953
                      WTERMSIG(proc->status));
981
954
            } else if(WCOREDUMP(proc->status)){
982
 
              fprintf(stderr, "Plugin %s [%" PRIdMAX "] dumped"
983
 
                      " core\n", proc->name, (intmax_t) (proc->pid));
 
955
              fprintf(stderr, "Plugin %d dumped core\n",
 
956
                      (unsigned int) (proc->pid));
984
957
            }
985
958
          }
986
959
          
1000
973
          proc = next_plugin;
1001
974
          
1002
975
          /* We are done modifying process list, so unblock signal */
1003
 
          ret = sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask,
1004
 
                            NULL);
 
976
          ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask,
 
977
                             NULL);
1005
978
          if(ret < 0){
1006
979
            perror("sigprocmask");
1007
980
            exitstatus = EXIT_FAILURE;
1036
1009
      if(proc->buffer_length + BUFFER_SIZE > proc->buffer_size){
1037
1010
        proc->buffer = realloc(proc->buffer, proc->buffer_size
1038
1011
                               + (size_t) BUFFER_SIZE);
1039
 
        if(proc->buffer == NULL){
 
1012
        if (proc->buffer == NULL){
1040
1013
          perror("malloc");
1041
1014
          exitstatus = EXIT_FAILURE;
1042
1015
          goto fallback;
1044
1017
        proc->buffer_size += BUFFER_SIZE;
1045
1018
      }
1046
1019
      /* Read from the process */
1047
 
      sret = read(proc->fd, proc->buffer + proc->buffer_length,
1048
 
                  BUFFER_SIZE);
1049
 
      if(sret < 0){
 
1020
      ret = read(proc->fd, proc->buffer + proc->buffer_length,
 
1021
                 BUFFER_SIZE);
 
1022
      if(ret < 0){
1050
1023
        /* Read error from this process; ignore the error */
1051
1024
        proc = proc->next;
1052
1025
        continue;
1053
1026
      }
1054
 
      if(sret == 0){
 
1027
      if(ret == 0){
1055
1028
        /* got EOF */
1056
1029
        proc->eof = true;
1057
1030
      } else {
1058
 
        proc->buffer_length += (size_t) sret;
 
1031
        proc->buffer_length += (size_t) ret;
1059
1032
      }
1060
1033
    }
1061
1034
  }