/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:
2
2
/*
3
3
 * Mandos plugin runner - Run Mandos plugins
4
4
 *
5
 
 * Copyright © 2008 Teddy Hogeborn
6
 
 * Copyright © 2008 Björn Påhlsson
 
5
 * Copyright © 2008,2009 Teddy Hogeborn
 
6
 * Copyright © 2008,2009 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
99
99
   or if none is found, creates a new one */
100
100
static plugin *getplugin(char *name){
101
101
  /* Check for exiting plugin with that name */
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))){
 
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))){
105
105
      return p;
106
106
    }
107
107
  }
108
108
  /* Create a new plugin */
109
109
  plugin *new_plugin = malloc(sizeof(plugin));
110
 
  if (new_plugin == NULL){
 
110
  if(new_plugin == NULL){
111
111
    return NULL;
112
112
  }
113
113
  char *copy_name = NULL;
124
124
                           .next = plugin_list };
125
125
  
126
126
  new_plugin->argv = malloc(sizeof(char *) * 2);
127
 
  if (new_plugin->argv == NULL){
 
127
  if(new_plugin->argv == NULL){
128
128
    free(copy_name);
129
129
    free(new_plugin);
130
130
    return NULL;
230
230
      break;
231
231
    }
232
232
    if(pid == -1){
233
 
      if (errno != ECHILD){
 
233
      if(errno != ECHILD){
234
234
        perror("waitpid");
235
235
      }
236
236
      /* No child processes */
309
309
  struct stat st;
310
310
  fd_set rfds_all;
311
311
  int ret, maxfd = 0;
 
312
  ssize_t sret;
312
313
  uid_t uid = 65534;
313
314
  gid_t gid = 65534;
314
315
  bool debug = false;
371
372
    { .name = NULL }
372
373
  };
373
374
  
374
 
  error_t parse_opt (int key, char *arg, __attribute__((unused))
375
 
                     struct argp_state *state) {
376
 
    switch (key) {
 
375
  error_t parse_opt(int key, char *arg, __attribute__((unused))
 
376
                    struct argp_state *state) {
 
377
    switch(key) {
377
378
    case 'g':                   /* --global-options */
378
 
      if (arg != NULL){
 
379
      if(arg != NULL){
379
380
        char *p;
380
381
        while((p = strsep(&arg, ",")) != NULL){
381
382
          if(p[0] == '\0'){
397
398
      }
398
399
      break;
399
400
    case 'o':                   /* --options-for */
400
 
      if (arg != NULL){
 
401
      if(arg != NULL){
401
402
        char *p_name = strsep(&arg, ":");
402
403
        if(p_name[0] == '\0' or arg == NULL){
403
404
          break;
434
435
      }
435
436
      break;
436
437
    case 'd':                   /* --disable */
437
 
      if (arg != NULL){
 
438
      if(arg != NULL){
438
439
        plugin *p = getplugin(arg);
439
440
        if(p == NULL){
440
441
          return ARGP_ERR_UNKNOWN;
443
444
      }
444
445
      break;
445
446
    case 'e':                   /* --enable */
446
 
      if (arg != NULL){
 
447
      if(arg != NULL){
447
448
        plugin *p = getplugin(arg);
448
449
        if(p == NULL){
449
450
          return ARGP_ERR_UNKNOWN;
462
463
      /* This is already done by parse_opt_config_file() */
463
464
      break;
464
465
    case 130:                   /* --userid */
465
 
      uid = (uid_t)strtol(arg, NULL, 10);
 
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);
 
471
      }
466
472
      break;
467
473
    case 131:                   /* --groupid */
468
 
      gid = (gid_t)strtol(arg, NULL, 10);
 
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);
 
479
      }
469
480
      break;
470
481
    case 132:                   /* --debug */
471
482
      debug = true;
472
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
 */
473
488
    case ARGP_KEY_ARG:
474
489
      /* Cryptsetup always passes an argument, which is an empty
475
490
         string if "none" was specified in /etc/crypttab.  So if
488
503
  
489
504
  /* This option parser is the same as parse_opt() above, except it
490
505
     ignores everything but the --config-file option. */
491
 
  error_t parse_opt_config_file (int key, char *arg,
492
 
                                 __attribute__((unused))
493
 
                                 struct argp_state *state) {
494
 
    switch (key) {
 
506
  error_t parse_opt_config_file(int key, char *arg,
 
507
                                __attribute__((unused))
 
508
                                struct argp_state *state) {
 
509
    switch(key) {
495
510
    case 'g':                   /* --global-options */
496
511
    case 'G':                   /* --global-env */
497
512
    case 'o':                   /* --options-for */
524
539
                       .args_doc = "",
525
540
                       .doc = "Mandos plugin runner -- Run plugins" };
526
541
  
527
 
  /* Parse using the parse_opt_config_file in order to get the custom
 
542
  /* Parse using parse_opt_config_file() in order to get the custom
528
543
     config file location, if any. */
529
 
  ret = argp_parse (&argp, argc, argv, ARGP_IN_ORDER, 0, NULL);
530
 
  if (ret == ARGP_ERR_UNKNOWN){
 
544
  ret = argp_parse(&argp, argc, argv, ARGP_IN_ORDER, 0, NULL);
 
545
  if(ret == ARGP_ERR_UNKNOWN){
531
546
    fprintf(stderr, "Unknown error while parsing arguments\n");
532
547
    exitstatus = EXIT_FAILURE;
533
548
    goto fallback;
537
552
  argp.parser = parse_opt;
538
553
  
539
554
  /* Open the configfile if available */
540
 
  if (argfile == NULL){
 
555
  if(argfile == NULL){
541
556
    conffp = fopen(AFILE, "r");
542
557
  } else {
543
558
    conffp = fopen(argfile, "r");
546
561
    char *org_line = NULL;
547
562
    char *p, *arg, *new_arg, *line;
548
563
    size_t size = 0;
549
 
    ssize_t sret;
550
564
    const char whitespace_delims[] = " \r\t\f\v\n";
551
565
    const char comment_delim[] = "#";
552
566
 
599
613
  } else {
600
614
    /* Check for harmful errors and go to fallback. Other errors might
601
615
       not affect opening plugins */
602
 
    if (errno == EMFILE or errno == ENFILE or errno == ENOMEM){
 
616
    if(errno == EMFILE or errno == ENFILE or errno == ENOMEM){
603
617
      perror("fopen");
604
618
      exitstatus = EXIT_FAILURE;
605
619
      goto fallback;
608
622
  /* If there was any arguments from configuration file,
609
623
     pass them to parser as command arguments */
610
624
  if(custom_argv != NULL){
611
 
    ret = argp_parse (&argp, custom_argc, custom_argv, ARGP_IN_ORDER,
612
 
                      0, NULL);
613
 
    if (ret == ARGP_ERR_UNKNOWN){
 
625
    ret = argp_parse(&argp, custom_argc, custom_argv, ARGP_IN_ORDER,
 
626
                     0, NULL);
 
627
    if(ret == ARGP_ERR_UNKNOWN){
614
628
      fprintf(stderr, "Unknown error while parsing arguments\n");
615
629
      exitstatus = EXIT_FAILURE;
616
630
      goto fallback;
619
633
  
620
634
  /* Parse actual command line arguments, to let them override the
621
635
     config file */
622
 
  ret = argp_parse (&argp, argc, argv, ARGP_IN_ORDER, 0, NULL);
623
 
  if (ret == ARGP_ERR_UNKNOWN){
 
636
  ret = argp_parse(&argp, argc, argv, ARGP_IN_ORDER, 0, NULL);
 
637
  if(ret == ARGP_ERR_UNKNOWN){
624
638
    fprintf(stderr, "Unknown error while parsing arguments\n");
625
639
    exitstatus = EXIT_FAILURE;
626
640
    goto fallback;
642
656
  
643
657
  /* Strip permissions down to nobody */
644
658
  ret = setuid(uid);
645
 
  if (ret == -1){
 
659
  if(ret == -1){
646
660
    perror("setuid");
647
661
  }  
648
662
  setgid(gid);
649
 
  if (ret == -1){
 
663
  if(ret == -1){
650
664
    perror("setgid");
651
665
  }
652
666
  
653
 
  if (plugindir == NULL){
 
667
  if(plugindir == NULL){
654
668
    dir = opendir(PDIR);
655
669
  } else {
656
670
    dir = opendir(plugindir);
683
697
    
684
698
    /* All directory entries have been processed */
685
699
    if(dirst == NULL){
686
 
      if (errno == EBADF){
 
700
      if(errno == EBADF){
687
701
        perror("readdir");
688
702
        exitstatus = EXIT_FAILURE;
689
703
        goto fallback;
749
763
    }
750
764
    
751
765
    ret = stat(filename, &st);
752
 
    if (ret == -1){
 
766
    if(ret == -1){
753
767
      perror("stat");
754
768
      free(filename);
755
769
      continue;
756
770
    }
757
771
 
758
772
    /* Ignore non-executable files */
759
 
    if (not S_ISREG(st.st_mode) or (access(filename, X_OK) != 0)){
 
773
    if(not S_ISREG(st.st_mode) or (access(filename, X_OK) != 0)){
760
774
      if(debug){
761
775
        fprintf(stderr, "Ignoring plugin dir entry \"%s\""
762
776
                " with bad type or mode\n", filename);
809
823
    
810
824
    int pipefd[2];
811
825
    ret = pipe(pipefd);
812
 
    if (ret == -1){
 
826
    if(ret == -1){
813
827
      perror("pipe");
814
828
      exitstatus = EXIT_FAILURE;
815
829
      goto fallback;
828
842
      goto fallback;
829
843
    }
830
844
    /* Block SIGCHLD until process is safely in process list */
831
 
    ret = sigprocmask (SIG_BLOCK, &sigchld_action.sa_mask, NULL);
 
845
    ret = sigprocmask(SIG_BLOCK, &sigchld_action.sa_mask, NULL);
832
846
    if(ret < 0){
833
847
      perror("sigprocmask");
834
848
      exitstatus = EXIT_FAILURE;
882
896
    close(pipefd[1]);           /* Close unused write end of pipe */
883
897
    free(filename);
884
898
    plugin *new_plugin = getplugin(dirst->d_name);
885
 
    if (new_plugin == NULL){
 
899
    if(new_plugin == NULL){
886
900
      perror("getplugin");
887
 
      ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
 
901
      ret = sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
888
902
      if(ret < 0){
889
903
        perror("sigprocmask");
890
904
      }
897
911
    
898
912
    /* Unblock SIGCHLD so signal handler can be run if this process
899
913
       has already completed */
900
 
    ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
 
914
    ret = sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
901
915
    if(ret < 0){
902
916
      perror("sigprocmask");
903
917
      exitstatus = EXIT_FAILURE;
906
920
    
907
921
    FD_SET(new_plugin->fd, &rfds_all);
908
922
    
909
 
    if (maxfd < new_plugin->fd){
 
923
    if(maxfd < new_plugin->fd){
910
924
      maxfd = new_plugin->fd;
911
925
    }
912
926
  }
929
943
  while(plugin_list){
930
944
    fd_set rfds = rfds_all;
931
945
    int select_ret = select(maxfd+1, &rfds, NULL, NULL, NULL);
932
 
    if (select_ret == -1){
 
946
    if(select_ret == -1){
933
947
      perror("select");
934
948
      exitstatus = EXIT_FAILURE;
935
949
      goto fallback;
954
968
                      (unsigned int) (proc->pid),
955
969
                      WTERMSIG(proc->status));
956
970
            } else if(WCOREDUMP(proc->status)){
957
 
              fprintf(stderr, "Plugin %d dumped core\n",
 
971
              fprintf(stderr, "Plugin %u dumped core\n",
958
972
                      (unsigned int) (proc->pid));
959
973
            }
960
974
          }
975
989
          proc = next_plugin;
976
990
          
977
991
          /* We are done modifying process list, so unblock signal */
978
 
          ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask,
979
 
                             NULL);
 
992
          ret = sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask,
 
993
                            NULL);
980
994
          if(ret < 0){
981
995
            perror("sigprocmask");
982
996
            exitstatus = EXIT_FAILURE;
1011
1025
      if(proc->buffer_length + BUFFER_SIZE > proc->buffer_size){
1012
1026
        proc->buffer = realloc(proc->buffer, proc->buffer_size
1013
1027
                               + (size_t) BUFFER_SIZE);
1014
 
        if (proc->buffer == NULL){
 
1028
        if(proc->buffer == NULL){
1015
1029
          perror("malloc");
1016
1030
          exitstatus = EXIT_FAILURE;
1017
1031
          goto fallback;
1019
1033
        proc->buffer_size += BUFFER_SIZE;
1020
1034
      }
1021
1035
      /* Read from the process */
1022
 
      ret = read(proc->fd, proc->buffer + proc->buffer_length,
1023
 
                 BUFFER_SIZE);
1024
 
      if(ret < 0){
 
1036
      sret = read(proc->fd, proc->buffer + proc->buffer_length,
 
1037
                  BUFFER_SIZE);
 
1038
      if(sret < 0){
1025
1039
        /* Read error from this process; ignore the error */
1026
1040
        proc = proc->next;
1027
1041
        continue;
1028
1042
      }
1029
 
      if(ret == 0){
 
1043
      if(sret == 0){
1030
1044
        /* got EOF */
1031
1045
        proc->eof = true;
1032
1046
      } else {
1033
 
        proc->buffer_length += (size_t) ret;
 
1047
        proc->buffer_length += (size_t) sret;
1034
1048
      }
1035
1049
    }
1036
1050
  }