18
18
* along with this program. If not, see
19
19
* <http://www.gnu.org/licenses/>.
21
* Contact the authors at <https://www.fukt.bsnet.se/~belorn/> and
22
* <https://www.fukt.bsnet.se/~teddy/>.
21
* Contact the authors at <mandos@fukt.bsnet.se>.
25
#define _FORTIFY_SOURCE 2
27
24
#include <stdio.h> /* popen, fileno */
28
25
#include <iso646.h> /* and, or, not */
29
26
#include <sys/types.h> /* DIR, opendir, stat, struct stat, waitpid,
51
49
struct process *next;
52
typedef struct plugin{
53
char *name; /* can be "global" and any plugin name */
59
plugin *getplugin(char *name, plugin **plugin_list){
60
for (plugin *p = *plugin_list; p != NULL; p = p->next){
62
or (p->name and name and (strcmp(p->name, name) == 0))){
66
/* Create a new plugin */
67
plugin *new_plugin = malloc(sizeof(plugin));
68
if (new_plugin == NULL){
72
new_plugin->name = name;
73
new_plugin->argv = malloc(sizeof(char *) * 2);
74
if (new_plugin->argv == NULL){
78
new_plugin->argv[0] = name;
79
new_plugin->argv[1] = NULL;
81
/* Append the new plugin to the list */
82
new_plugin->next = *plugin_list;
83
*plugin_list = new_plugin;
87
void addarguments(plugin *p, char *arg){
88
p->argv[p->argc] = arg;
89
p->argv = realloc(p->argv, sizeof(char *) * (size_t)(p->argc + 2));
95
p->argv[p->argc] = NULL;
54
98
#define BUFFER_SIZE 256
100
const char *argp_program_version =
101
"plugbasedclient 0.9";
102
const char *argp_program_bug_address =
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[] = "";
56
109
int main(int argc, char *argv[]){
57
110
char plugindir[] = "plugins.d";
58
111
size_t d_name_len, plugindir_len = sizeof(plugindir)-1;
63
116
int ret, maxfd = 0;
64
117
process *process_list = NULL;
119
/* The options we understand. */
120
struct argp_option options[] = {
121
{ .name = "global-options", .key = 'g',
122
.arg = "option[,option[,...]]", .flags = 0,
123
.doc = "Options effecting all plugins" },
124
{ .name = "options-for", .key = 'o',
125
.arg = "plugin:option[,option[,...]]", .flags = 0,
126
.doc = "Options effecting only specified plugins" },
130
error_t parse_opt (int key, char *arg, struct argp_state *state) {
131
/* Get the INPUT argument from `argp_parse', which we
132
know is a pointer to our arguments structure. */
133
plugin **plugins = state->input;
137
char *p = strtok(arg, ",");
139
addarguments(getplugin(NULL, plugins), p);
140
p = strtok(NULL, ",");
146
char *name = strtok(arg, ":");
147
char *p = strtok(NULL, ":");
150
addarguments(getplugin(name, plugins), p);
151
p = strtok(NULL, ",");
161
return ARGP_ERR_UNKNOWN;
166
plugin *plugin_list = NULL;
168
struct argp argp = { .options = options, .parser = parse_opt,
169
.args_doc = args_doc, .doc = doc };
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); */
66
182
dir = opendir(plugindir);
69
185
fprintf(stderr, "Can not open directory\n");
70
186
return EXIT_FAILURE;
99
215
// Starting a new process to be watched
100
216
process *new_process = malloc(sizeof(process));
103
223
new_process->pid = fork();
104
224
if(new_process->pid == 0){
105
225
/* this is the child process */
107
227
close(pipefd[0]); /* close unused read end of pipe */
108
228
dup2(pipefd[1], STDOUT_FILENO); /* replace our stdout */
109
/* create a new modified argument list */
110
char **new_argv = malloc(sizeof(char *)
111
* ((unsigned int) argc + 1));
112
new_argv[0] = filename;
113
for(int i = 1; i < argc; i++){
114
new_argv[i] = argv[i];
116
new_argv[argc] = NULL;
117
if(execv(filename, new_argv) < 0){
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){
119
244
close(pipefd[1]);
120
245
exit(EXIT_FAILURE);
180
305
waitpid(process_itr->pid, &status, 0);
181
306
if(WIFEXITED(status) and WEXITSTATUS(status) == 0){
182
write(STDOUT_FILENO, process_itr->buffer,
183
process_itr->buffer_length);
307
for(size_t written = 0;
308
written < process_itr->buffer_length;){
309
ret = write(STDOUT_FILENO,
310
process_itr->buffer + written,
311
process_itr->buffer_length - written);
316
written += (size_t)ret;
186
320
FD_CLR(process_itr->fd, &rfds_orig);