/mandos/trunk

To get this branch, use:
bzr branch http://bzr.recompile.se/loggerhead/mandos/trunk

« back to all changes in this revision

Viewing changes to plugin-runner.c

  • Committer: Teddy Hogeborn
  • Date: 2008-08-24 10:52:46 UTC
  • Revision ID: teddy@fukt.bsnet.se-20080824105246-4gu1o1q6sdjvqcyb
* mandos (fingerprint): Bug fix: Remove some temporary debugging code.

Show diffs side-by-side

added added

removed removed

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