blob: c1097c28c5e8e7c873cd26846e4550b2feecbc74 [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
Paul Fox6ab03782006-06-08 21:37:26 +0000178 if (strcmp(argv[0], "[") == 0) {
179 if (strcmp(argv[--argc], "]")) {
180 bb_error_msg("missing ]");
181 return 2;
182 }
183 argv[argc] = NULL;
184 } else if (strcmp(argv[0], "[[") == 0) {
185 if (strcmp(argv[--argc], "]]")) {
186 bb_error_msg("missing ]]");
187 return 2;
188 }
Erik Andersen13456d12000-03-16 08:09:57 +0000189 argv[argc] = NULL;
190 }
Paul Fox6ab03782006-06-08 21:37:26 +0000191
192 res = setjmp(leaving);
193 if (res)
194 return res;
195
196 /* resetting ngroups is probably unnecessary. it will
197 * force a new call to getgroups(), which prevents using
198 * group data fetched during a previous call. but the
199 * only way the group data could be stale is if there's
200 * been an intervening call to setgroups(), and this
201 * isn't likely in the case of a shell. paranoia
202 * prevails...
203 */
204 ngroups = 0;
205
Erik Andersen13456d12000-03-16 08:09:57 +0000206 /* Implement special cases from POSIX.2, section 4.62.4 */
207 switch (argc) {
208 case 1:
Paul Fox6ab03782006-06-08 21:37:26 +0000209 return 1;
Erik Andersen13456d12000-03-16 08:09:57 +0000210 case 2:
Paul Fox6ab03782006-06-08 21:37:26 +0000211 return *argv[1] == '\0';
Erik Andersen13456d12000-03-16 08:09:57 +0000212 case 3:
213 if (argv[1][0] == '!' && argv[1][1] == '\0') {
Paul Fox6ab03782006-06-08 21:37:26 +0000214 return *argv[2] != '\0';
Erik Andersen13456d12000-03-16 08:09:57 +0000215 }
216 break;
217 case 4:
218 if (argv[1][0] != '!' || argv[1][1] != '\0') {
Glenn L McGrathacfc0d82002-08-23 06:05:11 +0000219 if (t_lex(argv[2]), t_wp_op && t_wp_op->op_type == BINOP) {
Erik Andersen13456d12000-03-16 08:09:57 +0000220 t_wp = &argv[1];
Paul Fox6ab03782006-06-08 21:37:26 +0000221 return binop() == 0;
Erik Andersen13456d12000-03-16 08:09:57 +0000222 }
223 }
224 break;
225 case 5:
226 if (argv[1][0] == '!' && argv[1][1] == '\0') {
Glenn L McGrathacfc0d82002-08-23 06:05:11 +0000227 if (t_lex(argv[3]), t_wp_op && t_wp_op->op_type == BINOP) {
Erik Andersen13456d12000-03-16 08:09:57 +0000228 t_wp = &argv[2];
Paul Fox6ab03782006-06-08 21:37:26 +0000229 return binop() != 0;
Erik Andersen13456d12000-03-16 08:09:57 +0000230 }
231 }
232 break;
233 }
234
235 t_wp = &argv[1];
236 res = !oexpr(t_lex(*t_wp));
237
Paul Fox6ab03782006-06-08 21:37:26 +0000238 if (*t_wp != NULL && *++t_wp != NULL) {
239 bb_error_msg("%s: unknown operand", *t_wp);
240 return 2;
241 }
242 return res;
243}
Erik Andersen13456d12000-03-16 08:09:57 +0000244
Paul Fox6ab03782006-06-08 21:37:26 +0000245static void syntax(const char *op, const char *msg)
246{
247 if (op && *op) {
248 bb_error_msg("%s: %s", op, msg);
249 } else {
250 bb_error_msg("%s", msg);
251 }
252 longjmp(leaving, 2);
Erik Andersen13456d12000-03-16 08:09:57 +0000253}
254
Glenn L McGrath73db8be2004-08-11 02:45:47 +0000255static arith_t oexpr(enum token n)
Erik Andersen13456d12000-03-16 08:09:57 +0000256{
Glenn L McGrath73db8be2004-08-11 02:45:47 +0000257 arith_t res;
Erik Andersen13456d12000-03-16 08:09:57 +0000258
259 res = aexpr(n);
Glenn L McGrathacfc0d82002-08-23 06:05:11 +0000260 if (t_lex(*++t_wp) == BOR) {
Erik Andersen13456d12000-03-16 08:09:57 +0000261 return oexpr(t_lex(*++t_wp)) || res;
Glenn L McGrathacfc0d82002-08-23 06:05:11 +0000262 }
Erik Andersen13456d12000-03-16 08:09:57 +0000263 t_wp--;
264 return res;
265}
266
Glenn L McGrath73db8be2004-08-11 02:45:47 +0000267static arith_t aexpr(enum token n)
Erik Andersen13456d12000-03-16 08:09:57 +0000268{
Glenn L McGrath73db8be2004-08-11 02:45:47 +0000269 arith_t res;
Erik Andersen13456d12000-03-16 08:09:57 +0000270
271 res = nexpr(n);
272 if (t_lex(*++t_wp) == BAND)
273 return aexpr(t_lex(*++t_wp)) && res;
274 t_wp--;
275 return res;
276}
277
Glenn L McGrath73db8be2004-08-11 02:45:47 +0000278static arith_t nexpr(enum token n)
Erik Andersen13456d12000-03-16 08:09:57 +0000279{
280 if (n == UNOT)
281 return !nexpr(t_lex(*++t_wp));
282 return primary(n);
283}
284
Glenn L McGrath73db8be2004-08-11 02:45:47 +0000285static arith_t primary(enum token n)
Erik Andersen13456d12000-03-16 08:09:57 +0000286{
Glenn L McGrath73db8be2004-08-11 02:45:47 +0000287 arith_t res;
Erik Andersen13456d12000-03-16 08:09:57 +0000288
Glenn L McGrathacfc0d82002-08-23 06:05:11 +0000289 if (n == EOI) {
Paul Fox6ab03782006-06-08 21:37:26 +0000290 syntax(NULL, "argument expected");
Glenn L McGrathacfc0d82002-08-23 06:05:11 +0000291 }
Erik Andersen13456d12000-03-16 08:09:57 +0000292 if (n == LPAREN) {
293 res = oexpr(t_lex(*++t_wp));
294 if (t_lex(*++t_wp) != RPAREN)
Paul Fox6ab03782006-06-08 21:37:26 +0000295 syntax(NULL, "closing paren expected");
Erik Andersen13456d12000-03-16 08:09:57 +0000296 return res;
297 }
298 if (t_wp_op && t_wp_op->op_type == UNOP) {
299 /* unary expression */
300 if (*++t_wp == NULL)
Paul Fox6ab03782006-06-08 21:37:26 +0000301 syntax(t_wp_op->op_text, "argument expected");
Erik Andersen13456d12000-03-16 08:09:57 +0000302 switch (n) {
303 case STREZ:
304 return strlen(*t_wp) == 0;
305 case STRNZ:
306 return strlen(*t_wp) != 0;
307 case FILTT:
308 return isatty(getn(*t_wp));
309 default:
310 return filstat(*t_wp, n);
311 }
312 }
313
314 if (t_lex(t_wp[1]), t_wp_op && t_wp_op->op_type == BINOP) {
315 return binop();
Glenn L McGrathacfc0d82002-08-23 06:05:11 +0000316 }
Erik Andersen13456d12000-03-16 08:09:57 +0000317
318 return strlen(*t_wp) > 0;
319}
320
Mike Frysinger4e5936e2005-04-16 04:30:38 +0000321static int binop(void)
Erik Andersen13456d12000-03-16 08:09:57 +0000322{
323 const char *opnd1, *opnd2;
324 struct t_op const *op;
325
326 opnd1 = *t_wp;
327 (void) t_lex(*++t_wp);
328 op = t_wp_op;
329
Glenn L McGrathacfc0d82002-08-23 06:05:11 +0000330 if ((opnd2 = *++t_wp) == (char *) 0)
Paul Fox6ab03782006-06-08 21:37:26 +0000331 syntax(op->op_text, "argument expected");
Glenn L McGrathacfc0d82002-08-23 06:05:11 +0000332
Erik Andersen13456d12000-03-16 08:09:57 +0000333 switch (op->op_num) {
334 case STREQ:
335 return strcmp(opnd1, opnd2) == 0;
336 case STRNE:
337 return strcmp(opnd1, opnd2) != 0;
338 case STRLT:
339 return strcmp(opnd1, opnd2) < 0;
340 case STRGT:
341 return strcmp(opnd1, opnd2) > 0;
342 case INTEQ:
343 return getn(opnd1) == getn(opnd2);
344 case INTNE:
345 return getn(opnd1) != getn(opnd2);
346 case INTGE:
347 return getn(opnd1) >= getn(opnd2);
348 case INTGT:
349 return getn(opnd1) > getn(opnd2);
350 case INTLE:
351 return getn(opnd1) <= getn(opnd2);
352 case INTLT:
353 return getn(opnd1) < getn(opnd2);
354 case FILNT:
Glenn L McGrathacfc0d82002-08-23 06:05:11 +0000355 return newerf(opnd1, opnd2);
Erik Andersen13456d12000-03-16 08:09:57 +0000356 case FILOT:
Glenn L McGrathacfc0d82002-08-23 06:05:11 +0000357 return olderf(opnd1, opnd2);
Erik Andersen13456d12000-03-16 08:09:57 +0000358 case FILEQ:
Glenn L McGrathacfc0d82002-08-23 06:05:11 +0000359 return equalf(opnd1, opnd2);
Erik Andersen13456d12000-03-16 08:09:57 +0000360 }
361 /* NOTREACHED */
362 return 1;
363}
364
Glenn L McGrathacfc0d82002-08-23 06:05:11 +0000365static int filstat(char *nm, enum token mode)
Erik Andersen13456d12000-03-16 08:09:57 +0000366{
367 struct stat s;
Eric Andersenfad04fd2000-07-14 06:49:52 +0000368 unsigned int i;
Erik Andersen13456d12000-03-16 08:09:57 +0000369
370 if (mode == FILSYM) {
371#ifdef S_IFLNK
372 if (lstat(nm, &s) == 0) {
373 i = S_IFLNK;
374 goto filetype;
375 }
376#endif
377 return 0;
378 }
379
Glenn L McGrathacfc0d82002-08-23 06:05:11 +0000380 if (stat(nm, &s) != 0)
Erik Andersen13456d12000-03-16 08:09:57 +0000381 return 0;
382
383 switch (mode) {
384 case FILRD:
385 return test_eaccess(nm, R_OK) == 0;
386 case FILWR:
387 return test_eaccess(nm, W_OK) == 0;
388 case FILEX:
389 return test_eaccess(nm, X_OK) == 0;
390 case FILEXIST:
391 return 1;
392 case FILREG:
393 i = S_IFREG;
394 goto filetype;
395 case FILDIR:
396 i = S_IFDIR;
397 goto filetype;
398 case FILCDEV:
399 i = S_IFCHR;
400 goto filetype;
401 case FILBDEV:
402 i = S_IFBLK;
403 goto filetype;
404 case FILFIFO:
405#ifdef S_IFIFO
406 i = S_IFIFO;
407 goto filetype;
408#else
409 return 0;
410#endif
411 case FILSOCK:
412#ifdef S_IFSOCK
413 i = S_IFSOCK;
414 goto filetype;
415#else
416 return 0;
417#endif
418 case FILSUID:
419 i = S_ISUID;
420 goto filebit;
421 case FILSGID:
422 i = S_ISGID;
423 goto filebit;
424 case FILSTCK:
425 i = S_ISVTX;
426 goto filebit;
427 case FILGZ:
428 return s.st_size > 0L;
429 case FILUID:
430 return s.st_uid == geteuid();
431 case FILGID:
432 return s.st_gid == getegid();
433 default:
434 return 1;
435 }
436
Glenn L McGrathacfc0d82002-08-23 06:05:11 +0000437 filetype:
Erik Andersen13456d12000-03-16 08:09:57 +0000438 return ((s.st_mode & S_IFMT) == i);
439
Glenn L McGrathacfc0d82002-08-23 06:05:11 +0000440 filebit:
Erik Andersen13456d12000-03-16 08:09:57 +0000441 return ((s.st_mode & i) != 0);
442}
443
Glenn L McGrathacfc0d82002-08-23 06:05:11 +0000444static enum token t_lex(char *s)
Erik Andersen13456d12000-03-16 08:09:57 +0000445{
446 struct t_op const *op = ops;
447
448 if (s == 0) {
Glenn L McGrathacfc0d82002-08-23 06:05:11 +0000449 t_wp_op = (struct t_op *) 0;
Erik Andersen13456d12000-03-16 08:09:57 +0000450 return EOI;
451 }
452 while (op->op_text) {
453 if (strcmp(s, op->op_text) == 0) {
454 t_wp_op = op;
455 return op->op_num;
456 }
457 op++;
458 }
Glenn L McGrathacfc0d82002-08-23 06:05:11 +0000459 t_wp_op = (struct t_op *) 0;
Erik Andersen13456d12000-03-16 08:09:57 +0000460 return OPERAND;
461}
462
463/* atoi with error detection */
Glenn L McGrath73db8be2004-08-11 02:45:47 +0000464static arith_t getn(const char *s)
Erik Andersen13456d12000-03-16 08:09:57 +0000465{
466 char *p;
Glenn L McGrath73db8be2004-08-11 02:45:47 +0000467#ifdef CONFIG_FEATURE_TEST_64
468 long long r;
469#else
Erik Andersen13456d12000-03-16 08:09:57 +0000470 long r;
Glenn L McGrath73db8be2004-08-11 02:45:47 +0000471#endif
Erik Andersen13456d12000-03-16 08:09:57 +0000472
473 errno = 0;
Glenn L McGrath73db8be2004-08-11 02:45:47 +0000474#ifdef CONFIG_FEATURE_TEST_64
475 r = strtoll(s, &p, 10);
476#else
Erik Andersen13456d12000-03-16 08:09:57 +0000477 r = strtol(s, &p, 10);
Glenn L McGrath73db8be2004-08-11 02:45:47 +0000478#endif
Erik Andersen13456d12000-03-16 08:09:57 +0000479
480 if (errno != 0)
Paul Fox6ab03782006-06-08 21:37:26 +0000481 syntax(s, "out of range");
Erik Andersen13456d12000-03-16 08:09:57 +0000482
Rob Landleyea224be2006-06-18 20:20:07 +0000483 if (*(skip_whitespace(p)))
Paul Fox6ab03782006-06-08 21:37:26 +0000484 syntax(s, "bad number");
Erik Andersen13456d12000-03-16 08:09:57 +0000485
Glenn L McGrath73db8be2004-08-11 02:45:47 +0000486 return r;
Erik Andersen13456d12000-03-16 08:09:57 +0000487}
488
Glenn L McGrathacfc0d82002-08-23 06:05:11 +0000489static int newerf(const char *f1, const char *f2)
Erik Andersen13456d12000-03-16 08:09:57 +0000490{
491 struct stat b1, b2;
492
Glenn L McGrathacfc0d82002-08-23 06:05:11 +0000493 return (stat(f1, &b1) == 0 &&
494 stat(f2, &b2) == 0 && b1.st_mtime > b2.st_mtime);
Erik Andersen13456d12000-03-16 08:09:57 +0000495}
496
Glenn L McGrathacfc0d82002-08-23 06:05:11 +0000497static int olderf(const char *f1, const char *f2)
Erik Andersen13456d12000-03-16 08:09:57 +0000498{
499 struct stat b1, b2;
500
Glenn L McGrathacfc0d82002-08-23 06:05:11 +0000501 return (stat(f1, &b1) == 0 &&
502 stat(f2, &b2) == 0 && b1.st_mtime < b2.st_mtime);
Erik Andersen13456d12000-03-16 08:09:57 +0000503}
504
Glenn L McGrathacfc0d82002-08-23 06:05:11 +0000505static int equalf(const char *f1, const char *f2)
Erik Andersen13456d12000-03-16 08:09:57 +0000506{
507 struct stat b1, b2;
508
Glenn L McGrathacfc0d82002-08-23 06:05:11 +0000509 return (stat(f1, &b1) == 0 &&
510 stat(f2, &b2) == 0 &&
511 b1.st_dev == b2.st_dev && b1.st_ino == b2.st_ino);
Erik Andersen13456d12000-03-16 08:09:57 +0000512}
513
514/* Do the same thing access(2) does, but use the effective uid and gid,
515 and don't make the mistake of telling root that any file is
516 executable. */
Glenn L McGrathacfc0d82002-08-23 06:05:11 +0000517static int test_eaccess(char *path, int mode)
Erik Andersen13456d12000-03-16 08:09:57 +0000518{
519 struct stat st;
Eric Andersenfad04fd2000-07-14 06:49:52 +0000520 unsigned int euid = geteuid();
Erik Andersen13456d12000-03-16 08:09:57 +0000521
Glenn L McGrathacfc0d82002-08-23 06:05:11 +0000522 if (stat(path, &st) < 0)
Erik Andersen13456d12000-03-16 08:09:57 +0000523 return (-1);
524
525 if (euid == 0) {
526 /* Root can read or write any file. */
527 if (mode != X_OK)
Glenn L McGrathacfc0d82002-08-23 06:05:11 +0000528 return (0);
Erik Andersen13456d12000-03-16 08:09:57 +0000529
530 /* Root can execute any file that has any one of the execute
531 bits set. */
532 if (st.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH))
533 return (0);
534 }
535
Paul Fox6ab03782006-06-08 21:37:26 +0000536 if (st.st_uid == euid) /* owner */
Erik Andersen13456d12000-03-16 08:09:57 +0000537 mode <<= 6;
Glenn L McGrathacfc0d82002-08-23 06:05:11 +0000538 else if (is_a_group_member(st.st_gid))
Erik Andersen13456d12000-03-16 08:09:57 +0000539 mode <<= 3;
540
541 if (st.st_mode & mode)
542 return (0);
543
544 return (-1);
545}
546
Mike Frysinger4e5936e2005-04-16 04:30:38 +0000547static void initialize_group_array(void)
Erik Andersen13456d12000-03-16 08:09:57 +0000548{
549 ngroups = getgroups(0, NULL);
Rob Landleyecb29572006-08-22 23:40:28 +0000550 if (ngroups > 0) {
551 group_array = xmalloc(ngroups * sizeof(gid_t));
552 getgroups(ngroups, group_array);
553 }
Erik Andersen13456d12000-03-16 08:09:57 +0000554}
555
556/* Return non-zero if GID is one that we have in our groups list. */
Glenn L McGrathacfc0d82002-08-23 06:05:11 +0000557static int is_a_group_member(gid_t gid)
Erik Andersen13456d12000-03-16 08:09:57 +0000558{
"Robert P. J. Day"68229832006-07-01 13:08:46 +0000559 int i;
Erik Andersen13456d12000-03-16 08:09:57 +0000560
561 /* Short-circuit if possible, maybe saving a call to getgroups(). */
562 if (gid == getgid() || gid == getegid())
563 return (1);
564
565 if (ngroups == 0)
Glenn L McGrathacfc0d82002-08-23 06:05:11 +0000566 initialize_group_array();
Erik Andersen13456d12000-03-16 08:09:57 +0000567
568 /* Search through the list looking for GID. */
569 for (i = 0; i < ngroups; i++)
570 if (gid == group_array[i])
571 return (1);
572
573 return (0);
574}
Paul Fox6ab03782006-06-08 21:37:26 +0000575
576
577/* applet entry point */
578
579int test_main(int argc, char **argv)
580{
581 exit(bb_test(argc, argv));
582}
583