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 | |
Paul Fox | 6ab0378 | 2006-06-08 21:37:26 +0000 | [diff] [blame] | 178 | if (strcmp(argv[0], "[") == 0) { |
| 179 | if (strcmp(argv[--argc], "]")) { |
| 180 | bb_error_msg("missing ]"); |
| 181 | return 2; |
| 182 | } |
| 183 | argv[argc] = NULL; |
| 184 | } else if (strcmp(argv[0], "[[") == 0) { |
| 185 | if (strcmp(argv[--argc], "]]")) { |
| 186 | bb_error_msg("missing ]]"); |
| 187 | return 2; |
| 188 | } |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 189 | argv[argc] = NULL; |
| 190 | } |
Paul Fox | 6ab0378 | 2006-06-08 21:37:26 +0000 | [diff] [blame] | 191 | |
| 192 | res = setjmp(leaving); |
| 193 | if (res) |
| 194 | return res; |
| 195 | |
| 196 | /* resetting ngroups is probably unnecessary. it will |
| 197 | * force a new call to getgroups(), which prevents using |
| 198 | * group data fetched during a previous call. but the |
| 199 | * only way the group data could be stale is if there's |
| 200 | * been an intervening call to setgroups(), and this |
| 201 | * isn't likely in the case of a shell. paranoia |
| 202 | * prevails... |
| 203 | */ |
| 204 | ngroups = 0; |
| 205 | |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 206 | /* Implement special cases from POSIX.2, section 4.62.4 */ |
| 207 | switch (argc) { |
| 208 | case 1: |
Paul Fox | 6ab0378 | 2006-06-08 21:37:26 +0000 | [diff] [blame] | 209 | return 1; |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 210 | case 2: |
Paul Fox | 6ab0378 | 2006-06-08 21:37:26 +0000 | [diff] [blame] | 211 | return *argv[1] == '\0'; |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 212 | case 3: |
| 213 | if (argv[1][0] == '!' && argv[1][1] == '\0') { |
Paul Fox | 6ab0378 | 2006-06-08 21:37:26 +0000 | [diff] [blame] | 214 | return *argv[2] != '\0'; |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 215 | } |
| 216 | break; |
| 217 | case 4: |
| 218 | if (argv[1][0] != '!' || argv[1][1] != '\0') { |
Glenn L McGrath | acfc0d8 | 2002-08-23 06:05:11 +0000 | [diff] [blame] | 219 | 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] | 220 | t_wp = &argv[1]; |
Paul Fox | 6ab0378 | 2006-06-08 21:37:26 +0000 | [diff] [blame] | 221 | return binop() == 0; |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 222 | } |
| 223 | } |
| 224 | break; |
| 225 | case 5: |
| 226 | if (argv[1][0] == '!' && argv[1][1] == '\0') { |
Glenn L McGrath | acfc0d8 | 2002-08-23 06:05:11 +0000 | [diff] [blame] | 227 | 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] | 228 | t_wp = &argv[2]; |
Paul Fox | 6ab0378 | 2006-06-08 21:37:26 +0000 | [diff] [blame] | 229 | return binop() != 0; |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 230 | } |
| 231 | } |
| 232 | break; |
| 233 | } |
| 234 | |
| 235 | t_wp = &argv[1]; |
| 236 | res = !oexpr(t_lex(*t_wp)); |
| 237 | |
Paul Fox | 6ab0378 | 2006-06-08 21:37:26 +0000 | [diff] [blame] | 238 | if (*t_wp != NULL && *++t_wp != NULL) { |
| 239 | bb_error_msg("%s: unknown operand", *t_wp); |
| 240 | return 2; |
| 241 | } |
| 242 | return res; |
| 243 | } |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 244 | |
Paul Fox | 6ab0378 | 2006-06-08 21:37:26 +0000 | [diff] [blame] | 245 | static void syntax(const char *op, const char *msg) |
| 246 | { |
| 247 | if (op && *op) { |
| 248 | bb_error_msg("%s: %s", op, msg); |
| 249 | } else { |
| 250 | bb_error_msg("%s", msg); |
| 251 | } |
| 252 | longjmp(leaving, 2); |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 253 | } |
| 254 | |
Glenn L McGrath | 73db8be | 2004-08-11 02:45:47 +0000 | [diff] [blame] | 255 | static arith_t oexpr(enum token n) |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 256 | { |
Glenn L McGrath | 73db8be | 2004-08-11 02:45:47 +0000 | [diff] [blame] | 257 | arith_t res; |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 258 | |
| 259 | res = aexpr(n); |
Glenn L McGrath | acfc0d8 | 2002-08-23 06:05:11 +0000 | [diff] [blame] | 260 | if (t_lex(*++t_wp) == BOR) { |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 261 | return oexpr(t_lex(*++t_wp)) || res; |
Glenn L McGrath | acfc0d8 | 2002-08-23 06:05:11 +0000 | [diff] [blame] | 262 | } |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 263 | t_wp--; |
| 264 | return res; |
| 265 | } |
| 266 | |
Glenn L McGrath | 73db8be | 2004-08-11 02:45:47 +0000 | [diff] [blame] | 267 | static arith_t aexpr(enum token n) |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 268 | { |
Glenn L McGrath | 73db8be | 2004-08-11 02:45:47 +0000 | [diff] [blame] | 269 | arith_t res; |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 270 | |
| 271 | res = nexpr(n); |
| 272 | if (t_lex(*++t_wp) == BAND) |
| 273 | return aexpr(t_lex(*++t_wp)) && res; |
| 274 | t_wp--; |
| 275 | return res; |
| 276 | } |
| 277 | |
Glenn L McGrath | 73db8be | 2004-08-11 02:45:47 +0000 | [diff] [blame] | 278 | static arith_t nexpr(enum token n) |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 279 | { |
| 280 | if (n == UNOT) |
| 281 | return !nexpr(t_lex(*++t_wp)); |
| 282 | return primary(n); |
| 283 | } |
| 284 | |
Glenn L McGrath | 73db8be | 2004-08-11 02:45:47 +0000 | [diff] [blame] | 285 | static arith_t primary(enum token n) |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 286 | { |
Glenn L McGrath | 73db8be | 2004-08-11 02:45:47 +0000 | [diff] [blame] | 287 | arith_t res; |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 288 | |
Glenn L McGrath | acfc0d8 | 2002-08-23 06:05:11 +0000 | [diff] [blame] | 289 | if (n == EOI) { |
Paul Fox | 6ab0378 | 2006-06-08 21:37:26 +0000 | [diff] [blame] | 290 | syntax(NULL, "argument expected"); |
Glenn L McGrath | acfc0d8 | 2002-08-23 06:05:11 +0000 | [diff] [blame] | 291 | } |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 292 | if (n == LPAREN) { |
| 293 | res = oexpr(t_lex(*++t_wp)); |
| 294 | if (t_lex(*++t_wp) != RPAREN) |
Paul Fox | 6ab0378 | 2006-06-08 21:37:26 +0000 | [diff] [blame] | 295 | syntax(NULL, "closing paren expected"); |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 296 | return res; |
| 297 | } |
| 298 | if (t_wp_op && t_wp_op->op_type == UNOP) { |
| 299 | /* unary expression */ |
| 300 | if (*++t_wp == NULL) |
Paul Fox | 6ab0378 | 2006-06-08 21:37:26 +0000 | [diff] [blame] | 301 | syntax(t_wp_op->op_text, "argument expected"); |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 302 | switch (n) { |
| 303 | case STREZ: |
| 304 | return strlen(*t_wp) == 0; |
| 305 | case STRNZ: |
| 306 | return strlen(*t_wp) != 0; |
| 307 | case FILTT: |
| 308 | return isatty(getn(*t_wp)); |
| 309 | default: |
| 310 | return filstat(*t_wp, n); |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | if (t_lex(t_wp[1]), t_wp_op && t_wp_op->op_type == BINOP) { |
| 315 | return binop(); |
Glenn L McGrath | acfc0d8 | 2002-08-23 06:05:11 +0000 | [diff] [blame] | 316 | } |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 317 | |
| 318 | return strlen(*t_wp) > 0; |
| 319 | } |
| 320 | |
Mike Frysinger | 4e5936e | 2005-04-16 04:30:38 +0000 | [diff] [blame] | 321 | static int binop(void) |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 322 | { |
| 323 | const char *opnd1, *opnd2; |
| 324 | struct t_op const *op; |
| 325 | |
| 326 | opnd1 = *t_wp; |
| 327 | (void) t_lex(*++t_wp); |
| 328 | op = t_wp_op; |
| 329 | |
Glenn L McGrath | acfc0d8 | 2002-08-23 06:05:11 +0000 | [diff] [blame] | 330 | if ((opnd2 = *++t_wp) == (char *) 0) |
Paul Fox | 6ab0378 | 2006-06-08 21:37:26 +0000 | [diff] [blame] | 331 | syntax(op->op_text, "argument expected"); |
Glenn L McGrath | acfc0d8 | 2002-08-23 06:05:11 +0000 | [diff] [blame] | 332 | |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 333 | switch (op->op_num) { |
| 334 | case STREQ: |
| 335 | return strcmp(opnd1, opnd2) == 0; |
| 336 | case STRNE: |
| 337 | return strcmp(opnd1, opnd2) != 0; |
| 338 | case STRLT: |
| 339 | return strcmp(opnd1, opnd2) < 0; |
| 340 | case STRGT: |
| 341 | return strcmp(opnd1, opnd2) > 0; |
| 342 | case INTEQ: |
| 343 | return getn(opnd1) == getn(opnd2); |
| 344 | case INTNE: |
| 345 | return getn(opnd1) != getn(opnd2); |
| 346 | case INTGE: |
| 347 | return getn(opnd1) >= getn(opnd2); |
| 348 | case INTGT: |
| 349 | return getn(opnd1) > getn(opnd2); |
| 350 | case INTLE: |
| 351 | return getn(opnd1) <= getn(opnd2); |
| 352 | case INTLT: |
| 353 | return getn(opnd1) < getn(opnd2); |
| 354 | case FILNT: |
Glenn L McGrath | acfc0d8 | 2002-08-23 06:05:11 +0000 | [diff] [blame] | 355 | return newerf(opnd1, opnd2); |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 356 | case FILOT: |
Glenn L McGrath | acfc0d8 | 2002-08-23 06:05:11 +0000 | [diff] [blame] | 357 | return olderf(opnd1, opnd2); |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 358 | case FILEQ: |
Glenn L McGrath | acfc0d8 | 2002-08-23 06:05:11 +0000 | [diff] [blame] | 359 | return equalf(opnd1, opnd2); |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 360 | } |
| 361 | /* NOTREACHED */ |
| 362 | return 1; |
| 363 | } |
| 364 | |
Glenn L McGrath | acfc0d8 | 2002-08-23 06:05:11 +0000 | [diff] [blame] | 365 | static int filstat(char *nm, enum token mode) |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 366 | { |
| 367 | struct stat s; |
Eric Andersen | fad04fd | 2000-07-14 06:49:52 +0000 | [diff] [blame] | 368 | unsigned int i; |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 369 | |
| 370 | if (mode == FILSYM) { |
| 371 | #ifdef S_IFLNK |
| 372 | if (lstat(nm, &s) == 0) { |
| 373 | i = S_IFLNK; |
| 374 | goto filetype; |
| 375 | } |
| 376 | #endif |
| 377 | return 0; |
| 378 | } |
| 379 | |
Glenn L McGrath | acfc0d8 | 2002-08-23 06:05:11 +0000 | [diff] [blame] | 380 | if (stat(nm, &s) != 0) |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 381 | return 0; |
| 382 | |
| 383 | switch (mode) { |
| 384 | case FILRD: |
| 385 | return test_eaccess(nm, R_OK) == 0; |
| 386 | case FILWR: |
| 387 | return test_eaccess(nm, W_OK) == 0; |
| 388 | case FILEX: |
| 389 | return test_eaccess(nm, X_OK) == 0; |
| 390 | case FILEXIST: |
| 391 | return 1; |
| 392 | case FILREG: |
| 393 | i = S_IFREG; |
| 394 | goto filetype; |
| 395 | case FILDIR: |
| 396 | i = S_IFDIR; |
| 397 | goto filetype; |
| 398 | case FILCDEV: |
| 399 | i = S_IFCHR; |
| 400 | goto filetype; |
| 401 | case FILBDEV: |
| 402 | i = S_IFBLK; |
| 403 | goto filetype; |
| 404 | case FILFIFO: |
| 405 | #ifdef S_IFIFO |
| 406 | i = S_IFIFO; |
| 407 | goto filetype; |
| 408 | #else |
| 409 | return 0; |
| 410 | #endif |
| 411 | case FILSOCK: |
| 412 | #ifdef S_IFSOCK |
| 413 | i = S_IFSOCK; |
| 414 | goto filetype; |
| 415 | #else |
| 416 | return 0; |
| 417 | #endif |
| 418 | case FILSUID: |
| 419 | i = S_ISUID; |
| 420 | goto filebit; |
| 421 | case FILSGID: |
| 422 | i = S_ISGID; |
| 423 | goto filebit; |
| 424 | case FILSTCK: |
| 425 | i = S_ISVTX; |
| 426 | goto filebit; |
| 427 | case FILGZ: |
| 428 | return s.st_size > 0L; |
| 429 | case FILUID: |
| 430 | return s.st_uid == geteuid(); |
| 431 | case FILGID: |
| 432 | return s.st_gid == getegid(); |
| 433 | default: |
| 434 | return 1; |
| 435 | } |
| 436 | |
Glenn L McGrath | acfc0d8 | 2002-08-23 06:05:11 +0000 | [diff] [blame] | 437 | filetype: |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 438 | return ((s.st_mode & S_IFMT) == i); |
| 439 | |
Glenn L McGrath | acfc0d8 | 2002-08-23 06:05:11 +0000 | [diff] [blame] | 440 | filebit: |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 441 | return ((s.st_mode & i) != 0); |
| 442 | } |
| 443 | |
Glenn L McGrath | acfc0d8 | 2002-08-23 06:05:11 +0000 | [diff] [blame] | 444 | static enum token t_lex(char *s) |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 445 | { |
| 446 | struct t_op const *op = ops; |
| 447 | |
| 448 | if (s == 0) { |
Glenn L McGrath | acfc0d8 | 2002-08-23 06:05:11 +0000 | [diff] [blame] | 449 | t_wp_op = (struct t_op *) 0; |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 450 | return EOI; |
| 451 | } |
| 452 | while (op->op_text) { |
| 453 | if (strcmp(s, op->op_text) == 0) { |
| 454 | t_wp_op = op; |
| 455 | return op->op_num; |
| 456 | } |
| 457 | op++; |
| 458 | } |
Glenn L McGrath | acfc0d8 | 2002-08-23 06:05:11 +0000 | [diff] [blame] | 459 | t_wp_op = (struct t_op *) 0; |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 460 | return OPERAND; |
| 461 | } |
| 462 | |
| 463 | /* atoi with error detection */ |
Glenn L McGrath | 73db8be | 2004-08-11 02:45:47 +0000 | [diff] [blame] | 464 | static arith_t getn(const char *s) |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 465 | { |
| 466 | char *p; |
Glenn L McGrath | 73db8be | 2004-08-11 02:45:47 +0000 | [diff] [blame] | 467 | #ifdef CONFIG_FEATURE_TEST_64 |
| 468 | long long r; |
| 469 | #else |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 470 | long r; |
Glenn L McGrath | 73db8be | 2004-08-11 02:45:47 +0000 | [diff] [blame] | 471 | #endif |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 472 | |
| 473 | errno = 0; |
Glenn L McGrath | 73db8be | 2004-08-11 02:45:47 +0000 | [diff] [blame] | 474 | #ifdef CONFIG_FEATURE_TEST_64 |
| 475 | r = strtoll(s, &p, 10); |
| 476 | #else |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 477 | r = strtol(s, &p, 10); |
Glenn L McGrath | 73db8be | 2004-08-11 02:45:47 +0000 | [diff] [blame] | 478 | #endif |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 479 | |
| 480 | if (errno != 0) |
Paul Fox | 6ab0378 | 2006-06-08 21:37:26 +0000 | [diff] [blame] | 481 | syntax(s, "out of range"); |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 482 | |
Rob Landley | ea224be | 2006-06-18 20:20:07 +0000 | [diff] [blame] | 483 | if (*(skip_whitespace(p))) |
Paul Fox | 6ab0378 | 2006-06-08 21:37:26 +0000 | [diff] [blame] | 484 | syntax(s, "bad number"); |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 485 | |
Glenn L McGrath | 73db8be | 2004-08-11 02:45:47 +0000 | [diff] [blame] | 486 | return r; |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 487 | } |
| 488 | |
Glenn L McGrath | acfc0d8 | 2002-08-23 06:05:11 +0000 | [diff] [blame] | 489 | static int newerf(const char *f1, const char *f2) |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 490 | { |
| 491 | struct stat b1, b2; |
| 492 | |
Glenn L McGrath | acfc0d8 | 2002-08-23 06:05:11 +0000 | [diff] [blame] | 493 | return (stat(f1, &b1) == 0 && |
| 494 | stat(f2, &b2) == 0 && b1.st_mtime > b2.st_mtime); |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 495 | } |
| 496 | |
Glenn L McGrath | acfc0d8 | 2002-08-23 06:05:11 +0000 | [diff] [blame] | 497 | static int olderf(const char *f1, const char *f2) |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 498 | { |
| 499 | struct stat b1, b2; |
| 500 | |
Glenn L McGrath | acfc0d8 | 2002-08-23 06:05:11 +0000 | [diff] [blame] | 501 | return (stat(f1, &b1) == 0 && |
| 502 | stat(f2, &b2) == 0 && b1.st_mtime < b2.st_mtime); |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 503 | } |
| 504 | |
Glenn L McGrath | acfc0d8 | 2002-08-23 06:05:11 +0000 | [diff] [blame] | 505 | static int equalf(const char *f1, const char *f2) |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 506 | { |
| 507 | struct stat b1, b2; |
| 508 | |
Glenn L McGrath | acfc0d8 | 2002-08-23 06:05:11 +0000 | [diff] [blame] | 509 | return (stat(f1, &b1) == 0 && |
| 510 | stat(f2, &b2) == 0 && |
| 511 | b1.st_dev == b2.st_dev && b1.st_ino == b2.st_ino); |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 512 | } |
| 513 | |
| 514 | /* Do the same thing access(2) does, but use the effective uid and gid, |
| 515 | and don't make the mistake of telling root that any file is |
| 516 | executable. */ |
Glenn L McGrath | acfc0d8 | 2002-08-23 06:05:11 +0000 | [diff] [blame] | 517 | static int test_eaccess(char *path, int mode) |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 518 | { |
| 519 | struct stat st; |
Eric Andersen | fad04fd | 2000-07-14 06:49:52 +0000 | [diff] [blame] | 520 | unsigned int euid = geteuid(); |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 521 | |
Glenn L McGrath | acfc0d8 | 2002-08-23 06:05:11 +0000 | [diff] [blame] | 522 | if (stat(path, &st) < 0) |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame^] | 523 | return -1; |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 524 | |
| 525 | if (euid == 0) { |
| 526 | /* Root can read or write any file. */ |
| 527 | if (mode != X_OK) |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame^] | 528 | return 0; |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 529 | |
| 530 | /* Root can execute any file that has any one of the execute |
| 531 | bits set. */ |
| 532 | if (st.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH)) |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame^] | 533 | return 0; |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 534 | } |
| 535 | |
Paul Fox | 6ab0378 | 2006-06-08 21:37:26 +0000 | [diff] [blame] | 536 | if (st.st_uid == euid) /* owner */ |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 537 | mode <<= 6; |
Glenn L McGrath | acfc0d8 | 2002-08-23 06:05:11 +0000 | [diff] [blame] | 538 | else if (is_a_group_member(st.st_gid)) |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 539 | mode <<= 3; |
| 540 | |
| 541 | if (st.st_mode & mode) |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame^] | 542 | return 0; |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 543 | |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame^] | 544 | return -1; |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 545 | } |
| 546 | |
Mike Frysinger | 4e5936e | 2005-04-16 04:30:38 +0000 | [diff] [blame] | 547 | static void initialize_group_array(void) |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 548 | { |
| 549 | ngroups = getgroups(0, NULL); |
Rob Landley | ecb2957 | 2006-08-22 23:40:28 +0000 | [diff] [blame] | 550 | if (ngroups > 0) { |
| 551 | group_array = xmalloc(ngroups * sizeof(gid_t)); |
| 552 | getgroups(ngroups, group_array); |
| 553 | } |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 554 | } |
| 555 | |
| 556 | /* 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] | 557 | static int is_a_group_member(gid_t gid) |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 558 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 559 | int i; |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 560 | |
| 561 | /* Short-circuit if possible, maybe saving a call to getgroups(). */ |
| 562 | if (gid == getgid() || gid == getegid()) |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame^] | 563 | return 1; |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 564 | |
| 565 | if (ngroups == 0) |
Glenn L McGrath | acfc0d8 | 2002-08-23 06:05:11 +0000 | [diff] [blame] | 566 | initialize_group_array(); |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 567 | |
| 568 | /* Search through the list looking for GID. */ |
| 569 | for (i = 0; i < ngroups; i++) |
| 570 | if (gid == group_array[i]) |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame^] | 571 | return 1; |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 572 | |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame^] | 573 | return 0; |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 574 | } |
Paul Fox | 6ab0378 | 2006-06-08 21:37:26 +0000 | [diff] [blame] | 575 | |
| 576 | |
| 577 | /* applet entry point */ |
| 578 | |
| 579 | int test_main(int argc, char **argv) |
| 580 | { |
| 581 | exit(bb_test(argc, argv)); |
| 582 | } |
| 583 | |