idmap_ad: Restore querying SFU nss info
[sfrench/samba-autobuild/.git] / source3 / winbindd / nss_info.c
1 /*
2    Unix SMB/CIFS implementation.
3    Idmap NSS headers
4
5    Copyright (C) Gerald Carter             2006
6    Copyright (C) Michael Adam 2008
7
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.
12
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.
17
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/>.
20 */
21
22 #include "includes.h"
23 #include "ads.h"
24 #include "nss_info.h"
25
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;
29
30 /**********************************************************************
31  Get idmap nss methods.
32 **********************************************************************/
33
34 static struct nss_function_entry *nss_get_backend(const char *name )
35 {
36         struct nss_function_entry *entry = backends;
37
38         for(entry = backends; entry; entry = entry->next) {
39                 if ( strequal(entry->name, name) )
40                         return entry;
41         }
42
43         return NULL;
44 }
45
46 /*********************************************************************
47  Allow a module to register itself as a backend.
48 **********************************************************************/
49
50  NTSTATUS smb_register_idmap_nss(int version, const char *name, struct nss_info_methods *methods)
51 {
52         struct nss_function_entry *entry;
53
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;
61         }
62
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;
66         }
67
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;
72         }
73
74         entry = SMB_XMALLOC_P(struct nss_function_entry);
75         entry->name = smb_xstrdup(name);
76         entry->methods = methods;
77
78         DLIST_ADD(backends, entry);
79         DEBUG(5, ("smb_register_idmap_nss: Successfully added idmap "
80                   "nss backend '%s'\n", name));
81
82         return NT_STATUS_OK;
83 }
84
85 /********************************************************************
86  *******************************************************************/
87
88 static bool parse_nss_parm( const char *config, char **backend, char **domain )
89 {
90         char *p;
91
92         *backend = *domain = NULL;
93
94         if ( !config )
95                 return False;
96
97         p = strchr( config, ':' );
98
99         /* if no : then the string must be the backend name only */
100
101         if ( !p ) {
102                 *backend = SMB_STRDUP( config );
103                 return (*backend != NULL);
104         }
105
106         /* split the string and return the two parts */
107
108         if ( strlen(p+1) > 0 ) {
109                 *domain = SMB_STRDUP( p+1 );
110         }
111
112         *backend = SMB_STRNDUP(config, PTR_DIFF(p, config));
113         return (*backend != NULL);
114 }
115
116 static NTSTATUS nss_domain_list_add_domain(const char *domain,
117                                            struct nss_function_entry *nss_backend)
118 {
119         struct nss_domain_entry *nss_domain;
120
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;
125         }
126
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;
135                 }
136         }
137
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));
142         }
143
144         DLIST_ADD(nss_domain_list, nss_domain);
145
146         DEBUG(10, ("Added domain '%s' with backend '%s' to nss_domain_list.\n",
147                    domain, nss_backend->name));
148
149         return NT_STATUS_OK;
150 }
151
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  *******************************************************************/
156
157 static NTSTATUS nss_init(const char **nss_list)
158 {
159         NTSTATUS status;
160         static bool nss_initialized = false;
161         int i;
162         char *backend, *domain;
163         struct nss_function_entry *nss_backend;
164
165         /* check for previous successful initializations */
166
167         if (nss_initialized) {
168                 return NT_STATUS_OK;
169         }
170
171         /* The "template" backend should always be registered as it
172            is a static module */
173
174         nss_backend = nss_get_backend("template");
175         if (nss_backend == NULL) {
176                 static_init_nss_info;
177         }
178
179         /* Create the list of nss_domains (loading any shared plugins
180            as necessary) */
181
182         for ( i=0; nss_list && nss_list[i]; i++ ) {
183
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;
188                 }
189
190                 DEBUG(10, ("parsed backend = '%s', domain = '%s'\n",
191                            backend, domain));
192
193                 /* validate the backend */
194
195                 nss_backend = nss_get_backend(backend);
196                 if (nss_backend == NULL) {
197                         /*
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.
201                          */
202                         status = smb_probe_module("idmap", "ad");
203                         if ( !NT_STATUS_IS_OK(status) ) {
204                                 continue;
205                         }
206                 }
207
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;
214                         }
215                 }
216
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;
223                 }
224
225                 /*
226                  * The first config item of the list without an explicit domain
227                  * is treated as the default nss info backend.
228                  */
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;
233                 }
234
235                 status = nss_domain_list_add_domain(domain, nss_backend);
236                 if (!NT_STATUS_IS_OK(status)) {
237                         return status;
238                 }
239
240                 /* cleanup */
241
242                 SAFE_FREE( backend );
243                 SAFE_FREE( domain );
244         }
245
246         if ( !nss_domain_list ) {
247                 DEBUG(3,("nss_init: no nss backends configured.  "
248                          "Defaulting to \"template\".\n"));
249
250
251                 /* we should default to use template here */
252         }
253
254         nss_initialized = true;
255
256         return NT_STATUS_OK;
257 }
258
259 /********************************************************************
260  *******************************************************************/
261
262 static struct nss_domain_entry *find_nss_domain( const char *domain )
263 {
264         NTSTATUS status;
265         struct nss_domain_entry *p;
266
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;
272         }
273
274         for ( p=nss_domain_list; p; p=p->next ) {
275                 if ( strequal( p->domain, domain ) )
276                         break;
277         }
278
279         /* If we didn't find a match, then use the default nss backend */
280
281         if ( !p ) {
282                 if (!default_backend) {
283                         return NULL;
284                 }
285
286                 status = nss_domain_list_add_domain(domain, default_backend);
287                 if (!NT_STATUS_IS_OK(status)) {
288                         return NULL;
289                 }
290
291                 /*
292                  * HACK ALERT:
293                  * Here, we use the fact that the new domain was added at
294                  * the beginning of the list...
295                  */
296                 p = nss_domain_list;
297         }
298
299         if ( !NT_STATUS_IS_OK( p->init_status ) ) {
300                p->init_status = p->backend->methods->init( p );
301         }
302
303         return p;
304 }
305
306 /********************************************************************
307  *******************************************************************/
308
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)
313 {
314         struct nss_domain_entry *p;
315         struct nss_info_methods *m;
316
317         DEBUG(10, ("nss_get_info called for sid [%s] in domain '%s'\n",
318                    sid_string_dbg(user_sid), domain?domain:"NULL"));
319
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;
324         }
325
326         m = p->backend->methods;
327
328         return m->get_nss_info( p, user_sid, ctx,
329                                 homedir, shell, gecos, p_gid );
330 }
331
332 /********************************************************************
333  *******************************************************************/
334
335  NTSTATUS nss_map_to_alias( TALLOC_CTX *mem_ctx, const char *domain,
336                             const char *name, char **alias )
337 {
338         struct nss_domain_entry *p;
339         struct nss_info_methods *m;
340
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;
345         }
346
347         m = p->backend->methods;
348
349         return m->map_to_alias(mem_ctx, p, name, alias);
350 }
351
352
353 /********************************************************************
354  *******************************************************************/
355
356  NTSTATUS nss_map_from_alias( TALLOC_CTX *mem_ctx, const char *domain,
357                               const char *alias, char **name )
358 {
359         struct nss_domain_entry *p;
360         struct nss_info_methods *m;
361
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;
366         }
367
368         m = p->backend->methods;
369
370         return m->map_from_alias( mem_ctx, p, alias, name );
371 }
372
373 /********************************************************************
374  *******************************************************************/
375
376  NTSTATUS nss_close( const char *parameters )
377 {
378         struct nss_domain_entry *p = nss_domain_list;
379         struct nss_domain_entry *q;
380
381         while ( p && p->backend && p->backend->methods ) {
382                 /* close the backend */
383                 p->backend->methods->close_fn();
384
385                 /* free the memory */
386                 q = p;
387                 p = p->next;
388                 TALLOC_FREE( q );
389         }
390
391         return NT_STATUS_OK;
392 }
393