/mandos/release

To get this branch, use:
bzr branch http://bzr.recompile.se/loggerhead/mandos/release

« back to all changes in this revision

Viewing changes to mandos-client.c

  • Committer: Björn Påhlsson
  • Date: 2008-08-09 04:42:39 UTC
  • mto: (237.7.1 mandos) (24.1.154 mandos)
  • mto: This revision was merged to the branch mainline in revision 53.
  • Revision ID: belorn@braxen-20080809044239-a1ub3rvi88ydwvla
tally count of used symbols
return codes from functions checked.

Show diffs side-by-side

added added

removed removed

Lines of Context:
23
23
 
24
24
#define _GNU_SOURCE             /* TEMP_FAILURE_RETRY() */
25
25
 
26
 
#include <stdio.h>              /* popen(), fileno(), fprintf(),
27
 
                                   stderr, STDOUT_FILENO */
 
26
#include <stddef.h>             /* size_t, NULL */
 
27
#include <stdlib.h>             /* malloc(), exit(), EXIT_FAILURE,
 
28
                                   EXIT_SUCCESS, realloc() */
 
29
#include <stdbool.h>            /* bool, true, false */
 
30
#include <stdio.h>              /* perror, popen(), fileno(),
 
31
                                   fprintf(), stderr, STDOUT_FILENO */
 
32
#include <sys/types.h>          /* DIR, opendir(), stat(), struct
 
33
                                   stat, waitpid(), WIFEXITED(),
 
34
                                   WEXITSTATUS(), wait(), pid_t,
 
35
                                   uid_t, gid_t, getuid(), getgid(),
 
36
                                   dirfd() */
 
37
#include <sys/select.h>         /* fd_set, select(), FD_ZERO(),
 
38
                                   FD_SET(), FD_ISSET(), FD_CLR */
 
39
#include <sys/wait.h>           /* wait(), waitpid(), WIFEXITED(),
 
40
                                   WEXITSTATUS() */
 
41
#include <sys/stat.h>           /* struct stat, stat(), S_ISREG() */
28
42
#include <iso646.h>             /* and, or, not */
29
 
#include <sys/types.h>          /* DIR, opendir(), stat(),
30
 
                                   struct stat, waitpid(),
31
 
                                   WIFEXITED(), WEXITSTATUS(),
32
 
                                   wait() */
33
 
#include <sys/wait.h>           /* wait() */
34
43
#include <dirent.h>             /* DIR, struct dirent, opendir(),
35
 
                                   readdir(), closedir() */
36
 
#include <sys/stat.h>           /* struct stat, stat(), S_ISREG() */
 
44
                                   readdir(), closedir(), dirfd() */
37
45
#include <unistd.h>             /* struct stat, stat(), S_ISREG(),
38
 
                                   fcntl() */
39
 
#include <fcntl.h>              /* fcntl() */
40
 
#include <stddef.h>             /* NULL */
41
 
#include <stdlib.h>             /* EXIT_FAILURE */
42
 
#include <sys/select.h>         /* fd_set, select(), FD_ZERO(),
43
 
                                   FD_SET(), FD_ISSET() */
44
 
#include <string.h>             /* strlen(), strcpy(), strcat() */
45
 
#include <stdbool.h>            /* true */
46
 
#include <sys/wait.h>           /* waitpid(), WIFEXITED(),
47
 
                                   WEXITSTATUS() */
 
46
                                   fcntl(), setuid(), setgid(),
 
47
                                   F_GETFD, F_SETFD, FD_CLOEXEC,
 
48
                                   access(), pipe(), fork(), close()
 
49
                                   dup2, STDOUT_FILENO, _exit(),
 
50
                                   execv(), write(), read(),
 
51
                                   close() */
 
52
#include <fcntl.h>              /* fcntl(), F_GETFD, F_SETFD,
 
53
                                   FD_CLOEXEC */
 
54
#include <string.h>             /* strtok, strlen(), strcpy(),
 
55
                                   strcat() */
48
56
#include <errno.h>              /* errno */
49
 
#include <argp.h>               /* struct argp_option,
50
 
                                   struct argp_state, struct argp,
51
 
                                   argp_parse() */
 
57
#include <argp.h>               /* struct argp_option, struct
 
58
                                   argp_state, struct argp,
 
59
                                   argp_parse(), ARGP_ERR_UNKNOWN,
 
60
                                   ARGP_KEY_END, ARGP_KEY_ARG, error_t */
 
61
#include <signal.h>             /* struct sigaction, sigemptyset(),
 
62
                                   sigaddset(), sigaction(),
 
63
                                   sigprocmask(), SIG_BLOCK, SIGCHLD,
 
64
                                   SIG_UNBLOCK, kill() */
 
65
#include <errno.h>              /* errno, EBADF */
52
66
 
53
67
#define BUFFER_SIZE 256
54
68
 
141
155
  process *proc = process_list;
142
156
  int status;
143
157
  pid_t pid = wait(&status);
 
158
  if(pid == -1){
 
159
    perror("wait");
 
160
    return;
 
161
  }
144
162
  while(proc != NULL and proc->pid != pid){
145
163
    proc = proc->next;
146
164
  }
272
290
                       .args_doc = "[+PLUS_SEPARATED_OPTIONS]",
273
291
                       .doc = "Mandos plugin runner -- Run plugins" };
274
292
  
275
 
  argp_parse (&argp, argc, argv, 0, 0, &plugin_list);  
 
293
  ret = argp_parse (&argp, argc, argv, 0, 0, &plugin_list);
 
294
  if (ret == ARGP_ERR_UNKNOWN){
 
295
    perror("argp_parse");
 
296
    exitstatus = EXIT_FAILURE;
 
297
    goto end;
 
298
  }
276
299
  
277
300
  if(plus_options){
278
301
    /* This is a mangled argument in the form of
296
319
      plus_argv = realloc(plus_argv, sizeof(char *)
297
320
                         * ((unsigned int) new_argc + 1));
298
321
      if(plus_argv == NULL){
299
 
        perror("malloc");
 
322
        perror("realloc");
300
323
        exitstatus = EXIT_FAILURE;
301
324
        goto end;
302
325
      }
304
327
      plus_argv[new_argc] = NULL;
305
328
      arg = strtok(NULL, delims); /* Get next argument */
306
329
    }
307
 
    argp_parse (&argp, new_argc, plus_argv, 0, 0, &plugin_list);  
 
330
    ret = argp_parse (&argp, new_argc, plus_argv, 0, 0, &plugin_list);
 
331
    if (ret == ARGP_ERR_UNKNOWN){
 
332
      perror("argp_parse");
 
333
      exitstatus = EXIT_FAILURE;
 
334
      goto end;
 
335
    }
308
336
  }
309
337
  
310
338
  if(debug){
354
382
    
355
383
    // All directory entries have been processed
356
384
    if(dirst == NULL){
 
385
      if (errno == EBADF){
 
386
        perror("readdir");
 
387
        exitstatus = EXIT_FAILURE;
 
388
        goto end;
 
389
      }
357
390
      break;
358
391
    }
359
392
    
414
447
    strcat(filename, "/");      /* Spurious warning */
415
448
    strcat(filename, dirst->d_name); /* Spurious warning */
416
449
    
417
 
    stat(filename, &st);
 
450
    ret = stat(filename, &st);
 
451
    if (ret == -1){
 
452
      perror("stat");
 
453
      exitstatus = EXIT_FAILURE;
 
454
      goto end;
 
455
    }
418
456
    
419
457
    if (not S_ISREG(st.st_mode) or (access(filename, X_OK) != 0)){
420
458
      if(debug){
480
518
        perror("sigprocmask");
481
519
        _exit(EXIT_FAILURE);
482
520
      }
483
 
      dup2(pipefd[1], STDOUT_FILENO); /* replace our stdout */
 
521
 
 
522
      ret = dup2(pipefd[1], STDOUT_FILENO); /* replace our stdout */
 
523
      if(ret == -1){
 
524
        perror("dup2");
 
525
        _exit(EXIT_FAILURE);
 
526
      }
484
527
      
485
528
      if(dirfd(dir) < 0){
486
529
        /* If dir has no file descriptor, we could not set FD_CLOEXEC
538
581
  
539
582
  closedir(dir);
540
583
  dir = NULL;
541
 
  
 
584
    
542
585
  if (process_list == NULL){
543
586
    fprintf(stderr, "No plugin processes started, exiting\n");
544
587
    exitstatus = EXIT_FAILURE;
679
722
  for(process *next; process_list != NULL; process_list = next){
680
723
    next = process_list->next;
681
724
    close(process_list->fd);
682
 
    kill(process_list->pid, SIGTERM);
 
725
    ret = kill(process_list->pid, SIGTERM);
 
726
    if(ret == -1 and errno != ESRCH){
 
727
      /* set-uid proccesses migth not get closed */
 
728
      perror("kill");
 
729
    }
683
730
    free(process_list->buffer);
684
731
    free(process_list);
685
732
  }