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,
95
106
p->argv[p->argc] = NULL;
98
109
#define BUFFER_SIZE 256
100
111
const char *argp_program_version =
101
112
"plugbasedclient 0.9";
102
113
const char *argp_program_bug_address =
103
114
"<mandos@fukt.bsnet.se>";
105
"Mandos plugin runner -- Run Mandos plugins";
106
/* A description of the arguments we accept. */
107
static char args_doc[] = "";
109
116
int main(int argc, char *argv[]){
110
char plugindir[] = "plugins.d";
111
size_t d_name_len, plugindir_len = sizeof(plugindir)-1;
117
const char *plugindir = "/conf/conf.d/mandos/plugins.d";
113
120
struct dirent *dirst;
116
123
int ret, maxfd = 0;
117
124
process *process_list = NULL;
126
int exitstatus = EXIT_SUCCESS;
119
128
/* The options we understand. */
120
129
struct argp_option options[] = {
121
130
{ .name = "global-options", .key = 'g',
122
.arg = "option[,option[,...]]", .flags = 0,
123
.doc = "Options effecting all plugins" },
131
.arg = "OPTION[,OPTION[,...]]",
132
.doc = "Options passed to all plugins" },
124
133
{ .name = "options-for", .key = 'o',
125
.arg = "plugin:option[,option[,...]]", .flags = 0,
126
.doc = "Options effecting only specified 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 },
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 },
130
147
error_t parse_opt (int key, char *arg, struct argp_state *state) {
131
148
/* Get the INPUT argument from `argp_parse', which we
132
know is a pointer to our arguments structure. */
149
know is a pointer to our plugin list pointer. */
133
150
plugin **plugins = state->input;
136
153
if (arg != NULL){
137
154
char *p = strtok(arg, ",");
139
addarguments(getplugin(NULL, plugins), p);
156
addargument(getplugin(NULL, plugins), p);
140
157
p = strtok(NULL, ",");
166
196
plugin *plugin_list = NULL;
168
198
struct argp argp = { .options = options, .parser = parse_opt,
169
.args_doc = args_doc, .doc = doc };
200
.doc = "Mandos plugin runner -- Run plugins" };
171
202
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);
182
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");
185
231
fprintf(stderr, "Can not open directory\n");
186
232
return EXIT_FAILURE;
192
238
dirst = readdir(dir);
199
245
d_name_len = strlen(dirst->d_name);
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);
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;
208
298
strcpy(filename, plugindir);
209
299
strcat(filename, "/");
210
300
strcat(filename, dirst->d_name);
212
302
stat(filename, &st);
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;
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;
272
374
if (process_list != NULL){
274
fd_set rfds = rfds_orig;
376
fd_set rfds = rfds_all;
275
377
int select_ret = select(maxfd+1, &rfds, NULL, NULL, NULL);
276
378
if (select_ret == -1){