Some coding convention pedantism.
[ira/wip.git] / source3 / nsswitch / libwbclient / wbc_pwd.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 /**
28  *
29  **/
30
31 static struct passwd *copy_passwd_entry(struct winbindd_pw *p)
32 {
33         struct passwd *pwd = NULL;
34         wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
35
36         pwd = talloc(NULL, struct passwd);
37         BAIL_ON_PTR_ERROR(pwd, wbc_status);
38
39         pwd->pw_name = talloc_strdup(pwd,p->pw_name);
40         BAIL_ON_PTR_ERROR(pwd->pw_name, wbc_status);
41
42         pwd->pw_passwd = talloc_strdup(pwd, p->pw_passwd);
43         BAIL_ON_PTR_ERROR(pwd->pw_passwd, wbc_status);
44
45         pwd->pw_gecos = talloc_strdup(pwd, p->pw_gecos);
46         BAIL_ON_PTR_ERROR(pwd->pw_gecos, wbc_status);
47
48         pwd->pw_shell = talloc_strdup(pwd, p->pw_shell);
49         BAIL_ON_PTR_ERROR(pwd->pw_shell, wbc_status);
50
51         pwd->pw_dir = talloc_strdup(pwd, p->pw_dir);
52         BAIL_ON_PTR_ERROR(pwd->pw_dir, wbc_status);
53
54         pwd->pw_uid = p->pw_uid;
55         pwd->pw_gid = p->pw_gid;
56
57 done:
58         if (!WBC_ERROR_IS_OK(wbc_status)) {
59                 talloc_free(pwd);
60                 pwd = NULL;
61         }
62
63         return pwd;
64 }
65
66 /**
67  *
68  **/
69
70 static struct group *copy_group_entry(struct winbindd_gr *g,
71                                       char *mem_buf)
72 {
73         struct group *grp = NULL;
74         wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
75         int i;
76         char *mem_p, *mem_q;
77
78         grp = talloc(NULL, struct group);
79         BAIL_ON_PTR_ERROR(grp, wbc_status);
80
81         grp->gr_name = talloc_strdup(grp, g->gr_name);
82         BAIL_ON_PTR_ERROR(grp->gr_name, wbc_status);
83
84         grp->gr_passwd = talloc_strdup(grp, g->gr_passwd);
85         BAIL_ON_PTR_ERROR(grp->gr_passwd, wbc_status);
86
87         grp->gr_gid = g->gr_gid;
88
89         grp->gr_mem = talloc_array(grp, char*, g->num_gr_mem+1);
90
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) {
94                         *mem_q = '\0';
95                 }
96
97                 grp->gr_mem[i] = talloc_strdup(grp, mem_p);
98                 BAIL_ON_PTR_ERROR(grp->gr_mem[i], wbc_status);
99
100                 *mem_q = ',';
101                 mem_p++;
102                 mem_p = mem_q;
103         }
104         grp->gr_mem[g->num_gr_mem] = NULL;
105
106         wbc_status = WBC_ERR_SUCCESS;
107
108 done:
109         if (!WBC_ERROR_IS_OK(wbc_status)) {
110                 talloc_free(grp);
111                 grp = NULL;
112         }
113
114         return grp;
115 }
116
117 /** @brief Fill in a struct passwd* for a domain user based
118  *   on username
119  *
120  * @param *name     Username to lookup
121  * @param **pwd     Pointer to resulting struct passwd* from the query.
122  *
123  * @return #wbcErr
124  **/
125
126 wbcErr wbcGetpwnam(const char *name, struct passwd **pwd)
127 {
128         wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
129         struct winbindd_request request;
130         struct winbindd_response response;
131
132         if (!name || !pwd) {
133                 wbc_status = WBC_ERR_INVALID_PARAM;
134                 BAIL_ON_WBC_ERROR(wbc_status);
135         }
136
137         /* Initialize request */
138
139         ZERO_STRUCT(request);
140         ZERO_STRUCT(response);
141
142         /* dst is already null terminated from the memset above */
143
144         strncpy(request.data.username, name, sizeof(request.data.username)-1);
145
146         wbc_status = wbcRequestResponse(WINBINDD_GETPWNAM,
147                                         &request,
148                                         &response);
149         BAIL_ON_WBC_ERROR(wbc_status);
150
151         *pwd = copy_passwd_entry(&response.data.pw);
152         BAIL_ON_PTR_ERROR(*pwd, wbc_status);
153
154  done:
155         return wbc_status;
156 }
157
158 /** @brief Fill in a struct passwd* for a domain user based
159  *   on uid
160  *
161  * @param uid       Uid to lookup
162  * @param **pwd     Pointer to resulting struct passwd* from the query.
163  *
164  * @return #wbcErr
165  **/
166
167 wbcErr wbcGetpwuid(uid_t uid, struct passwd **pwd)
168 {
169         wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
170         struct winbindd_request request;
171         struct winbindd_response response;
172
173         if (!pwd) {
174                 wbc_status = WBC_ERR_INVALID_PARAM;
175                 BAIL_ON_WBC_ERROR(wbc_status);
176         }
177
178         /* Initialize request */
179
180         ZERO_STRUCT(request);
181         ZERO_STRUCT(response);
182
183         request.data.uid = uid;
184
185         wbc_status = wbcRequestResponse(WINBINDD_GETPWUID,
186                                         &request,
187                                         &response);
188         BAIL_ON_WBC_ERROR(wbc_status);
189
190         *pwd = copy_passwd_entry(&response.data.pw);
191         BAIL_ON_PTR_ERROR(*pwd, wbc_status);
192
193  done:
194         return wbc_status;
195 }
196
197 /** @brief Fill in a struct passwd* for a domain user based
198  *   on username
199  *
200  * @param *name     Username to lookup
201  * @param **grp     Pointer to resulting struct group* from the query.
202  *
203  * @return #wbcErr
204  **/
205
206 wbcErr wbcGetgrnam(const char *name, struct group **grp)
207 {
208         wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
209         struct winbindd_request request;
210         struct winbindd_response response;
211
212         if (!name || !grp) {
213                 wbc_status = WBC_ERR_INVALID_PARAM;
214                 BAIL_ON_WBC_ERROR(wbc_status);
215         }
216
217         /* Initialize request */
218
219         ZERO_STRUCT(request);
220         ZERO_STRUCT(response);
221
222         /* dst is already null terminated from the memset above */
223
224         strncpy(request.data.groupname, name, sizeof(request.data.groupname)-1);
225
226         wbc_status = wbcRequestResponse(WINBINDD_GETGRNAM,
227                                         &request,
228                                         &response);
229         BAIL_ON_WBC_ERROR(wbc_status);
230
231         *grp = copy_group_entry(&response.data.gr,
232                                 (char*)response.extra_data.data);
233         BAIL_ON_PTR_ERROR(*grp, wbc_status);
234
235  done:
236         if (response.extra_data.data)
237                 free(response.extra_data.data);
238
239         return wbc_status;
240 }
241
242 /** @brief Fill in a struct passwd* for a domain user based
243  *   on uid
244  *
245  * @param gid       Uid to lookup
246  * @param **grp     Pointer to resulting struct group* from the query.
247  *
248  * @return #wbcErr
249  **/
250
251 wbcErr wbcGetgrgid(gid_t gid, struct group **grp)
252 {
253         wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
254         struct winbindd_request request;
255         struct winbindd_response response;
256
257         if (!grp) {
258                 wbc_status = WBC_ERR_INVALID_PARAM;
259                 BAIL_ON_WBC_ERROR(wbc_status);
260         }
261
262         /* Initialize request */
263
264         ZERO_STRUCT(request);
265         ZERO_STRUCT(response);
266
267         request.data.gid = gid;
268
269         wbc_status = wbcRequestResponse(WINBINDD_GETGRGID,
270                                         &request,
271                                         &response);
272         BAIL_ON_WBC_ERROR(wbc_status);
273
274         *grp = copy_group_entry(&response.data.gr,
275                                 (char*)response.extra_data.data);
276         BAIL_ON_PTR_ERROR(*grp, wbc_status);
277
278  done:
279         if (response.extra_data.data)
280                 free(response.extra_data.data);
281
282         return wbc_status;
283 }
284
285 /** @brief Reset the passwd iterator
286  *
287  * @return #wbcErr
288  **/
289
290 wbcErr wbcSetpwent(void)
291 {
292         wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
293
294         wbc_status = wbcRequestResponse(WINBINDD_SETPWENT,
295                                         NULL, NULL);
296         BAIL_ON_WBC_ERROR(wbc_status);
297
298  done:
299         return wbc_status;
300 }
301
302 /** @brief Close the passwd iterator
303  *
304  * @return #wbcErr
305  **/
306
307 wbcErr wbcEndpwent(void)
308 {
309         wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
310
311         wbc_status = wbcRequestResponse(WINBINDD_ENDPWENT,
312                                         NULL, NULL);
313         BAIL_ON_WBC_ERROR(wbc_status);
314
315  done:
316         return wbc_status;
317 }
318
319 /** @brief Return the next struct passwd* entry from the pwent iterator
320  *
321  * @param **pwd       Pointer to resulting struct group* from the query.
322  *
323  * @return #wbcErr
324  **/
325
326 wbcErr wbcGetpwent(struct passwd **pwd)
327 {
328         return WBC_ERR_NOT_IMPLEMENTED;
329 }
330
331 /** @brief Reset the group iterator
332  *
333  * @return #wbcErr
334  **/
335
336 wbcErr wbcSetgrent(void)
337 {
338         wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
339
340         wbc_status = wbcRequestResponse(WINBINDD_SETGRENT,
341                                         NULL, NULL);
342         BAIL_ON_WBC_ERROR(wbc_status);
343
344  done:
345         return wbc_status;
346 }
347
348 /** @brief Close the group iterator
349  *
350  * @return #wbcErr
351  **/
352
353 wbcErr wbcEndgrent(void)
354 {
355         wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
356
357         wbc_status = wbcRequestResponse(WINBINDD_ENDGRENT,
358                                         NULL, NULL);
359         BAIL_ON_WBC_ERROR(wbc_status);
360
361  done:
362         return wbc_status;
363 }
364
365 /** @brief Return the next struct passwd* entry from the pwent iterator
366  *
367  * @param **grp       Pointer to resulting struct group* from the query.
368  *
369  * @return #wbcErr
370  **/
371
372 wbcErr wbcGetgrent(struct group **grp)
373 {
374         return WBC_ERR_NOT_IMPLEMENTED;
375 }
376