/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

  • Committer: Björn Påhlsson
  • Date: 2008-08-12 16:29:05 UTC
  • mto: (237.7.1 mandos) (24.1.154 mandos)
  • mto: This revision was merged to the branch mainline in revision 69.
  • Revision ID: belorn@braxen-20080812162905-6zm7vdvj81wkumwb
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:
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
 
69
72
struct process;
70
73
 
71
74
typedef struct process{
144
147
  return fcntl(fd, F_SETFD, ret | FD_CLOEXEC);
145
148
}
146
149
 
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
  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
    }
440
457
    char *filename = malloc(d_name_len + strlen(plugindir) + 2);
441
458
    if (filename == NULL){
442
459
      perror("malloc");
443
 
      exitstatus = EXIT_FAILURE;
444
 
      goto end;
 
460
      continue;
445
461
    }
446
462
    strcpy(filename, plugindir); /* Spurious warning */
447
463
    strcat(filename, "/");      /* Spurious warning */
450
466
    ret = stat(filename, &st);
451
467
    if (ret == -1){
452
468
      perror("stat");
453
 
      exitstatus = EXIT_FAILURE;
454
 
      goto end;
 
469
      free(filename);
 
470
      continue;
455
471
    }
456
472
    
457
473
    if (not S_ISREG(st.st_mode) or (access(filename, X_OK) != 0)){
506
522
    }
507
523
    // Starting a new process to be watched
508
524
    pid_t pid = fork();
 
525
    if(pid == -1){
 
526
      perror("fork");
 
527
      exitstatus = EXIT_FAILURE;
 
528
      goto end;
 
529
    }
509
530
    if(pid == 0){
510
531
      /* this is the child process */
511
532
      ret = sigaction(SIGCHLD, &old_sigchld_action, NULL);
583
604
  dir = NULL;
584
605
    
585
606
  if (process_list == NULL){
586
 
    fprintf(stderr, "No plugin processes started, exiting\n");
587
 
    exitstatus = EXIT_FAILURE;
588
 
    goto end;
 
607
    fprintf(stderr, "No plugin processes started. Incorrect plugin"
 
608
            " directory?\n");
 
609
    process_list = NULL;
589
610
  }
590
611
  while(process_list){
591
612
    fd_set rfds = rfds_all;
606
627
          /* Bad exit by plugin */
607
628
          if(debug){
608
629
            if(WIFEXITED(proc->status)){
609
 
              fprintf(stderr, "Plugin %d exited with status %d\n",
610
 
                      proc->pid, WEXITSTATUS(proc->status));
 
630
              fprintf(stderr, "Plugin %u exited with status %d\n",
 
631
                      (unsigned int) (proc->pid),
 
632
                      WEXITSTATUS(proc->status));
611
633
            } else if(WIFSIGNALED(proc->status)) {
612
 
              fprintf(stderr, "Plugin %d killed by signal %d\n",
613
 
                      proc->pid, WTERMSIG(proc->status));
 
634
              fprintf(stderr, "Plugin %u killed by signal %d\n",
 
635
                      (unsigned int) (proc->pid),
 
636
                      WTERMSIG(proc->status));
614
637
            } else if(WCOREDUMP(proc->status)){
615
 
              fprintf(stderr, "Plugin %d dumped core\n", proc->pid);
 
638
              fprintf(stderr, "Plugin %d dumped core\n",
 
639
                      (unsigned int) (proc->pid));
616
640
            }
617
641
          }
618
642
          /* Remove the plugin */
651
675
          break;
652
676
        }
653
677
        /* 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
 
          }
 
678
 
 
679
        bool bret = print_out_password(proc->buffer, proc->buffer_length);
 
680
        if(not bret){
 
681
          perror("print_out_password");
 
682
          exitstatus = EXIT_FAILURE;
665
683
        }
666
684
        goto end;
667
685
      }
696
714
      }
697
715
    }
698
716
  }
699
 
  if(process_list == NULL){
700
 
    fprintf(stderr, "All plugin processes failed, exiting\n");
701
 
    exitstatus = EXIT_FAILURE;
702
 
  }
703
 
  
 
717
 
 
718
 
704
719
 end:
 
720
  
 
721
  if(process_list == NULL or exitstatus != EXIT_SUCCESS){
 
722
    /* Fallback if all plugins failed, none are found or an error occured */
 
723
    bool bret;
 
724
    fprintf(stderr, "Going to fallback mode using getpass(3)\n");
 
725
    char *passwordbuffer = getpass("Password: ");
 
726
    bret = print_out_password(passwordbuffer, strlen(passwordbuffer));
 
727
    if(not bret){
 
728
      perror("print_out_password");
 
729
      exitstatus = EXIT_FAILURE;
 
730
      goto end;
 
731
    }
 
732
  }
 
733
  
705
734
  /* Restore old signal handler */
706
735
  sigaction(SIGCHLD, &old_sigchld_action, NULL);
707
736