/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-06 16:31:49 UTC
  • Revision ID: teddy@fukt.bsnet.se-20080906163149-1ddq2klhdwwiw1ry
Renamed "password-request" to "mandos-client".

* Makefile: - '' -
* mandos-keygen.xml: - '' -
* mandos-options.xml: - '' -
* mandos.xml: - '' -
* plugin-runner.conf: - '' -
* plugin-runner.xml: - '' -
* plugins.d/password-prompt.xml: - '' -
* plugins.d/password-request.c: Renamed to "mandos-client.c".
* plugins.d/password-request.xml: Renamed to "mandos-client.xml".

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
 
 
75
struct plugin;
 
76
 
76
77
typedef struct plugin{
77
78
  char *name;                   /* can be NULL or any plugin name */
78
79
  char **argv;
99
100
   or if none is found, creates a new one */
100
101
static plugin *getplugin(char *name){
101
102
  /* 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))){
 
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))){
105
106
      return p;
106
107
    }
107
108
  }
108
109
  /* Create a new plugin */
109
110
  plugin *new_plugin = malloc(sizeof(plugin));
110
 
  if(new_plugin == NULL){
 
111
  if (new_plugin == NULL){
111
112
    return NULL;
112
113
  }
113
114
  char *copy_name = NULL;
124
125
                           .next = plugin_list };
125
126
  
126
127
  new_plugin->argv = malloc(sizeof(char *) * 2);
127
 
  if(new_plugin->argv == NULL){
 
128
  if (new_plugin->argv == NULL){
128
129
    free(copy_name);
129
130
    free(new_plugin);
130
131
    return NULL;
207
208
 * Descriptor Flags".
208
209
 * *Note File Descriptor Flags:(libc)Descriptor Flags.
209
210
 */
210
 
static int set_cloexec_flag(int fd){
 
211
static int set_cloexec_flag(int fd)
 
212
{
211
213
  int ret = fcntl(fd, F_GETFD, 0);
212
214
  /* If reading the flags failed, return error indication now. */
213
215
  if(ret < 0){
220
222
 
221
223
/* Mark processes as completed when they exit, and save their exit
222
224
   status. */
223
 
static void handle_sigchld(__attribute__((unused)) int sig){
 
225
void handle_sigchld(__attribute__((unused)) int sig){
224
226
  while(true){
225
227
    plugin *proc = plugin_list;
226
228
    int status;
230
232
      break;
231
233
    }
232
234
    if(pid == -1){
233
 
      if(errno != ECHILD){
 
235
      if (errno != ECHILD){
234
236
        perror("waitpid");
235
237
      }
236
238
      /* No child processes */
251
253
}
252
254
 
253
255
/* Prints out a password to stdout */
254
 
static bool print_out_password(const char *buffer, size_t length){
 
256
bool print_out_password(const char *buffer, size_t length){
255
257
  ssize_t ret;
256
258
  for(size_t written = 0; written < length; written += (size_t)ret){
257
259
    ret = TEMP_FAILURE_RETRY(write(STDOUT_FILENO, buffer + written,
309
311
  struct stat st;
310
312
  fd_set rfds_all;
311
313
  int ret, maxfd = 0;
312
 
  ssize_t sret;
313
314
  uid_t uid = 65534;
314
315
  gid_t gid = 65534;
315
316
  bool debug = false;
372
373
    { .name = NULL }
373
374
  };
374
375
  
375
 
  error_t parse_opt(int key, char *arg, __attribute__((unused))
376
 
                    struct argp_state *state) {
377
 
    switch(key) {
 
376
  error_t parse_opt (int key, char *arg, __attribute__((unused))
 
377
                     struct argp_state *state) {
 
378
    switch (key) {
378
379
    case 'g':                   /* --global-options */
379
 
      if(arg != NULL){
 
380
      if (arg != NULL){
380
381
        char *p;
381
382
        while((p = strsep(&arg, ",")) != NULL){
382
383
          if(p[0] == '\0'){
393
394
      if(arg == NULL){
394
395
        break;
395
396
      }
396
 
      if(not add_environment(getplugin(NULL), arg, true)){
397
 
        perror("add_environment");
 
397
      {
 
398
        char *envdef = strdup(arg);
 
399
        if(envdef == NULL){
 
400
          break;
 
401
        }
 
402
        if(not add_environment(getplugin(NULL), envdef, true)){
 
403
          perror("add_environment");
 
404
        }
398
405
      }
399
406
      break;
400
407
    case 'o':                   /* --options-for */
401
 
      if(arg != NULL){
 
408
      if (arg != NULL){
402
409
        char *p_name = strsep(&arg, ":");
403
410
        if(p_name[0] == '\0' or arg == NULL){
404
411
          break;
428
435
        if(envdef == NULL){
429
436
          break;
430
437
        }
431
 
        *envdef = '\0';
432
 
        if(not add_environment(getplugin(arg), envdef+1, true)){
 
438
        char *p_name = strndup(arg, (size_t) (envdef-arg));
 
439
        if(p_name == NULL){
 
440
          break;
 
441
        }
 
442
        envdef++;
 
443
        if(not add_environment(getplugin(p_name), envdef, true)){
433
444
          perror("add_environment");
434
445
        }
435
446
      }
436
447
      break;
437
448
    case 'd':                   /* --disable */
438
 
      if(arg != NULL){
 
449
      if (arg != NULL){
439
450
        plugin *p = getplugin(arg);
440
451
        if(p == NULL){
441
452
          return ARGP_ERR_UNKNOWN;
444
455
      }
445
456
      break;
446
457
    case 'e':                   /* --enable */
447
 
      if(arg != NULL){
 
458
      if (arg != NULL){
448
459
        plugin *p = getplugin(arg);
449
460
        if(p == NULL){
450
461
          return ARGP_ERR_UNKNOWN;
453
464
      }
454
465
      break;
455
466
    case 128:                   /* --plugin-dir */
456
 
      free(plugindir);
457
467
      plugindir = strdup(arg);
458
468
      if(plugindir == NULL){
459
469
        perror("strdup");
463
473
      /* This is already done by parse_opt_config_file() */
464
474
      break;
465
475
    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
 
      }
 
476
      uid = (uid_t)strtol(arg, NULL, 10);
472
477
      break;
473
478
    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
 
      }
 
479
      gid = (gid_t)strtol(arg, NULL, 10);
480
480
      break;
481
481
    case 132:                   /* --debug */
482
482
      debug = true;
483
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
 
 */
488
484
    case ARGP_KEY_ARG:
489
 
      /* Cryptsetup always passes an argument, which is an empty
490
 
         string if "none" was specified in /etc/crypttab.  So if
491
 
         argument was empty, we ignore it silently. */
492
 
      if(arg[0] != '\0'){
493
 
        fprintf(stderr, "Ignoring unknown argument \"%s\"\n", arg);
494
 
      }
 
485
      fprintf(stderr, "Ignoring unknown argument \"%s\"\n", arg);
495
486
      break;
496
487
    case ARGP_KEY_END:
497
488
      break;
503
494
  
504
495
  /* This option parser is the same as parse_opt() above, except it
505
496
     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) {
 
497
  error_t parse_opt_config_file (int key, char *arg,
 
498
                                 __attribute__((unused))
 
499
                                 struct argp_state *state) {
 
500
    switch (key) {
510
501
    case 'g':                   /* --global-options */
511
502
    case 'G':                   /* --global-env */
512
503
    case 'o':                   /* --options-for */
516
507
    case 128:                   /* --plugin-dir */
517
508
      break;
518
509
    case 129:                   /* --config-file */
519
 
      free(argfile);
520
510
      argfile = strdup(arg);
521
511
      if(argfile == NULL){
522
512
        perror("strdup");
539
529
                       .args_doc = "",
540
530
                       .doc = "Mandos plugin runner -- Run plugins" };
541
531
  
542
 
  /* Parse using parse_opt_config_file() in order to get the custom
 
532
  /* Parse using the parse_opt_config_file in order to get the custom
543
533
     config file location, if any. */
544
 
  ret = argp_parse(&argp, argc, argv, ARGP_IN_ORDER, 0, NULL);
545
 
  if(ret == ARGP_ERR_UNKNOWN){
 
534
  ret = argp_parse (&argp, argc, argv, ARGP_IN_ORDER, 0, NULL);
 
535
  if (ret == ARGP_ERR_UNKNOWN){
546
536
    fprintf(stderr, "Unknown error while parsing arguments\n");
547
537
    exitstatus = EXIT_FAILURE;
548
538
    goto fallback;
552
542
  argp.parser = parse_opt;
553
543
  
554
544
  /* Open the configfile if available */
555
 
  if(argfile == NULL){
 
545
  if (argfile == NULL){
556
546
    conffp = fopen(AFILE, "r");
557
547
  } else {
558
548
    conffp = fopen(argfile, "r");
561
551
    char *org_line = NULL;
562
552
    char *p, *arg, *new_arg, *line;
563
553
    size_t size = 0;
 
554
    ssize_t sret;
564
555
    const char whitespace_delims[] = " \r\t\f\v\n";
565
556
    const char comment_delim[] = "#";
566
557
 
613
604
  } else {
614
605
    /* Check for harmful errors and go to fallback. Other errors might
615
606
       not affect opening plugins */
616
 
    if(errno == EMFILE or errno == ENFILE or errno == ENOMEM){
 
607
    if (errno == EMFILE or errno == ENFILE or errno == ENOMEM){
617
608
      perror("fopen");
618
609
      exitstatus = EXIT_FAILURE;
619
610
      goto fallback;
622
613
  /* If there was any arguments from configuration file,
623
614
     pass them to parser as command arguments */
624
615
  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){
 
616
    ret = argp_parse (&argp, custom_argc, custom_argv, ARGP_IN_ORDER,
 
617
                      0, NULL);
 
618
    if (ret == ARGP_ERR_UNKNOWN){
628
619
      fprintf(stderr, "Unknown error while parsing arguments\n");
629
620
      exitstatus = EXIT_FAILURE;
630
621
      goto fallback;
633
624
  
634
625
  /* Parse actual command line arguments, to let them override the
635
626
     config file */
636
 
  ret = argp_parse(&argp, argc, argv, ARGP_IN_ORDER, 0, NULL);
637
 
  if(ret == ARGP_ERR_UNKNOWN){
 
627
  ret = argp_parse (&argp, argc, argv, ARGP_IN_ORDER, 0, NULL);
 
628
  if (ret == ARGP_ERR_UNKNOWN){
638
629
    fprintf(stderr, "Unknown error while parsing arguments\n");
639
630
    exitstatus = EXIT_FAILURE;
640
631
    goto fallback;
656
647
  
657
648
  /* Strip permissions down to nobody */
658
649
  ret = setuid(uid);
659
 
  if(ret == -1){
 
650
  if (ret == -1){
660
651
    perror("setuid");
661
652
  }  
662
653
  setgid(gid);
663
 
  if(ret == -1){
 
654
  if (ret == -1){
664
655
    perror("setgid");
665
656
  }
666
657
  
667
 
  if(plugindir == NULL){
 
658
  if (plugindir == NULL){
668
659
    dir = opendir(PDIR);
669
660
  } else {
670
661
    dir = opendir(plugindir);
697
688
    
698
689
    /* All directory entries have been processed */
699
690
    if(dirst == NULL){
700
 
      if(errno == EBADF){
 
691
      if (errno == EBADF){
701
692
        perror("readdir");
702
693
        exitstatus = EXIT_FAILURE;
703
694
        goto fallback;
715
706
      
716
707
      const char const *bad_suffixes[] = { "~", "#", ".dpkg-new",
717
708
                                           ".dpkg-old",
718
 
                                           ".dpkg-bak",
719
709
                                           ".dpkg-divert", NULL };
720
710
      for(const char **pre = bad_prefixes; *pre != NULL; pre++){
721
711
        size_t pre_len = strlen(*pre);
763
753
    }
764
754
    
765
755
    ret = stat(filename, &st);
766
 
    if(ret == -1){
 
756
    if (ret == -1){
767
757
      perror("stat");
768
758
      free(filename);
769
759
      continue;
770
760
    }
771
761
 
772
762
    /* Ignore non-executable files */
773
 
    if(not S_ISREG(st.st_mode) or (access(filename, X_OK) != 0)){
 
763
    if (not S_ISREG(st.st_mode) or (access(filename, X_OK) != 0)){
774
764
      if(debug){
775
765
        fprintf(stderr, "Ignoring plugin dir entry \"%s\""
776
766
                " with bad type or mode\n", filename);
823
813
    
824
814
    int pipefd[2];
825
815
    ret = pipe(pipefd);
826
 
    if(ret == -1){
 
816
    if (ret == -1){
827
817
      perror("pipe");
828
818
      exitstatus = EXIT_FAILURE;
829
819
      goto fallback;
842
832
      goto fallback;
843
833
    }
844
834
    /* Block SIGCHLD until process is safely in process list */
845
 
    ret = sigprocmask(SIG_BLOCK, &sigchld_action.sa_mask, NULL);
 
835
    ret = sigprocmask (SIG_BLOCK, &sigchld_action.sa_mask, NULL);
846
836
    if(ret < 0){
847
837
      perror("sigprocmask");
848
838
      exitstatus = EXIT_FAILURE;
862
852
        perror("sigaction");
863
853
        _exit(EXIT_FAILURE);
864
854
      }
865
 
      ret = sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
 
855
      ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
866
856
      if(ret < 0){
867
857
        perror("sigprocmask");
868
858
        _exit(EXIT_FAILURE);
869
859
      }
870
 
      
 
860
 
871
861
      ret = dup2(pipefd[1], STDOUT_FILENO); /* replace our stdout */
872
862
      if(ret == -1){
873
863
        perror("dup2");
896
886
    close(pipefd[1]);           /* Close unused write end of pipe */
897
887
    free(filename);
898
888
    plugin *new_plugin = getplugin(dirst->d_name);
899
 
    if(new_plugin == NULL){
 
889
    if (new_plugin == NULL){
900
890
      perror("getplugin");
901
 
      ret = sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
 
891
      ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
902
892
      if(ret < 0){
903
893
        perror("sigprocmask");
904
894
      }
911
901
    
912
902
    /* Unblock SIGCHLD so signal handler can be run if this process
913
903
       has already completed */
914
 
    ret = sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
 
904
    ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
915
905
    if(ret < 0){
916
906
      perror("sigprocmask");
917
907
      exitstatus = EXIT_FAILURE;
920
910
    
921
911
    FD_SET(new_plugin->fd, &rfds_all);
922
912
    
923
 
    if(maxfd < new_plugin->fd){
 
913
    if (maxfd < new_plugin->fd){
924
914
      maxfd = new_plugin->fd;
925
915
    }
 
916
    
926
917
  }
927
918
  
928
919
  closedir(dir);
929
920
  dir = NULL;
930
 
  
 
921
 
931
922
  for(plugin *p = plugin_list; p != NULL; p = p->next){
932
923
    if(p->pid != 0){
933
924
      break;
938
929
      free_plugin_list();
939
930
    }
940
931
  }
941
 
  
 
932
 
942
933
  /* Main loop while running plugins exist */
943
934
  while(plugin_list){
944
935
    fd_set rfds = rfds_all;
945
936
    int select_ret = select(maxfd+1, &rfds, NULL, NULL, NULL);
946
 
    if(select_ret == -1){
 
937
    if (select_ret == -1){
947
938
      perror("select");
948
939
      exitstatus = EXIT_FAILURE;
949
940
      goto fallback;
950
941
    }
951
942
    /* OK, now either a process completed, or something can be read
952
943
       from one of them */
953
 
    for(plugin *proc = plugin_list; proc != NULL;){
 
944
    for(plugin *proc = plugin_list; proc != NULL; proc = proc->next){
954
945
      /* Is this process completely done? */
955
946
      if(proc->eof and proc->completed){
956
947
        /* Only accept the plugin output if it exited cleanly */
968
959
                      (unsigned int) (proc->pid),
969
960
                      WTERMSIG(proc->status));
970
961
            } else if(WCOREDUMP(proc->status)){
971
 
              fprintf(stderr, "Plugin %u dumped core\n",
 
962
              fprintf(stderr, "Plugin %d dumped core\n",
972
963
                      (unsigned int) (proc->pid));
973
964
            }
974
965
          }
983
974
            exitstatus = EXIT_FAILURE;
984
975
            goto fallback;
985
976
          }
986
 
          
987
 
          plugin *next_plugin = proc->next;
988
977
          free_plugin(proc);
989
 
          proc = next_plugin;
990
 
          
991
978
          /* We are done modifying process list, so unblock signal */
992
 
          ret = sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask,
993
 
                            NULL);
 
979
          ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask,
 
980
                             NULL);
994
981
          if(ret < 0){
995
982
            perror("sigprocmask");
996
983
            exitstatus = EXIT_FAILURE;
1000
987
          if(plugin_list == NULL){
1001
988
            break;
1002
989
          }
1003
 
          
1004
990
          continue;
1005
991
        }
1006
992
        
1007
993
        /* This process exited nicely, so print its buffer */
1008
 
        
 
994
 
1009
995
        bool bret = print_out_password(proc->buffer,
1010
996
                                       proc->buffer_length);
1011
997
        if(not bret){
1018
1004
      /* This process has not completed.  Does it have any output? */
1019
1005
      if(proc->eof or not FD_ISSET(proc->fd, &rfds)){
1020
1006
        /* This process had nothing to say at this time */
1021
 
        proc = proc->next;
1022
1007
        continue;
1023
1008
      }
1024
1009
      /* Before reading, make the process' data buffer large enough */
1025
1010
      if(proc->buffer_length + BUFFER_SIZE > proc->buffer_size){
1026
1011
        proc->buffer = realloc(proc->buffer, proc->buffer_size
1027
1012
                               + (size_t) BUFFER_SIZE);
1028
 
        if(proc->buffer == NULL){
 
1013
        if (proc->buffer == NULL){
1029
1014
          perror("malloc");
1030
1015
          exitstatus = EXIT_FAILURE;
1031
1016
          goto fallback;
1033
1018
        proc->buffer_size += BUFFER_SIZE;
1034
1019
      }
1035
1020
      /* Read from the process */
1036
 
      sret = read(proc->fd, proc->buffer + proc->buffer_length,
1037
 
                  BUFFER_SIZE);
1038
 
      if(sret < 0){
 
1021
      ret = read(proc->fd, proc->buffer + proc->buffer_length,
 
1022
                 BUFFER_SIZE);
 
1023
      if(ret < 0){
1039
1024
        /* Read error from this process; ignore the error */
1040
 
        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);