s3:vfs:gpfs convert sharemodes/leases parameter
[Samba.git] / source3 / winbindd / idmap_util.c
blob7525fa76203d30bfc3a24ad7a7f8c3cf0458155d
1 /*
2 Unix SMB/CIFS implementation.
3 ID Mapping
4 Copyright (C) Simo Sorce 2003
5 Copyright (C) Jeremy Allison 2006
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.*/
20 #include "includes.h"
21 #include "winbindd.h"
22 #include "winbindd_proto.h"
23 #include "idmap.h"
24 #include "idmap_cache.h"
25 #include "../libcli/security/security.h"
27 #undef DBGC_CLASS
28 #define DBGC_CLASS DBGC_IDMAP
30 /*****************************************************************
31 Returns the SID mapped to the given UID.
32 If mapping is not possible returns an error.
33 *****************************************************************/
35 NTSTATUS idmap_uid_to_sid(const char *domname, struct dom_sid *sid, uid_t uid)
37 NTSTATUS ret;
38 struct id_map map;
39 bool expired;
41 DEBUG(10,("idmap_uid_to_sid: uid = [%lu], domain = '%s'\n",
42 (unsigned long)uid, domname?domname:"NULL"));
44 if (winbindd_use_idmap_cache()
45 && idmap_cache_find_uid2sid(uid, sid, &expired)) {
46 DEBUG(10, ("idmap_cache_find_uid2sid found %u%s\n",
47 (unsigned int)uid,
48 expired ? " (expired)": ""));
49 if (expired && idmap_is_online()) {
50 DEBUG(10, ("revalidating expired entry\n"));
51 goto backend;
53 if (is_null_sid(sid)) {
54 DEBUG(10, ("Returning negative cache entry\n"));
55 return NT_STATUS_NONE_MAPPED;
57 DEBUG(10, ("Returning positive cache entry\n"));
58 return NT_STATUS_OK;
61 backend:
62 map.sid = sid;
63 map.xid.type = ID_TYPE_UID;
64 map.xid.id = uid;
66 ret = idmap_backends_unixid_to_sid(domname, &map);
67 if ( ! NT_STATUS_IS_OK(ret)) {
68 DEBUG(10, ("error mapping uid [%lu]\n", (unsigned long)uid));
69 return ret;
72 if (map.status != ID_MAPPED) {
73 if (winbindd_use_idmap_cache()) {
74 struct dom_sid null_sid;
75 ZERO_STRUCT(null_sid);
76 idmap_cache_set_sid2uid(&null_sid, uid);
78 DEBUG(10, ("uid [%lu] not mapped\n", (unsigned long)uid));
79 return NT_STATUS_NONE_MAPPED;
82 if (winbindd_use_idmap_cache()) {
83 idmap_cache_set_sid2uid(sid, uid);
86 return NT_STATUS_OK;
89 /*****************************************************************
90 Returns SID mapped to the given GID.
91 If mapping is not possible returns an error.
92 *****************************************************************/
94 NTSTATUS idmap_gid_to_sid(const char *domname, struct dom_sid *sid, gid_t gid)
96 NTSTATUS ret;
97 struct id_map map;
98 bool expired;
100 DEBUG(10,("idmap_gid_to_sid: gid = [%lu], domain = '%s'\n",
101 (unsigned long)gid, domname?domname:"NULL"));
103 if (winbindd_use_idmap_cache()
104 && idmap_cache_find_gid2sid(gid, sid, &expired)) {
105 DEBUG(10, ("idmap_cache_find_gid2sid found %u%s\n",
106 (unsigned int)gid,
107 expired ? " (expired)": ""));
108 if (expired && idmap_is_online()) {
109 DEBUG(10, ("revalidating expired entry\n"));
110 goto backend;
112 if (is_null_sid(sid)) {
113 DEBUG(10, ("Returning negative cache entry\n"));
114 return NT_STATUS_NONE_MAPPED;
116 DEBUG(10, ("Returning positive cache entry\n"));
117 return NT_STATUS_OK;
120 backend:
121 map.sid = sid;
122 map.xid.type = ID_TYPE_GID;
123 map.xid.id = gid;
125 ret = idmap_backends_unixid_to_sid(domname, &map);
126 if ( ! NT_STATUS_IS_OK(ret)) {
127 DEBUG(10, ("error mapping gid [%lu]\n", (unsigned long)gid));
128 return ret;
131 if (map.status != ID_MAPPED) {
132 if (winbindd_use_idmap_cache()) {
133 struct dom_sid null_sid;
134 ZERO_STRUCT(null_sid);
135 idmap_cache_set_sid2uid(&null_sid, gid);
137 DEBUG(10, ("gid [%lu] not mapped\n", (unsigned long)gid));
138 return NT_STATUS_NONE_MAPPED;
141 if (winbindd_use_idmap_cache()) {
142 idmap_cache_set_sid2gid(sid, gid);
145 return NT_STATUS_OK;
148 /*****************************************************************
149 Returns the UID mapped to the given SID.
150 If mapping is not possible or SID maps to a GID returns an error.
151 *****************************************************************/
153 NTSTATUS idmap_sid_to_uid(const char *dom_name, struct dom_sid *sid, uid_t *uid)
155 NTSTATUS ret;
156 struct id_map map;
157 bool expired;
159 DEBUG(10,("idmap_sid_to_uid: sid = [%s], domain = '%s'\n",
160 sid_string_dbg(sid), dom_name));
162 if (winbindd_use_idmap_cache()
163 && idmap_cache_find_sid2uid(sid, uid, &expired)) {
164 DEBUG(10, ("idmap_cache_find_sid2uid found %d%s\n",
165 (int)(*uid), expired ? " (expired)": ""));
166 if (expired && idmap_is_online()) {
167 DEBUG(10, ("revalidating expired entry\n"));
168 goto backend;
170 if ((*uid) == -1) {
171 DEBUG(10, ("Returning negative cache entry\n"));
172 return NT_STATUS_NONE_MAPPED;
174 DEBUG(10, ("Returning positive cache entry\n"));
175 return NT_STATUS_OK;
178 backend:
179 map.sid = sid;
180 map.xid.type = ID_TYPE_UID;
182 ret = idmap_backends_sid_to_unixid(dom_name, &map);
184 if (!NT_STATUS_IS_OK(ret)) {
185 DEBUG(10, ("idmap_backends_sid_to_unixid failed: %s\n",
186 nt_errstr(ret)));
187 if (winbindd_use_idmap_cache()) {
188 idmap_cache_set_sid2uid(sid, -1);
190 return ret;
193 if (map.status != ID_MAPPED) {
194 DEBUG(10, ("sid [%s] is not mapped\n", sid_string_dbg(sid)));
195 if (winbindd_use_idmap_cache()) {
196 idmap_cache_set_sid2uid(sid, -1);
198 return NT_STATUS_NONE_MAPPED;
201 if (map.xid.type != ID_TYPE_UID) {
202 DEBUG(10, ("sid [%s] not mapped to a uid "
203 "[%u,%u,%u]\n",
204 sid_string_dbg(sid),
205 map.status,
206 map.xid.type,
207 map.xid.id));
208 if (winbindd_use_idmap_cache()) {
209 idmap_cache_set_sid2uid(sid, -1);
211 return NT_STATUS_NONE_MAPPED;
214 *uid = (uid_t)map.xid.id;
215 if (winbindd_use_idmap_cache()) {
216 idmap_cache_set_sid2uid(sid, *uid);
218 return NT_STATUS_OK;
221 /*****************************************************************
222 Returns the GID mapped to the given SID.
223 If mapping is not possible or SID maps to a UID returns an error.
224 *****************************************************************/
226 NTSTATUS idmap_sid_to_gid(const char *domname, struct dom_sid *sid, gid_t *gid)
228 NTSTATUS ret;
229 struct id_map map;
230 bool expired;
232 DEBUG(10,("idmap_sid_to_gid: sid = [%s], domain = '%s'\n",
233 sid_string_dbg(sid), domname));
235 if (winbindd_use_idmap_cache()
236 && idmap_cache_find_sid2gid(sid, gid, &expired)) {
237 DEBUG(10, ("idmap_cache_find_sid2gid found %d%s\n",
238 (int)(*gid), expired ? " (expired)": ""));
239 if (expired && idmap_is_online()) {
240 DEBUG(10, ("revalidating expired entry\n"));
241 goto backend;
243 if ((*gid) == -1) {
244 DEBUG(10, ("Returning negative cache entry\n"));
245 return NT_STATUS_NONE_MAPPED;
247 DEBUG(10, ("Returning positive cache entry\n"));
248 return NT_STATUS_OK;
251 backend:
252 map.sid = sid;
253 map.xid.type = ID_TYPE_GID;
255 ret = idmap_backends_sid_to_unixid(domname, &map);
257 if (!NT_STATUS_IS_OK(ret)) {
258 DEBUG(10, ("idmap_backends_sid_to_unixid failed: %s\n",
259 nt_errstr(ret)));
260 if (winbindd_use_idmap_cache()) {
261 idmap_cache_set_sid2uid(sid, -1);
263 return ret;
266 if (map.status != ID_MAPPED) {
267 DEBUG(10, ("sid [%s] is not mapped\n", sid_string_dbg(sid)));
268 if (winbindd_use_idmap_cache()) {
269 idmap_cache_set_sid2uid(sid, -1);
271 return NT_STATUS_NONE_MAPPED;
274 if (map.xid.type != ID_TYPE_GID) {
275 DEBUG(10, ("sid [%s] not mapped to a gid "
276 "[%u,%u,%u]\n",
277 sid_string_dbg(sid),
278 map.status,
279 map.xid.type,
280 map.xid.id));
281 if (winbindd_use_idmap_cache()) {
282 idmap_cache_set_sid2gid(sid, -1);
284 return NT_STATUS_NONE_MAPPED;
287 *gid = map.xid.id;
288 if (winbindd_use_idmap_cache()) {
289 idmap_cache_set_sid2gid(sid, *gid);
291 return NT_STATUS_OK;
295 * check whether a given unix id is inside the filter range of an idmap domain
297 bool idmap_unix_id_is_in_range(uint32_t id, struct idmap_domain *dom)
299 if (id == 0) {
300 /* 0 is not an allowed unix id for id mapping */
301 return false;
304 if ((dom->low_id && (id < dom->low_id)) ||
305 (dom->high_id && (id > dom->high_id)))
307 return false;
310 return true;