/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

  • Committer: Teddy Hogeborn
  • Date: 2008-08-10 16:13:23 UTC
  • Revision ID: teddy@fukt.bsnet.se-20080810161323-wrh1rbpdrq0otuhf
* mandos (console): Define handler globally.
  (main): If in debug mode, also reduce consol log level.  Remove
          console log handler before daemonizing.

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
170
170
  proc->completed = true;
171
171
}
172
172
 
173
 
bool print_out_password(const char *buffer, size_t length){
174
 
  ssize_t ret;
175
 
  if(length>0 and buffer[length-1] == '\n'){
176
 
    length--;
177
 
  }
178
 
  for(size_t written = 0; written < length; written += (size_t)ret){
179
 
    ret = TEMP_FAILURE_RETRY(write(STDOUT_FILENO, buffer + written,
180
 
                                   length - written));
181
 
    if(ret < 0){
182
 
      return false;
183
 
    }
184
 
  }
185
 
  return true;
186
 
}
187
 
 
188
173
int main(int argc, char *argv[]){
189
174
  const char *plugindir = "/conf/conf.d/mandos/plugins.d";
190
175
  size_t d_name_len;
202
187
                                      .sa_flags = SA_NOCLDSTOP };
203
188
  char *plus_options = NULL;
204
189
  char **plus_argv = NULL;
205
 
 
 
190
  
206
191
  /* Establish a signal handler */
207
192
  sigemptyset(&sigchld_action.sa_mask);
208
193
  ret = sigaddset(&sigchld_action.sa_mask, SIGCHLD);
248
233
    switch (key) {
249
234
    case 'g':
250
235
      if (arg != NULL){
251
 
        char *p;
252
 
        while((p = strsep(&arg, ",")) != NULL){
253
 
          if(strcmp(p, "") == 0){
254
 
            continue;
255
 
          }
 
236
        char *p = strtok(arg, ",");
 
237
        do{
256
238
          addargument(getplugin(NULL, plugins), p);
257
 
        }
 
239
          p = strtok(NULL, ",");
 
240
        } while (p != NULL);
258
241
      }
259
242
      break;
260
243
    case 'o':
261
244
      if (arg != NULL){
262
 
        char *name = strsep(&arg, ":");
263
 
        if(strcmp(name, "") == 0){
264
 
          break;
265
 
        }
266
 
        char *opt = strsep(&arg, ":");
267
 
        if(strcmp(opt, "") == 0){
268
 
          break;
269
 
        }
270
 
        if(opt != NULL){
271
 
          char *p;
272
 
          while((p = strsep(&opt, ",")) != NULL){
273
 
            if(strcmp(p, "") == 0){
274
 
              continue;
275
 
            }
 
245
        char *name = strtok(arg, ":");
 
246
        char *p = strtok(NULL, ":");
 
247
        if(p != NULL){
 
248
          p = strtok(p, ",");
 
249
          do{
276
250
            addargument(getplugin(name, plugins), p);
277
 
          }
 
251
            p = strtok(NULL, ",");
 
252
          } while (p != NULL);
278
253
        }
279
254
      }
280
255
      break;
317
292
  
318
293
  ret = argp_parse (&argp, argc, argv, 0, 0, &plugin_list);
319
294
  if (ret == ARGP_ERR_UNKNOWN){
320
 
    fprintf(stderr, "Unknown error while parsing arguments\n");
 
295
    fprintf(stderr, "Unkown error while parsing arguments\n");
321
296
    exitstatus = EXIT_FAILURE;
322
297
    goto end;
323
298
  }
338
313
    }
339
314
    plus_argv[0] = argv[0];
340
315
    plus_argv[1] = NULL;
341
 
 
342
 
    while((arg = strsep(&plus_options, delims)) != NULL){
 
316
    arg = strtok(plus_options, delims); /* Get first argument */
 
317
    while(arg != NULL){
343
318
      new_argc++;
344
319
      plus_argv = realloc(plus_argv, sizeof(char *)
345
320
                         * ((unsigned int) new_argc + 1));
350
325
      }
351
326
      plus_argv[new_argc-1] = arg;
352
327
      plus_argv[new_argc] = NULL;
 
328
      arg = strtok(NULL, delims); /* Get next argument */
353
329
    }
354
 
 
355
330
    ret = argp_parse (&argp, new_argc, plus_argv, 0, 0, &plugin_list);
356
331
    if (ret == ARGP_ERR_UNKNOWN){
357
 
      fprintf(stderr, "Unknown error while parsing arguments\n");
 
332
      fprintf(stderr, "Unkown error while parsing arguments\n");
358
333
      exitstatus = EXIT_FAILURE;
359
334
      goto end;
360
335
    }
465
440
    char *filename = malloc(d_name_len + strlen(plugindir) + 2);
466
441
    if (filename == NULL){
467
442
      perror("malloc");
468
 
      continue;
 
443
      exitstatus = EXIT_FAILURE;
 
444
      goto end;
469
445
    }
470
446
    strcpy(filename, plugindir); /* Spurious warning */
471
447
    strcat(filename, "/");      /* Spurious warning */
474
450
    ret = stat(filename, &st);
475
451
    if (ret == -1){
476
452
      perror("stat");
477
 
      free(filename);
478
 
      continue;
 
453
      exitstatus = EXIT_FAILURE;
 
454
      goto end;
479
455
    }
480
456
    
481
457
    if (not S_ISREG(st.st_mode) or (access(filename, X_OK) != 0)){
530
506
    }
531
507
    // Starting a new process to be watched
532
508
    pid_t pid = fork();
533
 
    if(pid == -1){
534
 
      perror("fork");
535
 
      exitstatus = EXIT_FAILURE;
536
 
      goto end;
537
 
    }
538
509
    if(pid == 0){
539
510
      /* this is the child process */
540
511
      ret = sigaction(SIGCHLD, &old_sigchld_action, NULL);
612
583
  dir = NULL;
613
584
    
614
585
  if (process_list == NULL){
615
 
    fprintf(stderr, "No plugin processes started. Incorrect plugin"
616
 
            " directory?\n");
617
 
    process_list = NULL;
 
586
    fprintf(stderr, "No plugin processes started, exiting\n");
 
587
    exitstatus = EXIT_FAILURE;
 
588
    goto end;
618
589
  }
619
590
  while(process_list){
620
591
    fd_set rfds = rfds_all;
683
654
          break;
684
655
        }
685
656
        /* This process exited nicely, so print its buffer */
686
 
 
687
 
        bool bret = print_out_password(proc->buffer, proc->buffer_length);
688
 
        if(not bret){
689
 
          perror("print_out_password");
690
 
          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
          }
691
668
        }
692
669
        goto end;
693
670
      }
722
699
      }
723
700
    }
724
701
  }
725
 
 
726
 
 
 
702
  if(process_list == NULL){
 
703
    fprintf(stderr, "All plugin processes failed, exiting\n");
 
704
    exitstatus = EXIT_FAILURE;
 
705
  }
 
706
  
727
707
 end:
728
 
  
729
 
  if(process_list == NULL or exitstatus != EXIT_SUCCESS){
730
 
    /* Fallback if all plugins failed, none are found or an error occured */
731
 
    bool bret;
732
 
    fprintf(stderr, "Going to fallback mode using getpass(3)\n");
733
 
    char *passwordbuffer = getpass("Password: ");
734
 
    bret = print_out_password(passwordbuffer, strlen(passwordbuffer));
735
 
    if(not bret){
736
 
      perror("print_out_password");
737
 
      exitstatus = EXIT_FAILURE;
738
 
      goto end;
739
 
    }
740
 
  }
741
 
  
742
708
  /* Restore old signal handler */
743
709
  sigaction(SIGCHLD, &old_sigchld_action, NULL);
744
710