/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-06 16:33:54 UTC
  • Revision ID: teddy@fukt.bsnet.se-20080906163354-x2l238fvbs2xashj
* .bzrignore: Rename "password-request" to "mandos-client".

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