/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

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 process;
76
 
 
77
 
typedef struct process{
 
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*/
78
86
  pid_t pid;
79
87
  int fd;
80
88
  char *buffer;
83
91
  bool eof;
84
92
  volatile bool completed;
85
93
  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;
96
94
  struct plugin *next;
97
95
} plugin;
98
96
 
99
 
static plugin *getplugin(char *name, plugin **plugin_list){
100
 
  for (plugin *p = *plugin_list; p != NULL; p = p->next){
 
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){
101
104
    if ((p->name == name)
102
105
        or (p->name and name and (strcmp(p->name, name) == 0))){
103
106
      return p;
115
118
      return NULL;
116
119
    }
117
120
  }
118
 
  
 
121
 
119
122
  *new_plugin = (plugin) { .name = copy_name,
120
123
                           .argc = 1,
121
 
                           .envc = 0,
122
124
                           .disabled = false,
123
 
                           .next = *plugin_list };
 
125
                           .next = plugin_list };
124
126
  
125
127
  new_plugin->argv = malloc(sizeof(char *) * 2);
126
128
  if (new_plugin->argv == NULL){
130
132
  }
131
133
  new_plugin->argv[0] = copy_name;
132
134
  new_plugin->argv[1] = NULL;
133
 
 
 
135
  
134
136
  new_plugin->environ = malloc(sizeof(char *));
135
137
  if(new_plugin->environ == NULL){
136
138
    free(copy_name);
139
141
    return NULL;
140
142
  }
141
143
  new_plugin->environ[0] = NULL;
 
144
  
142
145
  /* Append the new plugin to the list */
143
 
  *plugin_list = new_plugin;
 
146
  plugin_list = new_plugin;
144
147
  return new_plugin;
145
148
}
146
149
 
180
183
  if(p == NULL){
181
184
    return false;
182
185
  }
 
186
  /* namelen = length of name of environment variable */
 
187
  size_t namelen = (size_t)(strchrnul(def, '=') - def);
 
188
  /* Search for this environment variable */
 
189
  for(char **e = p->environ; *e != NULL; e++){
 
190
    if(strncmp(*e, def, namelen+1) == 0){
 
191
      /* Refuse to add an existing variable */
 
192
      return true;
 
193
    }
 
194
  }
183
195
  return add_to_char_array(def, &(p->environ), &(p->envc));
184
196
}
185
197
 
186
 
 
187
198
/*
188
199
 * Based on the example in the GNU LibC manual chapter 13.13 "File
189
200
 * Descriptor Flags".
200
211
  return fcntl(fd, F_SETFD, ret | FD_CLOEXEC);
201
212
}
202
213
 
203
 
process *process_list = NULL;
204
214
 
205
215
/* Mark processes as completed when they exit, and save their exit
206
216
   status. */
207
217
void handle_sigchld(__attribute__((unused)) int sig){
208
218
  while(true){
209
 
    process *proc = process_list;
 
219
    plugin *proc = plugin_list;
210
220
    int status;
211
221
    pid_t pid = waitpid(-1, &status, WNOHANG);
212
222
    if(pid == 0){
234
244
  }
235
245
}
236
246
 
 
247
/* Prints out a password to stdout */
237
248
bool print_out_password(const char *buffer, size_t length){
238
249
  ssize_t ret;
239
250
  if(length>0 and buffer[length-1] == '\n'){
249
260
  return true;
250
261
}
251
262
 
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);
 
263
/* Removes and free a plugin from the plugin list */
 
264
static void free_plugin(plugin *plugin_node){
 
265
  
 
266
  for(char **arg = plugin_node->argv; *arg != NULL; arg++){
 
267
    free(*arg);
 
268
  }
 
269
  free(plugin_node->argv);
 
270
  for(char **env = plugin_node->environ; *env != NULL; env++){
 
271
    free(*env);
 
272
  }
 
273
  free(plugin_node->environ);
 
274
  free(plugin_node->buffer);
 
275
 
 
276
  /* Removes the plugin from the singly-linked list */
 
277
  if(plugin_node == plugin_list){
 
278
    /* First one - simple */
 
279
    plugin_list = plugin_list->next;
 
280
  } else {
 
281
    /* Second one or later */
 
282
    for(plugin *p = plugin_list; p != NULL; p = p->next){
 
283
      if(p->next == plugin_node){
 
284
        p->next = plugin_node->next;
 
285
        break;
 
286
      }
 
287
    }
 
288
  }
 
289
  
 
290
  free(plugin_node);
 
291
}
 
292
 
 
293
static void free_plugin_list(void){
 
294
  while(plugin_list != NULL){
 
295
    free_plugin(plugin_list);
264
296
  }
265
297
}
266
298
 
304
336
    { .name = "global-options", .key = 'g',
305
337
      .arg = "OPTION[,OPTION[,...]]",
306
338
      .doc = "Options passed to all plugins" },
307
 
    { .name = "global-envs", .key = 'e',
 
339
    { .name = "global-env", .key = 'e',
308
340
      .arg = "VAR=value",
309
341
      .doc = "Environment variable passed to all plugins" },
310
342
    { .name = "options-for", .key = 'o',
311
343
      .arg = "PLUGIN:OPTION[,OPTION[,...]]",
312
344
      .doc = "Options passed only to specified plugin" },
313
 
    { .name = "envs-for", .key = 'f',
 
345
    { .name = "env-for", .key = 'f',
314
346
      .arg = "PLUGIN:ENV=value",
315
347
      .doc = "Environment variable passed to specified plugin" },
316
348
    { .name = "disable", .key = 'd',
333
365
    { .name = NULL }
334
366
  };
335
367
  
336
 
  error_t parse_opt (int key, char *arg, struct argp_state *state) {
 
368
  error_t parse_opt (int key, char *arg, __attribute__((unused))
 
369
                     struct argp_state *state) {
337
370
    /* Get the INPUT argument from `argp_parse', which we know is a
338
371
       pointer to our plugin list pointer. */
339
 
    plugin **plugins = state->input;
340
372
    switch (key) {
341
 
    case 'g':
 
373
    case 'g':                   /* --global-options */
342
374
      if (arg != NULL){
343
375
        char *p;
344
376
        while((p = strsep(&arg, ",")) != NULL){
345
377
          if(p[0] == '\0'){
346
378
            continue;
347
379
          }
348
 
          if(not add_argument(getplugin(NULL, plugins), p)){
 
380
          if(not add_argument(getplugin(NULL), p)){
349
381
            perror("add_argument");
350
382
            return ARGP_ERR_UNKNOWN;
351
383
          }
352
384
        }
353
385
      }
354
386
      break;
355
 
    case 'e':
 
387
    case 'e':                   /* --global-env */
356
388
      if(arg == NULL){
357
389
        break;
358
390
      }
361
393
        if(envdef == NULL){
362
394
          break;
363
395
        }
364
 
        if(not add_environment(getplugin(NULL, plugins), envdef)){
 
396
        if(not add_environment(getplugin(NULL), envdef)){
365
397
          perror("add_environment");
366
398
        }
367
399
      }
368
400
      break;
369
 
    case 'o':
 
401
    case 'o':                   /* --options-for */
370
402
      if (arg != NULL){
371
403
        char *p_name = strsep(&arg, ":");
372
404
        if(p_name[0] == '\0'){
382
414
            if(p[0] == '\0'){
383
415
              continue;
384
416
            }
385
 
            if(not add_argument(getplugin(p_name, plugins), p)){
 
417
            if(not add_argument(getplugin(p_name), p)){
386
418
              perror("add_argument");
387
419
              return ARGP_ERR_UNKNOWN;
388
420
            }
390
422
        }
391
423
      }
392
424
      break;
393
 
    case 'f':
 
425
    case 'f':                   /* --env-for */
394
426
      if(arg == NULL){
395
427
        break;
396
428
      }
404
436
          break;
405
437
        }
406
438
        envdef++;
407
 
        if(not add_environment(getplugin(p_name, plugins), envdef)){
 
439
        if(not add_environment(getplugin(p_name), envdef)){
408
440
          perror("add_environment");
409
441
        }
410
442
      }
411
443
      break;
412
 
    case 'd':
 
444
    case 'd':                   /* --disable */
413
445
      if (arg != NULL){
414
 
        plugin *p = getplugin(arg, plugins);
 
446
        plugin *p = getplugin(arg);
415
447
        if(p == NULL){
416
448
          return ARGP_ERR_UNKNOWN;
417
449
        }
418
450
        p->disabled = true;
419
451
      }
420
452
      break;
421
 
    case 128:
 
453
    case 128:                   /* --plugin-dir */
422
454
      plugindir = strdup(arg);
423
455
      if(plugindir == NULL){
424
456
        perror("strdup");
425
457
      }      
426
458
      break;
427
 
    case 129:
 
459
    case 129:                   /* --config-file */
428
460
      argfile = strdup(arg);
429
461
      if(argfile == NULL){
430
462
        perror("strdup");
431
463
      }
432
464
      break;      
433
 
    case 130:
 
465
    case 130:                   /* --userid */
434
466
      uid = (uid_t)strtol(arg, NULL, 10);
435
467
      break;
436
 
    case 131:
 
468
    case 131:                   /* --groupid */
437
469
      gid = (gid_t)strtol(arg, NULL, 10);
438
470
      break;
439
 
    case 132:
 
471
    case 132:                   /* --debug */
440
472
      debug = true;
441
473
      break;
442
474
    case ARGP_KEY_ARG:
450
482
    return 0;
451
483
  }
452
484
  
453
 
  plugin *plugin_list = NULL;
454
 
  
455
485
  struct argp argp = { .options = options, .parser = parse_opt,
456
486
                       .args_doc = "[+PLUS_SEPARATED_OPTIONS]",
457
487
                       .doc = "Mandos plugin runner -- Run plugins" };
458
488
  
459
 
  ret = argp_parse (&argp, argc, argv, 0, 0, &plugin_list);
 
489
  ret = argp_parse (&argp, argc, argv, 0, 0, NULL);
460
490
  if (ret == ARGP_ERR_UNKNOWN){
461
491
    fprintf(stderr, "Unknown error while parsing arguments\n");
462
492
    exitstatus = EXIT_FAILURE;
463
493
    goto fallback;
464
494
  }
465
495
 
 
496
  /* Opens the configfile if aviable */
466
497
  if (argfile == NULL){
467
498
    conffp = fopen(AFILE, "r");
468
499
  } else {
469
500
    conffp = fopen(argfile, "r");
470
 
  }
471
 
  
 
501
  }  
472
502
  if(conffp != NULL){
473
503
    char *org_line = NULL;
474
504
    char *p, *arg, *new_arg, *line;
486
516
    }
487
517
    custom_argv[0] = argv[0];
488
518
    custom_argv[1] = NULL;
489
 
    
 
519
 
 
520
    /* for each line in the config file, strip whitespace and ignore
 
521
       commented text */
490
522
    while(true){
491
523
      sret = getline(&org_line, &size, conffp);
492
524
      if(sret == -1){
530
562
      goto fallback;
531
563
    }
532
564
  }
533
 
 
 
565
  /* If there was any arguments from configuration file,
 
566
     pass them to parser as command arguments */
534
567
  if(custom_argv != NULL){
535
 
    ret = argp_parse (&argp, custom_argc, custom_argv, 0, 0, &plugin_list);
 
568
    ret = argp_parse (&argp, custom_argc, custom_argv, 0, 0, NULL);
536
569
    if (ret == ARGP_ERR_UNKNOWN){
537
570
      fprintf(stderr, "Unknown error while parsing arguments\n");
538
571
      exitstatus = EXIT_FAILURE;
553
586
      }
554
587
    }
555
588
  }
556
 
  
 
589
 
 
590
  /* Strip permissions down to nobody */
557
591
  ret = setuid(uid);
558
592
  if (ret == -1){
559
593
    perror("setuid");
560
 
  }
561
 
  
 
594
  }  
562
595
  setgid(gid);
563
596
  if (ret == -1){
564
597
    perror("setgid");
590
623
  }
591
624
  
592
625
  FD_ZERO(&rfds_all);
593
 
  
 
626
 
 
627
  /* Read and execute any executable in the plugin directory*/
594
628
  while(true){
595
629
    dirst = readdir(dir);
596
630
    
627
661
          break;
628
662
        }
629
663
      }
630
 
      
631
664
      if(bad_name){
632
665
        continue;
633
666
      }
634
 
      
635
667
      for(const char **suf = bad_suffixes; *suf != NULL; suf++){
636
668
        size_t suf_len = strlen(*suf);
637
669
        if((d_name_len >= suf_len)
664
696
      free(filename);
665
697
      continue;
666
698
    }
667
 
    
 
699
 
 
700
    /* Ignore non-executable files */
668
701
    if (not S_ISREG(st.st_mode) or (access(filename, X_OK) != 0)){
669
702
      if(debug){
670
703
        fprintf(stderr, "Ignoring plugin dir entry \"%s\""
673
706
      free(filename);
674
707
      continue;
675
708
    }
676
 
    plugin *p = getplugin(dirst->d_name, &plugin_list);
 
709
    
 
710
    plugin *p = getplugin(dirst->d_name);
677
711
    if(p == NULL){
678
712
      perror("getplugin");
679
713
      free(filename);
689
723
    }
690
724
    {
691
725
      /* Add global arguments to argument list for this plugin */
692
 
      plugin *g = getplugin(NULL, &plugin_list);
 
726
      plugin *g = getplugin(NULL);
693
727
      if(g != NULL){
694
728
        for(char **a = g->argv + 1; *a != NULL; a++){
695
729
          if(not add_argument(p, *a)){
727
761
      exitstatus = EXIT_FAILURE;
728
762
      goto fallback;
729
763
    }
 
764
    /* Ask OS to automatic close the pipe on exec */
730
765
    ret = set_cloexec_flag(pipefd[0]);
731
766
    if(ret < 0){
732
767
      perror("set_cloexec_flag");
790
825
      }
791
826
      /* no return */
792
827
    }
793
 
    /* parent process */
 
828
    /* Parent process */
 
829
    close(pipefd[1]);           /* Close unused write end of pipe */
794
830
    free(filename);
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");
 
831
    plugin *new_plugin = getplugin(dirst->d_name);
 
832
    if (new_plugin == NULL){
 
833
      perror("getplugin");
799
834
      ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
800
835
      if(ret < 0){
801
 
        perror("sigprocmask");
 
836
        perror("sigprocmask");
802
837
      }
803
838
      exitstatus = EXIT_FAILURE;
804
839
      goto fallback;
805
840
    }
806
841
    
807
 
    *new_process = (struct process){ .pid = pid,
808
 
                                     .fd = pipefd[0],
809
 
                                     .next = process_list };
810
 
    // List handling
811
 
    process_list = new_process;
 
842
    new_plugin->pid = pid;
 
843
    new_plugin->fd = pipefd[0];
 
844
    
812
845
    /* Unblock SIGCHLD so signal handler can be run if this process
813
846
       has already completed */
814
847
    ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
818
851
      goto fallback;
819
852
    }
820
853
    
821
 
    FD_SET(new_process->fd, &rfds_all);
 
854
    FD_SET(new_plugin->fd, &rfds_all);
822
855
    
823
 
    if (maxfd < new_process->fd){
824
 
      maxfd = new_process->fd;
 
856
    if (maxfd < new_plugin->fd){
 
857
      maxfd = new_plugin->fd;
825
858
    }
826
859
    
827
860
  }
828
 
 
829
 
  free_plugin_list(plugin_list);
830
 
  plugin_list = NULL;
831
861
  
832
862
  closedir(dir);
833
863
  dir = NULL;
834
 
    
835
 
  if (process_list == NULL){
836
 
    fprintf(stderr, "No plugin processes started. Incorrect plugin"
837
 
            " directory?\n");
838
 
    process_list = NULL;
 
864
 
 
865
  for(plugin *p = plugin_list; p != NULL; p = p->next){
 
866
    if(p->pid != 0){
 
867
      break;
 
868
    }
 
869
    if(p->next == NULL){
 
870
      fprintf(stderr, "No plugin processes started. Incorrect plugin"
 
871
              " directory?\n");
 
872
      free_plugin_list();
 
873
    }
839
874
  }
840
 
  while(process_list){
 
875
 
 
876
  /* Main loop while running plugins exist */
 
877
  while(plugin_list){
841
878
    fd_set rfds = rfds_all;
842
879
    int select_ret = select(maxfd+1, &rfds, NULL, NULL, NULL);
843
880
    if (select_ret == -1){
847
884
    }
848
885
    /* OK, now either a process completed, or something can be read
849
886
       from one of them */
850
 
    for(process *proc = process_list; proc ; proc = proc->next){
 
887
    for(plugin *proc = plugin_list; proc != NULL; proc = proc->next){
851
888
      /* Is this process completely done? */
852
889
      if(proc->eof and proc->completed){
853
890
        /* Only accept the plugin output if it exited cleanly */
854
891
        if(not WIFEXITED(proc->status)
855
892
           or WEXITSTATUS(proc->status) != 0){
856
893
          /* Bad exit by plugin */
 
894
 
857
895
          if(debug){
858
896
            if(WIFEXITED(proc->status)){
859
897
              fprintf(stderr, "Plugin %u exited with status %d\n",
868
906
                      (unsigned int) (proc->pid));
869
907
            }
870
908
          }
 
909
          
871
910
          /* Remove the plugin */
872
911
          FD_CLR(proc->fd, &rfds_all);
 
912
 
873
913
          /* Block signal while modifying process_list */
874
914
          ret = sigprocmask(SIG_BLOCK, &sigchld_action.sa_mask, NULL);
875
915
          if(ret < 0){
877
917
            exitstatus = EXIT_FAILURE;
878
918
            goto fallback;
879
919
          }
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
 
          }
 
920
          free_plugin(proc);
893
921
          /* We are done modifying process list, so unblock signal */
894
922
          ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask,
895
923
                             NULL);
896
924
          if(ret < 0){
897
925
            perror("sigprocmask");
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;
 
926
            exitstatus = EXIT_FAILURE;
 
927
            goto fallback;
 
928
          }
 
929
          
 
930
          if(plugin_list == NULL){
 
931
            break;
 
932
          }
 
933
          continue;
905
934
        }
 
935
        
906
936
        /* This process exited nicely, so print its buffer */
907
937
 
908
938
        bool bret = print_out_password(proc->buffer,
913
943
        }
914
944
        goto fallback;
915
945
      }
 
946
      
916
947
      /* This process has not completed.  Does it have any output? */
917
948
      if(proc->eof or not FD_ISSET(proc->fd, &rfds)){
918
949
        /* This process had nothing to say at this time */
948
979
 
949
980
 fallback:
950
981
  
951
 
  if(process_list == NULL or exitstatus != EXIT_SUCCESS){
 
982
  if(plugin_list == NULL or exitstatus != EXIT_SUCCESS){
952
983
    /* Fallback if all plugins failed, none are found or an error
953
984
       occured */
954
985
    bool bret;
974
1005
    }
975
1006
    free(custom_argv);
976
1007
  }
977
 
  free_plugin_list(plugin_list);
978
1008
  
979
1009
  if(dir != NULL){
980
1010
    closedir(dir);
981
1011
  }
982
1012
  
983
1013
  /* Free the process list and kill the processes */
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");
 
1014
  for(plugin *p = plugin_list; p != NULL; p = p->next){
 
1015
    if(p->pid != 0){
 
1016
      close(p->fd);
 
1017
      ret = kill(p->pid, SIGTERM);
 
1018
      if(ret == -1 and errno != ESRCH){
 
1019
        /* Set-uid proccesses might not get closed */
 
1020
        perror("kill");
 
1021
      }
991
1022
    }
992
 
    free(process_list->buffer);
993
 
    free(process_list);
994
1023
  }
995
1024
  
996
1025
  /* Wait for any remaining child processes to terminate */
1001
1030
    perror("wait");
1002
1031
  }
1003
1032
 
 
1033
  free_plugin_list();
 
1034
  
1004
1035
  free(plugindir);
1005
1036
  free(argfile);
1006
1037