blob: d3d7604671fa264726cee90f1543321f80f74f52 [file] [log] [blame]
Erik Andersen13456d12000-03-16 08:09:57 +00001/* vi: set sw=4 ts=4: */
2/*
Erik Andersen5e1189e2000-04-15 16:34:54 +00003 * test implementation for busybox
Erik Andersen13456d12000-03-16 08:09:57 +00004 *
Eric Andersenc7bda1c2004-03-15 08:29:22 +00005 * Copyright (c) by a whole pile of folks:
Erik Andersen13456d12000-03-16 08:09:57 +00006 *
Paul Fox6ab03782006-06-08 21:37:26 +00007 * test(1); version 7-like -- author Erik Baalbergen
8 * modified by Eric Gisin to be used as built-in.
9 * modified by Arnold Robbins to add SVR3 compatibility
10 * (-x -c -b -p -u -g -k) plus Korn's -L -nt -ot -ef and new -S (socket).
11 * modified by J.T. Conklin for NetBSD.
12 * modified by Herbert Xu to be used as built-in in ash.
13 * modified by Erik Andersen <andersen@codepoet.org> to be used
14 * in busybox.
Erik Andersen13456d12000-03-16 08:09:57 +000015 *
Paul Fox6ab03782006-06-08 21:37:26 +000016 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
Erik Andersen13456d12000-03-16 08:09:57 +000017 *
18 * Original copyright notice states:
Paul Fox6ab03782006-06-08 21:37:26 +000019 * "This program is in the Public Domain."
Erik Andersen13456d12000-03-16 08:09:57 +000020 */
21
Rob Landleyea224be2006-06-18 20:20:07 +000022#include "busybox.h"
Erik Andersen13456d12000-03-16 08:09:57 +000023#include <unistd.h>
24#include <ctype.h>
25#include <errno.h>
Erik Andersen13456d12000-03-16 08:09:57 +000026#include <string.h>
Paul Fox6ab03782006-06-08 21:37:26 +000027#include <setjmp.h>
Erik Andersen13456d12000-03-16 08:09:57 +000028
29/* test(1) accepts the following grammar:
30 oexpr ::= aexpr | aexpr "-o" oexpr ;
31 aexpr ::= nexpr | nexpr "-a" aexpr ;
32 nexpr ::= primary | "!" primary
Paul Fox6ab03782006-06-08 21:37:26 +000033 primary ::= unary-operator operand
Erik Andersen13456d12000-03-16 08:09:57 +000034 | operand binary-operator operand
35 | operand
36 | "(" oexpr ")"
37 ;
38 unary-operator ::= "-r"|"-w"|"-x"|"-f"|"-d"|"-c"|"-b"|"-p"|
39 "-u"|"-g"|"-k"|"-s"|"-t"|"-z"|"-n"|"-o"|"-O"|"-G"|"-L"|"-S";
40
Mike Frysinger75ac42b2005-04-14 02:49:22 +000041 binary-operator ::= "="|"=="|"!="|"-eq"|"-ne"|"-ge"|"-gt"|"-le"|"-lt"|
Erik Andersen13456d12000-03-16 08:09:57 +000042 "-nt"|"-ot"|"-ef";
43 operand ::= <any legal UNIX file name>
44*/
45
46enum token {
47 EOI,
48 FILRD,
49 FILWR,
50 FILEX,
51 FILEXIST,
52 FILREG,
53 FILDIR,
54 FILCDEV,
55 FILBDEV,
56 FILFIFO,
57 FILSOCK,
58 FILSYM,
59 FILGZ,
60 FILTT,
61 FILSUID,
62 FILSGID,
63 FILSTCK,
64 FILNT,
65 FILOT,
66 FILEQ,
67 FILUID,
68 FILGID,
69 STREZ,
70 STRNZ,
71 STREQ,
72 STRNE,
73 STRLT,
74 STRGT,
75 INTEQ,
76 INTNE,
77 INTGE,
78 INTGT,
79 INTLE,
80 INTLT,
81 UNOT,
82 BAND,
83 BOR,
84 LPAREN,
85 RPAREN,
86 OPERAND
87};
88
89enum token_types {
90 UNOP,
91 BINOP,
92 BUNOP,
93 BBINOP,
94 PAREN
95};
96
Eric Andersen92d23242001-03-19 23:49:41 +000097static const struct t_op {
Erik Andersen13456d12000-03-16 08:09:57 +000098 const char *op_text;
99 short op_num, op_type;
Glenn L McGrathacfc0d82002-08-23 06:05:11 +0000100} ops[] = {
101 {
102 "-r", FILRD, UNOP}, {
103 "-w", FILWR, UNOP}, {
104 "-x", FILEX, UNOP}, {
105 "-e", FILEXIST, UNOP}, {
106 "-f", FILREG, UNOP}, {
107 "-d", FILDIR, UNOP}, {
108 "-c", FILCDEV, UNOP}, {
109 "-b", FILBDEV, UNOP}, {
110 "-p", FILFIFO, UNOP}, {
111 "-u", FILSUID, UNOP}, {
112 "-g", FILSGID, UNOP}, {
113 "-k", FILSTCK, UNOP}, {
114 "-s", FILGZ, UNOP}, {
115 "-t", FILTT, UNOP}, {
116 "-z", STREZ, UNOP}, {
117 "-n", STRNZ, UNOP}, {
Paul Fox6ab03782006-06-08 21:37:26 +0000118 "-h", FILSYM, UNOP}, /* for backwards compat */
Glenn L McGrathacfc0d82002-08-23 06:05:11 +0000119 {
120 "-O", FILUID, UNOP}, {
121 "-G", FILGID, UNOP}, {
122 "-L", FILSYM, UNOP}, {
123 "-S", FILSOCK, UNOP}, {
124 "=", STREQ, BINOP}, {
Mike Frysinger75ac42b2005-04-14 02:49:22 +0000125 "==", STREQ, BINOP}, {
Glenn L McGrathacfc0d82002-08-23 06:05:11 +0000126 "!=", STRNE, BINOP}, {
127 "<", STRLT, BINOP}, {
128 ">", STRGT, BINOP}, {
129 "-eq", INTEQ, BINOP}, {
130 "-ne", INTNE, BINOP}, {
131 "-ge", INTGE, BINOP}, {
132 "-gt", INTGT, BINOP}, {
133 "-le", INTLE, BINOP}, {
134 "-lt", INTLT, BINOP}, {
135 "-nt", FILNT, BINOP}, {
136 "-ot", FILOT, BINOP}, {
137 "-ef", FILEQ, BINOP}, {
138 "!", UNOT, BUNOP}, {
139 "-a", BAND, BBINOP}, {
140 "-o", BOR, BBINOP}, {
141 "(", LPAREN, PAREN}, {
142 ")", RPAREN, PAREN}, {
143 0, 0, 0}
Erik Andersen13456d12000-03-16 08:09:57 +0000144};
145
Glenn L McGrath73db8be2004-08-11 02:45:47 +0000146#ifdef CONFIG_FEATURE_TEST_64
147typedef int64_t arith_t;
148#else
149typedef int arith_t;
150#endif
151
Matt Kraaie6e81832000-12-30 07:46:23 +0000152static char **t_wp;
153static struct t_op const *t_wp_op;
Rob Landleyecb29572006-08-22 23:40:28 +0000154static gid_t *group_array;
Erik Andersen13456d12000-03-16 08:09:57 +0000155static int ngroups;
156
Glenn L McGrathacfc0d82002-08-23 06:05:11 +0000157static enum token t_lex(char *s);
Glenn L McGrath73db8be2004-08-11 02:45:47 +0000158static arith_t oexpr(enum token n);
159static arith_t aexpr(enum token n);
160static arith_t nexpr(enum token n);
Eric Andersen74400cc2001-10-18 04:11:39 +0000161static int binop(void);
Glenn L McGrath73db8be2004-08-11 02:45:47 +0000162static arith_t primary(enum token n);
Eric Andersen74400cc2001-10-18 04:11:39 +0000163static int filstat(char *nm, enum token mode);
Glenn L McGrath73db8be2004-08-11 02:45:47 +0000164static arith_t getn(const char *s);
Eric Andersen74400cc2001-10-18 04:11:39 +0000165static int newerf(const char *f1, const char *f2);
166static int olderf(const char *f1, const char *f2);
167static int equalf(const char *f1, const char *f2);
Eric Andersen74400cc2001-10-18 04:11:39 +0000168static int test_eaccess(char *path, int mode);
169static int is_a_group_member(gid_t gid);
170static void initialize_group_array(void);
Erik Andersen13456d12000-03-16 08:09:57 +0000171
Paul Fox6ab03782006-06-08 21:37:26 +0000172static jmp_buf leaving;
173
174int bb_test(int argc, char **argv)
Erik Andersen13456d12000-03-16 08:09:57 +0000175{
Glenn L McGrathacfc0d82002-08-23 06:05:11 +0000176 int res;
Erik Andersen13456d12000-03-16 08:09:57 +0000177
Denis Vlasenkobf66fbc2006-12-21 13:23:14 +0000178 if (LONE_CHAR(argv[0], '[')) {
179 --argc;
180 if (NOT_LONE_CHAR(argv[argc], ']')) {
Paul Fox6ab03782006-06-08 21:37:26 +0000181 bb_error_msg("missing ]");
182 return 2;
183 }
184 argv[argc] = NULL;
185 } else if (strcmp(argv[0], "[[") == 0) {
Denis Vlasenkobf66fbc2006-12-21 13:23:14 +0000186 --argc;
187 if (strcmp(argv[argc], "]]")) {
Paul Fox6ab03782006-06-08 21:37:26 +0000188 bb_error_msg("missing ]]");
189 return 2;
190 }
Erik Andersen13456d12000-03-16 08:09:57 +0000191 argv[argc] = NULL;
192 }
Paul Fox6ab03782006-06-08 21:37:26 +0000193
194 res = setjmp(leaving);
195 if (res)
196 return res;
197
198 /* resetting ngroups is probably unnecessary. it will
199 * force a new call to getgroups(), which prevents using
200 * group data fetched during a previous call. but the
201 * only way the group data could be stale is if there's
202 * been an intervening call to setgroups(), and this
203 * isn't likely in the case of a shell. paranoia
204 * prevails...
205 */
206 ngroups = 0;
207
Erik Andersen13456d12000-03-16 08:09:57 +0000208 /* Implement special cases from POSIX.2, section 4.62.4 */
209 switch (argc) {
210 case 1:
Paul Fox6ab03782006-06-08 21:37:26 +0000211 return 1;
Erik Andersen13456d12000-03-16 08:09:57 +0000212 case 2:
Paul Fox6ab03782006-06-08 21:37:26 +0000213 return *argv[1] == '\0';
Erik Andersen13456d12000-03-16 08:09:57 +0000214 case 3:
215 if (argv[1][0] == '!' && argv[1][1] == '\0') {
Paul Fox6ab03782006-06-08 21:37:26 +0000216 return *argv[2] != '\0';
Erik Andersen13456d12000-03-16 08:09:57 +0000217 }
218 break;
219 case 4:
220 if (argv[1][0] != '!' || argv[1][1] != '\0') {
Glenn L McGrathacfc0d82002-08-23 06:05:11 +0000221 if (t_lex(argv[2]), t_wp_op && t_wp_op->op_type == BINOP) {
Erik Andersen13456d12000-03-16 08:09:57 +0000222 t_wp = &argv[1];
Paul Fox6ab03782006-06-08 21:37:26 +0000223 return binop() == 0;
Erik Andersen13456d12000-03-16 08:09:57 +0000224 }
225 }
226 break;
227 case 5:
228 if (argv[1][0] == '!' && argv[1][1] == '\0') {
Glenn L McGrathacfc0d82002-08-23 06:05:11 +0000229 if (t_lex(argv[3]), t_wp_op && t_wp_op->op_type == BINOP) {
Erik Andersen13456d12000-03-16 08:09:57 +0000230 t_wp = &argv[2];
Paul Fox6ab03782006-06-08 21:37:26 +0000231 return binop() != 0;
Erik Andersen13456d12000-03-16 08:09:57 +0000232 }
233 }
234 break;
235 }
236
237 t_wp = &argv[1];
238 res = !oexpr(t_lex(*t_wp));
239
Paul Fox6ab03782006-06-08 21:37:26 +0000240 if (*t_wp != NULL && *++t_wp != NULL) {
241 bb_error_msg("%s: unknown operand", *t_wp);
242 return 2;
243 }
244 return res;
245}
Erik Andersen13456d12000-03-16 08:09:57 +0000246
Paul Fox6ab03782006-06-08 21:37:26 +0000247static void syntax(const char *op, const char *msg)
248{
249 if (op && *op) {
250 bb_error_msg("%s: %s", op, msg);
251 } else {
252 bb_error_msg("%s", msg);
253 }
254 longjmp(leaving, 2);
Erik Andersen13456d12000-03-16 08:09:57 +0000255}
256
Glenn L McGrath73db8be2004-08-11 02:45:47 +0000257static arith_t oexpr(enum token n)
Erik Andersen13456d12000-03-16 08:09:57 +0000258{
Glenn L McGrath73db8be2004-08-11 02:45:47 +0000259 arith_t res;
Erik Andersen13456d12000-03-16 08:09:57 +0000260
261 res = aexpr(n);
Glenn L McGrathacfc0d82002-08-23 06:05:11 +0000262 if (t_lex(*++t_wp) == BOR) {
Erik Andersen13456d12000-03-16 08:09:57 +0000263 return oexpr(t_lex(*++t_wp)) || res;
Glenn L McGrathacfc0d82002-08-23 06:05:11 +0000264 }
Erik Andersen13456d12000-03-16 08:09:57 +0000265 t_wp--;
266 return res;
267}
268
Glenn L McGrath73db8be2004-08-11 02:45:47 +0000269static arith_t aexpr(enum token n)
Erik Andersen13456d12000-03-16 08:09:57 +0000270{
Glenn L McGrath73db8be2004-08-11 02:45:47 +0000271 arith_t res;
Erik Andersen13456d12000-03-16 08:09:57 +0000272
273 res = nexpr(n);
274 if (t_lex(*++t_wp) == BAND)
275 return aexpr(t_lex(*++t_wp)) && res;
276 t_wp--;
277 return res;
278}
279
Glenn L McGrath73db8be2004-08-11 02:45:47 +0000280static arith_t nexpr(enum token n)
Erik Andersen13456d12000-03-16 08:09:57 +0000281{
282 if (n == UNOT)
283 return !nexpr(t_lex(*++t_wp));
284 return primary(n);
285}
286
Glenn L McGrath73db8be2004-08-11 02:45:47 +0000287static arith_t primary(enum token n)
Erik Andersen13456d12000-03-16 08:09:57 +0000288{
Glenn L McGrath73db8be2004-08-11 02:45:47 +0000289 arith_t res;
Erik Andersen13456d12000-03-16 08:09:57 +0000290
Glenn L McGrathacfc0d82002-08-23 06:05:11 +0000291 if (n == EOI) {
Paul Fox6ab03782006-06-08 21:37:26 +0000292 syntax(NULL, "argument expected");
Glenn L McGrathacfc0d82002-08-23 06:05:11 +0000293 }
Erik Andersen13456d12000-03-16 08:09:57 +0000294 if (n == LPAREN) {
295 res = oexpr(t_lex(*++t_wp));
296 if (t_lex(*++t_wp) != RPAREN)
Paul Fox6ab03782006-06-08 21:37:26 +0000297 syntax(NULL, "closing paren expected");
Erik Andersen13456d12000-03-16 08:09:57 +0000298 return res;
299 }
300 if (t_wp_op && t_wp_op->op_type == UNOP) {
301 /* unary expression */
302 if (*++t_wp == NULL)
Paul Fox6ab03782006-06-08 21:37:26 +0000303 syntax(t_wp_op->op_text, "argument expected");
Erik Andersen13456d12000-03-16 08:09:57 +0000304 switch (n) {
305 case STREZ:
306 return strlen(*t_wp) == 0;
307 case STRNZ:
308 return strlen(*t_wp) != 0;
309 case FILTT:
310 return isatty(getn(*t_wp));
311 default:
312 return filstat(*t_wp, n);
313 }
314 }
315
316 if (t_lex(t_wp[1]), t_wp_op && t_wp_op->op_type == BINOP) {
317 return binop();
Glenn L McGrathacfc0d82002-08-23 06:05:11 +0000318 }
Erik Andersen13456d12000-03-16 08:09:57 +0000319
320 return strlen(*t_wp) > 0;
321}
322
Mike Frysinger4e5936e2005-04-16 04:30:38 +0000323static int binop(void)
Erik Andersen13456d12000-03-16 08:09:57 +0000324{
325 const char *opnd1, *opnd2;
326 struct t_op const *op;
327
328 opnd1 = *t_wp;
329 (void) t_lex(*++t_wp);
330 op = t_wp_op;
331
Glenn L McGrathacfc0d82002-08-23 06:05:11 +0000332 if ((opnd2 = *++t_wp) == (char *) 0)
Paul Fox6ab03782006-06-08 21:37:26 +0000333 syntax(op->op_text, "argument expected");
Glenn L McGrathacfc0d82002-08-23 06:05:11 +0000334
Erik Andersen13456d12000-03-16 08:09:57 +0000335 switch (op->op_num) {
336 case STREQ:
337 return strcmp(opnd1, opnd2) == 0;
338 case STRNE:
339 return strcmp(opnd1, opnd2) != 0;
340 case STRLT:
341 return strcmp(opnd1, opnd2) < 0;
342 case STRGT:
343 return strcmp(opnd1, opnd2) > 0;
344 case INTEQ:
345 return getn(opnd1) == getn(opnd2);
346 case INTNE:
347 return getn(opnd1) != getn(opnd2);
348 case INTGE:
349 return getn(opnd1) >= getn(opnd2);
350 case INTGT:
351 return getn(opnd1) > getn(opnd2);
352 case INTLE:
353 return getn(opnd1) <= getn(opnd2);
354 case INTLT:
355 return getn(opnd1) < getn(opnd2);
356 case FILNT:
Glenn L McGrathacfc0d82002-08-23 06:05:11 +0000357 return newerf(opnd1, opnd2);
Erik Andersen13456d12000-03-16 08:09:57 +0000358 case FILOT:
Glenn L McGrathacfc0d82002-08-23 06:05:11 +0000359 return olderf(opnd1, opnd2);
Erik Andersen13456d12000-03-16 08:09:57 +0000360 case FILEQ:
Glenn L McGrathacfc0d82002-08-23 06:05:11 +0000361 return equalf(opnd1, opnd2);
Erik Andersen13456d12000-03-16 08:09:57 +0000362 }
363 /* NOTREACHED */
364 return 1;
365}
366
Glenn L McGrathacfc0d82002-08-23 06:05:11 +0000367static int filstat(char *nm, enum token mode)
Erik Andersen13456d12000-03-16 08:09:57 +0000368{
369 struct stat s;
Eric Andersenfad04fd2000-07-14 06:49:52 +0000370 unsigned int i;
Erik Andersen13456d12000-03-16 08:09:57 +0000371
372 if (mode == FILSYM) {
373#ifdef S_IFLNK
374 if (lstat(nm, &s) == 0) {
375 i = S_IFLNK;
376 goto filetype;
377 }
378#endif
379 return 0;
380 }
381
Glenn L McGrathacfc0d82002-08-23 06:05:11 +0000382 if (stat(nm, &s) != 0)
Erik Andersen13456d12000-03-16 08:09:57 +0000383 return 0;
384
385 switch (mode) {
386 case FILRD:
387 return test_eaccess(nm, R_OK) == 0;
388 case FILWR:
389 return test_eaccess(nm, W_OK) == 0;
390 case FILEX:
391 return test_eaccess(nm, X_OK) == 0;
392 case FILEXIST:
393 return 1;
394 case FILREG:
395 i = S_IFREG;
396 goto filetype;
397 case FILDIR:
398 i = S_IFDIR;
399 goto filetype;
400 case FILCDEV:
401 i = S_IFCHR;
402 goto filetype;
403 case FILBDEV:
404 i = S_IFBLK;
405 goto filetype;
406 case FILFIFO:
407#ifdef S_IFIFO
408 i = S_IFIFO;
409 goto filetype;
410#else
411 return 0;
412#endif
413 case FILSOCK:
414#ifdef S_IFSOCK
415 i = S_IFSOCK;
416 goto filetype;
417#else
418 return 0;
419#endif
420 case FILSUID:
421 i = S_ISUID;
422 goto filebit;
423 case FILSGID:
424 i = S_ISGID;
425 goto filebit;
426 case FILSTCK:
427 i = S_ISVTX;
428 goto filebit;
429 case FILGZ:
430 return s.st_size > 0L;
431 case FILUID:
432 return s.st_uid == geteuid();
433 case FILGID:
434 return s.st_gid == getegid();
435 default:
436 return 1;
437 }
438
Glenn L McGrathacfc0d82002-08-23 06:05:11 +0000439 filetype:
Erik Andersen13456d12000-03-16 08:09:57 +0000440 return ((s.st_mode & S_IFMT) == i);
441
Glenn L McGrathacfc0d82002-08-23 06:05:11 +0000442 filebit:
Erik Andersen13456d12000-03-16 08:09:57 +0000443 return ((s.st_mode & i) != 0);
444}
445
Glenn L McGrathacfc0d82002-08-23 06:05:11 +0000446static enum token t_lex(char *s)
Erik Andersen13456d12000-03-16 08:09:57 +0000447{
448 struct t_op const *op = ops;
449
450 if (s == 0) {
Glenn L McGrathacfc0d82002-08-23 06:05:11 +0000451 t_wp_op = (struct t_op *) 0;
Erik Andersen13456d12000-03-16 08:09:57 +0000452 return EOI;
453 }
454 while (op->op_text) {
455 if (strcmp(s, op->op_text) == 0) {
456 t_wp_op = op;
457 return op->op_num;
458 }
459 op++;
460 }
Glenn L McGrathacfc0d82002-08-23 06:05:11 +0000461 t_wp_op = (struct t_op *) 0;
Erik Andersen13456d12000-03-16 08:09:57 +0000462 return OPERAND;
463}
464
465/* atoi with error detection */
Glenn L McGrath73db8be2004-08-11 02:45:47 +0000466static arith_t getn(const char *s)
Erik Andersen13456d12000-03-16 08:09:57 +0000467{
468 char *p;
Glenn L McGrath73db8be2004-08-11 02:45:47 +0000469#ifdef CONFIG_FEATURE_TEST_64
470 long long r;
471#else
Erik Andersen13456d12000-03-16 08:09:57 +0000472 long r;
Glenn L McGrath73db8be2004-08-11 02:45:47 +0000473#endif
Erik Andersen13456d12000-03-16 08:09:57 +0000474
475 errno = 0;
Glenn L McGrath73db8be2004-08-11 02:45:47 +0000476#ifdef CONFIG_FEATURE_TEST_64
477 r = strtoll(s, &p, 10);
478#else
Erik Andersen13456d12000-03-16 08:09:57 +0000479 r = strtol(s, &p, 10);
Glenn L McGrath73db8be2004-08-11 02:45:47 +0000480#endif
Erik Andersen13456d12000-03-16 08:09:57 +0000481
482 if (errno != 0)
Paul Fox6ab03782006-06-08 21:37:26 +0000483 syntax(s, "out of range");
Erik Andersen13456d12000-03-16 08:09:57 +0000484
Rob Landleyea224be2006-06-18 20:20:07 +0000485 if (*(skip_whitespace(p)))
Paul Fox6ab03782006-06-08 21:37:26 +0000486 syntax(s, "bad number");
Erik Andersen13456d12000-03-16 08:09:57 +0000487
Glenn L McGrath73db8be2004-08-11 02:45:47 +0000488 return r;
Erik Andersen13456d12000-03-16 08:09:57 +0000489}
490
Glenn L McGrathacfc0d82002-08-23 06:05:11 +0000491static int newerf(const char *f1, const char *f2)
Erik Andersen13456d12000-03-16 08:09:57 +0000492{
493 struct stat b1, b2;
494
Glenn L McGrathacfc0d82002-08-23 06:05:11 +0000495 return (stat(f1, &b1) == 0 &&
496 stat(f2, &b2) == 0 && b1.st_mtime > b2.st_mtime);
Erik Andersen13456d12000-03-16 08:09:57 +0000497}
498
Glenn L McGrathacfc0d82002-08-23 06:05:11 +0000499static int olderf(const char *f1, const char *f2)
Erik Andersen13456d12000-03-16 08:09:57 +0000500{
501 struct stat b1, b2;
502
Glenn L McGrathacfc0d82002-08-23 06:05:11 +0000503 return (stat(f1, &b1) == 0 &&
504 stat(f2, &b2) == 0 && b1.st_mtime < b2.st_mtime);
Erik Andersen13456d12000-03-16 08:09:57 +0000505}
506
Glenn L McGrathacfc0d82002-08-23 06:05:11 +0000507static int equalf(const char *f1, const char *f2)
Erik Andersen13456d12000-03-16 08:09:57 +0000508{
509 struct stat b1, b2;
510
Glenn L McGrathacfc0d82002-08-23 06:05:11 +0000511 return (stat(f1, &b1) == 0 &&
512 stat(f2, &b2) == 0 &&
513 b1.st_dev == b2.st_dev && b1.st_ino == b2.st_ino);
Erik Andersen13456d12000-03-16 08:09:57 +0000514}
515
516/* Do the same thing access(2) does, but use the effective uid and gid,
517 and don't make the mistake of telling root that any file is
518 executable. */
Glenn L McGrathacfc0d82002-08-23 06:05:11 +0000519static int test_eaccess(char *path, int mode)
Erik Andersen13456d12000-03-16 08:09:57 +0000520{
521 struct stat st;
Eric Andersenfad04fd2000-07-14 06:49:52 +0000522 unsigned int euid = geteuid();
Erik Andersen13456d12000-03-16 08:09:57 +0000523
Glenn L McGrathacfc0d82002-08-23 06:05:11 +0000524 if (stat(path, &st) < 0)
Denis Vlasenko079f8af2006-11-27 16:49:31 +0000525 return -1;
Erik Andersen13456d12000-03-16 08:09:57 +0000526
527 if (euid == 0) {
528 /* Root can read or write any file. */
529 if (mode != X_OK)
Denis Vlasenko079f8af2006-11-27 16:49:31 +0000530 return 0;
Erik Andersen13456d12000-03-16 08:09:57 +0000531
532 /* Root can execute any file that has any one of the execute
533 bits set. */
534 if (st.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH))
Denis Vlasenko079f8af2006-11-27 16:49:31 +0000535 return 0;
Erik Andersen13456d12000-03-16 08:09:57 +0000536 }
537
Paul Fox6ab03782006-06-08 21:37:26 +0000538 if (st.st_uid == euid) /* owner */
Erik Andersen13456d12000-03-16 08:09:57 +0000539 mode <<= 6;
Glenn L McGrathacfc0d82002-08-23 06:05:11 +0000540 else if (is_a_group_member(st.st_gid))
Erik Andersen13456d12000-03-16 08:09:57 +0000541 mode <<= 3;
542
543 if (st.st_mode & mode)
Denis Vlasenko079f8af2006-11-27 16:49:31 +0000544 return 0;
Erik Andersen13456d12000-03-16 08:09:57 +0000545
Denis Vlasenko079f8af2006-11-27 16:49:31 +0000546 return -1;
Erik Andersen13456d12000-03-16 08:09:57 +0000547}
548
Mike Frysinger4e5936e2005-04-16 04:30:38 +0000549static void initialize_group_array(void)
Erik Andersen13456d12000-03-16 08:09:57 +0000550{
551 ngroups = getgroups(0, NULL);
Rob Landleyecb29572006-08-22 23:40:28 +0000552 if (ngroups > 0) {
553 group_array = xmalloc(ngroups * sizeof(gid_t));
554 getgroups(ngroups, group_array);
555 }
Erik Andersen13456d12000-03-16 08:09:57 +0000556}
557
558/* Return non-zero if GID is one that we have in our groups list. */
Glenn L McGrathacfc0d82002-08-23 06:05:11 +0000559static int is_a_group_member(gid_t gid)
Erik Andersen13456d12000-03-16 08:09:57 +0000560{
"Robert P. J. Day"68229832006-07-01 13:08:46 +0000561 int i;
Erik Andersen13456d12000-03-16 08:09:57 +0000562
563 /* Short-circuit if possible, maybe saving a call to getgroups(). */
564 if (gid == getgid() || gid == getegid())
Denis Vlasenko079f8af2006-11-27 16:49:31 +0000565 return 1;
Erik Andersen13456d12000-03-16 08:09:57 +0000566
567 if (ngroups == 0)
Glenn L McGrathacfc0d82002-08-23 06:05:11 +0000568 initialize_group_array();
Erik Andersen13456d12000-03-16 08:09:57 +0000569
570 /* Search through the list looking for GID. */
571 for (i = 0; i < ngroups; i++)
572 if (gid == group_array[i])
Denis Vlasenko079f8af2006-11-27 16:49:31 +0000573 return 1;
Erik Andersen13456d12000-03-16 08:09:57 +0000574
Denis Vlasenko079f8af2006-11-27 16:49:31 +0000575 return 0;
Erik Andersen13456d12000-03-16 08:09:57 +0000576}
Paul Fox6ab03782006-06-08 21:37:26 +0000577
578
579/* applet entry point */
580
581int test_main(int argc, char **argv)
582{
Denis Vlasenkobf66fbc2006-12-21 13:23:14 +0000583 return bb_test(argc, argv);
Paul Fox6ab03782006-06-08 21:37:26 +0000584}
585