/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-11-02 05:52:57 UTC
  • mfrom: (196.1.1 mandos)
  • Revision ID: teddy@fukt.bsnet.se-20091102055257-orjjddrfrtw0jc9b
* mandos (Client.enable): Bug fix: Start new immediate checker last to
                          avoid race condition.
  (Client.disable): Optionally do not log message.
  (ClientDBus.disable): Do not log message if no signal should be sent.
  (string_to_delta): More informative error message.
  (main.cleanup): Emit D-Bus "ClientRemoved" signal for all clients.
                  Do not emit any other signal.  Move to after
                  "mandos_dbus_service" is defined.

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-2013 Teddy Hogeborn
6
 
 * Copyright © 2008-2013 Björn Påhlsson
 
5
 * Copyright © 2008,2009 Teddy Hogeborn
 
6
 * Copyright © 2008,2009 Björn Påhlsson
7
7
 * 
8
8
 * This program is free software: you can redistribute it and/or
9
9
 * modify it under the terms of the GNU General Public License as
19
19
 * along with this program.  If not, see
20
20
 * <http://www.gnu.org/licenses/>.
21
21
 * 
22
 
 * Contact the authors at <mandos@recompile.se>.
 
22
 * Contact the authors at <mandos@fukt.bsnet.se>.
23
23
 */
24
24
 
25
25
#define _GNU_SOURCE             /* TEMP_FAILURE_RETRY(), getline(),
28
28
#include <stdlib.h>             /* malloc(), exit(), EXIT_SUCCESS,
29
29
                                   realloc() */
30
30
#include <stdbool.h>            /* bool, true, false */
31
 
#include <stdio.h>              /* fileno(), fprintf(),
 
31
#include <stdio.h>              /* perror, fileno(), fprintf(),
32
32
                                   stderr, STDOUT_FILENO */
33
33
#include <sys/types.h>          /* DIR, fdopendir(), stat(), struct
34
34
                                   stat, waitpid(), WIFEXITED(),
54
54
#include <fcntl.h>              /* fcntl(), F_GETFD, F_SETFD,
55
55
                                   FD_CLOEXEC */
56
56
#include <string.h>             /* strsep, strlen(), asprintf(),
57
 
                                   strsignal(), strcmp(), strncmp() */
 
57
                                   strsignal() */
58
58
#include <errno.h>              /* errno */
59
59
#include <argp.h>               /* struct argp_option, struct
60
60
                                   argp_state, struct argp,
70
70
#include <inttypes.h>           /* intmax_t, PRIdMAX, strtoimax() */
71
71
#include <sysexits.h>           /* EX_OSERR, EX_USAGE, EX_IOERR,
72
72
                                   EX_CONFIG, EX_UNAVAILABLE, EX_OK */
73
 
#include <errno.h>              /* errno */
74
 
#include <error.h>              /* error() */
75
73
 
76
74
#define BUFFER_SIZE 256
77
75
 
79
77
#define AFILE "/conf/conf.d/mandos/plugin-runner.conf"
80
78
 
81
79
const char *argp_program_version = "plugin-runner " VERSION;
82
 
const char *argp_program_bug_address = "<mandos@recompile.se>";
 
80
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
83
81
 
84
82
typedef struct plugin{
85
83
  char *name;                   /* can be NULL or any plugin name */
171
169
}
172
170
 
173
171
/* Helper function for add_argument and add_environment */
174
 
__attribute__((nonnull))
175
172
static bool add_to_char_array(const char *new, char ***array,
176
173
                              int *len){
177
174
  /* Resize the pointed-to array to hold one more pointer */
200
197
}
201
198
 
202
199
/* Add to a plugin's argument vector */
203
 
__attribute__((nonnull(2)))
204
200
static bool add_argument(plugin *p, const char *arg){
205
201
  if(p == NULL){
206
202
    return false;
209
205
}
210
206
 
211
207
/* Add to a plugin's environment */
212
 
__attribute__((nonnull(2)))
213
208
static bool add_environment(plugin *p, const char *def, bool replace){
214
209
  if(p == NULL){
215
210
    return false;
271
266
        /* No child processes */
272
267
        break;
273
268
      }
274
 
      error(0, errno, "waitpid");
 
269
      perror("waitpid");
275
270
    }
276
271
    
277
272
    /* A child exited, find it in process_list */
289
284
}
290
285
 
291
286
/* Prints out a password to stdout */
292
 
__attribute__((nonnull))
293
287
static bool print_out_password(const char *buffer, size_t length){
294
288
  ssize_t ret;
295
289
  for(size_t written = 0; written < length; written += (size_t)ret){
303
297
}
304
298
 
305
299
/* Removes and free a plugin from the plugin list */
306
 
__attribute__((nonnull))
307
300
static void free_plugin(plugin *plugin_node){
308
301
  
309
302
  for(char **arg = plugin_node->argv; *arg != NULL; arg++){
364
357
  sigemptyset(&sigchld_action.sa_mask);
365
358
  ret = sigaddset(&sigchld_action.sa_mask, SIGCHLD);
366
359
  if(ret == -1){
367
 
    error(0, errno, "sigaddset");
 
360
    perror("sigaddset");
368
361
    exitstatus = EX_OSERR;
369
362
    goto fallback;
370
363
  }
371
364
  ret = sigaction(SIGCHLD, &sigchld_action, &old_sigchld_action);
372
365
  if(ret == -1){
373
 
    error(0, errno, "sigaction");
 
366
    perror("sigaction");
374
367
    exitstatus = EX_OSERR;
375
368
    goto fallback;
376
369
  }
421
414
    { .name = NULL }
422
415
  };
423
416
  
424
 
  __attribute__((nonnull(3)))
425
417
  error_t parse_opt(int key, char *arg, struct argp_state *state){
426
418
    errno = 0;
427
419
    switch(key){
428
420
      char *tmp;
429
 
      intmax_t tmp_id;
 
421
      intmax_t tmpmax;
430
422
    case 'g':                   /* --global-options */
431
423
      {
432
424
        char *plugin_option;
505
497
      /* This is already done by parse_opt_config_file() */
506
498
      break;
507
499
    case 130:                   /* --userid */
508
 
      tmp_id = strtoimax(arg, &tmp, 10);
 
500
      tmpmax = strtoimax(arg, &tmp, 10);
509
501
      if(errno != 0 or tmp == arg or *tmp != '\0'
510
 
         or tmp_id != (uid_t)tmp_id){
 
502
         or tmpmax != (uid_t)tmpmax){
511
503
        argp_error(state, "Bad user ID number: \"%s\", using %"
512
504
                   PRIdMAX, arg, (intmax_t)uid);
513
505
        break;
514
506
      }
515
 
      uid = (uid_t)tmp_id;
 
507
      uid = (uid_t)tmpmax;
516
508
      break;
517
509
    case 131:                   /* --groupid */
518
 
      tmp_id = strtoimax(arg, &tmp, 10);
 
510
      tmpmax = strtoimax(arg, &tmp, 10);
519
511
      if(errno != 0 or tmp == arg or *tmp != '\0'
520
 
         or tmp_id != (gid_t)tmp_id){
 
512
         or tmpmax != (gid_t)tmpmax){
521
513
        argp_error(state, "Bad group ID number: \"%s\", using %"
522
514
                   PRIdMAX, arg, (intmax_t)gid);
523
515
        break;
524
516
      }
525
 
      gid = (gid_t)tmp_id;
 
517
      gid = (gid_t)tmpmax;
526
518
      break;
527
519
    case 132:                   /* --debug */
528
520
      debug = true;
607
599
  case ENOMEM:
608
600
  default:
609
601
    errno = ret;
610
 
    error(0, errno, "argp_parse");
 
602
    perror("argp_parse");
611
603
    exitstatus = EX_OSERR;
612
604
    goto fallback;
613
605
  case EINVAL:
634
626
    custom_argc = 1;
635
627
    custom_argv = malloc(sizeof(char*) * 2);
636
628
    if(custom_argv == NULL){
637
 
      error(0, errno, "malloc");
 
629
      perror("malloc");
638
630
      exitstatus = EX_OSERR;
639
631
      goto fallback;
640
632
    }
657
649
        }
658
650
        new_arg = strdup(p);
659
651
        if(new_arg == NULL){
660
 
          error(0, errno, "strdup");
 
652
          perror("strdup");
661
653
          exitstatus = EX_OSERR;
662
654
          free(org_line);
663
655
          goto fallback;
667
659
        custom_argv = realloc(custom_argv, sizeof(char *)
668
660
                              * ((unsigned int) custom_argc + 1));
669
661
        if(custom_argv == NULL){
670
 
          error(0, errno, "realloc");
 
662
          perror("realloc");
671
663
          exitstatus = EX_OSERR;
672
664
          free(org_line);
673
665
          goto fallback;
680
672
      ret = fclose(conffp);
681
673
    } while(ret == EOF and errno == EINTR);
682
674
    if(ret == EOF){
683
 
      error(0, errno, "fclose");
 
675
      perror("fclose");
684
676
      exitstatus = EX_IOERR;
685
677
      goto fallback;
686
678
    }
689
681
    /* Check for harmful errors and go to fallback. Other errors might
690
682
       not affect opening plugins */
691
683
    if(errno == EMFILE or errno == ENFILE or errno == ENOMEM){
692
 
      error(0, errno, "fopen");
693
 
      exitstatus = EX_OSERR;
 
684
      perror("fopen");
 
685
      exitstatus = EX_IOERR;
694
686
      goto fallback;
695
687
    }
696
688
  }
706
698
    case ENOMEM:
707
699
    default:
708
700
      errno = ret;
709
 
      error(0, errno, "argp_parse");
 
701
      perror("argp_parse");
710
702
      exitstatus = EX_OSERR;
711
703
      goto fallback;
712
704
    case EINVAL:
726
718
  case ENOMEM:
727
719
  default:
728
720
    errno = ret;
729
 
    error(0, errno, "argp_parse");
 
721
    perror("argp_parse");
730
722
    exitstatus = EX_OSERR;
731
723
    goto fallback;
732
724
  case EINVAL:
748
740
    }
749
741
  }
750
742
  
751
 
  if(getuid() == 0){
752
 
    /* Work around Debian bug #633582:
753
 
       <http://bugs.debian.org/633582> */
754
 
    int plugindir_fd = open(/* plugindir or */ PDIR, O_RDONLY);
755
 
    if(plugindir_fd == -1){
756
 
      error(0, errno, "open");
757
 
    } else {
758
 
      ret = (int)TEMP_FAILURE_RETRY(fstat(plugindir_fd, &st));
759
 
      if(ret == -1){
760
 
        error(0, errno, "fstat");
761
 
      } else {
762
 
        if(S_ISDIR(st.st_mode) and st.st_uid == 0 and st.st_gid == 0){
763
 
          ret = fchown(plugindir_fd, uid, gid);
764
 
          if(ret == -1){
765
 
            error(0, errno, "fchown");
766
 
          }
767
 
        }
768
 
      }
769
 
      TEMP_FAILURE_RETRY(close(plugindir_fd));
770
 
    }
771
 
  }
772
 
  
773
 
  /* Lower permissions */
774
 
  ret = setgid(gid);
 
743
  /* Strip permissions down to nobody */
 
744
  setgid(gid);
775
745
  if(ret == -1){
776
 
    error(0, errno, "setgid");
 
746
    perror("setgid");
777
747
  }
778
748
  ret = setuid(uid);
779
749
  if(ret == -1){
780
 
    error(0, errno, "setuid");
 
750
    perror("setuid");
781
751
  }
782
752
  
783
753
  /* Open plugin directory with close_on_exec flag */
801
771
                    );
802
772
    }
803
773
    if(dir_fd == -1){
804
 
      error(0, errno, "Could not open plugin dir");
 
774
      perror("Could not open plugin dir");
805
775
      exitstatus = EX_UNAVAILABLE;
806
776
      goto fallback;
807
777
    }
810
780
  /* Set the FD_CLOEXEC flag on the directory */
811
781
    ret = set_cloexec_flag(dir_fd);
812
782
    if(ret < 0){
813
 
      error(0, errno, "set_cloexec_flag");
 
783
      perror("set_cloexec_flag");
814
784
      TEMP_FAILURE_RETRY(close(dir_fd));
815
785
      exitstatus = EX_OSERR;
816
786
      goto fallback;
819
789
    
820
790
    dir = fdopendir(dir_fd);
821
791
    if(dir == NULL){
822
 
      error(0, errno, "Could not open plugin dir");
 
792
      perror("Could not open plugin dir");
823
793
      TEMP_FAILURE_RETRY(close(dir_fd));
824
794
      exitstatus = EX_OSERR;
825
795
      goto fallback;
837
807
    /* All directory entries have been processed */
838
808
    if(dirst == NULL){
839
809
      if(errno == EBADF){
840
 
        error(0, errno, "readdir");
 
810
        perror("readdir");
841
811
        exitstatus = EX_IOERR;
842
812
        goto fallback;
843
813
      }
874
844
      for(const char **suf = bad_suffixes; *suf != NULL; suf++){
875
845
        size_t suf_len = strlen(*suf);
876
846
        if((d_name_len >= suf_len)
877
 
           and (strcmp((dirst->d_name) + d_name_len-suf_len, *suf)
 
847
           and (strcmp((dirst->d_name)+d_name_len-suf_len, *suf)
878
848
                == 0)){
879
849
          if(debug){
880
850
            fprintf(stderr, "Ignoring plugin dir entry \"%s\""
900
870
                                             dirst->d_name));
901
871
    }
902
872
    if(ret < 0){
903
 
      error(0, errno, "asprintf");
 
873
      perror("asprintf");
904
874
      continue;
905
875
    }
906
876
    
907
877
    ret = (int)TEMP_FAILURE_RETRY(stat(filename, &st));
908
878
    if(ret == -1){
909
 
      error(0, errno, "stat");
 
879
      perror("stat");
910
880
      free(filename);
911
881
      continue;
912
882
    }
924
894
    
925
895
    plugin *p = getplugin(dirst->d_name);
926
896
    if(p == NULL){
927
 
      error(0, errno, "getplugin");
 
897
      perror("getplugin");
928
898
      free(filename);
929
899
      continue;
930
900
    }
942
912
      if(g != NULL){
943
913
        for(char **a = g->argv + 1; *a != NULL; a++){
944
914
          if(not add_argument(p, *a)){
945
 
            error(0, errno, "add_argument");
 
915
            perror("add_argument");
946
916
          }
947
917
        }
948
918
        /* Add global environment variables */
949
919
        for(char **e = g->environ; *e != NULL; e++){
950
920
          if(not add_environment(p, *e, false)){
951
 
            error(0, errno, "add_environment");
 
921
            perror("add_environment");
952
922
          }
953
923
        }
954
924
      }
959
929
    if(p->environ[0] != NULL){
960
930
      for(char **e = environ; *e != NULL; e++){
961
931
        if(not add_environment(p, *e, false)){
962
 
          error(0, errno, "add_environment");
 
932
          perror("add_environment");
963
933
        }
964
934
      }
965
935
    }
967
937
    int pipefd[2];
968
938
    ret = (int)TEMP_FAILURE_RETRY(pipe(pipefd));
969
939
    if(ret == -1){
970
 
      error(0, errno, "pipe");
 
940
      perror("pipe");
971
941
      exitstatus = EX_OSERR;
972
942
      goto fallback;
973
943
    }
974
944
    /* Ask OS to automatic close the pipe on exec */
975
945
    ret = set_cloexec_flag(pipefd[0]);
976
946
    if(ret < 0){
977
 
      error(0, errno, "set_cloexec_flag");
 
947
      perror("set_cloexec_flag");
978
948
      exitstatus = EX_OSERR;
979
949
      goto fallback;
980
950
    }
981
951
    ret = set_cloexec_flag(pipefd[1]);
982
952
    if(ret < 0){
983
 
      error(0, errno, "set_cloexec_flag");
 
953
      perror("set_cloexec_flag");
984
954
      exitstatus = EX_OSERR;
985
955
      goto fallback;
986
956
    }
989
959
                                              &sigchld_action.sa_mask,
990
960
                                              NULL));
991
961
    if(ret < 0){
992
 
      error(0, errno, "sigprocmask");
 
962
      perror("sigprocmask");
993
963
      exitstatus = EX_OSERR;
994
964
      goto fallback;
995
965
    }
999
969
      pid = fork();
1000
970
    } while(pid == -1 and errno == EINTR);
1001
971
    if(pid == -1){
1002
 
      error(0, errno, "fork");
 
972
      perror("fork");
1003
973
      exitstatus = EX_OSERR;
1004
974
      goto fallback;
1005
975
    }
1007
977
      /* this is the child process */
1008
978
      ret = sigaction(SIGCHLD, &old_sigchld_action, NULL);
1009
979
      if(ret < 0){
1010
 
        error(0, errno, "sigaction");
 
980
        perror("sigaction");
1011
981
        _exit(EX_OSERR);
1012
982
      }
1013
983
      ret = sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
1014
984
      if(ret < 0){
1015
 
        error(0, errno, "sigprocmask");
 
985
        perror("sigprocmask");
1016
986
        _exit(EX_OSERR);
1017
987
      }
1018
988
      
1019
989
      ret = dup2(pipefd[1], STDOUT_FILENO); /* replace our stdout */
1020
990
      if(ret == -1){
1021
 
        error(0, errno, "dup2");
 
991
        perror("dup2");
1022
992
        _exit(EX_OSERR);
1023
993
      }
1024
994
      
1029
999
      }
1030
1000
      if(p->environ[0] == NULL){
1031
1001
        if(execv(filename, p->argv) < 0){
1032
 
          error(0, errno, "execv for %s", filename);
 
1002
          perror("execv");
1033
1003
          _exit(EX_OSERR);
1034
1004
        }
1035
1005
      } else {
1036
1006
        if(execve(filename, p->argv, p->environ) < 0){
1037
 
          error(0, errno, "execve for %s", filename);
 
1007
          perror("execve");
1038
1008
          _exit(EX_OSERR);
1039
1009
        }
1040
1010
      }
1046
1016
    free(filename);
1047
1017
    plugin *new_plugin = getplugin(dirst->d_name);
1048
1018
    if(new_plugin == NULL){
1049
 
      error(0, errno, "getplugin");
 
1019
      perror("getplugin");
1050
1020
      ret = (int)(TEMP_FAILURE_RETRY
1051
1021
                  (sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask,
1052
1022
                               NULL)));
1053
1023
      if(ret < 0){
1054
 
        error(0, errno, "sigprocmask");
 
1024
        perror("sigprocmask");
1055
1025
      }
1056
1026
      exitstatus = EX_OSERR;
1057
1027
      goto fallback;
1066
1036
                                              &sigchld_action.sa_mask,
1067
1037
                                              NULL));
1068
1038
    if(ret < 0){
1069
 
      error(0, errno, "sigprocmask");
 
1039
      perror("sigprocmask");
1070
1040
      exitstatus = EX_OSERR;
1071
1041
      goto fallback;
1072
1042
    }
1099
1069
    fd_set rfds = rfds_all;
1100
1070
    int select_ret = select(maxfd+1, &rfds, NULL, NULL, NULL);
1101
1071
    if(select_ret == -1 and errno != EINTR){
1102
 
      error(0, errno, "select");
 
1072
      perror("select");
1103
1073
      exitstatus = EX_OSERR;
1104
1074
      goto fallback;
1105
1075
    }
1141
1111
                                         &sigchld_action.sa_mask,
1142
1112
                                         NULL));
1143
1113
          if(ret < 0){
1144
 
            error(0, errno, "sigprocmask");
 
1114
            perror("sigprocmask");
1145
1115
            exitstatus = EX_OSERR;
1146
1116
            goto fallback;
1147
1117
          }
1155
1125
                      (sigprocmask(SIG_UNBLOCK,
1156
1126
                                   &sigchld_action.sa_mask, NULL)));
1157
1127
          if(ret < 0){
1158
 
            error(0, errno, "sigprocmask");
 
1128
            perror("sigprocmask");
1159
1129
            exitstatus = EX_OSERR;
1160
1130
            goto fallback;
1161
1131
          }
1172
1142
        bool bret = print_out_password(proc->buffer,
1173
1143
                                       proc->buffer_length);
1174
1144
        if(not bret){
1175
 
          error(0, errno, "print_out_password");
 
1145
          perror("print_out_password");
1176
1146
          exitstatus = EX_IOERR;
1177
1147
        }
1178
1148
        goto fallback;
1191
1161
        proc->buffer = realloc(proc->buffer, proc->buffer_size
1192
1162
                               + (size_t) BUFFER_SIZE);
1193
1163
        if(proc->buffer == NULL){
1194
 
          error(0, errno, "malloc");
 
1164
          perror("malloc");
1195
1165
          exitstatus = EX_OSERR;
1196
1166
          goto fallback;
1197
1167
        }
1234
1204
    }
1235
1205
    bret = print_out_password(passwordbuffer, len);
1236
1206
    if(not bret){
1237
 
      error(0, errno, "print_out_password");
 
1207
      perror("print_out_password");
1238
1208
      exitstatus = EX_IOERR;
1239
1209
    }
1240
1210
  }
1242
1212
  /* Restore old signal handler */
1243
1213
  ret = sigaction(SIGCHLD, &old_sigchld_action, NULL);
1244
1214
  if(ret == -1){
1245
 
    error(0, errno, "sigaction");
 
1215
    perror("sigaction");
1246
1216
    exitstatus = EX_OSERR;
1247
1217
  }
1248
1218
  
1264
1234
      ret = kill(p->pid, SIGTERM);
1265
1235
      if(ret == -1 and errno != ESRCH){
1266
1236
        /* Set-uid proccesses might not get closed */
1267
 
        error(0, errno, "kill");
 
1237
        perror("kill");
1268
1238
      }
1269
1239
    }
1270
1240
  }
1274
1244
    ret = wait(NULL);
1275
1245
  } while(ret >= 0);
1276
1246
  if(errno != ECHILD){
1277
 
    error(0, errno, "wait");
 
1247
    perror("wait");
1278
1248
  }
1279
1249
  
1280
1250
  free_plugin_list();