a7b642440fc81c7a547126469f1e26600fd90dce
[metze/samba/wip.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(0,("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         char *q;
91         int len;
92
93         *backend = *domain = NULL;
94
95         if ( !config )
96                 return False;
97
98         p = strchr( config, ':' );
99
100         /* if no : then the string must be the backend name only */
101
102         if ( !p ) {
103                 *backend = SMB_STRDUP( config );
104                 return (*backend != NULL);
105         }
106
107         /* split the string and return the two parts */
108
109         if ( strlen(p+1) > 0 ) {
110                 *domain = SMB_STRDUP( p+1 );
111         }
112
113         len = PTR_DIFF(p,config)+1;
114         if ( (q = SMB_MALLOC_ARRAY( char, len )) == NULL ) {
115                 SAFE_FREE( *backend );
116                 return False;
117         }
118
119         StrnCpy( q, config, len-1);
120         q[len-1] = '\0';
121         *backend = q;
122
123         return True;
124 }
125
126 NTSTATUS nss_domain_list_add_domain(const char *domain,
127                                     struct nss_function_entry *nss_backend)
128 {
129         struct nss_domain_entry *nss_domain;
130
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;
135         }
136
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;
145                 }
146         }
147
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));
152         }
153
154         DLIST_ADD(nss_domain_list, nss_domain);
155
156         DEBUG(10, ("Added domain '%s' with backend '%s' to nss_domain_list.\n",
157                    domain, nss_backend->name));
158
159         return NT_STATUS_OK;
160 }
161
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  *******************************************************************/
166
167  NTSTATUS nss_init( const char **nss_list )
168 {
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;
174         struct nss_domain_entry *nss_domain;
175
176         /* check for previous successful initializations */
177
178         if ( NT_STATUS_IS_OK(nss_initialized) )
179                 return NT_STATUS_OK;
180
181         /* The "template" backend should alqays be registered as it
182            is a static module */
183
184         if ( (nss_backend = nss_get_backend( "template" )) == NULL ) {
185                 static_init_nss_info;
186         }
187
188         /* Create the list of nss_domains (loading any shared plugins
189            as necessary) */
190
191         for ( i=0; nss_list && nss_list[i]; i++ ) {
192
193                 if ( !parse_nss_parm(nss_list[i], &backend, &domain) ) {
194                         DEBUG(0,("nss_init: failed to parse \"%s\"!\n",
195                                  nss_list[i]));
196                         continue;
197                 }
198
199                 DEBUG(10, ("parsed backend = '%s', domain = '%s'\n",
200                            backend, domain));
201
202                 /* validate the backend */
203
204                 if ( (nss_backend = nss_get_backend( backend )) == NULL ) {
205                         /* attempt to register the backend */
206                         status = smb_probe_module( "nss_info", backend );
207                         if ( !NT_STATUS_IS_OK(status) ) {
208                                 continue;
209                         }
210
211                         /* try again */
212                         if ( (nss_backend = nss_get_backend( backend )) == NULL ) {
213                                 DEBUG(0,("nss_init: unregistered backend %s!.  Skipping\n",
214                                          backend));
215                                 continue;
216                         }
217                 }
218
219                 /*
220                  * The first config item of the list without an explicit domain
221                  * is treated as the default nss info backend.
222                  */
223                 if ((domain == NULL) && (default_backend == NULL)) {
224                         DEBUG(10, ("nss_init: using '%s' as default backend.\n",
225                                    backend));
226                         default_backend = nss_backend;
227                 }
228
229                 status = nss_domain_list_add_domain(domain, nss_backend);
230                 if (!NT_STATUS_IS_OK(status)) {
231                         return status;
232                 }
233
234                 /* cleanup */
235
236                 SAFE_FREE( backend );
237                 SAFE_FREE( domain );
238         }
239
240         if ( !nss_domain_list ) {
241                 DEBUG(3,("nss_init: no nss backends configured.  "
242                          "Defaulting to \"template\".\n"));
243
244
245                 /* we shouild default to use template here */
246         }
247
248         nss_initialized = NT_STATUS_OK;
249
250         return NT_STATUS_OK;
251 }
252
253 /********************************************************************
254  *******************************************************************/
255
256 static struct nss_domain_entry *find_nss_domain( const char *domain )
257 {
258         NTSTATUS status;
259         struct nss_domain_entry *p;
260
261         status = nss_init( lp_winbind_nss_info() );
262         if ( !NT_STATUS_IS_OK(status) ) {
263                 DEBUG(4,("nss_get_info: Failed to init nss_info API (%s)!\n",
264                          nt_errstr(status)));
265                 return NULL;
266         }
267
268         for ( p=nss_domain_list; p; p=p->next ) {
269                 if ( strequal( p->domain, domain ) )
270                         break;
271         }
272
273         /* If we didn't find a match, then use the default nss backend */
274
275         if ( !p ) {
276                 if (!default_backend) {
277                         return NULL;
278                 }
279
280                 status = nss_domain_list_add_domain(domain, default_backend);
281                 if (!NT_STATUS_IS_OK(status)) {
282                         return NULL;
283                 }
284
285                 /*
286                  * HACK ALERT:
287                  * Here, we use the fact that the new domain was added at
288                  * the beginning of the list...
289                  */
290                 p = nss_domain_list;
291         }
292
293         if ( !NT_STATUS_IS_OK( p->init_status ) ) {
294                p->init_status = p->backend->methods->init( p );
295         }
296
297         return p;
298 }
299
300 /********************************************************************
301  *******************************************************************/
302
303  NTSTATUS nss_get_info( const char *domain, const DOM_SID *user_sid,
304                        TALLOC_CTX *ctx,
305                        ADS_STRUCT *ads, LDAPMessage *msg,
306                        char **homedir, char **shell, char **gecos,
307                        gid_t *p_gid)
308 {
309         struct nss_domain_entry *p;
310         struct nss_info_methods *m;
311
312         DEBUG(10, ("nss_get_info called for sid [%s] in domain '%s'\n",
313                    sid_string_dbg(user_sid), domain?domain:"NULL"));
314
315         if ( (p = find_nss_domain( domain )) == NULL ) {
316                 DEBUG(4,("nss_get_info: Failed to find nss domain pointer for %s\n",
317                          domain ));
318                 return NT_STATUS_NOT_FOUND;
319         }
320
321         m = p->backend->methods;
322
323         return m->get_nss_info( p, user_sid, ctx, ads, msg,
324                                 homedir, shell, gecos, p_gid );
325 }
326
327 /********************************************************************
328  *******************************************************************/
329
330  NTSTATUS nss_map_to_alias( TALLOC_CTX *mem_ctx, const char *domain,
331                             const char *name, char **alias )
332 {
333         struct nss_domain_entry *p;
334         struct nss_info_methods *m;
335
336         if ( (p = find_nss_domain( domain )) == NULL ) {
337                 DEBUG(4,("nss_map_to_alias: Failed to find nss domain pointer for %s\n",
338                          domain ));
339                 return NT_STATUS_NOT_FOUND;
340         }
341
342         m = p->backend->methods;
343
344         return m->map_to_alias(mem_ctx, p, name, alias);
345 }
346
347
348 /********************************************************************
349  *******************************************************************/
350
351  NTSTATUS nss_map_from_alias( TALLOC_CTX *mem_ctx, const char *domain,
352                               const char *alias, char **name )
353 {
354         struct nss_domain_entry *p;
355         struct nss_info_methods *m;
356
357         if ( (p = find_nss_domain( domain )) == NULL ) {
358                 DEBUG(4,("nss_map_from_alias: Failed to find nss domain pointer for %s\n",
359                          domain ));
360                 return NT_STATUS_NOT_FOUND;
361         }
362
363         m = p->backend->methods;
364
365         return m->map_from_alias( mem_ctx, p, alias, name );
366 }
367
368 /********************************************************************
369  *******************************************************************/
370
371  NTSTATUS nss_close( const char *parameters )
372 {
373         struct nss_domain_entry *p = nss_domain_list;
374         struct nss_domain_entry *q;
375
376         while ( p && p->backend && p->backend->methods ) {
377                 /* close the backend */
378                 p->backend->methods->close_fn();
379
380                 /* free the memory */
381                 q = p;
382                 p = p->next;
383                 TALLOC_FREE( q );
384         }
385
386         return NT_STATUS_OK;
387 }
388