/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:
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(), getline() */
 
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,
65
65
#include <errno.h>              /* errno, EBADF */
66
66
 
67
67
#define BUFFER_SIZE 256
68
 
 
69
 
#define PDIR "/lib/mandos/plugins.d"
70
 
#define AFILE "/conf/conf.d/mandos/plugin-runner.conf"
 
68
#define ARGFILE "/conf/conf.d/mandos/plugin-runner.conf"
71
69
 
72
70
const char *argp_program_version = "plugin-runner 1.0";
73
71
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
74
72
 
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*/
 
73
struct process;
 
74
 
 
75
typedef struct process{
86
76
  pid_t pid;
87
77
  int fd;
88
78
  char *buffer;
89
79
  size_t buffer_size;
90
80
  size_t buffer_length;
91
81
  bool eof;
92
 
  volatile bool completed;
93
 
  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;
94
92
  struct plugin *next;
95
93
} plugin;
96
94
 
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){
 
95
static plugin *getplugin(char *name, plugin **plugin_list){
 
96
  for (plugin *p = *plugin_list; p != NULL; p = p->next){
104
97
    if ((p->name == name)
105
98
        or (p->name and name and (strcmp(p->name, name) == 0))){
106
99
      return p;
109
102
  /* Create a new plugin */
110
103
  plugin *new_plugin = malloc(sizeof(plugin));
111
104
  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
 
  
 
105
    perror("malloc");
 
106
    exit(EXIT_FAILURE);
 
107
  }
 
108
  new_plugin->name = name;
127
109
  new_plugin->argv = malloc(sizeof(char *) * 2);
128
110
  if (new_plugin->argv == NULL){
129
 
    free(copy_name);
130
 
    free(new_plugin);
131
 
    return NULL;
 
111
    perror("malloc");
 
112
    exit(EXIT_FAILURE);
132
113
  }
133
 
  new_plugin->argv[0] = copy_name;
 
114
  new_plugin->argv[0] = name;
134
115
  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
 
 
 
116
  new_plugin->argc = 1;
 
117
  new_plugin->disabled = false;
 
118
  new_plugin->next = *plugin_list;
145
119
  /* Append the new plugin to the list */
146
 
  plugin_list = new_plugin;
 
120
  *plugin_list = new_plugin;
147
121
  return new_plugin;
148
122
}
149
123
 
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));
 
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;
187
133
}
188
134
 
189
135
/*
202
148
  return fcntl(fd, F_SETFD, ret | FD_CLOEXEC);
203
149
}
204
150
 
 
151
process *process_list = NULL;
205
152
 
206
 
/* Mark processes as completed when they exit, and save their exit
 
153
/* Mark a process as completed when it exits, and save its exit
207
154
   status. */
208
155
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
 
  }
 
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;
236
172
}
237
173
 
238
 
/* Prints out a password to stdout */
239
174
bool print_out_password(const char *buffer, size_t length){
240
175
  ssize_t ret;
241
176
  if(length>0 and buffer[length-1] == '\n'){
251
186
  return true;
252
187
}
253
188
 
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);
 
189
char ** addcustomargument(char **argv, int *argc, char *arg){
266
190
 
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
 
      }
 
191
  if (argv == NULL){
 
192
    *argc = 1;
 
193
    argv = malloc(sizeof(char*) * 2);
 
194
    if(argv == NULL){
 
195
      return NULL;
278
196
    }
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
 
  }
 
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;
288
209
}
289
210
 
290
211
int main(int argc, char *argv[]){
291
 
  char *plugindir = NULL;
292
 
  char *argfile = NULL;
 
212
  const char *plugindir = "/lib/mandos/plugins.d";
 
213
  const char *argfile = ARGFILE;
293
214
  FILE *conffp;
294
215
  size_t d_name_len;
295
216
  DIR *dir = NULL;
310
231
  /* Establish a signal handler */
311
232
  sigemptyset(&sigchld_action.sa_mask);
312
233
  ret = sigaddset(&sigchld_action.sa_mask, SIGCHLD);
313
 
  if(ret == -1){
 
234
  if(ret < 0){
314
235
    perror("sigaddset");
315
 
    exitstatus = EXIT_FAILURE;
316
 
    goto fallback;
 
236
    exit(EXIT_FAILURE);
317
237
  }
318
238
  ret = sigaction(SIGCHLD, &sigchld_action, &old_sigchld_action);
319
 
  if(ret == -1){
 
239
  if(ret < 0){
320
240
    perror("sigaction");
321
 
    exitstatus = EXIT_FAILURE;
322
 
    goto fallback;
 
241
    exit(EXIT_FAILURE);
323
242
  }
324
243
  
325
244
  /* The options we understand. */
327
246
    { .name = "global-options", .key = 'g',
328
247
      .arg = "OPTION[,OPTION[,...]]",
329
248
      .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
249
    { .name = "options-for", .key = 'o',
334
250
      .arg = "PLUGIN:OPTION[,OPTION[,...]]",
335
251
      .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
252
    { .name = "disable", .key = 'd',
340
253
      .arg = "PLUGIN",
341
254
      .doc = "Disable a specific plugin", .group = 1 },
342
255
    { .name = "plugin-dir", .key = 128,
343
256
      .arg = "DIRECTORY",
344
257
      .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 },
 
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 },
356
266
    { .name = NULL }
357
267
  };
358
268
  
359
 
  error_t parse_opt (int key, char *arg, __attribute__((unused))
360
 
                     struct argp_state *state) {
 
269
  error_t parse_opt (int key, char *arg, struct argp_state *state) {
361
270
    /* Get the INPUT argument from `argp_parse', which we know is a
362
271
       pointer to our plugin list pointer. */
 
272
    plugin **plugins = state->input;
363
273
    switch (key) {
364
 
    case 'g':                   /* --global-options */
 
274
    case 'g':
365
275
      if (arg != NULL){
366
276
        char *p;
367
277
        while((p = strsep(&arg, ",")) != NULL){
368
278
          if(p[0] == '\0'){
369
279
            continue;
370
280
          }
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 */
 
281
          addargument(getplugin(NULL, plugins), p);
 
282
        }
 
283
      }
 
284
      break;
 
285
    case 'o':
393
286
      if (arg != NULL){
394
 
        char *p_name = strsep(&arg, ":");
395
 
        if(p_name[0] == '\0'){
 
287
        char *name = strsep(&arg, ":");
 
288
        if(name[0] == '\0'){
396
289
          break;
397
290
        }
398
291
        char *opt = strsep(&arg, ":");
405
298
            if(p[0] == '\0'){
406
299
              continue;
407
300
            }
408
 
            if(not add_argument(getplugin(p_name), p)){
409
 
              perror("add_argument");
410
 
              return ARGP_ERR_UNKNOWN;
411
 
            }
 
301
            addargument(getplugin(name, plugins), p);
412
302
          }
413
303
        }
414
304
      }
415
305
      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 */
 
306
    case 'd':
436
307
      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 */
 
308
        getplugin(arg, plugins)->disabled = true;
 
309
      }
 
310
      break;
 
311
    case 128:
 
312
      plugindir = arg;
 
313
      break;
 
314
    case 129:
457
315
      uid = (uid_t)strtol(arg, NULL, 10);
458
316
      break;
459
 
    case 131:                   /* --groupid */
 
317
    case 130:
460
318
      gid = (gid_t)strtol(arg, NULL, 10);
461
319
      break;
462
 
    case 132:                   /* --debug */
 
320
    case 131:
463
321
      debug = true;
464
322
      break;
465
323
    case ARGP_KEY_ARG:
466
 
      fprintf(stderr, "Ignoring unknown argument \"%s\"\n", arg);
 
324
      fprintf(stderr, "Ignoring unknown argument \"%s\"\n", arg); 
467
325
      break;
468
326
    case ARGP_KEY_END:
469
327
      break;
473
331
    return 0;
474
332
  }
475
333
  
 
334
  plugin *plugin_list = NULL;
 
335
  
476
336
  struct argp argp = { .options = options, .parser = parse_opt,
477
337
                       .args_doc = "[+PLUS_SEPARATED_OPTIONS]",
478
338
                       .doc = "Mandos plugin runner -- Run plugins" };
479
339
  
480
 
  ret = argp_parse (&argp, argc, argv, 0, 0, NULL);
 
340
  ret = argp_parse (&argp, argc, argv, 0, 0, &plugin_list);
481
341
  if (ret == ARGP_ERR_UNKNOWN){
482
342
    fprintf(stderr, "Unknown error while parsing arguments\n");
483
343
    exitstatus = EXIT_FAILURE;
484
 
    goto fallback;
 
344
    goto end;
485
345
  }
486
346
 
487
 
  /* Opens the configfile if aviable */
488
 
  if (argfile == NULL){
489
 
    conffp = fopen(AFILE, "r");
490
 
  } else {
491
 
    conffp = fopen(argfile, "r");
492
 
  }  
 
347
  conffp = fopen(argfile, "r");
493
348
  if(conffp != NULL){
494
349
    char *org_line = NULL;
495
 
    char *p, *arg, *new_arg, *line;
496
350
    size_t size = 0;
497
351
    ssize_t sret;
 
352
    char *p, *arg, *new_arg, *line;
498
353
    const char whitespace_delims[] = " \r\t\f\v\n";
499
354
    const char comment_delim[] = "#";
500
355
 
501
 
    custom_argc = 1;
502
 
    custom_argv = malloc(sizeof(char*) * 2);
503
 
    if(custom_argv == NULL){
504
 
      perror("malloc");
505
 
      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
356
    while(true){
514
357
      sret = getline(&org_line, &size, conffp);
515
358
      if(sret == -1){
523
366
          continue;
524
367
        }
525
368
        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;        
 
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
        }
544
375
      }
545
376
    }
546
377
    free(org_line);
550
381
    if (errno == EMFILE or errno == ENFILE or errno == ENOMEM){
551
382
      perror("fopen");
552
383
      exitstatus = EXIT_FAILURE;
553
 
      goto fallback;
 
384
      goto end;
554
385
    }
555
386
  }
556
 
  /* If there was any arguments from configuration file,
557
 
     pass them to parser as command arguments */
 
387
 
558
388
  if(custom_argv != NULL){
559
 
    ret = argp_parse (&argp, custom_argc, custom_argv, 0, 0, NULL);
 
389
    custom_argv[0] = argv[0];
 
390
    ret = argp_parse (&argp, custom_argc, custom_argv, 0, 0, &plugin_list);
560
391
    if (ret == ARGP_ERR_UNKNOWN){
561
392
      fprintf(stderr, "Unknown error while parsing arguments\n");
562
393
      exitstatus = EXIT_FAILURE;
563
 
      goto fallback;
 
394
      goto end;
564
395
    }
565
396
  }
566
397
  
571
402
      for(char **a = p->argv; *a != NULL; a++){
572
403
        fprintf(stderr, "\tArg: %s\n", *a);
573
404
      }
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
405
    }
579
406
  }
580
 
 
581
 
  /* Strip permissions down to nobody */
 
407
  
582
408
  ret = setuid(uid);
583
409
  if (ret == -1){
584
410
    perror("setuid");
585
 
  }  
 
411
  }
 
412
  
586
413
  setgid(gid);
587
414
  if (ret == -1){
588
415
    perror("setgid");
589
416
  }
590
 
 
591
 
  if (plugindir == NULL){
592
 
    dir = opendir(PDIR);
593
 
  } else {
594
 
    dir = opendir(plugindir);
595
 
  }
596
417
  
 
418
  dir = opendir(plugindir);
597
419
  if(dir == NULL){
598
420
    perror("Could not open plugin dir");
599
421
    exitstatus = EXIT_FAILURE;
600
 
    goto fallback;
 
422
    goto end;
601
423
  }
602
424
  
603
425
  /* Set the FD_CLOEXEC flag on the directory, if possible */
608
430
      if(ret < 0){
609
431
        perror("set_cloexec_flag");
610
432
        exitstatus = EXIT_FAILURE;
611
 
        goto fallback;
 
433
        goto end;
612
434
      }
613
435
    }
614
436
  }
615
437
  
616
438
  FD_ZERO(&rfds_all);
617
 
 
618
 
  /* Read and execute any executable in the plugin directory*/
 
439
  
619
440
  while(true){
620
441
    dirst = readdir(dir);
621
442
    
624
445
      if (errno == EBADF){
625
446
        perror("readdir");
626
447
        exitstatus = EXIT_FAILURE;
627
 
        goto fallback;
 
448
        goto end;
628
449
      }
629
450
      break;
630
451
    }
652
473
          break;
653
474
        }
654
475
      }
 
476
      
655
477
      if(bad_name){
656
478
        continue;
657
479
      }
 
480
      
658
481
      for(const char **suf = bad_suffixes; *suf != NULL; suf++){
659
482
        size_t suf_len = strlen(*suf);
660
483
        if((d_name_len >= suf_len)
673
496
        continue;
674
497
      }
675
498
    }
676
 
 
677
 
    char *filename;
678
 
    ret = asprintf(&filename, "%s/%s", plugindir, dirst->d_name);
679
 
    if(ret < 0){
680
 
      perror("asprintf");
 
499
    
 
500
    char *filename = malloc(d_name_len + strlen(plugindir) + 2);
 
501
    if (filename == NULL){
 
502
      perror("malloc");
681
503
      continue;
682
504
    }
 
505
    strcpy(filename, plugindir); /* Spurious warning */
 
506
    strcat(filename, "/");      /* Spurious warning */
 
507
    strcat(filename, dirst->d_name); /* Spurious warning */
683
508
    
684
509
    ret = stat(filename, &st);
685
510
    if (ret == -1){
687
512
      free(filename);
688
513
      continue;
689
514
    }
690
 
 
691
 
    /* Ignore non-executable files */
 
515
    
692
516
    if (not S_ISREG(st.st_mode) or (access(filename, X_OK) != 0)){
693
517
      if(debug){
694
518
        fprintf(stderr, "Ignoring plugin dir entry \"%s\""
697
521
      free(filename);
698
522
      continue;
699
523
    }
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){
 
524
    if(getplugin(dirst->d_name, &plugin_list)->disabled){
708
525
      if(debug){
709
526
        fprintf(stderr, "Ignoring disabled plugin \"%s\"\n",
710
527
                dirst->d_name);
712
529
      free(filename);
713
530
      continue;
714
531
    }
 
532
    plugin *p = getplugin(dirst->d_name, &plugin_list);
715
533
    {
716
534
      /* 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];
 
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]; 
749
541
    ret = pipe(pipefd);
750
542
    if (ret == -1){
751
543
      perror("pipe");
752
544
      exitstatus = EXIT_FAILURE;
753
 
      goto fallback;
 
545
      goto end;
754
546
    }
755
 
    /* Ask OS to automatic close the pipe on exec */
756
547
    ret = set_cloexec_flag(pipefd[0]);
757
548
    if(ret < 0){
758
549
      perror("set_cloexec_flag");
759
550
      exitstatus = EXIT_FAILURE;
760
 
      goto fallback;
 
551
      goto end;
761
552
    }
762
553
    ret = set_cloexec_flag(pipefd[1]);
763
554
    if(ret < 0){
764
555
      perror("set_cloexec_flag");
765
556
      exitstatus = EXIT_FAILURE;
766
 
      goto fallback;
 
557
      goto end;
767
558
    }
768
559
    /* Block SIGCHLD until process is safely in process list */
769
560
    ret = sigprocmask (SIG_BLOCK, &sigchld_action.sa_mask, NULL);
770
561
    if(ret < 0){
771
562
      perror("sigprocmask");
772
563
      exitstatus = EXIT_FAILURE;
773
 
      goto fallback;
 
564
      goto end;
774
565
    }
775
566
    // Starting a new process to be watched
776
567
    pid_t pid = fork();
777
568
    if(pid == -1){
778
569
      perror("fork");
779
570
      exitstatus = EXIT_FAILURE;
780
 
      goto fallback;
 
571
      goto end;
781
572
    }
782
573
    if(pid == 0){
783
574
      /* this is the child process */
803
594
           above and must now close it manually here. */
804
595
        closedir(dir);
805
596
      }
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
 
        }
 
597
      if(execv(filename, p->argv) < 0){
 
598
        perror("execv");
 
599
        _exit(EXIT_FAILURE);
816
600
      }
817
601
      /* no return */
818
602
    }
819
 
    /* Parent process */
820
 
    close(pipefd[1]);           /* Close unused write end of pipe */
 
603
    /* parent process */
821
604
    free(filename);
822
 
    plugin *new_plugin = getplugin(dirst->d_name);
823
 
    if (new_plugin == NULL){
824
 
      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");
825
609
      ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
826
610
      if(ret < 0){
827
 
        perror("sigprocmask");
 
611
        perror("sigprocmask");
828
612
      }
829
613
      exitstatus = EXIT_FAILURE;
830
 
      goto fallback;
 
614
      goto end;
831
615
    }
832
616
    
833
 
    new_plugin->pid = pid;
834
 
    new_plugin->fd = pipefd[0];
835
 
    
 
617
    *new_process = (struct process){ .pid = pid,
 
618
                                     .fd = pipefd[0],
 
619
                                     .next = process_list };
 
620
    // List handling
 
621
    process_list = new_process;
836
622
    /* Unblock SIGCHLD so signal handler can be run if this process
837
623
       has already completed */
838
624
    ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
839
625
    if(ret < 0){
840
626
      perror("sigprocmask");
841
627
      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
 
    
 
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);
851
644
  }
852
645
  
853
646
  closedir(dir);
854
647
  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
 
    }
 
648
    
 
649
  if (process_list == NULL){
 
650
    fprintf(stderr, "No plugin processes started. Incorrect plugin"
 
651
            " directory?\n");
 
652
    process_list = NULL;
865
653
  }
866
 
 
867
 
  /* Main loop while running plugins exist */
868
 
  while(plugin_list){
 
654
  while(process_list){
869
655
    fd_set rfds = rfds_all;
870
656
    int select_ret = select(maxfd+1, &rfds, NULL, NULL, NULL);
871
657
    if (select_ret == -1){
872
658
      perror("select");
873
659
      exitstatus = EXIT_FAILURE;
874
 
      goto fallback;
 
660
      goto end;
875
661
    }
876
662
    /* OK, now either a process completed, or something can be read
877
663
       from one of them */
878
 
    for(plugin *proc = plugin_list; proc != NULL; proc = proc->next){
 
664
    for(process *proc = process_list; proc ; proc = proc->next){
879
665
      /* Is this process completely done? */
880
666
      if(proc->eof and proc->completed){
881
667
        /* Only accept the plugin output if it exited cleanly */
882
668
        if(not WIFEXITED(proc->status)
883
669
           or WEXITSTATUS(proc->status) != 0){
884
670
          /* Bad exit by plugin */
885
 
 
886
671
          if(debug){
887
672
            if(WIFEXITED(proc->status)){
888
673
              fprintf(stderr, "Plugin %u exited with status %d\n",
897
682
                      (unsigned int) (proc->pid));
898
683
            }
899
684
          }
900
 
          
901
685
          /* Remove the plugin */
902
686
          FD_CLR(proc->fd, &rfds_all);
903
 
 
904
687
          /* Block signal while modifying process_list */
905
 
          ret = sigprocmask(SIG_BLOCK, &sigchld_action.sa_mask, NULL);
 
688
          ret = sigprocmask (SIG_BLOCK, &sigchld_action.sa_mask, NULL);
906
689
          if(ret < 0){
907
690
            perror("sigprocmask");
908
691
            exitstatus = EXIT_FAILURE;
909
 
            goto fallback;
910
 
          }
911
 
          free_plugin(proc);
 
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
          }
912
707
          /* We are done modifying process list, so unblock signal */
913
708
          ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask,
914
709
                             NULL);
915
710
          if(ret < 0){
916
711
            perror("sigprocmask");
917
 
            exitstatus = EXIT_FAILURE;
918
 
            goto fallback;
919
 
          }
920
 
          
921
 
          if(plugin_list == NULL){
922
 
            break;
923
 
          }
924
 
          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;
925
719
        }
926
 
        
927
720
        /* This process exited nicely, so print its buffer */
928
721
 
929
 
        bool bret = print_out_password(proc->buffer,
930
 
                                       proc->buffer_length);
 
722
        bool bret = print_out_password(proc->buffer, proc->buffer_length);
931
723
        if(not bret){
932
724
          perror("print_out_password");
933
725
          exitstatus = EXIT_FAILURE;
934
726
        }
935
 
        goto fallback;
 
727
        goto end;
936
728
      }
937
 
      
938
729
      /* This process has not completed.  Does it have any output? */
939
730
      if(proc->eof or not FD_ISSET(proc->fd, &rfds)){
940
731
        /* This process had nothing to say at this time */
947
738
        if (proc->buffer == NULL){
948
739
          perror("malloc");
949
740
          exitstatus = EXIT_FAILURE;
950
 
          goto fallback;
 
741
          goto end;
951
742
        }
952
743
        proc->buffer_size += BUFFER_SIZE;
953
744
      }
968
759
  }
969
760
 
970
761
 
971
 
 fallback:
 
762
 end:
972
763
  
973
 
  if(plugin_list == NULL or exitstatus != EXIT_SUCCESS){
974
 
    /* Fallback if all plugins failed, none are found or an error
975
 
       occured */
 
764
  if(process_list == NULL or exitstatus != EXIT_SUCCESS){
 
765
    /* Fallback if all plugins failed, none are found or an error occured */
976
766
    bool bret;
977
767
    fprintf(stderr, "Going to fallback mode using getpass(3)\n");
978
768
    char *passwordbuffer = getpass("Password: ");
980
770
    if(not bret){
981
771
      perror("print_out_password");
982
772
      exitstatus = EXIT_FAILURE;
 
773
      goto end;
983
774
    }
984
775
  }
985
776
  
986
777
  /* 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);
 
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);
998
787
  }
999
788
  
1000
789
  if(dir != NULL){
1002
791
  }
1003
792
  
1004
793
  /* 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
 
      }
 
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");
1013
801
    }
 
802
    free(process_list->buffer);
 
803
    free(process_list);
1014
804
  }
1015
805
  
1016
806
  /* Wait for any remaining child processes to terminate */
1020
810
  if(errno != ECHILD){
1021
811
    perror("wait");
1022
812
  }
1023
 
 
1024
 
  free_plugin_list();
1025
 
  
1026
 
  free(plugindir);
1027
 
  free(argfile);
1028
813
  
1029
814
  return exitstatus;
1030
815
}