2 Unix SMB/CIFS implementation.
4 local testing of the nss wrapper
6 Copyright (C) Guenther Deschner 2009
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include "torture/torture.h"
24 #include "lib/replace/system/passwd.h"
25 #include "lib/nss_wrapper/nss_wrapper.h"
27 static bool copy_passwd(struct torture_context *tctx,
28 const struct passwd *pwd,
31 p->pw_name = talloc_strdup(tctx, pwd->pw_name);
32 p->pw_passwd = talloc_strdup(tctx, pwd->pw_passwd);
33 p->pw_uid = pwd->pw_uid;
34 p->pw_gid = pwd->pw_gid;
35 p->pw_gecos = talloc_strdup(tctx, pwd->pw_gecos);
36 p->pw_dir = talloc_strdup(tctx, pwd->pw_dir);
37 p->pw_shell = talloc_strdup(tctx, pwd->pw_shell);
42 static void print_passwd(struct passwd *pwd)
44 printf("%s:%s:%lu:%lu:%s:%s:%s\n",
47 (unsigned long)pwd->pw_uid,
48 (unsigned long)pwd->pw_gid,
55 static bool test_nwrap_getpwnam(struct torture_context *tctx,
61 torture_comment(tctx, "Testing getpwnam: %s\n", name);
69 copy_passwd(tctx, pwd, pwd_p);
72 return pwd ? true : false;
75 static bool test_nwrap_getpwuid(struct torture_context *tctx,
81 torture_comment(tctx, "Testing getpwuid: %lu\n", (unsigned long)uid);
89 copy_passwd(tctx, pwd, pwd_p);
92 return pwd ? true : false;
95 static bool copy_group(struct torture_context *tctx,
96 const struct group *grp,
101 g->gr_name = talloc_strdup(tctx, grp->gr_name);
102 g->gr_passwd = talloc_strdup(tctx, grp->gr_passwd);
103 g->gr_gid = grp->gr_gid;
106 for (i=0; grp->gr_mem && grp->gr_mem[i]; i++) {
107 g->gr_mem = talloc_realloc(tctx, g->gr_mem, char *, i + 2);
108 g->gr_mem[i] = talloc_strdup(g->gr_mem, grp->gr_mem[i]);
109 g->gr_mem[i+1] = NULL;
115 static void print_group(struct group *grp)
121 (unsigned long)grp->gr_gid);
123 if (!grp->gr_mem[0]) {
128 for (i=0; grp->gr_mem[i+1]; i++) {
129 printf("%s,", grp->gr_mem[i]);
131 printf("%s\n", grp->gr_mem[i]);
134 static bool test_nwrap_getgrnam(struct torture_context *tctx,
140 torture_comment(tctx, "Testing getgrnam: %s\n", name);
142 grp = getgrnam(name);
148 copy_group(tctx, grp, grp_p);
151 return grp ? true : false;
154 static bool test_nwrap_getgrgid(struct torture_context *tctx,
160 torture_comment(tctx, "Testing getgrgid: %lu\n", (unsigned long)gid);
168 copy_group(tctx, grp, grp_p);
171 return grp ? true : false;
174 static bool test_nwrap_enum_passwd(struct torture_context *tctx,
175 struct passwd **pwd_array_p,
179 struct passwd *pwd_array = NULL;
182 torture_comment(tctx, "Testing setpwent\n");
185 while ((pwd = getpwent()) != NULL) {
186 torture_comment(tctx, "Testing getpwent\n");
189 if (pwd_array_p && num_pwd_p) {
190 pwd_array = talloc_realloc(tctx, pwd_array, struct passwd, num_pwd+1);
191 torture_assert(tctx, pwd_array, "out of memory");
192 copy_passwd(tctx, pwd, &pwd_array[num_pwd]);
197 torture_comment(tctx, "Testing endpwent\n");
201 *pwd_array_p = pwd_array;
204 *num_pwd_p = num_pwd;
210 static bool torture_assert_passwd_equal(struct torture_context *tctx,
211 const struct passwd *p1,
212 const struct passwd *p2,
215 torture_assert_str_equal(tctx, p1->pw_name, p2->pw_name, comment);
216 torture_assert_str_equal(tctx, p1->pw_passwd, p2->pw_passwd, comment);
217 torture_assert_int_equal(tctx, p1->pw_uid, p2->pw_uid, comment);
218 torture_assert_int_equal(tctx, p1->pw_gid, p2->pw_gid, comment);
219 torture_assert_str_equal(tctx, p1->pw_gecos, p2->pw_gecos, comment);
220 torture_assert_str_equal(tctx, p1->pw_dir, p2->pw_dir, comment);
221 torture_assert_str_equal(tctx, p1->pw_shell, p2->pw_shell, comment);
226 static bool test_nwrap_passwd(struct torture_context *tctx)
229 struct passwd *pwd, pwd1, pwd2;
232 torture_assert(tctx, test_nwrap_enum_passwd(tctx, &pwd, &num_pwd),
233 "failed to enumerate passwd");
235 for (i=0; i < num_pwd; i++) {
236 torture_assert(tctx, test_nwrap_getpwnam(tctx, pwd[i].pw_name, &pwd1),
237 "failed to call getpwnam for enumerated user");
238 torture_assert_passwd_equal(tctx, &pwd[i], &pwd1,
239 "getpwent and getpwnam gave different results");
240 torture_assert(tctx, test_nwrap_getpwuid(tctx, pwd[i].pw_uid, &pwd2),
241 "failed to call getpwuid for enumerated user");
242 torture_assert_passwd_equal(tctx, &pwd[i], &pwd2,
243 "getpwent and getpwuid gave different results");
244 torture_assert_passwd_equal(tctx, &pwd1, &pwd2,
245 "getpwnam and getpwuid gave different results");
251 static bool test_nwrap_enum_group(struct torture_context *tctx,
252 struct group **grp_array_p,
256 struct group *grp_array = NULL;
259 torture_comment(tctx, "Testing setgrent\n");
262 while ((grp = getgrent()) != NULL) {
263 torture_comment(tctx, "Testing getgrent\n");
266 if (grp_array_p && num_grp_p) {
267 grp_array = talloc_realloc(tctx, grp_array, struct group, num_grp+1);
268 torture_assert(tctx, grp_array, "out of memory");
269 copy_group(tctx, grp, &grp_array[num_grp]);
274 torture_comment(tctx, "Testing endgrent\n");
278 *grp_array_p = grp_array;
281 *num_grp_p = num_grp;
287 static bool torture_assert_group_equal(struct torture_context *tctx,
288 const struct group *g1,
289 const struct group *g2,
293 torture_assert_str_equal(tctx, g1->gr_name, g2->gr_name, comment);
294 torture_assert_str_equal(tctx, g1->gr_passwd, g2->gr_passwd, comment);
295 torture_assert_int_equal(tctx, g1->gr_gid, g2->gr_gid, comment);
296 if (g1->gr_mem && !g2->gr_mem) {
299 if (!g1->gr_mem && g2->gr_mem) {
302 if (!g1->gr_mem && !g2->gr_mem) {
305 for (i=0; g1->gr_mem[i] && g2->gr_mem[i]; i++) {
306 torture_assert_str_equal(tctx, g1->gr_mem[i], g2->gr_mem[i], comment);
312 static bool test_nwrap_group(struct torture_context *tctx)
315 struct group *grp, grp1, grp2;
318 torture_assert(tctx, test_nwrap_enum_group(tctx, &grp, &num_grp),
319 "failed to enumerate group");
321 for (i=0; i < num_grp; i++) {
322 torture_assert(tctx, test_nwrap_getgrnam(tctx, grp[i].gr_name, &grp1),
323 "failed to call getgrnam for enumerated user");
324 torture_assert_group_equal(tctx, &grp[i], &grp1,
325 "getgrent and getgrnam gave different results");
326 torture_assert(tctx, test_nwrap_getgrgid(tctx, grp[i].gr_gid, &grp2),
327 "failed to call getgrgid for enumerated user");
328 torture_assert_group_equal(tctx, &grp[i], &grp2,
329 "getgrent and getgruid gave different results");
330 torture_assert_group_equal(tctx, &grp1, &grp2,
331 "getgrnam and getgrgid gave different results");
337 static bool test_nwrap_getgrouplist(struct torture_context *tctx,
345 gid_t *groups = NULL;
347 torture_comment(tctx, "Testing getgrouplist: %s\n", user);
349 ret = getgrouplist(user, gid, NULL, &num_groups);
350 if (ret == -1 || num_groups != 0) {
352 groups = talloc_array(tctx, gid_t, num_groups);
353 torture_assert(tctx, groups, "out of memory\n");
355 ret = getgrouplist(user, gid, groups, &num_groups);
358 torture_assert(tctx, (ret != -1), "failed to call getgrouplist");
360 torture_comment(tctx, "%s is member in %d groups\n", user, num_groups);
366 *num_gids_p = num_groups;
372 static bool test_nwrap_user_in_group(struct torture_context *tctx,
373 const struct passwd *pwd,
374 const struct group *grp)
378 for (i=0; grp->gr_mem && grp->gr_mem[i] != NULL; i++) {
379 if (strequal(grp->gr_mem[i], pwd->pw_name)) {
387 static bool test_nwrap_membership_user(struct torture_context *tctx,
388 const struct passwd *pwd,
389 struct group *grp_array,
392 int num_user_groups = 0;
393 int num_user_groups_from_enum = 0;
394 gid_t *user_groups = NULL;
396 bool primary_group_had_user_member = false;
398 torture_assert(tctx, test_nwrap_getgrouplist(tctx,
403 "failed to test getgrouplist");
405 for (g=0; g < num_user_groups; g++) {
406 torture_assert(tctx, test_nwrap_getgrgid(tctx, user_groups[g], NULL),
407 "failed to find the group the user is a member of");
411 for (i=0; i < num_grp; i++) {
413 struct group grp = grp_array[i];
415 if (test_nwrap_user_in_group(tctx, pwd, &grp)) {
417 struct group current_grp;
418 num_user_groups_from_enum++;
420 torture_assert(tctx, test_nwrap_getgrnam(tctx, grp.gr_name, ¤t_grp),
421 "failed to find the group the user is a member of");
423 if (current_grp.gr_gid == pwd->pw_gid) {
424 torture_comment(tctx, "primary group %s of user %s lists user as member\n",
427 primary_group_had_user_member = true;
434 if (!primary_group_had_user_member) {
435 num_user_groups_from_enum++;
438 torture_assert_int_equal(tctx, num_user_groups, num_user_groups_from_enum,
439 "getgrouplist and real inspection of grouplist gave different results\n");
444 static bool test_nwrap_membership(struct torture_context *tctx)
446 const char *old_pwd = getenv("NSS_WRAPPER_PASSWD");
447 const char *old_group = getenv("NSS_WRAPPER_GROUP");
454 if (!old_pwd || !old_group) {
455 torture_skip(tctx, "nothing to test\n");
459 torture_assert(tctx, test_nwrap_enum_passwd(tctx, &pwd, &num_pwd),
460 "failed to enumerate passwd");
461 torture_assert(tctx, test_nwrap_enum_group(tctx, &grp, &num_grp),
462 "failed to enumerate group");
464 for (i=0; i < num_pwd; i++) {
466 torture_assert(tctx, test_nwrap_membership_user(tctx, &pwd[i], grp, num_grp),
467 "failed to test membership for user");
474 static bool test_nwrap_env(struct torture_context *tctx)
476 const char *old_pwd = getenv("NSS_WRAPPER_PASSWD");
477 const char *old_group = getenv("NSS_WRAPPER_GROUP");
479 if (!old_pwd || !old_group) {
480 torture_skip(tctx, "nothing to test\n");
484 torture_assert(tctx, test_nwrap_passwd(tctx),
485 "failed to test users");
486 torture_assert(tctx, test_nwrap_group(tctx),
487 "failed to test groups");
492 struct torture_suite *torture_local_nss_wrapper(TALLOC_CTX *mem_ctx)
494 struct torture_suite *suite = torture_suite_create(mem_ctx, "NSS-WRAPPER");
496 torture_suite_add_simple_test(suite, "env", test_nwrap_env);
497 torture_suite_add_simple_test(suite, "membership", test_nwrap_membership);