00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "includes.h"
00023
00024 static const char *so_path = "/lib/libnss_winbind.so";
00025 static const char *nss_name = "winbind";
00026 static int nss_errno;
00027 static NSS_STATUS last_error;
00028 static int total_errors;
00029
00030 static void *find_fn(const char *name)
00031 {
00032 pstring s;
00033 static void *h;
00034 void *res;
00035
00036 pstr_sprintf(s, "_nss_%s_%s", nss_name, name);
00037
00038 if (!h) {
00039 h = sys_dlopen(so_path, RTLD_LAZY);
00040 }
00041 if (!h) {
00042 printf("Can't open shared library %s\n", so_path);
00043 exit(1);
00044 }
00045 res = sys_dlsym(h, s);
00046 if (!res) {
00047 printf("Can't find function %s\n", s);
00048 total_errors++;
00049 return NULL;
00050 }
00051 return res;
00052 }
00053
00054 static void report_nss_error(const char *who, NSS_STATUS status)
00055 {
00056 last_error = status;
00057 total_errors++;
00058 printf("ERROR %s: NSS_STATUS=%d %d (nss_errno=%d)\n",
00059 who, status, NSS_STATUS_SUCCESS, nss_errno);
00060 }
00061
00062 static struct passwd *nss_getpwent(void)
00063 {
00064 NSS_STATUS (*_nss_getpwent_r)(struct passwd *, char *,
00065 size_t , int *) =
00066 (NSS_STATUS (*)(struct passwd *, char *,
00067 size_t, int *))find_fn("getpwent_r");
00068 static struct passwd pwd;
00069 static char buf[1000];
00070 NSS_STATUS status;
00071
00072 if (!_nss_getpwent_r)
00073 return NULL;
00074
00075 status = _nss_getpwent_r(&pwd, buf, sizeof(buf), &nss_errno);
00076 if (status == NSS_STATUS_NOTFOUND) {
00077 return NULL;
00078 }
00079 if (status != NSS_STATUS_SUCCESS) {
00080 report_nss_error("getpwent", status);
00081 return NULL;
00082 }
00083 return &pwd;
00084 }
00085
00086 static struct passwd *nss_getpwnam(const char *name)
00087 {
00088 NSS_STATUS (*_nss_getpwnam_r)(const char *, struct passwd *, char *,
00089 size_t , int *) =
00090 (NSS_STATUS (*)(const char *, struct passwd *, char *,
00091 size_t, int *))find_fn("getpwnam_r");
00092 static struct passwd pwd;
00093 static char buf[1000];
00094 NSS_STATUS status;
00095
00096 if (!_nss_getpwnam_r)
00097 return NULL;
00098
00099 status = _nss_getpwnam_r(name, &pwd, buf, sizeof(buf), &nss_errno);
00100 if (status == NSS_STATUS_NOTFOUND) {
00101 return NULL;
00102 }
00103 if (status != NSS_STATUS_SUCCESS) {
00104 report_nss_error("getpwnam", status);
00105 return NULL;
00106 }
00107 return &pwd;
00108 }
00109
00110 static struct passwd *nss_getpwuid(uid_t uid)
00111 {
00112 NSS_STATUS (*_nss_getpwuid_r)(uid_t , struct passwd *, char *,
00113 size_t , int *) =
00114 (NSS_STATUS (*)(uid_t, struct passwd *, char *,
00115 size_t, int *))find_fn("getpwuid_r");
00116 static struct passwd pwd;
00117 static char buf[1000];
00118 NSS_STATUS status;
00119
00120 if (!_nss_getpwuid_r)
00121 return NULL;
00122
00123 status = _nss_getpwuid_r(uid, &pwd, buf, sizeof(buf), &nss_errno);
00124 if (status == NSS_STATUS_NOTFOUND) {
00125 return NULL;
00126 }
00127 if (status != NSS_STATUS_SUCCESS) {
00128 report_nss_error("getpwuid", status);
00129 return NULL;
00130 }
00131 return &pwd;
00132 }
00133
00134 static void nss_setpwent(void)
00135 {
00136 NSS_STATUS (*_nss_setpwent)(void) =
00137 (NSS_STATUS(*)(void))find_fn("setpwent");
00138 NSS_STATUS status;
00139
00140 if (!_nss_setpwent)
00141 return;
00142
00143 status = _nss_setpwent();
00144 if (status != NSS_STATUS_SUCCESS) {
00145 report_nss_error("setpwent", status);
00146 }
00147 }
00148
00149 static void nss_endpwent(void)
00150 {
00151 NSS_STATUS (*_nss_endpwent)(void) =
00152 (NSS_STATUS (*)(void))find_fn("endpwent");
00153 NSS_STATUS status;
00154
00155 if (!_nss_endpwent)
00156 return;
00157
00158 status = _nss_endpwent();
00159 if (status != NSS_STATUS_SUCCESS) {
00160 report_nss_error("endpwent", status);
00161 }
00162 }
00163
00164
00165 static struct group *nss_getgrent(void)
00166 {
00167 NSS_STATUS (*_nss_getgrent_r)(struct group *, char *,
00168 size_t , int *) =
00169 (NSS_STATUS (*)(struct group *, char *,
00170 size_t, int *))find_fn("getgrent_r");
00171 static struct group grp;
00172 static char *buf;
00173 static int buflen = 1024;
00174 NSS_STATUS status;
00175
00176 if (!_nss_getgrent_r)
00177 return NULL;
00178
00179 if (!buf)
00180 buf = SMB_MALLOC_ARRAY(char, buflen);
00181
00182 again:
00183 status = _nss_getgrent_r(&grp, buf, buflen, &nss_errno);
00184 if (status == NSS_STATUS_TRYAGAIN) {
00185 buflen *= 2;
00186 buf = SMB_REALLOC_ARRAY(buf, char, buflen);
00187 if (!buf) {
00188 return NULL;
00189 }
00190 goto again;
00191 }
00192 if (status == NSS_STATUS_NOTFOUND) {
00193 SAFE_FREE(buf);
00194 return NULL;
00195 }
00196 if (status != NSS_STATUS_SUCCESS) {
00197 report_nss_error("getgrent", status);
00198 SAFE_FREE(buf);
00199 return NULL;
00200 }
00201 SAFE_FREE(buf);
00202 return &grp;
00203 }
00204
00205 static struct group *nss_getgrnam(const char *name)
00206 {
00207 NSS_STATUS (*_nss_getgrnam_r)(const char *, struct group *, char *,
00208 size_t , int *) =
00209 (NSS_STATUS (*)(const char *, struct group *, char *,
00210 size_t, int *))find_fn("getgrnam_r");
00211 static struct group grp;
00212 static char *buf;
00213 static int buflen = 1000;
00214 NSS_STATUS status;
00215
00216 if (!_nss_getgrnam_r)
00217 return NULL;
00218
00219 if (!buf)
00220 buf = SMB_MALLOC_ARRAY(char, buflen);
00221 again:
00222 status = _nss_getgrnam_r(name, &grp, buf, buflen, &nss_errno);
00223 if (status == NSS_STATUS_TRYAGAIN) {
00224 buflen *= 2;
00225 buf = SMB_REALLOC_ARRAY(buf, char, buflen);
00226 if (!buf) {
00227 return NULL;
00228 }
00229 goto again;
00230 }
00231 if (status == NSS_STATUS_NOTFOUND) {
00232 SAFE_FREE(buf);
00233 return NULL;
00234 }
00235 if (status != NSS_STATUS_SUCCESS) {
00236 report_nss_error("getgrnam", status);
00237 SAFE_FREE(buf);
00238 return NULL;
00239 }
00240 SAFE_FREE(buf);
00241 return &grp;
00242 }
00243
00244 static struct group *nss_getgrgid(gid_t gid)
00245 {
00246 NSS_STATUS (*_nss_getgrgid_r)(gid_t , struct group *, char *,
00247 size_t , int *) =
00248 (NSS_STATUS (*)(gid_t, struct group *, char *,
00249 size_t, int *))find_fn("getgrgid_r");
00250 static struct group grp;
00251 static char *buf;
00252 static int buflen = 1000;
00253 NSS_STATUS status;
00254
00255 if (!_nss_getgrgid_r)
00256 return NULL;
00257
00258 if (!buf)
00259 buf = SMB_MALLOC_ARRAY(char, buflen);
00260
00261 again:
00262 status = _nss_getgrgid_r(gid, &grp, buf, buflen, &nss_errno);
00263 if (status == NSS_STATUS_TRYAGAIN) {
00264 buflen *= 2;
00265 buf = SMB_REALLOC_ARRAY(buf, char, buflen);
00266 if (!buf) {
00267 return NULL;
00268 }
00269 goto again;
00270 }
00271 if (status == NSS_STATUS_NOTFOUND) {
00272 SAFE_FREE(buf);
00273 return NULL;
00274 }
00275 if (status != NSS_STATUS_SUCCESS) {
00276 report_nss_error("getgrgid", status);
00277 SAFE_FREE(buf);
00278 return NULL;
00279 }
00280 SAFE_FREE(buf);
00281 return &grp;
00282 }
00283
00284 static void nss_setgrent(void)
00285 {
00286 NSS_STATUS (*_nss_setgrent)(void) =
00287 (NSS_STATUS (*)(void))find_fn("setgrent");
00288 NSS_STATUS status;
00289
00290 if (!_nss_setgrent)
00291 return;
00292
00293 status = _nss_setgrent();
00294 if (status != NSS_STATUS_SUCCESS) {
00295 report_nss_error("setgrent", status);
00296 }
00297 }
00298
00299 static void nss_endgrent(void)
00300 {
00301 NSS_STATUS (*_nss_endgrent)(void) =
00302 (NSS_STATUS (*)(void))find_fn("endgrent");
00303 NSS_STATUS status;
00304
00305 if (!_nss_endgrent)
00306 return;
00307
00308 status = _nss_endgrent();
00309 if (status != NSS_STATUS_SUCCESS) {
00310 report_nss_error("endgrent", status);
00311 }
00312 }
00313
00314 static int nss_initgroups(char *user, gid_t group, gid_t **groups, long int *start, long int *size)
00315 {
00316 NSS_STATUS (*_nss_initgroups)(char *, gid_t , long int *,
00317 long int *, gid_t **, long int , int *) =
00318 (NSS_STATUS (*)(char *, gid_t, long int *,
00319 long int *, gid_t **,
00320 long int, int *))find_fn("initgroups_dyn");
00321 NSS_STATUS status;
00322
00323 if (!_nss_initgroups)
00324 return NSS_STATUS_UNAVAIL;
00325
00326 status = _nss_initgroups(user, group, start, size, groups, 0, &nss_errno);
00327 if (status != NSS_STATUS_SUCCESS) {
00328 report_nss_error("initgroups", status);
00329 }
00330 return status;
00331 }
00332
00333 static void print_passwd(struct passwd *pwd)
00334 {
00335 printf("%s:%s:%lu:%lu:%s:%s:%s\n",
00336 pwd->pw_name,
00337 pwd->pw_passwd,
00338 (unsigned long)pwd->pw_uid,
00339 (unsigned long)pwd->pw_gid,
00340 pwd->pw_gecos,
00341 pwd->pw_dir,
00342 pwd->pw_shell);
00343 }
00344
00345 static void print_group(struct group *grp)
00346 {
00347 int i;
00348 printf("%s:%s:%lu: ",
00349 grp->gr_name,
00350 grp->gr_passwd,
00351 (unsigned long)grp->gr_gid);
00352
00353 if (!grp->gr_mem[0]) {
00354 printf("\n");
00355 return;
00356 }
00357
00358 for (i=0; grp->gr_mem[i+1]; i++) {
00359 printf("%s, ", grp->gr_mem[i]);
00360 }
00361 printf("%s\n", grp->gr_mem[i]);
00362 }
00363
00364 static void nss_test_initgroups(char *name, gid_t gid)
00365 {
00366 long int size = 16;
00367 long int start = 1;
00368 gid_t *groups = NULL;
00369 int i;
00370 NSS_STATUS status;
00371
00372 groups = SMB_MALLOC_ARRAY(gid_t, size);
00373 groups[0] = gid;
00374
00375 status = nss_initgroups(name, gid, &groups, &start, &size);
00376 if (status == NSS_STATUS_UNAVAIL) {
00377 printf("No initgroups fn\n");
00378 return;
00379 }
00380
00381 for (i=0; i<start-1; i++) {
00382 printf("%lu, ", (unsigned long)groups[i]);
00383 }
00384 printf("%lu\n", (unsigned long)groups[i]);
00385 }
00386
00387
00388 static void nss_test_users(void)
00389 {
00390 struct passwd *pwd;
00391
00392 nss_setpwent();
00393
00394 while ((pwd = nss_getpwent())) {
00395 printf("Testing user %s\n", pwd->pw_name);
00396 printf("getpwent: "); print_passwd(pwd);
00397 pwd = nss_getpwuid(pwd->pw_uid);
00398 if (!pwd) {
00399 total_errors++;
00400 printf("ERROR: can't getpwuid\n");
00401 continue;
00402 }
00403 printf("getpwuid: "); print_passwd(pwd);
00404 pwd = nss_getpwnam(pwd->pw_name);
00405 if (!pwd) {
00406 total_errors++;
00407 printf("ERROR: can't getpwnam\n");
00408 continue;
00409 }
00410 printf("getpwnam: "); print_passwd(pwd);
00411 printf("initgroups: "); nss_test_initgroups(pwd->pw_name, pwd->pw_gid);
00412 printf("\n");
00413 }
00414 nss_endpwent();
00415 }
00416
00417 static void nss_test_groups(void)
00418 {
00419 struct group *grp;
00420
00421 nss_setgrent();
00422
00423 while ((grp = nss_getgrent())) {
00424 printf("Testing group %s\n", grp->gr_name);
00425 printf("getgrent: "); print_group(grp);
00426 grp = nss_getgrnam(grp->gr_name);
00427 if (!grp) {
00428 total_errors++;
00429 printf("ERROR: can't getgrnam\n");
00430 continue;
00431 }
00432 printf("getgrnam: "); print_group(grp);
00433 grp = nss_getgrgid(grp->gr_gid);
00434 if (!grp) {
00435 total_errors++;
00436 printf("ERROR: can't getgrgid\n");
00437 continue;
00438 }
00439 printf("getgrgid: "); print_group(grp);
00440 printf("\n");
00441 }
00442 nss_endgrent();
00443 }
00444
00445 static void nss_test_errors(void)
00446 {
00447 struct passwd *pwd;
00448 struct group *grp;
00449
00450 pwd = getpwnam("nosuchname");
00451 if (pwd || last_error != NSS_STATUS_NOTFOUND) {
00452 total_errors++;
00453 printf("ERROR Non existant user gave error %d\n", last_error);
00454 }
00455
00456 pwd = getpwuid(0xFFF0);
00457 if (pwd || last_error != NSS_STATUS_NOTFOUND) {
00458 total_errors++;
00459 printf("ERROR Non existant uid gave error %d\n", last_error);
00460 }
00461
00462 grp = getgrnam("nosuchgroup");
00463 if (grp || last_error != NSS_STATUS_NOTFOUND) {
00464 total_errors++;
00465 printf("ERROR Non existant group gave error %d\n", last_error);
00466 }
00467
00468 grp = getgrgid(0xFFF0);
00469 if (grp || last_error != NSS_STATUS_NOTFOUND) {
00470 total_errors++;
00471 printf("ERROR Non existant gid gave error %d\n", last_error);
00472 }
00473 }
00474
00475 int main(int argc, char *argv[])
00476 {
00477 if (argc > 1) so_path = argv[1];
00478 if (argc > 2) nss_name = argv[2];
00479
00480 nss_test_users();
00481 nss_test_groups();
00482 nss_test_errors();
00483
00484 printf("total_errors=%d\n", total_errors);
00485
00486 return total_errors;
00487 }