/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

Merge from Björn:

* plugin-runner.c (main): Bug fix: For the "--options-for" option, do
                          not mangle arguments with colon characters.
                          Also support adding empty arguments.

* plugins.d/mandos-client.c: (mc): New global variable; moved from
                                   "main".
  (init_gpgme, pgp_packet_decrypt, init_gnutls_global,
  init_gnutls_session, start_mandos_communication): Removed "mc"
                                                    argument.  All
                                                    callers changed.

  (resolve_callback, browse_callback): Ignore "userdata" argument.
                                       All callers changed.
  (handle_sigterm): New function.
  (main): Add "handle_sigterm" as signal handler for SIGTERM before
          starting the main loop.

Show diffs side-by-side

added added

removed removed

Lines of Context:
89
89
  size_t buffer_size;
90
90
  size_t buffer_length;
91
91
  bool eof;
92
 
  volatile bool completed;
93
 
  volatile int status;
 
92
  volatile sig_atomic_t completed;
 
93
  int status;
94
94
  struct plugin *next;
95
95
} plugin;
96
96
 
120
120
    }
121
121
  }
122
122
  
123
 
  *new_plugin = (plugin) { .name = copy_name,
124
 
                           .argc = 1,
125
 
                           .disabled = false,
126
 
                           .next = plugin_list };
 
123
  *new_plugin = (plugin){ .name = copy_name,
 
124
                          .argc = 1,
 
125
                          .disabled = false,
 
126
                          .next = plugin_list };
127
127
  
128
128
  new_plugin->argv = malloc(sizeof(char *) * 2);
129
129
  if(new_plugin->argv == NULL){
223
223
/* Mark processes as completed when they exit, and save their exit
224
224
   status. */
225
225
static void handle_sigchld(__attribute__((unused)) int sig){
 
226
  int old_errno = errno;
226
227
  while(true){
227
228
    plugin *proc = plugin_list;
228
229
    int status;
232
233
      break;
233
234
    }
234
235
    if(pid == -1){
235
 
      if(errno != ECHILD){
236
 
        perror("waitpid");
 
236
      if(errno == ECHILD){
 
237
        /* No child processes */
 
238
        break;
237
239
      }
238
 
      /* No child processes */
239
 
      break;
 
240
      perror("waitpid");
240
241
    }
241
242
    
242
243
    /* A child exited, find it in process_list */
248
249
      continue;
249
250
    }
250
251
    proc->status = status;
251
 
    proc->completed = true;
 
252
    proc->completed = 1;
252
253
  }
 
254
  errno = old_errno;
253
255
}
254
256
 
255
257
/* Prints out a password to stdout */
376
378
  };
377
379
  
378
380
  error_t parse_opt(int key, char *arg, __attribute__((unused))
379
 
                    struct argp_state *state) {
380
 
    switch(key) {
 
381
                    struct argp_state *state){
 
382
    switch(key){
381
383
    case 'g':                   /* --global-options */
382
384
      if(arg != NULL){
383
 
        char *p;
384
 
        while((p = strsep(&arg, ",")) != NULL){
385
 
          if(p[0] == '\0'){
 
385
        char *plugin_option;
 
386
        while((plugin_option = strsep(&arg, ",")) != NULL){
 
387
          if(plugin_option[0] == '\0'){
386
388
            continue;
387
389
          }
388
 
          if(not add_argument(getplugin(NULL), p)){
 
390
          if(not add_argument(getplugin(NULL), plugin_option)){
389
391
            perror("add_argument");
390
392
            return ARGP_ERR_UNKNOWN;
391
393
          }
402
404
      break;
403
405
    case 'o':                   /* --options-for */
404
406
      if(arg != NULL){
405
 
        char *p_name = strsep(&arg, ":");
406
 
        if(p_name[0] == '\0' or arg == NULL){
407
 
          break;
408
 
        }
409
 
        char *opt = strsep(&arg, ":");
410
 
        if(opt[0] == '\0' or opt == NULL){
411
 
          break;
412
 
        }
413
 
        char *p;
414
 
        while((p = strsep(&opt, ",")) != NULL){
415
 
          if(p[0] == '\0'){
416
 
            continue;
417
 
          }
418
 
          if(not add_argument(getplugin(p_name), p)){
 
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)){
419
414
            perror("add_argument");
420
415
            return ARGP_ERR_UNKNOWN;
421
416
          }
512
507
     ignores everything but the --config-file option. */
513
508
  error_t parse_opt_config_file(int key, char *arg,
514
509
                                __attribute__((unused))
515
 
                                struct argp_state *state) {
516
 
    switch(key) {
 
510
                                struct argp_state *state){
 
511
    switch(key){
517
512
    case 'g':                   /* --global-options */
518
513
    case 'G':                   /* --global-env */
519
514
    case 'o':                   /* --options-for */
662
657
  }
663
658
  
664
659
  /* Strip permissions down to nobody */
 
660
  setgid(gid);
 
661
  if(ret == -1){
 
662
    perror("setgid");
 
663
  }
665
664
  ret = setuid(uid);
666
665
  if(ret == -1){
667
666
    perror("setuid");
668
 
  }  
669
 
  setgid(gid);
670
 
  if(ret == -1){
671
 
    perror("setgid");
672
667
  }
673
668
  
674
669
  if(plugindir == NULL){
934
929
  
935
930
  closedir(dir);
936
931
  dir = NULL;
 
932
  free_plugin(getplugin(NULL));
937
933
  
938
934
  for(plugin *p = plugin_list; p != NULL; p = p->next){
939
935
    if(p->pid != 0){
959
955
       from one of them */
960
956
    for(plugin *proc = plugin_list; proc != NULL;){
961
957
      /* Is this process completely done? */
962
 
      if(proc->eof and proc->completed){
 
958
      if(proc->completed and proc->eof){
963
959
        /* Only accept the plugin output if it exited cleanly */
964
960
        if(not WIFEXITED(proc->status)
965
961
           or WEXITSTATUS(proc->status) != 0){
967
963
 
968
964
          if(debug){
969
965
            if(WIFEXITED(proc->status)){
970
 
              fprintf(stderr, "Plugin %" PRIdMAX " exited with status"
971
 
                      " %d\n", (intmax_t) (proc->pid),
 
966
              fprintf(stderr, "Plugin %s [%" PRIdMAX "] exited with"
 
967
                      " status %d\n", proc->name,
 
968
                      (intmax_t) (proc->pid),
972
969
                      WEXITSTATUS(proc->status));
973
 
            } else if(WIFSIGNALED(proc->status)) {
974
 
              fprintf(stderr, "Plugin %" PRIdMAX " killed by signal"
975
 
                      " %d\n", (intmax_t) (proc->pid),
 
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),
976
974
                      WTERMSIG(proc->status));
977
975
            } else if(WCOREDUMP(proc->status)){
978
 
              fprintf(stderr, "Plugin %" PRIdMAX " dumped core\n",
979
 
                      (intmax_t) (proc->pid));
 
976
              fprintf(stderr, "Plugin %s [%" PRIdMAX "] dumped"
 
977
                      " core\n", proc->name, (intmax_t) (proc->pid));
980
978
            }
981
979
          }
982
980