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,
1
#include <stdio.h> /* popen, fileno */
2
#include <iso646.h> /* and, or, not */
3
#include <sys/types.h> /* DIR, opendir, stat, struct stat, waitpid,
4
WIFEXITED, WEXITSTATUS, wait */
5
#include <sys/wait.h> /* wait */
6
#include <dirent.h> /* DIR, opendir */
7
#include <sys/stat.h> /* stat, struct stat */
8
#include <unistd.h> /* stat, struct stat, chdir */
9
#include <stdlib.h> /* EXIT_FAILURE */
10
#include <sys/select.h> /* fd_set, select, FD_ZERO, FD_SET, FD_ISSET */
11
#include <string.h> /* strlen, strcpy, strcat */
12
#include <stdbool.h> /* true */
13
#include <sys/wait.h> /* waitpid, WIFEXITED, WEXITSTATUS */
14
#include <errno.h> /* errno */
58
24
struct process *next;
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;
109
27
#define BUFFER_SIZE 256
111
const char *argp_program_version =
112
"plugbasedclient 0.9";
113
const char *argp_program_bug_address =
114
"<mandos@fukt.bsnet.se>";
116
29
int main(int argc, char *argv[]){
117
const char *plugindir = "/conf/conf.d/mandos/plugins.d";
30
char plugindir[] = "plugins.d";
31
size_t d_name_len, plugindir_len = sizeof(plugindir)-1;
120
33
struct dirent *dirst;
123
36
int ret, maxfd = 0;
124
37
process *process_list = NULL;
126
int exitstatus = EXIT_SUCCESS;
128
/* The options we understand. */
129
struct argp_option options[] = {
130
{ .name = "global-options", .key = 'g',
131
.arg = "OPTION[,OPTION[,...]]",
132
.doc = "Options passed to all plugins" },
133
{ .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 },
147
error_t parse_opt (int key, char *arg, struct argp_state *state) {
148
/* Get the INPUT argument from `argp_parse', which we
149
know is a pointer to our plugin list pointer. */
150
plugin **plugins = state->input;
154
char *p = strtok(arg, ",");
156
addargument(getplugin(NULL, plugins), p);
157
p = strtok(NULL, ",");
163
char *name = strtok(arg, ":");
164
char *p = strtok(NULL, ":");
168
addargument(getplugin(name, plugins), p);
169
p = strtok(NULL, ",");
176
getplugin(arg, plugins)->disabled = true;
191
return ARGP_ERR_UNKNOWN;
196
plugin *plugin_list = NULL;
198
struct argp argp = { .options = options, .parser = parse_opt,
200
.doc = "Mandos plugin runner -- Run plugins" };
202
argp_parse (&argp, argc, argv, 0, 0, &plugin_list);
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
39
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
42
fprintf(stderr, "Can not open directory\n");
232
43
return EXIT_FAILURE;
238
49
dirst = readdir(dir);
245
56
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);
58
// Ignore dotfiles and backup files
59
if (dirst->d_name[0] == '.'
60
or dirst->d_name[d_name_len - 1] == '~'){
292
char *filename = malloc(d_name_len + strlen(plugindir) + 2);
293
if (filename == NULL){
295
exitstatus =EXIT_FAILURE;
64
char *filename = malloc(d_name_len + plugindir_len + 1);
298
65
strcpy(filename, plugindir);
299
66
strcat(filename, "/");
300
67
strcat(filename, dirst->d_name);
302
69
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){
71
if (S_ISREG(st.st_mode) and (access(filename, X_OK) == 0)){
72
// Starting a new process to be watched
73
process *new_process = malloc(sizeof(process));
76
new_process->pid = fork();
77
if(new_process->pid == 0){
78
/* this is the child process */
80
close(pipefd[0]); /* close unused read end of pipe */
81
dup2(pipefd[1], STDOUT_FILENO); /* replace our stdout */
82
/* create a new modified argument list */
83
char **new_argv = malloc(sizeof(char *) * argc + 1);
84
new_argv[0] = filename;
85
for(int i = 1; i < argc; i++){
86
new_argv[i] = argv[i];
88
new_argv[argc] = NULL;
89
if(execv(filename, new_argv) < 0){
96
close(pipefd[1]); /* close unused write end of pipe */
97
new_process->fd = pipefd[0];
98
new_process->buffer = malloc(BUFFER_SIZE);
99
if (new_process->buffer == NULL){
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;
103
new_process->buffer_size = BUFFER_SIZE;
104
new_process->buffer_length = 0;
105
FD_SET(new_process->fd, &rfds_orig);
107
if (maxfd < new_process->fd){
108
maxfd = new_process->fd;
112
new_process->next = process_list;
113
process_list = new_process;
374
119
if (process_list != NULL){
376
fd_set rfds = rfds_all;
121
fd_set rfds = rfds_orig;
377
122
int select_ret = select(maxfd+1, &rfds, NULL, NULL, NULL);
378
123
if (select_ret == -1){
396
141
ret = read(process_itr->fd, process_itr->buffer
397
142
+ process_itr->buffer_length, BUFFER_SIZE);
399
/* Read error from this process; ignore it */
402
process_itr->buffer_length += (size_t) ret;
143
process_itr->buffer_length+=ret;
405
146
/* wait for process exit */
407
148
waitpid(process_itr->pid, &status, 0);
408
149
if(WIFEXITED(status) and WEXITSTATUS(status) == 0){
409
for(size_t written = 0;
410
written < process_itr->buffer_length;){
411
ret = write(STDOUT_FILENO,
412
process_itr->buffer + written,
413
process_itr->buffer_length - written);
418
written += (size_t)ret;
150
write(STDOUT_FILENO, process_itr->buffer,
151
process_itr->buffer_length);
422
FD_CLR(process_itr->fd, &rfds_all);
154
FD_CLR(process_itr->fd, &rfds_orig);