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