/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

Added optional parameters certdir, certkey and certfile that can be iven at start in the command line.

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
/*
3
3
 * Mandos plugin runner - Run Mandos plugins
4
4
 *
5
 
 * Copyright © 2007-2008 Teddy Hogeborn & Björn Påhlsson
 
5
 * Copyright © 2007-2008 Teddy Hogeborn and Björn Påhlsson.
6
6
 * 
7
7
 * This program is free software: you can redistribute it and/or
8
8
 * modify it under the terms of the GNU General Public License as
18
18
 * along with this program.  If not, see
19
19
 * <http://www.gnu.org/licenses/>.
20
20
 * 
21
 
 * Contact the authors at <mandos@fukt.bsnet.se>.
 
21
 * Contact the authors at <https://www.fukt.bsnet.se/~belorn/> and
 
22
 * <https://www.fukt.bsnet.se/~teddy/>.
22
23
 */
23
24
 
24
 
#define _GNU_SOURCE             /* TEMP_FAILURE_RETRY() */
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() */
42
 
#include <iso646.h>             /* and, or, not */
43
 
#include <dirent.h>             /* DIR, struct dirent, opendir(),
44
 
                                   readdir(), closedir(), dirfd() */
45
 
#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() */
56
 
#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
 
 
69
 
const char *argp_program_version = "mandos-client 1.0";
70
 
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
 
25
#define _FORTIFY_SOURCE 2
 
26
 
 
27
#include <stdio.h>      /* popen, fileno */
 
28
#include <iso646.h>     /* and, or, not */
 
29
#include <sys/types.h>  /* DIR, opendir, stat, struct stat, waitpid,
 
30
                           WIFEXITED, WEXITSTATUS, wait */
 
31
#include <sys/wait.h>   /* wait */
 
32
#include <dirent.h>     /* DIR, opendir */
 
33
#include <sys/stat.h>   /* stat, struct stat */
 
34
#include <unistd.h>     /* stat, struct stat, chdir */
 
35
#include <stdlib.h>     /* EXIT_FAILURE */
 
36
#include <sys/select.h> /* fd_set, select, FD_ZERO, FD_SET,
 
37
                           FD_ISSET */
 
38
#include <string.h>     /* strlen, strcpy, strcat */
 
39
#include <stdbool.h>    /* true */
 
40
#include <sys/wait.h>   /* waitpid, WIFEXITED, WEXITSTATUS */
 
41
#include <errno.h>      /* errno */
 
42
#include <argp.h>       /* argp */
71
43
 
72
44
struct process;
73
45
 
77
49
  char *buffer;
78
50
  size_t buffer_size;
79
51
  size_t buffer_length;
80
 
  bool eof;
81
 
  bool completed;
82
 
  int status;
83
52
  struct process *next;
84
53
} process;
85
54
 
86
55
typedef struct plugin{
87
 
  char *name;                   /* can be NULL or any plugin name */
 
56
  char *name;           /* can be "global" and any plugin name */
88
57
  char **argv;
89
58
  int argc;
90
 
  bool disabled;
91
59
  struct plugin *next;
92
60
} plugin;
93
61
 
94
 
static plugin *getplugin(char *name, plugin **plugin_list){
 
62
plugin *getplugin(char *name, plugin **plugin_list){
95
63
  for (plugin *p = *plugin_list; p != NULL; p = p->next){
96
64
    if ((p->name == name)
97
65
        or (p->name and name and (strcmp(p->name, name) == 0))){
113
81
  new_plugin->argv[0] = name;
114
82
  new_plugin->argv[1] = NULL;
115
83
  new_plugin->argc = 1;
116
 
  new_plugin->disabled = false;
 
84
  /* Append the new plugin to the list */
117
85
  new_plugin->next = *plugin_list;
118
 
  /* Append the new plugin to the list */
119
86
  *plugin_list = new_plugin;
120
87
  return new_plugin;
121
88
}
122
89
 
123
 
static void addargument(plugin *p, char *arg){
 
90
void addarguments(plugin *p, char *arg){
124
91
  p->argv[p->argc] = arg;
125
92
  p->argv = realloc(p->argv, sizeof(char *) * (size_t)(p->argc + 2));
126
93
  if (p->argv == NULL){
130
97
  p->argc++;
131
98
  p->argv[p->argc] = NULL;
132
99
}
133
 
 
134
 
/*
135
 
 * Based on the example in the GNU LibC manual chapter 13.13 "File
136
 
 * Descriptor Flags".
137
 
 * *Note File Descriptor Flags:(libc)Descriptor Flags.
138
 
 */
139
 
static int set_cloexec_flag(int fd)
140
 
{
141
 
  int ret = fcntl(fd, F_GETFD, 0);
142
 
  /* If reading the flags failed, return error indication now. */
143
 
  if(ret < 0){
144
 
    return ret;
145
 
  }
146
 
  /* Store modified flag word in the descriptor. */
147
 
  return fcntl(fd, F_SETFD, ret | FD_CLOEXEC);
148
 
}
149
 
 
150
 
process *process_list = NULL;
151
 
 
152
 
/* Mark a process as completed when it exits, and save its exit
153
 
   status. */
154
 
void handle_sigchld(__attribute__((unused)) int sig){
155
 
  process *proc = process_list;
156
 
  int status;
157
 
  pid_t pid = wait(&status);
158
 
  if(pid == -1){
159
 
    perror("wait");
160
 
    return;
161
 
  }
162
 
  while(proc != NULL and proc->pid != pid){
163
 
    proc = proc->next;
164
 
  }
165
 
  if(proc == NULL){
166
 
    /* Process not found in process list */
167
 
    return;
168
 
  }
169
 
  proc->status = status;
170
 
  proc->completed = true;
171
 
}
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
 
}
 
100
        
 
101
#define BUFFER_SIZE 256
 
102
 
 
103
const char *argp_program_version =
 
104
  "plugbasedclient 0.9";
 
105
const char *argp_program_bug_address =
 
106
  "<mandos@fukt.bsnet.se>";
 
107
static char doc[] =
 
108
  "Mandos plugin runner -- Run Mandos plugins";
 
109
/* A description of the arguments we accept. */
 
110
static char args_doc[] = "";
187
111
 
188
112
int main(int argc, char *argv[]){
189
 
  const char *plugindir = "/conf/conf.d/mandos/plugins.d";
190
 
  size_t d_name_len;
191
 
  DIR *dir = NULL;
 
113
  char plugindir[] = "plugins.d";
 
114
  size_t d_name_len, plugindir_len = sizeof(plugindir)-1;
 
115
  DIR *dir;
192
116
  struct dirent *dirst;
193
117
  struct stat st;
194
 
  fd_set rfds_all;
 
118
  fd_set rfds_orig;
195
119
  int ret, maxfd = 0;
196
 
  uid_t uid = 65534;
197
 
  gid_t gid = 65534;
198
 
  bool debug = false;
199
 
  int exitstatus = EXIT_SUCCESS;
200
 
  struct sigaction old_sigchld_action;
201
 
  struct sigaction sigchld_action = { .sa_handler = handle_sigchld,
202
 
                                      .sa_flags = SA_NOCLDSTOP };
203
 
  char *plus_options = NULL;
204
 
  char **plus_argv = NULL;
205
 
 
206
 
  /* Establish a signal handler */
207
 
  sigemptyset(&sigchld_action.sa_mask);
208
 
  ret = sigaddset(&sigchld_action.sa_mask, SIGCHLD);
209
 
  if(ret < 0){
210
 
    perror("sigaddset");
211
 
    exit(EXIT_FAILURE);
212
 
  }
213
 
  ret = sigaction(SIGCHLD, &sigchld_action, &old_sigchld_action);
214
 
  if(ret < 0){
215
 
    perror("sigaction");
216
 
    exit(EXIT_FAILURE);
217
 
  }
 
120
  process *process_list = NULL;
218
121
  
219
122
  /* The options we understand. */
220
123
  struct argp_option options[] = {
221
124
    { .name = "global-options", .key = 'g',
222
 
      .arg = "OPTION[,OPTION[,...]]",
223
 
      .doc = "Options passed to all plugins" },
 
125
      .arg = "option[,option[,...]]", .flags = 0,
 
126
      .doc = "Options effecting all plugins" },
224
127
    { .name = "options-for", .key = 'o',
225
 
      .arg = "PLUGIN:OPTION[,OPTION[,...]]",
226
 
      .doc = "Options passed only to specified plugin" },
227
 
    { .name = "disable", .key = 'd',
228
 
      .arg = "PLUGIN",
229
 
      .doc = "Disable a specific plugin", .group = 1 },
230
 
    { .name = "plugin-dir", .key = 128,
231
 
      .arg = "DIRECTORY",
232
 
      .doc = "Specify a different plugin directory", .group = 2 },
233
 
    { .name = "userid", .key = 129,
234
 
      .arg = "ID", .flags = 0,
235
 
      .doc = "User ID the plugins will run as", .group = 2 },
236
 
    { .name = "groupid", .key = 130,
237
 
      .arg = "ID", .flags = 0,
238
 
      .doc = "Group ID the plugins will run as", .group = 2 },
239
 
    { .name = "debug", .key = 131,
240
 
      .doc = "Debug mode", .group = 3 },
 
128
      .arg = "plugin:option[,option[,...]]", .flags = 0,
 
129
      .doc = "Options effecting only specified plugins" },
241
130
    { .name = NULL }
242
131
  };
243
132
  
244
133
  error_t parse_opt (int key, char *arg, struct argp_state *state) {
245
 
    /* Get the INPUT argument from `argp_parse', which we know is a
246
 
       pointer to our plugin list pointer. */
 
134
       /* Get the INPUT argument from `argp_parse', which we
 
135
          know is a pointer to our arguments structure. */
247
136
    plugin **plugins = state->input;
248
137
    switch (key) {
249
138
    case 'g':
250
139
      if (arg != NULL){
251
 
        char *p;
252
 
        while((p = strsep(&arg, ",")) != NULL){
253
 
          if(strcmp(p, "") == 0){
254
 
            continue;
255
 
          }
256
 
          addargument(getplugin(NULL, plugins), p);
257
 
        }
 
140
        char *p = strtok(arg, ",");
 
141
        do{
 
142
          addarguments(getplugin(NULL, plugins), p);
 
143
          p = strtok(NULL, ",");
 
144
        } while (p);
258
145
      }
259
146
      break;
260
147
    case 'o':
261
148
      if (arg != NULL){
262
 
        char *name = strsep(&arg, ":");
263
 
        if(strcmp(name, "") == 0){
264
 
          break;
265
 
        }
266
 
        char *opt = strsep(&arg, ":");
267
 
        if(strcmp(opt, "") == 0){
268
 
          break;
269
 
        }
270
 
        if(opt != NULL){
271
 
          char *p;
272
 
          while((p = strsep(&opt, ",")) != NULL){
273
 
            if(strcmp(p, "") == 0){
274
 
              continue;
275
 
            }
276
 
            addargument(getplugin(name, plugins), p);
277
 
          }
278
 
        }
279
 
      }
280
 
      break;
281
 
    case 'd':
282
 
      if (arg != NULL){
283
 
        getplugin(arg, plugins)->disabled = true;
284
 
      }
285
 
      break;
286
 
    case 128:
287
 
      plugindir = arg;
288
 
      break;
289
 
    case 129:
290
 
      uid = (uid_t)strtol(arg, NULL, 10);
291
 
      break;
292
 
    case 130:
293
 
      gid = (gid_t)strtol(arg, NULL, 10);
294
 
      break;
295
 
    case 131:
296
 
      debug = true;
 
149
        char *name = strtok(arg, ":");
 
150
        char *p = strtok(NULL, ":");
 
151
        p = strtok(p, ",");
 
152
        do{
 
153
          addarguments(getplugin(name, plugins), p);
 
154
          p = strtok(NULL, ",");
 
155
        } while (p);
 
156
      }
297
157
      break;
298
158
    case ARGP_KEY_ARG:
299
 
      if(plus_options != NULL or arg == NULL or arg[0] != '+'){
300
 
        argp_usage (state);
301
 
      }
302
 
      plus_options = arg;
 
159
      argp_usage (state);
303
160
      break;
304
161
    case ARGP_KEY_END:
305
162
      break;
308
165
    }
309
166
    return 0;
310
167
  }
311
 
  
 
168
 
312
169
  plugin *plugin_list = NULL;
313
170
  
314
171
  struct argp argp = { .options = options, .parser = parse_opt,
315
 
                       .args_doc = "[+PLUS_SEPARATED_OPTIONS]",
316
 
                       .doc = "Mandos plugin runner -- Run plugins" };
317
 
  
318
 
  ret = argp_parse (&argp, argc, argv, 0, 0, &plugin_list);
319
 
  if (ret == ARGP_ERR_UNKNOWN){
320
 
    fprintf(stderr, "Unknown error while parsing arguments\n");
321
 
    exitstatus = EXIT_FAILURE;
322
 
    goto end;
323
 
  }
324
 
  
325
 
  if(plus_options){
326
 
    /* This is a mangled argument in the form of
327
 
     "+--option+--other-option=parameter+--yet-another-option", etc */
328
 
    /* Make new argc and argv vars, and call argp_parse() again. */
329
 
    plus_options++;             /* skip the first '+' character */
330
 
    const char delims[] = "+";
331
 
    char *arg;
332
 
    int new_argc = 1;
333
 
    plus_argv = malloc(sizeof(char*) * 2);
334
 
    if(plus_argv == NULL){
335
 
      perror("malloc");
336
 
      exitstatus = EXIT_FAILURE;
337
 
      goto end;
338
 
    }
339
 
    plus_argv[0] = argv[0];
340
 
    plus_argv[1] = NULL;
341
 
 
342
 
    while((arg = strsep(&plus_options, delims)) != NULL){
343
 
      new_argc++;
344
 
      plus_argv = realloc(plus_argv, sizeof(char *)
345
 
                         * ((unsigned int) new_argc + 1));
346
 
      if(plus_argv == NULL){
347
 
        perror("realloc");
348
 
        exitstatus = EXIT_FAILURE;
349
 
        goto end;
350
 
      }
351
 
      plus_argv[new_argc-1] = arg;
352
 
      plus_argv[new_argc] = NULL;
353
 
    }
354
 
 
355
 
    ret = argp_parse (&argp, new_argc, plus_argv, 0, 0, &plugin_list);
356
 
    if (ret == ARGP_ERR_UNKNOWN){
357
 
      fprintf(stderr, "Unknown error while parsing arguments\n");
358
 
      exitstatus = EXIT_FAILURE;
359
 
      goto end;
360
 
    }
361
 
  }
362
 
  
363
 
  if(debug){
364
 
    for(plugin *p = plugin_list; p != NULL; p=p->next){
365
 
      fprintf(stderr, "Plugin: %s has %d arguments\n",
366
 
              p->name ? p->name : "Global", p->argc - 1);
367
 
      for(char **a = p->argv; *a != NULL; a++){
368
 
        fprintf(stderr, "\tArg: %s\n", *a);
369
 
      }
370
 
    }
371
 
  }
372
 
  
373
 
  ret = setuid(uid);
374
 
  if (ret == -1){
375
 
    perror("setuid");
376
 
  }
377
 
  
378
 
  setgid(gid);
379
 
  if (ret == -1){
380
 
    perror("setgid");
381
 
  }
 
172
                       .args_doc = args_doc, .doc = doc };
 
173
 
 
174
  argp_parse (&argp, argc, argv, 0, 0, &plugin_list);
 
175
 
 
176
/*   for(plugin *p = plugin_list; p != NULL; p=p->next){ */
 
177
/*     fprintf(stderr, "Plugin: %s has %d arguments\n", p->name ? p->name : "Global", p->argc); */
 
178
/*     for(char **a = p->argv + 1; *a != NULL; a++){ */
 
179
/*       fprintf(stderr, "\tArg: %s\n", *a); */
 
180
/*     } */
 
181
/*   } */
 
182
  
 
183
/*   return 0; */
382
184
  
383
185
  dir = opendir(plugindir);
 
186
  
384
187
  if(dir == NULL){
385
 
    perror("Could not open plugin dir");
386
 
    exitstatus = EXIT_FAILURE;
387
 
    goto end;
388
 
  }
389
 
  
390
 
  /* Set the FD_CLOEXEC flag on the directory, if possible */
391
 
  {
392
 
    int dir_fd = dirfd(dir);
393
 
    if(dir_fd >= 0){
394
 
      ret = set_cloexec_flag(dir_fd);
395
 
      if(ret < 0){
396
 
        perror("set_cloexec_flag");
397
 
        exitstatus = EXIT_FAILURE;
398
 
        goto end;
399
 
      }
400
 
    }
401
 
  }
402
 
  
403
 
  FD_ZERO(&rfds_all);
 
188
    fprintf(stderr, "Can not open directory\n");
 
189
    return EXIT_FAILURE;
 
190
  }
 
191
  
 
192
  FD_ZERO(&rfds_orig);
404
193
  
405
194
  while(true){
406
195
    dirst = readdir(dir);
407
196
    
408
197
    // All directory entries have been processed
409
198
    if(dirst == NULL){
410
 
      if (errno == EBADF){
411
 
        perror("readdir");
412
 
        exitstatus = EXIT_FAILURE;
413
 
        goto end;
414
 
      }
415
199
      break;
416
200
    }
417
201
    
418
202
    d_name_len = strlen(dirst->d_name);
419
203
    
420
 
    // Ignore dotfiles, backup files and other junk
421
 
    {
422
 
      bool bad_name = false;
423
 
      
424
 
      const char const *bad_prefixes[] = { ".", "#", NULL };
425
 
      
426
 
      const char const *bad_suffixes[] = { "~", "#", ".dpkg-new",
427
 
                                           ".dpkg-old",
428
 
                                           ".dpkg-divert", NULL };
429
 
      for(const char **pre = bad_prefixes; *pre != NULL; pre++){
430
 
        size_t pre_len = strlen(*pre);
431
 
        if((d_name_len >= pre_len)
432
 
           and strncmp((dirst->d_name), *pre, pre_len) == 0){
433
 
          if(debug){
434
 
            fprintf(stderr, "Ignoring plugin dir entry \"%s\""
435
 
                    " with bad prefix %s\n", dirst->d_name, *pre);
436
 
          }
437
 
          bad_name = true;
438
 
          break;
439
 
        }
440
 
      }
441
 
      
442
 
      if(bad_name){
443
 
        continue;
444
 
      }
445
 
      
446
 
      for(const char **suf = bad_suffixes; *suf != NULL; suf++){
447
 
        size_t suf_len = strlen(*suf);
448
 
        if((d_name_len >= suf_len)
449
 
           and (strcmp((dirst->d_name)+d_name_len-suf_len, *suf)
450
 
                == 0)){
451
 
          if(debug){
452
 
            fprintf(stderr, "Ignoring plugin dir entry \"%s\""
453
 
                    " with bad suffix %s\n", dirst->d_name, *suf);
454
 
          }
455
 
          bad_name = true;
456
 
          break;
457
 
        }
458
 
      }
459
 
      
460
 
      if(bad_name){
461
 
        continue;
462
 
      }
463
 
    }
464
 
    
465
 
    char *filename = malloc(d_name_len + strlen(plugindir) + 2);
466
 
    if (filename == NULL){
467
 
      perror("malloc");
468
 
      continue;
469
 
    }
470
 
    strcpy(filename, plugindir); /* Spurious warning */
471
 
    strcat(filename, "/");      /* Spurious warning */
472
 
    strcat(filename, dirst->d_name); /* Spurious warning */
473
 
    
474
 
    ret = stat(filename, &st);
475
 
    if (ret == -1){
476
 
      perror("stat");
477
 
      free(filename);
478
 
      continue;
479
 
    }
480
 
    
481
 
    if (not S_ISREG(st.st_mode) or (access(filename, X_OK) != 0)){
482
 
      if(debug){
483
 
        fprintf(stderr, "Ignoring plugin dir entry \"%s\""
484
 
                " with bad type or mode\n", filename);
485
 
      }
486
 
      free(filename);
487
 
      continue;
488
 
    }
489
 
    if(getplugin(dirst->d_name, &plugin_list)->disabled){
490
 
      if(debug){
491
 
        fprintf(stderr, "Ignoring disabled plugin \"%s\"\n",
492
 
                dirst->d_name);
493
 
      }
494
 
      free(filename);
495
 
      continue;
496
 
    }
497
 
    plugin *p = getplugin(dirst->d_name, &plugin_list);
498
 
    {
499
 
      /* Add global arguments to argument list for this plugin */
500
 
      plugin *g = getplugin(NULL, &plugin_list);
501
 
      for(char **a = g->argv + 1; *a != NULL; a++){
502
 
        addargument(p, *a);
503
 
      }
504
 
    }
505
 
    int pipefd[2]; 
506
 
    ret = pipe(pipefd);
507
 
    if (ret == -1){
508
 
      perror("pipe");
509
 
      exitstatus = EXIT_FAILURE;
510
 
      goto end;
511
 
    }
512
 
    ret = set_cloexec_flag(pipefd[0]);
513
 
    if(ret < 0){
514
 
      perror("set_cloexec_flag");
515
 
      exitstatus = EXIT_FAILURE;
516
 
      goto end;
517
 
    }
518
 
    ret = set_cloexec_flag(pipefd[1]);
519
 
    if(ret < 0){
520
 
      perror("set_cloexec_flag");
521
 
      exitstatus = EXIT_FAILURE;
522
 
      goto end;
523
 
    }
524
 
    /* Block SIGCHLD until process is safely in process list */
525
 
    ret = sigprocmask (SIG_BLOCK, &sigchld_action.sa_mask, NULL);
526
 
    if(ret < 0){
527
 
      perror("sigprocmask");
528
 
      exitstatus = EXIT_FAILURE;
529
 
      goto end;
530
 
    }
531
 
    // Starting a new process to be watched
532
 
    pid_t pid = fork();
533
 
    if(pid == -1){
534
 
      perror("fork");
535
 
      exitstatus = EXIT_FAILURE;
536
 
      goto end;
537
 
    }
538
 
    if(pid == 0){
539
 
      /* this is the child process */
540
 
      ret = sigaction(SIGCHLD, &old_sigchld_action, NULL);
541
 
      if(ret < 0){
542
 
        perror("sigaction");
543
 
        _exit(EXIT_FAILURE);
544
 
      }
545
 
      ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
546
 
      if(ret < 0){
547
 
        perror("sigprocmask");
548
 
        _exit(EXIT_FAILURE);
549
 
      }
550
 
 
551
 
      ret = dup2(pipefd[1], STDOUT_FILENO); /* replace our stdout */
552
 
      if(ret == -1){
553
 
        perror("dup2");
554
 
        _exit(EXIT_FAILURE);
555
 
      }
556
 
      
557
 
      if(dirfd(dir) < 0){
558
 
        /* If dir has no file descriptor, we could not set FD_CLOEXEC
559
 
           above and must now close it manually here. */
 
204
    // Ignore dotfiles and backup files
 
205
    if (dirst->d_name[0] == '.'
 
206
        or dirst->d_name[d_name_len - 1] == '~'){
 
207
      continue;
 
208
    }
 
209
 
 
210
    char *filename = malloc(d_name_len + plugindir_len + 2);
 
211
    strcpy(filename, plugindir);
 
212
    strcat(filename, "/");
 
213
    strcat(filename, dirst->d_name);    
 
214
 
 
215
    stat(filename, &st);
 
216
 
 
217
    if (S_ISREG(st.st_mode) and (access(filename, X_OK) == 0)){
 
218
      // Starting a new process to be watched
 
219
      process *new_process = malloc(sizeof(process));
 
220
      int pipefd[2];
 
221
      ret = pipe(pipefd);
 
222
      if (ret == -1){
 
223
        perror(argv[0]);
 
224
        goto end;
 
225
      }
 
226
      new_process->pid = fork();
 
227
      if(new_process->pid == 0){
 
228
        /* this is the child process */
560
229
        closedir(dir);
561
 
      }
562
 
      if(execv(filename, p->argv) < 0){
563
 
        perror("execv");
564
 
        _exit(EXIT_FAILURE);
565
 
      }
566
 
      /* no return */
567
 
    }
568
 
    /* parent process */
569
 
    free(filename);
570
 
    close(pipefd[1]);           /* close unused write end of pipe */
571
 
    process *new_process = malloc(sizeof(process));
572
 
    if (new_process == NULL){
573
 
      perror("malloc");
574
 
      ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
575
 
      if(ret < 0){
576
 
        perror("sigprocmask");
577
 
      }
578
 
      exitstatus = EXIT_FAILURE;
579
 
      goto end;
580
 
    }
581
 
    
582
 
    *new_process = (struct process){ .pid = pid,
583
 
                                     .fd = pipefd[0],
584
 
                                     .next = process_list };
585
 
    // List handling
586
 
    process_list = new_process;
587
 
    /* Unblock SIGCHLD so signal handler can be run if this process
588
 
       has already completed */
589
 
    ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
590
 
    if(ret < 0){
591
 
      perror("sigprocmask");
592
 
      exitstatus = EXIT_FAILURE;
593
 
      goto end;
594
 
    }
595
 
    
596
 
    FD_SET(new_process->fd, &rfds_all);
597
 
    
598
 
    if (maxfd < new_process->fd){
599
 
      maxfd = new_process->fd;
600
 
    }
601
 
    
602
 
  }
603
 
  
604
 
  /* Free the plugin list */
605
 
  for(plugin *next; plugin_list != NULL; plugin_list = next){
606
 
    next = plugin_list->next;
607
 
    free(plugin_list->argv);
608
 
    free(plugin_list);
 
230
        close(pipefd[0]);       /* close unused read end of pipe */
 
231
        dup2(pipefd[1], STDOUT_FILENO); /* replace our stdout */
 
232
        char *basename;
 
233
        basename = strrchr(filename, '/');
 
234
        if (basename == NULL){
 
235
          basename = filename;
 
236
        } else {
 
237
          basename++;
 
238
        }
 
239
        plugin *p = getplugin(basename, &plugin_list);
 
240
 
 
241
        plugin *g = getplugin(NULL, &plugin_list);
 
242
        for(char **a = g->argv + 1; *a != NULL; a++){
 
243
          addarguments(p, *a);
 
244
        }
 
245
        if(execv(filename, p->argv) < 0){
 
246
          perror(argv[0]);
 
247
          close(pipefd[1]);
 
248
          exit(EXIT_FAILURE);
 
249
        }
 
250
        /* no return */
 
251
      }
 
252
      close(pipefd[1]);         /* close unused write end of pipe */
 
253
      new_process->fd = pipefd[0];
 
254
      new_process->buffer = malloc(BUFFER_SIZE);
 
255
      if (new_process->buffer == NULL){
 
256
        perror(argv[0]);
 
257
        goto end;
 
258
      }
 
259
      new_process->buffer_size = BUFFER_SIZE;
 
260
      new_process->buffer_length = 0;
 
261
      FD_SET(new_process->fd, &rfds_orig);
 
262
      
 
263
      if (maxfd < new_process->fd){
 
264
        maxfd = new_process->fd;
 
265
      }
 
266
      
 
267
      //List handling
 
268
      new_process->next = process_list;
 
269
      process_list = new_process;
 
270
    }
609
271
  }
610
272
  
611
273
  closedir(dir);
612
 
  dir = NULL;
613
 
    
614
 
  if (process_list == NULL){
615
 
    fprintf(stderr, "No plugin processes started. Incorrect plugin"
616
 
            " directory?\n");
617
 
    process_list = NULL;
618
 
  }
619
 
  while(process_list){
620
 
    fd_set rfds = rfds_all;
621
 
    int select_ret = select(maxfd+1, &rfds, NULL, NULL, NULL);
622
 
    if (select_ret == -1){
623
 
      perror("select");
624
 
      exitstatus = EXIT_FAILURE;
625
 
      goto end;
626
 
    }
627
 
    /* OK, now either a process completed, or something can be read
628
 
       from one of them */
629
 
    for(process *proc = process_list; proc ; proc = proc->next){
630
 
      /* Is this process completely done? */
631
 
      if(proc->eof and proc->completed){
632
 
        /* Only accept the plugin output if it exited cleanly */
633
 
        if(not WIFEXITED(proc->status)
634
 
           or WEXITSTATUS(proc->status) != 0){
635
 
          /* Bad exit by plugin */
636
 
          if(debug){
637
 
            if(WIFEXITED(proc->status)){
638
 
              fprintf(stderr, "Plugin %u exited with status %d\n",
639
 
                      (unsigned int) (proc->pid),
640
 
                      WEXITSTATUS(proc->status));
641
 
            } else if(WIFSIGNALED(proc->status)) {
642
 
              fprintf(stderr, "Plugin %u killed by signal %d\n",
643
 
                      (unsigned int) (proc->pid),
644
 
                      WTERMSIG(proc->status));
645
 
            } else if(WCOREDUMP(proc->status)){
646
 
              fprintf(stderr, "Plugin %d dumped core\n",
647
 
                      (unsigned int) (proc->pid));
648
 
            }
649
 
          }
650
 
          /* Remove the plugin */
651
 
          FD_CLR(proc->fd, &rfds_all);
652
 
          /* Block signal while modifying process_list */
653
 
          ret = sigprocmask (SIG_BLOCK, &sigchld_action.sa_mask, NULL);
654
 
          if(ret < 0){
655
 
            perror("sigprocmask");
656
 
            exitstatus = EXIT_FAILURE;
657
 
            goto end;
658
 
          }
659
 
          /* Delete this process entry from the list */
660
 
          if(process_list == proc){
661
 
            /* First one - simple */
662
 
            process_list = proc->next;
663
 
          } else {
664
 
            /* Second one or later */
665
 
            for(process *p = process_list; p != NULL; p = p->next){
666
 
              if(p->next == proc){
667
 
                p->next = proc->next;
668
 
                break;
 
274
  
 
275
  if (process_list != NULL){
 
276
    while(true){
 
277
      fd_set rfds = rfds_orig;
 
278
      int select_ret = select(maxfd+1, &rfds, NULL, NULL, NULL);
 
279
      if (select_ret == -1){
 
280
        perror(argv[0]);
 
281
        goto end;
 
282
      }else{    
 
283
        for(process *process_itr = process_list; process_itr != NULL;
 
284
            process_itr = process_itr->next){
 
285
          if(FD_ISSET(process_itr->fd, &rfds)){
 
286
            if(process_itr->buffer_length + BUFFER_SIZE
 
287
               > process_itr->buffer_size){
 
288
                process_itr->buffer = realloc(process_itr->buffer,
 
289
                                              process_itr->buffer_size
 
290
                                              + (size_t) BUFFER_SIZE);
 
291
                if (process_itr->buffer == NULL){
 
292
                  perror(argv[0]);
 
293
                  goto end;
 
294
                }
 
295
                process_itr->buffer_size += BUFFER_SIZE;
 
296
            }
 
297
            ret = read(process_itr->fd, process_itr->buffer
 
298
                       + process_itr->buffer_length, BUFFER_SIZE);
 
299
            if(ret < 0){
 
300
              /* Read error from this process; ignore it */
 
301
              continue;
 
302
            }
 
303
            process_itr->buffer_length += (size_t) ret;
 
304
            if(ret == 0){
 
305
              /* got EOF */
 
306
              /* wait for process exit */
 
307
              int status;
 
308
              waitpid(process_itr->pid, &status, 0);
 
309
              if(WIFEXITED(status) and WEXITSTATUS(status) == 0){
 
310
                for(size_t written = 0;
 
311
                    written < process_itr->buffer_length;){
 
312
                  ret = write(STDOUT_FILENO,
 
313
                              process_itr->buffer + written,
 
314
                              process_itr->buffer_length - written);
 
315
                  if(ret < 0){
 
316
                    perror(argv[0]);
 
317
                    goto end;
 
318
                  }
 
319
                  written += (size_t)ret;
 
320
                }
 
321
                goto end;
 
322
              } else {
 
323
                FD_CLR(process_itr->fd, &rfds_orig);
669
324
              }
670
325
            }
671
326
          }
672
 
          /* We are done modifying process list, so unblock signal */
673
 
          ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask,
674
 
                             NULL);
675
 
          if(ret < 0){
676
 
            perror("sigprocmask");
677
 
          }
678
 
          free(proc->buffer);
679
 
          free(proc);
680
 
          /* We deleted this process from the list, so we can't go
681
 
             proc->next.  Therefore, start over from the beginning of
682
 
             the process list */
683
 
          break;
684
 
        }
685
 
        /* This process exited nicely, so print its buffer */
686
 
 
687
 
        bool bret = print_out_password(proc->buffer, proc->buffer_length);
688
 
        if(not bret){
689
 
          perror("print_out_password");
690
 
          exitstatus = EXIT_FAILURE;
691
 
        }
692
 
        goto end;
693
 
      }
694
 
      /* This process has not completed.  Does it have any output? */
695
 
      if(proc->eof or not FD_ISSET(proc->fd, &rfds)){
696
 
        /* This process had nothing to say at this time */
697
 
        continue;
698
 
      }
699
 
      /* Before reading, make the process' data buffer large enough */
700
 
      if(proc->buffer_length + BUFFER_SIZE > proc->buffer_size){
701
 
        proc->buffer = realloc(proc->buffer, proc->buffer_size
702
 
                               + (size_t) BUFFER_SIZE);
703
 
        if (proc->buffer == NULL){
704
 
          perror("malloc");
705
 
          exitstatus = EXIT_FAILURE;
706
 
          goto end;
707
 
        }
708
 
        proc->buffer_size += BUFFER_SIZE;
709
 
      }
710
 
      /* Read from the process */
711
 
      ret = read(proc->fd, proc->buffer + proc->buffer_length,
712
 
                 BUFFER_SIZE);
713
 
      if(ret < 0){
714
 
        /* Read error from this process; ignore the error */
715
 
        continue;
716
 
      }
717
 
      if(ret == 0){
718
 
        /* got EOF */
719
 
        proc->eof = true;
720
 
      } else {
721
 
        proc->buffer_length += (size_t) ret;
 
327
        }
722
328
      }
723
329
    }
724
330
  }
725
 
 
726
 
 
 
331
  
727
332
 end:
728
 
  
729
 
  if(process_list == NULL or exitstatus != EXIT_SUCCESS){
730
 
    /* Fallback if all plugins failed, none are found or an error occured */
731
 
    bool bret;
732
 
    fprintf(stderr, "Going to fallback mode using getpass(3)\n");
733
 
    char *passwordbuffer = getpass("Password: ");
734
 
    bret = print_out_password(passwordbuffer, strlen(passwordbuffer));
735
 
    if(not bret){
736
 
      perror("print_out_password");
737
 
      exitstatus = EXIT_FAILURE;
738
 
      goto end;
739
 
    }
740
 
  }
741
 
  
742
 
  /* Restore old signal handler */
743
 
  sigaction(SIGCHLD, &old_sigchld_action, NULL);
744
 
  
745
 
  free(plus_argv);
746
 
  
747
 
  /* Free the plugin list */
748
 
  for(plugin *next; plugin_list != NULL; plugin_list = next){
749
 
    next = plugin_list->next;
750
 
    free(plugin_list->argv);
751
 
    free(plugin_list);
752
 
  }
753
 
  
754
 
  if(dir != NULL){
755
 
    closedir(dir);
756
 
  }
757
 
  
758
 
  /* Free the process list and kill the processes */
759
 
  for(process *next; process_list != NULL; process_list = next){
760
 
    next = process_list->next;
761
 
    close(process_list->fd);
762
 
    ret = kill(process_list->pid, SIGTERM);
763
 
    if(ret == -1 and errno != ESRCH){
764
 
      /* set-uid proccesses migth not get closed */
765
 
      perror("kill");
766
 
    }
767
 
    free(process_list->buffer);
768
 
    free(process_list);
769
 
  }
770
 
  
771
 
  /* Wait for any remaining child processes to terminate */
772
 
  do{
773
 
    ret = wait(NULL);
774
 
  } while(ret >= 0);
775
 
  if(errno != ECHILD){
776
 
    perror("wait");
777
 
  }
778
 
  
779
 
  return exitstatus;
 
333
  for(process *process_itr = process_list; process_itr != NULL;
 
334
      process_itr = process_itr->next){
 
335
    close(process_itr->fd);
 
336
    kill(process_itr->pid, SIGTERM);
 
337
    free(process_itr->buffer);
 
338
  }
 
339
  
 
340
  while(true){
 
341
    int status;
 
342
    ret = wait(&status);
 
343
    if (ret == -1){
 
344
      if(errno != ECHILD){
 
345
        perror("wait");
 
346
      }
 
347
      break;
 
348
    }
 
349
  }  
 
350
  return EXIT_SUCCESS;
780
351
}