/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:29:35 UTC
  • Revision ID: teddy@fukt.bsnet.se-20090113042935-ztxe47n42q5z767b
* 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
 
struct plugin;
76
 
 
77
76
typedef struct plugin{
78
77
  char *name;                   /* can be NULL or any plugin name */
79
78
  char **argv;
208
207
 * Descriptor Flags".
209
208
 * *Note File Descriptor Flags:(libc)Descriptor Flags.
210
209
 */
211
 
static int set_cloexec_flag(int fd)
212
 
{
 
210
static int set_cloexec_flag(int fd){
213
211
  int ret = fcntl(fd, F_GETFD, 0);
214
212
  /* If reading the flags failed, return error indication now. */
215
213
  if(ret < 0){
222
220
 
223
221
/* Mark processes as completed when they exit, and save their exit
224
222
   status. */
225
 
void handle_sigchld(__attribute__((unused)) int sig){
 
223
static void handle_sigchld(__attribute__((unused)) int sig){
226
224
  while(true){
227
225
    plugin *proc = plugin_list;
228
226
    int status;
253
251
}
254
252
 
255
253
/* Prints out a password to stdout */
256
 
bool print_out_password(const char *buffer, size_t length){
 
254
static bool print_out_password(const char *buffer, size_t length){
257
255
  ssize_t ret;
258
256
  for(size_t written = 0; written < length; written += (size_t)ret){
259
257
    ret = TEMP_FAILURE_RETRY(write(STDOUT_FILENO, buffer + written,
311
309
  struct stat st;
312
310
  fd_set rfds_all;
313
311
  int ret, maxfd = 0;
 
312
  ssize_t sret;
314
313
  uid_t uid = 65534;
315
314
  gid_t gid = 65534;
316
315
  bool debug = false;
464
463
      /* This is already done by parse_opt_config_file() */
465
464
      break;
466
465
    case 130:                   /* --userid */
467
 
      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
      }
468
472
      break;
469
473
    case 131:                   /* --groupid */
470
 
      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
      }
471
480
      break;
472
481
    case 132:                   /* --debug */
473
482
      debug = true;
474
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
 */
475
488
    case ARGP_KEY_ARG:
476
 
      fprintf(stderr, "Ignoring unknown argument \"%s\"\n", 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
      }
477
495
      break;
478
496
    case ARGP_KEY_END:
479
497
      break;
521
539
                       .args_doc = "",
522
540
                       .doc = "Mandos plugin runner -- Run plugins" };
523
541
  
524
 
  /* 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
525
543
     config file location, if any. */
526
544
  ret = argp_parse (&argp, argc, argv, ARGP_IN_ORDER, 0, NULL);
527
545
  if (ret == ARGP_ERR_UNKNOWN){
543
561
    char *org_line = NULL;
544
562
    char *p, *arg, *new_arg, *line;
545
563
    size_t size = 0;
546
 
    ssize_t sret;
547
564
    const char whitespace_delims[] = " \r\t\f\v\n";
548
565
    const char comment_delim[] = "#";
549
566
 
698
715
      
699
716
      const char const *bad_suffixes[] = { "~", "#", ".dpkg-new",
700
717
                                           ".dpkg-old",
 
718
                                           ".dpkg-bak",
701
719
                                           ".dpkg-divert", NULL };
702
720
      for(const char **pre = bad_prefixes; *pre != NULL; pre++){
703
721
        size_t pre_len = strlen(*pre);
844
862
        perror("sigaction");
845
863
        _exit(EXIT_FAILURE);
846
864
      }
847
 
      ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
 
865
      ret = sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
848
866
      if(ret < 0){
849
867
        perror("sigprocmask");
850
868
        _exit(EXIT_FAILURE);
851
869
      }
852
 
 
 
870
      
853
871
      ret = dup2(pipefd[1], STDOUT_FILENO); /* replace our stdout */
854
872
      if(ret == -1){
855
873
        perror("dup2");
905
923
    if (maxfd < new_plugin->fd){
906
924
      maxfd = new_plugin->fd;
907
925
    }
908
 
    
909
926
  }
910
927
  
911
928
  closedir(dir);
912
929
  dir = NULL;
913
 
 
 
930
  
914
931
  for(plugin *p = plugin_list; p != NULL; p = p->next){
915
932
    if(p->pid != 0){
916
933
      break;
921
938
      free_plugin_list();
922
939
    }
923
940
  }
924
 
 
 
941
  
925
942
  /* Main loop while running plugins exist */
926
943
  while(plugin_list){
927
944
    fd_set rfds = rfds_all;
951
968
                      (unsigned int) (proc->pid),
952
969
                      WTERMSIG(proc->status));
953
970
            } else if(WCOREDUMP(proc->status)){
954
 
              fprintf(stderr, "Plugin %d dumped core\n",
 
971
              fprintf(stderr, "Plugin %u dumped core\n",
955
972
                      (unsigned int) (proc->pid));
956
973
            }
957
974
          }
967
984
            goto fallback;
968
985
          }
969
986
          
 
987
          plugin *next_plugin = proc->next;
 
988
          free_plugin(proc);
 
989
          proc = next_plugin;
 
990
          
970
991
          /* We are done modifying process list, so unblock signal */
971
992
          ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask,
972
993
                             NULL);
980
1001
            break;
981
1002
          }
982
1003
          
983
 
          plugin *next_plugin = proc->next;
984
 
          free_plugin(proc);
985
 
          proc = next_plugin;
986
1004
          continue;
987
1005
        }
988
1006
        
989
1007
        /* This process exited nicely, so print its buffer */
990
 
 
 
1008
        
991
1009
        bool bret = print_out_password(proc->buffer,
992
1010
                                       proc->buffer_length);
993
1011
        if(not bret){
1015
1033
        proc->buffer_size += BUFFER_SIZE;
1016
1034
      }
1017
1035
      /* Read from the process */
1018
 
      ret = read(proc->fd, proc->buffer + proc->buffer_length,
1019
 
                 BUFFER_SIZE);
1020
 
      if(ret < 0){
 
1036
      sret = read(proc->fd, proc->buffer + proc->buffer_length,
 
1037
                  BUFFER_SIZE);
 
1038
      if(sret < 0){
1021
1039
        /* Read error from this process; ignore the error */
1022
1040
        proc = proc->next;
1023
1041
        continue;
1024
1042
      }
1025
 
      if(ret == 0){
 
1043
      if(sret == 0){
1026
1044
        /* got EOF */
1027
1045
        proc->eof = true;
1028
1046
      } else {
1029
 
        proc->buffer_length += (size_t) ret;
 
1047
        proc->buffer_length += (size_t) sret;
1030
1048
      }
1031
1049
    }
1032
1050
  }
1059
1077
    perror("sigaction");
1060
1078
    exitstatus = EXIT_FAILURE;
1061
1079
  }
1062
 
 
 
1080
  
1063
1081
  if(custom_argv != NULL){
1064
1082
    for(char **arg = custom_argv+1; *arg != NULL; arg++){
1065
1083
      free(*arg);
1071
1089
    closedir(dir);
1072
1090
  }
1073
1091
  
1074
 
  /* Free the process list and kill the processes */
 
1092
  /* Kill the processes */
1075
1093
  for(plugin *p = plugin_list; p != NULL; p = p->next){
1076
1094
    if(p->pid != 0){
1077
1095
      close(p->fd);
1090
1108
  if(errno != ECHILD){
1091
1109
    perror("wait");
1092
1110
  }
1093
 
 
 
1111
  
1094
1112
  free_plugin_list();
1095
1113
  
1096
1114
  free(plugindir);