/mandos/release

To get this branch, use:
bzr branch http://bzr.recompile.se/loggerhead/mandos/release

« back to all changes in this revision

Viewing changes to plugin-runner.c

* plugin-runner.c (main): Bug fix; do not accept a "d" character after
                          user ID or group ID numbers.  Bug fix: use
                          "%u" when printing PID of coredumped plugin,
                          not "%d".
* plugins.d/password-prompt.c (main): Remove comment which was copied
                                      from another program by mistake.
* plugins.d/splashy.c: Only comment changes.
* plugins.d/usplash.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 © 2007-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
27
28
#include <stdlib.h>             /* malloc(), exit(), EXIT_FAILURE,
28
29
                                   EXIT_SUCCESS, realloc() */
29
30
#include <stdbool.h>            /* bool, true, false */
30
 
#include <stdio.h>              /* perror, popen(), fileno(),
31
 
                                   fprintf(), stderr, STDOUT_FILENO */
 
31
#include <stdio.h>              /* perror, fileno(), fprintf(),
 
32
                                   stderr, STDOUT_FILENO */
32
33
#include <sys/types.h>          /* DIR, opendir(), stat(), struct
33
34
                                   stat, waitpid(), WIFEXITED(),
34
35
                                   WEXITSTATUS(), wait(), pid_t,
46
47
                                   fcntl(), setuid(), setgid(),
47
48
                                   F_GETFD, F_SETFD, FD_CLOEXEC,
48
49
                                   access(), pipe(), fork(), close()
49
 
                                   dup2, STDOUT_FILENO, _exit(),
 
50
                                   dup2(), STDOUT_FILENO, _exit(),
50
51
                                   execv(), write(), read(),
51
52
                                   close() */
52
53
#include <fcntl.h>              /* fcntl(), F_GETFD, F_SETFD,
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{
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;
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
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
544
  ret = argp_parse (&argp, argc, argv, ARGP_IN_ORDER, 0, NULL);
529
545
  if (ret == ARGP_ERR_UNKNOWN){
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
 
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);
846
862
        perror("sigaction");
847
863
        _exit(EXIT_FAILURE);
848
864
      }
849
 
      ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
 
865
      ret = sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
850
866
      if(ret < 0){
851
867
        perror("sigprocmask");
852
868
        _exit(EXIT_FAILURE);
853
869
      }
854
 
 
 
870
      
855
871
      ret = dup2(pipefd[1], STDOUT_FILENO); /* replace our stdout */
856
872
      if(ret == -1){
857
873
        perror("dup2");
907
923
    if (maxfd < new_plugin->fd){
908
924
      maxfd = new_plugin->fd;
909
925
    }
910
 
    
911
926
  }
912
927
  
913
928
  closedir(dir);
914
929
  dir = NULL;
915
 
 
 
930
  
916
931
  for(plugin *p = plugin_list; p != NULL; p = p->next){
917
932
    if(p->pid != 0){
918
933
      break;
923
938
      free_plugin_list();
924
939
    }
925
940
  }
926
 
 
 
941
  
927
942
  /* Main loop while running plugins exist */
928
943
  while(plugin_list){
929
944
    fd_set rfds = rfds_all;
953
968
                      (unsigned int) (proc->pid),
954
969
                      WTERMSIG(proc->status));
955
970
            } else if(WCOREDUMP(proc->status)){
956
 
              fprintf(stderr, "Plugin %d dumped core\n",
 
971
              fprintf(stderr, "Plugin %u dumped core\n",
957
972
                      (unsigned int) (proc->pid));
958
973
            }
959
974
          }
969
984
            goto fallback;
970
985
          }
971
986
          
 
987
          plugin *next_plugin = proc->next;
 
988
          free_plugin(proc);
 
989
          proc = next_plugin;
 
990
          
972
991
          /* We are done modifying process list, so unblock signal */
973
992
          ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask,
974
993
                             NULL);
982
1001
            break;
983
1002
          }
984
1003
          
985
 
          plugin *next_plugin = proc->next;
986
 
          free_plugin(proc);
987
 
          proc = next_plugin;
988
1004
          continue;
989
1005
        }
990
1006
        
991
1007
        /* This process exited nicely, so print its buffer */
992
 
 
 
1008
        
993
1009
        bool bret = print_out_password(proc->buffer,
994
1010
                                       proc->buffer_length);
995
1011
        if(not bret){
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
  }
1061
1077
    perror("sigaction");
1062
1078
    exitstatus = EXIT_FAILURE;
1063
1079
  }
1064
 
 
 
1080
  
1065
1081
  if(custom_argv != NULL){
1066
1082
    for(char **arg = custom_argv+1; *arg != NULL; arg++){
1067
1083
      free(*arg);
1073
1089
    closedir(dir);
1074
1090
  }
1075
1091
  
1076
 
  /* Free the process list and kill the processes */
 
1092
  /* Kill the processes */
1077
1093
  for(plugin *p = plugin_list; p != NULL; p = p->next){
1078
1094
    if(p->pid != 0){
1079
1095
      close(p->fd);
1092
1108
  if(errno != ECHILD){
1093
1109
    perror("wait");
1094
1110
  }
1095
 
 
 
1111
  
1096
1112
  free_plugin_list();
1097
1113
  
1098
1114
  free(plugindir);