/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 plugin-runner.c

merge + minor adjustments

Show diffs side-by-side

added added

removed removed

Lines of Context:
51
51
                                   close() */
52
52
#include <fcntl.h>              /* fcntl(), F_GETFD, F_SETFD,
53
53
                                   FD_CLOEXEC */
54
 
#include <string.h>             /* strtok, strlen(), strcpy(),
 
54
#include <string.h>             /* strsep, strlen(), strcpy(),
55
55
                                   strcat() */
56
56
#include <errno.h>              /* errno */
57
57
#include <argp.h>               /* struct argp_option, struct
65
65
#include <errno.h>              /* errno, EBADF */
66
66
 
67
67
#define BUFFER_SIZE 256
 
68
#define ARGFILE "/conf/conf.d/mandos/plugins.conf"
68
69
 
69
 
const char *argp_program_version = "mandos-client 1.0";
 
70
const char *argp_program_version = "plugin-runner 1.0";
70
71
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
71
72
 
72
73
struct process;
171
172
}
172
173
 
173
174
bool print_out_password(const char *buffer, size_t length){
174
 
  size_t ret;
175
 
  for(size_t written = 0; written < length; written += ret){
 
175
  ssize_t ret;
 
176
  if(length>0 and buffer[length-1] == '\n'){
 
177
    length--;
 
178
  }
 
179
  for(size_t written = 0; written < length; written += (size_t)ret){
176
180
    ret = TEMP_FAILURE_RETRY(write(STDOUT_FILENO, buffer + written,
177
181
                                   length - written));
178
182
    if(ret < 0){
182
186
  return true;
183
187
}
184
188
 
 
189
char ** addcustomargument(char **argv, int *argc, char *arg){
 
190
 
 
191
  if (argv == NULL){
 
192
    *argc = 1;
 
193
    argv = malloc(sizeof(char*) * 2);
 
194
    if(argv == NULL){
 
195
      return NULL;
 
196
    }
 
197
    argv[0] = NULL;     /* Will be set to argv[0] in main before parsing */
 
198
    argv[1] = NULL;
 
199
  }
 
200
  *argc += 1;
 
201
  argv = realloc(argv, sizeof(char *)
 
202
                  * ((unsigned int) *argc + 1));
 
203
  if(argv == NULL){
 
204
    return NULL;
 
205
  }
 
206
  argv[*argc-1] = arg;
 
207
  argv[*argc] = NULL;   
 
208
  return argv;
 
209
}
 
210
 
185
211
int main(int argc, char *argv[]){
186
 
  const char *plugindir = "/conf/conf.d/mandos/plugins.d";
 
212
  const char *plugindir = "/lib/mandos/plugins.d";
 
213
  const char *argfile = ARGFILE;
 
214
  FILE *conffp;
187
215
  size_t d_name_len;
188
216
  DIR *dir = NULL;
189
217
  struct dirent *dirst;
197
225
  struct sigaction old_sigchld_action;
198
226
  struct sigaction sigchld_action = { .sa_handler = handle_sigchld,
199
227
                                      .sa_flags = SA_NOCLDSTOP };
200
 
  char *plus_options = NULL;
201
 
  char **plus_argv = NULL;
 
228
  char **custom_argv = NULL;
 
229
  int custom_argc = 0;
202
230
  
203
231
  /* Establish a signal handler */
204
232
  sigemptyset(&sigchld_action.sa_mask);
245
273
    switch (key) {
246
274
    case 'g':
247
275
      if (arg != NULL){
248
 
        char *p = strtok(arg, ",");
249
 
        do{
 
276
        char *p;
 
277
        while((p = strsep(&arg, ",")) != NULL){
 
278
          if(p[0] == '\0'){
 
279
            continue;
 
280
          }
250
281
          addargument(getplugin(NULL, plugins), p);
251
 
          p = strtok(NULL, ",");
252
 
        } while (p != NULL);
 
282
        }
253
283
      }
254
284
      break;
255
285
    case 'o':
256
286
      if (arg != NULL){
257
 
        char *name = strtok(arg, ":");
258
 
        char *p = strtok(NULL, ":");
259
 
        if(p != NULL){
260
 
          p = strtok(p, ",");
261
 
          do{
 
287
        char *name = strsep(&arg, ":");
 
288
        if(name[0] == '\0'){
 
289
          break;
 
290
        }
 
291
        char *opt = strsep(&arg, ":");
 
292
        if(opt[0] == '\0'){
 
293
          break;
 
294
        }
 
295
        if(opt != NULL){
 
296
          char *p;
 
297
          while((p = strsep(&opt, ",")) != NULL){
 
298
            if(p[0] == '\0'){
 
299
              continue;
 
300
            }
262
301
            addargument(getplugin(name, plugins), p);
263
 
            p = strtok(NULL, ",");
264
 
          } while (p != NULL);
 
302
          }
265
303
        }
266
304
      }
267
305
      break;
283
321
      debug = true;
284
322
      break;
285
323
    case ARGP_KEY_ARG:
286
 
      if(plus_options != NULL or arg == NULL or arg[0] != '+'){
287
 
        argp_usage (state);
288
 
      }
289
 
      plus_options = arg;
 
324
      fprintf(stderr, "Ignoring unknown argument \"%s\"\n", arg); 
290
325
      break;
291
326
    case ARGP_KEY_END:
292
327
      break;
304
339
  
305
340
  ret = argp_parse (&argp, argc, argv, 0, 0, &plugin_list);
306
341
  if (ret == ARGP_ERR_UNKNOWN){
307
 
    fprintf(stderr, "Unkown error while parsing arguments\n");
 
342
    fprintf(stderr, "Unknown error while parsing arguments\n");
308
343
    exitstatus = EXIT_FAILURE;
309
344
    goto end;
310
345
  }
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");
 
346
 
 
347
  conffp = fopen(argfile, "r");
 
348
  if(conffp != NULL){
 
349
    char *org_line = NULL;
 
350
    size_t size = 0;
 
351
    ssize_t sret;
 
352
    char *p, *arg, *new_arg, *line;
 
353
    const char whitespace_delims[] = " \r\t\f\v\n";
 
354
    const char comment_delim[] = "#";
 
355
 
 
356
    while(true){
 
357
      sret = getline(&org_line, &size, conffp);
 
358
      if(sret == -1){
 
359
        break;
 
360
      }
 
361
 
 
362
      line = org_line;
 
363
      arg = strsep(&line, comment_delim);
 
364
      while((p = strsep(&arg, whitespace_delims)) != NULL){
 
365
        if(p[0] == '\0'){
 
366
          continue;
 
367
        }
 
368
        new_arg = strdup(p);
 
369
        custom_argv = addcustomargument(custom_argv, &custom_argc, new_arg);
 
370
        if (custom_argv == NULL){
 
371
          perror("addcustomargument");
 
372
          exitstatus = EXIT_FAILURE;
 
373
          goto end;
 
374
        }
 
375
      }
 
376
    }
 
377
    free(org_line);
 
378
  } else{
 
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){
 
382
      perror("fopen");
323
383
      exitstatus = EXIT_FAILURE;
324
384
      goto end;
325
385
    }
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);
 
386
  }
 
387
 
 
388
  if(custom_argv != NULL){
 
389
    custom_argv[0] = argv[0];
 
390
    ret = argp_parse (&argp, custom_argc, custom_argv, 0, 0, &plugin_list);
343
391
    if (ret == ARGP_ERR_UNKNOWN){
344
 
      fprintf(stderr, "Unkown error while parsing arguments\n");
 
392
      fprintf(stderr, "Unknown error while parsing arguments\n");
345
393
      exitstatus = EXIT_FAILURE;
346
394
      goto end;
347
395
    }
452
500
    char *filename = malloc(d_name_len + strlen(plugindir) + 2);
453
501
    if (filename == NULL){
454
502
      perror("malloc");
455
 
      exitstatus = EXIT_FAILURE;
456
 
      goto end;
 
503
      continue;
457
504
    }
458
505
    strcpy(filename, plugindir); /* Spurious warning */
459
506
    strcat(filename, "/");      /* Spurious warning */
462
509
    ret = stat(filename, &st);
463
510
    if (ret == -1){
464
511
      perror("stat");
465
 
      exitstatus = EXIT_FAILURE;
466
 
      goto end;
 
512
      free(filename);
 
513
      continue;
467
514
    }
468
515
    
469
516
    if (not S_ISREG(st.st_mode) or (access(filename, X_OK) != 0)){
518
565
    }
519
566
    // Starting a new process to be watched
520
567
    pid_t pid = fork();
 
568
    if(pid == -1){
 
569
      perror("fork");
 
570
      exitstatus = EXIT_FAILURE;
 
571
      goto end;
 
572
    }
521
573
    if(pid == 0){
522
574
      /* this is the child process */
523
575
      ret = sigaction(SIGCHLD, &old_sigchld_action, NULL);
705
757
      }
706
758
    }
707
759
  }
 
760
 
 
761
 
 
762
 end:
708
763
  
709
 
  if(process_list == NULL){
 
764
  if(process_list == NULL or exitstatus != EXIT_SUCCESS){
 
765
    /* Fallback if all plugins failed, none are found or an error occured */
710
766
    bool bret;
711
767
    fprintf(stderr, "Going to fallback mode using getpass(3)\n");
712
768
    char *passwordbuffer = getpass("Password: ");
716
772
      exitstatus = EXIT_FAILURE;
717
773
      goto end;
718
774
    }
719
 
    bret = print_out_password("\n", 1);
720
 
    if(not bret){
721
 
      perror("print_out_password");
722
 
      exitstatus = EXIT_FAILURE;
723
 
    }
724
775
  }
725
 
 
726
 
 end:
727
776
  
728
777
  /* Restore old signal handler */
729
778
  sigaction(SIGCHLD, &old_sigchld_action, NULL);
730
779
  
731
 
  free(plus_argv);
 
780
  free(custom_argv);
732
781
  
733
782
  /* Free the plugin list */
734
783
  for(plugin *next; plugin_list != NULL; plugin_list = next){