s3:torture:delete: untangle function call from result check
[Samba/gebeck_regimport.git] / source3 / winbindd / idmap_util.c
blob8e9d468dc5875a4a9be5201199159945db59dcc1
1 /*
2 Unix SMB/CIFS implementation.
3 ID Mapping
4 Copyright (C) Simo Sorce 2003
5 Copyright (C) Jeremy Allison 2006
6 Copyright (C) Michael Adam 2010
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program 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
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.*/
21 #include "includes.h"
22 #include "winbindd.h"
23 #include "winbindd_proto.h"
24 #include "idmap.h"
25 #include "idmap_cache.h"
26 #include "../libcli/security/security.h"
28 #undef DBGC_CLASS
29 #define DBGC_CLASS DBGC_IDMAP
31 /*****************************************************************
32 Returns the SID mapped to the given UID.
33 If mapping is not possible returns an error.
34 *****************************************************************/
36 NTSTATUS idmap_uid_to_sid(const char *domname, struct dom_sid *sid, uid_t uid)
38 NTSTATUS ret;
39 struct id_map map;
40 bool expired;
42 DEBUG(10,("idmap_uid_to_sid: uid = [%lu], domain = '%s'\n",
43 (unsigned long)uid, domname?domname:"NULL"));
45 if (winbindd_use_idmap_cache()
46 && idmap_cache_find_uid2sid(uid, sid, &expired)) {
47 DEBUG(10, ("idmap_cache_find_uid2sid found %u%s\n",
48 (unsigned int)uid,
49 expired ? " (expired)": ""));
50 if (expired && idmap_is_online()) {
51 DEBUG(10, ("revalidating expired entry\n"));
52 goto backend;
54 if (is_null_sid(sid)) {
55 DEBUG(10, ("Returning negative cache entry\n"));
56 return NT_STATUS_NONE_MAPPED;
58 DEBUG(10, ("Returning positive cache entry\n"));
59 return NT_STATUS_OK;
62 backend:
63 ZERO_STRUCT(map);
64 map.sid = sid;
65 map.xid.type = ID_TYPE_UID;
66 map.xid.id = uid;
68 ret = idmap_backends_unixid_to_sid(domname, &map);
69 if ( ! NT_STATUS_IS_OK(ret)) {
70 DEBUG(10, ("error mapping uid [%lu]\n", (unsigned long)uid));
71 return ret;
74 if (map.status != ID_MAPPED) {
75 if (winbindd_use_idmap_cache()) {
76 struct dom_sid null_sid;
77 struct unixid id;
78 id.type = ID_TYPE_UID;
79 id.id = uid;
80 ZERO_STRUCT(null_sid);
81 idmap_cache_set_sid2unixid(&null_sid, &id);
83 DEBUG(10, ("uid [%lu] not mapped\n", (unsigned long)uid));
84 return NT_STATUS_NONE_MAPPED;
87 if (winbindd_use_idmap_cache()) {
88 idmap_cache_set_sid2unixid(sid, &map.xid);
91 return NT_STATUS_OK;
94 /*****************************************************************
95 Returns SID mapped to the given GID.
96 If mapping is not possible returns an error.
97 *****************************************************************/
99 NTSTATUS idmap_gid_to_sid(const char *domname, struct dom_sid *sid, gid_t gid)
101 NTSTATUS ret;
102 struct id_map map;
103 bool expired;
105 DEBUG(10,("idmap_gid_to_sid: gid = [%lu], domain = '%s'\n",
106 (unsigned long)gid, domname?domname:"NULL"));
108 if (winbindd_use_idmap_cache()
109 && idmap_cache_find_gid2sid(gid, sid, &expired)) {
110 DEBUG(10, ("idmap_cache_find_gid2sid found %u%s\n",
111 (unsigned int)gid,
112 expired ? " (expired)": ""));
113 if (expired && idmap_is_online()) {
114 DEBUG(10, ("revalidating expired entry\n"));
115 goto backend;
117 if (is_null_sid(sid)) {
118 DEBUG(10, ("Returning negative cache entry\n"));
119 return NT_STATUS_NONE_MAPPED;
121 DEBUG(10, ("Returning positive cache entry\n"));
122 return NT_STATUS_OK;
125 backend:
126 ZERO_STRUCT(map);
127 map.sid = sid;
128 map.xid.type = ID_TYPE_GID;
129 map.xid.id = gid;
131 ret = idmap_backends_unixid_to_sid(domname, &map);
132 if ( ! NT_STATUS_IS_OK(ret)) {
133 DEBUG(10, ("error mapping gid [%lu]\n", (unsigned long)gid));
134 return ret;
137 if (map.status != ID_MAPPED) {
138 if (winbindd_use_idmap_cache()) {
139 struct dom_sid null_sid;
140 struct unixid id;
141 id.type = ID_TYPE_GID;
142 id.id = gid;
143 ZERO_STRUCT(null_sid);
144 idmap_cache_set_sid2unixid(&null_sid, &id);
146 DEBUG(10, ("gid [%lu] not mapped\n", (unsigned long)gid));
147 return NT_STATUS_NONE_MAPPED;
150 if (winbindd_use_idmap_cache()) {
151 idmap_cache_set_sid2unixid(sid, &map.xid);
154 return NT_STATUS_OK;
157 /*****************************************************************
158 Returns the UID mapped to the given SID.
159 If mapping is not possible or SID maps to a GID returns an error.
160 *****************************************************************/
162 NTSTATUS idmap_sid_to_uid(const char *dom_name, struct dom_sid *sid, uid_t *uid)
164 NTSTATUS ret;
165 struct id_map map;
166 bool expired;
168 DEBUG(10,("idmap_sid_to_uid: sid = [%s], domain = '%s'\n",
169 sid_string_dbg(sid), dom_name));
171 if (winbindd_use_idmap_cache()
172 && idmap_cache_find_sid2uid(sid, uid, &expired)) {
173 DEBUG(10, ("idmap_cache_find_sid2uid found %d%s\n",
174 (int)(*uid), expired ? " (expired)": ""));
175 if (expired && idmap_is_online()) {
176 DEBUG(10, ("revalidating expired entry\n"));
177 goto backend;
179 if ((*uid) == -1) {
180 DEBUG(10, ("Returning negative cache entry\n"));
181 return NT_STATUS_NONE_MAPPED;
183 DEBUG(10, ("Returning positive cache entry\n"));
184 return NT_STATUS_OK;
187 backend:
188 ZERO_STRUCT(map);
189 map.sid = sid;
190 map.xid.type = ID_TYPE_UID;
192 ret = idmap_backends_sid_to_unixid(dom_name, &map);
194 if (!NT_STATUS_IS_OK(ret)) {
195 DEBUG(10, ("idmap_backends_sid_to_unixid failed: %s\n",
196 nt_errstr(ret)));
197 if (winbindd_use_idmap_cache()) {
198 idmap_cache_set_sid2uid(sid, -1);
200 return ret;
203 if (map.status != ID_MAPPED) {
204 DEBUG(10, ("sid [%s] is not mapped\n", sid_string_dbg(sid)));
205 if (winbindd_use_idmap_cache()) {
206 idmap_cache_set_sid2uid(sid, -1);
208 return NT_STATUS_NONE_MAPPED;
211 if (map.xid.type != ID_TYPE_UID) {
212 DEBUG(10, ("sid [%s] not mapped to a uid "
213 "[%u,%u,%u]\n",
214 sid_string_dbg(sid),
215 map.status,
216 map.xid.type,
217 map.xid.id));
218 if (winbindd_use_idmap_cache()) {
219 idmap_cache_set_sid2uid(sid, -1);
221 return NT_STATUS_NONE_MAPPED;
224 *uid = (uid_t)map.xid.id;
225 if (winbindd_use_idmap_cache()) {
226 idmap_cache_set_sid2unixid(sid, &map.xid);
228 return NT_STATUS_OK;
231 /*****************************************************************
232 Returns the GID mapped to the given SID.
233 If mapping is not possible or SID maps to a UID returns an error.
234 *****************************************************************/
236 NTSTATUS idmap_sid_to_gid(const char *domname, struct dom_sid *sid, gid_t *gid)
238 NTSTATUS ret;
239 struct id_map map;
240 bool expired;
242 DEBUG(10,("idmap_sid_to_gid: sid = [%s], domain = '%s'\n",
243 sid_string_dbg(sid), domname));
245 if (winbindd_use_idmap_cache()
246 && idmap_cache_find_sid2gid(sid, gid, &expired)) {
247 DEBUG(10, ("idmap_cache_find_sid2gid found %d%s\n",
248 (int)(*gid), expired ? " (expired)": ""));
249 if (expired && idmap_is_online()) {
250 DEBUG(10, ("revalidating expired entry\n"));
251 goto backend;
253 if ((*gid) == -1) {
254 DEBUG(10, ("Returning negative cache entry\n"));
255 return NT_STATUS_NONE_MAPPED;
257 DEBUG(10, ("Returning positive cache entry\n"));
258 return NT_STATUS_OK;
261 backend:
262 ZERO_STRUCT(map);
263 map.sid = sid;
264 map.xid.type = ID_TYPE_GID;
266 ret = idmap_backends_sid_to_unixid(domname, &map);
268 if (!NT_STATUS_IS_OK(ret)) {
269 DEBUG(10, ("idmap_backends_sid_to_unixid failed: %s\n",
270 nt_errstr(ret)));
271 if (winbindd_use_idmap_cache()) {
272 idmap_cache_set_sid2gid(sid, -1);
274 return ret;
277 if (map.status != ID_MAPPED) {
278 DEBUG(10, ("sid [%s] is not mapped\n", sid_string_dbg(sid)));
279 if (winbindd_use_idmap_cache()) {
280 idmap_cache_set_sid2gid(sid, -1);
282 return NT_STATUS_NONE_MAPPED;
285 if (map.xid.type != ID_TYPE_GID) {
286 DEBUG(10, ("sid [%s] not mapped to a gid "
287 "[%u,%u,%u]\n",
288 sid_string_dbg(sid),
289 map.status,
290 map.xid.type,
291 map.xid.id));
292 if (winbindd_use_idmap_cache()) {
293 idmap_cache_set_sid2gid(sid, -1);
295 return NT_STATUS_NONE_MAPPED;
298 *gid = map.xid.id;
299 if (winbindd_use_idmap_cache()) {
300 idmap_cache_set_sid2unixid(sid, &map.xid);
302 return NT_STATUS_OK;
306 * check whether a given unix id is inside the filter range of an idmap domain
308 bool idmap_unix_id_is_in_range(uint32_t id, struct idmap_domain *dom)
310 if (id == 0) {
311 /* 0 is not an allowed unix id for id mapping */
312 return false;
315 if ((dom->low_id && (id < dom->low_id)) ||
316 (dom->high_id && (id > dom->high_id)))
318 return false;
321 return true;