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