/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

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