nss_wrapper: call the nwrap_files_*() from nwrap_files_*_r()
[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(tctx, test_nwrap_getpwuid(tctx, pwd[i].pw_uid, &pwd2),
239                         "failed to call getpwuid for enumerated user");
240                 torture_assert_passwd_equal(tctx, &pwd1, &pwd2,
241                         "getpwnam and getpwuid gave different results");
242         }
243
244         return true;
245 }
246
247 static bool test_nwrap_enum_group(struct torture_context *tctx,
248                                   struct group **grp_array_p,
249                                   size_t *num_grp_p)
250 {
251         struct group *grp;
252         struct group *grp_array = NULL;
253         size_t num_grp = 0;
254
255         torture_comment(tctx, "Testing setgrent\n");
256         setgrent();
257
258         while ((grp = getgrent()) != NULL) {
259                 torture_comment(tctx, "Testing getgrent\n");
260
261                 print_group(grp);
262                 if (grp_array_p && num_grp_p) {
263                         grp_array = talloc_realloc(tctx, grp_array, struct group, num_grp+1);
264                         torture_assert(tctx, grp_array, "out of memory");
265                         copy_group(tctx, grp, &grp_array[num_grp]);
266                         num_grp++;
267                 }
268         }
269
270         torture_comment(tctx, "Testing endgrent\n");
271         endgrent();
272
273         if (grp_array_p) {
274                 *grp_array_p = grp_array;
275         }
276         if (num_grp_p) {
277                 *num_grp_p = num_grp;
278         }
279
280         return true;
281 }
282
283 static bool torture_assert_group_equal(struct torture_context *tctx,
284                                        const struct group *g1,
285                                        const struct group *g2,
286                                        const char *comment)
287 {
288         int i;
289         torture_assert_str_equal(tctx, g1->gr_name, g2->gr_name, comment);
290         torture_assert_str_equal(tctx, g1->gr_passwd, g2->gr_passwd, comment);
291         torture_assert_int_equal(tctx, g1->gr_gid, g2->gr_gid, comment);
292         if (g1->gr_mem && !g2->gr_mem) {
293                 return false;
294         }
295         if (!g1->gr_mem && g2->gr_mem) {
296                 return false;
297         }
298         if (!g1->gr_mem && !g2->gr_mem) {
299                 return true;
300         }
301         for (i=0; g1->gr_mem[i] && g2->gr_mem[i]; i++) {
302                 torture_assert_str_equal(tctx, g1->gr_mem[i], g2->gr_mem[i], comment);
303         }
304
305         return true;
306 }
307
308 static bool test_nwrap_group(struct torture_context *tctx)
309 {
310         int i;
311         struct group *grp, grp1, grp2;
312         size_t num_grp;
313
314         torture_assert(tctx, test_nwrap_enum_group(tctx, &grp, &num_grp),
315                                                    "failed to enumerate group");
316
317         for (i=0; i < num_grp; i++) {
318                 torture_assert(tctx, test_nwrap_getgrnam(tctx, grp[i].gr_name, &grp1),
319                         "failed to call getgrnam for enumerated user");
320                 torture_assert(tctx, test_nwrap_getgrgid(tctx, grp[i].gr_gid, &grp2),
321                         "failed to call getgrgid for enumerated user");
322                 torture_assert_group_equal(tctx, &grp1, &grp2,
323                         "getgrnam and getgrgid gave different results");
324         }
325
326         return true;
327 }
328
329 static bool test_nwrap_getgrouplist(struct torture_context *tctx,
330                                     const char *user,
331                                     gid_t gid,
332                                     gid_t **gids_p,
333                                     int *num_gids_p)
334 {
335         int ret;
336         int num_groups = 0;
337         gid_t *groups = NULL;
338
339         torture_comment(tctx, "Testing getgrouplist: %s\n", user);
340
341         ret = getgrouplist(user, gid, NULL, &num_groups);
342         if (ret == -1 || num_groups != 0) {
343
344                 groups = talloc_array(tctx, gid_t, num_groups);
345                 torture_assert(tctx, groups, "out of memory\n");
346
347                 ret = getgrouplist(user, gid, groups, &num_groups);
348         }
349
350         torture_assert(tctx, (ret != -1), "failed to call getgrouplist");
351
352         torture_comment(tctx, "%s is member in %d groups\n", user, num_groups);
353
354         if (gids_p) {
355                 *gids_p = groups;
356         }
357         if (num_gids_p) {
358                 *num_gids_p = num_groups;
359         }
360
361         return true;
362 }
363
364 static bool test_nwrap_user_in_group(struct torture_context *tctx,
365                                      const struct passwd *pwd,
366                                      const struct group *grp)
367 {
368         int i;
369
370         for (i=0; grp->gr_mem && grp->gr_mem[i] != NULL; i++) {
371                 if (strequal(grp->gr_mem[i], pwd->pw_name)) {
372                         return true;
373                 }
374         }
375
376         return false;
377 }
378
379 static bool test_nwrap_membership_user(struct torture_context *tctx,
380                                        const struct passwd *pwd,
381                                        struct group *grp_array,
382                                        size_t num_grp)
383 {
384         int num_user_groups = 0;
385         int num_user_groups_from_enum = 0;
386         gid_t *user_groups = NULL;
387         int g, i;
388         bool primary_group_had_user_member = false;
389
390         torture_assert(tctx, test_nwrap_getgrouplist(tctx,
391                                                      pwd->pw_name,
392                                                      pwd->pw_gid,
393                                                      &user_groups,
394                                                      &num_user_groups),
395                                                      "failed to test getgrouplist");
396
397         for (g=0; g < num_user_groups; g++) {
398                 torture_assert(tctx, test_nwrap_getgrgid(tctx, user_groups[g], NULL),
399                         "failed to find the group the user is a member of");
400         }
401
402
403         for (i=0; i < num_grp; i++) {
404
405                 struct group grp = grp_array[i];
406
407                 if (test_nwrap_user_in_group(tctx, pwd, &grp)) {
408
409                         struct group current_grp;
410                         num_user_groups_from_enum++;
411
412                         torture_assert(tctx, test_nwrap_getgrnam(tctx, grp.gr_name, &current_grp),
413                                         "failed to find the group the user is a member of");
414
415                         if (current_grp.gr_gid == pwd->pw_gid) {
416                                 torture_comment(tctx, "primary group %s of user %s lists user as member\n",
417                                                 current_grp.gr_name,
418                                                 pwd->pw_name);
419                                 primary_group_had_user_member = true;
420                         }
421
422                         continue;
423                 }
424         }
425
426         if (!primary_group_had_user_member) {
427                 num_user_groups_from_enum++;
428         }
429
430         torture_assert_int_equal(tctx, num_user_groups, num_user_groups_from_enum,
431                 "getgrouplist and real inspection of grouplist gave different results\n");
432
433         return true;
434 }
435
436 static bool test_nwrap_membership(struct torture_context *tctx)
437 {
438         const char *old_pwd = getenv("NSS_WRAPPER_PASSWD");
439         const char *old_group = getenv("NSS_WRAPPER_GROUP");
440         struct passwd *pwd;
441         size_t num_pwd;
442         struct group *grp;
443         size_t num_grp;
444         int i;
445
446         if (!old_pwd || !old_group) {
447                 torture_skip(tctx, "nothing to test\n");
448                 return true;
449         }
450
451         torture_assert(tctx, test_nwrap_enum_passwd(tctx, &pwd, &num_pwd),
452                                                     "failed to enumerate passwd");
453         torture_assert(tctx, test_nwrap_enum_group(tctx, &grp, &num_grp),
454                                                     "failed to enumerate group");
455
456         for (i=0; i < num_pwd; i++) {
457
458                 torture_assert(tctx, test_nwrap_membership_user(tctx, &pwd[i], grp, num_grp),
459                         "failed to test membership for user");
460
461         }
462
463         return true;
464 }
465
466 static bool test_nwrap_env(struct torture_context *tctx)
467 {
468         const char *old_pwd = getenv("NSS_WRAPPER_PASSWD");
469         const char *old_group = getenv("NSS_WRAPPER_GROUP");
470
471         if (!old_pwd || !old_group) {
472                 torture_skip(tctx, "nothing to test\n");
473                 return true;
474         }
475
476         torture_assert(tctx, test_nwrap_passwd(tctx),
477                         "failed to test users");
478         torture_assert(tctx, test_nwrap_group(tctx),
479                         "failed to test groups");
480
481         return true;
482 }
483
484 struct torture_suite *torture_local_nss_wrapper(TALLOC_CTX *mem_ctx)
485 {
486         struct torture_suite *suite = torture_suite_create(mem_ctx, "NSS-WRAPPER");
487
488         torture_suite_add_simple_test(suite, "env", test_nwrap_env);
489         torture_suite_add_simple_test(suite, "membership", test_nwrap_membership);
490
491         return suite;
492 }