]>
Commit | Line | Data |
---|---|---|
e07aabba FW |
1 | /* Copyright (C) 2015 Free Software Foundation, Inc. |
2 | This file is part of the GNU C Library. | |
3 | ||
4 | The GNU C Library is free software; you can redistribute it and/or | |
5 | modify it under the terms of the GNU Lesser General Public | |
6 | License as published by the Free Software Foundation; either | |
7 | version 2.1 of the License, or (at your option) any later version. | |
8 | ||
9 | The GNU C Library is distributed in the hope that it will be useful, | |
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
12 | Lesser General Public License for more details. | |
13 | ||
14 | You should have received a copy of the GNU Lesser General Public | |
15 | License along with the GNU C Library; if not, see | |
16 | <http://www.gnu.org/licenses/>. */ | |
17 | ||
18 | #include <errno.h> | |
19 | #include <pwd.h> | |
20 | #include <stdbool.h> | |
21 | #include <stdio.h> | |
22 | #include <stdlib.h> | |
23 | #include <string.h> | |
24 | ||
25 | /* Check if two passwd structs contain the same data. */ | |
26 | static bool | |
27 | equal (const struct passwd *a, const struct passwd *b) | |
28 | { | |
29 | return strcmp (a->pw_name, b->pw_name) == 0 | |
30 | && strcmp (a->pw_passwd, b->pw_passwd) == 0 | |
31 | && a->pw_uid == b->pw_uid | |
32 | && a->pw_gid == b->pw_gid | |
33 | && strcmp (a->pw_gecos, b->pw_gecos) == 0 | |
34 | && strcmp (a->pw_dir, b->pw_dir) == 0 | |
35 | && strcmp (a->pw_shell, b->pw_shell) == 0; | |
36 | } | |
37 | ||
38 | enum { MAX_TEST_ITEMS = 10 }; | |
39 | static struct passwd test_items[MAX_TEST_ITEMS]; | |
40 | static int test_count; | |
41 | ||
42 | /* Initialize test_items and test_count above, with data from the | |
43 | passwd database. */ | |
44 | static bool | |
45 | init_test_items (void) | |
46 | { | |
47 | setpwent (); | |
48 | do | |
49 | { | |
50 | struct passwd *pwd = getpwent (); | |
51 | if (pwd == NULL) | |
52 | break; | |
53 | struct passwd *target = test_items + test_count; | |
54 | target->pw_name = strdup (pwd->pw_name); | |
55 | target->pw_passwd = strdup (pwd->pw_passwd); | |
56 | target->pw_uid = pwd->pw_uid; | |
57 | target->pw_gid = pwd->pw_gid; | |
58 | target->pw_gecos = strdup (pwd->pw_gecos); | |
59 | target->pw_dir = strdup (pwd->pw_dir); | |
60 | target->pw_shell = strdup (pwd->pw_shell); | |
61 | } | |
62 | while (++test_count < MAX_TEST_ITEMS); | |
63 | endpwent (); | |
64 | ||
65 | /* Filter out those test items which cannot be looked up by name or | |
66 | UID. */ | |
67 | bool found = false; | |
68 | for (int i = 0; i < test_count; ++i) | |
69 | { | |
70 | struct passwd *pwd1 = getpwnam (test_items[i].pw_name); | |
71 | struct passwd *pwd2 = getpwuid (test_items[i].pw_uid); | |
72 | if (pwd1 == NULL || !equal (pwd1, test_items + i) | |
73 | || pwd2 == NULL || !equal (pwd2, test_items + i)) | |
74 | test_items[i].pw_name = NULL; | |
75 | else | |
76 | found = true; | |
77 | } | |
78 | ||
79 | if (!found) | |
80 | puts ("error: no accounts found which can be looked up by name and UID."); | |
81 | return found; | |
82 | } | |
83 | ||
84 | /* Set to true if an error is encountered. */ | |
85 | static bool errors; | |
86 | ||
87 | /* Return true if the padding has not been tampered with. */ | |
88 | static bool | |
89 | check_padding (char *buffer, size_t size, char pad) | |
90 | { | |
91 | char *end = buffer + size; | |
92 | while (buffer < end) | |
93 | { | |
94 | if (*buffer != pad) | |
95 | return false; | |
96 | ++buffer; | |
97 | } | |
98 | return true; | |
99 | } | |
100 | ||
101 | /* Test one buffer size and padding combination. */ | |
102 | static void | |
103 | test_one (const struct passwd *item, size_t buffer_size, | |
104 | char pad, size_t padding_size) | |
105 | { | |
106 | char *buffer = malloc (buffer_size + padding_size); | |
107 | if (buffer == NULL) | |
108 | { | |
109 | puts ("error: malloc failure"); | |
110 | errors = true; | |
111 | return; | |
112 | } | |
113 | ||
114 | struct passwd pwd; | |
115 | struct passwd *result; | |
116 | int ret; | |
117 | ||
118 | /* Test getpwname_r. */ | |
119 | memset (buffer, pad, buffer_size + padding_size); | |
120 | pwd = (struct passwd) {}; | |
121 | ret = getpwnam_r (item->pw_name, &pwd, buffer, buffer_size, &result); | |
122 | if (!check_padding (buffer + buffer_size, padding_size, pad)) | |
123 | { | |
124 | printf ("error: padding change: " | |
125 | "name \"%s\", buffer size %zu, padding size %zu, pad 0x%02x\n", | |
126 | item->pw_name, buffer_size, padding_size, (unsigned char) pad); | |
127 | errors = true; | |
128 | } | |
129 | if (ret == 0) | |
130 | { | |
131 | if (result == NULL) | |
132 | { | |
133 | printf ("error: no data: name \"%s\", buffer size %zu\n", | |
134 | item->pw_name, buffer_size); | |
135 | errors = true; | |
136 | } | |
137 | else if (!equal (item, result)) | |
138 | { | |
139 | printf ("error: lookup mismatch: name \"%s\", buffer size %zu\n", | |
140 | item->pw_name, buffer_size); | |
141 | errors = true; | |
142 | } | |
143 | } | |
144 | else if (ret != ERANGE) | |
145 | { | |
146 | errno = ret; | |
147 | printf ("error: lookup failure for name \"%s\": %m (%d)\n", | |
148 | item->pw_name, ret); | |
149 | errors = true; | |
150 | } | |
151 | ||
152 | /* Test getpwuid_r. */ | |
153 | memset (buffer, pad, buffer_size + padding_size); | |
154 | pwd = (struct passwd) {}; | |
155 | ret = getpwuid_r (item->pw_uid, &pwd, buffer, buffer_size, &result); | |
156 | if (!check_padding (buffer + buffer_size, padding_size, pad)) | |
157 | { | |
158 | printf ("error: padding change: " | |
159 | "UID %ld, buffer size %zu, padding size %zu, pad 0x%02x\n", | |
160 | (long) item->pw_uid, buffer_size, padding_size, | |
161 | (unsigned char) pad); | |
162 | errors = true; | |
163 | } | |
164 | if (ret == 0) | |
165 | { | |
166 | if (result == NULL) | |
167 | { | |
168 | printf ("error: no data: UID %ld, buffer size %zu\n", | |
169 | (long) item->pw_uid, buffer_size); | |
170 | errors = true; | |
171 | } | |
172 | else if (!equal (item, result)) | |
173 | { | |
174 | printf ("error: lookup mismatch: UID %ld, buffer size %zu\n", | |
175 | (long) item->pw_uid, buffer_size); | |
176 | errors = true; | |
177 | } | |
178 | } | |
179 | else if (ret != ERANGE) | |
180 | { | |
181 | errno = ret; | |
182 | printf ("error: lookup failure for UID \"%ld\": %m (%d)\n", | |
183 | (long) item->pw_uid, ret); | |
184 | errors = true; | |
185 | } | |
186 | ||
187 | free (buffer); | |
188 | } | |
189 | ||
190 | /* Test one buffer size with different paddings. */ | |
191 | static void | |
192 | test_buffer_size (size_t buffer_size) | |
193 | { | |
194 | for (int i = 0; i < test_count; ++i) | |
195 | for (size_t padding_size = 0; padding_size < 3; ++padding_size) | |
196 | { | |
197 | test_one (test_items + i, buffer_size, '\0', padding_size); | |
198 | if (padding_size > 0) | |
199 | { | |
200 | test_one (test_items + i, buffer_size, ':', padding_size); | |
201 | test_one (test_items + i, buffer_size, '\n', padding_size); | |
202 | test_one (test_items + i, buffer_size, '\xff', padding_size); | |
203 | test_one (test_items + i, buffer_size, '@', padding_size); | |
204 | } | |
205 | } | |
206 | } | |
207 | ||
208 | int | |
209 | do_test (void) | |
210 | { | |
211 | if (!init_test_items ()) | |
212 | return 1; | |
213 | printf ("info: %d test items\n", test_count); | |
214 | ||
215 | for (size_t buffer_size = 0; buffer_size <= 65; ++buffer_size) | |
216 | test_buffer_size (buffer_size); | |
217 | for (size_t buffer_size = 64 + 4; buffer_size < 256; buffer_size += 4) | |
218 | test_buffer_size (buffer_size); | |
219 | test_buffer_size (255); | |
220 | test_buffer_size (257); | |
221 | for (size_t buffer_size = 256; buffer_size < 512; buffer_size += 8) | |
222 | test_buffer_size (buffer_size); | |
223 | test_buffer_size (511); | |
224 | test_buffer_size (513); | |
225 | test_buffer_size (1024); | |
226 | test_buffer_size (2048); | |
227 | ||
228 | if (errors) | |
229 | return 1; | |
230 | else | |
231 | return 0; | |
232 | } | |
233 | ||
234 | #define TEST_FUNCTION do_test () | |
235 | #include "../test-skeleton.c" |