Initial import
[samba] / source / passdb / pdb_plugin.c
1 /* 
2    Unix SMB/CIFS implementation.
3    Loadable passdb module interface.
4    Copyright (C) Jelmer Vernooij 2002
5    Copyright (C) Andrew Bartlett 2002
6       
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11    
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16    
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22 #include "includes.h"
23
24 #undef DBGC_CLASS
25 #define DBGC_CLASS DBGC_PASSDB
26
27 NTSTATUS pdb_init_plugin(PDB_CONTEXT *pdb_context, PDB_METHODS **pdb_method, const char *location)
28 {
29         void * dl_handle;
30         char *plugin_location, *plugin_name, *p;
31         pdb_init_function plugin_init;
32         int (*plugin_version)(void);
33
34         if (location == NULL) {
35                 DEBUG(0, ("The plugin module needs an argument!\n"));
36                 return NT_STATUS_UNSUCCESSFUL;
37         }
38
39         plugin_name = smb_xstrdup(location);
40         p = strchr(plugin_name, ':');
41         if (p) {
42                 *p = 0;
43                 plugin_location = p+1;
44                 trim_char(plugin_location, ' ', ' ');
45         } else {
46                 plugin_location = NULL;
47         }
48         trim_char(plugin_name, ' ', ' ');
49
50         DEBUG(5, ("Trying to load sam plugin %s\n", plugin_name));
51         dl_handle = sys_dlopen(plugin_name, RTLD_NOW );
52         if (!dl_handle) {
53                 DEBUG(0, ("Failed to load sam plugin %s using sys_dlopen (%s)\n", plugin_name, sys_dlerror()));
54                 return NT_STATUS_UNSUCCESSFUL;
55         }
56     
57         plugin_version = sys_dlsym(dl_handle, "pdb_version");
58         if (!plugin_version) {
59                 sys_dlclose(dl_handle);
60                 DEBUG(0, ("Failed to find function 'pdb_version' using sys_dlsym in sam plugin %s (%s)\n", plugin_name, sys_dlerror()));            
61                 return NT_STATUS_UNSUCCESSFUL;
62         }
63
64         if (plugin_version() != PASSDB_INTERFACE_VERSION) {
65                 sys_dlclose(dl_handle);
66                 DEBUG(0, ("Wrong PASSDB_INTERFACE_VERSION! sam plugin has version %d and version %d is needed! Please update!\n",
67                             plugin_version(),PASSDB_INTERFACE_VERSION));
68                 return NT_STATUS_UNSUCCESSFUL;
69         }
70                                         
71         plugin_init = sys_dlsym(dl_handle, "pdb_init");
72         if (!plugin_init) {
73                 sys_dlclose(dl_handle);
74                 DEBUG(0, ("Failed to find function 'pdb_init' using sys_dlsym in sam plugin %s (%s)\n", plugin_name, sys_dlerror()));       
75                 return NT_STATUS_UNSUCCESSFUL;
76         }
77
78         DEBUG(5, ("Starting sam plugin %s with location %s\n", plugin_name, plugin_location));
79         return plugin_init(pdb_context, pdb_method, plugin_location);
80 }