1
/* -*- coding: utf-8 -*- */
3
* Mandos plugin runner - Run Mandos plugins
5
* Copyright © 2007-2008 Teddy Hogeborn and 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 <https://www.fukt.bsnet.se/~belorn/> and
22
* <https://www.fukt.bsnet.se/~teddy/>.
25
#define _FORTIFY_SOURCE 2
1
27
#include <stdio.h> /* popen, fileno */
2
28
#include <iso646.h> /* and, or, not */
3
29
#include <sys/types.h> /* DIR, opendir, stat, struct stat, waitpid,
7
33
#include <sys/stat.h> /* stat, struct stat */
8
34
#include <unistd.h> /* stat, struct stat, chdir */
9
35
#include <stdlib.h> /* EXIT_FAILURE */
10
#include <sys/select.h> /* fd_set, select, FD_ZERO, FD_SET, FD_ISSET */
36
#include <sys/select.h> /* fd_set, select, FD_ZERO, FD_SET,
11
38
#include <string.h> /* strlen, strcpy, strcat */
12
39
#include <stdbool.h> /* true */
13
40
#include <sys/wait.h> /* waitpid, WIFEXITED, WEXITSTATUS */
14
41
#include <errno.h> /* errno */
42
#include <argp.h> /* argp */
24
52
struct process *next;
55
typedef struct plugin{
56
char *name; /* can be "global" and any plugin name */
63
plugin *getplugin(char *name, plugin **plugin_list){
64
for (plugin *p = *plugin_list; p != NULL; p = p->next){
66
or (p->name and name and (strcmp(p->name, name) == 0))){
70
/* Create a new plugin */
71
plugin *new_plugin = malloc(sizeof(plugin));
72
if (new_plugin == NULL){
76
new_plugin->name = name;
77
new_plugin->argv = malloc(sizeof(char *) * 2);
78
if (new_plugin->argv == NULL){
82
new_plugin->argv[0] = name;
83
new_plugin->argv[1] = NULL;
85
new_plugin->disable = false;
86
new_plugin->next = *plugin_list;
87
/* Append the new plugin to the list */
88
*plugin_list = new_plugin;
92
void addarguments(plugin *p, char *arg){
93
p->argv[p->argc] = arg;
94
p->argv = realloc(p->argv, sizeof(char *) * (size_t)(p->argc + 2));
100
p->argv[p->argc] = NULL;
27
103
#define BUFFER_SIZE 256
105
const char *argp_program_version =
106
"plugbasedclient 0.9";
107
const char *argp_program_bug_address =
108
"<mandos@fukt.bsnet.se>";
110
"Mandos plugin runner -- Run Mandos plugins";
111
/* A description of the arguments we accept. */
112
static char args_doc[] = "";
29
114
int main(int argc, char *argv[]){
30
char plugindir[] = "plugins.d";
31
size_t d_name_len, plugindir_len = sizeof(plugindir)-1;
115
const char *plugindir = "plugins.d";
33
118
struct dirent *dirst;
36
121
int ret, maxfd = 0;
37
122
process *process_list = NULL;
125
/* The options we understand. */
126
struct argp_option options[] = {
127
{ .name = "global-options", .key = 'g',
128
.arg = "option[,option[,...]]", .flags = 0,
129
.doc = "Options effecting all plugins" },
130
{ .name = "options-for", .key = 'o',
131
.arg = "plugin:option[,option[,...]]", .flags = 0,
132
.doc = "Options effecting only specified plugins" },
133
{ .name = "disable-plugin", .key = 'd',
134
.arg = "Plugin[,Plugin[,...]]", .flags = 0,
135
.doc = "Option to disable specififed plugins" },
136
{ .name = "plugin-dir", .key = 128,
137
.arg = "Directory", .flags = 0,
138
.doc = "Option to change directory to search for plugins" },
139
{ .name = "userid", .key = 129,
140
.arg = "Id", .flags = 0,
141
.doc = "Option to change which user id the plugins will run as" },
142
{ .name = "groupid", .key = 130,
143
.arg = "Id", .flags = 0,
144
.doc = "Option to change which group id the plugins will run as" },
148
error_t parse_opt (int key, char *arg, struct argp_state *state) {
149
/* Get the INPUT argument from `argp_parse', which we
150
know is a pointer to our arguments structure. */
151
plugin **plugins = state->input;
155
char *p = strtok(arg, ",");
157
addarguments(getplugin(NULL, plugins), p);
158
p = strtok(NULL, ",");
164
char *name = strtok(arg, ":");
165
char *p = strtok(NULL, ":");
168
addarguments(getplugin(name, plugins), p);
169
p = strtok(NULL, ",");
175
char *p = strtok(arg, ",");
177
getplugin(p, plugins)->disable = true;
178
p = strtok(NULL, ",");
186
uid = (uid_t)strtol(arg, NULL, 10);
189
gid = (gid_t)strtol(arg, NULL, 10);
197
return ARGP_ERR_UNKNOWN;
202
plugin *plugin_list = NULL;
204
struct argp argp = { .options = options, .parser = parse_opt,
205
.args_doc = args_doc, .doc = doc };
207
argp_parse (&argp, argc, argv, 0, 0, &plugin_list);
209
/* for(plugin *p = plugin_list; p != NULL; p=p->next){ */
210
/* fprintf(stderr, "Plugin: %s has %d arguments\n", p->name ? p->name : "Global", p->argc); */
211
/* for(char **a = p->argv + 1; *a != NULL; a++){ */
212
/* fprintf(stderr, "\tArg: %s\n", *a); */
39
228
dir = opendir(plugindir);
42
231
fprintf(stderr, "Can not open directory\n");
43
232
return EXIT_FAILURE;
64
char *filename = malloc(d_name_len + plugindir_len + 2);
253
char *filename = malloc(d_name_len + strlen(plugindir) + 2);
65
254
strcpy(filename, plugindir);
66
255
strcat(filename, "/");
67
256
strcat(filename, dirst->d_name);
69
258
stat(filename, &st);
71
if (S_ISREG(st.st_mode) and (access(filename, X_OK) == 0)){
260
if (S_ISREG(st.st_mode)
261
and (access(filename, X_OK) == 0)
262
and not (getplugin(dirst->d_name, &plugin_list)->disable)){
72
263
// Starting a new process to be watched
73
264
process *new_process = malloc(sizeof(process));
76
271
new_process->pid = fork();
77
272
if(new_process->pid == 0){
78
273
/* this is the child process */
80
275
close(pipefd[0]); /* close unused read end of pipe */
81
276
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];
278
plugin *p = getplugin(dirst->d_name, &plugin_list);
279
plugin *g = getplugin(NULL, &plugin_list);
280
for(char **a = g->argv + 1; *a != NULL; a++){
88
new_argv[argc] = NULL;
89
if(execv(filename, new_argv) < 0){
283
if(execv(filename, p->argv) < 0){
92
286
exit(EXIT_FAILURE);
141
335
ret = read(process_itr->fd, process_itr->buffer
142
336
+ process_itr->buffer_length, BUFFER_SIZE);
143
process_itr->buffer_length+=ret;
338
/* Read error from this process; ignore it */
341
process_itr->buffer_length += (size_t) ret;
146
344
/* wait for process exit */
148
346
waitpid(process_itr->pid, &status, 0);
149
347
if(WIFEXITED(status) and WEXITSTATUS(status) == 0){
150
write(STDOUT_FILENO, process_itr->buffer,
151
process_itr->buffer_length);
348
for(size_t written = 0;
349
written < process_itr->buffer_length;){
350
ret = write(STDOUT_FILENO,
351
process_itr->buffer + written,
352
process_itr->buffer_length - written);
357
written += (size_t)ret;
154
361
FD_CLR(process_itr->fd, &rfds_orig);