/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 20:35:01 UTC
  • mfrom: (24.1.42 mandos)
  • Revision ID: teddy@fukt.bsnet.se-20080810203501-euh3qcf1nopilksm
Merge.

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 */
 
66
 
 
67
#define BUFFER_SIZE 256
 
68
 
 
69
const char *argp_program_version = "mandos-client 1.0";
 
70
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
52
71
 
53
72
struct process;
54
73
 
72
91
  struct plugin *next;
73
92
} plugin;
74
93
 
75
 
plugin *getplugin(char *name, plugin **plugin_list){
 
94
static plugin *getplugin(char *name, plugin **plugin_list){
76
95
  for (plugin *p = *plugin_list; p != NULL; p = p->next){
77
96
    if ((p->name == name)
78
97
        or (p->name and name and (strcmp(p->name, name) == 0))){
101
120
  return new_plugin;
102
121
}
103
122
 
104
 
void addargument(plugin *p, char *arg){
 
123
static void addargument(plugin *p, char *arg){
105
124
  p->argv[p->argc] = arg;
106
125
  p->argv = realloc(p->argv, sizeof(char *) * (size_t)(p->argc + 2));
107
126
  if (p->argv == NULL){
117
136
 * Descriptor Flags".
118
137
 * *Note File Descriptor Flags:(libc)Descriptor Flags.
119
138
 */
120
 
int set_cloexec_flag(int fd)
 
139
static int set_cloexec_flag(int fd)
121
140
{
122
141
  int ret = fcntl(fd, F_GETFD, 0);
123
142
  /* If reading the flags failed, return error indication now. */
128
147
  return fcntl(fd, F_SETFD, ret | FD_CLOEXEC);
129
148
}
130
149
 
131
 
#define BUFFER_SIZE 256
132
 
 
133
 
const char *argp_program_version = "plugbasedclient 0.9";
134
 
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
135
 
 
136
150
process *process_list = NULL;
137
151
 
138
152
/* Mark a process as completed when it exits, and save its exit
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
  }
152
170
  proc->completed = true;
153
171
}
154
172
 
 
173
bool print_out_password(const char *buffer, size_t length){
 
174
  size_t ret;
 
175
  for(size_t written = 0; written < length; written += ret){
 
176
    ret = TEMP_FAILURE_RETRY(write(STDOUT_FILENO, buffer + written,
 
177
                                   length - written));
 
178
    if(ret < 0){
 
179
      return false;
 
180
    }
 
181
  }
 
182
  return true;
 
183
}
 
184
 
155
185
int main(int argc, char *argv[]){
156
186
  const char *plugindir = "/conf/conf.d/mandos/plugins.d";
157
187
  size_t d_name_len;
272
302
                       .args_doc = "[+PLUS_SEPARATED_OPTIONS]",
273
303
                       .doc = "Mandos plugin runner -- Run plugins" };
274
304
  
275
 
  argp_parse (&argp, argc, argv, 0, 0, &plugin_list);  
 
305
  ret = argp_parse (&argp, argc, argv, 0, 0, &plugin_list);
 
306
  if (ret == ARGP_ERR_UNKNOWN){
 
307
    fprintf(stderr, "Unkown error while parsing arguments\n");
 
308
    exitstatus = EXIT_FAILURE;
 
309
    goto end;
 
310
  }
276
311
  
277
312
  if(plus_options){
278
313
    /* This is a mangled argument in the form of
296
331
      plus_argv = realloc(plus_argv, sizeof(char *)
297
332
                         * ((unsigned int) new_argc + 1));
298
333
      if(plus_argv == NULL){
299
 
        perror("malloc");
 
334
        perror("realloc");
300
335
        exitstatus = EXIT_FAILURE;
301
336
        goto end;
302
337
      }
304
339
      plus_argv[new_argc] = NULL;
305
340
      arg = strtok(NULL, delims); /* Get next argument */
306
341
    }
307
 
    argp_parse (&argp, new_argc, plus_argv, 0, 0, &plugin_list);  
 
342
    ret = argp_parse (&argp, new_argc, plus_argv, 0, 0, &plugin_list);
 
343
    if (ret == ARGP_ERR_UNKNOWN){
 
344
      fprintf(stderr, "Unkown error while parsing arguments\n");
 
345
      exitstatus = EXIT_FAILURE;
 
346
      goto end;
 
347
    }
308
348
  }
309
349
  
310
350
  if(debug){
324
364
  
325
365
  setgid(gid);
326
366
  if (ret == -1){
327
 
    perror("setuid");
 
367
    perror("setgid");
328
368
  }
329
369
  
330
370
  dir = opendir(plugindir);
354
394
    
355
395
    // All directory entries have been processed
356
396
    if(dirst == NULL){
 
397
      if (errno == EBADF){
 
398
        perror("readdir");
 
399
        exitstatus = EXIT_FAILURE;
 
400
        goto end;
 
401
      }
357
402
      break;
358
403
    }
359
404
    
414
459
    strcat(filename, "/");      /* Spurious warning */
415
460
    strcat(filename, dirst->d_name); /* Spurious warning */
416
461
    
417
 
    stat(filename, &st);
 
462
    ret = stat(filename, &st);
 
463
    if (ret == -1){
 
464
      perror("stat");
 
465
      exitstatus = EXIT_FAILURE;
 
466
      goto end;
 
467
    }
418
468
    
419
469
    if (not S_ISREG(st.st_mode) or (access(filename, X_OK) != 0)){
420
470
      if(debug){
480
530
        perror("sigprocmask");
481
531
        _exit(EXIT_FAILURE);
482
532
      }
483
 
      dup2(pipefd[1], STDOUT_FILENO); /* replace our stdout */
 
533
 
 
534
      ret = dup2(pipefd[1], STDOUT_FILENO); /* replace our stdout */
 
535
      if(ret == -1){
 
536
        perror("dup2");
 
537
        _exit(EXIT_FAILURE);
 
538
      }
484
539
      
485
540
      if(dirfd(dir) < 0){
486
541
        /* If dir has no file descriptor, we could not set FD_CLOEXEC
538
593
  
539
594
  closedir(dir);
540
595
  dir = NULL;
541
 
  
 
596
    
542
597
  if (process_list == NULL){
543
 
    fprintf(stderr, "No plugin processes started, exiting\n");
544
 
    exitstatus = EXIT_FAILURE;
545
 
    goto end;
 
598
    fprintf(stderr, "No plugin processes started. Incorrect plugin"
 
599
            " directory?\n");
 
600
    process_list = NULL;
546
601
  }
547
602
  while(process_list){
548
603
    fd_set rfds = rfds_all;
563
618
          /* Bad exit by plugin */
564
619
          if(debug){
565
620
            if(WIFEXITED(proc->status)){
566
 
              fprintf(stderr, "Plugin %d exited with status %d\n",
567
 
                      proc->pid, WEXITSTATUS(proc->status));
 
621
              fprintf(stderr, "Plugin %u exited with status %d\n",
 
622
                      (unsigned int) (proc->pid),
 
623
                      WEXITSTATUS(proc->status));
568
624
            } else if(WIFSIGNALED(proc->status)) {
569
 
              fprintf(stderr, "Plugin %d killed by signal %d\n",
570
 
                      proc->pid, WTERMSIG(proc->status));
 
625
              fprintf(stderr, "Plugin %u killed by signal %d\n",
 
626
                      (unsigned int) (proc->pid),
 
627
                      WTERMSIG(proc->status));
571
628
            } else if(WCOREDUMP(proc->status)){
572
 
              fprintf(stderr, "Plugin %d dumped core\n", proc->pid);
 
629
              fprintf(stderr, "Plugin %d dumped core\n",
 
630
                      (unsigned int) (proc->pid));
573
631
            }
574
632
          }
575
633
          /* Remove the plugin */
608
666
          break;
609
667
        }
610
668
        /* This process exited nicely, so print its buffer */
611
 
        for(size_t written = 0; written < proc->buffer_length;
612
 
            written += (size_t)ret){
613
 
          ret = TEMP_FAILURE_RETRY(write(STDOUT_FILENO,
614
 
                                         proc->buffer + written,
615
 
                                         proc->buffer_length
616
 
                                         - written));
617
 
          if(ret < 0){
618
 
            perror("write");
619
 
            exitstatus = EXIT_FAILURE;
620
 
            goto end;
621
 
          }
 
669
 
 
670
        bool bret = print_out_password(proc->buffer, proc->buffer_length);
 
671
        if(not bret){
 
672
          perror("print_out_password");
 
673
          exitstatus = EXIT_FAILURE;
622
674
        }
623
675
        goto end;
624
676
      }
653
705
      }
654
706
    }
655
707
  }
 
708
  
656
709
  if(process_list == NULL){
657
 
    fprintf(stderr, "All plugin processes failed, exiting\n");
658
 
    exitstatus = EXIT_FAILURE;
 
710
    bool bret;
 
711
    fprintf(stderr, "Going to fallback mode using getpass(3)\n");
 
712
    char *passwordbuffer = getpass("Password: ");
 
713
    bret = print_out_password(passwordbuffer, strlen(passwordbuffer));
 
714
    if(not bret){
 
715
      perror("print_out_password");
 
716
      exitstatus = EXIT_FAILURE;
 
717
      goto end;
 
718
    }
 
719
    bret = print_out_password("\n", 1);
 
720
    if(not bret){
 
721
      perror("print_out_password");
 
722
      exitstatus = EXIT_FAILURE;
 
723
    }
659
724
  }
660
 
  
 
725
 
661
726
 end:
 
727
  
662
728
  /* Restore old signal handler */
663
729
  sigaction(SIGCHLD, &old_sigchld_action, NULL);
664
730
  
679
745
  for(process *next; process_list != NULL; process_list = next){
680
746
    next = process_list->next;
681
747
    close(process_list->fd);
682
 
    kill(process_list->pid, SIGTERM);
 
748
    ret = kill(process_list->pid, SIGTERM);
 
749
    if(ret == -1 and errno != ESRCH){
 
750
      /* set-uid proccesses migth not get closed */
 
751
      perror("kill");
 
752
    }
683
753
    free(process_list->buffer);
684
754
    free(process_list);
685
755
  }