summaryrefslogtreecommitdiffstats
path: root/lib/util/modules.c
blob: 4260234b00708158dfba9baed1039416c55fc5bb (plain)
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
/*
   Unix SMB/CIFS implementation.
   Samba utility functions
   Copyright (C) Jelmer Vernooij 2002-2003,2005-2007
   Copyright (C) Stefan (metze) Metzmacher 2003
   Copyright (C) Andrew Bartlett 2011

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "includes.h"
#include "dynconfig/dynconfig.h"
#include "lib/util/samba_modules.h"
#include "lib/util/util_paths.h"
#include "system/filesys.h"
#include "system/dir.h"

/**
 * Obtain the init function from a shared library file
 */
init_module_fn load_module(const char *path, bool is_probe, void **handle_out)
{
	void *handle;
	void *init_fn;
	char *error;

	/* This should be a WAF build, where modules should be built
	 * with no undefined symbols and are already linked against
	 * the libraries that they are loaded by */
	handle = dlopen(path, RTLD_NOW);

	/* This call should reset any possible non-fatal errors that
	   occurred since last call to dl* functions */
	error = dlerror();

	if (handle == NULL) {
		int level = is_probe ? 5 : 0;
		DEBUG(level, ("Error loading module '%s': %s\n", path, error ? error : ""));
		return NULL;
	}

	init_fn = (init_module_fn)dlsym(handle, SAMBA_INIT_MODULE);

	/* we could check dlerror() to determine if it worked, because
           dlsym() can validly return NULL, but what would we do with
           a NULL pointer as a module init function? */

	if (init_fn == NULL) {
		DEBUG(0, ("Unable to find %s() in %s: %s\n",
			  SAMBA_INIT_MODULE, path, dlerror()));
		DEBUG(1, ("Loading module '%s' failed\n", path));
		dlclose(handle);
		return NULL;
	}

	if (handle_out) {
		*handle_out = handle;
	}

	return (init_module_fn)init_fn;
}

/**
 * Obtain list of init functions from the modules in the specified
 * directory
 */
static init_module_fn *load_modules(TALLOC_CTX *mem_ctx, const char *path)
{
	DIR *dir;
	struct dirent *entry;
	char *filename;
	int success = 0;
	init_module_fn *ret = talloc_array(mem_ctx, init_module_fn, 2);

	ret[0] = NULL;

	dir = opendir(path);
	if (dir == NULL) {
		talloc_free(ret);
		return NULL;
	}

	while((entry = readdir(dir))) {
		if (ISDOT(entry->d_name) || ISDOTDOT(entry->d_name))
			continue;

		filename = talloc_asprintf(mem_ctx, "%s/%s", path, entry->d_name);

		ret[success] = load_module(filename, true, NULL);
		if (ret[success]) {
			ret = talloc_realloc(mem_ctx, ret, init_module_fn, success+2);
			success++;
			ret[success] = NULL;
		}

		talloc_free(filename);
	}

	closedir(dir);

	return ret;
}

/**
 * Run the specified init functions.
 *
 * @return true if all functions ran successfully, false otherwise
 */
bool run_init_functions(TALLOC_CTX *ctx, init_module_fn *fns)
{
	int i;
	bool ret = true;

	if (fns == NULL)
		return true;

	for (i = 0; fns[i]; i++) { ret &= (bool)NT_STATUS_IS_OK(fns[i](ctx)); }

	return ret;
}

/**
 * Load the initialization functions from DSO files for a specific subsystem.
 *
 * Will return an array of function pointers to initialization functions
 */

init_module_fn *load_samba_modules(TALLOC_CTX *mem_ctx, const char *subsystem)
{
	char *path = modules_path(mem_ctx, subsystem);
	init_module_fn *ret;

	ret = load_modules(mem_ctx, path);

	talloc_free(path);

	return ret;
}

static NTSTATUS load_module_absolute_path(const char *module_path,
					  bool is_probe)
{
	void *handle;
	init_module_fn init;
	NTSTATUS status;

	DBG_INFO("%s module '%s'\n",
		 is_probe ? "Probing" : "Loading",
		 module_path);

	init = load_module(module_path, is_probe, &handle);
	if (init == NULL) {
		return NT_STATUS_UNSUCCESSFUL;
	}

	DBG_NOTICE("Module '%s' loaded\n", module_path);

	status = init(NULL);
	if (!NT_STATUS_IS_OK(status)) {
		DBG_ERR("Module '%s' initialization failed: %s\n",
			module_path,
			get_friendly_nt_error_msg(status));
		dlclose(handle);
		return status;
	}

	return NT_STATUS_OK;
}

/* Load all modules in list and return number of
 * modules that has been successfully loaded */
int smb_load_all_modules_absoute_path(const char **modules)
{
	int i;
	int success = 0;

	for(i = 0; modules[i] != NULL; i++) {
		const char *module = modules[i];
		NTSTATUS status;

		if (module[0] != '/') {
			continue;
		}

		status = load_module_absolute_path(module, false);
		if (NT_STATUS_IS_OK(status)) {
			success++;
		}
	}

	DEBUG(2, ("%d modules successfully loaded\n", success));

	return success;
}

/**
 * @brief Check if a module exist and load it.
 *
 * @param[in]  subsystem  The name of the subsystem the module belongs too.
 *
 * @param[in]  module     The name of the module
 *
 * @return  A NTSTATUS code
 */
NTSTATUS smb_probe_module(const char *subsystem, const char *module)
{
	NTSTATUS status;
	char *module_path = NULL;
	TALLOC_CTX *tmp_ctx = talloc_stackframe();

	if (subsystem == NULL) {
		status = NT_STATUS_INVALID_PARAMETER;
		goto done;
	}
	if (module == NULL) {
		status = NT_STATUS_INVALID_PARAMETER;
		goto done;
	}

	if (strchr(module, '/')) {
		status = NT_STATUS_INVALID_PARAMETER;
		goto done;
	}

	module_path = talloc_asprintf(tmp_ctx,
				      "%s/%s.%s",
				      modules_path(tmp_ctx, subsystem),
				      module,
				      shlib_ext());
	if (module_path == NULL) {
		status = NT_STATUS_NO_MEMORY;
		goto done;
	}

	status = load_module_absolute_path(module_path, true);

done:
	TALLOC_FREE(tmp_ctx);
	return status;
}

/**
 * @brief Check if a module exist and load it.
 *
 * Warning: Using this function can have security implecations!
 *
 * @param[in]  subsystem  The name of the subsystem the module belongs too.
 *
 * @param[in]  module     Load a module using an absolute path.
 *
 * @return  A NTSTATUS code
 */
NTSTATUS smb_probe_module_absolute_path(const char *module)
{
	if (module == NULL) {
		return NT_STATUS_INVALID_PARAMETER;
	}
	if (module[0] != '/') {
		return NT_STATUS_INVALID_PARAMETER;
	}

	return load_module_absolute_path(module, true);
}

/**
 * @brief Load a module.
 *
 * @param[in]  subsystem  The name of the subsystem the module belongs too.
 *
 * @param[in]  module     Check if a module exists and load it.
 *
 * @return  A NTSTATUS code
 */
NTSTATUS smb_load_module(const char *subsystem, const char *module)
{
	NTSTATUS status;
	char *module_path = NULL;
	TALLOC_CTX *tmp_ctx = talloc_stackframe();

	if (subsystem == NULL) {
		status = NT_STATUS_INVALID_PARAMETER;
		goto done;
	}
	if (module == NULL) {
		status = NT_STATUS_INVALID_PARAMETER;
		goto done;
	}

	if (strchr(module, '/')) {
		status = NT_STATUS_INVALID_PARAMETER;
		goto done;
	}

	module_path = talloc_asprintf(tmp_ctx,
				      "%s/%s.%s",
				      modules_path(tmp_ctx, subsystem),
				      module,
				      shlib_ext());
	if (module_path == NULL) {
		status = NT_STATUS_NO_MEMORY;
		goto done;
	}

	status = load_module_absolute_path(module_path, false);

done:
	TALLOC_FREE(tmp_ctx);
	return status;
}