/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 from using strtok to strsep

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>             /* strsep, 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
  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
 
155
188
int main(int argc, char *argv[]){
156
189
  const char *plugindir = "/conf/conf.d/mandos/plugins.d";
157
190
  size_t d_name_len;
169
202
                                      .sa_flags = SA_NOCLDSTOP };
170
203
  char *plus_options = NULL;
171
204
  char **plus_argv = NULL;
172
 
  
 
205
 
173
206
  /* Establish a signal handler */
174
207
  sigemptyset(&sigchld_action.sa_mask);
175
208
  ret = sigaddset(&sigchld_action.sa_mask, SIGCHLD);
215
248
    switch (key) {
216
249
    case 'g':
217
250
      if (arg != NULL){
218
 
        char *p = strtok(arg, ",");
219
 
        do{
 
251
        char *p;
 
252
        while((p = strsep(&arg, ",")) != NULL){
 
253
          if(strcmp(p, "") == 0){
 
254
            continue;
 
255
          }
220
256
          addargument(getplugin(NULL, plugins), p);
221
 
          p = strtok(NULL, ",");
222
 
        } while (p != NULL);
 
257
        }
223
258
      }
224
259
      break;
225
260
    case 'o':
226
261
      if (arg != NULL){
227
 
        char *name = strtok(arg, ":");
228
 
        char *p = strtok(NULL, ":");
229
 
        if(p != NULL){
230
 
          p = strtok(p, ",");
231
 
          do{
 
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
            }
232
276
            addargument(getplugin(name, plugins), p);
233
 
            p = strtok(NULL, ",");
234
 
          } while (p != NULL);
 
277
          }
235
278
        }
236
279
      }
237
280
      break;
272
315
                       .args_doc = "[+PLUS_SEPARATED_OPTIONS]",
273
316
                       .doc = "Mandos plugin runner -- Run plugins" };
274
317
  
275
 
  argp_parse (&argp, argc, argv, 0, 0, &plugin_list);  
 
318
  ret = argp_parse (&argp, argc, argv, 0, 0, &plugin_list);
 
319
  if (ret == ARGP_ERR_UNKNOWN){
 
320
    fprintf(stderr, "Unknown error while parsing arguments\n");
 
321
    exitstatus = EXIT_FAILURE;
 
322
    goto end;
 
323
  }
276
324
  
277
325
  if(plus_options){
278
326
    /* This is a mangled argument in the form of
290
338
    }
291
339
    plus_argv[0] = argv[0];
292
340
    plus_argv[1] = NULL;
293
 
    arg = strtok(plus_options, delims); /* Get first argument */
294
 
    while(arg != NULL){
 
341
 
 
342
    while((arg = strsep(&plus_options, delims)) != NULL){
295
343
      new_argc++;
296
344
      plus_argv = realloc(plus_argv, sizeof(char *)
297
345
                         * ((unsigned int) new_argc + 1));
298
346
      if(plus_argv == NULL){
299
 
        perror("malloc");
 
347
        perror("realloc");
300
348
        exitstatus = EXIT_FAILURE;
301
349
        goto end;
302
350
      }
303
351
      plus_argv[new_argc-1] = arg;
304
352
      plus_argv[new_argc] = NULL;
305
 
      arg = strtok(NULL, delims); /* Get next argument */
306
 
    }
307
 
    argp_parse (&argp, new_argc, plus_argv, 0, 0, &plugin_list);  
 
353
    }
 
354
 
 
355
    ret = argp_parse (&argp, new_argc, plus_argv, 0, 0, &plugin_list);
 
356
    if (ret == ARGP_ERR_UNKNOWN){
 
357
      fprintf(stderr, "Unknown error while parsing arguments\n");
 
358
      exitstatus = EXIT_FAILURE;
 
359
      goto end;
 
360
    }
308
361
  }
309
362
  
310
363
  if(debug){
324
377
  
325
378
  setgid(gid);
326
379
  if (ret == -1){
327
 
    perror("setuid");
 
380
    perror("setgid");
328
381
  }
329
382
  
330
383
  dir = opendir(plugindir);
354
407
    
355
408
    // All directory entries have been processed
356
409
    if(dirst == NULL){
 
410
      if (errno == EBADF){
 
411
        perror("readdir");
 
412
        exitstatus = EXIT_FAILURE;
 
413
        goto end;
 
414
      }
357
415
      break;
358
416
    }
359
417
    
407
465
    char *filename = malloc(d_name_len + strlen(plugindir) + 2);
408
466
    if (filename == NULL){
409
467
      perror("malloc");
410
 
      exitstatus = EXIT_FAILURE;
411
 
      goto end;
 
468
      continue;
412
469
    }
413
470
    strcpy(filename, plugindir); /* Spurious warning */
414
471
    strcat(filename, "/");      /* Spurious warning */
415
472
    strcat(filename, dirst->d_name); /* Spurious warning */
416
473
    
417
 
    stat(filename, &st);
 
474
    ret = stat(filename, &st);
 
475
    if (ret == -1){
 
476
      perror("stat");
 
477
      free(filename);
 
478
      continue;
 
479
    }
418
480
    
419
481
    if (not S_ISREG(st.st_mode) or (access(filename, X_OK) != 0)){
420
482
      if(debug){
468
530
    }
469
531
    // Starting a new process to be watched
470
532
    pid_t pid = fork();
 
533
    if(pid == -1){
 
534
      perror("fork");
 
535
      exitstatus = EXIT_FAILURE;
 
536
      goto end;
 
537
    }
471
538
    if(pid == 0){
472
539
      /* this is the child process */
473
540
      ret = sigaction(SIGCHLD, &old_sigchld_action, NULL);
480
547
        perror("sigprocmask");
481
548
        _exit(EXIT_FAILURE);
482
549
      }
483
 
      dup2(pipefd[1], STDOUT_FILENO); /* replace our stdout */
 
550
 
 
551
      ret = dup2(pipefd[1], STDOUT_FILENO); /* replace our stdout */
 
552
      if(ret == -1){
 
553
        perror("dup2");
 
554
        _exit(EXIT_FAILURE);
 
555
      }
484
556
      
485
557
      if(dirfd(dir) < 0){
486
558
        /* If dir has no file descriptor, we could not set FD_CLOEXEC
538
610
  
539
611
  closedir(dir);
540
612
  dir = NULL;
541
 
  
 
613
    
542
614
  if (process_list == NULL){
543
 
    fprintf(stderr, "No plugin processes started, exiting\n");
544
 
    exitstatus = EXIT_FAILURE;
545
 
    goto end;
 
615
    fprintf(stderr, "No plugin processes started. Incorrect plugin"
 
616
            " directory?\n");
 
617
    process_list = NULL;
546
618
  }
547
619
  while(process_list){
548
620
    fd_set rfds = rfds_all;
563
635
          /* Bad exit by plugin */
564
636
          if(debug){
565
637
            if(WIFEXITED(proc->status)){
566
 
              fprintf(stderr, "Plugin %d exited with status %d\n",
567
 
                      proc->pid, WEXITSTATUS(proc->status));
 
638
              fprintf(stderr, "Plugin %u exited with status %d\n",
 
639
                      (unsigned int) (proc->pid),
 
640
                      WEXITSTATUS(proc->status));
568
641
            } else if(WIFSIGNALED(proc->status)) {
569
 
              fprintf(stderr, "Plugin %d killed by signal %d\n",
570
 
                      proc->pid, WTERMSIG(proc->status));
 
642
              fprintf(stderr, "Plugin %u killed by signal %d\n",
 
643
                      (unsigned int) (proc->pid),
 
644
                      WTERMSIG(proc->status));
571
645
            } else if(WCOREDUMP(proc->status)){
572
 
              fprintf(stderr, "Plugin %d dumped core\n", proc->pid);
 
646
              fprintf(stderr, "Plugin %d dumped core\n",
 
647
                      (unsigned int) (proc->pid));
573
648
            }
574
649
          }
575
650
          /* Remove the plugin */
608
683
          break;
609
684
        }
610
685
        /* 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
 
          }
 
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;
622
691
        }
623
692
        goto end;
624
693
      }
653
722
      }
654
723
    }
655
724
  }
656
 
  if(process_list == NULL){
657
 
    fprintf(stderr, "All plugin processes failed, exiting\n");
658
 
    exitstatus = EXIT_FAILURE;
659
 
  }
660
 
  
 
725
 
 
726
 
661
727
 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
  
662
742
  /* Restore old signal handler */
663
743
  sigaction(SIGCHLD, &old_sigchld_action, NULL);
664
744
  
679
759
  for(process *next; process_list != NULL; process_list = next){
680
760
    next = process_list->next;
681
761
    close(process_list->fd);
682
 
    kill(process_list->pid, SIGTERM);
 
762
    ret = kill(process_list->pid, SIGTERM);
 
763
    if(ret == -1 and errno != ESRCH){
 
764
      /* set-uid proccesses migth not get closed */
 
765
      perror("kill");
 
766
    }
683
767
    free(process_list->buffer);
684
768
    free(process_list);
685
769
  }