libwbclient: unimplement wbcSetUidMapping()
[amitay/samba.git] / nsswitch / libwbclient / wbc_idmap.c
1 /*
2    Unix SMB/CIFS implementation.
3
4    Winbind client API
5
6    Copyright (C) Gerald (Jerry) Carter 2007
7
8    This library is free software; you can redistribute it and/or
9    modify it under the terms of the GNU Lesser General Public
10    License as published by the Free Software Foundation; either
11    version 3 of the License, or (at your option) any later version.
12
13    This library is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16    Library General Public License for more details.
17
18    You should have received a copy of the GNU Lesser General Public License
19    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 /* Required Headers */
23
24 #include "replace.h"
25 #include "libwbclient.h"
26
27 /* Convert a Windows SID to a Unix uid, allocating an uid if needed */
28 wbcErr wbcSidToUid(const struct wbcDomainSid *sid, uid_t *puid)
29 {
30         struct winbindd_request request;
31         struct winbindd_response response;
32         char *sid_string = NULL;
33         wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
34
35         if (!sid || !puid) {
36                 wbc_status = WBC_ERR_INVALID_PARAM;
37                 BAIL_ON_WBC_ERROR(wbc_status);
38         }
39
40         /* Initialize request */
41
42         ZERO_STRUCT(request);
43         ZERO_STRUCT(response);
44
45         wbc_status = wbcSidToString(sid, &sid_string);
46         BAIL_ON_WBC_ERROR(wbc_status);
47
48         strncpy(request.data.sid, sid_string, sizeof(request.data.sid)-1);
49         wbcFreeMemory(sid_string);
50
51         /* Make request */
52
53         wbc_status = wbcRequestResponse(WINBINDD_SID_TO_UID,
54                                         &request,
55                                         &response);
56         BAIL_ON_WBC_ERROR(wbc_status);
57
58         *puid = response.data.uid;
59
60         wbc_status = WBC_ERR_SUCCESS;
61
62  done:
63         return wbc_status;
64 }
65
66 /* Convert a Windows SID to a Unix uid if there already is a mapping */
67 wbcErr wbcQuerySidToUid(const struct wbcDomainSid *sid,
68                         uid_t *puid)
69 {
70         return WBC_ERR_NOT_IMPLEMENTED;
71 }
72
73 /* Convert a Unix uid to a Windows SID, allocating a SID if needed */
74 wbcErr wbcUidToSid(uid_t uid, struct wbcDomainSid *sid)
75 {
76         wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
77         struct winbindd_request request;
78         struct winbindd_response response;
79
80         if (!sid) {
81                 wbc_status = WBC_ERR_INVALID_PARAM;
82                 BAIL_ON_WBC_ERROR(wbc_status);
83         }
84
85         /* Initialize request */
86
87         ZERO_STRUCT(request);
88         ZERO_STRUCT(response);
89
90         request.data.uid = uid;
91
92         /* Make request */
93
94         wbc_status = wbcRequestResponse(WINBINDD_UID_TO_SID,
95                                         &request,
96                                         &response);
97         BAIL_ON_WBC_ERROR(wbc_status);
98
99         wbc_status = wbcStringToSid(response.data.sid.sid, sid);
100         BAIL_ON_WBC_ERROR(wbc_status);
101
102 done:
103         return wbc_status;
104 }
105
106 /* Convert a Unix uid to a Windows SID if there already is a mapping */
107 wbcErr wbcQueryUidToSid(uid_t uid,
108                         struct wbcDomainSid *sid)
109 {
110         return WBC_ERR_NOT_IMPLEMENTED;
111 }
112
113 /** @brief Convert a Windows SID to a Unix gid, allocating a gid if needed
114  *
115  * @param *sid        Pointer to the domain SID to be resolved
116  * @param *pgid       Pointer to the resolved gid_t value
117  *
118  * @return #wbcErr
119  *
120  **/
121
122 wbcErr wbcSidToGid(const struct wbcDomainSid *sid, gid_t *pgid)
123 {
124         struct winbindd_request request;
125         struct winbindd_response response;
126         wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
127         char *sid_string = NULL;
128
129         if (!sid || !pgid) {
130                 wbc_status = WBC_ERR_INVALID_PARAM;
131                 BAIL_ON_WBC_ERROR(wbc_status);
132         }
133
134         /* Initialize request */
135
136         ZERO_STRUCT(request);
137         ZERO_STRUCT(response);
138
139         wbc_status = wbcSidToString(sid, &sid_string);
140         BAIL_ON_WBC_ERROR(wbc_status);
141
142         strncpy(request.data.sid, sid_string, sizeof(request.data.sid)-1);
143         wbcFreeMemory(sid_string);
144
145         /* Make request */
146
147         wbc_status = wbcRequestResponse(WINBINDD_SID_TO_GID,
148                                         &request,
149                                         &response);
150         BAIL_ON_WBC_ERROR(wbc_status);
151
152         *pgid = response.data.gid;
153
154         wbc_status = WBC_ERR_SUCCESS;
155
156  done:
157         return wbc_status;
158 }
159
160
161 /* Convert a Windows SID to a Unix gid if there already is a mapping */
162
163 wbcErr wbcQuerySidToGid(const struct wbcDomainSid *sid,
164                         gid_t *pgid)
165 {
166         return WBC_ERR_NOT_IMPLEMENTED;
167 }
168
169
170 /* Convert a Unix gid to a Windows SID, allocating a SID if needed */
171 wbcErr wbcGidToSid(gid_t gid, struct wbcDomainSid *sid)
172 {
173         struct winbindd_request request;
174         struct winbindd_response response;
175         wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
176
177         if (!sid) {
178                 wbc_status = WBC_ERR_INVALID_PARAM;
179                 BAIL_ON_WBC_ERROR(wbc_status);
180         }
181
182         /* Initialize request */
183
184         ZERO_STRUCT(request);
185         ZERO_STRUCT(response);
186
187         request.data.gid = gid;
188
189         /* Make request */
190
191         wbc_status = wbcRequestResponse(WINBINDD_GID_TO_SID,
192                                         &request,
193                                         &response);
194         BAIL_ON_WBC_ERROR(wbc_status);
195
196         wbc_status = wbcStringToSid(response.data.sid.sid, sid);
197         BAIL_ON_WBC_ERROR(wbc_status);
198
199 done:
200         return wbc_status;
201 }
202
203 /* Convert a Unix gid to a Windows SID if there already is a mapping */
204 wbcErr wbcQueryGidToSid(gid_t gid,
205                         struct wbcDomainSid *sid)
206 {
207         return WBC_ERR_NOT_IMPLEMENTED;
208 }
209
210 /* Obtain a new uid from Winbind */
211 wbcErr wbcAllocateUid(uid_t *puid)
212 {
213         struct winbindd_request request;
214         struct winbindd_response response;
215         wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
216
217         if (!puid)
218                 return WBC_ERR_INVALID_PARAM;
219
220         /* Initialise request */
221
222         ZERO_STRUCT(request);
223         ZERO_STRUCT(response);
224
225         /* Make request */
226
227         wbc_status = wbcRequestResponsePriv(WINBINDD_ALLOCATE_UID,
228                                             &request, &response);
229         BAIL_ON_WBC_ERROR(wbc_status);
230
231         /* Copy out result */
232         *puid = response.data.uid;
233
234         wbc_status = WBC_ERR_SUCCESS;
235
236  done:
237         return wbc_status;
238 }
239
240 /* Obtain a new gid from Winbind */
241 wbcErr wbcAllocateGid(gid_t *pgid)
242 {
243         struct winbindd_request request;
244         struct winbindd_response response;
245         wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
246
247         if (!pgid)
248                 return WBC_ERR_INVALID_PARAM;
249
250         /* Initialise request */
251
252         ZERO_STRUCT(request);
253         ZERO_STRUCT(response);
254
255         /* Make request */
256
257         wbc_status = wbcRequestResponsePriv(WINBINDD_ALLOCATE_GID,
258                                             &request, &response);
259         BAIL_ON_WBC_ERROR(wbc_status);
260
261         /* Copy out result */
262         *pgid = response.data.gid;
263
264         wbc_status = WBC_ERR_SUCCESS;
265
266  done:
267         return wbc_status;
268 }
269
270 /* we can't include smb.h here... */
271 #define _ID_TYPE_UID 1
272 #define _ID_TYPE_GID 2
273
274 /* Set an user id mapping - not implemented any more */
275 wbcErr wbcSetUidMapping(uid_t uid, const struct wbcDomainSid *sid)
276 {
277         return WBC_ERR_NOT_IMPLEMENTED;
278 }
279
280 /* Set a group id mapping */
281 wbcErr wbcSetGidMapping(gid_t gid, const struct wbcDomainSid *sid)
282 {
283         struct winbindd_request request;
284         struct winbindd_response response;
285         wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
286         char *sid_string = NULL;
287
288         if (!sid) {
289                 return WBC_ERR_INVALID_PARAM;
290         }
291
292         /* Initialise request */
293
294         ZERO_STRUCT(request);
295         ZERO_STRUCT(response);
296
297         /* Make request */
298
299         request.data.dual_idmapset.id = gid;
300         request.data.dual_idmapset.type = _ID_TYPE_GID;
301
302         wbc_status = wbcSidToString(sid, &sid_string);
303         BAIL_ON_WBC_ERROR(wbc_status);
304
305         strncpy(request.data.dual_idmapset.sid, sid_string,
306                 sizeof(request.data.dual_idmapset.sid)-1);
307         wbcFreeMemory(sid_string);
308
309         wbc_status = wbcRequestResponsePriv(WINBINDD_SET_MAPPING,
310                                             &request, &response);
311         BAIL_ON_WBC_ERROR(wbc_status);
312
313  done:
314         return wbc_status;
315 }
316
317 /* Remove a user id mapping */
318 wbcErr wbcRemoveUidMapping(uid_t uid, const struct wbcDomainSid *sid)
319 {
320         struct winbindd_request request;
321         struct winbindd_response response;
322         wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
323         char *sid_string = NULL;
324
325         if (!sid) {
326                 return WBC_ERR_INVALID_PARAM;
327         }
328
329         /* Initialise request */
330
331         ZERO_STRUCT(request);
332         ZERO_STRUCT(response);
333
334         /* Make request */
335
336         request.data.dual_idmapset.id = uid;
337         request.data.dual_idmapset.type = _ID_TYPE_UID;
338
339         wbc_status = wbcSidToString(sid, &sid_string);
340         BAIL_ON_WBC_ERROR(wbc_status);
341
342         strncpy(request.data.dual_idmapset.sid, sid_string,
343                 sizeof(request.data.dual_idmapset.sid)-1);
344         wbcFreeMemory(sid_string);
345
346         wbc_status = wbcRequestResponsePriv(WINBINDD_REMOVE_MAPPING,
347                                             &request, &response);
348         BAIL_ON_WBC_ERROR(wbc_status);
349
350  done:
351         return wbc_status;
352 }
353
354 /* Remove a group id mapping */
355 wbcErr wbcRemoveGidMapping(gid_t gid, const struct wbcDomainSid *sid)
356 {
357         struct winbindd_request request;
358         struct winbindd_response response;
359         wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
360         char *sid_string = NULL;
361
362         if (!sid) {
363                 return WBC_ERR_INVALID_PARAM;
364         }
365
366         /* Initialise request */
367
368         ZERO_STRUCT(request);
369         ZERO_STRUCT(response);
370
371         /* Make request */
372
373         request.data.dual_idmapset.id = gid;
374         request.data.dual_idmapset.type = _ID_TYPE_GID;
375
376         wbc_status = wbcSidToString(sid, &sid_string);
377         BAIL_ON_WBC_ERROR(wbc_status);
378
379         strncpy(request.data.dual_idmapset.sid, sid_string,
380                 sizeof(request.data.dual_idmapset.sid)-1);
381         wbcFreeMemory(sid_string);
382
383         wbc_status = wbcRequestResponsePriv(WINBINDD_REMOVE_MAPPING,
384                                             &request, &response);
385         BAIL_ON_WBC_ERROR(wbc_status);
386
387  done:
388         return wbc_status;
389 }
390
391 /* Set the highwater mark for allocated uids - not implemented any more */
392 wbcErr wbcSetUidHwm(uid_t uid_hwm)
393 {
394         return WBC_ERR_NOT_IMPLEMENTED;
395 }
396
397 /* Set the highwater mark for allocated gids - not implemented any more */
398 wbcErr wbcSetGidHwm(gid_t gid_hwm)
399 {
400         return WBC_ERR_NOT_IMPLEMENTED;
401 }