Added ability to remove id mappings in wbinfo and libwbclient.
[Samba/nivanova.git] / source3 / nsswitch / libwbclient / wbc_idmap.c
blob6652f676364f3b7cd2eb136d22bf56bf309fc8f9
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 /** @brief Convert a Windows SID to a Unix uid
29 * @param *sid Pointer to the domain SID to be resolved
30 * @param *puid Pointer to the resolved uid_t value
32 * @return #wbcErr
34 **/
36 wbcErr wbcSidToUid(const struct wbcDomainSid *sid, uid_t *puid)
38 struct winbindd_request request;
39 struct winbindd_response response;
40 char *sid_string = NULL;
41 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
43 if (!sid || !puid) {
44 wbc_status = WBC_ERR_INVALID_PARAM;
45 BAIL_ON_WBC_ERROR(wbc_status);
48 /* Initialize request */
50 ZERO_STRUCT(request);
51 ZERO_STRUCT(response);
53 wbc_status = wbcSidToString(sid, &sid_string);
54 BAIL_ON_WBC_ERROR(wbc_status);
56 strncpy(request.data.sid, sid_string, sizeof(request.data.sid)-1);
57 wbcFreeMemory(sid_string);
59 /* Make request */
61 wbc_status = wbcRequestResponse(WINBINDD_SID_TO_UID,
62 &request,
63 &response);
64 BAIL_ON_WBC_ERROR(wbc_status);
66 *puid = response.data.uid;
68 wbc_status = WBC_ERR_SUCCESS;
70 done:
71 return wbc_status;
74 /** @brief Convert a Unix uid to a Windows SID
76 * @param uid Unix uid to be resolved
77 * @param *sid Pointer to the resolved domain SID
79 * @return #wbcErr
81 **/
83 wbcErr wbcUidToSid(uid_t uid, struct wbcDomainSid *sid)
85 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
86 struct winbindd_request request;
87 struct winbindd_response response;
89 if (!sid) {
90 wbc_status = WBC_ERR_INVALID_PARAM;
91 BAIL_ON_WBC_ERROR(wbc_status);
94 /* Initialize request */
96 ZERO_STRUCT(request);
97 ZERO_STRUCT(response);
99 request.data.uid = uid;
101 /* Make request */
103 wbc_status = wbcRequestResponse(WINBINDD_UID_TO_SID,
104 &request,
105 &response);
106 BAIL_ON_WBC_ERROR(wbc_status);
108 wbc_status = wbcStringToSid(response.data.sid.sid, sid);
109 BAIL_ON_WBC_ERROR(wbc_status);
111 done:
112 return wbc_status;
115 /** @brief Convert a Windows SID to a Unix gid
117 * @param *sid Pointer to the domain SID to be resolved
118 * @param *pgid Pointer to the resolved gid_t value
120 * @return #wbcErr
124 wbcErr wbcSidToGid(const struct wbcDomainSid *sid, gid_t *pgid)
126 struct winbindd_request request;
127 struct winbindd_response response;
128 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
129 char *sid_string = NULL;
131 if (!sid || !pgid) {
132 wbc_status = WBC_ERR_INVALID_PARAM;
133 BAIL_ON_WBC_ERROR(wbc_status);
136 /* Initialize request */
138 ZERO_STRUCT(request);
139 ZERO_STRUCT(response);
141 wbc_status = wbcSidToString(sid, &sid_string);
142 BAIL_ON_WBC_ERROR(wbc_status);
144 strncpy(request.data.sid, sid_string, sizeof(request.data.sid)-1);
145 wbcFreeMemory(sid_string);
147 /* Make request */
149 wbc_status = wbcRequestResponse(WINBINDD_SID_TO_GID,
150 &request,
151 &response);
152 BAIL_ON_WBC_ERROR(wbc_status);
154 *pgid = response.data.gid;
156 wbc_status = WBC_ERR_SUCCESS;
158 done:
159 return wbc_status;
162 /** @brief Convert a Unix uid to a Windows SID
164 * @param gid Unix gid to be resolved
165 * @param *sid Pointer to the resolved domain SID
167 * @return #wbcErr
171 wbcErr wbcGidToSid(gid_t gid, struct wbcDomainSid *sid)
173 struct winbindd_request request;
174 struct winbindd_response response;
175 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
177 if (!sid) {
178 wbc_status = WBC_ERR_INVALID_PARAM;
179 BAIL_ON_WBC_ERROR(wbc_status);
182 /* Initialize request */
184 ZERO_STRUCT(request);
185 ZERO_STRUCT(response);
187 request.data.gid = gid;
189 /* Make request */
191 wbc_status = wbcRequestResponse(WINBINDD_GID_TO_SID,
192 &request,
193 &response);
194 BAIL_ON_WBC_ERROR(wbc_status);
196 wbc_status = wbcStringToSid(response.data.sid.sid, sid);
197 BAIL_ON_WBC_ERROR(wbc_status);
199 done:
200 return wbc_status;
203 /** @brief Obtain a new uid from Winbind
205 * @param *puid *pointer to the allocated uid
207 * @return #wbcErr
210 wbcErr wbcAllocateUid(uid_t *puid)
212 struct winbindd_request request;
213 struct winbindd_response response;
214 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
216 if (!puid)
217 return WBC_ERR_INVALID_PARAM;
219 /* Initialise request */
221 ZERO_STRUCT(request);
222 ZERO_STRUCT(response);
224 /* Make request */
226 wbc_status = wbcRequestResponse(WINBINDD_ALLOCATE_UID,
227 &request, &response);
228 BAIL_ON_WBC_ERROR(wbc_status);
230 /* Copy out result */
231 *puid = response.data.uid;
233 wbc_status = WBC_ERR_SUCCESS;
235 done:
236 return wbc_status;
239 /** @brief Obtain a new gid from Winbind
241 * @param *pgid Pointer to the allocated gid
243 * @return #wbcErr
246 wbcErr wbcAllocateGid(gid_t *pgid)
248 struct winbindd_request request;
249 struct winbindd_response response;
250 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
252 if (!pgid)
253 return WBC_ERR_INVALID_PARAM;
255 /* Initialise request */
257 ZERO_STRUCT(request);
258 ZERO_STRUCT(response);
260 /* Make request */
262 wbc_status = wbcRequestResponse(WINBINDD_ALLOCATE_GID,
263 &request, &response);
264 BAIL_ON_WBC_ERROR(wbc_status);
266 /* Copy out result */
267 *pgid = response.data.gid;
269 wbc_status = WBC_ERR_SUCCESS;
271 done:
272 return wbc_status;
275 /* we can't include smb.h here... */
276 #define _ID_TYPE_UID 1
277 #define _ID_TYPE_GID 2
279 /** @brief Set an user id mapping
281 * @param uid Uid of the desired mapping.
282 * @param *sid Pointer to the sid of the diresired mapping.
284 * @return #wbcErr
286 wbcErr wbcSetUidMapping(uid_t uid, const struct wbcDomainSid *sid)
288 struct winbindd_request request;
289 struct winbindd_response response;
290 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
291 char *sid_string = NULL;
293 if (!sid) {
294 return WBC_ERR_INVALID_PARAM;
297 /* Initialise request */
299 ZERO_STRUCT(request);
300 ZERO_STRUCT(response);
302 /* Make request */
304 request.data.dual_idmapset.id = uid;
305 request.data.dual_idmapset.type = _ID_TYPE_UID;
307 wbc_status = wbcSidToString(sid, &sid_string);
308 BAIL_ON_WBC_ERROR(wbc_status);
310 strncpy(request.data.dual_idmapset.sid, sid_string,
311 sizeof(request.data.dual_idmapset.sid)-1);
312 wbcFreeMemory(sid_string);
314 wbc_status = wbcRequestResponse(WINBINDD_SET_MAPPING,
315 &request, &response);
316 BAIL_ON_WBC_ERROR(wbc_status);
318 done:
319 return wbc_status;
322 /** @brief Set a group id mapping
324 * @param gid Gid of the desired mapping.
325 * @param *sid Pointer to the sid of the diresired mapping.
327 * @return #wbcErr
329 wbcErr wbcSetGidMapping(gid_t gid, const struct wbcDomainSid *sid)
331 struct winbindd_request request;
332 struct winbindd_response response;
333 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
334 char *sid_string = NULL;
336 if (!sid) {
337 return WBC_ERR_INVALID_PARAM;
340 /* Initialise request */
342 ZERO_STRUCT(request);
343 ZERO_STRUCT(response);
345 /* Make request */
347 request.data.dual_idmapset.id = gid;
348 request.data.dual_idmapset.type = _ID_TYPE_GID;
350 wbc_status = wbcSidToString(sid, &sid_string);
351 BAIL_ON_WBC_ERROR(wbc_status);
353 strncpy(request.data.dual_idmapset.sid, sid_string,
354 sizeof(request.data.dual_idmapset.sid)-1);
355 wbcFreeMemory(sid_string);
357 wbc_status = wbcRequestResponse(WINBINDD_SET_MAPPING,
358 &request, &response);
359 BAIL_ON_WBC_ERROR(wbc_status);
361 done:
362 return wbc_status;
365 /** @brief Remove a user id mapping
367 * @param uid Uid of the mapping to remove.
368 * @param *sid Pointer to the sid of the mapping to remove.
370 * @return #wbcErr
372 wbcErr wbcRemoveUidMapping(uid_t uid, const struct wbcDomainSid *sid)
374 struct winbindd_request request;
375 struct winbindd_response response;
376 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
377 char *sid_string = NULL;
379 if (!sid) {
380 return WBC_ERR_INVALID_PARAM;
383 /* Initialise request */
385 ZERO_STRUCT(request);
386 ZERO_STRUCT(response);
388 /* Make request */
390 request.data.dual_idmapset.id = uid;
391 request.data.dual_idmapset.type = _ID_TYPE_UID;
393 wbc_status = wbcSidToString(sid, &sid_string);
394 BAIL_ON_WBC_ERROR(wbc_status);
396 strncpy(request.data.dual_idmapset.sid, sid_string,
397 sizeof(request.data.dual_idmapset.sid)-1);
398 wbcFreeMemory(sid_string);
400 wbc_status = wbcRequestResponse(WINBINDD_REMOVE_MAPPING,
401 &request, &response);
402 BAIL_ON_WBC_ERROR(wbc_status);
404 done:
405 return wbc_status;
408 /** @brief Remove a group id mapping
410 * @param gid Gid of the mapping to remove.
411 * @param *sid Pointer to the sid of the mapping to remove.
413 * @return #wbcErr
415 wbcErr wbcRemoveGidMapping(gid_t gid, const struct wbcDomainSid *sid)
417 struct winbindd_request request;
418 struct winbindd_response response;
419 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
420 char *sid_string = NULL;
422 if (!sid) {
423 return WBC_ERR_INVALID_PARAM;
426 /* Initialise request */
428 ZERO_STRUCT(request);
429 ZERO_STRUCT(response);
431 /* Make request */
433 request.data.dual_idmapset.id = gid;
434 request.data.dual_idmapset.type = _ID_TYPE_GID;
436 wbc_status = wbcSidToString(sid, &sid_string);
437 BAIL_ON_WBC_ERROR(wbc_status);
439 strncpy(request.data.dual_idmapset.sid, sid_string,
440 sizeof(request.data.dual_idmapset.sid)-1);
441 wbcFreeMemory(sid_string);
443 wbc_status = wbcRequestResponse(WINBINDD_REMOVE_MAPPING,
444 &request, &response);
445 BAIL_ON_WBC_ERROR(wbc_status);
447 done:
448 return wbc_status;
451 /** @brief Set the highwater mark for allocated uids.
453 * @param uid_hwm The new uid highwater mark value
455 * @return #wbcErr
457 wbcErr wbcSetUidHwm(uid_t uid_hwm)
459 struct winbindd_request request;
460 struct winbindd_response response;
461 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
463 /* Initialise request */
465 ZERO_STRUCT(request);
466 ZERO_STRUCT(response);
468 /* Make request */
470 request.data.dual_idmapset.id = uid_hwm;
471 request.data.dual_idmapset.type = _ID_TYPE_UID;
473 wbc_status = wbcRequestResponse(WINBINDD_SET_HWM,
474 &request, &response);
475 BAIL_ON_WBC_ERROR(wbc_status);
477 done:
478 return wbc_status;
481 /** @brief Set the highwater mark for allocated gids.
483 * @param gid_hwm The new gid highwater mark value
485 * @return #wbcErr
487 wbcErr wbcSetGidHwm(gid_t gid_hwm)
489 struct winbindd_request request;
490 struct winbindd_response response;
491 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
493 /* Initialise request */
495 ZERO_STRUCT(request);
496 ZERO_STRUCT(response);
498 /* Make request */
500 request.data.dual_idmapset.id = gid_hwm;
501 request.data.dual_idmapset.type = _ID_TYPE_GID;
503 wbc_status = wbcRequestResponse(WINBINDD_SET_HWM,
504 &request, &response);
505 BAIL_ON_WBC_ERROR(wbc_status);
507 done:
508 return wbc_status;