/mandos/release

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

« back to all changes in this revision

Viewing changes to plugin-runner.c

  • Committer: Björn Påhlsson
  • Date: 2009-02-08 01:50:24 UTC
  • mto: (237.7.1 mandos) (24.1.154 mandos)
  • mto: This revision was merged to the branch mainline in revision 250.
  • Revision ID: belorn@braxen-20090208015024-jewaulcqtxvbopq6
Fixed a bug in fallback handling

Show diffs side-by-side

added added

removed removed

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