24
24
#define _GNU_SOURCE /* TEMP_FAILURE_RETRY() */
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() */
26
#include <stdio.h> /* popen(), fileno(), fprintf(),
27
stderr, STDOUT_FILENO */
42
28
#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() */
43
34
#include <dirent.h> /* DIR, struct dirent, opendir(),
44
readdir(), closedir(), dirfd() */
35
readdir(), closedir() */
36
#include <sys/stat.h> /* struct stat, stat(), S_ISREG() */
45
37
#include <unistd.h> /* struct stat, stat(), S_ISREG(),
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(),
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(),
56
48
#include <errno.h> /* errno */
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 */
49
#include <argp.h> /* struct argp_option,
50
struct argp_state, struct argp,
67
53
#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>";
74
57
typedef struct process{
250
217
if (arg != NULL){
252
while((p = strsep(&arg, ",")) != NULL){
253
if(strcmp(p, "") == 0){
218
char *p = strtok(arg, ",");
256
220
addargument(getplugin(NULL, plugins), p);
221
p = strtok(NULL, ",");
261
226
if (arg != 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){
227
char *name = strtok(arg, ":");
228
char *p = strtok(NULL, ":");
276
232
addargument(getplugin(name, plugins), p);
233
p = strtok(NULL, ",");
339
291
plus_argv[0] = argv[0];
340
292
plus_argv[1] = NULL;
342
while((arg = strsep(&plus_options, delims)) != NULL){
293
arg = strtok(plus_options, delims); /* Get first argument */
344
296
plus_argv = realloc(plus_argv, sizeof(char *)
345
297
* ((unsigned int) new_argc + 1));
346
298
if(plus_argv == NULL){
348
300
exitstatus = EXIT_FAILURE;
351
303
plus_argv[new_argc-1] = arg;
352
304
plus_argv[new_argc] = NULL;
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;
305
arg = strtok(NULL, delims); /* Get next argument */
307
argp_parse (&argp, new_argc, plus_argv, 0, 0, &plugin_list);
635
563
/* Bad exit by plugin */
637
565
if(WIFEXITED(proc->status)){
638
fprintf(stderr, "Plugin %u exited with status %d\n",
639
(unsigned int) (proc->pid),
640
WEXITSTATUS(proc->status));
566
fprintf(stderr, "Plugin %d exited with status %d\n",
567
proc->pid, WEXITSTATUS(proc->status));
641
568
} else if(WIFSIGNALED(proc->status)) {
642
fprintf(stderr, "Plugin %u killed by signal %d\n",
643
(unsigned int) (proc->pid),
644
WTERMSIG(proc->status));
569
fprintf(stderr, "Plugin %d killed by signal %d\n",
570
proc->pid, WTERMSIG(proc->status));
645
571
} else if(WCOREDUMP(proc->status)){
646
fprintf(stderr, "Plugin %d dumped core\n",
647
(unsigned int) (proc->pid));
572
fprintf(stderr, "Plugin %d dumped core\n", proc->pid);
650
575
/* 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;
742
662
/* Restore old signal handler */
743
663
sigaction(SIGCHLD, &old_sigchld_action, NULL);