/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-08 12:03:16 UTC
  • Revision ID: teddy@fukt.bsnet.se-20080908120316-rrt6r0a5yezc3q1d
* INSTALL: New file.

* mandos-keygen: Use "/etc/mandos/keys" if "/etc/keys/mandos" does not
                 exist.

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,
187
187
  size_t namelen = (size_t)(strchrnul(def, '=') - def);
188
188
  /* Search for this environment variable */
189
189
  for(char **e = p->environ; *e != NULL; e++){
190
 
    if(strncmp(*e, def, namelen+1) == 0){
 
190
    if(strncmp(*e, def, namelen + 1) == 0){
191
191
      /* It already exists */
192
192
      if(replace){
193
 
        char *new = realloc(*e, strlen(def));
 
193
        char *new = realloc(*e, strlen(def) + 1);
194
194
        if(new == NULL){
195
195
          return false;
196
196
        }
238
238
      /* No child processes */
239
239
      break;
240
240
    }
241
 
 
 
241
    
242
242
    /* A child exited, find it in process_list */
243
243
    while(proc != NULL and proc->pid != pid){
244
244
      proc = proc->next;
255
255
/* Prints out a password to stdout */
256
256
bool print_out_password(const char *buffer, size_t length){
257
257
  ssize_t ret;
258
 
  if(length>0 and buffer[length-1] == '\n'){
259
 
    length--;
260
 
  }
261
258
  for(size_t written = 0; written < length; written += (size_t)ret){
262
259
    ret = TEMP_FAILURE_RETRY(write(STDOUT_FILENO, buffer + written,
263
260
                                   length - written));
397
394
      if(arg == NULL){
398
395
        break;
399
396
      }
400
 
      {
401
 
        char *envdef = strdup(arg);
402
 
        if(envdef == NULL){
403
 
          break;
404
 
        }
405
 
        if(not add_environment(getplugin(NULL), envdef, true)){
406
 
          perror("add_environment");
407
 
        }
 
397
      if(not add_environment(getplugin(NULL), arg, true)){
 
398
        perror("add_environment");
408
399
      }
409
400
      break;
410
401
    case 'o':                   /* --options-for */
411
402
      if (arg != NULL){
412
403
        char *p_name = strsep(&arg, ":");
413
 
        if(p_name[0] == '\0'){
 
404
        if(p_name[0] == '\0' or arg == NULL){
414
405
          break;
415
406
        }
416
407
        char *opt = strsep(&arg, ":");
417
 
        if(opt[0] == '\0'){
 
408
        if(opt[0] == '\0' or opt == NULL){
418
409
          break;
419
410
        }
420
 
        if(opt != NULL){
421
 
          char *p;
422
 
          while((p = strsep(&opt, ",")) != NULL){
423
 
            if(p[0] == '\0'){
424
 
              continue;
425
 
            }
426
 
            if(not add_argument(getplugin(p_name), p)){
427
 
              perror("add_argument");
428
 
              return ARGP_ERR_UNKNOWN;
429
 
            }
 
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;
430
419
          }
431
420
        }
432
421
      }
440
429
        if(envdef == NULL){
441
430
          break;
442
431
        }
443
 
        char *p_name = strndup(arg, (size_t) (envdef-arg));
444
 
        if(p_name == NULL){
445
 
          break;
446
 
        }
447
 
        envdef++;
448
 
        if(not add_environment(getplugin(p_name), envdef, true)){
 
432
        *envdef = '\0';
 
433
        if(not add_environment(getplugin(arg), envdef+1, true)){
449
434
          perror("add_environment");
450
435
        }
451
436
      }
469
454
      }
470
455
      break;
471
456
    case 128:                   /* --plugin-dir */
 
457
      free(plugindir);
472
458
      plugindir = strdup(arg);
473
459
      if(plugindir == NULL){
474
460
        perror("strdup");
487
473
      debug = true;
488
474
      break;
489
475
    case ARGP_KEY_ARG:
490
 
      fprintf(stderr, "Ignoring unknown argument \"%s\"\n", 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
      }
491
482
      break;
492
483
    case ARGP_KEY_END:
493
484
      break;
512
503
    case 128:                   /* --plugin-dir */
513
504
      break;
514
505
    case 129:                   /* --config-file */
 
506
      free(argfile);
515
507
      argfile = strdup(arg);
516
508
      if(argfile == NULL){
517
509
        perror("strdup");
747
739
    }
748
740
 
749
741
    char *filename;
750
 
    ret = asprintf(&filename, "%s/%s", plugindir, dirst->d_name);
 
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
    }
751
747
    if(ret < 0){
752
748
      perror("asprintf");
753
749
      continue;
942
938
    }
943
939
    /* OK, now either a process completed, or something can be read
944
940
       from one of them */
945
 
    for(plugin *proc = plugin_list; proc != NULL; proc = proc->next){
 
941
    for(plugin *proc = plugin_list; proc != NULL;){
946
942
      /* Is this process completely done? */
947
943
      if(proc->eof and proc->completed){
948
944
        /* Only accept the plugin output if it exited cleanly */
975
971
            exitstatus = EXIT_FAILURE;
976
972
            goto fallback;
977
973
          }
978
 
          free_plugin(proc);
 
974
          
979
975
          /* We are done modifying process list, so unblock signal */
980
976
          ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask,
981
977
                             NULL);
988
984
          if(plugin_list == NULL){
989
985
            break;
990
986
          }
 
987
          
 
988
          plugin *next_plugin = proc->next;
 
989
          free_plugin(proc);
 
990
          proc = next_plugin;
991
991
          continue;
992
992
        }
993
993
        
1005
1005
      /* This process has not completed.  Does it have any output? */
1006
1006
      if(proc->eof or not FD_ISSET(proc->fd, &rfds)){
1007
1007
        /* This process had nothing to say at this time */
 
1008
        proc = proc->next;
1008
1009
        continue;
1009
1010
      }
1010
1011
      /* Before reading, make the process' data buffer large enough */
1023
1024
                 BUFFER_SIZE);
1024
1025
      if(ret < 0){
1025
1026
        /* Read error from this process; ignore the error */
 
1027
        proc = proc->next;
1026
1028
        continue;
1027
1029
      }
1028
1030
      if(ret == 0){
1043
1045
    bool bret;
1044
1046
    fprintf(stderr, "Going to fallback mode using getpass(3)\n");
1045
1047
    char *passwordbuffer = getpass("Password: ");
1046
 
    bret = print_out_password(passwordbuffer, strlen(passwordbuffer));
 
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);
1047
1055
    if(not bret){
1048
1056
      perror("print_out_password");
1049
1057
      exitstatus = EXIT_FAILURE;