/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

  • Committer: Teddy Hogeborn
  • Date: 2008-08-02 10:48:24 UTC
  • Revision ID: teddy@fukt.bsnet.se-20080802104824-fx0miwp9o4g9r31e
* plugbasedclient.c (struct process): New fields "eof", "completed",
                                      and "status".
  (handle_sigchld): New function.
  (main): Initialize "dir" to NULL to only closedir() it if necessary.
          Move "process_list" to be a global variable to be accessible
          by "handle_sigchld".  Make "handle_sigchld" handle SIGCHLD.
          Remove redundant check for NULL "dir".  Free "filename" when
          no longer used.  Block SIGCHLD around fork()/exec().
          Restore normal signals in child.  Only loop while running
          processes exist.  Print process buffer when the process is
          done and it has emitted EOF, not when it only emits EOF.
          Remove processes from list which exit non-cleanly.  In
          cleaning up, closedir() if necessary.  Bug fix: set next
          pointer correctly when freeing process list.

* plugins.d/passprompt.c (main): Do not ignore SIGQUIT.

Show diffs side-by-side

added added

removed removed

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