docs: Remove comment about default backend.
[kai/samba-autobuild/.git] / source3 / nsswitch / libwbclient / wbc_idmap.c
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 /* Convert a Windows SID to a Unix uid, allocating an uid if needed */
28 wbcErr wbcSidToUid(const struct wbcDomainSid *sid, uid_t *puid)
29 {
30         struct winbindd_request request;
31         struct winbindd_response response;
32         char *sid_string = NULL;
33         wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
34
35         if (!sid || !puid) {
36                 wbc_status = WBC_ERR_INVALID_PARAM;
37                 BAIL_ON_WBC_ERROR(wbc_status);
38         }
39
40         /* Initialize request */
41
42         ZERO_STRUCT(request);
43         ZERO_STRUCT(response);
44
45         wbc_status = wbcSidToString(sid, &sid_string);
46         BAIL_ON_WBC_ERROR(wbc_status);
47
48         strncpy(request.data.sid, sid_string, sizeof(request.data.sid)-1);
49         wbcFreeMemory(sid_string);
50
51         /* Make request */
52
53         wbc_status = wbcRequestResponse(WINBINDD_SID_TO_UID,
54                                         &request,
55                                         &response);
56         BAIL_ON_WBC_ERROR(wbc_status);
57
58         *puid = response.data.uid;
59
60         wbc_status = WBC_ERR_SUCCESS;
61
62  done:
63         return wbc_status;
64 }
65
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)
69 {
70         return WBC_ERR_NOT_IMPLEMENTED;
71 }
72
73 /* Convert a Unix uid to a Windows SID, allocating a SID if needed */
74 wbcErr wbcUidToSid(uid_t uid, struct wbcDomainSid *sid)
75 {
76         wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
77         struct winbindd_request request;
78         struct winbindd_response response;
79
80         if (!sid) {
81                 wbc_status = WBC_ERR_INVALID_PARAM;
82                 BAIL_ON_WBC_ERROR(wbc_status);
83         }
84
85         /* Initialize request */
86
87         ZERO_STRUCT(request);
88         ZERO_STRUCT(response);
89
90         request.data.uid = uid;
91
92         /* Make request */
93
94         wbc_status = wbcRequestResponse(WINBINDD_UID_TO_SID,
95                                         &request,
96                                         &response);
97         BAIL_ON_WBC_ERROR(wbc_status);
98
99         wbc_status = wbcStringToSid(response.data.sid.sid, sid);
100         BAIL_ON_WBC_ERROR(wbc_status);
101
102 done:
103         return wbc_status;
104 }
105
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)
109 {
110         return WBC_ERR_NOT_IMPLEMENTED;
111 }
112
113 /** @brief Convert a Windows SID to a Unix gid, allocating a gid if needed
114  *
115  * @param *sid        Pointer to the domain SID to be resolved
116  * @param *pgid       Pointer to the resolved gid_t value
117  *
118  * @return #wbcErr
119  *
120  **/
121
122 wbcErr wbcSidToGid(const struct wbcDomainSid *sid, gid_t *pgid)
123 {
124         struct winbindd_request request;
125         struct winbindd_response response;
126         wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
127         char *sid_string = NULL;
128
129         if (!sid || !pgid) {
130                 wbc_status = WBC_ERR_INVALID_PARAM;
131                 BAIL_ON_WBC_ERROR(wbc_status);
132         }
133
134         /* Initialize request */
135
136         ZERO_STRUCT(request);
137         ZERO_STRUCT(response);
138
139         wbc_status = wbcSidToString(sid, &sid_string);
140         BAIL_ON_WBC_ERROR(wbc_status);
141
142         strncpy(request.data.sid, sid_string, sizeof(request.data.sid)-1);
143         wbcFreeMemory(sid_string);
144
145         /* Make request */
146
147         wbc_status = wbcRequestResponse(WINBINDD_SID_TO_GID,
148                                         &request,
149                                         &response);
150         BAIL_ON_WBC_ERROR(wbc_status);
151
152         *pgid = response.data.gid;
153
154         wbc_status = WBC_ERR_SUCCESS;
155
156  done:
157         return wbc_status;
158 }
159
160 /* Convert a Windows SID to a Unix gid if there already is a mapping */
161
162 wbcErr wbcQuerySidToGid(const struct wbcDomainSid *sid,
163                         gid_t *pgid)
164 {
165         return WBC_ERR_NOT_IMPLEMENTED;
166 }
167
168 /* Convert a Unix gid to a Windows SID, allocating a SID if needed */
169 wbcErr wbcGidToSid(gid_t gid, struct wbcDomainSid *sid)
170 {
171         struct winbindd_request request;
172         struct winbindd_response response;
173         wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
174
175         if (!sid) {
176                 wbc_status = WBC_ERR_INVALID_PARAM;
177                 BAIL_ON_WBC_ERROR(wbc_status);
178         }
179
180         /* Initialize request */
181
182         ZERO_STRUCT(request);
183         ZERO_STRUCT(response);
184
185         request.data.gid = gid;
186
187         /* Make request */
188
189         wbc_status = wbcRequestResponse(WINBINDD_GID_TO_SID,
190                                         &request,
191                                         &response);
192         BAIL_ON_WBC_ERROR(wbc_status);
193
194         wbc_status = wbcStringToSid(response.data.sid.sid, sid);
195         BAIL_ON_WBC_ERROR(wbc_status);
196
197 done:
198         return wbc_status;
199 }
200
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)
204 {
205         return WBC_ERR_NOT_IMPLEMENTED;
206 }
207
208 /* Obtain a new uid from Winbind */
209 wbcErr wbcAllocateUid(uid_t *puid)
210 {
211         struct winbindd_request request;
212         struct winbindd_response response;
213         wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
214
215         if (!puid)
216                 return WBC_ERR_INVALID_PARAM;
217
218         /* Initialise request */
219
220         ZERO_STRUCT(request);
221         ZERO_STRUCT(response);
222
223         /* Make request */
224
225         wbc_status = wbcRequestResponse(WINBINDD_ALLOCATE_UID,
226                                            &request, &response);
227         BAIL_ON_WBC_ERROR(wbc_status);
228
229         /* Copy out result */
230         *puid = response.data.uid;
231
232         wbc_status = WBC_ERR_SUCCESS;
233
234  done:
235         return wbc_status;
236 }
237
238 /* Obtain a new gid from Winbind */
239 wbcErr wbcAllocateGid(gid_t *pgid)
240 {
241         struct winbindd_request request;
242         struct winbindd_response response;
243         wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
244
245         if (!pgid)
246                 return WBC_ERR_INVALID_PARAM;
247
248         /* Initialise request */
249
250         ZERO_STRUCT(request);
251         ZERO_STRUCT(response);
252
253         /* Make request */
254
255         wbc_status = wbcRequestResponse(WINBINDD_ALLOCATE_GID,
256                                            &request, &response);
257         BAIL_ON_WBC_ERROR(wbc_status);
258
259         /* Copy out result */
260         *pgid = response.data.gid;
261
262         wbc_status = WBC_ERR_SUCCESS;
263
264  done:
265         return wbc_status;
266 }
267
268 /* we can't include smb.h here... */
269 #define _ID_TYPE_UID 1
270 #define _ID_TYPE_GID 2
271
272 /* Set an user id mapping */
273 wbcErr wbcSetUidMapping(uid_t uid, const struct wbcDomainSid *sid)
274 {
275         struct winbindd_request request;
276         struct winbindd_response response;
277         wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
278         char *sid_string = NULL;
279
280         if (!sid) {
281                 return WBC_ERR_INVALID_PARAM;
282         }
283
284         /* Initialise request */
285
286         ZERO_STRUCT(request);
287         ZERO_STRUCT(response);
288
289         /* Make request */
290
291         request.data.dual_idmapset.id = uid;
292         request.data.dual_idmapset.type = _ID_TYPE_UID;
293
294         wbc_status = wbcSidToString(sid, &sid_string);
295         BAIL_ON_WBC_ERROR(wbc_status);
296
297         strncpy(request.data.dual_idmapset.sid, sid_string,
298                 sizeof(request.data.dual_idmapset.sid)-1);
299         wbcFreeMemory(sid_string);
300
301         wbc_status = wbcRequestResponse(WINBINDD_SET_MAPPING,
302                                         &request, &response);
303         BAIL_ON_WBC_ERROR(wbc_status);
304
305  done:
306         return wbc_status;
307 }
308
309 /* Set a group id mapping */
310 wbcErr wbcSetGidMapping(gid_t gid, const struct wbcDomainSid *sid)
311 {
312         struct winbindd_request request;
313         struct winbindd_response response;
314         wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
315         char *sid_string = NULL;
316
317         if (!sid) {
318                 return WBC_ERR_INVALID_PARAM;
319         }
320
321         /* Initialise request */
322
323         ZERO_STRUCT(request);
324         ZERO_STRUCT(response);
325
326         /* Make request */
327
328         request.data.dual_idmapset.id = gid;
329         request.data.dual_idmapset.type = _ID_TYPE_GID;
330
331         wbc_status = wbcSidToString(sid, &sid_string);
332         BAIL_ON_WBC_ERROR(wbc_status);
333
334         strncpy(request.data.dual_idmapset.sid, sid_string,
335                 sizeof(request.data.dual_idmapset.sid)-1);
336         wbcFreeMemory(sid_string);
337
338         wbc_status = wbcRequestResponse(WINBINDD_SET_MAPPING,
339                                         &request, &response);
340         BAIL_ON_WBC_ERROR(wbc_status);
341
342  done:
343         return wbc_status;
344 }
345
346 /* Remove a user id mapping */
347 wbcErr wbcRemoveUidMapping(uid_t uid, const struct wbcDomainSid *sid)
348 {
349         struct winbindd_request request;
350         struct winbindd_response response;
351         wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
352         char *sid_string = NULL;
353
354         if (!sid) {
355                 return WBC_ERR_INVALID_PARAM;
356         }
357
358         /* Initialise request */
359
360         ZERO_STRUCT(request);
361         ZERO_STRUCT(response);
362
363         /* Make request */
364
365         request.data.dual_idmapset.id = uid;
366         request.data.dual_idmapset.type = _ID_TYPE_UID;
367
368         wbc_status = wbcSidToString(sid, &sid_string);
369         BAIL_ON_WBC_ERROR(wbc_status);
370
371         strncpy(request.data.dual_idmapset.sid, sid_string,
372                 sizeof(request.data.dual_idmapset.sid)-1);
373         wbcFreeMemory(sid_string);
374
375         wbc_status = wbcRequestResponse(WINBINDD_REMOVE_MAPPING,
376                                         &request, &response);
377         BAIL_ON_WBC_ERROR(wbc_status);
378
379  done:
380         return wbc_status;
381 }
382
383 /* Remove a group id mapping */
384 wbcErr wbcRemoveGidMapping(gid_t gid, const struct wbcDomainSid *sid)
385 {
386         struct winbindd_request request;
387         struct winbindd_response response;
388         wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
389         char *sid_string = NULL;
390
391         if (!sid) {
392                 return WBC_ERR_INVALID_PARAM;
393         }
394
395         /* Initialise request */
396
397         ZERO_STRUCT(request);
398         ZERO_STRUCT(response);
399
400         /* Make request */
401
402         request.data.dual_idmapset.id = gid;
403         request.data.dual_idmapset.type = _ID_TYPE_GID;
404
405         wbc_status = wbcSidToString(sid, &sid_string);
406         BAIL_ON_WBC_ERROR(wbc_status);
407
408         strncpy(request.data.dual_idmapset.sid, sid_string,
409                 sizeof(request.data.dual_idmapset.sid)-1);
410         wbcFreeMemory(sid_string);
411
412         wbc_status = wbcRequestResponse(WINBINDD_REMOVE_MAPPING,
413                                         &request, &response);
414         BAIL_ON_WBC_ERROR(wbc_status);
415
416  done:
417         return wbc_status;
418 }
419
420 /* Set the highwater mark for allocated uids. */
421 wbcErr wbcSetUidHwm(uid_t uid_hwm)
422 {
423         struct winbindd_request request;
424         struct winbindd_response response;
425         wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
426
427         /* Initialise request */
428
429         ZERO_STRUCT(request);
430         ZERO_STRUCT(response);
431
432         /* Make request */
433
434         request.data.dual_idmapset.id = uid_hwm;
435         request.data.dual_idmapset.type = _ID_TYPE_UID;
436
437         wbc_status = wbcRequestResponse(WINBINDD_SET_HWM,
438                                         &request, &response);
439         BAIL_ON_WBC_ERROR(wbc_status);
440
441  done:
442         return wbc_status;
443 }
444
445 /* Set the highwater mark for allocated gids. */
446 wbcErr wbcSetGidHwm(gid_t gid_hwm)
447 {
448         struct winbindd_request request;
449         struct winbindd_response response;
450         wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
451
452         /* Initialise request */
453
454         ZERO_STRUCT(request);
455         ZERO_STRUCT(response);
456
457         /* Make request */
458
459         request.data.dual_idmapset.id = gid_hwm;
460         request.data.dual_idmapset.type = _ID_TYPE_GID;
461
462         wbc_status = wbcRequestResponse(WINBINDD_SET_HWM,
463                                         &request, &response);
464         BAIL_ON_WBC_ERROR(wbc_status);
465
466  done:
467         return wbc_status;
468 }