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. |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 15 | * |
Paul Fox | 6ab0378 | 2006-06-08 21:37:26 +0000 | [diff] [blame] | 16 | * 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] | 17 | * |
| 18 | * Original copyright notice states: |
Paul Fox | 6ab0378 | 2006-06-08 21:37:26 +0000 | [diff] [blame] | 19 | * "This program is in the Public Domain." |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 20 | */ |
| 21 | |
Rob Landley | ea224be | 2006-06-18 20:20:07 +0000 | [diff] [blame] | 22 | #include "busybox.h" |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 23 | #include <unistd.h> |
| 24 | #include <ctype.h> |
| 25 | #include <errno.h> |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 26 | #include <string.h> |
Paul Fox | 6ab0378 | 2006-06-08 21:37:26 +0000 | [diff] [blame] | 27 | #include <setjmp.h> |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 28 | |
| 29 | /* test(1) accepts the following grammar: |
| 30 | oexpr ::= aexpr | aexpr "-o" oexpr ; |
| 31 | aexpr ::= nexpr | nexpr "-a" aexpr ; |
| 32 | nexpr ::= primary | "!" primary |
Paul Fox | 6ab0378 | 2006-06-08 21:37:26 +0000 | [diff] [blame] | 33 | primary ::= unary-operator operand |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 34 | | 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 Frysinger | 75ac42b | 2005-04-14 02:49:22 +0000 | [diff] [blame] | 41 | binary-operator ::= "="|"=="|"!="|"-eq"|"-ne"|"-ge"|"-gt"|"-le"|"-lt"| |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 42 | "-nt"|"-ot"|"-ef"; |
| 43 | operand ::= <any legal UNIX file name> |
| 44 | */ |
| 45 | |
| 46 | enum token { |
| 47 | EOI, |
| 48 | FILRD, |
| 49 | FILWR, |
| 50 | FILEX, |
| 51 | FILEXIST, |
| 52 | FILREG, |
| 53 | FILDIR, |
| 54 | FILCDEV, |
| 55 | FILBDEV, |
| 56 | FILFIFO, |
| 57 | FILSOCK, |
| 58 | FILSYM, |
| 59 | FILGZ, |
| 60 | FILTT, |
| 61 | FILSUID, |
| 62 | FILSGID, |
| 63 | FILSTCK, |
| 64 | FILNT, |
| 65 | FILOT, |
| 66 | FILEQ, |
| 67 | FILUID, |
| 68 | FILGID, |
| 69 | STREZ, |
| 70 | STRNZ, |
| 71 | STREQ, |
| 72 | STRNE, |
| 73 | STRLT, |
| 74 | STRGT, |
| 75 | INTEQ, |
| 76 | INTNE, |
| 77 | INTGE, |
| 78 | INTGT, |
| 79 | INTLE, |
| 80 | INTLT, |
| 81 | UNOT, |
| 82 | BAND, |
| 83 | BOR, |
| 84 | LPAREN, |
| 85 | RPAREN, |
| 86 | OPERAND |
| 87 | }; |
| 88 | |
| 89 | enum token_types { |
| 90 | UNOP, |
| 91 | BINOP, |
| 92 | BUNOP, |
| 93 | BBINOP, |
| 94 | PAREN |
| 95 | }; |
| 96 | |
Eric Andersen | 92d2324 | 2001-03-19 23:49:41 +0000 | [diff] [blame] | 97 | static const struct t_op { |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 98 | const char *op_text; |
| 99 | short op_num, op_type; |
Glenn L McGrath | acfc0d8 | 2002-08-23 06:05:11 +0000 | [diff] [blame] | 100 | } ops[] = { |
| 101 | { |
| 102 | "-r", FILRD, UNOP}, { |
| 103 | "-w", FILWR, UNOP}, { |
| 104 | "-x", FILEX, UNOP}, { |
| 105 | "-e", FILEXIST, UNOP}, { |
| 106 | "-f", FILREG, UNOP}, { |
| 107 | "-d", FILDIR, UNOP}, { |
| 108 | "-c", FILCDEV, UNOP}, { |
| 109 | "-b", FILBDEV, UNOP}, { |
| 110 | "-p", FILFIFO, UNOP}, { |
| 111 | "-u", FILSUID, UNOP}, { |
| 112 | "-g", FILSGID, UNOP}, { |
| 113 | "-k", FILSTCK, UNOP}, { |
| 114 | "-s", FILGZ, UNOP}, { |
| 115 | "-t", FILTT, UNOP}, { |
| 116 | "-z", STREZ, UNOP}, { |
| 117 | "-n", STRNZ, UNOP}, { |
Paul Fox | 6ab0378 | 2006-06-08 21:37:26 +0000 | [diff] [blame] | 118 | "-h", FILSYM, UNOP}, /* for backwards compat */ |
Glenn L McGrath | acfc0d8 | 2002-08-23 06:05:11 +0000 | [diff] [blame] | 119 | { |
| 120 | "-O", FILUID, UNOP}, { |
| 121 | "-G", FILGID, UNOP}, { |
| 122 | "-L", FILSYM, UNOP}, { |
| 123 | "-S", FILSOCK, UNOP}, { |
| 124 | "=", STREQ, BINOP}, { |
Mike Frysinger | 75ac42b | 2005-04-14 02:49:22 +0000 | [diff] [blame] | 125 | "==", STREQ, BINOP}, { |
Glenn L McGrath | acfc0d8 | 2002-08-23 06:05:11 +0000 | [diff] [blame] | 126 | "!=", STRNE, BINOP}, { |
| 127 | "<", STRLT, BINOP}, { |
| 128 | ">", STRGT, BINOP}, { |
| 129 | "-eq", INTEQ, BINOP}, { |
| 130 | "-ne", INTNE, BINOP}, { |
| 131 | "-ge", INTGE, BINOP}, { |
| 132 | "-gt", INTGT, BINOP}, { |
| 133 | "-le", INTLE, BINOP}, { |
| 134 | "-lt", INTLT, BINOP}, { |
| 135 | "-nt", FILNT, BINOP}, { |
| 136 | "-ot", FILOT, BINOP}, { |
| 137 | "-ef", FILEQ, BINOP}, { |
| 138 | "!", UNOT, BUNOP}, { |
| 139 | "-a", BAND, BBINOP}, { |
| 140 | "-o", BOR, BBINOP}, { |
| 141 | "(", LPAREN, PAREN}, { |
| 142 | ")", RPAREN, PAREN}, { |
| 143 | 0, 0, 0} |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 144 | }; |
| 145 | |
Glenn L McGrath | 73db8be | 2004-08-11 02:45:47 +0000 | [diff] [blame] | 146 | #ifdef CONFIG_FEATURE_TEST_64 |
| 147 | typedef int64_t arith_t; |
| 148 | #else |
| 149 | typedef int arith_t; |
| 150 | #endif |
| 151 | |
Matt Kraai | e6e8183 | 2000-12-30 07:46:23 +0000 | [diff] [blame] | 152 | static char **t_wp; |
| 153 | static struct t_op const *t_wp_op; |
Rob Landley | ecb2957 | 2006-08-22 23:40:28 +0000 | [diff] [blame] | 154 | static gid_t *group_array; |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 155 | static int ngroups; |
| 156 | |
Glenn L McGrath | acfc0d8 | 2002-08-23 06:05:11 +0000 | [diff] [blame] | 157 | static enum token t_lex(char *s); |
Glenn L McGrath | 73db8be | 2004-08-11 02:45:47 +0000 | [diff] [blame] | 158 | static arith_t oexpr(enum token n); |
| 159 | static arith_t aexpr(enum token n); |
| 160 | static arith_t nexpr(enum token n); |
Eric Andersen | 74400cc | 2001-10-18 04:11:39 +0000 | [diff] [blame] | 161 | static int binop(void); |
Glenn L McGrath | 73db8be | 2004-08-11 02:45:47 +0000 | [diff] [blame] | 162 | static arith_t primary(enum token n); |
Eric Andersen | 74400cc | 2001-10-18 04:11:39 +0000 | [diff] [blame] | 163 | static int filstat(char *nm, enum token mode); |
Glenn L McGrath | 73db8be | 2004-08-11 02:45:47 +0000 | [diff] [blame] | 164 | static arith_t getn(const char *s); |
Eric Andersen | 74400cc | 2001-10-18 04:11:39 +0000 | [diff] [blame] | 165 | static int newerf(const char *f1, const char *f2); |
| 166 | static int olderf(const char *f1, const char *f2); |
| 167 | static int equalf(const char *f1, const char *f2); |
Eric Andersen | 74400cc | 2001-10-18 04:11:39 +0000 | [diff] [blame] | 168 | static int test_eaccess(char *path, int mode); |
| 169 | static int is_a_group_member(gid_t gid); |
| 170 | static void initialize_group_array(void); |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 171 | |
Paul Fox | 6ab0378 | 2006-06-08 21:37:26 +0000 | [diff] [blame] | 172 | static jmp_buf leaving; |
| 173 | |
| 174 | int bb_test(int argc, char **argv) |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 175 | { |
Glenn L McGrath | acfc0d8 | 2002-08-23 06:05:11 +0000 | [diff] [blame] | 176 | int res; |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 177 | |
Denis Vlasenko | bf66fbc | 2006-12-21 13:23:14 +0000 | [diff] [blame^] | 178 | if (LONE_CHAR(argv[0], '[')) { |
| 179 | --argc; |
| 180 | if (NOT_LONE_CHAR(argv[argc], ']')) { |
Paul Fox | 6ab0378 | 2006-06-08 21:37:26 +0000 | [diff] [blame] | 181 | bb_error_msg("missing ]"); |
| 182 | return 2; |
| 183 | } |
| 184 | argv[argc] = NULL; |
| 185 | } else if (strcmp(argv[0], "[[") == 0) { |
Denis Vlasenko | bf66fbc | 2006-12-21 13:23:14 +0000 | [diff] [blame^] | 186 | --argc; |
| 187 | if (strcmp(argv[argc], "]]")) { |
Paul Fox | 6ab0378 | 2006-06-08 21:37:26 +0000 | [diff] [blame] | 188 | bb_error_msg("missing ]]"); |
| 189 | return 2; |
| 190 | } |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 191 | argv[argc] = NULL; |
| 192 | } |
Paul Fox | 6ab0378 | 2006-06-08 21:37:26 +0000 | [diff] [blame] | 193 | |
| 194 | res = setjmp(leaving); |
| 195 | if (res) |
| 196 | return res; |
| 197 | |
| 198 | /* resetting ngroups is probably unnecessary. it will |
| 199 | * force a new call to getgroups(), which prevents using |
| 200 | * group data fetched during a previous call. but the |
| 201 | * only way the group data could be stale is if there's |
| 202 | * been an intervening call to setgroups(), and this |
| 203 | * isn't likely in the case of a shell. paranoia |
| 204 | * prevails... |
| 205 | */ |
| 206 | ngroups = 0; |
| 207 | |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 208 | /* Implement special cases from POSIX.2, section 4.62.4 */ |
| 209 | switch (argc) { |
| 210 | case 1: |
Paul Fox | 6ab0378 | 2006-06-08 21:37:26 +0000 | [diff] [blame] | 211 | return 1; |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 212 | case 2: |
Paul Fox | 6ab0378 | 2006-06-08 21:37:26 +0000 | [diff] [blame] | 213 | return *argv[1] == '\0'; |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 214 | case 3: |
| 215 | if (argv[1][0] == '!' && argv[1][1] == '\0') { |
Paul Fox | 6ab0378 | 2006-06-08 21:37:26 +0000 | [diff] [blame] | 216 | return *argv[2] != '\0'; |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 217 | } |
| 218 | break; |
| 219 | case 4: |
| 220 | if (argv[1][0] != '!' || argv[1][1] != '\0') { |
Glenn L McGrath | acfc0d8 | 2002-08-23 06:05:11 +0000 | [diff] [blame] | 221 | if (t_lex(argv[2]), t_wp_op && t_wp_op->op_type == BINOP) { |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 222 | t_wp = &argv[1]; |
Paul Fox | 6ab0378 | 2006-06-08 21:37:26 +0000 | [diff] [blame] | 223 | return binop() == 0; |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 224 | } |
| 225 | } |
| 226 | break; |
| 227 | case 5: |
| 228 | if (argv[1][0] == '!' && argv[1][1] == '\0') { |
Glenn L McGrath | acfc0d8 | 2002-08-23 06:05:11 +0000 | [diff] [blame] | 229 | if (t_lex(argv[3]), t_wp_op && t_wp_op->op_type == BINOP) { |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 230 | t_wp = &argv[2]; |
Paul Fox | 6ab0378 | 2006-06-08 21:37:26 +0000 | [diff] [blame] | 231 | return binop() != 0; |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 232 | } |
| 233 | } |
| 234 | break; |
| 235 | } |
| 236 | |
| 237 | t_wp = &argv[1]; |
| 238 | res = !oexpr(t_lex(*t_wp)); |
| 239 | |
Paul Fox | 6ab0378 | 2006-06-08 21:37:26 +0000 | [diff] [blame] | 240 | if (*t_wp != NULL && *++t_wp != NULL) { |
| 241 | bb_error_msg("%s: unknown operand", *t_wp); |
| 242 | return 2; |
| 243 | } |
| 244 | return res; |
| 245 | } |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 246 | |
Paul Fox | 6ab0378 | 2006-06-08 21:37:26 +0000 | [diff] [blame] | 247 | static void syntax(const char *op, const char *msg) |
| 248 | { |
| 249 | if (op && *op) { |
| 250 | bb_error_msg("%s: %s", op, msg); |
| 251 | } else { |
| 252 | bb_error_msg("%s", msg); |
| 253 | } |
| 254 | longjmp(leaving, 2); |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 255 | } |
| 256 | |
Glenn L McGrath | 73db8be | 2004-08-11 02:45:47 +0000 | [diff] [blame] | 257 | static arith_t oexpr(enum token n) |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 258 | { |
Glenn L McGrath | 73db8be | 2004-08-11 02:45:47 +0000 | [diff] [blame] | 259 | arith_t res; |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 260 | |
| 261 | res = aexpr(n); |
Glenn L McGrath | acfc0d8 | 2002-08-23 06:05:11 +0000 | [diff] [blame] | 262 | if (t_lex(*++t_wp) == BOR) { |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 263 | return oexpr(t_lex(*++t_wp)) || res; |
Glenn L McGrath | acfc0d8 | 2002-08-23 06:05:11 +0000 | [diff] [blame] | 264 | } |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 265 | t_wp--; |
| 266 | return res; |
| 267 | } |
| 268 | |
Glenn L McGrath | 73db8be | 2004-08-11 02:45:47 +0000 | [diff] [blame] | 269 | static arith_t aexpr(enum token n) |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 270 | { |
Glenn L McGrath | 73db8be | 2004-08-11 02:45:47 +0000 | [diff] [blame] | 271 | arith_t res; |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 272 | |
| 273 | res = nexpr(n); |
| 274 | if (t_lex(*++t_wp) == BAND) |
| 275 | return aexpr(t_lex(*++t_wp)) && res; |
| 276 | t_wp--; |
| 277 | return res; |
| 278 | } |
| 279 | |
Glenn L McGrath | 73db8be | 2004-08-11 02:45:47 +0000 | [diff] [blame] | 280 | static arith_t nexpr(enum token n) |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 281 | { |
| 282 | if (n == UNOT) |
| 283 | return !nexpr(t_lex(*++t_wp)); |
| 284 | return primary(n); |
| 285 | } |
| 286 | |
Glenn L McGrath | 73db8be | 2004-08-11 02:45:47 +0000 | [diff] [blame] | 287 | static arith_t primary(enum token n) |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 288 | { |
Glenn L McGrath | 73db8be | 2004-08-11 02:45:47 +0000 | [diff] [blame] | 289 | arith_t res; |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 290 | |
Glenn L McGrath | acfc0d8 | 2002-08-23 06:05:11 +0000 | [diff] [blame] | 291 | if (n == EOI) { |
Paul Fox | 6ab0378 | 2006-06-08 21:37:26 +0000 | [diff] [blame] | 292 | syntax(NULL, "argument expected"); |
Glenn L McGrath | acfc0d8 | 2002-08-23 06:05:11 +0000 | [diff] [blame] | 293 | } |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 294 | if (n == LPAREN) { |
| 295 | res = oexpr(t_lex(*++t_wp)); |
| 296 | if (t_lex(*++t_wp) != RPAREN) |
Paul Fox | 6ab0378 | 2006-06-08 21:37:26 +0000 | [diff] [blame] | 297 | syntax(NULL, "closing paren expected"); |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 298 | return res; |
| 299 | } |
| 300 | if (t_wp_op && t_wp_op->op_type == UNOP) { |
| 301 | /* unary expression */ |
| 302 | if (*++t_wp == NULL) |
Paul Fox | 6ab0378 | 2006-06-08 21:37:26 +0000 | [diff] [blame] | 303 | syntax(t_wp_op->op_text, "argument expected"); |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 304 | switch (n) { |
| 305 | case STREZ: |
| 306 | return strlen(*t_wp) == 0; |
| 307 | case STRNZ: |
| 308 | return strlen(*t_wp) != 0; |
| 309 | case FILTT: |
| 310 | return isatty(getn(*t_wp)); |
| 311 | default: |
| 312 | return filstat(*t_wp, n); |
| 313 | } |
| 314 | } |
| 315 | |
| 316 | if (t_lex(t_wp[1]), t_wp_op && t_wp_op->op_type == BINOP) { |
| 317 | return binop(); |
Glenn L McGrath | acfc0d8 | 2002-08-23 06:05:11 +0000 | [diff] [blame] | 318 | } |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 319 | |
| 320 | return strlen(*t_wp) > 0; |
| 321 | } |
| 322 | |
Mike Frysinger | 4e5936e | 2005-04-16 04:30:38 +0000 | [diff] [blame] | 323 | static int binop(void) |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 324 | { |
| 325 | const char *opnd1, *opnd2; |
| 326 | struct t_op const *op; |
| 327 | |
| 328 | opnd1 = *t_wp; |
| 329 | (void) t_lex(*++t_wp); |
| 330 | op = t_wp_op; |
| 331 | |
Glenn L McGrath | acfc0d8 | 2002-08-23 06:05:11 +0000 | [diff] [blame] | 332 | if ((opnd2 = *++t_wp) == (char *) 0) |
Paul Fox | 6ab0378 | 2006-06-08 21:37:26 +0000 | [diff] [blame] | 333 | syntax(op->op_text, "argument expected"); |
Glenn L McGrath | acfc0d8 | 2002-08-23 06:05:11 +0000 | [diff] [blame] | 334 | |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 335 | switch (op->op_num) { |
| 336 | case STREQ: |
| 337 | return strcmp(opnd1, opnd2) == 0; |
| 338 | case STRNE: |
| 339 | return strcmp(opnd1, opnd2) != 0; |
| 340 | case STRLT: |
| 341 | return strcmp(opnd1, opnd2) < 0; |
| 342 | case STRGT: |
| 343 | return strcmp(opnd1, opnd2) > 0; |
| 344 | case INTEQ: |
| 345 | return getn(opnd1) == getn(opnd2); |
| 346 | case INTNE: |
| 347 | return getn(opnd1) != getn(opnd2); |
| 348 | case INTGE: |
| 349 | return getn(opnd1) >= getn(opnd2); |
| 350 | case INTGT: |
| 351 | return getn(opnd1) > getn(opnd2); |
| 352 | case INTLE: |
| 353 | return getn(opnd1) <= getn(opnd2); |
| 354 | case INTLT: |
| 355 | return getn(opnd1) < getn(opnd2); |
| 356 | case FILNT: |
Glenn L McGrath | acfc0d8 | 2002-08-23 06:05:11 +0000 | [diff] [blame] | 357 | return newerf(opnd1, opnd2); |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 358 | case FILOT: |
Glenn L McGrath | acfc0d8 | 2002-08-23 06:05:11 +0000 | [diff] [blame] | 359 | return olderf(opnd1, opnd2); |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 360 | case FILEQ: |
Glenn L McGrath | acfc0d8 | 2002-08-23 06:05:11 +0000 | [diff] [blame] | 361 | return equalf(opnd1, opnd2); |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 362 | } |
| 363 | /* NOTREACHED */ |
| 364 | return 1; |
| 365 | } |
| 366 | |
Glenn L McGrath | acfc0d8 | 2002-08-23 06:05:11 +0000 | [diff] [blame] | 367 | static int filstat(char *nm, enum token mode) |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 368 | { |
| 369 | struct stat s; |
Eric Andersen | fad04fd | 2000-07-14 06:49:52 +0000 | [diff] [blame] | 370 | unsigned int i; |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 371 | |
| 372 | if (mode == FILSYM) { |
| 373 | #ifdef S_IFLNK |
| 374 | if (lstat(nm, &s) == 0) { |
| 375 | i = S_IFLNK; |
| 376 | goto filetype; |
| 377 | } |
| 378 | #endif |
| 379 | return 0; |
| 380 | } |
| 381 | |
Glenn L McGrath | acfc0d8 | 2002-08-23 06:05:11 +0000 | [diff] [blame] | 382 | if (stat(nm, &s) != 0) |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 383 | return 0; |
| 384 | |
| 385 | switch (mode) { |
| 386 | case FILRD: |
| 387 | return test_eaccess(nm, R_OK) == 0; |
| 388 | case FILWR: |
| 389 | return test_eaccess(nm, W_OK) == 0; |
| 390 | case FILEX: |
| 391 | return test_eaccess(nm, X_OK) == 0; |
| 392 | case FILEXIST: |
| 393 | return 1; |
| 394 | case FILREG: |
| 395 | i = S_IFREG; |
| 396 | goto filetype; |
| 397 | case FILDIR: |
| 398 | i = S_IFDIR; |
| 399 | goto filetype; |
| 400 | case FILCDEV: |
| 401 | i = S_IFCHR; |
| 402 | goto filetype; |
| 403 | case FILBDEV: |
| 404 | i = S_IFBLK; |
| 405 | goto filetype; |
| 406 | case FILFIFO: |
| 407 | #ifdef S_IFIFO |
| 408 | i = S_IFIFO; |
| 409 | goto filetype; |
| 410 | #else |
| 411 | return 0; |
| 412 | #endif |
| 413 | case FILSOCK: |
| 414 | #ifdef S_IFSOCK |
| 415 | i = S_IFSOCK; |
| 416 | goto filetype; |
| 417 | #else |
| 418 | return 0; |
| 419 | #endif |
| 420 | case FILSUID: |
| 421 | i = S_ISUID; |
| 422 | goto filebit; |
| 423 | case FILSGID: |
| 424 | i = S_ISGID; |
| 425 | goto filebit; |
| 426 | case FILSTCK: |
| 427 | i = S_ISVTX; |
| 428 | goto filebit; |
| 429 | case FILGZ: |
| 430 | return s.st_size > 0L; |
| 431 | case FILUID: |
| 432 | return s.st_uid == geteuid(); |
| 433 | case FILGID: |
| 434 | return s.st_gid == getegid(); |
| 435 | default: |
| 436 | return 1; |
| 437 | } |
| 438 | |
Glenn L McGrath | acfc0d8 | 2002-08-23 06:05:11 +0000 | [diff] [blame] | 439 | filetype: |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 440 | return ((s.st_mode & S_IFMT) == i); |
| 441 | |
Glenn L McGrath | acfc0d8 | 2002-08-23 06:05:11 +0000 | [diff] [blame] | 442 | filebit: |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 443 | return ((s.st_mode & i) != 0); |
| 444 | } |
| 445 | |
Glenn L McGrath | acfc0d8 | 2002-08-23 06:05:11 +0000 | [diff] [blame] | 446 | static enum token t_lex(char *s) |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 447 | { |
| 448 | struct t_op const *op = ops; |
| 449 | |
| 450 | if (s == 0) { |
Glenn L McGrath | acfc0d8 | 2002-08-23 06:05:11 +0000 | [diff] [blame] | 451 | t_wp_op = (struct t_op *) 0; |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 452 | return EOI; |
| 453 | } |
| 454 | while (op->op_text) { |
| 455 | if (strcmp(s, op->op_text) == 0) { |
| 456 | t_wp_op = op; |
| 457 | return op->op_num; |
| 458 | } |
| 459 | op++; |
| 460 | } |
Glenn L McGrath | acfc0d8 | 2002-08-23 06:05:11 +0000 | [diff] [blame] | 461 | t_wp_op = (struct t_op *) 0; |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 462 | return OPERAND; |
| 463 | } |
| 464 | |
| 465 | /* atoi with error detection */ |
Glenn L McGrath | 73db8be | 2004-08-11 02:45:47 +0000 | [diff] [blame] | 466 | static arith_t getn(const char *s) |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 467 | { |
| 468 | char *p; |
Glenn L McGrath | 73db8be | 2004-08-11 02:45:47 +0000 | [diff] [blame] | 469 | #ifdef CONFIG_FEATURE_TEST_64 |
| 470 | long long r; |
| 471 | #else |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 472 | long r; |
Glenn L McGrath | 73db8be | 2004-08-11 02:45:47 +0000 | [diff] [blame] | 473 | #endif |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 474 | |
| 475 | errno = 0; |
Glenn L McGrath | 73db8be | 2004-08-11 02:45:47 +0000 | [diff] [blame] | 476 | #ifdef CONFIG_FEATURE_TEST_64 |
| 477 | r = strtoll(s, &p, 10); |
| 478 | #else |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 479 | r = strtol(s, &p, 10); |
Glenn L McGrath | 73db8be | 2004-08-11 02:45:47 +0000 | [diff] [blame] | 480 | #endif |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 481 | |
| 482 | if (errno != 0) |
Paul Fox | 6ab0378 | 2006-06-08 21:37:26 +0000 | [diff] [blame] | 483 | syntax(s, "out of range"); |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 484 | |
Rob Landley | ea224be | 2006-06-18 20:20:07 +0000 | [diff] [blame] | 485 | if (*(skip_whitespace(p))) |
Paul Fox | 6ab0378 | 2006-06-08 21:37:26 +0000 | [diff] [blame] | 486 | syntax(s, "bad number"); |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 487 | |
Glenn L McGrath | 73db8be | 2004-08-11 02:45:47 +0000 | [diff] [blame] | 488 | return r; |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 489 | } |
| 490 | |
Glenn L McGrath | acfc0d8 | 2002-08-23 06:05:11 +0000 | [diff] [blame] | 491 | static int newerf(const char *f1, const char *f2) |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 492 | { |
| 493 | struct stat b1, b2; |
| 494 | |
Glenn L McGrath | acfc0d8 | 2002-08-23 06:05:11 +0000 | [diff] [blame] | 495 | return (stat(f1, &b1) == 0 && |
| 496 | stat(f2, &b2) == 0 && b1.st_mtime > b2.st_mtime); |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 497 | } |
| 498 | |
Glenn L McGrath | acfc0d8 | 2002-08-23 06:05:11 +0000 | [diff] [blame] | 499 | static int olderf(const char *f1, const char *f2) |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 500 | { |
| 501 | struct stat b1, b2; |
| 502 | |
Glenn L McGrath | acfc0d8 | 2002-08-23 06:05:11 +0000 | [diff] [blame] | 503 | return (stat(f1, &b1) == 0 && |
| 504 | stat(f2, &b2) == 0 && b1.st_mtime < b2.st_mtime); |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 505 | } |
| 506 | |
Glenn L McGrath | acfc0d8 | 2002-08-23 06:05:11 +0000 | [diff] [blame] | 507 | static int equalf(const char *f1, const char *f2) |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 508 | { |
| 509 | struct stat b1, b2; |
| 510 | |
Glenn L McGrath | acfc0d8 | 2002-08-23 06:05:11 +0000 | [diff] [blame] | 511 | return (stat(f1, &b1) == 0 && |
| 512 | stat(f2, &b2) == 0 && |
| 513 | b1.st_dev == b2.st_dev && b1.st_ino == b2.st_ino); |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 514 | } |
| 515 | |
| 516 | /* Do the same thing access(2) does, but use the effective uid and gid, |
| 517 | and don't make the mistake of telling root that any file is |
| 518 | executable. */ |
Glenn L McGrath | acfc0d8 | 2002-08-23 06:05:11 +0000 | [diff] [blame] | 519 | static int test_eaccess(char *path, int mode) |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 520 | { |
| 521 | struct stat st; |
Eric Andersen | fad04fd | 2000-07-14 06:49:52 +0000 | [diff] [blame] | 522 | unsigned int euid = geteuid(); |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 523 | |
Glenn L McGrath | acfc0d8 | 2002-08-23 06:05:11 +0000 | [diff] [blame] | 524 | if (stat(path, &st) < 0) |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 525 | return -1; |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 526 | |
| 527 | if (euid == 0) { |
| 528 | /* Root can read or write any file. */ |
| 529 | if (mode != X_OK) |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 530 | return 0; |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 531 | |
| 532 | /* Root can execute any file that has any one of the execute |
| 533 | bits set. */ |
| 534 | if (st.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH)) |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 535 | return 0; |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 536 | } |
| 537 | |
Paul Fox | 6ab0378 | 2006-06-08 21:37:26 +0000 | [diff] [blame] | 538 | if (st.st_uid == euid) /* owner */ |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 539 | mode <<= 6; |
Glenn L McGrath | acfc0d8 | 2002-08-23 06:05:11 +0000 | [diff] [blame] | 540 | else if (is_a_group_member(st.st_gid)) |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 541 | mode <<= 3; |
| 542 | |
| 543 | if (st.st_mode & mode) |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 544 | return 0; |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 545 | |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 546 | return -1; |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 547 | } |
| 548 | |
Mike Frysinger | 4e5936e | 2005-04-16 04:30:38 +0000 | [diff] [blame] | 549 | static void initialize_group_array(void) |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 550 | { |
| 551 | ngroups = getgroups(0, NULL); |
Rob Landley | ecb2957 | 2006-08-22 23:40:28 +0000 | [diff] [blame] | 552 | if (ngroups > 0) { |
| 553 | group_array = xmalloc(ngroups * sizeof(gid_t)); |
| 554 | getgroups(ngroups, group_array); |
| 555 | } |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 556 | } |
| 557 | |
| 558 | /* Return non-zero if GID is one that we have in our groups list. */ |
Glenn L McGrath | acfc0d8 | 2002-08-23 06:05:11 +0000 | [diff] [blame] | 559 | static int is_a_group_member(gid_t gid) |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 560 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 561 | int i; |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 562 | |
| 563 | /* Short-circuit if possible, maybe saving a call to getgroups(). */ |
| 564 | if (gid == getgid() || gid == getegid()) |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 565 | return 1; |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 566 | |
| 567 | if (ngroups == 0) |
Glenn L McGrath | acfc0d8 | 2002-08-23 06:05:11 +0000 | [diff] [blame] | 568 | initialize_group_array(); |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 569 | |
| 570 | /* Search through the list looking for GID. */ |
| 571 | for (i = 0; i < ngroups; i++) |
| 572 | if (gid == group_array[i]) |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 573 | return 1; |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 574 | |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 575 | return 0; |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 576 | } |
Paul Fox | 6ab0378 | 2006-06-08 21:37:26 +0000 | [diff] [blame] | 577 | |
| 578 | |
| 579 | /* applet entry point */ |
| 580 | |
| 581 | int test_main(int argc, char **argv) |
| 582 | { |
Denis Vlasenko | bf66fbc | 2006-12-21 13:23:14 +0000 | [diff] [blame^] | 583 | return bb_test(argc, argv); |
Paul Fox | 6ab0378 | 2006-06-08 21:37:26 +0000 | [diff] [blame] | 584 | } |
| 585 | |