]> granicus.if.org Git - sudo/blob - lib/util/regress/getgrouplist/getgrouplist_test.c
4d44cf2f6db55290bb1b5bbe3606f81f207cb1a2
[sudo] / lib / util / regress / getgrouplist / getgrouplist_test.c
1 /*
2  * Copyright (c) 2018 Todd C. Miller <Todd.Miller@sudo.ws>
3  *
4  * Permission to use, copy, modify, and distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15  */
16
17 #include <config.h>
18
19 #include <sys/types.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #ifdef HAVE_STRING_H
23 # include <string.h>
24 #endif /* HAVE_STRING_H */
25 #ifdef HAVE_STRINGS_H
26 # include <strings.h>
27 #endif /* HAVE_STRINGS_H */
28 #ifdef HAVE_STDBOOL_H
29 # include <stdbool.h>
30 #else
31 # include "compat/stdbool.h"
32 #endif
33 #include <pwd.h>
34 #include <grp.h>
35
36 #include "sudo_compat.h"
37 #include "sudo_fatal.h"
38 #include "sudo_util.h"
39
40 __dso_public int main(int argc, char *argv[]);
41
42 /*
43  * Test that sudo_getgrouplist2() works as expected.
44  */
45
46 int
47 main(int argc, char *argv[])
48 {
49     int errors = 0;
50 #ifndef HAVE_GETGROUPLIST_2
51     GETGROUPS_T *groups = NULL;
52     struct passwd *pw;
53     struct group *grp;
54     char *username;
55     int i, j, ntests = 0;
56     int ngroups;
57     gid_t basegid;
58     initprogname(argc > 0 ? argv[0] : "getgrouplist_test");
59
60     if ((pw = getpwuid(0)) == NULL)
61         sudo_fatal_nodebug("getpwuid(0)");
62     basegid = pw->pw_gid;
63     if ((username = strdup(pw->pw_name)) == NULL)
64         sudo_fatal_nodebug(NULL);
65
66     if (sudo_getgrouplist2(username, basegid, &groups, &ngroups) == -1)
67         sudo_fatal_nodebug("sudo_getgroulist2");
68
69     for (i = 0; i < ngroups; i++) {
70         ntests++;
71
72         /* Verify group ID exists. */
73         if ((grp = getgrgid(groups[i])) == NULL) {
74             sudo_warnx_nodebug("unable to look up group ID %u",
75                 (unsigned int)groups[i]);
76             errors++;
77             continue;
78         }
79
80         /* Check user's primary gid from the passwd file. */
81         if (grp->gr_gid == basegid)
82             continue;
83
84         /* Verify group membership. */
85         for (j = 0; grp->gr_mem[j] != NULL; j++) {
86             if (strcmp(username, grp->gr_mem[j]) == 0) {
87                 /* match */
88                 break;
89             }
90         }
91         if (grp->gr_mem[j] == NULL) {
92             sudo_warnx_nodebug("unable to find %s in group %s",
93                 username, grp->gr_name);
94             errors++;
95             continue;
96         }
97     }
98     if (errors != 0) {
99         printf("%s: %d tests run, %d errors, %d%% success rate\n",
100             getprogname(), ntests, errors, (ntests - errors) * 100 / ntests);
101     }
102 #endif /* HAVE_GETGROUPLIST_2 */
103     exit(errors);
104 }