/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

fixed two bugs:
      A overbuffer flow in enviro variables causing seg fault
      A incorrect use of strsep causing seg fault

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
  }
258
261
  for(size_t written = 0; written < length; written += (size_t)ret){
259
262
    ret = TEMP_FAILURE_RETRY(write(STDOUT_FILENO, buffer + written,
260
263
                                   length - written));
394
397
      if(arg == NULL){
395
398
        break;
396
399
      }
397
 
      if(not add_environment(getplugin(NULL), arg, true)){
398
 
        perror("add_environment");
 
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
        }
399
408
      }
400
409
      break;
401
410
    case 'o':                   /* --options-for */
429
438
        if(envdef == NULL){
430
439
          break;
431
440
        }
432
 
        *envdef = '\0';
433
 
        if(not add_environment(getplugin(arg), envdef+1, true)){
 
441
        char *p_name = strndup(arg, (size_t) (envdef-arg));
 
442
        if(p_name == NULL){
 
443
          break;
 
444
        }
 
445
        envdef++;
 
446
        if(not add_environment(getplugin(p_name), envdef, true)){
434
447
          perror("add_environment");
435
448
        }
436
449
      }
454
467
      }
455
468
      break;
456
469
    case 128:                   /* --plugin-dir */
457
 
      free(plugindir);
458
470
      plugindir = strdup(arg);
459
471
      if(plugindir == NULL){
460
472
        perror("strdup");
498
510
    case 128:                   /* --plugin-dir */
499
511
      break;
500
512
    case 129:                   /* --config-file */
501
 
      free(argfile);
502
513
      argfile = strdup(arg);
503
514
      if(argfile == NULL){
504
515
        perror("strdup");
734
745
    }
735
746
 
736
747
    char *filename;
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
 
    }
 
748
    ret = asprintf(&filename, "%s/%s", plugindir, dirst->d_name);
742
749
    if(ret < 0){
743
750
      perror("asprintf");
744
751
      continue;
933
940
    }
934
941
    /* OK, now either a process completed, or something can be read
935
942
       from one of them */
936
 
    for(plugin *proc = plugin_list; proc != NULL;){
 
943
    for(plugin *proc = plugin_list; proc != NULL; proc = proc->next){
937
944
      /* Is this process completely done? */
938
945
      if(proc->eof and proc->completed){
939
946
        /* Only accept the plugin output if it exited cleanly */
966
973
            exitstatus = EXIT_FAILURE;
967
974
            goto fallback;
968
975
          }
969
 
          
 
976
          free_plugin(proc);
970
977
          /* We are done modifying process list, so unblock signal */
971
978
          ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask,
972
979
                             NULL);
979
986
          if(plugin_list == NULL){
980
987
            break;
981
988
          }
982
 
          
983
 
          plugin *next_plugin = proc->next;
984
 
          free_plugin(proc);
985
 
          proc = next_plugin;
986
989
          continue;
987
990
        }
988
991
        
1000
1003
      /* This process has not completed.  Does it have any output? */
1001
1004
      if(proc->eof or not FD_ISSET(proc->fd, &rfds)){
1002
1005
        /* This process had nothing to say at this time */
1003
 
        proc = proc->next;
1004
1006
        continue;
1005
1007
      }
1006
1008
      /* Before reading, make the process' data buffer large enough */
1019
1021
                 BUFFER_SIZE);
1020
1022
      if(ret < 0){
1021
1023
        /* Read error from this process; ignore the error */
1022
 
        proc = proc->next;
1023
1024
        continue;
1024
1025
      }
1025
1026
      if(ret == 0){
1040
1041
    bool bret;
1041
1042
    fprintf(stderr, "Going to fallback mode using getpass(3)\n");
1042
1043
    char *passwordbuffer = getpass("Password: ");
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);
 
1044
    bret = print_out_password(passwordbuffer, strlen(passwordbuffer));
1050
1045
    if(not bret){
1051
1046
      perror("print_out_password");
1052
1047
      exitstatus = EXIT_FAILURE;