21
21
* Contact the authors at <mandos@fukt.bsnet.se>.
24
#include <stdio.h> /* popen(), fileno(), fprintf(),
25
stderr, STDOUT_FILENO */
26
#include <iso646.h> /* and, or, not */
27
#include <sys/types.h> /* DIR, opendir(), stat(), struct stat,
28
waitpid(), WIFEXITED(),
29
WEXITSTATUS(), wait() */
30
#include <sys/wait.h> /* wait() */
31
#include <dirent.h> /* DIR, struct dirent, opendir(),
32
readdir(), closedir() */
33
#include <sys/stat.h> /* struct stat, stat(), S_ISREG() */
34
#include <unistd.h> /* struct stat, stat(), S_ISREG(),
36
#include <fcntl.h> /* fcntl() */
37
#include <stddef.h> /* NULL */
38
#include <stdlib.h> /* EXIT_FAILURE */
39
#include <sys/select.h> /* fd_set, select(), FD_ZERO(),
40
FD_SET(), FD_ISSET() */
41
#include <string.h> /* strlen(), strcpy(), strcat() */
42
#include <stdbool.h> /* true */
43
#include <sys/wait.h> /* waitpid(), WIFEXITED(),
45
#include <errno.h> /* errno */
46
#include <argp.h> /* struct argp_option,
47
struct argp_state, struct argp,
24
#include <stdio.h> /* popen, fileno */
25
#include <iso646.h> /* and, or, not */
26
#include <sys/types.h> /* DIR, opendir, stat, struct stat, waitpid,
27
WIFEXITED, WEXITSTATUS, wait */
28
#include <sys/wait.h> /* wait */
29
#include <dirent.h> /* DIR, opendir */
30
#include <sys/stat.h> /* stat, struct stat */
31
#include <unistd.h> /* stat, struct stat, chdir */
32
#include <stdlib.h> /* EXIT_FAILURE */
33
#include <sys/select.h> /* fd_set, select, FD_ZERO, FD_SET,
35
#include <string.h> /* strlen, strcpy, strcat */
36
#include <stdbool.h> /* true */
37
#include <sys/wait.h> /* waitpid, WIFEXITED, WEXITSTATUS */
38
#include <errno.h> /* errno */
39
#include <argp.h> /* argp */
106
95
p->argv[p->argc] = NULL;
109
98
#define BUFFER_SIZE 256
111
100
const char *argp_program_version =
112
101
"plugbasedclient 0.9";
113
102
const char *argp_program_bug_address =
114
103
"<mandos@fukt.bsnet.se>";
105
"Mandos plugin runner -- Run Mandos plugins";
106
/* A description of the arguments we accept. */
107
static char args_doc[] = "";
116
109
int main(int argc, char *argv[]){
117
const char *plugindir = "/conf/conf.d/mandos/plugins.d";
110
char plugindir[] = "plugins.d";
111
size_t d_name_len, plugindir_len = sizeof(plugindir)-1;
120
113
struct dirent *dirst;
123
116
int ret, maxfd = 0;
124
117
process *process_list = NULL;
126
int exitstatus = EXIT_SUCCESS;
128
119
/* The options we understand. */
129
120
struct argp_option options[] = {
130
121
{ .name = "global-options", .key = 'g',
131
.arg = "OPTION[,OPTION[,...]]",
132
.doc = "Options passed to all plugins" },
122
.arg = "option[,option[,...]]", .flags = 0,
123
.doc = "Options effecting all plugins" },
133
124
{ .name = "options-for", .key = 'o',
134
.arg = "PLUGIN:OPTION[,OPTION[,...]]",
135
.doc = "Options passed only to specified plugin" },
136
{ .name = "disable", .key = 'd',
138
.doc = "Disable a specific plugin", .group = 1 },
139
{ .name = "plugin-dir", .key = 128,
141
.doc = "Specify a different plugin directory", .group = 2 },
142
{ .name = "debug", .key = 129,
143
.doc = "Debug mode", .group = 3 },
125
.arg = "plugin:option[,option[,...]]", .flags = 0,
126
.doc = "Options effecting only specified plugins" },
147
130
error_t parse_opt (int key, char *arg, struct argp_state *state) {
148
131
/* Get the INPUT argument from `argp_parse', which we
149
know is a pointer to our plugin list pointer. */
132
know is a pointer to our arguments structure. */
150
133
plugin **plugins = state->input;
153
136
if (arg != NULL){
154
137
char *p = strtok(arg, ",");
156
addargument(getplugin(NULL, plugins), p);
139
addarguments(getplugin(NULL, plugins), p);
157
140
p = strtok(NULL, ",");
196
166
plugin *plugin_list = NULL;
198
168
struct argp argp = { .options = options, .parser = parse_opt,
200
.doc = "Mandos plugin runner -- Run plugins" };
169
.args_doc = args_doc, .doc = doc };
202
171
argp_parse (&argp, argc, argv, 0, 0, &plugin_list);
173
/* for(plugin *p = plugin_list; p != NULL; p=p->next){ */
174
/* fprintf(stderr, "Plugin: %s has %d arguments\n", p->name ? p->name : "Global", p->argc); */
175
/* for(char **a = p->argv + 1; *a != NULL; a++){ */
176
/* fprintf(stderr, "\tArg: %s\n", *a); */
205
for(plugin *p = plugin_list; p != NULL; p=p->next){
206
fprintf(stderr, "Plugin: %s has %d arguments\n",
207
p->name ? p->name : "Global", p->argc);
208
for(char **a = p->argv; *a != NULL; a++){
209
fprintf(stderr, "\tArg: %s\n", *a);
214
182
dir = opendir(plugindir);
216
/* Set the FD_CLOEXEC flag on the directory */
218
ret = fcntl (fd, F_GETFD, 0);
220
perror("fcntl F_GETFD");
223
ret = fcntl(fd, F_SETFD, FD_CLOEXEC | ret);
225
perror("fcntl F_SETFD");
231
185
fprintf(stderr, "Can not open directory\n");
232
186
return EXIT_FAILURE;
238
192
dirst = readdir(dir);
245
199
d_name_len = strlen(dirst->d_name);
247
// Ignore dotfiles, backup files and other junk
249
bool bad_name = false;
251
const char const *bad_prefixes[] = { ".", "#", NULL };
253
const char const *bad_suffixes[] = { "~", "#", ".dpkg-new",
255
".dpkg-divert", NULL };
256
for(const char **pre = bad_prefixes; *pre != NULL; pre++){
257
size_t pre_len = strlen(*pre);
258
if((d_name_len >= pre_len)
259
and strncmp((dirst->d_name), *pre, pre_len) == 0){
261
fprintf(stderr, "Ignoring plugin dir entry name \"%s\""
262
" with bad prefix %s\n", dirst->d_name, *pre);
273
for(const char **suf = bad_suffixes; *suf != NULL; suf++){
274
size_t suf_len = strlen(*suf);
275
if((d_name_len >= suf_len)
276
and (strcmp((dirst->d_name)+d_name_len-suf_len, *suf)
279
fprintf(stderr, "Ignoring plugin dir entry name \"%s\""
280
" with bad suffix %s\n", dirst->d_name, *suf);
292
char *filename = malloc(d_name_len + strlen(plugindir) + 2);
293
if (filename == NULL){
295
exitstatus =EXIT_FAILURE;
201
// Ignore dotfiles and backup files
202
if (dirst->d_name[0] == '.'
203
or dirst->d_name[d_name_len - 1] == '~'){
207
char *filename = malloc(d_name_len + plugindir_len + 2);
298
208
strcpy(filename, plugindir);
299
209
strcat(filename, "/");
300
210
strcat(filename, dirst->d_name);
302
212
stat(filename, &st);
304
if (not S_ISREG(st.st_mode) or (access(filename, X_OK) != 0)){
306
fprintf(stderr, "Ignoring plugin dir entry name \"%s\""
307
" with bad type or mode\n", filename);
311
if(getplugin(dirst->d_name, &plugin_list)->disabled){
313
fprintf(stderr, "Ignoring disabled plugin \"%s\"",
318
// Starting a new process to be watched
327
/* this is the child process */
329
close(pipefd[0]); /* close unused read end of pipe */
330
dup2(pipefd[1], STDOUT_FILENO); /* replace our stdout */
332
plugin *p = getplugin(dirst->d_name, &plugin_list);
333
plugin *g = getplugin(NULL, &plugin_list);
334
for(char **a = g->argv + 1; *a != NULL; a++){
337
if(execv(filename, p->argv) < 0){
344
close(pipefd[1]); /* close unused write end of pipe */
345
process *new_process = malloc(sizeof(process));
346
if (new_process == NULL){
348
exitstatus = EXIT_FAILURE;
352
new_process->fd = pipefd[0];
353
new_process->buffer = malloc(BUFFER_SIZE);
354
if (new_process->buffer == NULL){
356
exitstatus = EXIT_FAILURE;
359
new_process->buffer_size = BUFFER_SIZE;
360
new_process->buffer_length = 0;
361
FD_SET(new_process->fd, &rfds_all);
363
if (maxfd < new_process->fd){
364
maxfd = new_process->fd;
368
new_process->next = process_list;
369
process_list = new_process;
214
if (S_ISREG(st.st_mode) and (access(filename, X_OK) == 0)){
215
// Starting a new process to be watched
216
process *new_process = malloc(sizeof(process));
223
new_process->pid = fork();
224
if(new_process->pid == 0){
225
/* this is the child process */
227
close(pipefd[0]); /* close unused read end of pipe */
228
dup2(pipefd[1], STDOUT_FILENO); /* replace our stdout */
230
basename = strrchr(filename, '/');
231
if (basename == NULL){
236
plugin *p = getplugin(basename, &plugin_list);
238
plugin *g = getplugin(NULL, &plugin_list);
239
for(char **a = g->argv + 1; *a != NULL; a++){
242
if(execv(filename, p->argv) < 0){
249
close(pipefd[1]); /* close unused write end of pipe */
250
new_process->fd = pipefd[0];
251
new_process->buffer = malloc(BUFFER_SIZE);
252
if (new_process->buffer == NULL){
256
new_process->buffer_size = BUFFER_SIZE;
257
new_process->buffer_length = 0;
258
FD_SET(new_process->fd, &rfds_orig);
260
if (maxfd < new_process->fd){
261
maxfd = new_process->fd;
265
new_process->next = process_list;
266
process_list = new_process;
374
272
if (process_list != NULL){
376
fd_set rfds = rfds_all;
274
fd_set rfds = rfds_orig;
377
275
int select_ret = select(maxfd+1, &rfds, NULL, NULL, NULL);
378
276
if (select_ret == -1){