51
49
struct process *next;
52
typedef struct plugin{
53
char *name; /* can be "global" and any plugin name */
60
plugin *getplugin(char *name, plugin **plugin_list){
61
for (plugin *p = *plugin_list; p != NULL; p = p->next){
63
or (p->name and name and (strcmp(p->name, name) == 0))){
67
/* Create a new plugin */
68
plugin *new_plugin = malloc(sizeof(plugin));
69
if (new_plugin == NULL){
73
new_plugin->name = name;
74
new_plugin->argv = malloc(sizeof(char *) * 2);
75
if (new_plugin->argv == NULL){
79
new_plugin->argv[0] = name;
80
new_plugin->argv[1] = NULL;
82
new_plugin->disable = false;
83
new_plugin->next = *plugin_list;
84
/* Append the new plugin to the list */
85
*plugin_list = new_plugin;
89
void addarguments(plugin *p, char *arg){
90
p->argv[p->argc] = arg;
91
p->argv = realloc(p->argv, sizeof(char *) * (size_t)(p->argc + 2));
97
p->argv[p->argc] = NULL;
54
100
#define BUFFER_SIZE 256
102
const char *argp_program_version =
103
"plugbasedclient 0.9";
104
const char *argp_program_bug_address =
105
"<mandos@fukt.bsnet.se>";
107
"Mandos plugin runner -- Run Mandos plugins";
108
/* A description of the arguments we accept. */
109
static char args_doc[] = "";
56
111
int main(int argc, char *argv[]){
57
char plugindir[] = "plugins.d";
58
size_t d_name_len, plugindir_len = sizeof(plugindir)-1;
112
const char *plugindir = "plugins.d";
60
115
struct dirent *dirst;
63
118
int ret, maxfd = 0;
64
119
process *process_list = NULL;
121
/* The options we understand. */
122
struct argp_option options[] = {
123
{ .name = "global-options", .key = 'g',
124
.arg = "option[,option[,...]]", .flags = 0,
125
.doc = "Options effecting all plugins" },
126
{ .name = "options-for", .key = 'o',
127
.arg = "plugin:option[,option[,...]]", .flags = 0,
128
.doc = "Options effecting only specified plugins" },
129
{ .name = "disable-plugin", .key = 'd',
130
.arg = "Plugin[,Plugin[,...]]", .flags = 0,
131
.doc = "Option to disable specififed plugins" },
132
{ .name = "plugin-dir", .key = 128,
133
.arg = "Directory", .flags = 0,
134
.doc = "Option to change directory to search for plugins" },
138
error_t parse_opt (int key, char *arg, struct argp_state *state) {
139
/* Get the INPUT argument from `argp_parse', which we
140
know is a pointer to our arguments structure. */
141
plugin **plugins = state->input;
145
char *p = strtok(arg, ",");
147
addarguments(getplugin(NULL, plugins), p);
148
p = strtok(NULL, ",");
154
char *name = strtok(arg, ":");
155
char *p = strtok(NULL, ":");
158
addarguments(getplugin(name, plugins), p);
159
p = strtok(NULL, ",");
165
char *p = strtok(arg, ",");
167
getplugin(p, plugins)->disable = true;
168
p = strtok(NULL, ",");
181
return ARGP_ERR_UNKNOWN;
186
plugin *plugin_list = NULL;
188
struct argp argp = { .options = options, .parser = parse_opt,
189
.args_doc = args_doc, .doc = doc };
191
argp_parse (&argp, argc, argv, 0, 0, &plugin_list);
193
/* for(plugin *p = plugin_list; p != NULL; p=p->next){ */
194
/* fprintf(stderr, "Plugin: %s has %d arguments\n", p->name ? p->name : "Global", p->argc); */
195
/* for(char **a = p->argv + 1; *a != NULL; a++){ */
196
/* fprintf(stderr, "\tArg: %s\n", *a); */
66
202
dir = opendir(plugindir);
69
205
fprintf(stderr, "Can not open directory\n");
70
206
return EXIT_FAILURE;
91
char *filename = malloc(d_name_len + plugindir_len + 2);
227
char *filename = malloc(d_name_len + strlen(plugindir) + 2);
92
228
strcpy(filename, plugindir);
93
229
strcat(filename, "/");
94
230
strcat(filename, dirst->d_name);
96
232
stat(filename, &st);
98
if (S_ISREG(st.st_mode) and (access(filename, X_OK) == 0)){
234
if (S_ISREG(st.st_mode)
235
and (access(filename, X_OK) == 0)
236
and not (getplugin(dirst->d_name, &plugin_list)->disable)){
99
237
// Starting a new process to be watched
100
238
process *new_process = malloc(sizeof(process));
103
245
new_process->pid = fork();
104
246
if(new_process->pid == 0){
105
247
/* this is the child process */
107
249
close(pipefd[0]); /* close unused read end of pipe */
108
250
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];
252
plugin *p = getplugin(dirst->d_name, &plugin_list);
253
plugin *g = getplugin(NULL, &plugin_list);
254
for(char **a = g->argv + 1; *a != NULL; a++){
116
new_argv[argc] = NULL;
117
if(execv(filename, new_argv) < 0){
257
if(execv(filename, p->argv) < 0){
119
259
close(pipefd[1]);
120
260
exit(EXIT_FAILURE);