smbd: Make a fake file's stat a valid regular file
[Samba.git] / nsswitch / winbind_nss_freebsd.c
blobd3f54894dc2b6af034f62ec8628989df99f6addd
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 ns_mtab *nss_module_register(const char *, unsigned int *, nss_module_unregister_fn *);
29 NSS_METHOD_PROTOTYPE(__nss_compat_getgrnam_r);
30 NSS_METHOD_PROTOTYPE(__nss_compat_getgrgid_r);
31 NSS_METHOD_PROTOTYPE(__nss_compat_getgrent_r);
32 NSS_METHOD_PROTOTYPE(__nss_compat_setgrent);
33 NSS_METHOD_PROTOTYPE(__nss_compat_endgrent);
35 NSS_METHOD_PROTOTYPE(__nss_compat_getpwnam_r);
36 NSS_METHOD_PROTOTYPE(__nss_compat_getpwuid_r);
37 NSS_METHOD_PROTOTYPE(__nss_compat_getpwent_r);
38 NSS_METHOD_PROTOTYPE(__nss_compat_setpwent);
39 NSS_METHOD_PROTOTYPE(__nss_compat_endpwent);
40 NSS_METHOD_PROTOTYPE(__nss_compat_endpwent);
42 static NSS_METHOD_PROTOTYPE(__freebsd_getgroupmembership);
44 static ns_mtab methods[] = {
45 { NSDB_GROUP, "getgrnam_r", __nss_compat_getgrnam_r, _nss_winbind_getgrnam_r },
46 { NSDB_GROUP, "getgrgid_r", __nss_compat_getgrgid_r, _nss_winbind_getgrgid_r },
47 { NSDB_GROUP, "getgrent_r", __nss_compat_getgrent_r, _nss_winbind_getgrent_r },
48 { NSDB_GROUP, "setgrent", __nss_compat_setgrent, _nss_winbind_setgrent },
49 { NSDB_GROUP, "endgrent", __nss_compat_endgrent, _nss_winbind_endgrent },
50 { NSDB_GROUP, "getgroupmembership", __freebsd_getgroupmembership, NULL },
52 { NSDB_PASSWD, "getpwnam_r", __nss_compat_getpwnam_r, _nss_winbind_getpwnam_r },
53 { NSDB_PASSWD, "getpwuid_r", __nss_compat_getpwuid_r, _nss_winbind_getpwuid_r },
54 { NSDB_PASSWD, "getpwent_r", __nss_compat_getpwent_r, _nss_winbind_getpwent_r },
55 { NSDB_PASSWD, "setpwent", __nss_compat_setpwent, _nss_winbind_setpwent },
56 { NSDB_PASSWD, "endpwent", __nss_compat_endpwent, _nss_winbind_endpwent },
60 /* Taken from libc */
61 static int
62 gr_addgid(gid_t gid, gid_t *groups, int maxgrp, int *grpcnt)
64 int ret, dupc;
66 /* skip duplicates */
67 for (dupc = 0; dupc < MIN(maxgrp, *grpcnt); dupc++) {
68 if (groups[dupc] == gid)
69 return 1;
72 ret = 1;
73 if (*grpcnt < maxgrp) /* add this gid */
74 groups[*grpcnt] = gid;
75 else
76 ret = 0;
78 (*grpcnt)++;
80 return ret;
84 rv = _nsdispatch(NULL, dtab, NSDB_GROUP, "getgroupmembership",
85 defaultsrc, uname, agroup, groups, maxgrp, grpcnt);
88 static int
89 __freebsd_getgroupmembership(void *retval, void *mdata, va_list ap)
91 const char *uname = va_arg(ap, const char *);
92 gid_t group = va_arg(ap, gid_t);
93 gid_t *groups = va_arg(ap, gid_t *);
94 int maxgrp = va_arg(ap, int);
95 int *groupc = va_arg(ap, int *);
97 NSS_STATUS ret;
98 long int lcount, lsize;
99 int i, errnop;
100 gid_t *tmpgroups;
102 /* Can be realloc() inside _nss_winbind_initgroups_dyn() */
103 if ((tmpgroups=calloc(maxgrp, sizeof(gid_t))) == NULL) {
104 errno = ENOMEM;
105 return NS_TRYAGAIN;
108 lcount = 0;
109 lsize = maxgrp;
110 /* insert primary membership(possibly already there) */
111 gr_addgid(group, groups, maxgrp, groupc);
112 /* Don't limit number of groups, we want to know total size */
113 ret = _nss_winbind_initgroups_dyn(discard_const(uname),
114 group,
115 &lcount,
116 &lsize,
117 &tmpgroups,
119 &errnop);
120 if (ret == NSS_STATUS_SUCCESS) {
121 /* lcount potentially can be bigger than maxgrp, so would groupc */
122 for (i = 0; i < lcount; i++)
123 gr_addgid(tmpgroups[i], groups, maxgrp, groupc);
125 free(tmpgroups);
126 /* Let following nsswitch backend(s) add more groups(?) */
127 return NSS_STATUS_NOTFOUND;
130 _PUBLIC_ ns_mtab *
131 nss_module_register(const char *source, unsigned int *mtabsize,
132 nss_module_unregister_fn *unreg)
134 *mtabsize = sizeof(methods)/sizeof(methods[0]);
135 *unreg = NULL;
136 return (methods);