/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() */
 
24
#define _GNU_SOURCE             /* TEMP_FAILURE_RETRY(), getline() */
25
25
 
26
26
#include <stddef.h>             /* size_t, NULL */
27
27
#include <stdlib.h>             /* malloc(), exit(), EXIT_FAILURE,
51
51
                                   close() */
52
52
#include <fcntl.h>              /* fcntl(), F_GETFD, F_SETFD,
53
53
                                   FD_CLOEXEC */
54
 
#include <string.h>             /* strtok, strlen(), strcpy(),
 
54
#include <string.h>             /* strsep, strlen(), strcpy(),
55
55
                                   strcat() */
56
56
#include <errno.h>              /* errno */
57
57
#include <argp.h>               /* struct argp_option, struct
65
65
#include <errno.h>              /* errno, EBADF */
66
66
 
67
67
#define BUFFER_SIZE 256
 
68
#define ARGFILE "/conf/conf.d/mandos/plugin-runner.conf"
68
69
 
69
 
const char *argp_program_version = "mandos-client 1.0";
 
70
const char *argp_program_version = "plugin-runner 1.0";
70
71
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
71
72
 
72
73
struct process;
170
171
  proc->completed = true;
171
172
}
172
173
 
 
174
bool print_out_password(const char *buffer, size_t length){
 
175
  ssize_t ret;
 
176
  if(length>0 and buffer[length-1] == '\n'){
 
177
    length--;
 
178
  }
 
179
  for(size_t written = 0; written < length; written += (size_t)ret){
 
180
    ret = TEMP_FAILURE_RETRY(write(STDOUT_FILENO, buffer + written,
 
181
                                   length - written));
 
182
    if(ret < 0){
 
183
      return false;
 
184
    }
 
185
  }
 
186
  return true;
 
187
}
 
188
 
 
189
char ** addcustomargument(char **argv, int *argc, char *arg){
 
190
 
 
191
  if (argv == NULL){
 
192
    *argc = 1;
 
193
    argv = malloc(sizeof(char*) * 2);
 
194
    if(argv == NULL){
 
195
      return NULL;
 
196
    }
 
197
    argv[0] = NULL;     /* Will be set to argv[0] in main before parsing */
 
198
    argv[1] = NULL;
 
199
  }
 
200
  *argc += 1;
 
201
  argv = realloc(argv, sizeof(char *)
 
202
                  * ((unsigned int) *argc + 1));
 
203
  if(argv == NULL){
 
204
    return NULL;
 
205
  }
 
206
  argv[*argc-1] = arg;
 
207
  argv[*argc] = NULL;   
 
208
  return argv;
 
209
}
 
210
 
173
211
int main(int argc, char *argv[]){
174
 
  const char *plugindir = "/conf/conf.d/mandos/plugins.d";
 
212
  const char *plugindir = "/lib/mandos/plugins.d";
 
213
  const char *argfile = ARGFILE;
 
214
  FILE *conffp;
175
215
  size_t d_name_len;
176
216
  DIR *dir = NULL;
177
217
  struct dirent *dirst;
185
225
  struct sigaction old_sigchld_action;
186
226
  struct sigaction sigchld_action = { .sa_handler = handle_sigchld,
187
227
                                      .sa_flags = SA_NOCLDSTOP };
188
 
  char *plus_options = NULL;
189
 
  char **plus_argv = NULL;
 
228
  char **custom_argv = NULL;
 
229
  int custom_argc = 0;
190
230
  
191
231
  /* Establish a signal handler */
192
232
  sigemptyset(&sigchld_action.sa_mask);
233
273
    switch (key) {
234
274
    case 'g':
235
275
      if (arg != NULL){
236
 
        char *p = strtok(arg, ",");
237
 
        do{
 
276
        char *p;
 
277
        while((p = strsep(&arg, ",")) != NULL){
 
278
          if(p[0] == '\0'){
 
279
            continue;
 
280
          }
238
281
          addargument(getplugin(NULL, plugins), p);
239
 
          p = strtok(NULL, ",");
240
 
        } while (p != NULL);
 
282
        }
241
283
      }
242
284
      break;
243
285
    case 'o':
244
286
      if (arg != NULL){
245
 
        char *name = strtok(arg, ":");
246
 
        char *p = strtok(NULL, ":");
247
 
        if(p != NULL){
248
 
          p = strtok(p, ",");
249
 
          do{
 
287
        char *name = strsep(&arg, ":");
 
288
        if(name[0] == '\0'){
 
289
          break;
 
290
        }
 
291
        char *opt = strsep(&arg, ":");
 
292
        if(opt[0] == '\0'){
 
293
          break;
 
294
        }
 
295
        if(opt != NULL){
 
296
          char *p;
 
297
          while((p = strsep(&opt, ",")) != NULL){
 
298
            if(p[0] == '\0'){
 
299
              continue;
 
300
            }
250
301
            addargument(getplugin(name, plugins), p);
251
 
            p = strtok(NULL, ",");
252
 
          } while (p != NULL);
 
302
          }
253
303
        }
254
304
      }
255
305
      break;
271
321
      debug = true;
272
322
      break;
273
323
    case ARGP_KEY_ARG:
274
 
      if(plus_options != NULL or arg == NULL or arg[0] != '+'){
275
 
        argp_usage (state);
276
 
      }
277
 
      plus_options = arg;
 
324
      fprintf(stderr, "Ignoring unknown argument \"%s\"\n", arg); 
278
325
      break;
279
326
    case ARGP_KEY_END:
280
327
      break;
292
339
  
293
340
  ret = argp_parse (&argp, argc, argv, 0, 0, &plugin_list);
294
341
  if (ret == ARGP_ERR_UNKNOWN){
295
 
    fprintf(stderr, "Unkown error while parsing arguments\n");
 
342
    fprintf(stderr, "Unknown error while parsing arguments\n");
296
343
    exitstatus = EXIT_FAILURE;
297
344
    goto end;
298
345
  }
299
 
  
300
 
  if(plus_options){
301
 
    /* This is a mangled argument in the form of
302
 
     "+--option+--other-option=parameter+--yet-another-option", etc */
303
 
    /* Make new argc and argv vars, and call argp_parse() again. */
304
 
    plus_options++;             /* skip the first '+' character */
305
 
    const char delims[] = "+";
306
 
    char *arg;
307
 
    int new_argc = 1;
308
 
    plus_argv = malloc(sizeof(char*) * 2);
309
 
    if(plus_argv == NULL){
310
 
      perror("malloc");
 
346
 
 
347
  conffp = fopen(argfile, "r");
 
348
  if(conffp != NULL){
 
349
    char *org_line = NULL;
 
350
    size_t size = 0;
 
351
    ssize_t sret;
 
352
    char *p, *arg, *new_arg, *line;
 
353
    const char whitespace_delims[] = " \r\t\f\v\n";
 
354
    const char comment_delim[] = "#";
 
355
 
 
356
    while(true){
 
357
      sret = getline(&org_line, &size, conffp);
 
358
      if(sret == -1){
 
359
        break;
 
360
      }
 
361
 
 
362
      line = org_line;
 
363
      arg = strsep(&line, comment_delim);
 
364
      while((p = strsep(&arg, whitespace_delims)) != NULL){
 
365
        if(p[0] == '\0'){
 
366
          continue;
 
367
        }
 
368
        new_arg = strdup(p);
 
369
        custom_argv = addcustomargument(custom_argv, &custom_argc, new_arg);
 
370
        if (custom_argv == NULL){
 
371
          perror("addcustomargument");
 
372
          exitstatus = EXIT_FAILURE;
 
373
          goto end;
 
374
        }
 
375
      }
 
376
    }
 
377
    free(org_line);
 
378
  } else{
 
379
    /* Check for harmful errors and go to fallback. Other errors might
 
380
       not affect opening plugins */
 
381
    if (errno == EMFILE or errno == ENFILE or errno == ENOMEM){
 
382
      perror("fopen");
311
383
      exitstatus = EXIT_FAILURE;
312
384
      goto end;
313
385
    }
314
 
    plus_argv[0] = argv[0];
315
 
    plus_argv[1] = NULL;
316
 
    arg = strtok(plus_options, delims); /* Get first argument */
317
 
    while(arg != NULL){
318
 
      new_argc++;
319
 
      plus_argv = realloc(plus_argv, sizeof(char *)
320
 
                         * ((unsigned int) new_argc + 1));
321
 
      if(plus_argv == NULL){
322
 
        perror("realloc");
323
 
        exitstatus = EXIT_FAILURE;
324
 
        goto end;
325
 
      }
326
 
      plus_argv[new_argc-1] = arg;
327
 
      plus_argv[new_argc] = NULL;
328
 
      arg = strtok(NULL, delims); /* Get next argument */
329
 
    }
330
 
    ret = argp_parse (&argp, new_argc, plus_argv, 0, 0, &plugin_list);
 
386
  }
 
387
 
 
388
  if(custom_argv != NULL){
 
389
    custom_argv[0] = argv[0];
 
390
    ret = argp_parse (&argp, custom_argc, custom_argv, 0, 0, &plugin_list);
331
391
    if (ret == ARGP_ERR_UNKNOWN){
332
 
      fprintf(stderr, "Unkown error while parsing arguments\n");
 
392
      fprintf(stderr, "Unknown error while parsing arguments\n");
333
393
      exitstatus = EXIT_FAILURE;
334
394
      goto end;
335
395
    }
440
500
    char *filename = malloc(d_name_len + strlen(plugindir) + 2);
441
501
    if (filename == NULL){
442
502
      perror("malloc");
443
 
      exitstatus = EXIT_FAILURE;
444
 
      goto end;
 
503
      continue;
445
504
    }
446
505
    strcpy(filename, plugindir); /* Spurious warning */
447
506
    strcat(filename, "/");      /* Spurious warning */
450
509
    ret = stat(filename, &st);
451
510
    if (ret == -1){
452
511
      perror("stat");
453
 
      exitstatus = EXIT_FAILURE;
454
 
      goto end;
 
512
      free(filename);
 
513
      continue;
455
514
    }
456
515
    
457
516
    if (not S_ISREG(st.st_mode) or (access(filename, X_OK) != 0)){
506
565
    }
507
566
    // Starting a new process to be watched
508
567
    pid_t pid = fork();
 
568
    if(pid == -1){
 
569
      perror("fork");
 
570
      exitstatus = EXIT_FAILURE;
 
571
      goto end;
 
572
    }
509
573
    if(pid == 0){
510
574
      /* this is the child process */
511
575
      ret = sigaction(SIGCHLD, &old_sigchld_action, NULL);
583
647
  dir = NULL;
584
648
    
585
649
  if (process_list == NULL){
586
 
    fprintf(stderr, "No plugin processes started, exiting\n");
587
 
    exitstatus = EXIT_FAILURE;
588
 
    goto end;
 
650
    fprintf(stderr, "No plugin processes started. Incorrect plugin"
 
651
            " directory?\n");
 
652
    process_list = NULL;
589
653
  }
590
654
  while(process_list){
591
655
    fd_set rfds = rfds_all;
654
718
          break;
655
719
        }
656
720
        /* This process exited nicely, so print its buffer */
657
 
        for(size_t written = 0; written < proc->buffer_length;
658
 
            written += (size_t)ret){
659
 
          ret = TEMP_FAILURE_RETRY(write(STDOUT_FILENO,
660
 
                                         proc->buffer + written,
661
 
                                         proc->buffer_length
662
 
                                         - written));
663
 
          if(ret < 0){
664
 
            perror("write");
665
 
            exitstatus = EXIT_FAILURE;
666
 
            goto end;
667
 
          }
 
721
 
 
722
        bool bret = print_out_password(proc->buffer, proc->buffer_length);
 
723
        if(not bret){
 
724
          perror("print_out_password");
 
725
          exitstatus = EXIT_FAILURE;
668
726
        }
669
727
        goto end;
670
728
      }
699
757
      }
700
758
    }
701
759
  }
702
 
  if(process_list == NULL){
703
 
    fprintf(stderr, "All plugin processes failed, exiting\n");
704
 
    exitstatus = EXIT_FAILURE;
705
 
  }
706
 
  
 
760
 
 
761
 
707
762
 end:
 
763
  
 
764
  if(process_list == NULL or exitstatus != EXIT_SUCCESS){
 
765
    /* Fallback if all plugins failed, none are found or an error occured */
 
766
    bool bret;
 
767
    fprintf(stderr, "Going to fallback mode using getpass(3)\n");
 
768
    char *passwordbuffer = getpass("Password: ");
 
769
    bret = print_out_password(passwordbuffer, strlen(passwordbuffer));
 
770
    if(not bret){
 
771
      perror("print_out_password");
 
772
      exitstatus = EXIT_FAILURE;
 
773
      goto end;
 
774
    }
 
775
  }
 
776
  
708
777
  /* Restore old signal handler */
709
778
  sigaction(SIGCHLD, &old_sigchld_action, NULL);
710
779
  
711
 
  free(plus_argv);
 
780
  free(custom_argv);
712
781
  
713
782
  /* Free the plugin list */
714
783
  for(plugin *next; plugin_list != NULL; plugin_list = next){