/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

transformed a function to a part of main

Show diffs side-by-side

added added

removed removed

Lines of Context:
65
65
#include <errno.h>              /* errno, EBADF */
66
66
 
67
67
#define BUFFER_SIZE 256
68
 
 
69
 
#define PDIR "/lib/mandos/plugins.d"
70
 
#define AFILE "/conf/conf.d/mandos/plugin-runner.conf"
 
68
#define ARGFILE "/conf/conf.d/mandos/plugin-runner.conf"
71
69
 
72
70
const char *argp_program_version = "plugin-runner 1.0";
73
71
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
74
72
 
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*/
 
73
struct process;
 
74
 
 
75
typedef struct process{
86
76
  pid_t pid;
87
77
  int fd;
88
78
  char *buffer;
91
81
  bool eof;
92
82
  volatile bool completed;
93
83
  volatile int status;
 
84
  struct process *next;
 
85
} process;
 
86
 
 
87
typedef struct plugin{
 
88
  char *name;                   /* can be NULL or any plugin name */
 
89
  char **argv;
 
90
  int argc;
 
91
  char **environ;
 
92
  int envc;
 
93
  bool disabled;
94
94
  struct plugin *next;
95
95
} plugin;
96
96
 
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){
 
97
static plugin *getplugin(char *name, plugin **plugin_list){
 
98
  for (plugin *p = *plugin_list; p != NULL; p = p->next){
104
99
    if ((p->name == name)
105
100
        or (p->name and name and (strcmp(p->name, name) == 0))){
106
101
      return p;
118
113
      return NULL;
119
114
    }
120
115
  }
121
 
 
 
116
  
122
117
  *new_plugin = (plugin) { .name = copy_name,
123
118
                           .argc = 1,
 
119
                           .envc = 0,
124
120
                           .disabled = false,
125
 
                           .next = plugin_list };
 
121
                           .next = *plugin_list };
126
122
  
127
123
  new_plugin->argv = malloc(sizeof(char *) * 2);
128
124
  if (new_plugin->argv == NULL){
141
137
    return NULL;
142
138
  }
143
139
  new_plugin->environ[0] = NULL;
144
 
 
145
140
  /* Append the new plugin to the list */
146
 
  plugin_list = new_plugin;
 
141
  *plugin_list = new_plugin;
147
142
  return new_plugin;
148
143
}
149
144
 
186
181
  return add_to_char_array(def, &(p->environ), &(p->envc));
187
182
}
188
183
 
 
184
 
189
185
/*
190
186
 * Based on the example in the GNU LibC manual chapter 13.13 "File
191
187
 * Descriptor Flags".
202
198
  return fcntl(fd, F_SETFD, ret | FD_CLOEXEC);
203
199
}
204
200
 
 
201
process *process_list = NULL;
205
202
 
206
203
/* Mark processes as completed when they exit, and save their exit
207
204
   status. */
208
205
void handle_sigchld(__attribute__((unused)) int sig){
209
206
  while(true){
210
 
    plugin *proc = plugin_list;
 
207
    process *proc = process_list;
211
208
    int status;
212
209
    pid_t pid = waitpid(-1, &status, WNOHANG);
213
210
    if(pid == 0){
235
232
  }
236
233
}
237
234
 
238
 
/* Prints out a password to stdout */
239
235
bool print_out_password(const char *buffer, size_t length){
240
236
  ssize_t ret;
241
237
  if(length>0 and buffer[length-1] == '\n'){
251
247
  return true;
252
248
}
253
249
 
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);
 
250
static void free_plugin_list(plugin *plugin_list){
 
251
  for(plugin *next; plugin_list != NULL; plugin_list = next){
 
252
    next = plugin_list->next;
 
253
    for(char **arg = plugin_list->argv; *arg != NULL; arg++){
 
254
      free(*arg);
 
255
    }
 
256
    free(plugin_list->argv);
 
257
    for(char **env = plugin_list->environ; *env != NULL; env++){
 
258
      free(*env);
 
259
    }
 
260
    free(plugin_list->environ);
 
261
    free(plugin_list);
287
262
  }
288
263
}
289
264
 
290
265
int main(int argc, char *argv[]){
291
 
  char *plugindir = NULL;
292
 
  char *argfile = NULL;
 
266
  const char *plugindir = "/lib/mandos/plugins.d";
 
267
  const char *argfile = ARGFILE;
293
268
  FILE *conffp;
294
269
  size_t d_name_len;
295
270
  DIR *dir = NULL;
342
317
    { .name = "plugin-dir", .key = 128,
343
318
      .arg = "DIRECTORY",
344
319
      .doc = "Specify a different plugin directory", .group = 2 },
345
 
    { .name = "config-file", .key = 129,
346
 
      .arg = "FILE",
347
 
      .doc = "Specify a different configuration file", .group = 2 },
348
 
    { .name = "userid", .key = 130,
349
 
      .arg = "ID", .flags = 0,
350
 
      .doc = "User ID the plugins will run as", .group = 3 },
351
 
    { .name = "groupid", .key = 131,
352
 
      .arg = "ID", .flags = 0,
353
 
      .doc = "Group ID the plugins will run as", .group = 3 },
354
 
    { .name = "debug", .key = 132,
355
 
      .doc = "Debug mode", .group = 4 },
 
320
    { .name = "userid", .key = 129,
 
321
      .arg = "ID", .flags = 0,
 
322
      .doc = "User ID the plugins will run as", .group = 2 },
 
323
    { .name = "groupid", .key = 130,
 
324
      .arg = "ID", .flags = 0,
 
325
      .doc = "Group ID the plugins will run as", .group = 2 },
 
326
    { .name = "debug", .key = 131,
 
327
      .doc = "Debug mode", .group = 3 },
356
328
    { .name = NULL }
357
329
  };
358
330
  
359
 
  error_t parse_opt (int key, char *arg, __attribute__((unused)) struct argp_state *state) {
 
331
  error_t parse_opt (int key, char *arg, struct argp_state *state) {
360
332
    /* Get the INPUT argument from `argp_parse', which we know is a
361
333
       pointer to our plugin list pointer. */
 
334
    plugin **plugins = state->input;
362
335
    switch (key) {
363
 
    case 'g':                   /* --global-options */
 
336
    case 'g':
364
337
      if (arg != NULL){
365
338
        char *p;
366
339
        while((p = strsep(&arg, ",")) != NULL){
367
340
          if(p[0] == '\0'){
368
341
            continue;
369
342
          }
370
 
          if(not add_argument(getplugin(NULL), p)){
 
343
          if(not add_argument(getplugin(NULL, plugins), p)){
371
344
            perror("add_argument");
372
345
            return ARGP_ERR_UNKNOWN;
373
346
          }
374
347
        }
375
348
      }
376
349
      break;
377
 
    case 'e':                   /* --global-envs */
 
350
    case 'e':
378
351
      if(arg == NULL){
379
352
        break;
380
353
      }
383
356
        if(envdef == NULL){
384
357
          break;
385
358
        }
386
 
        if(not add_environment(getplugin(NULL), envdef)){
 
359
        if(not add_environment(getplugin(NULL, plugins), envdef)){
387
360
          perror("add_environment");
388
361
        }
389
362
      }
390
363
      break;
391
 
    case 'o':                   /* --options-for */
 
364
    case 'o':
392
365
      if (arg != NULL){
393
366
        char *p_name = strsep(&arg, ":");
394
367
        if(p_name[0] == '\0'){
404
377
            if(p[0] == '\0'){
405
378
              continue;
406
379
            }
407
 
            if(not add_argument(getplugin(p_name), p)){
 
380
            if(not add_argument(getplugin(p_name, plugins), p)){
408
381
              perror("add_argument");
409
382
              return ARGP_ERR_UNKNOWN;
410
383
            }
412
385
        }
413
386
      }
414
387
      break;
415
 
    case 'f':                   /* --envs-for */
 
388
    case 'f':
416
389
      if(arg == NULL){
417
390
        break;
418
391
      }
426
399
          break;
427
400
        }
428
401
        envdef++;
429
 
        if(not add_environment(getplugin(p_name), envdef)){
 
402
        if(not add_environment(getplugin(p_name, plugins), envdef)){
430
403
          perror("add_environment");
431
404
        }
432
405
      }
433
406
      break;
434
 
    case 'd':                   /* --disable */
 
407
    case 'd':
435
408
      if (arg != NULL){
436
 
        plugin *p = getplugin(arg);
 
409
        plugin *p = getplugin(arg, plugins);
437
410
        if(p == NULL){
438
411
          return ARGP_ERR_UNKNOWN;
439
412
        }
440
413
        p->disabled = true;
441
414
      }
442
415
      break;
443
 
    case 128:                   /* --plugin-dir */
444
 
      plugindir = strdup(arg);
445
 
      if(plugindir == NULL){
446
 
        perror("strdup");
447
 
      }      
 
416
    case 128:
 
417
      plugindir = arg;
448
418
      break;
449
 
    case 129:                   /* --config-file */
450
 
      argfile = strdup(arg);
451
 
      if(argfile == NULL){
452
 
        perror("strdup");
453
 
      }
454
 
      break;      
455
 
    case 130:                   /* --userid */
 
419
    case 129:
456
420
      uid = (uid_t)strtol(arg, NULL, 10);
457
421
      break;
458
 
    case 131:                   /* --groupid */
 
422
    case 130:
459
423
      gid = (gid_t)strtol(arg, NULL, 10);
460
424
      break;
461
 
    case 132:                   /* --debug */
 
425
    case 131:
462
426
      debug = true;
463
427
      break;
464
428
    case ARGP_KEY_ARG:
472
436
    return 0;
473
437
  }
474
438
  
 
439
  plugin *plugin_list = NULL;
 
440
  
475
441
  struct argp argp = { .options = options, .parser = parse_opt,
476
442
                       .args_doc = "[+PLUS_SEPARATED_OPTIONS]",
477
443
                       .doc = "Mandos plugin runner -- Run plugins" };
478
444
  
479
 
  ret = argp_parse (&argp, argc, argv, 0, 0, NULL);
 
445
  ret = argp_parse (&argp, argc, argv, 0, 0, &plugin_list);
480
446
  if (ret == ARGP_ERR_UNKNOWN){
481
447
    fprintf(stderr, "Unknown error while parsing arguments\n");
482
448
    exitstatus = EXIT_FAILURE;
483
449
    goto fallback;
484
450
  }
485
451
 
486
 
  /* Opens the configfile if aviable */
487
 
  if (argfile == NULL){
488
 
    conffp = fopen(AFILE, "r");
489
 
  } else {
490
 
    conffp = fopen(argfile, "r");
491
 
  }  
 
452
  conffp = fopen(argfile, "r");
492
453
  if(conffp != NULL){
493
454
    char *org_line = NULL;
494
455
    char *p, *arg, *new_arg, *line;
506
467
    }
507
468
    custom_argv[0] = argv[0];
508
469
    custom_argv[1] = NULL;
509
 
 
510
 
    /* for each line in the config file, strip whitespace and ignore commented text */
 
470
    
511
471
    while(true){
512
472
      sret = getline(&org_line, &size, conffp);
513
473
      if(sret == -1){
521
481
          continue;
522
482
        }
523
483
        new_arg = strdup(p);
524
 
        if(new_arg == NULL){
525
 
          perror("strdup");
526
 
          exitstatus = EXIT_FAILURE;
527
 
          free(org_line);
528
 
          goto fallback;
529
 
        }
530
 
        
 
484
 
531
485
        custom_argc += 1;
532
486
        custom_argv = realloc(custom_argv, sizeof(char *)
533
487
                              * ((unsigned int) custom_argc + 1));
551
505
      goto fallback;
552
506
    }
553
507
  }
554
 
  /* If there was any arguments from configuration file,
555
 
     pass them to parser as command arguments */
 
508
 
556
509
  if(custom_argv != NULL){
557
 
    ret = argp_parse (&argp, custom_argc, custom_argv, 0, 0, NULL);
 
510
    ret = argp_parse (&argp, custom_argc, custom_argv, 0, 0, &plugin_list);
558
511
    if (ret == ARGP_ERR_UNKNOWN){
559
512
      fprintf(stderr, "Unknown error while parsing arguments\n");
560
513
      exitstatus = EXIT_FAILURE;
575
528
      }
576
529
    }
577
530
  }
578
 
 
579
 
  /* Strip permissions down to nobody */
 
531
  
580
532
  ret = setuid(uid);
581
533
  if (ret == -1){
582
534
    perror("setuid");
583
 
  }  
 
535
  }
 
536
  
584
537
  setgid(gid);
585
538
  if (ret == -1){
586
539
    perror("setgid");
587
540
  }
588
 
 
589
 
  if (plugindir == NULL){
590
 
    dir = opendir(PDIR);
591
 
  } else {
592
 
    dir = opendir(plugindir);
593
 
  }
594
541
  
 
542
  dir = opendir(plugindir);
595
543
  if(dir == NULL){
596
544
    perror("Could not open plugin dir");
597
545
    exitstatus = EXIT_FAILURE;
612
560
  }
613
561
  
614
562
  FD_ZERO(&rfds_all);
615
 
 
616
 
  /* Read and execute any executable in the plugin directory*/
 
563
  
617
564
  while(true){
618
565
    dirst = readdir(dir);
619
566
    
650
597
          break;
651
598
        }
652
599
      }
 
600
      
653
601
      if(bad_name){
654
602
        continue;
655
603
      }
 
604
      
656
605
      for(const char **suf = bad_suffixes; *suf != NULL; suf++){
657
606
        size_t suf_len = strlen(*suf);
658
607
        if((d_name_len >= suf_len)
685
634
      free(filename);
686
635
      continue;
687
636
    }
688
 
 
689
 
    /* Ignore non-executable files */
 
637
    
690
638
    if (not S_ISREG(st.st_mode) or (access(filename, X_OK) != 0)){
691
639
      if(debug){
692
640
        fprintf(stderr, "Ignoring plugin dir entry \"%s\""
695
643
      free(filename);
696
644
      continue;
697
645
    }
698
 
    
699
 
    plugin *p = getplugin(dirst->d_name);
 
646
    plugin *p = getplugin(dirst->d_name, &plugin_list);
700
647
    if(p == NULL){
701
648
      perror("getplugin");
702
649
      free(filename);
712
659
    }
713
660
    {
714
661
      /* Add global arguments to argument list for this plugin */
715
 
      plugin *g = getplugin(NULL);
 
662
      plugin *g = getplugin(NULL, &plugin_list);
716
663
      if(g != NULL){
717
664
        for(char **a = g->argv + 1; *a != NULL; a++){
718
665
          if(not add_argument(p, *a)){
750
697
      exitstatus = EXIT_FAILURE;
751
698
      goto fallback;
752
699
    }
753
 
    /* Ask OS to automatic close the pipe on exec */
754
700
    ret = set_cloexec_flag(pipefd[0]);
755
701
    if(ret < 0){
756
702
      perror("set_cloexec_flag");
814
760
      }
815
761
      /* no return */
816
762
    }
817
 
    /* Parent process */
818
 
    close(pipefd[1]);           /* Close unused write end of pipe */
 
763
    /* parent process */
819
764
    free(filename);
820
 
    plugin *new_plugin = getplugin(dirst->d_name);
821
 
    if (new_plugin == NULL){
822
 
      perror("getplugin");
 
765
    close(pipefd[1]);           /* close unused write end of pipe */
 
766
    process *new_process = malloc(sizeof(process));
 
767
    if (new_process == NULL){
 
768
      perror("malloc");
823
769
      ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
824
770
      if(ret < 0){
825
 
        perror("sigprocmask");
 
771
        perror("sigprocmask");
826
772
      }
827
773
      exitstatus = EXIT_FAILURE;
828
774
      goto fallback;
829
775
    }
830
776
    
831
 
    new_plugin->pid = pid;
832
 
    new_plugin->fd = pipefd[0];
833
 
    
 
777
    *new_process = (struct process){ .pid = pid,
 
778
                                     .fd = pipefd[0],
 
779
                                     .next = process_list };
 
780
    // List handling
 
781
    process_list = new_process;
834
782
    /* Unblock SIGCHLD so signal handler can be run if this process
835
783
       has already completed */
836
784
    ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
840
788
      goto fallback;
841
789
    }
842
790
    
843
 
    FD_SET(new_plugin->fd, &rfds_all);
 
791
    FD_SET(new_process->fd, &rfds_all);
844
792
    
845
 
    if (maxfd < new_plugin->fd){
846
 
      maxfd = new_plugin->fd;
 
793
    if (maxfd < new_process->fd){
 
794
      maxfd = new_process->fd;
847
795
    }
848
796
    
849
797
  }
850
798
  
 
799
  free_plugin_list(plugin_list);
 
800
  plugin_list = NULL;
 
801
  
851
802
  closedir(dir);
852
803
  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
 
    }
 
804
    
 
805
  if (process_list == NULL){
 
806
    fprintf(stderr, "No plugin processes started. Incorrect plugin"
 
807
            " directory?\n");
 
808
    process_list = NULL;
863
809
  }
864
 
 
865
 
  /* Main loop while running plugins exist */
866
 
  while(plugin_list){
 
810
  while(process_list){
867
811
    fd_set rfds = rfds_all;
868
812
    int select_ret = select(maxfd+1, &rfds, NULL, NULL, NULL);
869
813
    if (select_ret == -1){
873
817
    }
874
818
    /* OK, now either a process completed, or something can be read
875
819
       from one of them */
876
 
    for(plugin *proc = plugin_list; proc != NULL; proc = proc->next){
 
820
    for(process *proc = process_list; proc ; proc = proc->next){
877
821
      /* Is this process completely done? */
878
822
      if(proc->eof and proc->completed){
879
823
        /* Only accept the plugin output if it exited cleanly */
880
824
        if(not WIFEXITED(proc->status)
881
825
           or WEXITSTATUS(proc->status) != 0){
882
826
          /* Bad exit by plugin */
883
 
 
884
827
          if(debug){
885
828
            if(WIFEXITED(proc->status)){
886
829
              fprintf(stderr, "Plugin %u exited with status %d\n",
895
838
                      (unsigned int) (proc->pid));
896
839
            }
897
840
          }
898
 
          
899
841
          /* Remove the plugin */
900
842
          FD_CLR(proc->fd, &rfds_all);
901
 
 
902
843
          /* Block signal while modifying process_list */
903
844
          ret = sigprocmask(SIG_BLOCK, &sigchld_action.sa_mask, NULL);
904
845
          if(ret < 0){
906
847
            exitstatus = EXIT_FAILURE;
907
848
            goto fallback;
908
849
          }
909
 
          free_plugin(proc);
 
850
          /* Delete this process entry from the list */
 
851
          if(process_list == proc){
 
852
            /* First one - simple */
 
853
            process_list = proc->next;
 
854
          } else {
 
855
            /* Second one or later */
 
856
            for(process *p = process_list; p != NULL; p = p->next){
 
857
              if(p->next == proc){
 
858
                p->next = proc->next;
 
859
                break;
 
860
              }
 
861
            }
 
862
          }
910
863
          /* We are done modifying process list, so unblock signal */
911
864
          ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask,
912
865
                             NULL);
913
866
          if(ret < 0){
914
867
            perror("sigprocmask");
915
 
            exitstatus = EXIT_FAILURE;
916
 
            goto fallback;
917
 
          }
918
 
          
919
 
          if(plugin_list == NULL){
920
 
            break;
921
 
          }
922
 
          continue;
 
868
          }
 
869
          free(proc->buffer);
 
870
          free(proc);
 
871
          /* We deleted this process from the list, so we can't go
 
872
             proc->next.  Therefore, start over from the beginning of
 
873
             the process list */
 
874
          break;
923
875
        }
924
 
        
925
876
        /* This process exited nicely, so print its buffer */
926
877
 
927
878
        bool bret = print_out_password(proc->buffer,
932
883
        }
933
884
        goto fallback;
934
885
      }
935
 
      
936
886
      /* This process has not completed.  Does it have any output? */
937
887
      if(proc->eof or not FD_ISSET(proc->fd, &rfds)){
938
888
        /* This process had nothing to say at this time */
968
918
 
969
919
 fallback:
970
920
  
971
 
  if(plugin_list == NULL or exitstatus != EXIT_SUCCESS){
 
921
  if(process_list == NULL or exitstatus != EXIT_SUCCESS){
972
922
    /* Fallback if all plugins failed, none are found or an error
973
923
       occured */
974
924
    bool bret;
994
944
    }
995
945
    free(custom_argv);
996
946
  }
 
947
  free_plugin_list(plugin_list);
997
948
  
998
949
  if(dir != NULL){
999
950
    closedir(dir);
1000
951
  }
1001
952
  
1002
953
  /* 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
 
      }
 
954
  for(process *next; process_list != NULL; process_list = next){
 
955
    next = process_list->next;
 
956
    close(process_list->fd);
 
957
    ret = kill(process_list->pid, SIGTERM);
 
958
    if(ret == -1 and errno != ESRCH){
 
959
      /* set-uid proccesses migth not get closed */
 
960
      perror("kill");
1011
961
    }
 
962
    free(process_list->buffer);
 
963
    free(process_list);
1012
964
  }
1013
965
  
1014
966
  /* Wait for any remaining child processes to terminate */
1018
970
  if(errno != ECHILD){
1019
971
    perror("wait");
1020
972
  }
1021
 
 
1022
 
  free_plugin_list();
1023
 
  
1024
 
  free(plugindir);
1025
 
  free(argfile);
1026
973
  
1027
974
  return exitstatus;
1028
975
}