s3: vfs: catia: compression get/set must act only on base file, and must cope with...
[Samba.git] / nsswitch / winbind_nss_freebsd.c
blob476349ffbc0d5d6e6f6a96297b79eac68945b630
1 /*
2 Unix SMB/CIFS implementation.
4 AIX loadable authentication module, providing identification
5 routines against Samba winbind/Windows NT Domain
7 Copyright (C) Aaron Collins 2003
8 Copyright (C) Timur I. Bakeyev 2013
10 This library is free software; you can redistribute it and/or
11 modify it under the terms of the GNU Lesser General Public
12 License as published by the Free Software Foundation; either
13 version 3 of the License, or (at your option) any later version.
15 This library is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 Library General Public License for more details.
20 You should have received a copy of the GNU Lesser General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>.
24 #include "winbind_client.h"
26 /* Make sure that the module gets registered needed by freebsd 5.1 */
27 extern enum nss_status _nss_winbind_getgrent_r(struct group *, char *, size_t,
28 int *);
29 extern enum nss_status _nss_winbind_getgrnam_r(const char *, struct group *,
30 char *, size_t, int *);
31 extern enum nss_status _nss_winbind_getgrgid_r(gid_t gid, struct group *, char *,
32 size_t, int *);
33 extern enum nss_status _nss_winbind_setgrent(void);
34 extern enum nss_status _nss_winbind_endgrent(void);
35 extern enum nss_status _nss_winbind_initgroups_dyn(char *, gid_t, long int *,
36 long int *, gid_t **, long int , int *);
38 extern enum nss_status _nss_winbind_getpwent_r(struct passwd *, char *, size_t,
39 int *);
40 extern enum nss_status _nss_winbind_getpwnam_r(const char *, struct passwd *,
41 char *, size_t, int *);
42 extern enum nss_status _nss_winbind_getpwuid_r(gid_t gid, struct passwd *, char *,
43 size_t, int *);
44 extern enum nss_status _nss_winbind_setpwent(void);
45 extern enum nss_status _nss_winbind_endpwent(void);
46 ns_mtab *nss_module_register(const char *, unsigned int *, nss_module_unregister_fn *);
48 NSS_METHOD_PROTOTYPE(__nss_compat_getgrnam_r);
49 NSS_METHOD_PROTOTYPE(__nss_compat_getgrgid_r);
50 NSS_METHOD_PROTOTYPE(__nss_compat_getgrent_r);
51 NSS_METHOD_PROTOTYPE(__nss_compat_setgrent);
52 NSS_METHOD_PROTOTYPE(__nss_compat_endgrent);
54 NSS_METHOD_PROTOTYPE(__nss_compat_getpwnam_r);
55 NSS_METHOD_PROTOTYPE(__nss_compat_getpwuid_r);
56 NSS_METHOD_PROTOTYPE(__nss_compat_getpwent_r);
57 NSS_METHOD_PROTOTYPE(__nss_compat_setpwent);
58 NSS_METHOD_PROTOTYPE(__nss_compat_endpwent);
59 NSS_METHOD_PROTOTYPE(__nss_compat_endpwent);
61 NSS_METHOD_PROTOTYPE(__freebsd_getgroupmembership);
63 static ns_mtab methods[] = {
64 { NSDB_GROUP, "getgrnam_r", __nss_compat_getgrnam_r, _nss_winbind_getgrnam_r },
65 { NSDB_GROUP, "getgrgid_r", __nss_compat_getgrgid_r, _nss_winbind_getgrgid_r },
66 { NSDB_GROUP, "getgrent_r", __nss_compat_getgrent_r, _nss_winbind_getgrent_r },
67 { NSDB_GROUP, "setgrent", __nss_compat_setgrent, _nss_winbind_setgrent },
68 { NSDB_GROUP, "endgrent", __nss_compat_endgrent, _nss_winbind_endgrent },
69 { NSDB_GROUP, "getgroupmembership", __freebsd_getgroupmembership, NULL },
71 { NSDB_PASSWD, "getpwnam_r", __nss_compat_getpwnam_r, _nss_winbind_getpwnam_r },
72 { NSDB_PASSWD, "getpwuid_r", __nss_compat_getpwuid_r, _nss_winbind_getpwuid_r },
73 { NSDB_PASSWD, "getpwent_r", __nss_compat_getpwent_r, _nss_winbind_getpwent_r },
74 { NSDB_PASSWD, "setpwent", __nss_compat_setpwent, _nss_winbind_setpwent },
75 { NSDB_PASSWD, "endpwent", __nss_compat_endpwent, _nss_winbind_endpwent },
79 /* Taken from libc */
80 static int
81 gr_addgid(gid_t gid, gid_t *groups, int maxgrp, int *grpcnt)
83 int ret, dupc;
85 /* skip duplicates */
86 for (dupc = 0; dupc < MIN(maxgrp, *grpcnt); dupc++) {
87 if (groups[dupc] == gid)
88 return 1;
91 ret = 1;
92 if (*grpcnt < maxgrp) /* add this gid */
93 groups[*grpcnt] = gid;
94 else
95 ret = 0;
97 (*grpcnt)++;
99 return ret;
103 rv = _nsdispatch(NULL, dtab, NSDB_GROUP, "getgroupmembership",
104 defaultsrc, uname, agroup, groups, maxgrp, grpcnt);
108 __freebsd_getgroupmembership(void *retval, void *mdata, va_list ap)
110 const char *uname = va_arg(ap, const char *);
111 gid_t group = va_arg(ap, gid_t);
112 gid_t *groups = va_arg(ap, gid_t *);
113 int maxgrp = va_arg(ap, int);
114 int *groupc = va_arg(ap, int *);
116 NSS_STATUS ret;
117 long int lcount, lsize;
118 int i, errnop;
119 gid_t *tmpgroups;
121 /* Can be realloc() inside _nss_winbind_initgroups_dyn() */
122 if ((tmpgroups=calloc(maxgrp, sizeof(gid_t))) == NULL) {
123 errno = ENOMEM;
124 return NS_TRYAGAIN;
127 lcount = 0;
128 lsize = maxgrp;
129 /* insert primary membership(possibly already there) */
130 gr_addgid(group, groups, maxgrp, groupc);
131 /* Don't limit number of groups, we want to know total size */
132 ret = _nss_winbind_initgroups_dyn(uname, group, &lcount, &lsize,
133 &tmpgroups, 0, &errnop);
134 if (ret == NSS_STATUS_SUCCESS) {
135 /* lcount potentially can be bigger than maxgrp, so would groupc */
136 for (i = 0; i < lcount; i++)
137 gr_addgid(tmpgroups[i], groups, maxgrp, groupc);
139 free(tmpgroups);
140 /* Let following nsswitch backend(s) add more groups(?) */
141 return NSS_STATUS_NOTFOUND;
144 ns_mtab *
145 nss_module_register(const char *source, unsigned int *mtabsize,
146 nss_module_unregister_fn *unreg)
148 *mtabsize = sizeof(methods)/sizeof(methods[0]);
149 *unreg = NULL;
150 return (methods);