21
21
* Contact the authors at <mandos@fukt.bsnet.se>.
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 */
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,
97
106
p->argv[p->argc] = NULL;
100
109
#define BUFFER_SIZE 256
102
111
const char *argp_program_version =
103
112
"plugbasedclient 0.9";
104
113
const char *argp_program_bug_address =
105
114
"<mandos@fukt.bsnet.se>";
107
"Mandos plugin runner -- Run Mandos plugins";
108
/* A description of the arguments we accept. */
109
static char args_doc[] = "";
111
116
int main(int argc, char *argv[]){
112
const char *plugindir = "plugins.d";
117
const char *plugindir = "/conf/conf.d/mandos/plugins.d";
113
118
size_t d_name_len;
115
120
struct dirent *dirst;
118
123
int ret, maxfd = 0;
119
124
process *process_list = NULL;
126
int exitstatus = EXIT_SUCCESS;
121
128
/* The options we understand. */
122
129
struct argp_option options[] = {
123
130
{ .name = "global-options", .key = 'g',
124
.arg = "option[,option[,...]]", .flags = 0,
125
.doc = "Options effecting all plugins" },
131
.arg = "OPTION[,OPTION[,...]]",
132
.doc = "Options passed to all plugins" },
126
133
{ .name = "options-for", .key = 'o',
127
.arg = "plugin:option[,option[,...]]", .flags = 0,
128
.doc = "Options effecting only specified plugins" },
129
{ .name = "disable-plugin", .key = 'd',
130
.arg = "Plugin[,Plugin[,...]]", .flags = 0,
131
.doc = "Option to disable specififed plugins" },
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 },
132
139
{ .name = "plugin-dir", .key = 128,
133
.arg = "Directory", .flags = 0,
134
.doc = "Option to change directory to search for plugins" },
141
.doc = "Specify a different plugin directory", .group = 2 },
142
{ .name = "debug", .key = 129,
143
.doc = "Debug mode", .group = 3 },
138
147
error_t parse_opt (int key, char *arg, struct argp_state *state) {
139
148
/* Get the INPUT argument from `argp_parse', which we
140
know is a pointer to our arguments structure. */
149
know is a pointer to our plugin list pointer. */
141
150
plugin **plugins = state->input;
144
153
if (arg != NULL){
145
154
char *p = strtok(arg, ",");
147
addarguments(getplugin(NULL, plugins), p);
156
addargument(getplugin(NULL, plugins), p);
148
157
p = strtok(NULL, ",");
186
196
plugin *plugin_list = NULL;
188
198
struct argp argp = { .options = options, .parser = parse_opt,
189
.args_doc = args_doc, .doc = doc };
200
.doc = "Mandos plugin runner -- Run plugins" };
191
202
argp_parse (&argp, argc, argv, 0, 0, &plugin_list);
193
/* for(plugin *p = plugin_list; p != NULL; p=p->next){ */
194
/* fprintf(stderr, "Plugin: %s has %d arguments\n", p->name ? p->name : "Global", p->argc); */
195
/* for(char **a = p->argv + 1; *a != NULL; a++){ */
196
/* 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);
202
214
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");
205
231
fprintf(stderr, "Can not open directory\n");
206
232
return EXIT_FAILURE;
212
238
dirst = readdir(dir);
219
245
d_name_len = strlen(dirst->d_name);
221
// Ignore dotfiles and backup files
222
if (dirst->d_name[0] == '.'
223
or dirst->d_name[d_name_len - 1] == '~'){
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);
227
292
char *filename = malloc(d_name_len + strlen(plugindir) + 2);
293
if (filename == NULL){
295
exitstatus =EXIT_FAILURE;
228
298
strcpy(filename, plugindir);
229
299
strcat(filename, "/");
230
300
strcat(filename, dirst->d_name);
232
302
stat(filename, &st);
234
if (S_ISREG(st.st_mode)
235
and (access(filename, X_OK) == 0)
236
and not (getplugin(dirst->d_name, &plugin_list)->disable)){
237
// Starting a new process to be watched
238
process *new_process = malloc(sizeof(process));
245
new_process->pid = fork();
246
if(new_process->pid == 0){
247
/* this is the child process */
249
close(pipefd[0]); /* close unused read end of pipe */
250
dup2(pipefd[1], STDOUT_FILENO); /* replace our stdout */
252
plugin *p = getplugin(dirst->d_name, &plugin_list);
253
plugin *g = getplugin(NULL, &plugin_list);
254
for(char **a = g->argv + 1; *a != NULL; a++){
257
if(execv(filename, p->argv) < 0){
264
close(pipefd[1]); /* close unused write end of pipe */
265
new_process->fd = pipefd[0];
266
new_process->buffer = malloc(BUFFER_SIZE);
267
if (new_process->buffer == NULL){
271
new_process->buffer_size = BUFFER_SIZE;
272
new_process->buffer_length = 0;
273
FD_SET(new_process->fd, &rfds_orig);
275
if (maxfd < new_process->fd){
276
maxfd = new_process->fd;
280
new_process->next = process_list;
281
process_list = new_process;
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;
287
374
if (process_list != NULL){
289
fd_set rfds = rfds_orig;
376
fd_set rfds = rfds_all;
290
377
int select_ret = select(maxfd+1, &rfds, NULL, NULL, NULL);
291
378
if (select_ret == -1){