/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

fixed two bugs:
      A overbuffer flow in enviro variables causing seg fault
      A incorrect use of strsep causing seg fault

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 Teddy Hogeborn
6
 
 * Copyright © 2008 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;
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;
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;
 
258
  if(length>0 and buffer[length-1] == '\n'){
 
259
    length--;
 
260
  }
256
261
  for(size_t written = 0; written < length; written += (size_t)ret){
257
262
    ret = TEMP_FAILURE_RETRY(write(STDOUT_FILENO, buffer + written,
258
263
                                   length - written));
392
397
      if(arg == NULL){
393
398
        break;
394
399
      }
395
 
      if(not add_environment(getplugin(NULL), arg, true)){
396
 
        perror("add_environment");
 
400
      {
 
401
        char *envdef = strdup(arg);
 
402
        if(envdef == NULL){
 
403
          break;
 
404
        }
 
405
        if(not add_environment(getplugin(NULL), envdef, true)){
 
406
          perror("add_environment");
 
407
        }
397
408
      }
398
409
      break;
399
410
    case 'o':                   /* --options-for */
427
438
        if(envdef == NULL){
428
439
          break;
429
440
        }
430
 
        *envdef = '\0';
431
 
        if(not add_environment(getplugin(arg), envdef+1, true)){
 
441
        char *p_name = strndup(arg, (size_t) (envdef-arg));
 
442
        if(p_name == NULL){
 
443
          break;
 
444
        }
 
445
        envdef++;
 
446
        if(not add_environment(getplugin(p_name), envdef, true)){
432
447
          perror("add_environment");
433
448
        }
434
449
      }
452
467
      }
453
468
      break;
454
469
    case 128:                   /* --plugin-dir */
455
 
      free(plugindir);
456
470
      plugindir = strdup(arg);
457
471
      if(plugindir == NULL){
458
472
        perror("strdup");
471
485
      debug = true;
472
486
      break;
473
487
    case ARGP_KEY_ARG:
474
 
      /* Cryptsetup always passes an argument, which is an empty
475
 
         string if "none" was specified in /etc/crypttab.  So if
476
 
         argument was empty, we ignore it silently. */
477
 
      if(arg[0] != '\0'){
478
 
        fprintf(stderr, "Ignoring unknown argument \"%s\"\n", arg);
479
 
      }
 
488
      fprintf(stderr, "Ignoring unknown argument \"%s\"\n", arg);
480
489
      break;
481
490
    case ARGP_KEY_END:
482
491
      break;
501
510
    case 128:                   /* --plugin-dir */
502
511
      break;
503
512
    case 129:                   /* --config-file */
504
 
      free(argfile);
505
513
      argfile = strdup(arg);
506
514
      if(argfile == NULL){
507
515
        perror("strdup");
701
709
      
702
710
      const char const *bad_suffixes[] = { "~", "#", ".dpkg-new",
703
711
                                           ".dpkg-old",
704
 
                                           ".dpkg-bak",
705
712
                                           ".dpkg-divert", NULL };
706
713
      for(const char **pre = bad_prefixes; *pre != NULL; pre++){
707
714
        size_t pre_len = strlen(*pre);
738
745
    }
739
746
 
740
747
    char *filename;
741
 
    if(plugindir == NULL){
742
 
      ret = asprintf(&filename, PDIR "/%s", dirst->d_name);
743
 
    } else {
744
 
      ret = asprintf(&filename, "%s/%s", plugindir, dirst->d_name);
745
 
    }
 
748
    ret = asprintf(&filename, "%s/%s", plugindir, dirst->d_name);
746
749
    if(ret < 0){
747
750
      perror("asprintf");
748
751
      continue;
848
851
        perror("sigaction");
849
852
        _exit(EXIT_FAILURE);
850
853
      }
851
 
      ret = sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
 
854
      ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
852
855
      if(ret < 0){
853
856
        perror("sigprocmask");
854
857
        _exit(EXIT_FAILURE);
855
858
      }
856
 
      
 
859
 
857
860
      ret = dup2(pipefd[1], STDOUT_FILENO); /* replace our stdout */
858
861
      if(ret == -1){
859
862
        perror("dup2");
909
912
    if (maxfd < new_plugin->fd){
910
913
      maxfd = new_plugin->fd;
911
914
    }
 
915
    
912
916
  }
913
917
  
914
918
  closedir(dir);
915
919
  dir = NULL;
916
 
  
 
920
 
917
921
  for(plugin *p = plugin_list; p != NULL; p = p->next){
918
922
    if(p->pid != 0){
919
923
      break;
924
928
      free_plugin_list();
925
929
    }
926
930
  }
927
 
  
 
931
 
928
932
  /* Main loop while running plugins exist */
929
933
  while(plugin_list){
930
934
    fd_set rfds = rfds_all;
936
940
    }
937
941
    /* OK, now either a process completed, or something can be read
938
942
       from one of them */
939
 
    for(plugin *proc = plugin_list; proc != NULL;){
 
943
    for(plugin *proc = plugin_list; proc != NULL; proc = proc->next){
940
944
      /* Is this process completely done? */
941
945
      if(proc->eof and proc->completed){
942
946
        /* Only accept the plugin output if it exited cleanly */
969
973
            exitstatus = EXIT_FAILURE;
970
974
            goto fallback;
971
975
          }
972
 
          
973
 
          plugin *next_plugin = proc->next;
974
976
          free_plugin(proc);
975
 
          proc = next_plugin;
976
 
          
977
977
          /* We are done modifying process list, so unblock signal */
978
978
          ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask,
979
979
                             NULL);
986
986
          if(plugin_list == NULL){
987
987
            break;
988
988
          }
989
 
          
990
989
          continue;
991
990
        }
992
991
        
993
992
        /* This process exited nicely, so print its buffer */
994
 
        
 
993
 
995
994
        bool bret = print_out_password(proc->buffer,
996
995
                                       proc->buffer_length);
997
996
        if(not bret){
1004
1003
      /* This process has not completed.  Does it have any output? */
1005
1004
      if(proc->eof or not FD_ISSET(proc->fd, &rfds)){
1006
1005
        /* This process had nothing to say at this time */
1007
 
        proc = proc->next;
1008
1006
        continue;
1009
1007
      }
1010
1008
      /* Before reading, make the process' data buffer large enough */
1023
1021
                 BUFFER_SIZE);
1024
1022
      if(ret < 0){
1025
1023
        /* Read error from this process; ignore the error */
1026
 
        proc = proc->next;
1027
1024
        continue;
1028
1025
      }
1029
1026
      if(ret == 0){
1044
1041
    bool bret;
1045
1042
    fprintf(stderr, "Going to fallback mode using getpass(3)\n");
1046
1043
    char *passwordbuffer = getpass("Password: ");
1047
 
    size_t len = strlen(passwordbuffer);
1048
 
    /* Strip trailing newline */
1049
 
    if(len > 0 and passwordbuffer[len-1] == '\n'){
1050
 
      passwordbuffer[len-1] = '\0'; /* not strictly necessary */
1051
 
      len--;
1052
 
    }
1053
 
    bret = print_out_password(passwordbuffer, len);
 
1044
    bret = print_out_password(passwordbuffer, strlen(passwordbuffer));
1054
1045
    if(not bret){
1055
1046
      perror("print_out_password");
1056
1047
      exitstatus = EXIT_FAILURE;
1063
1054
    perror("sigaction");
1064
1055
    exitstatus = EXIT_FAILURE;
1065
1056
  }
1066
 
  
 
1057
 
1067
1058
  if(custom_argv != NULL){
1068
1059
    for(char **arg = custom_argv+1; *arg != NULL; arg++){
1069
1060
      free(*arg);
1075
1066
    closedir(dir);
1076
1067
  }
1077
1068
  
1078
 
  /* Kill the processes */
 
1069
  /* Free the process list and kill the processes */
1079
1070
  for(plugin *p = plugin_list; p != NULL; p = p->next){
1080
1071
    if(p->pid != 0){
1081
1072
      close(p->fd);
1094
1085
  if(errno != ECHILD){
1095
1086
    perror("wait");
1096
1087
  }
1097
 
  
 
1088
 
1098
1089
  free_plugin_list();
1099
1090
  
1100
1091
  free(plugindir);