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;
110
* Based on the example in the GNU LibC manual chapter 13.13 "File
112
* *Note File Descriptor Flags:(libc)Descriptor Flags.
114
int set_cloexec_flag(int fd)
116
int ret = fcntl(fd, F_GETFD, 0);
117
/* If reading the flags failed, return error indication now. */
121
/* Store modified flag word in the descriptor. */
122
return fcntl(fd, F_SETFD, ret | FD_CLOEXEC);
126
98
#define BUFFER_SIZE 256
128
100
const char *argp_program_version =
129
101
"plugbasedclient 0.9";
130
102
const char *argp_program_bug_address =
131
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[] = "";
133
109
int main(int argc, char *argv[]){
134
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;
137
113
struct dirent *dirst;
140
116
int ret, maxfd = 0;
141
117
process *process_list = NULL;
143
int exitstatus = EXIT_SUCCESS;
145
119
/* The options we understand. */
146
120
struct argp_option options[] = {
147
121
{ .name = "global-options", .key = 'g',
148
.arg = "OPTION[,OPTION[,...]]",
149
.doc = "Options passed to all plugins" },
122
.arg = "option[,option[,...]]", .flags = 0,
123
.doc = "Options effecting all plugins" },
150
124
{ .name = "options-for", .key = 'o',
151
.arg = "PLUGIN:OPTION[,OPTION[,...]]",
152
.doc = "Options passed only to specified plugin" },
153
{ .name = "disable", .key = 'd',
155
.doc = "Disable a specific plugin", .group = 1 },
156
{ .name = "plugin-dir", .key = 128,
158
.doc = "Specify a different plugin directory", .group = 2 },
159
{ .name = "debug", .key = 129,
160
.doc = "Debug mode", .group = 3 },
125
.arg = "plugin:option[,option[,...]]", .flags = 0,
126
.doc = "Options effecting only specified plugins" },
164
130
error_t parse_opt (int key, char *arg, struct argp_state *state) {
165
131
/* Get the INPUT argument from `argp_parse', which we
166
know is a pointer to our plugin list pointer. */
132
know is a pointer to our arguments structure. */
167
133
plugin **plugins = state->input;
170
136
if (arg != NULL){
171
137
char *p = strtok(arg, ",");
173
addargument(getplugin(NULL, plugins), p);
139
addarguments(getplugin(NULL, plugins), p);
174
140
p = strtok(NULL, ",");
213
166
plugin *plugin_list = NULL;
215
168
struct argp argp = { .options = options, .parser = parse_opt,
217
.doc = "Mandos plugin runner -- Run plugins" };
169
.args_doc = args_doc, .doc = doc };
219
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); */
222
for(plugin *p = plugin_list; p != NULL; p=p->next){
223
fprintf(stderr, "Plugin: %s has %d arguments\n",
224
p->name ? p->name : "Global", p->argc - 1);
225
for(char **a = p->argv; *a != NULL; a++){
226
fprintf(stderr, "\tArg: %s\n", *a);
231
182
dir = opendir(plugindir);
232
/* Set the FD_CLOEXEC flag on the directory */
233
ret = set_cloexec_flag(dirfd(dir));
235
perror("set_cloexec_flag");
240
185
fprintf(stderr, "Can not open directory\n");
241
186
return EXIT_FAILURE;
247
192
dirst = readdir(dir);
254
199
d_name_len = strlen(dirst->d_name);
256
// Ignore dotfiles, backup files and other junk
258
bool bad_name = false;
260
const char const *bad_prefixes[] = { ".", "#", NULL };
262
const char const *bad_suffixes[] = { "~", "#", ".dpkg-new",
264
".dpkg-divert", NULL };
265
for(const char **pre = bad_prefixes; *pre != NULL; pre++){
266
size_t pre_len = strlen(*pre);
267
if((d_name_len >= pre_len)
268
and strncmp((dirst->d_name), *pre, pre_len) == 0){
270
fprintf(stderr, "Ignoring plugin dir entry name \"%s\""
271
" with bad prefix %s\n", dirst->d_name, *pre);
282
for(const char **suf = bad_suffixes; *suf != NULL; suf++){
283
size_t suf_len = strlen(*suf);
284
if((d_name_len >= suf_len)
285
and (strcmp((dirst->d_name)+d_name_len-suf_len, *suf)
288
fprintf(stderr, "Ignoring plugin dir entry name \"%s\""
289
" with bad suffix %s\n", dirst->d_name, *suf);
301
char *filename = malloc(d_name_len + strlen(plugindir) + 2);
302
if (filename == NULL){
304
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);
307
208
strcpy(filename, plugindir);
308
209
strcat(filename, "/");
309
210
strcat(filename, dirst->d_name);
311
212
stat(filename, &st);
313
if (not S_ISREG(st.st_mode) or (access(filename, X_OK) != 0)){
315
fprintf(stderr, "Ignoring plugin dir entry name \"%s\""
316
" with bad type or mode\n", filename);
320
if(getplugin(dirst->d_name, &plugin_list)->disabled){
322
fprintf(stderr, "Ignoring disabled plugin \"%s\"",
327
// Starting a new process to be watched
334
plugin *p = getplugin(dirst->d_name, &plugin_list);
336
/* Add global arguments to argument list for this plugin */
337
plugin *g = getplugin(NULL, &plugin_list);
338
for(char **a = g->argv + 1; *a != NULL; a++){
344
/* this is the child process */
346
close(pipefd[0]); /* close unused read end of pipe */
347
dup2(pipefd[1], STDOUT_FILENO); /* replace our stdout */
349
if(execv(filename, p->argv) < 0){
356
close(pipefd[1]); /* close unused write end of pipe */
357
process *new_process = malloc(sizeof(process));
358
if (new_process == NULL){
360
exitstatus = EXIT_FAILURE;
364
new_process->fd = pipefd[0];
365
new_process->buffer = malloc(BUFFER_SIZE);
366
if (new_process->buffer == NULL){
368
exitstatus = EXIT_FAILURE;
371
new_process->buffer_size = BUFFER_SIZE;
372
new_process->buffer_length = 0;
373
FD_SET(new_process->fd, &rfds_all);
375
if (maxfd < new_process->fd){
376
maxfd = new_process->fd;
380
new_process->next = process_list;
381
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;
386
272
if (process_list != NULL){
388
fd_set rfds = rfds_all;
274
fd_set rfds = rfds_orig;
389
275
int select_ret = select(maxfd+1, &rfds, NULL, NULL, NULL);
390
276
if (select_ret == -1){