2 Unix SMB/CIFS implementation.
5 Copyright (C) Gerald Carter 2006
6 Copyright (C) Michael Adam 2008
8 This library is free software; you can redistribute it and/or
9 modify it under the terms of the GNU Lesser General Public
10 License as published by the Free Software Foundation; either
11 version 3 of the License, or (at your option) any later version.
13 This library 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 GNU
16 Library General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
25 static struct nss_function_entry *backends = NULL;
26 static struct nss_function_entry *default_backend = NULL;
27 static struct nss_domain_entry *nss_domain_list = NULL;
29 /**********************************************************************
30 Get idmap nss methods.
31 **********************************************************************/
33 static struct nss_function_entry *nss_get_backend(const char *name )
35 struct nss_function_entry *entry = backends;
37 for(entry = backends; entry; entry = entry->next) {
38 if ( strequal(entry->name, name) )
45 /*********************************************************************
46 Allow a module to register itself as a backend.
47 **********************************************************************/
49 NTSTATUS smb_register_idmap_nss(int version, const char *name, struct nss_info_methods *methods)
51 struct nss_function_entry *entry;
53 if ((version != SMB_NSS_INFO_INTERFACE_VERSION)) {
54 DEBUG(0, ("smb_register_idmap_nss: Failed to register idmap_nss module.\n"
55 "The module was compiled against SMB_NSS_INFO_INTERFACE_VERSION %d,\n"
56 "current SMB_NSS_INFO_INTERFACE_VERSION is %d.\n"
57 "Please recompile against the current version of samba!\n",
58 version, SMB_NSS_INFO_INTERFACE_VERSION));
59 return NT_STATUS_OBJECT_TYPE_MISMATCH;
62 if (!name || !name[0] || !methods) {
63 DEBUG(0,("smb_register_idmap_nss: called with NULL pointer or empty name!\n"));
64 return NT_STATUS_INVALID_PARAMETER;
67 if ( nss_get_backend(name) ) {
68 DEBUG(5,("smb_register_idmap_nss: idmap module %s "
69 "already registered!\n", name));
70 return NT_STATUS_OBJECT_NAME_COLLISION;
73 entry = SMB_XMALLOC_P(struct nss_function_entry);
74 entry->name = smb_xstrdup(name);
75 entry->methods = methods;
77 DLIST_ADD(backends, entry);
78 DEBUG(5, ("smb_register_idmap_nss: Successfully added idmap "
79 "nss backend '%s'\n", name));
84 /********************************************************************
85 *******************************************************************/
87 static bool parse_nss_parm( const char *config, char **backend, char **domain )
91 *backend = *domain = NULL;
96 p = strchr( config, ':' );
98 /* if no : then the string must be the backend name only */
101 *backend = SMB_STRDUP( config );
102 return (*backend != NULL);
105 /* split the string and return the two parts */
107 if ( strlen(p+1) > 0 ) {
108 *domain = SMB_STRDUP( p+1 );
111 *backend = SMB_STRNDUP(config, PTR_DIFF(p, config));
112 return (*backend != NULL);
115 static NTSTATUS nss_domain_list_add_domain(const char *domain,
116 struct nss_function_entry *nss_backend)
118 struct nss_domain_entry *nss_domain;
120 nss_domain = talloc_zero(nss_domain_list, struct nss_domain_entry);
122 DEBUG(0, ("nss_domain_list_add_domain: talloc() failure!\n"));
123 return NT_STATUS_NO_MEMORY;
126 nss_domain->backend = nss_backend;
128 nss_domain->domain = talloc_strdup(nss_domain, domain);
129 if (!nss_domain->domain) {
130 DEBUG(0, ("nss_domain_list_add_domain: talloc() "
132 TALLOC_FREE(nss_domain);
133 return NT_STATUS_NO_MEMORY;
137 nss_domain->init_status = nss_domain->backend->methods->init(nss_domain);
138 if (!NT_STATUS_IS_OK(nss_domain->init_status)) {
139 DEBUG(0, ("nss_init: Failed to init backend '%s' for domain "
140 "'%s'!\n", nss_backend->name, nss_domain->domain));
143 DLIST_ADD(nss_domain_list, nss_domain);
145 DEBUG(10, ("Added domain '%s' with backend '%s' to nss_domain_list.\n",
146 domain, nss_backend->name));
151 /********************************************************************
152 Each nss backend must not store global state, but rather be able
153 to initialize the state on a per domain basis.
154 *******************************************************************/
156 static NTSTATUS nss_init(const char **nss_list)
159 static bool nss_initialized = false;
161 char *backend, *domain;
162 struct nss_function_entry *nss_backend;
164 /* check for previous successful initializations */
166 if (nss_initialized) {
170 /* The "template" backend should always be registered as it
171 is a static module */
173 nss_backend = nss_get_backend("template");
174 if (nss_backend == NULL) {
175 static_init_nss_info(NULL);
178 /* Create the list of nss_domains (loading any shared plugins
181 for ( i=0; nss_list && nss_list[i]; i++ ) {
183 if ( !parse_nss_parm(nss_list[i], &backend, &domain) ) {
184 DEBUG(0,("nss_init: failed to parse \"%s\"!\n",
189 DEBUG(10, ("parsed backend = '%s', domain = '%s'\n",
192 /* validate the backend */
194 nss_backend = nss_get_backend(backend);
195 if (nss_backend == NULL) {
197 * This is a freaking hack. We don't have proper
198 * modules for nss_info backends. Right now we have
199 * our standard nss_info backends in the ad backend.
201 status = smb_probe_module("idmap", "ad");
202 if ( !NT_STATUS_IS_OK(status) ) {
207 nss_backend = nss_get_backend(backend);
208 if (nss_backend == NULL) {
209 /* attempt to register the backend */
210 status = smb_probe_module( "nss_info", backend );
211 if ( !NT_STATUS_IS_OK(status) ) {
217 nss_backend = nss_get_backend(backend);
218 if (nss_backend == NULL) {
219 DEBUG(0, ("nss_init: unregistered backend %s!. "
220 "Skipping\n", backend));
225 * The first config item of the list without an explicit domain
226 * is treated as the default nss info backend.
228 if ((domain == NULL) && (default_backend == NULL)) {
229 DEBUG(10, ("nss_init: using '%s' as default backend.\n",
231 default_backend = nss_backend;
234 status = nss_domain_list_add_domain(domain, nss_backend);
235 if (!NT_STATUS_IS_OK(status)) {
241 SAFE_FREE( backend );
245 if ( !nss_domain_list ) {
246 DEBUG(3,("nss_init: no nss backends configured. "
247 "Defaulting to \"template\".\n"));
250 /* we should default to use template here */
253 nss_initialized = true;
258 /********************************************************************
259 *******************************************************************/
261 static struct nss_domain_entry *find_nss_domain( const char *domain )
264 struct nss_domain_entry *p;
266 status = nss_init( lp_winbind_nss_info() );
267 if ( !NT_STATUS_IS_OK(status) ) {
268 DEBUG(4,("find_nss_domain: Failed to init nss_info API "
269 "(%s)!\n", nt_errstr(status)));
273 for ( p=nss_domain_list; p; p=p->next ) {
274 if ( strequal( p->domain, domain ) )
278 /* If we didn't find a match, then use the default nss backend */
281 if (!default_backend) {
285 status = nss_domain_list_add_domain(domain, default_backend);
286 if (!NT_STATUS_IS_OK(status)) {
292 * Here, we use the fact that the new domain was added at
293 * the beginning of the list...
298 if ( !NT_STATUS_IS_OK( p->init_status ) ) {
299 p->init_status = p->backend->methods->init( p );
305 /********************************************************************
306 *******************************************************************/
308 NTSTATUS nss_map_to_alias( TALLOC_CTX *mem_ctx, const char *domain,
309 const char *name, char **alias )
311 struct nss_domain_entry *p;
312 struct nss_info_methods *m;
314 if ( (p = find_nss_domain( domain )) == NULL ) {
315 DEBUG(4,("nss_map_to_alias: Failed to find nss domain pointer for %s\n",
317 return NT_STATUS_NOT_FOUND;
320 m = p->backend->methods;
322 return m->map_to_alias(mem_ctx, p, name, alias);
326 /********************************************************************
327 *******************************************************************/
329 NTSTATUS nss_map_from_alias( TALLOC_CTX *mem_ctx, const char *domain,
330 const char *alias, char **name )
332 struct nss_domain_entry *p;
333 struct nss_info_methods *m;
335 if ( (p = find_nss_domain( domain )) == NULL ) {
336 DEBUG(4,("nss_map_from_alias: Failed to find nss domain pointer for %s\n",
338 return NT_STATUS_NOT_FOUND;
341 m = p->backend->methods;
343 return m->map_from_alias( mem_ctx, p, alias, name );
346 /********************************************************************
347 *******************************************************************/
349 NTSTATUS nss_close( const char *parameters )
351 struct nss_domain_entry *p = nss_domain_list;
352 struct nss_domain_entry *q;
354 while ( p && p->backend && p->backend->methods ) {
355 /* close the backend */
356 p->backend->methods->close_fn();
358 /* free the memory */