/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-05 07:11:24 UTC
  • Revision ID: teddy@fukt.bsnet.se-20080905071124-9dq11jq5rfd6zfxf
* Makefile: Changed to use symbolic instead of octal modes throughout.
  (KEYDIR): New variable for the key directory.
  (install-server): Bug fix: remove "--parents" from install args.
  (install-client): Bug fix: - '' -  Also create key directory.  Do
                    not chmod plugin dir.  Create custom plugin directory
                    if not the same as normal plugin directory.  Add
                    "--dir" option to "mandos-keygen".  Add note about
                    running "mandos-keygen --password".
  (uninstall-server): Do not depend on the installed server binary,
                      since this made it impossible to do a purge
                      after an uninstall.
  (purge-client): Shred seckey.txt.  Use $(KEYDIR).

* README: Improved wording.

* initramfs-tools-hook: Use a loop to find prefix.  Also find keydir.
                        Remove "${DESTDIR}" from "copy_exec".  Do not
                        try to copy literal "*" if no custom plugins
                        are found.  Copy key files from keydir, not
                        config dir.  Only repair mode on directories
                        that actually exist.  Do not run chmod if
                        nothing needs repairing.

* plugin-runner.conf: New file.

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,
69
69
#define PDIR "/lib/mandos/plugins.d"
70
70
#define AFILE "/conf/conf.d/mandos/plugin-runner.conf"
71
71
 
72
 
const char *argp_program_version = "plugin-runner " VERSION;
 
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");
700
711
      
701
712
      const char const *bad_suffixes[] = { "~", "#", ".dpkg-new",
702
713
                                           ".dpkg-old",
703
 
                                           ".dpkg-bak",
704
714
                                           ".dpkg-divert", NULL };
705
715
      for(const char **pre = bad_prefixes; *pre != NULL; pre++){
706
716
        size_t pre_len = strlen(*pre);
737
747
    }
738
748
 
739
749
    char *filename;
740
 
    if(plugindir == NULL){
741
 
      ret = asprintf(&filename, PDIR "/%s", dirst->d_name);
742
 
    } else {
743
 
      ret = asprintf(&filename, "%s/%s", plugindir, dirst->d_name);
744
 
    }
 
750
    ret = asprintf(&filename, "%s/%s", plugindir, dirst->d_name);
745
751
    if(ret < 0){
746
752
      perror("asprintf");
747
753
      continue;
847
853
        perror("sigaction");
848
854
        _exit(EXIT_FAILURE);
849
855
      }
850
 
      ret = sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
 
856
      ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
851
857
      if(ret < 0){
852
858
        perror("sigprocmask");
853
859
        _exit(EXIT_FAILURE);
854
860
      }
855
 
      
 
861
 
856
862
      ret = dup2(pipefd[1], STDOUT_FILENO); /* replace our stdout */
857
863
      if(ret == -1){
858
864
        perror("dup2");
908
914
    if (maxfd < new_plugin->fd){
909
915
      maxfd = new_plugin->fd;
910
916
    }
 
917
    
911
918
  }
912
919
  
913
920
  closedir(dir);
914
921
  dir = NULL;
915
 
  
 
922
 
916
923
  for(plugin *p = plugin_list; p != NULL; p = p->next){
917
924
    if(p->pid != 0){
918
925
      break;
923
930
      free_plugin_list();
924
931
    }
925
932
  }
926
 
  
 
933
 
927
934
  /* Main loop while running plugins exist */
928
935
  while(plugin_list){
929
936
    fd_set rfds = rfds_all;
935
942
    }
936
943
    /* OK, now either a process completed, or something can be read
937
944
       from one of them */
938
 
    for(plugin *proc = plugin_list; proc != NULL;){
 
945
    for(plugin *proc = plugin_list; proc != NULL; proc = proc->next){
939
946
      /* Is this process completely done? */
940
947
      if(proc->eof and proc->completed){
941
948
        /* Only accept the plugin output if it exited cleanly */
968
975
            exitstatus = EXIT_FAILURE;
969
976
            goto fallback;
970
977
          }
971
 
          
972
 
          plugin *next_plugin = proc->next;
973
978
          free_plugin(proc);
974
 
          proc = next_plugin;
975
 
          
976
979
          /* We are done modifying process list, so unblock signal */
977
980
          ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask,
978
981
                             NULL);
985
988
          if(plugin_list == NULL){
986
989
            break;
987
990
          }
988
 
          
989
991
          continue;
990
992
        }
991
993
        
992
994
        /* This process exited nicely, so print its buffer */
993
 
        
 
995
 
994
996
        bool bret = print_out_password(proc->buffer,
995
997
                                       proc->buffer_length);
996
998
        if(not bret){
1003
1005
      /* This process has not completed.  Does it have any output? */
1004
1006
      if(proc->eof or not FD_ISSET(proc->fd, &rfds)){
1005
1007
        /* This process had nothing to say at this time */
1006
 
        proc = proc->next;
1007
1008
        continue;
1008
1009
      }
1009
1010
      /* Before reading, make the process' data buffer large enough */
1022
1023
                 BUFFER_SIZE);
1023
1024
      if(ret < 0){
1024
1025
        /* Read error from this process; ignore the error */
1025
 
        proc = proc->next;
1026
1026
        continue;
1027
1027
      }
1028
1028
      if(ret == 0){
1043
1043
    bool bret;
1044
1044
    fprintf(stderr, "Going to fallback mode using getpass(3)\n");
1045
1045
    char *passwordbuffer = getpass("Password: ");
1046
 
    size_t len = strlen(passwordbuffer);
1047
 
    /* Strip trailing newline */
1048
 
    if(len > 0 and passwordbuffer[len-1] == '\n'){
1049
 
      passwordbuffer[len-1] = '\0'; /* not strictly necessary */
1050
 
      len--;
1051
 
    }
1052
 
    bret = print_out_password(passwordbuffer, len);
 
1046
    bret = print_out_password(passwordbuffer, strlen(passwordbuffer));
1053
1047
    if(not bret){
1054
1048
      perror("print_out_password");
1055
1049
      exitstatus = EXIT_FAILURE;
1062
1056
    perror("sigaction");
1063
1057
    exitstatus = EXIT_FAILURE;
1064
1058
  }
1065
 
  
 
1059
 
1066
1060
  if(custom_argv != NULL){
1067
1061
    for(char **arg = custom_argv+1; *arg != NULL; arg++){
1068
1062
      free(*arg);
1074
1068
    closedir(dir);
1075
1069
  }
1076
1070
  
1077
 
  /* Kill the processes */
 
1071
  /* Free the process list and kill the processes */
1078
1072
  for(plugin *p = plugin_list; p != NULL; p = p->next){
1079
1073
    if(p->pid != 0){
1080
1074
      close(p->fd);
1093
1087
  if(errno != ECHILD){
1094
1088
    perror("wait");
1095
1089
  }
1096
 
  
 
1090
 
1097
1091
  free_plugin_list();
1098
1092
  
1099
1093
  free(plugindir);