blob: bbbfce58e15de321edbd55a0f72c887eb960eeff [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.
Bernhard Reutner-Fischercc890262007-03-30 18:23:36 +000015 * modified by Bernhard Fischer to be useable (i.e. a bit less bloaty).
Erik Andersen13456d12000-03-16 08:09:57 +000016 *
Paul Fox6ab03782006-06-08 21:37:26 +000017 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
Erik Andersen13456d12000-03-16 08:09:57 +000018 *
19 * Original copyright notice states:
Paul Fox6ab03782006-06-08 21:37:26 +000020 * "This program is in the Public Domain."
Erik Andersen13456d12000-03-16 08:09:57 +000021 */
22
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000023#include "libbb.h"
Paul Fox6ab03782006-06-08 21:37:26 +000024#include <setjmp.h>
Erik Andersen13456d12000-03-16 08:09:57 +000025
Denis Vlasenko99912ca2007-04-10 15:43:37 +000026/* This is a NOEXEC applet. Be very careful! */
27
28
Erik Andersen13456d12000-03-16 08:09:57 +000029/* 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};
Denis Vlasenkodcf4de22007-05-01 20:07:29 +000088#define is_int_op(a) (((unsigned char)((a) - INTEQ)) <= 5)
89#define is_str_op(a) (((unsigned char)((a) - STREZ)) <= 5)
90#define is_file_op(a) (((unsigned char)((a) - FILNT)) <= 2)
91#define is_file_access(a) (((unsigned char)((a) - FILRD)) <= 2)
92#define is_file_type(a) (((unsigned char)((a) - FILREG)) <= 5)
93#define is_file_bit(a) (((unsigned char)((a) - FILSUID)) <= 2)
Erik Andersen13456d12000-03-16 08:09:57 +000094enum token_types {
95 UNOP,
96 BINOP,
97 BUNOP,
98 BBINOP,
99 PAREN
100};
101
Eric Andersen92d23242001-03-19 23:49:41 +0000102static const struct t_op {
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000103 char op_text[4];
Bernhard Reutner-Fischercc890262007-03-30 18:23:36 +0000104 unsigned char op_num, op_type;
Glenn L McGrathacfc0d82002-08-23 06:05:11 +0000105} ops[] = {
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000106 { "-r", FILRD , UNOP },
107 { "-w", FILWR , UNOP },
108 { "-x", FILEX , UNOP },
109 { "-e", FILEXIST, UNOP },
110 { "-f", FILREG , UNOP },
111 { "-d", FILDIR , UNOP },
112 { "-c", FILCDEV , UNOP },
113 { "-b", FILBDEV , UNOP },
114 { "-p", FILFIFO , UNOP },
115 { "-u", FILSUID , UNOP },
116 { "-g", FILSGID , UNOP },
117 { "-k", FILSTCK , UNOP },
118 { "-s", FILGZ , UNOP },
119 { "-t", FILTT , UNOP },
120 { "-z", STREZ , UNOP },
121 { "-n", STRNZ , UNOP },
122 { "-h", FILSYM , UNOP }, /* for backwards compat */
123
124 { "-O" , FILUID , UNOP },
125 { "-G" , FILGID , UNOP },
126 { "-L" , FILSYM , UNOP },
127 { "-S" , FILSOCK, UNOP },
128 { "=" , STREQ , BINOP },
129 { "==" , STREQ , BINOP },
130 { "!=" , STRNE , BINOP },
131 { "<" , STRLT , BINOP },
132 { ">" , STRGT , BINOP },
133 { "-eq", INTEQ , BINOP },
134 { "-ne", INTNE , BINOP },
135 { "-ge", INTGE , BINOP },
136 { "-gt", INTGT , BINOP },
137 { "-le", INTLE , BINOP },
138 { "-lt", INTLT , BINOP },
139 { "-nt", FILNT , BINOP },
140 { "-ot", FILOT , BINOP },
141 { "-ef", FILEQ , BINOP },
142 { "!" , UNOT , BUNOP },
143 { "-a" , BAND , BBINOP },
144 { "-o" , BOR , BBINOP },
145 { "(" , LPAREN , PAREN },
146 { ")" , RPAREN , PAREN },
Erik Andersen13456d12000-03-16 08:09:57 +0000147};
148
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000149enum { NUM_OPS = sizeof(ops) / sizeof(ops[0]) };
150
151#if ENABLE_FEATURE_TEST_64
Glenn L McGrath73db8be2004-08-11 02:45:47 +0000152typedef int64_t arith_t;
153#else
154typedef int arith_t;
155#endif
156
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000157/* Cannot eliminate these static data (do the G trick)
158 * because of bb_test usage from other applets */
Matt Kraaie6e81832000-12-30 07:46:23 +0000159static char **t_wp;
160static struct t_op const *t_wp_op;
Rob Landleyecb29572006-08-22 23:40:28 +0000161static gid_t *group_array;
Erik Andersen13456d12000-03-16 08:09:57 +0000162static int ngroups;
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000163static jmp_buf leaving;
Erik Andersen13456d12000-03-16 08:09:57 +0000164
Glenn L McGrathacfc0d82002-08-23 06:05:11 +0000165static enum token t_lex(char *s);
Glenn L McGrath73db8be2004-08-11 02:45:47 +0000166static arith_t oexpr(enum token n);
167static arith_t aexpr(enum token n);
168static arith_t nexpr(enum token n);
Eric Andersen74400cc2001-10-18 04:11:39 +0000169static int binop(void);
Glenn L McGrath73db8be2004-08-11 02:45:47 +0000170static arith_t primary(enum token n);
Eric Andersen74400cc2001-10-18 04:11:39 +0000171static int filstat(char *nm, enum token mode);
Glenn L McGrath73db8be2004-08-11 02:45:47 +0000172static arith_t getn(const char *s);
Denis Vlasenko729bd9e2007-04-02 12:37:28 +0000173/* UNUSED
Eric Andersen74400cc2001-10-18 04:11:39 +0000174static int newerf(const char *f1, const char *f2);
175static int olderf(const char *f1, const char *f2);
176static int equalf(const char *f1, const char *f2);
Denis Vlasenko729bd9e2007-04-02 12:37:28 +0000177*/
Eric Andersen74400cc2001-10-18 04:11:39 +0000178static int test_eaccess(char *path, int mode);
179static int is_a_group_member(gid_t gid);
180static void initialize_group_array(void);
Erik Andersen13456d12000-03-16 08:09:57 +0000181
Paul Fox6ab03782006-06-08 21:37:26 +0000182int bb_test(int argc, char **argv)
Erik Andersen13456d12000-03-16 08:09:57 +0000183{
Glenn L McGrathacfc0d82002-08-23 06:05:11 +0000184 int res;
Denis Vlasenko89bca272007-06-08 07:33:45 +0000185 char *arg0;
Erik Andersen13456d12000-03-16 08:09:57 +0000186
Denis Vlasenko89bca272007-06-08 07:33:45 +0000187 arg0 = strrchr(argv[0], '/');
188 if (!arg0++) arg0 = argv[0];
189 if (arg0[0] == '[') {
190 if (!arg0[1]) { /* "[" ? */
191 --argc;
192 if (NOT_LONE_CHAR(argv[argc], ']')) {
193 bb_error_msg("missing ]");
194 return 2;
195 }
196 argv[argc] = NULL;
Denis Vlasenkodc4ab542007-06-08 07:59:54 +0000197 } else if (LONE_CHAR(arg0+1, '[') == 0) { /* "[[" ? */
Denis Vlasenko89bca272007-06-08 07:33:45 +0000198 --argc;
199 if (strcmp(argv[argc], "]]") != 0) {
200 bb_error_msg("missing ]]");
201 return 2;
202 }
203 argv[argc] = NULL;
Paul Fox6ab03782006-06-08 21:37:26 +0000204 }
Erik Andersen13456d12000-03-16 08:09:57 +0000205 }
Paul Fox6ab03782006-06-08 21:37:26 +0000206
207 res = setjmp(leaving);
208 if (res)
209 return res;
210
211 /* resetting ngroups is probably unnecessary. it will
212 * force a new call to getgroups(), which prevents using
213 * group data fetched during a previous call. but the
214 * only way the group data could be stale is if there's
215 * been an intervening call to setgroups(), and this
216 * isn't likely in the case of a shell. paranoia
217 * prevails...
218 */
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000219 ngroups = 0;
Paul Fox6ab03782006-06-08 21:37:26 +0000220
Erik Andersen13456d12000-03-16 08:09:57 +0000221 /* Implement special cases from POSIX.2, section 4.62.4 */
Bernhard Reutner-Fischercc890262007-03-30 18:23:36 +0000222 if (argc == 1)
Paul Fox6ab03782006-06-08 21:37:26 +0000223 return 1;
Bernhard Reutner-Fischercc890262007-03-30 18:23:36 +0000224 if (argc == 2)
Paul Fox6ab03782006-06-08 21:37:26 +0000225 return *argv[1] == '\0';
Bernhard Reutner-Fischercc890262007-03-30 18:23:36 +0000226//assert(argc);
Bernhard Reutner-Fischerfdcbc4e2007-06-12 13:21:08 +0000227 {
Bernhard Reutner-Fischercc890262007-03-30 18:23:36 +0000228 bool _off;
229 if (argc == 3)
Paul Fox6ab03782006-06-08 21:37:26 +0000230 return *argv[2] != '\0';
Bernhard Reutner-Fischercc890262007-03-30 18:23:36 +0000231 _off = argc - 4;
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000232 t_lex(argv[2 + _off]);
233 if (t_wp_op && t_wp_op->op_type == BINOP) {
234 t_wp = &argv[1 + _off];
Bernhard Reutner-Fischerfdcbc4e2007-06-12 13:21:08 +0000235 return binop() == (LONE_CHAR(argv[1], '!'));
Erik Andersen13456d12000-03-16 08:09:57 +0000236 }
Erik Andersen13456d12000-03-16 08:09:57 +0000237 }
Erik Andersen13456d12000-03-16 08:09:57 +0000238 t_wp = &argv[1];
239 res = !oexpr(t_lex(*t_wp));
240
Paul Fox6ab03782006-06-08 21:37:26 +0000241 if (*t_wp != NULL && *++t_wp != NULL) {
242 bb_error_msg("%s: unknown operand", *t_wp);
243 return 2;
244 }
245 return res;
246}
Erik Andersen13456d12000-03-16 08:09:57 +0000247
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000248static void syntax(const char *op, const char *msg) ATTRIBUTE_NORETURN;
Paul Fox6ab03782006-06-08 21:37:26 +0000249static void syntax(const char *op, const char *msg)
250{
251 if (op && *op) {
252 bb_error_msg("%s: %s", op, msg);
253 } else {
Denis Vlasenkofe3e1772007-05-27 03:39:50 +0000254 bb_error_msg("%s: %s"+4, msg);
Paul Fox6ab03782006-06-08 21:37:26 +0000255 }
256 longjmp(leaving, 2);
Erik Andersen13456d12000-03-16 08:09:57 +0000257}
258
Glenn L McGrath73db8be2004-08-11 02:45:47 +0000259static arith_t oexpr(enum token n)
Erik Andersen13456d12000-03-16 08:09:57 +0000260{
Glenn L McGrath73db8be2004-08-11 02:45:47 +0000261 arith_t res;
Erik Andersen13456d12000-03-16 08:09:57 +0000262
263 res = aexpr(n);
Glenn L McGrathacfc0d82002-08-23 06:05:11 +0000264 if (t_lex(*++t_wp) == BOR) {
Erik Andersen13456d12000-03-16 08:09:57 +0000265 return oexpr(t_lex(*++t_wp)) || res;
Glenn L McGrathacfc0d82002-08-23 06:05:11 +0000266 }
Erik Andersen13456d12000-03-16 08:09:57 +0000267 t_wp--;
268 return res;
269}
270
Glenn L McGrath73db8be2004-08-11 02:45:47 +0000271static arith_t aexpr(enum token n)
Erik Andersen13456d12000-03-16 08:09:57 +0000272{
Glenn L McGrath73db8be2004-08-11 02:45:47 +0000273 arith_t res;
Erik Andersen13456d12000-03-16 08:09:57 +0000274
275 res = nexpr(n);
276 if (t_lex(*++t_wp) == BAND)
277 return aexpr(t_lex(*++t_wp)) && res;
278 t_wp--;
279 return res;
280}
281
Glenn L McGrath73db8be2004-08-11 02:45:47 +0000282static arith_t nexpr(enum token n)
Erik Andersen13456d12000-03-16 08:09:57 +0000283{
284 if (n == UNOT)
285 return !nexpr(t_lex(*++t_wp));
286 return primary(n);
287}
288
Glenn L McGrath73db8be2004-08-11 02:45:47 +0000289static arith_t primary(enum token n)
Erik Andersen13456d12000-03-16 08:09:57 +0000290{
Glenn L McGrath73db8be2004-08-11 02:45:47 +0000291 arith_t res;
Erik Andersen13456d12000-03-16 08:09:57 +0000292
Glenn L McGrathacfc0d82002-08-23 06:05:11 +0000293 if (n == EOI) {
Paul Fox6ab03782006-06-08 21:37:26 +0000294 syntax(NULL, "argument expected");
Glenn L McGrathacfc0d82002-08-23 06:05:11 +0000295 }
Erik Andersen13456d12000-03-16 08:09:57 +0000296 if (n == LPAREN) {
297 res = oexpr(t_lex(*++t_wp));
298 if (t_lex(*++t_wp) != RPAREN)
Paul Fox6ab03782006-06-08 21:37:26 +0000299 syntax(NULL, "closing paren expected");
Erik Andersen13456d12000-03-16 08:09:57 +0000300 return res;
301 }
302 if (t_wp_op && t_wp_op->op_type == UNOP) {
303 /* unary expression */
304 if (*++t_wp == NULL)
Paul Fox6ab03782006-06-08 21:37:26 +0000305 syntax(t_wp_op->op_text, "argument expected");
Bernhard Reutner-Fischercc890262007-03-30 18:23:36 +0000306 if (n == STREZ)
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000307 return t_wp[0][0] == '\0';
308 if (n == STRNZ)
309 return t_wp[0][0] != '\0';
310 if (n == FILTT)
Erik Andersen13456d12000-03-16 08:09:57 +0000311 return isatty(getn(*t_wp));
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000312 return filstat(*t_wp, n);
Erik Andersen13456d12000-03-16 08:09:57 +0000313 }
314
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000315 t_lex(t_wp[1]);
316 if (t_wp_op && t_wp_op->op_type == BINOP) {
Erik Andersen13456d12000-03-16 08:09:57 +0000317 return binop();
Glenn L McGrathacfc0d82002-08-23 06:05:11 +0000318 }
Erik Andersen13456d12000-03-16 08:09:57 +0000319
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000320 return t_wp[0][0] != '\0';
Erik Andersen13456d12000-03-16 08:09:57 +0000321}
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;
Denis Vlasenkofe3e1772007-05-27 03:39:50 +0000327 arith_t val1, val2;
Erik Andersen13456d12000-03-16 08:09:57 +0000328
329 opnd1 = *t_wp;
330 (void) t_lex(*++t_wp);
331 op = t_wp_op;
332
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000333 opnd2 = *++t_wp;
334 if (opnd2 == NULL)
Paul Fox6ab03782006-06-08 21:37:26 +0000335 syntax(op->op_text, "argument expected");
Glenn L McGrathacfc0d82002-08-23 06:05:11 +0000336
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000337 if (is_int_op(op->op_num)) {
Bernhard Reutner-Fischercc890262007-03-30 18:23:36 +0000338 val1 = getn(opnd1);
339 val2 = getn(opnd2);
340 if (op->op_num == INTEQ)
341 return val1 == val2;
342 if (op->op_num == INTNE)
343 return val1 != val2;
344 if (op->op_num == INTGE)
345 return val1 >= val2;
346 if (op->op_num == INTGT)
347 return val1 > val2;
348 if (op->op_num == INTLE)
349 return val1 <= val2;
350 if (op->op_num == INTLT)
351 return val1 < val2;
Erik Andersen13456d12000-03-16 08:09:57 +0000352 }
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000353 if (is_str_op(op->op_num)) {
Bernhard Reutner-Fischercc890262007-03-30 18:23:36 +0000354 val1 = strcmp(opnd1, opnd2);
355 if (op->op_num == STREQ)
356 return val1 == 0;
357 if (op->op_num == STRNE)
358 return val1 != 0;
359 if (op->op_num == STRLT)
360 return val1 < 0;
361 if (op->op_num == STRGT)
362 return val1 > 0;
363 }
364 /* We are sure that these three are by now the only binops we didn't check
365 * yet, so we do not check if the class is correct:
366 */
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000367/* if (is_file_op(op->op_num)) */
Bernhard Reutner-Fischercc890262007-03-30 18:23:36 +0000368 {
369 struct stat b1, b2;
370
Denis Vlasenkofe3e1772007-05-27 03:39:50 +0000371 if (stat(opnd1, &b1) || stat(opnd2, &b2))
372 return 0; /* false, since at least one stat failed */
Bernhard Reutner-Fischercc890262007-03-30 18:23:36 +0000373 if (op->op_num == FILNT)
374 return b1.st_mtime > b2.st_mtime;
375 if (op->op_num == FILOT)
376 return b1.st_mtime < b2.st_mtime;
377 if (op->op_num == FILEQ)
378 return b1.st_dev == b2.st_dev && b1.st_ino == b2.st_ino;
379 }
380 return 1; /* NOTREACHED */
Erik Andersen13456d12000-03-16 08:09:57 +0000381}
382
Glenn L McGrathacfc0d82002-08-23 06:05:11 +0000383static int filstat(char *nm, enum token mode)
Erik Andersen13456d12000-03-16 08:09:57 +0000384{
385 struct stat s;
Bernhard Reutner-Fischercc890262007-03-30 18:23:36 +0000386 int i;
Erik Andersen13456d12000-03-16 08:09:57 +0000387
388 if (mode == FILSYM) {
389#ifdef S_IFLNK
390 if (lstat(nm, &s) == 0) {
391 i = S_IFLNK;
392 goto filetype;
393 }
394#endif
395 return 0;
396 }
397
Glenn L McGrathacfc0d82002-08-23 06:05:11 +0000398 if (stat(nm, &s) != 0)
Erik Andersen13456d12000-03-16 08:09:57 +0000399 return 0;
Bernhard Reutner-Fischercc890262007-03-30 18:23:36 +0000400 if (mode == FILEXIST)
Erik Andersen13456d12000-03-16 08:09:57 +0000401 return 1;
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000402 if (is_file_access(mode)) {
Bernhard Reutner-Fischercc890262007-03-30 18:23:36 +0000403 if (mode == FILRD)
404 i = R_OK;
405 if (mode == FILWR)
406 i = W_OK;
407 if (mode == FILEX)
408 i = X_OK;
409 return test_eaccess(nm, i) == 0;
Erik Andersen13456d12000-03-16 08:09:57 +0000410 }
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000411 if (is_file_type(mode)) {
Bernhard Reutner-Fischercc890262007-03-30 18:23:36 +0000412 if (mode == FILREG)
413 i = S_IFREG;
414 if (mode == FILDIR)
415 i = S_IFDIR;
416 if (mode == FILCDEV)
417 i = S_IFCHR;
418 if (mode == FILBDEV)
419 i = S_IFBLK;
420 if (mode == FILFIFO) {
421#ifdef S_IFIFO
422 i = S_IFIFO;
423#else
424 return 0;
425#endif
426 }
427 if (mode == FILSOCK) {
428#ifdef S_IFSOCK
429 i = S_IFSOCK;
430#else
431 return 0;
432#endif
433 }
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000434 filetype:
Bernhard Reutner-Fischercc890262007-03-30 18:23:36 +0000435 return ((s.st_mode & S_IFMT) == i);
436 }
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000437 if (is_file_bit(mode)) {
Bernhard Reutner-Fischercc890262007-03-30 18:23:36 +0000438 if (mode == FILSUID)
439 i = S_ISUID;
440 if (mode == FILSGID)
441 i = S_ISGID;
442 if (mode == FILSTCK)
443 i = S_ISVTX;
444 return ((s.st_mode & i) != 0);
445 }
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000446 if (mode == FILGZ)
Bernhard Reutner-Fischercc890262007-03-30 18:23:36 +0000447 return s.st_size > 0L;
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000448 if (mode == FILUID)
Bernhard Reutner-Fischercc890262007-03-30 18:23:36 +0000449 return s.st_uid == geteuid();
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000450 if (mode == FILGID)
Bernhard Reutner-Fischercc890262007-03-30 18:23:36 +0000451 return s.st_gid == getegid();
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000452 return 1; /* NOTREACHED */
Erik Andersen13456d12000-03-16 08:09:57 +0000453}
454
Glenn L McGrathacfc0d82002-08-23 06:05:11 +0000455static enum token t_lex(char *s)
Erik Andersen13456d12000-03-16 08:09:57 +0000456{
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000457 const struct t_op *op;
Erik Andersen13456d12000-03-16 08:09:57 +0000458
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000459 t_wp_op = NULL;
460 if (s == NULL) {
Erik Andersen13456d12000-03-16 08:09:57 +0000461 return EOI;
462 }
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000463
464 op = ops;
465 do {
Erik Andersen13456d12000-03-16 08:09:57 +0000466 if (strcmp(s, op->op_text) == 0) {
467 t_wp_op = op;
468 return op->op_num;
469 }
470 op++;
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000471 } while (op < ops + NUM_OPS);
472
Erik Andersen13456d12000-03-16 08:09:57 +0000473 return OPERAND;
474}
475
476/* atoi with error detection */
Bernhard Reutner-Fischercc890262007-03-30 18:23:36 +0000477//XXX: FIXME: duplicate of existing libbb function?
Glenn L McGrath73db8be2004-08-11 02:45:47 +0000478static arith_t getn(const char *s)
Erik Andersen13456d12000-03-16 08:09:57 +0000479{
480 char *p;
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000481#if ENABLE_FEATURE_TEST_64
Glenn L McGrath73db8be2004-08-11 02:45:47 +0000482 long long r;
483#else
Erik Andersen13456d12000-03-16 08:09:57 +0000484 long r;
Glenn L McGrath73db8be2004-08-11 02:45:47 +0000485#endif
Erik Andersen13456d12000-03-16 08:09:57 +0000486
487 errno = 0;
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000488#if ENABLE_FEATURE_TEST_64
Glenn L McGrath73db8be2004-08-11 02:45:47 +0000489 r = strtoll(s, &p, 10);
490#else
Erik Andersen13456d12000-03-16 08:09:57 +0000491 r = strtol(s, &p, 10);
Glenn L McGrath73db8be2004-08-11 02:45:47 +0000492#endif
Erik Andersen13456d12000-03-16 08:09:57 +0000493
494 if (errno != 0)
Paul Fox6ab03782006-06-08 21:37:26 +0000495 syntax(s, "out of range");
Erik Andersen13456d12000-03-16 08:09:57 +0000496
Rob Landleyea224be2006-06-18 20:20:07 +0000497 if (*(skip_whitespace(p)))
Paul Fox6ab03782006-06-08 21:37:26 +0000498 syntax(s, "bad number");
Erik Andersen13456d12000-03-16 08:09:57 +0000499
Glenn L McGrath73db8be2004-08-11 02:45:47 +0000500 return r;
Erik Andersen13456d12000-03-16 08:09:57 +0000501}
502
Denis Vlasenko729bd9e2007-04-02 12:37:28 +0000503/* UNUSED
Glenn L McGrathacfc0d82002-08-23 06:05:11 +0000504static int newerf(const char *f1, const char *f2)
Erik Andersen13456d12000-03-16 08:09:57 +0000505{
506 struct stat b1, b2;
507
Glenn L McGrathacfc0d82002-08-23 06:05:11 +0000508 return (stat(f1, &b1) == 0 &&
509 stat(f2, &b2) == 0 && b1.st_mtime > b2.st_mtime);
Erik Andersen13456d12000-03-16 08:09:57 +0000510}
511
Glenn L McGrathacfc0d82002-08-23 06:05:11 +0000512static int olderf(const char *f1, const char *f2)
Erik Andersen13456d12000-03-16 08:09:57 +0000513{
514 struct stat b1, b2;
515
Glenn L McGrathacfc0d82002-08-23 06:05:11 +0000516 return (stat(f1, &b1) == 0 &&
517 stat(f2, &b2) == 0 && b1.st_mtime < b2.st_mtime);
Erik Andersen13456d12000-03-16 08:09:57 +0000518}
519
Glenn L McGrathacfc0d82002-08-23 06:05:11 +0000520static int equalf(const char *f1, const char *f2)
Erik Andersen13456d12000-03-16 08:09:57 +0000521{
522 struct stat b1, b2;
523
Glenn L McGrathacfc0d82002-08-23 06:05:11 +0000524 return (stat(f1, &b1) == 0 &&
525 stat(f2, &b2) == 0 &&
526 b1.st_dev == b2.st_dev && b1.st_ino == b2.st_ino);
Erik Andersen13456d12000-03-16 08:09:57 +0000527}
Denis Vlasenko729bd9e2007-04-02 12:37:28 +0000528*/
Erik Andersen13456d12000-03-16 08:09:57 +0000529
530/* Do the same thing access(2) does, but use the effective uid and gid,
531 and don't make the mistake of telling root that any file is
532 executable. */
Glenn L McGrathacfc0d82002-08-23 06:05:11 +0000533static int test_eaccess(char *path, int mode)
Erik Andersen13456d12000-03-16 08:09:57 +0000534{
535 struct stat st;
Eric Andersenfad04fd2000-07-14 06:49:52 +0000536 unsigned int euid = geteuid();
Erik Andersen13456d12000-03-16 08:09:57 +0000537
Glenn L McGrathacfc0d82002-08-23 06:05:11 +0000538 if (stat(path, &st) < 0)
Denis Vlasenko079f8af2006-11-27 16:49:31 +0000539 return -1;
Erik Andersen13456d12000-03-16 08:09:57 +0000540
541 if (euid == 0) {
542 /* Root can read or write any file. */
543 if (mode != X_OK)
Denis Vlasenko079f8af2006-11-27 16:49:31 +0000544 return 0;
Erik Andersen13456d12000-03-16 08:09:57 +0000545
546 /* Root can execute any file that has any one of the execute
547 bits set. */
548 if (st.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH))
Denis Vlasenko079f8af2006-11-27 16:49:31 +0000549 return 0;
Erik Andersen13456d12000-03-16 08:09:57 +0000550 }
551
Paul Fox6ab03782006-06-08 21:37:26 +0000552 if (st.st_uid == euid) /* owner */
Erik Andersen13456d12000-03-16 08:09:57 +0000553 mode <<= 6;
Glenn L McGrathacfc0d82002-08-23 06:05:11 +0000554 else if (is_a_group_member(st.st_gid))
Erik Andersen13456d12000-03-16 08:09:57 +0000555 mode <<= 3;
556
557 if (st.st_mode & mode)
Denis Vlasenko079f8af2006-11-27 16:49:31 +0000558 return 0;
Erik Andersen13456d12000-03-16 08:09:57 +0000559
Denis Vlasenko079f8af2006-11-27 16:49:31 +0000560 return -1;
Erik Andersen13456d12000-03-16 08:09:57 +0000561}
562
Mike Frysinger4e5936e2005-04-16 04:30:38 +0000563static void initialize_group_array(void)
Erik Andersen13456d12000-03-16 08:09:57 +0000564{
565 ngroups = getgroups(0, NULL);
Rob Landleyecb29572006-08-22 23:40:28 +0000566 if (ngroups > 0) {
Denis Vlasenkofe3e1772007-05-27 03:39:50 +0000567 /* FIXME: ash tries so hard to not die on OOM,
568 * and we spoil it with just one xrealloc here */
569 /* We realloc, because bb_test can be entered repeatedly by shell.
570 * Testcase (ash): 'while true; do test -x some_file; done'
571 * and watch top. (some_file must have owner != you) */
572 group_array = xrealloc(group_array, ngroups * sizeof(gid_t));
Rob Landleyecb29572006-08-22 23:40:28 +0000573 getgroups(ngroups, group_array);
574 }
Erik Andersen13456d12000-03-16 08:09:57 +0000575}
576
577/* Return non-zero if GID is one that we have in our groups list. */
Bernhard Reutner-Fischercc890262007-03-30 18:23:36 +0000578//XXX: FIXME: duplicate of existing libbb function?
579// see toplevel TODO file:
580// possible code duplication ingroup() and is_a_group_member()
Glenn L McGrathacfc0d82002-08-23 06:05:11 +0000581static int is_a_group_member(gid_t gid)
Erik Andersen13456d12000-03-16 08:09:57 +0000582{
"Robert P. J. Day"68229832006-07-01 13:08:46 +0000583 int i;
Erik Andersen13456d12000-03-16 08:09:57 +0000584
585 /* Short-circuit if possible, maybe saving a call to getgroups(). */
586 if (gid == getgid() || gid == getegid())
Denis Vlasenko079f8af2006-11-27 16:49:31 +0000587 return 1;
Erik Andersen13456d12000-03-16 08:09:57 +0000588
589 if (ngroups == 0)
Glenn L McGrathacfc0d82002-08-23 06:05:11 +0000590 initialize_group_array();
Erik Andersen13456d12000-03-16 08:09:57 +0000591
592 /* Search through the list looking for GID. */
593 for (i = 0; i < ngroups; i++)
594 if (gid == group_array[i])
Denis Vlasenko079f8af2006-11-27 16:49:31 +0000595 return 1;
Erik Andersen13456d12000-03-16 08:09:57 +0000596
Denis Vlasenko079f8af2006-11-27 16:49:31 +0000597 return 0;
Erik Andersen13456d12000-03-16 08:09:57 +0000598}
Paul Fox6ab03782006-06-08 21:37:26 +0000599
600
601/* applet entry point */
602
Denis Vlasenko06af2162007-02-03 17:28:39 +0000603int test_main(int argc, char **argv);
Paul Fox6ab03782006-06-08 21:37:26 +0000604int test_main(int argc, char **argv)
605{
Denis Vlasenkobf66fbc2006-12-21 13:23:14 +0000606 return bb_test(argc, argv);
Paul Fox6ab03782006-06-08 21:37:26 +0000607}