blob: 3c57cf418613109033e9be3f3cafd4e99ed87531 [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 +0000149
150#if ENABLE_FEATURE_TEST_64
Glenn L McGrath73db8be2004-08-11 02:45:47 +0000151typedef int64_t arith_t;
152#else
153typedef int arith_t;
154#endif
155
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000156/* Cannot eliminate these static data (do the G trick)
Denis Vlasenkob304ead2007-06-21 13:35:52 +0000157 * because of test_main usage from other applets */
Matt Kraaie6e81832000-12-30 07:46:23 +0000158static char **t_wp;
Denis Vlasenkofc5d1592007-09-21 17:58:43 +0000159static const struct t_op *t_wp_op;
Rob Landleyecb29572006-08-22 23:40:28 +0000160static gid_t *group_array;
Erik Andersen13456d12000-03-16 08:09:57 +0000161static int ngroups;
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000162static jmp_buf leaving;
Erik Andersen13456d12000-03-16 08:09:57 +0000163
Glenn L McGrathacfc0d82002-08-23 06:05:11 +0000164static enum token t_lex(char *s);
Glenn L McGrath73db8be2004-08-11 02:45:47 +0000165static arith_t oexpr(enum token n);
166static arith_t aexpr(enum token n);
167static arith_t nexpr(enum token n);
Eric Andersen74400cc2001-10-18 04:11:39 +0000168static int binop(void);
Glenn L McGrath73db8be2004-08-11 02:45:47 +0000169static arith_t primary(enum token n);
Eric Andersen74400cc2001-10-18 04:11:39 +0000170static int filstat(char *nm, enum token mode);
Glenn L McGrath73db8be2004-08-11 02:45:47 +0000171static arith_t getn(const char *s);
Denis Vlasenko729bd9e2007-04-02 12:37:28 +0000172/* UNUSED
Eric Andersen74400cc2001-10-18 04:11:39 +0000173static int newerf(const char *f1, const char *f2);
174static int olderf(const char *f1, const char *f2);
175static int equalf(const char *f1, const char *f2);
Denis Vlasenko729bd9e2007-04-02 12:37:28 +0000176*/
Eric Andersen74400cc2001-10-18 04:11:39 +0000177static int test_eaccess(char *path, int mode);
178static int is_a_group_member(gid_t gid);
179static void initialize_group_array(void);
Erik Andersen13456d12000-03-16 08:09:57 +0000180
Denis Vlasenkob304ead2007-06-21 13:35:52 +0000181int test_main(int argc, char **argv)
Erik Andersen13456d12000-03-16 08:09:57 +0000182{
Glenn L McGrathacfc0d82002-08-23 06:05:11 +0000183 int res;
Denis Vlasenkodc757aa2007-06-30 08:04:05 +0000184 const char *arg0;
Bernhard Reutner-Fischerbe992432007-06-15 10:36:49 +0000185 bool _off;
Erik Andersen13456d12000-03-16 08:09:57 +0000186
Denis Vlasenkodc757aa2007-06-30 08:04:05 +0000187 arg0 = bb_basename(argv[0]);
Denis Vlasenko89bca272007-06-08 07:33:45 +0000188 if (arg0[0] == '[') {
Denis Vlasenkob304ead2007-06-21 13:35:52 +0000189 --argc;
Denis Vlasenko89bca272007-06-08 07:33:45 +0000190 if (!arg0[1]) { /* "[" ? */
Denis Vlasenko89bca272007-06-08 07:33:45 +0000191 if (NOT_LONE_CHAR(argv[argc], ']')) {
192 bb_error_msg("missing ]");
193 return 2;
194 }
Denis Vlasenkob304ead2007-06-21 13:35:52 +0000195 } else { /* assuming "[[" */
Denis Vlasenko89bca272007-06-08 07:33:45 +0000196 if (strcmp(argv[argc], "]]") != 0) {
197 bb_error_msg("missing ]]");
198 return 2;
199 }
Paul Fox6ab03782006-06-08 21:37:26 +0000200 }
Denis Vlasenkob304ead2007-06-21 13:35:52 +0000201 argv[argc] = NULL;
Erik Andersen13456d12000-03-16 08:09:57 +0000202 }
Paul Fox6ab03782006-06-08 21:37:26 +0000203
204 res = setjmp(leaving);
205 if (res)
206 return res;
207
208 /* resetting ngroups is probably unnecessary. it will
209 * force a new call to getgroups(), which prevents using
210 * group data fetched during a previous call. but the
211 * only way the group data could be stale is if there's
212 * been an intervening call to setgroups(), and this
213 * isn't likely in the case of a shell. paranoia
214 * prevails...
215 */
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000216 ngroups = 0;
Paul Fox6ab03782006-06-08 21:37:26 +0000217
Erik Andersen13456d12000-03-16 08:09:57 +0000218 /* Implement special cases from POSIX.2, section 4.62.4 */
Bernhard Reutner-Fischercc890262007-03-30 18:23:36 +0000219 if (argc == 1)
Paul Fox6ab03782006-06-08 21:37:26 +0000220 return 1;
Bernhard Reutner-Fischercc890262007-03-30 18:23:36 +0000221 if (argc == 2)
Paul Fox6ab03782006-06-08 21:37:26 +0000222 return *argv[1] == '\0';
Bernhard Reutner-Fischercc890262007-03-30 18:23:36 +0000223//assert(argc);
Bernhard Reutner-Fischerbe992432007-06-15 10:36:49 +0000224 /* remember if we saw argc==4 which wants *no* '!' test */
225 _off = argc - 4;
226 if (_off ?
227 (LONE_CHAR(argv[1], '!'))
228 : (argv[1][0] != '!' || argv[1][1] != '\0'))
Bernhard Reutner-Fischerfdcbc4e2007-06-12 13:21:08 +0000229 {
Bernhard Reutner-Fischercc890262007-03-30 18:23:36 +0000230 if (argc == 3)
Paul Fox6ab03782006-06-08 21:37:26 +0000231 return *argv[2] != '\0';
Bernhard Reutner-Fischerbe992432007-06-15 10:36:49 +0000232
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000233 t_lex(argv[2 + _off]);
234 if (t_wp_op && t_wp_op->op_type == BINOP) {
235 t_wp = &argv[1 + _off];
Bernhard Reutner-Fischerbe992432007-06-15 10:36:49 +0000236 return binop() == _off;
Erik Andersen13456d12000-03-16 08:09:57 +0000237 }
Erik Andersen13456d12000-03-16 08:09:57 +0000238 }
Erik Andersen13456d12000-03-16 08:09:57 +0000239 t_wp = &argv[1];
240 res = !oexpr(t_lex(*t_wp));
241
Paul Fox6ab03782006-06-08 21:37:26 +0000242 if (*t_wp != NULL && *++t_wp != NULL) {
243 bb_error_msg("%s: unknown operand", *t_wp);
244 return 2;
245 }
246 return res;
247}
Erik Andersen13456d12000-03-16 08:09:57 +0000248
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000249static void syntax(const char *op, const char *msg) ATTRIBUTE_NORETURN;
Paul Fox6ab03782006-06-08 21:37:26 +0000250static void syntax(const char *op, const char *msg)
251{
252 if (op && *op) {
253 bb_error_msg("%s: %s", op, msg);
254 } else {
Denis Vlasenkofe3e1772007-05-27 03:39:50 +0000255 bb_error_msg("%s: %s"+4, msg);
Paul Fox6ab03782006-06-08 21:37:26 +0000256 }
257 longjmp(leaving, 2);
Erik Andersen13456d12000-03-16 08:09:57 +0000258}
259
Glenn L McGrath73db8be2004-08-11 02:45:47 +0000260static arith_t oexpr(enum token n)
Erik Andersen13456d12000-03-16 08:09:57 +0000261{
Glenn L McGrath73db8be2004-08-11 02:45:47 +0000262 arith_t res;
Erik Andersen13456d12000-03-16 08:09:57 +0000263
264 res = aexpr(n);
Glenn L McGrathacfc0d82002-08-23 06:05:11 +0000265 if (t_lex(*++t_wp) == BOR) {
Erik Andersen13456d12000-03-16 08:09:57 +0000266 return oexpr(t_lex(*++t_wp)) || res;
Glenn L McGrathacfc0d82002-08-23 06:05:11 +0000267 }
Erik Andersen13456d12000-03-16 08:09:57 +0000268 t_wp--;
269 return res;
270}
271
Glenn L McGrath73db8be2004-08-11 02:45:47 +0000272static arith_t aexpr(enum token n)
Erik Andersen13456d12000-03-16 08:09:57 +0000273{
Glenn L McGrath73db8be2004-08-11 02:45:47 +0000274 arith_t res;
Erik Andersen13456d12000-03-16 08:09:57 +0000275
276 res = nexpr(n);
277 if (t_lex(*++t_wp) == BAND)
278 return aexpr(t_lex(*++t_wp)) && res;
279 t_wp--;
280 return res;
281}
282
Glenn L McGrath73db8be2004-08-11 02:45:47 +0000283static arith_t nexpr(enum token n)
Erik Andersen13456d12000-03-16 08:09:57 +0000284{
285 if (n == UNOT)
286 return !nexpr(t_lex(*++t_wp));
287 return primary(n);
288}
289
Glenn L McGrath73db8be2004-08-11 02:45:47 +0000290static arith_t primary(enum token n)
Erik Andersen13456d12000-03-16 08:09:57 +0000291{
Glenn L McGrath73db8be2004-08-11 02:45:47 +0000292 arith_t res;
Erik Andersen13456d12000-03-16 08:09:57 +0000293
Glenn L McGrathacfc0d82002-08-23 06:05:11 +0000294 if (n == EOI) {
Paul Fox6ab03782006-06-08 21:37:26 +0000295 syntax(NULL, "argument expected");
Glenn L McGrathacfc0d82002-08-23 06:05:11 +0000296 }
Erik Andersen13456d12000-03-16 08:09:57 +0000297 if (n == LPAREN) {
298 res = oexpr(t_lex(*++t_wp));
299 if (t_lex(*++t_wp) != RPAREN)
Paul Fox6ab03782006-06-08 21:37:26 +0000300 syntax(NULL, "closing paren expected");
Erik Andersen13456d12000-03-16 08:09:57 +0000301 return res;
302 }
303 if (t_wp_op && t_wp_op->op_type == UNOP) {
304 /* unary expression */
305 if (*++t_wp == NULL)
Paul Fox6ab03782006-06-08 21:37:26 +0000306 syntax(t_wp_op->op_text, "argument expected");
Bernhard Reutner-Fischercc890262007-03-30 18:23:36 +0000307 if (n == STREZ)
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000308 return t_wp[0][0] == '\0';
309 if (n == STRNZ)
310 return t_wp[0][0] != '\0';
311 if (n == FILTT)
Erik Andersen13456d12000-03-16 08:09:57 +0000312 return isatty(getn(*t_wp));
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000313 return filstat(*t_wp, n);
Erik Andersen13456d12000-03-16 08:09:57 +0000314 }
315
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000316 t_lex(t_wp[1]);
317 if (t_wp_op && t_wp_op->op_type == BINOP) {
Erik Andersen13456d12000-03-16 08:09:57 +0000318 return binop();
Glenn L McGrathacfc0d82002-08-23 06:05:11 +0000319 }
Erik Andersen13456d12000-03-16 08:09:57 +0000320
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000321 return t_wp[0][0] != '\0';
Erik Andersen13456d12000-03-16 08:09:57 +0000322}
323
Mike Frysinger4e5936e2005-04-16 04:30:38 +0000324static int binop(void)
Erik Andersen13456d12000-03-16 08:09:57 +0000325{
326 const char *opnd1, *opnd2;
327 struct t_op const *op;
Denis Vlasenkofe3e1772007-05-27 03:39:50 +0000328 arith_t val1, val2;
Erik Andersen13456d12000-03-16 08:09:57 +0000329
330 opnd1 = *t_wp;
331 (void) t_lex(*++t_wp);
332 op = t_wp_op;
333
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000334 opnd2 = *++t_wp;
335 if (opnd2 == NULL)
Paul Fox6ab03782006-06-08 21:37:26 +0000336 syntax(op->op_text, "argument expected");
Glenn L McGrathacfc0d82002-08-23 06:05:11 +0000337
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000338 if (is_int_op(op->op_num)) {
Bernhard Reutner-Fischercc890262007-03-30 18:23:36 +0000339 val1 = getn(opnd1);
340 val2 = getn(opnd2);
341 if (op->op_num == INTEQ)
342 return val1 == val2;
343 if (op->op_num == INTNE)
344 return val1 != val2;
345 if (op->op_num == INTGE)
346 return val1 >= val2;
347 if (op->op_num == INTGT)
348 return val1 > val2;
349 if (op->op_num == INTLE)
350 return val1 <= val2;
351 if (op->op_num == INTLT)
352 return val1 < val2;
Erik Andersen13456d12000-03-16 08:09:57 +0000353 }
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000354 if (is_str_op(op->op_num)) {
Bernhard Reutner-Fischercc890262007-03-30 18:23:36 +0000355 val1 = strcmp(opnd1, opnd2);
356 if (op->op_num == STREQ)
357 return val1 == 0;
358 if (op->op_num == STRNE)
359 return val1 != 0;
360 if (op->op_num == STRLT)
361 return val1 < 0;
362 if (op->op_num == STRGT)
363 return val1 > 0;
364 }
365 /* We are sure that these three are by now the only binops we didn't check
366 * yet, so we do not check if the class is correct:
367 */
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000368/* if (is_file_op(op->op_num)) */
Bernhard Reutner-Fischercc890262007-03-30 18:23:36 +0000369 {
370 struct stat b1, b2;
371
Denis Vlasenkofe3e1772007-05-27 03:39:50 +0000372 if (stat(opnd1, &b1) || stat(opnd2, &b2))
373 return 0; /* false, since at least one stat failed */
Bernhard Reutner-Fischercc890262007-03-30 18:23:36 +0000374 if (op->op_num == FILNT)
375 return b1.st_mtime > b2.st_mtime;
376 if (op->op_num == FILOT)
377 return b1.st_mtime < b2.st_mtime;
378 if (op->op_num == FILEQ)
379 return b1.st_dev == b2.st_dev && b1.st_ino == b2.st_ino;
380 }
381 return 1; /* NOTREACHED */
Erik Andersen13456d12000-03-16 08:09:57 +0000382}
383
Glenn L McGrathacfc0d82002-08-23 06:05:11 +0000384static int filstat(char *nm, enum token mode)
Erik Andersen13456d12000-03-16 08:09:57 +0000385{
386 struct stat s;
Denis Vlasenko88308fe2007-06-25 10:35:11 +0000387 int i = i; /* gcc 3.x thinks it can be used uninitialized */
Erik Andersen13456d12000-03-16 08:09:57 +0000388
389 if (mode == FILSYM) {
390#ifdef S_IFLNK
391 if (lstat(nm, &s) == 0) {
392 i = S_IFLNK;
393 goto filetype;
394 }
395#endif
396 return 0;
397 }
398
Glenn L McGrathacfc0d82002-08-23 06:05:11 +0000399 if (stat(nm, &s) != 0)
Erik Andersen13456d12000-03-16 08:09:57 +0000400 return 0;
Bernhard Reutner-Fischercc890262007-03-30 18:23:36 +0000401 if (mode == FILEXIST)
Erik Andersen13456d12000-03-16 08:09:57 +0000402 return 1;
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000403 if (is_file_access(mode)) {
Bernhard Reutner-Fischercc890262007-03-30 18:23:36 +0000404 if (mode == FILRD)
405 i = R_OK;
406 if (mode == FILWR)
407 i = W_OK;
408 if (mode == FILEX)
409 i = X_OK;
410 return test_eaccess(nm, i) == 0;
Erik Andersen13456d12000-03-16 08:09:57 +0000411 }
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000412 if (is_file_type(mode)) {
Bernhard Reutner-Fischercc890262007-03-30 18:23:36 +0000413 if (mode == FILREG)
414 i = S_IFREG;
415 if (mode == FILDIR)
416 i = S_IFDIR;
417 if (mode == FILCDEV)
418 i = S_IFCHR;
419 if (mode == FILBDEV)
420 i = S_IFBLK;
421 if (mode == FILFIFO) {
422#ifdef S_IFIFO
423 i = S_IFIFO;
424#else
425 return 0;
426#endif
427 }
428 if (mode == FILSOCK) {
429#ifdef S_IFSOCK
430 i = S_IFSOCK;
431#else
432 return 0;
433#endif
434 }
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000435 filetype:
Bernhard Reutner-Fischercc890262007-03-30 18:23:36 +0000436 return ((s.st_mode & S_IFMT) == i);
437 }
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000438 if (is_file_bit(mode)) {
Bernhard Reutner-Fischercc890262007-03-30 18:23:36 +0000439 if (mode == FILSUID)
440 i = S_ISUID;
441 if (mode == FILSGID)
442 i = S_ISGID;
443 if (mode == FILSTCK)
444 i = S_ISVTX;
445 return ((s.st_mode & i) != 0);
446 }
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000447 if (mode == FILGZ)
Bernhard Reutner-Fischercc890262007-03-30 18:23:36 +0000448 return s.st_size > 0L;
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000449 if (mode == FILUID)
Bernhard Reutner-Fischercc890262007-03-30 18:23:36 +0000450 return s.st_uid == geteuid();
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000451 if (mode == FILGID)
Bernhard Reutner-Fischercc890262007-03-30 18:23:36 +0000452 return s.st_gid == getegid();
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000453 return 1; /* NOTREACHED */
Erik Andersen13456d12000-03-16 08:09:57 +0000454}
455
Glenn L McGrathacfc0d82002-08-23 06:05:11 +0000456static enum token t_lex(char *s)
Erik Andersen13456d12000-03-16 08:09:57 +0000457{
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000458 const struct t_op *op;
Erik Andersen13456d12000-03-16 08:09:57 +0000459
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000460 t_wp_op = NULL;
461 if (s == NULL) {
Erik Andersen13456d12000-03-16 08:09:57 +0000462 return EOI;
463 }
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000464
465 op = ops;
466 do {
Erik Andersen13456d12000-03-16 08:09:57 +0000467 if (strcmp(s, op->op_text) == 0) {
468 t_wp_op = op;
469 return op->op_num;
470 }
471 op++;
Denis Vlasenko80b8b392007-06-25 10:55:35 +0000472 } while (op < ops + ARRAY_SIZE(ops));
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000473
Erik Andersen13456d12000-03-16 08:09:57 +0000474 return OPERAND;
475}
476
477/* atoi with error detection */
Bernhard Reutner-Fischercc890262007-03-30 18:23:36 +0000478//XXX: FIXME: duplicate of existing libbb function?
Glenn L McGrath73db8be2004-08-11 02:45:47 +0000479static arith_t getn(const char *s)
Erik Andersen13456d12000-03-16 08:09:57 +0000480{
481 char *p;
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000482#if ENABLE_FEATURE_TEST_64
Glenn L McGrath73db8be2004-08-11 02:45:47 +0000483 long long r;
484#else
Erik Andersen13456d12000-03-16 08:09:57 +0000485 long r;
Glenn L McGrath73db8be2004-08-11 02:45:47 +0000486#endif
Erik Andersen13456d12000-03-16 08:09:57 +0000487
488 errno = 0;
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000489#if ENABLE_FEATURE_TEST_64
Glenn L McGrath73db8be2004-08-11 02:45:47 +0000490 r = strtoll(s, &p, 10);
491#else
Erik Andersen13456d12000-03-16 08:09:57 +0000492 r = strtol(s, &p, 10);
Glenn L McGrath73db8be2004-08-11 02:45:47 +0000493#endif
Erik Andersen13456d12000-03-16 08:09:57 +0000494
495 if (errno != 0)
Paul Fox6ab03782006-06-08 21:37:26 +0000496 syntax(s, "out of range");
Erik Andersen13456d12000-03-16 08:09:57 +0000497
Rob Landleyea224be2006-06-18 20:20:07 +0000498 if (*(skip_whitespace(p)))
Paul Fox6ab03782006-06-08 21:37:26 +0000499 syntax(s, "bad number");
Erik Andersen13456d12000-03-16 08:09:57 +0000500
Glenn L McGrath73db8be2004-08-11 02:45:47 +0000501 return r;
Erik Andersen13456d12000-03-16 08:09:57 +0000502}
503
Denis Vlasenko729bd9e2007-04-02 12:37:28 +0000504/* UNUSED
Glenn L McGrathacfc0d82002-08-23 06:05:11 +0000505static int newerf(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 && b1.st_mtime > b2.st_mtime);
Erik Andersen13456d12000-03-16 08:09:57 +0000511}
512
Glenn L McGrathacfc0d82002-08-23 06:05:11 +0000513static int olderf(const char *f1, const char *f2)
Erik Andersen13456d12000-03-16 08:09:57 +0000514{
515 struct stat b1, b2;
516
Glenn L McGrathacfc0d82002-08-23 06:05:11 +0000517 return (stat(f1, &b1) == 0 &&
518 stat(f2, &b2) == 0 && b1.st_mtime < b2.st_mtime);
Erik Andersen13456d12000-03-16 08:09:57 +0000519}
520
Glenn L McGrathacfc0d82002-08-23 06:05:11 +0000521static int equalf(const char *f1, const char *f2)
Erik Andersen13456d12000-03-16 08:09:57 +0000522{
523 struct stat b1, b2;
524
Glenn L McGrathacfc0d82002-08-23 06:05:11 +0000525 return (stat(f1, &b1) == 0 &&
526 stat(f2, &b2) == 0 &&
527 b1.st_dev == b2.st_dev && b1.st_ino == b2.st_ino);
Erik Andersen13456d12000-03-16 08:09:57 +0000528}
Denis Vlasenko729bd9e2007-04-02 12:37:28 +0000529*/
Erik Andersen13456d12000-03-16 08:09:57 +0000530
531/* Do the same thing access(2) does, but use the effective uid and gid,
532 and don't make the mistake of telling root that any file is
533 executable. */
Glenn L McGrathacfc0d82002-08-23 06:05:11 +0000534static int test_eaccess(char *path, int mode)
Erik Andersen13456d12000-03-16 08:09:57 +0000535{
536 struct stat st;
Eric Andersenfad04fd2000-07-14 06:49:52 +0000537 unsigned int euid = geteuid();
Erik Andersen13456d12000-03-16 08:09:57 +0000538
Glenn L McGrathacfc0d82002-08-23 06:05:11 +0000539 if (stat(path, &st) < 0)
Denis Vlasenko079f8af2006-11-27 16:49:31 +0000540 return -1;
Erik Andersen13456d12000-03-16 08:09:57 +0000541
542 if (euid == 0) {
543 /* Root can read or write any file. */
544 if (mode != X_OK)
Denis Vlasenko079f8af2006-11-27 16:49:31 +0000545 return 0;
Erik Andersen13456d12000-03-16 08:09:57 +0000546
547 /* Root can execute any file that has any one of the execute
548 bits set. */
549 if (st.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH))
Denis Vlasenko079f8af2006-11-27 16:49:31 +0000550 return 0;
Erik Andersen13456d12000-03-16 08:09:57 +0000551 }
552
Paul Fox6ab03782006-06-08 21:37:26 +0000553 if (st.st_uid == euid) /* owner */
Erik Andersen13456d12000-03-16 08:09:57 +0000554 mode <<= 6;
Glenn L McGrathacfc0d82002-08-23 06:05:11 +0000555 else if (is_a_group_member(st.st_gid))
Erik Andersen13456d12000-03-16 08:09:57 +0000556 mode <<= 3;
557
558 if (st.st_mode & mode)
Denis Vlasenko079f8af2006-11-27 16:49:31 +0000559 return 0;
Erik Andersen13456d12000-03-16 08:09:57 +0000560
Denis Vlasenko079f8af2006-11-27 16:49:31 +0000561 return -1;
Erik Andersen13456d12000-03-16 08:09:57 +0000562}
563
Mike Frysinger4e5936e2005-04-16 04:30:38 +0000564static void initialize_group_array(void)
Erik Andersen13456d12000-03-16 08:09:57 +0000565{
566 ngroups = getgroups(0, NULL);
Rob Landleyecb29572006-08-22 23:40:28 +0000567 if (ngroups > 0) {
Denis Vlasenkofe3e1772007-05-27 03:39:50 +0000568 /* FIXME: ash tries so hard to not die on OOM,
569 * and we spoil it with just one xrealloc here */
Denis Vlasenkob304ead2007-06-21 13:35:52 +0000570 /* We realloc, because test_main can be entered repeatedly by shell.
Denis Vlasenkofe3e1772007-05-27 03:39:50 +0000571 * Testcase (ash): 'while true; do test -x some_file; done'
572 * and watch top. (some_file must have owner != you) */
573 group_array = xrealloc(group_array, ngroups * sizeof(gid_t));
Rob Landleyecb29572006-08-22 23:40:28 +0000574 getgroups(ngroups, group_array);
575 }
Erik Andersen13456d12000-03-16 08:09:57 +0000576}
577
578/* Return non-zero if GID is one that we have in our groups list. */
Bernhard Reutner-Fischercc890262007-03-30 18:23:36 +0000579//XXX: FIXME: duplicate of existing libbb function?
580// see toplevel TODO file:
581// possible code duplication ingroup() and is_a_group_member()
Glenn L McGrathacfc0d82002-08-23 06:05:11 +0000582static int is_a_group_member(gid_t gid)
Erik Andersen13456d12000-03-16 08:09:57 +0000583{
"Robert P. J. Day"68229832006-07-01 13:08:46 +0000584 int i;
Erik Andersen13456d12000-03-16 08:09:57 +0000585
586 /* Short-circuit if possible, maybe saving a call to getgroups(). */
587 if (gid == getgid() || gid == getegid())
Denis Vlasenko079f8af2006-11-27 16:49:31 +0000588 return 1;
Erik Andersen13456d12000-03-16 08:09:57 +0000589
590 if (ngroups == 0)
Glenn L McGrathacfc0d82002-08-23 06:05:11 +0000591 initialize_group_array();
Erik Andersen13456d12000-03-16 08:09:57 +0000592
593 /* Search through the list looking for GID. */
594 for (i = 0; i < ngroups; i++)
595 if (gid == group_array[i])
Denis Vlasenko079f8af2006-11-27 16:49:31 +0000596 return 1;
Erik Andersen13456d12000-03-16 08:09:57 +0000597
Denis Vlasenko079f8af2006-11-27 16:49:31 +0000598 return 0;
Erik Andersen13456d12000-03-16 08:09:57 +0000599}