Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
Erik Andersen | 5e1189e | 2000-04-15 16:34:54 +0000 | [diff] [blame] | 3 | * test implementation for busybox |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 4 | * |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 5 | * Copyright (c) by a whole pile of folks: |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 6 | * |
Paul Fox | 6ab0378 | 2006-06-08 21:37:26 +0000 | [diff] [blame] | 7 | * 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-Fischer | cc89026 | 2007-03-30 18:23:36 +0000 | [diff] [blame] | 15 | * modified by Bernhard Fischer to be useable (i.e. a bit less bloaty). |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 16 | * |
Paul Fox | 6ab0378 | 2006-06-08 21:37:26 +0000 | [diff] [blame] | 17 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 18 | * |
| 19 | * Original copyright notice states: |
Paul Fox | 6ab0378 | 2006-06-08 21:37:26 +0000 | [diff] [blame] | 20 | * "This program is in the Public Domain." |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 21 | */ |
| 22 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 23 | #include "libbb.h" |
Paul Fox | 6ab0378 | 2006-06-08 21:37:26 +0000 | [diff] [blame] | 24 | #include <setjmp.h> |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 25 | |
Denis Vlasenko | 6672c8e | 2007-11-30 07:29:05 +0000 | [diff] [blame] | 26 | /* This is a NOFORK applet. Be very careful! */ |
| 27 | |
| 28 | /* test_main() is called from shells, and we need to be extra careful here. |
| 29 | * This is true regardless of PREFER_APPLETS and STANDALONE_SHELL |
| 30 | * state. */ |
Denis Vlasenko | 99912ca | 2007-04-10 15:43:37 +0000 | [diff] [blame] | 31 | |
| 32 | |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 33 | /* test(1) accepts the following grammar: |
| 34 | oexpr ::= aexpr | aexpr "-o" oexpr ; |
| 35 | aexpr ::= nexpr | nexpr "-a" aexpr ; |
| 36 | nexpr ::= primary | "!" primary |
Paul Fox | 6ab0378 | 2006-06-08 21:37:26 +0000 | [diff] [blame] | 37 | primary ::= unary-operator operand |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 38 | | operand binary-operator operand |
| 39 | | operand |
| 40 | | "(" oexpr ")" |
| 41 | ; |
| 42 | unary-operator ::= "-r"|"-w"|"-x"|"-f"|"-d"|"-c"|"-b"|"-p"| |
| 43 | "-u"|"-g"|"-k"|"-s"|"-t"|"-z"|"-n"|"-o"|"-O"|"-G"|"-L"|"-S"; |
| 44 | |
Mike Frysinger | 75ac42b | 2005-04-14 02:49:22 +0000 | [diff] [blame] | 45 | binary-operator ::= "="|"=="|"!="|"-eq"|"-ne"|"-ge"|"-gt"|"-le"|"-lt"| |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 46 | "-nt"|"-ot"|"-ef"; |
| 47 | operand ::= <any legal UNIX file name> |
| 48 | */ |
| 49 | |
| 50 | enum token { |
| 51 | EOI, |
| 52 | FILRD, |
| 53 | FILWR, |
| 54 | FILEX, |
| 55 | FILEXIST, |
| 56 | FILREG, |
| 57 | FILDIR, |
| 58 | FILCDEV, |
| 59 | FILBDEV, |
| 60 | FILFIFO, |
| 61 | FILSOCK, |
| 62 | FILSYM, |
| 63 | FILGZ, |
| 64 | FILTT, |
| 65 | FILSUID, |
| 66 | FILSGID, |
| 67 | FILSTCK, |
| 68 | FILNT, |
| 69 | FILOT, |
| 70 | FILEQ, |
| 71 | FILUID, |
| 72 | FILGID, |
| 73 | STREZ, |
| 74 | STRNZ, |
| 75 | STREQ, |
| 76 | STRNE, |
| 77 | STRLT, |
| 78 | STRGT, |
| 79 | INTEQ, |
| 80 | INTNE, |
| 81 | INTGE, |
| 82 | INTGT, |
| 83 | INTLE, |
| 84 | INTLT, |
| 85 | UNOT, |
| 86 | BAND, |
| 87 | BOR, |
| 88 | LPAREN, |
| 89 | RPAREN, |
| 90 | OPERAND |
| 91 | }; |
Denis Vlasenko | 6672c8e | 2007-11-30 07:29:05 +0000 | [diff] [blame] | 92 | #define is_int_op(a) (((unsigned char)((a) - INTEQ)) <= 5) |
| 93 | #define is_str_op(a) (((unsigned char)((a) - STREZ)) <= 5) |
| 94 | #define is_file_op(a) (((unsigned char)((a) - FILNT)) <= 2) |
Denis Vlasenko | dcf4de2 | 2007-05-01 20:07:29 +0000 | [diff] [blame] | 95 | #define is_file_access(a) (((unsigned char)((a) - FILRD)) <= 2) |
Denis Vlasenko | 6672c8e | 2007-11-30 07:29:05 +0000 | [diff] [blame] | 96 | #define is_file_type(a) (((unsigned char)((a) - FILREG)) <= 5) |
| 97 | #define is_file_bit(a) (((unsigned char)((a) - FILSUID)) <= 2) |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 98 | enum token_types { |
| 99 | UNOP, |
| 100 | BINOP, |
| 101 | BUNOP, |
| 102 | BBINOP, |
| 103 | PAREN |
| 104 | }; |
| 105 | |
Eric Andersen | 92d2324 | 2001-03-19 23:49:41 +0000 | [diff] [blame] | 106 | static const struct t_op { |
Denis Vlasenko | dcf4de2 | 2007-05-01 20:07:29 +0000 | [diff] [blame] | 107 | char op_text[4]; |
Bernhard Reutner-Fischer | cc89026 | 2007-03-30 18:23:36 +0000 | [diff] [blame] | 108 | unsigned char op_num, op_type; |
Glenn L McGrath | acfc0d8 | 2002-08-23 06:05:11 +0000 | [diff] [blame] | 109 | } ops[] = { |
Denis Vlasenko | dcf4de2 | 2007-05-01 20:07:29 +0000 | [diff] [blame] | 110 | { "-r", FILRD , UNOP }, |
| 111 | { "-w", FILWR , UNOP }, |
| 112 | { "-x", FILEX , UNOP }, |
| 113 | { "-e", FILEXIST, UNOP }, |
| 114 | { "-f", FILREG , UNOP }, |
| 115 | { "-d", FILDIR , UNOP }, |
| 116 | { "-c", FILCDEV , UNOP }, |
| 117 | { "-b", FILBDEV , UNOP }, |
| 118 | { "-p", FILFIFO , UNOP }, |
| 119 | { "-u", FILSUID , UNOP }, |
| 120 | { "-g", FILSGID , UNOP }, |
| 121 | { "-k", FILSTCK , UNOP }, |
| 122 | { "-s", FILGZ , UNOP }, |
| 123 | { "-t", FILTT , UNOP }, |
| 124 | { "-z", STREZ , UNOP }, |
| 125 | { "-n", STRNZ , UNOP }, |
| 126 | { "-h", FILSYM , UNOP }, /* for backwards compat */ |
| 127 | |
| 128 | { "-O" , FILUID , UNOP }, |
| 129 | { "-G" , FILGID , UNOP }, |
| 130 | { "-L" , FILSYM , UNOP }, |
| 131 | { "-S" , FILSOCK, UNOP }, |
| 132 | { "=" , STREQ , BINOP }, |
| 133 | { "==" , STREQ , BINOP }, |
| 134 | { "!=" , STRNE , BINOP }, |
| 135 | { "<" , STRLT , BINOP }, |
| 136 | { ">" , STRGT , BINOP }, |
| 137 | { "-eq", INTEQ , BINOP }, |
| 138 | { "-ne", INTNE , BINOP }, |
| 139 | { "-ge", INTGE , BINOP }, |
| 140 | { "-gt", INTGT , BINOP }, |
| 141 | { "-le", INTLE , BINOP }, |
| 142 | { "-lt", INTLT , BINOP }, |
| 143 | { "-nt", FILNT , BINOP }, |
| 144 | { "-ot", FILOT , BINOP }, |
| 145 | { "-ef", FILEQ , BINOP }, |
| 146 | { "!" , UNOT , BUNOP }, |
| 147 | { "-a" , BAND , BBINOP }, |
| 148 | { "-o" , BOR , BBINOP }, |
| 149 | { "(" , LPAREN , PAREN }, |
| 150 | { ")" , RPAREN , PAREN }, |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 151 | }; |
| 152 | |
Denis Vlasenko | dcf4de2 | 2007-05-01 20:07:29 +0000 | [diff] [blame] | 153 | |
| 154 | #if ENABLE_FEATURE_TEST_64 |
Glenn L McGrath | 73db8be | 2004-08-11 02:45:47 +0000 | [diff] [blame] | 155 | typedef int64_t arith_t; |
| 156 | #else |
| 157 | typedef int arith_t; |
| 158 | #endif |
| 159 | |
Denis Vlasenko | 6672c8e | 2007-11-30 07:29:05 +0000 | [diff] [blame] | 160 | |
| 161 | /* We try to minimize both static and stack usage. */ |
| 162 | struct statics { |
| 163 | char **t_wp; |
| 164 | const struct t_op *t_wp_op; |
| 165 | gid_t *group_array; |
| 166 | int ngroups; |
| 167 | jmp_buf leaving; |
| 168 | }; |
| 169 | |
| 170 | /* Make it reside in writable memory, yet make compiler understand |
| 171 | * that it is not going to change. */ |
| 172 | static struct statics *const ptr_to_statics __attribute__ ((section (".data"))); |
| 173 | |
| 174 | #define S (*ptr_to_statics) |
| 175 | #define t_wp (S.t_wp ) |
| 176 | #define t_wp_op (S.t_wp_op ) |
| 177 | #define group_array (S.group_array ) |
| 178 | #define ngroups (S.ngroups ) |
| 179 | #define leaving (S.leaving ) |
| 180 | |
| 181 | #define INIT_S() do { \ |
| 182 | (*(struct statics**)&ptr_to_statics) = xzalloc(sizeof(S)); \ |
Denis Vlasenko | 574f2f4 | 2008-02-27 18:41:59 +0000 | [diff] [blame^] | 183 | barrier(); \ |
Denis Vlasenko | 6672c8e | 2007-11-30 07:29:05 +0000 | [diff] [blame] | 184 | } while (0) |
| 185 | #define DEINIT_S() do { \ |
| 186 | free(ptr_to_statics); \ |
| 187 | } while (0) |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 188 | |
Glenn L McGrath | 73db8be | 2004-08-11 02:45:47 +0000 | [diff] [blame] | 189 | static arith_t primary(enum token n); |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 190 | |
Denis Vlasenko | dcf4de2 | 2007-05-01 20:07:29 +0000 | [diff] [blame] | 191 | static void syntax(const char *op, const char *msg) ATTRIBUTE_NORETURN; |
Paul Fox | 6ab0378 | 2006-06-08 21:37:26 +0000 | [diff] [blame] | 192 | static void syntax(const char *op, const char *msg) |
| 193 | { |
| 194 | if (op && *op) { |
| 195 | bb_error_msg("%s: %s", op, msg); |
| 196 | } else { |
Denis Vlasenko | fe3e177 | 2007-05-27 03:39:50 +0000 | [diff] [blame] | 197 | bb_error_msg("%s: %s"+4, msg); |
Paul Fox | 6ab0378 | 2006-06-08 21:37:26 +0000 | [diff] [blame] | 198 | } |
| 199 | longjmp(leaving, 2); |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 200 | } |
| 201 | |
Bernhard Reutner-Fischer | a702457 | 2007-11-16 12:20:30 +0000 | [diff] [blame] | 202 | /* atoi with error detection */ |
| 203 | //XXX: FIXME: duplicate of existing libbb function? |
| 204 | static arith_t getn(const char *s) |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 205 | { |
Bernhard Reutner-Fischer | a702457 | 2007-11-16 12:20:30 +0000 | [diff] [blame] | 206 | char *p; |
| 207 | #if ENABLE_FEATURE_TEST_64 |
| 208 | long long r; |
| 209 | #else |
| 210 | long r; |
| 211 | #endif |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 212 | |
Bernhard Reutner-Fischer | a702457 | 2007-11-16 12:20:30 +0000 | [diff] [blame] | 213 | errno = 0; |
| 214 | #if ENABLE_FEATURE_TEST_64 |
| 215 | r = strtoll(s, &p, 10); |
| 216 | #else |
| 217 | r = strtol(s, &p, 10); |
| 218 | #endif |
| 219 | |
| 220 | if (errno != 0) |
| 221 | syntax(s, "out of range"); |
| 222 | |
| 223 | if (*(skip_whitespace(p))) |
| 224 | syntax(s, "bad number"); |
| 225 | |
| 226 | return r; |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 227 | } |
| 228 | |
Bernhard Reutner-Fischer | a702457 | 2007-11-16 12:20:30 +0000 | [diff] [blame] | 229 | /* UNUSED |
| 230 | static int newerf(const char *f1, const char *f2) |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 231 | { |
Bernhard Reutner-Fischer | a702457 | 2007-11-16 12:20:30 +0000 | [diff] [blame] | 232 | struct stat b1, b2; |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 233 | |
Bernhard Reutner-Fischer | a702457 | 2007-11-16 12:20:30 +0000 | [diff] [blame] | 234 | return (stat(f1, &b1) == 0 && |
| 235 | stat(f2, &b2) == 0 && b1.st_mtime > b2.st_mtime); |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 236 | } |
| 237 | |
Bernhard Reutner-Fischer | a702457 | 2007-11-16 12:20:30 +0000 | [diff] [blame] | 238 | static int olderf(const char *f1, const char *f2) |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 239 | { |
Bernhard Reutner-Fischer | a702457 | 2007-11-16 12:20:30 +0000 | [diff] [blame] | 240 | struct stat b1, b2; |
| 241 | |
| 242 | return (stat(f1, &b1) == 0 && |
| 243 | stat(f2, &b2) == 0 && b1.st_mtime < b2.st_mtime); |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 244 | } |
| 245 | |
Bernhard Reutner-Fischer | a702457 | 2007-11-16 12:20:30 +0000 | [diff] [blame] | 246 | static int equalf(const char *f1, const char *f2) |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 247 | { |
Bernhard Reutner-Fischer | a702457 | 2007-11-16 12:20:30 +0000 | [diff] [blame] | 248 | struct stat b1, b2; |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 249 | |
Bernhard Reutner-Fischer | a702457 | 2007-11-16 12:20:30 +0000 | [diff] [blame] | 250 | return (stat(f1, &b1) == 0 && |
| 251 | stat(f2, &b2) == 0 && |
| 252 | b1.st_dev == b2.st_dev && b1.st_ino == b2.st_ino); |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 253 | } |
Bernhard Reutner-Fischer | a702457 | 2007-11-16 12:20:30 +0000 | [diff] [blame] | 254 | */ |
| 255 | |
| 256 | |
| 257 | static enum token t_lex(char *s) |
| 258 | { |
| 259 | const struct t_op *op; |
| 260 | |
| 261 | t_wp_op = NULL; |
| 262 | if (s == NULL) { |
| 263 | return EOI; |
| 264 | } |
| 265 | |
| 266 | op = ops; |
| 267 | do { |
| 268 | if (strcmp(s, op->op_text) == 0) { |
| 269 | t_wp_op = op; |
| 270 | return op->op_num; |
| 271 | } |
| 272 | op++; |
| 273 | } while (op < ops + ARRAY_SIZE(ops)); |
| 274 | |
| 275 | return OPERAND; |
| 276 | } |
| 277 | |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 278 | |
Mike Frysinger | 4e5936e | 2005-04-16 04:30:38 +0000 | [diff] [blame] | 279 | static int binop(void) |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 280 | { |
| 281 | const char *opnd1, *opnd2; |
| 282 | struct t_op const *op; |
Denis Vlasenko | fe3e177 | 2007-05-27 03:39:50 +0000 | [diff] [blame] | 283 | arith_t val1, val2; |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 284 | |
| 285 | opnd1 = *t_wp; |
| 286 | (void) t_lex(*++t_wp); |
| 287 | op = t_wp_op; |
| 288 | |
Denis Vlasenko | dcf4de2 | 2007-05-01 20:07:29 +0000 | [diff] [blame] | 289 | opnd2 = *++t_wp; |
| 290 | if (opnd2 == NULL) |
Paul Fox | 6ab0378 | 2006-06-08 21:37:26 +0000 | [diff] [blame] | 291 | syntax(op->op_text, "argument expected"); |
Glenn L McGrath | acfc0d8 | 2002-08-23 06:05:11 +0000 | [diff] [blame] | 292 | |
Denis Vlasenko | dcf4de2 | 2007-05-01 20:07:29 +0000 | [diff] [blame] | 293 | if (is_int_op(op->op_num)) { |
Bernhard Reutner-Fischer | cc89026 | 2007-03-30 18:23:36 +0000 | [diff] [blame] | 294 | val1 = getn(opnd1); |
| 295 | val2 = getn(opnd2); |
| 296 | if (op->op_num == INTEQ) |
| 297 | return val1 == val2; |
| 298 | if (op->op_num == INTNE) |
| 299 | return val1 != val2; |
| 300 | if (op->op_num == INTGE) |
| 301 | return val1 >= val2; |
| 302 | if (op->op_num == INTGT) |
| 303 | return val1 > val2; |
| 304 | if (op->op_num == INTLE) |
| 305 | return val1 <= val2; |
| 306 | if (op->op_num == INTLT) |
| 307 | return val1 < val2; |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 308 | } |
Denis Vlasenko | dcf4de2 | 2007-05-01 20:07:29 +0000 | [diff] [blame] | 309 | if (is_str_op(op->op_num)) { |
Bernhard Reutner-Fischer | cc89026 | 2007-03-30 18:23:36 +0000 | [diff] [blame] | 310 | val1 = strcmp(opnd1, opnd2); |
| 311 | if (op->op_num == STREQ) |
| 312 | return val1 == 0; |
| 313 | if (op->op_num == STRNE) |
| 314 | return val1 != 0; |
| 315 | if (op->op_num == STRLT) |
| 316 | return val1 < 0; |
| 317 | if (op->op_num == STRGT) |
| 318 | return val1 > 0; |
| 319 | } |
| 320 | /* We are sure that these three are by now the only binops we didn't check |
| 321 | * yet, so we do not check if the class is correct: |
| 322 | */ |
Denis Vlasenko | dcf4de2 | 2007-05-01 20:07:29 +0000 | [diff] [blame] | 323 | /* if (is_file_op(op->op_num)) */ |
Bernhard Reutner-Fischer | cc89026 | 2007-03-30 18:23:36 +0000 | [diff] [blame] | 324 | { |
| 325 | struct stat b1, b2; |
| 326 | |
Denis Vlasenko | fe3e177 | 2007-05-27 03:39:50 +0000 | [diff] [blame] | 327 | if (stat(opnd1, &b1) || stat(opnd2, &b2)) |
| 328 | return 0; /* false, since at least one stat failed */ |
Bernhard Reutner-Fischer | cc89026 | 2007-03-30 18:23:36 +0000 | [diff] [blame] | 329 | if (op->op_num == FILNT) |
| 330 | return b1.st_mtime > b2.st_mtime; |
| 331 | if (op->op_num == FILOT) |
| 332 | return b1.st_mtime < b2.st_mtime; |
| 333 | if (op->op_num == FILEQ) |
| 334 | return b1.st_dev == b2.st_dev && b1.st_ino == b2.st_ino; |
| 335 | } |
| 336 | return 1; /* NOTREACHED */ |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 337 | } |
| 338 | |
Bernhard Reutner-Fischer | a702457 | 2007-11-16 12:20:30 +0000 | [diff] [blame] | 339 | |
| 340 | static void initialize_group_array(void) |
| 341 | { |
| 342 | ngroups = getgroups(0, NULL); |
| 343 | if (ngroups > 0) { |
| 344 | /* FIXME: ash tries so hard to not die on OOM, |
| 345 | * and we spoil it with just one xrealloc here */ |
| 346 | /* We realloc, because test_main can be entered repeatedly by shell. |
| 347 | * Testcase (ash): 'while true; do test -x some_file; done' |
| 348 | * and watch top. (some_file must have owner != you) */ |
| 349 | group_array = xrealloc(group_array, ngroups * sizeof(gid_t)); |
| 350 | getgroups(ngroups, group_array); |
| 351 | } |
| 352 | } |
| 353 | |
| 354 | |
| 355 | /* Return non-zero if GID is one that we have in our groups list. */ |
| 356 | //XXX: FIXME: duplicate of existing libbb function? |
| 357 | // see toplevel TODO file: |
| 358 | // possible code duplication ingroup() and is_a_group_member() |
| 359 | static int is_a_group_member(gid_t gid) |
| 360 | { |
| 361 | int i; |
| 362 | |
| 363 | /* Short-circuit if possible, maybe saving a call to getgroups(). */ |
| 364 | if (gid == getgid() || gid == getegid()) |
| 365 | return 1; |
| 366 | |
| 367 | if (ngroups == 0) |
| 368 | initialize_group_array(); |
| 369 | |
| 370 | /* Search through the list looking for GID. */ |
| 371 | for (i = 0; i < ngroups; i++) |
| 372 | if (gid == group_array[i]) |
| 373 | return 1; |
| 374 | |
| 375 | return 0; |
| 376 | } |
| 377 | |
| 378 | |
| 379 | /* Do the same thing access(2) does, but use the effective uid and gid, |
| 380 | and don't make the mistake of telling root that any file is |
| 381 | executable. */ |
| 382 | static int test_eaccess(char *path, int mode) |
| 383 | { |
| 384 | struct stat st; |
| 385 | unsigned int euid = geteuid(); |
| 386 | |
| 387 | if (stat(path, &st) < 0) |
| 388 | return -1; |
| 389 | |
| 390 | if (euid == 0) { |
| 391 | /* Root can read or write any file. */ |
| 392 | if (mode != X_OK) |
| 393 | return 0; |
| 394 | |
| 395 | /* Root can execute any file that has any one of the execute |
| 396 | bits set. */ |
| 397 | if (st.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH)) |
| 398 | return 0; |
| 399 | } |
| 400 | |
| 401 | if (st.st_uid == euid) /* owner */ |
| 402 | mode <<= 6; |
| 403 | else if (is_a_group_member(st.st_gid)) |
| 404 | mode <<= 3; |
| 405 | |
| 406 | if (st.st_mode & mode) |
| 407 | return 0; |
| 408 | |
| 409 | return -1; |
| 410 | } |
| 411 | |
| 412 | |
Glenn L McGrath | acfc0d8 | 2002-08-23 06:05:11 +0000 | [diff] [blame] | 413 | static int filstat(char *nm, enum token mode) |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 414 | { |
| 415 | struct stat s; |
Denis Vlasenko | 88308fe | 2007-06-25 10:35:11 +0000 | [diff] [blame] | 416 | int i = i; /* gcc 3.x thinks it can be used uninitialized */ |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 417 | |
| 418 | if (mode == FILSYM) { |
| 419 | #ifdef S_IFLNK |
| 420 | if (lstat(nm, &s) == 0) { |
| 421 | i = S_IFLNK; |
| 422 | goto filetype; |
| 423 | } |
| 424 | #endif |
| 425 | return 0; |
| 426 | } |
| 427 | |
Glenn L McGrath | acfc0d8 | 2002-08-23 06:05:11 +0000 | [diff] [blame] | 428 | if (stat(nm, &s) != 0) |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 429 | return 0; |
Bernhard Reutner-Fischer | cc89026 | 2007-03-30 18:23:36 +0000 | [diff] [blame] | 430 | if (mode == FILEXIST) |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 431 | return 1; |
Denis Vlasenko | dcf4de2 | 2007-05-01 20:07:29 +0000 | [diff] [blame] | 432 | if (is_file_access(mode)) { |
Bernhard Reutner-Fischer | cc89026 | 2007-03-30 18:23:36 +0000 | [diff] [blame] | 433 | if (mode == FILRD) |
| 434 | i = R_OK; |
| 435 | if (mode == FILWR) |
| 436 | i = W_OK; |
| 437 | if (mode == FILEX) |
| 438 | i = X_OK; |
| 439 | return test_eaccess(nm, i) == 0; |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 440 | } |
Denis Vlasenko | dcf4de2 | 2007-05-01 20:07:29 +0000 | [diff] [blame] | 441 | if (is_file_type(mode)) { |
Bernhard Reutner-Fischer | cc89026 | 2007-03-30 18:23:36 +0000 | [diff] [blame] | 442 | if (mode == FILREG) |
| 443 | i = S_IFREG; |
| 444 | if (mode == FILDIR) |
| 445 | i = S_IFDIR; |
| 446 | if (mode == FILCDEV) |
| 447 | i = S_IFCHR; |
| 448 | if (mode == FILBDEV) |
| 449 | i = S_IFBLK; |
| 450 | if (mode == FILFIFO) { |
| 451 | #ifdef S_IFIFO |
| 452 | i = S_IFIFO; |
| 453 | #else |
| 454 | return 0; |
| 455 | #endif |
| 456 | } |
| 457 | if (mode == FILSOCK) { |
| 458 | #ifdef S_IFSOCK |
| 459 | i = S_IFSOCK; |
| 460 | #else |
| 461 | return 0; |
| 462 | #endif |
| 463 | } |
Denis Vlasenko | dcf4de2 | 2007-05-01 20:07:29 +0000 | [diff] [blame] | 464 | filetype: |
Bernhard Reutner-Fischer | cc89026 | 2007-03-30 18:23:36 +0000 | [diff] [blame] | 465 | return ((s.st_mode & S_IFMT) == i); |
| 466 | } |
Denis Vlasenko | dcf4de2 | 2007-05-01 20:07:29 +0000 | [diff] [blame] | 467 | if (is_file_bit(mode)) { |
Bernhard Reutner-Fischer | cc89026 | 2007-03-30 18:23:36 +0000 | [diff] [blame] | 468 | if (mode == FILSUID) |
| 469 | i = S_ISUID; |
| 470 | if (mode == FILSGID) |
| 471 | i = S_ISGID; |
| 472 | if (mode == FILSTCK) |
| 473 | i = S_ISVTX; |
| 474 | return ((s.st_mode & i) != 0); |
| 475 | } |
Denis Vlasenko | dcf4de2 | 2007-05-01 20:07:29 +0000 | [diff] [blame] | 476 | if (mode == FILGZ) |
Bernhard Reutner-Fischer | cc89026 | 2007-03-30 18:23:36 +0000 | [diff] [blame] | 477 | return s.st_size > 0L; |
Denis Vlasenko | dcf4de2 | 2007-05-01 20:07:29 +0000 | [diff] [blame] | 478 | if (mode == FILUID) |
Bernhard Reutner-Fischer | cc89026 | 2007-03-30 18:23:36 +0000 | [diff] [blame] | 479 | return s.st_uid == geteuid(); |
Denis Vlasenko | dcf4de2 | 2007-05-01 20:07:29 +0000 | [diff] [blame] | 480 | if (mode == FILGID) |
Bernhard Reutner-Fischer | cc89026 | 2007-03-30 18:23:36 +0000 | [diff] [blame] | 481 | return s.st_gid == getegid(); |
Denis Vlasenko | dcf4de2 | 2007-05-01 20:07:29 +0000 | [diff] [blame] | 482 | return 1; /* NOTREACHED */ |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 483 | } |
| 484 | |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 485 | |
Bernhard Reutner-Fischer | a702457 | 2007-11-16 12:20:30 +0000 | [diff] [blame] | 486 | static arith_t nexpr(enum token n) |
| 487 | { |
| 488 | if (n == UNOT) |
| 489 | return !nexpr(t_lex(*++t_wp)); |
| 490 | return primary(n); |
| 491 | } |
| 492 | |
| 493 | |
| 494 | static arith_t aexpr(enum token n) |
| 495 | { |
| 496 | arith_t res; |
| 497 | |
| 498 | res = nexpr(n); |
| 499 | if (t_lex(*++t_wp) == BAND) |
| 500 | return aexpr(t_lex(*++t_wp)) && res; |
| 501 | t_wp--; |
| 502 | return res; |
| 503 | } |
| 504 | |
| 505 | |
| 506 | static arith_t oexpr(enum token n) |
| 507 | { |
| 508 | arith_t res; |
| 509 | |
| 510 | res = aexpr(n); |
| 511 | if (t_lex(*++t_wp) == BOR) { |
| 512 | return oexpr(t_lex(*++t_wp)) || res; |
| 513 | } |
| 514 | t_wp--; |
| 515 | return res; |
| 516 | } |
| 517 | |
| 518 | |
| 519 | |
| 520 | static arith_t primary(enum token n) |
| 521 | { |
| 522 | arith_t res; |
| 523 | |
| 524 | if (n == EOI) { |
| 525 | syntax(NULL, "argument expected"); |
| 526 | } |
| 527 | if (n == LPAREN) { |
| 528 | res = oexpr(t_lex(*++t_wp)); |
| 529 | if (t_lex(*++t_wp) != RPAREN) |
| 530 | syntax(NULL, "closing paren expected"); |
| 531 | return res; |
| 532 | } |
| 533 | if (t_wp_op && t_wp_op->op_type == UNOP) { |
| 534 | /* unary expression */ |
| 535 | if (*++t_wp == NULL) |
| 536 | syntax(t_wp_op->op_text, "argument expected"); |
| 537 | if (n == STREZ) |
| 538 | return t_wp[0][0] == '\0'; |
| 539 | if (n == STRNZ) |
| 540 | return t_wp[0][0] != '\0'; |
| 541 | if (n == FILTT) |
| 542 | return isatty(getn(*t_wp)); |
| 543 | return filstat(*t_wp, n); |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 544 | } |
Denis Vlasenko | dcf4de2 | 2007-05-01 20:07:29 +0000 | [diff] [blame] | 545 | |
Bernhard Reutner-Fischer | a702457 | 2007-11-16 12:20:30 +0000 | [diff] [blame] | 546 | t_lex(t_wp[1]); |
| 547 | if (t_wp_op && t_wp_op->op_type == BINOP) { |
| 548 | return binop(); |
| 549 | } |
| 550 | |
| 551 | return t_wp[0][0] != '\0'; |
| 552 | } |
| 553 | |
| 554 | |
| 555 | int test_main(int argc, char **argv) |
| 556 | { |
| 557 | int res; |
| 558 | const char *arg0; |
Denis Vlasenko | 1e2a7e4 | 2008-02-09 05:48:42 +0000 | [diff] [blame] | 559 | bool negate = 0; |
Bernhard Reutner-Fischer | a702457 | 2007-11-16 12:20:30 +0000 | [diff] [blame] | 560 | |
| 561 | arg0 = bb_basename(argv[0]); |
| 562 | if (arg0[0] == '[') { |
| 563 | --argc; |
| 564 | if (!arg0[1]) { /* "[" ? */ |
| 565 | if (NOT_LONE_CHAR(argv[argc], ']')) { |
| 566 | bb_error_msg("missing ]"); |
| 567 | return 2; |
| 568 | } |
| 569 | } else { /* assuming "[[" */ |
| 570 | if (strcmp(argv[argc], "]]") != 0) { |
| 571 | bb_error_msg("missing ]]"); |
| 572 | return 2; |
| 573 | } |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 574 | } |
Bernhard Reutner-Fischer | a702457 | 2007-11-16 12:20:30 +0000 | [diff] [blame] | 575 | argv[argc] = NULL; |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 576 | } |
| 577 | |
Denis Vlasenko | 6672c8e | 2007-11-30 07:29:05 +0000 | [diff] [blame] | 578 | /* We must do DEINIT_S() prior to returning */ |
| 579 | INIT_S(); |
| 580 | |
Bernhard Reutner-Fischer | a702457 | 2007-11-16 12:20:30 +0000 | [diff] [blame] | 581 | res = setjmp(leaving); |
Denis Vlasenko | 1e2a7e4 | 2008-02-09 05:48:42 +0000 | [diff] [blame] | 582 | if (res) |
Denis Vlasenko | 6672c8e | 2007-11-30 07:29:05 +0000 | [diff] [blame] | 583 | goto ret; |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 584 | |
Bernhard Reutner-Fischer | a702457 | 2007-11-16 12:20:30 +0000 | [diff] [blame] | 585 | /* resetting ngroups is probably unnecessary. it will |
| 586 | * force a new call to getgroups(), which prevents using |
| 587 | * group data fetched during a previous call. but the |
| 588 | * only way the group data could be stale is if there's |
| 589 | * been an intervening call to setgroups(), and this |
| 590 | * isn't likely in the case of a shell. paranoia |
| 591 | * prevails... |
| 592 | */ |
| 593 | ngroups = 0; |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 594 | |
Denis Vlasenko | 1e2a7e4 | 2008-02-09 05:48:42 +0000 | [diff] [blame] | 595 | //argc--; |
| 596 | argv++; |
| 597 | |
Bernhard Reutner-Fischer | a702457 | 2007-11-16 12:20:30 +0000 | [diff] [blame] | 598 | /* Implement special cases from POSIX.2, section 4.62.4 */ |
Denis Vlasenko | 1e2a7e4 | 2008-02-09 05:48:42 +0000 | [diff] [blame] | 599 | if (!argv[0]) { /* "test" */ |
Denis Vlasenko | 6672c8e | 2007-11-30 07:29:05 +0000 | [diff] [blame] | 600 | res = 1; |
| 601 | goto ret; |
| 602 | } |
Denis Vlasenko | 1e2a7e4 | 2008-02-09 05:48:42 +0000 | [diff] [blame] | 603 | if (LONE_CHAR(argv[0], '!') && argv[1]) { |
| 604 | negate = 1; |
| 605 | //argc--; |
| 606 | argv++; |
| 607 | } |
| 608 | if (!argv[1]) { /* "test [!] arg" */ |
| 609 | res = (*argv[0] == '\0'); |
Denis Vlasenko | 6672c8e | 2007-11-30 07:29:05 +0000 | [diff] [blame] | 610 | goto ret; |
| 611 | } |
Denis Vlasenko | 1e2a7e4 | 2008-02-09 05:48:42 +0000 | [diff] [blame] | 612 | if (argv[2] && !argv[3]) { |
| 613 | t_lex(argv[1]); |
Bernhard Reutner-Fischer | a702457 | 2007-11-16 12:20:30 +0000 | [diff] [blame] | 614 | if (t_wp_op && t_wp_op->op_type == BINOP) { |
Denis Vlasenko | 1e2a7e4 | 2008-02-09 05:48:42 +0000 | [diff] [blame] | 615 | /* "test [!] arg1 <binary_op> arg2" */ |
| 616 | t_wp = &argv[0]; |
| 617 | res = (binop() == 0); |
Denis Vlasenko | 6672c8e | 2007-11-30 07:29:05 +0000 | [diff] [blame] | 618 | goto ret; |
Bernhard Reutner-Fischer | a702457 | 2007-11-16 12:20:30 +0000 | [diff] [blame] | 619 | } |
| 620 | } |
Denis Vlasenko | 1e2a7e4 | 2008-02-09 05:48:42 +0000 | [diff] [blame] | 621 | |
| 622 | /* Some complex expression. Undo '!' removal */ |
| 623 | if (negate) { |
| 624 | negate = 0; |
| 625 | //argc++; |
| 626 | argv--; |
| 627 | } |
| 628 | t_wp = &argv[0]; |
Bernhard Reutner-Fischer | a702457 | 2007-11-16 12:20:30 +0000 | [diff] [blame] | 629 | res = !oexpr(t_lex(*t_wp)); |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 630 | |
Bernhard Reutner-Fischer | a702457 | 2007-11-16 12:20:30 +0000 | [diff] [blame] | 631 | if (*t_wp != NULL && *++t_wp != NULL) { |
| 632 | bb_error_msg("%s: unknown operand", *t_wp); |
Denis Vlasenko | 6672c8e | 2007-11-30 07:29:05 +0000 | [diff] [blame] | 633 | res = 2; |
Bernhard Reutner-Fischer | a702457 | 2007-11-16 12:20:30 +0000 | [diff] [blame] | 634 | } |
Denis Vlasenko | 6672c8e | 2007-11-30 07:29:05 +0000 | [diff] [blame] | 635 | ret: |
| 636 | DEINIT_S(); |
Denis Vlasenko | 1e2a7e4 | 2008-02-09 05:48:42 +0000 | [diff] [blame] | 637 | return negate ? !res : res; |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 638 | } |