xref: /minix/minix/tests/test34.c (revision ebfedea0)
1 /* test34: chmod() chown() 	Author: Jan-Mark Wams (jms@cs.vu.nl) */
2 
3 /* There is a problem getting valid uids and gids, so we use the passwd
4 ** file (ie. /etc/passwd). I don't like this, but I see no other way.
5 ** The read-only-device-error (EROFS) is not checked!
6 ** Supplementary group IDs are ignored.
7 */
8 
9 #include <sys/types.h>
10 #include <sys/stat.h>
11 #include <sys/wait.h>
12 #include <stdlib.h>
13 #include <unistd.h>
14 #include <string.h>
15 #include <fcntl.h>
16 #include <limits.h>
17 #include <errno.h>
18 #include <ctype.h>
19 #include <time.h>
20 #include <stdio.h>
21 
22 int max_error = 	4;
23 #include "common.h"
24 
25 #define ITERATIONS      4
26 #define N 100
27 
28 
29 #define ALL_RWXB	(S_IRWXU | S_IRWXG | S_IRWXO)
30 #define ALL_SETB	(S_ISUID | S_ISGID)
31 #define ALL_BITS	(ALL_RWXB | ALL_SETB)
32 
33 #define System(cmd)   if (system(cmd) != 0) printf("``%s'' failed\n", cmd)
34 #define Chdir(dir)    if (chdir(dir) != 0) printf("Can't goto %s\n", dir)
35 #define Stat(a,b)     if (stat(a,b) != 0) printf("Can't stat %s\n", a)
36 #define Mkfifo(f)     if (mkfifo(f,0777)!=0) printf("Can't make fifo %s\n", f)
37 #define Mkdir(f)      if (mkdir(f,0777)!=0) printf("Can't make dir %s\n", f)
38 #define Creat(f)      if (close(creat(f,0777))!=0) printf("Can't creat %s\n",f)
39 
40 /* This program uses /etc/passwd and assumes things about it's contents. */
41 #define PASSWD_FILE	"/etc/passwd"
42 
43 int superuser;
44 int I_can_chown;
45 char *MaxName;			/* Name of maximum length */
46 char MaxPath[PATH_MAX];		/* Same for path */
47 char *NameTooLong;		/* Name of maximum +1 length */
48 char PathTooLong[PATH_MAX + 1];	/* Same for path, both too long */
49 
50 void test34a(void);
51 void test34b(void);
52 void test34c(void);
53 mode_t mode(char *file_name);
54 void makelongnames(void);
55 void getids(uid_t * uid, gid_t * gid);
56 
57 int main(int argc, char *argv[])
58 {
59   int i, m = 0xFFFF;
60 
61   sync();
62   if (argc == 2) m = atoi(argv[1]);
63   umask(0000);
64   start(34);
65   makelongnames();
66   superuser = (geteuid() == (uid_t) 0);
67 
68 #ifdef _POSIX_CHOWN_RESTRICTED
69   I_can_chown = superuser;
70 #else
71   I_can_chown = 1;
72 #endif
73 
74 
75   for (i = 1; i < ITERATIONS; i++) {
76 	if (m & 0001) test34a();
77 	if (m & 0002) test34b();
78 	if (m & 0004) test34c();
79   }
80   quit();
81 
82   return(-1);	/* Unreachable */
83 }
84 
85 void test34a()
86 {				/* Test normal operation. */
87   time_t time1, time2;
88   mode_t mod;
89   struct stat st1, st2;
90   int cnt;
91   uid_t uid, uid2;
92   gid_t gid, gid2;
93   int stat_loc;
94 
95   subtest = 1;
96 
97   /* Make scratch file. */
98   Creat("foo");
99 
100   for (mod = 0; mod <= ALL_BITS; mod++) {
101 	if ((mod & ALL_BITS) != mod)	/* If not a valid mod next. */
102 		continue;
103 	Stat("foo", &st1);
104 	if (time(&time1) == (time_t) - 1) e(1);
105 	if (chmod("foo", mod) != 0) e(2);
106 	Stat("foo", &st2);
107 	if (time(&time2) == (time_t) - 1) e(3);
108 	if (superuser)
109 		if ((st2.st_mode & ALL_BITS) != mod) e(4);
110 	if (!superuser)
111 		if ((st2.st_mode & ALL_RWXB) != (mod & ALL_RWXB)) e(5);
112 
113 	/* Test the C time feald. */
114 	if (st1.st_ctime > st2.st_ctime) e(6);
115 	if (st1.st_ctime > time1) e(7);
116 	if (st1.st_ctime > time2) e(8);
117 #ifndef V1_FILESYSTEM
118 	if (st2.st_ctime < time1) e(9);
119 #endif
120 	if (st2.st_ctime > time2) e(10);
121 	if (st1.st_atime != st2.st_atime) e(11);
122 	if (st1.st_mtime != st2.st_mtime) e(12);
123   }				/* End for loop. */
124 
125   /* Check if chown(file, geteuid(), getegid()) works. */
126   for (cnt = 0; cnt < 20; cnt++) {
127 	/* Set all rights on foo, including the set .id bits. */
128 	if (chmod("foo", ALL_BITS) != 0) e(13);
129 	Stat("foo", &st1);
130 	if (time(&time1) == (time_t) -1) e(14);
131 
132 	if (chown("foo", geteuid(), getegid()) != 0) e(15);
133 	Stat("foo", &st2);
134 	if (time(&time2) == (time_t) -1) e(16);
135 
136 	/* Check ``chown()'' killed the set .id bits. */
137 	if (!superuser) {
138 		if ((st1.st_mode & ALL_RWXB) != ALL_RWXB) e(17);
139 		if ((st2.st_mode & ALL_BITS) != ALL_RWXB) e(18);
140 	}
141 	if (superuser) {
142 		if ((st1.st_mode & ALL_BITS) != ALL_BITS) e(19);
143 		if ((st1.st_mode & ALL_RWXB) != ALL_RWXB) e(20);
144 	}
145 
146 	/* Check the timing. */
147 	if (st1.st_ctime > st2.st_ctime) e(21);
148 	if (st1.st_ctime > time1) e(22);
149 	if (st1.st_ctime > time2) e(23);
150 #ifndef V1_FILESYSTEM
151 	if (st2.st_ctime < time1) e(24);
152 #endif
153 	if (st2.st_ctime > time2) e(25);
154 	if (st1.st_atime != st2.st_atime) e(26);
155 	if (st1.st_mtime != st2.st_mtime) e(27);
156   }				/* End for loop. */
157 
158   /* Make scratch file. */
159   if (chmod("foo", ALL_RWXB) != 0) e(28);
160 
161   if (I_can_chown) {
162 	/* Do a 20 tests on a gid and uid. */
163 	for (cnt = 0; cnt < 20; cnt++) {
164 		/* Get a uid and a gid, test chown. */
165 		getids(&uid, &gid);
166 		Stat("foo", &st1);
167 		if (time(&time1) == (time_t) -1) e(29);
168 		if (chown("foo", (uid_t) 0, (gid_t) 0) != 0) e(30);
169 		Stat("foo", &st2);
170 		if (time(&time2) == (time_t) -1) e(31);
171 
172 		/* Test the C time field. */
173 		if (st1.st_ctime > st2.st_ctime) e(32);
174 		if (st1.st_ctime > time1) e(33);
175 		if (st1.st_ctime > time2) e(34);
176 		if (st2.st_ctime < time1) e(35);
177 		if (st2.st_ctime > time2) e(36);
178 		if (st1.st_atime != st2.st_atime) e(37);
179 		if (st1.st_mtime != st2.st_mtime) e(38);
180 
181 		/* Do aditional tests. */
182 		if (chown("foo", (uid_t) 0, gid) != 0) e(39);
183 		if (chown("foo", uid, (gid_t) 0) != 0) e(40);
184 		if (chown("foo", uid, gid) != 0) e(41);
185 	}
186   }
187   if (superuser) {
188 	/* Check if a non-superuser can change a files gid to gid2 *
189 	 * if gid2 is the current process gid. */
190 	for (cnt = 0; cnt < 5; cnt++) {
191 		switch (fork()) {
192 		    case -1:
193 			printf("Can't fork\n");
194 			break;
195 		    case 0:
196 			alarm(20);
197 
198 			getids(&uid, &gid);
199 			if (uid == 0) {
200 				getids(&uid, &gid);
201 				if (uid == 0) e(42);
202 			}
203 			getids(&uid2, &gid2);
204 			if (gid == gid2) e(43);
205 
206 			/* Creat boo and bar for user uid of group gid. */
207 			Creat("boo");
208 			if (chown("boo", uid, gid) != 0) e(44);
209 			if (chmod("boo", ALL_BITS) != 0) e(45);
210 			Creat("bar");
211 			if (chown("bar", uid, gid) != 0) e(46);
212 			if (chmod("bar", ALL_BITS) != 0) e(47);
213 
214 			/* We now become user uid of group gid2. */
215 			setgid(gid2);
216 			setuid(uid);
217 
218 			Stat("bar", &st1);
219 			if (time(&time1) == (time_t) -1) e(48);
220 			if (chown("bar", uid, gid2) != 0) e(49);
221 			Stat("bar", &st2);
222 			if (time(&time2) == (time_t) -1) e(50);
223 
224 			/* Check if the SET_BITS are cleared. */
225 			if ((st1.st_mode & ALL_BITS) != ALL_BITS) e(51);
226 			if ((st2.st_mode & ALL_BITS) != ALL_RWXB) e(52);
227 
228 			/* Check the st_times. */
229 			if (st1.st_ctime > st2.st_ctime) e(53);
230 			if (st1.st_ctime > time1) e(54);
231 			if (st1.st_ctime > time2) e(55);
232 			if (st2.st_ctime < time1) e(56);
233 			if (st2.st_ctime > time2) e(57);
234 			if (st1.st_atime != st2.st_atime) e(58);
235 			if (st1.st_mtime != st2.st_mtime) e(59);
236 
237 			Stat("boo", &st1);
238 			if (chmod("boo", ALL_BITS) != 0) e(60);
239 			Stat("boo", &st2);
240 
241 			/* Check if the set gid bit is cleared. */
242 			if ((st1.st_mode & ALL_RWXB) != ALL_RWXB) e(61);
243 			if ((st2.st_mode & S_ISGID) != 0) e(62);
244 
245 			if (chown("boo", uid, gid2) != 0) e(63);
246 			Stat("boo", &st1);
247 
248 			/* Check if the set uid bit is cleared. */
249 			if ((st1.st_mode & S_ISUID) != 0) e(64);
250 
251 			exit(0);
252 		    default:
253 			wait(&stat_loc);
254 			if (stat_loc != 0) e(65);	/* Alarm? */
255 		}
256 	}			/* end for loop. */
257   }				/* end if (superuser). */
258   if (chmod("foo", ALL_BITS) != 0) e(66);
259   Stat("foo", &st1);
260   if (chown("foo", geteuid(), getegid()) != 0) e(67);
261   Stat("foo", &st2);
262   if ((st1.st_mode & ALL_BITS) != ALL_BITS) e(68);	/* See intro! */
263   if (superuser)
264 	if ((st2.st_mode & ALL_RWXB) != ALL_RWXB) e(69);
265   if (!superuser)
266 	if ((st2.st_mode & ALL_BITS) != ALL_RWXB) e(70);
267 
268   (void) system("chmod 777 ../DIR_34/* > /dev/null 2> /dev/null");
269   System("rm -rf ../DIR_34/*");
270 }
271 
272 void test34b()
273 {
274   time_t time1, time2;
275   mode_t mod;
276   struct stat st1, st2;
277 
278   subtest = 2;
279 
280   /* Test chmod() and chown() on non regular files and on MaxName and
281    * MaxPath. * Funny, but dirs should also have S_IS.ID bits.
282    */
283   Mkfifo("fifo");
284   Mkdir("dir");
285   Creat(MaxName);
286   MaxPath[strlen(MaxPath) - 2] = '/';
287   MaxPath[strlen(MaxPath) - 1] = 'a';	/* make ././.../a */
288   Creat(MaxPath);
289 
290   for (mod = 1; mod <= ALL_BITS; mod <<= 1) {
291 	if ((mod & ALL_BITS) != mod) continue;	/* bad mod */
292 	Stat("dir", &st1);
293 	if (time(&time1) == (time_t) -1) e(1);
294 	if (chmod("dir", mod) != 0) e(2);
295 	Stat("dir", &st2);
296 	if (time(&time2) == (time_t) -1) e(3);
297 	if (superuser)
298 		if ((st2.st_mode & ALL_BITS) != mod) e(4);
299 	if (!superuser)
300 		if ((st2.st_mode & ALL_RWXB) != (mod & ALL_RWXB)) e(5);
301 
302 	/* Test the C time field. */
303 	if (st1.st_ctime > st2.st_ctime) e(6);
304 	if (st1.st_ctime > time1) e(7);
305 	if (st1.st_ctime > time2) e(8);
306 #ifndef V1_FILESYSTEM
307 	if (st2.st_ctime < time1) e(9);
308 #endif
309 	if (st2.st_ctime > time2) e(10);
310 	if (st1.st_atime != st2.st_atime) e(11);
311 	if (st1.st_mtime != st2.st_mtime) e(12);
312 
313 	Stat("fifo", &st1);
314 	if (time(&time1) == (time_t) -1) e(13);
315 	if (chmod("fifo", mod) != 0) e(14);
316 	Stat("fifo", &st2);
317 	if (time(&time2) == (time_t) -1) e(15);
318 	if (superuser)
319 		if ((st2.st_mode & ALL_BITS) != mod) e(16);
320 	if (!superuser)
321 		if ((st2.st_mode & ALL_RWXB) != (mod & ALL_RWXB)) e(17);
322 
323 	/* Test the C time field. */
324 	if (st1.st_ctime > st2.st_ctime) e(18);
325 	if (st1.st_ctime > time1) e(19);
326 	if (st1.st_ctime > time2) e(20);
327 #ifndef V1_FILESYSTEM
328 	if (st2.st_ctime < time1) e(21);
329 #endif
330 	if (st2.st_ctime > time2) e(22);
331 	if (st1.st_atime != st2.st_atime) e(23);
332 	if (st1.st_mtime != st2.st_mtime) e(24);
333 
334 	Stat(MaxName, &st1);
335 	if (time(&time1) == (time_t) -1) e(25);
336 	if (chmod(MaxName, mod) != 0) e(26);
337 	Stat(MaxName, &st2);
338 	if (time(&time2) == (time_t) -1) e(27);
339 	if (superuser)
340 		if ((st2.st_mode & ALL_BITS) != mod) e(28);
341 	if (!superuser)
342 		if ((st2.st_mode & ALL_RWXB) != (mod & ALL_RWXB)) e(29);
343 
344 	/* Test the C time field. */
345 	if (st1.st_ctime > st2.st_ctime) e(30);
346 	if (st1.st_ctime > time1) e(31);
347 	if (st1.st_ctime > time2) e(32);
348 #ifndef V1_FILESYSTEM
349 	if (st2.st_ctime < time1) e(33);
350 #endif
351 	if (st2.st_ctime > time2) e(34);
352 	if (st1.st_atime != st2.st_atime) e(35);
353 	if (st1.st_mtime != st2.st_mtime) e(36);
354 
355 	Stat(MaxPath, &st1);
356 	if (time(&time1) == (time_t) -1) e(37);
357 	if (chmod(MaxPath, mod) != 0) e(38);
358 	Stat(MaxPath, &st2);
359 	if (time(&time2) == (time_t) -1) e(39);
360 	if (superuser)
361 		if ((st2.st_mode & ALL_BITS) != mod) e(40);
362 	if (!superuser)
363 		if ((st2.st_mode & ALL_RWXB) != (mod & ALL_RWXB)) e(41);
364 
365 	/* Test the C time field. */
366 	if (st1.st_ctime > st2.st_ctime) e(42);
367 	if (st1.st_ctime > time1) e(43);
368 	if (st1.st_ctime > time2) e(44);
369 #ifndef V1_FILESYSTEM
370 	if (st2.st_ctime < time1) e(45);
371 #endif
372 	if (st2.st_ctime > time2) e(46);
373 	if (st1.st_atime != st2.st_atime) e(47);
374 	if (st1.st_mtime != st2.st_mtime) e(48);
375   }
376 
377   if (chmod("dir", 0777) != 0) e(49);
378   if (chmod("fifo", 0777) != 0) e(50);
379   if (chmod(MaxName, 0777) != 0) e(51);
380   if (chmod(MaxPath, 0777) != 0) e(52);
381 
382   (void) system("chmod 777 ../DIR_34/* > /dev/null 2> /dev/null");
383   System("rm -rf ../DIR_34/*");
384 }
385 
386 void test34c()
387 {
388   struct stat st;
389   uid_t uid, uid2;
390   gid_t gid, gid2;
391   int fd, does_truncate, stat_loc;
392 
393   subtest = 3;
394 
395   Mkdir("dir");
396   Creat("dir/try_me");
397 
398   /* Disalow search permission and see if chmod() and chown() return
399    * EACCES.
400    */
401   if (chmod("dir", ALL_BITS & ~S_IXUSR) != 0) e(1);
402   if (!superuser) {
403 	if (chmod("dir/try_me", 0) != -1) e(2);
404 	if (errno != EACCES) e(3);
405 	if (I_can_chown) {
406 		if (chown("dir/try_me", geteuid(), getegid()) != -1) e(4);
407 		if (errno != EACCES) e(5);
408 	}
409   }
410 
411   /* Check ENOTDIR. */
412   Mkfifo("fifo");
413   if (chmod("fifo/try_me", 0) != -1) e(6);
414   if (errno != ENOTDIR) e(7);
415   if (chown("fifo/try_me", geteuid(), getegid()) != -1) e(8);
416   if (errno != ENOTDIR) e(9);
417 
418   Creat("file");
419   if (chmod("file/try_me", 0) != -1) e(10);
420   if (errno != ENOTDIR) e(11);
421   if (chown("file/try_me", geteuid(), getegid()) != -1) e(12);
422   if (errno != ENOTDIR) e(13);
423 
424   /* Check empty path. */
425   if (chmod("", 0) != -1) e(14);
426   if (errno != ENOENT) e(15);
427   if (chown("", geteuid(), getegid()) != -1) e(16);
428   if (errno != ENOENT) e(17);
429 
430   /* Check non existing file name. */
431   if (chmod("non_exist", 0) != -1) e(18);
432   if (errno != ENOENT) e(19);
433   if (chown("non_exist", geteuid(), getegid()) != -1) e(20);
434   if (errno != ENOENT) e(21);
435 
436   /* Check what we get if we do not have permisson. */
437   if (!superuser) {
438 	Stat("/", &st);
439 	if (st.st_uid == geteuid()) e(22);
440 
441 	/* First I had 0, I changed it to st.st_mode 8-). */
442 	if (chmod("/", st.st_mode) != -1) e(23);
443 	if (errno != EPERM) e(24);
444   }
445   if (!I_can_chown) {
446 	Stat("/", &st);
447 	if (st.st_uid == geteuid()) e(25);
448 	if (chown("/", geteuid(), getegid()) != -1) e(26);
449 	if (errno != EPERM) e(27);
450   }
451 
452   /* If we are superuser, we can test all id combinations. */
453   if (superuser) {
454 	switch (fork()) {
455 	    case -1:	printf("Can't fork\n");	break;
456 	    case 0:
457 		alarm(20);
458 
459 		getids(&uid, &gid);
460 		if (uid == 0) {
461 			getids(&uid, &gid);
462 			if (uid == 0) e(28);
463 		}
464 		getids(&uid2, &gid2);
465 		if (gid == gid2) e(29);
466 		if (uid == uid2) e(30);
467 
468 		/* Creat boo, owned by root. */
469 		Creat("boo");
470 		if (chmod("boo", ALL_BITS) != 0) e(31);
471 
472 		/* Creat boo for user uid2 of group gid2. */
473 		Creat("bar");
474 		if (chown("bar", uid2, gid2) != 0) e(32);
475 		if (chmod("bar", ALL_BITS) != 0) e(33);
476 
477 		/* Creat my_gid for user uid2 of group gid. */
478 		Creat("my_gid");
479 		if (chown("my_gid", uid2, gid) != 0) e(34);
480 		if (chmod("my_gid", ALL_BITS) != 0) e(35);
481 
482 		/* Creat my_uid for user uid of uid gid. */
483 		Creat("my_uid");
484 		if (chown("my_uid", uid, gid) != 0) e(36);
485 		if (chmod("my_uid", ALL_BITS) != 0) e(37);
486 
487 		/* We now become user uid of uid gid. */
488 		setgid(gid);
489 		setuid(uid);
490 
491 		if (chown("boo", uid, gid) != -1) e(38);
492 		if (errno != EPERM) e(39);
493 		if (chown("bar", uid, gid) != -1) e(40);
494 		if (errno != EPERM) e(41);
495 		if (chown("my_gid", uid, gid) != -1) e(42);
496 		if (errno != EPERM) e(43);
497 		if (chown("my_uid", uid, gid2) != -1) e(44);
498 
499 		/* The EPERM is not strict POSIX. */
500 		if (errno != EPERM) e(45);
501 
502 		if (chmod("boo", 0) != -1) e(46);
503 		if (errno != EPERM) e(47);
504 		if (chmod("bar", 0) != -1) e(48);
505 		if (errno != EPERM) e(49);
506 		if (chmod("my_gid", 0) != -1) e(50);
507 		if (errno != EPERM) e(51);
508 
509 		exit(0);
510 	    default:
511 		wait(&stat_loc);
512 		if (stat_loc != 0) e(52);	/* Alarm? */
513 	}
514   }
515 
516   /* Check too long path ed. */
517   does_truncate = does_fs_truncate();
518   fd = creat(NameTooLong, 0777);
519   if (does_truncate) {
520   	if (fd == -1) e(53);
521 	if (close(fd) != 0) e(54);
522 	if (chmod(NameTooLong, 0777) != 0) e(55);
523 	if (chown(NameTooLong, geteuid(), getegid()) != 0) e(56);
524   } else {
525   	if (fd != -1) e(57);
526   	if (errno != ENAMETOOLONG) e(58);
527   	(void) close(fd);		/* Just in case */
528   }
529 
530   /* Make PathTooLong contain ././.../a */
531   PathTooLong[strlen(PathTooLong) - 2] = '/';
532   PathTooLong[strlen(PathTooLong) - 1] = 'a';
533   Creat("a");
534   if (chmod(PathTooLong, 0777) != -1) e(59);
535   if (errno != ENAMETOOLONG) e(60);
536   if (chown(PathTooLong, geteuid(), getegid()) != -1) e(61);
537   if (errno != ENAMETOOLONG) e(62);
538 
539   (void) system("chmod 777 ../DIR_34/* > /dev/null 2> /dev/null");
540   System("rm -rf ../DIR_34/*");
541 }
542 
543 void makelongnames()
544 {
545   register int i;
546   int max_name_length;
547 
548   max_name_length = name_max("."); /* Aka NAME_MAX, but not every FS supports
549 				    * the same length, hence runtime check */
550   MaxName = malloc(max_name_length + 1);
551   NameTooLong = malloc(max_name_length + 1 + 1); /* Name of maximum +1 length */
552   memset(MaxName, 'a', max_name_length);
553   MaxName[max_name_length] = '\0';
554 
555   for (i = 0; i < PATH_MAX - 1; i++) {	/* idem path */
556 	MaxPath[i++] = '.';
557 	MaxPath[i] = '/';
558   }
559   MaxPath[PATH_MAX - 1] = '\0';
560 
561   strcpy(NameTooLong, MaxName);	/* copy them Max to ToLong */
562   strcpy(PathTooLong, MaxPath);
563 
564   NameTooLong[max_name_length] = 'a';
565   NameTooLong[max_name_length+1] = '\0';/* extend ToLongName by one too many */
566   PathTooLong[PATH_MAX - 1] = '/';
567   PathTooLong[PATH_MAX] = '\0';	/* inc ToLongPath by one */
568 }
569 
570 /* Getids returns a valid uid and gid. Is used PASSWD FILE.
571  * It assumes the following format for a passwd file line:
572  * <user_name>:<passwd>:<uid>:<gid>:<other_stuff>
573  * If no uids and gids can be found, it will only return 0 ids.
574  */
575 void getids(r_uid, r_gid)
576 uid_t * r_uid;
577 gid_t * r_gid;
578 {
579   char line[N];
580   unsigned char *p;
581   uid_t uid;
582   gid_t gid;
583   FILE *fp;
584   int i;
585 
586   static uid_t a_uid[N];	/* Array for uids. */
587   static gid_t a_gid[N];	/* Array for gids. */
588   static int nuid = 0, ngid = 0;/* The number of user & group ids. */
589   static int cuid = 0, cgid = 0;/* The current id index. */
590 
591   /* If we don't have any uids go read some from the passwd file. */
592   if (nuid == 0) {
593 	a_uid[nuid++] = 0;	/* Root uid and gid. */
594 	a_gid[ngid++] = 0;
595 	if ((fp = fopen(PASSWD_FILE, "r")) == NULL) {
596 		printf("Can't open ");
597 		perror(PASSWD_FILE);
598 	}
599 	while (fp != NULL && fgets(line, sizeof(line), fp) != NULL) {
600 		p = (unsigned char *)strchr(line, ':');
601 		if (p != NULL) p = (unsigned char *)strchr((char *)p + 1, ':');
602 		if (p != NULL) {
603 			p++;
604 			uid = 0;
605 			while (isdigit(*p)) {
606 				uid *= 10;
607 				uid += (uid_t) (*p - '0');
608 				p++;
609 			}
610 			if (*p != ':') continue;
611 			p++;
612 			gid = 0;
613 			while (isdigit(*p)) {
614 				gid *= 10;
615 				gid += (gid_t) (*p - '0');
616 				p++;
617 			}
618 			if (*p != ':') continue;
619 			if (nuid < N) {
620 				for (i = 0; i < nuid; i++)
621 					if (a_uid[i] == uid) break;
622 				if (i == nuid) a_uid[nuid++] = uid;
623 			}
624 			if (ngid < N) {
625 				for (i = 0; i < ngid; i++)
626 					if (a_gid[i] == gid) break;
627 				if (i == ngid) a_gid[ngid++] = gid;
628 			}
629 			if (nuid >= N && ngid >= N) break;
630 		}
631 	}
632 	if (fp != NULL) fclose(fp);
633   }
634 
635   /* We now have uids and gids in a_uid and a_gid. */
636   if (cuid >= nuid) cuid = 0;
637   if (cgid >= ngid) cgid = 0;
638   *r_uid = a_uid[cuid++];
639   *r_gid = a_gid[cgid++];
640 }
641