/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

Rearranged so plugins and processes is the same thing

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 an existing plugin based on name,
 
99
/* Gets a 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, bool replace){
 
182
static bool add_environment(plugin *p, const char *def){
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
 
  }
203
186
  return add_to_char_array(def, &(p->environ), &(p->envc));
204
187
}
205
188
 
238
221
      /* No child processes */
239
222
      break;
240
223
    }
241
 
    
 
224
 
242
225
    /* A child exited, find it in process_list */
243
226
    while(proc != NULL and proc->pid != pid){
244
227
      proc = proc->next;
255
238
/* Prints out a password to stdout */
256
239
bool print_out_password(const char *buffer, size_t length){
257
240
  ssize_t ret;
 
241
  if(length>0 and buffer[length-1] == '\n'){
 
242
    length--;
 
243
  }
258
244
  for(size_t written = 0; written < length; written += (size_t)ret){
259
245
    ret = TEMP_FAILURE_RETRY(write(STDOUT_FILENO, buffer + written,
260
246
                                   length - written));
341
327
    { .name = "global-options", .key = 'g',
342
328
      .arg = "OPTION[,OPTION[,...]]",
343
329
      .doc = "Options passed to all plugins" },
344
 
    { .name = "global-env", .key = 'G',
 
330
    { .name = "global-envs", .key = 'e',
345
331
      .arg = "VAR=value",
346
332
      .doc = "Environment variable passed to all plugins" },
347
333
    { .name = "options-for", .key = 'o',
348
334
      .arg = "PLUGIN:OPTION[,OPTION[,...]]",
349
335
      .doc = "Options passed only to specified plugin" },
350
 
    { .name = "env-for", .key = 'E',
 
336
    { .name = "envs-for", .key = 'f',
351
337
      .arg = "PLUGIN:ENV=value",
352
338
      .doc = "Environment variable passed to specified plugin" },
353
339
    { .name = "disable", .key = 'd',
354
340
      .arg = "PLUGIN",
355
341
      .doc = "Disable a specific plugin", .group = 1 },
356
 
    { .name = "enable", .key = 'e',
357
 
      .arg = "PLUGIN",
358
 
      .doc = "Enable a specific plugin", .group = 1 },
359
342
    { .name = "plugin-dir", .key = 128,
360
343
      .arg = "DIRECTORY",
361
344
      .doc = "Specify a different plugin directory", .group = 2 },
373
356
    { .name = NULL }
374
357
  };
375
358
  
376
 
  error_t parse_opt (int key, char *arg, __attribute__((unused))
377
 
                     struct argp_state *state) {
 
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. */
378
362
    switch (key) {
379
363
    case 'g':                   /* --global-options */
380
364
      if (arg != NULL){
390
374
        }
391
375
      }
392
376
      break;
393
 
    case 'G':                   /* --global-env */
 
377
    case 'e':                   /* --global-envs */
394
378
      if(arg == NULL){
395
379
        break;
396
380
      }
397
 
      if(not add_environment(getplugin(NULL), arg, true)){
398
 
        perror("add_environment");
 
381
      {
 
382
        char *envdef = strdup(arg);
 
383
        if(envdef == NULL){
 
384
          break;
 
385
        }
 
386
        if(not add_environment(getplugin(NULL), envdef)){
 
387
          perror("add_environment");
 
388
        }
399
389
      }
400
390
      break;
401
391
    case 'o':                   /* --options-for */
402
392
      if (arg != NULL){
403
393
        char *p_name = strsep(&arg, ":");
404
 
        if(p_name[0] == '\0' or arg == NULL){
 
394
        if(p_name[0] == '\0'){
405
395
          break;
406
396
        }
407
397
        char *opt = strsep(&arg, ":");
408
 
        if(opt[0] == '\0' or opt == NULL){
 
398
        if(opt[0] == '\0'){
409
399
          break;
410
400
        }
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;
 
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
            }
419
411
          }
420
412
        }
421
413
      }
422
414
      break;
423
 
    case 'E':                   /* --env-for */
 
415
    case 'f':                   /* --envs-for */
424
416
      if(arg == NULL){
425
417
        break;
426
418
      }
429
421
        if(envdef == NULL){
430
422
          break;
431
423
        }
432
 
        *envdef = '\0';
433
 
        if(not add_environment(getplugin(arg), envdef+1, true)){
 
424
        char *p_name = strndup(arg, (size_t) (envdef-arg));
 
425
        if(p_name == NULL){
 
426
          break;
 
427
        }
 
428
        envdef++;
 
429
        if(not add_environment(getplugin(p_name), envdef)){
434
430
          perror("add_environment");
435
431
        }
436
432
      }
444
440
        p->disabled = true;
445
441
      }
446
442
      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;
456
443
    case 128:                   /* --plugin-dir */
457
 
      free(plugindir);
458
444
      plugindir = strdup(arg);
459
445
      if(plugindir == NULL){
460
446
        perror("strdup");
461
447
      }      
462
448
      break;
463
449
    case 129:                   /* --config-file */
464
 
      /* This is already done by parse_opt_config_file() */
465
 
      break;
 
450
      argfile = strdup(arg);
 
451
      if(argfile == NULL){
 
452
        perror("strdup");
 
453
      }
 
454
      break;      
466
455
    case 130:                   /* --userid */
467
456
      uid = (uid_t)strtol(arg, NULL, 10);
468
457
      break;
473
462
      debug = true;
474
463
      break;
475
464
    case ARGP_KEY_ARG:
476
 
      /* Cryptsetup always passes an argument, which is an empty
477
 
         string if "none" was specified in /etc/crypttab.  So if
478
 
         argument was empty, we ignore it silently. */
479
 
      if(arg[0] != '\0'){
480
 
        fprintf(stderr, "Ignoring unknown argument \"%s\"\n", arg);
481
 
      }
482
 
      break;
483
 
    case ARGP_KEY_END:
484
 
      break;
485
 
    default:
486
 
      return ARGP_ERR_UNKNOWN;
487
 
    }
488
 
    return 0;
489
 
  }
490
 
  
491
 
  /* This option parser is the same as parse_opt() above, except it
492
 
     ignores everything but the --config-file option. */
493
 
  error_t parse_opt_config_file (int key, char *arg,
494
 
                                 __attribute__((unused))
495
 
                                 struct argp_state *state) {
496
 
    switch (key) {
497
 
    case 'g':                   /* --global-options */
498
 
    case 'G':                   /* --global-env */
499
 
    case 'o':                   /* --options-for */
500
 
    case 'E':                   /* --env-for */
501
 
    case 'd':                   /* --disable */
502
 
    case 'e':                   /* --enable */
503
 
    case 128:                   /* --plugin-dir */
504
 
      break;
505
 
    case 129:                   /* --config-file */
506
 
      free(argfile);
507
 
      argfile = strdup(arg);
508
 
      if(argfile == NULL){
509
 
        perror("strdup");
510
 
      }
511
 
      break;      
512
 
    case 130:                   /* --userid */
513
 
    case 131:                   /* --groupid */
514
 
    case 132:                   /* --debug */
515
 
    case ARGP_KEY_ARG:
516
 
    case ARGP_KEY_END:
517
 
      break;
518
 
    default:
519
 
      return ARGP_ERR_UNKNOWN;
520
 
    }
521
 
    return 0;
522
 
  }
523
 
  
524
 
  struct argp argp = { .options = options,
525
 
                       .parser = parse_opt_config_file,
526
 
                       .args_doc = "",
 
465
      fprintf(stderr, "Ignoring unknown argument \"%s\"\n", arg);
 
466
      break;
 
467
    case ARGP_KEY_END:
 
468
      break;
 
469
    default:
 
470
      return ARGP_ERR_UNKNOWN;
 
471
    }
 
472
    return 0;
 
473
  }
 
474
  
 
475
  struct argp argp = { .options = options, .parser = parse_opt,
 
476
                       .args_doc = "[+PLUS_SEPARATED_OPTIONS]",
527
477
                       .doc = "Mandos plugin runner -- Run plugins" };
528
478
  
529
 
  /* Parse using the parse_opt_config_file in order to get the custom
530
 
     config file location, if any. */
531
 
  ret = argp_parse (&argp, argc, argv, ARGP_IN_ORDER, 0, NULL);
 
479
  ret = argp_parse (&argp, argc, argv, 0, 0, NULL);
532
480
  if (ret == ARGP_ERR_UNKNOWN){
533
481
    fprintf(stderr, "Unknown error while parsing arguments\n");
534
482
    exitstatus = EXIT_FAILURE;
535
483
    goto fallback;
536
484
  }
537
 
  
538
 
  /* Reset to the normal argument parser */
539
 
  argp.parser = parse_opt;
540
 
  
541
 
  /* Open the configfile if available */
 
485
 
 
486
  /* Opens the configfile if aviable */
542
487
  if (argfile == NULL){
543
488
    conffp = fopen(AFILE, "r");
544
489
  } else {
562
507
    custom_argv[0] = argv[0];
563
508
    custom_argv[1] = NULL;
564
509
 
565
 
    /* for each line in the config file, strip whitespace and ignore
566
 
       commented text */
 
510
    /* for each line in the config file, strip whitespace and ignore commented text */
567
511
    while(true){
568
512
      sret = getline(&org_line, &size, conffp);
569
513
      if(sret == -1){
598
542
      }
599
543
    }
600
544
    free(org_line);
601
 
  } else {
 
545
  } else{
602
546
    /* Check for harmful errors and go to fallback. Other errors might
603
547
       not affect opening plugins */
604
548
    if (errno == EMFILE or errno == ENFILE or errno == ENOMEM){
610
554
  /* If there was any arguments from configuration file,
611
555
     pass them to parser as command arguments */
612
556
  if(custom_argv != NULL){
613
 
    ret = argp_parse (&argp, custom_argc, custom_argv, ARGP_IN_ORDER,
614
 
                      0, NULL);
 
557
    ret = argp_parse (&argp, custom_argc, custom_argv, 0, 0, NULL);
615
558
    if (ret == ARGP_ERR_UNKNOWN){
616
559
      fprintf(stderr, "Unknown error while parsing arguments\n");
617
560
      exitstatus = EXIT_FAILURE;
619
562
    }
620
563
  }
621
564
  
622
 
  /* Parse actual command line arguments, to let them override the
623
 
     config file */
624
 
  ret = argp_parse (&argp, argc, argv, ARGP_IN_ORDER, 0, NULL);
625
 
  if (ret == ARGP_ERR_UNKNOWN){
626
 
    fprintf(stderr, "Unknown error while parsing arguments\n");
627
 
    exitstatus = EXIT_FAILURE;
628
 
    goto fallback;
629
 
  }
630
 
  
631
565
  if(debug){
632
566
    for(plugin *p = plugin_list; p != NULL; p=p->next){
633
567
      fprintf(stderr, "Plugin: %s has %d arguments\n",
641
575
      }
642
576
    }
643
577
  }
644
 
  
 
578
 
645
579
  /* Strip permissions down to nobody */
646
580
  ret = setuid(uid);
647
581
  if (ret == -1){
651
585
  if (ret == -1){
652
586
    perror("setgid");
653
587
  }
654
 
  
 
588
 
655
589
  if (plugindir == NULL){
656
590
    dir = opendir(PDIR);
657
591
  } else {
678
612
  }
679
613
  
680
614
  FD_ZERO(&rfds_all);
681
 
  
 
615
 
682
616
  /* Read and execute any executable in the plugin directory*/
683
617
  while(true){
684
618
    dirst = readdir(dir);
685
619
    
686
 
    /* All directory entries have been processed */
 
620
    // All directory entries have been processed
687
621
    if(dirst == NULL){
688
622
      if (errno == EBADF){
689
623
        perror("readdir");
695
629
    
696
630
    d_name_len = strlen(dirst->d_name);
697
631
    
698
 
    /* Ignore dotfiles, backup files and other junk */
 
632
    // Ignore dotfiles, backup files and other junk
699
633
    {
700
634
      bool bad_name = false;
701
635
      
739
673
    }
740
674
 
741
675
    char *filename;
742
 
    if(plugindir == NULL){
743
 
      ret = asprintf(&filename, PDIR "/%s", dirst->d_name);
744
 
    } else {
745
 
      ret = asprintf(&filename, "%s/%s", plugindir, dirst->d_name);
746
 
    }
 
676
    ret = asprintf(&filename, "%s/%s", plugindir, dirst->d_name);
747
677
    if(ret < 0){
748
678
      perror("asprintf");
749
679
      continue;
791
721
        }
792
722
        /* Add global environment variables */
793
723
        for(char **e = g->environ; *e != NULL; e++){
794
 
          if(not add_environment(p, *e, false)){
 
724
          if(not add_environment(p, *e)){
795
725
            perror("add_environment");
796
726
          }
797
727
        }
802
732
       process, too. */
803
733
    if(p->environ[0] != NULL){
804
734
      for(char **e = environ; *e != NULL; e++){
805
 
        if(not add_environment(p, *e, false)){
 
735
        char *copy = strdup(*e);
 
736
        if(copy == NULL){
 
737
          perror("strdup");
 
738
          continue;
 
739
        }
 
740
        if(not add_environment(p, copy)){
806
741
          perror("add_environment");
807
742
        }
808
743
      }
835
770
      exitstatus = EXIT_FAILURE;
836
771
      goto fallback;
837
772
    }
838
 
    /* Starting a new process to be watched */
 
773
    // Starting a new process to be watched
839
774
    pid_t pid = fork();
840
775
    if(pid == -1){
841
776
      perror("fork");
938
873
    }
939
874
    /* OK, now either a process completed, or something can be read
940
875
       from one of them */
941
 
    for(plugin *proc = plugin_list; proc != NULL;){
 
876
    for(plugin *proc = plugin_list; proc != NULL; proc = proc->next){
942
877
      /* Is this process completely done? */
943
878
      if(proc->eof and proc->completed){
944
879
        /* Only accept the plugin output if it exited cleanly */
971
906
            exitstatus = EXIT_FAILURE;
972
907
            goto fallback;
973
908
          }
974
 
          
 
909
          free_plugin(proc);
975
910
          /* We are done modifying process list, so unblock signal */
976
911
          ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask,
977
912
                             NULL);
984
919
          if(plugin_list == NULL){
985
920
            break;
986
921
          }
987
 
          
988
 
          plugin *next_plugin = proc->next;
989
 
          free_plugin(proc);
990
 
          proc = next_plugin;
991
922
          continue;
992
923
        }
993
924
        
1005
936
      /* This process has not completed.  Does it have any output? */
1006
937
      if(proc->eof or not FD_ISSET(proc->fd, &rfds)){
1007
938
        /* This process had nothing to say at this time */
1008
 
        proc = proc->next;
1009
939
        continue;
1010
940
      }
1011
941
      /* Before reading, make the process' data buffer large enough */
1024
954
                 BUFFER_SIZE);
1025
955
      if(ret < 0){
1026
956
        /* Read error from this process; ignore the error */
1027
 
        proc = proc->next;
1028
957
        continue;
1029
958
      }
1030
959
      if(ret == 0){
1045
974
    bool bret;
1046
975
    fprintf(stderr, "Going to fallback mode using getpass(3)\n");
1047
976
    char *passwordbuffer = getpass("Password: ");
1048
 
    size_t len = strlen(passwordbuffer);
1049
 
    /* Strip trailing newline */
1050
 
    if(len > 0 and passwordbuffer[len-1] == '\n'){
1051
 
      passwordbuffer[len-1] = '\0'; /* not strictly necessary */
1052
 
      len--;
1053
 
    }
1054
 
    bret = print_out_password(passwordbuffer, len);
 
977
    bret = print_out_password(passwordbuffer, strlen(passwordbuffer));
1055
978
    if(not bret){
1056
979
      perror("print_out_password");
1057
980
      exitstatus = EXIT_FAILURE;