/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-07 01:44:44 UTC
  • mfrom: (24.1.93 mandos)
  • Revision ID: teddy@fukt.bsnet.se-20080907014444-cf4ilzndc0tbn8va
Merge & resolve.

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 },
358
375
  
359
376
  error_t parse_opt (int key, char *arg, __attribute__((unused))
360
377
                     struct argp_state *state) {
361
 
    /* Get the INPUT argument from `argp_parse', which we know is a
362
 
       pointer to our plugin list pointer. */
363
378
    switch (key) {
364
379
    case 'g':                   /* --global-options */
365
380
      if (arg != NULL){
375
390
        }
376
391
      }
377
392
      break;
378
 
    case 'e':                   /* --global-envs */
 
393
    case 'G':                   /* --global-env */
379
394
      if(arg == NULL){
380
395
        break;
381
396
      }
382
 
      {
383
 
        char *envdef = strdup(arg);
384
 
        if(envdef == NULL){
385
 
          break;
386
 
        }
387
 
        if(not add_environment(getplugin(NULL), envdef)){
388
 
          perror("add_environment");
389
 
        }
 
397
      if(not add_environment(getplugin(NULL), arg, true)){
 
398
        perror("add_environment");
390
399
      }
391
400
      break;
392
401
    case 'o':                   /* --options-for */
393
402
      if (arg != NULL){
394
403
        char *p_name = strsep(&arg, ":");
395
 
        if(p_name[0] == '\0'){
 
404
        if(p_name[0] == '\0' or arg == NULL){
396
405
          break;
397
406
        }
398
407
        char *opt = strsep(&arg, ":");
399
 
        if(opt[0] == '\0'){
 
408
        if(opt[0] == '\0' or opt == NULL){
400
409
          break;
401
410
        }
402
 
        if(opt != NULL){
403
 
          char *p;
404
 
          while((p = strsep(&opt, ",")) != NULL){
405
 
            if(p[0] == '\0'){
406
 
              continue;
407
 
            }
408
 
            if(not add_argument(getplugin(p_name), p)){
409
 
              perror("add_argument");
410
 
              return ARGP_ERR_UNKNOWN;
411
 
            }
 
411
        char *p;
 
412
        while((p = strsep(&opt, ",")) != NULL){
 
413
          if(p[0] == '\0'){
 
414
            continue;
 
415
          }
 
416
          if(not add_argument(getplugin(p_name), p)){
 
417
            perror("add_argument");
 
418
            return ARGP_ERR_UNKNOWN;
412
419
          }
413
420
        }
414
421
      }
415
422
      break;
416
 
    case 'f':                   /* --envs-for */
 
423
    case 'E':                   /* --env-for */
417
424
      if(arg == NULL){
418
425
        break;
419
426
      }
422
429
        if(envdef == NULL){
423
430
          break;
424
431
        }
425
 
        char *p_name = strndup(arg, (size_t) (envdef-arg));
426
 
        if(p_name == NULL){
427
 
          break;
428
 
        }
429
 
        envdef++;
430
 
        if(not add_environment(getplugin(p_name), envdef)){
 
432
        *envdef = '\0';
 
433
        if(not add_environment(getplugin(arg), envdef+1, true)){
431
434
          perror("add_environment");
432
435
        }
433
436
      }
441
444
        p->disabled = true;
442
445
      }
443
446
      break;
 
447
    case 'e':                   /* --enable */
 
448
      if (arg != NULL){
 
449
        plugin *p = getplugin(arg);
 
450
        if(p == NULL){
 
451
          return ARGP_ERR_UNKNOWN;
 
452
        }
 
453
        p->disabled = false;
 
454
      }
 
455
      break;
444
456
    case 128:                   /* --plugin-dir */
 
457
      free(plugindir);
445
458
      plugindir = strdup(arg);
446
459
      if(plugindir == NULL){
447
460
        perror("strdup");
448
461
      }      
449
462
      break;
450
463
    case 129:                   /* --config-file */
451
 
      argfile = strdup(arg);
452
 
      if(argfile == NULL){
453
 
        perror("strdup");
454
 
      }
455
 
      break;      
 
464
      /* This is already done by parse_opt_config_file() */
 
465
      break;
456
466
    case 130:                   /* --userid */
457
467
      uid = (uid_t)strtol(arg, NULL, 10);
458
468
      break;
473
483
    return 0;
474
484
  }
475
485
  
476
 
  struct argp argp = { .options = options, .parser = parse_opt,
477
 
                       .args_doc = "[+PLUS_SEPARATED_OPTIONS]",
 
486
  /* This option parser is the same as parse_opt() above, except it
 
487
     ignores everything but the --config-file option. */
 
488
  error_t parse_opt_config_file (int key, char *arg,
 
489
                                 __attribute__((unused))
 
490
                                 struct argp_state *state) {
 
491
    switch (key) {
 
492
    case 'g':                   /* --global-options */
 
493
    case 'G':                   /* --global-env */
 
494
    case 'o':                   /* --options-for */
 
495
    case 'E':                   /* --env-for */
 
496
    case 'd':                   /* --disable */
 
497
    case 'e':                   /* --enable */
 
498
    case 128:                   /* --plugin-dir */
 
499
      break;
 
500
    case 129:                   /* --config-file */
 
501
      free(argfile);
 
502
      argfile = strdup(arg);
 
503
      if(argfile == NULL){
 
504
        perror("strdup");
 
505
      }
 
506
      break;      
 
507
    case 130:                   /* --userid */
 
508
    case 131:                   /* --groupid */
 
509
    case 132:                   /* --debug */
 
510
    case ARGP_KEY_ARG:
 
511
    case ARGP_KEY_END:
 
512
      break;
 
513
    default:
 
514
      return ARGP_ERR_UNKNOWN;
 
515
    }
 
516
    return 0;
 
517
  }
 
518
  
 
519
  struct argp argp = { .options = options,
 
520
                       .parser = parse_opt_config_file,
 
521
                       .args_doc = "",
478
522
                       .doc = "Mandos plugin runner -- Run plugins" };
479
523
  
480
 
  ret = argp_parse (&argp, argc, argv, 0, 0, NULL);
 
524
  /* Parse using the parse_opt_config_file in order to get the custom
 
525
     config file location, if any. */
 
526
  ret = argp_parse (&argp, argc, argv, ARGP_IN_ORDER, 0, NULL);
481
527
  if (ret == ARGP_ERR_UNKNOWN){
482
528
    fprintf(stderr, "Unknown error while parsing arguments\n");
483
529
    exitstatus = EXIT_FAILURE;
484
530
    goto fallback;
485
531
  }
486
 
 
487
 
  /* Opens the configfile if aviable */
 
532
  
 
533
  /* Reset to the normal argument parser */
 
534
  argp.parser = parse_opt;
 
535
  
 
536
  /* Open the configfile if available */
488
537
  if (argfile == NULL){
489
538
    conffp = fopen(AFILE, "r");
490
539
  } else {
544
593
      }
545
594
    }
546
595
    free(org_line);
547
 
  } else{
 
596
  } else {
548
597
    /* Check for harmful errors and go to fallback. Other errors might
549
598
       not affect opening plugins */
550
599
    if (errno == EMFILE or errno == ENFILE or errno == ENOMEM){
556
605
  /* If there was any arguments from configuration file,
557
606
     pass them to parser as command arguments */
558
607
  if(custom_argv != NULL){
559
 
    ret = argp_parse (&argp, custom_argc, custom_argv, 0, 0, NULL);
 
608
    ret = argp_parse (&argp, custom_argc, custom_argv, ARGP_IN_ORDER,
 
609
                      0, NULL);
560
610
    if (ret == ARGP_ERR_UNKNOWN){
561
611
      fprintf(stderr, "Unknown error while parsing arguments\n");
562
612
      exitstatus = EXIT_FAILURE;
564
614
    }
565
615
  }
566
616
  
 
617
  /* Parse actual command line arguments, to let them override the
 
618
     config file */
 
619
  ret = argp_parse (&argp, argc, argv, ARGP_IN_ORDER, 0, NULL);
 
620
  if (ret == ARGP_ERR_UNKNOWN){
 
621
    fprintf(stderr, "Unknown error while parsing arguments\n");
 
622
    exitstatus = EXIT_FAILURE;
 
623
    goto fallback;
 
624
  }
 
625
  
567
626
  if(debug){
568
627
    for(plugin *p = plugin_list; p != NULL; p=p->next){
569
628
      fprintf(stderr, "Plugin: %s has %d arguments\n",
577
636
      }
578
637
    }
579
638
  }
580
 
 
 
639
  
581
640
  /* Strip permissions down to nobody */
582
641
  ret = setuid(uid);
583
642
  if (ret == -1){
587
646
  if (ret == -1){
588
647
    perror("setgid");
589
648
  }
590
 
 
 
649
  
591
650
  if (plugindir == NULL){
592
651
    dir = opendir(PDIR);
593
652
  } else {
614
673
  }
615
674
  
616
675
  FD_ZERO(&rfds_all);
617
 
 
 
676
  
618
677
  /* Read and execute any executable in the plugin directory*/
619
678
  while(true){
620
679
    dirst = readdir(dir);
621
680
    
622
 
    // All directory entries have been processed
 
681
    /* All directory entries have been processed */
623
682
    if(dirst == NULL){
624
683
      if (errno == EBADF){
625
684
        perror("readdir");
631
690
    
632
691
    d_name_len = strlen(dirst->d_name);
633
692
    
634
 
    // Ignore dotfiles, backup files and other junk
 
693
    /* Ignore dotfiles, backup files and other junk */
635
694
    {
636
695
      bool bad_name = false;
637
696
      
675
734
    }
676
735
 
677
736
    char *filename;
678
 
    ret = asprintf(&filename, "%s/%s", plugindir, dirst->d_name);
 
737
    if(plugindir == NULL){
 
738
      ret = asprintf(&filename, PDIR "/%s", dirst->d_name);
 
739
    } else {
 
740
      ret = asprintf(&filename, "%s/%s", plugindir, dirst->d_name);
 
741
    }
679
742
    if(ret < 0){
680
743
      perror("asprintf");
681
744
      continue;
723
786
        }
724
787
        /* Add global environment variables */
725
788
        for(char **e = g->environ; *e != NULL; e++){
726
 
          if(not add_environment(p, *e)){
 
789
          if(not add_environment(p, *e, false)){
727
790
            perror("add_environment");
728
791
          }
729
792
        }
734
797
       process, too. */
735
798
    if(p->environ[0] != NULL){
736
799
      for(char **e = environ; *e != NULL; e++){
737
 
        char *copy = strdup(*e);
738
 
        if(copy == NULL){
739
 
          perror("strdup");
740
 
          continue;
741
 
        }
742
 
        if(not add_environment(p, copy)){
 
800
        if(not add_environment(p, *e, false)){
743
801
          perror("add_environment");
744
802
        }
745
803
      }
772
830
      exitstatus = EXIT_FAILURE;
773
831
      goto fallback;
774
832
    }
775
 
    // Starting a new process to be watched
 
833
    /* Starting a new process to be watched */
776
834
    pid_t pid = fork();
777
835
    if(pid == -1){
778
836
      perror("fork");
875
933
    }
876
934
    /* OK, now either a process completed, or something can be read
877
935
       from one of them */
878
 
    for(plugin *proc = plugin_list; proc != NULL; proc = proc->next){
 
936
    for(plugin *proc = plugin_list; proc != NULL;){
879
937
      /* Is this process completely done? */
880
938
      if(proc->eof and proc->completed){
881
939
        /* Only accept the plugin output if it exited cleanly */
908
966
            exitstatus = EXIT_FAILURE;
909
967
            goto fallback;
910
968
          }
911
 
          free_plugin(proc);
 
969
          
912
970
          /* We are done modifying process list, so unblock signal */
913
971
          ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask,
914
972
                             NULL);
921
979
          if(plugin_list == NULL){
922
980
            break;
923
981
          }
 
982
          
 
983
          plugin *next_plugin = proc->next;
 
984
          free_plugin(proc);
 
985
          proc = next_plugin;
924
986
          continue;
925
987
        }
926
988
        
938
1000
      /* This process has not completed.  Does it have any output? */
939
1001
      if(proc->eof or not FD_ISSET(proc->fd, &rfds)){
940
1002
        /* This process had nothing to say at this time */
 
1003
        proc = proc->next;
941
1004
        continue;
942
1005
      }
943
1006
      /* Before reading, make the process' data buffer large enough */
956
1019
                 BUFFER_SIZE);
957
1020
      if(ret < 0){
958
1021
        /* Read error from this process; ignore the error */
 
1022
        proc = proc->next;
959
1023
        continue;
960
1024
      }
961
1025
      if(ret == 0){
976
1040
    bool bret;
977
1041
    fprintf(stderr, "Going to fallback mode using getpass(3)\n");
978
1042
    char *passwordbuffer = getpass("Password: ");
979
 
    bret = print_out_password(passwordbuffer, strlen(passwordbuffer));
 
1043
    size_t len = strlen(passwordbuffer);
 
1044
    /* Strip trailing newline */
 
1045
    if(len > 0 and passwordbuffer[len-1] == '\n'){
 
1046
      passwordbuffer[len-1] = '\0'; /* not strictly necessary */
 
1047
      len--;
 
1048
    }
 
1049
    bret = print_out_password(passwordbuffer, len);
980
1050
    if(not bret){
981
1051
      perror("print_out_password");
982
1052
      exitstatus = EXIT_FAILURE;