s4: lib: auth: If NTLMSSP_NEGOTIATE_TARGET_INFO isn't set, cope with servers that...
[Samba.git] / source3 / winbindd / nss_info.c
blobd7306ed71acaf92fa4f194873e60add37e5e70c0
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(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( const char *config, char **backend, char **domain )
90 char *p;
92 *backend = *domain = NULL;
94 if ( !config )
95 return False;
97 p = strchr( config, ':' );
99 /* if no : then the string must be the backend name only */
101 if ( !p ) {
102 *backend = SMB_STRDUP( config );
103 return (*backend != NULL);
106 /* split the string and return the two parts */
108 if ( strlen(p+1) > 0 ) {
109 *domain = SMB_STRDUP( p+1 );
112 *backend = SMB_STRNDUP(config, PTR_DIFF(p, config));
113 return (*backend != NULL);
116 static NTSTATUS nss_domain_list_add_domain(const char *domain,
117 struct nss_function_entry *nss_backend)
119 struct nss_domain_entry *nss_domain;
121 nss_domain = talloc_zero(nss_domain_list, struct nss_domain_entry);
122 if (!nss_domain) {
123 DEBUG(0, ("nss_domain_list_add_domain: talloc() failure!\n"));
124 return NT_STATUS_NO_MEMORY;
127 nss_domain->backend = nss_backend;
128 if (domain) {
129 nss_domain->domain = talloc_strdup(nss_domain, domain);
130 if (!nss_domain->domain) {
131 DEBUG(0, ("nss_domain_list_add_domain: talloc() "
132 "failure!\n"));
133 TALLOC_FREE(nss_domain);
134 return NT_STATUS_NO_MEMORY;
138 nss_domain->init_status = nss_domain->backend->methods->init(nss_domain);
139 if (!NT_STATUS_IS_OK(nss_domain->init_status)) {
140 DEBUG(0, ("nss_init: Failed to init backend '%s' for domain "
141 "'%s'!\n", nss_backend->name, nss_domain->domain));
144 DLIST_ADD(nss_domain_list, nss_domain);
146 DEBUG(10, ("Added domain '%s' with backend '%s' to nss_domain_list.\n",
147 domain, nss_backend->name));
149 return NT_STATUS_OK;
152 /********************************************************************
153 Each nss backend must not store global state, but rather be able
154 to initialize the state on a per domain basis.
155 *******************************************************************/
157 static NTSTATUS nss_init(const char **nss_list)
159 NTSTATUS status;
160 static bool nss_initialized = false;
161 int i;
162 char *backend, *domain;
163 struct nss_function_entry *nss_backend;
165 /* check for previous successful initializations */
167 if (nss_initialized) {
168 return NT_STATUS_OK;
171 /* The "template" backend should always be registered as it
172 is a static module */
174 nss_backend = nss_get_backend("template");
175 if (nss_backend == NULL) {
176 static_init_nss_info;
179 /* Create the list of nss_domains (loading any shared plugins
180 as necessary) */
182 for ( i=0; nss_list && nss_list[i]; i++ ) {
184 if ( !parse_nss_parm(nss_list[i], &backend, &domain) ) {
185 DEBUG(0,("nss_init: failed to parse \"%s\"!\n",
186 nss_list[i]));
187 continue;
190 DEBUG(10, ("parsed backend = '%s', domain = '%s'\n",
191 backend, domain));
193 /* validate the backend */
195 nss_backend = nss_get_backend(backend);
196 if (nss_backend == NULL) {
198 * This is a freaking hack. We don't have proper
199 * modules for nss_info backends. Right now we have
200 * our standard nss_info backends in the ad backend.
202 status = smb_probe_module("idmap", "ad");
203 if ( !NT_STATUS_IS_OK(status) ) {
204 continue;
208 nss_backend = nss_get_backend(backend);
209 if (nss_backend == NULL) {
210 /* attempt to register the backend */
211 status = smb_probe_module( "nss_info", backend );
212 if ( !NT_STATUS_IS_OK(status) ) {
213 continue;
217 /* try again */
218 nss_backend = nss_get_backend(backend);
219 if (nss_backend == NULL) {
220 DEBUG(0, ("nss_init: unregistered backend %s!. "
221 "Skipping\n", backend));
222 continue;
226 * The first config item of the list without an explicit domain
227 * is treated as the default nss info backend.
229 if ((domain == NULL) && (default_backend == NULL)) {
230 DEBUG(10, ("nss_init: using '%s' as default backend.\n",
231 backend));
232 default_backend = nss_backend;
235 status = nss_domain_list_add_domain(domain, nss_backend);
236 if (!NT_STATUS_IS_OK(status)) {
237 return status;
240 /* cleanup */
242 SAFE_FREE( backend );
243 SAFE_FREE( domain );
246 if ( !nss_domain_list ) {
247 DEBUG(3,("nss_init: no nss backends configured. "
248 "Defaulting to \"template\".\n"));
251 /* we should default to use template here */
254 nss_initialized = true;
256 return NT_STATUS_OK;
259 /********************************************************************
260 *******************************************************************/
262 static struct nss_domain_entry *find_nss_domain( const char *domain )
264 NTSTATUS status;
265 struct nss_domain_entry *p;
267 status = nss_init( lp_winbind_nss_info() );
268 if ( !NT_STATUS_IS_OK(status) ) {
269 DEBUG(4,("find_nss_domain: Failed to init nss_info API "
270 "(%s)!\n", nt_errstr(status)));
271 return NULL;
274 for ( p=nss_domain_list; p; p=p->next ) {
275 if ( strequal( p->domain, domain ) )
276 break;
279 /* If we didn't find a match, then use the default nss backend */
281 if ( !p ) {
282 if (!default_backend) {
283 return NULL;
286 status = nss_domain_list_add_domain(domain, default_backend);
287 if (!NT_STATUS_IS_OK(status)) {
288 return NULL;
292 * HACK ALERT:
293 * Here, we use the fact that the new domain was added at
294 * the beginning of the list...
296 p = nss_domain_list;
299 if ( !NT_STATUS_IS_OK( p->init_status ) ) {
300 p->init_status = p->backend->methods->init( p );
303 return p;
306 /********************************************************************
307 *******************************************************************/
309 NTSTATUS nss_get_info( const char *domain, const struct dom_sid *user_sid,
310 TALLOC_CTX *ctx,
311 const char **homedir, const char **shell,
312 const char **gecos, gid_t *p_gid)
314 struct nss_domain_entry *p;
315 struct nss_info_methods *m;
317 DEBUG(10, ("nss_get_info called for sid [%s] in domain '%s'\n",
318 sid_string_dbg(user_sid), domain?domain:"NULL"));
320 if ( (p = find_nss_domain( domain )) == NULL ) {
321 DEBUG(4,("nss_get_info: Failed to find nss domain pointer for %s\n",
322 domain ));
323 return NT_STATUS_NOT_FOUND;
326 m = p->backend->methods;
328 return m->get_nss_info( p, user_sid, ctx,
329 homedir, shell, gecos, p_gid );
332 /********************************************************************
333 *******************************************************************/
335 NTSTATUS nss_map_to_alias( TALLOC_CTX *mem_ctx, const char *domain,
336 const char *name, char **alias )
338 struct nss_domain_entry *p;
339 struct nss_info_methods *m;
341 if ( (p = find_nss_domain( domain )) == NULL ) {
342 DEBUG(4,("nss_map_to_alias: Failed to find nss domain pointer for %s\n",
343 domain ));
344 return NT_STATUS_NOT_FOUND;
347 m = p->backend->methods;
349 return m->map_to_alias(mem_ctx, p, name, alias);
353 /********************************************************************
354 *******************************************************************/
356 NTSTATUS nss_map_from_alias( TALLOC_CTX *mem_ctx, const char *domain,
357 const char *alias, char **name )
359 struct nss_domain_entry *p;
360 struct nss_info_methods *m;
362 if ( (p = find_nss_domain( domain )) == NULL ) {
363 DEBUG(4,("nss_map_from_alias: Failed to find nss domain pointer for %s\n",
364 domain ));
365 return NT_STATUS_NOT_FOUND;
368 m = p->backend->methods;
370 return m->map_from_alias( mem_ctx, p, alias, name );
373 /********************************************************************
374 *******************************************************************/
376 NTSTATUS nss_close( const char *parameters )
378 struct nss_domain_entry *p = nss_domain_list;
379 struct nss_domain_entry *q;
381 while ( p && p->backend && p->backend->methods ) {
382 /* close the backend */
383 p->backend->methods->close_fn();
385 /* free the memory */
386 q = p;
387 p = p->next;
388 TALLOC_FREE( q );
391 return NT_STATUS_OK;