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