/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-12-10 01:26:02 UTC
  • mfrom: (237.1.2 mandos)
  • Revision ID: teddy@fukt.bsnet.se-20081210012602-vhz3h75xkj24t340
First version of a somewhat complete D-Bus server interface.  Also
change user/group name to "_mandos".

* debian/mandos.postinst: Rename old "mandos" user and group to
                          "_mandos"; create "_mandos" user and group
                          if none exist.
* debian/mandos-client.postinst: - '' -

* initramfs-tools-hook: Try "_mandos" before "mandos" as user and
                        group name.

* mandos (_datetime_to_dbus_struct): New; was previously local.
  (Client.started): Renamed to "last_started".  All users changed.
  (Client.started): New; boolean.
  (Client.dbus_object_path): New.
  (Client.check_command): Renamed to "checker_command".  All users
                          changed.
  (Client.__init__): Set and use "self.dbus_object_path".  Set
                     "self.started".
  (Client.start): Update "self.started".  Emit "self.PropertyChanged"
                  signals for both "started" and "last_started".
  (Client.stop): Update "self.started".  Emit "self.PropertyChanged"
                 signal for "started".
  (Client.checker_callback): Take additional "command" argument.  All
                             callers changed. Emit
                             "self.PropertyChanged" signal.
  (Client.bump_timeout): Emit "self.PropertyChanged" signal for
                         "last_checked_ok".
  (Client.start_checker): Emit "self.PropertyChanged" signal for
                          "checker_running".
  (Client.stop_checker): Emit "self.PropertyChanged" signal for
                         "checker_running".
  (Client.still_valid): Bug fix: use "getattr(self, started, False)"
                        instead of "self.started" in case this client
                        object is so new that the "started" attribute
                        has not been created yet.
  (Client.IntervalChanged, Client.CheckerIsRunning, Client.GetChecker,
  Client.GetCreated, Client.GetFingerprint, Client.GetHost,
  Client.GetInterval, Client.GetName, Client.GetStarted,
  Client.GetTimeout, Client.StateChanged, Client.TimeoutChanged):
  Removed; all callers changed.
  (Client.CheckerCompleted): Add "condition" and "command" arguments.
                             All callers changed.
  (Client.GetAllProperties, Client.PropertyChanged): New.
  (Client.StillValid): Renamed to "IsStillValid".
  (Client.StartChecker): Changed to its own function to avoid the
                         return value from "Client.start_checker()".
  (Client.Stop): Changed to its own function to avoid the return value
                 from "Client.stop()".
  (main): Try "_mandos" before "mandos" as user and group name.
          Removed inner function "remove_from_clients".  New inner
          class "MandosServer".

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 Teddy Hogeborn
 
6
 * Copyright © 2008 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));
397
392
      if(arg == NULL){
398
393
        break;
399
394
      }
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
 
        }
 
395
      if(not add_environment(getplugin(NULL), arg, true)){
 
396
        perror("add_environment");
408
397
      }
409
398
      break;
410
399
    case 'o':                   /* --options-for */
438
427
        if(envdef == NULL){
439
428
          break;
440
429
        }
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)){
 
430
        *envdef = '\0';
 
431
        if(not add_environment(getplugin(arg), envdef+1, true)){
447
432
          perror("add_environment");
448
433
        }
449
434
      }
467
452
      }
468
453
      break;
469
454
    case 128:                   /* --plugin-dir */
 
455
      free(plugindir);
470
456
      plugindir = strdup(arg);
471
457
      if(plugindir == NULL){
472
458
        perror("strdup");
485
471
      debug = true;
486
472
      break;
487
473
    case ARGP_KEY_ARG:
488
 
      fprintf(stderr, "Ignoring unknown argument \"%s\"\n", arg);
 
474
      /* Cryptsetup always passes an argument, which is an empty
 
475
         string if "none" was specified in /etc/crypttab.  So if
 
476
         argument was empty, we ignore it silently. */
 
477
      if(arg[0] != '\0'){
 
478
        fprintf(stderr, "Ignoring unknown argument \"%s\"\n", arg);
 
479
      }
489
480
      break;
490
481
    case ARGP_KEY_END:
491
482
      break;
510
501
    case 128:                   /* --plugin-dir */
511
502
      break;
512
503
    case 129:                   /* --config-file */
 
504
      free(argfile);
513
505
      argfile = strdup(arg);
514
506
      if(argfile == NULL){
515
507
        perror("strdup");
709
701
      
710
702
      const char const *bad_suffixes[] = { "~", "#", ".dpkg-new",
711
703
                                           ".dpkg-old",
 
704
                                           ".dpkg-bak",
712
705
                                           ".dpkg-divert", NULL };
713
706
      for(const char **pre = bad_prefixes; *pre != NULL; pre++){
714
707
        size_t pre_len = strlen(*pre);
745
738
    }
746
739
 
747
740
    char *filename;
748
 
    ret = asprintf(&filename, "%s/%s", plugindir, dirst->d_name);
 
741
    if(plugindir == NULL){
 
742
      ret = asprintf(&filename, PDIR "/%s", dirst->d_name);
 
743
    } else {
 
744
      ret = asprintf(&filename, "%s/%s", plugindir, dirst->d_name);
 
745
    }
749
746
    if(ret < 0){
750
747
      perror("asprintf");
751
748
      continue;
851
848
        perror("sigaction");
852
849
        _exit(EXIT_FAILURE);
853
850
      }
854
 
      ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
 
851
      ret = sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
855
852
      if(ret < 0){
856
853
        perror("sigprocmask");
857
854
        _exit(EXIT_FAILURE);
858
855
      }
859
 
 
 
856
      
860
857
      ret = dup2(pipefd[1], STDOUT_FILENO); /* replace our stdout */
861
858
      if(ret == -1){
862
859
        perror("dup2");
912
909
    if (maxfd < new_plugin->fd){
913
910
      maxfd = new_plugin->fd;
914
911
    }
915
 
    
916
912
  }
917
913
  
918
914
  closedir(dir);
919
915
  dir = NULL;
920
 
 
 
916
  
921
917
  for(plugin *p = plugin_list; p != NULL; p = p->next){
922
918
    if(p->pid != 0){
923
919
      break;
928
924
      free_plugin_list();
929
925
    }
930
926
  }
931
 
 
 
927
  
932
928
  /* Main loop while running plugins exist */
933
929
  while(plugin_list){
934
930
    fd_set rfds = rfds_all;
940
936
    }
941
937
    /* OK, now either a process completed, or something can be read
942
938
       from one of them */
943
 
    for(plugin *proc = plugin_list; proc != NULL; proc = proc->next){
 
939
    for(plugin *proc = plugin_list; proc != NULL;){
944
940
      /* Is this process completely done? */
945
941
      if(proc->eof and proc->completed){
946
942
        /* Only accept the plugin output if it exited cleanly */
973
969
            exitstatus = EXIT_FAILURE;
974
970
            goto fallback;
975
971
          }
 
972
          
 
973
          plugin *next_plugin = proc->next;
976
974
          free_plugin(proc);
 
975
          proc = next_plugin;
 
976
          
977
977
          /* We are done modifying process list, so unblock signal */
978
978
          ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask,
979
979
                             NULL);
986
986
          if(plugin_list == NULL){
987
987
            break;
988
988
          }
 
989
          
989
990
          continue;
990
991
        }
991
992
        
992
993
        /* This process exited nicely, so print its buffer */
993
 
 
 
994
        
994
995
        bool bret = print_out_password(proc->buffer,
995
996
                                       proc->buffer_length);
996
997
        if(not bret){
1003
1004
      /* This process has not completed.  Does it have any output? */
1004
1005
      if(proc->eof or not FD_ISSET(proc->fd, &rfds)){
1005
1006
        /* This process had nothing to say at this time */
 
1007
        proc = proc->next;
1006
1008
        continue;
1007
1009
      }
1008
1010
      /* Before reading, make the process' data buffer large enough */
1021
1023
                 BUFFER_SIZE);
1022
1024
      if(ret < 0){
1023
1025
        /* Read error from this process; ignore the error */
 
1026
        proc = proc->next;
1024
1027
        continue;
1025
1028
      }
1026
1029
      if(ret == 0){
1041
1044
    bool bret;
1042
1045
    fprintf(stderr, "Going to fallback mode using getpass(3)\n");
1043
1046
    char *passwordbuffer = getpass("Password: ");
1044
 
    bret = print_out_password(passwordbuffer, strlen(passwordbuffer));
 
1047
    size_t len = strlen(passwordbuffer);
 
1048
    /* Strip trailing newline */
 
1049
    if(len > 0 and passwordbuffer[len-1] == '\n'){
 
1050
      passwordbuffer[len-1] = '\0'; /* not strictly necessary */
 
1051
      len--;
 
1052
    }
 
1053
    bret = print_out_password(passwordbuffer, len);
1045
1054
    if(not bret){
1046
1055
      perror("print_out_password");
1047
1056
      exitstatus = EXIT_FAILURE;
1054
1063
    perror("sigaction");
1055
1064
    exitstatus = EXIT_FAILURE;
1056
1065
  }
1057
 
 
 
1066
  
1058
1067
  if(custom_argv != NULL){
1059
1068
    for(char **arg = custom_argv+1; *arg != NULL; arg++){
1060
1069
      free(*arg);
1066
1075
    closedir(dir);
1067
1076
  }
1068
1077
  
1069
 
  /* Free the process list and kill the processes */
 
1078
  /* Kill the processes */
1070
1079
  for(plugin *p = plugin_list; p != NULL; p = p->next){
1071
1080
    if(p->pid != 0){
1072
1081
      close(p->fd);
1085
1094
  if(errno != ECHILD){
1086
1095
    perror("wait");
1087
1096
  }
1088
 
 
 
1097
  
1089
1098
  free_plugin_list();
1090
1099
  
1091
1100
  free(plugindir);