/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

plugbasedclient:
        Added support to disable plugins
        Added support to change plugin directory

mandosclient.c
        Fixed some funtion calls that lacked error handling
        small bugfix

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