/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

version 1.0

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 = "plugin-runner 1.0";
 
69
const char *argp_program_version = "plugbasedclient 1.0";
70
70
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
71
71
 
72
72
struct process;
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
 
 
188
173
int main(int argc, char *argv[]){
189
 
  const char *plugindir = "/lib/mandos/plugins.d";
 
174
  const char *plugindir = "/conf/conf.d/mandos/plugins.d";
190
175
  size_t d_name_len;
191
176
  DIR *dir = NULL;
192
177
  struct dirent *dirst;
202
187
                                      .sa_flags = SA_NOCLDSTOP };
203
188
  char *plus_options = NULL;
204
189
  char **plus_argv = NULL;
205
 
 
 
190
  
206
191
  /* Establish a signal handler */
207
192
  sigemptyset(&sigchld_action.sa_mask);
208
193
  ret = sigaddset(&sigchld_action.sa_mask, SIGCHLD);
307
292
  
308
293
  ret = argp_parse (&argp, argc, argv, 0, 0, &plugin_list);
309
294
  if (ret == ARGP_ERR_UNKNOWN){
310
 
    fprintf(stderr, "Unknown error while parsing arguments\n");
 
295
    fprintf(stderr, "Unkown error while parsing arguments\n");
311
296
    exitstatus = EXIT_FAILURE;
312
297
    goto end;
313
298
  }
344
329
    }
345
330
    ret = argp_parse (&argp, new_argc, plus_argv, 0, 0, &plugin_list);
346
331
    if (ret == ARGP_ERR_UNKNOWN){
347
 
      fprintf(stderr, "Unknown error while parsing arguments\n");
 
332
      fprintf(stderr, "Unkown error while parsing arguments\n");
348
333
      exitstatus = EXIT_FAILURE;
349
334
      goto end;
350
335
    }
455
440
    char *filename = malloc(d_name_len + strlen(plugindir) + 2);
456
441
    if (filename == NULL){
457
442
      perror("malloc");
458
 
      continue;
 
443
      exitstatus = EXIT_FAILURE;
 
444
      goto end;
459
445
    }
460
446
    strcpy(filename, plugindir); /* Spurious warning */
461
447
    strcat(filename, "/");      /* Spurious warning */
464
450
    ret = stat(filename, &st);
465
451
    if (ret == -1){
466
452
      perror("stat");
467
 
      free(filename);
468
 
      continue;
 
453
      exitstatus = EXIT_FAILURE;
 
454
      goto end;
469
455
    }
470
456
    
471
457
    if (not S_ISREG(st.st_mode) or (access(filename, X_OK) != 0)){
520
506
    }
521
507
    // Starting a new process to be watched
522
508
    pid_t pid = fork();
523
 
    if(pid == -1){
524
 
      perror("fork");
525
 
      exitstatus = EXIT_FAILURE;
526
 
      goto end;
527
 
    }
528
509
    if(pid == 0){
529
510
      /* this is the child process */
530
511
      ret = sigaction(SIGCHLD, &old_sigchld_action, NULL);
602
583
  dir = NULL;
603
584
    
604
585
  if (process_list == NULL){
605
 
    fprintf(stderr, "No plugin processes started. Incorrect plugin"
606
 
            " directory?\n");
607
 
    process_list = NULL;
 
586
    fprintf(stderr, "No plugin processes started, exiting\n");
 
587
    exitstatus = EXIT_FAILURE;
 
588
    goto end;
608
589
  }
609
590
  while(process_list){
610
591
    fd_set rfds = rfds_all;
625
606
          /* Bad exit by plugin */
626
607
          if(debug){
627
608
            if(WIFEXITED(proc->status)){
628
 
              fprintf(stderr, "Plugin %u exited with status %d\n",
629
 
                      (unsigned int) (proc->pid),
630
 
                      WEXITSTATUS(proc->status));
 
609
              fprintf(stderr, "Plugin %d exited with status %d\n",
 
610
                      proc->pid, WEXITSTATUS(proc->status));
631
611
            } else if(WIFSIGNALED(proc->status)) {
632
 
              fprintf(stderr, "Plugin %u killed by signal %d\n",
633
 
                      (unsigned int) (proc->pid),
634
 
                      WTERMSIG(proc->status));
 
612
              fprintf(stderr, "Plugin %d killed by signal %d\n",
 
613
                      proc->pid, WTERMSIG(proc->status));
635
614
            } else if(WCOREDUMP(proc->status)){
636
 
              fprintf(stderr, "Plugin %d dumped core\n",
637
 
                      (unsigned int) (proc->pid));
 
615
              fprintf(stderr, "Plugin %d dumped core\n", proc->pid);
638
616
            }
639
617
          }
640
618
          /* Remove the plugin */
673
651
          break;
674
652
        }
675
653
        /* This process exited nicely, so print its buffer */
676
 
 
677
 
        bool bret = print_out_password(proc->buffer, proc->buffer_length);
678
 
        if(not bret){
679
 
          perror("print_out_password");
680
 
          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
          }
681
665
        }
682
666
        goto end;
683
667
      }
712
696
      }
713
697
    }
714
698
  }
715
 
 
716
 
 
 
699
  if(process_list == NULL){
 
700
    fprintf(stderr, "All plugin processes failed, exiting\n");
 
701
    exitstatus = EXIT_FAILURE;
 
702
  }
 
703
  
717
704
 end:
718
 
  
719
 
  if(process_list == NULL or exitstatus != EXIT_SUCCESS){
720
 
    /* Fallback if all plugins failed, none are found or an error occured */
721
 
    bool bret;
722
 
    fprintf(stderr, "Going to fallback mode using getpass(3)\n");
723
 
    char *passwordbuffer = getpass("Password: ");
724
 
    bret = print_out_password(passwordbuffer, strlen(passwordbuffer));
725
 
    if(not bret){
726
 
      perror("print_out_password");
727
 
      exitstatus = EXIT_FAILURE;
728
 
      goto end;
729
 
    }
730
 
  }
731
 
  
732
705
  /* Restore old signal handler */
733
706
  sigaction(SIGCHLD, &old_sigchld_action, NULL);
734
707