/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-13 04:35:19 UTC
  • Revision ID: teddy@fukt.bsnet.se-20090113043519-0yr39snphoegq8l1
* plugin-runner.c: Only space changes.
* plugins.d/mandos-client.c: - '' -
* plugins.d/password-prompt.c: - '' -

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 © 2008,2009 Teddy Hogeborn
 
6
 * Copyright © 2008,2009 Björn Påhlsson
6
7
 * 
7
8
 * This program is free software: you can redistribute it and/or
8
9
 * modify it under the terms of the GNU General Public License as
69
70
#define PDIR "/lib/mandos/plugins.d"
70
71
#define AFILE "/conf/conf.d/mandos/plugin-runner.conf"
71
72
 
72
 
const char *argp_program_version = "plugin-runner 1.0";
 
73
const char *argp_program_version = "plugin-runner " VERSION;
73
74
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
74
75
 
75
76
typedef struct plugin{
98
99
   or if none is found, creates a new one */
99
100
static plugin *getplugin(char *name){
100
101
  /* Check for exiting plugin with that name */
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))){
 
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
105
      return p;
105
106
    }
106
107
  }
107
108
  /* Create a new plugin */
108
109
  plugin *new_plugin = malloc(sizeof(plugin));
109
 
  if (new_plugin == NULL){
 
110
  if(new_plugin == NULL){
110
111
    return NULL;
111
112
  }
112
113
  char *copy_name = NULL;
123
124
                           .next = plugin_list };
124
125
  
125
126
  new_plugin->argv = malloc(sizeof(char *) * 2);
126
 
  if (new_plugin->argv == NULL){
 
127
  if(new_plugin->argv == NULL){
127
128
    free(copy_name);
128
129
    free(new_plugin);
129
130
    return NULL;
229
230
      break;
230
231
    }
231
232
    if(pid == -1){
232
 
      if (errno != ECHILD){
 
233
      if(errno != ECHILD){
233
234
        perror("waitpid");
234
235
      }
235
236
      /* No child processes */
308
309
  struct stat st;
309
310
  fd_set rfds_all;
310
311
  int ret, maxfd = 0;
 
312
  ssize_t sret;
311
313
  uid_t uid = 65534;
312
314
  gid_t gid = 65534;
313
315
  bool debug = false;
370
372
    { .name = NULL }
371
373
  };
372
374
  
373
 
  error_t parse_opt (int key, char *arg, __attribute__((unused))
374
 
                     struct argp_state *state) {
375
 
    switch (key) {
 
375
  error_t parse_opt(int key, char *arg, __attribute__((unused))
 
376
                    struct argp_state *state) {
 
377
    switch(key) {
376
378
    case 'g':                   /* --global-options */
377
 
      if (arg != NULL){
 
379
      if(arg != NULL){
378
380
        char *p;
379
381
        while((p = strsep(&arg, ",")) != NULL){
380
382
          if(p[0] == '\0'){
396
398
      }
397
399
      break;
398
400
    case 'o':                   /* --options-for */
399
 
      if (arg != NULL){
 
401
      if(arg != NULL){
400
402
        char *p_name = strsep(&arg, ":");
401
403
        if(p_name[0] == '\0' or arg == NULL){
402
404
          break;
433
435
      }
434
436
      break;
435
437
    case 'd':                   /* --disable */
436
 
      if (arg != NULL){
 
438
      if(arg != NULL){
437
439
        plugin *p = getplugin(arg);
438
440
        if(p == NULL){
439
441
          return ARGP_ERR_UNKNOWN;
442
444
      }
443
445
      break;
444
446
    case 'e':                   /* --enable */
445
 
      if (arg != NULL){
 
447
      if(arg != NULL){
446
448
        plugin *p = getplugin(arg);
447
449
        if(p == NULL){
448
450
          return ARGP_ERR_UNKNOWN;
461
463
      /* This is already done by parse_opt_config_file() */
462
464
      break;
463
465
    case 130:                   /* --userid */
464
 
      uid = (uid_t)strtol(arg, NULL, 10);
 
466
      /* In the GNU C library, uid_t is always unsigned int */
 
467
      ret = sscanf(arg, "%u", &uid);
 
468
      if(ret != 1){
 
469
        fprintf(stderr, "Bad user ID number: \"%s\", using %u\n", arg,
 
470
                uid);
 
471
      }
465
472
      break;
466
473
    case 131:                   /* --groupid */
467
 
      gid = (gid_t)strtol(arg, NULL, 10);
 
474
      /* In the GNU C library, gid_t is always unsigned int */
 
475
      ret = sscanf(arg, "%u", &gid);
 
476
      if(ret != 1){
 
477
        fprintf(stderr, "Bad group ID number: \"%s\", using %u\n",
 
478
                arg, gid);
 
479
      }
468
480
      break;
469
481
    case 132:                   /* --debug */
470
482
      debug = true;
471
483
      break;
 
484
/*
 
485
 * When adding more options before this line, remember to also add a
 
486
 * "case" to the "parse_opt_config_file" function below.
 
487
 */
472
488
    case ARGP_KEY_ARG:
473
489
      /* Cryptsetup always passes an argument, which is an empty
474
490
         string if "none" was specified in /etc/crypttab.  So if
487
503
  
488
504
  /* This option parser is the same as parse_opt() above, except it
489
505
     ignores everything but the --config-file option. */
490
 
  error_t parse_opt_config_file (int key, char *arg,
491
 
                                 __attribute__((unused))
492
 
                                 struct argp_state *state) {
493
 
    switch (key) {
 
506
  error_t parse_opt_config_file(int key, char *arg,
 
507
                                __attribute__((unused))
 
508
                                struct argp_state *state) {
 
509
    switch(key) {
494
510
    case 'g':                   /* --global-options */
495
511
    case 'G':                   /* --global-env */
496
512
    case 'o':                   /* --options-for */
523
539
                       .args_doc = "",
524
540
                       .doc = "Mandos plugin runner -- Run plugins" };
525
541
  
526
 
  /* Parse using the parse_opt_config_file in order to get the custom
 
542
  /* Parse using parse_opt_config_file() in order to get the custom
527
543
     config file location, if any. */
528
 
  ret = argp_parse (&argp, argc, argv, ARGP_IN_ORDER, 0, NULL);
529
 
  if (ret == ARGP_ERR_UNKNOWN){
 
544
  ret = argp_parse(&argp, argc, argv, ARGP_IN_ORDER, 0, NULL);
 
545
  if(ret == ARGP_ERR_UNKNOWN){
530
546
    fprintf(stderr, "Unknown error while parsing arguments\n");
531
547
    exitstatus = EXIT_FAILURE;
532
548
    goto fallback;
536
552
  argp.parser = parse_opt;
537
553
  
538
554
  /* Open the configfile if available */
539
 
  if (argfile == NULL){
 
555
  if(argfile == NULL){
540
556
    conffp = fopen(AFILE, "r");
541
557
  } else {
542
558
    conffp = fopen(argfile, "r");
545
561
    char *org_line = NULL;
546
562
    char *p, *arg, *new_arg, *line;
547
563
    size_t size = 0;
548
 
    ssize_t sret;
549
564
    const char whitespace_delims[] = " \r\t\f\v\n";
550
565
    const char comment_delim[] = "#";
551
566
 
598
613
  } else {
599
614
    /* Check for harmful errors and go to fallback. Other errors might
600
615
       not affect opening plugins */
601
 
    if (errno == EMFILE or errno == ENFILE or errno == ENOMEM){
 
616
    if(errno == EMFILE or errno == ENFILE or errno == ENOMEM){
602
617
      perror("fopen");
603
618
      exitstatus = EXIT_FAILURE;
604
619
      goto fallback;
607
622
  /* If there was any arguments from configuration file,
608
623
     pass them to parser as command arguments */
609
624
  if(custom_argv != NULL){
610
 
    ret = argp_parse (&argp, custom_argc, custom_argv, ARGP_IN_ORDER,
611
 
                      0, NULL);
612
 
    if (ret == ARGP_ERR_UNKNOWN){
 
625
    ret = argp_parse(&argp, custom_argc, custom_argv, ARGP_IN_ORDER,
 
626
                     0, NULL);
 
627
    if(ret == ARGP_ERR_UNKNOWN){
613
628
      fprintf(stderr, "Unknown error while parsing arguments\n");
614
629
      exitstatus = EXIT_FAILURE;
615
630
      goto fallback;
618
633
  
619
634
  /* Parse actual command line arguments, to let them override the
620
635
     config file */
621
 
  ret = argp_parse (&argp, argc, argv, ARGP_IN_ORDER, 0, NULL);
622
 
  if (ret == ARGP_ERR_UNKNOWN){
 
636
  ret = argp_parse(&argp, argc, argv, ARGP_IN_ORDER, 0, NULL);
 
637
  if(ret == ARGP_ERR_UNKNOWN){
623
638
    fprintf(stderr, "Unknown error while parsing arguments\n");
624
639
    exitstatus = EXIT_FAILURE;
625
640
    goto fallback;
641
656
  
642
657
  /* Strip permissions down to nobody */
643
658
  ret = setuid(uid);
644
 
  if (ret == -1){
 
659
  if(ret == -1){
645
660
    perror("setuid");
646
661
  }  
647
662
  setgid(gid);
648
 
  if (ret == -1){
 
663
  if(ret == -1){
649
664
    perror("setgid");
650
665
  }
651
666
  
652
 
  if (plugindir == NULL){
 
667
  if(plugindir == NULL){
653
668
    dir = opendir(PDIR);
654
669
  } else {
655
670
    dir = opendir(plugindir);
682
697
    
683
698
    /* All directory entries have been processed */
684
699
    if(dirst == NULL){
685
 
      if (errno == EBADF){
 
700
      if(errno == EBADF){
686
701
        perror("readdir");
687
702
        exitstatus = EXIT_FAILURE;
688
703
        goto fallback;
700
715
      
701
716
      const char const *bad_suffixes[] = { "~", "#", ".dpkg-new",
702
717
                                           ".dpkg-old",
 
718
                                           ".dpkg-bak",
703
719
                                           ".dpkg-divert", NULL };
704
720
      for(const char **pre = bad_prefixes; *pre != NULL; pre++){
705
721
        size_t pre_len = strlen(*pre);
747
763
    }
748
764
    
749
765
    ret = stat(filename, &st);
750
 
    if (ret == -1){
 
766
    if(ret == -1){
751
767
      perror("stat");
752
768
      free(filename);
753
769
      continue;
754
770
    }
755
771
 
756
772
    /* Ignore non-executable files */
757
 
    if (not S_ISREG(st.st_mode) or (access(filename, X_OK) != 0)){
 
773
    if(not S_ISREG(st.st_mode) or (access(filename, X_OK) != 0)){
758
774
      if(debug){
759
775
        fprintf(stderr, "Ignoring plugin dir entry \"%s\""
760
776
                " with bad type or mode\n", filename);
807
823
    
808
824
    int pipefd[2];
809
825
    ret = pipe(pipefd);
810
 
    if (ret == -1){
 
826
    if(ret == -1){
811
827
      perror("pipe");
812
828
      exitstatus = EXIT_FAILURE;
813
829
      goto fallback;
826
842
      goto fallback;
827
843
    }
828
844
    /* Block SIGCHLD until process is safely in process list */
829
 
    ret = sigprocmask (SIG_BLOCK, &sigchld_action.sa_mask, NULL);
 
845
    ret = sigprocmask(SIG_BLOCK, &sigchld_action.sa_mask, NULL);
830
846
    if(ret < 0){
831
847
      perror("sigprocmask");
832
848
      exitstatus = EXIT_FAILURE;
880
896
    close(pipefd[1]);           /* Close unused write end of pipe */
881
897
    free(filename);
882
898
    plugin *new_plugin = getplugin(dirst->d_name);
883
 
    if (new_plugin == NULL){
 
899
    if(new_plugin == NULL){
884
900
      perror("getplugin");
885
 
      ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
 
901
      ret = sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
886
902
      if(ret < 0){
887
903
        perror("sigprocmask");
888
904
      }
895
911
    
896
912
    /* Unblock SIGCHLD so signal handler can be run if this process
897
913
       has already completed */
898
 
    ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
 
914
    ret = sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
899
915
    if(ret < 0){
900
916
      perror("sigprocmask");
901
917
      exitstatus = EXIT_FAILURE;
904
920
    
905
921
    FD_SET(new_plugin->fd, &rfds_all);
906
922
    
907
 
    if (maxfd < new_plugin->fd){
 
923
    if(maxfd < new_plugin->fd){
908
924
      maxfd = new_plugin->fd;
909
925
    }
910
926
  }
927
943
  while(plugin_list){
928
944
    fd_set rfds = rfds_all;
929
945
    int select_ret = select(maxfd+1, &rfds, NULL, NULL, NULL);
930
 
    if (select_ret == -1){
 
946
    if(select_ret == -1){
931
947
      perror("select");
932
948
      exitstatus = EXIT_FAILURE;
933
949
      goto fallback;
952
968
                      (unsigned int) (proc->pid),
953
969
                      WTERMSIG(proc->status));
954
970
            } else if(WCOREDUMP(proc->status)){
955
 
              fprintf(stderr, "Plugin %d dumped core\n",
 
971
              fprintf(stderr, "Plugin %u dumped core\n",
956
972
                      (unsigned int) (proc->pid));
957
973
            }
958
974
          }
973
989
          proc = next_plugin;
974
990
          
975
991
          /* We are done modifying process list, so unblock signal */
976
 
          ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask,
977
 
                             NULL);
 
992
          ret = sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask,
 
993
                            NULL);
978
994
          if(ret < 0){
979
995
            perror("sigprocmask");
980
996
            exitstatus = EXIT_FAILURE;
1009
1025
      if(proc->buffer_length + BUFFER_SIZE > proc->buffer_size){
1010
1026
        proc->buffer = realloc(proc->buffer, proc->buffer_size
1011
1027
                               + (size_t) BUFFER_SIZE);
1012
 
        if (proc->buffer == NULL){
 
1028
        if(proc->buffer == NULL){
1013
1029
          perror("malloc");
1014
1030
          exitstatus = EXIT_FAILURE;
1015
1031
          goto fallback;
1017
1033
        proc->buffer_size += BUFFER_SIZE;
1018
1034
      }
1019
1035
      /* Read from the process */
1020
 
      ret = read(proc->fd, proc->buffer + proc->buffer_length,
1021
 
                 BUFFER_SIZE);
1022
 
      if(ret < 0){
 
1036
      sret = read(proc->fd, proc->buffer + proc->buffer_length,
 
1037
                  BUFFER_SIZE);
 
1038
      if(sret < 0){
1023
1039
        /* Read error from this process; ignore the error */
1024
1040
        proc = proc->next;
1025
1041
        continue;
1026
1042
      }
1027
 
      if(ret == 0){
 
1043
      if(sret == 0){
1028
1044
        /* got EOF */
1029
1045
        proc->eof = true;
1030
1046
      } else {
1031
 
        proc->buffer_length += (size_t) ret;
 
1047
        proc->buffer_length += (size_t) sret;
1032
1048
      }
1033
1049
    }
1034
1050
  }