/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,
72
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");
736
745
    }
737
746
 
738
747
    char *filename;
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
 
    }
 
748
    ret = asprintf(&filename, "%s/%s", plugindir, dirst->d_name);
744
749
    if(ret < 0){
745
750
      perror("asprintf");
746
751
      continue;
846
851
        perror("sigaction");
847
852
        _exit(EXIT_FAILURE);
848
853
      }
849
 
      ret = sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
 
854
      ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
850
855
      if(ret < 0){
851
856
        perror("sigprocmask");
852
857
        _exit(EXIT_FAILURE);
853
858
      }
854
 
      
 
859
 
855
860
      ret = dup2(pipefd[1], STDOUT_FILENO); /* replace our stdout */
856
861
      if(ret == -1){
857
862
        perror("dup2");
907
912
    if (maxfd < new_plugin->fd){
908
913
      maxfd = new_plugin->fd;
909
914
    }
 
915
    
910
916
  }
911
917
  
912
918
  closedir(dir);
913
919
  dir = NULL;
914
 
  
 
920
 
915
921
  for(plugin *p = plugin_list; p != NULL; p = p->next){
916
922
    if(p->pid != 0){
917
923
      break;
922
928
      free_plugin_list();
923
929
    }
924
930
  }
925
 
  
 
931
 
926
932
  /* Main loop while running plugins exist */
927
933
  while(plugin_list){
928
934
    fd_set rfds = rfds_all;
934
940
    }
935
941
    /* OK, now either a process completed, or something can be read
936
942
       from one of them */
937
 
    for(plugin *proc = plugin_list; proc != NULL;){
 
943
    for(plugin *proc = plugin_list; proc != NULL; proc = proc->next){
938
944
      /* Is this process completely done? */
939
945
      if(proc->eof and proc->completed){
940
946
        /* Only accept the plugin output if it exited cleanly */
967
973
            exitstatus = EXIT_FAILURE;
968
974
            goto fallback;
969
975
          }
970
 
          
971
 
          plugin *next_plugin = proc->next;
972
976
          free_plugin(proc);
973
 
          proc = next_plugin;
974
 
          
975
977
          /* We are done modifying process list, so unblock signal */
976
978
          ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask,
977
979
                             NULL);
984
986
          if(plugin_list == NULL){
985
987
            break;
986
988
          }
987
 
          
988
989
          continue;
989
990
        }
990
991
        
991
992
        /* This process exited nicely, so print its buffer */
992
 
        
 
993
 
993
994
        bool bret = print_out_password(proc->buffer,
994
995
                                       proc->buffer_length);
995
996
        if(not bret){
1002
1003
      /* This process has not completed.  Does it have any output? */
1003
1004
      if(proc->eof or not FD_ISSET(proc->fd, &rfds)){
1004
1005
        /* 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;
1025
1024
        continue;
1026
1025
      }
1027
1026
      if(ret == 0){
1042
1041
    bool bret;
1043
1042
    fprintf(stderr, "Going to fallback mode using getpass(3)\n");
1044
1043
    char *passwordbuffer = getpass("Password: ");
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);
 
1044
    bret = print_out_password(passwordbuffer, strlen(passwordbuffer));
1052
1045
    if(not bret){
1053
1046
      perror("print_out_password");
1054
1047
      exitstatus = EXIT_FAILURE;
1061
1054
    perror("sigaction");
1062
1055
    exitstatus = EXIT_FAILURE;
1063
1056
  }
1064
 
  
 
1057
 
1065
1058
  if(custom_argv != NULL){
1066
1059
    for(char **arg = custom_argv+1; *arg != NULL; arg++){
1067
1060
      free(*arg);
1073
1066
    closedir(dir);
1074
1067
  }
1075
1068
  
1076
 
  /* Kill the processes */
 
1069
  /* Free the process list and kill the processes */
1077
1070
  for(plugin *p = plugin_list; p != NULL; p = p->next){
1078
1071
    if(p->pid != 0){
1079
1072
      close(p->fd);
1092
1085
  if(errno != ECHILD){
1093
1086
    perror("wait");
1094
1087
  }
1095
 
  
 
1088
 
1096
1089
  free_plugin_list();
1097
1090
  
1098
1091
  free(plugindir);