-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathplugin.c
214 lines (186 loc) · 6.8 KB
/
plugin.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/* plugin.c
* Copyright (C) 2006-2007 Tillmann Werner <[email protected]>
*
* This file is free software; as a special exception the author gives
* unlimited permission to copy and/or distribute it, with or without
* modifications, as long as this notice is preserved.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY, to the extent permitted by law; without even the
* implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*/
#include <stdio.h>
#include <dlfcn.h>
#include <errno.h>
#include <string.h>
#include <sys/stat.h>
#include <dirent.h>
#include <fnmatch.h>
#include <stdlib.h>
#include "honeytrap.h"
#include "plugin.h"
#include "plughook.h"
#include "logging.h"
int load_plugin(const char *dir, const char* plugname) {
struct stat statbuf;
struct dirent **namelist;
int n, ret;
char *full_path, full_name[264];
DIR *plugindir;
ret = 0;
full_path = NULL;
plugin_list = NULL;
if (strlen(plugname) > 265) {
fprintf(stderr, " Error - Plugin name exceeds maximum length of 256 charakters: %s\n", plugname);
return(-1);
}
/* plugin directory must be configured */
if (!dir) {
fprintf(stderr, " Error - Plugin directory not set while trying to load plugin %s.\n", plugname);
exit(EXIT_FAILURE);
}
if ((plugindir = opendir(dir)) == NULL) {
fprintf(stderr, " Error - Unable to open plugin directory: %m.\n");
exit(EXIT_FAILURE);
}
DEBUG_FPRINTF(stdout, " Looking for plugin %s in %s\n", plugname, dir);
if ((n = scandir(dir, &namelist, 0, alphasort)) < 0) {
fprintf(stderr, " Error - Unable to scan plugin directory: %m.\n");
return(-1);
} else while(n--) {
stat(namelist[n]->d_name, &statbuf);
/* assemble full name */
memset(full_name, 0, 264);
strncpy(full_name, "htm_", 4);
strncpy(&full_name[4], plugname, strlen(plugname));
strncpy(&full_name[4+strlen(plugname)], ".so", 3);
if ((ret = fnmatch(full_name, namelist[n]->d_name, 0)) == 0) {
/* found the plugin */
if ((full_path = (char *) malloc(strlen(dir) + strlen(namelist[n]->d_name) + 2)) == NULL) {
perror(" Error - Unable to allocate memory");
exit(EXIT_FAILURE);
}
snprintf(full_path, strlen(dir)+strlen(namelist[n]->d_name)+2, "%s/%s", dir, namelist[n]->d_name);
DEBUG_FPRINTF(stdout, " Plugin found: %s\n", full_path);
init_plugin(full_path);
free(full_path);
free(namelist[n]);
break;
}
free(namelist[n]);
}
if (ret != 0) {
fprintf(stderr, " Error - Unable to load plugin %s: %m.\n", full_name);
exit(EXIT_FAILURE);
}
free(namelist);
return(1);
}
int init_plugin(char *plugin_name) {
int (*init_plugin)();
void (*unload_plugin)();
Plugin *last_plugin, *new_plugin;
/* allocate memory for new plugin and attach it to the plugin list */
if ((new_plugin = (Plugin *) malloc(sizeof(Plugin))) == NULL) {
fprintf(stderr, " Error - Unable to allocate memory: %m.\n");
return(-1);
} else {
new_plugin->handle = NULL;
new_plugin->name = NULL;
new_plugin->version = NULL;
new_plugin->next = NULL;
new_plugin->filename = NULL;
}
if (plugin_name == NULL) {
fprintf(stderr, " Error loading plugin - No name given.\n");
return(-1);
} else {
if ((new_plugin->handle = (void *) malloc(sizeof(int))) == NULL) {
fprintf(stderr, " Error loading plugin - Unable to allocate memory: %m.\n");
free(new_plugin);
return(-1);
} else new_plugin->filename = (char *) strdup(plugin_name);
}
dlerror(); /* Clear any existing error */
if (((new_plugin->handle = dlopen(new_plugin->filename, RTLD_NOW)) == NULL) &&
((plugin_error_str = (char *) dlerror()) != NULL)) {
fprintf(stderr, " Unable to initialize plugin: %s\n", plugin_error_str);
unload_on_err(new_plugin);
exit(EXIT_FAILURE);
}
/* determin internal module name and version string */
if (((new_plugin->name = (char *) dlsym(new_plugin->handle, "module_name")) == NULL) &&
((plugin_error_str = (char *) dlerror()) != NULL)) {
/* handle error, the symbol wasn't found */
fprintf(stderr, " Unable to initialize plugin: %s\n", plugin_error_str);
fprintf(stderr, " %s seems not to be a honeytrap plugin.\n", new_plugin->filename);
unload_on_err(new_plugin);
return(-1);
}
if (((new_plugin->version = (char *) dlsym(new_plugin->handle, "module_version")) == NULL) &&
((plugin_error_str = (char *) dlerror()) != NULL)) {
/* handle error, the symbol wasn't found */
fprintf(stderr, " Unable to initialize plugin %s: %s\n", new_plugin->name, plugin_error_str);
fprintf(stderr, " %s seems not to be a honeytrap plugin.\n", new_plugin->filename);
unload_on_err(new_plugin);
return(-1);
}
fprintf(stdout, " Loading plugin %s v%s\n", new_plugin->name, new_plugin->version);
DEBUG_FPRINTF(stdout, " Initializing plugin %s.\n", new_plugin->name);
/* resolve module's unload function and add it to unload hook */
if (((unload_plugin = dlsym(new_plugin->handle, "plugin_unload")) == NULL) &&
((plugin_error_str = (char *) dlerror()) != NULL)) {
/* handle error, the symbol wasn't found */
fprintf(stderr, " Unable to initialize plugin %s: %s\n", new_plugin->name, plugin_error_str);
fprintf(stderr, " %s seems not to be a honeytrap plugin.\n", new_plugin->filename);
unload_on_err(new_plugin);
return(-1);
}
if (!add_unload_func_to_list(new_plugin->name, "plugin_unload", unload_plugin)) {
fprintf(stderr, " Unable to register module for hook 'unload_plugins': %s\n", plugin_error_str);
unload_on_err(new_plugin);
return(-1);
}
/* resolve and call module's init function */
if (((init_plugin = dlsym(new_plugin->handle, "plugin_init")) == NULL) &&
((plugin_error_str = (char *) dlerror()) != NULL)) {
/* handle error, the symbol wasn't found */
fprintf(stderr, "\n Unable to resolve symbol 'plugin_init': %s\n", plugin_error_str);
return(-1);
}
init_plugin();
/* attach plugin to plugin_list */
if (!plugin_list) plugin_list = new_plugin;
else {
last_plugin = plugin_list;
while(last_plugin->next) last_plugin = last_plugin->next;
last_plugin->next = new_plugin;
}
return(1);
}
void unload_plugins(void) {
Plugin *cur_plugin;
/* call unload functions from plugins */
logmsg(LOG_DEBUG, 1, "Calling plugins for hook 'unload_plugins'.\n");
plughook_unload_plugins();
/* unload plugin and free mem for filename and plugin
* other variables in struct are symbols and do not need to be freed */
while(plugin_list) {
cur_plugin = plugin_list->next;
if (dlclose(plugin_list->handle) != 0)
logmsg(LOG_ERR, 1, "Error - Unable to unload plugin %s.\n", plugin_list->name);
free(plugin_list->filename);
free(plugin_list);
plugin_list = cur_plugin;
}
return;
}
void unload_on_err(Plugin *plugin) {
fprintf(stderr, " Unloading plugin on initialization error.\n");
if (plugin) {
if (plugin->handle) dlclose(plugin->handle);
free(plugin->filename);
}
free(plugin);
return;
}