test.c 13 KB

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