/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

minor edits

Show diffs side-by-side

added added

removed removed

Lines of Context:
50
50
                                   struct argp_state, struct argp,
51
51
                                   argp_parse() */
52
52
 
 
53
#define BUFFER_SIZE 256
 
54
 
53
55
struct process;
54
56
 
55
57
typedef struct process{
58
60
  char *buffer;
59
61
  size_t buffer_size;
60
62
  size_t buffer_length;
 
63
  bool eof;
 
64
  bool completed;
 
65
  int status;
61
66
  struct process *next;
62
67
} process;
63
68
 
69
74
  struct plugin *next;
70
75
} plugin;
71
76
 
72
 
plugin *getplugin(char *name, plugin **plugin_list){
 
77
static plugin *getplugin(char *name, plugin **plugin_list){
73
78
  for (plugin *p = *plugin_list; p != NULL; p = p->next){
74
79
    if ((p->name == name)
75
80
        or (p->name and name and (strcmp(p->name, name) == 0))){
98
103
  return new_plugin;
99
104
}
100
105
 
101
 
void addargument(plugin *p, char *arg){
 
106
static void addargument(plugin *p, char *arg){
102
107
  p->argv[p->argc] = arg;
103
108
  p->argv = realloc(p->argv, sizeof(char *) * (size_t)(p->argc + 2));
104
109
  if (p->argv == NULL){
114
119
 * Descriptor Flags".
115
120
 * *Note File Descriptor Flags:(libc)Descriptor Flags.
116
121
 */
117
 
int set_cloexec_flag(int fd)
 
122
static int set_cloexec_flag(int fd)
118
123
{
119
124
  int ret = fcntl(fd, F_GETFD, 0);
120
125
  /* If reading the flags failed, return error indication now. */
125
130
  return fcntl(fd, F_SETFD, ret | FD_CLOEXEC);
126
131
}
127
132
 
128
 
 
129
 
#define BUFFER_SIZE 256
130
 
 
131
 
const char *argp_program_version =
132
 
  "plugbasedclient 0.9";
133
 
const char *argp_program_bug_address =
134
 
  "<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
}
135
154
 
136
155
int main(int argc, char *argv[]){
137
156
  const char *plugindir = "/conf/conf.d/mandos/plugins.d";
138
157
  size_t d_name_len;
139
 
  DIR *dir;
 
158
  DIR *dir = NULL;
140
159
  struct dirent *dirst;
141
160
  struct stat st;
142
161
  fd_set rfds_all;
143
162
  int ret, maxfd = 0;
144
 
  process *process_list = NULL;
 
163
  uid_t uid = 65534;
 
164
  gid_t gid = 65534;
145
165
  bool debug = false;
146
166
  int exitstatus = EXIT_SUCCESS;
 
167
  struct sigaction old_sigchld_action;
 
168
  struct sigaction sigchld_action = { .sa_handler = handle_sigchld,
 
169
                                      .sa_flags = SA_NOCLDSTOP };
 
170
  char *plus_options = NULL;
 
171
  char **plus_argv = NULL;
 
172
  
 
173
  /* Establish a signal handler */
 
174
  sigemptyset(&sigchld_action.sa_mask);
 
175
  ret = sigaddset(&sigchld_action.sa_mask, SIGCHLD);
 
176
  if(ret < 0){
 
177
    perror("sigaddset");
 
178
    exit(EXIT_FAILURE);
 
179
  }
 
180
  ret = sigaction(SIGCHLD, &sigchld_action, &old_sigchld_action);
 
181
  if(ret < 0){
 
182
    perror("sigaction");
 
183
    exit(EXIT_FAILURE);
 
184
  }
147
185
  
148
186
  /* The options we understand. */
149
187
  struct argp_option options[] = {
159
197
    { .name = "plugin-dir", .key = 128,
160
198
      .arg = "DIRECTORY",
161
199
      .doc = "Specify a different plugin directory", .group = 2 },
162
 
    { .name = "debug", .key = 129,
 
200
    { .name = "userid", .key = 129,
 
201
      .arg = "ID", .flags = 0,
 
202
      .doc = "User ID the plugins will run as", .group = 2 },
 
203
    { .name = "groupid", .key = 130,
 
204
      .arg = "ID", .flags = 0,
 
205
      .doc = "Group ID the plugins will run as", .group = 2 },
 
206
    { .name = "debug", .key = 131,
163
207
      .doc = "Debug mode", .group = 3 },
164
208
    { .name = NULL }
165
209
  };
166
210
  
167
211
  error_t parse_opt (int key, char *arg, struct argp_state *state) {
168
 
       /* Get the INPUT argument from `argp_parse', which we
169
 
          know is a pointer to our plugin list pointer. */
 
212
    /* Get the INPUT argument from `argp_parse', which we know is a
 
213
       pointer to our plugin list pointer. */
170
214
    plugin **plugins = state->input;
171
215
    switch (key) {
172
216
    case 'g':
175
219
        do{
176
220
          addargument(getplugin(NULL, plugins), p);
177
221
          p = strtok(NULL, ",");
178
 
        } while (p);
 
222
        } while (p != NULL);
179
223
      }
180
224
      break;
181
225
    case 'o':
182
226
      if (arg != NULL){
183
227
        char *name = strtok(arg, ":");
184
228
        char *p = strtok(NULL, ":");
185
 
        if(p){
 
229
        if(p != NULL){
186
230
          p = strtok(p, ",");
187
231
          do{
188
232
            addargument(getplugin(name, plugins), p);
189
233
            p = strtok(NULL, ",");
190
 
          } while (p);
 
234
          } while (p != NULL);
191
235
        }
192
236
      }
193
237
      break;
200
244
      plugindir = arg;
201
245
      break;
202
246
    case 129:
 
247
      uid = (uid_t)strtol(arg, NULL, 10);
 
248
      break;
 
249
    case 130:
 
250
      gid = (gid_t)strtol(arg, NULL, 10);
 
251
      break;
 
252
    case 131:
203
253
      debug = true;
204
254
      break;
205
255
    case ARGP_KEY_ARG:
206
 
      argp_usage (state);
 
256
      if(plus_options != NULL or arg == NULL or arg[0] != '+'){
 
257
        argp_usage (state);
 
258
      }
 
259
      plus_options = arg;
207
260
      break;
208
261
    case ARGP_KEY_END:
209
262
      break;
216
269
  plugin *plugin_list = NULL;
217
270
  
218
271
  struct argp argp = { .options = options, .parser = parse_opt,
219
 
                       .args_doc = "",
 
272
                       .args_doc = "[+PLUS_SEPARATED_OPTIONS]",
220
273
                       .doc = "Mandos plugin runner -- Run plugins" };
221
274
  
222
 
  argp_parse (&argp, argc, argv, 0, 0, &plugin_list);
 
275
  argp_parse (&argp, argc, argv, 0, 0, &plugin_list);  
 
276
  
 
277
  if(plus_options){
 
278
    /* This is a mangled argument in the form of
 
279
     "+--option+--other-option=parameter+--yet-another-option", etc */
 
280
    /* Make new argc and argv vars, and call argp_parse() again. */
 
281
    plus_options++;             /* skip the first '+' character */
 
282
    const char delims[] = "+";
 
283
    char *arg;
 
284
    int new_argc = 1;
 
285
    plus_argv = malloc(sizeof(char*) * 2);
 
286
    if(plus_argv == NULL){
 
287
      perror("malloc");
 
288
      exitstatus = EXIT_FAILURE;
 
289
      goto end;
 
290
    }
 
291
    plus_argv[0] = argv[0];
 
292
    plus_argv[1] = NULL;
 
293
    arg = strtok(plus_options, delims); /* Get first argument */
 
294
    while(arg != NULL){
 
295
      new_argc++;
 
296
      plus_argv = realloc(plus_argv, sizeof(char *)
 
297
                         * ((unsigned int) new_argc + 1));
 
298
      if(plus_argv == NULL){
 
299
        perror("malloc");
 
300
        exitstatus = EXIT_FAILURE;
 
301
        goto end;
 
302
      }
 
303
      plus_argv[new_argc-1] = arg;
 
304
      plus_argv[new_argc] = NULL;
 
305
      arg = strtok(NULL, delims); /* Get next argument */
 
306
    }
 
307
    argp_parse (&argp, new_argc, plus_argv, 0, 0, &plugin_list);  
 
308
  }
223
309
  
224
310
  if(debug){
225
311
    for(plugin *p = plugin_list; p != NULL; p=p->next){
231
317
    }
232
318
  }
233
319
  
 
320
  ret = setuid(uid);
 
321
  if (ret == -1){
 
322
    perror("setuid");
 
323
  }
 
324
  
 
325
  setgid(gid);
 
326
  if (ret == -1){
 
327
    perror("setgid");
 
328
  }
 
329
  
234
330
  dir = opendir(plugindir);
235
331
  if(dir == NULL){
236
332
    perror("Could not open plugin dir");
237
333
    exitstatus = EXIT_FAILURE;
238
334
    goto end;
239
335
  }
 
336
  
240
337
  /* Set the FD_CLOEXEC flag on the directory, if possible */
241
338
  {
242
339
    int dir_fd = dirfd(dir);
250
347
    }
251
348
  }
252
349
  
253
 
  if(dir == NULL){
254
 
    fprintf(stderr, "Can not open directory\n");
255
 
    return EXIT_FAILURE;
256
 
  }
257
 
  
258
350
  FD_ZERO(&rfds_all);
259
351
  
260
352
  while(true){
318
410
      exitstatus = EXIT_FAILURE;
319
411
      goto end;
320
412
    }
321
 
    strcpy(filename, plugindir);
322
 
    strcat(filename, "/");
323
 
    strcat(filename, dirst->d_name);    
324
 
 
 
413
    strcpy(filename, plugindir); /* Spurious warning */
 
414
    strcat(filename, "/");      /* Spurious warning */
 
415
    strcat(filename, dirst->d_name); /* Spurious warning */
 
416
    
325
417
    stat(filename, &st);
326
 
 
 
418
    
327
419
    if (not S_ISREG(st.st_mode) or (access(filename, X_OK) != 0)){
328
420
      if(debug){
329
421
        fprintf(stderr, "Ignoring plugin dir entry \"%s\""
330
422
                " with bad type or mode\n", filename);
331
423
      }
 
424
      free(filename);
332
425
      continue;
333
426
    }
334
427
    if(getplugin(dirst->d_name, &plugin_list)->disabled){
336
429
        fprintf(stderr, "Ignoring disabled plugin \"%s\"\n",
337
430
                dirst->d_name);
338
431
      }
 
432
      free(filename);
339
433
      continue;
340
434
    }
341
435
    plugin *p = getplugin(dirst->d_name, &plugin_list);
365
459
      exitstatus = EXIT_FAILURE;
366
460
      goto end;
367
461
    }
 
462
    /* Block SIGCHLD until process is safely in process list */
 
463
    ret = sigprocmask (SIG_BLOCK, &sigchld_action.sa_mask, NULL);
 
464
    if(ret < 0){
 
465
      perror("sigprocmask");
 
466
      exitstatus = EXIT_FAILURE;
 
467
      goto end;
 
468
    }
368
469
    // Starting a new process to be watched
369
470
    pid_t pid = fork();
370
471
    if(pid == 0){
371
472
      /* this is the child process */
 
473
      ret = sigaction(SIGCHLD, &old_sigchld_action, NULL);
 
474
      if(ret < 0){
 
475
        perror("sigaction");
 
476
        _exit(EXIT_FAILURE);
 
477
      }
 
478
      ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
 
479
      if(ret < 0){
 
480
        perror("sigprocmask");
 
481
        _exit(EXIT_FAILURE);
 
482
      }
372
483
      dup2(pipefd[1], STDOUT_FILENO); /* replace our stdout */
373
484
      
374
485
      if(dirfd(dir) < 0){
375
486
        /* If dir has no file descriptor, we could not set FD_CLOEXEC
376
 
           and must close it manually  */
 
487
           above and must now close it manually here. */
377
488
        closedir(dir);
378
489
      }
379
490
      if(execv(filename, p->argv) < 0){
383
494
      /* no return */
384
495
    }
385
496
    /* parent process */
 
497
    free(filename);
386
498
    close(pipefd[1]);           /* close unused write end of pipe */
387
499
    process *new_process = malloc(sizeof(process));
388
500
    if (new_process == NULL){
389
501
      perror("malloc");
 
502
      ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
 
503
      if(ret < 0){
 
504
        perror("sigprocmask");
 
505
      }
390
506
      exitstatus = EXIT_FAILURE;
391
507
      goto end;
392
508
    }
394
510
    *new_process = (struct process){ .pid = pid,
395
511
                                     .fd = pipefd[0],
396
512
                                     .next = process_list };
 
513
    // List handling
 
514
    process_list = new_process;
 
515
    /* Unblock SIGCHLD so signal handler can be run if this process
 
516
       has already completed */
 
517
    ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
 
518
    if(ret < 0){
 
519
      perror("sigprocmask");
 
520
      exitstatus = EXIT_FAILURE;
 
521
      goto end;
 
522
    }
 
523
    
397
524
    FD_SET(new_process->fd, &rfds_all);
398
525
    
399
526
    if (maxfd < new_process->fd){
400
527
      maxfd = new_process->fd;
401
528
    }
402
529
    
403
 
    //List handling
404
 
    process_list = new_process;
405
530
  }
406
531
  
407
532
  /* Free the plugin list */
412
537
  }
413
538
  
414
539
  closedir(dir);
 
540
  dir = NULL;
415
541
  
416
542
  if (process_list == NULL){
417
543
    fprintf(stderr, "No plugin processes started, exiting\n");
418
 
    return EXIT_FAILURE;
 
544
    exitstatus = EXIT_FAILURE;
 
545
    goto end;
419
546
  }
420
 
  while(true){
 
547
  while(process_list){
421
548
    fd_set rfds = rfds_all;
422
549
    int select_ret = select(maxfd+1, &rfds, NULL, NULL, NULL);
423
550
    if (select_ret == -1){
425
552
      exitstatus = EXIT_FAILURE;
426
553
      goto end;
427
554
    }
 
555
    /* OK, now either a process completed, or something can be read
 
556
       from one of them */
428
557
    for(process *proc = process_list; proc ; proc = proc->next){
429
 
      if(not FD_ISSET(proc->fd, &rfds)){
 
558
      /* Is this process completely done? */
 
559
      if(proc->eof and proc->completed){
 
560
        /* Only accept the plugin output if it exited cleanly */
 
561
        if(not WIFEXITED(proc->status)
 
562
           or WEXITSTATUS(proc->status) != 0){
 
563
          /* Bad exit by plugin */
 
564
          if(debug){
 
565
            if(WIFEXITED(proc->status)){
 
566
              fprintf(stderr, "Plugin %d exited with status %d\n",
 
567
                      proc->pid, WEXITSTATUS(proc->status));
 
568
            } else if(WIFSIGNALED(proc->status)) {
 
569
              fprintf(stderr, "Plugin %d killed by signal %d\n",
 
570
                      proc->pid, WTERMSIG(proc->status));
 
571
            } else if(WCOREDUMP(proc->status)){
 
572
              fprintf(stderr, "Plugin %d dumped core\n", proc->pid);
 
573
            }
 
574
          }
 
575
          /* Remove the plugin */
 
576
          FD_CLR(proc->fd, &rfds_all);
 
577
          /* Block signal while modifying process_list */
 
578
          ret = sigprocmask (SIG_BLOCK, &sigchld_action.sa_mask, NULL);
 
579
          if(ret < 0){
 
580
            perror("sigprocmask");
 
581
            exitstatus = EXIT_FAILURE;
 
582
            goto end;
 
583
          }
 
584
          /* Delete this process entry from the list */
 
585
          if(process_list == proc){
 
586
            /* First one - simple */
 
587
            process_list = proc->next;
 
588
          } else {
 
589
            /* Second one or later */
 
590
            for(process *p = process_list; p != NULL; p = p->next){
 
591
              if(p->next == proc){
 
592
                p->next = proc->next;
 
593
                break;
 
594
              }
 
595
            }
 
596
          }
 
597
          /* We are done modifying process list, so unblock signal */
 
598
          ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask,
 
599
                             NULL);
 
600
          if(ret < 0){
 
601
            perror("sigprocmask");
 
602
          }
 
603
          free(proc->buffer);
 
604
          free(proc);
 
605
          /* We deleted this process from the list, so we can't go
 
606
             proc->next.  Therefore, start over from the beginning of
 
607
             the process list */
 
608
          break;
 
609
        }
 
610
        /* This process exited nicely, so print its buffer */
 
611
        for(size_t written = 0; written < proc->buffer_length;
 
612
            written += (size_t)ret){
 
613
          ret = TEMP_FAILURE_RETRY(write(STDOUT_FILENO,
 
614
                                         proc->buffer + written,
 
615
                                         proc->buffer_length
 
616
                                         - written));
 
617
          if(ret < 0){
 
618
            perror("write");
 
619
            exitstatus = EXIT_FAILURE;
 
620
            goto end;
 
621
          }
 
622
        }
 
623
        goto end;
 
624
      }
 
625
      /* This process has not completed.  Does it have any output? */
 
626
      if(proc->eof or not FD_ISSET(proc->fd, &rfds)){
 
627
        /* This process had nothing to say at this time */
430
628
        continue;
431
629
      }
 
630
      /* Before reading, make the process' data buffer large enough */
432
631
      if(proc->buffer_length + BUFFER_SIZE > proc->buffer_size){
433
632
        proc->buffer = realloc(proc->buffer, proc->buffer_size
434
633
                               + (size_t) BUFFER_SIZE);
439
638
        }
440
639
        proc->buffer_size += BUFFER_SIZE;
441
640
      }
 
641
      /* Read from the process */
442
642
      ret = read(proc->fd, proc->buffer + proc->buffer_length,
443
643
                 BUFFER_SIZE);
444
644
      if(ret < 0){
445
 
        /* Read error from this process; ignore it */
 
645
        /* Read error from this process; ignore the error */
446
646
        continue;
447
647
      }
448
 
      proc->buffer_length += (size_t) ret;
449
648
      if(ret == 0){
450
649
        /* got EOF */
451
 
        /* wait for process exit */
452
 
        int status;
453
 
        waitpid(proc->pid, &status, 0);
454
 
        if(not WIFEXITED(status) or WEXITSTATUS(status) != 0){
455
 
          FD_CLR(proc->fd, &rfds_all);
456
 
          continue;
457
 
        }
458
 
        for(size_t written = 0;
459
 
            written < proc->buffer_length; written += (size_t)ret){
460
 
          ret = TEMP_FAILURE_RETRY(write(STDOUT_FILENO,
461
 
                                         proc->buffer + written,
462
 
                                         proc->buffer_length
463
 
                                         - written));
464
 
          if(ret < 0){
465
 
            perror("write");
466
 
            exitstatus = EXIT_FAILURE;
467
 
            goto end;
468
 
          }
469
 
        }
470
 
        goto end;
 
650
        proc->eof = true;
 
651
      } else {
 
652
        proc->buffer_length += (size_t) ret;
471
653
      }
472
654
    }
473
655
  }
 
656
  if(process_list == NULL){
 
657
    fprintf(stderr, "All plugin processes failed, exiting\n");
 
658
    exitstatus = EXIT_FAILURE;
 
659
  }
474
660
  
475
661
 end:
 
662
  /* Restore old signal handler */
 
663
  sigaction(SIGCHLD, &old_sigchld_action, NULL);
 
664
  
 
665
  free(plus_argv);
 
666
  
 
667
  /* Free the plugin list */
 
668
  for(plugin *next; plugin_list != NULL; plugin_list = next){
 
669
    next = plugin_list->next;
 
670
    free(plugin_list->argv);
 
671
    free(plugin_list);
 
672
  }
 
673
  
 
674
  if(dir != NULL){
 
675
    closedir(dir);
 
676
  }
 
677
  
 
678
  /* Free the process list and kill the processes */
476
679
  for(process *next; process_list != NULL; process_list = next){
 
680
    next = process_list->next;
477
681
    close(process_list->fd);
478
682
    kill(process_list->pid, SIGTERM);
479
683
    free(process_list->buffer);
480
684
    free(process_list);
481
685
  }
482
686
  
483
 
  while(true){
484
 
    int status;
485
 
    ret = wait(&status);
486
 
    if (ret == -1){
487
 
      if(errno != ECHILD){
488
 
        perror("wait");
489
 
      }
490
 
      break;
491
 
    }
492
 
  }  
 
687
  /* Wait for any remaining child processes to terminate */
 
688
  do{
 
689
    ret = wait(NULL);
 
690
  } while(ret >= 0);
 
691
  if(errno != ECHILD){
 
692
    perror("wait");
 
693
  }
 
694
  
493
695
  return exitstatus;
494
696
}