vfs_fruit: Fix CID 1416474 Dereference null return value
[Samba.git] / nsswitch / winbind_nss_netbsd.c
blob4edf64c2c113d31fe1313a808e07a418ab2d35a5
1 /*
2 Unix SMB/CIFS implementation.
4 NetBSD loadable authentication module, providing identification
5 routines against Samba winbind/Windows NT Domain
7 Copyright (C) Luke Mewburn 2004-2005
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
20 License along with this library; if not, see <http://www.gnu.org/licenses/>.
24 #include "winbind_client.h"
26 #include <sys/param.h>
27 #include <stdarg.h>
28 #include <syslog.h>
30 /* dynamic nsswitch with "new" getpw* nsdispatch API available */
31 #if defined(NSS_MODULE_INTERFACE_VERSION) && defined(HAVE_GETPWENT_R)
34 group functions
35 ---------------
38 static struct group _winbind_group;
39 static char _winbind_groupbuf[1024];
41 int
42 netbsdwinbind_endgrent(void *nsrv, void *nscb, va_list ap)
44 int rv;
46 rv = _nss_winbind_endgrent();
47 return rv;
50 int
51 netbsdwinbind_setgrent(void *nsrv, void *nscb, va_list ap)
53 int rv;
55 rv = _nss_winbind_setgrent();
56 return rv;
59 int
60 netbsdwinbind_getgrent(void *nsrv, void *nscb, va_list ap)
62 struct group **retval = va_arg(ap, struct group **);
64 int rv, rerrno;
66 *retval = NULL;
67 rv = _nss_winbind_getgrent_r(&_winbind_group,
68 _winbind_groupbuf, sizeof(_winbind_groupbuf), &rerrno);
69 if (rv == NS_SUCCESS)
70 *retval = &_winbind_group;
71 return rv;
74 int
75 netbsdwinbind_getgrent_r(void *nsrv, void *nscb, va_list ap)
77 int *retval = va_arg(ap, int *);
78 struct group *grp = va_arg(ap, struct group *);
79 char *buffer = va_arg(ap, char *);
80 size_t buflen = va_arg(ap, size_t);
81 struct group **result = va_arg(ap, struct group **);
83 int rv, rerrno;
85 *result = NULL;
86 rerrno = 0;
88 rv = _nss_winbind_getgrent_r(grp, buffer, buflen, &rerrno);
89 if (rv == NS_SUCCESS)
90 *result = grp;
91 else
92 *retval = rerrno;
93 return rv;
96 int
97 netbsdwinbind_getgrgid(void *nsrv, void *nscb, va_list ap)
99 struct group **retval = va_arg(ap, struct group **);
100 gid_t gid = va_arg(ap, gid_t);
102 int rv, rerrno;
104 *retval = NULL;
105 rv = _nss_winbind_getgrgid_r(gid, &_winbind_group,
106 _winbind_groupbuf, sizeof(_winbind_groupbuf), &rerrno);
107 if (rv == NS_SUCCESS)
108 *retval = &_winbind_group;
109 return rv;
113 netbsdwinbind_getgrgid_r(void *nsrv, void *nscb, va_list ap)
115 int *retval = va_arg(ap, int *);
116 gid_t gid = va_arg(ap, gid_t);
117 struct group *grp = va_arg(ap, struct group *);
118 char *buffer = va_arg(ap, char *);
119 size_t buflen = va_arg(ap, size_t);
120 struct group **result = va_arg(ap, struct group **);
122 int rv, rerrno;
124 *result = NULL;
125 rerrno = 0;
127 rv = _nss_winbind_getgrgid_r(gid, grp, buffer, buflen, &rerrno);
128 if (rv == NS_SUCCESS)
129 *result = grp;
130 else
131 *retval = rerrno;
132 return rv;
136 netbsdwinbind_getgrnam(void *nsrv, void *nscb, va_list ap)
138 struct group **retval = va_arg(ap, struct group **);
139 const char *name = va_arg(ap, const char *);
141 int rv, rerrno;
143 *retval = NULL;
144 rv = _nss_winbind_getgrnam_r(name, &_winbind_group,
145 _winbind_groupbuf, sizeof(_winbind_groupbuf), &rerrno);
146 if (rv == NS_SUCCESS)
147 *retval = &_winbind_group;
148 return rv;
152 netbsdwinbind_getgrnam_r(void *nsrv, void *nscb, va_list ap)
154 int *retval = va_arg(ap, int *);
155 const char *name = va_arg(ap, const char *);
156 struct group *grp = va_arg(ap, struct group *);
157 char *buffer = va_arg(ap, char *);
158 size_t buflen = va_arg(ap, size_t);
159 struct group **result = va_arg(ap, struct group **);
161 int rv, rerrno;
163 *result = NULL;
164 rerrno = 0;
166 rv = _nss_winbind_getgrnam_r(name, grp, buffer, buflen, &rerrno);
167 if (rv == NS_SUCCESS)
168 *result = grp;
169 else
170 *retval = rerrno;
171 return rv;
175 netbsdwinbind_getgroupmembership(void *nsrv, void *nscb, va_list ap)
177 int *result = va_arg(ap, int *);
178 const char *uname = va_arg(ap, const char *);
179 gid_t *groups = va_arg(ap, gid_t *);
180 int maxgrp = va_arg(ap, int);
181 int *groupc = va_arg(ap, int *);
183 struct winbindd_request request;
184 struct winbindd_response response;
185 gid_t *wblistv;
186 int wblistc, i, isdup, dupc;
188 ZERO_STRUCT(request);
189 ZERO_STRUCT(response);
190 strncpy(request.data.username, uname,
191 sizeof(request.data.username) - 1);
192 i = winbindd_request_response(NULL, WINBINDD_GETGROUPS,
193 &request, &response);
194 if (i != NSS_STATUS_SUCCESS)
195 return NS_NOTFOUND;
196 wblistv = (gid_t *)response.extra_data.data;
197 wblistc = response.data.num_entries;
199 for (i = 0; i < wblistc; i++) { /* add winbind gids */
200 isdup = 0; /* skip duplicates */
201 for (dupc = 0; dupc < MIN(maxgrp, *groupc); dupc++) {
202 if (groups[dupc] == wblistv[i]) {
203 isdup = 1;
204 break;
207 if (isdup)
208 continue;
209 if (*groupc < maxgrp) /* add this gid */
210 groups[*groupc] = wblistv[i];
211 else
212 *result = -1;
213 (*groupc)++;
215 SAFE_FREE(wblistv);
216 return NS_NOTFOUND;
221 passwd functions
222 ----------------
225 static struct passwd _winbind_passwd;
226 static char _winbind_passwdbuf[1024];
229 netbsdwinbind_endpwent(void *nsrv, void *nscb, va_list ap)
231 int rv;
233 rv = _nss_winbind_endpwent();
234 return rv;
238 netbsdwinbind_setpwent(void *nsrv, void *nscb, va_list ap)
240 int rv;
242 rv = _nss_winbind_setpwent();
243 return rv;
247 netbsdwinbind_getpwent(void *nsrv, void *nscb, va_list ap)
249 struct passwd **retval = va_arg(ap, struct passwd **);
251 int rv, rerrno;
253 *retval = NULL;
255 rv = _nss_winbind_getpwent_r(&_winbind_passwd,
256 _winbind_passwdbuf, sizeof(_winbind_passwdbuf), &rerrno);
257 if (rv == NS_SUCCESS)
258 *retval = &_winbind_passwd;
259 return rv;
263 netbsdwinbind_getpwent_r(void *nsrv, void *nscb, va_list ap)
265 int *retval = va_arg(ap, int *);
266 struct passwd *pw = va_arg(ap, struct passwd *);
267 char *buffer = va_arg(ap, char *);
268 size_t buflen = va_arg(ap, size_t);
269 struct passwd **result = va_arg(ap, struct passwd **);
271 int rv, rerrno;
273 *result = NULL;
274 rerrno = 0;
276 rv = _nss_winbind_getpwent_r(pw, buffer, buflen, &rerrno);
277 if (rv == NS_SUCCESS)
278 *result = pw;
279 else
280 *retval = rerrno;
281 return rv;
285 netbsdwinbind_getpwnam(void *nsrv, void *nscb, va_list ap)
287 struct passwd **retval = va_arg(ap, struct passwd **);
288 const char *name = va_arg(ap, const char *);
290 int rv, rerrno;
292 *retval = NULL;
293 rv = _nss_winbind_getpwnam_r(name, &_winbind_passwd,
294 _winbind_passwdbuf, sizeof(_winbind_passwdbuf), &rerrno);
295 if (rv == NS_SUCCESS)
296 *retval = &_winbind_passwd;
297 return rv;
301 netbsdwinbind_getpwnam_r(void *nsrv, void *nscb, va_list ap)
303 int *retval = va_arg(ap, int *);
304 const char *name = va_arg(ap, const char *);
305 struct passwd *pw = va_arg(ap, struct passwd *);
306 char *buffer = va_arg(ap, char *);
307 size_t buflen = va_arg(ap, size_t);
308 struct passwd **result = va_arg(ap, struct passwd **);
310 int rv, rerrno;
312 *result = NULL;
313 rerrno = 0;
315 rv = _nss_winbind_getpwnam_r(name, pw, buffer, buflen, &rerrno);
316 if (rv == NS_SUCCESS)
317 *result = pw;
318 else
319 *retval = rerrno;
320 return rv;
324 netbsdwinbind_getpwuid(void *nsrv, void *nscb, va_list ap)
326 struct passwd **retval = va_arg(ap, struct passwd **);
327 uid_t uid = va_arg(ap, uid_t);
329 int rv, rerrno;
331 *retval = NULL;
332 rv = _nss_winbind_getpwuid_r(uid, &_winbind_passwd,
333 _winbind_passwdbuf, sizeof(_winbind_passwdbuf), &rerrno);
334 if (rv == NS_SUCCESS)
335 *retval = &_winbind_passwd;
336 return rv;
340 netbsdwinbind_getpwuid_r(void *nsrv, void *nscb, va_list ap)
342 int *retval = va_arg(ap, int *);
343 uid_t uid = va_arg(ap, uid_t);
344 struct passwd *pw = va_arg(ap, struct passwd *);
345 char *buffer = va_arg(ap, char *);
346 size_t buflen = va_arg(ap, size_t);
347 struct passwd **result = va_arg(ap, struct passwd **);
349 int rv, rerrno;
351 *result = NULL;
352 rerrno = 0;
354 rv = _nss_winbind_getpwuid_r(uid, pw, buffer, buflen, &rerrno);
355 if (rv == NS_SUCCESS)
356 *result = pw;
357 else
358 *retval = rerrno;
359 return rv;
364 nsswitch module setup
365 ---------------------
369 static ns_mtab winbind_methods[] = {
371 { NSDB_GROUP, "endgrent", netbsdwinbind_endgrent, NULL },
372 { NSDB_GROUP, "getgrent", netbsdwinbind_getgrent, NULL },
373 { NSDB_GROUP, "getgrent_r", netbsdwinbind_getgrent_r, NULL },
374 { NSDB_GROUP, "getgrgid", netbsdwinbind_getgrgid, NULL },
375 { NSDB_GROUP, "getgrgid_r", netbsdwinbind_getgrgid_r, NULL },
376 { NSDB_GROUP, "getgrnam", netbsdwinbind_getgrnam, NULL },
377 { NSDB_GROUP, "getgrnam_r", netbsdwinbind_getgrnam_r, NULL },
378 { NSDB_GROUP, "setgrent", netbsdwinbind_setgrent, NULL },
379 { NSDB_GROUP, "setgroupent", netbsdwinbind_setgrent, NULL },
380 { NSDB_GROUP, "getgroupmembership", netbsdwinbind_getgroupmembership, NULL },
382 { NSDB_PASSWD, "endpwent", netbsdwinbind_endpwent, NULL },
383 { NSDB_PASSWD, "getpwent", netbsdwinbind_getpwent, NULL },
384 { NSDB_PASSWD, "getpwent_r", netbsdwinbind_getpwent_r, NULL },
385 { NSDB_PASSWD, "getpwnam", netbsdwinbind_getpwnam, NULL },
386 { NSDB_PASSWD, "getpwnam_r", netbsdwinbind_getpwnam_r, NULL },
387 { NSDB_PASSWD, "getpwuid", netbsdwinbind_getpwuid, NULL },
388 { NSDB_PASSWD, "getpwuid_r", netbsdwinbind_getpwuid_r, NULL },
389 { NSDB_PASSWD, "setpassent", netbsdwinbind_setpwent, NULL },
390 { NSDB_PASSWD, "setpwent", netbsdwinbind_setpwent, NULL },
394 ns_mtab *
395 nss_module_register(const char *source, unsigned int *mtabsize,
396 nss_module_unregister_fn *unreg)
398 *mtabsize = sizeof(winbind_methods)/sizeof(winbind_methods[0]);
399 *unreg = NULL;
400 return (winbind_methods);
403 #endif /* NSS_MODULE_INTERFACE_VERSION && HAVE_GETPWENT_R */