/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 plugin-runner.c

  • Committer: Teddy Hogeborn
  • Date: 2008-11-08 17:26:35 UTC
  • Revision ID: teddy@fukt.bsnet.se-20081108172635-6eexzbysvsk4bu1z
* mandos: Also import "with_statement" and "absolute_import" from
          "__future__.  Import "closing" from "contextlib".
  (Client.__init__): Use "with closing" on "secfile".
  (Client.checker_callback): Call "self.bump_timeout()" instead of
                             doing it directly.
  (Client.bump_timeout): New method to bump up the timeout.
  (TCP_handler.handle): Simplify code to provide fallback default for
                        GnuTLS priority string.
  (IPv6_TCPServer): Do not inherit from
                    "SocketServer.ForkingTCPServer", since that is
                    undocumented - instead, follow the example code
                    from the documentation.
  (if_nametoindex): Use "with closing" on the socket.

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 © 2008 Teddy Hogeborn & 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
21
21
 * Contact the authors at <mandos@fukt.bsnet.se>.
22
22
 */
23
23
 
24
 
#define _GNU_SOURCE             /* TEMP_FAILURE_RETRY() */
25
 
 
 
24
#define _GNU_SOURCE             /* TEMP_FAILURE_RETRY(), getline(),
 
25
                                   asprintf() */
26
26
#include <stddef.h>             /* size_t, NULL */
27
27
#include <stdlib.h>             /* malloc(), exit(), EXIT_FAILURE,
28
28
                                   EXIT_SUCCESS, realloc() */
29
29
#include <stdbool.h>            /* bool, true, false */
30
 
#include <stdio.h>              /* perror, popen(), fileno(),
31
 
                                   fprintf(), stderr, STDOUT_FILENO */
 
30
#include <stdio.h>              /* perror, fileno(), fprintf(),
 
31
                                   stderr, STDOUT_FILENO */
32
32
#include <sys/types.h>          /* DIR, opendir(), stat(), struct
33
33
                                   stat, waitpid(), WIFEXITED(),
34
34
                                   WEXITSTATUS(), wait(), pid_t,
46
46
                                   fcntl(), setuid(), setgid(),
47
47
                                   F_GETFD, F_SETFD, FD_CLOEXEC,
48
48
                                   access(), pipe(), fork(), close()
49
 
                                   dup2, STDOUT_FILENO, _exit(),
 
49
                                   dup2(), STDOUT_FILENO, _exit(),
50
50
                                   execv(), write(), read(),
51
51
                                   close() */
52
52
#include <fcntl.h>              /* fcntl(), F_GETFD, F_SETFD,
53
53
                                   FD_CLOEXEC */
54
 
#include <string.h>             /* strsep, strlen(), strcpy(),
55
 
                                   strcat() */
 
54
#include <string.h>             /* strsep, strlen(), asprintf() */
56
55
#include <errno.h>              /* errno */
57
56
#include <argp.h>               /* struct argp_option, struct
58
57
                                   argp_state, struct argp,
59
58
                                   argp_parse(), ARGP_ERR_UNKNOWN,
60
 
                                   ARGP_KEY_END, ARGP_KEY_ARG, error_t */
 
59
                                   ARGP_KEY_END, ARGP_KEY_ARG,
 
60
                                   error_t */
61
61
#include <signal.h>             /* struct sigaction, sigemptyset(),
62
62
                                   sigaddset(), sigaction(),
63
63
                                   sigprocmask(), SIG_BLOCK, SIGCHLD,
65
65
#include <errno.h>              /* errno, EBADF */
66
66
 
67
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";
 
68
 
 
69
#define PDIR "/lib/mandos/plugins.d"
 
70
#define AFILE "/conf/conf.d/mandos/plugin-runner.conf"
 
71
 
 
72
const char *argp_program_version = "plugin-runner " VERSION;
71
73
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
72
74
 
73
 
struct process;
 
75
typedef struct plugin{
 
76
  char *name;                   /* can be NULL or any plugin name */
 
77
  char **argv;
 
78
  int argc;
 
79
  char **environ;
 
80
  int envc;
 
81
  bool disabled;
74
82
 
75
 
typedef struct process{
 
83
  /* Variables used for running processes*/
76
84
  pid_t pid;
77
85
  int fd;
78
86
  char *buffer;
79
87
  size_t buffer_size;
80
88
  size_t buffer_length;
81
89
  bool eof;
82
 
  bool completed;
83
 
  int status;
84
 
  struct process *next;
85
 
} process;
86
 
 
87
 
typedef struct plugin{
88
 
  char *name;                   /* can be NULL or any plugin name */
89
 
  char **argv;
90
 
  int argc;
91
 
  bool disabled;
 
90
  volatile bool completed;
 
91
  volatile int status;
92
92
  struct plugin *next;
93
93
} plugin;
94
94
 
95
 
static plugin *getplugin(char *name, plugin **plugin_list){
96
 
  for (plugin *p = *plugin_list; p != NULL; p = p->next){
 
95
static plugin *plugin_list = NULL;
 
96
 
 
97
/* Gets an existing plugin based on name,
 
98
   or if none is found, creates a new one */
 
99
static plugin *getplugin(char *name){
 
100
  /* Check for exiting plugin with that name */
 
101
  for (plugin *p = plugin_list; p != NULL; p = p->next){
97
102
    if ((p->name == name)
98
103
        or (p->name and name and (strcmp(p->name, name) == 0))){
99
104
      return p;
102
107
  /* Create a new plugin */
103
108
  plugin *new_plugin = malloc(sizeof(plugin));
104
109
  if (new_plugin == NULL){
105
 
    perror("malloc");
106
 
    exit(EXIT_FAILURE);
107
 
  }
108
 
  new_plugin->name = name;
 
110
    return NULL;
 
111
  }
 
112
  char *copy_name = NULL;
 
113
  if(name != NULL){
 
114
    copy_name = strdup(name);
 
115
    if(copy_name == NULL){
 
116
      return NULL;
 
117
    }
 
118
  }
 
119
  
 
120
  *new_plugin = (plugin) { .name = copy_name,
 
121
                           .argc = 1,
 
122
                           .disabled = false,
 
123
                           .next = plugin_list };
 
124
  
109
125
  new_plugin->argv = malloc(sizeof(char *) * 2);
110
126
  if (new_plugin->argv == NULL){
111
 
    perror("malloc");
112
 
    exit(EXIT_FAILURE);
 
127
    free(copy_name);
 
128
    free(new_plugin);
 
129
    return NULL;
113
130
  }
114
 
  new_plugin->argv[0] = name;
 
131
  new_plugin->argv[0] = copy_name;
115
132
  new_plugin->argv[1] = NULL;
116
 
  new_plugin->argc = 1;
117
 
  new_plugin->disabled = false;
118
 
  new_plugin->next = *plugin_list;
 
133
  
 
134
  new_plugin->environ = malloc(sizeof(char *));
 
135
  if(new_plugin->environ == NULL){
 
136
    free(copy_name);
 
137
    free(new_plugin->argv);
 
138
    free(new_plugin);
 
139
    return NULL;
 
140
  }
 
141
  new_plugin->environ[0] = NULL;
 
142
  
119
143
  /* Append the new plugin to the list */
120
 
  *plugin_list = new_plugin;
 
144
  plugin_list = new_plugin;
121
145
  return new_plugin;
122
146
}
123
147
 
124
 
static void addargument(plugin *p, char *arg){
125
 
  p->argv[p->argc] = arg;
126
 
  p->argv = realloc(p->argv, sizeof(char *) * (size_t)(p->argc + 2));
127
 
  if (p->argv == NULL){
128
 
    perror("malloc");
129
 
    exit(EXIT_FAILURE);
130
 
  }
131
 
  p->argc++;
132
 
  p->argv[p->argc] = NULL;
 
148
/* Helper function for add_argument and add_environment */
 
149
static bool add_to_char_array(const char *new, char ***array,
 
150
                              int *len){
 
151
  /* Resize the pointed-to array to hold one more pointer */
 
152
  *array = realloc(*array, sizeof(char *)
 
153
                   * (size_t) ((*len) + 2));
 
154
  /* Malloc check */
 
155
  if(*array == NULL){
 
156
    return false;
 
157
  }
 
158
  /* Make a copy of the new string */
 
159
  char *copy = strdup(new);
 
160
  if(copy == NULL){
 
161
    return false;
 
162
  }
 
163
  /* Insert the copy */
 
164
  (*array)[*len] = copy;
 
165
  (*len)++;
 
166
  /* Add a new terminating NULL pointer to the last element */
 
167
  (*array)[*len] = NULL;
 
168
  return true;
 
169
}
 
170
 
 
171
/* Add to a plugin's argument vector */
 
172
static bool add_argument(plugin *p, const char *arg){
 
173
  if(p == NULL){
 
174
    return false;
 
175
  }
 
176
  return add_to_char_array(arg, &(p->argv), &(p->argc));
 
177
}
 
178
 
 
179
/* Add to a plugin's environment */
 
180
static bool add_environment(plugin *p, const char *def, bool replace){
 
181
  if(p == NULL){
 
182
    return false;
 
183
  }
 
184
  /* namelen = length of name of environment variable */
 
185
  size_t namelen = (size_t)(strchrnul(def, '=') - def);
 
186
  /* Search for this environment variable */
 
187
  for(char **e = p->environ; *e != NULL; e++){
 
188
    if(strncmp(*e, def, namelen + 1) == 0){
 
189
      /* It already exists */
 
190
      if(replace){
 
191
        char *new = realloc(*e, strlen(def) + 1);
 
192
        if(new == NULL){
 
193
          return false;
 
194
        }
 
195
        *e = new;
 
196
        strcpy(*e, def);
 
197
      }
 
198
      return true;
 
199
    }
 
200
  }
 
201
  return add_to_char_array(def, &(p->environ), &(p->envc));
133
202
}
134
203
 
135
204
/*
137
206
 * Descriptor Flags".
138
207
 * *Note File Descriptor Flags:(libc)Descriptor Flags.
139
208
 */
140
 
static int set_cloexec_flag(int fd)
141
 
{
 
209
static int set_cloexec_flag(int fd){
142
210
  int ret = fcntl(fd, F_GETFD, 0);
143
211
  /* If reading the flags failed, return error indication now. */
144
212
  if(ret < 0){
148
216
  return fcntl(fd, F_SETFD, ret | FD_CLOEXEC);
149
217
}
150
218
 
151
 
process *process_list = NULL;
152
219
 
153
 
/* Mark a process as completed when it exits, and save its exit
 
220
/* Mark processes as completed when they exit, and save their exit
154
221
   status. */
155
 
void handle_sigchld(__attribute__((unused)) int sig){
156
 
  process *proc = process_list;
157
 
  int status;
158
 
  pid_t pid = wait(&status);
159
 
  if(pid == -1){
160
 
    perror("wait");
161
 
    return;
162
 
  }
163
 
  while(proc != NULL and proc->pid != pid){
164
 
    proc = proc->next;
165
 
  }
166
 
  if(proc == NULL){
167
 
    /* Process not found in process list */
168
 
    return;
169
 
  }
170
 
  proc->status = status;
171
 
  proc->completed = true;
 
222
static void handle_sigchld(__attribute__((unused)) int sig){
 
223
  while(true){
 
224
    plugin *proc = plugin_list;
 
225
    int status;
 
226
    pid_t pid = waitpid(-1, &status, WNOHANG);
 
227
    if(pid == 0){
 
228
      /* Only still running child processes */
 
229
      break;
 
230
    }
 
231
    if(pid == -1){
 
232
      if (errno != ECHILD){
 
233
        perror("waitpid");
 
234
      }
 
235
      /* No child processes */
 
236
      break;
 
237
    }
 
238
    
 
239
    /* A child exited, find it in process_list */
 
240
    while(proc != NULL and proc->pid != pid){
 
241
      proc = proc->next;
 
242
    }
 
243
    if(proc == NULL){
 
244
      /* Process not found in process list */
 
245
      continue;
 
246
    }
 
247
    proc->status = status;
 
248
    proc->completed = true;
 
249
  }
172
250
}
173
251
 
174
 
bool print_out_password(const char *buffer, size_t length){
 
252
/* Prints out a password to stdout */
 
253
static bool print_out_password(const char *buffer, size_t length){
175
254
  ssize_t ret;
176
 
  if(length>0 and buffer[length-1] == '\n'){
177
 
    length--;
178
 
  }
179
255
  for(size_t written = 0; written < length; written += (size_t)ret){
180
256
    ret = TEMP_FAILURE_RETRY(write(STDOUT_FILENO, buffer + written,
181
257
                                   length - written));
186
262
  return true;
187
263
}
188
264
 
189
 
char ** addcustomargument(char **argv, int *argc, char *arg){
 
265
/* Removes and free a plugin from the plugin list */
 
266
static void free_plugin(plugin *plugin_node){
 
267
  
 
268
  for(char **arg = plugin_node->argv; *arg != NULL; arg++){
 
269
    free(*arg);
 
270
  }
 
271
  free(plugin_node->argv);
 
272
  for(char **env = plugin_node->environ; *env != NULL; env++){
 
273
    free(*env);
 
274
  }
 
275
  free(plugin_node->environ);
 
276
  free(plugin_node->buffer);
190
277
 
191
 
  if (argv == NULL){
192
 
    *argc = 1;
193
 
    argv = malloc(sizeof(char*) * 2);
194
 
    if(argv == NULL){
195
 
      return NULL;
 
278
  /* Removes the plugin from the singly-linked list */
 
279
  if(plugin_node == plugin_list){
 
280
    /* First one - simple */
 
281
    plugin_list = plugin_list->next;
 
282
  } else {
 
283
    /* Second one or later */
 
284
    for(plugin *p = plugin_list; p != NULL; p = p->next){
 
285
      if(p->next == plugin_node){
 
286
        p->next = plugin_node->next;
 
287
        break;
 
288
      }
196
289
    }
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;
 
290
  }
 
291
  
 
292
  free(plugin_node);
 
293
}
 
294
 
 
295
static void free_plugin_list(void){
 
296
  while(plugin_list != NULL){
 
297
    free_plugin(plugin_list);
 
298
  }
209
299
}
210
300
 
211
301
int main(int argc, char *argv[]){
212
 
  const char *plugindir = "/conf/conf.d/mandos/plugins.d";
213
 
  const char *conffile = CONFFILE;
 
302
  char *plugindir = NULL;
 
303
  char *argfile = NULL;
214
304
  FILE *conffp;
215
305
  size_t d_name_len;
216
306
  DIR *dir = NULL;
231
321
  /* Establish a signal handler */
232
322
  sigemptyset(&sigchld_action.sa_mask);
233
323
  ret = sigaddset(&sigchld_action.sa_mask, SIGCHLD);
234
 
  if(ret < 0){
 
324
  if(ret == -1){
235
325
    perror("sigaddset");
236
 
    exit(EXIT_FAILURE);
 
326
    exitstatus = EXIT_FAILURE;
 
327
    goto fallback;
237
328
  }
238
329
  ret = sigaction(SIGCHLD, &sigchld_action, &old_sigchld_action);
239
 
  if(ret < 0){
 
330
  if(ret == -1){
240
331
    perror("sigaction");
241
 
    exit(EXIT_FAILURE);
 
332
    exitstatus = EXIT_FAILURE;
 
333
    goto fallback;
242
334
  }
243
335
  
244
336
  /* The options we understand. */
246
338
    { .name = "global-options", .key = 'g',
247
339
      .arg = "OPTION[,OPTION[,...]]",
248
340
      .doc = "Options passed to all plugins" },
 
341
    { .name = "global-env", .key = 'G',
 
342
      .arg = "VAR=value",
 
343
      .doc = "Environment variable passed to all plugins" },
249
344
    { .name = "options-for", .key = 'o',
250
345
      .arg = "PLUGIN:OPTION[,OPTION[,...]]",
251
346
      .doc = "Options passed only to specified plugin" },
 
347
    { .name = "env-for", .key = 'E',
 
348
      .arg = "PLUGIN:ENV=value",
 
349
      .doc = "Environment variable passed to specified plugin" },
252
350
    { .name = "disable", .key = 'd',
253
351
      .arg = "PLUGIN",
254
352
      .doc = "Disable a specific plugin", .group = 1 },
 
353
    { .name = "enable", .key = 'e',
 
354
      .arg = "PLUGIN",
 
355
      .doc = "Enable a specific plugin", .group = 1 },
255
356
    { .name = "plugin-dir", .key = 128,
256
357
      .arg = "DIRECTORY",
257
358
      .doc = "Specify a different plugin directory", .group = 2 },
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,
265
 
      .doc = "Debug mode", .group = 3 },
 
359
    { .name = "config-file", .key = 129,
 
360
      .arg = "FILE",
 
361
      .doc = "Specify a different configuration file", .group = 2 },
 
362
    { .name = "userid", .key = 130,
 
363
      .arg = "ID", .flags = 0,
 
364
      .doc = "User ID the plugins will run as", .group = 3 },
 
365
    { .name = "groupid", .key = 131,
 
366
      .arg = "ID", .flags = 0,
 
367
      .doc = "Group ID the plugins will run as", .group = 3 },
 
368
    { .name = "debug", .key = 132,
 
369
      .doc = "Debug mode", .group = 4 },
266
370
    { .name = NULL }
267
371
  };
268
372
  
269
 
  error_t parse_opt (int key, char *arg, struct argp_state *state) {
270
 
    /* Get the INPUT argument from `argp_parse', which we know is a
271
 
       pointer to our plugin list pointer. */
272
 
    plugin **plugins = state->input;
 
373
  error_t parse_opt (int key, char *arg, __attribute__((unused))
 
374
                     struct argp_state *state) {
273
375
    switch (key) {
274
 
    case 'g':
 
376
    case 'g':                   /* --global-options */
275
377
      if (arg != NULL){
276
378
        char *p;
277
379
        while((p = strsep(&arg, ",")) != NULL){
278
380
          if(p[0] == '\0'){
279
381
            continue;
280
382
          }
281
 
          addargument(getplugin(NULL, plugins), p);
 
383
          if(not add_argument(getplugin(NULL), p)){
 
384
            perror("add_argument");
 
385
            return ARGP_ERR_UNKNOWN;
 
386
          }
282
387
        }
283
388
      }
284
389
      break;
285
 
    case 'o':
 
390
    case 'G':                   /* --global-env */
 
391
      if(arg == NULL){
 
392
        break;
 
393
      }
 
394
      if(not add_environment(getplugin(NULL), arg, true)){
 
395
        perror("add_environment");
 
396
      }
 
397
      break;
 
398
    case 'o':                   /* --options-for */
286
399
      if (arg != NULL){
287
 
        char *name = strsep(&arg, ":");
288
 
        if(name[0] == '\0'){
 
400
        char *p_name = strsep(&arg, ":");
 
401
        if(p_name[0] == '\0' or arg == NULL){
289
402
          break;
290
403
        }
291
404
        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
 
            }
301
 
            addargument(getplugin(name, plugins), p);
302
 
          }
303
 
        }
304
 
      }
305
 
      break;
306
 
    case 'd':
307
 
      if (arg != NULL){
308
 
        getplugin(arg, plugins)->disabled = true;
309
 
      }
310
 
      break;
311
 
    case 128:
312
 
      plugindir = arg;
313
 
      break;
314
 
    case 129:
 
405
        if(opt[0] == '\0' or opt == NULL){
 
406
          break;
 
407
        }
 
408
        char *p;
 
409
        while((p = strsep(&opt, ",")) != NULL){
 
410
          if(p[0] == '\0'){
 
411
            continue;
 
412
          }
 
413
          if(not add_argument(getplugin(p_name), p)){
 
414
            perror("add_argument");
 
415
            return ARGP_ERR_UNKNOWN;
 
416
          }
 
417
        }
 
418
      }
 
419
      break;
 
420
    case 'E':                   /* --env-for */
 
421
      if(arg == NULL){
 
422
        break;
 
423
      }
 
424
      {
 
425
        char *envdef = strchr(arg, ':');
 
426
        if(envdef == NULL){
 
427
          break;
 
428
        }
 
429
        *envdef = '\0';
 
430
        if(not add_environment(getplugin(arg), envdef+1, true)){
 
431
          perror("add_environment");
 
432
        }
 
433
      }
 
434
      break;
 
435
    case 'd':                   /* --disable */
 
436
      if (arg != NULL){
 
437
        plugin *p = getplugin(arg);
 
438
        if(p == NULL){
 
439
          return ARGP_ERR_UNKNOWN;
 
440
        }
 
441
        p->disabled = true;
 
442
      }
 
443
      break;
 
444
    case 'e':                   /* --enable */
 
445
      if (arg != NULL){
 
446
        plugin *p = getplugin(arg);
 
447
        if(p == NULL){
 
448
          return ARGP_ERR_UNKNOWN;
 
449
        }
 
450
        p->disabled = false;
 
451
      }
 
452
      break;
 
453
    case 128:                   /* --plugin-dir */
 
454
      free(plugindir);
 
455
      plugindir = strdup(arg);
 
456
      if(plugindir == NULL){
 
457
        perror("strdup");
 
458
      }      
 
459
      break;
 
460
    case 129:                   /* --config-file */
 
461
      /* This is already done by parse_opt_config_file() */
 
462
      break;
 
463
    case 130:                   /* --userid */
315
464
      uid = (uid_t)strtol(arg, NULL, 10);
316
465
      break;
317
 
    case 130:
 
466
    case 131:                   /* --groupid */
318
467
      gid = (gid_t)strtol(arg, NULL, 10);
319
468
      break;
320
 
    case 131:
 
469
    case 132:                   /* --debug */
321
470
      debug = true;
322
471
      break;
323
472
    case ARGP_KEY_ARG:
324
 
      fprintf(stderr, "Ignoring unknown argument \"%s\"\n", arg); 
325
 
      break;
326
 
    case ARGP_KEY_END:
327
 
      break;
328
 
    default:
329
 
      return ARGP_ERR_UNKNOWN;
330
 
    }
331
 
    return 0;
332
 
  }
333
 
  
334
 
  plugin *plugin_list = NULL;
335
 
  
336
 
  struct argp argp = { .options = options, .parser = parse_opt,
337
 
                       .args_doc = "[+PLUS_SEPARATED_OPTIONS]",
 
473
      /* Cryptsetup always passes an argument, which is an empty
 
474
         string if "none" was specified in /etc/crypttab.  So if
 
475
         argument was empty, we ignore it silently. */
 
476
      if(arg[0] != '\0'){
 
477
        fprintf(stderr, "Ignoring unknown argument \"%s\"\n", arg);
 
478
      }
 
479
      break;
 
480
    case ARGP_KEY_END:
 
481
      break;
 
482
    default:
 
483
      return ARGP_ERR_UNKNOWN;
 
484
    }
 
485
    return 0;
 
486
  }
 
487
  
 
488
  /* This option parser is the same as parse_opt() above, except it
 
489
     ignores everything but the --config-file option. */
 
490
  error_t parse_opt_config_file (int key, char *arg,
 
491
                                 __attribute__((unused))
 
492
                                 struct argp_state *state) {
 
493
    switch (key) {
 
494
    case 'g':                   /* --global-options */
 
495
    case 'G':                   /* --global-env */
 
496
    case 'o':                   /* --options-for */
 
497
    case 'E':                   /* --env-for */
 
498
    case 'd':                   /* --disable */
 
499
    case 'e':                   /* --enable */
 
500
    case 128:                   /* --plugin-dir */
 
501
      break;
 
502
    case 129:                   /* --config-file */
 
503
      free(argfile);
 
504
      argfile = strdup(arg);
 
505
      if(argfile == NULL){
 
506
        perror("strdup");
 
507
      }
 
508
      break;      
 
509
    case 130:                   /* --userid */
 
510
    case 131:                   /* --groupid */
 
511
    case 132:                   /* --debug */
 
512
    case ARGP_KEY_ARG:
 
513
    case ARGP_KEY_END:
 
514
      break;
 
515
    default:
 
516
      return ARGP_ERR_UNKNOWN;
 
517
    }
 
518
    return 0;
 
519
  }
 
520
  
 
521
  struct argp argp = { .options = options,
 
522
                       .parser = parse_opt_config_file,
 
523
                       .args_doc = "",
338
524
                       .doc = "Mandos plugin runner -- Run plugins" };
339
525
  
340
 
  ret = argp_parse (&argp, argc, argv, 0, 0, &plugin_list);
 
526
  /* Parse using the parse_opt_config_file in order to get the custom
 
527
     config file location, if any. */
 
528
  ret = argp_parse (&argp, argc, argv, ARGP_IN_ORDER, 0, NULL);
341
529
  if (ret == ARGP_ERR_UNKNOWN){
342
530
    fprintf(stderr, "Unknown error while parsing arguments\n");
343
531
    exitstatus = EXIT_FAILURE;
344
 
    goto end;
 
532
    goto fallback;
345
533
  }
346
 
 
347
 
  conffp = fopen(conffile, "r");
 
534
  
 
535
  /* Reset to the normal argument parser */
 
536
  argp.parser = parse_opt;
 
537
  
 
538
  /* Open the configfile if available */
 
539
  if (argfile == NULL){
 
540
    conffp = fopen(AFILE, "r");
 
541
  } else {
 
542
    conffp = fopen(argfile, "r");
 
543
  }  
348
544
  if(conffp != NULL){
349
545
    char *org_line = NULL;
 
546
    char *p, *arg, *new_arg, *line;
350
547
    size_t size = 0;
351
548
    ssize_t sret;
352
 
    char *p, *arg, *new_arg, *line;
353
549
    const char whitespace_delims[] = " \r\t\f\v\n";
354
550
    const char comment_delim[] = "#";
355
551
 
 
552
    custom_argc = 1;
 
553
    custom_argv = malloc(sizeof(char*) * 2);
 
554
    if(custom_argv == NULL){
 
555
      perror("malloc");
 
556
      exitstatus = EXIT_FAILURE;
 
557
      goto fallback;
 
558
    }
 
559
    custom_argv[0] = argv[0];
 
560
    custom_argv[1] = NULL;
 
561
 
 
562
    /* for each line in the config file, strip whitespace and ignore
 
563
       commented text */
356
564
    while(true){
357
565
      sret = getline(&org_line, &size, conffp);
358
566
      if(sret == -1){
366
574
          continue;
367
575
        }
368
576
        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
 
        }
 
577
        if(new_arg == NULL){
 
578
          perror("strdup");
 
579
          exitstatus = EXIT_FAILURE;
 
580
          free(org_line);
 
581
          goto fallback;
 
582
        }
 
583
        
 
584
        custom_argc += 1;
 
585
        custom_argv = realloc(custom_argv, sizeof(char *)
 
586
                              * ((unsigned int) custom_argc + 1));
 
587
        if(custom_argv == NULL){
 
588
          perror("realloc");
 
589
          exitstatus = EXIT_FAILURE;
 
590
          free(org_line);
 
591
          goto fallback;
 
592
        }
 
593
        custom_argv[custom_argc-1] = new_arg;
 
594
        custom_argv[custom_argc] = NULL;        
375
595
      }
376
596
    }
377
597
    free(org_line);
378
 
  } else{
379
 
    /* check for harmfull errors */
 
598
  } else {
 
599
    /* Check for harmful errors and go to fallback. Other errors might
 
600
       not affect opening plugins */
380
601
    if (errno == EMFILE or errno == ENFILE or errno == ENOMEM){
381
602
      perror("fopen");
382
603
      exitstatus = EXIT_FAILURE;
383
 
      goto end;
 
604
      goto fallback;
384
605
    }
385
606
  }
386
 
 
 
607
  /* If there was any arguments from configuration file,
 
608
     pass them to parser as command arguments */
387
609
  if(custom_argv != NULL){
388
 
    custom_argv[0] = argv[0];
389
 
    ret = argp_parse (&argp, custom_argc, custom_argv, 0, 0, &plugin_list);
 
610
    ret = argp_parse (&argp, custom_argc, custom_argv, ARGP_IN_ORDER,
 
611
                      0, NULL);
390
612
    if (ret == ARGP_ERR_UNKNOWN){
391
613
      fprintf(stderr, "Unknown error while parsing arguments\n");
392
614
      exitstatus = EXIT_FAILURE;
393
 
      goto end;
 
615
      goto fallback;
394
616
    }
395
617
  }
396
618
  
 
619
  /* Parse actual command line arguments, to let them override the
 
620
     config file */
 
621
  ret = argp_parse (&argp, argc, argv, ARGP_IN_ORDER, 0, NULL);
 
622
  if (ret == ARGP_ERR_UNKNOWN){
 
623
    fprintf(stderr, "Unknown error while parsing arguments\n");
 
624
    exitstatus = EXIT_FAILURE;
 
625
    goto fallback;
 
626
  }
 
627
  
397
628
  if(debug){
398
629
    for(plugin *p = plugin_list; p != NULL; p=p->next){
399
630
      fprintf(stderr, "Plugin: %s has %d arguments\n",
401
632
      for(char **a = p->argv; *a != NULL; a++){
402
633
        fprintf(stderr, "\tArg: %s\n", *a);
403
634
      }
 
635
      fprintf(stderr, "...and %u environment variables\n", p->envc);
 
636
      for(char **a = p->environ; *a != NULL; a++){
 
637
        fprintf(stderr, "\t%s\n", *a);
 
638
      }
404
639
    }
405
640
  }
406
641
  
 
642
  /* Strip permissions down to nobody */
407
643
  ret = setuid(uid);
408
644
  if (ret == -1){
409
645
    perror("setuid");
410
 
  }
411
 
  
 
646
  }  
412
647
  setgid(gid);
413
648
  if (ret == -1){
414
649
    perror("setgid");
415
650
  }
416
651
  
417
 
  dir = opendir(plugindir);
 
652
  if (plugindir == NULL){
 
653
    dir = opendir(PDIR);
 
654
  } else {
 
655
    dir = opendir(plugindir);
 
656
  }
 
657
  
418
658
  if(dir == NULL){
419
659
    perror("Could not open plugin dir");
420
660
    exitstatus = EXIT_FAILURE;
421
 
    goto end;
 
661
    goto fallback;
422
662
  }
423
663
  
424
664
  /* Set the FD_CLOEXEC flag on the directory, if possible */
429
669
      if(ret < 0){
430
670
        perror("set_cloexec_flag");
431
671
        exitstatus = EXIT_FAILURE;
432
 
        goto end;
 
672
        goto fallback;
433
673
      }
434
674
    }
435
675
  }
436
676
  
437
677
  FD_ZERO(&rfds_all);
438
678
  
 
679
  /* Read and execute any executable in the plugin directory*/
439
680
  while(true){
440
681
    dirst = readdir(dir);
441
682
    
442
 
    // All directory entries have been processed
 
683
    /* All directory entries have been processed */
443
684
    if(dirst == NULL){
444
685
      if (errno == EBADF){
445
686
        perror("readdir");
446
687
        exitstatus = EXIT_FAILURE;
447
 
        goto end;
 
688
        goto fallback;
448
689
      }
449
690
      break;
450
691
    }
451
692
    
452
693
    d_name_len = strlen(dirst->d_name);
453
694
    
454
 
    // Ignore dotfiles, backup files and other junk
 
695
    /* Ignore dotfiles, backup files and other junk */
455
696
    {
456
697
      bool bad_name = false;
457
698
      
459
700
      
460
701
      const char const *bad_suffixes[] = { "~", "#", ".dpkg-new",
461
702
                                           ".dpkg-old",
 
703
                                           ".dpkg-bak",
462
704
                                           ".dpkg-divert", NULL };
463
705
      for(const char **pre = bad_prefixes; *pre != NULL; pre++){
464
706
        size_t pre_len = strlen(*pre);
472
714
          break;
473
715
        }
474
716
      }
475
 
      
476
717
      if(bad_name){
477
718
        continue;
478
719
      }
479
 
      
480
720
      for(const char **suf = bad_suffixes; *suf != NULL; suf++){
481
721
        size_t suf_len = strlen(*suf);
482
722
        if((d_name_len >= suf_len)
495
735
        continue;
496
736
      }
497
737
    }
498
 
    
499
 
    char *filename = malloc(d_name_len + strlen(plugindir) + 2);
500
 
    if (filename == NULL){
501
 
      perror("malloc");
 
738
 
 
739
    char *filename;
 
740
    if(plugindir == NULL){
 
741
      ret = asprintf(&filename, PDIR "/%s", dirst->d_name);
 
742
    } else {
 
743
      ret = asprintf(&filename, "%s/%s", plugindir, dirst->d_name);
 
744
    }
 
745
    if(ret < 0){
 
746
      perror("asprintf");
502
747
      continue;
503
748
    }
504
 
    strcpy(filename, plugindir); /* Spurious warning */
505
 
    strcat(filename, "/");      /* Spurious warning */
506
 
    strcat(filename, dirst->d_name); /* Spurious warning */
507
749
    
508
750
    ret = stat(filename, &st);
509
751
    if (ret == -1){
511
753
      free(filename);
512
754
      continue;
513
755
    }
514
 
    
 
756
 
 
757
    /* Ignore non-executable files */
515
758
    if (not S_ISREG(st.st_mode) or (access(filename, X_OK) != 0)){
516
759
      if(debug){
517
760
        fprintf(stderr, "Ignoring plugin dir entry \"%s\""
520
763
      free(filename);
521
764
      continue;
522
765
    }
523
 
    if(getplugin(dirst->d_name, &plugin_list)->disabled){
 
766
    
 
767
    plugin *p = getplugin(dirst->d_name);
 
768
    if(p == NULL){
 
769
      perror("getplugin");
 
770
      free(filename);
 
771
      continue;
 
772
    }
 
773
    if(p->disabled){
524
774
      if(debug){
525
775
        fprintf(stderr, "Ignoring disabled plugin \"%s\"\n",
526
776
                dirst->d_name);
528
778
      free(filename);
529
779
      continue;
530
780
    }
531
 
    plugin *p = getplugin(dirst->d_name, &plugin_list);
532
781
    {
533
782
      /* Add global arguments to argument list for this plugin */
534
 
      plugin *g = getplugin(NULL, &plugin_list);
535
 
      for(char **a = g->argv + 1; *a != NULL; a++){
536
 
        addargument(p, *a);
537
 
      }
538
 
    }
539
 
    int pipefd[2]; 
 
783
      plugin *g = getplugin(NULL);
 
784
      if(g != NULL){
 
785
        for(char **a = g->argv + 1; *a != NULL; a++){
 
786
          if(not add_argument(p, *a)){
 
787
            perror("add_argument");
 
788
          }
 
789
        }
 
790
        /* Add global environment variables */
 
791
        for(char **e = g->environ; *e != NULL; e++){
 
792
          if(not add_environment(p, *e, false)){
 
793
            perror("add_environment");
 
794
          }
 
795
        }
 
796
      }
 
797
    }
 
798
    /* If this plugin has any environment variables, we will call
 
799
       using execve and need to duplicate the environment from this
 
800
       process, too. */
 
801
    if(p->environ[0] != NULL){
 
802
      for(char **e = environ; *e != NULL; e++){
 
803
        if(not add_environment(p, *e, false)){
 
804
          perror("add_environment");
 
805
        }
 
806
      }
 
807
    }
 
808
    
 
809
    int pipefd[2];
540
810
    ret = pipe(pipefd);
541
811
    if (ret == -1){
542
812
      perror("pipe");
543
813
      exitstatus = EXIT_FAILURE;
544
 
      goto end;
 
814
      goto fallback;
545
815
    }
 
816
    /* Ask OS to automatic close the pipe on exec */
546
817
    ret = set_cloexec_flag(pipefd[0]);
547
818
    if(ret < 0){
548
819
      perror("set_cloexec_flag");
549
820
      exitstatus = EXIT_FAILURE;
550
 
      goto end;
 
821
      goto fallback;
551
822
    }
552
823
    ret = set_cloexec_flag(pipefd[1]);
553
824
    if(ret < 0){
554
825
      perror("set_cloexec_flag");
555
826
      exitstatus = EXIT_FAILURE;
556
 
      goto end;
 
827
      goto fallback;
557
828
    }
558
829
    /* Block SIGCHLD until process is safely in process list */
559
830
    ret = sigprocmask (SIG_BLOCK, &sigchld_action.sa_mask, NULL);
560
831
    if(ret < 0){
561
832
      perror("sigprocmask");
562
833
      exitstatus = EXIT_FAILURE;
563
 
      goto end;
 
834
      goto fallback;
564
835
    }
565
 
    // Starting a new process to be watched
 
836
    /* Starting a new process to be watched */
566
837
    pid_t pid = fork();
567
838
    if(pid == -1){
568
839
      perror("fork");
569
840
      exitstatus = EXIT_FAILURE;
570
 
      goto end;
 
841
      goto fallback;
571
842
    }
572
843
    if(pid == 0){
573
844
      /* this is the child process */
576
847
        perror("sigaction");
577
848
        _exit(EXIT_FAILURE);
578
849
      }
579
 
      ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
 
850
      ret = sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
580
851
      if(ret < 0){
581
852
        perror("sigprocmask");
582
853
        _exit(EXIT_FAILURE);
583
854
      }
584
 
 
 
855
      
585
856
      ret = dup2(pipefd[1], STDOUT_FILENO); /* replace our stdout */
586
857
      if(ret == -1){
587
858
        perror("dup2");
593
864
           above and must now close it manually here. */
594
865
        closedir(dir);
595
866
      }
596
 
      if(execv(filename, p->argv) < 0){
597
 
        perror("execv");
598
 
        _exit(EXIT_FAILURE);
 
867
      if(p->environ[0] == NULL){
 
868
        if(execv(filename, p->argv) < 0){
 
869
          perror("execv");
 
870
          _exit(EXIT_FAILURE);
 
871
        }
 
872
      } else {
 
873
        if(execve(filename, p->argv, p->environ) < 0){
 
874
          perror("execve");
 
875
          _exit(EXIT_FAILURE);
 
876
        }
599
877
      }
600
878
      /* no return */
601
879
    }
602
 
    /* parent process */
 
880
    /* Parent process */
 
881
    close(pipefd[1]);           /* Close unused write end of pipe */
603
882
    free(filename);
604
 
    close(pipefd[1]);           /* close unused write end of pipe */
605
 
    process *new_process = malloc(sizeof(process));
606
 
    if (new_process == NULL){
607
 
      perror("malloc");
 
883
    plugin *new_plugin = getplugin(dirst->d_name);
 
884
    if (new_plugin == NULL){
 
885
      perror("getplugin");
608
886
      ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
609
887
      if(ret < 0){
610
 
        perror("sigprocmask");
 
888
        perror("sigprocmask");
611
889
      }
612
890
      exitstatus = EXIT_FAILURE;
613
 
      goto end;
 
891
      goto fallback;
614
892
    }
615
893
    
616
 
    *new_process = (struct process){ .pid = pid,
617
 
                                     .fd = pipefd[0],
618
 
                                     .next = process_list };
619
 
    // List handling
620
 
    process_list = new_process;
 
894
    new_plugin->pid = pid;
 
895
    new_plugin->fd = pipefd[0];
 
896
    
621
897
    /* Unblock SIGCHLD so signal handler can be run if this process
622
898
       has already completed */
623
899
    ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
624
900
    if(ret < 0){
625
901
      perror("sigprocmask");
626
902
      exitstatus = EXIT_FAILURE;
627
 
      goto end;
628
 
    }
629
 
    
630
 
    FD_SET(new_process->fd, &rfds_all);
631
 
    
632
 
    if (maxfd < new_process->fd){
633
 
      maxfd = new_process->fd;
634
 
    }
635
 
    
636
 
  }
637
 
  
638
 
  /* Free the plugin list */
639
 
  for(plugin *next; plugin_list != NULL; plugin_list = next){
640
 
    next = plugin_list->next;
641
 
    free(plugin_list->argv);
642
 
    free(plugin_list);
 
903
      goto fallback;
 
904
    }
 
905
    
 
906
    FD_SET(new_plugin->fd, &rfds_all);
 
907
    
 
908
    if (maxfd < new_plugin->fd){
 
909
      maxfd = new_plugin->fd;
 
910
    }
643
911
  }
644
912
  
645
913
  closedir(dir);
646
914
  dir = NULL;
647
 
    
648
 
  if (process_list == NULL){
649
 
    fprintf(stderr, "No plugin processes started. Incorrect plugin"
650
 
            " directory?\n");
651
 
    process_list = NULL;
 
915
  
 
916
  for(plugin *p = plugin_list; p != NULL; p = p->next){
 
917
    if(p->pid != 0){
 
918
      break;
 
919
    }
 
920
    if(p->next == NULL){
 
921
      fprintf(stderr, "No plugin processes started. Incorrect plugin"
 
922
              " directory?\n");
 
923
      free_plugin_list();
 
924
    }
652
925
  }
653
 
  while(process_list){
 
926
  
 
927
  /* Main loop while running plugins exist */
 
928
  while(plugin_list){
654
929
    fd_set rfds = rfds_all;
655
930
    int select_ret = select(maxfd+1, &rfds, NULL, NULL, NULL);
656
931
    if (select_ret == -1){
657
932
      perror("select");
658
933
      exitstatus = EXIT_FAILURE;
659
 
      goto end;
 
934
      goto fallback;
660
935
    }
661
936
    /* OK, now either a process completed, or something can be read
662
937
       from one of them */
663
 
    for(process *proc = process_list; proc ; proc = proc->next){
 
938
    for(plugin *proc = plugin_list; proc != NULL;){
664
939
      /* Is this process completely done? */
665
940
      if(proc->eof and proc->completed){
666
941
        /* Only accept the plugin output if it exited cleanly */
667
942
        if(not WIFEXITED(proc->status)
668
943
           or WEXITSTATUS(proc->status) != 0){
669
944
          /* Bad exit by plugin */
 
945
 
670
946
          if(debug){
671
947
            if(WIFEXITED(proc->status)){
672
948
              fprintf(stderr, "Plugin %u exited with status %d\n",
681
957
                      (unsigned int) (proc->pid));
682
958
            }
683
959
          }
 
960
          
684
961
          /* Remove the plugin */
685
962
          FD_CLR(proc->fd, &rfds_all);
 
963
 
686
964
          /* Block signal while modifying process_list */
687
 
          ret = sigprocmask (SIG_BLOCK, &sigchld_action.sa_mask, NULL);
 
965
          ret = sigprocmask(SIG_BLOCK, &sigchld_action.sa_mask, NULL);
688
966
          if(ret < 0){
689
967
            perror("sigprocmask");
690
968
            exitstatus = EXIT_FAILURE;
691
 
            goto end;
692
 
          }
693
 
          /* Delete this process entry from the list */
694
 
          if(process_list == proc){
695
 
            /* First one - simple */
696
 
            process_list = proc->next;
697
 
          } else {
698
 
            /* Second one or later */
699
 
            for(process *p = process_list; p != NULL; p = p->next){
700
 
              if(p->next == proc){
701
 
                p->next = proc->next;
702
 
                break;
703
 
              }
704
 
            }
705
 
          }
 
969
            goto fallback;
 
970
          }
 
971
          
 
972
          plugin *next_plugin = proc->next;
 
973
          free_plugin(proc);
 
974
          proc = next_plugin;
 
975
          
706
976
          /* We are done modifying process list, so unblock signal */
707
977
          ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask,
708
978
                             NULL);
709
979
          if(ret < 0){
710
980
            perror("sigprocmask");
711
 
          }
712
 
          free(proc->buffer);
713
 
          free(proc);
714
 
          /* We deleted this process from the list, so we can't go
715
 
             proc->next.  Therefore, start over from the beginning of
716
 
             the process list */
717
 
          break;
 
981
            exitstatus = EXIT_FAILURE;
 
982
            goto fallback;
 
983
          }
 
984
          
 
985
          if(plugin_list == NULL){
 
986
            break;
 
987
          }
 
988
          
 
989
          continue;
718
990
        }
 
991
        
719
992
        /* This process exited nicely, so print its buffer */
720
 
 
721
 
        bool bret = print_out_password(proc->buffer, proc->buffer_length);
 
993
        
 
994
        bool bret = print_out_password(proc->buffer,
 
995
                                       proc->buffer_length);
722
996
        if(not bret){
723
997
          perror("print_out_password");
724
998
          exitstatus = EXIT_FAILURE;
725
999
        }
726
 
        goto end;
 
1000
        goto fallback;
727
1001
      }
 
1002
      
728
1003
      /* This process has not completed.  Does it have any output? */
729
1004
      if(proc->eof or not FD_ISSET(proc->fd, &rfds)){
730
1005
        /* This process had nothing to say at this time */
 
1006
        proc = proc->next;
731
1007
        continue;
732
1008
      }
733
1009
      /* Before reading, make the process' data buffer large enough */
737
1013
        if (proc->buffer == NULL){
738
1014
          perror("malloc");
739
1015
          exitstatus = EXIT_FAILURE;
740
 
          goto end;
 
1016
          goto fallback;
741
1017
        }
742
1018
        proc->buffer_size += BUFFER_SIZE;
743
1019
      }
746
1022
                 BUFFER_SIZE);
747
1023
      if(ret < 0){
748
1024
        /* Read error from this process; ignore the error */
 
1025
        proc = proc->next;
749
1026
        continue;
750
1027
      }
751
1028
      if(ret == 0){
758
1035
  }
759
1036
 
760
1037
 
761
 
 end:
 
1038
 fallback:
762
1039
  
763
 
  if(process_list == NULL or exitstatus != EXIT_SUCCESS){
764
 
    /* Fallback if all plugins failed, none are found or an error occured */
 
1040
  if(plugin_list == NULL or exitstatus != EXIT_SUCCESS){
 
1041
    /* Fallback if all plugins failed, none are found or an error
 
1042
       occured */
765
1043
    bool bret;
766
1044
    fprintf(stderr, "Going to fallback mode using getpass(3)\n");
767
1045
    char *passwordbuffer = getpass("Password: ");
768
 
    bret = print_out_password(passwordbuffer, strlen(passwordbuffer));
 
1046
    size_t len = strlen(passwordbuffer);
 
1047
    /* Strip trailing newline */
 
1048
    if(len > 0 and passwordbuffer[len-1] == '\n'){
 
1049
      passwordbuffer[len-1] = '\0'; /* not strictly necessary */
 
1050
      len--;
 
1051
    }
 
1052
    bret = print_out_password(passwordbuffer, len);
769
1053
    if(not bret){
770
1054
      perror("print_out_password");
771
1055
      exitstatus = EXIT_FAILURE;
772
 
      goto end;
773
1056
    }
774
1057
  }
775
1058
  
776
1059
  /* Restore old signal handler */
777
 
  sigaction(SIGCHLD, &old_sigchld_action, NULL);
778
 
  
779
 
  free(custom_argv);
780
 
  
781
 
  /* Free the plugin list */
782
 
  for(plugin *next; plugin_list != NULL; plugin_list = next){
783
 
    next = plugin_list->next;
784
 
    free(plugin_list->argv);
785
 
    free(plugin_list);
 
1060
  ret = sigaction(SIGCHLD, &old_sigchld_action, NULL);
 
1061
  if(ret == -1){
 
1062
    perror("sigaction");
 
1063
    exitstatus = EXIT_FAILURE;
 
1064
  }
 
1065
  
 
1066
  if(custom_argv != NULL){
 
1067
    for(char **arg = custom_argv+1; *arg != NULL; arg++){
 
1068
      free(*arg);
 
1069
    }
 
1070
    free(custom_argv);
786
1071
  }
787
1072
  
788
1073
  if(dir != NULL){
789
1074
    closedir(dir);
790
1075
  }
791
1076
  
792
 
  /* Free the process list and kill the processes */
793
 
  for(process *next; process_list != NULL; process_list = next){
794
 
    next = process_list->next;
795
 
    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");
 
1077
  /* Kill the processes */
 
1078
  for(plugin *p = plugin_list; p != NULL; p = p->next){
 
1079
    if(p->pid != 0){
 
1080
      close(p->fd);
 
1081
      ret = kill(p->pid, SIGTERM);
 
1082
      if(ret == -1 and errno != ESRCH){
 
1083
        /* Set-uid proccesses might not get closed */
 
1084
        perror("kill");
 
1085
      }
800
1086
    }
801
 
    free(process_list->buffer);
802
 
    free(process_list);
803
1087
  }
804
1088
  
805
1089
  /* Wait for any remaining child processes to terminate */
810
1094
    perror("wait");
811
1095
  }
812
1096
  
 
1097
  free_plugin_list();
 
1098
  
 
1099
  free(plugindir);
 
1100
  free(argfile);
 
1101
  
813
1102
  return exitstatus;
814
1103
}