s3:winbindd/nss_info: remove unused variable from nss_init()
[Samba/bb.git] / source3 / winbindd / nss_info.c
blob734c0096024b663cc6e4ee3817564c09d5d222fb
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(0,("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( const char *config, char **backend, char **domain )
89 char *p;
90 char *q;
91 int len;
93 *backend = *domain = NULL;
95 if ( !config )
96 return False;
98 p = strchr( config, ':' );
100 /* if no : then the string must be the backend name only */
102 if ( !p ) {
103 *backend = SMB_STRDUP( config );
104 return (*backend != NULL);
107 /* split the string and return the two parts */
109 if ( strlen(p+1) > 0 ) {
110 *domain = SMB_STRDUP( p+1 );
113 len = PTR_DIFF(p,config)+1;
114 if ( (q = SMB_MALLOC_ARRAY( char, len )) == NULL ) {
115 SAFE_FREE( *backend );
116 return False;
119 StrnCpy( q, config, len-1);
120 q[len-1] = '\0';
121 *backend = q;
123 return True;
126 static NTSTATUS nss_domain_list_add_domain(const char *domain,
127 struct nss_function_entry *nss_backend)
129 struct nss_domain_entry *nss_domain;
131 nss_domain = TALLOC_ZERO_P(nss_domain_list, struct nss_domain_entry);
132 if (!nss_domain) {
133 DEBUG(0, ("nss_domain_list_add_domain: talloc() failure!\n"));
134 return NT_STATUS_NO_MEMORY;
137 nss_domain->backend = nss_backend;
138 if (domain) {
139 nss_domain->domain = talloc_strdup(nss_domain, domain);
140 if (!nss_domain->domain) {
141 DEBUG(0, ("nss_domain_list_add_domain: talloc() "
142 "failure!\n"));
143 TALLOC_FREE(nss_domain);
144 return NT_STATUS_NO_MEMORY;
148 nss_domain->init_status = nss_domain->backend->methods->init(nss_domain);
149 if (!NT_STATUS_IS_OK(nss_domain->init_status)) {
150 DEBUG(0, ("nss_init: Failed to init backend '%s' for domain "
151 "'%s'!\n", nss_backend->name, nss_domain->domain));
154 DLIST_ADD(nss_domain_list, nss_domain);
156 DEBUG(10, ("Added domain '%s' with backend '%s' to nss_domain_list.\n",
157 domain, nss_backend->name));
159 return NT_STATUS_OK;
162 /********************************************************************
163 Each nss backend must not store global state, but rather be able
164 to initialize the state on a per domain basis.
165 *******************************************************************/
167 NTSTATUS nss_init( const char **nss_list )
169 NTSTATUS status;
170 static NTSTATUS nss_initialized = NT_STATUS_UNSUCCESSFUL;
171 int i;
172 char *backend, *domain;
173 struct nss_function_entry *nss_backend;
175 /* check for previous successful initializations */
177 if ( NT_STATUS_IS_OK(nss_initialized) )
178 return NT_STATUS_OK;
180 /* The "template" backend should alqays be registered as it
181 is a static module */
183 if ( (nss_backend = nss_get_backend( "template" )) == NULL ) {
184 static_init_nss_info;
187 /* Create the list of nss_domains (loading any shared plugins
188 as necessary) */
190 for ( i=0; nss_list && nss_list[i]; i++ ) {
192 if ( !parse_nss_parm(nss_list[i], &backend, &domain) ) {
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 if ( (nss_backend = nss_get_backend( backend )) == NULL ) {
204 /* attempt to register the backend */
205 status = smb_probe_module( "nss_info", backend );
206 if ( !NT_STATUS_IS_OK(status) ) {
207 continue;
210 /* try again */
211 if ( (nss_backend = nss_get_backend( backend )) == NULL ) {
212 DEBUG(0,("nss_init: unregistered backend %s!. Skipping\n",
213 backend));
214 continue;
219 * The first config item of the list without an explicit domain
220 * is treated as the default nss info backend.
222 if ((domain == NULL) && (default_backend == NULL)) {
223 DEBUG(10, ("nss_init: using '%s' as default backend.\n",
224 backend));
225 default_backend = nss_backend;
228 status = nss_domain_list_add_domain(domain, nss_backend);
229 if (!NT_STATUS_IS_OK(status)) {
230 return status;
233 /* cleanup */
235 SAFE_FREE( backend );
236 SAFE_FREE( domain );
239 if ( !nss_domain_list ) {
240 DEBUG(3,("nss_init: no nss backends configured. "
241 "Defaulting to \"template\".\n"));
244 /* we shouild default to use template here */
247 nss_initialized = NT_STATUS_OK;
249 return NT_STATUS_OK;
252 /********************************************************************
253 *******************************************************************/
255 static struct nss_domain_entry *find_nss_domain( const char *domain )
257 NTSTATUS status;
258 struct nss_domain_entry *p;
260 status = nss_init( lp_winbind_nss_info() );
261 if ( !NT_STATUS_IS_OK(status) ) {
262 DEBUG(4,("nss_get_info: Failed to init nss_info API (%s)!\n",
263 nt_errstr(status)));
264 return NULL;
267 for ( p=nss_domain_list; p; p=p->next ) {
268 if ( strequal( p->domain, domain ) )
269 break;
272 /* If we didn't find a match, then use the default nss backend */
274 if ( !p ) {
275 if (!default_backend) {
276 return NULL;
279 status = nss_domain_list_add_domain(domain, default_backend);
280 if (!NT_STATUS_IS_OK(status)) {
281 return NULL;
285 * HACK ALERT:
286 * Here, we use the fact that the new domain was added at
287 * the beginning of the list...
289 p = nss_domain_list;
292 if ( !NT_STATUS_IS_OK( p->init_status ) ) {
293 p->init_status = p->backend->methods->init( p );
296 return p;
299 /********************************************************************
300 *******************************************************************/
302 NTSTATUS nss_get_info( const char *domain, const DOM_SID *user_sid,
303 TALLOC_CTX *ctx,
304 ADS_STRUCT *ads, LDAPMessage *msg,
305 char **homedir, char **shell, char **gecos,
306 gid_t *p_gid)
308 struct nss_domain_entry *p;
309 struct nss_info_methods *m;
311 DEBUG(10, ("nss_get_info called for sid [%s] in domain '%s'\n",
312 sid_string_dbg(user_sid), domain?domain:"NULL"));
314 if ( (p = find_nss_domain( domain )) == NULL ) {
315 DEBUG(4,("nss_get_info: Failed to find nss domain pointer for %s\n",
316 domain ));
317 return NT_STATUS_NOT_FOUND;
320 m = p->backend->methods;
322 return m->get_nss_info( p, user_sid, ctx, ads, msg,
323 homedir, shell, gecos, p_gid );
326 /********************************************************************
327 *******************************************************************/
329 NTSTATUS nss_map_to_alias( TALLOC_CTX *mem_ctx, const char *domain,
330 const char *name, char **alias )
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_to_alias: Failed to find nss domain pointer for %s\n",
337 domain ));
338 return NT_STATUS_NOT_FOUND;
341 m = p->backend->methods;
343 return m->map_to_alias(mem_ctx, p, name, alias);
347 /********************************************************************
348 *******************************************************************/
350 NTSTATUS nss_map_from_alias( TALLOC_CTX *mem_ctx, const char *domain,
351 const char *alias, char **name )
353 struct nss_domain_entry *p;
354 struct nss_info_methods *m;
356 if ( (p = find_nss_domain( domain )) == NULL ) {
357 DEBUG(4,("nss_map_from_alias: Failed to find nss domain pointer for %s\n",
358 domain ));
359 return NT_STATUS_NOT_FOUND;
362 m = p->backend->methods;
364 return m->map_from_alias( mem_ctx, p, alias, name );
367 /********************************************************************
368 *******************************************************************/
370 NTSTATUS nss_close( const char *parameters )
372 struct nss_domain_entry *p = nss_domain_list;
373 struct nss_domain_entry *q;
375 while ( p && p->backend && p->backend->methods ) {
376 /* close the backend */
377 p->backend->methods->close_fn();
379 /* free the memory */
380 q = p;
381 p = p->next;
382 TALLOC_FREE( q );
385 return NT_STATUS_OK;