/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

changed description to better fit role

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>             /* 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 CONFFILE "/conf/conf.d/mandos/client.conf"
69
68
 
70
69
const char *argp_program_version = "mandos-client 1.0";
71
70
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
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
174
  const char *plugindir = "/conf/conf.d/mandos/plugins.d";
213
 
  const char *conffile = CONFFILE;
214
 
  FILE *conffp;
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(conffile, "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 harmfull errors */
380
 
    if (errno == EMFILE or errno == ENFILE or errno == ENOMEM){
381
 
      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");
382
311
      exitstatus = EXIT_FAILURE;
383
312
      goto end;
384
313
    }
385
 
  }
386
 
 
387
 
  if(custom_argv != NULL){
388
 
    custom_argv[0] = argv[0];
389
 
    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);
390
331
    if (ret == ARGP_ERR_UNKNOWN){
391
 
      fprintf(stderr, "Unknown error while parsing arguments\n");
 
332
      fprintf(stderr, "Unkown error while parsing arguments\n");
392
333
      exitstatus = EXIT_FAILURE;
393
334
      goto end;
394
335
    }
499
440
    char *filename = malloc(d_name_len + strlen(plugindir) + 2);
500
441
    if (filename == NULL){
501
442
      perror("malloc");
502
 
      continue;
 
443
      exitstatus = EXIT_FAILURE;
 
444
      goto end;
503
445
    }
504
446
    strcpy(filename, plugindir); /* Spurious warning */
505
447
    strcat(filename, "/");      /* Spurious warning */
508
450
    ret = stat(filename, &st);
509
451
    if (ret == -1){
510
452
      perror("stat");
511
 
      free(filename);
512
 
      continue;
 
453
      exitstatus = EXIT_FAILURE;
 
454
      goto end;
513
455
    }
514
456
    
515
457
    if (not S_ISREG(st.st_mode) or (access(filename, X_OK) != 0)){
564
506
    }
565
507
    // Starting a new process to be watched
566
508
    pid_t pid = fork();
567
 
    if(pid == -1){
568
 
      perror("fork");
569
 
      exitstatus = EXIT_FAILURE;
570
 
      goto end;
571
 
    }
572
509
    if(pid == 0){
573
510
      /* this is the child process */
574
511
      ret = sigaction(SIGCHLD, &old_sigchld_action, NULL);
646
583
  dir = NULL;
647
584
    
648
585
  if (process_list == NULL){
649
 
    fprintf(stderr, "No plugin processes started. Incorrect plugin"
650
 
            " directory?\n");
651
 
    process_list = NULL;
 
586
    fprintf(stderr, "No plugin processes started, exiting\n");
 
587
    exitstatus = EXIT_FAILURE;
 
588
    goto end;
652
589
  }
653
590
  while(process_list){
654
591
    fd_set rfds = rfds_all;
669
606
          /* Bad exit by plugin */
670
607
          if(debug){
671
608
            if(WIFEXITED(proc->status)){
672
 
              fprintf(stderr, "Plugin %u exited with status %d\n",
673
 
                      (unsigned int) (proc->pid),
674
 
                      WEXITSTATUS(proc->status));
 
609
              fprintf(stderr, "Plugin %d exited with status %d\n",
 
610
                      proc->pid, WEXITSTATUS(proc->status));
675
611
            } else if(WIFSIGNALED(proc->status)) {
676
 
              fprintf(stderr, "Plugin %u killed by signal %d\n",
677
 
                      (unsigned int) (proc->pid),
678
 
                      WTERMSIG(proc->status));
 
612
              fprintf(stderr, "Plugin %d killed by signal %d\n",
 
613
                      proc->pid, WTERMSIG(proc->status));
679
614
            } else if(WCOREDUMP(proc->status)){
680
 
              fprintf(stderr, "Plugin %d dumped core\n",
681
 
                      (unsigned int) (proc->pid));
 
615
              fprintf(stderr, "Plugin %d dumped core\n", proc->pid);
682
616
            }
683
617
          }
684
618
          /* Remove the plugin */
717
651
          break;
718
652
        }
719
653
        /* This process exited nicely, so print its buffer */
720
 
 
721
 
        bool bret = print_out_password(proc->buffer, proc->buffer_length);
722
 
        if(not bret){
723
 
          perror("print_out_password");
724
 
          exitstatus = EXIT_FAILURE;
 
654
        for(size_t written = 0; written < proc->buffer_length;
 
655
            written += (size_t)ret){
 
656
          ret = TEMP_FAILURE_RETRY(write(STDOUT_FILENO,
 
657
                                         proc->buffer + written,
 
658
                                         proc->buffer_length
 
659
                                         - written));
 
660
          if(ret < 0){
 
661
            perror("write");
 
662
            exitstatus = EXIT_FAILURE;
 
663
            goto end;
 
664
          }
725
665
        }
726
666
        goto end;
727
667
      }
756
696
      }
757
697
    }
758
698
  }
759
 
 
760
 
 
 
699
  if(process_list == NULL){
 
700
    fprintf(stderr, "All plugin processes failed, exiting\n");
 
701
    exitstatus = EXIT_FAILURE;
 
702
  }
 
703
  
761
704
 end:
762
 
  
763
 
  if(process_list == NULL or exitstatus != EXIT_SUCCESS){
764
 
    /* Fallback if all plugins failed, none are found or an error occured */
765
 
    bool bret;
766
 
    fprintf(stderr, "Going to fallback mode using getpass(3)\n");
767
 
    char *passwordbuffer = getpass("Password: ");
768
 
    bret = print_out_password(passwordbuffer, strlen(passwordbuffer));
769
 
    if(not bret){
770
 
      perror("print_out_password");
771
 
      exitstatus = EXIT_FAILURE;
772
 
      goto end;
773
 
    }
774
 
  }
775
 
  
776
705
  /* Restore old signal handler */
777
706
  sigaction(SIGCHLD, &old_sigchld_action, NULL);
778
707
  
779
 
  free(custom_argv);
 
708
  free(plus_argv);
780
709
  
781
710
  /* Free the plugin list */
782
711
  for(plugin *next; plugin_list != NULL; plugin_list = next){