2 Unix SMB/CIFS implementation.
4 Copyright (C) Tim Potter 2000
5 Copyright (C) Jim McDonough <jmcd@us.ibm.com> 2003
6 Copyright (C) Simo Sorce 2003-2007
7 Copyright (C) Jeremy Allison 2006
8 Copyright (C) Michael Adam 2010
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>.
27 #include "lib/util_sid_passdb.h"
31 #define DBGC_CLASS DBGC_IDMAP
36 * Pointer to the backend methods. Modules register themselves here via
40 struct idmap_backend
{
42 struct idmap_methods
*methods
;
43 struct idmap_backend
*prev
, *next
;
45 static struct idmap_backend
*backends
= NULL
;
48 * Default idmap domain configured via "idmap backend".
50 static struct idmap_domain
*default_idmap_domain
;
53 * Passdb idmap domain, not configurable. winbind must always give passdb a
56 static struct idmap_domain
*passdb_idmap_domain
;
59 * List of specially configured idmap domains. This list is filled on demand
60 * in the winbind idmap child when the parent winbind figures out via the
61 * special range parameter or via the domain SID that a special "idmap config
62 * domain" configuration is present.
64 static struct idmap_domain
**idmap_domains
= NULL
;
65 static int num_domains
= 0;
67 static struct idmap_domain
*idmap_init_named_domain(TALLOC_CTX
*mem_ctx
,
69 static struct idmap_domain
*idmap_init_domain(TALLOC_CTX
*mem_ctx
,
70 const char *domainname
,
71 const char *modulename
,
73 static bool idmap_found_domain_backend(
74 const char *string
, regmatch_t matches
[], void *private_data
);
76 static bool idmap_init(void)
78 static bool initialized
;
85 DEBUG(10, ("idmap_init(): calling static_init_idmap\n"));
91 if (!pdb_is_responsible_for_everything_else()) {
92 default_idmap_domain
= idmap_init_named_domain(NULL
, "*");
93 if (default_idmap_domain
== NULL
) {
98 passdb_idmap_domain
= idmap_init_domain(
99 NULL
, get_global_sam_name(), "passdb", false);
100 if (passdb_idmap_domain
== NULL
) {
101 TALLOC_FREE(default_idmap_domain
);
105 idmap_domains
= talloc_array(NULL
, struct idmap_domain
*, 0);
106 if (idmap_domains
== NULL
) {
107 TALLOC_FREE(passdb_idmap_domain
);
108 TALLOC_FREE(default_idmap_domain
);
112 ret
= lp_wi_scan_global_parametrics(
113 "idmapconfig\\(.*\\):backend", 2,
114 idmap_found_domain_backend
, NULL
);
116 DBG_WARNING("wi_scan_global_parametrics returned %d\n", ret
);
123 static bool idmap_found_domain_backend(
124 const char *string
, regmatch_t matches
[], void *private_data
)
126 if (matches
[1].rm_so
== -1) {
127 DBG_WARNING("Found match, but no name??\n");
132 struct idmap_domain
*dom
, **tmp
;
133 regoff_t len
= matches
[1].rm_eo
- matches
[1].rm_so
;
136 memcpy(domname
, string
+ matches
[1].rm_so
, len
);
139 DBG_DEBUG("Found idmap domain \"%s\"\n", domname
);
141 if (strcmp(domname
, "*") == 0) {
145 dom
= idmap_init_named_domain(idmap_domains
, domname
);
147 DBG_NOTICE("Could not init idmap domain %s\n",
151 tmp
= talloc_realloc(idmap_domains
, idmap_domains
,
152 struct idmap_domain
*, num_domains
+ 1);
154 DBG_WARNING("talloc_realloc failed\n");
159 idmap_domains
[num_domains
] = dom
;
166 static struct idmap_methods
*get_methods(const char *name
)
168 struct idmap_backend
*b
;
170 for (b
= backends
; b
; b
= b
->next
) {
171 if (strequal(b
->name
, name
)) {
179 bool idmap_is_offline(void)
181 return ( lp_winbind_offline_logon() &&
182 get_global_winbindd_state_offline() );
185 bool idmap_is_online(void)
187 return !idmap_is_offline();
190 /**********************************************************************
191 Allow a module to register itself as a method.
192 **********************************************************************/
194 NTSTATUS
smb_register_idmap(int version
, const char *name
,
195 struct idmap_methods
*methods
)
197 struct idmap_backend
*entry
;
199 if ((version
!= SMB_IDMAP_INTERFACE_VERSION
)) {
200 DEBUG(0, ("Failed to register idmap module.\n"
201 "The module was compiled against "
202 "SMB_IDMAP_INTERFACE_VERSION %d,\n"
203 "current SMB_IDMAP_INTERFACE_VERSION is %d.\n"
204 "Please recompile against the current version "
206 version
, SMB_IDMAP_INTERFACE_VERSION
));
207 return NT_STATUS_OBJECT_TYPE_MISMATCH
;
210 if (!name
|| !name
[0] || !methods
) {
211 DEBUG(0,("Called with NULL pointer or empty name!\n"));
212 return NT_STATUS_INVALID_PARAMETER
;
215 for (entry
= backends
; entry
!= NULL
; entry
= entry
->next
) {
216 if (strequal(entry
->name
, name
)) {
217 DEBUG(5,("Idmap module %s already registered!\n",
219 return NT_STATUS_OBJECT_NAME_COLLISION
;
223 entry
= talloc(NULL
, struct idmap_backend
);
225 DEBUG(0,("Out of memory!\n"));
227 return NT_STATUS_NO_MEMORY
;
229 entry
->name
= talloc_strdup(entry
, name
);
230 if ( ! entry
->name
) {
231 DEBUG(0,("Out of memory!\n"));
233 return NT_STATUS_NO_MEMORY
;
235 entry
->methods
= methods
;
237 DLIST_ADD(backends
, entry
);
238 DEBUG(5, ("Successfully added idmap backend '%s'\n", name
));
243 * Initialize a domain structure
244 * @param[in] mem_ctx memory context for the result
245 * @param[in] domainname which domain is this for
246 * @param[in] modulename which backend module
247 * @param[in] check_range whether range checking should be done
248 * @result The initialized structure
250 static struct idmap_domain
*idmap_init_domain(TALLOC_CTX
*mem_ctx
,
251 const char *domainname
,
252 const char *modulename
,
255 struct idmap_domain
*result
;
257 char *config_option
= NULL
;
260 unsigned high_id
= 0;
262 result
= talloc_zero(mem_ctx
, struct idmap_domain
);
263 if (result
== NULL
) {
264 DEBUG(0, ("talloc failed\n"));
268 result
->name
= talloc_strdup(result
, domainname
);
269 if (result
->name
== NULL
) {
270 DEBUG(0, ("talloc failed\n"));
275 * Check whether the requested backend module exists and
279 result
->methods
= get_methods(modulename
);
280 if (result
->methods
== NULL
) {
281 DEBUG(3, ("idmap backend %s not found\n", modulename
));
283 status
= smb_probe_module("idmap", modulename
);
284 if (!NT_STATUS_IS_OK(status
)) {
285 DEBUG(3, ("Could not probe idmap module %s\n",
290 result
->methods
= get_methods(modulename
);
292 if (result
->methods
== NULL
) {
293 DEBUG(1, ("idmap backend %s not found\n", modulename
));
298 * load ranges and read only information from the config
301 config_option
= talloc_asprintf(result
, "idmap config %s",
303 if (config_option
== NULL
) {
304 DEBUG(0, ("Out of memory!\n"));
308 result
->read_only
= lp_parm_bool(-1, config_option
, "read only", false);
309 range
= lp_parm_const_string(-1, config_option
, "range", NULL
);
311 talloc_free(config_option
);
315 DEBUG(1, ("idmap range not specified for domain %s\n",
319 } else if (sscanf(range
, "%u - %u", &low_id
, &high_id
) != 2)
321 DEBUG(1, ("invalid range '%s' specified for domain "
322 "'%s'\n", range
, result
->name
));
326 } else if (low_id
> high_id
) {
327 DEBUG(1, ("Error: invalid idmap range detected: %u - %u\n",
334 result
->low_id
= low_id
;
335 result
->high_id
= high_id
;
337 status
= result
->methods
->init(result
);
338 if (!NT_STATUS_IS_OK(status
)) {
339 DEBUG(1, ("idmap initialization returned %s\n",
352 * Initialize a named domain structure
353 * @param[in] mem_ctx memory context for the result
354 * @param[in] domname the domain name
355 * @result The default domain structure
357 * This routine looks at the "idmap config <domname>" parameters to figure out
361 static struct idmap_domain
*idmap_init_named_domain(TALLOC_CTX
*mem_ctx
,
364 struct idmap_domain
*result
= NULL
;
374 config_option
= talloc_asprintf(talloc_tos(), "idmap config %s",
376 if (config_option
== NULL
) {
377 DEBUG(0, ("talloc failed\n"));
381 backend
= lp_parm_const_string(-1, config_option
, "backend", NULL
);
382 if (backend
== NULL
) {
383 DEBUG(10, ("no idmap backend configured for domain '%s'\n",
388 result
= idmap_init_domain(mem_ctx
, domname
, backend
, true);
389 if (result
== NULL
) {
393 TALLOC_FREE(config_option
);
397 TALLOC_FREE(config_option
);
403 * Find a domain struct according to a domain name
404 * @param[in] domname Domain name to get the config for
405 * @result The default domain structure that fits
407 * This is the central routine in the winbindd-idmap child to pick the correct
408 * domain for looking up IDs. If domname is NULL or empty, we use the default
409 * domain. If it contains something, we try to use idmap_init_named_domain()
410 * to fetch the correct backend.
412 * The choice about "domname" is being made by the winbind parent, look at the
413 * "have_idmap_config" of "struct winbindd_domain" which is set in
414 * add_trusted_domain.
417 static struct idmap_domain
*idmap_find_domain(const char *domname
)
422 DEBUG(10, ("idmap_find_domain called for domain '%s'\n",
423 domname
?domname
:"NULL"));
430 if ((domname
== NULL
) || (domname
[0] == '\0')) {
431 return default_idmap_domain
;
434 for (i
=0; i
<num_domains
; i
++) {
435 if (strequal(idmap_domains
[i
]->name
, domname
)) {
436 return idmap_domains
[i
];
440 return default_idmap_domain
;
443 struct idmap_domain
*idmap_find_domain_with_sid(const char *domname
,
444 const struct dom_sid
*sid
)
453 if (sid_check_is_for_passdb(sid
)) {
454 return passdb_idmap_domain
;
457 return idmap_find_domain(domname
);
460 void idmap_close(void)
462 TALLOC_FREE(default_idmap_domain
);
463 TALLOC_FREE(passdb_idmap_domain
);
464 TALLOC_FREE(idmap_domains
);
468 /**************************************************************************
469 idmap allocator interface functions
470 **************************************************************************/
472 static NTSTATUS
idmap_allocate_unixid(struct unixid
*id
)
474 struct idmap_domain
*dom
;
477 dom
= idmap_find_domain(NULL
);
480 return NT_STATUS_UNSUCCESSFUL
;
483 if (dom
->methods
->allocate_id
== NULL
) {
484 return NT_STATUS_NOT_IMPLEMENTED
;
487 ret
= dom
->methods
->allocate_id(dom
, id
);
493 NTSTATUS
idmap_allocate_uid(struct unixid
*id
)
495 id
->type
= ID_TYPE_UID
;
496 return idmap_allocate_unixid(id
);
499 NTSTATUS
idmap_allocate_gid(struct unixid
*id
)
501 id
->type
= ID_TYPE_GID
;
502 return idmap_allocate_unixid(id
);
505 NTSTATUS
idmap_backends_unixid_to_sid(struct id_map
*id
)
507 struct idmap_domain
*dom
;
508 struct id_map
*maps
[2];
514 return NT_STATUS_NONE_MAPPED
;
517 DEBUG(10, ("idmap_backend_unixid_to_sid: xid = %d (type %d)\n",
518 id
->xid
.id
, id
->xid
.type
));
524 * Always give passdb a chance first
527 dom
= passdb_idmap_domain
;
529 && NT_STATUS_IS_OK(dom
->methods
->unixids_to_sids(dom
, maps
))
530 && id
->status
== ID_MAPPED
) {
534 dom
= default_idmap_domain
;
536 for (i
=0; i
<num_domains
; i
++) {
537 if ((id
->xid
.id
>= idmap_domains
[i
]->low_id
) &&
538 (id
->xid
.id
<= idmap_domains
[i
]->high_id
)) {
539 dom
= idmap_domains
[i
];
545 return NT_STATUS_NONE_MAPPED
;
548 return dom
->methods
->unixids_to_sids(dom
, maps
);