selftest: fix domain name of nt4_dc_smb1 environment
[Samba.git] / source3 / winbindd / nss_info.c
blob9c502e84ef0609c8771bc63db0522ae69000d921
1 /*
2 Unix SMB/CIFS implementation.
3 Idmap NSS headers
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/>.
22 #include "includes.h"
23 #include "nss_info.h"
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) )
39 return entry;
42 return NULL;
45 /*********************************************************************
46 Allow a module to register itself as a backend.
47 **********************************************************************/
49 NTSTATUS smb_register_idmap_nss(int version, const char *name,
50 const struct nss_info_methods *methods)
52 struct nss_function_entry *entry;
54 if ((version != SMB_NSS_INFO_INTERFACE_VERSION)) {
55 DEBUG(0, ("smb_register_idmap_nss: Failed to register idmap_nss module.\n"
56 "The module was compiled against SMB_NSS_INFO_INTERFACE_VERSION %d,\n"
57 "current SMB_NSS_INFO_INTERFACE_VERSION is %d.\n"
58 "Please recompile against the current version of samba!\n",
59 version, SMB_NSS_INFO_INTERFACE_VERSION));
60 return NT_STATUS_OBJECT_TYPE_MISMATCH;
63 if (!name || !name[0] || !methods) {
64 DEBUG(0,("smb_register_idmap_nss: called with NULL pointer or empty name!\n"));
65 return NT_STATUS_INVALID_PARAMETER;
68 if ( nss_get_backend(name) ) {
69 DEBUG(5,("smb_register_idmap_nss: idmap module %s "
70 "already registered!\n", name));
71 return NT_STATUS_OBJECT_NAME_COLLISION;
74 entry = SMB_XMALLOC_P(struct nss_function_entry);
75 entry->name = smb_xstrdup(name);
76 entry->methods = methods;
78 DLIST_ADD(backends, entry);
79 DEBUG(5, ("smb_register_idmap_nss: Successfully added idmap "
80 "nss backend '%s'\n", name));
82 return NT_STATUS_OK;
85 /********************************************************************
86 *******************************************************************/
88 static bool parse_nss_parm(TALLOC_CTX *mem_ctx,
89 const char *config,
90 char **backend,
91 char **domain)
93 char *p;
95 *backend = *domain = NULL;
97 if ( !config )
98 return False;
100 p = strchr( config, ':' );
102 /* if no : then the string must be the backend name only */
104 if ( !p ) {
105 *backend = talloc_strdup(mem_ctx, config);
106 return (*backend != NULL);
109 /* split the string and return the two parts */
111 if ( strlen(p+1) > 0 ) {
112 *domain = talloc_strdup(mem_ctx, p + 1);
115 *backend = talloc_strndup(mem_ctx, config, PTR_DIFF(p, config));
116 return (*backend != NULL);
119 static NTSTATUS nss_domain_list_add_domain(const char *domain,
120 struct nss_function_entry *nss_backend)
122 struct nss_domain_entry *nss_domain;
124 nss_domain = talloc_zero(nss_domain_list, struct nss_domain_entry);
125 if (!nss_domain) {
126 DEBUG(0, ("nss_domain_list_add_domain: talloc() failure!\n"));
127 return NT_STATUS_NO_MEMORY;
130 nss_domain->backend = nss_backend;
131 if (domain) {
132 nss_domain->domain = talloc_strdup(nss_domain, domain);
133 if (!nss_domain->domain) {
134 DEBUG(0, ("nss_domain_list_add_domain: talloc() "
135 "failure!\n"));
136 TALLOC_FREE(nss_domain);
137 return NT_STATUS_NO_MEMORY;
141 nss_domain->init_status = nss_domain->backend->methods->init(nss_domain);
142 if (!NT_STATUS_IS_OK(nss_domain->init_status)) {
143 DEBUG(0, ("nss_init: Failed to init backend '%s' for domain "
144 "'%s'!\n", nss_backend->name, nss_domain->domain));
147 DLIST_ADD(nss_domain_list, nss_domain);
149 DEBUG(10, ("Added domain '%s' with backend '%s' to nss_domain_list.\n",
150 domain, nss_backend->name));
152 return NT_STATUS_OK;
155 /********************************************************************
156 Each nss backend must not store global state, but rather be able
157 to initialize the state on a per domain basis.
158 *******************************************************************/
160 static NTSTATUS nss_init(const char **nss_list)
162 NTSTATUS status;
163 static bool nss_initialized = false;
164 int i;
165 char *backend = NULL, *domain = NULL;
166 struct nss_function_entry *nss_backend;
167 TALLOC_CTX *frame;
169 /* check for previous successful initializations */
171 if (nss_initialized) {
172 return NT_STATUS_OK;
175 frame = talloc_stackframe();
177 /* The "template" backend should always be registered as it
178 is a static module */
180 nss_backend = nss_get_backend("template");
181 if (nss_backend == NULL) {
182 static_init_nss_info(NULL);
185 /* Create the list of nss_domains (loading any shared plugins
186 as necessary) */
188 for ( i=0; nss_list && nss_list[i]; i++ ) {
189 bool ok;
191 ok = parse_nss_parm(frame, nss_list[i], &backend, &domain);
192 if (!ok) {
193 DEBUG(0,("nss_init: failed to parse \"%s\"!\n",
194 nss_list[i]));
195 continue;
198 DEBUG(10, ("parsed backend = '%s', domain = '%s'\n",
199 backend, domain));
201 /* validate the backend */
203 nss_backend = nss_get_backend(backend);
204 if (nss_backend == NULL) {
206 * This is a freaking hack. We don't have proper
207 * modules for nss_info backends. Right now we have
208 * our standard nss_info backends in the ad backend.
210 status = smb_probe_module("idmap", "ad");
211 if ( !NT_STATUS_IS_OK(status) ) {
212 continue;
216 nss_backend = nss_get_backend(backend);
217 if (nss_backend == NULL) {
218 /* attempt to register the backend */
219 status = smb_probe_module( "nss_info", backend );
220 if ( !NT_STATUS_IS_OK(status) ) {
221 continue;
225 /* try again */
226 nss_backend = nss_get_backend(backend);
227 if (nss_backend == NULL) {
228 DEBUG(0, ("nss_init: unregistered backend %s!. "
229 "Skipping\n", backend));
230 continue;
234 * The first config item of the list without an explicit domain
235 * is treated as the default nss info backend.
237 if ((domain == NULL) && (default_backend == NULL)) {
238 DEBUG(10, ("nss_init: using '%s' as default backend.\n",
239 backend));
240 default_backend = nss_backend;
243 status = nss_domain_list_add_domain(domain, nss_backend);
244 if (!NT_STATUS_IS_OK(status)) {
245 return status;
248 /* cleanup */
250 TALLOC_FREE(domain);
251 TALLOC_FREE(backend);
255 if ( !nss_domain_list ) {
256 DEBUG(3,("nss_init: no nss backends configured. "
257 "Defaulting to \"template\".\n"));
260 /* we should default to use template here */
263 nss_initialized = true;
265 TALLOC_FREE(frame);
266 return NT_STATUS_OK;
269 /********************************************************************
270 *******************************************************************/
272 static struct nss_domain_entry *find_nss_domain( const char *domain )
274 NTSTATUS status;
275 struct nss_domain_entry *p;
277 status = nss_init( lp_winbind_nss_info() );
278 if ( !NT_STATUS_IS_OK(status) ) {
279 DEBUG(4,("find_nss_domain: Failed to init nss_info API "
280 "(%s)!\n", nt_errstr(status)));
281 return NULL;
284 for ( p=nss_domain_list; p; p=p->next ) {
285 if ( strequal( p->domain, domain ) )
286 break;
289 /* If we didn't find a match, then use the default nss backend */
291 if ( !p ) {
292 if (!default_backend) {
293 return NULL;
296 status = nss_domain_list_add_domain(domain, default_backend);
297 if (!NT_STATUS_IS_OK(status)) {
298 return NULL;
302 * HACK ALERT:
303 * Here, we use the fact that the new domain was added at
304 * the beginning of the list...
306 p = nss_domain_list;
309 if ( !NT_STATUS_IS_OK( p->init_status ) ) {
310 p->init_status = p->backend->methods->init( p );
313 return p;
316 /********************************************************************
317 *******************************************************************/
319 NTSTATUS nss_map_to_alias( TALLOC_CTX *mem_ctx, const char *domain,
320 const char *name, char **alias )
322 struct nss_domain_entry *p;
323 const struct nss_info_methods *m;
325 if ( (p = find_nss_domain( domain )) == NULL ) {
326 DEBUG(4,("nss_map_to_alias: Failed to find nss domain pointer for %s\n",
327 domain ));
328 return NT_STATUS_NOT_FOUND;
331 m = p->backend->methods;
333 return m->map_to_alias(mem_ctx, p, name, alias);
337 /********************************************************************
338 *******************************************************************/
340 NTSTATUS nss_map_from_alias( TALLOC_CTX *mem_ctx, const char *domain,
341 const char *alias, char **name )
343 struct nss_domain_entry *p;
344 const struct nss_info_methods *m;
346 if ( (p = find_nss_domain( domain )) == NULL ) {
347 DEBUG(4,("nss_map_from_alias: Failed to find nss domain pointer for %s\n",
348 domain ));
349 return NT_STATUS_NOT_FOUND;
352 m = p->backend->methods;
354 return m->map_from_alias( mem_ctx, p, alias, name );
357 /********************************************************************
358 *******************************************************************/
360 NTSTATUS nss_close( const char *parameters )
362 struct nss_domain_entry *p = nss_domain_list;
363 struct nss_domain_entry *q;
365 while ( p && p->backend && p->backend->methods ) {
366 /* close the backend */
367 p->backend->methods->close_fn();
369 /* free the memory */
370 q = p;
371 p = p->next;
372 TALLOC_FREE( q );
375 return NT_STATUS_OK;