1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
|
/*
Unix SMB/Netbios implementation.
Version 3.0
nss tester for winbindd
Copyright (C) Andrew Tridgell 2001
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include "includes.h"
static char *so_path = "/lib/libnss_winbind.so";
static int nss_errno;
static void *find_fn(const char *name)
{
static void *h;
void *res;
if (!h) {
h = dlopen(so_path, RTLD_LAZY);
}
if (!h) {
printf("Can't open shared library %s\n", so_path);
exit(1);
}
res = dlsym(h, name);
if (!res) {
printf("Can't find function %s\n", name);
exit(1);
}
return res;
}
static void report_nss_error(NSS_STATUS status)
{
if (status >= NSS_STATUS_SUCCESS) return;
printf("NSS_STATUS=%d %d\n", status, NSS_STATUS_SUCCESS);
}
static struct passwd *nss_getpwent(void)
{
NSS_STATUS (*_nss_getpwent_r)(struct passwd *, char *,
size_t , int *) = find_fn("_nss_winbind_getpwent_r");
static struct passwd pwd;
static char buf[1000];
NSS_STATUS status;
status = _nss_getpwent_r(&pwd, buf, sizeof(buf), &nss_errno);
if (status == NSS_STATUS_NOTFOUND) {
return NULL;
}
if (status == NSS_STATUS_RETURN) {
report_nss_error(status);
return NULL;
}
return &pwd;
}
static struct passwd *nss_getpwnam(const char *name)
{
NSS_STATUS (*_nss_getpwnam_r)(const char *, struct passwd *, char *,
size_t , int *) = find_fn("_nss_winbind_getpwnam_r");
static struct passwd pwd;
static char buf[1000];
NSS_STATUS status;
status = _nss_getpwnam_r(name, &pwd, buf, sizeof(buf), &nss_errno);
if (status == NSS_STATUS_NOTFOUND) {
return NULL;
}
if (status == NSS_STATUS_RETURN) {
report_nss_error(status);
return NULL;
}
return &pwd;
}
static struct passwd *nss_getpwuid(uid_t uid)
{
NSS_STATUS (*_nss_getpwuid_r)(uid_t , struct passwd *, char *,
size_t , int *) = find_fn("_nss_winbind_getpwuid_r");
static struct passwd pwd;
static char buf[1000];
NSS_STATUS status;
status = _nss_getpwuid_r(uid, &pwd, buf, sizeof(buf), &nss_errno);
if (status == NSS_STATUS_NOTFOUND) {
return NULL;
}
if (status == NSS_STATUS_RETURN) {
report_nss_error(status);
return NULL;
}
return &pwd;
}
static void nss_setpwent(void)
{
NSS_STATUS (*_nss_setpwent)(void) = find_fn("_nss_winbind_setpwent");
report_nss_error(_nss_setpwent());
}
static void nss_endpwent(void)
{
NSS_STATUS (*_nss_endpwent)(void) = find_fn("_nss_winbind_endpwent");
report_nss_error(_nss_endpwent());
}
static struct group *nss_getgrent(void)
{
NSS_STATUS (*_nss_getgrent_r)(struct group *, char *,
size_t , int *) = find_fn("_nss_winbind_getgrent_r");
static struct group grp;
static char buf[1000];
NSS_STATUS status;
status = _nss_getgrent_r(&grp, buf, sizeof(buf), &nss_errno);
if (status == NSS_STATUS_NOTFOUND) {
return NULL;
}
if (status == NSS_STATUS_RETURN) {
report_nss_error(status);
return NULL;
}
return &grp;
}
static struct group *nss_getgrnam(const char *name)
{
NSS_STATUS (*_nss_getgrnam_r)(const char *, struct group *, char *,
size_t , int *) = find_fn("_nss_winbind_getgrnam_r");
static struct group grp;
static char buf[1000];
NSS_STATUS status;
status = _nss_getgrnam_r(name, &grp, buf, sizeof(buf), &nss_errno);
if (status == NSS_STATUS_NOTFOUND) {
return NULL;
}
if (status == NSS_STATUS_RETURN) {
report_nss_error(status);
return NULL;
}
return &grp;
}
static struct group *nss_getgrgid(gid_t gid)
{
NSS_STATUS (*_nss_getgrgid_r)(gid_t , struct group *, char *,
size_t , int *) = find_fn("_nss_winbind_getgrgid_r");
static struct group grp;
static char buf[1000];
NSS_STATUS status;
status = _nss_getgrgid_r(gid, &grp, buf, sizeof(buf), &nss_errno);
if (status == NSS_STATUS_NOTFOUND) {
return NULL;
}
if (status == NSS_STATUS_RETURN) {
report_nss_error(status);
return NULL;
}
return &grp;
}
static void nss_setgrent(void)
{
NSS_STATUS (*_nss_setgrent)(void) = find_fn("_nss_winbind_setgrent");
report_nss_error(_nss_setgrent());
}
static void nss_endgrent(void)
{
NSS_STATUS (*_nss_endgrent)(void) = find_fn("_nss_winbind_endgrent");
report_nss_error(_nss_endgrent());
}
static int nss_initgroups(char *user, gid_t group, gid_t **groups, long int *start, long int *size)
{
NSS_STATUS (*_nss_initgroups)(char *, gid_t , long int *,
long int *, gid_t **, long int , int *) =
find_fn("_nss_winbind_initgroups_dyn");
NSS_STATUS status;
status = _nss_initgroups(user, group, start, size, groups, 0, &nss_errno);
report_nss_error(status);
return status;
}
static void print_passwd(struct passwd *pwd)
{
printf("%s:%s:%d:%d:%s:%s:%s\n",
pwd->pw_name,
pwd->pw_passwd,
pwd->pw_uid,
pwd->pw_gid,
pwd->pw_gecos,
pwd->pw_dir,
pwd->pw_shell);
}
static void print_group(struct group *grp)
{
int i;
printf("%s:%s:%d: ",
grp->gr_name,
grp->gr_passwd,
grp->gr_gid);
if (!grp->gr_mem[0]) {
printf("\n");
return;
}
for (i=0; grp->gr_mem[i+1]; i++) {
printf("%s, ", grp->gr_mem[i]);
}
printf("%s\n", grp->gr_mem[i]);
}
static void nss_test_initgroups(char *name, gid_t gid)
{
long int size = 16;
long int start = 1;
gid_t *groups = NULL;
int i;
groups = (gid_t *)malloc(size * sizeof(gid_t));
groups[0] = gid;
nss_initgroups(name, gid, &groups, &start, &size);
for (i=0; i<start-1; i++) {
printf("%d, ", groups[i]);
}
printf("%d\n", groups[i]);
}
static void nss_test_users(void)
{
struct passwd *pwd;
nss_setpwent();
/* loop over all users */
while ((pwd = nss_getpwent())) {
printf("Testing user %s\n", pwd->pw_name);
printf("getpwent: "); print_passwd(pwd);
pwd = nss_getpwnam(pwd->pw_name);
printf("getpwnam: "); print_passwd(pwd);
pwd = nss_getpwuid(pwd->pw_uid);
printf("getpwuid: "); print_passwd(pwd);
printf("initgroups: "); nss_test_initgroups(pwd->pw_name, pwd->pw_gid);
printf("\n");
}
nss_endpwent();
}
static void nss_test_groups(void)
{
struct group *grp;
nss_setgrent();
/* loop over all groups */
while ((grp = nss_getgrent())) {
printf("Testing group %s\n", grp->gr_name);
printf("getgrent: "); print_group(grp);
grp = nss_getgrnam(grp->gr_name);
printf("getgrnam: "); print_group(grp);
grp = nss_getgrgid(grp->gr_gid);
printf("getgrgid: "); print_group(grp);
printf("\n");
}
nss_endgrent();
}
int main(int argc, char *argv[])
{
if (argc > 1) so_path = argv[1];
nss_test_users();
nss_test_groups();
return 0;
}
|