2 * idmap_rid: static map between Active Directory/NT RIDs and RFC 2307 accounts
3 * Copyright (C) Guenther Deschner, 2004
4 * Copyright (C) Sumit Bose, 2004
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 #define DBGC_CLASS DBGC_IDMAP
27 struct idmap_rid_context {
34 /* compat params can't be used because of the completely different way we support multiple domains in the new idmap */
35 static NTSTATUS idmap_rid_initialize(struct idmap_domain *dom, const char *compat_params)
38 struct idmap_rid_context *ctx;
39 char *config_option = NULL;
42 ctx = talloc_zero(dom, struct idmap_rid_context);
44 DEBUG(0, ("Out of memory!\n"));
45 return NT_STATUS_NO_MEMORY;
48 config_option = talloc_asprintf(ctx, "idmap config %s", dom->name);
49 if ( ! config_option) {
50 DEBUG(0, ("Out of memory!\n"));
51 ret = NT_STATUS_NO_MEMORY;
55 range = lp_parm_const_string(-1, config_option, "range", NULL);
57 (sscanf(range, "%u - %u", &ctx->low_id, &ctx->high_id) != 2) ||
58 (ctx->low_id > ctx->high_id)) {
63 if (( ! ctx->low_id) || ( ! ctx->high_id)) {
64 DEBUG(1, ("ERROR: Invalid configuration, ID range missing\n"));
65 ret = NT_STATUS_UNSUCCESSFUL;
69 ctx->base_rid = lp_parm_int(-1, config_option, "base_rid", 0);
71 sid_copy(&ctx->dom_sid, dom->sid);
73 dom->private_data = ctx;
75 talloc_free(config_option);
83 static NTSTATUS idmap_rid_id_to_sid(struct idmap_rid_context *ctx, struct id_map *map)
86 enum lsa_SidType sid_type;
89 return NT_STATUS_INVALID_PARAMETER;
92 /* apply filters before checking */
93 if ((map->xid.id < ctx->low_id) || (map->xid.id > ctx->high_id)) {
94 DEBUG(5, ("Requested id (%u) out of range (%u - %u). Filtered!\n",
95 map->xid.id, ctx->low_id, ctx->high_id));
96 return NT_STATUS_NONE_MAPPED;
99 sid_compose(map->sid, &ctx->dom_sid, map->xid.id - ctx->low_id + ctx->base_rid);
101 if (winbindd_lookup_name_by_sid(ctx, map->sid, &domname, &name, &sid_type)) {
104 if (map->xid.type != ID_TYPE_UID) {
106 DEBUG(5, ("Resulting SID is of wrong ID type\n"));
107 return NT_STATUS_NONE_MAPPED;
110 case SID_NAME_DOM_GRP:
112 case SID_NAME_WKN_GRP:
113 if (map->xid.type != ID_TYPE_GID) {
115 DEBUG(5, ("Resulting SID is of wrong ID type\n"));
116 return NT_STATUS_NONE_MAPPED;
120 /* invalid sid, let's just leave it unmapped */
121 DEBUG(10, ("SID %s is UNKNOWN, skip mapping\n", sid_string_static(map->sid)));
122 return NT_STATUS_NONE_MAPPED;
125 DEBUG(2, ("Failed: to resolve SID\n"));
126 return NT_STATUS_UNSUCCESSFUL;
134 /**********************************
135 Single sid to id lookup function.
136 **********************************/
138 static NTSTATUS idmap_rid_sid_to_id(struct idmap_rid_context *ctx, struct id_map *map)
140 char *domname, *name;
141 enum lsa_SidType sid_type;
145 return NT_STATUS_INVALID_PARAMETER;
148 sid_peek_rid(map->sid, &rid);
149 map->xid.id = rid - ctx->base_rid + ctx->low_id;
151 /* check if this is a valid SID and set the type */
152 if (winbindd_lookup_name_by_sid(ctx, map->sid, &domname, &name, &sid_type)) {
155 map->xid.type = ID_TYPE_UID;
157 case SID_NAME_DOM_GRP:
159 case SID_NAME_WKN_GRP:
160 map->xid.type = ID_TYPE_GID;
163 /* invalid sid, let's just leave it unmapped */
164 DEBUG(10, ("SID %s is UNKNOWN, skip mapping\n", sid_string_static(map->sid)));
165 return NT_STATUS_NONE_MAPPED;
168 DEBUG(2, ("Failed: to resolve SID\n"));
169 return NT_STATUS_UNSUCCESSFUL;
172 /* apply filters before returning result */
173 if ((map->xid.id < ctx->low_id) || (map->xid.id > ctx->high_id)) {
174 DEBUG(5, ("Requested id (%u) out of range (%u - %u). Filtered!\n",
175 map->xid.id, ctx->low_id, ctx->high_id));
176 return NT_STATUS_NONE_MAPPED;
184 /**********************************
185 lookup a set of unix ids.
186 **********************************/
188 static NTSTATUS idmap_rid_unixids_to_sids(struct idmap_domain *dom, struct id_map **ids)
190 struct idmap_rid_context *ctx;
194 ctx = talloc_get_type(dom->private_data, struct idmap_rid_context);
196 for (i = 0; ids[i]; i++) {
197 /* make sure it is marked as unmapped before resolveing */
198 ids[i]->mapped = False;
200 ret = idmap_rid_id_to_sid(ctx, ids[i]);
202 if (( ! NT_STATUS_IS_OK(ret)) &&
203 ( ! NT_STATUS_EQUAL(ret, NT_STATUS_NONE_MAPPED))) {
204 /* some fatal error occurred, log it */
205 DEBUG(3, ("Unexpected error resolving an ID (%d)\n", ids[i]->xid.id));
212 /**********************************
213 lookup a set of sids.
214 **********************************/
216 static NTSTATUS idmap_rid_sids_to_unixids(struct idmap_domain *dom, struct id_map **ids)
218 struct idmap_rid_context *ctx;
222 ctx = talloc_get_type(dom->private_data, struct idmap_rid_context);
224 for (i = 0; ids[i]; i++) {
225 /* make sure it is marked as unmapped before resolveing */
226 ids[i]->mapped = False;
228 ret = idmap_rid_sid_to_id(ctx, ids[i]);
230 if (( ! NT_STATUS_IS_OK(ret)) &&
231 ( ! NT_STATUS_EQUAL(ret, NT_STATUS_NONE_MAPPED))) {
232 /* some fatal error occurred, log it */
233 DEBUG(3, ("Unexpected error resolving a SID (%s)\n",
234 sid_string_static(ids[i]->sid)));
241 static NTSTATUS idmap_rid_close(struct idmap_domain *dom)
243 struct idmap_tdb_context *ctx;
245 if (dom->private_data) {
246 TALLOC_FREE(dom->private_data);
251 static struct idmap_methods rid_methods = {
252 .init = idmap_rid_initialize,
253 .unixids_to_sids = idmap_rid_unixids_to_sids,
254 .sids_to_unixids = idmap_rid_sids_to_unixids,
255 .close_fn = idmap_rid_close
258 NTSTATUS idmap_rid_init(void)
260 return smb_register_idmap(SMB_IDMAP_INTERFACE_VERSION, "rid", &rid_methods);