/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 18:37:28 UTC
  • Revision ID: teddy@fukt.bsnet.se-20080905183728-l0m8v3vqa302uwrw
* mandos (main): Use EAFP with pidfile.

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;
 
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 */
426
438
        if(envdef == NULL){
427
439
          break;
428
440
        }
429
 
        *envdef = '\0';
430
 
        if(not add_environment(getplugin(arg), envdef+1, true)){
 
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)){
431
447
          perror("add_environment");
432
448
        }
433
449
      }
451
467
      }
452
468
      break;
453
469
    case 128:                   /* --plugin-dir */
454
 
      free(plugindir);
455
470
      plugindir = strdup(arg);
456
471
      if(plugindir == NULL){
457
472
        perror("strdup");
470
485
      debug = true;
471
486
      break;
472
487
    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
 
      }
 
488
      fprintf(stderr, "Ignoring unknown argument \"%s\"\n", arg);
479
489
      break;
480
490
    case ARGP_KEY_END:
481
491
      break;
500
510
    case 128:                   /* --plugin-dir */
501
511
      break;
502
512
    case 129:                   /* --config-file */
503
 
      free(argfile);
504
513
      argfile = strdup(arg);
505
514
      if(argfile == NULL){
506
515
        perror("strdup");
700
709
      
701
710
      const char const *bad_suffixes[] = { "~", "#", ".dpkg-new",
702
711
                                           ".dpkg-old",
703
 
                                           ".dpkg-bak",
704
712
                                           ".dpkg-divert", NULL };
705
713
      for(const char **pre = bad_prefixes; *pre != NULL; pre++){
706
714
        size_t pre_len = strlen(*pre);
737
745
    }
738
746
 
739
747
    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
 
    }
 
748
    ret = asprintf(&filename, "%s/%s", plugindir, dirst->d_name);
745
749
    if(ret < 0){
746
750
      perror("asprintf");
747
751
      continue;
847
851
        perror("sigaction");
848
852
        _exit(EXIT_FAILURE);
849
853
      }
850
 
      ret = sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
 
854
      ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
851
855
      if(ret < 0){
852
856
        perror("sigprocmask");
853
857
        _exit(EXIT_FAILURE);
854
858
      }
855
 
      
 
859
 
856
860
      ret = dup2(pipefd[1], STDOUT_FILENO); /* replace our stdout */
857
861
      if(ret == -1){
858
862
        perror("dup2");
908
912
    if (maxfd < new_plugin->fd){
909
913
      maxfd = new_plugin->fd;
910
914
    }
 
915
    
911
916
  }
912
917
  
913
918
  closedir(dir);
914
919
  dir = NULL;
915
 
  
 
920
 
916
921
  for(plugin *p = plugin_list; p != NULL; p = p->next){
917
922
    if(p->pid != 0){
918
923
      break;
923
928
      free_plugin_list();
924
929
    }
925
930
  }
926
 
  
 
931
 
927
932
  /* Main loop while running plugins exist */
928
933
  while(plugin_list){
929
934
    fd_set rfds = rfds_all;
935
940
    }
936
941
    /* OK, now either a process completed, or something can be read
937
942
       from one of them */
938
 
    for(plugin *proc = plugin_list; proc != NULL;){
 
943
    for(plugin *proc = plugin_list; proc != NULL; proc = proc->next){
939
944
      /* Is this process completely done? */
940
945
      if(proc->eof and proc->completed){
941
946
        /* Only accept the plugin output if it exited cleanly */
968
973
            exitstatus = EXIT_FAILURE;
969
974
            goto fallback;
970
975
          }
971
 
          
972
 
          plugin *next_plugin = proc->next;
973
976
          free_plugin(proc);
974
 
          proc = next_plugin;
975
 
          
976
977
          /* We are done modifying process list, so unblock signal */
977
978
          ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask,
978
979
                             NULL);
985
986
          if(plugin_list == NULL){
986
987
            break;
987
988
          }
988
 
          
989
989
          continue;
990
990
        }
991
991
        
992
992
        /* This process exited nicely, so print its buffer */
993
 
        
 
993
 
994
994
        bool bret = print_out_password(proc->buffer,
995
995
                                       proc->buffer_length);
996
996
        if(not bret){
1003
1003
      /* This process has not completed.  Does it have any output? */
1004
1004
      if(proc->eof or not FD_ISSET(proc->fd, &rfds)){
1005
1005
        /* This process had nothing to say at this time */
1006
 
        proc = proc->next;
1007
1006
        continue;
1008
1007
      }
1009
1008
      /* Before reading, make the process' data buffer large enough */
1022
1021
                 BUFFER_SIZE);
1023
1022
      if(ret < 0){
1024
1023
        /* Read error from this process; ignore the error */
1025
 
        proc = proc->next;
1026
1024
        continue;
1027
1025
      }
1028
1026
      if(ret == 0){
1043
1041
    bool bret;
1044
1042
    fprintf(stderr, "Going to fallback mode using getpass(3)\n");
1045
1043
    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);
 
1044
    bret = print_out_password(passwordbuffer, strlen(passwordbuffer));
1053
1045
    if(not bret){
1054
1046
      perror("print_out_password");
1055
1047
      exitstatus = EXIT_FAILURE;
1062
1054
    perror("sigaction");
1063
1055
    exitstatus = EXIT_FAILURE;
1064
1056
  }
1065
 
  
 
1057
 
1066
1058
  if(custom_argv != NULL){
1067
1059
    for(char **arg = custom_argv+1; *arg != NULL; arg++){
1068
1060
      free(*arg);
1074
1066
    closedir(dir);
1075
1067
  }
1076
1068
  
1077
 
  /* Kill the processes */
 
1069
  /* Free the process list and kill the processes */
1078
1070
  for(plugin *p = plugin_list; p != NULL; p = p->next){
1079
1071
    if(p->pid != 0){
1080
1072
      close(p->fd);
1093
1085
  if(errno != ECHILD){
1094
1086
    perror("wait");
1095
1087
  }
1096
 
  
 
1088
 
1097
1089
  free_plugin_list();
1098
1090
  
1099
1091
  free(plugindir);