s3:lib/events: make use of samba_tevent_set_debug()
[Samba/gebeck_regimport.git] / nsswitch / winbind_nss_netbsd.c
blobf6738061adb1bc61adb1983dd213e28577ff3561
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];
42 * We need a proper prototype for this :-)
45 NSS_STATUS _nss_winbind_setpwent(void);
46 NSS_STATUS _nss_winbind_endpwent(void);
47 NSS_STATUS _nss_winbind_getpwent_r(struct passwd *result, char *buffer,
48 size_t buflen, int *errnop);
49 NSS_STATUS _nss_winbind_getpwuid_r(uid_t uid, struct passwd *result,
50 char *buffer, size_t buflen, int *errnop);
51 NSS_STATUS _nss_winbind_getpwnam_r(const char *name, struct passwd *result,
52 char *buffer, size_t buflen, int *errnop);
53 NSS_STATUS _nss_winbind_setgrent(void);
54 NSS_STATUS _nss_winbind_endgrent(void);
55 NSS_STATUS _nss_winbind_getgrent_r(struct group *result, char *buffer,
56 size_t buflen, int *errnop);
57 NSS_STATUS _nss_winbind_getgrlst_r(struct group *result, char *buffer,
58 size_t buflen, int *errnop);
59 NSS_STATUS _nss_winbind_getgrnam_r(const char *name, struct group *result,
60 char *buffer, size_t buflen, int *errnop);
61 NSS_STATUS _nss_winbind_getgrgid_r(gid_t gid, struct group *result, char *buffer,
62 size_t buflen, int *errnop);
63 NSS_STATUS _nss_winbind_initgroups_dyn(char *user, gid_t group, long int *start,
64 long int *size, gid_t **groups,
65 long int limit, int *errnop);
66 NSS_STATUS _nss_winbind_getusersids(const char *user_sid, char **group_sids,
67 int *num_groups, char *buffer, size_t buf_size,
68 int *errnop);
69 NSS_STATUS _nss_winbind_nametosid(const char *name, char **sid, char *buffer,
70 size_t buflen, int *errnop);
71 NSS_STATUS _nss_winbind_sidtoname(const char *sid, char **name, char *buffer,
72 size_t buflen, int *errnop);
73 NSS_STATUS _nss_winbind_sidtouid(const char *sid, uid_t *uid, int *errnop);
74 NSS_STATUS _nss_winbind_sidtogid(const char *sid, gid_t *gid, int *errnop);
75 NSS_STATUS _nss_winbind_uidtosid(uid_t uid, char **sid, char *buffer,
76 size_t buflen, int *errnop);
77 NSS_STATUS _nss_winbind_gidtosid(gid_t gid, char **sid, char *buffer,
78 size_t buflen, int *errnop);
80 int
81 netbsdwinbind_endgrent(void *nsrv, void *nscb, va_list ap)
83 int rv;
85 rv = _nss_winbind_endgrent();
86 return rv;
89 int
90 netbsdwinbind_setgrent(void *nsrv, void *nscb, va_list ap)
92 int rv;
94 rv = _nss_winbind_setgrent();
95 return rv;
98 int
99 netbsdwinbind_getgrent(void *nsrv, void *nscb, va_list ap)
101 struct group **retval = va_arg(ap, struct group **);
103 int rv, rerrno;
105 *retval = NULL;
106 rv = _nss_winbind_getgrent_r(&_winbind_group,
107 _winbind_groupbuf, sizeof(_winbind_groupbuf), &rerrno);
108 if (rv == NS_SUCCESS)
109 *retval = &_winbind_group;
110 return rv;
114 netbsdwinbind_getgrent_r(void *nsrv, void *nscb, va_list ap)
116 int *retval = va_arg(ap, int *);
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_getgrent_r(grp, buffer, buflen, &rerrno);
128 if (rv == NS_SUCCESS)
129 *result = grp;
130 else
131 *retval = rerrno;
132 return rv;
136 netbsdwinbind_getgrgid(void *nsrv, void *nscb, va_list ap)
138 struct group **retval = va_arg(ap, struct group **);
139 gid_t gid = va_arg(ap, gid_t);
141 int rv, rerrno;
143 *retval = NULL;
144 rv = _nss_winbind_getgrgid_r(gid, &_winbind_group,
145 _winbind_groupbuf, sizeof(_winbind_groupbuf), &rerrno);
146 if (rv == NS_SUCCESS)
147 *retval = &_winbind_group;
148 return rv;
152 netbsdwinbind_getgrgid_r(void *nsrv, void *nscb, va_list ap)
154 int *retval = va_arg(ap, int *);
155 gid_t gid = va_arg(ap, gid_t);
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_getgrgid_r(gid, grp, buffer, buflen, &rerrno);
167 if (rv == NS_SUCCESS)
168 *result = grp;
169 else
170 *retval = rerrno;
171 return rv;
175 netbsdwinbind_getgrnam(void *nsrv, void *nscb, va_list ap)
177 struct group **retval = va_arg(ap, struct group **);
178 const char *name = va_arg(ap, const char *);
180 int rv, rerrno;
182 *retval = NULL;
183 rv = _nss_winbind_getgrnam_r(name, &_winbind_group,
184 _winbind_groupbuf, sizeof(_winbind_groupbuf), &rerrno);
185 if (rv == NS_SUCCESS)
186 *retval = &_winbind_group;
187 return rv;
191 netbsdwinbind_getgrnam_r(void *nsrv, void *nscb, va_list ap)
193 int *retval = va_arg(ap, int *);
194 const char *name = va_arg(ap, const char *);
195 struct group *grp = va_arg(ap, struct group *);
196 char *buffer = va_arg(ap, char *);
197 size_t buflen = va_arg(ap, size_t);
198 struct group **result = va_arg(ap, struct group **);
200 int rv, rerrno;
202 *result = NULL;
203 rerrno = 0;
205 rv = _nss_winbind_getgrnam_r(name, grp, buffer, buflen, &rerrno);
206 if (rv == NS_SUCCESS)
207 *result = grp;
208 else
209 *retval = rerrno;
210 return rv;
214 netbsdwinbind_getgroupmembership(void *nsrv, void *nscb, va_list ap)
216 int *result = va_arg(ap, int *);
217 const char *uname = va_arg(ap, const char *);
218 gid_t *groups = va_arg(ap, gid_t *);
219 int maxgrp = va_arg(ap, int);
220 int *groupc = va_arg(ap, int *);
222 struct winbindd_request request;
223 struct winbindd_response response;
224 gid_t *wblistv;
225 int wblistc, i, isdup, dupc;
227 ZERO_STRUCT(request);
228 ZERO_STRUCT(response);
229 strncpy(request.data.username, uname,
230 sizeof(request.data.username) - 1);
231 i = winbindd_request_response(WINBINDD_GETGROUPS, &request, &response);
232 if (i != NSS_STATUS_SUCCESS)
233 return NS_NOTFOUND;
234 wblistv = (gid_t *)response.extra_data.data;
235 wblistc = response.data.num_entries;
237 for (i = 0; i < wblistc; i++) { /* add winbind gids */
238 isdup = 0; /* skip duplicates */
239 for (dupc = 0; dupc < MIN(maxgrp, *groupc); dupc++) {
240 if (groups[dupc] == wblistv[i]) {
241 isdup = 1;
242 break;
245 if (isdup)
246 continue;
247 if (*groupc < maxgrp) /* add this gid */
248 groups[*groupc] = wblistv[i];
249 else
250 *result = -1;
251 (*groupc)++;
253 SAFE_FREE(wblistv);
254 return NS_NOTFOUND;
259 passwd functions
260 ----------------
263 static struct passwd _winbind_passwd;
264 static char _winbind_passwdbuf[1024];
267 netbsdwinbind_endpwent(void *nsrv, void *nscb, va_list ap)
269 int rv;
271 rv = _nss_winbind_endpwent();
272 return rv;
276 netbsdwinbind_setpwent(void *nsrv, void *nscb, va_list ap)
278 int rv;
280 rv = _nss_winbind_setpwent();
281 return rv;
285 netbsdwinbind_getpwent(void *nsrv, void *nscb, va_list ap)
287 struct passwd **retval = va_arg(ap, struct passwd **);
289 int rv, rerrno;
291 *retval = NULL;
293 rv = _nss_winbind_getpwent_r(&_winbind_passwd,
294 _winbind_passwdbuf, sizeof(_winbind_passwdbuf), &rerrno);
295 if (rv == NS_SUCCESS)
296 *retval = &_winbind_passwd;
297 return rv;
301 netbsdwinbind_getpwent_r(void *nsrv, void *nscb, va_list ap)
303 int *retval = va_arg(ap, int *);
304 struct passwd *pw = va_arg(ap, struct passwd *);
305 char *buffer = va_arg(ap, char *);
306 size_t buflen = va_arg(ap, size_t);
307 struct passwd **result = va_arg(ap, struct passwd **);
309 int rv, rerrno;
311 *result = NULL;
312 rerrno = 0;
314 rv = _nss_winbind_getpwent_r(pw, buffer, buflen, &rerrno);
315 if (rv == NS_SUCCESS)
316 *result = pw;
317 else
318 *retval = rerrno;
319 return rv;
323 netbsdwinbind_getpwnam(void *nsrv, void *nscb, va_list ap)
325 struct passwd **retval = va_arg(ap, struct passwd **);
326 const char *name = va_arg(ap, const char *);
328 int rv, rerrno;
330 *retval = NULL;
331 rv = _nss_winbind_getpwnam_r(name, &_winbind_passwd,
332 _winbind_passwdbuf, sizeof(_winbind_passwdbuf), &rerrno);
333 if (rv == NS_SUCCESS)
334 *retval = &_winbind_passwd;
335 return rv;
339 netbsdwinbind_getpwnam_r(void *nsrv, void *nscb, va_list ap)
341 int *retval = va_arg(ap, int *);
342 const char *name = va_arg(ap, const char *);
343 struct passwd *pw = va_arg(ap, struct passwd *);
344 char *buffer = va_arg(ap, char *);
345 size_t buflen = va_arg(ap, size_t);
346 struct passwd **result = va_arg(ap, struct passwd **);
348 int rv, rerrno;
350 *result = NULL;
351 rerrno = 0;
353 rv = _nss_winbind_getpwnam_r(name, pw, buffer, buflen, &rerrno);
354 if (rv == NS_SUCCESS)
355 *result = pw;
356 else
357 *retval = rerrno;
358 return rv;
362 netbsdwinbind_getpwuid(void *nsrv, void *nscb, va_list ap)
364 struct passwd **retval = va_arg(ap, struct passwd **);
365 uid_t uid = va_arg(ap, uid_t);
367 int rv, rerrno;
369 *retval = NULL;
370 rv = _nss_winbind_getpwuid_r(uid, &_winbind_passwd,
371 _winbind_passwdbuf, sizeof(_winbind_passwdbuf), &rerrno);
372 if (rv == NS_SUCCESS)
373 *retval = &_winbind_passwd;
374 return rv;
378 netbsdwinbind_getpwuid_r(void *nsrv, void *nscb, va_list ap)
380 int *retval = va_arg(ap, int *);
381 uid_t uid = va_arg(ap, uid_t);
382 struct passwd *pw = va_arg(ap, struct passwd *);
383 char *buffer = va_arg(ap, char *);
384 size_t buflen = va_arg(ap, size_t);
385 struct passwd **result = va_arg(ap, struct passwd **);
387 int rv, rerrno;
389 *result = NULL;
390 rerrno = 0;
392 rv = _nss_winbind_getpwuid_r(uid, pw, buffer, buflen, &rerrno);
393 if (rv == NS_SUCCESS)
394 *result = pw;
395 else
396 *retval = rerrno;
397 return rv;
402 nsswitch module setup
403 ---------------------
407 static ns_mtab winbind_methods[] = {
409 { NSDB_GROUP, "endgrent", netbsdwinbind_endgrent, NULL },
410 { NSDB_GROUP, "getgrent", netbsdwinbind_getgrent, NULL },
411 { NSDB_GROUP, "getgrent_r", netbsdwinbind_getgrent_r, NULL },
412 { NSDB_GROUP, "getgrgid", netbsdwinbind_getgrgid, NULL },
413 { NSDB_GROUP, "getgrgid_r", netbsdwinbind_getgrgid_r, NULL },
414 { NSDB_GROUP, "getgrnam", netbsdwinbind_getgrnam, NULL },
415 { NSDB_GROUP, "getgrnam_r", netbsdwinbind_getgrnam_r, NULL },
416 { NSDB_GROUP, "setgrent", netbsdwinbind_setgrent, NULL },
417 { NSDB_GROUP, "setgroupent", netbsdwinbind_setgrent, NULL },
418 { NSDB_GROUP, "getgroupmembership", netbsdwinbind_getgroupmembership, NULL },
420 { NSDB_PASSWD, "endpwent", netbsdwinbind_endpwent, NULL },
421 { NSDB_PASSWD, "getpwent", netbsdwinbind_getpwent, NULL },
422 { NSDB_PASSWD, "getpwent_r", netbsdwinbind_getpwent_r, NULL },
423 { NSDB_PASSWD, "getpwnam", netbsdwinbind_getpwnam, NULL },
424 { NSDB_PASSWD, "getpwnam_r", netbsdwinbind_getpwnam_r, NULL },
425 { NSDB_PASSWD, "getpwuid", netbsdwinbind_getpwuid, NULL },
426 { NSDB_PASSWD, "getpwuid_r", netbsdwinbind_getpwuid_r, NULL },
427 { NSDB_PASSWD, "setpassent", netbsdwinbind_setpwent, NULL },
428 { NSDB_PASSWD, "setpwent", netbsdwinbind_setpwent, NULL },
432 ns_mtab *
433 nss_module_register(const char *source, unsigned int *mtabsize,
434 nss_module_unregister_fn *unreg)
436 *mtabsize = sizeof(winbind_methods)/sizeof(winbind_methods[0]);
437 *unreg = NULL;
438 return (winbind_methods);
441 #endif /* NSS_MODULE_INTERFACE_VERSION && HAVE_GETPWENT_R */