978053e367b4986cb1942b6b801b6453a55b6e38
[metze/samba/wip.git] / lib / util / modules.c
1 /*
2    Unix SMB/CIFS implementation.
3    Samba utility functions
4    Copyright (C) Jelmer Vernooij 2002-2003,2005-2007
5    Copyright (C) Stefan (metze) Metzmacher 2003
6    Copyright (C) Andrew Bartlett 2011
7
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3 of the License, or
11    (at your option) any later version.
12
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include "includes.h"
23 #include "dynconfig/dynconfig.h"
24 #include "lib/util/samba_modules.h"
25 #include "system/filesys.h"
26 #include "system/dir.h"
27
28 /**
29  * Obtain the init function from a shared library file
30  */
31 init_module_fn load_module(const char *path, bool is_probe, void **handle_out)
32 {
33         void *handle;
34         void *init_fn;
35         char *error;
36
37         /* This should be a WAF build, where modules should be built
38          * with no undefined symbols and are already linked against
39          * the libraries that they are loaded by */
40         handle = dlopen(path, RTLD_NOW);
41
42         /* This call should reset any possible non-fatal errors that
43            occurred since last call to dl* functions */
44         error = dlerror();
45
46         if (handle == NULL) {
47                 int level = is_probe ? 5 : 0;
48                 DEBUG(level, ("Error loading module '%s': %s\n", path, error ? error : ""));
49                 return NULL;
50         }
51
52         init_fn = (init_module_fn)dlsym(handle, SAMBA_INIT_MODULE);
53
54         /* we could check dlerror() to determine if it worked, because
55            dlsym() can validly return NULL, but what would we do with
56            a NULL pointer as a module init function? */
57
58         if (init_fn == NULL) {
59                 DEBUG(0, ("Unable to find %s() in %s: %s\n",
60                           SAMBA_INIT_MODULE, path, dlerror()));
61                 DEBUG(1, ("Loading module '%s' failed\n", path));
62                 dlclose(handle);
63                 return NULL;
64         }
65
66         if (handle_out) {
67                 *handle_out = handle;
68         }
69
70         return (init_module_fn)init_fn;
71 }
72
73 /**
74  * Obtain list of init functions from the modules in the specified
75  * directory
76  */
77 static init_module_fn *load_modules(TALLOC_CTX *mem_ctx, const char *path)
78 {
79         DIR *dir;
80         struct dirent *entry;
81         char *filename;
82         int success = 0;
83         init_module_fn *ret = talloc_array(mem_ctx, init_module_fn, 2);
84
85         ret[0] = NULL;
86
87         dir = opendir(path);
88         if (dir == NULL) {
89                 talloc_free(ret);
90                 return NULL;
91         }
92
93         while((entry = readdir(dir))) {
94                 if (ISDOT(entry->d_name) || ISDOTDOT(entry->d_name))
95                         continue;
96
97                 filename = talloc_asprintf(mem_ctx, "%s/%s", path, entry->d_name);
98
99                 ret[success] = load_module(filename, true, NULL);
100                 if (ret[success]) {
101                         ret = talloc_realloc(mem_ctx, ret, init_module_fn, success+2);
102                         success++;
103                         ret[success] = NULL;
104                 }
105
106                 talloc_free(filename);
107         }
108
109         closedir(dir);
110
111         return ret;
112 }
113
114 /**
115  * Run the specified init functions.
116  *
117  * @return true if all functions ran successfully, false otherwise
118  */
119 bool run_init_functions(TALLOC_CTX *ctx, init_module_fn *fns)
120 {
121         int i;
122         bool ret = true;
123
124         if (fns == NULL)
125                 return true;
126
127         for (i = 0; fns[i]; i++) { ret &= (bool)NT_STATUS_IS_OK(fns[i](ctx)); }
128
129         return ret;
130 }
131
132 /**
133  * Load the initialization functions from DSO files for a specific subsystem.
134  *
135  * Will return an array of function pointers to initialization functions
136  */
137
138 init_module_fn *load_samba_modules(TALLOC_CTX *mem_ctx, const char *subsystem)
139 {
140         char *path = modules_path(mem_ctx, subsystem);
141         init_module_fn *ret;
142
143         ret = load_modules(mem_ctx, path);
144
145         talloc_free(path);
146
147         return ret;
148 }
149
150 static NTSTATUS load_module_absolute_path(const char *module_path,
151                                           bool is_probe)
152 {
153         void *handle;
154         init_module_fn init;
155         NTSTATUS status;
156
157         DBG_INFO("%s module '%s'\n",
158                  is_probe ? "Probing" : "Loading",
159                  module_path);
160
161         init = load_module(module_path, is_probe, &handle);
162         if (init == NULL) {
163                 return NT_STATUS_UNSUCCESSFUL;
164         }
165
166         DBG_NOTICE("Module '%s' loaded\n", module_path);
167
168         status = init(NULL);
169         if (!NT_STATUS_IS_OK(status)) {
170                 DBG_ERR("Module '%s' initialization failed: %s\n",
171                         module_path,
172                         get_friendly_nt_error_msg(status));
173                 dlclose(handle);
174                 return status;
175         }
176
177         return NT_STATUS_OK;
178 }
179
180
181 /* Load a dynamic module.  Only log a level 0 error if we are not checking
182    for the existence of a module (probling). */
183
184 static NTSTATUS do_smb_load_module(const char *subsystem,
185                                    const char *module_name, bool is_probe)
186 {
187         void *handle;
188         init_module_fn init;
189         NTSTATUS status;
190
191         char *full_path = NULL;
192         TALLOC_CTX *ctx = talloc_stackframe();
193
194         if (module_name == NULL) {
195                 TALLOC_FREE(ctx);
196                 return NT_STATUS_INVALID_PARAMETER;
197         }
198
199         /* Check for absolute path */
200
201         DEBUG(5, ("%s module '%s'\n", is_probe ? "Probing" : "Loading", module_name));
202
203         if (subsystem && module_name[0] != '/') {
204                 full_path = talloc_asprintf(ctx,
205                                             "%s/%s.%s",
206                                             modules_path(ctx, subsystem),
207                                             module_name,
208                                             shlib_ext());
209                 if (!full_path) {
210                         TALLOC_FREE(ctx);
211                         return NT_STATUS_NO_MEMORY;
212                 }
213
214                 DEBUG(5, ("%s module '%s': Trying to load from %s\n",
215                           is_probe ? "Probing": "Loading", module_name, full_path));
216                 init = load_module(full_path, is_probe, &handle);
217         } else {
218                 init = load_module(module_name, is_probe, &handle);
219         }
220
221         if (!init) {
222                 TALLOC_FREE(ctx);
223                 return NT_STATUS_UNSUCCESSFUL;
224         }
225
226         DEBUG(2, ("Module '%s' loaded\n", module_name));
227
228         status = init(NULL);
229         if (!NT_STATUS_IS_OK(status)) {
230                 DEBUG(0, ("Module '%s' initialization failed: %s\n",
231                           module_name, get_friendly_nt_error_msg(status)));
232                 dlclose(handle);
233         }
234         TALLOC_FREE(ctx);
235         return status;
236 }
237
238 /* Load all modules in list and return number of
239  * modules that has been successfully loaded */
240 int smb_load_all_modules_absoute_path(const char **modules)
241 {
242         int i;
243         int success = 0;
244
245         for(i = 0; modules[i] != NULL; i++) {
246                 const char *module = modules[i];
247                 NTSTATUS status;
248
249                 if (module[0] != '/') {
250                         continue;
251                 }
252
253                 status = load_module_absolute_path(module, false);
254                 if (NT_STATUS_IS_OK(status)) {
255                         success++;
256                 }
257         }
258
259         DEBUG(2, ("%d modules successfully loaded\n", success));
260
261         return success;
262 }
263
264 /**
265  * @brief Check if a module exist and load it.
266  *
267  * @param[in]  subsystem  The name of the subsystem the module belongs too.
268  *
269  * @param[in]  module     The name of the module
270  *
271  * @return  A NTSTATUS code
272  */
273 NTSTATUS smb_probe_module(const char *subsystem, const char *module)
274 {
275         NTSTATUS status;
276         char *module_path = NULL;
277         TALLOC_CTX *tmp_ctx = talloc_stackframe();
278
279         if (subsystem == NULL) {
280                 status = NT_STATUS_INVALID_PARAMETER;
281                 goto done;
282         }
283         if (module == NULL) {
284                 status = NT_STATUS_INVALID_PARAMETER;
285                 goto done;
286         }
287
288         if (strchr(module, '/')) {
289                 status = NT_STATUS_INVALID_PARAMETER;
290                 goto done;
291         }
292
293         module_path = talloc_asprintf(tmp_ctx,
294                                       "%s/%s.%s",
295                                       modules_path(tmp_ctx, subsystem),
296                                       module,
297                                       shlib_ext());
298         if (module_path == NULL) {
299                 status = NT_STATUS_NO_MEMORY;
300                 goto done;
301         }
302
303         status = load_module_absolute_path(module_path, true);
304
305 done:
306         TALLOC_FREE(tmp_ctx);
307         return status;
308 }
309
310 /**
311  * @brief Check if a module exist and load it.
312  *
313  * Warning: Using this function can have security implecations!
314  *
315  * @param[in]  subsystem  The name of the subsystem the module belongs too.
316  *
317  * @param[in]  module     Load a module using an abolute path.
318  *
319  * @return  A NTSTATUS code
320  */
321 NTSTATUS smb_probe_module_absolute_path(const char *module)
322 {
323         if (module == NULL) {
324                 return NT_STATUS_INVALID_PARAMETER;
325         }
326         if (module[0] != '/') {
327                 return NT_STATUS_INVALID_PARAMETER;
328         }
329
330         return load_module_absolute_path(module, true);
331 }
332
333 NTSTATUS smb_load_module(const char *subsystem, const char *module)
334 {
335         return do_smb_load_module(subsystem, module, false);
336 }