2 Unix SMB/CIFS implementation.
6 Copyright (C) Gerald (Jerry) Carter 2007
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.
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.
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/>.
22 /* 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
;
36 wbc_status
= WBC_ERR_INVALID_PARAM
;
37 BAIL_ON_WBC_ERROR(wbc_status
);
40 /* Initialize 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
);
53 wbc_status
= wbcRequestResponse(WINBINDD_SID_TO_UID
,
56 BAIL_ON_WBC_ERROR(wbc_status
);
58 *puid
= response
.data
.uid
;
60 wbc_status
= WBC_ERR_SUCCESS
;
66 /* Convert a Windows SID to a Unix uid if there already is a mapping */
67 wbcErr
wbcQuerySidToUid(const struct wbcDomainSid
*sid
,
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
;
81 wbc_status
= WBC_ERR_INVALID_PARAM
;
82 BAIL_ON_WBC_ERROR(wbc_status
);
85 /* Initialize request */
88 ZERO_STRUCT(response
);
90 request
.data
.uid
= uid
;
94 wbc_status
= wbcRequestResponse(WINBINDD_UID_TO_SID
,
97 BAIL_ON_WBC_ERROR(wbc_status
);
99 wbc_status
= wbcStringToSid(response
.data
.sid
.sid
, sid
);
100 BAIL_ON_WBC_ERROR(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
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
;
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
);
147 wbc_status
= wbcRequestResponse(WINBINDD_SID_TO_GID
,
150 BAIL_ON_WBC_ERROR(wbc_status
);
152 *pgid
= response
.data
.gid
;
154 wbc_status
= WBC_ERR_SUCCESS
;
161 /* Convert a Windows SID to a Unix gid if there already is a mapping */
163 wbcErr
wbcQuerySidToGid(const struct wbcDomainSid
*sid
,
166 return WBC_ERR_NOT_IMPLEMENTED
;
170 /* Convert a Unix gid to a Windows SID, allocating a SID if needed */
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 /* Convert a Unix gid to a Windows SID if there already is a mapping */
204 wbcErr
wbcQueryGidToSid(gid_t gid
,
205 struct wbcDomainSid
*sid
)
207 return WBC_ERR_NOT_IMPLEMENTED
;
210 /* Obtain a new uid from Winbind */
211 wbcErr
wbcAllocateUid(uid_t
*puid
)
213 struct winbindd_request request
;
214 struct winbindd_response response
;
215 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
218 return WBC_ERR_INVALID_PARAM
;
220 /* Initialise request */
222 ZERO_STRUCT(request
);
223 ZERO_STRUCT(response
);
227 wbc_status
= wbcRequestResponse(WINBINDD_ALLOCATE_UID
,
228 &request
, &response
);
229 BAIL_ON_WBC_ERROR(wbc_status
);
231 /* Copy out result */
232 *puid
= response
.data
.uid
;
234 wbc_status
= WBC_ERR_SUCCESS
;
240 /* Obtain a new gid from Winbind */
241 wbcErr
wbcAllocateGid(gid_t
*pgid
)
243 struct winbindd_request request
;
244 struct winbindd_response response
;
245 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
248 return WBC_ERR_INVALID_PARAM
;
250 /* Initialise request */
252 ZERO_STRUCT(request
);
253 ZERO_STRUCT(response
);
257 wbc_status
= wbcRequestResponse(WINBINDD_ALLOCATE_GID
,
258 &request
, &response
);
259 BAIL_ON_WBC_ERROR(wbc_status
);
261 /* Copy out result */
262 *pgid
= response
.data
.gid
;
264 wbc_status
= WBC_ERR_SUCCESS
;
270 /* we can't include smb.h here... */
271 #define _ID_TYPE_UID 1
272 #define _ID_TYPE_GID 2
274 /* Set an user id mapping */
275 wbcErr
wbcSetUidMapping(uid_t uid
, const struct wbcDomainSid
*sid
)
277 struct winbindd_request request
;
278 struct winbindd_response response
;
279 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
280 char *sid_string
= NULL
;
283 return WBC_ERR_INVALID_PARAM
;
286 /* Initialise request */
288 ZERO_STRUCT(request
);
289 ZERO_STRUCT(response
);
293 request
.data
.dual_idmapset
.id
= uid
;
294 request
.data
.dual_idmapset
.type
= _ID_TYPE_UID
;
296 wbc_status
= wbcSidToString(sid
, &sid_string
);
297 BAIL_ON_WBC_ERROR(wbc_status
);
299 strncpy(request
.data
.dual_idmapset
.sid
, sid_string
,
300 sizeof(request
.data
.dual_idmapset
.sid
)-1);
301 wbcFreeMemory(sid_string
);
303 wbc_status
= wbcRequestResponse(WINBINDD_SET_MAPPING
,
304 &request
, &response
);
305 BAIL_ON_WBC_ERROR(wbc_status
);
311 /* Set a group id mapping */
312 wbcErr
wbcSetGidMapping(gid_t gid
, const struct wbcDomainSid
*sid
)
314 struct winbindd_request request
;
315 struct winbindd_response response
;
316 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
317 char *sid_string
= NULL
;
320 return WBC_ERR_INVALID_PARAM
;
323 /* Initialise request */
325 ZERO_STRUCT(request
);
326 ZERO_STRUCT(response
);
330 request
.data
.dual_idmapset
.id
= gid
;
331 request
.data
.dual_idmapset
.type
= _ID_TYPE_GID
;
333 wbc_status
= wbcSidToString(sid
, &sid_string
);
334 BAIL_ON_WBC_ERROR(wbc_status
);
336 strncpy(request
.data
.dual_idmapset
.sid
, sid_string
,
337 sizeof(request
.data
.dual_idmapset
.sid
)-1);
338 wbcFreeMemory(sid_string
);
340 wbc_status
= wbcRequestResponse(WINBINDD_SET_MAPPING
,
341 &request
, &response
);
342 BAIL_ON_WBC_ERROR(wbc_status
);
348 /* Remove a user id mapping */
349 wbcErr
wbcRemoveUidMapping(uid_t uid
, const struct wbcDomainSid
*sid
)
351 struct winbindd_request request
;
352 struct winbindd_response response
;
353 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
354 char *sid_string
= NULL
;
357 return WBC_ERR_INVALID_PARAM
;
360 /* Initialise request */
362 ZERO_STRUCT(request
);
363 ZERO_STRUCT(response
);
367 request
.data
.dual_idmapset
.id
= uid
;
368 request
.data
.dual_idmapset
.type
= _ID_TYPE_UID
;
370 wbc_status
= wbcSidToString(sid
, &sid_string
);
371 BAIL_ON_WBC_ERROR(wbc_status
);
373 strncpy(request
.data
.dual_idmapset
.sid
, sid_string
,
374 sizeof(request
.data
.dual_idmapset
.sid
)-1);
375 wbcFreeMemory(sid_string
);
377 wbc_status
= wbcRequestResponse(WINBINDD_REMOVE_MAPPING
,
378 &request
, &response
);
379 BAIL_ON_WBC_ERROR(wbc_status
);
385 /* Remove a group id mapping */
386 wbcErr
wbcRemoveGidMapping(gid_t gid
, const struct wbcDomainSid
*sid
)
388 struct winbindd_request request
;
389 struct winbindd_response response
;
390 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
391 char *sid_string
= NULL
;
394 return WBC_ERR_INVALID_PARAM
;
397 /* Initialise request */
399 ZERO_STRUCT(request
);
400 ZERO_STRUCT(response
);
404 request
.data
.dual_idmapset
.id
= gid
;
405 request
.data
.dual_idmapset
.type
= _ID_TYPE_GID
;
407 wbc_status
= wbcSidToString(sid
, &sid_string
);
408 BAIL_ON_WBC_ERROR(wbc_status
);
410 strncpy(request
.data
.dual_idmapset
.sid
, sid_string
,
411 sizeof(request
.data
.dual_idmapset
.sid
)-1);
412 wbcFreeMemory(sid_string
);
414 wbc_status
= wbcRequestResponse(WINBINDD_REMOVE_MAPPING
,
415 &request
, &response
);
416 BAIL_ON_WBC_ERROR(wbc_status
);
422 /* Set the highwater mark for allocated uids. */
423 wbcErr
wbcSetUidHwm(uid_t uid_hwm
)
425 struct winbindd_request request
;
426 struct winbindd_response response
;
427 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
429 /* Initialise request */
431 ZERO_STRUCT(request
);
432 ZERO_STRUCT(response
);
436 request
.data
.dual_idmapset
.id
= uid_hwm
;
437 request
.data
.dual_idmapset
.type
= _ID_TYPE_UID
;
439 wbc_status
= wbcRequestResponse(WINBINDD_SET_HWM
,
440 &request
, &response
);
441 BAIL_ON_WBC_ERROR(wbc_status
);
447 /* Set the highwater mark for allocated gids. */
448 wbcErr
wbcSetGidHwm(gid_t gid_hwm
)
450 struct winbindd_request request
;
451 struct winbindd_response response
;
452 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
454 /* Initialise request */
456 ZERO_STRUCT(request
);
457 ZERO_STRUCT(response
);
461 request
.data
.dual_idmapset
.id
= gid_hwm
;
462 request
.data
.dual_idmapset
.type
= _ID_TYPE_GID
;
464 wbc_status
= wbcRequestResponse(WINBINDD_SET_HWM
,
465 &request
, &response
);
466 BAIL_ON_WBC_ERROR(wbc_status
);