/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-22 00:16:20 UTC
  • mfrom: (24.1.57 mandos)
  • Revision ID: teddy@fukt.bsnet.se-20080822001620-vxpn1evy0t0kyvj0
* clients.conf ([DEFAULT]/checker): Update to new default value.

* mandos (Client.start_checker): Bug fix: OSError, not
                                 subprocess.OSError.
  (main): Use "fping -q -- %(host)s" instead of "fping -q --
          %%(host)s" as default value for "checker".  Always redirect
          stdin to be from /dev/null, even if in debug mode.

* mandos-clients.conf.xml (DESCRIPTION): Improved wording and refer to
                                         the EXPANSION section.
  (OPTIONS): Added synopsis and improved wording for "checker",
             "fingerprint", and "secret".  Refer to the RUNTIME
             EXPANSION section for the "checker" option.
  (EXAMPLE): Update to new default value for "checker".

* mandos-keygen (trap): Split lines and add "set +e".

Show diffs side-by-side

added added

removed removed

Lines of Context:
21
21
 * Contact the authors at <mandos@fukt.bsnet.se>.
22
22
 */
23
23
 
24
 
#define _GNU_SOURCE             /* TEMP_FAILURE_RETRY() */
25
 
 
26
 
#include <stdio.h>              /* popen(), fileno(), fprintf(),
27
 
                                   stderr, STDOUT_FILENO */
 
24
#define _GNU_SOURCE             /* TEMP_FAILURE_RETRY(), getline(),
 
25
                                   asprintf() */
 
26
#include <stddef.h>             /* size_t, NULL */
 
27
#include <stdlib.h>             /* malloc(), exit(), EXIT_FAILURE,
 
28
                                   EXIT_SUCCESS, realloc() */
 
29
#include <stdbool.h>            /* bool, true, false */
 
30
#include <stdio.h>              /* perror, popen(), fileno(),
 
31
                                   fprintf(), stderr, STDOUT_FILENO */
 
32
#include <sys/types.h>          /* DIR, opendir(), stat(), struct
 
33
                                   stat, waitpid(), WIFEXITED(),
 
34
                                   WEXITSTATUS(), wait(), pid_t,
 
35
                                   uid_t, gid_t, getuid(), getgid(),
 
36
                                   dirfd() */
 
37
#include <sys/select.h>         /* fd_set, select(), FD_ZERO(),
 
38
                                   FD_SET(), FD_ISSET(), FD_CLR */
 
39
#include <sys/wait.h>           /* wait(), waitpid(), WIFEXITED(),
 
40
                                   WEXITSTATUS() */
 
41
#include <sys/stat.h>           /* struct stat, stat(), S_ISREG() */
28
42
#include <iso646.h>             /* and, or, not */
29
 
#include <sys/types.h>          /* DIR, opendir(), stat(),
30
 
                                   struct stat, waitpid(),
31
 
                                   WIFEXITED(), WEXITSTATUS(),
32
 
                                   wait() */
33
 
#include <sys/wait.h>           /* wait() */
34
43
#include <dirent.h>             /* DIR, struct dirent, opendir(),
35
 
                                   readdir(), closedir() */
36
 
#include <sys/stat.h>           /* struct stat, stat(), S_ISREG() */
 
44
                                   readdir(), closedir(), dirfd() */
37
45
#include <unistd.h>             /* struct stat, stat(), S_ISREG(),
38
 
                                   fcntl() */
39
 
#include <fcntl.h>              /* fcntl() */
40
 
#include <stddef.h>             /* NULL */
41
 
#include <stdlib.h>             /* EXIT_FAILURE */
42
 
#include <sys/select.h>         /* fd_set, select(), FD_ZERO(),
43
 
                                   FD_SET(), FD_ISSET() */
44
 
#include <string.h>             /* strlen(), strcpy(), strcat() */
45
 
#include <stdbool.h>            /* true */
46
 
#include <sys/wait.h>           /* waitpid(), WIFEXITED(),
47
 
                                   WEXITSTATUS() */
 
46
                                   fcntl(), setuid(), setgid(),
 
47
                                   F_GETFD, F_SETFD, FD_CLOEXEC,
 
48
                                   access(), pipe(), fork(), close()
 
49
                                   dup2, STDOUT_FILENO, _exit(),
 
50
                                   execv(), write(), read(),
 
51
                                   close() */
 
52
#include <fcntl.h>              /* fcntl(), F_GETFD, F_SETFD,
 
53
                                   FD_CLOEXEC */
 
54
#include <string.h>             /* strsep, strlen(), asprintf() */
48
55
#include <errno.h>              /* errno */
49
 
#include <argp.h>               /* struct argp_option,
50
 
                                   struct argp_state, struct argp,
51
 
                                   argp_parse() */
 
56
#include <argp.h>               /* struct argp_option, struct
 
57
                                   argp_state, struct argp,
 
58
                                   argp_parse(), ARGP_ERR_UNKNOWN,
 
59
                                   ARGP_KEY_END, ARGP_KEY_ARG, error_t */
 
60
#include <signal.h>             /* struct sigaction, sigemptyset(),
 
61
                                   sigaddset(), sigaction(),
 
62
                                   sigprocmask(), SIG_BLOCK, SIGCHLD,
 
63
                                   SIG_UNBLOCK, kill() */
 
64
#include <errno.h>              /* errno, EBADF */
52
65
 
53
66
#define BUFFER_SIZE 256
 
67
#define ARGFILE "/conf/conf.d/mandos/plugin-runner.conf"
 
68
 
 
69
const char *argp_program_version = "plugin-runner 1.0";
 
70
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
54
71
 
55
72
struct process;
56
73
 
70
87
  char *name;                   /* can be NULL or any plugin name */
71
88
  char **argv;
72
89
  int argc;
 
90
  char **environ;
 
91
  int envc;
73
92
  bool disabled;
74
93
  struct plugin *next;
75
94
} plugin;
84
103
  /* Create a new plugin */
85
104
  plugin *new_plugin = malloc(sizeof(plugin));
86
105
  if (new_plugin == NULL){
87
 
    perror("malloc");
88
 
    exit(EXIT_FAILURE);
89
 
  }
90
 
  new_plugin->name = name;
 
106
    return NULL;
 
107
  }
 
108
  char *copy_name = strdup(name);
 
109
  if(copy_name == NULL){
 
110
    return NULL;
 
111
  }
 
112
  
 
113
  *new_plugin = (plugin) { .name = copy_name,
 
114
                           .argc = 1,
 
115
                           .envc = 0,
 
116
                           .disabled = false,
 
117
                           .next = *plugin_list };
 
118
  
91
119
  new_plugin->argv = malloc(sizeof(char *) * 2);
92
120
  if (new_plugin->argv == NULL){
93
 
    perror("malloc");
94
 
    exit(EXIT_FAILURE);
 
121
    free(new_plugin);
 
122
    return NULL;
95
123
  }
96
 
  new_plugin->argv[0] = name;
 
124
  new_plugin->argv[0] = copy_name;
97
125
  new_plugin->argv[1] = NULL;
98
 
  new_plugin->argc = 1;
99
 
  new_plugin->disabled = false;
100
 
  new_plugin->next = *plugin_list;
 
126
 
 
127
  new_plugin->environ = malloc(sizeof(char *));
 
128
  if(new_plugin->environ == NULL){
 
129
    free(new_plugin->argv);
 
130
    free(new_plugin);
 
131
    return NULL;
 
132
  }
 
133
  new_plugin->environ[0] = NULL;
101
134
  /* Append the new plugin to the list */
102
135
  *plugin_list = new_plugin;
103
136
  return new_plugin;
104
137
}
105
138
 
106
 
static void addargument(plugin *p, char *arg){
107
 
  p->argv[p->argc] = arg;
108
 
  p->argv = realloc(p->argv, sizeof(char *) * (size_t)(p->argc + 2));
109
 
  if (p->argv == NULL){
110
 
    perror("malloc");
111
 
    exit(EXIT_FAILURE);
112
 
  }
113
 
  p->argc++;
114
 
  p->argv[p->argc] = NULL;
115
 
}
 
139
/* Helper function for add_argument and add_environment */
 
140
static bool add_to_char_array(const char *new, char ***array,
 
141
                              int *len){
 
142
  /* Resize the pointed-to array to hold one more pointer */
 
143
  *array = realloc(*array, sizeof(char *)
 
144
                   * (size_t) ((*len) + 2));
 
145
  /* Malloc check */
 
146
  if(*array == NULL){
 
147
    return false;
 
148
  }
 
149
  /* Make a copy of the new string */
 
150
  char *copy = strdup(new);
 
151
  if(copy == NULL){
 
152
    return false;
 
153
  }
 
154
  /* Insert the copy */
 
155
  (*array)[*len] = copy;
 
156
  (*len)++;
 
157
  /* Add a new terminating NULL pointer to the last element */
 
158
  (*array)[*len] = NULL;
 
159
  return true;
 
160
}
 
161
 
 
162
/* Add to a plugin's argument vector */
 
163
static bool add_argument(plugin *p, const char *arg){
 
164
  if(p == NULL){
 
165
    return false;
 
166
  }
 
167
  return add_to_char_array(arg, &(p->argv), &(p->argc));
 
168
}
 
169
 
 
170
/* Add to a plugin's environment */
 
171
static bool add_environment(plugin *p, const char *def){
 
172
  if(p == NULL){
 
173
    return false;
 
174
  }
 
175
  return add_to_char_array(def, &(p->environ), &(p->envc));
 
176
}
 
177
 
116
178
 
117
179
/*
118
180
 * Based on the example in the GNU LibC manual chapter 13.13 "File
130
192
  return fcntl(fd, F_SETFD, ret | FD_CLOEXEC);
131
193
}
132
194
 
133
 
const char *argp_program_version = "plugbasedclient 0.9";
134
 
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
135
 
 
136
195
process *process_list = NULL;
137
196
 
138
197
/* Mark a process as completed when it exits, and save its exit
141
200
  process *proc = process_list;
142
201
  int status;
143
202
  pid_t pid = wait(&status);
 
203
  if(pid == -1){
 
204
    perror("wait");
 
205
    return;
 
206
  }
144
207
  while(proc != NULL and proc->pid != pid){
145
208
    proc = proc->next;
146
209
  }
152
215
  proc->completed = true;
153
216
}
154
217
 
 
218
bool print_out_password(const char *buffer, size_t length){
 
219
  ssize_t ret;
 
220
  if(length>0 and buffer[length-1] == '\n'){
 
221
    length--;
 
222
  }
 
223
  for(size_t written = 0; written < length; written += (size_t)ret){
 
224
    ret = TEMP_FAILURE_RETRY(write(STDOUT_FILENO, buffer + written,
 
225
                                   length - written));
 
226
    if(ret < 0){
 
227
      return false;
 
228
    }
 
229
  }
 
230
  return true;
 
231
}
 
232
 
 
233
char **add_to_argv(char **argv, int *argc, char *arg){
 
234
  if (argv == NULL){
 
235
    *argc = 1;
 
236
    argv = malloc(sizeof(char*) * 2);
 
237
    if(argv == NULL){
 
238
      return NULL;
 
239
    }
 
240
    argv[0] = NULL;     /* Will be set to argv[0] in main before parsing */
 
241
    argv[1] = NULL;
 
242
  }
 
243
  *argc += 1;
 
244
  argv = realloc(argv, sizeof(char *)
 
245
                  * ((unsigned int) *argc + 1));
 
246
  if(argv == NULL){
 
247
    return NULL;
 
248
  }
 
249
  argv[*argc-1] = arg;
 
250
  argv[*argc] = NULL;   
 
251
  return argv;
 
252
}
 
253
 
 
254
static void free_plugin_list(plugin *plugin_list){
 
255
  for(plugin *next = plugin_list; plugin_list != NULL; plugin_list = next){
 
256
    next = plugin_list->next;
 
257
    free(plugin_list->name);
 
258
    for(char **arg = plugin_list->argv; *arg != NULL; arg++){
 
259
      free(*arg);
 
260
    }    
 
261
    free(plugin_list->argv);
 
262
    for(char **env = plugin_list->environ; *env != NULL; env++){
 
263
      free(*env);
 
264
    }
 
265
    free(plugin_list->environ);
 
266
    free(plugin_list);
 
267
  }  
 
268
}
 
269
 
155
270
int main(int argc, char *argv[]){
156
 
  const char *plugindir = "/conf/conf.d/mandos/plugins.d";
 
271
  const char *plugindir = "/lib/mandos/plugins.d";
 
272
  const char *argfile = ARGFILE;
 
273
  FILE *conffp;
157
274
  size_t d_name_len;
158
275
  DIR *dir = NULL;
159
276
  struct dirent *dirst;
167
284
  struct sigaction old_sigchld_action;
168
285
  struct sigaction sigchld_action = { .sa_handler = handle_sigchld,
169
286
                                      .sa_flags = SA_NOCLDSTOP };
170
 
  char *plus_options = NULL;
171
 
  char **plus_argv = NULL;
 
287
  char **custom_argv = NULL;
 
288
  int custom_argc = 0;
172
289
  
173
290
  /* Establish a signal handler */
174
291
  sigemptyset(&sigchld_action.sa_mask);
175
292
  ret = sigaddset(&sigchld_action.sa_mask, SIGCHLD);
176
 
  if(ret < 0){
 
293
  if(ret == -1){
177
294
    perror("sigaddset");
178
 
    exit(EXIT_FAILURE);
 
295
    exitstatus = EXIT_FAILURE;
 
296
    goto fallback;
179
297
  }
180
298
  ret = sigaction(SIGCHLD, &sigchld_action, &old_sigchld_action);
181
 
  if(ret < 0){
 
299
  if(ret == -1){
182
300
    perror("sigaction");
183
 
    exit(EXIT_FAILURE);
 
301
    exitstatus = EXIT_FAILURE;    
 
302
    goto fallback;
184
303
  }
185
304
  
186
305
  /* The options we understand. */
188
307
    { .name = "global-options", .key = 'g',
189
308
      .arg = "OPTION[,OPTION[,...]]",
190
309
      .doc = "Options passed to all plugins" },
 
310
    { .name = "global-envs", .key = 'e',
 
311
      .arg = "VAR=value",
 
312
      .doc = "Environment variable passed to all plugins" },
191
313
    { .name = "options-for", .key = 'o',
192
314
      .arg = "PLUGIN:OPTION[,OPTION[,...]]",
193
315
      .doc = "Options passed only to specified plugin" },
 
316
    { .name = "envs-for", .key = 'f',
 
317
      .arg = "PLUGIN:ENV=value",
 
318
      .doc = "Environment variable passed to specified plugin" },
194
319
    { .name = "disable", .key = 'd',
195
320
      .arg = "PLUGIN",
196
321
      .doc = "Disable a specific plugin", .group = 1 },
215
340
    switch (key) {
216
341
    case 'g':
217
342
      if (arg != NULL){
218
 
        char *p = strtok(arg, ",");
219
 
        do{
220
 
          addargument(getplugin(NULL, plugins), p);
221
 
          p = strtok(NULL, ",");
222
 
        } while (p != NULL);
 
343
        char *p;
 
344
        while((p = strsep(&arg, ",")) != NULL){
 
345
          if(p[0] == '\0'){
 
346
            continue;
 
347
          }
 
348
          if(not add_argument(getplugin(NULL, plugins), p)){
 
349
            perror("add_argument");
 
350
            return ARGP_ERR_UNKNOWN;
 
351
          }
 
352
        }
 
353
      }
 
354
      break;
 
355
    case 'e':
 
356
      if(arg == NULL){
 
357
        break;
 
358
      }
 
359
      {
 
360
        char *envdef = strdup(arg);
 
361
        if(envdef == NULL){
 
362
          break;
 
363
        }
 
364
        if(not add_environment(getplugin(NULL, plugins), envdef)){
 
365
          perror("add_environment");
 
366
        }
223
367
      }
224
368
      break;
225
369
    case 'o':
226
370
      if (arg != NULL){
227
 
        char *name = strtok(arg, ":");
228
 
        char *p = strtok(NULL, ":");
229
 
        if(p != NULL){
230
 
          p = strtok(p, ",");
231
 
          do{
232
 
            addargument(getplugin(name, plugins), p);
233
 
            p = strtok(NULL, ",");
234
 
          } while (p != NULL);
 
371
        char *p_name = strsep(&arg, ":");
 
372
        if(p_name[0] == '\0'){
 
373
          break;
 
374
        }
 
375
        char *opt = strsep(&arg, ":");
 
376
        if(opt[0] == '\0'){
 
377
          break;
 
378
        }
 
379
        if(opt != NULL){
 
380
          char *p;
 
381
          while((p = strsep(&opt, ",")) != NULL){
 
382
            if(p[0] == '\0'){
 
383
              continue;
 
384
            }
 
385
            if(not add_argument(getplugin(p_name, plugins), p)){
 
386
              perror("add_argument");
 
387
              return ARGP_ERR_UNKNOWN;
 
388
            }
 
389
          }
 
390
        }
 
391
      }
 
392
      break;
 
393
    case 'f':
 
394
      if(arg == NULL){
 
395
        break;
 
396
      }
 
397
      {
 
398
        char *envdef = strchr(arg, ':');
 
399
        if(envdef == NULL){
 
400
          break;
 
401
        }
 
402
        char *p_name = strndup(arg, (size_t) (envdef-arg));
 
403
        if(p_name == NULL){
 
404
          break;
 
405
        }
 
406
        envdef++;
 
407
        if(not add_environment(getplugin(p_name, plugins), envdef)){
 
408
          perror("add_environment");
235
409
        }
236
410
      }
237
411
      break;
238
412
    case 'd':
239
413
      if (arg != NULL){
240
 
        getplugin(arg, plugins)->disabled = true;
 
414
        plugin *p = getplugin(arg, plugins);
 
415
        if(p == NULL){
 
416
          return ARGP_ERR_UNKNOWN;
 
417
        }
 
418
        p->disabled = true;
241
419
      }
242
420
      break;
243
421
    case 128:
253
431
      debug = true;
254
432
      break;
255
433
    case ARGP_KEY_ARG:
256
 
      if(plus_options != NULL or arg == NULL or arg[0] != '+'){
257
 
        argp_usage (state);
258
 
      }
259
 
      plus_options = arg;
 
434
      fprintf(stderr, "Ignoring unknown argument \"%s\"\n", arg);
260
435
      break;
261
436
    case ARGP_KEY_END:
262
437
      break;
272
447
                       .args_doc = "[+PLUS_SEPARATED_OPTIONS]",
273
448
                       .doc = "Mandos plugin runner -- Run plugins" };
274
449
  
275
 
  argp_parse (&argp, argc, argv, 0, 0, &plugin_list);  
276
 
  
277
 
  if(plus_options){
278
 
    /* This is a mangled argument in the form of
279
 
     "+--option+--other-option=parameter+--yet-another-option", etc */
280
 
    /* Make new argc and argv vars, and call argp_parse() again. */
281
 
    plus_options++;             /* skip the first '+' character */
282
 
    const char delims[] = "+";
283
 
    char *arg;
284
 
    int new_argc = 1;
285
 
    plus_argv = malloc(sizeof(char*) * 2);
286
 
    if(plus_argv == NULL){
287
 
      perror("malloc");
288
 
      exitstatus = EXIT_FAILURE;
289
 
      goto end;
290
 
    }
291
 
    plus_argv[0] = argv[0];
292
 
    plus_argv[1] = NULL;
293
 
    arg = strtok(plus_options, delims); /* Get first argument */
294
 
    while(arg != NULL){
295
 
      new_argc++;
296
 
      plus_argv = realloc(plus_argv, sizeof(char *)
297
 
                         * ((unsigned int) new_argc + 1));
298
 
      if(plus_argv == NULL){
299
 
        perror("malloc");
300
 
        exitstatus = EXIT_FAILURE;
301
 
        goto end;
302
 
      }
303
 
      plus_argv[new_argc-1] = arg;
304
 
      plus_argv[new_argc] = NULL;
305
 
      arg = strtok(NULL, delims); /* Get next argument */
306
 
    }
307
 
    argp_parse (&argp, new_argc, plus_argv, 0, 0, &plugin_list);  
 
450
  ret = argp_parse (&argp, argc, argv, 0, 0, &plugin_list);
 
451
  if (ret == ARGP_ERR_UNKNOWN){
 
452
    fprintf(stderr, "Unknown error while parsing arguments\n");
 
453
    exitstatus = EXIT_FAILURE;
 
454
    goto fallback;
 
455
  }
 
456
 
 
457
  conffp = fopen(argfile, "r");
 
458
  if(conffp != NULL){
 
459
    char *org_line = NULL;
 
460
    char *p, *arg, *new_arg, *line;
 
461
    size_t size = 0;
 
462
    ssize_t sret;
 
463
    const char whitespace_delims[] = " \r\t\f\v\n";
 
464
    const char comment_delim[] = "#";
 
465
 
 
466
    while(true){
 
467
      sret = getline(&org_line, &size, conffp);
 
468
      if(sret == -1){
 
469
        break;
 
470
      }
 
471
 
 
472
      line = org_line;
 
473
      arg = strsep(&line, comment_delim);
 
474
      while((p = strsep(&arg, whitespace_delims)) != NULL){
 
475
        if(p[0] == '\0'){
 
476
          continue;
 
477
        }
 
478
        new_arg = strdup(p);
 
479
        custom_argv = add_to_argv(custom_argv, &custom_argc, new_arg);
 
480
        if (custom_argv == NULL){
 
481
          perror("add_to_argv");
 
482
          exitstatus = EXIT_FAILURE;
 
483
          goto fallback;
 
484
        }
 
485
      }
 
486
    }
 
487
    free(org_line);
 
488
  } else{
 
489
    /* Check for harmful errors and go to fallback. Other errors might
 
490
       not affect opening plugins */
 
491
    if (errno == EMFILE or errno == ENFILE or errno == ENOMEM){
 
492
      perror("fopen");
 
493
      exitstatus = EXIT_FAILURE;
 
494
      goto fallback;
 
495
    }
 
496
  }
 
497
 
 
498
  if(custom_argv != NULL){
 
499
    custom_argv[0] = argv[0];
 
500
    ret = argp_parse (&argp, custom_argc, custom_argv, 0, 0, &plugin_list);
 
501
    if (ret == ARGP_ERR_UNKNOWN){
 
502
      fprintf(stderr, "Unknown error while parsing arguments\n");
 
503
      exitstatus = EXIT_FAILURE;
 
504
      goto fallback;
 
505
    }
308
506
  }
309
507
  
310
508
  if(debug){
314
512
      for(char **a = p->argv; *a != NULL; a++){
315
513
        fprintf(stderr, "\tArg: %s\n", *a);
316
514
      }
 
515
      fprintf(stderr, "...and %u environment variables\n", p->envc);
 
516
      for(char **a = p->environ; *a != NULL; a++){
 
517
        fprintf(stderr, "\t%s\n", *a);
 
518
      }
317
519
    }
318
520
  }
319
521
  
331
533
  if(dir == NULL){
332
534
    perror("Could not open plugin dir");
333
535
    exitstatus = EXIT_FAILURE;
334
 
    goto end;
 
536
    goto fallback;
335
537
  }
336
538
  
337
539
  /* Set the FD_CLOEXEC flag on the directory, if possible */
342
544
      if(ret < 0){
343
545
        perror("set_cloexec_flag");
344
546
        exitstatus = EXIT_FAILURE;
345
 
        goto end;
 
547
        goto fallback;
346
548
      }
347
549
    }
348
550
  }
354
556
    
355
557
    // All directory entries have been processed
356
558
    if(dirst == NULL){
 
559
      if (errno == EBADF){
 
560
        perror("readdir");
 
561
        exitstatus = EXIT_FAILURE;
 
562
        goto fallback;
 
563
      }
357
564
      break;
358
565
    }
359
566
    
403
610
        continue;
404
611
      }
405
612
    }
406
 
    
407
 
    char *filename = malloc(d_name_len + strlen(plugindir) + 2);
408
 
    if (filename == NULL){
409
 
      perror("malloc");
410
 
      exitstatus = EXIT_FAILURE;
411
 
      goto end;
412
 
    }
413
 
    strcpy(filename, plugindir); /* Spurious warning */
414
 
    strcat(filename, "/");      /* Spurious warning */
415
 
    strcat(filename, dirst->d_name); /* Spurious warning */
416
 
    
417
 
    stat(filename, &st);
 
613
 
 
614
    char *filename;
 
615
    ret = asprintf(&filename, "%s/%s", plugindir, dirst->d_name);
 
616
    if(ret < 0){
 
617
      perror("asprintf");
 
618
      continue;
 
619
    }
 
620
    
 
621
    ret = stat(filename, &st);
 
622
    if (ret == -1){
 
623
      perror("stat");
 
624
      free(filename);
 
625
      continue;
 
626
    }
418
627
    
419
628
    if (not S_ISREG(st.st_mode) or (access(filename, X_OK) != 0)){
420
629
      if(debug){
424
633
      free(filename);
425
634
      continue;
426
635
    }
427
 
    if(getplugin(dirst->d_name, &plugin_list)->disabled){
 
636
    plugin *p = getplugin(dirst->d_name, &plugin_list);
 
637
    if(p == NULL){
 
638
      perror("getplugin");
 
639
      free(filename);
 
640
      continue;
 
641
    }
 
642
    if(p->disabled){
428
643
      if(debug){
429
644
        fprintf(stderr, "Ignoring disabled plugin \"%s\"\n",
430
645
                dirst->d_name);
432
647
      free(filename);
433
648
      continue;
434
649
    }
435
 
    plugin *p = getplugin(dirst->d_name, &plugin_list);
436
650
    {
437
651
      /* Add global arguments to argument list for this plugin */
438
652
      plugin *g = getplugin(NULL, &plugin_list);
439
 
      for(char **a = g->argv + 1; *a != NULL; a++){
440
 
        addargument(p, *a);
441
 
      }
442
 
    }
 
653
      if(g != NULL){
 
654
        for(char **a = g->argv + 1; *a != NULL; a++){
 
655
          if(not add_argument(p, *a)){
 
656
            perror("add_argument");
 
657
          }
 
658
        }
 
659
        /* Add global environment variables */
 
660
        for(char **e = g->environ; *e != NULL; e++){
 
661
          if(not add_environment(p, *e)){
 
662
            perror("add_environment");
 
663
          }
 
664
        }
 
665
      }
 
666
    }
 
667
    /* If this plugin has any environment variables, we will call
 
668
       using execve and need to duplicate the environment from this
 
669
       process, too. */
 
670
    if(p->environ[0] != NULL){
 
671
      for(char **e = environ; *e != NULL; e++){
 
672
        char *copy = strdup(*e);
 
673
        if(copy == NULL){
 
674
          perror("strdup");
 
675
          continue;
 
676
        }
 
677
        if(not add_environment(p, copy)){
 
678
          perror("add_environment");
 
679
        }
 
680
      }
 
681
    }
 
682
    
443
683
    int pipefd[2]; 
444
684
    ret = pipe(pipefd);
445
685
    if (ret == -1){
446
686
      perror("pipe");
447
687
      exitstatus = EXIT_FAILURE;
448
 
      goto end;
 
688
      goto fallback;
449
689
    }
450
690
    ret = set_cloexec_flag(pipefd[0]);
451
691
    if(ret < 0){
452
692
      perror("set_cloexec_flag");
453
693
      exitstatus = EXIT_FAILURE;
454
 
      goto end;
 
694
      goto fallback;
455
695
    }
456
696
    ret = set_cloexec_flag(pipefd[1]);
457
697
    if(ret < 0){
458
698
      perror("set_cloexec_flag");
459
699
      exitstatus = EXIT_FAILURE;
460
 
      goto end;
 
700
      goto fallback;
461
701
    }
462
702
    /* Block SIGCHLD until process is safely in process list */
463
703
    ret = sigprocmask (SIG_BLOCK, &sigchld_action.sa_mask, NULL);
464
704
    if(ret < 0){
465
705
      perror("sigprocmask");
466
706
      exitstatus = EXIT_FAILURE;
467
 
      goto end;
 
707
      goto fallback;
468
708
    }
469
709
    // Starting a new process to be watched
470
710
    pid_t pid = fork();
 
711
    if(pid == -1){
 
712
      perror("fork");
 
713
      exitstatus = EXIT_FAILURE;
 
714
      goto fallback;
 
715
    }
471
716
    if(pid == 0){
472
717
      /* this is the child process */
473
718
      ret = sigaction(SIGCHLD, &old_sigchld_action, NULL);
480
725
        perror("sigprocmask");
481
726
        _exit(EXIT_FAILURE);
482
727
      }
483
 
      dup2(pipefd[1], STDOUT_FILENO); /* replace our stdout */
 
728
 
 
729
      ret = dup2(pipefd[1], STDOUT_FILENO); /* replace our stdout */
 
730
      if(ret == -1){
 
731
        perror("dup2");
 
732
        _exit(EXIT_FAILURE);
 
733
      }
484
734
      
485
735
      if(dirfd(dir) < 0){
486
736
        /* If dir has no file descriptor, we could not set FD_CLOEXEC
487
737
           above and must now close it manually here. */
488
738
        closedir(dir);
489
739
      }
490
 
      if(execv(filename, p->argv) < 0){
491
 
        perror("execv");
492
 
        _exit(EXIT_FAILURE);
 
740
      if(p->environ[0] == NULL){
 
741
        if(execv(filename, p->argv) < 0){
 
742
          perror("execv");
 
743
          _exit(EXIT_FAILURE);
 
744
        }
 
745
      } else {
 
746
        if(execve(filename, p->argv, p->environ) < 0){
 
747
          perror("execve");
 
748
          _exit(EXIT_FAILURE);
 
749
        }
493
750
      }
494
751
      /* no return */
495
752
    }
504
761
        perror("sigprocmask");
505
762
      }
506
763
      exitstatus = EXIT_FAILURE;
507
 
      goto end;
 
764
      goto fallback;
508
765
    }
509
766
    
510
767
    *new_process = (struct process){ .pid = pid,
518
775
    if(ret < 0){
519
776
      perror("sigprocmask");
520
777
      exitstatus = EXIT_FAILURE;
521
 
      goto end;
 
778
      goto fallback;
522
779
    }
523
780
    
524
781
    FD_SET(new_process->fd, &rfds_all);
529
786
    
530
787
  }
531
788
  
532
 
  /* Free the plugin list */
533
 
  for(plugin *next; plugin_list != NULL; plugin_list = next){
534
 
    next = plugin_list->next;
535
 
    free(plugin_list->argv);
536
 
    free(plugin_list);
537
 
  }
 
789
  free_plugin_list(plugin_list);
538
790
  
539
791
  closedir(dir);
540
792
  dir = NULL;
541
 
  
 
793
    
542
794
  if (process_list == NULL){
543
 
    fprintf(stderr, "No plugin processes started, exiting\n");
544
 
    exitstatus = EXIT_FAILURE;
545
 
    goto end;
 
795
    fprintf(stderr, "No plugin processes started. Incorrect plugin"
 
796
            " directory?\n");
 
797
    process_list = NULL;
546
798
  }
547
799
  while(process_list){
548
800
    fd_set rfds = rfds_all;
550
802
    if (select_ret == -1){
551
803
      perror("select");
552
804
      exitstatus = EXIT_FAILURE;
553
 
      goto end;
 
805
      goto fallback;
554
806
    }
555
807
    /* OK, now either a process completed, or something can be read
556
808
       from one of them */
563
815
          /* Bad exit by plugin */
564
816
          if(debug){
565
817
            if(WIFEXITED(proc->status)){
566
 
              fprintf(stderr, "Plugin %d exited with status %d\n",
567
 
                      proc->pid, WEXITSTATUS(proc->status));
 
818
              fprintf(stderr, "Plugin %u exited with status %d\n",
 
819
                      (unsigned int) (proc->pid),
 
820
                      WEXITSTATUS(proc->status));
568
821
            } else if(WIFSIGNALED(proc->status)) {
569
 
              fprintf(stderr, "Plugin %d killed by signal %d\n",
570
 
                      proc->pid, WTERMSIG(proc->status));
 
822
              fprintf(stderr, "Plugin %u killed by signal %d\n",
 
823
                      (unsigned int) (proc->pid),
 
824
                      WTERMSIG(proc->status));
571
825
            } else if(WCOREDUMP(proc->status)){
572
 
              fprintf(stderr, "Plugin %d dumped core\n", proc->pid);
 
826
              fprintf(stderr, "Plugin %d dumped core\n",
 
827
                      (unsigned int) (proc->pid));
573
828
            }
574
829
          }
575
830
          /* Remove the plugin */
579
834
          if(ret < 0){
580
835
            perror("sigprocmask");
581
836
            exitstatus = EXIT_FAILURE;
582
 
            goto end;
 
837
            goto fallback;
583
838
          }
584
839
          /* Delete this process entry from the list */
585
840
          if(process_list == proc){
608
863
          break;
609
864
        }
610
865
        /* This process exited nicely, so print its buffer */
611
 
        for(size_t written = 0; written < proc->buffer_length;
612
 
            written += (size_t)ret){
613
 
          ret = TEMP_FAILURE_RETRY(write(STDOUT_FILENO,
614
 
                                         proc->buffer + written,
615
 
                                         proc->buffer_length
616
 
                                         - written));
617
 
          if(ret < 0){
618
 
            perror("write");
619
 
            exitstatus = EXIT_FAILURE;
620
 
            goto end;
621
 
          }
 
866
 
 
867
        bool bret = print_out_password(proc->buffer, proc->buffer_length);
 
868
        if(not bret){
 
869
          perror("print_out_password");
 
870
          exitstatus = EXIT_FAILURE;
622
871
        }
623
 
        goto end;
 
872
        goto fallback;
624
873
      }
625
874
      /* This process has not completed.  Does it have any output? */
626
875
      if(proc->eof or not FD_ISSET(proc->fd, &rfds)){
634
883
        if (proc->buffer == NULL){
635
884
          perror("malloc");
636
885
          exitstatus = EXIT_FAILURE;
637
 
          goto end;
 
886
          goto fallback;
638
887
        }
639
888
        proc->buffer_size += BUFFER_SIZE;
640
889
      }
653
902
      }
654
903
    }
655
904
  }
656
 
  if(process_list == NULL){
657
 
    fprintf(stderr, "All plugin processes failed, exiting\n");
658
 
    exitstatus = EXIT_FAILURE;
 
905
 
 
906
 
 
907
 fallback:
 
908
  
 
909
  if(process_list == NULL or exitstatus != EXIT_SUCCESS){
 
910
    /* Fallback if all plugins failed, none are found or an error occured */
 
911
    bool bret;
 
912
    fprintf(stderr, "Going to fallback mode using getpass(3)\n");
 
913
    char *passwordbuffer = getpass("Password: ");
 
914
    bret = print_out_password(passwordbuffer, strlen(passwordbuffer));
 
915
    if(not bret){
 
916
      perror("print_out_password");
 
917
      exitstatus = EXIT_FAILURE;
 
918
    }
659
919
  }
660
920
  
661
 
 end:
662
921
  /* Restore old signal handler */
663
 
  sigaction(SIGCHLD, &old_sigchld_action, NULL);
664
 
  
665
 
  free(plus_argv);
666
 
  
667
 
  /* Free the plugin list */
668
 
  for(plugin *next; plugin_list != NULL; plugin_list = next){
669
 
    next = plugin_list->next;
670
 
    free(plugin_list->argv);
671
 
    free(plugin_list);
672
 
  }
 
922
  ret = sigaction(SIGCHLD, &old_sigchld_action, NULL);
 
923
  if(ret == -1){
 
924
    perror("sigaction");
 
925
    exitstatus = EXIT_FAILURE;
 
926
  }
 
927
 
 
928
  if(custom_argv != NULL){
 
929
    for(char **arg = custom_argv; *arg != NULL; arg++){
 
930
      free(*arg);
 
931
    }
 
932
    free(custom_argv);
 
933
  }
 
934
  free_plugin_list(plugin_list);
673
935
  
674
936
  if(dir != NULL){
675
937
    closedir(dir);
679
941
  for(process *next; process_list != NULL; process_list = next){
680
942
    next = process_list->next;
681
943
    close(process_list->fd);
682
 
    kill(process_list->pid, SIGTERM);
 
944
    ret = kill(process_list->pid, SIGTERM);
 
945
    if(ret == -1 and errno != ESRCH){
 
946
      /* set-uid proccesses migth not get closed */
 
947
      perror("kill");
 
948
    }
683
949
    free(process_list->buffer);
684
950
    free(process_list);
685
951
  }