winbindd: Remove ads.h include from nss_info
[samba.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 "nss_info.h"
24
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;
28
29 /**********************************************************************
30  Get idmap nss methods.
31 **********************************************************************/
32
33 static struct nss_function_entry *nss_get_backend(const char *name )
34 {
35         struct nss_function_entry *entry = backends;
36
37         for(entry = backends; entry; entry = entry->next) {
38                 if ( strequal(entry->name, name) )
39                         return entry;
40         }
41
42         return NULL;
43 }
44
45 /*********************************************************************
46  Allow a module to register itself as a backend.
47 **********************************************************************/
48
49  NTSTATUS smb_register_idmap_nss(int version, const char *name, struct nss_info_methods *methods)
50 {
51         struct nss_function_entry *entry;
52
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;
60         }
61
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;
65         }
66
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;
71         }
72
73         entry = SMB_XMALLOC_P(struct nss_function_entry);
74         entry->name = smb_xstrdup(name);
75         entry->methods = methods;
76
77         DLIST_ADD(backends, entry);
78         DEBUG(5, ("smb_register_idmap_nss: Successfully added idmap "
79                   "nss backend '%s'\n", name));
80
81         return NT_STATUS_OK;
82 }
83
84 /********************************************************************
85  *******************************************************************/
86
87 static bool parse_nss_parm( const char *config, char **backend, char **domain )
88 {
89         char *p;
90
91         *backend = *domain = NULL;
92
93         if ( !config )
94                 return False;
95
96         p = strchr( config, ':' );
97
98         /* if no : then the string must be the backend name only */
99
100         if ( !p ) {
101                 *backend = SMB_STRDUP( config );
102                 return (*backend != NULL);
103         }
104
105         /* split the string and return the two parts */
106
107         if ( strlen(p+1) > 0 ) {
108                 *domain = SMB_STRDUP( p+1 );
109         }
110
111         *backend = SMB_STRNDUP(config, PTR_DIFF(p, config));
112         return (*backend != NULL);
113 }
114
115 static NTSTATUS nss_domain_list_add_domain(const char *domain,
116                                            struct nss_function_entry *nss_backend)
117 {
118         struct nss_domain_entry *nss_domain;
119
120         nss_domain = talloc_zero(nss_domain_list, struct nss_domain_entry);
121         if (!nss_domain) {
122                 DEBUG(0, ("nss_domain_list_add_domain: talloc() failure!\n"));
123                 return NT_STATUS_NO_MEMORY;
124         }
125
126         nss_domain->backend = nss_backend;
127         if (domain) {
128                 nss_domain->domain  = talloc_strdup(nss_domain, domain);
129                 if (!nss_domain->domain) {
130                         DEBUG(0, ("nss_domain_list_add_domain: talloc() "
131                                   "failure!\n"));
132                         TALLOC_FREE(nss_domain);
133                         return NT_STATUS_NO_MEMORY;
134                 }
135         }
136
137         nss_domain->init_status = nss_domain->backend->methods->init(nss_domain);
138         if (!NT_STATUS_IS_OK(nss_domain->init_status))  {
139                 DEBUG(0, ("nss_init: Failed to init backend '%s' for domain "
140                           "'%s'!\n", nss_backend->name, nss_domain->domain));
141         }
142
143         DLIST_ADD(nss_domain_list, nss_domain);
144
145         DEBUG(10, ("Added domain '%s' with backend '%s' to nss_domain_list.\n",
146                    domain, nss_backend->name));
147
148         return NT_STATUS_OK;
149 }
150
151 /********************************************************************
152  Each nss backend must not store global state, but rather be able
153  to initialize the state on a per domain basis.
154  *******************************************************************/
155
156 static NTSTATUS nss_init(const char **nss_list)
157 {
158         NTSTATUS status;
159         static bool nss_initialized = false;
160         int i;
161         char *backend, *domain;
162         struct nss_function_entry *nss_backend;
163
164         /* check for previous successful initializations */
165
166         if (nss_initialized) {
167                 return NT_STATUS_OK;
168         }
169
170         /* The "template" backend should always be registered as it
171            is a static module */
172
173         nss_backend = nss_get_backend("template");
174         if (nss_backend == NULL) {
175                 static_init_nss_info(NULL);
176         }
177
178         /* Create the list of nss_domains (loading any shared plugins
179            as necessary) */
180
181         for ( i=0; nss_list && nss_list[i]; i++ ) {
182
183                 if ( !parse_nss_parm(nss_list[i], &backend, &domain) ) {
184                         DEBUG(0,("nss_init: failed to parse \"%s\"!\n",
185                                  nss_list[i]));
186                         continue;
187                 }
188
189                 DEBUG(10, ("parsed backend = '%s', domain = '%s'\n",
190                            backend, domain));
191
192                 /* validate the backend */
193
194                 nss_backend = nss_get_backend(backend);
195                 if (nss_backend == NULL) {
196                         /*
197                          * This is a freaking hack. We don't have proper
198                          * modules for nss_info backends. Right now we have
199                          * our standard nss_info backends in the ad backend.
200                          */
201                         status = smb_probe_module("idmap", "ad");
202                         if ( !NT_STATUS_IS_OK(status) ) {
203                                 continue;
204                         }
205                 }
206
207                 nss_backend = nss_get_backend(backend);
208                 if (nss_backend == NULL) {
209                         /* attempt to register the backend */
210                         status = smb_probe_module( "nss_info", backend );
211                         if ( !NT_STATUS_IS_OK(status) ) {
212                                 continue;
213                         }
214                 }
215
216                 /* try again */
217                 nss_backend = nss_get_backend(backend);
218                 if (nss_backend == NULL) {
219                         DEBUG(0, ("nss_init: unregistered backend %s!. "
220                                   "Skipping\n", backend));
221                         continue;
222                 }
223
224                 /*
225                  * The first config item of the list without an explicit domain
226                  * is treated as the default nss info backend.
227                  */
228                 if ((domain == NULL) && (default_backend == NULL)) {
229                         DEBUG(10, ("nss_init: using '%s' as default backend.\n",
230                                    backend));
231                         default_backend = nss_backend;
232                 }
233
234                 status = nss_domain_list_add_domain(domain, nss_backend);
235                 if (!NT_STATUS_IS_OK(status)) {
236                         return status;
237                 }
238
239                 /* cleanup */
240
241                 SAFE_FREE( backend );
242                 SAFE_FREE( domain );
243         }
244
245         if ( !nss_domain_list ) {
246                 DEBUG(3,("nss_init: no nss backends configured.  "
247                          "Defaulting to \"template\".\n"));
248
249
250                 /* we should default to use template here */
251         }
252
253         nss_initialized = true;
254
255         return NT_STATUS_OK;
256 }
257
258 /********************************************************************
259  *******************************************************************/
260
261 static struct nss_domain_entry *find_nss_domain( const char *domain )
262 {
263         NTSTATUS status;
264         struct nss_domain_entry *p;
265
266         status = nss_init( lp_winbind_nss_info() );
267         if ( !NT_STATUS_IS_OK(status) ) {
268                 DEBUG(4,("find_nss_domain: Failed to init nss_info API "
269                          "(%s)!\n", nt_errstr(status)));
270                 return NULL;
271         }
272
273         for ( p=nss_domain_list; p; p=p->next ) {
274                 if ( strequal( p->domain, domain ) )
275                         break;
276         }
277
278         /* If we didn't find a match, then use the default nss backend */
279
280         if ( !p ) {
281                 if (!default_backend) {
282                         return NULL;
283                 }
284
285                 status = nss_domain_list_add_domain(domain, default_backend);
286                 if (!NT_STATUS_IS_OK(status)) {
287                         return NULL;
288                 }
289
290                 /*
291                  * HACK ALERT:
292                  * Here, we use the fact that the new domain was added at
293                  * the beginning of the list...
294                  */
295                 p = nss_domain_list;
296         }
297
298         if ( !NT_STATUS_IS_OK( p->init_status ) ) {
299                p->init_status = p->backend->methods->init( p );
300         }
301
302         return p;
303 }
304
305 /********************************************************************
306  *******************************************************************/
307
308  NTSTATUS nss_map_to_alias( TALLOC_CTX *mem_ctx, const char *domain,
309                             const char *name, char **alias )
310 {
311         struct nss_domain_entry *p;
312         struct nss_info_methods *m;
313
314         if ( (p = find_nss_domain( domain )) == NULL ) {
315                 DEBUG(4,("nss_map_to_alias: Failed to find nss domain pointer for %s\n",
316                          domain ));
317                 return NT_STATUS_NOT_FOUND;
318         }
319
320         m = p->backend->methods;
321
322         return m->map_to_alias(mem_ctx, p, name, alias);
323 }
324
325
326 /********************************************************************
327  *******************************************************************/
328
329  NTSTATUS nss_map_from_alias( TALLOC_CTX *mem_ctx, const char *domain,
330                               const char *alias, char **name )
331 {
332         struct nss_domain_entry *p;
333         struct nss_info_methods *m;
334
335         if ( (p = find_nss_domain( domain )) == NULL ) {
336                 DEBUG(4,("nss_map_from_alias: Failed to find nss domain pointer for %s\n",
337                          domain ));
338                 return NT_STATUS_NOT_FOUND;
339         }
340
341         m = p->backend->methods;
342
343         return m->map_from_alias( mem_ctx, p, alias, name );
344 }
345
346 /********************************************************************
347  *******************************************************************/
348
349  NTSTATUS nss_close( const char *parameters )
350 {
351         struct nss_domain_entry *p = nss_domain_list;
352         struct nss_domain_entry *q;
353
354         while ( p && p->backend && p->backend->methods ) {
355                 /* close the backend */
356                 p->backend->methods->close_fn();
357
358                 /* free the memory */
359                 q = p;
360                 p = p->next;
361                 TALLOC_FREE( q );
362         }
363
364         return NT_STATUS_OK;
365 }
366