/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-08-25 10:41:16 UTC
  • Revision ID: teddy@fukt.bsnet.se-20080825104116-cjd54qqs535kpdxq
* mandos-options.xml (address): Refer to IPv4-mapped IPv6 address
                                syntax by name.

* mandos.conf.xml (SEE ALSO): Also refer to gnutls_priority_init(3),
                              RFC 4291, and Zeroconf.

* mandos.xml (SEE ALSO): Also refer to sections 2.2 and 2.5.5.2 of RFC
                         4291.  Stop using <citation> tags.

Show diffs side-by-side

added added

removed removed

Lines of Context:
72
72
const char *argp_program_version = "plugin-runner 1.0";
73
73
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
74
74
 
75
 
struct plugin;
76
 
 
77
 
typedef struct plugin{
78
 
  char *name;                   /* can be NULL or any plugin name */
79
 
  char **argv;
80
 
  int argc;
81
 
  char **environ;
82
 
  int envc;
83
 
  bool disabled;
84
 
 
85
 
  /* Variables used for running processes*/
 
75
struct process;
 
76
 
 
77
typedef struct process{
86
78
  pid_t pid;
87
79
  int fd;
88
80
  char *buffer;
91
83
  bool eof;
92
84
  volatile bool completed;
93
85
  volatile int status;
 
86
  struct process *next;
 
87
} process;
 
88
 
 
89
typedef struct plugin{
 
90
  char *name;                   /* can be NULL or any plugin name */
 
91
  char **argv;
 
92
  int argc;
 
93
  char **environ;
 
94
  int envc;
 
95
  bool disabled;
94
96
  struct plugin *next;
95
97
} plugin;
96
98
 
97
 
static plugin *plugin_list = NULL;
98
 
 
99
 
/* Gets a existing plugin based on name,
100
 
   or if none is found, creates a new one */
101
 
static plugin *getplugin(char *name){
102
 
  /* Check for exiting plugin with that name */
103
 
  for (plugin *p = plugin_list; p != NULL; p = p->next){
 
99
static plugin *getplugin(char *name, plugin **plugin_list){
 
100
  for (plugin *p = *plugin_list; p != NULL; p = p->next){
104
101
    if ((p->name == name)
105
102
        or (p->name and name and (strcmp(p->name, name) == 0))){
106
103
      return p;
118
115
      return NULL;
119
116
    }
120
117
  }
121
 
 
 
118
  
122
119
  *new_plugin = (plugin) { .name = copy_name,
123
120
                           .argc = 1,
 
121
                           .envc = 0,
124
122
                           .disabled = false,
125
 
                           .next = plugin_list };
 
123
                           .next = *plugin_list };
126
124
  
127
125
  new_plugin->argv = malloc(sizeof(char *) * 2);
128
126
  if (new_plugin->argv == NULL){
141
139
    return NULL;
142
140
  }
143
141
  new_plugin->environ[0] = NULL;
144
 
 
145
142
  /* Append the new plugin to the list */
146
 
  plugin_list = new_plugin;
 
143
  *plugin_list = new_plugin;
147
144
  return new_plugin;
148
145
}
149
146
 
186
183
  return add_to_char_array(def, &(p->environ), &(p->envc));
187
184
}
188
185
 
 
186
 
189
187
/*
190
188
 * Based on the example in the GNU LibC manual chapter 13.13 "File
191
189
 * Descriptor Flags".
202
200
  return fcntl(fd, F_SETFD, ret | FD_CLOEXEC);
203
201
}
204
202
 
 
203
process *process_list = NULL;
205
204
 
206
205
/* Mark processes as completed when they exit, and save their exit
207
206
   status. */
208
207
void handle_sigchld(__attribute__((unused)) int sig){
209
208
  while(true){
210
 
    plugin *proc = plugin_list;
 
209
    process *proc = process_list;
211
210
    int status;
212
211
    pid_t pid = waitpid(-1, &status, WNOHANG);
213
212
    if(pid == 0){
235
234
  }
236
235
}
237
236
 
238
 
/* Prints out a password to stdout */
239
237
bool print_out_password(const char *buffer, size_t length){
240
238
  ssize_t ret;
241
239
  if(length>0 and buffer[length-1] == '\n'){
251
249
  return true;
252
250
}
253
251
 
254
 
/* Removes and free a plugin from the plugin list */
255
 
static void free_plugin(plugin *plugin_node){
256
 
  
257
 
  for(char **arg = plugin_node->argv; *arg != NULL; arg++){
258
 
    free(*arg);
259
 
  }
260
 
  free(plugin_node->argv);
261
 
  for(char **env = plugin_node->environ; *env != NULL; env++){
262
 
    free(*env);
263
 
  }
264
 
  free(plugin_node->environ);
265
 
  free(plugin_node->buffer);
266
 
 
267
 
  /* Removes the plugin from the singly-linked list */
268
 
  if(plugin_node == plugin_list){
269
 
    /* First one - simple */
270
 
    plugin_list = plugin_list->next;
271
 
  } else {
272
 
    /* Second one or later */
273
 
    for(plugin *p = plugin_list; p != NULL; p = p->next){
274
 
      if(p->next == plugin_node){
275
 
        p->next = plugin_node->next;
276
 
        break;
277
 
      }
278
 
    }
279
 
  }
280
 
  
281
 
  free(plugin_node);
282
 
}
283
 
 
284
 
static void free_plugin_list(void){
285
 
  while(plugin_list != NULL){
286
 
    free_plugin(plugin_list);
 
252
static void free_plugin_list(plugin *plugin_list){
 
253
  for(plugin *next; plugin_list != NULL; plugin_list = next){
 
254
    next = plugin_list->next;
 
255
    for(char **arg = plugin_list->argv; *arg != NULL; arg++){
 
256
      free(*arg);
 
257
    }
 
258
    free(plugin_list->argv);
 
259
    for(char **env = plugin_list->environ; *env != NULL; env++){
 
260
      free(*env);
 
261
    }
 
262
    free(plugin_list->environ);
 
263
    free(plugin_list);
287
264
  }
288
265
}
289
266
 
356
333
    { .name = NULL }
357
334
  };
358
335
  
359
 
  error_t parse_opt (int key, char *arg, __attribute__((unused)) struct argp_state *state) {
 
336
  error_t parse_opt (int key, char *arg, struct argp_state *state) {
360
337
    /* Get the INPUT argument from `argp_parse', which we know is a
361
338
       pointer to our plugin list pointer. */
 
339
    plugin **plugins = state->input;
362
340
    switch (key) {
363
 
    case 'g':                   /* --global-options */
 
341
    case 'g':
364
342
      if (arg != NULL){
365
343
        char *p;
366
344
        while((p = strsep(&arg, ",")) != NULL){
367
345
          if(p[0] == '\0'){
368
346
            continue;
369
347
          }
370
 
          if(not add_argument(getplugin(NULL), p)){
 
348
          if(not add_argument(getplugin(NULL, plugins), p)){
371
349
            perror("add_argument");
372
350
            return ARGP_ERR_UNKNOWN;
373
351
          }
374
352
        }
375
353
      }
376
354
      break;
377
 
    case 'e':                   /* --global-envs */
 
355
    case 'e':
378
356
      if(arg == NULL){
379
357
        break;
380
358
      }
383
361
        if(envdef == NULL){
384
362
          break;
385
363
        }
386
 
        if(not add_environment(getplugin(NULL), envdef)){
 
364
        if(not add_environment(getplugin(NULL, plugins), envdef)){
387
365
          perror("add_environment");
388
366
        }
389
367
      }
390
368
      break;
391
 
    case 'o':                   /* --options-for */
 
369
    case 'o':
392
370
      if (arg != NULL){
393
371
        char *p_name = strsep(&arg, ":");
394
372
        if(p_name[0] == '\0'){
404
382
            if(p[0] == '\0'){
405
383
              continue;
406
384
            }
407
 
            if(not add_argument(getplugin(p_name), p)){
 
385
            if(not add_argument(getplugin(p_name, plugins), p)){
408
386
              perror("add_argument");
409
387
              return ARGP_ERR_UNKNOWN;
410
388
            }
412
390
        }
413
391
      }
414
392
      break;
415
 
    case 'f':                   /* --envs-for */
 
393
    case 'f':
416
394
      if(arg == NULL){
417
395
        break;
418
396
      }
426
404
          break;
427
405
        }
428
406
        envdef++;
429
 
        if(not add_environment(getplugin(p_name), envdef)){
 
407
        if(not add_environment(getplugin(p_name, plugins), envdef)){
430
408
          perror("add_environment");
431
409
        }
432
410
      }
433
411
      break;
434
 
    case 'd':                   /* --disable */
 
412
    case 'd':
435
413
      if (arg != NULL){
436
 
        plugin *p = getplugin(arg);
 
414
        plugin *p = getplugin(arg, plugins);
437
415
        if(p == NULL){
438
416
          return ARGP_ERR_UNKNOWN;
439
417
        }
440
418
        p->disabled = true;
441
419
      }
442
420
      break;
443
 
    case 128:                   /* --plugin-dir */
 
421
    case 128:
444
422
      plugindir = strdup(arg);
445
423
      if(plugindir == NULL){
446
424
        perror("strdup");
447
425
      }      
448
426
      break;
449
 
    case 129:                   /* --config-file */
 
427
    case 129:
450
428
      argfile = strdup(arg);
451
429
      if(argfile == NULL){
452
430
        perror("strdup");
453
431
      }
454
432
      break;      
455
 
    case 130:                   /* --userid */
 
433
    case 130:
456
434
      uid = (uid_t)strtol(arg, NULL, 10);
457
435
      break;
458
 
    case 131:                   /* --groupid */
 
436
    case 131:
459
437
      gid = (gid_t)strtol(arg, NULL, 10);
460
438
      break;
461
 
    case 132:                   /* --debug */
 
439
    case 132:
462
440
      debug = true;
463
441
      break;
464
442
    case ARGP_KEY_ARG:
472
450
    return 0;
473
451
  }
474
452
  
 
453
  plugin *plugin_list = NULL;
 
454
  
475
455
  struct argp argp = { .options = options, .parser = parse_opt,
476
456
                       .args_doc = "[+PLUS_SEPARATED_OPTIONS]",
477
457
                       .doc = "Mandos plugin runner -- Run plugins" };
478
458
  
479
 
  ret = argp_parse (&argp, argc, argv, 0, 0, NULL);
 
459
  ret = argp_parse (&argp, argc, argv, 0, 0, &plugin_list);
480
460
  if (ret == ARGP_ERR_UNKNOWN){
481
461
    fprintf(stderr, "Unknown error while parsing arguments\n");
482
462
    exitstatus = EXIT_FAILURE;
483
463
    goto fallback;
484
464
  }
485
465
 
486
 
  /* Opens the configfile if aviable */
487
466
  if (argfile == NULL){
488
467
    conffp = fopen(AFILE, "r");
489
468
  } else {
490
469
    conffp = fopen(argfile, "r");
491
 
  }  
 
470
  }
 
471
  
492
472
  if(conffp != NULL){
493
473
    char *org_line = NULL;
494
474
    char *p, *arg, *new_arg, *line;
506
486
    }
507
487
    custom_argv[0] = argv[0];
508
488
    custom_argv[1] = NULL;
509
 
 
510
 
    /* for each line in the config file, strip whitespace and ignore commented text */
 
489
    
511
490
    while(true){
512
491
      sret = getline(&org_line, &size, conffp);
513
492
      if(sret == -1){
551
530
      goto fallback;
552
531
    }
553
532
  }
554
 
  /* If there was any arguments from configuration file,
555
 
     pass them to parser as command arguments */
 
533
 
556
534
  if(custom_argv != NULL){
557
 
    ret = argp_parse (&argp, custom_argc, custom_argv, 0, 0, NULL);
 
535
    ret = argp_parse (&argp, custom_argc, custom_argv, 0, 0, &plugin_list);
558
536
    if (ret == ARGP_ERR_UNKNOWN){
559
537
      fprintf(stderr, "Unknown error while parsing arguments\n");
560
538
      exitstatus = EXIT_FAILURE;
575
553
      }
576
554
    }
577
555
  }
578
 
 
579
 
  /* Strip permissions down to nobody */
 
556
  
580
557
  ret = setuid(uid);
581
558
  if (ret == -1){
582
559
    perror("setuid");
583
 
  }  
 
560
  }
 
561
  
584
562
  setgid(gid);
585
563
  if (ret == -1){
586
564
    perror("setgid");
612
590
  }
613
591
  
614
592
  FD_ZERO(&rfds_all);
615
 
 
616
 
  /* Read and execute any executable in the plugin directory*/
 
593
  
617
594
  while(true){
618
595
    dirst = readdir(dir);
619
596
    
650
627
          break;
651
628
        }
652
629
      }
 
630
      
653
631
      if(bad_name){
654
632
        continue;
655
633
      }
 
634
      
656
635
      for(const char **suf = bad_suffixes; *suf != NULL; suf++){
657
636
        size_t suf_len = strlen(*suf);
658
637
        if((d_name_len >= suf_len)
685
664
      free(filename);
686
665
      continue;
687
666
    }
688
 
 
689
 
    /* Ignore non-executable files */
 
667
    
690
668
    if (not S_ISREG(st.st_mode) or (access(filename, X_OK) != 0)){
691
669
      if(debug){
692
670
        fprintf(stderr, "Ignoring plugin dir entry \"%s\""
695
673
      free(filename);
696
674
      continue;
697
675
    }
698
 
    
699
 
    plugin *p = getplugin(dirst->d_name);
 
676
    plugin *p = getplugin(dirst->d_name, &plugin_list);
700
677
    if(p == NULL){
701
678
      perror("getplugin");
702
679
      free(filename);
712
689
    }
713
690
    {
714
691
      /* Add global arguments to argument list for this plugin */
715
 
      plugin *g = getplugin(NULL);
 
692
      plugin *g = getplugin(NULL, &plugin_list);
716
693
      if(g != NULL){
717
694
        for(char **a = g->argv + 1; *a != NULL; a++){
718
695
          if(not add_argument(p, *a)){
750
727
      exitstatus = EXIT_FAILURE;
751
728
      goto fallback;
752
729
    }
753
 
    /* Ask OS to automatic close the pipe on exec */
754
730
    ret = set_cloexec_flag(pipefd[0]);
755
731
    if(ret < 0){
756
732
      perror("set_cloexec_flag");
814
790
      }
815
791
      /* no return */
816
792
    }
817
 
    /* Parent process */
818
 
    close(pipefd[1]);           /* Close unused write end of pipe */
 
793
    /* parent process */
819
794
    free(filename);
820
 
    plugin *new_plugin = getplugin(dirst->d_name);
821
 
    if (new_plugin == NULL){
822
 
      perror("getplugin");
 
795
    close(pipefd[1]);           /* close unused write end of pipe */
 
796
    process *new_process = malloc(sizeof(process));
 
797
    if (new_process == NULL){
 
798
      perror("malloc");
823
799
      ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
824
800
      if(ret < 0){
825
 
        perror("sigprocmask");
 
801
        perror("sigprocmask");
826
802
      }
827
803
      exitstatus = EXIT_FAILURE;
828
804
      goto fallback;
829
805
    }
830
806
    
831
 
    new_plugin->pid = pid;
832
 
    new_plugin->fd = pipefd[0];
833
 
    
 
807
    *new_process = (struct process){ .pid = pid,
 
808
                                     .fd = pipefd[0],
 
809
                                     .next = process_list };
 
810
    // List handling
 
811
    process_list = new_process;
834
812
    /* Unblock SIGCHLD so signal handler can be run if this process
835
813
       has already completed */
836
814
    ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
840
818
      goto fallback;
841
819
    }
842
820
    
843
 
    FD_SET(new_plugin->fd, &rfds_all);
 
821
    FD_SET(new_process->fd, &rfds_all);
844
822
    
845
 
    if (maxfd < new_plugin->fd){
846
 
      maxfd = new_plugin->fd;
 
823
    if (maxfd < new_process->fd){
 
824
      maxfd = new_process->fd;
847
825
    }
848
826
    
849
827
  }
 
828
 
 
829
  free_plugin_list(plugin_list);
 
830
  plugin_list = NULL;
850
831
  
851
832
  closedir(dir);
852
833
  dir = NULL;
853
 
 
854
 
  for(plugin *p = plugin_list; p != NULL; p = p->next){
855
 
    if(p->pid != 0){
856
 
      break;
857
 
    }
858
 
    if(p->next == NULL){
859
 
      fprintf(stderr, "No plugin processes started. Incorrect plugin"
860
 
              " directory?\n");
861
 
      free_plugin_list();
862
 
    }
 
834
    
 
835
  if (process_list == NULL){
 
836
    fprintf(stderr, "No plugin processes started. Incorrect plugin"
 
837
            " directory?\n");
 
838
    process_list = NULL;
863
839
  }
864
 
 
865
 
  /* Main loop while running plugins exist */
866
 
  while(plugin_list){
 
840
  while(process_list){
867
841
    fd_set rfds = rfds_all;
868
842
    int select_ret = select(maxfd+1, &rfds, NULL, NULL, NULL);
869
843
    if (select_ret == -1){
873
847
    }
874
848
    /* OK, now either a process completed, or something can be read
875
849
       from one of them */
876
 
    for(plugin *proc = plugin_list; proc != NULL; proc = proc->next){
 
850
    for(process *proc = process_list; proc ; proc = proc->next){
877
851
      /* Is this process completely done? */
878
852
      if(proc->eof and proc->completed){
879
853
        /* Only accept the plugin output if it exited cleanly */
880
854
        if(not WIFEXITED(proc->status)
881
855
           or WEXITSTATUS(proc->status) != 0){
882
856
          /* Bad exit by plugin */
883
 
 
884
857
          if(debug){
885
858
            if(WIFEXITED(proc->status)){
886
859
              fprintf(stderr, "Plugin %u exited with status %d\n",
895
868
                      (unsigned int) (proc->pid));
896
869
            }
897
870
          }
898
 
          
899
871
          /* Remove the plugin */
900
872
          FD_CLR(proc->fd, &rfds_all);
901
 
 
902
873
          /* Block signal while modifying process_list */
903
874
          ret = sigprocmask(SIG_BLOCK, &sigchld_action.sa_mask, NULL);
904
875
          if(ret < 0){
906
877
            exitstatus = EXIT_FAILURE;
907
878
            goto fallback;
908
879
          }
909
 
          free_plugin(proc);
 
880
          /* Delete this process entry from the list */
 
881
          if(process_list == proc){
 
882
            /* First one - simple */
 
883
            process_list = proc->next;
 
884
          } else {
 
885
            /* Second one or later */
 
886
            for(process *p = process_list; p != NULL; p = p->next){
 
887
              if(p->next == proc){
 
888
                p->next = proc->next;
 
889
                break;
 
890
              }
 
891
            }
 
892
          }
910
893
          /* We are done modifying process list, so unblock signal */
911
894
          ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask,
912
895
                             NULL);
913
896
          if(ret < 0){
914
897
            perror("sigprocmask");
915
 
            exitstatus = EXIT_FAILURE;
916
 
            goto fallback;
917
 
          }
918
 
          
919
 
          if(plugin_list == NULL){
920
 
            break;
921
 
          }
922
 
          continue;
 
898
          }
 
899
          free(proc->buffer);
 
900
          free(proc);
 
901
          /* We deleted this process from the list, so we can't go
 
902
             proc->next.  Therefore, start over from the beginning of
 
903
             the process list */
 
904
          break;
923
905
        }
924
 
        
925
906
        /* This process exited nicely, so print its buffer */
926
907
 
927
908
        bool bret = print_out_password(proc->buffer,
932
913
        }
933
914
        goto fallback;
934
915
      }
935
 
      
936
916
      /* This process has not completed.  Does it have any output? */
937
917
      if(proc->eof or not FD_ISSET(proc->fd, &rfds)){
938
918
        /* This process had nothing to say at this time */
968
948
 
969
949
 fallback:
970
950
  
971
 
  if(plugin_list == NULL or exitstatus != EXIT_SUCCESS){
 
951
  if(process_list == NULL or exitstatus != EXIT_SUCCESS){
972
952
    /* Fallback if all plugins failed, none are found or an error
973
953
       occured */
974
954
    bool bret;
994
974
    }
995
975
    free(custom_argv);
996
976
  }
 
977
  free_plugin_list(plugin_list);
997
978
  
998
979
  if(dir != NULL){
999
980
    closedir(dir);
1000
981
  }
1001
982
  
1002
983
  /* Free the process list and kill the processes */
1003
 
  for(plugin *p = plugin_list; p != NULL; p = p->next){
1004
 
    if(p->pid != 0){
1005
 
      close(p->fd);
1006
 
      ret = kill(p->pid, SIGTERM);
1007
 
      if(ret == -1 and errno != ESRCH){
1008
 
        /* Set-uid proccesses might not get closed */
1009
 
        perror("kill");
1010
 
      }
 
984
  for(process *next; process_list != NULL; process_list = next){
 
985
    next = process_list->next;
 
986
    close(process_list->fd);
 
987
    ret = kill(process_list->pid, SIGTERM);
 
988
    if(ret == -1 and errno != ESRCH){
 
989
      /* set-uid proccesses migth not get closed */
 
990
      perror("kill");
1011
991
    }
 
992
    free(process_list->buffer);
 
993
    free(process_list);
1012
994
  }
1013
995
  
1014
996
  /* Wait for any remaining child processes to terminate */
1019
1001
    perror("wait");
1020
1002
  }
1021
1003
 
1022
 
  free_plugin_list();
1023
 
  
1024
1004
  free(plugindir);
1025
1005
  free(argfile);
1026
1006