/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

Added configuration files support for mandos-client
Removed plus argument support for mandos-client

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>             /* strsep, 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
 
68
#define CONFFILE "/conf/conf.d/mandos/client.conf"
 
69
 
 
70
const char *argp_program_version = "mandos-client 1.0";
 
71
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
54
72
 
55
73
struct process;
56
74
 
130
148
  return fcntl(fd, F_SETFD, ret | FD_CLOEXEC);
131
149
}
132
150
 
133
 
const char *argp_program_version = "plugbasedclient 0.9";
134
 
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
135
 
 
136
151
process *process_list = NULL;
137
152
 
138
153
/* Mark a process as completed when it exits, and save its exit
141
156
  process *proc = process_list;
142
157
  int status;
143
158
  pid_t pid = wait(&status);
 
159
  if(pid == -1){
 
160
    perror("wait");
 
161
    return;
 
162
  }
144
163
  while(proc != NULL and proc->pid != pid){
145
164
    proc = proc->next;
146
165
  }
152
171
  proc->completed = true;
153
172
}
154
173
 
 
174
bool print_out_password(const char *buffer, size_t length){
 
175
  ssize_t ret;
 
176
  if(length>0 and buffer[length-1] == '\n'){
 
177
    length--;
 
178
  }
 
179
  for(size_t written = 0; written < length; written += (size_t)ret){
 
180
    ret = TEMP_FAILURE_RETRY(write(STDOUT_FILENO, buffer + written,
 
181
                                   length - written));
 
182
    if(ret < 0){
 
183
      return false;
 
184
    }
 
185
  }
 
186
  return true;
 
187
}
 
188
 
 
189
char ** addcustomargument(char **argv, int *argc, char *arg){
 
190
 
 
191
  if (argv == NULL){
 
192
    *argc = 1;
 
193
    argv = malloc(sizeof(char*) * 2);
 
194
    if(argv == NULL){
 
195
      return NULL;
 
196
    }
 
197
    argv[0] = NULL;     /* Will be set to argv[0] in main before parsing */
 
198
    argv[1] = NULL;
 
199
  }
 
200
  *argc += 1;
 
201
  argv = realloc(argv, sizeof(char *)
 
202
                  * ((unsigned int) *argc + 1));
 
203
  if(argv == NULL){
 
204
    return NULL;
 
205
  }
 
206
  argv[*argc-1] = arg;
 
207
  argv[*argc] = NULL;   
 
208
  return argv;
 
209
}
 
210
 
155
211
int main(int argc, char *argv[]){
156
212
  const char *plugindir = "/conf/conf.d/mandos/plugins.d";
 
213
  const char *conffile = CONFFILE;
 
214
  FILE *conffp;
157
215
  size_t d_name_len;
158
216
  DIR *dir = NULL;
159
217
  struct dirent *dirst;
167
225
  struct sigaction old_sigchld_action;
168
226
  struct sigaction sigchld_action = { .sa_handler = handle_sigchld,
169
227
                                      .sa_flags = SA_NOCLDSTOP };
170
 
  char *plus_options = NULL;
171
 
  char **plus_argv = NULL;
 
228
  char **custom_argv = NULL;
 
229
  int custom_argc = 0;
172
230
  
173
231
  /* Establish a signal handler */
174
232
  sigemptyset(&sigchld_action.sa_mask);
215
273
    switch (key) {
216
274
    case 'g':
217
275
      if (arg != NULL){
218
 
        char *p = strtok(arg, ",");
219
 
        do{
 
276
        char *p;
 
277
        while((p = strsep(&arg, ",")) != NULL){
 
278
          if(p[0] == '\0'){
 
279
            continue;
 
280
          }
220
281
          addargument(getplugin(NULL, plugins), p);
221
 
          p = strtok(NULL, ",");
222
 
        } while (p != NULL);
 
282
        }
223
283
      }
224
284
      break;
225
285
    case 'o':
226
286
      if (arg != NULL){
227
 
        char *name = strtok(arg, ":");
228
 
        char *p = strtok(NULL, ":");
229
 
        if(p != NULL){
230
 
          p = strtok(p, ",");
231
 
          do{
 
287
        char *name = strsep(&arg, ":");
 
288
        if(name[0] == '\0'){
 
289
          break;
 
290
        }
 
291
        char *opt = strsep(&arg, ":");
 
292
        if(opt[0] == '\0'){
 
293
          break;
 
294
        }
 
295
        if(opt != NULL){
 
296
          char *p;
 
297
          while((p = strsep(&opt, ",")) != NULL){
 
298
            if(p[0] == '\0'){
 
299
              continue;
 
300
            }
232
301
            addargument(getplugin(name, plugins), p);
233
 
            p = strtok(NULL, ",");
234
 
          } while (p != NULL);
 
302
          }
235
303
        }
236
304
      }
237
305
      break;
253
321
      debug = true;
254
322
      break;
255
323
    case ARGP_KEY_ARG:
256
 
      if(plus_options != NULL or arg == NULL or arg[0] != '+'){
257
 
        argp_usage (state);
258
 
      }
259
 
      plus_options = arg;
 
324
      fprintf(stderr, "Ignoring unknown argument \"%s\"\n", arg); 
260
325
      break;
261
326
    case ARGP_KEY_END:
262
327
      break;
272
337
                       .args_doc = "[+PLUS_SEPARATED_OPTIONS]",
273
338
                       .doc = "Mandos plugin runner -- Run plugins" };
274
339
  
275
 
  argp_parse (&argp, argc, argv, 0, 0, &plugin_list);  
276
 
  
277
 
  if(plus_options){
278
 
    /* This is a mangled argument in the form of
279
 
     "+--option+--other-option=parameter+--yet-another-option", etc */
280
 
    /* Make new argc and argv vars, and call argp_parse() again. */
281
 
    plus_options++;             /* skip the first '+' character */
282
 
    const char delims[] = "+";
283
 
    char *arg;
284
 
    int new_argc = 1;
285
 
    plus_argv = malloc(sizeof(char*) * 2);
286
 
    if(plus_argv == NULL){
287
 
      perror("malloc");
288
 
      exitstatus = EXIT_FAILURE;
289
 
      goto end;
290
 
    }
291
 
    plus_argv[0] = argv[0];
292
 
    plus_argv[1] = NULL;
293
 
    arg = strtok(plus_options, delims); /* Get first argument */
294
 
    while(arg != NULL){
295
 
      new_argc++;
296
 
      plus_argv = realloc(plus_argv, sizeof(char *)
297
 
                         * ((unsigned int) new_argc + 1));
298
 
      if(plus_argv == NULL){
299
 
        perror("malloc");
300
 
        exitstatus = EXIT_FAILURE;
301
 
        goto end;
302
 
      }
303
 
      plus_argv[new_argc-1] = arg;
304
 
      plus_argv[new_argc] = NULL;
305
 
      arg = strtok(NULL, delims); /* Get next argument */
306
 
    }
307
 
    argp_parse (&argp, new_argc, plus_argv, 0, 0, &plugin_list);  
 
340
  ret = argp_parse (&argp, argc, argv, 0, 0, &plugin_list);
 
341
  if (ret == ARGP_ERR_UNKNOWN){
 
342
    fprintf(stderr, "Unknown error while parsing arguments\n");
 
343
    exitstatus = EXIT_FAILURE;
 
344
    goto end;
 
345
  }
 
346
 
 
347
  conffp = fopen(conffile, "r");
 
348
  if(conffp != NULL){
 
349
    char *org_line = NULL;
 
350
    size_t size = 0;
 
351
    ssize_t sret;
 
352
    char *p, *arg, *new_arg, *line;
 
353
    const char whitespace_delims[] = " \r\t\f\v\n";
 
354
    const char comment_delim[] = "#";
 
355
 
 
356
    while(true){
 
357
      sret = getline(&org_line, &size, conffp);
 
358
      if(sret == -1){
 
359
        break;
 
360
      }
 
361
 
 
362
      line = org_line;
 
363
      arg = strsep(&line, comment_delim);
 
364
      while((p = strsep(&arg, whitespace_delims)) != NULL){
 
365
        if(p[0] == '\0'){
 
366
          continue;
 
367
        }
 
368
        new_arg = strdup(p);
 
369
        custom_argv = addcustomargument(custom_argv, &custom_argc, new_arg);
 
370
        if (custom_argv == NULL){
 
371
          perror("addcustomargument");
 
372
          exitstatus = EXIT_FAILURE;
 
373
          goto end;
 
374
        }
 
375
      }
 
376
    }
 
377
    free(org_line);
 
378
  } else{
 
379
    /* check for harmfull errors */
 
380
    if (errno == EMFILE or errno == ENFILE or errno == ENOMEM){
 
381
      perror("fopen");
 
382
      exitstatus = EXIT_FAILURE;
 
383
      goto end;
 
384
    }
 
385
  }
 
386
 
 
387
  if(custom_argv != NULL){
 
388
    custom_argv[0] = argv[0];
 
389
    ret = argp_parse (&argp, custom_argc, custom_argv, 0, 0, &plugin_list);
 
390
    if (ret == ARGP_ERR_UNKNOWN){
 
391
      fprintf(stderr, "Unknown error while parsing arguments\n");
 
392
      exitstatus = EXIT_FAILURE;
 
393
      goto end;
 
394
    }
308
395
  }
309
396
  
310
397
  if(debug){
354
441
    
355
442
    // All directory entries have been processed
356
443
    if(dirst == NULL){
 
444
      if (errno == EBADF){
 
445
        perror("readdir");
 
446
        exitstatus = EXIT_FAILURE;
 
447
        goto end;
 
448
      }
357
449
      break;
358
450
    }
359
451
    
407
499
    char *filename = malloc(d_name_len + strlen(plugindir) + 2);
408
500
    if (filename == NULL){
409
501
      perror("malloc");
410
 
      exitstatus = EXIT_FAILURE;
411
 
      goto end;
 
502
      continue;
412
503
    }
413
504
    strcpy(filename, plugindir); /* Spurious warning */
414
505
    strcat(filename, "/");      /* Spurious warning */
415
506
    strcat(filename, dirst->d_name); /* Spurious warning */
416
507
    
417
 
    stat(filename, &st);
 
508
    ret = stat(filename, &st);
 
509
    if (ret == -1){
 
510
      perror("stat");
 
511
      free(filename);
 
512
      continue;
 
513
    }
418
514
    
419
515
    if (not S_ISREG(st.st_mode) or (access(filename, X_OK) != 0)){
420
516
      if(debug){
468
564
    }
469
565
    // Starting a new process to be watched
470
566
    pid_t pid = fork();
 
567
    if(pid == -1){
 
568
      perror("fork");
 
569
      exitstatus = EXIT_FAILURE;
 
570
      goto end;
 
571
    }
471
572
    if(pid == 0){
472
573
      /* this is the child process */
473
574
      ret = sigaction(SIGCHLD, &old_sigchld_action, NULL);
480
581
        perror("sigprocmask");
481
582
        _exit(EXIT_FAILURE);
482
583
      }
483
 
      dup2(pipefd[1], STDOUT_FILENO); /* replace our stdout */
 
584
 
 
585
      ret = dup2(pipefd[1], STDOUT_FILENO); /* replace our stdout */
 
586
      if(ret == -1){
 
587
        perror("dup2");
 
588
        _exit(EXIT_FAILURE);
 
589
      }
484
590
      
485
591
      if(dirfd(dir) < 0){
486
592
        /* If dir has no file descriptor, we could not set FD_CLOEXEC
538
644
  
539
645
  closedir(dir);
540
646
  dir = NULL;
541
 
  
 
647
    
542
648
  if (process_list == NULL){
543
 
    fprintf(stderr, "No plugin processes started, exiting\n");
544
 
    exitstatus = EXIT_FAILURE;
545
 
    goto end;
 
649
    fprintf(stderr, "No plugin processes started. Incorrect plugin"
 
650
            " directory?\n");
 
651
    process_list = NULL;
546
652
  }
547
653
  while(process_list){
548
654
    fd_set rfds = rfds_all;
563
669
          /* Bad exit by plugin */
564
670
          if(debug){
565
671
            if(WIFEXITED(proc->status)){
566
 
              fprintf(stderr, "Plugin %d exited with status %d\n",
567
 
                      proc->pid, WEXITSTATUS(proc->status));
 
672
              fprintf(stderr, "Plugin %u exited with status %d\n",
 
673
                      (unsigned int) (proc->pid),
 
674
                      WEXITSTATUS(proc->status));
568
675
            } else if(WIFSIGNALED(proc->status)) {
569
 
              fprintf(stderr, "Plugin %d killed by signal %d\n",
570
 
                      proc->pid, WTERMSIG(proc->status));
 
676
              fprintf(stderr, "Plugin %u killed by signal %d\n",
 
677
                      (unsigned int) (proc->pid),
 
678
                      WTERMSIG(proc->status));
571
679
            } else if(WCOREDUMP(proc->status)){
572
 
              fprintf(stderr, "Plugin %d dumped core\n", proc->pid);
 
680
              fprintf(stderr, "Plugin %d dumped core\n",
 
681
                      (unsigned int) (proc->pid));
573
682
            }
574
683
          }
575
684
          /* Remove the plugin */
608
717
          break;
609
718
        }
610
719
        /* 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
 
          }
 
720
 
 
721
        bool bret = print_out_password(proc->buffer, proc->buffer_length);
 
722
        if(not bret){
 
723
          perror("print_out_password");
 
724
          exitstatus = EXIT_FAILURE;
622
725
        }
623
726
        goto end;
624
727
      }
653
756
      }
654
757
    }
655
758
  }
656
 
  if(process_list == NULL){
657
 
    fprintf(stderr, "All plugin processes failed, exiting\n");
658
 
    exitstatus = EXIT_FAILURE;
659
 
  }
660
 
  
 
759
 
 
760
 
661
761
 end:
 
762
  
 
763
  if(process_list == NULL or exitstatus != EXIT_SUCCESS){
 
764
    /* Fallback if all plugins failed, none are found or an error occured */
 
765
    bool bret;
 
766
    fprintf(stderr, "Going to fallback mode using getpass(3)\n");
 
767
    char *passwordbuffer = getpass("Password: ");
 
768
    bret = print_out_password(passwordbuffer, strlen(passwordbuffer));
 
769
    if(not bret){
 
770
      perror("print_out_password");
 
771
      exitstatus = EXIT_FAILURE;
 
772
      goto end;
 
773
    }
 
774
  }
 
775
  
662
776
  /* Restore old signal handler */
663
777
  sigaction(SIGCHLD, &old_sigchld_action, NULL);
664
778
  
665
 
  free(plus_argv);
 
779
  free(custom_argv);
666
780
  
667
781
  /* Free the plugin list */
668
782
  for(plugin *next; plugin_list != NULL; plugin_list = next){
679
793
  for(process *next; process_list != NULL; process_list = next){
680
794
    next = process_list->next;
681
795
    close(process_list->fd);
682
 
    kill(process_list->pid, SIGTERM);
 
796
    ret = kill(process_list->pid, SIGTERM);
 
797
    if(ret == -1 and errno != ESRCH){
 
798
      /* set-uid proccesses migth not get closed */
 
799
      perror("kill");
 
800
    }
683
801
    free(process_list->buffer);
684
802
    free(process_list);
685
803
  }