/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-15 21:09:25 UTC
  • mfrom: (24.1.52 mandos)
  • Revision ID: teddy@fukt.bsnet.se-20080815210925-32718zu3nrlotix5
Merge.

* plugin-runner.c (ARGFILE): Renamed to "plugin-runner.conf".

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