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