/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

  • Committer: Teddy Hogeborn
  • Date: 2008-08-12 19:22:34 UTC
  • Revision ID: teddy@fukt.bsnet.se-20080812192234-8bm17713ltih9ud1
* initramfs-tools-hook: New.
* initramfs-tools-hook-conf: New.

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;
190
 
  
 
205
 
191
206
  /* Establish a signal handler */
192
207
  sigemptyset(&sigchld_action.sa_mask);
193
208
  ret = sigaddset(&sigchld_action.sa_mask, SIGCHLD);
292
307
  
293
308
  ret = argp_parse (&argp, argc, argv, 0, 0, &plugin_list);
294
309
  if (ret == ARGP_ERR_UNKNOWN){
295
 
    fprintf(stderr, "Unkown error while parsing arguments\n");
 
310
    fprintf(stderr, "Unknown error while parsing arguments\n");
296
311
    exitstatus = EXIT_FAILURE;
297
312
    goto end;
298
313
  }
329
344
    }
330
345
    ret = argp_parse (&argp, new_argc, plus_argv, 0, 0, &plugin_list);
331
346
    if (ret == ARGP_ERR_UNKNOWN){
332
 
      fprintf(stderr, "Unkown error while parsing arguments\n");
 
347
      fprintf(stderr, "Unknown error while parsing arguments\n");
333
348
      exitstatus = EXIT_FAILURE;
334
349
      goto end;
335
350
    }
583
598
  dir = NULL;
584
599
    
585
600
  if (process_list == NULL){
586
 
    fprintf(stderr, "No plugin processes started, exiting\n");
587
 
    exitstatus = EXIT_FAILURE;
588
 
    goto end;
 
601
    fprintf(stderr, "No plugin processes started. Incorrect plugin"
 
602
            " directory?\n");
 
603
    process_list = NULL;
589
604
  }
590
605
  while(process_list){
591
606
    fd_set rfds = rfds_all;
606
621
          /* Bad exit by plugin */
607
622
          if(debug){
608
623
            if(WIFEXITED(proc->status)){
609
 
              fprintf(stderr, "Plugin %d exited with status %d\n",
610
 
                      proc->pid, WEXITSTATUS(proc->status));
 
624
              fprintf(stderr, "Plugin %u exited with status %d\n",
 
625
                      (unsigned int) (proc->pid),
 
626
                      WEXITSTATUS(proc->status));
611
627
            } else if(WIFSIGNALED(proc->status)) {
612
 
              fprintf(stderr, "Plugin %d killed by signal %d\n",
613
 
                      proc->pid, WTERMSIG(proc->status));
 
628
              fprintf(stderr, "Plugin %u killed by signal %d\n",
 
629
                      (unsigned int) (proc->pid),
 
630
                      WTERMSIG(proc->status));
614
631
            } else if(WCOREDUMP(proc->status)){
615
 
              fprintf(stderr, "Plugin %d dumped core\n", proc->pid);
 
632
              fprintf(stderr, "Plugin %d dumped core\n",
 
633
                      (unsigned int) (proc->pid));
616
634
            }
617
635
          }
618
636
          /* Remove the plugin */
651
669
          break;
652
670
        }
653
671
        /* This process exited nicely, so print its buffer */
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
 
          }
 
672
 
 
673
        bool bret = print_out_password(proc->buffer, proc->buffer_length);
 
674
        if(not bret){
 
675
          perror("print_out_password");
 
676
          exitstatus = EXIT_FAILURE;
665
677
        }
666
678
        goto end;
667
679
      }
696
708
      }
697
709
    }
698
710
  }
699
 
  if(process_list == NULL){
700
 
    fprintf(stderr, "All plugin processes failed, exiting\n");
701
 
    exitstatus = EXIT_FAILURE;
702
 
  }
703
 
  
 
711
 
 
712
 
704
713
 end:
 
714
  
 
715
  if(process_list == NULL or exitstatus != EXIT_SUCCESS){
 
716
    /* Fallback if all plugins failed or an error occured */
 
717
    bool bret;
 
718
    fprintf(stderr, "Going to fallback mode using getpass(3)\n");
 
719
    char *passwordbuffer = getpass("Password: ");
 
720
    bret = print_out_password(passwordbuffer, strlen(passwordbuffer));
 
721
    if(not bret){
 
722
      perror("print_out_password");
 
723
      exitstatus = EXIT_FAILURE;
 
724
      goto end;
 
725
    }
 
726
  }
 
727
  
705
728
  /* Restore old signal handler */
706
729
  sigaction(SIGCHLD, &old_sigchld_action, NULL);
707
730