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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
|
/* Modular support
*
* (C) 2009 Anope Team
* Contact us at team@anope.org
*
* Please read COPYING and README for further details.
*
* $Id$
*
*/
#include "modules.h"
#include "language.h"
#include "version.h"
void ModuleManager::LoadModuleList(int total_modules, char **module_list)
{
int idx;
Module *m;
int status = 0;
for (idx = 0; idx < total_modules; idx++) {
m = findModule(module_list[idx]);
if (!m) {
status = ModuleManager::LoadModule(module_list[idx], NULL);
}
}
}
/**
* Copy the module from the modules folder to the runtime folder.
* This will prevent module updates while the modules is loaded from
* triggering a segfault, as the actaul file in use will be in the
* runtime folder.
* @param name the name of the module to copy
* @param output the destination to copy the module to
* @return MOD_ERR_OK on success
*/
static int moduleCopyFile(const char *name, const char *output)
{
int ch;
FILE *source, *target;
#ifndef _WIN32
int srcfp;
#endif
char input[4096];
int len;
strncpy(input, services_dir.c_str(), 4095);
len = strlen(input);
strncat(input, "/modules/", 4095 - len); /* Get full path with module extension */
len = strlen(input);
strncat(input, name, 4095 - len);
len = strlen(output);
strncat(input, MODULE_EXT, 4095 - len);
#ifndef _WIN32
if ((srcfp = mkstemp(const_cast<char *>(output))) == -1)
return MOD_ERR_FILE_IO;
#else
if (!mktemp(const_cast<char *>(output)))
return MOD_ERR_FILE_IO;
#endif
if (debug)
alog("Runtime module location: %s", output);
/* Linux/UNIX should ignore the b param, why do we still have seperate
* calls for it here? -GD
*/
#ifndef _WIN32
if ((source = fopen(input, "r")) == NULL) {
#else
if ((source = fopen(input, "rb")) == NULL) {
#endif
return MOD_ERR_NOEXIST;
}
#ifndef _WIN32
if ((target = fdopen(srcfp, "w")) == NULL) {
#else
if ((target = fopen(output, "wb")) == NULL) {
#endif
return MOD_ERR_FILE_IO;
}
while ((ch = fgetc(source)) != EOF) {
fputc(ch, target);
}
fclose(source);
if (fclose(target) != 0) {
return MOD_ERR_FILE_IO;
}
return MOD_ERR_OK;
}
static bool IsOneOfModuleTypeLoaded(MODType mt)
{
int idx = 0;
ModuleHash *current = NULL;
int pmods = 0;
for (idx = 0; idx != MAX_CMD_HASH; idx++)
{
for (current = MODULE_HASH[idx]; current; current = current->next)
{
if (current->m->type == mt)
pmods++;
}
}
/*
* 2, because module constructors now add modules to the hash.. so 1 (original module)
* and 2 (this module). -- w00t
*/
if (pmods == 2)
return true;
return false;
}
/* This code was found online at http://www.linuxjournal.com/article/3687#comment-26593
*
* This function will take a pointer from either dlsym or GetProcAddress and cast it in
* a way that won't cause C++ warnings/errors to come up.
*/
template <class TYPE>
TYPE function_cast(ano_module_t symbol)
{
union {
ano_module_t symbol;
TYPE function;
} cast;
cast.symbol = symbol;
return cast.function;
}
int ModuleManager::LoadModule(const std::string &modname, User * u)
{
const char *err;
Module *(*func)(const std::string &);
int ret = 0;
if (modname.empty())
return MOD_ERR_PARAMS;
if (findModule(modname.c_str()) != NULL)
return MOD_ERR_EXISTS;
if (debug)
alog("trying to load [%s]", modname.c_str());
/* Generate the filename for the temporary copy of the module */
std::string pbuf;
pbuf = services_dir + "/modules/";
#ifndef _WIN32
pbuf += "runtime/";
#else
pbuf += "runtime\\";
#endif
pbuf += modname;
pbuf += MODULE_EXT;
pbuf += ".";
pbuf += "XXXXXX";
/* Don't skip return value checking! -GD */
if ((ret = moduleCopyFile(modname.c_str(), pbuf.c_str())) != MOD_ERR_OK)
{
/* XXX: This used to assign filename here, but I don't think that was correct..
* even if it was, it makes life very fucking difficult, so.
*/
return ret;
}
ano_modclearerr();
ano_module_t handle = ano_modopen(pbuf.c_str());
if (handle == NULL && (err = ano_moderr()) != NULL)
{
alog("%s", err);
return MOD_ERR_NOLOAD;
}
ano_modclearerr();
func = function_cast<Module *(*)(const std::string &)>(ano_modsym(handle, "init_module"));
if (func == NULL && (err = ano_moderr()) != NULL)
{
alog("No magical init function found, not an Anope module");
ano_modclose(handle);
return MOD_ERR_NOLOAD;
}
if (!func)
{
throw CoreException("Couldn't find constructor, yet moderror wasn't set?");
}
/* Create module. */
std::string nick;
if (u)
nick = u->nick;
else
nick = "";
Module *m;
try
{
m = func(nick);
}
catch (ModuleException &ex)
{
alog("Error while loading %s: %s", modname.c_str(), ex.GetReason());
return MOD_STOP;
}
m->filename = pbuf;
m->handle = handle;
if (m->type == PROTOCOL && IsOneOfModuleTypeLoaded(PROTOCOL))
{
DeleteModule(m);
alog("You cannot load two protocol modules");
return MOD_STOP;
}
else if (m->type == ENCRYPTION && IsOneOfModuleTypeLoaded(ENCRYPTION))
{
DeleteModule(m);
alog("You cannot load two encryption modules");
return MOD_STOP;
}
if (u)
{
ircdproto->SendGlobops(s_OperServ, "%s loaded module %s", u->nick, modname.c_str());
notice_lang(s_OperServ, u, OPER_MODULE_LOADED, modname.c_str());
}
return MOD_ERR_OK;
}
int ModuleManager::UnloadModule(Module *m, User *u)
{
if (!m || !m->handle)
{
if (u)
notice_lang(s_OperServ, u, OPER_MODULE_REMOVE_FAIL, m->name.c_str());
return MOD_ERR_PARAMS;
}
if (m->GetPermanent() || m->type == PROTOCOL || m->type == ENCRYPTION)
{
if (u)
notice_lang(s_OperServ, u, OPER_MODULE_NO_UNLOAD);
return MOD_ERR_NOUNLOAD;
}
if (u)
{
ircdproto->SendGlobops(s_OperServ, "%s unloaded module %s", u->nick, m->name.c_str());
notice_lang(s_OperServ, u, OPER_MODULE_UNLOADED, m->name.c_str());
}
DeleteModule(m);
return MOD_ERR_OK;
}
void ModuleManager::DeleteModule(Module *m)
{
const char *err;
void (*destroy_func)(Module *m);
ano_module_t handle;
if (!m || !m->handle) /* check m is least possibly valid */
{
return;
}
handle = m->handle;
ano_modclearerr();
destroy_func = function_cast<void (*)(Module *)>(ano_modsym(m->handle, "destroy_module"));
if (destroy_func == NULL && (err = ano_moderr()) != NULL)
{
alog("No magical destroy function found, chancing delete...");
delete m; /* we just have to chance they haven't overwrote the delete operator then... */
}
else
{
destroy_func(m); /* Let the module delete it self, just in case */
}
if (handle)
{
if ((ano_modclose(handle)) != 0)
alog("%s", ano_moderr());
}
}
|