/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-16 16:58:31 UTC
  • Revision ID: teddy@fukt.bsnet.se-20080816165831-cx018vo5i9oqmgo0
* plugins.d/password-request.c (main): Include environment variables
                                       "cryptsource" and "crypttarget"
                                       in password prompt.

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