Debian Codegraph @

modules/mod_dso.c

raw symbols SCIP
1 /*
2 * ProFTPD: mod_dso -- support for loading/unloading modules at run-time
3 * Copyright (c) 2004-2024 TJ Saunders <[email protected]>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA.
18 *
19 * As a special exemption, TJ Saunders and other respective copyright holders
20 * give permission to link this program with OpenSSL, and distribute the
21 * resulting executable, without including the source code for OpenSSL in the
22 * source distribution.
23 *
24 * This is mod_dso, contrib software for proftpd 1.3.x.
25 * For more information contact TJ Saunders <[email protected]>.
26 */
27
28 #include "conf.h"
29 #include "mod_ctrls.h"
30 #include "ltdl.h"
31
32 #if defined(HAVE_DLFCN_H)
33 # include <dlfcn.h>
34 #endif /* HAVE_DLFCN_H */
35
36 #define MOD_DSO_VERSION "mod_dso/0.5"
37
38 /* From modules/module_glue.c */
39 extern module *static_modules[];
40 extern module *loaded_modules;
41
42 module dso_module;
43 static const char *dso_module_path = PR_LIBEXEC_DIR;
44 static pool *dso_pool = NULL;
45
46 static const char *trace_channel = "dso";
47
48 #if defined(PR_USE_CTRLS)
49 static ctrls_acttab_t dso_acttab[];
50 #endif
51
52 static int dso_load_file(char *path) {
53 if (path == NULL) {
54 errno = EINVAL;
55 return -1;
56 }
57
58 pr_trace_msg(trace_channel, 5, "loading file '%s'", path);
59
60 /* XXX Is this sufficient for loading an external library? */
61 if (lt_dlopenext(path) == NULL) {
62 const char *dl_error = NULL;
63
64 #if defined(HAVE_DLFCN_H)
65 /* Grab a copy of the dlerror(3) text as well, as we may be working
66 * around a libtool bug; see:
67 * https://bug-libtool.gnu.narkive.com/eWFGvX8k/misleading-error-message-from-lt-dlopen
68 */
69 dl_error = dlerror();
70 #endif /* HAVE_DLFCN_H */
71 if (dl_error == NULL) {
72 dl_error = lt_dlerror();
73 }
74
75 pr_log_pri(PR_LOG_NOTICE, MOD_DSO_VERSION ": unable to open '%s': %s",
76 path, dl_error);
77 errno = EPERM;
78 return -1;
79 }
80
81 pr_trace_msg(trace_channel, 8, "file '%s' successfully loaded", path);
82 return 0;
83 }
84
85 static int dso_load_module(pool *p, char *name) {
86 int module_load_errno = 0, res;
87 char *symbol_name, *path, *ptr;
88 size_t namelen;
89 module *m;
90 lt_ptr mh = NULL;
91 lt_dladvise advise;
92 const lt_dlinfo *info = NULL;
93
94 if (name == NULL) {
95 errno = EINVAL;
96 return -1;
97 }
98
99 namelen = strlen(name);
100
101 if (namelen < 5 ||
102 strncmp(name, "mod_", 4) != 0) {
103 errno = EINVAL;
104 return -1;
105 }
106
107 /* Handle ".c" and ".cpp" extensions. */
108 if (pr_strnrstr(name, namelen, ".c", 2, 0) != TRUE &&
109 pr_strnrstr(name, namelen, ".cpp", 4, 0) != TRUE) {
110 errno = EINVAL;
111 return -1;
112 }
113
114 pr_log_debug(DEBUG7, MOD_DSO_VERSION ": loading '%s'", name);
115
116 ptr = strrchr(name, '.');
117 if (ptr == NULL) {
118 errno = EINVAL;
119 return -1;
120 }
121
122 if (lt_dladvise_init(&advise) < 0) {
123 pr_log_pri(PR_LOG_NOTICE, MOD_DSO_VERSION
124 ": unable to initialise advise: %s", lt_dlerror());
125 errno = EPERM;
126 return -1;
127 }
128
129 if (lt_dladvise_ext(&advise) < 0) {
130 pr_log_pri(PR_LOG_NOTICE, MOD_DSO_VERSION
131 ": unable to setting 'ext' advise hint: %s", lt_dlerror());
132 lt_dladvise_destroy(&advise);
133 errno = EPERM;
134 return -1;
135 }
136
137 if (lt_dladvise_global(&advise) < 0) {
138 pr_log_pri(PR_LOG_NOTICE, MOD_DSO_VERSION
139 ": unable to setting 'global' advise hint: %s", lt_dlerror());
140 lt_dladvise_destroy(&advise);
141 errno = EPERM;
142 return -1;
143 }
144
145 *ptr = '\0';
146
147 /* Load file: $prefix/libexec/<module> */
148 path = pdircat(dso_pool, dso_module_path, name, NULL);
149
150 pr_trace_msg(trace_channel, 5, "loading module '%s'", path);
151
152 mh = lt_dlopenadvise(path, advise);
153 if (mh == NULL) {
154 int xerrno = errno;
155 const char *dl_error = NULL;
156
157 *ptr = '.';
158
159 #if defined(HAVE_DLFCN_H)
160 /* Grab a copy of the dlerror(3) text as well, as we may be working
161 * around a libtool bug; see:
162 * https://bug-libtool.gnu.narkive.com/eWFGvX8k/misleading-error-message-from-lt-dlopen
163 */
164 dl_error = dlerror();
165 #endif /* HAVE_DLFCN_H */
166 if (dl_error == NULL) {
167 dl_error = lt_dlerror();
168 }
169
170 /* Remember this errno value, for reporting later if we cannot resolve
171 * the symbol from the main executable.
172 */
173 module_load_errno = errno;
174
175 pr_log_debug(DEBUG3, MOD_DSO_VERSION ": unable to dlopen '%s': %s (%s)",
176 name, dl_error, strerror(xerrno));
177
178 if (xerrno == ENOENT) {
179 pr_log_pri(PR_LOG_NOTICE, MOD_DSO_VERSION
180 ": unable to load '%s'; check to see if '%s.la' exists", name, path);
181 }
182
183 pr_log_debug(DEBUG3, MOD_DSO_VERSION
184 ": defaulting to 'self' for symbol resolution");
185
186 lt_dladvise_destroy(&advise);
187
188 mh = lt_dlopen(NULL);
189 if (mh == NULL) {
190 pr_log_debug(DEBUG0, MOD_DSO_VERSION ": error loading 'self': %s",
191 lt_dlerror());
192
193 errno = xerrno;
194 return -1;
195 }
196 }
197
198 lt_dladvise_destroy(&advise);
199
200 info = lt_dlgetinfo(mh);
201 if (info != NULL) {
202 struct stat st;
203
204 res = stat(info->filename, &st);
205 if (res == 0) {
206 pr_log_debug(DEBUG7, MOD_DSO_VERSION
207 ": loaded module '%s' (from '%s', last modified on %s)", info->name,
208 info->filename, pr_strtime3(p, st.st_mtime, FALSE));
209 }
210 }
211
212 /* Tease name of the module structure out of the given name:
213 * <module>.<ext> --> <module>_module
214 */
215
216 *ptr = '\0';
217 symbol_name = pstrcat(dso_pool, name+4, "_module", NULL);
218
219 /* Lookup module structure symbol by name. */
220
221 pr_trace_msg(trace_channel, 7, "looking for symbol '%s' in loaded module",
222 symbol_name);
223
224 m = (module *) lt_dlsym(mh, symbol_name);
225 if (m == NULL) {
226 int xerrno = errno;
227
228 *ptr = '.';
229 pr_log_debug(DEBUG1, MOD_DSO_VERSION
230 ": unable to find module symbol '%s' in 'self'", symbol_name);
231 pr_trace_msg(trace_channel, 1,
232 "unable to find module symbol '%s' in 'self'", symbol_name);
233
234 lt_dlclose(mh);
235 mh = NULL;
236
237 if (xerrno == ENOENT) {
238 pr_log_pri(PR_LOG_NOTICE, MOD_DSO_VERSION
239 ": check to see if '%s.la' exists", path);
240 }
241
242 if (module_load_errno != 0) {
243 /* If we had an error loading the original module, AND we had an error
244 * resolving the symbol in the main executable, then return the original
245 * errno from loading the module, rather than the symbol resolution
246 * error.
247 */
248 errno = module_load_errno;
249
250 } else {
251 errno = xerrno;
252 }
253
254 return -1;
255 }
256 *ptr = '.';
257
258 m->handle = mh;
259
260 /* Add the module to the core structures */
261 res = pr_module_load(m);
262 if (res < 0) {
263 int xerrno = errno;
264
265 if (xerrno == EEXIST) {
266 pr_log_pri(PR_LOG_INFO, MOD_DSO_VERSION
267 ": module 'mod_%s.c' already loaded", m->name);
268 pr_trace_msg(trace_channel, 1, "module 'mod_%s.c' already loaded",
269 m->name);
270
271 } else if (xerrno == EACCES) {
272 pr_log_pri(PR_LOG_ERR, MOD_DSO_VERSION
273 ": module 'mod_%s.c' has wrong API version (0x%x), must be 0x%x",
274 m->name, m->api_version, PR_MODULE_API_VERSION);
275 pr_trace_msg(trace_channel, 1,
276 "module 'mod_%s.c' has wrong API version (0x%x), must be 0x%x",
277 m->name, m->api_version, PR_MODULE_API_VERSION);
278
279 } else if (xerrno == EPERM) {
280 pr_log_pri(PR_LOG_ERR, MOD_DSO_VERSION
281 ": module 'mod_%s.c' failed to initialize", m->name);
282 pr_trace_msg(trace_channel, 1, "module 'mod_%s.c' failed to initialize",
283 m->name);
284 }
285
286 lt_dlclose(mh);
287 mh = NULL;
288
289 errno = xerrno;
290 return -1;
291 }
292
293 pr_trace_msg(trace_channel, 8, "module '%s' successfully loaded", path);
294 return 0;
295 }
296
297 static int dso_unload_module(module *m) {
298 int res;
299 char *name;
300
301 /* Some modules cannot be unloaded. */
302 if (strncmp(m->name, "dso", 4) == 0) {
303 errno = EPERM;
304 return -1;
305 }
306
307 name = pstrdup(dso_pool, m->name);
308
309 pr_trace_msg(trace_channel, 5, "unloading module 'mod_%s.c'", name);
310
311 res = pr_module_unload(m);
312 if (res < 0) {
313 int xerrno = errno;
314
315 pr_log_debug(DEBUG1, MOD_DSO_VERSION
316 ": error unloading module 'mod_%s.c': %s", m->name, strerror(xerrno));
317 pr_trace_msg(trace_channel, 1,
318 "error unloading module 'mod_%s.c': %s", m->name, strerror(xerrno));
319 }
320
321 if (lt_dlclose(m->handle) < 0) {
322 int xerrno = errno;
323
324 pr_log_debug(DEBUG1, MOD_DSO_VERSION ": error closing '%s': %s",
325 name, lt_dlerror());
326
327 errno = xerrno;
328 return -1;
329 }
330
331 pr_trace_msg(trace_channel, 8, "module 'mod_%s.c' successfully unloaded",
332 name);
333 return 0;
334 }
335
336 #if defined(PR_USE_CTRLS)
337 static int dso_unload_module_by_name(const char *name) {
338 module *m;
339
340 if (strncmp(name, "mod_", 4) != 0 ||
341 name[strlen(name)-2] != '.' ||
342 name[strlen(name)-1] != 'c') {
343 errno = EINVAL;
344 return -1;
345 }
346
347 /* Lookup the module pointer for the given module name. */
348 m = pr_module_get(name);
349 if (m == NULL) {
350 errno = ENOENT;
351 return -1;
352 }
353
354 return dso_unload_module(m);
355 }
356 #endif /* PR_USE_CTRLS */
357
358 #if defined(PR_USE_CTRLS)
359 /* Controls handlers
360 */
361
362 static int dso_handle_insmod(pr_ctrls_t *ctrl, int reqargc,
363 char **reqargv) {
364 register int i;
365
366 /* Check the ACL. */
367 if (pr_ctrls_check_acl(ctrl, dso_acttab, "insmod") != TRUE) {
368
369 /* Access denied. */
370 pr_ctrls_add_response(ctrl, "access denied");
371 return PR_CTRLS_STATUS_ACCESS_DENIED;
372 }
373
374 /* Sanity check */
375 if (reqargc == 0 ||
376 reqargv == NULL) {
377 pr_ctrls_add_response(ctrl, "missing required parameters");
378 return PR_CTRLS_STATUS_WRONG_PARAMETERS;
379 }
380
381 for (i = 0; i < reqargc; i++) {
382 int res, xerrno;
383
384 res = dso_load_module(ctrl->ctrls_tmp_pool, reqargv[i]);
385 xerrno = errno;
386
387 if (res < 0) {
388 /* Make the error messages a little more relevant. */
389 switch (xerrno) {
390 case EINVAL:
391 pr_ctrls_add_response(ctrl, "error loading '%s': Bad module name",
392 reqargv[i]);
393 return PR_CTRLS_STATUS_SUBJECT_NOT_FOUND;
394
395 case EEXIST:
396 pr_ctrls_add_response(ctrl, "error loading '%s': Already loaded",
397 reqargv[i]);
398 return PR_CTRLS_STATUS_OPERATION_IGNORED;
399
400 default:
401 pr_ctrls_add_response(ctrl, "error loading '%s': %s", reqargv[i],
402 strerror(xerrno));
403 return PR_CTRLS_STATUS_INTERNAL_ERROR;
404 }
405
406 } else {
407 pr_ctrls_add_response(ctrl, "'%s' loaded", reqargv[i]);
408 }
409 }
410
411 return PR_CTRLS_STATUS_OK;
412 }
413
414 static int dso_handle_lsmod(pr_ctrls_t *ctrl, int reqargc,
415 char **reqargv) {
416 module *m;
417
418 /* Check the ACL. */
419 if (pr_ctrls_check_acl(ctrl, dso_acttab, "lsmod") != TRUE) {
420
421 /* Access denied. */
422 pr_ctrls_add_response(ctrl, "access denied");
423 return PR_CTRLS_STATUS_ACCESS_DENIED;
424 }
425
426 if (reqargc != 0) {
427 pr_ctrls_add_response(ctrl, "wrong number of parameters");
428 return PR_CTRLS_STATUS_WRONG_PARAMETERS;
429 }
430
431 /* We want to show the modules as `proftpd -l` shows them, in module
432 * load order. So first we find the end of the loaded_modules list,
433 * then walk it backwards.
434 */
435 for (m = loaded_modules; m && m->next; m = m->next);
436
437 pr_ctrls_add_response(ctrl, "Loaded Modules:");
438 for (; m; m = m->prev) {
439 pr_ctrls_add_response(ctrl, " mod_%s.c", m->name);
440 }
441
442 return PR_CTRLS_STATUS_OK;
443 }
444
445 static int dso_handle_rmmod(pr_ctrls_t *ctrl, int reqargc,
446 char **reqargv) {
447 register int i;
448
449 /* Check the ACL. */
450 if (pr_ctrls_check_acl(ctrl, dso_acttab, "rmmod") != TRUE) {
451
452 /* Access denied. */
453 pr_ctrls_add_response(ctrl, "access denied");
454 return PR_CTRLS_STATUS_ACCESS_DENIED;
455 }
456
457 /* Sanity check */
458 if (reqargc == 0 ||
459 reqargv == NULL) {
460 pr_ctrls_add_response(ctrl, "missing required parameters");
461 return PR_CTRLS_STATUS_WRONG_PARAMETERS;
462 }
463
464 for (i = 0; i < reqargc; i++) {
465 int res, xerrno;
466
467 res = dso_unload_module_by_name(reqargv[i]);
468 xerrno = errno;
469
470 if (res < 0) {
471 switch (xerrno) {
472 case EINVAL:
473 pr_ctrls_add_response(ctrl, "error unloading '%s': Bad module name",
474 reqargv[i]);
475 return PR_CTRLS_STATUS_SUBJECT_NOT_FOUND;
476
477 case ENOENT:
478 pr_ctrls_add_response(ctrl, "error unloading '%s': Module not loaded",
479 reqargv[i]);
480 return PR_CTRLS_STATUS_SUBJECT_NOT_FOUND;
481
482 default:
483 pr_ctrls_add_response(ctrl, "error unloading '%s': %s",
484 reqargv[i], strerror(errno));
485 return PR_CTRLS_STATUS_INTERNAL_ERROR;
486 }
487
488 } else {
489 pr_ctrls_add_response(ctrl, "'%s' unloaded", reqargv[i]);
490 }
491 }
492
493 return PR_CTRLS_STATUS_OK;
494 }
495 #endif /* PR_USE_CTRLS */
496
497 /* Configuration handlers
498 */
499
500 /* usage: LoadFile path */
501 MODRET set_loadfile(cmd_rec *cmd) {
502 CHECK_ARGS(cmd, 1);
503 CHECK_CONF(cmd, CONF_ROOT);
504
505 if (pr_fs_valid_path(cmd->argv[1]) < 0) {
506 CONF_ERROR(cmd, "must be an absolute path");
507 }
508
509 if (dso_load_file(cmd->argv[1]) < 0) {
510 CONF_ERROR(cmd, pstrcat(cmd->tmp_pool, "error loading '", cmd->argv[1],
511 "': ", strerror(errno), NULL));
512 }
513
514 return PR_HANDLED(cmd);
515 }
516
517 /* usage: LoadModule module */
518 MODRET set_loadmodule(cmd_rec *cmd) {
519 CHECK_ARGS(cmd, 1);
520 CHECK_CONF(cmd, CONF_ROOT);
521
522 if (dso_load_module(cmd->tmp_pool, cmd->argv[1]) < 0) {
523 int xerrno = errno;
524
525 if (xerrno != EEXIST) {
526 CONF_ERROR(cmd, pstrcat(cmd->tmp_pool, "error loading module '",
527 cmd->argv[1], "': ", strerror(xerrno), NULL));
528 }
529 }
530
531 return PR_HANDLED(cmd);
532 }
533
534 /* usage: ModuleControlsACLs actions|all allow|deny user|group list */
535 MODRET set_modulectrlsacls(cmd_rec *cmd) {
536 #if defined(PR_USE_CTRLS)
537 char *bad_action = NULL, **actions = NULL;
538
539 CHECK_ARGS(cmd, 4);
540 CHECK_CONF(cmd, CONF_ROOT);
541
542 actions = pr_ctrls_parse_acl(cmd->tmp_pool, cmd->argv[1]);
543
544 if (strcmp(cmd->argv[2], "allow") != 0 &&
545 strcmp(cmd->argv[2], "deny") != 0) {
546 CONF_ERROR(cmd, "second parameter must be 'allow' or 'deny'");
547 }
548
549 if (strcmp(cmd->argv[3], "user") != 0 &&
550 strcmp(cmd->argv[3], "group") != 0) {
551 CONF_ERROR(cmd, "third parameter must be 'user' or 'group'");
552 }
553
554 bad_action = pr_ctrls_set_module_acls(dso_acttab, dso_pool, actions,
555 cmd->argv[2], cmd->argv[3], cmd->argv[4]);
556 if (bad_action != NULL) {
557 CONF_ERROR(cmd, pstrcat(cmd->tmp_pool, ": unknown action: '",
558 bad_action, "'", NULL));
559 }
560
561 return PR_HANDLED(cmd);
562 #else
563 CONF_ERROR(cmd, "requires Controls support (--enable-ctrls)");
564 #endif
565 }
566
567 /* usage: ModuleOrder mod1 mod2 ... modN */
568 MODRET set_moduleorder(cmd_rec *cmd) {
569 register unsigned int i;
570 module *m, *mn, *module_list = NULL;
571
572 if (cmd->argc-1 < 1)
573 CONF_ERROR(cmd, "wrong number of parameters");
574
575 CHECK_CONF(cmd, CONF_ROOT);
576
577 /* What about duplicate names in the list?
578 *
579 * What if the given list is longer than the one already in loaded_modules?
580 * This will be caught by the existence check. Otherwise, the only way for
581 * the list to be longer is if there are duplicates, which will be caught
582 * by the duplicate check.
583 */
584
585 /* Make sure the given module names exist. */
586 for (i = 1; i < cmd->argc; i++) {
587 if (pr_module_get(cmd->argv[i]) == NULL) {
588 CONF_ERROR(cmd, pstrcat(cmd->tmp_pool, "no such module '", cmd->argv[i],
589 "' loaded", NULL));
590 }
591 }
592
593 /* Make sure there are no duplicate module names in the list. */
594 for (i = 1; i < cmd->argc; i++) {
595 register unsigned int j;
596
597 for (j = i + 1; j < cmd->argc; j++) {
598 if (strcmp(cmd->argv[i], cmd->argv[j]) == 0) {
599 char ibuf[4], jbuf[4];
600
601 pr_snprintf(ibuf, sizeof(ibuf), "%u", i);
602 ibuf[sizeof(ibuf)-1] = '\0';
603
604 pr_snprintf(jbuf, sizeof(jbuf), "%u", j);
605 jbuf[sizeof(jbuf)-1] = '\0';
606
607 CONF_ERROR(cmd, pstrcat(cmd->tmp_pool,
608 "duplicate module name '", cmd->argv[i], "' as parameters ",
609 ibuf, " and ", jbuf, NULL));
610 }
611 }
612 }
613
614 pr_log_debug(DEBUG4, "%s: reordering modules", (char *) cmd->argv[0]);
615 for (i = 1; i < cmd->argc; i++) {
616 m = pr_module_get(cmd->argv[i]);
617
618 if (module_list) {
619 m->next = module_list;
620 module_list->prev = m;
621 module_list = m;
622
623 } else {
624 module_list = m;
625 }
626 }
627
628 /* Now, unload all the modules in the loaded_modules list, then load
629 * the modules in our module_list.
630 */
631 for (m = loaded_modules; m;) {
632 mn = m->next;
633
634 if (pr_module_unload(m) < 0) {
635 pr_log_debug(DEBUG0, "%s: error unloading module 'mod_%s.c': %s",
636 (char *) cmd->argv[0], m->name, strerror(errno));
637 }
638
639 m = mn;
640 }
641
642 for (m = module_list; m; m = m->next) {
643 if (pr_module_load(m) < 0) {
644 pr_log_debug(DEBUG0, "%s: error loading module 'mod_%s.c': %s",
645 (char *) cmd->argv[0], m->name, strerror(errno));
646 exit(1);
647 }
648 }
649
650 pr_log_pri(PR_LOG_NOTICE, "%s: module order is now:", (char *) cmd->argv[0]);
651 for (m = loaded_modules; m; m = m->next) {
652 pr_log_pri(PR_LOG_NOTICE, " mod_%s.c", m->name);
653 }
654
655 return PR_HANDLED(cmd);
656 }
657
658 /* usage: ModulePath path */
659 MODRET set_modulepath(cmd_rec *cmd) {
660 int res;
661 struct stat st;
662
663 CHECK_ARGS(cmd, 1);
664 CHECK_CONF(cmd, CONF_ROOT);
665
666 if (pr_fs_valid_path(cmd->argv[1]) < 0) {
667 CONF_ERROR(cmd, "must be an absolute path");
668 }
669
670 /* Make sure that the configured path is not world-writable. */
671 res = pr_fsio_stat(cmd->argv[1], &st);
672 if (res < 0) {
673 CONF_ERROR(cmd, pstrcat(cmd->tmp_pool, "error checking '",
674 cmd->argv[1], "': ", strerror(errno), NULL));
675 }
676
677 if (!S_ISDIR(st.st_mode)) {
678 CONF_ERROR(cmd, pstrcat(cmd->tmp_pool, cmd->argv[1], " is not a directory",
679 NULL));
680 }
681
682 if (st.st_mode & S_IWOTH) {
683 CONF_ERROR(cmd, pstrcat(cmd->tmp_pool, cmd->argv[1], " is world-writable",
684 NULL));
685 }
686
687 if (lt_dlsetsearchpath(cmd->argv[1]) < 0) {
688 CONF_ERROR(cmd, pstrcat(cmd->tmp_pool, "error setting module path: ",
689 lt_dlerror(), NULL));
690 }
691
692 dso_module_path = pstrdup(dso_pool, cmd->argv[1]);
693 return PR_HANDLED(cmd);
694 }
695
696 /* Event handlers
697 */
698
699 static void dso_restart_ev(const void *event_data, void *user_data) {
700 module *m, *mi;
701 #if defined(PR_USE_CTRLS)
702 register unsigned int i = 0;
703 #endif /* PR_USE_CTRLS */
704
705 if (dso_pool != NULL) {
706 destroy_pool(dso_pool);
707 }
708
709 dso_pool = make_sub_pool(permanent_pool);
710 pr_pool_tag(dso_pool, MOD_DSO_VERSION);
711
712 /* Reset the ModulePath (see Issue #1476). */
713 dso_module_path = PR_LIBEXEC_DIR;
714
715 #if defined(PR_USE_CTRLS)
716 /* Re-register the control handlers */
717 for (i = 0; dso_acttab[i].act_action; i++) {
718 pool *sub_pool = make_sub_pool(dso_pool);
719 pr_pool_tag(sub_pool, "DSO control action pool");
720
721 /* Allocate and initialize the ACL for this control. */
722 dso_acttab[i].act_acl = pcalloc(sub_pool, sizeof(ctrls_acl_t));
723 dso_acttab[i].act_acl->acl_pool = sub_pool;
724 pr_ctrls_init_acl(dso_acttab[i].act_acl);
725 }
726 #endif /* PR_USE_CTRLS */
727
728 /* Unload all shared modules. */
729 for (mi = loaded_modules; mi; mi = m) {
730 #if !defined(PR_USE_CTRLS)
731 register unsigned int i;
732 #endif /* PR_USE_CTRLS */
733 int is_static = FALSE;
734
735 m = mi->next;
736
737 for (i = 0; static_modules[i]; i++) {
738 if (strcmp(mi->name, static_modules[i]->name) == 0) {
739 is_static = TRUE;
740 break;
741 }
742 }
743
744 if (is_static == FALSE) {
745 pr_log_debug(DEBUG7, MOD_DSO_VERSION ": unloading 'mod_%s.c'", mi->name);
746 if (dso_unload_module(mi) < 0) {
747 pr_log_pri(PR_LOG_NOTICE, MOD_DSO_VERSION
748 ": error unloading 'mod_%s.c': %s", mi->name, strerror(errno));
749 }
750 }
751 }
752 }
753
754 /* Initialization routines
755 */
756
757 /* We should be using the LTDL_SET_PRELOADED_SYMBOLS macro provided by
758 * the ltdl.h header. However, doing so resulted in compiler warnings
759 * about "nested extern declaraction of lt_preloaded_symbols". To
760 * work around these warnings, we will use the expanded version of the
761 * macro directly.
762 *
763 * By the way, it appears that this lt_preloaded_symbols list is defined
764 * at link-time by the libtool script.
765 */
766 extern const lt_dlsymlist lt_preloaded_symbols[];
767
768 static int dso_init(void) {
769 #if defined(PR_USE_CTRLS)
770 register unsigned int i = 0;
771 #endif /* PR_USE_CTRLS */
772
773 /* Allocate the pool for this module's use. */
774 dso_pool = make_sub_pool(permanent_pool);
775 pr_pool_tag(dso_pool, MOD_DSO_VERSION);
776
777 lt_dlpreload_default(lt_preloaded_symbols);
778
779 /* Initialize libltdl. */
780 if (lt_dlinit() < 0) {
781 pr_log_pri(PR_LOG_ERR, MOD_DSO_VERSION ": error initializing libltdl: %s",
782 lt_dlerror());
783 return -1;
784 }
785
786 /* Explicitly set the search path used for opening modules. */
787 if (lt_dlsetsearchpath(dso_module_path) < 0) {
788 pr_log_pri(PR_LOG_ERR, MOD_DSO_VERSION ": error setting module path: %s",
789 lt_dlerror());
790 return -1;
791 }
792
793 #if defined(PR_USE_CTRLS)
794 /* Register ctrls handlers. */
795 for (i = 0; dso_acttab[i].act_action; i++) {
796 pool *sub_pool = make_sub_pool(dso_pool);
797 pr_pool_tag(sub_pool, "DSO control action pool");
798
799 /* Allocate and initialize the ACL for this control. */
800 dso_acttab[i].act_acl = pcalloc(sub_pool, sizeof(ctrls_acl_t));
801 dso_acttab[i].act_acl->acl_pool = sub_pool;
802 pr_ctrls_init_acl(dso_acttab[i].act_acl);
803
804 if (pr_ctrls_register(&dso_module, dso_acttab[i].act_action,
805 dso_acttab[i].act_desc, dso_acttab[i].act_cb) < 0) {
806 pr_log_pri(PR_LOG_NOTICE, MOD_DSO_VERSION
807 ": error registering '%s' control: %s", dso_acttab[i].act_action,
808 strerror(errno));
809 }
810 }
811 #endif /* PR_USE_CTRLS */
812
813 /* Ideally, we'd call register a listener for the 'core.exit' event
814 * and call lt_dlexit() there, politely freeing up any resources allocated
815 * by the ltdl library. However, it's possible that other modules, later in
816 * the dispatch cycles, may need to use pointers to memory in shared modules
817 * that would become invalid by such finalization. So we skip it, for now.
818 *
819 * If there was a way to schedule this handler, to happen after all other
820 * exit handlers, that'd be best.
821 */
822 pr_event_register(&dso_module, "core.restart", dso_restart_ev, NULL);
823
824 return 0;
825 }
826
827 static int dso_sess_init(void) {
828 pr_event_unregister(&dso_module, "core.restart", dso_restart_ev);
829 return 0;
830 }
831
832 #if defined(PR_USE_CTRLS)
833 static ctrls_acttab_t dso_acttab[] = {
834 { "insmod", "load modules", NULL, dso_handle_insmod },
835 { "lsmod", "list modules", NULL, dso_handle_lsmod },
836 { "rmmod", "unload modules", NULL, dso_handle_rmmod },
837 { NULL, NULL, NULL, NULL }
838 };
839 #endif /* PR_USE_CTRLS */
840
841 /* Module API tables
842 */
843
844 static conftable dso_conftab[] = {
845 { "LoadFile", set_loadfile, NULL },
846 { "LoadModule", set_loadmodule, NULL },
847 { "ModuleControlsACLs", set_modulectrlsacls, NULL },
848 { "ModuleOrder", set_moduleorder, NULL },
849 { "ModulePath", set_modulepath, NULL },
850 { NULL }
851 };
852
853 module dso_module = {
854 /* Always NULL */
855 NULL, NULL,
856
857 /* Module API version 2.0 */
858 0x20,
859
860 /* Module name */
861 "dso",
862
863 /* Module configuration handler table */
864 dso_conftab,
865
866 /* Module command handler table */
867 NULL,
868
869 /* Module authentication handler table */
870 NULL,
871
872 /* Module initialization function */
873 dso_init,
874
875 /* Session initialization function */
876 dso_sess_init,
877
878 /* Module version */
879 MOD_DSO_VERSION
880 };