/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-11-11 16:07:18 UTC
  • mto: (24.1.113 mandos)
  • mto: This revision was merged to the branch mainline in revision 238.
  • Revision ID: teddy@fukt.bsnet.se-20081111160718-gvqhs581md4inh5u
Further steps towards a D-Bus server interface, plus minor syntax
changes.

* mandos: Split copyright line.  Throughout, use parentheses instead
          of backslashes to continue long lines.
  (AvahiService.__init__): Use new "foo if bar else baz" syntax.
  (Client): All D-Bus methods renamed to be capitalized.
  (Client.__init__): Use D-Bus path "/Mandos/clients/%s".
  (Client.created): Changed to always be in UTC.  All users changed.
  (Client.started): Changed to be a "datetime.datetime()" in UTC.  All
                    users changed.
  (Client.stop): Bug fix: set "self.started" to "None".
  (Client.interface): Renamed to "_interface".
  (Client._datetime_to_dbus_struct): New temporary function.
  (Client.CheckerIsRunning): New D-Bus method.
  (Client.GetChecker): - '' -
  (Client.GetCreated): - '' -
  (Client.GetHost): - '' -
  (Client.GetStarted): - '' -
  (Client.SetHost): - '' -
  (Client.SetChecker): - '' -
  (Client.SetInterval): - '' -
  (Client.SetTimeout): - '' -
  (Client.StartChecker): - '' -
  (Client.TimeoutChanged): New D-Bus signal.
  (TCP_handler.handle): Use new "for...else" syntax.
  (main): Bug fix: get a D-Bus bus name.

* mandos-keygen: Split copyright line.
* plugin-runner.c: - '' -
* plugins.d/mandos-client: - '' -
* plugins.d/password-prompt.c: - '' -

* plugins.d/askpass-fifo.c: Added copyright statement and license.
* plugins.s/splashy.c: - '' -
* plugins.d/usplash.c: - '' -

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,2009 Teddy Hogeborn
6
 
 * Copyright © 2008,2009 Björn Påhlsson
 
5
 * Copyright © 2008 Teddy Hogeborn
 
6
 * Copyright © 2008 Björn Påhlsson
7
7
 * 
8
8
 * This program is free software: you can redistribute it and/or
9
9
 * modify it under the terms of the GNU General Public License as
64
64
                                   sigprocmask(), SIG_BLOCK, SIGCHLD,
65
65
                                   SIG_UNBLOCK, kill() */
66
66
#include <errno.h>              /* errno, EBADF */
67
 
#include <inttypes.h>           /* intmax_t, SCNdMAX, PRIdMAX,  */
68
67
 
69
68
#define BUFFER_SIZE 256
70
69
 
89
88
  size_t buffer_size;
90
89
  size_t buffer_length;
91
90
  bool eof;
92
 
  volatile sig_atomic_t completed;
93
 
  int status;
 
91
  volatile bool completed;
 
92
  volatile int status;
94
93
  struct plugin *next;
95
94
} plugin;
96
95
 
100
99
   or if none is found, creates a new one */
101
100
static plugin *getplugin(char *name){
102
101
  /* Check for exiting plugin with that name */
103
 
  for(plugin *p = plugin_list; p != NULL; p = p->next){
104
 
    if((p->name == name)
105
 
       or (p->name and name and (strcmp(p->name, name) == 0))){
 
102
  for (plugin *p = plugin_list; p != NULL; p = p->next){
 
103
    if ((p->name == name)
 
104
        or (p->name and name and (strcmp(p->name, name) == 0))){
106
105
      return p;
107
106
    }
108
107
  }
109
108
  /* Create a new plugin */
110
109
  plugin *new_plugin = malloc(sizeof(plugin));
111
 
  if(new_plugin == NULL){
 
110
  if (new_plugin == NULL){
112
111
    return NULL;
113
112
  }
114
113
  char *copy_name = NULL;
115
114
  if(name != NULL){
116
115
    copy_name = strdup(name);
117
116
    if(copy_name == NULL){
118
 
      free(new_plugin);
119
117
      return NULL;
120
118
    }
121
119
  }
122
120
  
123
 
  *new_plugin = (plugin){ .name = copy_name,
124
 
                          .argc = 1,
125
 
                          .disabled = false,
126
 
                          .next = plugin_list };
 
121
  *new_plugin = (plugin) { .name = copy_name,
 
122
                           .argc = 1,
 
123
                           .disabled = false,
 
124
                           .next = plugin_list };
127
125
  
128
126
  new_plugin->argv = malloc(sizeof(char *) * 2);
129
 
  if(new_plugin->argv == NULL){
 
127
  if (new_plugin->argv == NULL){
130
128
    free(copy_name);
131
129
    free(new_plugin);
132
130
    return NULL;
223
221
/* Mark processes as completed when they exit, and save their exit
224
222
   status. */
225
223
static void handle_sigchld(__attribute__((unused)) int sig){
226
 
  int old_errno = errno;
227
224
  while(true){
228
225
    plugin *proc = plugin_list;
229
226
    int status;
233
230
      break;
234
231
    }
235
232
    if(pid == -1){
236
 
      if(errno == ECHILD){
237
 
        /* No child processes */
238
 
        break;
 
233
      if (errno != ECHILD){
 
234
        perror("waitpid");
239
235
      }
240
 
      perror("waitpid");
 
236
      /* No child processes */
 
237
      break;
241
238
    }
242
239
    
243
240
    /* A child exited, find it in process_list */
249
246
      continue;
250
247
    }
251
248
    proc->status = status;
252
 
    proc->completed = 1;
 
249
    proc->completed = true;
253
250
  }
254
 
  errno = old_errno;
255
251
}
256
252
 
257
253
/* Prints out a password to stdout */
312
308
  struct dirent *dirst;
313
309
  struct stat st;
314
310
  fd_set rfds_all;
315
 
  int ret, numchars, maxfd = 0;
316
 
  ssize_t sret;
317
 
  intmax_t tmpmax;
 
311
  int ret, maxfd = 0;
318
312
  uid_t uid = 65534;
319
313
  gid_t gid = 65534;
320
314
  bool debug = false;
377
371
    { .name = NULL }
378
372
  };
379
373
  
380
 
  error_t parse_opt(int key, char *arg, __attribute__((unused))
381
 
                    struct argp_state *state){
382
 
    switch(key){
 
374
  error_t parse_opt (int key, char *arg, __attribute__((unused))
 
375
                     struct argp_state *state) {
 
376
    switch (key) {
383
377
    case 'g':                   /* --global-options */
384
 
      if(arg != NULL){
385
 
        char *plugin_option;
386
 
        while((plugin_option = strsep(&arg, ",")) != NULL){
387
 
          if(plugin_option[0] == '\0'){
 
378
      if (arg != NULL){
 
379
        char *p;
 
380
        while((p = strsep(&arg, ",")) != NULL){
 
381
          if(p[0] == '\0'){
388
382
            continue;
389
383
          }
390
 
          if(not add_argument(getplugin(NULL), plugin_option)){
 
384
          if(not add_argument(getplugin(NULL), p)){
391
385
            perror("add_argument");
392
386
            return ARGP_ERR_UNKNOWN;
393
387
          }
403
397
      }
404
398
      break;
405
399
    case 'o':                   /* --options-for */
406
 
      if(arg != NULL){
407
 
        char *plugin_name = strsep(&arg, ":");
408
 
        if(plugin_name[0] == '\0'){
409
 
          break;
410
 
        }
411
 
        char *plugin_option;
412
 
        while((plugin_option = strsep(&arg, ",")) != NULL){
413
 
          if(not add_argument(getplugin(plugin_name), plugin_option)){
 
400
      if (arg != NULL){
 
401
        char *p_name = strsep(&arg, ":");
 
402
        if(p_name[0] == '\0' or arg == NULL){
 
403
          break;
 
404
        }
 
405
        char *opt = strsep(&arg, ":");
 
406
        if(opt[0] == '\0' or opt == NULL){
 
407
          break;
 
408
        }
 
409
        char *p;
 
410
        while((p = strsep(&opt, ",")) != NULL){
 
411
          if(p[0] == '\0'){
 
412
            continue;
 
413
          }
 
414
          if(not add_argument(getplugin(p_name), p)){
414
415
            perror("add_argument");
415
416
            return ARGP_ERR_UNKNOWN;
416
417
          }
433
434
      }
434
435
      break;
435
436
    case 'd':                   /* --disable */
436
 
      if(arg != NULL){
 
437
      if (arg != NULL){
437
438
        plugin *p = getplugin(arg);
438
439
        if(p == NULL){
439
440
          return ARGP_ERR_UNKNOWN;
442
443
      }
443
444
      break;
444
445
    case 'e':                   /* --enable */
445
 
      if(arg != NULL){
 
446
      if (arg != NULL){
446
447
        plugin *p = getplugin(arg);
447
448
        if(p == NULL){
448
449
          return ARGP_ERR_UNKNOWN;
461
462
      /* This is already done by parse_opt_config_file() */
462
463
      break;
463
464
    case 130:                   /* --userid */
464
 
      ret = sscanf(arg, "%" SCNdMAX "%n", &tmpmax, &numchars);
465
 
      if(ret < 1 or tmpmax != (uid_t)tmpmax
466
 
         or arg[numchars] != '\0'){
467
 
        fprintf(stderr, "Bad user ID number: \"%s\", using %"
468
 
                PRIdMAX "\n", arg, (intmax_t)uid);
469
 
      } else {
470
 
        uid = (uid_t)tmpmax;
471
 
      }
 
465
      uid = (uid_t)strtol(arg, NULL, 10);
472
466
      break;
473
467
    case 131:                   /* --groupid */
474
 
      ret = sscanf(arg, "%" SCNdMAX "%n", &tmpmax, &numchars);
475
 
      if(ret < 1 or tmpmax != (gid_t)tmpmax
476
 
         or arg[numchars] != '\0'){
477
 
        fprintf(stderr, "Bad group ID number: \"%s\", using %"
478
 
                PRIdMAX "\n", arg, (intmax_t)gid);
479
 
      } else {
480
 
        gid = (gid_t)tmpmax;
481
 
      }
 
468
      gid = (gid_t)strtol(arg, NULL, 10);
482
469
      break;
483
470
    case 132:                   /* --debug */
484
471
      debug = true;
485
472
      break;
486
 
/*
487
 
 * When adding more options before this line, remember to also add a
488
 
 * "case" to the "parse_opt_config_file" function below.
489
 
 */
490
473
    case ARGP_KEY_ARG:
491
474
      /* Cryptsetup always passes an argument, which is an empty
492
475
         string if "none" was specified in /etc/crypttab.  So if
505
488
  
506
489
  /* This option parser is the same as parse_opt() above, except it
507
490
     ignores everything but the --config-file option. */
508
 
  error_t parse_opt_config_file(int key, char *arg,
509
 
                                __attribute__((unused))
510
 
                                struct argp_state *state){
511
 
    switch(key){
 
491
  error_t parse_opt_config_file (int key, char *arg,
 
492
                                 __attribute__((unused))
 
493
                                 struct argp_state *state) {
 
494
    switch (key) {
512
495
    case 'g':                   /* --global-options */
513
496
    case 'G':                   /* --global-env */
514
497
    case 'o':                   /* --options-for */
541
524
                       .args_doc = "",
542
525
                       .doc = "Mandos plugin runner -- Run plugins" };
543
526
  
544
 
  /* Parse using parse_opt_config_file() in order to get the custom
 
527
  /* Parse using the parse_opt_config_file in order to get the custom
545
528
     config file location, if any. */
546
 
  ret = argp_parse(&argp, argc, argv, ARGP_IN_ORDER, 0, NULL);
547
 
  if(ret == ARGP_ERR_UNKNOWN){
 
529
  ret = argp_parse (&argp, argc, argv, ARGP_IN_ORDER, 0, NULL);
 
530
  if (ret == ARGP_ERR_UNKNOWN){
548
531
    fprintf(stderr, "Unknown error while parsing arguments\n");
549
532
    exitstatus = EXIT_FAILURE;
550
533
    goto fallback;
554
537
  argp.parser = parse_opt;
555
538
  
556
539
  /* Open the configfile if available */
557
 
  if(argfile == NULL){
 
540
  if (argfile == NULL){
558
541
    conffp = fopen(AFILE, "r");
559
542
  } else {
560
543
    conffp = fopen(argfile, "r");
563
546
    char *org_line = NULL;
564
547
    char *p, *arg, *new_arg, *line;
565
548
    size_t size = 0;
 
549
    ssize_t sret;
566
550
    const char whitespace_delims[] = " \r\t\f\v\n";
567
551
    const char comment_delim[] = "#";
568
552
 
615
599
  } else {
616
600
    /* Check for harmful errors and go to fallback. Other errors might
617
601
       not affect opening plugins */
618
 
    if(errno == EMFILE or errno == ENFILE or errno == ENOMEM){
 
602
    if (errno == EMFILE or errno == ENFILE or errno == ENOMEM){
619
603
      perror("fopen");
620
604
      exitstatus = EXIT_FAILURE;
621
605
      goto fallback;
624
608
  /* If there was any arguments from configuration file,
625
609
     pass them to parser as command arguments */
626
610
  if(custom_argv != NULL){
627
 
    ret = argp_parse(&argp, custom_argc, custom_argv, ARGP_IN_ORDER,
628
 
                     0, NULL);
629
 
    if(ret == ARGP_ERR_UNKNOWN){
 
611
    ret = argp_parse (&argp, custom_argc, custom_argv, ARGP_IN_ORDER,
 
612
                      0, NULL);
 
613
    if (ret == ARGP_ERR_UNKNOWN){
630
614
      fprintf(stderr, "Unknown error while parsing arguments\n");
631
615
      exitstatus = EXIT_FAILURE;
632
616
      goto fallback;
635
619
  
636
620
  /* Parse actual command line arguments, to let them override the
637
621
     config file */
638
 
  ret = argp_parse(&argp, argc, argv, ARGP_IN_ORDER, 0, NULL);
639
 
  if(ret == ARGP_ERR_UNKNOWN){
 
622
  ret = argp_parse (&argp, argc, argv, ARGP_IN_ORDER, 0, NULL);
 
623
  if (ret == ARGP_ERR_UNKNOWN){
640
624
    fprintf(stderr, "Unknown error while parsing arguments\n");
641
625
    exitstatus = EXIT_FAILURE;
642
626
    goto fallback;
649
633
      for(char **a = p->argv; *a != NULL; a++){
650
634
        fprintf(stderr, "\tArg: %s\n", *a);
651
635
      }
652
 
      fprintf(stderr, "...and %d environment variables\n", p->envc);
 
636
      fprintf(stderr, "...and %u environment variables\n", p->envc);
653
637
      for(char **a = p->environ; *a != NULL; a++){
654
638
        fprintf(stderr, "\t%s\n", *a);
655
639
      }
657
641
  }
658
642
  
659
643
  /* Strip permissions down to nobody */
 
644
  ret = setuid(uid);
 
645
  if (ret == -1){
 
646
    perror("setuid");
 
647
  }  
660
648
  setgid(gid);
661
 
  if(ret == -1){
 
649
  if (ret == -1){
662
650
    perror("setgid");
663
651
  }
664
 
  ret = setuid(uid);
665
 
  if(ret == -1){
666
 
    perror("setuid");
667
 
  }
668
652
  
669
 
  if(plugindir == NULL){
 
653
  if (plugindir == NULL){
670
654
    dir = opendir(PDIR);
671
655
  } else {
672
656
    dir = opendir(plugindir);
699
683
    
700
684
    /* All directory entries have been processed */
701
685
    if(dirst == NULL){
702
 
      if(errno == EBADF){
 
686
      if (errno == EBADF){
703
687
        perror("readdir");
704
688
        exitstatus = EXIT_FAILURE;
705
689
        goto fallback;
765
749
    }
766
750
    
767
751
    ret = stat(filename, &st);
768
 
    if(ret == -1){
 
752
    if (ret == -1){
769
753
      perror("stat");
770
754
      free(filename);
771
755
      continue;
772
756
    }
773
757
 
774
758
    /* Ignore non-executable files */
775
 
    if(not S_ISREG(st.st_mode) or (access(filename, X_OK) != 0)){
 
759
    if (not S_ISREG(st.st_mode) or (access(filename, X_OK) != 0)){
776
760
      if(debug){
777
761
        fprintf(stderr, "Ignoring plugin dir entry \"%s\""
778
762
                " with bad type or mode\n", filename);
825
809
    
826
810
    int pipefd[2];
827
811
    ret = pipe(pipefd);
828
 
    if(ret == -1){
 
812
    if (ret == -1){
829
813
      perror("pipe");
830
814
      exitstatus = EXIT_FAILURE;
831
815
      goto fallback;
844
828
      goto fallback;
845
829
    }
846
830
    /* Block SIGCHLD until process is safely in process list */
847
 
    ret = sigprocmask(SIG_BLOCK, &sigchld_action.sa_mask, NULL);
 
831
    ret = sigprocmask (SIG_BLOCK, &sigchld_action.sa_mask, NULL);
848
832
    if(ret < 0){
849
833
      perror("sigprocmask");
850
834
      exitstatus = EXIT_FAILURE;
898
882
    close(pipefd[1]);           /* Close unused write end of pipe */
899
883
    free(filename);
900
884
    plugin *new_plugin = getplugin(dirst->d_name);
901
 
    if(new_plugin == NULL){
 
885
    if (new_plugin == NULL){
902
886
      perror("getplugin");
903
 
      ret = sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
 
887
      ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
904
888
      if(ret < 0){
905
889
        perror("sigprocmask");
906
890
      }
913
897
    
914
898
    /* Unblock SIGCHLD so signal handler can be run if this process
915
899
       has already completed */
916
 
    ret = sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
 
900
    ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
917
901
    if(ret < 0){
918
902
      perror("sigprocmask");
919
903
      exitstatus = EXIT_FAILURE;
922
906
    
923
907
    FD_SET(new_plugin->fd, &rfds_all);
924
908
    
925
 
    if(maxfd < new_plugin->fd){
 
909
    if (maxfd < new_plugin->fd){
926
910
      maxfd = new_plugin->fd;
927
911
    }
928
912
  }
929
913
  
930
914
  closedir(dir);
931
915
  dir = NULL;
932
 
  free_plugin(getplugin(NULL));
933
916
  
934
917
  for(plugin *p = plugin_list; p != NULL; p = p->next){
935
918
    if(p->pid != 0){
946
929
  while(plugin_list){
947
930
    fd_set rfds = rfds_all;
948
931
    int select_ret = select(maxfd+1, &rfds, NULL, NULL, NULL);
949
 
    if(select_ret == -1){
 
932
    if (select_ret == -1){
950
933
      perror("select");
951
934
      exitstatus = EXIT_FAILURE;
952
935
      goto fallback;
955
938
       from one of them */
956
939
    for(plugin *proc = plugin_list; proc != NULL;){
957
940
      /* Is this process completely done? */
958
 
      if(proc->completed and proc->eof){
 
941
      if(proc->eof and proc->completed){
959
942
        /* Only accept the plugin output if it exited cleanly */
960
943
        if(not WIFEXITED(proc->status)
961
944
           or WEXITSTATUS(proc->status) != 0){
963
946
 
964
947
          if(debug){
965
948
            if(WIFEXITED(proc->status)){
966
 
              fprintf(stderr, "Plugin %s [%" PRIdMAX "] exited with"
967
 
                      " status %d\n", proc->name,
968
 
                      (intmax_t) (proc->pid),
 
949
              fprintf(stderr, "Plugin %u exited with status %d\n",
 
950
                      (unsigned int) (proc->pid),
969
951
                      WEXITSTATUS(proc->status));
970
 
            } else if(WIFSIGNALED(proc->status)){
971
 
              fprintf(stderr, "Plugin %s [%" PRIdMAX "] killed by"
972
 
                      " signal %d\n", proc->name,
973
 
                      (intmax_t) (proc->pid),
 
952
            } else if(WIFSIGNALED(proc->status)) {
 
953
              fprintf(stderr, "Plugin %u killed by signal %d\n",
 
954
                      (unsigned int) (proc->pid),
974
955
                      WTERMSIG(proc->status));
975
956
            } else if(WCOREDUMP(proc->status)){
976
 
              fprintf(stderr, "Plugin %s [%" PRIdMAX "] dumped"
977
 
                      " core\n", proc->name, (intmax_t) (proc->pid));
 
957
              fprintf(stderr, "Plugin %d dumped core\n",
 
958
                      (unsigned int) (proc->pid));
978
959
            }
979
960
          }
980
961
          
994
975
          proc = next_plugin;
995
976
          
996
977
          /* We are done modifying process list, so unblock signal */
997
 
          ret = sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask,
998
 
                            NULL);
 
978
          ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask,
 
979
                             NULL);
999
980
          if(ret < 0){
1000
981
            perror("sigprocmask");
1001
982
            exitstatus = EXIT_FAILURE;
1030
1011
      if(proc->buffer_length + BUFFER_SIZE > proc->buffer_size){
1031
1012
        proc->buffer = realloc(proc->buffer, proc->buffer_size
1032
1013
                               + (size_t) BUFFER_SIZE);
1033
 
        if(proc->buffer == NULL){
 
1014
        if (proc->buffer == NULL){
1034
1015
          perror("malloc");
1035
1016
          exitstatus = EXIT_FAILURE;
1036
1017
          goto fallback;
1038
1019
        proc->buffer_size += BUFFER_SIZE;
1039
1020
      }
1040
1021
      /* Read from the process */
1041
 
      sret = read(proc->fd, proc->buffer + proc->buffer_length,
1042
 
                  BUFFER_SIZE);
1043
 
      if(sret < 0){
 
1022
      ret = read(proc->fd, proc->buffer + proc->buffer_length,
 
1023
                 BUFFER_SIZE);
 
1024
      if(ret < 0){
1044
1025
        /* Read error from this process; ignore the error */
1045
1026
        proc = proc->next;
1046
1027
        continue;
1047
1028
      }
1048
 
      if(sret == 0){
 
1029
      if(ret == 0){
1049
1030
        /* got EOF */
1050
1031
        proc->eof = true;
1051
1032
      } else {
1052
 
        proc->buffer_length += (size_t) sret;
 
1033
        proc->buffer_length += (size_t) ret;
1053
1034
      }
1054
1035
    }
1055
1036
  }