/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 plugbasedclient.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>             /* strsep, 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 */
66
 
 
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>";
 
49
#include <argp.h>               /* struct argp_option,
 
50
                                   struct argp_state, struct argp,
 
51
                                   argp_parse() */
72
52
 
73
53
struct process;
74
54
 
92
72
  struct plugin *next;
93
73
} plugin;
94
74
 
95
 
static plugin *getplugin(char *name, plugin **plugin_list){
 
75
plugin *getplugin(char *name, plugin **plugin_list){
96
76
  for (plugin *p = *plugin_list; p != NULL; p = p->next){
97
77
    if ((p->name == name)
98
78
        or (p->name and name and (strcmp(p->name, name) == 0))){
121
101
  return new_plugin;
122
102
}
123
103
 
124
 
static void addargument(plugin *p, char *arg){
 
104
void addargument(plugin *p, char *arg){
125
105
  p->argv[p->argc] = arg;
126
106
  p->argv = realloc(p->argv, sizeof(char *) * (size_t)(p->argc + 2));
127
107
  if (p->argv == NULL){
137
117
 * Descriptor Flags".
138
118
 * *Note File Descriptor Flags:(libc)Descriptor Flags.
139
119
 */
140
 
static int set_cloexec_flag(int fd)
 
120
int set_cloexec_flag(int fd)
141
121
{
142
122
  int ret = fcntl(fd, F_GETFD, 0);
143
123
  /* If reading the flags failed, return error indication now. */
148
128
  return fcntl(fd, F_SETFD, ret | FD_CLOEXEC);
149
129
}
150
130
 
 
131
#define BUFFER_SIZE 256
 
132
 
 
133
const char *argp_program_version = "plugbasedclient 0.9";
 
134
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
 
135
 
151
136
process *process_list = NULL;
152
137
 
153
138
/* Mark a process as completed when it exits, and save its exit
156
141
  process *proc = process_list;
157
142
  int status;
158
143
  pid_t pid = wait(&status);
159
 
  if(pid == -1){
160
 
    perror("wait");
161
 
    return;
162
 
  }
163
144
  while(proc != NULL and proc->pid != pid){
164
 
    proc = proc->next;
 
145
    proc = proc->next;    
165
146
  }
166
147
  if(proc == NULL){
167
148
    /* Process not found in process list */
171
152
  proc->completed = true;
172
153
}
173
154
 
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
 
 
211
155
int main(int argc, char *argv[]){
212
156
  const char *plugindir = "/conf/conf.d/mandos/plugins.d";
213
 
  const char *conffile = CONFFILE;
214
 
  FILE *conffp;
215
157
  size_t d_name_len;
216
158
  DIR *dir = NULL;
217
159
  struct dirent *dirst;
225
167
  struct sigaction old_sigchld_action;
226
168
  struct sigaction sigchld_action = { .sa_handler = handle_sigchld,
227
169
                                      .sa_flags = SA_NOCLDSTOP };
228
 
  char **custom_argv = NULL;
229
 
  int custom_argc = 0;
230
 
  
 
170
 
231
171
  /* Establish a signal handler */
232
172
  sigemptyset(&sigchld_action.sa_mask);
233
173
  ret = sigaddset(&sigchld_action.sa_mask, SIGCHLD);
273
213
    switch (key) {
274
214
    case 'g':
275
215
      if (arg != NULL){
276
 
        char *p;
277
 
        while((p = strsep(&arg, ",")) != NULL){
278
 
          if(p[0] == '\0'){
279
 
            continue;
280
 
          }
 
216
        char *p = strtok(arg, ",");
 
217
        do{
281
218
          addargument(getplugin(NULL, plugins), p);
282
 
        }
 
219
          p = strtok(NULL, ",");
 
220
        } while (p);
283
221
      }
284
222
      break;
285
223
    case 'o':
286
224
      if (arg != NULL){
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
 
            }
 
225
        char *name = strtok(arg, ":");
 
226
        char *p = strtok(NULL, ":");
 
227
        if(p){
 
228
          p = strtok(p, ",");
 
229
          do{
301
230
            addargument(getplugin(name, plugins), p);
302
 
          }
 
231
            p = strtok(NULL, ",");
 
232
          } while (p);
303
233
        }
304
234
      }
305
235
      break;
321
251
      debug = true;
322
252
      break;
323
253
    case ARGP_KEY_ARG:
324
 
      fprintf(stderr, "Ignoring unknown argument \"%s\"\n", arg); 
 
254
      argp_usage (state);
325
255
      break;
326
256
    case ARGP_KEY_END:
327
257
      break;
334
264
  plugin *plugin_list = NULL;
335
265
  
336
266
  struct argp argp = { .options = options, .parser = parse_opt,
337
 
                       .args_doc = "[+PLUS_SEPARATED_OPTIONS]",
 
267
                       .args_doc = "",
338
268
                       .doc = "Mandos plugin runner -- Run plugins" };
339
269
  
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
 
    }
395
 
  }
396
 
  
 
270
  argp_parse (&argp, argc, argv, 0, 0, &plugin_list);  
 
271
 
397
272
  if(debug){
398
273
    for(plugin *p = plugin_list; p != NULL; p=p->next){
399
274
      fprintf(stderr, "Plugin: %s has %d arguments\n",
403
278
      }
404
279
    }
405
280
  }
406
 
  
 
281
 
407
282
  ret = setuid(uid);
408
283
  if (ret == -1){
409
284
    perror("setuid");
411
286
  
412
287
  setgid(gid);
413
288
  if (ret == -1){
414
 
    perror("setgid");
 
289
    perror("setuid");
415
290
  }
416
291
  
417
292
  dir = opendir(plugindir);
441
316
    
442
317
    // All directory entries have been processed
443
318
    if(dirst == NULL){
444
 
      if (errno == EBADF){
445
 
        perror("readdir");
446
 
        exitstatus = EXIT_FAILURE;
447
 
        goto end;
448
 
      }
449
319
      break;
450
320
    }
451
321
    
499
369
    char *filename = malloc(d_name_len + strlen(plugindir) + 2);
500
370
    if (filename == NULL){
501
371
      perror("malloc");
502
 
      continue;
 
372
      exitstatus = EXIT_FAILURE;
 
373
      goto end;
503
374
    }
504
375
    strcpy(filename, plugindir); /* Spurious warning */
505
376
    strcat(filename, "/");      /* Spurious warning */
506
377
    strcat(filename, dirst->d_name); /* Spurious warning */
507
378
    
508
 
    ret = stat(filename, &st);
509
 
    if (ret == -1){
510
 
      perror("stat");
511
 
      free(filename);
512
 
      continue;
513
 
    }
 
379
    stat(filename, &st);
514
380
    
515
381
    if (not S_ISREG(st.st_mode) or (access(filename, X_OK) != 0)){
516
382
      if(debug){
564
430
    }
565
431
    // Starting a new process to be watched
566
432
    pid_t pid = fork();
567
 
    if(pid == -1){
568
 
      perror("fork");
569
 
      exitstatus = EXIT_FAILURE;
570
 
      goto end;
571
 
    }
572
433
    if(pid == 0){
573
434
      /* this is the child process */
574
435
      ret = sigaction(SIGCHLD, &old_sigchld_action, NULL);
581
442
        perror("sigprocmask");
582
443
        _exit(EXIT_FAILURE);
583
444
      }
584
 
 
585
 
      ret = dup2(pipefd[1], STDOUT_FILENO); /* replace our stdout */
586
 
      if(ret == -1){
587
 
        perror("dup2");
588
 
        _exit(EXIT_FAILURE);
589
 
      }
 
445
      dup2(pipefd[1], STDOUT_FILENO); /* replace our stdout */
590
446
      
591
447
      if(dirfd(dir) < 0){
592
448
        /* If dir has no file descriptor, we could not set FD_CLOEXEC
644
500
  
645
501
  closedir(dir);
646
502
  dir = NULL;
647
 
    
 
503
  
648
504
  if (process_list == NULL){
649
 
    fprintf(stderr, "No plugin processes started. Incorrect plugin"
650
 
            " directory?\n");
651
 
    process_list = NULL;
 
505
    fprintf(stderr, "No plugin processes started, exiting\n");
 
506
    exitstatus = EXIT_FAILURE;
 
507
    goto end;
652
508
  }
653
509
  while(process_list){
654
510
    fd_set rfds = rfds_all;
669
525
          /* Bad exit by plugin */
670
526
          if(debug){
671
527
            if(WIFEXITED(proc->status)){
672
 
              fprintf(stderr, "Plugin %u exited with status %d\n",
673
 
                      (unsigned int) (proc->pid),
674
 
                      WEXITSTATUS(proc->status));
 
528
              fprintf(stderr, "Plugin %d exited with status %d\n",
 
529
                      proc->pid, WEXITSTATUS(proc->status));
675
530
            } else if(WIFSIGNALED(proc->status)) {
676
 
              fprintf(stderr, "Plugin %u killed by signal %d\n",
677
 
                      (unsigned int) (proc->pid),
678
 
                      WTERMSIG(proc->status));
 
531
              fprintf(stderr, "Plugin %d killed by signal %d\n",
 
532
                      proc->pid, WTERMSIG(proc->status));
679
533
            } else if(WCOREDUMP(proc->status)){
680
 
              fprintf(stderr, "Plugin %d dumped core\n",
681
 
                      (unsigned int) (proc->pid));
 
534
              fprintf(stderr, "Plugin %d dumped core\n", proc->pid);
682
535
            }
683
536
          }
684
537
          /* Remove the plugin */
717
570
          break;
718
571
        }
719
572
        /* This process exited nicely, so print its buffer */
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;
 
573
        for(size_t written = 0; written < proc->buffer_length;
 
574
            written += (size_t)ret){
 
575
          ret = TEMP_FAILURE_RETRY(write(STDOUT_FILENO,
 
576
                                         proc->buffer + written,
 
577
                                         proc->buffer_length
 
578
                                         - written));
 
579
          if(ret < 0){
 
580
            perror("write");
 
581
            exitstatus = EXIT_FAILURE;
 
582
            goto end;
 
583
          }
725
584
        }
726
585
        goto end;
727
586
      }
756
615
      }
757
616
    }
758
617
  }
759
 
 
760
 
 
 
618
  if(process_list == NULL){
 
619
    fprintf(stderr, "All plugin processes failed, exiting\n");
 
620
    exitstatus = EXIT_FAILURE;
 
621
  }
 
622
  
761
623
 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
 
  
776
624
  /* Restore old signal handler */
777
625
  sigaction(SIGCHLD, &old_sigchld_action, NULL);
778
626
  
779
 
  free(custom_argv);
780
 
  
781
627
  /* Free the plugin list */
782
628
  for(plugin *next; plugin_list != NULL; plugin_list = next){
783
629
    next = plugin_list->next;
793
639
  for(process *next; process_list != NULL; process_list = next){
794
640
    next = process_list->next;
795
641
    close(process_list->fd);
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
 
    }
 
642
    kill(process_list->pid, SIGTERM);
801
643
    free(process_list->buffer);
802
644
    free(process_list);
803
645
  }