/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: 2009-09-05 17:39:34 UTC
  • Revision ID: teddy@fukt.bsnet.se-20090905173934-gs0r858omhudc71m
* plugin-runner.c (main): Move variables "tmpmax" and "tmp" into
                          the innermost scope possible.

Show diffs side-by-side

added added

removed removed

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