/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-13 15:36:18 UTC
  • Revision ID: teddy@fukt.bsnet.se-20080913153618-atp386q2bqj0ku99
* Makefile (install-client-nokey): Do "&&" instead of ";" to catch
                                   errors.

* README: Kill the straight quotes.  Add copyright notice.

* overview.xml: Improved wording.

Show diffs side-by-side

added added

removed removed

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