tdb: fix non-WAF build, commit 1.2.6 ABI file.
[Samba.git] / source3 / winbindd / nss_info.c
blob67ffbf21cc48329b547d1816a2c6dc7467a342f8
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 "ads.h"
24 #include "nss_info.h"
26 static struct nss_function_entry *backends = NULL;
27 static struct nss_function_entry *default_backend = NULL;
28 static struct nss_domain_entry *nss_domain_list = NULL;
30 /**********************************************************************
31 Get idmap nss methods.
32 **********************************************************************/
34 static struct nss_function_entry *nss_get_backend(const char *name )
36 struct nss_function_entry *entry = backends;
38 for(entry = backends; entry; entry = entry->next) {
39 if ( strequal(entry->name, name) )
40 return entry;
43 return NULL;
46 /*********************************************************************
47 Allow a module to register itself as a backend.
48 **********************************************************************/
50 NTSTATUS smb_register_idmap_nss(int version, const char *name, 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(0,("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( const char *config, char **backend, char **domain )
90 char *p;
91 char *q;
92 int len;
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 = SMB_STRDUP( config );
105 return (*backend != NULL);
108 /* split the string and return the two parts */
110 if ( strlen(p+1) > 0 ) {
111 *domain = SMB_STRDUP( p+1 );
114 len = PTR_DIFF(p,config)+1;
115 if ( (q = SMB_MALLOC_ARRAY( char, len )) == NULL ) {
116 SAFE_FREE( *backend );
117 return False;
120 StrnCpy( q, config, len-1);
121 q[len-1] = '\0';
122 *backend = q;
124 return True;
127 static NTSTATUS nss_domain_list_add_domain(const char *domain,
128 struct nss_function_entry *nss_backend)
130 struct nss_domain_entry *nss_domain;
132 nss_domain = TALLOC_ZERO_P(nss_domain_list, struct nss_domain_entry);
133 if (!nss_domain) {
134 DEBUG(0, ("nss_domain_list_add_domain: talloc() failure!\n"));
135 return NT_STATUS_NO_MEMORY;
138 nss_domain->backend = nss_backend;
139 if (domain) {
140 nss_domain->domain = talloc_strdup(nss_domain, domain);
141 if (!nss_domain->domain) {
142 DEBUG(0, ("nss_domain_list_add_domain: talloc() "
143 "failure!\n"));
144 TALLOC_FREE(nss_domain);
145 return NT_STATUS_NO_MEMORY;
149 nss_domain->init_status = nss_domain->backend->methods->init(nss_domain);
150 if (!NT_STATUS_IS_OK(nss_domain->init_status)) {
151 DEBUG(0, ("nss_init: Failed to init backend '%s' for domain "
152 "'%s'!\n", nss_backend->name, nss_domain->domain));
155 DLIST_ADD(nss_domain_list, nss_domain);
157 DEBUG(10, ("Added domain '%s' with backend '%s' to nss_domain_list.\n",
158 domain, nss_backend->name));
160 return NT_STATUS_OK;
163 /********************************************************************
164 Each nss backend must not store global state, but rather be able
165 to initialize the state on a per domain basis.
166 *******************************************************************/
168 static NTSTATUS nss_init(const char **nss_list)
170 NTSTATUS status;
171 static bool nss_initialized = false;
172 int i;
173 char *backend, *domain;
174 struct nss_function_entry *nss_backend;
176 /* check for previous successful initializations */
178 if (nss_initialized) {
179 return NT_STATUS_OK;
182 /* The "template" backend should always be registered as it
183 is a static module */
185 nss_backend = nss_get_backend("template");
186 if (nss_backend == NULL) {
187 static_init_nss_info;
190 /* Create the list of nss_domains (loading any shared plugins
191 as necessary) */
193 for ( i=0; nss_list && nss_list[i]; i++ ) {
195 if ( !parse_nss_parm(nss_list[i], &backend, &domain) ) {
196 DEBUG(0,("nss_init: failed to parse \"%s\"!\n",
197 nss_list[i]));
198 continue;
201 DEBUG(10, ("parsed backend = '%s', domain = '%s'\n",
202 backend, domain));
204 /* validate the backend */
206 nss_backend = nss_get_backend(backend);
207 if (nss_backend == NULL) {
208 /* attempt to register the backend */
209 status = smb_probe_module( "nss_info", backend );
210 if ( !NT_STATUS_IS_OK(status) ) {
211 continue;
215 /* try again */
216 nss_backend = nss_get_backend(backend);
217 if (nss_backend == NULL) {
218 DEBUG(0, ("nss_init: unregistered backend %s!. "
219 "Skipping\n", backend));
220 continue;
224 * The first config item of the list without an explicit domain
225 * is treated as the default nss info backend.
227 if ((domain == NULL) && (default_backend == NULL)) {
228 DEBUG(10, ("nss_init: using '%s' as default backend.\n",
229 backend));
230 default_backend = nss_backend;
233 status = nss_domain_list_add_domain(domain, nss_backend);
234 if (!NT_STATUS_IS_OK(status)) {
235 return status;
238 /* cleanup */
240 SAFE_FREE( backend );
241 SAFE_FREE( domain );
244 if ( !nss_domain_list ) {
245 DEBUG(3,("nss_init: no nss backends configured. "
246 "Defaulting to \"template\".\n"));
249 /* we should default to use template here */
252 nss_initialized = true;
254 return NT_STATUS_OK;
257 /********************************************************************
258 *******************************************************************/
260 static struct nss_domain_entry *find_nss_domain( const char *domain )
262 NTSTATUS status;
263 struct nss_domain_entry *p;
265 status = nss_init( lp_winbind_nss_info() );
266 if ( !NT_STATUS_IS_OK(status) ) {
267 DEBUG(4,("find_nss_domain: Failed to init nss_info API "
268 "(%s)!\n", nt_errstr(status)));
269 return NULL;
272 for ( p=nss_domain_list; p; p=p->next ) {
273 if ( strequal( p->domain, domain ) )
274 break;
277 /* If we didn't find a match, then use the default nss backend */
279 if ( !p ) {
280 if (!default_backend) {
281 return NULL;
284 status = nss_domain_list_add_domain(domain, default_backend);
285 if (!NT_STATUS_IS_OK(status)) {
286 return NULL;
290 * HACK ALERT:
291 * Here, we use the fact that the new domain was added at
292 * the beginning of the list...
294 p = nss_domain_list;
297 if ( !NT_STATUS_IS_OK( p->init_status ) ) {
298 p->init_status = p->backend->methods->init( p );
301 return p;
304 /********************************************************************
305 *******************************************************************/
307 NTSTATUS nss_get_info( const char *domain, const struct dom_sid *user_sid,
308 TALLOC_CTX *ctx,
309 ADS_STRUCT *ads, LDAPMessage *msg,
310 const char **homedir, const char **shell,
311 const char **gecos, gid_t *p_gid)
313 struct nss_domain_entry *p;
314 struct nss_info_methods *m;
316 DEBUG(10, ("nss_get_info called for sid [%s] in domain '%s'\n",
317 sid_string_dbg(user_sid), domain?domain:"NULL"));
319 if ( (p = find_nss_domain( domain )) == NULL ) {
320 DEBUG(4,("nss_get_info: Failed to find nss domain pointer for %s\n",
321 domain ));
322 return NT_STATUS_NOT_FOUND;
325 m = p->backend->methods;
327 return m->get_nss_info( p, user_sid, ctx, ads, msg,
328 homedir, shell, gecos, p_gid );
331 /********************************************************************
332 *******************************************************************/
334 NTSTATUS nss_map_to_alias( TALLOC_CTX *mem_ctx, const char *domain,
335 const char *name, char **alias )
337 struct nss_domain_entry *p;
338 struct nss_info_methods *m;
340 if ( (p = find_nss_domain( domain )) == NULL ) {
341 DEBUG(4,("nss_map_to_alias: Failed to find nss domain pointer for %s\n",
342 domain ));
343 return NT_STATUS_NOT_FOUND;
346 m = p->backend->methods;
348 return m->map_to_alias(mem_ctx, p, name, alias);
352 /********************************************************************
353 *******************************************************************/
355 NTSTATUS nss_map_from_alias( TALLOC_CTX *mem_ctx, const char *domain,
356 const char *alias, char **name )
358 struct nss_domain_entry *p;
359 struct nss_info_methods *m;
361 if ( (p = find_nss_domain( domain )) == NULL ) {
362 DEBUG(4,("nss_map_from_alias: Failed to find nss domain pointer for %s\n",
363 domain ));
364 return NT_STATUS_NOT_FOUND;
367 m = p->backend->methods;
369 return m->map_from_alias( mem_ctx, p, alias, name );
372 /********************************************************************
373 *******************************************************************/
375 NTSTATUS nss_close( const char *parameters )
377 struct nss_domain_entry *p = nss_domain_list;
378 struct nss_domain_entry *q;
380 while ( p && p->backend && p->backend->methods ) {
381 /* close the backend */
382 p->backend->methods->close_fn();
384 /* free the memory */
385 q = p;
386 p = p->next;
387 TALLOC_FREE( q );
390 return NT_STATUS_OK;