/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-14 21:03:26 UTC
  • Revision ID: teddy@fukt.bsnet.se-20080814210326-u58d7rj78g3i0ed4
* plugins.d/password-request.c (init_gnutls_global): Renamed
                               "pubkeyfile" to "pubkeyfilename" and
                               "seckeyfile" to "seckeyfilename".
  (combinepath): Changed to use asprintf instead of malloc and memcpy.

  (main): Renamed "pubkeyfile" to "pubkeyname", renamed "seckeyfile"
          to "seckeyname" and created new non-const variables
          "pubkeyfilename" and "seckeyfilename".

* mandos (AvahiService.__init__): Bug fix: initialize
                                  "self.max_renames".
  (AvahiService.rename): Bug fix: use "self.name" instead of "name".

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