s3-selftest: enable the RPC-NTSVCS torture test against Samba3.
[Samba.git] / nsswitch / libwbclient / wbc_idmap.c
blob5b2ab875f62b168fe6ba9195dbe3b53a9661957b
1 /*
2 Unix SMB/CIFS implementation.
4 Winbind client API
6 Copyright (C) Gerald (Jerry) Carter 2007
9 This library is free software; you can redistribute it and/or
10 modify it under the terms of the GNU Lesser General Public
11 License as published by the Free Software Foundation; either
12 version 3 of the License, or (at your option) any later version.
14 This library is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 Library General Public License for more details.
19 You should have received a copy of the GNU Lesser General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 /* Required Headers */
25 #include "libwbclient.h"
27 /* Convert a Windows SID to a Unix uid, allocating an uid if needed */
28 wbcErr wbcSidToUid(const struct wbcDomainSid *sid, uid_t *puid)
30 struct winbindd_request request;
31 struct winbindd_response response;
32 char *sid_string = NULL;
33 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
35 if (!sid || !puid) {
36 wbc_status = WBC_ERR_INVALID_PARAM;
37 BAIL_ON_WBC_ERROR(wbc_status);
40 /* Initialize request */
42 ZERO_STRUCT(request);
43 ZERO_STRUCT(response);
45 wbc_status = wbcSidToString(sid, &sid_string);
46 BAIL_ON_WBC_ERROR(wbc_status);
48 strncpy(request.data.sid, sid_string, sizeof(request.data.sid)-1);
49 wbcFreeMemory(sid_string);
51 /* Make request */
53 wbc_status = wbcRequestResponse(WINBINDD_SID_TO_UID,
54 &request,
55 &response);
56 BAIL_ON_WBC_ERROR(wbc_status);
58 *puid = response.data.uid;
60 wbc_status = WBC_ERR_SUCCESS;
62 done:
63 return wbc_status;
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)
70 return WBC_ERR_NOT_IMPLEMENTED;
73 /* Convert a Unix uid to a Windows SID, allocating a SID if needed */
74 wbcErr wbcUidToSid(uid_t uid, struct wbcDomainSid *sid)
76 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
77 struct winbindd_request request;
78 struct winbindd_response response;
80 if (!sid) {
81 wbc_status = WBC_ERR_INVALID_PARAM;
82 BAIL_ON_WBC_ERROR(wbc_status);
85 /* Initialize request */
87 ZERO_STRUCT(request);
88 ZERO_STRUCT(response);
90 request.data.uid = uid;
92 /* Make request */
94 wbc_status = wbcRequestResponse(WINBINDD_UID_TO_SID,
95 &request,
96 &response);
97 BAIL_ON_WBC_ERROR(wbc_status);
99 wbc_status = wbcStringToSid(response.data.sid.sid, sid);
100 BAIL_ON_WBC_ERROR(wbc_status);
102 done:
103 return wbc_status;
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)
110 return WBC_ERR_NOT_IMPLEMENTED;
113 /** @brief Convert a Windows SID to a Unix gid, allocating a gid if needed
115 * @param *sid Pointer to the domain SID to be resolved
116 * @param *pgid Pointer to the resolved gid_t value
118 * @return #wbcErr
122 wbcErr wbcSidToGid(const struct wbcDomainSid *sid, gid_t *pgid)
124 struct winbindd_request request;
125 struct winbindd_response response;
126 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
127 char *sid_string = NULL;
129 if (!sid || !pgid) {
130 wbc_status = WBC_ERR_INVALID_PARAM;
131 BAIL_ON_WBC_ERROR(wbc_status);
134 /* Initialize request */
136 ZERO_STRUCT(request);
137 ZERO_STRUCT(response);
139 wbc_status = wbcSidToString(sid, &sid_string);
140 BAIL_ON_WBC_ERROR(wbc_status);
142 strncpy(request.data.sid, sid_string, sizeof(request.data.sid)-1);
143 wbcFreeMemory(sid_string);
145 /* Make request */
147 wbc_status = wbcRequestResponse(WINBINDD_SID_TO_GID,
148 &request,
149 &response);
150 BAIL_ON_WBC_ERROR(wbc_status);
152 *pgid = response.data.gid;
154 wbc_status = WBC_ERR_SUCCESS;
156 done:
157 return wbc_status;
160 /* Convert a Windows SID to a Unix gid if there already is a mapping */
162 wbcErr wbcQuerySidToGid(const struct wbcDomainSid *sid,
163 gid_t *pgid)
165 return WBC_ERR_NOT_IMPLEMENTED;
168 /* Convert a Unix gid to a Windows SID, allocating a SID if needed */
169 wbcErr wbcGidToSid(gid_t gid, struct wbcDomainSid *sid)
171 struct winbindd_request request;
172 struct winbindd_response response;
173 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
175 if (!sid) {
176 wbc_status = WBC_ERR_INVALID_PARAM;
177 BAIL_ON_WBC_ERROR(wbc_status);
180 /* Initialize request */
182 ZERO_STRUCT(request);
183 ZERO_STRUCT(response);
185 request.data.gid = gid;
187 /* Make request */
189 wbc_status = wbcRequestResponse(WINBINDD_GID_TO_SID,
190 &request,
191 &response);
192 BAIL_ON_WBC_ERROR(wbc_status);
194 wbc_status = wbcStringToSid(response.data.sid.sid, sid);
195 BAIL_ON_WBC_ERROR(wbc_status);
197 done:
198 return wbc_status;
201 /* Convert a Unix gid to a Windows SID if there already is a mapping */
202 wbcErr wbcQueryGidToSid(gid_t gid,
203 struct wbcDomainSid *sid)
205 return WBC_ERR_NOT_IMPLEMENTED;
208 /* Obtain a new uid from Winbind */
209 wbcErr wbcAllocateUid(uid_t *puid)
211 struct winbindd_request request;
212 struct winbindd_response response;
213 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
215 if (!puid)
216 return WBC_ERR_INVALID_PARAM;
218 /* Initialise request */
220 ZERO_STRUCT(request);
221 ZERO_STRUCT(response);
223 /* Make request */
225 wbc_status = wbcRequestResponse(WINBINDD_ALLOCATE_UID,
226 &request, &response);
227 BAIL_ON_WBC_ERROR(wbc_status);
229 /* Copy out result */
230 *puid = response.data.uid;
232 wbc_status = WBC_ERR_SUCCESS;
234 done:
235 return wbc_status;
238 /* Obtain a new gid from Winbind */
239 wbcErr wbcAllocateGid(gid_t *pgid)
241 struct winbindd_request request;
242 struct winbindd_response response;
243 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
245 if (!pgid)
246 return WBC_ERR_INVALID_PARAM;
248 /* Initialise request */
250 ZERO_STRUCT(request);
251 ZERO_STRUCT(response);
253 /* Make request */
255 wbc_status = wbcRequestResponse(WINBINDD_ALLOCATE_GID,
256 &request, &response);
257 BAIL_ON_WBC_ERROR(wbc_status);
259 /* Copy out result */
260 *pgid = response.data.gid;
262 wbc_status = WBC_ERR_SUCCESS;
264 done:
265 return wbc_status;
268 /* we can't include smb.h here... */
269 #define _ID_TYPE_UID 1
270 #define _ID_TYPE_GID 2
272 /* Set an user id mapping */
273 wbcErr wbcSetUidMapping(uid_t uid, const struct wbcDomainSid *sid)
275 struct winbindd_request request;
276 struct winbindd_response response;
277 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
278 char *sid_string = NULL;
280 if (!sid) {
281 return WBC_ERR_INVALID_PARAM;
284 /* Initialise request */
286 ZERO_STRUCT(request);
287 ZERO_STRUCT(response);
289 /* Make request */
291 request.data.dual_idmapset.id = uid;
292 request.data.dual_idmapset.type = _ID_TYPE_UID;
294 wbc_status = wbcSidToString(sid, &sid_string);
295 BAIL_ON_WBC_ERROR(wbc_status);
297 strncpy(request.data.dual_idmapset.sid, sid_string,
298 sizeof(request.data.dual_idmapset.sid)-1);
299 wbcFreeMemory(sid_string);
301 wbc_status = wbcRequestResponse(WINBINDD_SET_MAPPING,
302 &request, &response);
303 BAIL_ON_WBC_ERROR(wbc_status);
305 done:
306 return wbc_status;
309 /* Set a group id mapping */
310 wbcErr wbcSetGidMapping(gid_t gid, const struct wbcDomainSid *sid)
312 struct winbindd_request request;
313 struct winbindd_response response;
314 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
315 char *sid_string = NULL;
317 if (!sid) {
318 return WBC_ERR_INVALID_PARAM;
321 /* Initialise request */
323 ZERO_STRUCT(request);
324 ZERO_STRUCT(response);
326 /* Make request */
328 request.data.dual_idmapset.id = gid;
329 request.data.dual_idmapset.type = _ID_TYPE_GID;
331 wbc_status = wbcSidToString(sid, &sid_string);
332 BAIL_ON_WBC_ERROR(wbc_status);
334 strncpy(request.data.dual_idmapset.sid, sid_string,
335 sizeof(request.data.dual_idmapset.sid)-1);
336 wbcFreeMemory(sid_string);
338 wbc_status = wbcRequestResponse(WINBINDD_SET_MAPPING,
339 &request, &response);
340 BAIL_ON_WBC_ERROR(wbc_status);
342 done:
343 return wbc_status;
346 /* Remove a user id mapping */
347 wbcErr wbcRemoveUidMapping(uid_t uid, const struct wbcDomainSid *sid)
349 struct winbindd_request request;
350 struct winbindd_response response;
351 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
352 char *sid_string = NULL;
354 if (!sid) {
355 return WBC_ERR_INVALID_PARAM;
358 /* Initialise request */
360 ZERO_STRUCT(request);
361 ZERO_STRUCT(response);
363 /* Make request */
365 request.data.dual_idmapset.id = uid;
366 request.data.dual_idmapset.type = _ID_TYPE_UID;
368 wbc_status = wbcSidToString(sid, &sid_string);
369 BAIL_ON_WBC_ERROR(wbc_status);
371 strncpy(request.data.dual_idmapset.sid, sid_string,
372 sizeof(request.data.dual_idmapset.sid)-1);
373 wbcFreeMemory(sid_string);
375 wbc_status = wbcRequestResponse(WINBINDD_REMOVE_MAPPING,
376 &request, &response);
377 BAIL_ON_WBC_ERROR(wbc_status);
379 done:
380 return wbc_status;
383 /* Remove a group id mapping */
384 wbcErr wbcRemoveGidMapping(gid_t gid, const struct wbcDomainSid *sid)
386 struct winbindd_request request;
387 struct winbindd_response response;
388 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
389 char *sid_string = NULL;
391 if (!sid) {
392 return WBC_ERR_INVALID_PARAM;
395 /* Initialise request */
397 ZERO_STRUCT(request);
398 ZERO_STRUCT(response);
400 /* Make request */
402 request.data.dual_idmapset.id = gid;
403 request.data.dual_idmapset.type = _ID_TYPE_GID;
405 wbc_status = wbcSidToString(sid, &sid_string);
406 BAIL_ON_WBC_ERROR(wbc_status);
408 strncpy(request.data.dual_idmapset.sid, sid_string,
409 sizeof(request.data.dual_idmapset.sid)-1);
410 wbcFreeMemory(sid_string);
412 wbc_status = wbcRequestResponse(WINBINDD_REMOVE_MAPPING,
413 &request, &response);
414 BAIL_ON_WBC_ERROR(wbc_status);
416 done:
417 return wbc_status;
420 /* Set the highwater mark for allocated uids. */
421 wbcErr wbcSetUidHwm(uid_t uid_hwm)
423 struct winbindd_request request;
424 struct winbindd_response response;
425 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
427 /* Initialise request */
429 ZERO_STRUCT(request);
430 ZERO_STRUCT(response);
432 /* Make request */
434 request.data.dual_idmapset.id = uid_hwm;
435 request.data.dual_idmapset.type = _ID_TYPE_UID;
437 wbc_status = wbcRequestResponse(WINBINDD_SET_HWM,
438 &request, &response);
439 BAIL_ON_WBC_ERROR(wbc_status);
441 done:
442 return wbc_status;
445 /* Set the highwater mark for allocated gids. */
446 wbcErr wbcSetGidHwm(gid_t gid_hwm)
448 struct winbindd_request request;
449 struct winbindd_response response;
450 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
452 /* Initialise request */
454 ZERO_STRUCT(request);
455 ZERO_STRUCT(response);
457 /* Make request */
459 request.data.dual_idmapset.id = gid_hwm;
460 request.data.dual_idmapset.type = _ID_TYPE_GID;
462 wbc_status = wbcRequestResponse(WINBINDD_SET_HWM,
463 &request, &response);
464 BAIL_ON_WBC_ERROR(wbc_status);
466 done:
467 return wbc_status;