/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-10-12 07:27:43 UTC
  • Revision ID: teddy@fukt.bsnet.se-20081012072743-0g7o9e5mqqv4fkob
* README: Refer to man pages of usplash, splashy, and askpass-fifo.

* debian/copyright: Rewritten to conform to
  <http://wiki.debian.org/Proposals/CopyrightFormat?action=recall&rev=233>.

* mandos-keygen: Sign encrypted keys.

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 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, popen(), fileno(),
31
 
                                   fprintf(), stderr, STDOUT_FILENO */
 
30
#include <stdio.h>              /* perror, fileno(), fprintf(),
 
31
                                   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 1.0";
 
72
const char *argp_program_version = "plugin-runner " VERSION;
73
73
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
74
74
 
75
 
struct plugin;
76
 
 
77
75
typedef struct plugin{
78
76
  char *name;                   /* can be NULL or any plugin name */
79
77
  char **argv;
208
206
 * Descriptor Flags".
209
207
 * *Note File Descriptor Flags:(libc)Descriptor Flags.
210
208
 */
211
 
static int set_cloexec_flag(int fd)
212
 
{
 
209
static int set_cloexec_flag(int fd){
213
210
  int ret = fcntl(fd, F_GETFD, 0);
214
211
  /* If reading the flags failed, return error indication now. */
215
212
  if(ret < 0){
222
219
 
223
220
/* Mark processes as completed when they exit, and save their exit
224
221
   status. */
225
 
void handle_sigchld(__attribute__((unused)) int sig){
 
222
static void handle_sigchld(__attribute__((unused)) int sig){
226
223
  while(true){
227
224
    plugin *proc = plugin_list;
228
225
    int status;
253
250
}
254
251
 
255
252
/* Prints out a password to stdout */
256
 
bool print_out_password(const char *buffer, size_t length){
 
253
static bool print_out_password(const char *buffer, size_t length){
257
254
  ssize_t ret;
258
 
  if(length>0 and buffer[length-1] == '\n'){
259
 
    length--;
260
 
  }
261
255
  for(size_t written = 0; written < length; written += (size_t)ret){
262
256
    ret = TEMP_FAILURE_RETRY(write(STDOUT_FILENO, buffer + written,
263
257
                                   length - written));
397
391
      if(arg == NULL){
398
392
        break;
399
393
      }
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
 
        }
 
394
      if(not add_environment(getplugin(NULL), arg, true)){
 
395
        perror("add_environment");
408
396
      }
409
397
      break;
410
398
    case 'o':                   /* --options-for */
438
426
        if(envdef == NULL){
439
427
          break;
440
428
        }
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)){
 
429
        *envdef = '\0';
 
430
        if(not add_environment(getplugin(arg), envdef+1, true)){
447
431
          perror("add_environment");
448
432
        }
449
433
      }
467
451
      }
468
452
      break;
469
453
    case 128:                   /* --plugin-dir */
 
454
      free(plugindir);
470
455
      plugindir = strdup(arg);
471
456
      if(plugindir == NULL){
472
457
        perror("strdup");
485
470
      debug = true;
486
471
      break;
487
472
    case ARGP_KEY_ARG:
488
 
      fprintf(stderr, "Ignoring unknown argument \"%s\"\n", 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
      }
489
479
      break;
490
480
    case ARGP_KEY_END:
491
481
      break;
510
500
    case 128:                   /* --plugin-dir */
511
501
      break;
512
502
    case 129:                   /* --config-file */
 
503
      free(argfile);
513
504
      argfile = strdup(arg);
514
505
      if(argfile == NULL){
515
506
        perror("strdup");
745
736
    }
746
737
 
747
738
    char *filename;
748
 
    ret = asprintf(&filename, "%s/%s", plugindir, dirst->d_name);
 
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
    }
749
744
    if(ret < 0){
750
745
      perror("asprintf");
751
746
      continue;
851
846
        perror("sigaction");
852
847
        _exit(EXIT_FAILURE);
853
848
      }
854
 
      ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
 
849
      ret = sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
855
850
      if(ret < 0){
856
851
        perror("sigprocmask");
857
852
        _exit(EXIT_FAILURE);
858
853
      }
859
 
 
 
854
      
860
855
      ret = dup2(pipefd[1], STDOUT_FILENO); /* replace our stdout */
861
856
      if(ret == -1){
862
857
        perror("dup2");
912
907
    if (maxfd < new_plugin->fd){
913
908
      maxfd = new_plugin->fd;
914
909
    }
915
 
    
916
910
  }
917
911
  
918
912
  closedir(dir);
919
913
  dir = NULL;
920
 
 
 
914
  
921
915
  for(plugin *p = plugin_list; p != NULL; p = p->next){
922
916
    if(p->pid != 0){
923
917
      break;
928
922
      free_plugin_list();
929
923
    }
930
924
  }
931
 
 
 
925
  
932
926
  /* Main loop while running plugins exist */
933
927
  while(plugin_list){
934
928
    fd_set rfds = rfds_all;
940
934
    }
941
935
    /* OK, now either a process completed, or something can be read
942
936
       from one of them */
943
 
    for(plugin *proc = plugin_list; proc != NULL; proc = proc->next){
 
937
    for(plugin *proc = plugin_list; proc != NULL;){
944
938
      /* Is this process completely done? */
945
939
      if(proc->eof and proc->completed){
946
940
        /* Only accept the plugin output if it exited cleanly */
973
967
            exitstatus = EXIT_FAILURE;
974
968
            goto fallback;
975
969
          }
 
970
          
 
971
          plugin *next_plugin = proc->next;
976
972
          free_plugin(proc);
 
973
          proc = next_plugin;
 
974
          
977
975
          /* We are done modifying process list, so unblock signal */
978
976
          ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask,
979
977
                             NULL);
986
984
          if(plugin_list == NULL){
987
985
            break;
988
986
          }
 
987
          
989
988
          continue;
990
989
        }
991
990
        
992
991
        /* This process exited nicely, so print its buffer */
993
 
 
 
992
        
994
993
        bool bret = print_out_password(proc->buffer,
995
994
                                       proc->buffer_length);
996
995
        if(not bret){
1003
1002
      /* This process has not completed.  Does it have any output? */
1004
1003
      if(proc->eof or not FD_ISSET(proc->fd, &rfds)){
1005
1004
        /* This process had nothing to say at this time */
 
1005
        proc = proc->next;
1006
1006
        continue;
1007
1007
      }
1008
1008
      /* Before reading, make the process' data buffer large enough */
1021
1021
                 BUFFER_SIZE);
1022
1022
      if(ret < 0){
1023
1023
        /* Read error from this process; ignore the error */
 
1024
        proc = proc->next;
1024
1025
        continue;
1025
1026
      }
1026
1027
      if(ret == 0){
1041
1042
    bool bret;
1042
1043
    fprintf(stderr, "Going to fallback mode using getpass(3)\n");
1043
1044
    char *passwordbuffer = getpass("Password: ");
1044
 
    bret = print_out_password(passwordbuffer, strlen(passwordbuffer));
 
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);
1045
1052
    if(not bret){
1046
1053
      perror("print_out_password");
1047
1054
      exitstatus = EXIT_FAILURE;
1054
1061
    perror("sigaction");
1055
1062
    exitstatus = EXIT_FAILURE;
1056
1063
  }
1057
 
 
 
1064
  
1058
1065
  if(custom_argv != NULL){
1059
1066
    for(char **arg = custom_argv+1; *arg != NULL; arg++){
1060
1067
      free(*arg);
1066
1073
    closedir(dir);
1067
1074
  }
1068
1075
  
1069
 
  /* Free the process list and kill the processes */
 
1076
  /* Kill the processes */
1070
1077
  for(plugin *p = plugin_list; p != NULL; p = p->next){
1071
1078
    if(p->pid != 0){
1072
1079
      close(p->fd);
1085
1092
  if(errno != ECHILD){
1086
1093
    perror("wait");
1087
1094
  }
1088
 
 
 
1095
  
1089
1096
  free_plugin_list();
1090
1097
  
1091
1098
  free(plugindir);