/mandos/release

To get this branch, use:
bzr branch http://bzr.recompile.se/loggerhead/mandos/release

« back to all changes in this revision

Viewing changes to mandos-client.c

merge

Show diffs side-by-side

added added

removed removed

Lines of Context:
66
66
 
67
67
#define BUFFER_SIZE 256
68
68
 
69
 
const char *argp_program_version = "mandos-client 1.0";
70
 
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
71
 
 
72
69
struct process;
73
70
 
74
71
typedef struct process{
147
144
  return fcntl(fd, F_SETFD, ret | FD_CLOEXEC);
148
145
}
149
146
 
 
147
const char *argp_program_version = "plugbasedclient 0.9";
 
148
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
 
149
 
150
150
process *process_list = NULL;
151
151
 
152
152
/* Mark a process as completed when it exits, and save its exit
170
170
  proc->completed = true;
171
171
}
172
172
 
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){
176
 
    ret = TEMP_FAILURE_RETRY(write(STDOUT_FILENO, buffer + written,
177
 
                                   length - written));
178
 
    if(ret < 0){
179
 
      return false;
180
 
    }
181
 
  }
182
 
  return true;
183
 
}
184
 
 
185
173
int main(int argc, char *argv[]){
186
174
  const char *plugindir = "/conf/conf.d/mandos/plugins.d";
187
175
  size_t d_name_len;
595
583
  dir = NULL;
596
584
    
597
585
  if (process_list == NULL){
598
 
    fprintf(stderr, "No plugin processes started. Incorrect plugin"
599
 
            " directory?\n");
600
 
    process_list = NULL;
 
586
    fprintf(stderr, "No plugin processes started, exiting\n");
 
587
    exitstatus = EXIT_FAILURE;
 
588
    goto end;
601
589
  }
602
590
  while(process_list){
603
591
    fd_set rfds = rfds_all;
618
606
          /* Bad exit by plugin */
619
607
          if(debug){
620
608
            if(WIFEXITED(proc->status)){
621
 
              fprintf(stderr, "Plugin %u exited with status %d\n",
622
 
                      (unsigned int) (proc->pid),
623
 
                      WEXITSTATUS(proc->status));
 
609
              fprintf(stderr, "Plugin %d exited with status %d\n",
 
610
                      proc->pid, WEXITSTATUS(proc->status));
624
611
            } else if(WIFSIGNALED(proc->status)) {
625
 
              fprintf(stderr, "Plugin %u killed by signal %d\n",
626
 
                      (unsigned int) (proc->pid),
627
 
                      WTERMSIG(proc->status));
 
612
              fprintf(stderr, "Plugin %d killed by signal %d\n",
 
613
                      proc->pid, WTERMSIG(proc->status));
628
614
            } else if(WCOREDUMP(proc->status)){
629
 
              fprintf(stderr, "Plugin %d dumped core\n",
630
 
                      (unsigned int) (proc->pid));
 
615
              fprintf(stderr, "Plugin %d dumped core\n", proc->pid);
631
616
            }
632
617
          }
633
618
          /* Remove the plugin */
666
651
          break;
667
652
        }
668
653
        /* This process exited nicely, so print its buffer */
669
 
 
670
 
        bool bret = print_out_password(proc->buffer, proc->buffer_length);
671
 
        if(not bret){
672
 
          perror("print_out_password");
673
 
          exitstatus = EXIT_FAILURE;
 
654
        for(size_t written = 0; written < proc->buffer_length;
 
655
            written += (size_t)ret){
 
656
          ret = TEMP_FAILURE_RETRY(write(STDOUT_FILENO,
 
657
                                         proc->buffer + written,
 
658
                                         proc->buffer_length
 
659
                                         - written));
 
660
          if(ret < 0){
 
661
            perror("write");
 
662
            exitstatus = EXIT_FAILURE;
 
663
            goto end;
 
664
          }
674
665
        }
675
666
        goto end;
676
667
      }
705
696
      }
706
697
    }
707
698
  }
708
 
  
709
699
  if(process_list == NULL){
710
 
    bool bret;
711
 
    fprintf(stderr, "Going to fallback mode using getpass(3)\n");
712
 
    char *passwordbuffer = getpass("Password: ");
713
 
    bret = print_out_password(passwordbuffer, strlen(passwordbuffer));
714
 
    if(not bret){
715
 
      perror("print_out_password");
716
 
      exitstatus = EXIT_FAILURE;
717
 
      goto end;
718
 
    }
719
 
    bret = print_out_password("\n", 1);
720
 
    if(not bret){
721
 
      perror("print_out_password");
722
 
      exitstatus = EXIT_FAILURE;
723
 
    }
 
700
    fprintf(stderr, "All plugin processes failed, exiting\n");
 
701
    exitstatus = EXIT_FAILURE;
724
702
  }
725
 
 
 
703
  
726
704
 end:
727
 
  
728
705
  /* Restore old signal handler */
729
706
  sigaction(SIGCHLD, &old_sigchld_action, NULL);
730
707