s3:winbindd/nss_info: add entry debug message to nss_get_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
7    This library is free software; you can redistribute it and/or
8    modify it under the terms of the GNU Lesser General Public
9    License as published by the Free Software Foundation; either
10    version 3 of the License, or (at your option) any later version.
11    
12    This library is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15    Library General Public License for more details.
16    
17    You should have received a copy of the GNU Lesser General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include "includes.h"
22 #include "nss_info.h"
23
24 static struct nss_function_entry *backends = NULL;
25 static struct nss_domain_entry *nss_domain_list = NULL;
26
27 /**********************************************************************
28  Get idmap nss methods.
29 **********************************************************************/
30
31 static struct nss_function_entry *nss_get_backend(const char *name )
32 {
33         struct nss_function_entry *entry = backends;
34
35         for(entry = backends; entry; entry = entry->next) {
36                 if ( strequal(entry->name, name) )
37                         return entry;
38         }
39
40         return NULL;
41 }
42
43 /*********************************************************************
44  Allow a module to register itself as a backend.
45 **********************************************************************/
46
47  NTSTATUS smb_register_idmap_nss(int version, const char *name, struct nss_info_methods *methods)
48 {
49         struct nss_function_entry *entry;
50
51         if ((version != SMB_NSS_INFO_INTERFACE_VERSION)) {
52                 DEBUG(0, ("smb_register_idmap_nss: Failed to register idmap_nss module.\n"
53                           "The module was compiled against SMB_NSS_INFO_INTERFACE_VERSION %d,\n"
54                           "current SMB_NSS_INFO_INTERFACE_VERSION is %d.\n"
55                           "Please recompile against the current version of samba!\n",  
56                           version, SMB_NSS_INFO_INTERFACE_VERSION));
57                 return NT_STATUS_OBJECT_TYPE_MISMATCH;
58         }
59
60         if (!name || !name[0] || !methods) {
61                 DEBUG(0,("smb_register_idmap_nss: called with NULL pointer or empty name!\n"));
62                 return NT_STATUS_INVALID_PARAMETER;
63         }
64
65         if ( nss_get_backend(name) ) {
66                 DEBUG(0,("smb_register_idmap_nss: idmap module %s "
67                          "already registered!\n", name));
68                 return NT_STATUS_OBJECT_NAME_COLLISION;
69         }
70
71         entry = SMB_XMALLOC_P(struct nss_function_entry);
72         entry->name = smb_xstrdup(name);
73         entry->methods = methods;
74
75         DLIST_ADD(backends, entry);
76         DEBUG(5, ("smb_register_idmap_nss: Successfully added idmap "
77                   "nss backend '%s'\n", name));
78
79         return NT_STATUS_OK;
80 }
81
82 /********************************************************************
83  *******************************************************************/
84
85 static bool parse_nss_parm( const char *config, char **backend, char **domain )
86 {
87         char *p;
88         char *q;
89         int len;
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         len = PTR_DIFF(p,config)+1;
112         if ( (q = SMB_MALLOC_ARRAY( char, len )) == NULL ) {
113                 SAFE_FREE( *backend );
114                 return False;
115         }
116         
117         StrnCpy( q, config, len-1);
118         q[len-1] = '\0';
119         *backend = q;
120
121         return True;
122 }
123
124 /********************************************************************
125  Each nss backend must not store global state, but rather be able 
126  to initialize the state on a per domain basis.
127  *******************************************************************/
128
129  NTSTATUS nss_init( const char **nss_list )
130 {
131         NTSTATUS status;
132         static NTSTATUS nss_initialized = NT_STATUS_UNSUCCESSFUL;
133         int i;
134         char *backend, *domain;
135         struct nss_function_entry *nss_backend;
136         struct nss_domain_entry *nss_domain;
137
138         /* check for previous successful initializations */
139
140         if ( NT_STATUS_IS_OK(nss_initialized) )
141                 return NT_STATUS_OK;
142         
143         /* The "template" backend should alqays be registered as it
144            is a static module */
145
146         if ( (nss_backend = nss_get_backend( "template" )) == NULL ) {
147                 static_init_nss_info;          
148         }
149
150         /* Create the list of nss_domains (loading any shared plugins
151            as necessary) */
152
153         for ( i=0; nss_list && nss_list[i]; i++ ) {
154
155                 if ( !parse_nss_parm(nss_list[i], &backend, &domain) )  {
156                         DEBUG(0,("nss_init: failed to parse \"%s\"!\n",
157                                  nss_list[i]));
158                         continue;                       
159                 }
160
161                 DEBUG(10, ("parsed backend = '%s', domain = '%s'\n",
162                            backend, domain));
163
164                 /* validate the backend */
165
166                 if ( (nss_backend = nss_get_backend( backend )) == NULL ) {
167                         /* attempt to register the backend */
168                         status = smb_probe_module( "nss_info", backend );
169                         if ( !NT_STATUS_IS_OK(status) ) {
170                                 continue;
171                         }
172                         
173                         /* try again */
174                         if ( (nss_backend = nss_get_backend( backend )) == NULL ) {
175                                 DEBUG(0,("nss_init: unregistered backend %s!.  Skipping\n",
176                                          backend));
177                                 continue;
178                         }
179
180                 }
181
182                 /* fill in the nss_domain_entry and add it to the 
183                    list of domains */
184
185                 nss_domain = TALLOC_ZERO_P( nss_domain_list, struct nss_domain_entry );
186                 if ( !nss_domain ) {
187                         DEBUG(0,("nss_init: talloc() failure!\n"));
188                         return NT_STATUS_NO_MEMORY;
189                 }
190                 
191                 nss_domain->backend = nss_backend;
192                 nss_domain->domain  = talloc_strdup( nss_domain, domain );
193
194                 /* Try to init and ave the result */
195
196                 nss_domain->init_status = nss_domain->backend->methods->init( nss_domain );
197                         DLIST_ADD( nss_domain_list, nss_domain );
198                 if ( !NT_STATUS_IS_OK(nss_domain->init_status) ) {                      
199                         DEBUG(0,("nss_init: Failed to init backend for %s domain!\n", 
200                                  nss_domain->domain));
201                 }
202
203                 DEBUG(10, ("nss_init: nss domain initialized: "
204                            "domain = '%s', backend = '%s'\n",
205                            domain, backend));
206
207                 /* cleanup */
208
209                 SAFE_FREE( backend );
210                 SAFE_FREE( domain );            
211         }
212
213         if ( !nss_domain_list ) {
214                 DEBUG(3,("nss_init: no nss backends configured.  "
215                          "Defaulting to \"template\".\n"));
216
217
218                 /* we shouild default to use template here */
219         }
220         
221                 
222         nss_initialized = NT_STATUS_OK;
223         
224         return NT_STATUS_OK;
225 }
226
227 /********************************************************************
228  *******************************************************************/
229
230 static struct nss_domain_entry *find_nss_domain( const char *domain )
231 {
232         NTSTATUS status;        
233         struct nss_domain_entry *p;
234
235         status = nss_init( lp_winbind_nss_info() );
236         if ( !NT_STATUS_IS_OK(status) ) {
237                 DEBUG(4,("nss_get_info: Failed to init nss_info API (%s)!\n",
238                          nt_errstr(status)));
239                 return NULL;
240         }
241         
242         for ( p=nss_domain_list; p; p=p->next ) {
243                 if ( strequal( p->domain, domain ) )
244                         break;
245         }
246         
247         /* If we didn't find a match, then use the default nss info */
248
249         if ( !p ) {
250                 if ( !nss_domain_list ) {
251                         return NULL;
252                 }
253                 
254                 p = nss_domain_list;            
255         }
256
257         if ( !NT_STATUS_IS_OK( p->init_status ) ) {
258                p->init_status = p->backend->methods->init( p );
259         }
260
261         return p;
262 }
263
264 /********************************************************************
265  *******************************************************************/
266
267  NTSTATUS nss_get_info( const char *domain, const DOM_SID *user_sid,
268                        TALLOC_CTX *ctx,
269                        ADS_STRUCT *ads, LDAPMessage *msg,
270                        char **homedir, char **shell, char **gecos,
271                        gid_t *p_gid)
272 {
273         struct nss_domain_entry *p;
274         struct nss_info_methods *m;
275
276         DEBUG(10, ("nss_get_info called for sid [%s] in domain '%s'\n",
277                    sid_string_dbg(user_sid), domain?domain:"NULL"));
278
279         if ( (p = find_nss_domain( domain )) == NULL ) {
280                 DEBUG(4,("nss_get_info: Failed to find nss domain pointer for %s\n",
281                          domain ));
282                 return NT_STATUS_NOT_FOUND;
283         }
284                 
285         m = p->backend->methods;
286
287         return m->get_nss_info( p, user_sid, ctx, ads, msg, 
288                                 homedir, shell, gecos, p_gid );
289 }
290
291 /********************************************************************
292  *******************************************************************/
293
294  NTSTATUS nss_map_to_alias( TALLOC_CTX *mem_ctx, const char *domain,
295                             const char *name, char **alias )
296 {
297         struct nss_domain_entry *p;
298         struct nss_info_methods *m;
299
300         if ( (p = find_nss_domain( domain )) == NULL ) {
301                 DEBUG(4,("nss_map_to_alias: Failed to find nss domain pointer for %s\n",
302                          domain ));
303                 return NT_STATUS_NOT_FOUND;
304         }
305
306         m = p->backend->methods;
307
308         return m->map_to_alias(mem_ctx, p, name, alias);
309 }
310
311
312 /********************************************************************
313  *******************************************************************/
314
315  NTSTATUS nss_map_from_alias( TALLOC_CTX *mem_ctx, const char *domain,
316                               const char *alias, char **name )
317 {
318         struct nss_domain_entry *p;
319         struct nss_info_methods *m;
320
321         if ( (p = find_nss_domain( domain )) == NULL ) {
322                 DEBUG(4,("nss_map_from_alias: Failed to find nss domain pointer for %s\n",
323                          domain ));
324                 return NT_STATUS_NOT_FOUND;
325         }
326
327         m = p->backend->methods;
328
329         return m->map_from_alias( mem_ctx, p, alias, name );
330 }
331
332 /********************************************************************
333  *******************************************************************/
334
335  NTSTATUS nss_close( const char *parameters )
336 {
337         struct nss_domain_entry *p = nss_domain_list;
338         struct nss_domain_entry *q;
339
340         while ( p && p->backend && p->backend->methods ) {
341                 /* close the backend */
342                 p->backend->methods->close_fn();
343
344                 /* free the memory */
345                 q = p;
346                 p = p->next;
347                 TALLOC_FREE( q );
348         }
349
350         return NT_STATUS_OK;
351 }
352