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"
31 static struct passwd *copy_passwd_entry(struct winbindd_pw *p)
33 struct passwd *pwd = NULL;
34 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
36 pwd = talloc(NULL, struct passwd);
37 BAIL_ON_PTR_ERROR(pwd, wbc_status);
39 pwd->pw_name = talloc_strdup(pwd,p->pw_name);
40 BAIL_ON_PTR_ERROR(pwd->pw_name, wbc_status);
42 pwd->pw_passwd = talloc_strdup(pwd, p->pw_passwd);
43 BAIL_ON_PTR_ERROR(pwd->pw_passwd, wbc_status);
45 pwd->pw_gecos = talloc_strdup(pwd, p->pw_gecos);
46 BAIL_ON_PTR_ERROR(pwd->pw_gecos, wbc_status);
48 pwd->pw_shell = talloc_strdup(pwd, p->pw_shell);
49 BAIL_ON_PTR_ERROR(pwd->pw_shell, wbc_status);
51 pwd->pw_dir = talloc_strdup(pwd, p->pw_dir);
52 BAIL_ON_PTR_ERROR(pwd->pw_dir, wbc_status);
54 pwd->pw_uid = p->pw_uid;
55 pwd->pw_gid = p->pw_gid;
58 if (!WBC_ERROR_IS_OK(wbc_status)) {
70 static struct group *copy_group_entry(struct winbindd_gr *g,
73 struct group *grp = NULL;
74 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
78 grp = talloc(NULL, struct group);
79 BAIL_ON_PTR_ERROR(grp, wbc_status);
81 grp->gr_name = talloc_strdup(grp, g->gr_name);
82 BAIL_ON_PTR_ERROR(grp->gr_name, wbc_status);
84 grp->gr_passwd = talloc_strdup(grp, g->gr_passwd);
85 BAIL_ON_PTR_ERROR(grp->gr_passwd, wbc_status);
87 grp->gr_gid = g->gr_gid;
89 grp->gr_mem = talloc_array(grp, char*, g->num_gr_mem+1);
91 mem_p = mem_q = mem_buf;
92 for (i=0; i<g->num_gr_mem && mem_p; i++) {
93 if ((mem_q = strchr(mem_p, ',')) != NULL) {
97 grp->gr_mem[i] = talloc_strdup(grp, mem_p);
98 BAIL_ON_PTR_ERROR(grp->gr_mem[i], wbc_status);
106 grp->gr_mem[i] = NULL;
108 wbc_status = WBC_ERR_SUCCESS;
111 if (!WBC_ERROR_IS_OK(wbc_status)) {
119 /** @brief Fill in a struct passwd* for a domain user based
122 * @param *name Username to lookup
123 * @param **pwd Pointer to resulting struct passwd* from the query.
128 wbcErr wbcGetpwnam(const char *name, struct passwd **pwd)
130 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
131 struct winbindd_request request;
132 struct winbindd_response response;
135 wbc_status = WBC_ERR_INVALID_PARAM;
136 BAIL_ON_WBC_ERROR(wbc_status);
139 /* Initialize request */
141 ZERO_STRUCT(request);
142 ZERO_STRUCT(response);
144 /* dst is already null terminated from the memset above */
146 strncpy(request.data.username, name, sizeof(request.data.username)-1);
148 wbc_status = wbcRequestResponse(WINBINDD_GETPWNAM,
151 BAIL_ON_WBC_ERROR(wbc_status);
153 *pwd = copy_passwd_entry(&response.data.pw);
154 BAIL_ON_PTR_ERROR(*pwd, wbc_status);
160 /** @brief Fill in a struct passwd* for a domain user based
163 * @param uid Uid to lookup
164 * @param **pwd Pointer to resulting struct passwd* from the query.
169 wbcErr wbcGetpwuid(uid_t uid, struct passwd **pwd)
171 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
172 struct winbindd_request request;
173 struct winbindd_response response;
176 wbc_status = WBC_ERR_INVALID_PARAM;
177 BAIL_ON_WBC_ERROR(wbc_status);
180 /* Initialize request */
182 ZERO_STRUCT(request);
183 ZERO_STRUCT(response);
185 request.data.uid = uid;
187 wbc_status = wbcRequestResponse(WINBINDD_GETPWUID,
190 BAIL_ON_WBC_ERROR(wbc_status);
192 *pwd = copy_passwd_entry(&response.data.pw);
193 BAIL_ON_PTR_ERROR(*pwd, wbc_status);
199 /** @brief Fill in a struct passwd* for a domain user based
202 * @param *name Username to lookup
203 * @param **grp Pointer to resulting struct group* from the query.
208 wbcErr wbcGetgrnam(const char *name, struct group **grp)
210 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
211 struct winbindd_request request;
212 struct winbindd_response response;
214 /* Initialize request */
216 ZERO_STRUCT(request);
217 ZERO_STRUCT(response);
220 wbc_status = WBC_ERR_INVALID_PARAM;
221 BAIL_ON_WBC_ERROR(wbc_status);
224 /* dst is already null terminated from the memset above */
226 strncpy(request.data.groupname, name, sizeof(request.data.groupname)-1);
228 wbc_status = wbcRequestResponse(WINBINDD_GETGRNAM,
231 BAIL_ON_WBC_ERROR(wbc_status);
233 *grp = copy_group_entry(&response.data.gr,
234 (char*)response.extra_data.data);
235 BAIL_ON_PTR_ERROR(*grp, wbc_status);
238 if (response.extra_data.data)
239 free(response.extra_data.data);
244 /** @brief Fill in a struct passwd* for a domain user based
247 * @param gid Uid to lookup
248 * @param **grp Pointer to resulting struct group* from the query.
253 wbcErr wbcGetgrgid(gid_t gid, struct group **grp)
255 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
256 struct winbindd_request request;
257 struct winbindd_response response;
259 /* Initialize request */
261 ZERO_STRUCT(request);
262 ZERO_STRUCT(response);
265 wbc_status = WBC_ERR_INVALID_PARAM;
266 BAIL_ON_WBC_ERROR(wbc_status);
269 request.data.gid = gid;
271 wbc_status = wbcRequestResponse(WINBINDD_GETGRGID,
274 BAIL_ON_WBC_ERROR(wbc_status);
276 *grp = copy_group_entry(&response.data.gr,
277 (char*)response.extra_data.data);
278 BAIL_ON_PTR_ERROR(*grp, wbc_status);
281 if (response.extra_data.data)
282 free(response.extra_data.data);
287 /** @brief Reset the passwd iterator
292 wbcErr wbcSetpwent(void)
294 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
296 wbc_status = wbcRequestResponse(WINBINDD_SETPWENT,
298 BAIL_ON_WBC_ERROR(wbc_status);
304 /** @brief Close the passwd iterator
309 wbcErr wbcEndpwent(void)
311 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
313 wbc_status = wbcRequestResponse(WINBINDD_ENDPWENT,
315 BAIL_ON_WBC_ERROR(wbc_status);
321 /** @brief Return the next struct passwd* entry from the pwent iterator
323 * @param **pwd Pointer to resulting struct group* from the query.
328 wbcErr wbcGetpwent(struct passwd **pwd)
330 return WBC_ERR_NOT_IMPLEMENTED;
333 /** @brief Reset the group iterator
338 wbcErr wbcSetgrent(void)
340 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
342 wbc_status = wbcRequestResponse(WINBINDD_SETGRENT,
344 BAIL_ON_WBC_ERROR(wbc_status);
350 /** @brief Close the group iterator
355 wbcErr wbcEndgrent(void)
357 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
359 wbc_status = wbcRequestResponse(WINBINDD_ENDGRENT,
361 BAIL_ON_WBC_ERROR(wbc_status);
367 /** @brief Return the next struct passwd* entry from the pwent iterator
369 * @param **grp Pointer to resulting struct group* from the query.
374 wbcErr wbcGetgrent(struct group **grp)
376 return WBC_ERR_NOT_IMPLEMENTED;
379 /** @brief Return the next struct group* entry from the pwent iterator
381 * This is similar to #wbcGetgrent, just that the member list is empty
383 * @param **grp Pointer to resulting struct group* from the query.
388 wbcErr wbcGetgrlist(struct group **grp)
390 return WBC_ERR_NOT_IMPLEMENTED;
393 /** @brief Return the unix group array belonging to the given user
395 * @param *account The given user name
396 * @param *num_groups Number of elements returned in the groups array
397 * @param **_groups Pointer to resulting gid_t array.
401 wbcErr wbcGetGroups(const char *account,
402 uint32_t *num_groups,
405 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
406 struct winbindd_request request;
407 struct winbindd_response response;
409 gid_t *groups = NULL;
411 /* Initialize request */
413 ZERO_STRUCT(request);
414 ZERO_STRUCT(response);
417 wbc_status = WBC_ERR_INVALID_PARAM;
418 BAIL_ON_WBC_ERROR(wbc_status);
423 strncpy(request.data.username, account, sizeof(request.data.username)-1);
425 wbc_status = wbcRequestResponse(WINBINDD_GETGROUPS,
428 BAIL_ON_WBC_ERROR(wbc_status);
430 groups = talloc_array(NULL, gid_t, response.data.num_entries);
431 BAIL_ON_PTR_ERROR(groups, wbc_status);
433 for (i = 0; i < response.data.num_entries; i++) {
434 groups[i] = ((gid_t *)response.extra_data.data)[i];
437 *num_groups = response.data.num_entries;
441 wbc_status = WBC_ERR_SUCCESS;
444 if (response.extra_data.data) {
445 free(response.extra_data.data);