/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-06 15:34:21 UTC
  • Revision ID: teddy@fukt.bsnet.se-20080906153421-605ydhwyo404krh9
* initramfs-tools-hook: Use long options where available.  Use only
                        one "test" invocation instead of several.

* mandos-keygen: Use only one "test" invocation instead of several.
                 Use long options where available.  Strip newline from
                 entered password.

* plugin-runner.c (print_out_password): Do not strip trailing newline.
  (fallback): Strip trailing newline from getpass().

* plugin-runner.xml (PLUGINS): Plugins should not print trailing
                               newlines.

* plugins.d/password-prompt.c (main): Strip trailing newline.

* plugins.d/password-request.c (main): Bug fix: Read "pubkey.txt", not
                                       "pupkey.txt".

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,2009 Teddy Hogeborn
6
 
 * Copyright © 2008,2009 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;
256
258
  for(size_t written = 0; written < length; written += (size_t)ret){
257
259
    ret = TEMP_FAILURE_RETRY(write(STDOUT_FILENO, buffer + written,
392
394
      if(arg == NULL){
393
395
        break;
394
396
      }
395
 
      if(not add_environment(getplugin(NULL), arg, true)){
396
 
        perror("add_environment");
 
397
      {
 
398
        char *envdef = strdup(arg);
 
399
        if(envdef == NULL){
 
400
          break;
 
401
        }
 
402
        if(not add_environment(getplugin(NULL), envdef, true)){
 
403
          perror("add_environment");
 
404
        }
397
405
      }
398
406
      break;
399
407
    case 'o':                   /* --options-for */
427
435
        if(envdef == NULL){
428
436
          break;
429
437
        }
430
 
        *envdef = '\0';
431
 
        if(not add_environment(getplugin(arg), envdef+1, true)){
 
438
        char *p_name = strndup(arg, (size_t) (envdef-arg));
 
439
        if(p_name == NULL){
 
440
          break;
 
441
        }
 
442
        envdef++;
 
443
        if(not add_environment(getplugin(p_name), envdef, true)){
432
444
          perror("add_environment");
433
445
        }
434
446
      }
452
464
      }
453
465
      break;
454
466
    case 128:                   /* --plugin-dir */
455
 
      free(plugindir);
456
467
      plugindir = strdup(arg);
457
468
      if(plugindir == NULL){
458
469
        perror("strdup");
471
482
      debug = true;
472
483
      break;
473
484
    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
 
      }
 
485
      fprintf(stderr, "Ignoring unknown argument \"%s\"\n", arg);
480
486
      break;
481
487
    case ARGP_KEY_END:
482
488
      break;
501
507
    case 128:                   /* --plugin-dir */
502
508
      break;
503
509
    case 129:                   /* --config-file */
504
 
      free(argfile);
505
510
      argfile = strdup(arg);
506
511
      if(argfile == NULL){
507
512
        perror("strdup");
701
706
      
702
707
      const char const *bad_suffixes[] = { "~", "#", ".dpkg-new",
703
708
                                           ".dpkg-old",
704
 
                                           ".dpkg-bak",
705
709
                                           ".dpkg-divert", NULL };
706
710
      for(const char **pre = bad_prefixes; *pre != NULL; pre++){
707
711
        size_t pre_len = strlen(*pre);
738
742
    }
739
743
 
740
744
    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
 
    }
 
745
    ret = asprintf(&filename, "%s/%s", plugindir, dirst->d_name);
746
746
    if(ret < 0){
747
747
      perror("asprintf");
748
748
      continue;
848
848
        perror("sigaction");
849
849
        _exit(EXIT_FAILURE);
850
850
      }
851
 
      ret = sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
 
851
      ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
852
852
      if(ret < 0){
853
853
        perror("sigprocmask");
854
854
        _exit(EXIT_FAILURE);
855
855
      }
856
 
      
 
856
 
857
857
      ret = dup2(pipefd[1], STDOUT_FILENO); /* replace our stdout */
858
858
      if(ret == -1){
859
859
        perror("dup2");
909
909
    if (maxfd < new_plugin->fd){
910
910
      maxfd = new_plugin->fd;
911
911
    }
 
912
    
912
913
  }
913
914
  
914
915
  closedir(dir);
915
916
  dir = NULL;
916
 
  
 
917
 
917
918
  for(plugin *p = plugin_list; p != NULL; p = p->next){
918
919
    if(p->pid != 0){
919
920
      break;
924
925
      free_plugin_list();
925
926
    }
926
927
  }
927
 
  
 
928
 
928
929
  /* Main loop while running plugins exist */
929
930
  while(plugin_list){
930
931
    fd_set rfds = rfds_all;
936
937
    }
937
938
    /* OK, now either a process completed, or something can be read
938
939
       from one of them */
939
 
    for(plugin *proc = plugin_list; proc != NULL;){
 
940
    for(plugin *proc = plugin_list; proc != NULL; proc = proc->next){
940
941
      /* Is this process completely done? */
941
942
      if(proc->eof and proc->completed){
942
943
        /* Only accept the plugin output if it exited cleanly */
969
970
            exitstatus = EXIT_FAILURE;
970
971
            goto fallback;
971
972
          }
972
 
          
973
 
          plugin *next_plugin = proc->next;
974
973
          free_plugin(proc);
975
 
          proc = next_plugin;
976
 
          
977
974
          /* We are done modifying process list, so unblock signal */
978
975
          ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask,
979
976
                             NULL);
986
983
          if(plugin_list == NULL){
987
984
            break;
988
985
          }
989
 
          
990
986
          continue;
991
987
        }
992
988
        
993
989
        /* This process exited nicely, so print its buffer */
994
 
        
 
990
 
995
991
        bool bret = print_out_password(proc->buffer,
996
992
                                       proc->buffer_length);
997
993
        if(not bret){
1004
1000
      /* This process has not completed.  Does it have any output? */
1005
1001
      if(proc->eof or not FD_ISSET(proc->fd, &rfds)){
1006
1002
        /* This process had nothing to say at this time */
1007
 
        proc = proc->next;
1008
1003
        continue;
1009
1004
      }
1010
1005
      /* Before reading, make the process' data buffer large enough */
1023
1018
                 BUFFER_SIZE);
1024
1019
      if(ret < 0){
1025
1020
        /* Read error from this process; ignore the error */
1026
 
        proc = proc->next;
1027
1021
        continue;
1028
1022
      }
1029
1023
      if(ret == 0){
1063
1057
    perror("sigaction");
1064
1058
    exitstatus = EXIT_FAILURE;
1065
1059
  }
1066
 
  
 
1060
 
1067
1061
  if(custom_argv != NULL){
1068
1062
    for(char **arg = custom_argv+1; *arg != NULL; arg++){
1069
1063
      free(*arg);
1075
1069
    closedir(dir);
1076
1070
  }
1077
1071
  
1078
 
  /* Kill the processes */
 
1072
  /* Free the process list and kill the processes */
1079
1073
  for(plugin *p = plugin_list; p != NULL; p = p->next){
1080
1074
    if(p->pid != 0){
1081
1075
      close(p->fd);
1094
1088
  if(errno != ECHILD){
1095
1089
    perror("wait");
1096
1090
  }
1097
 
  
 
1091
 
1098
1092
  free_plugin_list();
1099
1093
  
1100
1094
  free(plugindir);