/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
 
  if(length>0 and buffer[length-1] == '\n'){
259
 
    length--;
260
 
  }
261
256
  for(size_t written = 0; written < length; written += (size_t)ret){
262
257
    ret = TEMP_FAILURE_RETRY(write(STDOUT_FILENO, buffer + written,
263
258
                                   length - written));
314
309
  struct stat st;
315
310
  fd_set rfds_all;
316
311
  int ret, maxfd = 0;
 
312
  ssize_t sret;
317
313
  uid_t uid = 65534;
318
314
  gid_t gid = 65534;
319
315
  bool debug = false;
397
393
      if(arg == NULL){
398
394
        break;
399
395
      }
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
      if(not add_environment(getplugin(NULL), arg, true)){
 
397
        perror("add_environment");
408
398
      }
409
399
      break;
410
400
    case 'o':                   /* --options-for */
438
428
        if(envdef == NULL){
439
429
          break;
440
430
        }
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
        *envdef = '\0';
 
432
        if(not add_environment(getplugin(arg), envdef+1, true)){
447
433
          perror("add_environment");
448
434
        }
449
435
      }
467
453
      }
468
454
      break;
469
455
    case 128:                   /* --plugin-dir */
 
456
      free(plugindir);
470
457
      plugindir = strdup(arg);
471
458
      if(plugindir == NULL){
472
459
        perror("strdup");
476
463
      /* This is already done by parse_opt_config_file() */
477
464
      break;
478
465
    case 130:                   /* --userid */
479
 
      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
      }
480
472
      break;
481
473
    case 131:                   /* --groupid */
482
 
      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
      }
483
480
      break;
484
481
    case 132:                   /* --debug */
485
482
      debug = true;
486
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
 */
487
488
    case ARGP_KEY_ARG:
488
 
      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
      }
489
495
      break;
490
496
    case ARGP_KEY_END:
491
497
      break;
510
516
    case 128:                   /* --plugin-dir */
511
517
      break;
512
518
    case 129:                   /* --config-file */
 
519
      free(argfile);
513
520
      argfile = strdup(arg);
514
521
      if(argfile == NULL){
515
522
        perror("strdup");
554
561
    char *org_line = NULL;
555
562
    char *p, *arg, *new_arg, *line;
556
563
    size_t size = 0;
557
 
    ssize_t sret;
558
564
    const char whitespace_delims[] = " \r\t\f\v\n";
559
565
    const char comment_delim[] = "#";
560
566
 
709
715
      
710
716
      const char const *bad_suffixes[] = { "~", "#", ".dpkg-new",
711
717
                                           ".dpkg-old",
 
718
                                           ".dpkg-bak",
712
719
                                           ".dpkg-divert", NULL };
713
720
      for(const char **pre = bad_prefixes; *pre != NULL; pre++){
714
721
        size_t pre_len = strlen(*pre);
745
752
    }
746
753
 
747
754
    char *filename;
748
 
    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
    }
749
760
    if(ret < 0){
750
761
      perror("asprintf");
751
762
      continue;
851
862
        perror("sigaction");
852
863
        _exit(EXIT_FAILURE);
853
864
      }
854
 
      ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
 
865
      ret = sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
855
866
      if(ret < 0){
856
867
        perror("sigprocmask");
857
868
        _exit(EXIT_FAILURE);
858
869
      }
859
 
 
 
870
      
860
871
      ret = dup2(pipefd[1], STDOUT_FILENO); /* replace our stdout */
861
872
      if(ret == -1){
862
873
        perror("dup2");
912
923
    if (maxfd < new_plugin->fd){
913
924
      maxfd = new_plugin->fd;
914
925
    }
915
 
    
916
926
  }
917
927
  
918
928
  closedir(dir);
919
929
  dir = NULL;
920
 
 
 
930
  
921
931
  for(plugin *p = plugin_list; p != NULL; p = p->next){
922
932
    if(p->pid != 0){
923
933
      break;
928
938
      free_plugin_list();
929
939
    }
930
940
  }
931
 
 
 
941
  
932
942
  /* Main loop while running plugins exist */
933
943
  while(plugin_list){
934
944
    fd_set rfds = rfds_all;
940
950
    }
941
951
    /* OK, now either a process completed, or something can be read
942
952
       from one of them */
943
 
    for(plugin *proc = plugin_list; proc != NULL; proc = proc->next){
 
953
    for(plugin *proc = plugin_list; proc != NULL;){
944
954
      /* Is this process completely done? */
945
955
      if(proc->eof and proc->completed){
946
956
        /* Only accept the plugin output if it exited cleanly */
973
983
            exitstatus = EXIT_FAILURE;
974
984
            goto fallback;
975
985
          }
 
986
          
 
987
          plugin *next_plugin = proc->next;
976
988
          free_plugin(proc);
 
989
          proc = next_plugin;
 
990
          
977
991
          /* We are done modifying process list, so unblock signal */
978
992
          ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask,
979
993
                             NULL);
986
1000
          if(plugin_list == NULL){
987
1001
            break;
988
1002
          }
 
1003
          
989
1004
          continue;
990
1005
        }
991
1006
        
992
1007
        /* This process exited nicely, so print its buffer */
993
 
 
 
1008
        
994
1009
        bool bret = print_out_password(proc->buffer,
995
1010
                                       proc->buffer_length);
996
1011
        if(not bret){
1003
1018
      /* This process has not completed.  Does it have any output? */
1004
1019
      if(proc->eof or not FD_ISSET(proc->fd, &rfds)){
1005
1020
        /* This process had nothing to say at this time */
 
1021
        proc = proc->next;
1006
1022
        continue;
1007
1023
      }
1008
1024
      /* Before reading, make the process' data buffer large enough */
1017
1033
        proc->buffer_size += BUFFER_SIZE;
1018
1034
      }
1019
1035
      /* Read from the process */
1020
 
      ret = read(proc->fd, proc->buffer + proc->buffer_length,
1021
 
                 BUFFER_SIZE);
1022
 
      if(ret < 0){
 
1036
      sret = read(proc->fd, proc->buffer + proc->buffer_length,
 
1037
                  BUFFER_SIZE);
 
1038
      if(sret < 0){
1023
1039
        /* Read error from this process; ignore the error */
 
1040
        proc = proc->next;
1024
1041
        continue;
1025
1042
      }
1026
 
      if(ret == 0){
 
1043
      if(sret == 0){
1027
1044
        /* got EOF */
1028
1045
        proc->eof = true;
1029
1046
      } else {
1030
 
        proc->buffer_length += (size_t) ret;
 
1047
        proc->buffer_length += (size_t) sret;
1031
1048
      }
1032
1049
    }
1033
1050
  }
1041
1058
    bool bret;
1042
1059
    fprintf(stderr, "Going to fallback mode using getpass(3)\n");
1043
1060
    char *passwordbuffer = getpass("Password: ");
1044
 
    bret = print_out_password(passwordbuffer, strlen(passwordbuffer));
 
1061
    size_t len = strlen(passwordbuffer);
 
1062
    /* Strip trailing newline */
 
1063
    if(len > 0 and passwordbuffer[len-1] == '\n'){
 
1064
      passwordbuffer[len-1] = '\0'; /* not strictly necessary */
 
1065
      len--;
 
1066
    }
 
1067
    bret = print_out_password(passwordbuffer, len);
1045
1068
    if(not bret){
1046
1069
      perror("print_out_password");
1047
1070
      exitstatus = EXIT_FAILURE;
1054
1077
    perror("sigaction");
1055
1078
    exitstatus = EXIT_FAILURE;
1056
1079
  }
1057
 
 
 
1080
  
1058
1081
  if(custom_argv != NULL){
1059
1082
    for(char **arg = custom_argv+1; *arg != NULL; arg++){
1060
1083
      free(*arg);
1066
1089
    closedir(dir);
1067
1090
  }
1068
1091
  
1069
 
  /* Free the process list and kill the processes */
 
1092
  /* Kill the processes */
1070
1093
  for(plugin *p = plugin_list; p != NULL; p = p->next){
1071
1094
    if(p->pid != 0){
1072
1095
      close(p->fd);
1085
1108
  if(errno != ECHILD){
1086
1109
    perror("wait");
1087
1110
  }
1088
 
 
 
1111
  
1089
1112
  free_plugin_list();
1090
1113
  
1091
1114
  free(plugindir);