/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

  • Committer: Teddy Hogeborn
  • Date: 2008-08-15 21:09:25 UTC
  • mfrom: (24.1.52 mandos)
  • Revision ID: teddy@fukt.bsnet.se-20080815210925-32718zu3nrlotix5
Merge.

* plugin-runner.c (ARGFILE): Renamed to "plugin-runner.conf".

Show diffs side-by-side

added added

removed removed

Lines of Context:
21
21
 * Contact the authors at <mandos@fukt.bsnet.se>.
22
22
 */
23
23
 
24
 
#define _GNU_SOURCE             /* TEMP_FAILURE_RETRY(), getline(),
25
 
                                   asprintf() */
 
24
#define _GNU_SOURCE             /* TEMP_FAILURE_RETRY(), getline() */
 
25
 
26
26
#include <stddef.h>             /* size_t, NULL */
27
27
#include <stdlib.h>             /* malloc(), exit(), EXIT_FAILURE,
28
28
                                   EXIT_SUCCESS, realloc() */
51
51
                                   close() */
52
52
#include <fcntl.h>              /* fcntl(), F_GETFD, F_SETFD,
53
53
                                   FD_CLOEXEC */
54
 
#include <string.h>             /* strsep, strlen(), asprintf() */
 
54
#include <string.h>             /* strsep, strlen(), strcpy(),
 
55
                                   strcat() */
55
56
#include <errno.h>              /* errno */
56
57
#include <argp.h>               /* struct argp_option, struct
57
58
                                   argp_state, struct argp,
87
88
  char *name;                   /* can be NULL or any plugin name */
88
89
  char **argv;
89
90
  int argc;
90
 
  char **environ;
91
 
  int envc;
92
91
  bool disabled;
93
92
  struct plugin *next;
94
93
} plugin;
103
102
  /* Create a new plugin */
104
103
  plugin *new_plugin = malloc(sizeof(plugin));
105
104
  if (new_plugin == NULL){
106
 
    return NULL;
107
 
  }
108
 
  char *copy_name = NULL;
109
 
  if(name != NULL){
110
 
    copy_name = strdup(name);
111
 
  }
112
 
  if(copy_name == NULL){
113
 
    return NULL;
114
 
  }
115
 
  
116
 
  *new_plugin = (plugin) { .name = copy_name,
117
 
                           .argc = 1,
118
 
                           .envc = 0,
119
 
                           .disabled = false,
120
 
                           .next = *plugin_list };
121
 
  
 
105
    perror("malloc");
 
106
    exit(EXIT_FAILURE);
 
107
  }
 
108
  new_plugin->name = name;
122
109
  new_plugin->argv = malloc(sizeof(char *) * 2);
123
110
  if (new_plugin->argv == NULL){
124
 
    free(copy_name);
125
 
    free(new_plugin);
126
 
    return NULL;
 
111
    perror("malloc");
 
112
    exit(EXIT_FAILURE);
127
113
  }
128
 
  new_plugin->argv[0] = copy_name;
 
114
  new_plugin->argv[0] = name;
129
115
  new_plugin->argv[1] = NULL;
130
 
 
131
 
  new_plugin->environ = malloc(sizeof(char *));
132
 
  if(new_plugin->environ == NULL){
133
 
    free(copy_name);
134
 
    free(new_plugin->argv);
135
 
    free(new_plugin);
136
 
    return NULL;
137
 
  }
138
 
  new_plugin->environ[0] = NULL;
 
116
  new_plugin->argc = 1;
 
117
  new_plugin->disabled = false;
 
118
  new_plugin->next = *plugin_list;
139
119
  /* Append the new plugin to the list */
140
120
  *plugin_list = new_plugin;
141
121
  return new_plugin;
142
122
}
143
123
 
144
 
/* Helper function for add_argument and add_environment */
145
 
static bool add_to_char_array(const char *new, char ***array,
146
 
                              int *len){
147
 
  /* Resize the pointed-to array to hold one more pointer */
148
 
  *array = realloc(*array, sizeof(char *)
149
 
                   * (size_t) ((*len) + 2));
150
 
  /* Malloc check */
151
 
  if(*array == NULL){
152
 
    return false;
153
 
  }
154
 
  /* Make a copy of the new string */
155
 
  char *copy = strdup(new);
156
 
  if(copy == NULL){
157
 
    return false;
158
 
  }
159
 
  /* Insert the copy */
160
 
  (*array)[*len] = copy;
161
 
  (*len)++;
162
 
  /* Add a new terminating NULL pointer to the last element */
163
 
  (*array)[*len] = NULL;
164
 
  return true;
165
 
}
166
 
 
167
 
/* Add to a plugin's argument vector */
168
 
static bool add_argument(plugin *p, const char *arg){
169
 
  if(p == NULL){
170
 
    return false;
171
 
  }
172
 
  return add_to_char_array(arg, &(p->argv), &(p->argc));
173
 
}
174
 
 
175
 
/* Add to a plugin's environment */
176
 
static bool add_environment(plugin *p, const char *def){
177
 
  if(p == NULL){
178
 
    return false;
179
 
  }
180
 
  return add_to_char_array(def, &(p->environ), &(p->envc));
181
 
}
182
 
 
 
124
static void addargument(plugin *p, char *arg){
 
125
  p->argv[p->argc] = arg;
 
126
  p->argv = realloc(p->argv, sizeof(char *) * (size_t)(p->argc + 2));
 
127
  if (p->argv == NULL){
 
128
    perror("malloc");
 
129
    exit(EXIT_FAILURE);
 
130
  }
 
131
  p->argc++;
 
132
  p->argv[p->argc] = NULL;
 
133
}
183
134
 
184
135
/*
185
136
 * Based on the example in the GNU LibC manual chapter 13.13 "File
235
186
  return true;
236
187
}
237
188
 
238
 
char **add_to_argv(char **argv, int *argc, char *arg){
 
189
char ** addcustomargument(char **argv, int *argc, char *arg){
 
190
 
239
191
  if (argv == NULL){
240
192
    *argc = 1;
241
193
    argv = malloc(sizeof(char*) * 2);
256
208
  return argv;
257
209
}
258
210
 
259
 
static void free_plugin_list(plugin *plugin_list){
260
 
  for(plugin *next = plugin_list; plugin_list != NULL; plugin_list = next){
261
 
    next = plugin_list->next;
262
 
    free(plugin_list->name);
263
 
    for(char **arg = plugin_list->argv; *arg != NULL; arg++){
264
 
      free(*arg);
265
 
    }    
266
 
    free(plugin_list->argv);
267
 
    for(char **env = plugin_list->environ; *env != NULL; env++){
268
 
      free(*env);
269
 
    }
270
 
    free(plugin_list->environ);
271
 
    free(plugin_list);
272
 
  }  
273
 
}
274
 
 
275
211
int main(int argc, char *argv[]){
276
212
  const char *plugindir = "/lib/mandos/plugins.d";
277
213
  const char *argfile = ARGFILE;
295
231
  /* Establish a signal handler */
296
232
  sigemptyset(&sigchld_action.sa_mask);
297
233
  ret = sigaddset(&sigchld_action.sa_mask, SIGCHLD);
298
 
  if(ret == -1){
 
234
  if(ret < 0){
299
235
    perror("sigaddset");
300
 
    exitstatus = EXIT_FAILURE;
301
 
    goto fallback;
 
236
    exit(EXIT_FAILURE);
302
237
  }
303
238
  ret = sigaction(SIGCHLD, &sigchld_action, &old_sigchld_action);
304
 
  if(ret == -1){
 
239
  if(ret < 0){
305
240
    perror("sigaction");
306
 
    exitstatus = EXIT_FAILURE;    
307
 
    goto fallback;
 
241
    exit(EXIT_FAILURE);
308
242
  }
309
243
  
310
244
  /* The options we understand. */
312
246
    { .name = "global-options", .key = 'g',
313
247
      .arg = "OPTION[,OPTION[,...]]",
314
248
      .doc = "Options passed to all plugins" },
315
 
    { .name = "global-envs", .key = 'e',
316
 
      .arg = "VAR=value",
317
 
      .doc = "Environment variable passed to all plugins" },
318
249
    { .name = "options-for", .key = 'o',
319
250
      .arg = "PLUGIN:OPTION[,OPTION[,...]]",
320
251
      .doc = "Options passed only to specified plugin" },
321
 
    { .name = "envs-for", .key = 'f',
322
 
      .arg = "PLUGIN:ENV=value",
323
 
      .doc = "Environment variable passed to specified plugin" },
324
252
    { .name = "disable", .key = 'd',
325
253
      .arg = "PLUGIN",
326
254
      .doc = "Disable a specific plugin", .group = 1 },
350
278
          if(p[0] == '\0'){
351
279
            continue;
352
280
          }
353
 
          if(not add_argument(getplugin(NULL, plugins), p)){
354
 
            perror("add_argument");
355
 
            return ARGP_ERR_UNKNOWN;
356
 
          }
357
 
        }
358
 
      }
359
 
      break;
360
 
    case 'e':
361
 
      if(arg == NULL){
362
 
        break;
363
 
      }
364
 
      {
365
 
        char *envdef = strdup(arg);
366
 
        if(envdef == NULL){
367
 
          break;
368
 
        }
369
 
        if(not add_environment(getplugin(NULL, plugins), envdef)){
370
 
          perror("add_environment");
 
281
          addargument(getplugin(NULL, plugins), p);
371
282
        }
372
283
      }
373
284
      break;
374
285
    case 'o':
375
286
      if (arg != NULL){
376
 
        char *p_name = strsep(&arg, ":");
377
 
        if(p_name[0] == '\0'){
 
287
        char *name = strsep(&arg, ":");
 
288
        if(name[0] == '\0'){
378
289
          break;
379
290
        }
380
291
        char *opt = strsep(&arg, ":");
387
298
            if(p[0] == '\0'){
388
299
              continue;
389
300
            }
390
 
            if(not add_argument(getplugin(p_name, plugins), p)){
391
 
              perror("add_argument");
392
 
              return ARGP_ERR_UNKNOWN;
393
 
            }
 
301
            addargument(getplugin(name, plugins), p);
394
302
          }
395
303
        }
396
304
      }
397
305
      break;
398
 
    case 'f':
399
 
      if(arg == NULL){
400
 
        break;
401
 
      }
402
 
      {
403
 
        char *envdef = strchr(arg, ':');
404
 
        if(envdef == NULL){
405
 
          break;
406
 
        }
407
 
        char *p_name = strndup(arg, (size_t) (envdef-arg));
408
 
        if(p_name == NULL){
409
 
          break;
410
 
        }
411
 
        envdef++;
412
 
        if(not add_environment(getplugin(p_name, plugins), envdef)){
413
 
          perror("add_environment");
414
 
        }
415
 
      }
416
 
      break;
417
306
    case 'd':
418
307
      if (arg != NULL){
419
 
        plugin *p = getplugin(arg, plugins);
420
 
        if(p == NULL){
421
 
          return ARGP_ERR_UNKNOWN;
422
 
        }
423
 
        p->disabled = true;
 
308
        getplugin(arg, plugins)->disabled = true;
424
309
      }
425
310
      break;
426
311
    case 128:
436
321
      debug = true;
437
322
      break;
438
323
    case ARGP_KEY_ARG:
439
 
      fprintf(stderr, "Ignoring unknown argument \"%s\"\n", arg);
 
324
      fprintf(stderr, "Ignoring unknown argument \"%s\"\n", arg); 
440
325
      break;
441
326
    case ARGP_KEY_END:
442
327
      break;
456
341
  if (ret == ARGP_ERR_UNKNOWN){
457
342
    fprintf(stderr, "Unknown error while parsing arguments\n");
458
343
    exitstatus = EXIT_FAILURE;
459
 
    goto fallback;
 
344
    goto end;
460
345
  }
461
346
 
462
347
  conffp = fopen(argfile, "r");
463
348
  if(conffp != NULL){
464
349
    char *org_line = NULL;
465
 
    char *p, *arg, *new_arg, *line;
466
350
    size_t size = 0;
467
351
    ssize_t sret;
 
352
    char *p, *arg, *new_arg, *line;
468
353
    const char whitespace_delims[] = " \r\t\f\v\n";
469
354
    const char comment_delim[] = "#";
470
355
 
481
366
          continue;
482
367
        }
483
368
        new_arg = strdup(p);
484
 
        custom_argv = add_to_argv(custom_argv, &custom_argc, new_arg);
 
369
        custom_argv = addcustomargument(custom_argv, &custom_argc, new_arg);
485
370
        if (custom_argv == NULL){
486
 
          perror("add_to_argv");
 
371
          perror("addcustomargument");
487
372
          exitstatus = EXIT_FAILURE;
488
 
          goto fallback;
 
373
          goto end;
489
374
        }
490
375
      }
491
376
    }
496
381
    if (errno == EMFILE or errno == ENFILE or errno == ENOMEM){
497
382
      perror("fopen");
498
383
      exitstatus = EXIT_FAILURE;
499
 
      goto fallback;
 
384
      goto end;
500
385
    }
501
386
  }
502
387
 
506
391
    if (ret == ARGP_ERR_UNKNOWN){
507
392
      fprintf(stderr, "Unknown error while parsing arguments\n");
508
393
      exitstatus = EXIT_FAILURE;
509
 
      goto fallback;
 
394
      goto end;
510
395
    }
511
396
  }
512
397
  
517
402
      for(char **a = p->argv; *a != NULL; a++){
518
403
        fprintf(stderr, "\tArg: %s\n", *a);
519
404
      }
520
 
      fprintf(stderr, "...and %u environment variables\n", p->envc);
521
 
      for(char **a = p->environ; *a != NULL; a++){
522
 
        fprintf(stderr, "\t%s\n", *a);
523
 
      }
524
405
    }
525
406
  }
526
407
  
538
419
  if(dir == NULL){
539
420
    perror("Could not open plugin dir");
540
421
    exitstatus = EXIT_FAILURE;
541
 
    goto fallback;
 
422
    goto end;
542
423
  }
543
424
  
544
425
  /* Set the FD_CLOEXEC flag on the directory, if possible */
549
430
      if(ret < 0){
550
431
        perror("set_cloexec_flag");
551
432
        exitstatus = EXIT_FAILURE;
552
 
        goto fallback;
 
433
        goto end;
553
434
      }
554
435
    }
555
436
  }
564
445
      if (errno == EBADF){
565
446
        perror("readdir");
566
447
        exitstatus = EXIT_FAILURE;
567
 
        goto fallback;
 
448
        goto end;
568
449
      }
569
450
      break;
570
451
    }
615
496
        continue;
616
497
      }
617
498
    }
618
 
 
619
 
    char *filename;
620
 
    ret = asprintf(&filename, "%s/%s", plugindir, dirst->d_name);
621
 
    if(ret < 0){
622
 
      perror("asprintf");
 
499
    
 
500
    char *filename = malloc(d_name_len + strlen(plugindir) + 2);
 
501
    if (filename == NULL){
 
502
      perror("malloc");
623
503
      continue;
624
504
    }
 
505
    strcpy(filename, plugindir); /* Spurious warning */
 
506
    strcat(filename, "/");      /* Spurious warning */
 
507
    strcat(filename, dirst->d_name); /* Spurious warning */
625
508
    
626
509
    ret = stat(filename, &st);
627
510
    if (ret == -1){
638
521
      free(filename);
639
522
      continue;
640
523
    }
641
 
    plugin *p = getplugin(dirst->d_name, &plugin_list);
642
 
    if(p == NULL){
643
 
      perror("getplugin");
644
 
      free(filename);
645
 
      continue;
646
 
    }
647
 
    if(p->disabled){
 
524
    if(getplugin(dirst->d_name, &plugin_list)->disabled){
648
525
      if(debug){
649
526
        fprintf(stderr, "Ignoring disabled plugin \"%s\"\n",
650
527
                dirst->d_name);
652
529
      free(filename);
653
530
      continue;
654
531
    }
 
532
    plugin *p = getplugin(dirst->d_name, &plugin_list);
655
533
    {
656
534
      /* Add global arguments to argument list for this plugin */
657
535
      plugin *g = getplugin(NULL, &plugin_list);
658
 
      if(g != NULL){
659
 
        for(char **a = g->argv + 1; *a != NULL; a++){
660
 
          if(not add_argument(p, *a)){
661
 
            perror("add_argument");
662
 
          }
663
 
        }
664
 
        /* Add global environment variables */
665
 
        for(char **e = g->environ; *e != NULL; e++){
666
 
          if(not add_environment(p, *e)){
667
 
            perror("add_environment");
668
 
          }
669
 
        }
670
 
      }
671
 
    }
672
 
    /* If this plugin has any environment variables, we will call
673
 
       using execve and need to duplicate the environment from this
674
 
       process, too. */
675
 
    if(p->environ[0] != NULL){
676
 
      for(char **e = environ; *e != NULL; e++){
677
 
        char *copy = strdup(*e);
678
 
        if(copy == NULL){
679
 
          perror("strdup");
680
 
          continue;
681
 
        }
682
 
        if(not add_environment(p, copy)){
683
 
          perror("add_environment");
684
 
        }
685
 
      }
686
 
    }
687
 
    
 
536
      for(char **a = g->argv + 1; *a != NULL; a++){
 
537
        addargument(p, *a);
 
538
      }
 
539
    }
688
540
    int pipefd[2]; 
689
541
    ret = pipe(pipefd);
690
542
    if (ret == -1){
691
543
      perror("pipe");
692
544
      exitstatus = EXIT_FAILURE;
693
 
      goto fallback;
 
545
      goto end;
694
546
    }
695
547
    ret = set_cloexec_flag(pipefd[0]);
696
548
    if(ret < 0){
697
549
      perror("set_cloexec_flag");
698
550
      exitstatus = EXIT_FAILURE;
699
 
      goto fallback;
 
551
      goto end;
700
552
    }
701
553
    ret = set_cloexec_flag(pipefd[1]);
702
554
    if(ret < 0){
703
555
      perror("set_cloexec_flag");
704
556
      exitstatus = EXIT_FAILURE;
705
 
      goto fallback;
 
557
      goto end;
706
558
    }
707
559
    /* Block SIGCHLD until process is safely in process list */
708
560
    ret = sigprocmask (SIG_BLOCK, &sigchld_action.sa_mask, NULL);
709
561
    if(ret < 0){
710
562
      perror("sigprocmask");
711
563
      exitstatus = EXIT_FAILURE;
712
 
      goto fallback;
 
564
      goto end;
713
565
    }
714
566
    // Starting a new process to be watched
715
567
    pid_t pid = fork();
716
568
    if(pid == -1){
717
569
      perror("fork");
718
570
      exitstatus = EXIT_FAILURE;
719
 
      goto fallback;
 
571
      goto end;
720
572
    }
721
573
    if(pid == 0){
722
574
      /* this is the child process */
742
594
           above and must now close it manually here. */
743
595
        closedir(dir);
744
596
      }
745
 
      if(p->environ[0] == NULL){
746
 
        if(execv(filename, p->argv) < 0){
747
 
          perror("execv");
748
 
          _exit(EXIT_FAILURE);
749
 
        }
750
 
      } else {
751
 
        if(execve(filename, p->argv, p->environ) < 0){
752
 
          perror("execve");
753
 
          _exit(EXIT_FAILURE);
754
 
        }
 
597
      if(execv(filename, p->argv) < 0){
 
598
        perror("execv");
 
599
        _exit(EXIT_FAILURE);
755
600
      }
756
601
      /* no return */
757
602
    }
766
611
        perror("sigprocmask");
767
612
      }
768
613
      exitstatus = EXIT_FAILURE;
769
 
      goto fallback;
 
614
      goto end;
770
615
    }
771
616
    
772
617
    *new_process = (struct process){ .pid = pid,
780
625
    if(ret < 0){
781
626
      perror("sigprocmask");
782
627
      exitstatus = EXIT_FAILURE;
783
 
      goto fallback;
 
628
      goto end;
784
629
    }
785
630
    
786
631
    FD_SET(new_process->fd, &rfds_all);
791
636
    
792
637
  }
793
638
  
794
 
  free_plugin_list(plugin_list);
 
639
  /* Free the plugin list */
 
640
  for(plugin *next; plugin_list != NULL; plugin_list = next){
 
641
    next = plugin_list->next;
 
642
    free(plugin_list->argv);
 
643
    free(plugin_list);
 
644
  }
795
645
  
796
646
  closedir(dir);
797
647
  dir = NULL;
807
657
    if (select_ret == -1){
808
658
      perror("select");
809
659
      exitstatus = EXIT_FAILURE;
810
 
      goto fallback;
 
660
      goto end;
811
661
    }
812
662
    /* OK, now either a process completed, or something can be read
813
663
       from one of them */
839
689
          if(ret < 0){
840
690
            perror("sigprocmask");
841
691
            exitstatus = EXIT_FAILURE;
842
 
            goto fallback;
 
692
            goto end;
843
693
          }
844
694
          /* Delete this process entry from the list */
845
695
          if(process_list == proc){
874
724
          perror("print_out_password");
875
725
          exitstatus = EXIT_FAILURE;
876
726
        }
877
 
        goto fallback;
 
727
        goto end;
878
728
      }
879
729
      /* This process has not completed.  Does it have any output? */
880
730
      if(proc->eof or not FD_ISSET(proc->fd, &rfds)){
888
738
        if (proc->buffer == NULL){
889
739
          perror("malloc");
890
740
          exitstatus = EXIT_FAILURE;
891
 
          goto fallback;
 
741
          goto end;
892
742
        }
893
743
        proc->buffer_size += BUFFER_SIZE;
894
744
      }
909
759
  }
910
760
 
911
761
 
912
 
 fallback:
 
762
 end:
913
763
  
914
764
  if(process_list == NULL or exitstatus != EXIT_SUCCESS){
915
765
    /* Fallback if all plugins failed, none are found or an error occured */
920
770
    if(not bret){
921
771
      perror("print_out_password");
922
772
      exitstatus = EXIT_FAILURE;
 
773
      goto end;
923
774
    }
924
775
  }
925
776
  
926
777
  /* Restore old signal handler */
927
 
  ret = sigaction(SIGCHLD, &old_sigchld_action, NULL);
928
 
  if(ret == -1){
929
 
    perror("sigaction");
930
 
    exitstatus = EXIT_FAILURE;
931
 
  }
932
 
 
933
 
  if(custom_argv != NULL){
934
 
    for(char **arg = custom_argv; *arg != NULL; arg++){
935
 
      free(*arg);
936
 
    }
937
 
    free(custom_argv);
938
 
  }
939
 
  free_plugin_list(plugin_list);
 
778
  sigaction(SIGCHLD, &old_sigchld_action, NULL);
 
779
  
 
780
  free(custom_argv);
 
781
  
 
782
  /* Free the plugin list */
 
783
  for(plugin *next; plugin_list != NULL; plugin_list = next){
 
784
    next = plugin_list->next;
 
785
    free(plugin_list->argv);
 
786
    free(plugin_list);
 
787
  }
940
788
  
941
789
  if(dir != NULL){
942
790
    closedir(dir);