Fix bug #6017, - magic script does not work. based on a patch from monyo@samba.gr.jp.
[Samba/gbeck.git] / source3 / winbindd / idmap_util.c
blob9abf425f3e54284cc67c37d37c30f7108acd2a1f
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"
22 #undef DBGC_CLASS
23 #define DBGC_CLASS DBGC_IDMAP
25 /*****************************************************************
26 Returns the SID mapped to the given UID.
27 If mapping is not possible returns an error.
28 *****************************************************************/
30 NTSTATUS idmap_uid_to_sid(const char *domname, DOM_SID *sid, uid_t uid)
32 NTSTATUS ret;
33 struct id_map map;
34 bool expired;
36 DEBUG(10,("idmap_uid_to_sid: uid = [%lu], domain = '%s'\n",
37 (unsigned long)uid, domname?domname:"NULL"));
39 if (idmap_cache_find_uid2sid(uid, sid, &expired)) {
40 DEBUG(10, ("idmap_cache_find_uid2sid found %d%s\n", uid,
41 expired ? " (expired)": ""));
42 if (expired && idmap_is_online()) {
43 DEBUG(10, ("revalidating expired entry\n"));
44 goto backend;
46 if (is_null_sid(sid)) {
47 DEBUG(10, ("Returning negative cache entry\n"));
48 return NT_STATUS_NONE_MAPPED;
50 DEBUG(10, ("Returning positive cache entry\n"));
51 return NT_STATUS_OK;
54 backend:
55 map.sid = sid;
56 map.xid.type = ID_TYPE_UID;
57 map.xid.id = uid;
59 ret = idmap_backends_unixid_to_sid(domname, &map);
60 if ( ! NT_STATUS_IS_OK(ret)) {
61 DEBUG(10, ("error mapping uid [%lu]\n", (unsigned long)uid));
62 return ret;
65 if (map.status != ID_MAPPED) {
66 struct dom_sid null_sid;
67 ZERO_STRUCT(null_sid);
68 idmap_cache_set_sid2uid(&null_sid, uid);
69 DEBUG(10, ("uid [%lu] not mapped\n", (unsigned long)uid));
70 return NT_STATUS_NONE_MAPPED;
73 idmap_cache_set_sid2uid(sid, uid);
75 return NT_STATUS_OK;
78 /*****************************************************************
79 Returns SID mapped to the given GID.
80 If mapping is not possible returns an error.
81 *****************************************************************/
83 NTSTATUS idmap_gid_to_sid(const char *domname, DOM_SID *sid, gid_t gid)
85 NTSTATUS ret;
86 struct id_map map;
87 bool expired;
89 DEBUG(10,("idmap_gid_to_si: gid = [%lu], domain = '%s'\n",
90 (unsigned long)gid, domname?domname:"NULL"));
92 if (idmap_cache_find_gid2sid(gid, sid, &expired)) {
93 DEBUG(10, ("idmap_cache_find_gid2sid found %d%s\n", gid,
94 expired ? " (expired)": ""));
95 if (expired && idmap_is_online()) {
96 DEBUG(10, ("revalidating expired entry\n"));
97 goto backend;
99 if (is_null_sid(sid)) {
100 DEBUG(10, ("Returning negative cache entry\n"));
101 return NT_STATUS_NONE_MAPPED;
103 DEBUG(10, ("Returning positive cache entry\n"));
104 return NT_STATUS_OK;
107 backend:
108 map.sid = sid;
109 map.xid.type = ID_TYPE_GID;
110 map.xid.id = gid;
112 ret = idmap_backends_unixid_to_sid(domname, &map);
113 if ( ! NT_STATUS_IS_OK(ret)) {
114 DEBUG(10, ("error mapping gid [%lu]\n", (unsigned long)gid));
115 return ret;
118 if (map.status != ID_MAPPED) {
119 struct dom_sid null_sid;
120 ZERO_STRUCT(null_sid);
121 idmap_cache_set_sid2uid(&null_sid, gid);
122 DEBUG(10, ("gid [%lu] not mapped\n", (unsigned long)gid));
123 return NT_STATUS_NONE_MAPPED;
126 idmap_cache_set_sid2gid(sid, gid);
128 return NT_STATUS_OK;
131 /*****************************************************************
132 Returns the UID mapped to the given SID.
133 If mapping is not possible or SID maps to a GID returns an error.
134 *****************************************************************/
136 NTSTATUS idmap_sid_to_uid(const char *dom_name, DOM_SID *sid, uid_t *uid)
138 NTSTATUS ret;
139 struct id_map map;
140 bool expired;
142 DEBUG(10,("idmap_sid_to_uid: sid = [%s], domain = '%s'\n",
143 sid_string_dbg(sid), dom_name));
145 if (idmap_cache_find_sid2uid(sid, uid, &expired)) {
146 DEBUG(10, ("idmap_cache_find_sid2uid found %d%s\n",
147 (int)(*uid), expired ? " (expired)": ""));
148 if (expired && idmap_is_online()) {
149 DEBUG(10, ("revalidating expired entry\n"));
150 goto backend;
152 if ((*uid) == -1) {
153 DEBUG(10, ("Returning negative cache entry\n"));
154 return NT_STATUS_NONE_MAPPED;
156 DEBUG(10, ("Returning positive cache entry\n"));
157 return NT_STATUS_OK;
160 backend:
161 map.sid = sid;
162 map.xid.type = ID_TYPE_UID;
164 ret = idmap_backends_sid_to_unixid(dom_name, &map);
166 if (NT_STATUS_IS_OK(ret) && (map.status == ID_MAPPED)) {
167 if (map.xid.type != ID_TYPE_UID) {
168 DEBUG(10, ("sid [%s] not mapped to a uid "
169 "[%u,%u,%u]\n",
170 sid_string_dbg(sid),
171 map.status,
172 map.xid.type,
173 map.xid.id));
174 idmap_cache_set_sid2uid(sid, -1);
175 return NT_STATUS_NONE_MAPPED;
177 goto done;
180 if (dom_name[0] != '\0') {
182 * We had the task to go to a specific domain which
183 * could not answer our request. Fail.
185 idmap_cache_set_sid2uid(sid, -1);
186 return NT_STATUS_NONE_MAPPED;
189 ret = idmap_new_mapping(sid, ID_TYPE_UID, &map.xid);
191 if (!NT_STATUS_IS_OK(ret)) {
192 DEBUG(10, ("idmap_new_mapping failed: %s\n",
193 nt_errstr(ret)));
194 idmap_cache_set_sid2uid(sid, -1);
195 return ret;
198 done:
199 *uid = (uid_t)map.xid.id;
200 idmap_cache_set_sid2uid(sid, *uid);
201 return NT_STATUS_OK;
204 /*****************************************************************
205 Returns the GID mapped to the given SID.
206 If mapping is not possible or SID maps to a UID returns an error.
207 *****************************************************************/
209 NTSTATUS idmap_sid_to_gid(const char *domname, DOM_SID *sid, gid_t *gid)
211 NTSTATUS ret;
212 struct id_map map;
213 bool expired;
215 DEBUG(10,("idmap_sid_to_gid: sid = [%s], domain = '%s'\n",
216 sid_string_dbg(sid), domname));
218 if (idmap_cache_find_sid2gid(sid, gid, &expired)) {
219 DEBUG(10, ("idmap_cache_find_sid2gid found %d%s\n",
220 (int)(*gid), expired ? " (expired)": ""));
221 if (expired && idmap_is_online()) {
222 DEBUG(10, ("revalidating expired entry\n"));
223 goto backend;
225 if ((*gid) == -1) {
226 DEBUG(10, ("Returning negative cache entry\n"));
227 return NT_STATUS_NONE_MAPPED;
229 DEBUG(10, ("Returning positive cache entry\n"));
230 return NT_STATUS_OK;
233 backend:
234 map.sid = sid;
235 map.xid.type = ID_TYPE_GID;
237 ret = idmap_backends_sid_to_unixid(domname, &map);
238 if (NT_STATUS_IS_OK(ret) && (map.status == ID_MAPPED)) {
239 if (map.xid.type != ID_TYPE_GID) {
240 DEBUG(10, ("sid [%s] not mapped to a gid "
241 "[%u,%u,%u]\n",
242 sid_string_dbg(sid),
243 map.status,
244 map.xid.type,
245 map.xid.id));
246 idmap_cache_set_sid2gid(sid, -1);
247 return NT_STATUS_NONE_MAPPED;
249 goto done;
252 if (domname[0] != '\0') {
254 * We had the task to go to a specific domain which
255 * could not answer our request. Fail.
257 idmap_cache_set_sid2uid(sid, -1);
258 return NT_STATUS_NONE_MAPPED;
261 ret = idmap_new_mapping(sid, ID_TYPE_GID, &map.xid);
263 if (!NT_STATUS_IS_OK(ret)) {
264 DEBUG(10, ("idmap_new_mapping failed: %s\n",
265 nt_errstr(ret)));
266 idmap_cache_set_sid2gid(sid, -1);
267 return ret;
270 done:
271 *gid = map.xid.id;
272 idmap_cache_set_sid2gid(sid, *gid);
273 return NT_STATUS_OK;