/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 plugbasedclient.c

  • Committer: Teddy Hogeborn
  • Date: 2008-08-02 21:06:12 UTC
  • Revision ID: teddy@fukt.bsnet.se-20080802210612-4c1waup4z0f66ya7
Non-tested commit for merge purposes.

* plugbasedclient.c (getplugin, addargument, set_cloexec): Made static.

* plugins.d/passprompt.c (termination_handler): Made static.

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
 
24
26
#include <stdio.h>              /* popen(), fileno(), fprintf(),
25
27
                                   stderr, STDOUT_FILENO */
26
28
#include <iso646.h>             /* and, or, not */
27
 
#include <sys/types.h>         /* DIR, opendir(), stat(), struct stat,
28
 
                                  waitpid(), WIFEXITED(),
29
 
                                  WEXITSTATUS(), wait() */
 
29
#include <sys/types.h>          /* DIR, opendir(), stat(),
 
30
                                   struct stat, waitpid(),
 
31
                                   WIFEXITED(), WEXITSTATUS(),
 
32
                                   wait() */
30
33
#include <sys/wait.h>           /* wait() */
31
34
#include <dirent.h>             /* DIR, struct dirent, opendir(),
32
35
                                   readdir(), closedir() */
47
50
                                   struct argp_state, struct argp,
48
51
                                   argp_parse() */
49
52
 
 
53
#define BUFFER_SIZE 256
 
54
 
50
55
struct process;
51
56
 
52
57
typedef struct process{
55
60
  char *buffer;
56
61
  size_t buffer_size;
57
62
  size_t buffer_length;
 
63
  bool eof;
 
64
  bool completed;
 
65
  int status;
58
66
  struct process *next;
59
67
} process;
60
68
 
66
74
  struct plugin *next;
67
75
} plugin;
68
76
 
69
 
plugin *getplugin(char *name, plugin **plugin_list){
 
77
static plugin *getplugin(char *name, plugin **plugin_list){
70
78
  for (plugin *p = *plugin_list; p != NULL; p = p->next){
71
79
    if ((p->name == name)
72
80
        or (p->name and name and (strcmp(p->name, name) == 0))){
95
103
  return new_plugin;
96
104
}
97
105
 
98
 
void addargument(plugin *p, char *arg){
 
106
static void addargument(plugin *p, char *arg){
99
107
  p->argv[p->argc] = arg;
100
108
  p->argv = realloc(p->argv, sizeof(char *) * (size_t)(p->argc + 2));
101
109
  if (p->argv == NULL){
111
119
 * Descriptor Flags".
112
120
 * *Note File Descriptor Flags:(libc)Descriptor Flags.
113
121
 */
114
 
int set_cloexec_flag(int fd)
 
122
static int set_cloexec_flag(int fd)
115
123
{
116
124
  int ret = fcntl(fd, F_GETFD, 0);
117
125
  /* If reading the flags failed, return error indication now. */
122
130
  return fcntl(fd, F_SETFD, ret | FD_CLOEXEC);
123
131
}
124
132
 
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>";
 
133
const char *argp_program_version = "plugbasedclient 0.9";
 
134
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
 
135
 
 
136
process *process_list = NULL;
 
137
 
 
138
/* Mark a process as completed when it exits, and save its exit
 
139
   status. */
 
140
void handle_sigchld(__attribute__((unused)) int sig){
 
141
  process *proc = process_list;
 
142
  int status;
 
143
  pid_t pid = wait(&status);
 
144
  while(proc != NULL and proc->pid != pid){
 
145
    proc = proc->next;    
 
146
  }
 
147
  if(proc == NULL){
 
148
    /* Process not found in process list */
 
149
    return;
 
150
  }
 
151
  proc->status = status;
 
152
  proc->completed = true;
 
153
}
132
154
 
133
155
int main(int argc, char *argv[]){
134
156
  const char *plugindir = "/conf/conf.d/mandos/plugins.d";
135
157
  size_t d_name_len;
136
 
  DIR *dir;
 
158
  DIR *dir = NULL;
137
159
  struct dirent *dirst;
138
160
  struct stat st;
139
161
  fd_set rfds_all;
140
162
  int ret, maxfd = 0;
141
 
  process *process_list = NULL;
142
163
  bool debug = false;
143
164
  int exitstatus = EXIT_SUCCESS;
144
165
  
 
166
  /* Establish a signal handler */
 
167
  struct sigaction old_sigchld_action,
 
168
    sigchld_action = { .sa_handler = handle_sigchld,
 
169
                       .sa_flags = SA_NOCLDSTOP };
 
170
  sigemptyset(&sigchld_action.sa_mask);
 
171
  ret = sigaddset(&sigchld_action.sa_mask, SIGCHLD);
 
172
  if(ret < 0){
 
173
    perror("sigaddset");
 
174
    exit(EXIT_FAILURE);
 
175
  }
 
176
  ret = sigaction(SIGCHLD, &sigchld_action, &old_sigchld_action);
 
177
  if(ret < 0){
 
178
    perror("sigaction");
 
179
    exit(EXIT_FAILURE);
 
180
  }
 
181
  
145
182
  /* The options we understand. */
146
183
  struct argp_option options[] = {
147
184
    { .name = "global-options", .key = 'g',
162
199
  };
163
200
  
164
201
  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. */
 
202
    /* Get the INPUT argument from `argp_parse', which we know is a
 
203
       pointer to our plugin list pointer. */
167
204
    plugin **plugins = state->input;
168
205
    switch (key) {
169
206
    case 'g':
229
266
  }
230
267
  
231
268
  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
269
  if(dir == NULL){
240
 
    fprintf(stderr, "Can not open directory\n");
241
 
    return EXIT_FAILURE;
 
270
    perror("Could not open plugin dir");
 
271
    exitstatus = EXIT_FAILURE;
 
272
    goto end;
 
273
  }
 
274
  
 
275
  /* Set the FD_CLOEXEC flag on the directory, if possible */
 
276
  {
 
277
    int dir_fd = dirfd(dir);
 
278
    if(dir_fd >= 0){
 
279
      ret = set_cloexec_flag(dir_fd);
 
280
      if(ret < 0){
 
281
        perror("set_cloexec_flag");
 
282
        exitstatus = EXIT_FAILURE;
 
283
        goto end;
 
284
      }
 
285
    }
242
286
  }
243
287
  
244
288
  FD_ZERO(&rfds_all);
267
311
        if((d_name_len >= pre_len)
268
312
           and strncmp((dirst->d_name), *pre, pre_len) == 0){
269
313
          if(debug){
270
 
            fprintf(stderr, "Ignoring plugin dir entry name \"%s\""
 
314
            fprintf(stderr, "Ignoring plugin dir entry \"%s\""
271
315
                    " with bad prefix %s\n", dirst->d_name, *pre);
272
316
          }
273
317
          bad_name = true;
285
329
           and (strcmp((dirst->d_name)+d_name_len-suf_len, *suf)
286
330
                == 0)){
287
331
          if(debug){
288
 
            fprintf(stderr, "Ignoring plugin dir entry name \"%s\""
 
332
            fprintf(stderr, "Ignoring plugin dir entry \"%s\""
289
333
                    " with bad suffix %s\n", dirst->d_name, *suf);
290
334
          }
291
335
          bad_name = true;
301
345
    char *filename = malloc(d_name_len + strlen(plugindir) + 2);
302
346
    if (filename == NULL){
303
347
      perror("malloc");
304
 
      exitstatus =EXIT_FAILURE;
 
348
      exitstatus = EXIT_FAILURE;
305
349
      goto end;
306
350
    }
307
 
    strcpy(filename, plugindir);
308
 
    strcat(filename, "/");
309
 
    strcat(filename, dirst->d_name);    
310
 
 
 
351
    strcpy(filename, plugindir); /* Spurious warning */
 
352
    strcat(filename, "/");      /* Spurious warning */
 
353
    strcat(filename, dirst->d_name); /* Spurious warning */
 
354
    
311
355
    stat(filename, &st);
312
 
 
 
356
    
313
357
    if (not S_ISREG(st.st_mode) or (access(filename, X_OK) != 0)){
314
358
      if(debug){
315
 
        fprintf(stderr, "Ignoring plugin dir entry name \"%s\""
 
359
        fprintf(stderr, "Ignoring plugin dir entry \"%s\""
316
360
                " with bad type or mode\n", filename);
317
361
      }
 
362
      free(filename);
318
363
      continue;
319
364
    }
320
365
    if(getplugin(dirst->d_name, &plugin_list)->disabled){
321
366
      if(debug){
322
 
        fprintf(stderr, "Ignoring disabled plugin \"%s\"",
 
367
        fprintf(stderr, "Ignoring disabled plugin \"%s\"\n",
323
368
                dirst->d_name);
324
369
      }
 
370
      free(filename);
325
371
      continue;
326
372
    }
327
 
    // Starting a new process to be watched
328
 
    int pipefd[2]; 
329
 
    ret = pipe(pipefd);
330
 
    if (ret == -1){
331
 
      perror(argv[0]);
332
 
      goto end;
333
 
    }
334
373
    plugin *p = getplugin(dirst->d_name, &plugin_list);
335
374
    {
336
375
      /* Add global arguments to argument list for this plugin */
339
378
        addargument(p, *a);
340
379
      }
341
380
    }
 
381
    int pipefd[2]; 
 
382
    ret = pipe(pipefd);
 
383
    if (ret == -1){
 
384
      perror("pipe");
 
385
      exitstatus = EXIT_FAILURE;
 
386
      goto end;
 
387
    }
 
388
    ret = set_cloexec_flag(pipefd[0]);
 
389
    if(ret < 0){
 
390
      perror("set_cloexec_flag");
 
391
      exitstatus = EXIT_FAILURE;
 
392
      goto end;
 
393
    }
 
394
    ret = set_cloexec_flag(pipefd[1]);
 
395
    if(ret < 0){
 
396
      perror("set_cloexec_flag");
 
397
      exitstatus = EXIT_FAILURE;
 
398
      goto end;
 
399
    }
 
400
    /* Block SIGCHLD until process is safely in process list */
 
401
    ret = sigprocmask (SIG_BLOCK, &sigchld_action.sa_mask, NULL);
 
402
    if(ret < 0){
 
403
      perror("sigprocmask");
 
404
      exitstatus = EXIT_FAILURE;
 
405
      goto end;
 
406
    }
 
407
    // Starting a new process to be watched
342
408
    pid_t pid = fork();
343
409
    if(pid == 0){
344
410
      /* this is the child process */
345
 
      closedir(dir);
346
 
      close(pipefd[0]); /* close unused read end of pipe */
 
411
      ret = sigaction(SIGCHLD, &old_sigchld_action, NULL);
 
412
      if(ret < 0){
 
413
        perror("sigaction");
 
414
        _exit(EXIT_FAILURE);
 
415
      }
 
416
      ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
 
417
      if(ret < 0){
 
418
        perror("sigprocmask");
 
419
        _exit(EXIT_FAILURE);
 
420
      }
347
421
      dup2(pipefd[1], STDOUT_FILENO); /* replace our stdout */
348
422
      
 
423
      if(dirfd(dir) < 0){
 
424
        /* If dir has no file descriptor, we could not set FD_CLOEXEC
 
425
           above and must now close it manually here. */
 
426
        closedir(dir);
 
427
      }
349
428
      if(execv(filename, p->argv) < 0){
350
 
        perror(argv[0]);
351
 
        close(pipefd[1]);
 
429
        perror("execv");
352
430
        _exit(EXIT_FAILURE);
353
431
      }
354
432
      /* no return */
355
433
    }
 
434
    /* parent process */
 
435
    free(filename);
356
436
    close(pipefd[1]);           /* close unused write end of pipe */
357
437
    process *new_process = malloc(sizeof(process));
358
438
    if (new_process == NULL){
359
439
      perror("malloc");
360
 
      exitstatus = EXIT_FAILURE;
361
 
      goto end;
362
 
    }
363
 
    
364
 
    new_process->fd = pipefd[0];
365
 
    new_process->buffer = malloc(BUFFER_SIZE);
366
 
    if (new_process->buffer == NULL){
367
 
      perror("malloc");
368
 
      exitstatus = EXIT_FAILURE;
369
 
      goto end;
370
 
    }
371
 
    new_process->buffer_size = BUFFER_SIZE;
372
 
    new_process->buffer_length = 0;
 
440
      ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
 
441
      if(ret < 0){
 
442
        perror("sigprocmask");
 
443
      }
 
444
      exitstatus = EXIT_FAILURE;
 
445
      goto end;
 
446
    }
 
447
    
 
448
    *new_process = (struct process){ .pid = pid,
 
449
                                     .fd = pipefd[0],
 
450
                                     .next = process_list };
 
451
    // List handling
 
452
    process_list = new_process;
 
453
    /* Unblock SIGCHLD so signal handler can be run if this process
 
454
       has already completed */
 
455
    ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
 
456
    if(ret < 0){
 
457
      perror("sigprocmask");
 
458
      exitstatus = EXIT_FAILURE;
 
459
      goto end;
 
460
    }
 
461
    
373
462
    FD_SET(new_process->fd, &rfds_all);
374
 
      
 
463
    
375
464
    if (maxfd < new_process->fd){
376
465
      maxfd = new_process->fd;
377
466
    }
378
467
    
379
 
    //List handling
380
 
    new_process->next = process_list;
381
 
    process_list = new_process;
 
468
  }
 
469
  
 
470
  /* Free the plugin list */
 
471
  for(plugin *next; plugin_list != NULL; plugin_list = next){
 
472
    next = plugin_list->next;
 
473
    free(plugin_list->argv);
 
474
    free(plugin_list);
382
475
  }
383
476
  
384
477
  closedir(dir);
 
478
  dir = NULL;
385
479
  
386
 
  if (process_list != NULL){
387
 
    while(true){
388
 
      fd_set rfds = rfds_all;
389
 
      int select_ret = select(maxfd+1, &rfds, NULL, NULL, NULL);
390
 
      if (select_ret == -1){
391
 
        perror(argv[0]);
392
 
        goto end;
393
 
      }else{    
394
 
        for(process *process_itr = process_list; process_itr != NULL;
395
 
            process_itr = process_itr->next){
396
 
          if(FD_ISSET(process_itr->fd, &rfds)){
397
 
            if(process_itr->buffer_length + BUFFER_SIZE
398
 
               > process_itr->buffer_size){
399
 
                process_itr->buffer = realloc(process_itr->buffer,
400
 
                                              process_itr->buffer_size
401
 
                                              + (size_t) BUFFER_SIZE);
402
 
                if (process_itr->buffer == NULL){
403
 
                  perror(argv[0]);
404
 
                  goto end;
405
 
                }
406
 
                process_itr->buffer_size += BUFFER_SIZE;
407
 
            }
408
 
            ret = read(process_itr->fd, process_itr->buffer
409
 
                       + process_itr->buffer_length, BUFFER_SIZE);
410
 
            if(ret < 0){
411
 
              /* Read error from this process; ignore it */
412
 
              continue;
413
 
            }
414
 
            process_itr->buffer_length += (size_t) ret;
415
 
            if(ret == 0){
416
 
              /* got EOF */
417
 
              /* wait for process exit */
418
 
              int status;
419
 
              waitpid(process_itr->pid, &status, 0);
420
 
              if(WIFEXITED(status) and WEXITSTATUS(status) == 0){
421
 
                for(size_t written = 0;
422
 
                    written < process_itr->buffer_length;){
423
 
                  ret = write(STDOUT_FILENO,
424
 
                              process_itr->buffer + written,
425
 
                              process_itr->buffer_length - written);
426
 
                  if(ret < 0){
427
 
                    perror(argv[0]);
428
 
                    goto end;
429
 
                  }
430
 
                  written += (size_t)ret;
431
 
                }
432
 
                goto end;
433
 
              } else {
434
 
                FD_CLR(process_itr->fd, &rfds_all);
 
480
  if (process_list == NULL){
 
481
    fprintf(stderr, "No plugin processes started, exiting\n");
 
482
    exitstatus = EXIT_FAILURE;
 
483
    goto end;
 
484
  }
 
485
  while(process_list){
 
486
    fd_set rfds = rfds_all;
 
487
    int select_ret = select(maxfd+1, &rfds, NULL, NULL, NULL);
 
488
    if (select_ret == -1){
 
489
      perror("select");
 
490
      exitstatus = EXIT_FAILURE;
 
491
      goto end;
 
492
    }
 
493
    /* OK, now either a process completed, or something can be read
 
494
       from one of them */
 
495
    for(process *proc = process_list; proc ; proc = proc->next){
 
496
      /* Is this process completely done? */
 
497
      if(proc->eof and proc->completed){
 
498
        /* Only accept the plugin output if it exited cleanly */
 
499
        if(not WIFEXITED(proc->status)
 
500
           or WEXITSTATUS(proc->status) != 0){
 
501
          /* Bad exit by plugin */
 
502
          if(debug){
 
503
            if(WIFEXITED(proc->status)){
 
504
              fprintf(stderr, "Plugin %d exited with status %d\n",
 
505
                      proc->pid, WEXITSTATUS(proc->status));
 
506
            } else if(WIFSIGNALED(proc->status)) {
 
507
              fprintf(stderr, "Plugin %d killed by signal %d\n",
 
508
                      proc->pid, WTERMSIG(proc->status));
 
509
            } else if(WCOREDUMP(proc->status)){
 
510
              fprintf(stderr, "Plugin %d dumped core\n", proc->pid);
 
511
            }
 
512
          }
 
513
          /* Remove the plugin */
 
514
          FD_CLR(proc->fd, &rfds_all);
 
515
          /* Block signal while modifying process_list */
 
516
          ret = sigprocmask (SIG_BLOCK, &sigchld_action.sa_mask, NULL);
 
517
          if(ret < 0){
 
518
            perror("sigprocmask");
 
519
            exitstatus = EXIT_FAILURE;
 
520
            goto end;
 
521
          }
 
522
          /* Delete this process entry from the list */
 
523
          if(process_list == proc){
 
524
            /* First one - simple */
 
525
            process_list = proc->next;
 
526
          } else {
 
527
            /* Second one or later */
 
528
            for(process *p = process_list; p != NULL; p = p->next){
 
529
              if(p->next == proc){
 
530
                p->next = proc->next;
 
531
                break;
435
532
              }
436
533
            }
437
534
          }
438
 
        }
 
535
          /* We are done modifying process list, so unblock signal */
 
536
          ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask,
 
537
                             NULL);
 
538
          if(ret < 0){
 
539
            perror("sigprocmask");
 
540
          }
 
541
          free(proc->buffer);
 
542
          free(proc);
 
543
          /* We deleted this process from the list, so we can't go
 
544
             proc->next.  Therefore, start over from the beginning of
 
545
             the process list */
 
546
          break;
 
547
        }
 
548
        /* This process exited nicely, so print its buffer */
 
549
        for(size_t written = 0; written < proc->buffer_length;
 
550
            written += (size_t)ret){
 
551
          ret = TEMP_FAILURE_RETRY(write(STDOUT_FILENO,
 
552
                                         proc->buffer + written,
 
553
                                         proc->buffer_length
 
554
                                         - written));
 
555
          if(ret < 0){
 
556
            perror("write");
 
557
            exitstatus = EXIT_FAILURE;
 
558
            goto end;
 
559
          }
 
560
        }
 
561
        goto end;
 
562
      }
 
563
      /* This process has not completed.  Does it have any output? */
 
564
      if(proc->eof or not FD_ISSET(proc->fd, &rfds)){
 
565
        /* This process had nothing to say at this time */
 
566
        continue;
 
567
      }
 
568
      /* Before reading, make the process' data buffer large enough */
 
569
      if(proc->buffer_length + BUFFER_SIZE > proc->buffer_size){
 
570
        proc->buffer = realloc(proc->buffer, proc->buffer_size
 
571
                               + (size_t) BUFFER_SIZE);
 
572
        if (proc->buffer == NULL){
 
573
          perror("malloc");
 
574
          exitstatus = EXIT_FAILURE;
 
575
          goto end;
 
576
        }
 
577
        proc->buffer_size += BUFFER_SIZE;
 
578
      }
 
579
      /* Read from the process */
 
580
      ret = read(proc->fd, proc->buffer + proc->buffer_length,
 
581
                 BUFFER_SIZE);
 
582
      if(ret < 0){
 
583
        /* Read error from this process; ignore the error */
 
584
        continue;
 
585
      }
 
586
      if(ret == 0){
 
587
        /* got EOF */
 
588
        proc->eof = true;
 
589
      } else {
 
590
        proc->buffer_length += (size_t) ret;
439
591
      }
440
592
    }
441
593
  }
 
594
  if(process_list == NULL){
 
595
    fprintf(stderr, "All plugin processes failed, exiting\n");
 
596
    exitstatus = EXIT_FAILURE;
 
597
  }
442
598
  
443
599
 end:
444
 
  for(process *process_itr = process_list; process_itr != NULL;
445
 
      process_itr = process_itr->next){
446
 
    close(process_itr->fd);
447
 
    kill(process_itr->pid, SIGTERM);
448
 
    free(process_itr->buffer);
449
 
  }
450
 
  
451
 
  while(true){
452
 
    int status;
453
 
    ret = wait(&status);
454
 
    if (ret == -1){
455
 
      if(errno != ECHILD){
456
 
        perror("wait");
457
 
      }
458
 
      break;
459
 
    }
460
 
  }  
 
600
  /* Restore old signal handler */
 
601
  sigaction(SIGCHLD, &old_sigchld_action, NULL);
 
602
  
 
603
  /* Free the plugin list */
 
604
  for(plugin *next; plugin_list != NULL; plugin_list = next){
 
605
    next = plugin_list->next;
 
606
    free(plugin_list->argv);
 
607
    free(plugin_list);
 
608
  }
 
609
  
 
610
  if(dir != NULL){
 
611
    closedir(dir);
 
612
  }
 
613
  
 
614
  /* Free the process list and kill the processes */
 
615
  for(process *next; process_list != NULL; process_list = next){
 
616
    next = process_list->next;
 
617
    close(process_list->fd);
 
618
    kill(process_list->pid, SIGTERM);
 
619
    free(process_list->buffer);
 
620
    free(process_list);
 
621
  }
 
622
  
 
623
  /* Wait for any remaining child processes to terminate */
 
624
  do{
 
625
    ret = wait(NULL);
 
626
  } while(ret >= 0);
 
627
  if(errno != ECHILD){
 
628
    perror("wait");
 
629
  }
 
630
  
461
631
  return exitstatus;
462
632
}