blob: 36fb38a64606e7b4874a3a803321d118ef5bd517 [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)
Denis Vlasenkob304ead2007-06-21 13:35:52 +0000158 * because of test_main 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
Denis Vlasenkob304ead2007-06-21 13:35:52 +0000182int test_main(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;
Bernhard Reutner-Fischerbe992432007-06-15 10:36:49 +0000186 bool _off;
Erik Andersen13456d12000-03-16 08:09:57 +0000187
Denis Vlasenko89bca272007-06-08 07:33:45 +0000188 arg0 = strrchr(argv[0], '/');
189 if (!arg0++) arg0 = argv[0];
190 if (arg0[0] == '[') {
Denis Vlasenkob304ead2007-06-21 13:35:52 +0000191 --argc;
Denis Vlasenko89bca272007-06-08 07:33:45 +0000192 if (!arg0[1]) { /* "[" ? */
Denis Vlasenko89bca272007-06-08 07:33:45 +0000193 if (NOT_LONE_CHAR(argv[argc], ']')) {
194 bb_error_msg("missing ]");
195 return 2;
196 }
Denis Vlasenkob304ead2007-06-21 13:35:52 +0000197 } else { /* assuming "[[" */
Denis Vlasenko89bca272007-06-08 07:33:45 +0000198 if (strcmp(argv[argc], "]]") != 0) {
199 bb_error_msg("missing ]]");
200 return 2;
201 }
Paul Fox6ab03782006-06-08 21:37:26 +0000202 }
Denis Vlasenkob304ead2007-06-21 13:35:52 +0000203 argv[argc] = NULL;
Erik Andersen13456d12000-03-16 08:09:57 +0000204 }
Paul Fox6ab03782006-06-08 21:37:26 +0000205
206 res = setjmp(leaving);
207 if (res)
208 return res;
209
210 /* resetting ngroups is probably unnecessary. it will
211 * force a new call to getgroups(), which prevents using
212 * group data fetched during a previous call. but the
213 * only way the group data could be stale is if there's
214 * been an intervening call to setgroups(), and this
215 * isn't likely in the case of a shell. paranoia
216 * prevails...
217 */
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000218 ngroups = 0;
Paul Fox6ab03782006-06-08 21:37:26 +0000219
Erik Andersen13456d12000-03-16 08:09:57 +0000220 /* Implement special cases from POSIX.2, section 4.62.4 */
Bernhard Reutner-Fischercc890262007-03-30 18:23:36 +0000221 if (argc == 1)
Paul Fox6ab03782006-06-08 21:37:26 +0000222 return 1;
Bernhard Reutner-Fischercc890262007-03-30 18:23:36 +0000223 if (argc == 2)
Paul Fox6ab03782006-06-08 21:37:26 +0000224 return *argv[1] == '\0';
Bernhard Reutner-Fischercc890262007-03-30 18:23:36 +0000225//assert(argc);
Bernhard Reutner-Fischerbe992432007-06-15 10:36:49 +0000226 /* remember if we saw argc==4 which wants *no* '!' test */
227 _off = argc - 4;
228 if (_off ?
229 (LONE_CHAR(argv[1], '!'))
230 : (argv[1][0] != '!' || argv[1][1] != '\0'))
Bernhard Reutner-Fischerfdcbc4e2007-06-12 13:21:08 +0000231 {
Bernhard Reutner-Fischercc890262007-03-30 18:23:36 +0000232 if (argc == 3)
Paul Fox6ab03782006-06-08 21:37:26 +0000233 return *argv[2] != '\0';
Bernhard Reutner-Fischerbe992432007-06-15 10:36:49 +0000234
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000235 t_lex(argv[2 + _off]);
236 if (t_wp_op && t_wp_op->op_type == BINOP) {
237 t_wp = &argv[1 + _off];
Bernhard Reutner-Fischerbe992432007-06-15 10:36:49 +0000238 return binop() == _off;
Erik Andersen13456d12000-03-16 08:09:57 +0000239 }
Erik Andersen13456d12000-03-16 08:09:57 +0000240 }
Erik Andersen13456d12000-03-16 08:09:57 +0000241 t_wp = &argv[1];
242 res = !oexpr(t_lex(*t_wp));
243
Paul Fox6ab03782006-06-08 21:37:26 +0000244 if (*t_wp != NULL && *++t_wp != NULL) {
245 bb_error_msg("%s: unknown operand", *t_wp);
246 return 2;
247 }
248 return res;
249}
Erik Andersen13456d12000-03-16 08:09:57 +0000250
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000251static void syntax(const char *op, const char *msg) ATTRIBUTE_NORETURN;
Paul Fox6ab03782006-06-08 21:37:26 +0000252static void syntax(const char *op, const char *msg)
253{
254 if (op && *op) {
255 bb_error_msg("%s: %s", op, msg);
256 } else {
Denis Vlasenkofe3e1772007-05-27 03:39:50 +0000257 bb_error_msg("%s: %s"+4, msg);
Paul Fox6ab03782006-06-08 21:37:26 +0000258 }
259 longjmp(leaving, 2);
Erik Andersen13456d12000-03-16 08:09:57 +0000260}
261
Glenn L McGrath73db8be2004-08-11 02:45:47 +0000262static arith_t oexpr(enum token n)
Erik Andersen13456d12000-03-16 08:09:57 +0000263{
Glenn L McGrath73db8be2004-08-11 02:45:47 +0000264 arith_t res;
Erik Andersen13456d12000-03-16 08:09:57 +0000265
266 res = aexpr(n);
Glenn L McGrathacfc0d82002-08-23 06:05:11 +0000267 if (t_lex(*++t_wp) == BOR) {
Erik Andersen13456d12000-03-16 08:09:57 +0000268 return oexpr(t_lex(*++t_wp)) || res;
Glenn L McGrathacfc0d82002-08-23 06:05:11 +0000269 }
Erik Andersen13456d12000-03-16 08:09:57 +0000270 t_wp--;
271 return res;
272}
273
Glenn L McGrath73db8be2004-08-11 02:45:47 +0000274static arith_t aexpr(enum token n)
Erik Andersen13456d12000-03-16 08:09:57 +0000275{
Glenn L McGrath73db8be2004-08-11 02:45:47 +0000276 arith_t res;
Erik Andersen13456d12000-03-16 08:09:57 +0000277
278 res = nexpr(n);
279 if (t_lex(*++t_wp) == BAND)
280 return aexpr(t_lex(*++t_wp)) && res;
281 t_wp--;
282 return res;
283}
284
Glenn L McGrath73db8be2004-08-11 02:45:47 +0000285static arith_t nexpr(enum token n)
Erik Andersen13456d12000-03-16 08:09:57 +0000286{
287 if (n == UNOT)
288 return !nexpr(t_lex(*++t_wp));
289 return primary(n);
290}
291
Glenn L McGrath73db8be2004-08-11 02:45:47 +0000292static arith_t primary(enum token n)
Erik Andersen13456d12000-03-16 08:09:57 +0000293{
Glenn L McGrath73db8be2004-08-11 02:45:47 +0000294 arith_t res;
Erik Andersen13456d12000-03-16 08:09:57 +0000295
Glenn L McGrathacfc0d82002-08-23 06:05:11 +0000296 if (n == EOI) {
Paul Fox6ab03782006-06-08 21:37:26 +0000297 syntax(NULL, "argument expected");
Glenn L McGrathacfc0d82002-08-23 06:05:11 +0000298 }
Erik Andersen13456d12000-03-16 08:09:57 +0000299 if (n == LPAREN) {
300 res = oexpr(t_lex(*++t_wp));
301 if (t_lex(*++t_wp) != RPAREN)
Paul Fox6ab03782006-06-08 21:37:26 +0000302 syntax(NULL, "closing paren expected");
Erik Andersen13456d12000-03-16 08:09:57 +0000303 return res;
304 }
305 if (t_wp_op && t_wp_op->op_type == UNOP) {
306 /* unary expression */
307 if (*++t_wp == NULL)
Paul Fox6ab03782006-06-08 21:37:26 +0000308 syntax(t_wp_op->op_text, "argument expected");
Bernhard Reutner-Fischercc890262007-03-30 18:23:36 +0000309 if (n == STREZ)
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000310 return t_wp[0][0] == '\0';
311 if (n == STRNZ)
312 return t_wp[0][0] != '\0';
313 if (n == FILTT)
Erik Andersen13456d12000-03-16 08:09:57 +0000314 return isatty(getn(*t_wp));
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000315 return filstat(*t_wp, n);
Erik Andersen13456d12000-03-16 08:09:57 +0000316 }
317
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000318 t_lex(t_wp[1]);
319 if (t_wp_op && t_wp_op->op_type == BINOP) {
Erik Andersen13456d12000-03-16 08:09:57 +0000320 return binop();
Glenn L McGrathacfc0d82002-08-23 06:05:11 +0000321 }
Erik Andersen13456d12000-03-16 08:09:57 +0000322
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000323 return t_wp[0][0] != '\0';
Erik Andersen13456d12000-03-16 08:09:57 +0000324}
325
Mike Frysinger4e5936e2005-04-16 04:30:38 +0000326static int binop(void)
Erik Andersen13456d12000-03-16 08:09:57 +0000327{
328 const char *opnd1, *opnd2;
329 struct t_op const *op;
Denis Vlasenkofe3e1772007-05-27 03:39:50 +0000330 arith_t val1, val2;
Erik Andersen13456d12000-03-16 08:09:57 +0000331
332 opnd1 = *t_wp;
333 (void) t_lex(*++t_wp);
334 op = t_wp_op;
335
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000336 opnd2 = *++t_wp;
337 if (opnd2 == NULL)
Paul Fox6ab03782006-06-08 21:37:26 +0000338 syntax(op->op_text, "argument expected");
Glenn L McGrathacfc0d82002-08-23 06:05:11 +0000339
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000340 if (is_int_op(op->op_num)) {
Bernhard Reutner-Fischercc890262007-03-30 18:23:36 +0000341 val1 = getn(opnd1);
342 val2 = getn(opnd2);
343 if (op->op_num == INTEQ)
344 return val1 == val2;
345 if (op->op_num == INTNE)
346 return val1 != val2;
347 if (op->op_num == INTGE)
348 return val1 >= val2;
349 if (op->op_num == INTGT)
350 return val1 > val2;
351 if (op->op_num == INTLE)
352 return val1 <= val2;
353 if (op->op_num == INTLT)
354 return val1 < val2;
Erik Andersen13456d12000-03-16 08:09:57 +0000355 }
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000356 if (is_str_op(op->op_num)) {
Bernhard Reutner-Fischercc890262007-03-30 18:23:36 +0000357 val1 = strcmp(opnd1, opnd2);
358 if (op->op_num == STREQ)
359 return val1 == 0;
360 if (op->op_num == STRNE)
361 return val1 != 0;
362 if (op->op_num == STRLT)
363 return val1 < 0;
364 if (op->op_num == STRGT)
365 return val1 > 0;
366 }
367 /* We are sure that these three are by now the only binops we didn't check
368 * yet, so we do not check if the class is correct:
369 */
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000370/* if (is_file_op(op->op_num)) */
Bernhard Reutner-Fischercc890262007-03-30 18:23:36 +0000371 {
372 struct stat b1, b2;
373
Denis Vlasenkofe3e1772007-05-27 03:39:50 +0000374 if (stat(opnd1, &b1) || stat(opnd2, &b2))
375 return 0; /* false, since at least one stat failed */
Bernhard Reutner-Fischercc890262007-03-30 18:23:36 +0000376 if (op->op_num == FILNT)
377 return b1.st_mtime > b2.st_mtime;
378 if (op->op_num == FILOT)
379 return b1.st_mtime < b2.st_mtime;
380 if (op->op_num == FILEQ)
381 return b1.st_dev == b2.st_dev && b1.st_ino == b2.st_ino;
382 }
383 return 1; /* NOTREACHED */
Erik Andersen13456d12000-03-16 08:09:57 +0000384}
385
Glenn L McGrathacfc0d82002-08-23 06:05:11 +0000386static int filstat(char *nm, enum token mode)
Erik Andersen13456d12000-03-16 08:09:57 +0000387{
388 struct stat s;
Denis Vlasenko88308fe2007-06-25 10:35:11 +0000389 int i = i; /* gcc 3.x thinks it can be used uninitialized */
Erik Andersen13456d12000-03-16 08:09:57 +0000390
391 if (mode == FILSYM) {
392#ifdef S_IFLNK
393 if (lstat(nm, &s) == 0) {
394 i = S_IFLNK;
395 goto filetype;
396 }
397#endif
398 return 0;
399 }
400
Glenn L McGrathacfc0d82002-08-23 06:05:11 +0000401 if (stat(nm, &s) != 0)
Erik Andersen13456d12000-03-16 08:09:57 +0000402 return 0;
Bernhard Reutner-Fischercc890262007-03-30 18:23:36 +0000403 if (mode == FILEXIST)
Erik Andersen13456d12000-03-16 08:09:57 +0000404 return 1;
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000405 if (is_file_access(mode)) {
Bernhard Reutner-Fischercc890262007-03-30 18:23:36 +0000406 if (mode == FILRD)
407 i = R_OK;
408 if (mode == FILWR)
409 i = W_OK;
410 if (mode == FILEX)
411 i = X_OK;
412 return test_eaccess(nm, i) == 0;
Erik Andersen13456d12000-03-16 08:09:57 +0000413 }
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000414 if (is_file_type(mode)) {
Bernhard Reutner-Fischercc890262007-03-30 18:23:36 +0000415 if (mode == FILREG)
416 i = S_IFREG;
417 if (mode == FILDIR)
418 i = S_IFDIR;
419 if (mode == FILCDEV)
420 i = S_IFCHR;
421 if (mode == FILBDEV)
422 i = S_IFBLK;
423 if (mode == FILFIFO) {
424#ifdef S_IFIFO
425 i = S_IFIFO;
426#else
427 return 0;
428#endif
429 }
430 if (mode == FILSOCK) {
431#ifdef S_IFSOCK
432 i = S_IFSOCK;
433#else
434 return 0;
435#endif
436 }
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000437 filetype:
Bernhard Reutner-Fischercc890262007-03-30 18:23:36 +0000438 return ((s.st_mode & S_IFMT) == i);
439 }
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000440 if (is_file_bit(mode)) {
Bernhard Reutner-Fischercc890262007-03-30 18:23:36 +0000441 if (mode == FILSUID)
442 i = S_ISUID;
443 if (mode == FILSGID)
444 i = S_ISGID;
445 if (mode == FILSTCK)
446 i = S_ISVTX;
447 return ((s.st_mode & i) != 0);
448 }
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000449 if (mode == FILGZ)
Bernhard Reutner-Fischercc890262007-03-30 18:23:36 +0000450 return s.st_size > 0L;
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000451 if (mode == FILUID)
Bernhard Reutner-Fischercc890262007-03-30 18:23:36 +0000452 return s.st_uid == geteuid();
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000453 if (mode == FILGID)
Bernhard Reutner-Fischercc890262007-03-30 18:23:36 +0000454 return s.st_gid == getegid();
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000455 return 1; /* NOTREACHED */
Erik Andersen13456d12000-03-16 08:09:57 +0000456}
457
Glenn L McGrathacfc0d82002-08-23 06:05:11 +0000458static enum token t_lex(char *s)
Erik Andersen13456d12000-03-16 08:09:57 +0000459{
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000460 const struct t_op *op;
Erik Andersen13456d12000-03-16 08:09:57 +0000461
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000462 t_wp_op = NULL;
463 if (s == NULL) {
Erik Andersen13456d12000-03-16 08:09:57 +0000464 return EOI;
465 }
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000466
467 op = ops;
468 do {
Erik Andersen13456d12000-03-16 08:09:57 +0000469 if (strcmp(s, op->op_text) == 0) {
470 t_wp_op = op;
471 return op->op_num;
472 }
473 op++;
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000474 } while (op < ops + NUM_OPS);
475
Erik Andersen13456d12000-03-16 08:09:57 +0000476 return OPERAND;
477}
478
479/* atoi with error detection */
Bernhard Reutner-Fischercc890262007-03-30 18:23:36 +0000480//XXX: FIXME: duplicate of existing libbb function?
Glenn L McGrath73db8be2004-08-11 02:45:47 +0000481static arith_t getn(const char *s)
Erik Andersen13456d12000-03-16 08:09:57 +0000482{
483 char *p;
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000484#if ENABLE_FEATURE_TEST_64
Glenn L McGrath73db8be2004-08-11 02:45:47 +0000485 long long r;
486#else
Erik Andersen13456d12000-03-16 08:09:57 +0000487 long r;
Glenn L McGrath73db8be2004-08-11 02:45:47 +0000488#endif
Erik Andersen13456d12000-03-16 08:09:57 +0000489
490 errno = 0;
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000491#if ENABLE_FEATURE_TEST_64
Glenn L McGrath73db8be2004-08-11 02:45:47 +0000492 r = strtoll(s, &p, 10);
493#else
Erik Andersen13456d12000-03-16 08:09:57 +0000494 r = strtol(s, &p, 10);
Glenn L McGrath73db8be2004-08-11 02:45:47 +0000495#endif
Erik Andersen13456d12000-03-16 08:09:57 +0000496
497 if (errno != 0)
Paul Fox6ab03782006-06-08 21:37:26 +0000498 syntax(s, "out of range");
Erik Andersen13456d12000-03-16 08:09:57 +0000499
Rob Landleyea224be2006-06-18 20:20:07 +0000500 if (*(skip_whitespace(p)))
Paul Fox6ab03782006-06-08 21:37:26 +0000501 syntax(s, "bad number");
Erik Andersen13456d12000-03-16 08:09:57 +0000502
Glenn L McGrath73db8be2004-08-11 02:45:47 +0000503 return r;
Erik Andersen13456d12000-03-16 08:09:57 +0000504}
505
Denis Vlasenko729bd9e2007-04-02 12:37:28 +0000506/* UNUSED
Glenn L McGrathacfc0d82002-08-23 06:05:11 +0000507static int newerf(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 && b1.st_mtime > b2.st_mtime);
Erik Andersen13456d12000-03-16 08:09:57 +0000513}
514
Glenn L McGrathacfc0d82002-08-23 06:05:11 +0000515static int olderf(const char *f1, const char *f2)
Erik Andersen13456d12000-03-16 08:09:57 +0000516{
517 struct stat b1, b2;
518
Glenn L McGrathacfc0d82002-08-23 06:05:11 +0000519 return (stat(f1, &b1) == 0 &&
520 stat(f2, &b2) == 0 && b1.st_mtime < b2.st_mtime);
Erik Andersen13456d12000-03-16 08:09:57 +0000521}
522
Glenn L McGrathacfc0d82002-08-23 06:05:11 +0000523static int equalf(const char *f1, const char *f2)
Erik Andersen13456d12000-03-16 08:09:57 +0000524{
525 struct stat b1, b2;
526
Glenn L McGrathacfc0d82002-08-23 06:05:11 +0000527 return (stat(f1, &b1) == 0 &&
528 stat(f2, &b2) == 0 &&
529 b1.st_dev == b2.st_dev && b1.st_ino == b2.st_ino);
Erik Andersen13456d12000-03-16 08:09:57 +0000530}
Denis Vlasenko729bd9e2007-04-02 12:37:28 +0000531*/
Erik Andersen13456d12000-03-16 08:09:57 +0000532
533/* Do the same thing access(2) does, but use the effective uid and gid,
534 and don't make the mistake of telling root that any file is
535 executable. */
Glenn L McGrathacfc0d82002-08-23 06:05:11 +0000536static int test_eaccess(char *path, int mode)
Erik Andersen13456d12000-03-16 08:09:57 +0000537{
538 struct stat st;
Eric Andersenfad04fd2000-07-14 06:49:52 +0000539 unsigned int euid = geteuid();
Erik Andersen13456d12000-03-16 08:09:57 +0000540
Glenn L McGrathacfc0d82002-08-23 06:05:11 +0000541 if (stat(path, &st) < 0)
Denis Vlasenko079f8af2006-11-27 16:49:31 +0000542 return -1;
Erik Andersen13456d12000-03-16 08:09:57 +0000543
544 if (euid == 0) {
545 /* Root can read or write any file. */
546 if (mode != X_OK)
Denis Vlasenko079f8af2006-11-27 16:49:31 +0000547 return 0;
Erik Andersen13456d12000-03-16 08:09:57 +0000548
549 /* Root can execute any file that has any one of the execute
550 bits set. */
551 if (st.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH))
Denis Vlasenko079f8af2006-11-27 16:49:31 +0000552 return 0;
Erik Andersen13456d12000-03-16 08:09:57 +0000553 }
554
Paul Fox6ab03782006-06-08 21:37:26 +0000555 if (st.st_uid == euid) /* owner */
Erik Andersen13456d12000-03-16 08:09:57 +0000556 mode <<= 6;
Glenn L McGrathacfc0d82002-08-23 06:05:11 +0000557 else if (is_a_group_member(st.st_gid))
Erik Andersen13456d12000-03-16 08:09:57 +0000558 mode <<= 3;
559
560 if (st.st_mode & mode)
Denis Vlasenko079f8af2006-11-27 16:49:31 +0000561 return 0;
Erik Andersen13456d12000-03-16 08:09:57 +0000562
Denis Vlasenko079f8af2006-11-27 16:49:31 +0000563 return -1;
Erik Andersen13456d12000-03-16 08:09:57 +0000564}
565
Mike Frysinger4e5936e2005-04-16 04:30:38 +0000566static void initialize_group_array(void)
Erik Andersen13456d12000-03-16 08:09:57 +0000567{
568 ngroups = getgroups(0, NULL);
Rob Landleyecb29572006-08-22 23:40:28 +0000569 if (ngroups > 0) {
Denis Vlasenkofe3e1772007-05-27 03:39:50 +0000570 /* FIXME: ash tries so hard to not die on OOM,
571 * and we spoil it with just one xrealloc here */
Denis Vlasenkob304ead2007-06-21 13:35:52 +0000572 /* We realloc, because test_main can be entered repeatedly by shell.
Denis Vlasenkofe3e1772007-05-27 03:39:50 +0000573 * Testcase (ash): 'while true; do test -x some_file; done'
574 * and watch top. (some_file must have owner != you) */
575 group_array = xrealloc(group_array, ngroups * sizeof(gid_t));
Rob Landleyecb29572006-08-22 23:40:28 +0000576 getgroups(ngroups, group_array);
577 }
Erik Andersen13456d12000-03-16 08:09:57 +0000578}
579
580/* Return non-zero if GID is one that we have in our groups list. */
Bernhard Reutner-Fischercc890262007-03-30 18:23:36 +0000581//XXX: FIXME: duplicate of existing libbb function?
582// see toplevel TODO file:
583// possible code duplication ingroup() and is_a_group_member()
Glenn L McGrathacfc0d82002-08-23 06:05:11 +0000584static int is_a_group_member(gid_t gid)
Erik Andersen13456d12000-03-16 08:09:57 +0000585{
"Robert P. J. Day"68229832006-07-01 13:08:46 +0000586 int i;
Erik Andersen13456d12000-03-16 08:09:57 +0000587
588 /* Short-circuit if possible, maybe saving a call to getgroups(). */
589 if (gid == getgid() || gid == getegid())
Denis Vlasenko079f8af2006-11-27 16:49:31 +0000590 return 1;
Erik Andersen13456d12000-03-16 08:09:57 +0000591
592 if (ngroups == 0)
Glenn L McGrathacfc0d82002-08-23 06:05:11 +0000593 initialize_group_array();
Erik Andersen13456d12000-03-16 08:09:57 +0000594
595 /* Search through the list looking for GID. */
596 for (i = 0; i < ngroups; i++)
597 if (gid == group_array[i])
Denis Vlasenko079f8af2006-11-27 16:49:31 +0000598 return 1;
Erik Andersen13456d12000-03-16 08:09:57 +0000599
Denis Vlasenko079f8af2006-11-27 16:49:31 +0000600 return 0;
Erik Andersen13456d12000-03-16 08:09:57 +0000601}