s4-auth Move libcli/security/session.c to the top level
[Samba.git] / source3 / winbindd / idmap_rid.c
blobae54475dfd04b5955f863a3e6ddf2fed862e217b
1 /*
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 3 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, see <http://www.gnu.org/licenses/>.
21 #include "includes.h"
22 #include "winbindd.h"
23 #include "idmap.h"
24 #include "../libcli/security/dom_sid.h"
26 #undef DBGC_CLASS
27 #define DBGC_CLASS DBGC_IDMAP
29 struct idmap_rid_context {
30 uint32_t base_rid;
33 /******************************************************************************
34 compat params can't be used because of the completely different way
35 we support multiple domains in the new idmap
36 *****************************************************************************/
38 static NTSTATUS idmap_rid_initialize(struct idmap_domain *dom,
39 const char *params)
41 NTSTATUS ret;
42 struct idmap_rid_context *ctx;
43 char *config_option = NULL;
45 ctx = TALLOC_ZERO_P(dom, struct idmap_rid_context);
46 if (ctx == NULL) {
47 DEBUG(0, ("Out of memory!\n"));
48 return NT_STATUS_NO_MEMORY;
51 config_option = talloc_asprintf(ctx, "idmap config %s", dom->name);
52 if ( ! config_option) {
53 DEBUG(0, ("Out of memory!\n"));
54 ret = NT_STATUS_NO_MEMORY;
55 goto failed;
58 ctx->base_rid = lp_parm_int(-1, config_option, "base_rid", 0);
60 dom->private_data = ctx;
62 talloc_free(config_option);
63 return NT_STATUS_OK;
65 failed:
66 talloc_free(ctx);
67 return ret;
70 static NTSTATUS idmap_rid_id_to_sid(struct idmap_domain *dom, struct id_map *map)
72 struct winbindd_domain *domain;
73 struct idmap_rid_context *ctx;
75 ctx = talloc_get_type(dom->private_data, struct idmap_rid_context);
77 /* apply filters before checking */
78 if (!idmap_unix_id_is_in_range(map->xid.id, dom)) {
79 DEBUG(5, ("Requested id (%u) out of range (%u - %u). Filtered!\n",
80 map->xid.id, dom->low_id, dom->high_id));
81 return NT_STATUS_NONE_MAPPED;
84 domain = find_domain_from_name_noinit(dom->name);
85 if (domain == NULL ) {
86 return NT_STATUS_NO_SUCH_DOMAIN;
89 sid_compose(map->sid, &domain->sid, map->xid.id - dom->low_id + ctx->base_rid);
91 /* We **really** should have some way of validating
92 the SID exists and is the correct type here. But
93 that is a deficiency in the idmap_rid design. */
95 map->status = ID_MAPPED;
97 return NT_STATUS_OK;
100 /**********************************
101 Single sid to id lookup function.
102 **********************************/
104 static NTSTATUS idmap_rid_sid_to_id(struct idmap_domain *dom, struct id_map *map)
106 uint32_t rid;
107 struct idmap_rid_context *ctx;
109 ctx = talloc_get_type(dom->private_data, struct idmap_rid_context);
111 sid_peek_rid(map->sid, &rid);
112 map->xid.id = rid - ctx->base_rid + dom->low_id;
114 /* apply filters before returning result */
116 if (!idmap_unix_id_is_in_range(map->xid.id, dom)) {
117 DEBUG(5, ("Requested id (%u) out of range (%u - %u). Filtered!\n",
118 map->xid.id, dom->low_id, dom->high_id));
119 map->status = ID_UNMAPPED;
120 return NT_STATUS_NONE_MAPPED;
123 map->status = ID_MAPPED;
125 return NT_STATUS_OK;
128 /**********************************
129 lookup a set of unix ids.
130 **********************************/
132 static NTSTATUS idmap_rid_unixids_to_sids(struct idmap_domain *dom, struct id_map **ids)
134 NTSTATUS ret;
135 int i;
137 /* initialize the status to avoid suprise */
138 for (i = 0; ids[i]; i++) {
139 ids[i]->status = ID_UNKNOWN;
142 for (i = 0; ids[i]; i++) {
144 ret = idmap_rid_id_to_sid(dom, ids[i]);
146 if (( ! NT_STATUS_IS_OK(ret)) &&
147 ( ! NT_STATUS_EQUAL(ret, NT_STATUS_NONE_MAPPED))) {
148 /* some fatal error occurred, log it */
149 DEBUG(3, ("Unexpected error resolving an ID (%d)\n", ids[i]->xid.id));
153 return NT_STATUS_OK;
156 /**********************************
157 lookup a set of sids.
158 **********************************/
160 static NTSTATUS idmap_rid_sids_to_unixids(struct idmap_domain *dom, struct id_map **ids)
162 NTSTATUS ret;
163 int i;
165 /* initialize the status to avoid suprise */
166 for (i = 0; ids[i]; i++) {
167 ids[i]->status = ID_UNKNOWN;
170 for (i = 0; ids[i]; i++) {
172 ret = idmap_rid_sid_to_id(dom, ids[i]);
174 if (( ! NT_STATUS_IS_OK(ret)) &&
175 ( ! NT_STATUS_EQUAL(ret, NT_STATUS_NONE_MAPPED))) {
176 /* some fatal error occurred, log it */
177 DEBUG(3, ("Unexpected error resolving a SID (%s)\n",
178 sid_string_dbg(ids[i]->sid)));
182 return NT_STATUS_OK;
185 static NTSTATUS idmap_rid_close(struct idmap_domain *dom)
187 if (dom->private_data) {
188 TALLOC_FREE(dom->private_data);
190 return NT_STATUS_OK;
193 static struct idmap_methods rid_methods = {
194 .init = idmap_rid_initialize,
195 .unixids_to_sids = idmap_rid_unixids_to_sids,
196 .sids_to_unixids = idmap_rid_sids_to_unixids,
197 .close_fn = idmap_rid_close
200 NTSTATUS idmap_rid_init(void)
202 return smb_register_idmap(SMB_IDMAP_INTERFACE_VERSION, "rid", &rid_methods);