2 Unix SMB/CIFS implementation.
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
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
;
44 wbc_status
= WBC_ERR_INVALID_PARAM
;
45 BAIL_ON_WBC_ERROR(wbc_status
);
48 /* Initialize 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
);
61 wbc_status
= wbcRequestResponse(WINBINDD_SID_TO_UID
,
64 BAIL_ON_WBC_ERROR(wbc_status
);
66 *puid
= response
.data
.uid
;
68 wbc_status
= WBC_ERR_SUCCESS
;
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
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
;
90 wbc_status
= WBC_ERR_INVALID_PARAM
;
91 BAIL_ON_WBC_ERROR(wbc_status
);
94 /* Initialize request */
97 ZERO_STRUCT(response
);
99 request
.data
.uid
= uid
;
103 wbc_status
= wbcRequestResponse(WINBINDD_UID_TO_SID
,
106 BAIL_ON_WBC_ERROR(wbc_status
);
108 wbc_status
= wbcStringToSid(response
.data
.sid
.sid
, sid
);
109 BAIL_ON_WBC_ERROR(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
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
;
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
);
149 wbc_status
= wbcRequestResponse(WINBINDD_SID_TO_GID
,
152 BAIL_ON_WBC_ERROR(wbc_status
);
154 *pgid
= response
.data
.gid
;
156 wbc_status
= WBC_ERR_SUCCESS
;
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
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
;
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
;
191 wbc_status
= wbcRequestResponse(WINBINDD_GID_TO_SID
,
194 BAIL_ON_WBC_ERROR(wbc_status
);
196 wbc_status
= wbcStringToSid(response
.data
.sid
.sid
, sid
);
197 BAIL_ON_WBC_ERROR(wbc_status
);
203 /** @brief Obtain a new uid from Winbind
205 * @param *puid *pointer to the allocated uid
210 wbcErr
wbcAllocateUid(uid_t
*puid
)
212 struct winbindd_request request
;
213 struct winbindd_response response
;
214 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
217 return WBC_ERR_INVALID_PARAM
;
219 /* Initialise request */
221 ZERO_STRUCT(request
);
222 ZERO_STRUCT(response
);
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
;
239 /** @brief Obtain a new gid from Winbind
241 * @param *pgid Pointer to the allocated gid
246 wbcErr
wbcAllocateGid(gid_t
*pgid
)
248 struct winbindd_request request
;
249 struct winbindd_response response
;
250 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
253 return WBC_ERR_INVALID_PARAM
;
255 /* Initialise request */
257 ZERO_STRUCT(request
);
258 ZERO_STRUCT(response
);
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
;
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.
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
;
294 return WBC_ERR_INVALID_PARAM
;
297 /* Initialise request */
299 ZERO_STRUCT(request
);
300 ZERO_STRUCT(response
);
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
);
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.
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
;
337 return WBC_ERR_INVALID_PARAM
;
340 /* Initialise request */
342 ZERO_STRUCT(request
);
343 ZERO_STRUCT(response
);
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
);
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.
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
;
380 return WBC_ERR_INVALID_PARAM
;
383 /* Initialise request */
385 ZERO_STRUCT(request
);
386 ZERO_STRUCT(response
);
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
);
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.
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
;
423 return WBC_ERR_INVALID_PARAM
;
426 /* Initialise request */
428 ZERO_STRUCT(request
);
429 ZERO_STRUCT(response
);
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
);
451 /** @brief Set the highwater mark for allocated uids.
453 * @param uid_hwm The new uid highwater mark value
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
);
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
);
481 /** @brief Set the highwater mark for allocated gids.
483 * @param gid_hwm The new gid highwater mark value
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
);
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
);