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
);
104 grp
->gr_mem
[g
->num_gr_mem
] = NULL
;
106 wbc_status
= WBC_ERR_SUCCESS
;
109 if (!WBC_ERROR_IS_OK(wbc_status
)) {
117 /** @brief Fill in a struct passwd* for a domain user based
120 * @param *name Username to lookup
121 * @param **pwd Pointer to resulting struct passwd* from the query.
126 wbcErr
wbcGetpwnam(const char *name
, struct passwd
**pwd
)
128 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
129 struct winbindd_request request
;
130 struct winbindd_response response
;
133 wbc_status
= WBC_ERR_INVALID_PARAM
;
134 BAIL_ON_WBC_ERROR(wbc_status
);
137 /* Initialize request */
139 ZERO_STRUCT(request
);
140 ZERO_STRUCT(response
);
142 /* dst is already null terminated from the memset above */
144 strncpy(request
.data
.username
, name
, sizeof(request
.data
.username
)-1);
146 wbc_status
= wbcRequestResponse(WINBINDD_GETPWNAM
,
149 BAIL_ON_WBC_ERROR(wbc_status
);
151 *pwd
= copy_passwd_entry(&response
.data
.pw
);
152 BAIL_ON_PTR_ERROR(*pwd
, wbc_status
);
158 /** @brief Fill in a struct passwd* for a domain user based
161 * @param uid Uid to lookup
162 * @param **pwd Pointer to resulting struct passwd* from the query.
167 wbcErr
wbcGetpwuid(uid_t uid
, struct passwd
**pwd
)
169 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
170 struct winbindd_request request
;
171 struct winbindd_response response
;
174 wbc_status
= WBC_ERR_INVALID_PARAM
;
175 BAIL_ON_WBC_ERROR(wbc_status
);
178 /* Initialize request */
180 ZERO_STRUCT(request
);
181 ZERO_STRUCT(response
);
183 request
.data
.uid
= uid
;
185 wbc_status
= wbcRequestResponse(WINBINDD_GETPWUID
,
188 BAIL_ON_WBC_ERROR(wbc_status
);
190 *pwd
= copy_passwd_entry(&response
.data
.pw
);
191 BAIL_ON_PTR_ERROR(*pwd
, wbc_status
);
197 /** @brief Fill in a struct passwd* for a domain user based
200 * @param *name Username to lookup
201 * @param **grp Pointer to resulting struct group* from the query.
206 wbcErr
wbcGetgrnam(const char *name
, struct group
**grp
)
208 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
209 struct winbindd_request request
;
210 struct winbindd_response response
;
213 wbc_status
= WBC_ERR_INVALID_PARAM
;
214 BAIL_ON_WBC_ERROR(wbc_status
);
217 /* Initialize request */
219 ZERO_STRUCT(request
);
220 ZERO_STRUCT(response
);
222 /* dst is already null terminated from the memset above */
224 strncpy(request
.data
.groupname
, name
, sizeof(request
.data
.groupname
)-1);
226 wbc_status
= wbcRequestResponse(WINBINDD_GETGRNAM
,
229 BAIL_ON_WBC_ERROR(wbc_status
);
231 *grp
= copy_group_entry(&response
.data
.gr
,
232 (char*)response
.extra_data
.data
);
233 BAIL_ON_PTR_ERROR(*grp
, wbc_status
);
236 if (response
.extra_data
.data
)
237 free(response
.extra_data
.data
);
242 /** @brief Fill in a struct passwd* for a domain user based
245 * @param gid Uid to lookup
246 * @param **grp Pointer to resulting struct group* from the query.
251 wbcErr
wbcGetgrgid(gid_t gid
, struct group
**grp
)
253 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
254 struct winbindd_request request
;
255 struct winbindd_response response
;
258 wbc_status
= WBC_ERR_INVALID_PARAM
;
259 BAIL_ON_WBC_ERROR(wbc_status
);
262 /* Initialize request */
264 ZERO_STRUCT(request
);
265 ZERO_STRUCT(response
);
267 request
.data
.gid
= gid
;
269 wbc_status
= wbcRequestResponse(WINBINDD_GETGRGID
,
272 BAIL_ON_WBC_ERROR(wbc_status
);
274 *grp
= copy_group_entry(&response
.data
.gr
,
275 (char*)response
.extra_data
.data
);
276 BAIL_ON_PTR_ERROR(*grp
, wbc_status
);
279 if (response
.extra_data
.data
)
280 free(response
.extra_data
.data
);
285 /** @brief Reset the passwd iterator
290 wbcErr
wbcSetpwent(void)
292 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
294 wbc_status
= wbcRequestResponse(WINBINDD_SETPWENT
,
296 BAIL_ON_WBC_ERROR(wbc_status
);
302 /** @brief Close the passwd iterator
307 wbcErr
wbcEndpwent(void)
309 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
311 wbc_status
= wbcRequestResponse(WINBINDD_ENDPWENT
,
313 BAIL_ON_WBC_ERROR(wbc_status
);
319 /** @brief Return the next struct passwd* entry from the pwent iterator
321 * @param **pwd Pointer to resulting struct group* from the query.
326 wbcErr
wbcGetpwent(struct passwd
**pwd
)
328 return WBC_ERR_NOT_IMPLEMENTED
;
331 /** @brief Reset the group iterator
336 wbcErr
wbcSetgrent(void)
338 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
340 wbc_status
= wbcRequestResponse(WINBINDD_SETGRENT
,
342 BAIL_ON_WBC_ERROR(wbc_status
);
348 /** @brief Close the group iterator
353 wbcErr
wbcEndgrent(void)
355 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
357 wbc_status
= wbcRequestResponse(WINBINDD_ENDGRENT
,
359 BAIL_ON_WBC_ERROR(wbc_status
);
365 /** @brief Return the next struct passwd* entry from the pwent iterator
367 * @param **grp Pointer to resulting struct group* from the query.
372 wbcErr
wbcGetgrent(struct group
**grp
)
374 return WBC_ERR_NOT_IMPLEMENTED
;