s3:idmap_rid: remove a comment that does not apply in that place.
[Samba.git] / source3 / winbindd / idmap_rid.c
blob6e948025d2897db2e4c7319973affd3868a5f5a4
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"
24 #undef DBGC_CLASS
25 #define DBGC_CLASS DBGC_IDMAP
27 struct idmap_rid_context {
28 uint32_t base_rid;
31 /******************************************************************************
32 compat params can't be used because of the completely different way
33 we support multiple domains in the new idmap
34 *****************************************************************************/
36 static NTSTATUS idmap_rid_initialize(struct idmap_domain *dom,
37 const char *params)
39 NTSTATUS ret;
40 struct idmap_rid_context *ctx;
41 char *config_option = NULL;
43 ctx = TALLOC_ZERO_P(dom, struct idmap_rid_context);
44 if (ctx == NULL) {
45 DEBUG(0, ("Out of memory!\n"));
46 return NT_STATUS_NO_MEMORY;
49 config_option = talloc_asprintf(ctx, "idmap config %s", dom->name);
50 if ( ! config_option) {
51 DEBUG(0, ("Out of memory!\n"));
52 ret = NT_STATUS_NO_MEMORY;
53 goto failed;
56 ctx->base_rid = lp_parm_int(-1, config_option, "base_rid", 0);
58 dom->private_data = ctx;
60 talloc_free(config_option);
61 return NT_STATUS_OK;
63 failed:
64 talloc_free(ctx);
65 return ret;
68 static NTSTATUS idmap_rid_id_to_sid(struct idmap_domain *dom, struct id_map *map)
70 struct winbindd_domain *domain;
71 struct idmap_rid_context *ctx;
73 ctx = talloc_get_type(dom->private_data, struct idmap_rid_context);
75 /* apply filters before checking */
76 if (!idmap_unix_id_is_in_range(map->xid.id, dom)) {
77 DEBUG(5, ("Requested id (%u) out of range (%u - %u). Filtered!\n",
78 map->xid.id, dom->low_id, dom->high_id));
79 return NT_STATUS_NONE_MAPPED;
82 domain = find_domain_from_name_noinit(dom->name);
83 if (domain == NULL ) {
84 return NT_STATUS_NO_SUCH_DOMAIN;
87 sid_compose(map->sid, &domain->sid, map->xid.id - dom->low_id + ctx->base_rid);
89 /* We **really** should have some way of validating
90 the SID exists and is the correct type here. But
91 that is a deficiency in the idmap_rid design. */
93 map->status = ID_MAPPED;
95 return NT_STATUS_OK;
98 /**********************************
99 Single sid to id lookup function.
100 **********************************/
102 static NTSTATUS idmap_rid_sid_to_id(struct idmap_domain *dom, struct id_map *map)
104 uint32_t rid;
105 struct idmap_rid_context *ctx;
107 ctx = talloc_get_type(dom->private_data, struct idmap_rid_context);
109 sid_peek_rid(map->sid, &rid);
110 map->xid.id = rid - ctx->base_rid + dom->low_id;
112 /* apply filters before returning result */
114 if (!idmap_unix_id_is_in_range(map->xid.id, dom)) {
115 DEBUG(5, ("Requested id (%u) out of range (%u - %u). Filtered!\n",
116 map->xid.id, dom->low_id, dom->high_id));
117 map->status = ID_UNMAPPED;
118 return NT_STATUS_NONE_MAPPED;
121 map->status = ID_MAPPED;
123 return NT_STATUS_OK;
126 /**********************************
127 lookup a set of unix ids.
128 **********************************/
130 static NTSTATUS idmap_rid_unixids_to_sids(struct idmap_domain *dom, struct id_map **ids)
132 NTSTATUS ret;
133 int i;
135 /* initialize the status to avoid suprise */
136 for (i = 0; ids[i]; i++) {
137 ids[i]->status = ID_UNKNOWN;
140 for (i = 0; ids[i]; i++) {
142 ret = idmap_rid_id_to_sid(dom, ids[i]);
144 if (( ! NT_STATUS_IS_OK(ret)) &&
145 ( ! NT_STATUS_EQUAL(ret, NT_STATUS_NONE_MAPPED))) {
146 /* some fatal error occurred, log it */
147 DEBUG(3, ("Unexpected error resolving an ID (%d)\n", ids[i]->xid.id));
151 return NT_STATUS_OK;
154 /**********************************
155 lookup a set of sids.
156 **********************************/
158 static NTSTATUS idmap_rid_sids_to_unixids(struct idmap_domain *dom, struct id_map **ids)
160 NTSTATUS ret;
161 int i;
163 /* initialize the status to avoid suprise */
164 for (i = 0; ids[i]; i++) {
165 ids[i]->status = ID_UNKNOWN;
168 for (i = 0; ids[i]; i++) {
170 ret = idmap_rid_sid_to_id(dom, ids[i]);
172 if (( ! NT_STATUS_IS_OK(ret)) &&
173 ( ! NT_STATUS_EQUAL(ret, NT_STATUS_NONE_MAPPED))) {
174 /* some fatal error occurred, log it */
175 DEBUG(3, ("Unexpected error resolving a SID (%s)\n",
176 sid_string_dbg(ids[i]->sid)));
180 return NT_STATUS_OK;
183 static NTSTATUS idmap_rid_close(struct idmap_domain *dom)
185 if (dom->private_data) {
186 TALLOC_FREE(dom->private_data);
188 return NT_STATUS_OK;
191 static struct idmap_methods rid_methods = {
192 .init = idmap_rid_initialize,
193 .unixids_to_sids = idmap_rid_unixids_to_sids,
194 .sids_to_unixids = idmap_rid_sids_to_unixids,
195 .close_fn = idmap_rid_close
198 NTSTATUS idmap_rid_init(void)
200 return smb_register_idmap(SMB_IDMAP_INTERFACE_VERSION, "rid", &rid_methods);