2 Unix SMB/CIFS implementation.
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/>.*/
22 #include "winbindd_proto.h"
25 #define DBGC_CLASS DBGC_IDMAP
27 /*****************************************************************
28 Returns the SID mapped to the given UID.
29 If mapping is not possible returns an error.
30 *****************************************************************/
32 NTSTATUS idmap_uid_to_sid(const char *domname, DOM_SID *sid, uid_t uid)
38 DEBUG(10,("idmap_uid_to_sid: uid = [%lu], domain = '%s'\n",
39 (unsigned long)uid, domname?domname:"NULL"));
41 if (winbindd_use_idmap_cache()
42 && idmap_cache_find_uid2sid(uid, sid, &expired)) {
43 DEBUG(10, ("idmap_cache_find_uid2sid found %u%s\n",
45 expired ? " (expired)": ""));
46 if (expired && idmap_is_online()) {
47 DEBUG(10, ("revalidating expired entry\n"));
50 if (is_null_sid(sid)) {
51 DEBUG(10, ("Returning negative cache entry\n"));
52 return NT_STATUS_NONE_MAPPED;
54 DEBUG(10, ("Returning positive cache entry\n"));
60 map.xid.type = ID_TYPE_UID;
63 ret = idmap_backends_unixid_to_sid(domname, &map);
64 if ( ! NT_STATUS_IS_OK(ret)) {
65 DEBUG(10, ("error mapping uid [%lu]\n", (unsigned long)uid));
69 if (map.status != ID_MAPPED) {
70 if (winbindd_use_idmap_cache()) {
71 struct dom_sid null_sid;
72 ZERO_STRUCT(null_sid);
73 idmap_cache_set_sid2uid(&null_sid, uid);
75 DEBUG(10, ("uid [%lu] not mapped\n", (unsigned long)uid));
76 return NT_STATUS_NONE_MAPPED;
79 if (winbindd_use_idmap_cache()) {
80 idmap_cache_set_sid2uid(sid, uid);
86 /*****************************************************************
87 Returns SID mapped to the given GID.
88 If mapping is not possible returns an error.
89 *****************************************************************/
91 NTSTATUS idmap_gid_to_sid(const char *domname, DOM_SID *sid, gid_t gid)
97 DEBUG(10,("idmap_gid_to_sid: gid = [%lu], domain = '%s'\n",
98 (unsigned long)gid, domname?domname:"NULL"));
100 if (winbindd_use_idmap_cache()
101 && idmap_cache_find_gid2sid(gid, sid, &expired)) {
102 DEBUG(10, ("idmap_cache_find_gid2sid found %u%s\n",
104 expired ? " (expired)": ""));
105 if (expired && idmap_is_online()) {
106 DEBUG(10, ("revalidating expired entry\n"));
109 if (is_null_sid(sid)) {
110 DEBUG(10, ("Returning negative cache entry\n"));
111 return NT_STATUS_NONE_MAPPED;
113 DEBUG(10, ("Returning positive cache entry\n"));
119 map.xid.type = ID_TYPE_GID;
122 ret = idmap_backends_unixid_to_sid(domname, &map);
123 if ( ! NT_STATUS_IS_OK(ret)) {
124 DEBUG(10, ("error mapping gid [%lu]\n", (unsigned long)gid));
128 if (map.status != ID_MAPPED) {
129 if (winbindd_use_idmap_cache()) {
130 struct dom_sid null_sid;
131 ZERO_STRUCT(null_sid);
132 idmap_cache_set_sid2uid(&null_sid, gid);
134 DEBUG(10, ("gid [%lu] not mapped\n", (unsigned long)gid));
135 return NT_STATUS_NONE_MAPPED;
138 if (winbindd_use_idmap_cache()) {
139 idmap_cache_set_sid2gid(sid, gid);
145 /*****************************************************************
146 Returns the UID mapped to the given SID.
147 If mapping is not possible or SID maps to a GID returns an error.
148 *****************************************************************/
150 NTSTATUS idmap_sid_to_uid(const char *dom_name, DOM_SID *sid, uid_t *uid)
156 DEBUG(10,("idmap_sid_to_uid: sid = [%s], domain = '%s'\n",
157 sid_string_dbg(sid), dom_name));
159 if (winbindd_use_idmap_cache()
160 && idmap_cache_find_sid2uid(sid, uid, &expired)) {
161 DEBUG(10, ("idmap_cache_find_sid2uid found %d%s\n",
162 (int)(*uid), expired ? " (expired)": ""));
163 if (expired && idmap_is_online()) {
164 DEBUG(10, ("revalidating expired entry\n"));
168 DEBUG(10, ("Returning negative cache entry\n"));
169 return NT_STATUS_NONE_MAPPED;
171 DEBUG(10, ("Returning positive cache entry\n"));
177 map.xid.type = ID_TYPE_UID;
179 ret = idmap_backends_sid_to_unixid(dom_name, &map);
181 if (NT_STATUS_IS_OK(ret) && (map.status == ID_MAPPED)) {
182 if (map.xid.type != ID_TYPE_UID) {
183 DEBUG(10, ("sid [%s] not mapped to a uid "
189 if (winbindd_use_idmap_cache()) {
190 idmap_cache_set_sid2uid(sid, -1);
192 return NT_STATUS_NONE_MAPPED;
197 if (dom_name[0] != '\0') {
199 * We had the task to go to a specific domain which
200 * could not answer our request. Fail.
202 if (winbindd_use_idmap_cache()) {
203 idmap_cache_set_sid2uid(sid, -1);
205 return NT_STATUS_NONE_MAPPED;
208 ret = idmap_new_mapping(sid, ID_TYPE_UID, &map.xid);
210 if (!NT_STATUS_IS_OK(ret)) {
211 DEBUG(10, ("idmap_new_mapping failed: %s\n",
213 if (winbindd_use_idmap_cache()) {
214 idmap_cache_set_sid2uid(sid, -1);
220 *uid = (uid_t)map.xid.id;
221 if (winbindd_use_idmap_cache()) {
222 idmap_cache_set_sid2uid(sid, *uid);
227 /*****************************************************************
228 Returns the GID mapped to the given SID.
229 If mapping is not possible or SID maps to a UID returns an error.
230 *****************************************************************/
232 NTSTATUS idmap_sid_to_gid(const char *domname, DOM_SID *sid, gid_t *gid)
238 DEBUG(10,("idmap_sid_to_gid: sid = [%s], domain = '%s'\n",
239 sid_string_dbg(sid), domname));
241 if (winbindd_use_idmap_cache()
242 && idmap_cache_find_sid2gid(sid, gid, &expired)) {
243 DEBUG(10, ("idmap_cache_find_sid2gid found %d%s\n",
244 (int)(*gid), expired ? " (expired)": ""));
245 if (expired && idmap_is_online()) {
246 DEBUG(10, ("revalidating expired entry\n"));
250 DEBUG(10, ("Returning negative cache entry\n"));
251 return NT_STATUS_NONE_MAPPED;
253 DEBUG(10, ("Returning positive cache entry\n"));
259 map.xid.type = ID_TYPE_GID;
261 ret = idmap_backends_sid_to_unixid(domname, &map);
262 if (NT_STATUS_IS_OK(ret) && (map.status == ID_MAPPED)) {
263 if (map.xid.type != ID_TYPE_GID) {
264 DEBUG(10, ("sid [%s] not mapped to a gid "
270 if (winbindd_use_idmap_cache()) {
271 idmap_cache_set_sid2gid(sid, -1);
273 return NT_STATUS_NONE_MAPPED;
278 if (domname[0] != '\0') {
280 * We had the task to go to a specific domain which
281 * could not answer our request. Fail.
283 if (winbindd_use_idmap_cache()) {
284 idmap_cache_set_sid2uid(sid, -1);
286 return NT_STATUS_NONE_MAPPED;
289 ret = idmap_new_mapping(sid, ID_TYPE_GID, &map.xid);
291 if (!NT_STATUS_IS_OK(ret)) {
292 DEBUG(10, ("idmap_new_mapping failed: %s\n",
294 if (winbindd_use_idmap_cache()) {
295 idmap_cache_set_sid2gid(sid, -1);
302 if (winbindd_use_idmap_cache()) {
303 idmap_cache_set_sid2gid(sid, *gid);