source: trunk/minix/test/test34.c@ 9

Last change on this file since 9 was 9, checked in by Mattia Monga, 13 years ago

Minix 3.1.2a

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