/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

Fixed fallback on error in mandos-client
fixed a bug in password-request

Show diffs side-by-side

added added

removed removed

Lines of Context:
170
170
  proc->completed = true;
171
171
}
172
172
 
 
173
bool print_out_password(const char *buffer, size_t length){
 
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){
 
179
    ret = TEMP_FAILURE_RETRY(write(STDOUT_FILENO, buffer + written,
 
180
                                   length - written));
 
181
    if(ret < 0){
 
182
      return false;
 
183
    }
 
184
  }
 
185
  return true;
 
186
}
 
187
 
173
188
int main(int argc, char *argv[]){
174
189
  const char *plugindir = "/conf/conf.d/mandos/plugins.d";
175
190
  size_t d_name_len;
187
202
                                      .sa_flags = SA_NOCLDSTOP };
188
203
  char *plus_options = NULL;
189
204
  char **plus_argv = NULL;
 
205
 
 
206
  errno = 0;
190
207
  
191
208
  /* Establish a signal handler */
192
209
  sigemptyset(&sigchld_action.sa_mask);
292
309
  
293
310
  ret = argp_parse (&argp, argc, argv, 0, 0, &plugin_list);
294
311
  if (ret == ARGP_ERR_UNKNOWN){
295
 
    fprintf(stderr, "Unkown error while parsing arguments\n");
 
312
    fprintf(stderr, "Unknown error while parsing arguments\n");
296
313
    exitstatus = EXIT_FAILURE;
297
314
    goto end;
298
315
  }
329
346
    }
330
347
    ret = argp_parse (&argp, new_argc, plus_argv, 0, 0, &plugin_list);
331
348
    if (ret == ARGP_ERR_UNKNOWN){
332
 
      fprintf(stderr, "Unkown error while parsing arguments\n");
 
349
      fprintf(stderr, "Unknown error while parsing arguments\n");
333
350
      exitstatus = EXIT_FAILURE;
334
351
      goto end;
335
352
    }
583
600
  dir = NULL;
584
601
    
585
602
  if (process_list == NULL){
586
 
    fprintf(stderr, "No plugin processes started, exiting\n");
587
 
    exitstatus = EXIT_FAILURE;
588
 
    goto end;
 
603
    fprintf(stderr, "No plugin processes started. Incorrect plugin"
 
604
            " directory?\n");
 
605
    process_list = NULL;
589
606
  }
590
607
  while(process_list){
591
608
    fd_set rfds = rfds_all;
654
671
          break;
655
672
        }
656
673
        /* This process exited nicely, so print its buffer */
657
 
        for(size_t written = 0; written < proc->buffer_length;
658
 
            written += (size_t)ret){
659
 
          ret = TEMP_FAILURE_RETRY(write(STDOUT_FILENO,
660
 
                                         proc->buffer + written,
661
 
                                         proc->buffer_length
662
 
                                         - written));
663
 
          if(ret < 0){
664
 
            perror("write");
665
 
            exitstatus = EXIT_FAILURE;
666
 
            goto end;
667
 
          }
 
674
 
 
675
        bool bret = print_out_password(proc->buffer, proc->buffer_length);
 
676
        if(not bret){
 
677
          perror("print_out_password");
 
678
          exitstatus = EXIT_FAILURE;
668
679
        }
669
680
        goto end;
670
681
      }
699
710
      }
700
711
    }
701
712
  }
702
 
  if(process_list == NULL){
703
 
    fprintf(stderr, "All plugin processes failed, exiting\n");
704
 
    exitstatus = EXIT_FAILURE;
705
 
  }
706
 
  
 
713
 
 
714
 
707
715
 end:
 
716
  
 
717
  if(process_list == NULL or exitstatus != EXIT_SUCCESS){
 
718
    /* Fallback if all plugins failed or an error occured */
 
719
    bool bret;
 
720
    fprintf(stderr, "Going to fallback mode using getpass(3)\n");
 
721
    char *passwordbuffer = getpass("Password: ");
 
722
    bret = print_out_password(passwordbuffer, strlen(passwordbuffer));
 
723
    if(not bret){
 
724
      perror("print_out_password");
 
725
      exitstatus = EXIT_FAILURE;
 
726
      goto end;
 
727
    }
 
728
  }
 
729
  
708
730
  /* Restore old signal handler */
709
731
  sigaction(SIGCHLD, &old_sigchld_action, NULL);
710
732