24
24
#define _GNU_SOURCE /* TEMP_FAILURE_RETRY() */
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(),
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(),
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(),
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(),
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(),
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(),
52
#include <fcntl.h> /* fcntl(), F_GETFD, F_SETFD,
54
#include <string.h> /* strsep, strlen(), strcpy(),
48
56
#include <errno.h> /* errno */
49
#include <argp.h> /* struct argp_option,
50
struct argp_state, struct argp,
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 */
53
67
#define BUFFER_SIZE 256
69
const char *argp_program_version = "mandos-client 1.0";
70
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
57
74
typedef struct process{
217
250
if (arg != NULL){
218
char *p = strtok(arg, ",");
252
while((p = strsep(&arg, ",")) != NULL){
253
if(strcmp(p, "") == 0){
220
256
addargument(getplugin(NULL, plugins), p);
221
p = strtok(NULL, ",");
226
261
if (arg != NULL){
227
char *name = strtok(arg, ":");
228
char *p = strtok(NULL, ":");
262
char *name = strsep(&arg, ":");
263
if(strcmp(name, "") == 0){
266
char *opt = strsep(&arg, ":");
267
if(strcmp(opt, "") == 0){
272
while((p = strsep(&opt, ",")) != NULL){
273
if(strcmp(p, "") == 0){
232
276
addargument(getplugin(name, plugins), p);
233
p = strtok(NULL, ",");
291
339
plus_argv[0] = argv[0];
292
340
plus_argv[1] = NULL;
293
arg = strtok(plus_options, delims); /* Get first argument */
342
while((arg = strsep(&plus_options, delims)) != NULL){
296
344
plus_argv = realloc(plus_argv, sizeof(char *)
297
345
* ((unsigned int) new_argc + 1));
298
346
if(plus_argv == NULL){
300
348
exitstatus = EXIT_FAILURE;
303
351
plus_argv[new_argc-1] = arg;
304
352
plus_argv[new_argc] = NULL;
305
arg = strtok(NULL, delims); /* Get next argument */
307
argp_parse (&argp, new_argc, plus_argv, 0, 0, &plugin_list);
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;
563
635
/* Bad exit by plugin */
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));
575
650
/* Remove the plugin */
656
if(process_list == NULL){
657
fprintf(stderr, "All plugin processes failed, exiting\n");
658
exitstatus = EXIT_FAILURE;
729
if(process_list == NULL or exitstatus != EXIT_SUCCESS){
730
/* Fallback if all plugins failed, none are found or an error occured */
732
fprintf(stderr, "Going to fallback mode using getpass(3)\n");
733
char *passwordbuffer = getpass("Password: ");
734
bret = print_out_password(passwordbuffer, strlen(passwordbuffer));
736
perror("print_out_password");
737
exitstatus = EXIT_FAILURE;
662
742
/* Restore old signal handler */
663
743
sigaction(SIGCHLD, &old_sigchld_action, NULL);