/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-09-06 16:11:50 UTC
  • Revision ID: teddy@fukt.bsnet.se-20080906161150-1umb8yz6a29zxg28
* Makefile (install-client): Add "/etc/plugins.d/README".
  (uninstall-client): Remove "/etc/plugins.d/README".

* etc-plugins.d-README: New file.

Show diffs side-by-side

added added

removed removed

Lines of Context:
96
96
 
97
97
static plugin *plugin_list = NULL;
98
98
 
99
 
/* Gets a existing plugin based on name,
 
99
/* Gets an existing plugin based on name,
100
100
   or if none is found, creates a new one */
101
101
static plugin *getplugin(char *name){
102
102
  /* Check for exiting plugin with that name */
118
118
      return NULL;
119
119
    }
120
120
  }
121
 
 
 
121
  
122
122
  *new_plugin = (plugin) { .name = copy_name,
123
123
                           .argc = 1,
124
124
                           .disabled = false,
132
132
  }
133
133
  new_plugin->argv[0] = copy_name;
134
134
  new_plugin->argv[1] = NULL;
135
 
 
 
135
  
136
136
  new_plugin->environ = malloc(sizeof(char *));
137
137
  if(new_plugin->environ == NULL){
138
138
    free(copy_name);
141
141
    return NULL;
142
142
  }
143
143
  new_plugin->environ[0] = NULL;
144
 
 
 
144
  
145
145
  /* Append the new plugin to the list */
146
146
  plugin_list = new_plugin;
147
147
  return new_plugin;
179
179
}
180
180
 
181
181
/* Add to a plugin's environment */
182
 
static bool add_environment(plugin *p, const char *def){
 
182
static bool add_environment(plugin *p, const char *def, bool replace){
183
183
  if(p == NULL){
184
184
    return false;
185
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
      /* It already exists */
 
192
      if(replace){
 
193
        char *new = realloc(*e, strlen(def) + 1);
 
194
        if(new == NULL){
 
195
          return false;
 
196
        }
 
197
        *e = new;
 
198
        strcpy(*e, def);
 
199
      }
 
200
      return true;
 
201
    }
 
202
  }
186
203
  return add_to_char_array(def, &(p->environ), &(p->envc));
187
204
}
188
205
 
221
238
      /* No child processes */
222
239
      break;
223
240
    }
224
 
 
 
241
    
225
242
    /* A child exited, find it in process_list */
226
243
    while(proc != NULL and proc->pid != pid){
227
244
      proc = proc->next;
238
255
/* Prints out a password to stdout */
239
256
bool print_out_password(const char *buffer, size_t length){
240
257
  ssize_t ret;
241
 
  if(length>0 and buffer[length-1] == '\n'){
242
 
    length--;
243
 
  }
244
258
  for(size_t written = 0; written < length; written += (size_t)ret){
245
259
    ret = TEMP_FAILURE_RETRY(write(STDOUT_FILENO, buffer + written,
246
260
                                   length - written));
327
341
    { .name = "global-options", .key = 'g',
328
342
      .arg = "OPTION[,OPTION[,...]]",
329
343
      .doc = "Options passed to all plugins" },
330
 
    { .name = "global-envs", .key = 'e',
 
344
    { .name = "global-env", .key = 'G',
331
345
      .arg = "VAR=value",
332
346
      .doc = "Environment variable passed to all plugins" },
333
347
    { .name = "options-for", .key = 'o',
334
348
      .arg = "PLUGIN:OPTION[,OPTION[,...]]",
335
349
      .doc = "Options passed only to specified plugin" },
336
 
    { .name = "envs-for", .key = 'f',
 
350
    { .name = "env-for", .key = 'E',
337
351
      .arg = "PLUGIN:ENV=value",
338
352
      .doc = "Environment variable passed to specified plugin" },
339
353
    { .name = "disable", .key = 'd',
340
354
      .arg = "PLUGIN",
341
355
      .doc = "Disable a specific plugin", .group = 1 },
 
356
    { .name = "enable", .key = 'e',
 
357
      .arg = "PLUGIN",
 
358
      .doc = "Enable a specific plugin", .group = 1 },
342
359
    { .name = "plugin-dir", .key = 128,
343
360
      .arg = "DIRECTORY",
344
361
      .doc = "Specify a different plugin directory", .group = 2 },
356
373
    { .name = NULL }
357
374
  };
358
375
  
359
 
  error_t parse_opt (int key, char *arg, __attribute__((unused)) struct argp_state *state) {
360
 
    /* Get the INPUT argument from `argp_parse', which we know is a
361
 
       pointer to our plugin list pointer. */
 
376
  error_t parse_opt (int key, char *arg, __attribute__((unused))
 
377
                     struct argp_state *state) {
362
378
    switch (key) {
363
379
    case 'g':                   /* --global-options */
364
380
      if (arg != NULL){
374
390
        }
375
391
      }
376
392
      break;
377
 
    case 'e':                   /* --global-envs */
 
393
    case 'G':                   /* --global-env */
378
394
      if(arg == NULL){
379
395
        break;
380
396
      }
383
399
        if(envdef == NULL){
384
400
          break;
385
401
        }
386
 
        if(not add_environment(getplugin(NULL), envdef)){
 
402
        if(not add_environment(getplugin(NULL), envdef, true)){
387
403
          perror("add_environment");
388
404
        }
389
405
      }
391
407
    case 'o':                   /* --options-for */
392
408
      if (arg != NULL){
393
409
        char *p_name = strsep(&arg, ":");
394
 
        if(p_name[0] == '\0'){
 
410
        if(p_name[0] == '\0' or arg == NULL){
395
411
          break;
396
412
        }
397
413
        char *opt = strsep(&arg, ":");
398
 
        if(opt[0] == '\0'){
 
414
        if(opt[0] == '\0' or opt == NULL){
399
415
          break;
400
416
        }
401
 
        if(opt != NULL){
402
 
          char *p;
403
 
          while((p = strsep(&opt, ",")) != NULL){
404
 
            if(p[0] == '\0'){
405
 
              continue;
406
 
            }
407
 
            if(not add_argument(getplugin(p_name), p)){
408
 
              perror("add_argument");
409
 
              return ARGP_ERR_UNKNOWN;
410
 
            }
 
417
        char *p;
 
418
        while((p = strsep(&opt, ",")) != NULL){
 
419
          if(p[0] == '\0'){
 
420
            continue;
 
421
          }
 
422
          if(not add_argument(getplugin(p_name), p)){
 
423
            perror("add_argument");
 
424
            return ARGP_ERR_UNKNOWN;
411
425
          }
412
426
        }
413
427
      }
414
428
      break;
415
 
    case 'f':                   /* --envs-for */
 
429
    case 'E':                   /* --env-for */
416
430
      if(arg == NULL){
417
431
        break;
418
432
      }
426
440
          break;
427
441
        }
428
442
        envdef++;
429
 
        if(not add_environment(getplugin(p_name), envdef)){
 
443
        if(not add_environment(getplugin(p_name), envdef, true)){
430
444
          perror("add_environment");
431
445
        }
432
446
      }
440
454
        p->disabled = true;
441
455
      }
442
456
      break;
 
457
    case 'e':                   /* --enable */
 
458
      if (arg != NULL){
 
459
        plugin *p = getplugin(arg);
 
460
        if(p == NULL){
 
461
          return ARGP_ERR_UNKNOWN;
 
462
        }
 
463
        p->disabled = false;
 
464
      }
 
465
      break;
443
466
    case 128:                   /* --plugin-dir */
444
467
      plugindir = strdup(arg);
445
468
      if(plugindir == NULL){
447
470
      }      
448
471
      break;
449
472
    case 129:                   /* --config-file */
450
 
      argfile = strdup(arg);
451
 
      if(argfile == NULL){
452
 
        perror("strdup");
453
 
      }
454
 
      break;      
 
473
      /* This is already done by parse_opt_config_file() */
 
474
      break;
455
475
    case 130:                   /* --userid */
456
476
      uid = (uid_t)strtol(arg, NULL, 10);
457
477
      break;
472
492
    return 0;
473
493
  }
474
494
  
475
 
  struct argp argp = { .options = options, .parser = parse_opt,
476
 
                       .args_doc = "[+PLUS_SEPARATED_OPTIONS]",
 
495
  /* This option parser is the same as parse_opt() above, except it
 
496
     ignores everything but the --config-file option. */
 
497
  error_t parse_opt_config_file (int key, char *arg,
 
498
                                 __attribute__((unused))
 
499
                                 struct argp_state *state) {
 
500
    switch (key) {
 
501
    case 'g':                   /* --global-options */
 
502
    case 'G':                   /* --global-env */
 
503
    case 'o':                   /* --options-for */
 
504
    case 'E':                   /* --env-for */
 
505
    case 'd':                   /* --disable */
 
506
    case 'e':                   /* --enable */
 
507
    case 128:                   /* --plugin-dir */
 
508
      break;
 
509
    case 129:                   /* --config-file */
 
510
      argfile = strdup(arg);
 
511
      if(argfile == NULL){
 
512
        perror("strdup");
 
513
      }
 
514
      break;      
 
515
    case 130:                   /* --userid */
 
516
    case 131:                   /* --groupid */
 
517
    case 132:                   /* --debug */
 
518
    case ARGP_KEY_ARG:
 
519
    case ARGP_KEY_END:
 
520
      break;
 
521
    default:
 
522
      return ARGP_ERR_UNKNOWN;
 
523
    }
 
524
    return 0;
 
525
  }
 
526
  
 
527
  struct argp argp = { .options = options,
 
528
                       .parser = parse_opt_config_file,
 
529
                       .args_doc = "",
477
530
                       .doc = "Mandos plugin runner -- Run plugins" };
478
531
  
479
 
  ret = argp_parse (&argp, argc, argv, 0, 0, NULL);
 
532
  /* Parse using the parse_opt_config_file in order to get the custom
 
533
     config file location, if any. */
 
534
  ret = argp_parse (&argp, argc, argv, ARGP_IN_ORDER, 0, NULL);
480
535
  if (ret == ARGP_ERR_UNKNOWN){
481
536
    fprintf(stderr, "Unknown error while parsing arguments\n");
482
537
    exitstatus = EXIT_FAILURE;
483
538
    goto fallback;
484
539
  }
485
 
 
486
 
  /* Opens the configfile if aviable */
 
540
  
 
541
  /* Reset to the normal argument parser */
 
542
  argp.parser = parse_opt;
 
543
  
 
544
  /* Open the configfile if available */
487
545
  if (argfile == NULL){
488
546
    conffp = fopen(AFILE, "r");
489
547
  } else {
507
565
    custom_argv[0] = argv[0];
508
566
    custom_argv[1] = NULL;
509
567
 
510
 
    /* for each line in the config file, strip whitespace and ignore commented text */
 
568
    /* for each line in the config file, strip whitespace and ignore
 
569
       commented text */
511
570
    while(true){
512
571
      sret = getline(&org_line, &size, conffp);
513
572
      if(sret == -1){
542
601
      }
543
602
    }
544
603
    free(org_line);
545
 
  } else{
 
604
  } else {
546
605
    /* Check for harmful errors and go to fallback. Other errors might
547
606
       not affect opening plugins */
548
607
    if (errno == EMFILE or errno == ENFILE or errno == ENOMEM){
554
613
  /* If there was any arguments from configuration file,
555
614
     pass them to parser as command arguments */
556
615
  if(custom_argv != NULL){
557
 
    ret = argp_parse (&argp, custom_argc, custom_argv, 0, 0, NULL);
 
616
    ret = argp_parse (&argp, custom_argc, custom_argv, ARGP_IN_ORDER,
 
617
                      0, NULL);
558
618
    if (ret == ARGP_ERR_UNKNOWN){
559
619
      fprintf(stderr, "Unknown error while parsing arguments\n");
560
620
      exitstatus = EXIT_FAILURE;
562
622
    }
563
623
  }
564
624
  
 
625
  /* Parse actual command line arguments, to let them override the
 
626
     config file */
 
627
  ret = argp_parse (&argp, argc, argv, ARGP_IN_ORDER, 0, NULL);
 
628
  if (ret == ARGP_ERR_UNKNOWN){
 
629
    fprintf(stderr, "Unknown error while parsing arguments\n");
 
630
    exitstatus = EXIT_FAILURE;
 
631
    goto fallback;
 
632
  }
 
633
  
565
634
  if(debug){
566
635
    for(plugin *p = plugin_list; p != NULL; p=p->next){
567
636
      fprintf(stderr, "Plugin: %s has %d arguments\n",
575
644
      }
576
645
    }
577
646
  }
578
 
 
 
647
  
579
648
  /* Strip permissions down to nobody */
580
649
  ret = setuid(uid);
581
650
  if (ret == -1){
585
654
  if (ret == -1){
586
655
    perror("setgid");
587
656
  }
588
 
 
 
657
  
589
658
  if (plugindir == NULL){
590
659
    dir = opendir(PDIR);
591
660
  } else {
612
681
  }
613
682
  
614
683
  FD_ZERO(&rfds_all);
615
 
 
 
684
  
616
685
  /* Read and execute any executable in the plugin directory*/
617
686
  while(true){
618
687
    dirst = readdir(dir);
619
688
    
620
 
    // All directory entries have been processed
 
689
    /* All directory entries have been processed */
621
690
    if(dirst == NULL){
622
691
      if (errno == EBADF){
623
692
        perror("readdir");
629
698
    
630
699
    d_name_len = strlen(dirst->d_name);
631
700
    
632
 
    // Ignore dotfiles, backup files and other junk
 
701
    /* Ignore dotfiles, backup files and other junk */
633
702
    {
634
703
      bool bad_name = false;
635
704
      
673
742
    }
674
743
 
675
744
    char *filename;
676
 
    ret = asprintf(&filename, "%s/%s", plugindir, dirst->d_name);
 
745
    if(plugindir == NULL){
 
746
      ret = asprintf(&filename, PDIR "/%s", dirst->d_name);
 
747
    } else {
 
748
      ret = asprintf(&filename, "%s/%s", plugindir, dirst->d_name);
 
749
    }
677
750
    if(ret < 0){
678
751
      perror("asprintf");
679
752
      continue;
721
794
        }
722
795
        /* Add global environment variables */
723
796
        for(char **e = g->environ; *e != NULL; e++){
724
 
          if(not add_environment(p, *e)){
 
797
          if(not add_environment(p, *e, false)){
725
798
            perror("add_environment");
726
799
          }
727
800
        }
732
805
       process, too. */
733
806
    if(p->environ[0] != NULL){
734
807
      for(char **e = environ; *e != NULL; e++){
735
 
        char *copy = strdup(*e);
736
 
        if(copy == NULL){
737
 
          perror("strdup");
738
 
          continue;
739
 
        }
740
 
        if(not add_environment(p, copy)){
 
808
        if(not add_environment(p, *e, false)){
741
809
          perror("add_environment");
742
810
        }
743
811
      }
770
838
      exitstatus = EXIT_FAILURE;
771
839
      goto fallback;
772
840
    }
773
 
    // Starting a new process to be watched
 
841
    /* Starting a new process to be watched */
774
842
    pid_t pid = fork();
775
843
    if(pid == -1){
776
844
      perror("fork");
974
1042
    bool bret;
975
1043
    fprintf(stderr, "Going to fallback mode using getpass(3)\n");
976
1044
    char *passwordbuffer = getpass("Password: ");
977
 
    bret = print_out_password(passwordbuffer, strlen(passwordbuffer));
 
1045
    size_t len = strlen(passwordbuffer);
 
1046
    /* Strip trailing newline */
 
1047
    if(len > 0 and passwordbuffer[len-1] == '\n'){
 
1048
      passwordbuffer[len-1] = '\0'; /* not strictly necessary */
 
1049
      len--;
 
1050
    }
 
1051
    bret = print_out_password(passwordbuffer, len);
978
1052
    if(not bret){
979
1053
      perror("print_out_password");
980
1054
      exitstatus = EXIT_FAILURE;