/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-09-21 12:04:02 UTC
  • Revision ID: teddy@fukt.bsnet.se-20080921120402-mgd2jl8xo634jw18
* Makefile: Put the init script before avahi-daemon.

* debian/mandos.prerm: Bug fix: stop mandos, not ssh.

* debian/rules (install-indep): Put the init script before
                                avahi-daemon.

* init.d/mandos: Require "avahi-daemon".

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 © 2007-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
28
27
#include <stdlib.h>             /* malloc(), exit(), EXIT_FAILURE,
29
28
                                   EXIT_SUCCESS, realloc() */
30
29
#include <stdbool.h>            /* bool, true, false */
31
 
#include <stdio.h>              /* perror, fileno(), fprintf(),
32
 
                                   stderr, STDOUT_FILENO */
 
30
#include <stdio.h>              /* perror, popen(), fileno(),
 
31
                                   fprintf(), stderr, STDOUT_FILENO */
33
32
#include <sys/types.h>          /* DIR, opendir(), stat(), struct
34
33
                                   stat, waitpid(), WIFEXITED(),
35
34
                                   WEXITSTATUS(), wait(), pid_t,
47
46
                                   fcntl(), setuid(), setgid(),
48
47
                                   F_GETFD, F_SETFD, FD_CLOEXEC,
49
48
                                   access(), pipe(), fork(), close()
50
 
                                   dup2(), STDOUT_FILENO, _exit(),
 
49
                                   dup2, STDOUT_FILENO, _exit(),
51
50
                                   execv(), write(), read(),
52
51
                                   close() */
53
52
#include <fcntl.h>              /* fcntl(), F_GETFD, F_SETFD,
70
69
#define PDIR "/lib/mandos/plugins.d"
71
70
#define AFILE "/conf/conf.d/mandos/plugin-runner.conf"
72
71
 
73
 
const char *argp_program_version = "plugin-runner " VERSION;
 
72
const char *argp_program_version = "plugin-runner 1.0";
74
73
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
75
74
 
76
75
typedef struct plugin{
309
308
  struct stat st;
310
309
  fd_set rfds_all;
311
310
  int ret, maxfd = 0;
312
 
  ssize_t sret;
313
311
  uid_t uid = 65534;
314
312
  gid_t gid = 65534;
315
313
  bool debug = false;
463
461
      /* This is already done by parse_opt_config_file() */
464
462
      break;
465
463
    case 130:                   /* --userid */
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
 
      }
 
464
      uid = (uid_t)strtol(arg, NULL, 10);
472
465
      break;
473
466
    case 131:                   /* --groupid */
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
 
      }
 
467
      gid = (gid_t)strtol(arg, NULL, 10);
480
468
      break;
481
469
    case 132:                   /* --debug */
482
470
      debug = true;
483
471
      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
 
 */
488
472
    case ARGP_KEY_ARG:
489
473
      /* Cryptsetup always passes an argument, which is an empty
490
474
         string if "none" was specified in /etc/crypttab.  So if
539
523
                       .args_doc = "",
540
524
                       .doc = "Mandos plugin runner -- Run plugins" };
541
525
  
542
 
  /* 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
543
527
     config file location, if any. */
544
528
  ret = argp_parse (&argp, argc, argv, ARGP_IN_ORDER, 0, NULL);
545
529
  if (ret == ARGP_ERR_UNKNOWN){
561
545
    char *org_line = NULL;
562
546
    char *p, *arg, *new_arg, *line;
563
547
    size_t size = 0;
 
548
    ssize_t sret;
564
549
    const char whitespace_delims[] = " \r\t\f\v\n";
565
550
    const char comment_delim[] = "#";
566
551
 
715
700
      
716
701
      const char const *bad_suffixes[] = { "~", "#", ".dpkg-new",
717
702
                                           ".dpkg-old",
718
 
                                           ".dpkg-bak",
719
703
                                           ".dpkg-divert", NULL };
720
704
      for(const char **pre = bad_prefixes; *pre != NULL; pre++){
721
705
        size_t pre_len = strlen(*pre);
862
846
        perror("sigaction");
863
847
        _exit(EXIT_FAILURE);
864
848
      }
865
 
      ret = sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
 
849
      ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
866
850
      if(ret < 0){
867
851
        perror("sigprocmask");
868
852
        _exit(EXIT_FAILURE);
869
853
      }
870
 
      
 
854
 
871
855
      ret = dup2(pipefd[1], STDOUT_FILENO); /* replace our stdout */
872
856
      if(ret == -1){
873
857
        perror("dup2");
923
907
    if (maxfd < new_plugin->fd){
924
908
      maxfd = new_plugin->fd;
925
909
    }
 
910
    
926
911
  }
927
912
  
928
913
  closedir(dir);
929
914
  dir = NULL;
930
 
  
 
915
 
931
916
  for(plugin *p = plugin_list; p != NULL; p = p->next){
932
917
    if(p->pid != 0){
933
918
      break;
938
923
      free_plugin_list();
939
924
    }
940
925
  }
941
 
  
 
926
 
942
927
  /* Main loop while running plugins exist */
943
928
  while(plugin_list){
944
929
    fd_set rfds = rfds_all;
968
953
                      (unsigned int) (proc->pid),
969
954
                      WTERMSIG(proc->status));
970
955
            } else if(WCOREDUMP(proc->status)){
971
 
              fprintf(stderr, "Plugin %u dumped core\n",
 
956
              fprintf(stderr, "Plugin %d dumped core\n",
972
957
                      (unsigned int) (proc->pid));
973
958
            }
974
959
          }
984
969
            goto fallback;
985
970
          }
986
971
          
987
 
          plugin *next_plugin = proc->next;
988
 
          free_plugin(proc);
989
 
          proc = next_plugin;
990
 
          
991
972
          /* We are done modifying process list, so unblock signal */
992
973
          ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask,
993
974
                             NULL);
1001
982
            break;
1002
983
          }
1003
984
          
 
985
          plugin *next_plugin = proc->next;
 
986
          free_plugin(proc);
 
987
          proc = next_plugin;
1004
988
          continue;
1005
989
        }
1006
990
        
1007
991
        /* This process exited nicely, so print its buffer */
1008
 
        
 
992
 
1009
993
        bool bret = print_out_password(proc->buffer,
1010
994
                                       proc->buffer_length);
1011
995
        if(not bret){
1033
1017
        proc->buffer_size += BUFFER_SIZE;
1034
1018
      }
1035
1019
      /* Read from the process */
1036
 
      sret = read(proc->fd, proc->buffer + proc->buffer_length,
1037
 
                  BUFFER_SIZE);
1038
 
      if(sret < 0){
 
1020
      ret = read(proc->fd, proc->buffer + proc->buffer_length,
 
1021
                 BUFFER_SIZE);
 
1022
      if(ret < 0){
1039
1023
        /* Read error from this process; ignore the error */
1040
1024
        proc = proc->next;
1041
1025
        continue;
1042
1026
      }
1043
 
      if(sret == 0){
 
1027
      if(ret == 0){
1044
1028
        /* got EOF */
1045
1029
        proc->eof = true;
1046
1030
      } else {
1047
 
        proc->buffer_length += (size_t) sret;
 
1031
        proc->buffer_length += (size_t) ret;
1048
1032
      }
1049
1033
    }
1050
1034
  }
1077
1061
    perror("sigaction");
1078
1062
    exitstatus = EXIT_FAILURE;
1079
1063
  }
1080
 
  
 
1064
 
1081
1065
  if(custom_argv != NULL){
1082
1066
    for(char **arg = custom_argv+1; *arg != NULL; arg++){
1083
1067
      free(*arg);
1089
1073
    closedir(dir);
1090
1074
  }
1091
1075
  
1092
 
  /* Kill the processes */
 
1076
  /* Free the process list and kill the processes */
1093
1077
  for(plugin *p = plugin_list; p != NULL; p = p->next){
1094
1078
    if(p->pid != 0){
1095
1079
      close(p->fd);
1108
1092
  if(errno != ECHILD){
1109
1093
    perror("wait");
1110
1094
  }
1111
 
  
 
1095
 
1112
1096
  free_plugin_list();
1113
1097
  
1114
1098
  free(plugindir);