/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

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() */
 
24
#define _GNU_SOURCE             /* TEMP_FAILURE_RETRY() */
25
25
 
26
26
#include <stddef.h>             /* size_t, NULL */
27
27
#include <stdlib.h>             /* malloc(), exit(), EXIT_FAILURE,
51
51
                                   close() */
52
52
#include <fcntl.h>              /* fcntl(), F_GETFD, F_SETFD,
53
53
                                   FD_CLOEXEC */
54
 
#include <string.h>             /* strsep, strlen(), strcpy(),
 
54
#include <string.h>             /* strtok, 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/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;
171
170
  proc->completed = true;
172
171
}
173
172
 
174
 
bool print_out_password(const char *buffer, size_t length){
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){
180
 
    ret = TEMP_FAILURE_RETRY(write(STDOUT_FILENO, buffer + written,
181
 
                                   length - written));
182
 
    if(ret < 0){
183
 
      return false;
184
 
    }
185
 
  }
186
 
  return true;
187
 
}
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
 
 
211
173
int main(int argc, char *argv[]){
212
 
  const char *plugindir = "/lib/mandos/plugins.d";
213
 
  const char *argfile = ARGFILE;
214
 
  FILE *conffp;
 
174
  const char *plugindir = "/conf/conf.d/mandos/plugins.d";
215
175
  size_t d_name_len;
216
176
  DIR *dir = NULL;
217
177
  struct dirent *dirst;
225
185
  struct sigaction old_sigchld_action;
226
186
  struct sigaction sigchld_action = { .sa_handler = handle_sigchld,
227
187
                                      .sa_flags = SA_NOCLDSTOP };
228
 
  char **custom_argv = NULL;
229
 
  int custom_argc = 0;
 
188
  char *plus_options = NULL;
 
189
  char **plus_argv = NULL;
230
190
  
231
191
  /* Establish a signal handler */
232
192
  sigemptyset(&sigchld_action.sa_mask);
273
233
    switch (key) {
274
234
    case 'g':
275
235
      if (arg != NULL){
276
 
        char *p;
277
 
        while((p = strsep(&arg, ",")) != NULL){
278
 
          if(p[0] == '\0'){
279
 
            continue;
280
 
          }
 
236
        char *p = strtok(arg, ",");
 
237
        do{
281
238
          addargument(getplugin(NULL, plugins), p);
282
 
        }
 
239
          p = strtok(NULL, ",");
 
240
        } while (p != NULL);
283
241
      }
284
242
      break;
285
243
    case 'o':
286
244
      if (arg != NULL){
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
 
            }
 
245
        char *name = strtok(arg, ":");
 
246
        char *p = strtok(NULL, ":");
 
247
        if(p != NULL){
 
248
          p = strtok(p, ",");
 
249
          do{
301
250
            addargument(getplugin(name, plugins), p);
302
 
          }
 
251
            p = strtok(NULL, ",");
 
252
          } while (p != NULL);
303
253
        }
304
254
      }
305
255
      break;
321
271
      debug = true;
322
272
      break;
323
273
    case ARGP_KEY_ARG:
324
 
      fprintf(stderr, "Ignoring unknown argument \"%s\"\n", arg); 
 
274
      if(plus_options != NULL or arg == NULL or arg[0] != '+'){
 
275
        argp_usage (state);
 
276
      }
 
277
      plus_options = arg;
325
278
      break;
326
279
    case ARGP_KEY_END:
327
280
      break;
339
292
  
340
293
  ret = argp_parse (&argp, argc, argv, 0, 0, &plugin_list);
341
294
  if (ret == ARGP_ERR_UNKNOWN){
342
 
    fprintf(stderr, "Unknown error while parsing arguments\n");
 
295
    fprintf(stderr, "Unkown error while parsing arguments\n");
343
296
    exitstatus = EXIT_FAILURE;
344
297
    goto end;
345
298
  }
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");
 
299
  
 
300
  if(plus_options){
 
301
    /* This is a mangled argument in the form of
 
302
     "+--option+--other-option=parameter+--yet-another-option", etc */
 
303
    /* Make new argc and argv vars, and call argp_parse() again. */
 
304
    plus_options++;             /* skip the first '+' character */
 
305
    const char delims[] = "+";
 
306
    char *arg;
 
307
    int new_argc = 1;
 
308
    plus_argv = malloc(sizeof(char*) * 2);
 
309
    if(plus_argv == NULL){
 
310
      perror("malloc");
383
311
      exitstatus = EXIT_FAILURE;
384
312
      goto end;
385
313
    }
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);
 
314
    plus_argv[0] = argv[0];
 
315
    plus_argv[1] = NULL;
 
316
    arg = strtok(plus_options, delims); /* Get first argument */
 
317
    while(arg != NULL){
 
318
      new_argc++;
 
319
      plus_argv = realloc(plus_argv, sizeof(char *)
 
320
                         * ((unsigned int) new_argc + 1));
 
321
      if(plus_argv == NULL){
 
322
        perror("realloc");
 
323
        exitstatus = EXIT_FAILURE;
 
324
        goto end;
 
325
      }
 
326
      plus_argv[new_argc-1] = arg;
 
327
      plus_argv[new_argc] = NULL;
 
328
      arg = strtok(NULL, delims); /* Get next argument */
 
329
    }
 
330
    ret = argp_parse (&argp, new_argc, plus_argv, 0, 0, &plugin_list);
391
331
    if (ret == ARGP_ERR_UNKNOWN){
392
 
      fprintf(stderr, "Unknown error while parsing arguments\n");
 
332
      fprintf(stderr, "Unkown error while parsing arguments\n");
393
333
      exitstatus = EXIT_FAILURE;
394
334
      goto end;
395
335
    }
500
440
    char *filename = malloc(d_name_len + strlen(plugindir) + 2);
501
441
    if (filename == NULL){
502
442
      perror("malloc");
503
 
      continue;
 
443
      exitstatus = EXIT_FAILURE;
 
444
      goto end;
504
445
    }
505
446
    strcpy(filename, plugindir); /* Spurious warning */
506
447
    strcat(filename, "/");      /* Spurious warning */
509
450
    ret = stat(filename, &st);
510
451
    if (ret == -1){
511
452
      perror("stat");
512
 
      free(filename);
513
 
      continue;
 
453
      exitstatus = EXIT_FAILURE;
 
454
      goto end;
514
455
    }
515
456
    
516
457
    if (not S_ISREG(st.st_mode) or (access(filename, X_OK) != 0)){
565
506
    }
566
507
    // Starting a new process to be watched
567
508
    pid_t pid = fork();
568
 
    if(pid == -1){
569
 
      perror("fork");
570
 
      exitstatus = EXIT_FAILURE;
571
 
      goto end;
572
 
    }
573
509
    if(pid == 0){
574
510
      /* this is the child process */
575
511
      ret = sigaction(SIGCHLD, &old_sigchld_action, NULL);
647
583
  dir = NULL;
648
584
    
649
585
  if (process_list == NULL){
650
 
    fprintf(stderr, "No plugin processes started. Incorrect plugin"
651
 
            " directory?\n");
652
 
    process_list = NULL;
 
586
    fprintf(stderr, "No plugin processes started, exiting\n");
 
587
    exitstatus = EXIT_FAILURE;
 
588
    goto end;
653
589
  }
654
590
  while(process_list){
655
591
    fd_set rfds = rfds_all;
718
654
          break;
719
655
        }
720
656
        /* This process exited nicely, so print its buffer */
721
 
 
722
 
        bool bret = print_out_password(proc->buffer, proc->buffer_length);
723
 
        if(not bret){
724
 
          perror("print_out_password");
725
 
          exitstatus = EXIT_FAILURE;
 
657
        for(size_t written = 0; written < proc->buffer_length;
 
658
            written += (size_t)ret){
 
659
          ret = TEMP_FAILURE_RETRY(write(STDOUT_FILENO,
 
660
                                         proc->buffer + written,
 
661
                                         proc->buffer_length
 
662
                                         - written));
 
663
          if(ret < 0){
 
664
            perror("write");
 
665
            exitstatus = EXIT_FAILURE;
 
666
            goto end;
 
667
          }
726
668
        }
727
669
        goto end;
728
670
      }
757
699
      }
758
700
    }
759
701
  }
760
 
 
761
 
 
 
702
  if(process_list == NULL){
 
703
    fprintf(stderr, "All plugin processes failed, exiting\n");
 
704
    exitstatus = EXIT_FAILURE;
 
705
  }
 
706
  
762
707
 end:
763
 
  
764
 
  if(process_list == NULL or exitstatus != EXIT_SUCCESS){
765
 
    /* Fallback if all plugins failed, none are found or an error occured */
766
 
    bool bret;
767
 
    fprintf(stderr, "Going to fallback mode using getpass(3)\n");
768
 
    char *passwordbuffer = getpass("Password: ");
769
 
    bret = print_out_password(passwordbuffer, strlen(passwordbuffer));
770
 
    if(not bret){
771
 
      perror("print_out_password");
772
 
      exitstatus = EXIT_FAILURE;
773
 
      goto end;
774
 
    }
775
 
  }
776
 
  
777
708
  /* Restore old signal handler */
778
709
  sigaction(SIGCHLD, &old_sigchld_action, NULL);
779
710
  
780
 
  free(custom_argv);
 
711
  free(plus_argv);
781
712
  
782
713
  /* Free the plugin list */
783
714
  for(plugin *next; plugin_list != NULL; plugin_list = next){