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/>.*/
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
)
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"));
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"));
56 map
.xid
.type
= ID_TYPE_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
));
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
);
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
)
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"));
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"));
109 map
.xid
.type
= ID_TYPE_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
));
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
);
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
)
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"));
153 DEBUG(10, ("Returning negative cache entry\n"));
154 return NT_STATUS_NONE_MAPPED
;
156 DEBUG(10, ("Returning positive cache entry\n"));
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 "
174 idmap_cache_set_sid2uid(sid
, -1);
175 return NT_STATUS_NONE_MAPPED
;
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",
194 idmap_cache_set_sid2uid(sid
, -1);
199 *uid
= (uid_t
)map
.xid
.id
;
200 idmap_cache_set_sid2uid(sid
, *uid
);
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
)
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"));
226 DEBUG(10, ("Returning negative cache entry\n"));
227 return NT_STATUS_NONE_MAPPED
;
229 DEBUG(10, ("Returning positive cache entry\n"));
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 "
246 idmap_cache_set_sid2gid(sid
, -1);
247 return NT_STATUS_NONE_MAPPED
;
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",
266 idmap_cache_set_sid2gid(sid
, -1);
272 idmap_cache_set_sid2gid(sid
, *gid
);