/mandos/release

To get this branch, use:
bzr branch http://bzr.recompile.se/loggerhead/mandos/release

« back to all changes in this revision

Viewing changes to plugin-runner.c

* plugin-runner.c: Only space changes.
* plugins.d/mandos-client.c: - '' -
* plugins.d/password-prompt.c: - '' -

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
 
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
127
  if(new_plugin->argv == 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;
 
311
  int ret, maxfd = 0;
316
312
  ssize_t sret;
317
 
  intmax_t tmpmax;
318
313
  uid_t uid = 65534;
319
314
  gid_t gid = 65534;
320
315
  bool debug = false;
378
373
  };
379
374
  
380
375
  error_t parse_opt(int key, char *arg, __attribute__((unused))
381
 
                    struct argp_state *state){
382
 
    switch(key){
 
376
                    struct argp_state *state) {
 
377
    switch(key) {
383
378
    case 'g':                   /* --global-options */
384
379
      if(arg != NULL){
385
380
        char *p;
468
463
      /* This is already done by parse_opt_config_file() */
469
464
      break;
470
465
    case 130:                   /* --userid */
471
 
      ret = sscanf(arg, "%" SCNdMAX "%n", &tmpmax, &numchars);
472
 
      if(ret < 1 or tmpmax != (uid_t)tmpmax
473
 
         or arg[numchars] != '\0'){
474
 
        fprintf(stderr, "Bad user ID number: \"%s\", using %"
475
 
                PRIdMAX "\n", arg, (intmax_t)uid);
476
 
      } else {
477
 
        uid = (uid_t)tmpmax;
 
466
      /* In the GNU C library, uid_t is always unsigned int */
 
467
      ret = sscanf(arg, "%u", &uid);
 
468
      if(ret != 1){
 
469
        fprintf(stderr, "Bad user ID number: \"%s\", using %u\n", arg,
 
470
                uid);
478
471
      }
479
472
      break;
480
473
    case 131:                   /* --groupid */
481
 
      ret = sscanf(arg, "%" SCNdMAX "%n", &tmpmax, &numchars);
482
 
      if(ret < 1 or tmpmax != (gid_t)tmpmax
483
 
         or arg[numchars] != '\0'){
484
 
        fprintf(stderr, "Bad group ID number: \"%s\", using %"
485
 
                PRIdMAX "\n", arg, (intmax_t)gid);
486
 
      } else {
487
 
        gid = (gid_t)tmpmax;
 
474
      /* In the GNU C library, gid_t is always unsigned int */
 
475
      ret = sscanf(arg, "%u", &gid);
 
476
      if(ret != 1){
 
477
        fprintf(stderr, "Bad group ID number: \"%s\", using %u\n",
 
478
                arg, gid);
488
479
      }
489
480
      break;
490
481
    case 132:                   /* --debug */
514
505
     ignores everything but the --config-file option. */
515
506
  error_t parse_opt_config_file(int key, char *arg,
516
507
                                __attribute__((unused))
517
 
                                struct argp_state *state){
518
 
    switch(key){
 
508
                                struct argp_state *state) {
 
509
    switch(key) {
519
510
    case 'g':                   /* --global-options */
520
511
    case 'G':                   /* --global-env */
521
512
    case 'o':                   /* --options-for */
656
647
      for(char **a = p->argv; *a != NULL; a++){
657
648
        fprintf(stderr, "\tArg: %s\n", *a);
658
649
      }
659
 
      fprintf(stderr, "...and %d environment variables\n", p->envc);
 
650
      fprintf(stderr, "...and %u environment variables\n", p->envc);
660
651
      for(char **a = p->environ; *a != NULL; a++){
661
652
        fprintf(stderr, "\t%s\n", *a);
662
653
      }
664
655
  }
665
656
  
666
657
  /* Strip permissions down to nobody */
 
658
  ret = setuid(uid);
 
659
  if(ret == -1){
 
660
    perror("setuid");
 
661
  }  
667
662
  setgid(gid);
668
663
  if(ret == -1){
669
664
    perror("setgid");
670
665
  }
671
 
  ret = setuid(uid);
672
 
  if(ret == -1){
673
 
    perror("setuid");
674
 
  }
675
666
  
676
667
  if(plugindir == NULL){
677
668
    dir = opendir(PDIR);
936
927
  
937
928
  closedir(dir);
938
929
  dir = NULL;
939
 
  free_plugin(getplugin(NULL));
940
930
  
941
931
  for(plugin *p = plugin_list; p != NULL; p = p->next){
942
932
    if(p->pid != 0){
962
952
       from one of them */
963
953
    for(plugin *proc = plugin_list; proc != NULL;){
964
954
      /* Is this process completely done? */
965
 
      if(proc->completed and proc->eof){
 
955
      if(proc->eof and proc->completed){
966
956
        /* Only accept the plugin output if it exited cleanly */
967
957
        if(not WIFEXITED(proc->status)
968
958
           or WEXITSTATUS(proc->status) != 0){
970
960
 
971
961
          if(debug){
972
962
            if(WIFEXITED(proc->status)){
973
 
              fprintf(stderr, "Plugin %" PRIdMAX " exited with status"
974
 
                      " %d\n", (intmax_t) (proc->pid),
 
963
              fprintf(stderr, "Plugin %u exited with status %d\n",
 
964
                      (unsigned int) (proc->pid),
975
965
                      WEXITSTATUS(proc->status));
976
 
            } else if(WIFSIGNALED(proc->status)){
977
 
              fprintf(stderr, "Plugin %" PRIdMAX " killed by signal"
978
 
                      " %d\n", (intmax_t) (proc->pid),
 
966
            } else if(WIFSIGNALED(proc->status)) {
 
967
              fprintf(stderr, "Plugin %u killed by signal %d\n",
 
968
                      (unsigned int) (proc->pid),
979
969
                      WTERMSIG(proc->status));
980
970
            } else if(WCOREDUMP(proc->status)){
981
 
              fprintf(stderr, "Plugin %" PRIdMAX " dumped core\n",
982
 
                      (intmax_t) (proc->pid));
 
971
              fprintf(stderr, "Plugin %u dumped core\n",
 
972
                      (unsigned int) (proc->pid));
983
973
            }
984
974
          }
985
975