source: branches/samba-3.2.x/source/nsswitch/libwbclient/wbc_idmap.c

Last change on this file was 133, checked in by Paul Smedley, 17 years ago

Update trunk to 3.2.0pre3

File size: 8.8 KB
Line 
1/*
2 Unix SMB/CIFS implementation.
3
4 Winbind client API
5
6 Copyright (C) Gerald (Jerry) Carter 2007
7
8
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.
13
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.
18
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/>.
21*/
22
23/* Required Headers */
24
25#include "libwbclient.h"
26
27/** @brief Convert a Windows SID to a Unix uid
28 *
29 * @param *sid Pointer to the domain SID to be resolved
30 * @param *puid Pointer to the resolved uid_t value
31 *
32 * @return #wbcErr
33 *
34 **/
35
36wbcErr wbcSidToUid(const struct wbcDomainSid *sid, uid_t *puid)
37{
38 struct winbindd_request request;
39 struct winbindd_response response;
40 char *sid_string = NULL;
41 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
42
43 if (!sid || !puid) {
44 wbc_status = WBC_ERR_INVALID_PARAM;
45 BAIL_ON_WBC_ERROR(wbc_status);
46 }
47
48 /* Initialize request */
49
50 ZERO_STRUCT(request);
51 ZERO_STRUCT(response);
52
53 wbc_status = wbcSidToString(sid, &sid_string);
54 BAIL_ON_WBC_ERROR(wbc_status);
55
56 strncpy(request.data.sid, sid_string, sizeof(request.data.sid)-1);
57 wbcFreeMemory(sid_string);
58
59 /* Make request */
60
61 wbc_status = wbcRequestResponse(WINBINDD_SID_TO_UID,
62 &request,
63 &response);
64 BAIL_ON_WBC_ERROR(wbc_status);
65
66 *puid = response.data.uid;
67
68 wbc_status = WBC_ERR_SUCCESS;
69
70 done:
71 return wbc_status;
72}
73
74/** @brief Convert a Unix uid to a Windows SID
75 *
76 * @param uid Unix uid to be resolved
77 * @param *sid Pointer to the resolved domain SID
78 *
79 * @return #wbcErr
80 *
81 **/
82
83wbcErr wbcUidToSid(uid_t uid, struct wbcDomainSid *sid)
84{
85 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
86 struct winbindd_request request;
87 struct winbindd_response response;
88
89 if (!sid) {
90 wbc_status = WBC_ERR_INVALID_PARAM;
91 BAIL_ON_WBC_ERROR(wbc_status);
92 }
93
94 /* Initialize request */
95
96 ZERO_STRUCT(request);
97 ZERO_STRUCT(response);
98
99 request.data.uid = uid;
100
101 /* Make request */
102
103 wbc_status = wbcRequestResponse(WINBINDD_UID_TO_SID,
104 &request,
105 &response);
106 BAIL_ON_WBC_ERROR(wbc_status);
107
108 wbc_status = wbcStringToSid(response.data.sid.sid, sid);
109 BAIL_ON_WBC_ERROR(wbc_status);
110
111done:
112 return wbc_status;
113}
114
115/** @brief Convert a Windows SID to a Unix gid
116 *
117 * @param *sid Pointer to the domain SID to be resolved
118 * @param *pgid Pointer to the resolved gid_t value
119 *
120 * @return #wbcErr
121 *
122 **/
123
124wbcErr wbcSidToGid(const struct wbcDomainSid *sid, gid_t *pgid)
125{
126 struct winbindd_request request;
127 struct winbindd_response response;
128 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
129 char *sid_string = NULL;
130
131 if (!sid || !pgid) {
132 wbc_status = WBC_ERR_INVALID_PARAM;
133 BAIL_ON_WBC_ERROR(wbc_status);
134 }
135
136 /* Initialize request */
137
138 ZERO_STRUCT(request);
139 ZERO_STRUCT(response);
140
141 wbc_status = wbcSidToString(sid, &sid_string);
142 BAIL_ON_WBC_ERROR(wbc_status);
143
144 strncpy(request.data.sid, sid_string, sizeof(request.data.sid)-1);
145 wbcFreeMemory(sid_string);
146
147 /* Make request */
148
149 wbc_status = wbcRequestResponse(WINBINDD_SID_TO_GID,
150 &request,
151 &response);
152 BAIL_ON_WBC_ERROR(wbc_status);
153
154 *pgid = response.data.gid;
155
156 wbc_status = WBC_ERR_SUCCESS;
157
158 done:
159 return wbc_status;
160}
161
162/** @brief Convert a Unix uid to a Windows SID
163 *
164 * @param gid Unix gid to be resolved
165 * @param *sid Pointer to the resolved domain SID
166 *
167 * @return #wbcErr
168 *
169 **/
170
171wbcErr 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
199done:
200 return wbc_status;
201}
202
203/** @brief Obtain a new uid from Winbind
204 *
205 * @param *puid *pointer to the allocated uid
206 *
207 * @return #wbcErr
208 **/
209
210wbcErr wbcAllocateUid(uid_t *puid)
211{
212 struct winbindd_request request;
213 struct winbindd_response response;
214 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
215
216 if (!puid)
217 return WBC_ERR_INVALID_PARAM;
218
219 /* Initialise request */
220
221 ZERO_STRUCT(request);
222 ZERO_STRUCT(response);
223
224 /* Make request */
225
226 wbc_status = wbcRequestResponse(WINBINDD_ALLOCATE_UID,
227 &request, &response);
228 BAIL_ON_WBC_ERROR(wbc_status);
229
230 /* Copy out result */
231 *puid = response.data.uid;
232
233 wbc_status = WBC_ERR_SUCCESS;
234
235 done:
236 return wbc_status;
237}
238
239/** @brief Obtain a new gid from Winbind
240 *
241 * @param *pgid Pointer to the allocated gid
242 *
243 * @return #wbcErr
244 **/
245
246wbcErr wbcAllocateGid(gid_t *pgid)
247{
248 struct winbindd_request request;
249 struct winbindd_response response;
250 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
251
252 if (!pgid)
253 return WBC_ERR_INVALID_PARAM;
254
255 /* Initialise request */
256
257 ZERO_STRUCT(request);
258 ZERO_STRUCT(response);
259
260 /* Make request */
261
262 wbc_status = wbcRequestResponse(WINBINDD_ALLOCATE_GID,
263 &request, &response);
264 BAIL_ON_WBC_ERROR(wbc_status);
265
266 /* Copy out result */
267 *pgid = response.data.gid;
268
269 wbc_status = WBC_ERR_SUCCESS;
270
271 done:
272 return wbc_status;
273}
274
275/* we can't include smb.h here... */
276#define _ID_TYPE_UID 1
277#define _ID_TYPE_GID 2
278
279/** @brief Set an user id mapping
280 *
281 * @param uid Uid of the desired mapping.
282 * @param *sid Pointer to the sid of the diresired mapping.
283 *
284 * @return #wbcErr
285 **/
286wbcErr wbcSetUidMapping(uid_t uid, const struct wbcDomainSid *sid)
287{
288 struct winbindd_request request;
289 struct winbindd_response response;
290 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
291 char *sid_string = NULL;
292
293 if (!sid) {
294 return WBC_ERR_INVALID_PARAM;
295 }
296
297 /* Initialise request */
298
299 ZERO_STRUCT(request);
300 ZERO_STRUCT(response);
301
302 /* Make request */
303
304 request.data.dual_idmapset.id = uid;
305 request.data.dual_idmapset.type = _ID_TYPE_UID;
306
307 wbc_status = wbcSidToString(sid, &sid_string);
308 BAIL_ON_WBC_ERROR(wbc_status);
309
310 strncpy(request.data.dual_idmapset.sid, sid_string,
311 sizeof(request.data.dual_idmapset.sid)-1);
312 wbcFreeMemory(sid_string);
313
314 wbc_status = wbcRequestResponse(WINBINDD_SET_MAPPING,
315 &request, &response);
316 BAIL_ON_WBC_ERROR(wbc_status);
317
318 done:
319 return wbc_status;
320}
321
322/** @brief Set a group id mapping
323 *
324 * @param gid Gid of the desired mapping.
325 * @param *sid Pointer to the sid of the diresired mapping.
326 *
327 * @return #wbcErr
328 **/
329wbcErr wbcSetGidMapping(gid_t gid, const struct wbcDomainSid *sid)
330{
331 struct winbindd_request request;
332 struct winbindd_response response;
333 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
334 char *sid_string = NULL;
335
336 if (!sid) {
337 return WBC_ERR_INVALID_PARAM;
338 }
339
340 /* Initialise request */
341
342 ZERO_STRUCT(request);
343 ZERO_STRUCT(response);
344
345 /* Make request */
346
347 request.data.dual_idmapset.id = gid;
348 request.data.dual_idmapset.type = _ID_TYPE_GID;
349
350 wbc_status = wbcSidToString(sid, &sid_string);
351 BAIL_ON_WBC_ERROR(wbc_status);
352
353 strncpy(request.data.dual_idmapset.sid, sid_string,
354 sizeof(request.data.dual_idmapset.sid)-1);
355 wbcFreeMemory(sid_string);
356
357 wbc_status = wbcRequestResponse(WINBINDD_SET_MAPPING,
358 &request, &response);
359 BAIL_ON_WBC_ERROR(wbc_status);
360
361 done:
362 return wbc_status;
363}
364
365/** @brief Set the highwater mark for allocated uids.
366 *
367 * @param uid_hwm The new uid highwater mark value
368 *
369 * @return #wbcErr
370 **/
371wbcErr wbcSetUidHwm(uid_t uid_hwm)
372{
373 struct winbindd_request request;
374 struct winbindd_response response;
375 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
376
377 /* Initialise request */
378
379 ZERO_STRUCT(request);
380 ZERO_STRUCT(response);
381
382 /* Make request */
383
384 request.data.dual_idmapset.id = uid_hwm;
385 request.data.dual_idmapset.type = _ID_TYPE_UID;
386
387 wbc_status = wbcRequestResponse(WINBINDD_SET_HWM,
388 &request, &response);
389 BAIL_ON_WBC_ERROR(wbc_status);
390
391 done:
392 return wbc_status;
393}
394
395/** @brief Set the highwater mark for allocated gids.
396 *
397 * @param uid_hwm The new gid highwater mark value
398 *
399 * @return #wbcErr
400 **/
401wbcErr wbcSetGidHwm(gid_t gid_hwm)
402{
403 struct winbindd_request request;
404 struct winbindd_response response;
405 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
406
407 /* Initialise request */
408
409 ZERO_STRUCT(request);
410 ZERO_STRUCT(response);
411
412 /* Make request */
413
414 request.data.dual_idmapset.id = gid_hwm;
415 request.data.dual_idmapset.type = _ID_TYPE_GID;
416
417 wbc_status = wbcRequestResponse(WINBINDD_SET_HWM,
418 &request, &response);
419 BAIL_ON_WBC_ERROR(wbc_status);
420
421 done:
422 return wbc_status;
423}
Note: See TracBrowser for help on using the repository browser.