/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: Teddy Hogeborn
  • Date: 2008-08-10 20:35:01 UTC
  • mfrom: (24.1.42 mandos)
  • Revision ID: teddy@fukt.bsnet.se-20080810203501-euh3qcf1nopilksm
Merge.

Show diffs side-by-side

added added

removed removed

Lines of Context:
21
21
 * Contact the authors at <mandos@fukt.bsnet.se>.
22
22
 */
23
23
 
24
 
#define _GNU_SOURCE             /* TEMP_FAILURE_RETRY(), getline(),
25
 
                                   asprintf() */
 
24
#define _GNU_SOURCE             /* TEMP_FAILURE_RETRY() */
 
25
 
26
26
#include <stddef.h>             /* size_t, NULL */
27
27
#include <stdlib.h>             /* malloc(), exit(), EXIT_FAILURE,
28
28
                                   EXIT_SUCCESS, realloc() */
51
51
                                   close() */
52
52
#include <fcntl.h>              /* fcntl(), F_GETFD, F_SETFD,
53
53
                                   FD_CLOEXEC */
54
 
#include <string.h>             /* strsep, strlen(), asprintf() */
 
54
#include <string.h>             /* strtok, strlen(), strcpy(),
 
55
                                   strcat() */
55
56
#include <errno.h>              /* errno */
56
57
#include <argp.h>               /* struct argp_option, struct
57
58
                                   argp_state, struct argp,
58
59
                                   argp_parse(), ARGP_ERR_UNKNOWN,
59
 
                                   ARGP_KEY_END, ARGP_KEY_ARG,
60
 
                                   error_t */
 
60
                                   ARGP_KEY_END, ARGP_KEY_ARG, error_t */
61
61
#include <signal.h>             /* struct sigaction, sigemptyset(),
62
62
                                   sigaddset(), sigaction(),
63
63
                                   sigprocmask(), SIG_BLOCK, SIGCHLD,
65
65
#include <errno.h>              /* errno, EBADF */
66
66
 
67
67
#define BUFFER_SIZE 256
68
 
#define ARGFILE "/conf/conf.d/mandos/plugin-runner.conf"
69
68
 
70
 
const char *argp_program_version = "plugin-runner 1.0";
 
69
const char *argp_program_version = "mandos-client 1.0";
71
70
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
72
71
 
73
72
struct process;
79
78
  size_t buffer_size;
80
79
  size_t buffer_length;
81
80
  bool eof;
82
 
  volatile bool completed;
83
 
  volatile int status;
 
81
  bool completed;
 
82
  int status;
84
83
  struct process *next;
85
84
} process;
86
85
 
88
87
  char *name;                   /* can be NULL or any plugin name */
89
88
  char **argv;
90
89
  int argc;
91
 
  char **environ;
92
 
  int envc;
93
90
  bool disabled;
94
91
  struct plugin *next;
95
92
} plugin;
104
101
  /* Create a new plugin */
105
102
  plugin *new_plugin = malloc(sizeof(plugin));
106
103
  if (new_plugin == NULL){
107
 
    return NULL;
108
 
  }
109
 
  char *copy_name = NULL;
110
 
  if(name != NULL){
111
 
    copy_name = strdup(name);
112
 
    if(copy_name == NULL){
113
 
      return NULL;
114
 
    }
115
 
  }
116
 
  
117
 
  *new_plugin = (plugin) { .name = copy_name,
118
 
                           .argc = 1,
119
 
                           .envc = 0,
120
 
                           .disabled = false,
121
 
                           .next = *plugin_list };
122
 
  
 
104
    perror("malloc");
 
105
    exit(EXIT_FAILURE);
 
106
  }
 
107
  new_plugin->name = name;
123
108
  new_plugin->argv = malloc(sizeof(char *) * 2);
124
109
  if (new_plugin->argv == NULL){
125
 
    free(copy_name);
126
 
    free(new_plugin);
127
 
    return NULL;
 
110
    perror("malloc");
 
111
    exit(EXIT_FAILURE);
128
112
  }
129
 
  new_plugin->argv[0] = copy_name;
 
113
  new_plugin->argv[0] = name;
130
114
  new_plugin->argv[1] = NULL;
131
 
 
132
 
  new_plugin->environ = malloc(sizeof(char *));
133
 
  if(new_plugin->environ == NULL){
134
 
    free(copy_name);
135
 
    free(new_plugin->argv);
136
 
    free(new_plugin);
137
 
    return NULL;
138
 
  }
139
 
  new_plugin->environ[0] = NULL;
 
115
  new_plugin->argc = 1;
 
116
  new_plugin->disabled = false;
 
117
  new_plugin->next = *plugin_list;
140
118
  /* Append the new plugin to the list */
141
119
  *plugin_list = new_plugin;
142
120
  return new_plugin;
143
121
}
144
122
 
145
 
/* Helper function for add_argument and add_environment */
146
 
static bool add_to_char_array(const char *new, char ***array,
147
 
                              int *len){
148
 
  /* Resize the pointed-to array to hold one more pointer */
149
 
  *array = realloc(*array, sizeof(char *)
150
 
                   * (size_t) ((*len) + 2));
151
 
  /* Malloc check */
152
 
  if(*array == NULL){
153
 
    return false;
154
 
  }
155
 
  /* Make a copy of the new string */
156
 
  char *copy = strdup(new);
157
 
  if(copy == NULL){
158
 
    return false;
159
 
  }
160
 
  /* Insert the copy */
161
 
  (*array)[*len] = copy;
162
 
  (*len)++;
163
 
  /* Add a new terminating NULL pointer to the last element */
164
 
  (*array)[*len] = NULL;
165
 
  return true;
166
 
}
167
 
 
168
 
/* Add to a plugin's argument vector */
169
 
static bool add_argument(plugin *p, const char *arg){
170
 
  if(p == NULL){
171
 
    return false;
172
 
  }
173
 
  return add_to_char_array(arg, &(p->argv), &(p->argc));
174
 
}
175
 
 
176
 
/* Add to a plugin's environment */
177
 
static bool add_environment(plugin *p, const char *def){
178
 
  if(p == NULL){
179
 
    return false;
180
 
  }
181
 
  return add_to_char_array(def, &(p->environ), &(p->envc));
182
 
}
183
 
 
 
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
}
184
133
 
185
134
/*
186
135
 * Based on the example in the GNU LibC manual chapter 13.13 "File
200
149
 
201
150
process *process_list = NULL;
202
151
 
203
 
/* Mark processes as completed when they exit, and save their exit
 
152
/* Mark a process as completed when it exits, and save its exit
204
153
   status. */
205
154
void handle_sigchld(__attribute__((unused)) int sig){
206
 
  while(true){
207
 
    process *proc = process_list;
208
 
    int status;
209
 
    pid_t pid = waitpid(-1, &status, WNOHANG);
210
 
    if(pid == 0){
211
 
      /* Only still running child processes */
212
 
      break;
213
 
    }
214
 
    if(pid == -1){
215
 
      if (errno != ECHILD){
216
 
        perror("waitpid");
217
 
      }
218
 
      /* No child processes */
219
 
      break;
220
 
    }
221
 
 
222
 
    /* A child exited, find it in process_list */
223
 
    while(proc != NULL and proc->pid != pid){
224
 
      proc = proc->next;
225
 
    }
226
 
    if(proc == NULL){
227
 
      /* Process not found in process list */
228
 
      continue;
229
 
    }
230
 
    proc->status = status;
231
 
    proc->completed = true;
232
 
  }
 
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;
233
171
}
234
172
 
235
173
bool print_out_password(const char *buffer, size_t length){
236
 
  ssize_t ret;
237
 
  if(length>0 and buffer[length-1] == '\n'){
238
 
    length--;
239
 
  }
240
 
  for(size_t written = 0; written < length; written += (size_t)ret){
 
174
  size_t ret;
 
175
  for(size_t written = 0; written < length; written += ret){
241
176
    ret = TEMP_FAILURE_RETRY(write(STDOUT_FILENO, buffer + written,
242
177
                                   length - written));
243
178
    if(ret < 0){
247
182
  return true;
248
183
}
249
184
 
250
 
char **add_to_argv(char **argv, int *argc, char *arg){
251
 
  if (argv == NULL){
252
 
    *argc = 1;
253
 
    argv = malloc(sizeof(char*) * 2);
254
 
    if(argv == NULL){
255
 
      return NULL;
256
 
    }
257
 
    argv[0] = NULL;     /* Will be set to argv[0] in main before
258
 
                           parsing */
259
 
    argv[1] = NULL;
260
 
  }
261
 
  *argc += 1;
262
 
  argv = realloc(argv, sizeof(char *)
263
 
                  * ((unsigned int) *argc + 1));
264
 
  if(argv == NULL){
265
 
    return NULL;
266
 
  }
267
 
  argv[*argc-1] = arg;
268
 
  argv[*argc] = NULL;
269
 
  return argv;
270
 
}
271
 
 
272
 
static void free_plugin_list(plugin *plugin_list){
273
 
  for(plugin *next; plugin_list != NULL; plugin_list = next){
274
 
    next = plugin_list->next;
275
 
    for(char **arg = plugin_list->argv; *arg != NULL; arg++){
276
 
      free(*arg);
277
 
    }
278
 
    free(plugin_list->argv);
279
 
    for(char **env = plugin_list->environ; *env != NULL; env++){
280
 
      free(*env);
281
 
    }
282
 
    free(plugin_list->environ);
283
 
    free(plugin_list);
284
 
  }
285
 
}
286
 
 
287
185
int main(int argc, char *argv[]){
288
 
  const char *plugindir = "/lib/mandos/plugins.d";
289
 
  const char *argfile = ARGFILE;
290
 
  FILE *conffp;
 
186
  const char *plugindir = "/conf/conf.d/mandos/plugins.d";
291
187
  size_t d_name_len;
292
188
  DIR *dir = NULL;
293
189
  struct dirent *dirst;
301
197
  struct sigaction old_sigchld_action;
302
198
  struct sigaction sigchld_action = { .sa_handler = handle_sigchld,
303
199
                                      .sa_flags = SA_NOCLDSTOP };
304
 
  char **custom_argv = NULL;
305
 
  int custom_argc = 0;
 
200
  char *plus_options = NULL;
 
201
  char **plus_argv = NULL;
306
202
  
307
203
  /* Establish a signal handler */
308
204
  sigemptyset(&sigchld_action.sa_mask);
309
205
  ret = sigaddset(&sigchld_action.sa_mask, SIGCHLD);
310
 
  if(ret == -1){
 
206
  if(ret < 0){
311
207
    perror("sigaddset");
312
 
    exitstatus = EXIT_FAILURE;
313
 
    goto fallback;
 
208
    exit(EXIT_FAILURE);
314
209
  }
315
210
  ret = sigaction(SIGCHLD, &sigchld_action, &old_sigchld_action);
316
 
  if(ret == -1){
 
211
  if(ret < 0){
317
212
    perror("sigaction");
318
 
    exitstatus = EXIT_FAILURE;
319
 
    goto fallback;
 
213
    exit(EXIT_FAILURE);
320
214
  }
321
215
  
322
216
  /* The options we understand. */
324
218
    { .name = "global-options", .key = 'g',
325
219
      .arg = "OPTION[,OPTION[,...]]",
326
220
      .doc = "Options passed to all plugins" },
327
 
    { .name = "global-envs", .key = 'e',
328
 
      .arg = "VAR=value",
329
 
      .doc = "Environment variable passed to all plugins" },
330
221
    { .name = "options-for", .key = 'o',
331
222
      .arg = "PLUGIN:OPTION[,OPTION[,...]]",
332
223
      .doc = "Options passed only to specified plugin" },
333
 
    { .name = "envs-for", .key = 'f',
334
 
      .arg = "PLUGIN:ENV=value",
335
 
      .doc = "Environment variable passed to specified plugin" },
336
224
    { .name = "disable", .key = 'd',
337
225
      .arg = "PLUGIN",
338
226
      .doc = "Disable a specific plugin", .group = 1 },
357
245
    switch (key) {
358
246
    case 'g':
359
247
      if (arg != NULL){
360
 
        char *p;
361
 
        while((p = strsep(&arg, ",")) != NULL){
362
 
          if(p[0] == '\0'){
363
 
            continue;
364
 
          }
365
 
          if(not add_argument(getplugin(NULL, plugins), p)){
366
 
            perror("add_argument");
367
 
            return ARGP_ERR_UNKNOWN;
368
 
          }
369
 
        }
370
 
      }
371
 
      break;
372
 
    case 'e':
373
 
      if(arg == NULL){
374
 
        break;
375
 
      }
376
 
      {
377
 
        char *envdef = strdup(arg);
378
 
        if(envdef == NULL){
379
 
          break;
380
 
        }
381
 
        if(not add_environment(getplugin(NULL, plugins), envdef)){
382
 
          perror("add_environment");
383
 
        }
 
248
        char *p = strtok(arg, ",");
 
249
        do{
 
250
          addargument(getplugin(NULL, plugins), p);
 
251
          p = strtok(NULL, ",");
 
252
        } while (p != NULL);
384
253
      }
385
254
      break;
386
255
    case 'o':
387
256
      if (arg != NULL){
388
 
        char *p_name = strsep(&arg, ":");
389
 
        if(p_name[0] == '\0'){
390
 
          break;
391
 
        }
392
 
        char *opt = strsep(&arg, ":");
393
 
        if(opt[0] == '\0'){
394
 
          break;
395
 
        }
396
 
        if(opt != NULL){
397
 
          char *p;
398
 
          while((p = strsep(&opt, ",")) != NULL){
399
 
            if(p[0] == '\0'){
400
 
              continue;
401
 
            }
402
 
            if(not add_argument(getplugin(p_name, plugins), p)){
403
 
              perror("add_argument");
404
 
              return ARGP_ERR_UNKNOWN;
405
 
            }
406
 
          }
407
 
        }
408
 
      }
409
 
      break;
410
 
    case 'f':
411
 
      if(arg == NULL){
412
 
        break;
413
 
      }
414
 
      {
415
 
        char *envdef = strchr(arg, ':');
416
 
        if(envdef == NULL){
417
 
          break;
418
 
        }
419
 
        char *p_name = strndup(arg, (size_t) (envdef-arg));
420
 
        if(p_name == NULL){
421
 
          break;
422
 
        }
423
 
        envdef++;
424
 
        if(not add_environment(getplugin(p_name, plugins), envdef)){
425
 
          perror("add_environment");
 
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);
426
265
        }
427
266
      }
428
267
      break;
429
268
    case 'd':
430
269
      if (arg != NULL){
431
 
        plugin *p = getplugin(arg, plugins);
432
 
        if(p == NULL){
433
 
          return ARGP_ERR_UNKNOWN;
434
 
        }
435
 
        p->disabled = true;
 
270
        getplugin(arg, plugins)->disabled = true;
436
271
      }
437
272
      break;
438
273
    case 128:
448
283
      debug = true;
449
284
      break;
450
285
    case ARGP_KEY_ARG:
451
 
      fprintf(stderr, "Ignoring unknown argument \"%s\"\n", arg);
 
286
      if(plus_options != NULL or arg == NULL or arg[0] != '+'){
 
287
        argp_usage (state);
 
288
      }
 
289
      plus_options = arg;
452
290
      break;
453
291
    case ARGP_KEY_END:
454
292
      break;
466
304
  
467
305
  ret = argp_parse (&argp, argc, argv, 0, 0, &plugin_list);
468
306
  if (ret == ARGP_ERR_UNKNOWN){
469
 
    fprintf(stderr, "Unknown error while parsing arguments\n");
 
307
    fprintf(stderr, "Unkown error while parsing arguments\n");
470
308
    exitstatus = EXIT_FAILURE;
471
 
    goto fallback;
 
309
    goto end;
472
310
  }
473
 
 
474
 
  conffp = fopen(argfile, "r");
475
 
  if(conffp != NULL){
476
 
    char *org_line = NULL;
477
 
    char *p, *arg, *new_arg, *line;
478
 
    size_t size = 0;
479
 
    ssize_t sret;
480
 
    const char whitespace_delims[] = " \r\t\f\v\n";
481
 
    const char comment_delim[] = "#";
482
 
 
483
 
    while(true){
484
 
      sret = getline(&org_line, &size, conffp);
485
 
      if(sret == -1){
486
 
        break;
487
 
      }
488
 
 
489
 
      line = org_line;
490
 
      arg = strsep(&line, comment_delim);
491
 
      while((p = strsep(&arg, whitespace_delims)) != NULL){
492
 
        if(p[0] == '\0'){
493
 
          continue;
494
 
        }
495
 
        new_arg = strdup(p);
496
 
        custom_argv = add_to_argv(custom_argv, &custom_argc, new_arg);
497
 
        if (custom_argv == NULL){
498
 
          perror("add_to_argv");
499
 
          exitstatus = EXIT_FAILURE;
500
 
          goto fallback;
501
 
        }
502
 
      }
503
 
    }
504
 
    free(org_line);
505
 
  } else{
506
 
    /* Check for harmful errors and go to fallback. Other errors might
507
 
       not affect opening plugins */
508
 
    if (errno == EMFILE or errno == ENFILE or errno == ENOMEM){
509
 
      perror("fopen");
 
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");
510
323
      exitstatus = EXIT_FAILURE;
511
 
      goto fallback;
512
 
    }
513
 
  }
514
 
 
515
 
  if(custom_argv != NULL){
516
 
    custom_argv[0] = argv[0];
517
 
    ret = argp_parse (&argp, custom_argc, custom_argv, 0, 0,
518
 
                      &plugin_list);
 
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);
519
343
    if (ret == ARGP_ERR_UNKNOWN){
520
 
      fprintf(stderr, "Unknown error while parsing arguments\n");
 
344
      fprintf(stderr, "Unkown error while parsing arguments\n");
521
345
      exitstatus = EXIT_FAILURE;
522
 
      goto fallback;
 
346
      goto end;
523
347
    }
524
348
  }
525
349
  
530
354
      for(char **a = p->argv; *a != NULL; a++){
531
355
        fprintf(stderr, "\tArg: %s\n", *a);
532
356
      }
533
 
      fprintf(stderr, "...and %u environment variables\n", p->envc);
534
 
      for(char **a = p->environ; *a != NULL; a++){
535
 
        fprintf(stderr, "\t%s\n", *a);
536
 
      }
537
357
    }
538
358
  }
539
359
  
551
371
  if(dir == NULL){
552
372
    perror("Could not open plugin dir");
553
373
    exitstatus = EXIT_FAILURE;
554
 
    goto fallback;
 
374
    goto end;
555
375
  }
556
376
  
557
377
  /* Set the FD_CLOEXEC flag on the directory, if possible */
562
382
      if(ret < 0){
563
383
        perror("set_cloexec_flag");
564
384
        exitstatus = EXIT_FAILURE;
565
 
        goto fallback;
 
385
        goto end;
566
386
      }
567
387
    }
568
388
  }
577
397
      if (errno == EBADF){
578
398
        perror("readdir");
579
399
        exitstatus = EXIT_FAILURE;
580
 
        goto fallback;
 
400
        goto end;
581
401
      }
582
402
      break;
583
403
    }
628
448
        continue;
629
449
      }
630
450
    }
631
 
 
632
 
    char *filename;
633
 
    ret = asprintf(&filename, "%s/%s", plugindir, dirst->d_name);
634
 
    if(ret < 0){
635
 
      perror("asprintf");
636
 
      continue;
 
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;
637
457
    }
 
458
    strcpy(filename, plugindir); /* Spurious warning */
 
459
    strcat(filename, "/");      /* Spurious warning */
 
460
    strcat(filename, dirst->d_name); /* Spurious warning */
638
461
    
639
462
    ret = stat(filename, &st);
640
463
    if (ret == -1){
641
464
      perror("stat");
642
 
      free(filename);
643
 
      continue;
 
465
      exitstatus = EXIT_FAILURE;
 
466
      goto end;
644
467
    }
645
468
    
646
469
    if (not S_ISREG(st.st_mode) or (access(filename, X_OK) != 0)){
651
474
      free(filename);
652
475
      continue;
653
476
    }
654
 
    plugin *p = getplugin(dirst->d_name, &plugin_list);
655
 
    if(p == NULL){
656
 
      perror("getplugin");
657
 
      free(filename);
658
 
      continue;
659
 
    }
660
 
    if(p->disabled){
 
477
    if(getplugin(dirst->d_name, &plugin_list)->disabled){
661
478
      if(debug){
662
479
        fprintf(stderr, "Ignoring disabled plugin \"%s\"\n",
663
480
                dirst->d_name);
665
482
      free(filename);
666
483
      continue;
667
484
    }
 
485
    plugin *p = getplugin(dirst->d_name, &plugin_list);
668
486
    {
669
487
      /* Add global arguments to argument list for this plugin */
670
488
      plugin *g = getplugin(NULL, &plugin_list);
671
 
      if(g != NULL){
672
 
        for(char **a = g->argv + 1; *a != NULL; a++){
673
 
          if(not add_argument(p, *a)){
674
 
            perror("add_argument");
675
 
          }
676
 
        }
677
 
        /* Add global environment variables */
678
 
        for(char **e = g->environ; *e != NULL; e++){
679
 
          if(not add_environment(p, *e)){
680
 
            perror("add_environment");
681
 
          }
682
 
        }
683
 
      }
684
 
    }
685
 
    /* If this plugin has any environment variables, we will call
686
 
       using execve and need to duplicate the environment from this
687
 
       process, too. */
688
 
    if(p->environ[0] != NULL){
689
 
      for(char **e = environ; *e != NULL; e++){
690
 
        char *copy = strdup(*e);
691
 
        if(copy == NULL){
692
 
          perror("strdup");
693
 
          continue;
694
 
        }
695
 
        if(not add_environment(p, copy)){
696
 
          perror("add_environment");
697
 
        }
698
 
      }
699
 
    }
700
 
    
701
 
    int pipefd[2];
 
489
      for(char **a = g->argv + 1; *a != NULL; a++){
 
490
        addargument(p, *a);
 
491
      }
 
492
    }
 
493
    int pipefd[2]; 
702
494
    ret = pipe(pipefd);
703
495
    if (ret == -1){
704
496
      perror("pipe");
705
497
      exitstatus = EXIT_FAILURE;
706
 
      goto fallback;
 
498
      goto end;
707
499
    }
708
500
    ret = set_cloexec_flag(pipefd[0]);
709
501
    if(ret < 0){
710
502
      perror("set_cloexec_flag");
711
503
      exitstatus = EXIT_FAILURE;
712
 
      goto fallback;
 
504
      goto end;
713
505
    }
714
506
    ret = set_cloexec_flag(pipefd[1]);
715
507
    if(ret < 0){
716
508
      perror("set_cloexec_flag");
717
509
      exitstatus = EXIT_FAILURE;
718
 
      goto fallback;
 
510
      goto end;
719
511
    }
720
512
    /* Block SIGCHLD until process is safely in process list */
721
513
    ret = sigprocmask (SIG_BLOCK, &sigchld_action.sa_mask, NULL);
722
514
    if(ret < 0){
723
515
      perror("sigprocmask");
724
516
      exitstatus = EXIT_FAILURE;
725
 
      goto fallback;
 
517
      goto end;
726
518
    }
727
519
    // Starting a new process to be watched
728
520
    pid_t pid = fork();
729
 
    if(pid == -1){
730
 
      perror("fork");
731
 
      exitstatus = EXIT_FAILURE;
732
 
      goto fallback;
733
 
    }
734
521
    if(pid == 0){
735
522
      /* this is the child process */
736
523
      ret = sigaction(SIGCHLD, &old_sigchld_action, NULL);
755
542
           above and must now close it manually here. */
756
543
        closedir(dir);
757
544
      }
758
 
      if(p->environ[0] == NULL){
759
 
        if(execv(filename, p->argv) < 0){
760
 
          perror("execv");
761
 
          _exit(EXIT_FAILURE);
762
 
        }
763
 
      } else {
764
 
        if(execve(filename, p->argv, p->environ) < 0){
765
 
          perror("execve");
766
 
          _exit(EXIT_FAILURE);
767
 
        }
 
545
      if(execv(filename, p->argv) < 0){
 
546
        perror("execv");
 
547
        _exit(EXIT_FAILURE);
768
548
      }
769
549
      /* no return */
770
550
    }
779
559
        perror("sigprocmask");
780
560
      }
781
561
      exitstatus = EXIT_FAILURE;
782
 
      goto fallback;
 
562
      goto end;
783
563
    }
784
564
    
785
565
    *new_process = (struct process){ .pid = pid,
793
573
    if(ret < 0){
794
574
      perror("sigprocmask");
795
575
      exitstatus = EXIT_FAILURE;
796
 
      goto fallback;
 
576
      goto end;
797
577
    }
798
578
    
799
579
    FD_SET(new_process->fd, &rfds_all);
804
584
    
805
585
  }
806
586
  
807
 
  free_plugin_list(plugin_list);
808
 
  plugin_list = NULL;
 
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
  }
809
593
  
810
594
  closedir(dir);
811
595
  dir = NULL;
821
605
    if (select_ret == -1){
822
606
      perror("select");
823
607
      exitstatus = EXIT_FAILURE;
824
 
      goto fallback;
 
608
      goto end;
825
609
    }
826
610
    /* OK, now either a process completed, or something can be read
827
611
       from one of them */
849
633
          /* Remove the plugin */
850
634
          FD_CLR(proc->fd, &rfds_all);
851
635
          /* Block signal while modifying process_list */
852
 
          ret = sigprocmask(SIG_BLOCK, &sigchld_action.sa_mask, NULL);
 
636
          ret = sigprocmask (SIG_BLOCK, &sigchld_action.sa_mask, NULL);
853
637
          if(ret < 0){
854
638
            perror("sigprocmask");
855
639
            exitstatus = EXIT_FAILURE;
856
 
            goto fallback;
 
640
            goto end;
857
641
          }
858
642
          /* Delete this process entry from the list */
859
643
          if(process_list == proc){
883
667
        }
884
668
        /* This process exited nicely, so print its buffer */
885
669
 
886
 
        bool bret = print_out_password(proc->buffer,
887
 
                                       proc->buffer_length);
 
670
        bool bret = print_out_password(proc->buffer, proc->buffer_length);
888
671
        if(not bret){
889
672
          perror("print_out_password");
890
673
          exitstatus = EXIT_FAILURE;
891
674
        }
892
 
        goto fallback;
 
675
        goto end;
893
676
      }
894
677
      /* This process has not completed.  Does it have any output? */
895
678
      if(proc->eof or not FD_ISSET(proc->fd, &rfds)){
903
686
        if (proc->buffer == NULL){
904
687
          perror("malloc");
905
688
          exitstatus = EXIT_FAILURE;
906
 
          goto fallback;
 
689
          goto end;
907
690
        }
908
691
        proc->buffer_size += BUFFER_SIZE;
909
692
      }
922
705
      }
923
706
    }
924
707
  }
925
 
 
926
 
 
927
 
 fallback:
928
708
  
929
 
  if(process_list == NULL or exitstatus != EXIT_SUCCESS){
930
 
    /* Fallback if all plugins failed, none are found or an error
931
 
       occured */
 
709
  if(process_list == NULL){
932
710
    bool bret;
933
711
    fprintf(stderr, "Going to fallback mode using getpass(3)\n");
934
712
    char *passwordbuffer = getpass("Password: ");
936
714
    if(not bret){
937
715
      perror("print_out_password");
938
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;
939
723
    }
940
724
  }
 
725
 
 
726
 end:
941
727
  
942
728
  /* Restore old signal handler */
943
 
  ret = sigaction(SIGCHLD, &old_sigchld_action, NULL);
944
 
  if(ret == -1){
945
 
    perror("sigaction");
946
 
    exitstatus = EXIT_FAILURE;
947
 
  }
948
 
 
949
 
  if(custom_argv != NULL){
950
 
    for(char **arg = custom_argv; *arg != NULL; arg++){
951
 
      free(*arg);
952
 
    }
953
 
    free(custom_argv);
954
 
  }
955
 
  free_plugin_list(plugin_list);
 
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
  }
956
739
  
957
740
  if(dir != NULL){
958
741
    closedir(dir);