1
/* -*- coding: utf-8 -*- */
3
* Mandos plugin runner - Run Mandos plugins
5
* Copyright © 2007-2008 Teddy Hogeborn & Björn Påhlsson
7
* This program is free software: you can redistribute it and/or
8
* modify it under the terms of the GNU General Public License as
9
* published by the Free Software Foundation, either version 3 of the
10
* License, or (at your option) any later version.
12
* This program is distributed in the hope that it will be useful, but
13
* WITHOUT ANY WARRANTY; without even the implied warranty of
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15
* General Public License for more details.
17
* You should have received a copy of the GNU General Public License
18
* along with this program. If not, see
19
* <http://www.gnu.org/licenses/>.
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,
52
typedef struct process{
61
typedef struct plugin{
62
char *name; /* can be NULL or any plugin name */
69
plugin *getplugin(char *name, plugin **plugin_list){
70
for (plugin *p = *plugin_list; p != NULL; p = p->next){
72
or (p->name and name and (strcmp(p->name, name) == 0))){
76
/* Create a new plugin */
77
plugin *new_plugin = malloc(sizeof(plugin));
78
if (new_plugin == NULL){
82
new_plugin->name = name;
83
new_plugin->argv = malloc(sizeof(char *) * 2);
84
if (new_plugin->argv == NULL){
88
new_plugin->argv[0] = name;
89
new_plugin->argv[1] = NULL;
91
new_plugin->disabled = false;
92
new_plugin->next = *plugin_list;
93
/* Append the new plugin to the list */
94
*plugin_list = new_plugin;
98
void addargument(plugin *p, char *arg){
99
p->argv[p->argc] = arg;
100
p->argv = realloc(p->argv, sizeof(char *) * (size_t)(p->argc + 2));
101
if (p->argv == NULL){
106
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
#define BUFFER_SIZE 256
128
const char *argp_program_version =
129
"plugbasedclient 0.9";
130
const char *argp_program_bug_address =
131
"<mandos@fukt.bsnet.se>";
133
int main(int argc, char *argv[]){
134
const char *plugindir = "/conf/conf.d/mandos/plugins.d";
137
struct dirent *dirst;
141
process *process_list = NULL;
143
int exitstatus = EXIT_SUCCESS;
145
/* The options we understand. */
146
struct argp_option options[] = {
147
{ .name = "global-options", .key = 'g',
148
.arg = "OPTION[,OPTION[,...]]",
149
.doc = "Options passed to all plugins" },
150
{ .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 },
164
error_t parse_opt (int key, char *arg, struct argp_state *state) {
165
/* Get the INPUT argument from `argp_parse', which we
166
know is a pointer to our plugin list pointer. */
167
plugin **plugins = state->input;
171
char *p = strtok(arg, ",");
173
addargument(getplugin(NULL, plugins), p);
174
p = strtok(NULL, ",");
180
char *name = strtok(arg, ":");
181
char *p = strtok(NULL, ":");
185
addargument(getplugin(name, plugins), p);
186
p = strtok(NULL, ",");
193
getplugin(arg, plugins)->disabled = true;
208
return ARGP_ERR_UNKNOWN;
213
plugin *plugin_list = NULL;
215
struct argp argp = { .options = options, .parser = parse_opt,
217
.doc = "Mandos plugin runner -- Run plugins" };
219
argp_parse (&argp, argc, argv, 0, 0, &plugin_list);
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
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
fprintf(stderr, "Can not open directory\n");
247
dirst = readdir(dir);
249
// All directory entries have been processed
254
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;
307
strcpy(filename, plugindir);
308
strcat(filename, "/");
309
strcat(filename, dirst->d_name);
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 */
352
if(execv(filename, p->argv) < 0){
359
close(pipefd[1]); /* close unused write end of pipe */
360
process *new_process = malloc(sizeof(process));
361
if (new_process == NULL){
363
exitstatus = EXIT_FAILURE;
367
new_process->fd = pipefd[0];
368
new_process->buffer = malloc(BUFFER_SIZE);
369
if (new_process->buffer == NULL){
371
exitstatus = EXIT_FAILURE;
374
new_process->buffer_size = BUFFER_SIZE;
375
new_process->buffer_length = 0;
376
FD_SET(new_process->fd, &rfds_all);
378
if (maxfd < new_process->fd){
379
maxfd = new_process->fd;
383
new_process->next = process_list;
384
process_list = new_process;
389
if (process_list != NULL){
391
fd_set rfds = rfds_all;
392
int select_ret = select(maxfd+1, &rfds, NULL, NULL, NULL);
393
if (select_ret == -1){
397
for(process *process_itr = process_list; process_itr != NULL;
398
process_itr = process_itr->next){
399
if(FD_ISSET(process_itr->fd, &rfds)){
400
if(process_itr->buffer_length + BUFFER_SIZE
401
> process_itr->buffer_size){
402
process_itr->buffer = realloc(process_itr->buffer,
403
process_itr->buffer_size
404
+ (size_t) BUFFER_SIZE);
405
if (process_itr->buffer == NULL){
409
process_itr->buffer_size += BUFFER_SIZE;
411
ret = read(process_itr->fd, process_itr->buffer
412
+ process_itr->buffer_length, BUFFER_SIZE);
414
/* Read error from this process; ignore it */
417
process_itr->buffer_length += (size_t) ret;
420
/* wait for process exit */
422
waitpid(process_itr->pid, &status, 0);
423
if(WIFEXITED(status) and WEXITSTATUS(status) == 0){
424
for(size_t written = 0;
425
written < process_itr->buffer_length;){
426
ret = write(STDOUT_FILENO,
427
process_itr->buffer + written,
428
process_itr->buffer_length - written);
433
written += (size_t)ret;
437
FD_CLR(process_itr->fd, &rfds_all);
447
for(process *process_itr = process_list; process_itr != NULL;
448
process_itr = process_itr->next){
449
close(process_itr->fd);
450
kill(process_itr->pid, SIGTERM);
451
free(process_itr->buffer);