/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

  • Committer: Teddy Hogeborn
  • Date: 2008-09-04 12:38:35 UTC
  • Revision ID: teddy@fukt.bsnet.se-20080904123835-hervuvv4pmpxl8r1
* README: Improved wording.

* initramfs-tools-hook: Do not copy auto-save files from
                        "/etc/mandos/plugins.d".  Do not create key
                        ring files.  Do not reset permissions on
                        things like "$DESTDIR/lib/mandos/plugins.d",
                        which should not be publicly accessible.

* plugin-runner.xml (DESCRIPTION, PLUGINS): Improved grammar.

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 & Björn Påhlsson
 
5
 * Copyright © 2007-2008 Teddy Hogeborn & Björn Påhlsson
6
6
 * 
7
7
 * This program is free software: you can redistribute it and/or
8
8
 * modify it under the terms of the GNU General Public License as
27
27
#include <stdlib.h>             /* malloc(), exit(), EXIT_FAILURE,
28
28
                                   EXIT_SUCCESS, realloc() */
29
29
#include <stdbool.h>            /* bool, true, false */
30
 
#include <stdio.h>              /* perror, fileno(), fprintf(),
31
 
                                   stderr, STDOUT_FILENO */
 
30
#include <stdio.h>              /* perror, popen(), fileno(),
 
31
                                   fprintf(), stderr, STDOUT_FILENO */
32
32
#include <sys/types.h>          /* DIR, opendir(), stat(), struct
33
33
                                   stat, waitpid(), WIFEXITED(),
34
34
                                   WEXITSTATUS(), wait(), pid_t,
46
46
                                   fcntl(), setuid(), setgid(),
47
47
                                   F_GETFD, F_SETFD, FD_CLOEXEC,
48
48
                                   access(), pipe(), fork(), close()
49
 
                                   dup2(), STDOUT_FILENO, _exit(),
 
49
                                   dup2, STDOUT_FILENO, _exit(),
50
50
                                   execv(), write(), read(),
51
51
                                   close() */
52
52
#include <fcntl.h>              /* fcntl(), F_GETFD, F_SETFD,
72
72
const char *argp_program_version = "plugin-runner 1.0";
73
73
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
74
74
 
 
75
struct plugin;
 
76
 
75
77
typedef struct plugin{
76
78
  char *name;                   /* can be NULL or any plugin name */
77
79
  char **argv;
94
96
 
95
97
static plugin *plugin_list = NULL;
96
98
 
97
 
/* Gets an existing plugin based on name,
 
99
/* Gets a existing plugin based on name,
98
100
   or if none is found, creates a new one */
99
101
static plugin *getplugin(char *name){
100
102
  /* Check for exiting plugin with that name */
116
118
      return NULL;
117
119
    }
118
120
  }
119
 
  
 
121
 
120
122
  *new_plugin = (plugin) { .name = copy_name,
121
123
                           .argc = 1,
122
124
                           .disabled = false,
185
187
  size_t namelen = (size_t)(strchrnul(def, '=') - def);
186
188
  /* Search for this environment variable */
187
189
  for(char **e = p->environ; *e != NULL; e++){
188
 
    if(strncmp(*e, def, namelen + 1) == 0){
 
190
    if(strncmp(*e, def, namelen+1) == 0){
189
191
      /* It already exists */
190
192
      if(replace){
191
 
        char *new = realloc(*e, strlen(def) + 1);
 
193
        char *new = realloc(*e, strlen(def));
192
194
        if(new == NULL){
193
195
          return false;
194
196
        }
206
208
 * Descriptor Flags".
207
209
 * *Note File Descriptor Flags:(libc)Descriptor Flags.
208
210
 */
209
 
static int set_cloexec_flag(int fd){
 
211
static int set_cloexec_flag(int fd)
 
212
{
210
213
  int ret = fcntl(fd, F_GETFD, 0);
211
214
  /* If reading the flags failed, return error indication now. */
212
215
  if(ret < 0){
219
222
 
220
223
/* Mark processes as completed when they exit, and save their exit
221
224
   status. */
222
 
static void handle_sigchld(__attribute__((unused)) int sig){
 
225
void handle_sigchld(__attribute__((unused)) int sig){
223
226
  while(true){
224
227
    plugin *proc = plugin_list;
225
228
    int status;
235
238
      /* No child processes */
236
239
      break;
237
240
    }
238
 
    
 
241
 
239
242
    /* A child exited, find it in process_list */
240
243
    while(proc != NULL and proc->pid != pid){
241
244
      proc = proc->next;
250
253
}
251
254
 
252
255
/* Prints out a password to stdout */
253
 
static bool print_out_password(const char *buffer, size_t length){
 
256
bool print_out_password(const char *buffer, size_t length){
254
257
  ssize_t ret;
 
258
  if(length>0 and buffer[length-1] == '\n'){
 
259
    length--;
 
260
  }
255
261
  for(size_t written = 0; written < length; written += (size_t)ret){
256
262
    ret = TEMP_FAILURE_RETRY(write(STDOUT_FILENO, buffer + written,
257
263
                                   length - written));
391
397
      if(arg == NULL){
392
398
        break;
393
399
      }
394
 
      if(not add_environment(getplugin(NULL), arg, true)){
395
 
        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
        }
396
408
      }
397
409
      break;
398
410
    case 'o':                   /* --options-for */
399
411
      if (arg != NULL){
400
412
        char *p_name = strsep(&arg, ":");
401
 
        if(p_name[0] == '\0' or arg == NULL){
 
413
        if(p_name[0] == '\0'){
402
414
          break;
403
415
        }
404
416
        char *opt = strsep(&arg, ":");
405
 
        if(opt[0] == '\0' or opt == NULL){
 
417
        if(opt[0] == '\0'){
406
418
          break;
407
419
        }
408
 
        char *p;
409
 
        while((p = strsep(&opt, ",")) != NULL){
410
 
          if(p[0] == '\0'){
411
 
            continue;
412
 
          }
413
 
          if(not add_argument(getplugin(p_name), p)){
414
 
            perror("add_argument");
415
 
            return ARGP_ERR_UNKNOWN;
 
420
        if(opt != NULL){
 
421
          char *p;
 
422
          while((p = strsep(&opt, ",")) != NULL){
 
423
            if(p[0] == '\0'){
 
424
              continue;
 
425
            }
 
426
            if(not add_argument(getplugin(p_name), p)){
 
427
              perror("add_argument");
 
428
              return ARGP_ERR_UNKNOWN;
 
429
            }
416
430
          }
417
431
        }
418
432
      }
426
440
        if(envdef == NULL){
427
441
          break;
428
442
        }
429
 
        *envdef = '\0';
430
 
        if(not add_environment(getplugin(arg), envdef+1, true)){
 
443
        char *p_name = strndup(arg, (size_t) (envdef-arg));
 
444
        if(p_name == NULL){
 
445
          break;
 
446
        }
 
447
        envdef++;
 
448
        if(not add_environment(getplugin(p_name), envdef, true)){
431
449
          perror("add_environment");
432
450
        }
433
451
      }
451
469
      }
452
470
      break;
453
471
    case 128:                   /* --plugin-dir */
454
 
      free(plugindir);
455
472
      plugindir = strdup(arg);
456
473
      if(plugindir == NULL){
457
474
        perror("strdup");
470
487
      debug = true;
471
488
      break;
472
489
    case ARGP_KEY_ARG:
473
 
      /* Cryptsetup always passes an argument, which is an empty
474
 
         string if "none" was specified in /etc/crypttab.  So if
475
 
         argument was empty, we ignore it silently. */
476
 
      if(arg[0] != '\0'){
477
 
        fprintf(stderr, "Ignoring unknown argument \"%s\"\n", arg);
478
 
      }
 
490
      fprintf(stderr, "Ignoring unknown argument \"%s\"\n", arg);
479
491
      break;
480
492
    case ARGP_KEY_END:
481
493
      break;
500
512
    case 128:                   /* --plugin-dir */
501
513
      break;
502
514
    case 129:                   /* --config-file */
503
 
      free(argfile);
504
515
      argfile = strdup(arg);
505
516
      if(argfile == NULL){
506
517
        perror("strdup");
736
747
    }
737
748
 
738
749
    char *filename;
739
 
    if(plugindir == NULL){
740
 
      ret = asprintf(&filename, PDIR "/%s", dirst->d_name);
741
 
    } else {
742
 
      ret = asprintf(&filename, "%s/%s", plugindir, dirst->d_name);
743
 
    }
 
750
    ret = asprintf(&filename, "%s/%s", plugindir, dirst->d_name);
744
751
    if(ret < 0){
745
752
      perror("asprintf");
746
753
      continue;
846
853
        perror("sigaction");
847
854
        _exit(EXIT_FAILURE);
848
855
      }
849
 
      ret = sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
 
856
      ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
850
857
      if(ret < 0){
851
858
        perror("sigprocmask");
852
859
        _exit(EXIT_FAILURE);
853
860
      }
854
 
      
 
861
 
855
862
      ret = dup2(pipefd[1], STDOUT_FILENO); /* replace our stdout */
856
863
      if(ret == -1){
857
864
        perror("dup2");
907
914
    if (maxfd < new_plugin->fd){
908
915
      maxfd = new_plugin->fd;
909
916
    }
 
917
    
910
918
  }
911
919
  
912
920
  closedir(dir);
913
921
  dir = NULL;
914
 
  
 
922
 
915
923
  for(plugin *p = plugin_list; p != NULL; p = p->next){
916
924
    if(p->pid != 0){
917
925
      break;
922
930
      free_plugin_list();
923
931
    }
924
932
  }
925
 
  
 
933
 
926
934
  /* Main loop while running plugins exist */
927
935
  while(plugin_list){
928
936
    fd_set rfds = rfds_all;
934
942
    }
935
943
    /* OK, now either a process completed, or something can be read
936
944
       from one of them */
937
 
    for(plugin *proc = plugin_list; proc != NULL;){
 
945
    for(plugin *proc = plugin_list; proc != NULL; proc = proc->next){
938
946
      /* Is this process completely done? */
939
947
      if(proc->eof and proc->completed){
940
948
        /* Only accept the plugin output if it exited cleanly */
967
975
            exitstatus = EXIT_FAILURE;
968
976
            goto fallback;
969
977
          }
970
 
          
971
 
          plugin *next_plugin = proc->next;
972
978
          free_plugin(proc);
973
 
          proc = next_plugin;
974
 
          
975
979
          /* We are done modifying process list, so unblock signal */
976
980
          ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask,
977
981
                             NULL);
984
988
          if(plugin_list == NULL){
985
989
            break;
986
990
          }
987
 
          
988
991
          continue;
989
992
        }
990
993
        
991
994
        /* This process exited nicely, so print its buffer */
992
 
        
 
995
 
993
996
        bool bret = print_out_password(proc->buffer,
994
997
                                       proc->buffer_length);
995
998
        if(not bret){
1002
1005
      /* This process has not completed.  Does it have any output? */
1003
1006
      if(proc->eof or not FD_ISSET(proc->fd, &rfds)){
1004
1007
        /* This process had nothing to say at this time */
1005
 
        proc = proc->next;
1006
1008
        continue;
1007
1009
      }
1008
1010
      /* Before reading, make the process' data buffer large enough */
1021
1023
                 BUFFER_SIZE);
1022
1024
      if(ret < 0){
1023
1025
        /* Read error from this process; ignore the error */
1024
 
        proc = proc->next;
1025
1026
        continue;
1026
1027
      }
1027
1028
      if(ret == 0){
1042
1043
    bool bret;
1043
1044
    fprintf(stderr, "Going to fallback mode using getpass(3)\n");
1044
1045
    char *passwordbuffer = getpass("Password: ");
1045
 
    size_t len = strlen(passwordbuffer);
1046
 
    /* Strip trailing newline */
1047
 
    if(len > 0 and passwordbuffer[len-1] == '\n'){
1048
 
      passwordbuffer[len-1] = '\0'; /* not strictly necessary */
1049
 
      len--;
1050
 
    }
1051
 
    bret = print_out_password(passwordbuffer, len);
 
1046
    bret = print_out_password(passwordbuffer, strlen(passwordbuffer));
1052
1047
    if(not bret){
1053
1048
      perror("print_out_password");
1054
1049
      exitstatus = EXIT_FAILURE;
1061
1056
    perror("sigaction");
1062
1057
    exitstatus = EXIT_FAILURE;
1063
1058
  }
1064
 
  
 
1059
 
1065
1060
  if(custom_argv != NULL){
1066
1061
    for(char **arg = custom_argv+1; *arg != NULL; arg++){
1067
1062
      free(*arg);
1073
1068
    closedir(dir);
1074
1069
  }
1075
1070
  
1076
 
  /* Kill the processes */
 
1071
  /* Free the process list and kill the processes */
1077
1072
  for(plugin *p = plugin_list; p != NULL; p = p->next){
1078
1073
    if(p->pid != 0){
1079
1074
      close(p->fd);
1092
1087
  if(errno != ECHILD){
1093
1088
    perror("wait");
1094
1089
  }
1095
 
  
 
1090
 
1096
1091
  free_plugin_list();
1097
1092
  
1098
1093
  free(plugindir);