/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 mandos-client.c

  • Committer: Björn Påhlsson
  • Date: 2007-10-20 21:38:25 UTC
  • Revision ID: belorn@tower-20071020213825-abf6f0d1c33ee961
First working version with: IPv6, GnuTLS, X.509 certificates, DN
retrieval.

Show diffs side-by-side

added added

removed removed

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