/mandos/release

To get this branch, use:
bzr branch http://bzr.recompile.se/loggerhead/mandos/release

« back to all changes in this revision

Viewing changes to plugin-runner.c

  • Committer: Teddy Hogeborn
  • Date: 2008-08-15 21:09:25 UTC
  • mfrom: (24.1.52 mandos)
  • Revision ID: teddy@fukt.bsnet.se-20080815210925-32718zu3nrlotix5
Merge.

* plugin-runner.c (ARGFILE): Renamed to "plugin-runner.conf".

Show diffs side-by-side

added added

removed removed

Lines of Context:
21
21
 * Contact the authors at <mandos@fukt.bsnet.se>.
22
22
 */
23
23
 
24
 
#define _GNU_SOURCE             /* TEMP_FAILURE_RETRY() */
 
24
#define _GNU_SOURCE             /* TEMP_FAILURE_RETRY(), getline() */
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 */
 
66
 
 
67
#define BUFFER_SIZE 256
 
68
#define ARGFILE "/conf/conf.d/mandos/plugin-runner.conf"
 
69
 
 
70
const char *argp_program_version = "plugin-runner 1.0";
 
71
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
52
72
 
53
73
struct process;
54
74
 
72
92
  struct plugin *next;
73
93
} plugin;
74
94
 
75
 
plugin *getplugin(char *name, plugin **plugin_list){
 
95
static plugin *getplugin(char *name, plugin **plugin_list){
76
96
  for (plugin *p = *plugin_list; p != NULL; p = p->next){
77
97
    if ((p->name == name)
78
98
        or (p->name and name and (strcmp(p->name, name) == 0))){
101
121
  return new_plugin;
102
122
}
103
123
 
104
 
void addargument(plugin *p, char *arg){
 
124
static void addargument(plugin *p, char *arg){
105
125
  p->argv[p->argc] = arg;
106
126
  p->argv = realloc(p->argv, sizeof(char *) * (size_t)(p->argc + 2));
107
127
  if (p->argv == NULL){
117
137
 * Descriptor Flags".
118
138
 * *Note File Descriptor Flags:(libc)Descriptor Flags.
119
139
 */
120
 
int set_cloexec_flag(int fd)
 
140
static int set_cloexec_flag(int fd)
121
141
{
122
142
  int ret = fcntl(fd, F_GETFD, 0);
123
143
  /* If reading the flags failed, return error indication now. */
128
148
  return fcntl(fd, F_SETFD, ret | FD_CLOEXEC);
129
149
}
130
150
 
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
 
 
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
 
    proc = proc->next;    
 
164
    proc = proc->next;
146
165
  }
147
166
  if(proc == NULL){
148
167
    /* Process not found in process list */
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
 
  const char *plugindir = "/conf/conf.d/mandos/plugins.d";
 
212
  const char *plugindir = "/lib/mandos/plugins.d";
 
213
  const char *argfile = ARGFILE;
 
214
  FILE *conffp;
157
215
  size_t d_name_len;
158
216
  DIR *dir = NULL;
159
217
  struct dirent *dirst;
160
218
  struct stat st;
161
219
  fd_set rfds_all;
162
220
  int ret, maxfd = 0;
 
221
  uid_t uid = 65534;
 
222
  gid_t gid = 65534;
163
223
  bool debug = false;
164
224
  int exitstatus = EXIT_SUCCESS;
 
225
  struct sigaction old_sigchld_action;
 
226
  struct sigaction sigchld_action = { .sa_handler = handle_sigchld,
 
227
                                      .sa_flags = SA_NOCLDSTOP };
 
228
  char **custom_argv = NULL;
 
229
  int custom_argc = 0;
165
230
  
166
231
  /* Establish a signal handler */
167
 
  struct sigaction old_sigchld_action,
168
 
    sigchld_action = { .sa_handler = handle_sigchld,
169
 
                       .sa_flags = SA_NOCLDSTOP };
170
232
  sigemptyset(&sigchld_action.sa_mask);
171
233
  ret = sigaddset(&sigchld_action.sa_mask, SIGCHLD);
172
234
  if(ret < 0){
193
255
    { .name = "plugin-dir", .key = 128,
194
256
      .arg = "DIRECTORY",
195
257
      .doc = "Specify a different plugin directory", .group = 2 },
196
 
    { .name = "debug", .key = 129,
 
258
    { .name = "userid", .key = 129,
 
259
      .arg = "ID", .flags = 0,
 
260
      .doc = "User ID the plugins will run as", .group = 2 },
 
261
    { .name = "groupid", .key = 130,
 
262
      .arg = "ID", .flags = 0,
 
263
      .doc = "Group ID the plugins will run as", .group = 2 },
 
264
    { .name = "debug", .key = 131,
197
265
      .doc = "Debug mode", .group = 3 },
198
266
    { .name = NULL }
199
267
  };
205
273
    switch (key) {
206
274
    case 'g':
207
275
      if (arg != NULL){
208
 
        char *p = strtok(arg, ",");
209
 
        do{
 
276
        char *p;
 
277
        while((p = strsep(&arg, ",")) != NULL){
 
278
          if(p[0] == '\0'){
 
279
            continue;
 
280
          }
210
281
          addargument(getplugin(NULL, plugins), p);
211
 
          p = strtok(NULL, ",");
212
 
        } while (p);
 
282
        }
213
283
      }
214
284
      break;
215
285
    case 'o':
216
286
      if (arg != NULL){
217
 
        char *name = strtok(arg, ":");
218
 
        char *p = strtok(NULL, ":");
219
 
        if(p){
220
 
          p = strtok(p, ",");
221
 
          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
            }
222
301
            addargument(getplugin(name, plugins), p);
223
 
            p = strtok(NULL, ",");
224
 
          } while (p);
 
302
          }
225
303
        }
226
304
      }
227
305
      break;
234
312
      plugindir = arg;
235
313
      break;
236
314
    case 129:
 
315
      uid = (uid_t)strtol(arg, NULL, 10);
 
316
      break;
 
317
    case 130:
 
318
      gid = (gid_t)strtol(arg, NULL, 10);
 
319
      break;
 
320
    case 131:
237
321
      debug = true;
238
322
      break;
239
323
    case ARGP_KEY_ARG:
240
 
      argp_usage (state);
 
324
      fprintf(stderr, "Ignoring unknown argument \"%s\"\n", arg); 
241
325
      break;
242
326
    case ARGP_KEY_END:
243
327
      break;
250
334
  plugin *plugin_list = NULL;
251
335
  
252
336
  struct argp argp = { .options = options, .parser = parse_opt,
253
 
                       .args_doc = "",
 
337
                       .args_doc = "[+PLUS_SEPARATED_OPTIONS]",
254
338
                       .doc = "Mandos plugin runner -- Run plugins" };
255
339
  
256
 
  argp_parse (&argp, argc, 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(argfile, "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 harmful errors and go to fallback. Other errors might
 
380
       not affect opening plugins */
 
381
    if (errno == EMFILE or errno == ENFILE or errno == ENOMEM){
 
382
      perror("fopen");
 
383
      exitstatus = EXIT_FAILURE;
 
384
      goto end;
 
385
    }
 
386
  }
 
387
 
 
388
  if(custom_argv != NULL){
 
389
    custom_argv[0] = argv[0];
 
390
    ret = argp_parse (&argp, custom_argc, custom_argv, 0, 0, &plugin_list);
 
391
    if (ret == ARGP_ERR_UNKNOWN){
 
392
      fprintf(stderr, "Unknown error while parsing arguments\n");
 
393
      exitstatus = EXIT_FAILURE;
 
394
      goto end;
 
395
    }
 
396
  }
257
397
  
258
398
  if(debug){
259
399
    for(plugin *p = plugin_list; p != NULL; p=p->next){
265
405
    }
266
406
  }
267
407
  
 
408
  ret = setuid(uid);
 
409
  if (ret == -1){
 
410
    perror("setuid");
 
411
  }
 
412
  
 
413
  setgid(gid);
 
414
  if (ret == -1){
 
415
    perror("setgid");
 
416
  }
 
417
  
268
418
  dir = opendir(plugindir);
269
419
  if(dir == NULL){
270
420
    perror("Could not open plugin dir");
292
442
    
293
443
    // All directory entries have been processed
294
444
    if(dirst == NULL){
 
445
      if (errno == EBADF){
 
446
        perror("readdir");
 
447
        exitstatus = EXIT_FAILURE;
 
448
        goto end;
 
449
      }
295
450
      break;
296
451
    }
297
452
    
345
500
    char *filename = malloc(d_name_len + strlen(plugindir) + 2);
346
501
    if (filename == NULL){
347
502
      perror("malloc");
348
 
      exitstatus = EXIT_FAILURE;
349
 
      goto end;
 
503
      continue;
350
504
    }
351
505
    strcpy(filename, plugindir); /* Spurious warning */
352
506
    strcat(filename, "/");      /* Spurious warning */
353
507
    strcat(filename, dirst->d_name); /* Spurious warning */
354
508
    
355
 
    stat(filename, &st);
 
509
    ret = stat(filename, &st);
 
510
    if (ret == -1){
 
511
      perror("stat");
 
512
      free(filename);
 
513
      continue;
 
514
    }
356
515
    
357
516
    if (not S_ISREG(st.st_mode) or (access(filename, X_OK) != 0)){
358
517
      if(debug){
406
565
    }
407
566
    // Starting a new process to be watched
408
567
    pid_t pid = fork();
 
568
    if(pid == -1){
 
569
      perror("fork");
 
570
      exitstatus = EXIT_FAILURE;
 
571
      goto end;
 
572
    }
409
573
    if(pid == 0){
410
574
      /* this is the child process */
411
575
      ret = sigaction(SIGCHLD, &old_sigchld_action, NULL);
418
582
        perror("sigprocmask");
419
583
        _exit(EXIT_FAILURE);
420
584
      }
421
 
      dup2(pipefd[1], STDOUT_FILENO); /* replace our stdout */
 
585
 
 
586
      ret = dup2(pipefd[1], STDOUT_FILENO); /* replace our stdout */
 
587
      if(ret == -1){
 
588
        perror("dup2");
 
589
        _exit(EXIT_FAILURE);
 
590
      }
422
591
      
423
592
      if(dirfd(dir) < 0){
424
593
        /* If dir has no file descriptor, we could not set FD_CLOEXEC
476
645
  
477
646
  closedir(dir);
478
647
  dir = NULL;
479
 
  
 
648
    
480
649
  if (process_list == NULL){
481
 
    fprintf(stderr, "No plugin processes started, exiting\n");
482
 
    exitstatus = EXIT_FAILURE;
483
 
    goto end;
 
650
    fprintf(stderr, "No plugin processes started. Incorrect plugin"
 
651
            " directory?\n");
 
652
    process_list = NULL;
484
653
  }
485
654
  while(process_list){
486
655
    fd_set rfds = rfds_all;
501
670
          /* Bad exit by plugin */
502
671
          if(debug){
503
672
            if(WIFEXITED(proc->status)){
504
 
              fprintf(stderr, "Plugin %d exited with status %d\n",
505
 
                      proc->pid, WEXITSTATUS(proc->status));
 
673
              fprintf(stderr, "Plugin %u exited with status %d\n",
 
674
                      (unsigned int) (proc->pid),
 
675
                      WEXITSTATUS(proc->status));
506
676
            } else if(WIFSIGNALED(proc->status)) {
507
 
              fprintf(stderr, "Plugin %d killed by signal %d\n",
508
 
                      proc->pid, WTERMSIG(proc->status));
 
677
              fprintf(stderr, "Plugin %u killed by signal %d\n",
 
678
                      (unsigned int) (proc->pid),
 
679
                      WTERMSIG(proc->status));
509
680
            } else if(WCOREDUMP(proc->status)){
510
 
              fprintf(stderr, "Plugin %d dumped core\n", proc->pid);
 
681
              fprintf(stderr, "Plugin %d dumped core\n",
 
682
                      (unsigned int) (proc->pid));
511
683
            }
512
684
          }
513
685
          /* Remove the plugin */
546
718
          break;
547
719
        }
548
720
        /* This process exited nicely, so print its buffer */
549
 
        for(size_t written = 0; written < proc->buffer_length;
550
 
            written += (size_t)ret){
551
 
          ret = TEMP_FAILURE_RETRY(write(STDOUT_FILENO,
552
 
                                         proc->buffer + written,
553
 
                                         proc->buffer_length
554
 
                                         - written));
555
 
          if(ret < 0){
556
 
            perror("write");
557
 
            exitstatus = EXIT_FAILURE;
558
 
            goto end;
559
 
          }
 
721
 
 
722
        bool bret = print_out_password(proc->buffer, proc->buffer_length);
 
723
        if(not bret){
 
724
          perror("print_out_password");
 
725
          exitstatus = EXIT_FAILURE;
560
726
        }
561
727
        goto end;
562
728
      }
591
757
      }
592
758
    }
593
759
  }
594
 
  if(process_list == NULL){
595
 
    fprintf(stderr, "All plugin processes failed, exiting\n");
596
 
    exitstatus = EXIT_FAILURE;
597
 
  }
598
 
  
 
760
 
 
761
 
599
762
 end:
 
763
  
 
764
  if(process_list == NULL or exitstatus != EXIT_SUCCESS){
 
765
    /* Fallback if all plugins failed, none are found or an error occured */
 
766
    bool bret;
 
767
    fprintf(stderr, "Going to fallback mode using getpass(3)\n");
 
768
    char *passwordbuffer = getpass("Password: ");
 
769
    bret = print_out_password(passwordbuffer, strlen(passwordbuffer));
 
770
    if(not bret){
 
771
      perror("print_out_password");
 
772
      exitstatus = EXIT_FAILURE;
 
773
      goto end;
 
774
    }
 
775
  }
 
776
  
600
777
  /* Restore old signal handler */
601
778
  sigaction(SIGCHLD, &old_sigchld_action, NULL);
602
779
  
 
780
  free(custom_argv);
 
781
  
603
782
  /* Free the plugin list */
604
783
  for(plugin *next; plugin_list != NULL; plugin_list = next){
605
784
    next = plugin_list->next;
615
794
  for(process *next; process_list != NULL; process_list = next){
616
795
    next = process_list->next;
617
796
    close(process_list->fd);
618
 
    kill(process_list->pid, SIGTERM);
 
797
    ret = kill(process_list->pid, SIGTERM);
 
798
    if(ret == -1 and errno != ESRCH){
 
799
      /* set-uid proccesses migth not get closed */
 
800
      perror("kill");
 
801
    }
619
802
    free(process_list->buffer);
620
803
    free(process_list);
621
804
  }