/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

merge

Show diffs side-by-side

added added

removed removed

Lines of Context:
23
23
 
24
24
#define _GNU_SOURCE             /* TEMP_FAILURE_RETRY() */
25
25
 
26
 
#include <stddef.h>             /* size_t, NULL */
27
 
#include <stdlib.h>             /* malloc(), exit(), EXIT_FAILURE,
28
 
                                   EXIT_SUCCESS, realloc() */
29
 
#include <stdbool.h>            /* bool, true, false */
30
 
#include <stdio.h>              /* perror, popen(), fileno(),
31
 
                                   fprintf(), stderr, STDOUT_FILENO */
32
 
#include <sys/types.h>          /* DIR, opendir(), stat(), struct
33
 
                                   stat, waitpid(), WIFEXITED(),
34
 
                                   WEXITSTATUS(), wait(), pid_t,
35
 
                                   uid_t, gid_t, getuid(), getgid(),
36
 
                                   dirfd() */
37
 
#include <sys/select.h>         /* fd_set, select(), FD_ZERO(),
38
 
                                   FD_SET(), FD_ISSET(), FD_CLR */
39
 
#include <sys/wait.h>           /* wait(), waitpid(), WIFEXITED(),
40
 
                                   WEXITSTATUS() */
41
 
#include <sys/stat.h>           /* struct stat, stat(), S_ISREG() */
 
26
#include <stdio.h>              /* popen(), fileno(), fprintf(),
 
27
                                   stderr, STDOUT_FILENO */
42
28
#include <iso646.h>             /* and, or, not */
 
29
#include <sys/types.h>          /* DIR, opendir(), stat(),
 
30
                                   struct stat, waitpid(),
 
31
                                   WIFEXITED(), WEXITSTATUS(),
 
32
                                   wait() */
 
33
#include <sys/wait.h>           /* wait() */
43
34
#include <dirent.h>             /* DIR, struct dirent, opendir(),
44
 
                                   readdir(), closedir(), dirfd() */
 
35
                                   readdir(), closedir() */
 
36
#include <sys/stat.h>           /* struct stat, stat(), S_ISREG() */
45
37
#include <unistd.h>             /* struct stat, stat(), S_ISREG(),
46
 
                                   fcntl(), setuid(), setgid(),
47
 
                                   F_GETFD, F_SETFD, FD_CLOEXEC,
48
 
                                   access(), pipe(), fork(), close()
49
 
                                   dup2, STDOUT_FILENO, _exit(),
50
 
                                   execv(), write(), read(),
51
 
                                   close() */
52
 
#include <fcntl.h>              /* fcntl(), F_GETFD, F_SETFD,
53
 
                                   FD_CLOEXEC */
54
 
#include <string.h>             /* strtok, strlen(), strcpy(),
55
 
                                   strcat() */
 
38
                                   fcntl() */
 
39
#include <fcntl.h>              /* fcntl() */
 
40
#include <stddef.h>             /* NULL */
 
41
#include <stdlib.h>             /* EXIT_FAILURE */
 
42
#include <sys/select.h>         /* fd_set, select(), FD_ZERO(),
 
43
                                   FD_SET(), FD_ISSET() */
 
44
#include <string.h>             /* strlen(), strcpy(), strcat() */
 
45
#include <stdbool.h>            /* true */
 
46
#include <sys/wait.h>           /* waitpid(), WIFEXITED(),
 
47
                                   WEXITSTATUS() */
56
48
#include <errno.h>              /* errno */
57
 
#include <argp.h>               /* struct argp_option, struct
58
 
                                   argp_state, struct argp,
59
 
                                   argp_parse(), ARGP_ERR_UNKNOWN,
60
 
                                   ARGP_KEY_END, ARGP_KEY_ARG, error_t */
61
 
#include <signal.h>             /* struct sigaction, sigemptyset(),
62
 
                                   sigaddset(), sigaction(),
63
 
                                   sigprocmask(), SIG_BLOCK, SIGCHLD,
64
 
                                   SIG_UNBLOCK, kill() */
65
 
#include <errno.h>              /* errno, EBADF */
 
49
#include <argp.h>               /* struct argp_option,
 
50
                                   struct argp_state, struct argp,
 
51
                                   argp_parse() */
66
52
 
67
53
#define BUFFER_SIZE 256
68
54
 
69
 
const char *argp_program_version = "plugin-runner 1.0";
70
 
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
71
 
 
72
55
struct process;
73
56
 
74
57
typedef struct process{
147
130
  return fcntl(fd, F_SETFD, ret | FD_CLOEXEC);
148
131
}
149
132
 
 
133
const char *argp_program_version = "plugbasedclient 0.9";
 
134
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
 
135
 
150
136
process *process_list = NULL;
151
137
 
152
138
/* Mark a process as completed when it exits, and save its exit
155
141
  process *proc = process_list;
156
142
  int status;
157
143
  pid_t pid = wait(&status);
158
 
  if(pid == -1){
159
 
    perror("wait");
160
 
    return;
161
 
  }
162
144
  while(proc != NULL and proc->pid != pid){
163
145
    proc = proc->next;
164
146
  }
170
152
  proc->completed = true;
171
153
}
172
154
 
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
155
int main(int argc, char *argv[]){
189
 
  const char *plugindir = "/lib/mandos/plugins.d";
 
156
  const char *plugindir = "/conf/conf.d/mandos/plugins.d";
190
157
  size_t d_name_len;
191
158
  DIR *dir = NULL;
192
159
  struct dirent *dirst;
202
169
                                      .sa_flags = SA_NOCLDSTOP };
203
170
  char *plus_options = NULL;
204
171
  char **plus_argv = NULL;
205
 
 
 
172
  
206
173
  /* Establish a signal handler */
207
174
  sigemptyset(&sigchld_action.sa_mask);
208
175
  ret = sigaddset(&sigchld_action.sa_mask, SIGCHLD);
305
272
                       .args_doc = "[+PLUS_SEPARATED_OPTIONS]",
306
273
                       .doc = "Mandos plugin runner -- Run plugins" };
307
274
  
308
 
  ret = argp_parse (&argp, argc, argv, 0, 0, &plugin_list);
309
 
  if (ret == ARGP_ERR_UNKNOWN){
310
 
    fprintf(stderr, "Unknown error while parsing arguments\n");
311
 
    exitstatus = EXIT_FAILURE;
312
 
    goto end;
313
 
  }
 
275
  argp_parse (&argp, argc, argv, 0, 0, &plugin_list);  
314
276
  
315
277
  if(plus_options){
316
278
    /* This is a mangled argument in the form of
334
296
      plus_argv = realloc(plus_argv, sizeof(char *)
335
297
                         * ((unsigned int) new_argc + 1));
336
298
      if(plus_argv == NULL){
337
 
        perror("realloc");
 
299
        perror("malloc");
338
300
        exitstatus = EXIT_FAILURE;
339
301
        goto end;
340
302
      }
342
304
      plus_argv[new_argc] = NULL;
343
305
      arg = strtok(NULL, delims); /* Get next argument */
344
306
    }
345
 
    ret = argp_parse (&argp, new_argc, plus_argv, 0, 0, &plugin_list);
346
 
    if (ret == ARGP_ERR_UNKNOWN){
347
 
      fprintf(stderr, "Unknown error while parsing arguments\n");
348
 
      exitstatus = EXIT_FAILURE;
349
 
      goto end;
350
 
    }
 
307
    argp_parse (&argp, new_argc, plus_argv, 0, 0, &plugin_list);  
351
308
  }
352
309
  
353
310
  if(debug){
397
354
    
398
355
    // All directory entries have been processed
399
356
    if(dirst == NULL){
400
 
      if (errno == EBADF){
401
 
        perror("readdir");
402
 
        exitstatus = EXIT_FAILURE;
403
 
        goto end;
404
 
      }
405
357
      break;
406
358
    }
407
359
    
455
407
    char *filename = malloc(d_name_len + strlen(plugindir) + 2);
456
408
    if (filename == NULL){
457
409
      perror("malloc");
458
 
      continue;
 
410
      exitstatus = EXIT_FAILURE;
 
411
      goto end;
459
412
    }
460
413
    strcpy(filename, plugindir); /* Spurious warning */
461
414
    strcat(filename, "/");      /* Spurious warning */
462
415
    strcat(filename, dirst->d_name); /* Spurious warning */
463
416
    
464
 
    ret = stat(filename, &st);
465
 
    if (ret == -1){
466
 
      perror("stat");
467
 
      free(filename);
468
 
      continue;
469
 
    }
 
417
    stat(filename, &st);
470
418
    
471
419
    if (not S_ISREG(st.st_mode) or (access(filename, X_OK) != 0)){
472
420
      if(debug){
520
468
    }
521
469
    // Starting a new process to be watched
522
470
    pid_t pid = fork();
523
 
    if(pid == -1){
524
 
      perror("fork");
525
 
      exitstatus = EXIT_FAILURE;
526
 
      goto end;
527
 
    }
528
471
    if(pid == 0){
529
472
      /* this is the child process */
530
473
      ret = sigaction(SIGCHLD, &old_sigchld_action, NULL);
537
480
        perror("sigprocmask");
538
481
        _exit(EXIT_FAILURE);
539
482
      }
540
 
 
541
 
      ret = dup2(pipefd[1], STDOUT_FILENO); /* replace our stdout */
542
 
      if(ret == -1){
543
 
        perror("dup2");
544
 
        _exit(EXIT_FAILURE);
545
 
      }
 
483
      dup2(pipefd[1], STDOUT_FILENO); /* replace our stdout */
546
484
      
547
485
      if(dirfd(dir) < 0){
548
486
        /* If dir has no file descriptor, we could not set FD_CLOEXEC
600
538
  
601
539
  closedir(dir);
602
540
  dir = NULL;
603
 
    
 
541
  
604
542
  if (process_list == NULL){
605
 
    fprintf(stderr, "No plugin processes started. Incorrect plugin"
606
 
            " directory?\n");
607
 
    process_list = NULL;
 
543
    fprintf(stderr, "No plugin processes started, exiting\n");
 
544
    exitstatus = EXIT_FAILURE;
 
545
    goto end;
608
546
  }
609
547
  while(process_list){
610
548
    fd_set rfds = rfds_all;
625
563
          /* Bad exit by plugin */
626
564
          if(debug){
627
565
            if(WIFEXITED(proc->status)){
628
 
              fprintf(stderr, "Plugin %u exited with status %d\n",
629
 
                      (unsigned int) (proc->pid),
630
 
                      WEXITSTATUS(proc->status));
 
566
              fprintf(stderr, "Plugin %d exited with status %d\n",
 
567
                      proc->pid, WEXITSTATUS(proc->status));
631
568
            } else if(WIFSIGNALED(proc->status)) {
632
 
              fprintf(stderr, "Plugin %u killed by signal %d\n",
633
 
                      (unsigned int) (proc->pid),
634
 
                      WTERMSIG(proc->status));
 
569
              fprintf(stderr, "Plugin %d killed by signal %d\n",
 
570
                      proc->pid, WTERMSIG(proc->status));
635
571
            } else if(WCOREDUMP(proc->status)){
636
 
              fprintf(stderr, "Plugin %d dumped core\n",
637
 
                      (unsigned int) (proc->pid));
 
572
              fprintf(stderr, "Plugin %d dumped core\n", proc->pid);
638
573
            }
639
574
          }
640
575
          /* Remove the plugin */
673
608
          break;
674
609
        }
675
610
        /* 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;
 
611
        for(size_t written = 0; written < proc->buffer_length;
 
612
            written += (size_t)ret){
 
613
          ret = TEMP_FAILURE_RETRY(write(STDOUT_FILENO,
 
614
                                         proc->buffer + written,
 
615
                                         proc->buffer_length
 
616
                                         - written));
 
617
          if(ret < 0){
 
618
            perror("write");
 
619
            exitstatus = EXIT_FAILURE;
 
620
            goto end;
 
621
          }
681
622
        }
682
623
        goto end;
683
624
      }
712
653
      }
713
654
    }
714
655
  }
715
 
 
716
 
 
 
656
  if(process_list == NULL){
 
657
    fprintf(stderr, "All plugin processes failed, exiting\n");
 
658
    exitstatus = EXIT_FAILURE;
 
659
  }
 
660
  
717
661
 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
662
  /* Restore old signal handler */
733
663
  sigaction(SIGCHLD, &old_sigchld_action, NULL);
734
664
  
749
679
  for(process *next; process_list != NULL; process_list = next){
750
680
    next = process_list->next;
751
681
    close(process_list->fd);
752
 
    ret = kill(process_list->pid, SIGTERM);
753
 
    if(ret == -1 and errno != ESRCH){
754
 
      /* set-uid proccesses migth not get closed */
755
 
      perror("kill");
756
 
    }
 
682
    kill(process_list->pid, SIGTERM);
757
683
    free(process_list->buffer);
758
684
    free(process_list);
759
685
  }