/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-08-14 02:29:36 UTC
  • mfrom: (24.1.50 mandos)
  • Revision ID: teddy@fukt.bsnet.se-20080814022936-ook77vj8i1a42msu
Merge.

* Makefile (OPTIMIZE): Uncommented again.

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
 
#include <stdio.h>              /* popen(), fileno(), fprintf(),
25
 
                                   stderr, STDOUT_FILENO */
 
24
#define _GNU_SOURCE             /* TEMP_FAILURE_RETRY() */
 
25
 
 
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() */
26
42
#include <iso646.h>             /* and, or, not */
27
 
#include <sys/types.h>         /* DIR, opendir(), stat(), struct stat,
28
 
                                  waitpid(), WIFEXITED(),
29
 
                                  WEXITSTATUS(), wait() */
30
 
#include <sys/wait.h>           /* wait() */
31
43
#include <dirent.h>             /* DIR, struct dirent, opendir(),
32
 
                                   readdir(), closedir() */
33
 
#include <sys/stat.h>           /* struct stat, stat(), S_ISREG() */
 
44
                                   readdir(), closedir(), dirfd() */
34
45
#include <unistd.h>             /* struct stat, stat(), S_ISREG(),
35
 
                                   fcntl() */
36
 
#include <fcntl.h>              /* fcntl() */
37
 
#include <stddef.h>             /* NULL */
38
 
#include <stdlib.h>             /* EXIT_FAILURE */
39
 
#include <sys/select.h>         /* fd_set, select(), FD_ZERO(),
40
 
                                   FD_SET(), FD_ISSET() */
41
 
#include <string.h>             /* strlen(), strcpy(), strcat() */
42
 
#include <stdbool.h>            /* true */
43
 
#include <sys/wait.h>           /* waitpid(), WIFEXITED(),
44
 
                                   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(), strcpy(),
 
55
                                   strcat() */
45
56
#include <errno.h>              /* errno */
46
 
#include <argp.h>               /* struct argp_option,
47
 
                                   struct argp_state, struct argp,
48
 
                                   argp_parse() */
 
57
#include <argp.h>               /* struct argp_option, struct
 
58
                                   argp_state, struct argp,
 
59
                                   argp_parse(), ARGP_ERR_UNKNOWN,
 
60
                                   ARGP_KEY_END, ARGP_KEY_ARG, error_t */
 
61
#include <signal.h>             /* struct sigaction, sigemptyset(),
 
62
                                   sigaddset(), sigaction(),
 
63
                                   sigprocmask(), SIG_BLOCK, SIGCHLD,
 
64
                                   SIG_UNBLOCK, kill() */
 
65
#include <errno.h>              /* errno, EBADF */
 
66
 
 
67
#define BUFFER_SIZE 256
 
68
 
 
69
const char *argp_program_version = "plugin-runner 1.0";
 
70
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
49
71
 
50
72
struct process;
51
73
 
55
77
  char *buffer;
56
78
  size_t buffer_size;
57
79
  size_t buffer_length;
 
80
  bool eof;
 
81
  bool completed;
 
82
  int status;
58
83
  struct process *next;
59
84
} process;
60
85
 
66
91
  struct plugin *next;
67
92
} plugin;
68
93
 
69
 
plugin *getplugin(char *name, plugin **plugin_list){
 
94
static plugin *getplugin(char *name, plugin **plugin_list){
70
95
  for (plugin *p = *plugin_list; p != NULL; p = p->next){
71
96
    if ((p->name == name)
72
97
        or (p->name and name and (strcmp(p->name, name) == 0))){
95
120
  return new_plugin;
96
121
}
97
122
 
98
 
void addargument(plugin *p, char *arg){
 
123
static void addargument(plugin *p, char *arg){
99
124
  p->argv[p->argc] = arg;
100
125
  p->argv = realloc(p->argv, sizeof(char *) * (size_t)(p->argc + 2));
101
126
  if (p->argv == NULL){
111
136
 * Descriptor Flags".
112
137
 * *Note File Descriptor Flags:(libc)Descriptor Flags.
113
138
 */
114
 
int set_cloexec_flag(int fd)
 
139
static int set_cloexec_flag(int fd)
115
140
{
116
141
  int ret = fcntl(fd, F_GETFD, 0);
117
142
  /* If reading the flags failed, return error indication now. */
122
147
  return fcntl(fd, F_SETFD, ret | FD_CLOEXEC);
123
148
}
124
149
 
125
 
 
126
 
#define BUFFER_SIZE 256
127
 
 
128
 
const char *argp_program_version =
129
 
  "plugbasedclient 0.9";
130
 
const char *argp_program_bug_address =
131
 
  "<mandos@fukt.bsnet.se>";
 
150
process *process_list = NULL;
 
151
 
 
152
/* Mark a process as completed when it exits, and save its exit
 
153
   status. */
 
154
void handle_sigchld(__attribute__((unused)) int sig){
 
155
  process *proc = process_list;
 
156
  int status;
 
157
  pid_t pid = wait(&status);
 
158
  if(pid == -1){
 
159
    perror("wait");
 
160
    return;
 
161
  }
 
162
  while(proc != NULL and proc->pid != pid){
 
163
    proc = proc->next;
 
164
  }
 
165
  if(proc == NULL){
 
166
    /* Process not found in process list */
 
167
    return;
 
168
  }
 
169
  proc->status = status;
 
170
  proc->completed = true;
 
171
}
 
172
 
 
173
bool print_out_password(const char *buffer, size_t length){
 
174
  ssize_t ret;
 
175
  if(length>0 and buffer[length-1] == '\n'){
 
176
    length--;
 
177
  }
 
178
  for(size_t written = 0; written < length; written += (size_t)ret){
 
179
    ret = TEMP_FAILURE_RETRY(write(STDOUT_FILENO, buffer + written,
 
180
                                   length - written));
 
181
    if(ret < 0){
 
182
      return false;
 
183
    }
 
184
  }
 
185
  return true;
 
186
}
132
187
 
133
188
int main(int argc, char *argv[]){
134
 
  const char *plugindir = "/conf/conf.d/mandos/plugins.d";
 
189
  const char *plugindir = "/lib/mandos/plugins.d";
135
190
  size_t d_name_len;
136
 
  DIR *dir;
 
191
  DIR *dir = NULL;
137
192
  struct dirent *dirst;
138
193
  struct stat st;
139
194
  fd_set rfds_all;
140
195
  int ret, maxfd = 0;
141
 
  process *process_list = NULL;
 
196
  uid_t uid = 65534;
 
197
  gid_t gid = 65534;
142
198
  bool debug = false;
143
199
  int exitstatus = EXIT_SUCCESS;
 
200
  struct sigaction old_sigchld_action;
 
201
  struct sigaction sigchld_action = { .sa_handler = handle_sigchld,
 
202
                                      .sa_flags = SA_NOCLDSTOP };
 
203
  char *plus_options = NULL;
 
204
  char **plus_argv = NULL;
 
205
 
 
206
  /* Establish a signal handler */
 
207
  sigemptyset(&sigchld_action.sa_mask);
 
208
  ret = sigaddset(&sigchld_action.sa_mask, SIGCHLD);
 
209
  if(ret < 0){
 
210
    perror("sigaddset");
 
211
    exit(EXIT_FAILURE);
 
212
  }
 
213
  ret = sigaction(SIGCHLD, &sigchld_action, &old_sigchld_action);
 
214
  if(ret < 0){
 
215
    perror("sigaction");
 
216
    exit(EXIT_FAILURE);
 
217
  }
144
218
  
145
219
  /* The options we understand. */
146
220
  struct argp_option options[] = {
156
230
    { .name = "plugin-dir", .key = 128,
157
231
      .arg = "DIRECTORY",
158
232
      .doc = "Specify a different plugin directory", .group = 2 },
159
 
    { .name = "debug", .key = 129,
 
233
    { .name = "userid", .key = 129,
 
234
      .arg = "ID", .flags = 0,
 
235
      .doc = "User ID the plugins will run as", .group = 2 },
 
236
    { .name = "groupid", .key = 130,
 
237
      .arg = "ID", .flags = 0,
 
238
      .doc = "Group ID the plugins will run as", .group = 2 },
 
239
    { .name = "debug", .key = 131,
160
240
      .doc = "Debug mode", .group = 3 },
161
241
    { .name = NULL }
162
242
  };
163
243
  
164
244
  error_t parse_opt (int key, char *arg, struct argp_state *state) {
165
 
       /* Get the INPUT argument from `argp_parse', which we
166
 
          know is a pointer to our plugin list pointer. */
 
245
    /* Get the INPUT argument from `argp_parse', which we know is a
 
246
       pointer to our plugin list pointer. */
167
247
    plugin **plugins = state->input;
168
248
    switch (key) {
169
249
    case 'g':
170
250
      if (arg != NULL){
171
 
        char *p = strtok(arg, ",");
172
 
        do{
 
251
        char *p;
 
252
        while((p = strsep(&arg, ",")) != NULL){
 
253
          if(strcmp(p, "") == 0){
 
254
            continue;
 
255
          }
173
256
          addargument(getplugin(NULL, plugins), p);
174
 
          p = strtok(NULL, ",");
175
 
        } while (p);
 
257
        }
176
258
      }
177
259
      break;
178
260
    case 'o':
179
261
      if (arg != NULL){
180
 
        char *name = strtok(arg, ":");
181
 
        char *p = strtok(NULL, ":");
182
 
        if(p){
183
 
          p = strtok(p, ",");
184
 
          do{
 
262
        char *name = strsep(&arg, ":");
 
263
        if(strcmp(name, "") == 0){
 
264
          break;
 
265
        }
 
266
        char *opt = strsep(&arg, ":");
 
267
        if(strcmp(opt, "") == 0){
 
268
          break;
 
269
        }
 
270
        if(opt != NULL){
 
271
          char *p;
 
272
          while((p = strsep(&opt, ",")) != NULL){
 
273
            if(strcmp(p, "") == 0){
 
274
              continue;
 
275
            }
185
276
            addargument(getplugin(name, plugins), p);
186
 
            p = strtok(NULL, ",");
187
 
          } while (p);
 
277
          }
188
278
        }
189
279
      }
190
280
      break;
197
287
      plugindir = arg;
198
288
      break;
199
289
    case 129:
 
290
      uid = (uid_t)strtol(arg, NULL, 10);
 
291
      break;
 
292
    case 130:
 
293
      gid = (gid_t)strtol(arg, NULL, 10);
 
294
      break;
 
295
    case 131:
200
296
      debug = true;
201
297
      break;
202
298
    case ARGP_KEY_ARG:
203
 
      argp_usage (state);
 
299
      if(plus_options != NULL or arg == NULL or arg[0] != '+'){
 
300
        argp_usage (state);
 
301
      }
 
302
      plus_options = arg;
204
303
      break;
205
304
    case ARGP_KEY_END:
206
305
      break;
213
312
  plugin *plugin_list = NULL;
214
313
  
215
314
  struct argp argp = { .options = options, .parser = parse_opt,
216
 
                       .args_doc = "",
 
315
                       .args_doc = "[+PLUS_SEPARATED_OPTIONS]",
217
316
                       .doc = "Mandos plugin runner -- Run plugins" };
218
317
  
219
 
  argp_parse (&argp, argc, argv, 0, 0, &plugin_list);
 
318
  ret = argp_parse (&argp, argc, argv, 0, 0, &plugin_list);
 
319
  if (ret == ARGP_ERR_UNKNOWN){
 
320
    fprintf(stderr, "Unknown error while parsing arguments\n");
 
321
    exitstatus = EXIT_FAILURE;
 
322
    goto end;
 
323
  }
 
324
  
 
325
  if(plus_options){
 
326
    /* This is a mangled argument in the form of
 
327
     "+--option+--other-option=parameter+--yet-another-option", etc */
 
328
    /* Make new argc and argv vars, and call argp_parse() again. */
 
329
    plus_options++;             /* skip the first '+' character */
 
330
    const char delims[] = "+";
 
331
    char *arg;
 
332
    int new_argc = 1;
 
333
    plus_argv = malloc(sizeof(char*) * 2);
 
334
    if(plus_argv == NULL){
 
335
      perror("malloc");
 
336
      exitstatus = EXIT_FAILURE;
 
337
      goto end;
 
338
    }
 
339
    plus_argv[0] = argv[0];
 
340
    plus_argv[1] = NULL;
 
341
 
 
342
    while((arg = strsep(&plus_options, delims)) != NULL){
 
343
      new_argc++;
 
344
      plus_argv = realloc(plus_argv, sizeof(char *)
 
345
                         * ((unsigned int) new_argc + 1));
 
346
      if(plus_argv == NULL){
 
347
        perror("realloc");
 
348
        exitstatus = EXIT_FAILURE;
 
349
        goto end;
 
350
      }
 
351
      plus_argv[new_argc-1] = arg;
 
352
      plus_argv[new_argc] = NULL;
 
353
    }
 
354
    
 
355
    ret = argp_parse (&argp, new_argc, plus_argv, 0, 0, &plugin_list);
 
356
    if (ret == ARGP_ERR_UNKNOWN){
 
357
      fprintf(stderr, "Unknown error while parsing arguments\n");
 
358
      exitstatus = EXIT_FAILURE;
 
359
      goto end;
 
360
    }
 
361
  }
220
362
  
221
363
  if(debug){
222
364
    for(plugin *p = plugin_list; p != NULL; p=p->next){
228
370
    }
229
371
  }
230
372
  
 
373
  ret = setuid(uid);
 
374
  if (ret == -1){
 
375
    perror("setuid");
 
376
  }
 
377
  
 
378
  setgid(gid);
 
379
  if (ret == -1){
 
380
    perror("setgid");
 
381
  }
 
382
  
231
383
  dir = opendir(plugindir);
232
 
  /* Set the FD_CLOEXEC flag on the directory */
233
 
  ret = set_cloexec_flag(dirfd(dir));
234
 
  if(ret < 0){
235
 
    perror("set_cloexec_flag");
236
 
    goto end;
237
 
  }
238
 
  
239
384
  if(dir == NULL){
240
 
    fprintf(stderr, "Can not open directory\n");
241
 
    return EXIT_FAILURE;
 
385
    perror("Could not open plugin dir");
 
386
    exitstatus = EXIT_FAILURE;
 
387
    goto end;
 
388
  }
 
389
  
 
390
  /* Set the FD_CLOEXEC flag on the directory, if possible */
 
391
  {
 
392
    int dir_fd = dirfd(dir);
 
393
    if(dir_fd >= 0){
 
394
      ret = set_cloexec_flag(dir_fd);
 
395
      if(ret < 0){
 
396
        perror("set_cloexec_flag");
 
397
        exitstatus = EXIT_FAILURE;
 
398
        goto end;
 
399
      }
 
400
    }
242
401
  }
243
402
  
244
403
  FD_ZERO(&rfds_all);
248
407
    
249
408
    // All directory entries have been processed
250
409
    if(dirst == NULL){
 
410
      if (errno == EBADF){
 
411
        perror("readdir");
 
412
        exitstatus = EXIT_FAILURE;
 
413
        goto end;
 
414
      }
251
415
      break;
252
416
    }
253
417
    
267
431
        if((d_name_len >= pre_len)
268
432
           and strncmp((dirst->d_name), *pre, pre_len) == 0){
269
433
          if(debug){
270
 
            fprintf(stderr, "Ignoring plugin dir entry name \"%s\""
 
434
            fprintf(stderr, "Ignoring plugin dir entry \"%s\""
271
435
                    " with bad prefix %s\n", dirst->d_name, *pre);
272
436
          }
273
437
          bad_name = true;
285
449
           and (strcmp((dirst->d_name)+d_name_len-suf_len, *suf)
286
450
                == 0)){
287
451
          if(debug){
288
 
            fprintf(stderr, "Ignoring plugin dir entry name \"%s\""
 
452
            fprintf(stderr, "Ignoring plugin dir entry \"%s\""
289
453
                    " with bad suffix %s\n", dirst->d_name, *suf);
290
454
          }
291
455
          bad_name = true;
301
465
    char *filename = malloc(d_name_len + strlen(plugindir) + 2);
302
466
    if (filename == NULL){
303
467
      perror("malloc");
304
 
      exitstatus =EXIT_FAILURE;
305
 
      goto end;
306
 
    }
307
 
    strcpy(filename, plugindir);
308
 
    strcat(filename, "/");
309
 
    strcat(filename, dirst->d_name);    
310
 
 
311
 
    stat(filename, &st);
312
 
 
 
468
      continue;
 
469
    }
 
470
    strcpy(filename, plugindir); /* Spurious warning */
 
471
    strcat(filename, "/");      /* Spurious warning */
 
472
    strcat(filename, dirst->d_name); /* Spurious warning */
 
473
    
 
474
    ret = stat(filename, &st);
 
475
    if (ret == -1){
 
476
      perror("stat");
 
477
      free(filename);
 
478
      continue;
 
479
    }
 
480
    
313
481
    if (not S_ISREG(st.st_mode) or (access(filename, X_OK) != 0)){
314
482
      if(debug){
315
 
        fprintf(stderr, "Ignoring plugin dir entry name \"%s\""
 
483
        fprintf(stderr, "Ignoring plugin dir entry \"%s\""
316
484
                " with bad type or mode\n", filename);
317
485
      }
 
486
      free(filename);
318
487
      continue;
319
488
    }
320
489
    if(getplugin(dirst->d_name, &plugin_list)->disabled){
321
490
      if(debug){
322
 
        fprintf(stderr, "Ignoring disabled plugin \"%s\"",
 
491
        fprintf(stderr, "Ignoring disabled plugin \"%s\"\n",
323
492
                dirst->d_name);
324
493
      }
 
494
      free(filename);
325
495
      continue;
326
496
    }
327
 
    // Starting a new process to be watched
328
 
    int pipefd[2]; 
329
 
    ret = pipe(pipefd);
330
 
    if (ret == -1){
331
 
      perror("pipe");
332
 
      goto end;
333
 
    }
334
497
    plugin *p = getplugin(dirst->d_name, &plugin_list);
335
498
    {
336
499
      /* Add global arguments to argument list for this plugin */
339
502
        addargument(p, *a);
340
503
      }
341
504
    }
 
505
    int pipefd[2]; 
 
506
    ret = pipe(pipefd);
 
507
    if (ret == -1){
 
508
      perror("pipe");
 
509
      exitstatus = EXIT_FAILURE;
 
510
      goto end;
 
511
    }
 
512
    ret = set_cloexec_flag(pipefd[0]);
 
513
    if(ret < 0){
 
514
      perror("set_cloexec_flag");
 
515
      exitstatus = EXIT_FAILURE;
 
516
      goto end;
 
517
    }
 
518
    ret = set_cloexec_flag(pipefd[1]);
 
519
    if(ret < 0){
 
520
      perror("set_cloexec_flag");
 
521
      exitstatus = EXIT_FAILURE;
 
522
      goto end;
 
523
    }
 
524
    /* Block SIGCHLD until process is safely in process list */
 
525
    ret = sigprocmask (SIG_BLOCK, &sigchld_action.sa_mask, NULL);
 
526
    if(ret < 0){
 
527
      perror("sigprocmask");
 
528
      exitstatus = EXIT_FAILURE;
 
529
      goto end;
 
530
    }
 
531
    // Starting a new process to be watched
342
532
    pid_t pid = fork();
 
533
    if(pid == -1){
 
534
      perror("fork");
 
535
      exitstatus = EXIT_FAILURE;
 
536
      goto end;
 
537
    }
343
538
    if(pid == 0){
344
539
      /* this is the child process */
345
 
      closedir(dir);
346
 
      close(pipefd[0]); /* close unused read end of pipe */
347
 
      dup2(pipefd[1], STDOUT_FILENO); /* replace our stdout */
348
 
      if(pipefd[1] > 2){
349
 
        close(pipefd[1]);
 
540
      ret = sigaction(SIGCHLD, &old_sigchld_action, NULL);
 
541
      if(ret < 0){
 
542
        perror("sigaction");
 
543
        _exit(EXIT_FAILURE);
 
544
      }
 
545
      ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
 
546
      if(ret < 0){
 
547
        perror("sigprocmask");
 
548
        _exit(EXIT_FAILURE);
 
549
      }
 
550
 
 
551
      ret = dup2(pipefd[1], STDOUT_FILENO); /* replace our stdout */
 
552
      if(ret == -1){
 
553
        perror("dup2");
 
554
        _exit(EXIT_FAILURE);
350
555
      }
351
556
      
 
557
      if(dirfd(dir) < 0){
 
558
        /* If dir has no file descriptor, we could not set FD_CLOEXEC
 
559
           above and must now close it manually here. */
 
560
        closedir(dir);
 
561
      }
352
562
      if(execv(filename, p->argv) < 0){
353
 
        perror(argv[0]);
354
 
        close(pipefd[1]);
 
563
        perror("execv");
355
564
        _exit(EXIT_FAILURE);
356
565
      }
357
566
      /* no return */
358
567
    }
 
568
    /* parent process */
 
569
    free(filename);
359
570
    close(pipefd[1]);           /* close unused write end of pipe */
360
571
    process *new_process = malloc(sizeof(process));
361
572
    if (new_process == NULL){
362
573
      perror("malloc");
363
 
      exitstatus = EXIT_FAILURE;
364
 
      goto end;
365
 
    }
366
 
    
367
 
    new_process->fd = pipefd[0];
368
 
    new_process->buffer = malloc(BUFFER_SIZE);
369
 
    if (new_process->buffer == NULL){
370
 
      perror("malloc");
371
 
      exitstatus = EXIT_FAILURE;
372
 
      goto end;
373
 
    }
374
 
    new_process->buffer_size = BUFFER_SIZE;
375
 
    new_process->buffer_length = 0;
 
574
      ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
 
575
      if(ret < 0){
 
576
        perror("sigprocmask");
 
577
      }
 
578
      exitstatus = EXIT_FAILURE;
 
579
      goto end;
 
580
    }
 
581
    
 
582
    *new_process = (struct process){ .pid = pid,
 
583
                                     .fd = pipefd[0],
 
584
                                     .next = process_list };
 
585
    // List handling
 
586
    process_list = new_process;
 
587
    /* Unblock SIGCHLD so signal handler can be run if this process
 
588
       has already completed */
 
589
    ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
 
590
    if(ret < 0){
 
591
      perror("sigprocmask");
 
592
      exitstatus = EXIT_FAILURE;
 
593
      goto end;
 
594
    }
 
595
    
376
596
    FD_SET(new_process->fd, &rfds_all);
377
 
      
 
597
    
378
598
    if (maxfd < new_process->fd){
379
599
      maxfd = new_process->fd;
380
600
    }
381
601
    
382
 
    //List handling
383
 
    new_process->next = process_list;
384
 
    process_list = new_process;
 
602
  }
 
603
  
 
604
  /* Free the plugin list */
 
605
  for(plugin *next; plugin_list != NULL; plugin_list = next){
 
606
    next = plugin_list->next;
 
607
    free(plugin_list->argv);
 
608
    free(plugin_list);
385
609
  }
386
610
  
387
611
  closedir(dir);
388
 
  
389
 
  if (process_list != NULL){
390
 
    while(true){
391
 
      fd_set rfds = rfds_all;
392
 
      int select_ret = select(maxfd+1, &rfds, NULL, NULL, NULL);
393
 
      if (select_ret == -1){
394
 
        perror(argv[0]);
395
 
        goto end;
396
 
      }else{    
397
 
        for(process *process_itr = process_list; process_itr != NULL;
398
 
            process_itr = process_itr->next){
399
 
          if(FD_ISSET(process_itr->fd, &rfds)){
400
 
            if(process_itr->buffer_length + BUFFER_SIZE
401
 
               > process_itr->buffer_size){
402
 
                process_itr->buffer = realloc(process_itr->buffer,
403
 
                                              process_itr->buffer_size
404
 
                                              + (size_t) BUFFER_SIZE);
405
 
                if (process_itr->buffer == NULL){
406
 
                  perror(argv[0]);
407
 
                  goto end;
408
 
                }
409
 
                process_itr->buffer_size += BUFFER_SIZE;
410
 
            }
411
 
            ret = read(process_itr->fd, process_itr->buffer
412
 
                       + process_itr->buffer_length, BUFFER_SIZE);
413
 
            if(ret < 0){
414
 
              /* Read error from this process; ignore it */
415
 
              continue;
416
 
            }
417
 
            process_itr->buffer_length += (size_t) ret;
418
 
            if(ret == 0){
419
 
              /* got EOF */
420
 
              /* wait for process exit */
421
 
              int status;
422
 
              waitpid(process_itr->pid, &status, 0);
423
 
              if(WIFEXITED(status) and WEXITSTATUS(status) == 0){
424
 
                for(size_t written = 0;
425
 
                    written < process_itr->buffer_length;){
426
 
                  ret = write(STDOUT_FILENO,
427
 
                              process_itr->buffer + written,
428
 
                              process_itr->buffer_length - written);
429
 
                  if(ret < 0){
430
 
                    perror(argv[0]);
431
 
                    goto end;
432
 
                  }
433
 
                  written += (size_t)ret;
434
 
                }
435
 
                goto end;
436
 
              } else {
437
 
                FD_CLR(process_itr->fd, &rfds_all);
 
612
  dir = NULL;
 
613
    
 
614
  if (process_list == NULL){
 
615
    fprintf(stderr, "No plugin processes started. Incorrect plugin"
 
616
            " directory?\n");
 
617
    process_list = NULL;
 
618
  }
 
619
  while(process_list){
 
620
    fd_set rfds = rfds_all;
 
621
    int select_ret = select(maxfd+1, &rfds, NULL, NULL, NULL);
 
622
    if (select_ret == -1){
 
623
      perror("select");
 
624
      exitstatus = EXIT_FAILURE;
 
625
      goto end;
 
626
    }
 
627
    /* OK, now either a process completed, or something can be read
 
628
       from one of them */
 
629
    for(process *proc = process_list; proc ; proc = proc->next){
 
630
      /* Is this process completely done? */
 
631
      if(proc->eof and proc->completed){
 
632
        /* Only accept the plugin output if it exited cleanly */
 
633
        if(not WIFEXITED(proc->status)
 
634
           or WEXITSTATUS(proc->status) != 0){
 
635
          /* Bad exit by plugin */
 
636
          if(debug){
 
637
            if(WIFEXITED(proc->status)){
 
638
              fprintf(stderr, "Plugin %u exited with status %d\n",
 
639
                      (unsigned int) (proc->pid),
 
640
                      WEXITSTATUS(proc->status));
 
641
            } else if(WIFSIGNALED(proc->status)) {
 
642
              fprintf(stderr, "Plugin %u killed by signal %d\n",
 
643
                      (unsigned int) (proc->pid),
 
644
                      WTERMSIG(proc->status));
 
645
            } else if(WCOREDUMP(proc->status)){
 
646
              fprintf(stderr, "Plugin %d dumped core\n",
 
647
                      (unsigned int) (proc->pid));
 
648
            }
 
649
          }
 
650
          /* Remove the plugin */
 
651
          FD_CLR(proc->fd, &rfds_all);
 
652
          /* Block signal while modifying process_list */
 
653
          ret = sigprocmask (SIG_BLOCK, &sigchld_action.sa_mask, NULL);
 
654
          if(ret < 0){
 
655
            perror("sigprocmask");
 
656
            exitstatus = EXIT_FAILURE;
 
657
            goto end;
 
658
          }
 
659
          /* Delete this process entry from the list */
 
660
          if(process_list == proc){
 
661
            /* First one - simple */
 
662
            process_list = proc->next;
 
663
          } else {
 
664
            /* Second one or later */
 
665
            for(process *p = process_list; p != NULL; p = p->next){
 
666
              if(p->next == proc){
 
667
                p->next = proc->next;
 
668
                break;
438
669
              }
439
670
            }
440
671
          }
441
 
        }
 
672
          /* We are done modifying process list, so unblock signal */
 
673
          ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask,
 
674
                             NULL);
 
675
          if(ret < 0){
 
676
            perror("sigprocmask");
 
677
          }
 
678
          free(proc->buffer);
 
679
          free(proc);
 
680
          /* We deleted this process from the list, so we can't go
 
681
             proc->next.  Therefore, start over from the beginning of
 
682
             the process list */
 
683
          break;
 
684
        }
 
685
        /* This process exited nicely, so print its buffer */
 
686
 
 
687
        bool bret = print_out_password(proc->buffer, proc->buffer_length);
 
688
        if(not bret){
 
689
          perror("print_out_password");
 
690
          exitstatus = EXIT_FAILURE;
 
691
        }
 
692
        goto end;
 
693
      }
 
694
      /* This process has not completed.  Does it have any output? */
 
695
      if(proc->eof or not FD_ISSET(proc->fd, &rfds)){
 
696
        /* This process had nothing to say at this time */
 
697
        continue;
 
698
      }
 
699
      /* Before reading, make the process' data buffer large enough */
 
700
      if(proc->buffer_length + BUFFER_SIZE > proc->buffer_size){
 
701
        proc->buffer = realloc(proc->buffer, proc->buffer_size
 
702
                               + (size_t) BUFFER_SIZE);
 
703
        if (proc->buffer == NULL){
 
704
          perror("malloc");
 
705
          exitstatus = EXIT_FAILURE;
 
706
          goto end;
 
707
        }
 
708
        proc->buffer_size += BUFFER_SIZE;
 
709
      }
 
710
      /* Read from the process */
 
711
      ret = read(proc->fd, proc->buffer + proc->buffer_length,
 
712
                 BUFFER_SIZE);
 
713
      if(ret < 0){
 
714
        /* Read error from this process; ignore the error */
 
715
        continue;
 
716
      }
 
717
      if(ret == 0){
 
718
        /* got EOF */
 
719
        proc->eof = true;
 
720
      } else {
 
721
        proc->buffer_length += (size_t) ret;
442
722
      }
443
723
    }
444
724
  }
445
 
  
 
725
 
 
726
 
446
727
 end:
447
 
  for(process *process_itr = process_list; process_itr != NULL;
448
 
      process_itr = process_itr->next){
449
 
    close(process_itr->fd);
450
 
    kill(process_itr->pid, SIGTERM);
451
 
    free(process_itr->buffer);
452
 
  }
453
 
  
454
 
  while(true){
455
 
    int status;
456
 
    ret = wait(&status);
457
 
    if (ret == -1){
458
 
      if(errno != ECHILD){
459
 
        perror("wait");
460
 
      }
461
 
      break;
462
 
    }
463
 
  }  
 
728
  
 
729
  if(process_list == NULL or exitstatus != EXIT_SUCCESS){
 
730
    /* Fallback if all plugins failed, none are found or an error occured */
 
731
    bool bret;
 
732
    fprintf(stderr, "Going to fallback mode using getpass(3)\n");
 
733
    char *passwordbuffer = getpass("Password: ");
 
734
    bret = print_out_password(passwordbuffer, strlen(passwordbuffer));
 
735
    if(not bret){
 
736
      perror("print_out_password");
 
737
      exitstatus = EXIT_FAILURE;
 
738
      goto end;
 
739
    }
 
740
  }
 
741
  
 
742
  /* Restore old signal handler */
 
743
  sigaction(SIGCHLD, &old_sigchld_action, NULL);
 
744
  
 
745
  free(plus_argv);
 
746
  
 
747
  /* Free the plugin list */
 
748
  for(plugin *next; plugin_list != NULL; plugin_list = next){
 
749
    next = plugin_list->next;
 
750
    free(plugin_list->argv);
 
751
    free(plugin_list);
 
752
  }
 
753
  
 
754
  if(dir != NULL){
 
755
    closedir(dir);
 
756
  }
 
757
  
 
758
  /* Free the process list and kill the processes */
 
759
  for(process *next; process_list != NULL; process_list = next){
 
760
    next = process_list->next;
 
761
    close(process_list->fd);
 
762
    ret = kill(process_list->pid, SIGTERM);
 
763
    if(ret == -1 and errno != ESRCH){
 
764
      /* set-uid proccesses migth not get closed */
 
765
      perror("kill");
 
766
    }
 
767
    free(process_list->buffer);
 
768
    free(process_list);
 
769
  }
 
770
  
 
771
  /* Wait for any remaining child processes to terminate */
 
772
  do{
 
773
    ret = wait(NULL);
 
774
  } while(ret >= 0);
 
775
  if(errno != ECHILD){
 
776
    perror("wait");
 
777
  }
 
778
  
464
779
  return exitstatus;
465
780
}