2 Unix SMB/CIFS implementation.
5 Copyright (C) Gerald Carter 2006
6 Copyright (C) Michael Adam 2008
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.
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.
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/>.
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
;
29 /**********************************************************************
30 Get idmap nss methods.
31 **********************************************************************/
33 static struct nss_function_entry
*nss_get_backend(const char *name
)
35 struct nss_function_entry
*entry
= backends
;
37 for(entry
= backends
; entry
; entry
= entry
->next
) {
38 if ( strequal(entry
->name
, name
) )
45 /*********************************************************************
46 Allow a module to register itself as a backend.
47 **********************************************************************/
49 NTSTATUS
smb_register_idmap_nss(int version
, const char *name
, struct nss_info_methods
*methods
)
51 struct nss_function_entry
*entry
;
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
;
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
;
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
;
73 entry
= SMB_XMALLOC_P(struct nss_function_entry
);
74 entry
->name
= smb_xstrdup(name
);
75 entry
->methods
= methods
;
77 DLIST_ADD(backends
, entry
);
78 DEBUG(5, ("smb_register_idmap_nss: Successfully added idmap "
79 "nss backend '%s'\n", name
));
84 /********************************************************************
85 *******************************************************************/
87 static bool parse_nss_parm( const char *config
, char **backend
, char **domain
)
93 *backend
= *domain
= NULL
;
98 p
= strchr( config
, ':' );
100 /* if no : then the string must be the backend name only */
103 *backend
= SMB_STRDUP( config
);
104 return (*backend
!= NULL
);
107 /* split the string and return the two parts */
109 if ( strlen(p
+1) > 0 ) {
110 *domain
= SMB_STRDUP( p
+1 );
113 len
= PTR_DIFF(p
,config
)+1;
114 if ( (q
= SMB_MALLOC_ARRAY( char, len
)) == NULL
) {
115 SAFE_FREE( *backend
);
119 StrnCpy( q
, config
, len
-1);
126 static NTSTATUS
nss_domain_list_add_domain(const char *domain
,
127 struct nss_function_entry
*nss_backend
)
129 struct nss_domain_entry
*nss_domain
;
131 nss_domain
= TALLOC_ZERO_P(nss_domain_list
, struct nss_domain_entry
);
133 DEBUG(0, ("nss_domain_list_add_domain: talloc() failure!\n"));
134 return NT_STATUS_NO_MEMORY
;
137 nss_domain
->backend
= nss_backend
;
139 nss_domain
->domain
= talloc_strdup(nss_domain
, domain
);
140 if (!nss_domain
->domain
) {
141 DEBUG(0, ("nss_domain_list_add_domain: talloc() "
143 TALLOC_FREE(nss_domain
);
144 return NT_STATUS_NO_MEMORY
;
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
));
154 DLIST_ADD(nss_domain_list
, nss_domain
);
156 DEBUG(10, ("Added domain '%s' with backend '%s' to nss_domain_list.\n",
157 domain
, nss_backend
->name
));
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 *******************************************************************/
167 static NTSTATUS
nss_init(const char **nss_list
)
170 static bool nss_initialized
= false;
172 char *backend
, *domain
;
173 struct nss_function_entry
*nss_backend
;
175 /* check for previous successful initializations */
177 if (nss_initialized
) {
181 /* The "template" backend should always be registered as it
182 is a static module */
184 nss_backend
= nss_get_backend("template");
185 if (nss_backend
== NULL
) {
186 static_init_nss_info
;
189 /* Create the list of nss_domains (loading any shared plugins
192 for ( i
=0; nss_list
&& nss_list
[i
]; i
++ ) {
194 if ( !parse_nss_parm(nss_list
[i
], &backend
, &domain
) ) {
195 DEBUG(0,("nss_init: failed to parse \"%s\"!\n",
200 DEBUG(10, ("parsed backend = '%s', domain = '%s'\n",
203 /* validate the backend */
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
) ) {
215 nss_backend
= nss_get_backend(backend
);
216 if (nss_backend
== NULL
) {
217 DEBUG(0, ("nss_init: unregistered backend %s!. "
218 "Skipping\n", backend
));
223 * The first config item of the list without an explicit domain
224 * is treated as the default nss info backend.
226 if ((domain
== NULL
) && (default_backend
== NULL
)) {
227 DEBUG(10, ("nss_init: using '%s' as default backend.\n",
229 default_backend
= nss_backend
;
232 status
= nss_domain_list_add_domain(domain
, nss_backend
);
233 if (!NT_STATUS_IS_OK(status
)) {
239 SAFE_FREE( backend
);
243 if ( !nss_domain_list
) {
244 DEBUG(3,("nss_init: no nss backends configured. "
245 "Defaulting to \"template\".\n"));
248 /* we should default to use template here */
251 nss_initialized
= true;
256 /********************************************************************
257 *******************************************************************/
259 static struct nss_domain_entry
*find_nss_domain( const char *domain
)
262 struct nss_domain_entry
*p
;
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
)));
271 for ( p
=nss_domain_list
; p
; p
=p
->next
) {
272 if ( strequal( p
->domain
, domain
) )
276 /* If we didn't find a match, then use the default nss backend */
279 if (!default_backend
) {
283 status
= nss_domain_list_add_domain(domain
, default_backend
);
284 if (!NT_STATUS_IS_OK(status
)) {
290 * Here, we use the fact that the new domain was added at
291 * the beginning of the list...
296 if ( !NT_STATUS_IS_OK( p
->init_status
) ) {
297 p
->init_status
= p
->backend
->methods
->init( p
);
303 /********************************************************************
304 *******************************************************************/
306 NTSTATUS
nss_get_info( const char *domain
, const struct dom_sid
*user_sid
,
308 ADS_STRUCT
*ads
, LDAPMessage
*msg
,
309 const char **homedir
, const char **shell
,
310 const char **gecos
, gid_t
*p_gid
)
312 struct nss_domain_entry
*p
;
313 struct nss_info_methods
*m
;
315 DEBUG(10, ("nss_get_info called for sid [%s] in domain '%s'\n",
316 sid_string_dbg(user_sid
), domain
?domain
:"NULL"));
318 if ( (p
= find_nss_domain( domain
)) == NULL
) {
319 DEBUG(4,("nss_get_info: Failed to find nss domain pointer for %s\n",
321 return NT_STATUS_NOT_FOUND
;
324 m
= p
->backend
->methods
;
326 return m
->get_nss_info( p
, user_sid
, ctx
, ads
, msg
,
327 homedir
, shell
, gecos
, p_gid
);
330 /********************************************************************
331 *******************************************************************/
333 NTSTATUS
nss_map_to_alias( TALLOC_CTX
*mem_ctx
, const char *domain
,
334 const char *name
, char **alias
)
336 struct nss_domain_entry
*p
;
337 struct nss_info_methods
*m
;
339 if ( (p
= find_nss_domain( domain
)) == NULL
) {
340 DEBUG(4,("nss_map_to_alias: Failed to find nss domain pointer for %s\n",
342 return NT_STATUS_NOT_FOUND
;
345 m
= p
->backend
->methods
;
347 return m
->map_to_alias(mem_ctx
, p
, name
, alias
);
351 /********************************************************************
352 *******************************************************************/
354 NTSTATUS
nss_map_from_alias( TALLOC_CTX
*mem_ctx
, const char *domain
,
355 const char *alias
, char **name
)
357 struct nss_domain_entry
*p
;
358 struct nss_info_methods
*m
;
360 if ( (p
= find_nss_domain( domain
)) == NULL
) {
361 DEBUG(4,("nss_map_from_alias: Failed to find nss domain pointer for %s\n",
363 return NT_STATUS_NOT_FOUND
;
366 m
= p
->backend
->methods
;
368 return m
->map_from_alias( mem_ctx
, p
, alias
, name
);
371 /********************************************************************
372 *******************************************************************/
374 NTSTATUS
nss_close( const char *parameters
)
376 struct nss_domain_entry
*p
= nss_domain_list
;
377 struct nss_domain_entry
*q
;
379 while ( p
&& p
->backend
&& p
->backend
->methods
) {
380 /* close the backend */
381 p
->backend
->methods
->close_fn();
383 /* free the memory */