s3:vfs: Correctly check if OFD locks should be enabled or not
[Samba.git] / source3 / winbindd / nss_info.c
blob1a8325ce7dc6168b22a504674e69de546ba5e51e
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, 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));
81 return NT_STATUS_OK;
84 /********************************************************************
85 *******************************************************************/
87 static bool parse_nss_parm(TALLOC_CTX *mem_ctx,
88 const char *config,
89 char **backend,
90 char **domain)
92 char *p;
94 *backend = *domain = NULL;
96 if ( !config )
97 return False;
99 p = strchr( config, ':' );
101 /* if no : then the string must be the backend name only */
103 if ( !p ) {
104 *backend = talloc_strdup(mem_ctx, config);
105 return (*backend != NULL);
108 /* split the string and return the two parts */
110 if ( strlen(p+1) > 0 ) {
111 *domain = talloc_strdup(mem_ctx, p + 1);
114 *backend = talloc_strndup(mem_ctx, config, PTR_DIFF(p, config));
115 return (*backend != NULL);
118 static NTSTATUS nss_domain_list_add_domain(const char *domain,
119 struct nss_function_entry *nss_backend)
121 struct nss_domain_entry *nss_domain;
123 nss_domain = talloc_zero(nss_domain_list, struct nss_domain_entry);
124 if (!nss_domain) {
125 DEBUG(0, ("nss_domain_list_add_domain: talloc() failure!\n"));
126 return NT_STATUS_NO_MEMORY;
129 nss_domain->backend = nss_backend;
130 if (domain) {
131 nss_domain->domain = talloc_strdup(nss_domain, domain);
132 if (!nss_domain->domain) {
133 DEBUG(0, ("nss_domain_list_add_domain: talloc() "
134 "failure!\n"));
135 TALLOC_FREE(nss_domain);
136 return NT_STATUS_NO_MEMORY;
140 nss_domain->init_status = nss_domain->backend->methods->init(nss_domain);
141 if (!NT_STATUS_IS_OK(nss_domain->init_status)) {
142 DEBUG(0, ("nss_init: Failed to init backend '%s' for domain "
143 "'%s'!\n", nss_backend->name, nss_domain->domain));
146 DLIST_ADD(nss_domain_list, nss_domain);
148 DEBUG(10, ("Added domain '%s' with backend '%s' to nss_domain_list.\n",
149 domain, nss_backend->name));
151 return NT_STATUS_OK;
154 /********************************************************************
155 Each nss backend must not store global state, but rather be able
156 to initialize the state on a per domain basis.
157 *******************************************************************/
159 static NTSTATUS nss_init(const char **nss_list)
161 NTSTATUS status;
162 static bool nss_initialized = false;
163 int i;
164 char *backend = NULL, *domain = NULL;
165 struct nss_function_entry *nss_backend;
166 TALLOC_CTX *frame;
168 /* check for previous successful initializations */
170 if (nss_initialized) {
171 return NT_STATUS_OK;
174 frame = talloc_stackframe();
176 /* The "template" backend should always be registered as it
177 is a static module */
179 nss_backend = nss_get_backend("template");
180 if (nss_backend == NULL) {
181 static_init_nss_info(NULL);
184 /* Create the list of nss_domains (loading any shared plugins
185 as necessary) */
187 for ( i=0; nss_list && nss_list[i]; i++ ) {
188 bool ok;
190 ok = parse_nss_parm(frame, nss_list[i], &backend, &domain);
191 if (!ok) {
192 DEBUG(0,("nss_init: failed to parse \"%s\"!\n",
193 nss_list[i]));
194 continue;
197 DEBUG(10, ("parsed backend = '%s', domain = '%s'\n",
198 backend, domain));
200 /* validate the backend */
202 nss_backend = nss_get_backend(backend);
203 if (nss_backend == NULL) {
205 * This is a freaking hack. We don't have proper
206 * modules for nss_info backends. Right now we have
207 * our standard nss_info backends in the ad backend.
209 status = smb_probe_module("idmap", "ad");
210 if ( !NT_STATUS_IS_OK(status) ) {
211 continue;
215 nss_backend = nss_get_backend(backend);
216 if (nss_backend == NULL) {
217 /* attempt to register the backend */
218 status = smb_probe_module( "nss_info", backend );
219 if ( !NT_STATUS_IS_OK(status) ) {
220 continue;
224 /* try again */
225 nss_backend = nss_get_backend(backend);
226 if (nss_backend == NULL) {
227 DEBUG(0, ("nss_init: unregistered backend %s!. "
228 "Skipping\n", backend));
229 continue;
233 * The first config item of the list without an explicit domain
234 * is treated as the default nss info backend.
236 if ((domain == NULL) && (default_backend == NULL)) {
237 DEBUG(10, ("nss_init: using '%s' as default backend.\n",
238 backend));
239 default_backend = nss_backend;
242 status = nss_domain_list_add_domain(domain, nss_backend);
243 if (!NT_STATUS_IS_OK(status)) {
244 return status;
247 /* cleanup */
249 TALLOC_FREE(domain);
250 TALLOC_FREE(backend);
254 if ( !nss_domain_list ) {
255 DEBUG(3,("nss_init: no nss backends configured. "
256 "Defaulting to \"template\".\n"));
259 /* we should default to use template here */
262 nss_initialized = true;
264 TALLOC_FREE(frame);
265 return NT_STATUS_OK;
268 /********************************************************************
269 *******************************************************************/
271 static struct nss_domain_entry *find_nss_domain( const char *domain )
273 NTSTATUS status;
274 struct nss_domain_entry *p;
276 status = nss_init( lp_winbind_nss_info() );
277 if ( !NT_STATUS_IS_OK(status) ) {
278 DEBUG(4,("find_nss_domain: Failed to init nss_info API "
279 "(%s)!\n", nt_errstr(status)));
280 return NULL;
283 for ( p=nss_domain_list; p; p=p->next ) {
284 if ( strequal( p->domain, domain ) )
285 break;
288 /* If we didn't find a match, then use the default nss backend */
290 if ( !p ) {
291 if (!default_backend) {
292 return NULL;
295 status = nss_domain_list_add_domain(domain, default_backend);
296 if (!NT_STATUS_IS_OK(status)) {
297 return NULL;
301 * HACK ALERT:
302 * Here, we use the fact that the new domain was added at
303 * the beginning of the list...
305 p = nss_domain_list;
308 if ( !NT_STATUS_IS_OK( p->init_status ) ) {
309 p->init_status = p->backend->methods->init( p );
312 return p;
315 /********************************************************************
316 *******************************************************************/
318 NTSTATUS nss_map_to_alias( TALLOC_CTX *mem_ctx, const char *domain,
319 const char *name, char **alias )
321 struct nss_domain_entry *p;
322 struct nss_info_methods *m;
324 if ( (p = find_nss_domain( domain )) == NULL ) {
325 DEBUG(4,("nss_map_to_alias: Failed to find nss domain pointer for %s\n",
326 domain ));
327 return NT_STATUS_NOT_FOUND;
330 m = p->backend->methods;
332 return m->map_to_alias(mem_ctx, p, name, alias);
336 /********************************************************************
337 *******************************************************************/
339 NTSTATUS nss_map_from_alias( TALLOC_CTX *mem_ctx, const char *domain,
340 const char *alias, char **name )
342 struct nss_domain_entry *p;
343 struct nss_info_methods *m;
345 if ( (p = find_nss_domain( domain )) == NULL ) {
346 DEBUG(4,("nss_map_from_alias: Failed to find nss domain pointer for %s\n",
347 domain ));
348 return NT_STATUS_NOT_FOUND;
351 m = p->backend->methods;
353 return m->map_from_alias( mem_ctx, p, alias, name );
356 /********************************************************************
357 *******************************************************************/
359 NTSTATUS nss_close( const char *parameters )
361 struct nss_domain_entry *p = nss_domain_list;
362 struct nss_domain_entry *q;
364 while ( p && p->backend && p->backend->methods ) {
365 /* close the backend */
366 p->backend->methods->close_fn();
368 /* free the memory */
369 q = p;
370 p = p->next;
371 TALLOC_FREE( q );
374 return NT_STATUS_OK;