/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: 2009-01-13 01:36:46 UTC
  • Revision ID: teddy@fukt.bsnet.se-20090113013646-1u21ie2oaotcekdz
* plugin-runner.c (main): Use "sscanf" instead of "strtol"; using the
                          correct type instead of casting.
* plugins.d/mandos-cliend.c (main): Detect an empty string when
                                    parsing DH bits and port number.
* plugins.d/splashy.c (main): Use "sscanf" instead of "strtoul"; using
                              the correct type instead of casting.
* plugins.d/usplash.c (main): - '' -

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,2009 Teddy Hogeborn
 
6
 * Copyright © 2008,2009 Björn Påhlsson
6
7
 * 
7
8
 * This program is free software: you can redistribute it and/or
8
9
 * modify it under the terms of the GNU General Public License as
27
28
#include <stdlib.h>             /* malloc(), exit(), EXIT_FAILURE,
28
29
                                   EXIT_SUCCESS, realloc() */
29
30
#include <stdbool.h>            /* bool, true, false */
30
 
#include <stdio.h>              /* perror, popen(), fileno(),
31
 
                                   fprintf(), stderr, STDOUT_FILENO */
 
31
#include <stdio.h>              /* perror, fileno(), fprintf(),
 
32
                                   stderr, STDOUT_FILENO */
32
33
#include <sys/types.h>          /* DIR, opendir(), stat(), struct
33
34
                                   stat, waitpid(), WIFEXITED(),
34
35
                                   WEXITSTATUS(), wait(), pid_t,
46
47
                                   fcntl(), setuid(), setgid(),
47
48
                                   F_GETFD, F_SETFD, FD_CLOEXEC,
48
49
                                   access(), pipe(), fork(), close()
49
 
                                   dup2, STDOUT_FILENO, _exit(),
 
50
                                   dup2(), STDOUT_FILENO, _exit(),
50
51
                                   execv(), write(), read(),
51
52
                                   close() */
52
53
#include <fcntl.h>              /* fcntl(), F_GETFD, F_SETFD,
69
70
#define PDIR "/lib/mandos/plugins.d"
70
71
#define AFILE "/conf/conf.d/mandos/plugin-runner.conf"
71
72
 
72
 
const char *argp_program_version = "plugin-runner 1.0";
 
73
const char *argp_program_version = "plugin-runner " VERSION;
73
74
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
74
75
 
75
 
struct plugin;
76
 
 
77
76
typedef struct plugin{
78
77
  char *name;                   /* can be NULL or any plugin name */
79
78
  char **argv;
208
207
 * Descriptor Flags".
209
208
 * *Note File Descriptor Flags:(libc)Descriptor Flags.
210
209
 */
211
 
static int set_cloexec_flag(int fd)
212
 
{
 
210
static int set_cloexec_flag(int fd){
213
211
  int ret = fcntl(fd, F_GETFD, 0);
214
212
  /* If reading the flags failed, return error indication now. */
215
213
  if(ret < 0){
222
220
 
223
221
/* Mark processes as completed when they exit, and save their exit
224
222
   status. */
225
 
void handle_sigchld(__attribute__((unused)) int sig){
 
223
static void handle_sigchld(__attribute__((unused)) int sig){
226
224
  while(true){
227
225
    plugin *proc = plugin_list;
228
226
    int status;
253
251
}
254
252
 
255
253
/* Prints out a password to stdout */
256
 
bool print_out_password(const char *buffer, size_t length){
 
254
static bool print_out_password(const char *buffer, size_t length){
257
255
  ssize_t ret;
258
256
  for(size_t written = 0; written < length; written += (size_t)ret){
259
257
    ret = TEMP_FAILURE_RETRY(write(STDOUT_FILENO, buffer + written,
311
309
  struct stat st;
312
310
  fd_set rfds_all;
313
311
  int ret, maxfd = 0;
 
312
  ssize_t sret;
314
313
  uid_t uid = 65534;
315
314
  gid_t gid = 65534;
316
315
  bool debug = false;
394
393
      if(arg == NULL){
395
394
        break;
396
395
      }
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
      if(not add_environment(getplugin(NULL), arg, true)){
 
397
        perror("add_environment");
405
398
      }
406
399
      break;
407
400
    case 'o':                   /* --options-for */
435
428
        if(envdef == NULL){
436
429
          break;
437
430
        }
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
        *envdef = '\0';
 
432
        if(not add_environment(getplugin(arg), envdef+1, true)){
444
433
          perror("add_environment");
445
434
        }
446
435
      }
464
453
      }
465
454
      break;
466
455
    case 128:                   /* --plugin-dir */
 
456
      free(plugindir);
467
457
      plugindir = strdup(arg);
468
458
      if(plugindir == NULL){
469
459
        perror("strdup");
473
463
      /* This is already done by parse_opt_config_file() */
474
464
      break;
475
465
    case 130:                   /* --userid */
476
 
      uid = (uid_t)strtol(arg, NULL, 10);
 
466
      /* In the GNU C library, uid_t is always unsigned int */
 
467
      ret = sscanf(arg, "%ud", &uid);
 
468
      if(ret != 1){
 
469
        fprintf(stderr, "Bad user ID number: \"%s\", using %ud\n",
 
470
                arg, uid);
 
471
      }
477
472
      break;
478
473
    case 131:                   /* --groupid */
479
 
      gid = (gid_t)strtol(arg, NULL, 10);
 
474
      /* In the GNU C library, gid_t is always unsigned int */
 
475
      ret = sscanf(arg, "%ud", &gid);
 
476
      if(ret != 1){
 
477
        fprintf(stderr, "Bad group ID number: \"%s\", using %ud\n",
 
478
                arg, gid);
 
479
      }
480
480
      break;
481
481
    case 132:                   /* --debug */
482
482
      debug = true;
483
483
      break;
 
484
/*
 
485
 * When adding more options before this line, remember to also add a
 
486
 * "case" to the "parse_opt_config_file" function below.
 
487
 */
484
488
    case ARGP_KEY_ARG:
485
 
      fprintf(stderr, "Ignoring unknown argument \"%s\"\n", arg);
 
489
      /* Cryptsetup always passes an argument, which is an empty
 
490
         string if "none" was specified in /etc/crypttab.  So if
 
491
         argument was empty, we ignore it silently. */
 
492
      if(arg[0] != '\0'){
 
493
        fprintf(stderr, "Ignoring unknown argument \"%s\"\n", arg);
 
494
      }
486
495
      break;
487
496
    case ARGP_KEY_END:
488
497
      break;
507
516
    case 128:                   /* --plugin-dir */
508
517
      break;
509
518
    case 129:                   /* --config-file */
 
519
      free(argfile);
510
520
      argfile = strdup(arg);
511
521
      if(argfile == NULL){
512
522
        perror("strdup");
551
561
    char *org_line = NULL;
552
562
    char *p, *arg, *new_arg, *line;
553
563
    size_t size = 0;
554
 
    ssize_t sret;
555
564
    const char whitespace_delims[] = " \r\t\f\v\n";
556
565
    const char comment_delim[] = "#";
557
566
 
706
715
      
707
716
      const char const *bad_suffixes[] = { "~", "#", ".dpkg-new",
708
717
                                           ".dpkg-old",
 
718
                                           ".dpkg-bak",
709
719
                                           ".dpkg-divert", NULL };
710
720
      for(const char **pre = bad_prefixes; *pre != NULL; pre++){
711
721
        size_t pre_len = strlen(*pre);
742
752
    }
743
753
 
744
754
    char *filename;
745
 
    ret = asprintf(&filename, "%s/%s", plugindir, dirst->d_name);
 
755
    if(plugindir == NULL){
 
756
      ret = asprintf(&filename, PDIR "/%s", dirst->d_name);
 
757
    } else {
 
758
      ret = asprintf(&filename, "%s/%s", plugindir, dirst->d_name);
 
759
    }
746
760
    if(ret < 0){
747
761
      perror("asprintf");
748
762
      continue;
848
862
        perror("sigaction");
849
863
        _exit(EXIT_FAILURE);
850
864
      }
851
 
      ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
 
865
      ret = sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
852
866
      if(ret < 0){
853
867
        perror("sigprocmask");
854
868
        _exit(EXIT_FAILURE);
855
869
      }
856
 
 
 
870
      
857
871
      ret = dup2(pipefd[1], STDOUT_FILENO); /* replace our stdout */
858
872
      if(ret == -1){
859
873
        perror("dup2");
909
923
    if (maxfd < new_plugin->fd){
910
924
      maxfd = new_plugin->fd;
911
925
    }
912
 
    
913
926
  }
914
927
  
915
928
  closedir(dir);
916
929
  dir = NULL;
917
 
 
 
930
  
918
931
  for(plugin *p = plugin_list; p != NULL; p = p->next){
919
932
    if(p->pid != 0){
920
933
      break;
925
938
      free_plugin_list();
926
939
    }
927
940
  }
928
 
 
 
941
  
929
942
  /* Main loop while running plugins exist */
930
943
  while(plugin_list){
931
944
    fd_set rfds = rfds_all;
937
950
    }
938
951
    /* OK, now either a process completed, or something can be read
939
952
       from one of them */
940
 
    for(plugin *proc = plugin_list; proc != NULL; proc = proc->next){
 
953
    for(plugin *proc = plugin_list; proc != NULL;){
941
954
      /* Is this process completely done? */
942
955
      if(proc->eof and proc->completed){
943
956
        /* Only accept the plugin output if it exited cleanly */
970
983
            exitstatus = EXIT_FAILURE;
971
984
            goto fallback;
972
985
          }
 
986
          
 
987
          plugin *next_plugin = proc->next;
973
988
          free_plugin(proc);
 
989
          proc = next_plugin;
 
990
          
974
991
          /* We are done modifying process list, so unblock signal */
975
992
          ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask,
976
993
                             NULL);
983
1000
          if(plugin_list == NULL){
984
1001
            break;
985
1002
          }
 
1003
          
986
1004
          continue;
987
1005
        }
988
1006
        
989
1007
        /* This process exited nicely, so print its buffer */
990
 
 
 
1008
        
991
1009
        bool bret = print_out_password(proc->buffer,
992
1010
                                       proc->buffer_length);
993
1011
        if(not bret){
1000
1018
      /* This process has not completed.  Does it have any output? */
1001
1019
      if(proc->eof or not FD_ISSET(proc->fd, &rfds)){
1002
1020
        /* This process had nothing to say at this time */
 
1021
        proc = proc->next;
1003
1022
        continue;
1004
1023
      }
1005
1024
      /* Before reading, make the process' data buffer large enough */
1014
1033
        proc->buffer_size += BUFFER_SIZE;
1015
1034
      }
1016
1035
      /* Read from the process */
1017
 
      ret = read(proc->fd, proc->buffer + proc->buffer_length,
1018
 
                 BUFFER_SIZE);
1019
 
      if(ret < 0){
 
1036
      sret = read(proc->fd, proc->buffer + proc->buffer_length,
 
1037
                  BUFFER_SIZE);
 
1038
      if(sret < 0){
1020
1039
        /* Read error from this process; ignore the error */
 
1040
        proc = proc->next;
1021
1041
        continue;
1022
1042
      }
1023
 
      if(ret == 0){
 
1043
      if(sret == 0){
1024
1044
        /* got EOF */
1025
1045
        proc->eof = true;
1026
1046
      } else {
1027
 
        proc->buffer_length += (size_t) ret;
 
1047
        proc->buffer_length += (size_t) sret;
1028
1048
      }
1029
1049
    }
1030
1050
  }
1057
1077
    perror("sigaction");
1058
1078
    exitstatus = EXIT_FAILURE;
1059
1079
  }
1060
 
 
 
1080
  
1061
1081
  if(custom_argv != NULL){
1062
1082
    for(char **arg = custom_argv+1; *arg != NULL; arg++){
1063
1083
      free(*arg);
1069
1089
    closedir(dir);
1070
1090
  }
1071
1091
  
1072
 
  /* Free the process list and kill the processes */
 
1092
  /* Kill the processes */
1073
1093
  for(plugin *p = plugin_list; p != NULL; p = p->next){
1074
1094
    if(p->pid != 0){
1075
1095
      close(p->fd);
1088
1108
  if(errno != ECHILD){
1089
1109
    perror("wait");
1090
1110
  }
1091
 
 
 
1111
  
1092
1112
  free_plugin_list();
1093
1113
  
1094
1114
  free(plugindir);