/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 mandos-client.c

mandos-client
        Better error handling for failing to start plugins
        Handle fork errors
password-request
        correct exit routine

Show diffs side-by-side

added added

removed removed

Lines of Context:
171
171
}
172
172
 
173
173
bool print_out_password(const char *buffer, size_t length){
174
 
  size_t ret;
175
 
  for(size_t written = 0; written < length; written += ret){
 
174
  ssize_t ret;
 
175
  if(length>0 and buffer[length-1] == '\n'){
 
176
    length--;
 
177
  }
 
178
  for(size_t written = 0; written < length; written += (size_t)ret){
176
179
    ret = TEMP_FAILURE_RETRY(write(STDOUT_FILENO, buffer + written,
177
180
                                   length - written));
178
181
    if(ret < 0){
199
202
                                      .sa_flags = SA_NOCLDSTOP };
200
203
  char *plus_options = NULL;
201
204
  char **plus_argv = NULL;
 
205
 
 
206
  errno = 0;
202
207
  
203
208
  /* Establish a signal handler */
204
209
  sigemptyset(&sigchld_action.sa_mask);
304
309
  
305
310
  ret = argp_parse (&argp, argc, argv, 0, 0, &plugin_list);
306
311
  if (ret == ARGP_ERR_UNKNOWN){
307
 
    fprintf(stderr, "Unkown error while parsing arguments\n");
 
312
    fprintf(stderr, "Unknown error while parsing arguments\n");
308
313
    exitstatus = EXIT_FAILURE;
309
314
    goto end;
310
315
  }
341
346
    }
342
347
    ret = argp_parse (&argp, new_argc, plus_argv, 0, 0, &plugin_list);
343
348
    if (ret == ARGP_ERR_UNKNOWN){
344
 
      fprintf(stderr, "Unkown error while parsing arguments\n");
 
349
      fprintf(stderr, "Unknown error while parsing arguments\n");
345
350
      exitstatus = EXIT_FAILURE;
346
351
      goto end;
347
352
    }
452
457
    char *filename = malloc(d_name_len + strlen(plugindir) + 2);
453
458
    if (filename == NULL){
454
459
      perror("malloc");
455
 
      exitstatus = EXIT_FAILURE;
456
 
      goto end;
 
460
      continue;
457
461
    }
458
462
    strcpy(filename, plugindir); /* Spurious warning */
459
463
    strcat(filename, "/");      /* Spurious warning */
462
466
    ret = stat(filename, &st);
463
467
    if (ret == -1){
464
468
      perror("stat");
465
 
      exitstatus = EXIT_FAILURE;
466
 
      goto end;
 
469
      free(filename);
 
470
      continue;
467
471
    }
468
472
    
469
473
    if (not S_ISREG(st.st_mode) or (access(filename, X_OK) != 0)){
518
522
    }
519
523
    // Starting a new process to be watched
520
524
    pid_t pid = fork();
 
525
    if(pid == -1){
 
526
      perror("fork");
 
527
      exitstatus = EXIT_FAILURE;
 
528
      goto end;
 
529
    }
521
530
    if(pid == 0){
522
531
      /* this is the child process */
523
532
      ret = sigaction(SIGCHLD, &old_sigchld_action, NULL);
705
714
      }
706
715
    }
707
716
  }
 
717
 
 
718
 
 
719
 end:
708
720
  
709
 
  if(process_list == NULL){
 
721
  if(process_list == NULL or exitstatus != EXIT_SUCCESS){
 
722
    /* Fallback if all plugins failed, none are found or an error occured */
710
723
    bool bret;
711
724
    fprintf(stderr, "Going to fallback mode using getpass(3)\n");
712
725
    char *passwordbuffer = getpass("Password: ");
716
729
      exitstatus = EXIT_FAILURE;
717
730
      goto end;
718
731
    }
719
 
    bret = print_out_password("\n", 1);
720
 
    if(not bret){
721
 
      perror("print_out_password");
722
 
      exitstatus = EXIT_FAILURE;
723
 
    }
724
732
  }
725
 
 
726
 
 end:
727
733
  
728
734
  /* Restore old signal handler */
729
735
  sigaction(SIGCHLD, &old_sigchld_action, NULL);