Merge branch 'master' of ssh://git.samba.org/data/git/samba into wspp-schema
[ira/wip.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
24 #undef DBGC_CLASS
25 #define DBGC_CLASS DBGC_IDMAP
26
27 /*****************************************************************
28  Returns the SID mapped to the given UID.
29  If mapping is not possible returns an error.
30 *****************************************************************/  
31
32 NTSTATUS idmap_uid_to_sid(const char *domname, DOM_SID *sid, uid_t uid)
33 {
34         NTSTATUS ret;
35         struct id_map map;
36         bool expired;
37
38         DEBUG(10,("idmap_uid_to_sid: uid = [%lu], domain = '%s'\n",
39                   (unsigned long)uid, domname?domname:"NULL"));
40
41         if (winbindd_use_idmap_cache()
42             && idmap_cache_find_uid2sid(uid, sid, &expired)) {
43                 DEBUG(10, ("idmap_cache_find_uid2sid found %d%s\n", uid,
44                            expired ? " (expired)": ""));
45                 if (expired && idmap_is_online()) {
46                         DEBUG(10, ("revalidating expired entry\n"));
47                         goto backend;
48                 }
49                 if (is_null_sid(sid)) {
50                         DEBUG(10, ("Returning negative cache entry\n"));
51                         return NT_STATUS_NONE_MAPPED;
52                 }
53                 DEBUG(10, ("Returning positive cache entry\n"));
54                 return NT_STATUS_OK;
55         }
56
57 backend:
58         map.sid = sid;
59         map.xid.type = ID_TYPE_UID;
60         map.xid.id = uid;
61
62         ret = idmap_backends_unixid_to_sid(domname, &map);
63         if ( ! NT_STATUS_IS_OK(ret)) {
64                 DEBUG(10, ("error mapping uid [%lu]\n", (unsigned long)uid));
65                 return ret;
66         }
67
68         if (map.status != ID_MAPPED) {
69                 if (winbindd_use_idmap_cache()) {
70                         struct dom_sid null_sid;
71                         ZERO_STRUCT(null_sid);
72                         idmap_cache_set_sid2uid(&null_sid, uid);
73                 }
74                 DEBUG(10, ("uid [%lu] not mapped\n", (unsigned long)uid));
75                 return NT_STATUS_NONE_MAPPED;
76         }
77
78         if (winbindd_use_idmap_cache()) {
79                 idmap_cache_set_sid2uid(sid, uid);
80         }
81
82         return NT_STATUS_OK;
83 }
84
85 /*****************************************************************
86  Returns SID mapped to the given GID.
87  If mapping is not possible returns an error.
88 *****************************************************************/  
89
90 NTSTATUS idmap_gid_to_sid(const char *domname, DOM_SID *sid, gid_t gid)
91 {
92         NTSTATUS ret;
93         struct id_map map;
94         bool expired;
95
96         DEBUG(10,("idmap_gid_to_si: gid = [%lu], domain = '%s'\n",
97                   (unsigned long)gid, domname?domname:"NULL"));
98
99         if (winbindd_use_idmap_cache()
100             && idmap_cache_find_gid2sid(gid, sid, &expired)) {
101                 DEBUG(10, ("idmap_cache_find_gid2sid found %d%s\n", gid,
102                            expired ? " (expired)": ""));
103                 if (expired && idmap_is_online()) {
104                         DEBUG(10, ("revalidating expired entry\n"));
105                         goto backend;
106                 }
107                 if (is_null_sid(sid)) {
108                         DEBUG(10, ("Returning negative cache entry\n"));
109                         return NT_STATUS_NONE_MAPPED;
110                 }
111                 DEBUG(10, ("Returning positive cache entry\n"));
112                 return NT_STATUS_OK;
113         }
114
115 backend:
116         map.sid = sid;
117         map.xid.type = ID_TYPE_GID;
118         map.xid.id = gid;
119
120         ret = idmap_backends_unixid_to_sid(domname, &map);
121         if ( ! NT_STATUS_IS_OK(ret)) {
122                 DEBUG(10, ("error mapping gid [%lu]\n", (unsigned long)gid));
123                 return ret;
124         }
125
126         if (map.status != ID_MAPPED) {
127                 if (winbindd_use_idmap_cache()) {
128                         struct dom_sid null_sid;
129                         ZERO_STRUCT(null_sid);
130                         idmap_cache_set_sid2uid(&null_sid, gid);
131                 }
132                 DEBUG(10, ("gid [%lu] not mapped\n", (unsigned long)gid));
133                 return NT_STATUS_NONE_MAPPED;
134         }
135
136         if (winbindd_use_idmap_cache()) {
137                 idmap_cache_set_sid2gid(sid, gid);
138         }
139
140         return NT_STATUS_OK;
141 }
142
143 /*****************************************************************
144  Returns the UID mapped to the given SID.
145  If mapping is not possible or SID maps to a GID returns an error.
146 *****************************************************************/  
147
148 NTSTATUS idmap_sid_to_uid(const char *dom_name, DOM_SID *sid, uid_t *uid)
149 {
150         NTSTATUS ret;
151         struct id_map map;
152         bool expired;
153
154         DEBUG(10,("idmap_sid_to_uid: sid = [%s], domain = '%s'\n",
155                   sid_string_dbg(sid), dom_name));
156
157         if (winbindd_use_idmap_cache()
158             && idmap_cache_find_sid2uid(sid, uid, &expired)) {
159                 DEBUG(10, ("idmap_cache_find_sid2uid found %d%s\n",
160                            (int)(*uid), expired ? " (expired)": ""));
161                 if (expired && idmap_is_online()) {
162                         DEBUG(10, ("revalidating expired entry\n"));
163                         goto backend;
164                 }
165                 if ((*uid) == -1) {
166                         DEBUG(10, ("Returning negative cache entry\n"));
167                         return NT_STATUS_NONE_MAPPED;
168                 }
169                 DEBUG(10, ("Returning positive cache entry\n"));
170                 return NT_STATUS_OK;
171         }
172
173 backend:
174         map.sid = sid;
175         map.xid.type = ID_TYPE_UID;     
176
177         ret = idmap_backends_sid_to_unixid(dom_name, &map);
178
179         if (NT_STATUS_IS_OK(ret) && (map.status == ID_MAPPED)) {
180                 if (map.xid.type != ID_TYPE_UID) {
181                         DEBUG(10, ("sid [%s] not mapped to a uid "
182                                    "[%u,%u,%u]\n",
183                                    sid_string_dbg(sid),
184                                    map.status,
185                                    map.xid.type,
186                                    map.xid.id));
187                         if (winbindd_use_idmap_cache()) {
188                                 idmap_cache_set_sid2uid(sid, -1);
189                         }
190                         return NT_STATUS_NONE_MAPPED;
191                 }
192                 goto done;
193         }
194
195         if (dom_name[0] != '\0') {
196                 /*
197                  * We had the task to go to a specific domain which
198                  * could not answer our request. Fail.
199                  */
200                 if (winbindd_use_idmap_cache()) {
201                         idmap_cache_set_sid2uid(sid, -1);
202                 }
203                 return NT_STATUS_NONE_MAPPED;
204         }
205
206         ret = idmap_new_mapping(sid, ID_TYPE_UID, &map.xid);
207
208         if (!NT_STATUS_IS_OK(ret)) {
209                 DEBUG(10, ("idmap_new_mapping failed: %s\n",
210                            nt_errstr(ret)));
211                 if (winbindd_use_idmap_cache()) {
212                         idmap_cache_set_sid2uid(sid, -1);
213                 }
214                 return ret;
215         }
216
217 done:
218         *uid = (uid_t)map.xid.id;
219         if (winbindd_use_idmap_cache()) {
220                 idmap_cache_set_sid2uid(sid, *uid);
221         }
222         return NT_STATUS_OK;
223 }
224
225 /*****************************************************************
226  Returns the GID mapped to the given SID.
227  If mapping is not possible or SID maps to a UID returns an error.
228 *****************************************************************/  
229
230 NTSTATUS idmap_sid_to_gid(const char *domname, DOM_SID *sid, gid_t *gid)
231 {
232         NTSTATUS ret;
233         struct id_map map;
234         bool expired;
235
236         DEBUG(10,("idmap_sid_to_gid: sid = [%s], domain = '%s'\n",
237                   sid_string_dbg(sid), domname));
238
239         if (winbindd_use_idmap_cache()
240             && idmap_cache_find_sid2gid(sid, gid, &expired)) {
241                 DEBUG(10, ("idmap_cache_find_sid2gid found %d%s\n",
242                            (int)(*gid), expired ? " (expired)": ""));
243                 if (expired && idmap_is_online()) {
244                         DEBUG(10, ("revalidating expired entry\n"));
245                         goto backend;
246                 }
247                 if ((*gid) == -1) {
248                         DEBUG(10, ("Returning negative cache entry\n"));
249                         return NT_STATUS_NONE_MAPPED;
250                 }
251                 DEBUG(10, ("Returning positive cache entry\n"));
252                 return NT_STATUS_OK;
253         }
254
255 backend:
256         map.sid = sid;
257         map.xid.type = ID_TYPE_GID;
258
259         ret = idmap_backends_sid_to_unixid(domname, &map);
260         if (NT_STATUS_IS_OK(ret) && (map.status == ID_MAPPED)) {
261                 if (map.xid.type != ID_TYPE_GID) {
262                         DEBUG(10, ("sid [%s] not mapped to a gid "
263                                    "[%u,%u,%u]\n",
264                                    sid_string_dbg(sid),
265                                    map.status,
266                                    map.xid.type,
267                                    map.xid.id));
268                         if (winbindd_use_idmap_cache()) {
269                                 idmap_cache_set_sid2gid(sid, -1);
270                         }
271                         return NT_STATUS_NONE_MAPPED;
272                 }
273                 goto done;
274         }
275
276         if (domname[0] != '\0') {
277                 /*
278                  * We had the task to go to a specific domain which
279                  * could not answer our request. Fail.
280                  */
281                 if (winbindd_use_idmap_cache()) {
282                         idmap_cache_set_sid2uid(sid, -1);
283                 }
284                 return NT_STATUS_NONE_MAPPED;
285         }
286
287         ret = idmap_new_mapping(sid, ID_TYPE_GID, &map.xid);
288
289         if (!NT_STATUS_IS_OK(ret)) {
290                 DEBUG(10, ("idmap_new_mapping failed: %s\n",
291                            nt_errstr(ret)));
292                 if (winbindd_use_idmap_cache()) {
293                         idmap_cache_set_sid2gid(sid, -1);
294                 }
295                 return ret;
296         }
297
298 done:
299         *gid = map.xid.id;
300         if (winbindd_use_idmap_cache()) {
301                 idmap_cache_set_sid2gid(sid, *gid);
302         }
303         return NT_STATUS_OK;
304 }