52
52
#include <fcntl.h>              /* fcntl(), F_GETFD, F_SETFD,
 
54
 
#include <string.h>             /* strtok, strlen(), strcpy(),
 
 
54
#include <string.h>             /* strsep, strlen(), strcpy(),
 
56
56
#include <errno.h>              /* errno */
 
57
57
#include <argp.h>               /* struct argp_option, struct
 
 
 
189
char ** addcustomargument(char **argv, int *argc, char *arg){
 
 
193
    argv = malloc(sizeof(char*) * 2);
 
 
197
    argv[0] = NULL;     /* Will be set to argv[0] in main before parsing */
 
 
201
  argv = realloc(argv, sizeof(char *)
 
 
202
                  * ((unsigned int) *argc + 1));
 
188
211
int main(int argc, char *argv[]){
 
189
 
  const char *plugindir = "/conf/conf.d/mandos/plugins.d";
 
 
212
  const char *plugindir = "/lib/mandos/plugins.d";
 
 
213
  const char *argfile = ARGFILE;
 
190
215
  size_t d_name_len;
 
192
217
  struct dirent *dirst;
 
 
200
225
  struct sigaction old_sigchld_action;
 
201
226
  struct sigaction sigchld_action = { .sa_handler = handle_sigchld,
 
202
227
                                      .sa_flags = SA_NOCLDSTOP };
 
203
 
  char *plus_options = NULL;
 
204
 
  char **plus_argv = NULL;
 
 
228
  char **custom_argv = NULL;
 
206
231
  /* Establish a signal handler */
 
207
232
  sigemptyset(&sigchld_action.sa_mask);
 
208
233
  ret = sigaddset(&sigchld_action.sa_mask, SIGCHLD);
 
 
250
275
      if (arg != NULL){
 
251
 
        char *p = strtok(arg, ",");
 
 
277
        while((p = strsep(&arg, ",")) != NULL){
 
253
281
          addargument(getplugin(NULL, plugins), p);
 
254
 
          p = strtok(NULL, ",");
 
259
286
      if (arg != NULL){
 
260
 
        char *name = strtok(arg, ":");
 
261
 
        char *p = strtok(NULL, ":");
 
 
287
        char *name = strsep(&arg, ":");
 
 
291
        char *opt = strsep(&arg, ":");
 
 
297
          while((p = strsep(&opt, ",")) != NULL){
 
265
301
            addargument(getplugin(name, plugins), p);
 
266
 
            p = strtok(NULL, ",");
 
 
311
343
    exitstatus = EXIT_FAILURE;
 
316
 
    /* This is a mangled argument in the form of
 
317
 
     "+--option+--other-option=parameter+--yet-another-option", etc */
 
318
 
    /* Make new argc and argv vars, and call argp_parse() again. */
 
319
 
    plus_options++;             /* skip the first '+' character */
 
320
 
    const char delims[] = "+";
 
323
 
    plus_argv = malloc(sizeof(char*) * 2);
 
324
 
    if(plus_argv == NULL){
 
 
347
  conffp = fopen(argfile, "r");
 
 
349
    char *org_line = NULL;
 
 
352
    char *p, *arg, *new_arg, *line;
 
 
353
    const char whitespace_delims[] = " \r\t\f\v\n";
 
 
354
    const char comment_delim[] = "#";
 
 
357
      sret = getline(&org_line, &size, conffp);
 
 
363
      arg = strsep(&line, comment_delim);
 
 
364
      while((p = strsep(&arg, whitespace_delims)) != NULL){
 
 
369
        custom_argv = addcustomargument(custom_argv, &custom_argc, new_arg);
 
 
370
        if (custom_argv == NULL){
 
 
371
          perror("addcustomargument");
 
 
372
          exitstatus = EXIT_FAILURE;
 
 
379
    /* Check for harmful errors and go to fallback. Other errors might
 
 
380
       not affect opening plugins */
 
 
381
    if (errno == EMFILE or errno == ENFILE or errno == ENOMEM){
 
326
383
      exitstatus = EXIT_FAILURE;
 
329
 
    plus_argv[0] = argv[0];
 
331
 
    arg = strtok(plus_options, delims); /* Get first argument */
 
334
 
      plus_argv = realloc(plus_argv, sizeof(char *)
 
335
 
                         * ((unsigned int) new_argc + 1));
 
336
 
      if(plus_argv == NULL){
 
338
 
        exitstatus = EXIT_FAILURE;
 
341
 
      plus_argv[new_argc-1] = arg;
 
342
 
      plus_argv[new_argc] = NULL;
 
343
 
      arg = strtok(NULL, delims); /* Get next argument */
 
345
 
    ret = argp_parse (&argp, new_argc, plus_argv, 0, 0, &plugin_list);
 
 
388
  if(custom_argv != NULL){
 
 
389
    custom_argv[0] = argv[0];
 
 
390
    ret = argp_parse (&argp, custom_argc, custom_argv, 0, 0, &plugin_list);
 
346
391
    if (ret == ARGP_ERR_UNKNOWN){
 
347
392
      fprintf(stderr, "Unknown error while parsing arguments\n");
 
348
393
      exitstatus = EXIT_FAILURE;