/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: Teddy Hogeborn
  • Date: 2008-09-01 08:29:23 UTC
  • Revision ID: teddy@fukt.bsnet.se-20080901082923-i2liq6t7warmu9xe
* mandos.xml: Enclose "RAM" with <acronym>.
* overview.xml: - '' -

* plugin-runner.xml (DESCRIPTION): Improved wording.
  (PURPOSE): New section.
  (OPTIONS): Improved wording.
  (OVERVIEW, PLUGINS): New section.
  (FALLBACK): New empty placeholder section.

* plugins.d/password-prompt.xml: Enclose "RAM" with <acronym>.

Show diffs side-by-side

added added

removed removed

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