nss_wrapper: add more coherency checks for user and group enumeration.
[ira/wip.git] / lib / nss_wrapper / testsuite.c
1 /*
2    Unix SMB/CIFS implementation.
3
4    local testing of the nss wrapper
5
6    Copyright (C) Guenther Deschner 2009
7
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.
12
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.
17
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/>.
20 */
21
22 #include "includes.h"
23 #include "torture/torture.h"
24 #include "lib/replace/system/passwd.h"
25 #include "lib/nss_wrapper/nss_wrapper.h"
26
27 static bool copy_passwd(struct torture_context *tctx,
28                         const struct passwd *pwd,
29                         struct passwd *p)
30 {
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);
38
39         return true;
40 }
41
42 static void print_passwd(struct passwd *pwd)
43 {
44         printf("%s:%s:%lu:%lu:%s:%s:%s\n",
45                pwd->pw_name,
46                pwd->pw_passwd,
47                (unsigned long)pwd->pw_uid,
48                (unsigned long)pwd->pw_gid,
49                pwd->pw_gecos,
50                pwd->pw_dir,
51                pwd->pw_shell);
52 }
53
54
55 static bool test_nwrap_getpwnam(struct torture_context *tctx,
56                                 const char *name,
57                                 struct passwd *pwd_p)
58 {
59         struct passwd *pwd;
60
61         torture_comment(tctx, "Testing getpwnam: %s\n", name);
62
63         pwd = getpwnam(name);
64         if (pwd) {
65                 print_passwd(pwd);
66         }
67
68         if (pwd_p) {
69                 copy_passwd(tctx, pwd, pwd_p);
70         }
71
72         return pwd ? true : false;
73 }
74
75 static bool test_nwrap_getpwuid(struct torture_context *tctx,
76                                 uid_t uid,
77                                 struct passwd *pwd_p)
78 {
79         struct passwd *pwd;
80
81         torture_comment(tctx, "Testing getpwuid: %lu\n", (unsigned long)uid);
82
83         pwd = getpwuid(uid);
84         if (pwd) {
85                 print_passwd(pwd);
86         }
87
88         if (pwd_p) {
89                 copy_passwd(tctx, pwd, pwd_p);
90         }
91
92         return pwd ? true : false;
93 }
94
95 static bool copy_group(struct torture_context *tctx,
96                        const struct group *grp,
97                        struct group *g)
98 {
99         int i;
100
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;
104         g->gr_mem       = NULL;
105
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;
110         }
111
112         return true;
113 }
114
115 static void print_group(struct group *grp)
116 {
117         int i;
118         printf("%s:%s:%lu:",
119                grp->gr_name,
120                grp->gr_passwd,
121                (unsigned long)grp->gr_gid);
122
123         if (!grp->gr_mem[0]) {
124                 printf("\n");
125                 return;
126         }
127
128         for (i=0; grp->gr_mem[i+1]; i++) {
129                 printf("%s,", grp->gr_mem[i]);
130         }
131         printf("%s\n", grp->gr_mem[i]);
132 }
133
134 static bool test_nwrap_getgrnam(struct torture_context *tctx,
135                                 const char *name,
136                                 struct group *grp_p)
137 {
138         struct group *grp;
139
140         torture_comment(tctx, "Testing getgrnam: %s\n", name);
141
142         grp = getgrnam(name);
143         if (grp) {
144                 print_group(grp);
145         }
146
147         if (grp_p) {
148                 copy_group(tctx, grp, grp_p);
149         }
150
151         return grp ? true : false;
152 }
153
154 static bool test_nwrap_getgrgid(struct torture_context *tctx,
155                                 gid_t gid,
156                                 struct group *grp_p)
157 {
158         struct group *grp;
159
160         torture_comment(tctx, "Testing getgrgid: %lu\n", (unsigned long)gid);
161
162         grp = getgrgid(gid);
163         if (grp) {
164                 print_group(grp);
165         }
166
167         if (grp_p) {
168                 copy_group(tctx, grp, grp_p);
169         }
170
171         return grp ? true : false;
172 }
173
174 static bool test_nwrap_enum_passwd(struct torture_context *tctx,
175                                    struct passwd **pwd_array_p,
176                                    size_t *num_pwd_p)
177 {
178         struct passwd *pwd;
179         struct passwd *pwd_array = NULL;
180         size_t num_pwd = 0;
181
182         torture_comment(tctx, "Testing setpwent\n");
183         setpwent();
184
185         while ((pwd = getpwent()) != NULL) {
186                 torture_comment(tctx, "Testing getpwent\n");
187
188                 print_passwd(pwd);
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]);
193                         num_pwd++;
194                 }
195         }
196
197         torture_comment(tctx, "Testing endpwent\n");
198         endpwent();
199
200         if (pwd_array_p) {
201                 *pwd_array_p = pwd_array;
202         }
203         if (num_pwd_p) {
204                 *num_pwd_p = num_pwd;
205         }
206
207         return true;
208 }
209
210 static bool torture_assert_passwd_equal(struct torture_context *tctx,
211                                         const struct passwd *p1,
212                                         const struct passwd *p2,
213                                         const char *comment)
214 {
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);
222
223         return true;
224 }
225
226 static bool test_nwrap_passwd(struct torture_context *tctx)
227 {
228         int i;
229         struct passwd *pwd, pwd1, pwd2;
230         size_t num_pwd;
231
232         torture_assert(tctx, test_nwrap_enum_passwd(tctx, &pwd, &num_pwd),
233                                                     "failed to enumerate passwd");
234
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");
246         }
247
248         return true;
249 }
250
251 static bool test_nwrap_enum_group(struct torture_context *tctx,
252                                   struct group **grp_array_p,
253                                   size_t *num_grp_p)
254 {
255         struct group *grp;
256         struct group *grp_array = NULL;
257         size_t num_grp = 0;
258
259         torture_comment(tctx, "Testing setgrent\n");
260         setgrent();
261
262         while ((grp = getgrent()) != NULL) {
263                 torture_comment(tctx, "Testing getgrent\n");
264
265                 print_group(grp);
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]);
270                         num_grp++;
271                 }
272         }
273
274         torture_comment(tctx, "Testing endgrent\n");
275         endgrent();
276
277         if (grp_array_p) {
278                 *grp_array_p = grp_array;
279         }
280         if (num_grp_p) {
281                 *num_grp_p = num_grp;
282         }
283
284         return true;
285 }
286
287 static bool torture_assert_group_equal(struct torture_context *tctx,
288                                        const struct group *g1,
289                                        const struct group *g2,
290                                        const char *comment)
291 {
292         int i;
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) {
297                 return false;
298         }
299         if (!g1->gr_mem && g2->gr_mem) {
300                 return false;
301         }
302         if (!g1->gr_mem && !g2->gr_mem) {
303                 return true;
304         }
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);
307         }
308
309         return true;
310 }
311
312 static bool test_nwrap_group(struct torture_context *tctx)
313 {
314         int i;
315         struct group *grp, grp1, grp2;
316         size_t num_grp;
317
318         torture_assert(tctx, test_nwrap_enum_group(tctx, &grp, &num_grp),
319                                                    "failed to enumerate group");
320
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");
332         }
333
334         return true;
335 }
336
337 static bool test_nwrap_getgrouplist(struct torture_context *tctx,
338                                     const char *user,
339                                     gid_t gid,
340                                     gid_t **gids_p,
341                                     int *num_gids_p)
342 {
343         int ret;
344         int num_groups = 0;
345         gid_t *groups = NULL;
346
347         torture_comment(tctx, "Testing getgrouplist: %s\n", user);
348
349         ret = getgrouplist(user, gid, NULL, &num_groups);
350         if (ret == -1 || num_groups != 0) {
351
352                 groups = talloc_array(tctx, gid_t, num_groups);
353                 torture_assert(tctx, groups, "out of memory\n");
354
355                 ret = getgrouplist(user, gid, groups, &num_groups);
356         }
357
358         torture_assert(tctx, (ret != -1), "failed to call getgrouplist");
359
360         torture_comment(tctx, "%s is member in %d groups\n", user, num_groups);
361
362         if (gids_p) {
363                 *gids_p = groups;
364         }
365         if (num_gids_p) {
366                 *num_gids_p = num_groups;
367         }
368
369         return true;
370 }
371
372 static bool test_nwrap_user_in_group(struct torture_context *tctx,
373                                      const struct passwd *pwd,
374                                      const struct group *grp)
375 {
376         int i;
377
378         for (i=0; grp->gr_mem && grp->gr_mem[i] != NULL; i++) {
379                 if (strequal(grp->gr_mem[i], pwd->pw_name)) {
380                         return true;
381                 }
382         }
383
384         return false;
385 }
386
387 static bool test_nwrap_membership_user(struct torture_context *tctx,
388                                        const struct passwd *pwd,
389                                        struct group *grp_array,
390                                        size_t num_grp)
391 {
392         int num_user_groups = 0;
393         int num_user_groups_from_enum = 0;
394         gid_t *user_groups = NULL;
395         int g, i;
396         bool primary_group_had_user_member = false;
397
398         torture_assert(tctx, test_nwrap_getgrouplist(tctx,
399                                                      pwd->pw_name,
400                                                      pwd->pw_gid,
401                                                      &user_groups,
402                                                      &num_user_groups),
403                                                      "failed to test getgrouplist");
404
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");
408         }
409
410
411         for (i=0; i < num_grp; i++) {
412
413                 struct group grp = grp_array[i];
414
415                 if (test_nwrap_user_in_group(tctx, pwd, &grp)) {
416
417                         struct group current_grp;
418                         num_user_groups_from_enum++;
419
420                         torture_assert(tctx, test_nwrap_getgrnam(tctx, grp.gr_name, &current_grp),
421                                         "failed to find the group the user is a member of");
422
423                         if (current_grp.gr_gid == pwd->pw_gid) {
424                                 torture_comment(tctx, "primary group %s of user %s lists user as member\n",
425                                                 current_grp.gr_name,
426                                                 pwd->pw_name);
427                                 primary_group_had_user_member = true;
428                         }
429
430                         continue;
431                 }
432         }
433
434         if (!primary_group_had_user_member) {
435                 num_user_groups_from_enum++;
436         }
437
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");
440
441         return true;
442 }
443
444 static bool test_nwrap_membership(struct torture_context *tctx)
445 {
446         const char *old_pwd = getenv("NSS_WRAPPER_PASSWD");
447         const char *old_group = getenv("NSS_WRAPPER_GROUP");
448         struct passwd *pwd;
449         size_t num_pwd;
450         struct group *grp;
451         size_t num_grp;
452         int i;
453
454         if (!old_pwd || !old_group) {
455                 torture_skip(tctx, "nothing to test\n");
456                 return true;
457         }
458
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");
463
464         for (i=0; i < num_pwd; i++) {
465
466                 torture_assert(tctx, test_nwrap_membership_user(tctx, &pwd[i], grp, num_grp),
467                         "failed to test membership for user");
468
469         }
470
471         return true;
472 }
473
474 static bool test_nwrap_env(struct torture_context *tctx)
475 {
476         const char *old_pwd = getenv("NSS_WRAPPER_PASSWD");
477         const char *old_group = getenv("NSS_WRAPPER_GROUP");
478
479         if (!old_pwd || !old_group) {
480                 torture_skip(tctx, "nothing to test\n");
481                 return true;
482         }
483
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");
488
489         return true;
490 }
491
492 struct torture_suite *torture_local_nss_wrapper(TALLOC_CTX *mem_ctx)
493 {
494         struct torture_suite *suite = torture_suite_create(mem_ctx, "NSS-WRAPPER");
495
496         torture_suite_add_simple_test(suite, "env", test_nwrap_env);
497         torture_suite_add_simple_test(suite, "membership", test_nwrap_membership);
498
499         return suite;
500 }