libcli/security Provide a common, top level libcli/security/security.h
[bbaumbach/samba-autobuild/.git] / source3 / winbindd / idmap_util.c
1 /* 
2    Unix SMB/CIFS implementation.
3    ID Mapping
4    Copyright (C) Simo Sorce 2003
5    Copyright (C) Jeremy Allison 2006
6
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.
11
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.
16
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/>.*/
19
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"
26
27 #undef DBGC_CLASS
28 #define DBGC_CLASS DBGC_IDMAP
29
30 /*****************************************************************
31  Returns the SID mapped to the given UID.
32  If mapping is not possible returns an error.
33 *****************************************************************/  
34
35 NTSTATUS idmap_uid_to_sid(const char *domname, struct dom_sid *sid, uid_t uid)
36 {
37         NTSTATUS ret;
38         struct id_map map;
39         bool expired;
40
41         DEBUG(10,("idmap_uid_to_sid: uid = [%lu], domain = '%s'\n",
42                   (unsigned long)uid, domname?domname:"NULL"));
43
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;
52                 }
53                 if (is_null_sid(sid)) {
54                         DEBUG(10, ("Returning negative cache entry\n"));
55                         return NT_STATUS_NONE_MAPPED;
56                 }
57                 DEBUG(10, ("Returning positive cache entry\n"));
58                 return NT_STATUS_OK;
59         }
60
61 backend:
62         map.sid = sid;
63         map.xid.type = ID_TYPE_UID;
64         map.xid.id = uid;
65
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;
70         }
71
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);
77                 }
78                 DEBUG(10, ("uid [%lu] not mapped\n", (unsigned long)uid));
79                 return NT_STATUS_NONE_MAPPED;
80         }
81
82         if (winbindd_use_idmap_cache()) {
83                 idmap_cache_set_sid2uid(sid, uid);
84         }
85
86         return NT_STATUS_OK;
87 }
88
89 /*****************************************************************
90  Returns SID mapped to the given GID.
91  If mapping is not possible returns an error.
92 *****************************************************************/  
93
94 NTSTATUS idmap_gid_to_sid(const char *domname, struct dom_sid *sid, gid_t gid)
95 {
96         NTSTATUS ret;
97         struct id_map map;
98         bool expired;
99
100         DEBUG(10,("idmap_gid_to_sid: gid = [%lu], domain = '%s'\n",
101                   (unsigned long)gid, domname?domname:"NULL"));
102
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;
111                 }
112                 if (is_null_sid(sid)) {
113                         DEBUG(10, ("Returning negative cache entry\n"));
114                         return NT_STATUS_NONE_MAPPED;
115                 }
116                 DEBUG(10, ("Returning positive cache entry\n"));
117                 return NT_STATUS_OK;
118         }
119
120 backend:
121         map.sid = sid;
122         map.xid.type = ID_TYPE_GID;
123         map.xid.id = gid;
124
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;
129         }
130
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);
136                 }
137                 DEBUG(10, ("gid [%lu] not mapped\n", (unsigned long)gid));
138                 return NT_STATUS_NONE_MAPPED;
139         }
140
141         if (winbindd_use_idmap_cache()) {
142                 idmap_cache_set_sid2gid(sid, gid);
143         }
144
145         return NT_STATUS_OK;
146 }
147
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 *****************************************************************/  
152
153 NTSTATUS idmap_sid_to_uid(const char *dom_name, struct dom_sid *sid, uid_t *uid)
154 {
155         NTSTATUS ret;
156         struct id_map map;
157         bool expired;
158
159         DEBUG(10,("idmap_sid_to_uid: sid = [%s], domain = '%s'\n",
160                   sid_string_dbg(sid), dom_name));
161
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;
169                 }
170                 if ((*uid) == -1) {
171                         DEBUG(10, ("Returning negative cache entry\n"));
172                         return NT_STATUS_NONE_MAPPED;
173                 }
174                 DEBUG(10, ("Returning positive cache entry\n"));
175                 return NT_STATUS_OK;
176         }
177
178 backend:
179         map.sid = sid;
180         map.xid.type = ID_TYPE_UID;     
181
182         ret = idmap_backends_sid_to_unixid(dom_name, &map);
183
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);
189                 }
190                 return ret;
191         }
192
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);
197                 }
198                 return NT_STATUS_NONE_MAPPED;
199         }
200
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);
210                 }
211                 return NT_STATUS_NONE_MAPPED;
212         }
213
214         *uid = (uid_t)map.xid.id;
215         if (winbindd_use_idmap_cache()) {
216                 idmap_cache_set_sid2uid(sid, *uid);
217         }
218         return NT_STATUS_OK;
219 }
220
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 *****************************************************************/  
225
226 NTSTATUS idmap_sid_to_gid(const char *domname, struct dom_sid *sid, gid_t *gid)
227 {
228         NTSTATUS ret;
229         struct id_map map;
230         bool expired;
231
232         DEBUG(10,("idmap_sid_to_gid: sid = [%s], domain = '%s'\n",
233                   sid_string_dbg(sid), domname));
234
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;
242                 }
243                 if ((*gid) == -1) {
244                         DEBUG(10, ("Returning negative cache entry\n"));
245                         return NT_STATUS_NONE_MAPPED;
246                 }
247                 DEBUG(10, ("Returning positive cache entry\n"));
248                 return NT_STATUS_OK;
249         }
250
251 backend:
252         map.sid = sid;
253         map.xid.type = ID_TYPE_GID;
254
255         ret = idmap_backends_sid_to_unixid(domname, &map);
256
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);
262                 }
263                 return ret;
264         }
265
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);
270                 }
271                 return NT_STATUS_NONE_MAPPED;
272         }
273
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);
283                 }
284                 return NT_STATUS_NONE_MAPPED;
285         }
286
287         *gid = map.xid.id;
288         if (winbindd_use_idmap_cache()) {
289                 idmap_cache_set_sid2gid(sid, *gid);
290         }
291         return NT_STATUS_OK;
292 }
293
294 /**
295  * check whether a given unix id is inside the filter range of an idmap domain
296  */
297 bool idmap_unix_id_is_in_range(uint32_t id, struct idmap_domain *dom)
298 {
299         if (id == 0) {
300                 /* 0 is not an allowed unix id for id mapping */
301                 return false;
302         }
303
304         if ((dom->low_id && (id < dom->low_id)) ||
305             (dom->high_id && (id > dom->high_id)))
306         {
307                 return false;
308         }
309
310         return true;
311 }