source: branches/samba-3.0/source/nsswitch/winbind_nss_netbsd.c

Last change on this file was 1, checked in by Paul Smedley, 18 years ago

Initial code import

File size: 9.6 KB
Line 
1/*
2 Unix SMB/CIFS implementation.
3
4 NetBSD loadable authentication module, providing identification
5 routines against Samba winbind/Windows NT Domain
6
7 Copyright (C) Luke Mewburn 2004-2005
8
9 This library is free software; you can redistribute it and/or
10 modify it under the terms of the GNU Library General Public
11 License as published by the Free Software Foundation; either
12 version 2 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 Library General Public
20 License along with this library; if not, write to the
21 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 Boston, MA 02111-1307, USA.
23*/
24
25#include <sys/param.h>
26
27#include "winbind_client.h"
28
29#include <stdarg.h>
30#include <syslog.h>
31
32 /* dynamic nsswitch with "new" getpw* nsdispatch API available */
33#if defined(NSS_MODULE_INTERFACE_VERSION) && defined(HAVE_GETPWENT_R)
34
35/*
36 group functions
37 ---------------
38*/
39
40static struct group _winbind_group;
41static char _winbind_groupbuf[1024];
42
43int
44netbsdwinbind_endgrent(void *nsrv, void *nscb, va_list ap)
45{
46 int rv;
47
48 rv = _nss_winbind_endgrent();
49 return rv;
50}
51
52int
53netbsdwinbind_setgrent(void *nsrv, void *nscb, va_list ap)
54{
55 int rv;
56
57 rv = _nss_winbind_setgrent();
58 return rv;
59}
60
61int
62netbsdwinbind_getgrent(void *nsrv, void *nscb, va_list ap)
63{
64 struct group **retval = va_arg(ap, struct group **);
65
66 int rv, rerrno;
67
68 *retval = NULL;
69 rv = _nss_winbind_getgrent_r(&_winbind_group,
70 _winbind_groupbuf, sizeof(_winbind_groupbuf), &rerrno);
71 if (rv == NS_SUCCESS)
72 *retval = &_winbind_group;
73 return rv;
74}
75
76int
77netbsdwinbind_getgrent_r(void *nsrv, void *nscb, va_list ap)
78{
79 int *retval = va_arg(ap, int *);
80 struct group *grp = va_arg(ap, struct group *);
81 char *buffer = va_arg(ap, char *);
82 size_t buflen = va_arg(ap, size_t);
83 struct group **result = va_arg(ap, struct group **);
84
85 int rv, rerrno;
86
87 *result = NULL;
88 rerrno = 0;
89
90 rv = _nss_winbind_getgrent_r(grp, buffer, buflen, rerrno);
91 if (rv == NS_SUCCESS)
92 *result = grp;
93 else
94 *retval = rerrno;
95 return rv;
96}
97
98int
99netbsdwinbind_getgrgid(void *nsrv, void *nscb, va_list ap)
100{
101 struct group **retval = va_arg(ap, struct group **);
102 gid_t gid = va_arg(ap, gid_t);
103
104 int rv, rerrno;
105
106 *retval = NULL;
107 rv = _nss_winbind_getgrgid_r(gid, &_winbind_group,
108 _winbind_groupbuf, sizeof(_winbind_groupbuf), &rerrno);
109 if (rv == NS_SUCCESS)
110 *retval = &_winbind_group;
111 return rv;
112}
113
114int
115netbsdwinbind_getgrgid_r(void *nsrv, void *nscb, va_list ap)
116{
117 int *retval = va_arg(ap, int *);
118 gid_t gid = va_arg(ap, gid_t);
119 struct group *grp = va_arg(ap, struct group *);
120 char *buffer = va_arg(ap, char *);
121 size_t buflen = va_arg(ap, size_t);
122 struct group **result = va_arg(ap, struct group **);
123
124 int rv, rerrno;
125
126 *result = NULL;
127 rerrno = 0;
128
129 rv = _nss_winbind_getgrgid_r(gid, grp, buffer, buflen, &rerrno);
130 if (rv == NS_SUCCESS)
131 *result = grp;
132 else
133 *retval = rerrno;
134 return rv;
135}
136
137int
138netbsdwinbind_getgrnam(void *nsrv, void *nscb, va_list ap)
139{
140 struct group **retval = va_arg(ap, struct group **);
141 const char *name = va_arg(ap, const char *);
142
143 int rv, rerrno;
144
145 *retval = NULL;
146 rv = _nss_winbind_getgrnam_r(name, &_winbind_group,
147 _winbind_groupbuf, sizeof(_winbind_groupbuf), &rerrno);
148 if (rv == NS_SUCCESS)
149 *retval = &_winbind_group;
150 return rv;
151}
152
153int
154netbsdwinbind_getgrnam_r(void *nsrv, void *nscb, va_list ap)
155{
156 int *retval = va_arg(ap, int *);
157 const char *name = va_arg(ap, const char *);
158 struct group *grp = va_arg(ap, struct group *);
159 char *buffer = va_arg(ap, char *);
160 size_t buflen = va_arg(ap, size_t);
161 struct group **result = va_arg(ap, struct group **);
162
163 int rv, rerrno;
164
165 *result = NULL;
166 rerrno = 0;
167
168 rv = _nss_winbind_getgrnam_r(name, grp, buffer, buflen, &rerrno);
169 if (rv == NS_SUCCESS)
170 *result = grp;
171 else
172 *retval = rerrno;
173 return rv;
174}
175
176int
177netbsdwinbind_getgroupmembership(void *nsrv, void *nscb, va_list ap)
178{
179 int *result = va_arg(ap, int *);
180 const char *uname = va_arg(ap, const char *);
181 gid_t agroup = va_arg(ap, gid_t);
182 gid_t *groups = va_arg(ap, gid_t *);
183 int maxgrp = va_arg(ap, int);
184 int *groupc = va_arg(ap, int *);
185
186 struct winbindd_request request;
187 struct winbindd_response response;
188 gid_t *wblistv;
189 int wblistc, i, isdup, dupc;
190
191 ZERO_STRUCT(request);
192 ZERO_STRUCT(response);
193 strncpy(request.data.username, uname,
194 sizeof(request.data.username) - 1);
195 i = winbindd_request_response(WINBINDD_GETGROUPS, &request, &response);
196 if (i != NSS_STATUS_SUCCESS)
197 return NS_NOTFOUND;
198 wblistv = (gid_t *)response.extra_data.data;
199 wblistc = response.data.num_entries;
200
201 for (i = 0; i < wblistc; i++) { /* add winbind gids */
202 isdup = 0; /* skip duplicates */
203 for (dupc = 0; dupc < MIN(maxgrp, *groupc); dupc++) {
204 if (groups[dupc] == wblistv[i]) {
205 isdup = 1;
206 break;
207 }
208 }
209 if (isdup)
210 continue;
211 if (*groupc < maxgrp) /* add this gid */
212 groups[*groupc] = wblistv[i];
213 else
214 *result = -1;
215 (*groupc)++;
216 }
217 SAFE_FREE(wblistv);
218 return NS_NOTFOUND;
219}
220
221
222/*
223 passwd functions
224 ----------------
225*/
226
227static struct passwd _winbind_passwd;
228static char _winbind_passwdbuf[1024];
229
230int
231netbsdwinbind_endpwent(void *nsrv, void *nscb, va_list ap)
232{
233 int rv;
234
235 rv = _nss_winbind_endpwent();
236 return rv;
237}
238
239int
240netbsdwinbind_setpwent(void *nsrv, void *nscb, va_list ap)
241{
242 int rv;
243
244 rv = _nss_winbind_setpwent();
245 return rv;
246}
247
248int
249netbsdwinbind_getpwent(void *nsrv, void *nscb, va_list ap)
250{
251 struct passwd **retval = va_arg(ap, struct passwd **);
252
253 int rv, rerrno;
254
255 *retval = NULL;
256
257 rv = _nss_winbind_getpwent_r(&_winbind_passwd,
258 _winbind_passwdbuf, sizeof(_winbind_passwdbuf), &rerrno);
259 if (rv == NS_SUCCESS)
260 *retval = &_winbind_passwd;
261 return rv;
262}
263
264int
265netbsdwinbind_getpwent_r(void *nsrv, void *nscb, va_list ap)
266{
267 int *retval = va_arg(ap, int *);
268 struct passwd *pw = va_arg(ap, struct passwd *);
269 char *buffer = va_arg(ap, char *);
270 size_t buflen = va_arg(ap, size_t);
271 struct passwd **result = va_arg(ap, struct passwd **);
272
273 int rv, rerrno;
274
275 *result = NULL;
276 rerrno = 0;
277
278 rv = _nss_winbind_getpwent_r(pw, buffer, buflen, rerrno);
279 if (rv == NS_SUCCESS)
280 *result = pw;
281 else
282 *retval = rerrno;
283 return rv;
284}
285
286int
287netbsdwinbind_getpwnam(void *nsrv, void *nscb, va_list ap)
288{
289 struct passwd **retval = va_arg(ap, struct passwd **);
290 const char *name = va_arg(ap, const char *);
291
292 int rv, rerrno;
293
294 *retval = NULL;
295 rv = _nss_winbind_getpwnam_r(name, &_winbind_passwd,
296 _winbind_passwdbuf, sizeof(_winbind_passwdbuf), &rerrno);
297 if (rv == NS_SUCCESS)
298 *retval = &_winbind_passwd;
299 return rv;
300}
301
302int
303netbsdwinbind_getpwnam_r(void *nsrv, void *nscb, va_list ap)
304{
305 int *retval = va_arg(ap, int *);
306 const char *name = va_arg(ap, const char *);
307 struct passwd *pw = va_arg(ap, struct passwd *);
308 char *buffer = va_arg(ap, char *);
309 size_t buflen = va_arg(ap, size_t);
310 struct passwd **result = va_arg(ap, struct passwd **);
311
312 int rv, rerrno;
313
314 *result = NULL;
315 rerrno = 0;
316
317 rv = _nss_winbind_getpwnam_r(name, pw, buffer, buflen, &rerrno);
318 if (rv == NS_SUCCESS)
319 *result = pw;
320 else
321 *retval = rerrno;
322 return rv;
323}
324
325int
326netbsdwinbind_getpwuid(void *nsrv, void *nscb, va_list ap)
327{
328 struct passwd **retval = va_arg(ap, struct passwd **);
329 uid_t uid = va_arg(ap, uid_t);
330
331 int rv, rerrno;
332
333 *retval = NULL;
334 rv = _nss_winbind_getpwuid_r(uid, &_winbind_passwd,
335 _winbind_passwdbuf, sizeof(_winbind_passwdbuf), &rerrno);
336 if (rv == NS_SUCCESS)
337 *retval = &_winbind_passwd;
338 return rv;
339}
340
341int
342netbsdwinbind_getpwuid_r(void *nsrv, void *nscb, va_list ap)
343{
344 int *retval = va_arg(ap, int *);
345 uid_t uid = va_arg(ap, uid_t);
346 struct passwd *pw = va_arg(ap, struct passwd *);
347 char *buffer = va_arg(ap, char *);
348 size_t buflen = va_arg(ap, size_t);
349 struct passwd **result = va_arg(ap, struct passwd **);
350
351 int rv, rerrno;
352
353 *result = NULL;
354 rerrno = 0;
355
356 rv = _nss_winbind_getpwuid_r(uid, pw, buffer, buflen, &rerrno);
357 if (rv == NS_SUCCESS)
358 *result = pw;
359 else
360 *retval = rerrno;
361 return rv;
362}
363
364
365/*
366 nsswitch module setup
367 ---------------------
368*/
369
370
371static ns_mtab winbind_methods[] = {
372
373{ NSDB_GROUP, "endgrent", netbsdwinbind_endgrent, NULL },
374{ NSDB_GROUP, "getgrent", netbsdwinbind_getgrent, NULL },
375{ NSDB_GROUP, "getgrent_r", netbsdwinbind_getgrent_r, NULL },
376{ NSDB_GROUP, "getgrgid", netbsdwinbind_getgrgid, NULL },
377{ NSDB_GROUP, "getgrgid_r", netbsdwinbind_getgrgid_r, NULL },
378{ NSDB_GROUP, "getgrnam", netbsdwinbind_getgrnam, NULL },
379{ NSDB_GROUP, "getgrnam_r", netbsdwinbind_getgrnam_r, NULL },
380{ NSDB_GROUP, "setgrent", netbsdwinbind_setgrent, NULL },
381{ NSDB_GROUP, "setgroupent", netbsdwinbind_setgrent, NULL },
382{ NSDB_GROUP, "getgroupmembership", netbsdwinbind_getgroupmembership, NULL },
383
384{ NSDB_PASSWD, "endpwent", netbsdwinbind_endpwent, NULL },
385{ NSDB_PASSWD, "getpwent", netbsdwinbind_getpwent, NULL },
386{ NSDB_PASSWD, "getpwent_r", netbsdwinbind_getpwent_r, NULL },
387{ NSDB_PASSWD, "getpwnam", netbsdwinbind_getpwnam, NULL },
388{ NSDB_PASSWD, "getpwnam_r", netbsdwinbind_getpwnam_r, NULL },
389{ NSDB_PASSWD, "getpwuid", netbsdwinbind_getpwuid, NULL },
390{ NSDB_PASSWD, "getpwuid_r", netbsdwinbind_getpwuid_r, NULL },
391{ NSDB_PASSWD, "setpassent", netbsdwinbind_setpwent, NULL },
392{ NSDB_PASSWD, "setpwent", netbsdwinbind_setpwent, NULL },
393
394};
395
396ns_mtab *
397nss_module_register(const char *source, unsigned int *mtabsize,
398 nss_module_unregister_fn *unreg)
399{
400 *mtabsize = sizeof(winbind_methods)/sizeof(winbind_methods[0]);
401 *unreg = NULL;
402 return (winbind_methods);
403}
404
405#endif /* NSS_MODULE_INTERFACE_VERSION && HAVE_GETPWENT_R */
Note: See TracBrowser for help on using the repository browser.