test.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638
  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 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. /* Make it reside in writable memory, yet make compiler understand
  157. * that it is not going to change. */
  158. static struct statics *const ptr_to_statics __attribute__ ((section (".data")));
  159. #define S (*ptr_to_statics)
  160. #define t_wp (S.t_wp )
  161. #define t_wp_op (S.t_wp_op )
  162. #define group_array (S.group_array )
  163. #define ngroups (S.ngroups )
  164. #define leaving (S.leaving )
  165. #define INIT_S() do { \
  166. (*(struct statics**)&ptr_to_statics) = xzalloc(sizeof(S)); \
  167. barrier(); \
  168. } while (0)
  169. #define DEINIT_S() do { \
  170. free(ptr_to_statics); \
  171. } while (0)
  172. static arith_t primary(enum token n);
  173. static void syntax(const char *op, const char *msg) ATTRIBUTE_NORETURN;
  174. static void syntax(const char *op, const char *msg)
  175. {
  176. if (op && *op) {
  177. bb_error_msg("%s: %s", op, msg);
  178. } else {
  179. bb_error_msg("%s: %s"+4, msg);
  180. }
  181. longjmp(leaving, 2);
  182. }
  183. /* atoi with error detection */
  184. //XXX: FIXME: duplicate of existing libbb function?
  185. static arith_t getn(const char *s)
  186. {
  187. char *p;
  188. #if ENABLE_FEATURE_TEST_64
  189. long long r;
  190. #else
  191. long r;
  192. #endif
  193. errno = 0;
  194. #if ENABLE_FEATURE_TEST_64
  195. r = strtoll(s, &p, 10);
  196. #else
  197. r = strtol(s, &p, 10);
  198. #endif
  199. if (errno != 0)
  200. syntax(s, "out of range");
  201. if (*(skip_whitespace(p)))
  202. syntax(s, "bad number");
  203. return r;
  204. }
  205. /* UNUSED
  206. static int newerf(const char *f1, const char *f2)
  207. {
  208. struct stat b1, b2;
  209. return (stat(f1, &b1) == 0 &&
  210. stat(f2, &b2) == 0 && b1.st_mtime > b2.st_mtime);
  211. }
  212. static int olderf(const char *f1, const char *f2)
  213. {
  214. struct stat b1, b2;
  215. return (stat(f1, &b1) == 0 &&
  216. stat(f2, &b2) == 0 && b1.st_mtime < b2.st_mtime);
  217. }
  218. static int equalf(const char *f1, const char *f2)
  219. {
  220. struct stat b1, b2;
  221. return (stat(f1, &b1) == 0 &&
  222. stat(f2, &b2) == 0 &&
  223. b1.st_dev == b2.st_dev && b1.st_ino == b2.st_ino);
  224. }
  225. */
  226. static enum token t_lex(char *s)
  227. {
  228. const struct t_op *op;
  229. t_wp_op = NULL;
  230. if (s == NULL) {
  231. return EOI;
  232. }
  233. op = ops;
  234. do {
  235. if (strcmp(s, op->op_text) == 0) {
  236. t_wp_op = op;
  237. return op->op_num;
  238. }
  239. op++;
  240. } while (op < ops + ARRAY_SIZE(ops));
  241. return OPERAND;
  242. }
  243. static int binop(void)
  244. {
  245. const char *opnd1, *opnd2;
  246. struct t_op const *op;
  247. arith_t val1, val2;
  248. opnd1 = *t_wp;
  249. (void) t_lex(*++t_wp);
  250. op = t_wp_op;
  251. opnd2 = *++t_wp;
  252. if (opnd2 == NULL)
  253. syntax(op->op_text, "argument expected");
  254. if (is_int_op(op->op_num)) {
  255. val1 = getn(opnd1);
  256. val2 = getn(opnd2);
  257. if (op->op_num == INTEQ)
  258. return val1 == val2;
  259. if (op->op_num == INTNE)
  260. return val1 != val2;
  261. if (op->op_num == INTGE)
  262. return val1 >= val2;
  263. if (op->op_num == INTGT)
  264. return val1 > val2;
  265. if (op->op_num == INTLE)
  266. return val1 <= val2;
  267. if (op->op_num == INTLT)
  268. return val1 < val2;
  269. }
  270. if (is_str_op(op->op_num)) {
  271. val1 = strcmp(opnd1, opnd2);
  272. if (op->op_num == STREQ)
  273. return val1 == 0;
  274. if (op->op_num == STRNE)
  275. return val1 != 0;
  276. if (op->op_num == STRLT)
  277. return val1 < 0;
  278. if (op->op_num == STRGT)
  279. return val1 > 0;
  280. }
  281. /* We are sure that these three are by now the only binops we didn't check
  282. * yet, so we do not check if the class is correct:
  283. */
  284. /* if (is_file_op(op->op_num)) */
  285. {
  286. struct stat b1, b2;
  287. if (stat(opnd1, &b1) || stat(opnd2, &b2))
  288. return 0; /* false, since at least one stat failed */
  289. if (op->op_num == FILNT)
  290. return b1.st_mtime > b2.st_mtime;
  291. if (op->op_num == FILOT)
  292. return b1.st_mtime < b2.st_mtime;
  293. if (op->op_num == FILEQ)
  294. return b1.st_dev == b2.st_dev && b1.st_ino == b2.st_ino;
  295. }
  296. return 1; /* NOTREACHED */
  297. }
  298. static void initialize_group_array(void)
  299. {
  300. ngroups = getgroups(0, NULL);
  301. if (ngroups > 0) {
  302. /* FIXME: ash tries so hard to not die on OOM,
  303. * and we spoil it with just one xrealloc here */
  304. /* We realloc, because test_main can be entered repeatedly by shell.
  305. * Testcase (ash): 'while true; do test -x some_file; done'
  306. * and watch top. (some_file must have owner != you) */
  307. group_array = xrealloc(group_array, ngroups * sizeof(gid_t));
  308. getgroups(ngroups, group_array);
  309. }
  310. }
  311. /* Return non-zero if GID is one that we have in our groups list. */
  312. //XXX: FIXME: duplicate of existing libbb function?
  313. // see toplevel TODO file:
  314. // possible code duplication ingroup() and is_a_group_member()
  315. static int is_a_group_member(gid_t gid)
  316. {
  317. int i;
  318. /* Short-circuit if possible, maybe saving a call to getgroups(). */
  319. if (gid == getgid() || gid == getegid())
  320. return 1;
  321. if (ngroups == 0)
  322. initialize_group_array();
  323. /* Search through the list looking for GID. */
  324. for (i = 0; i < ngroups; i++)
  325. if (gid == group_array[i])
  326. return 1;
  327. return 0;
  328. }
  329. /* Do the same thing access(2) does, but use the effective uid and gid,
  330. and don't make the mistake of telling root that any file is
  331. executable. */
  332. static int test_eaccess(char *path, int mode)
  333. {
  334. struct stat st;
  335. unsigned int euid = geteuid();
  336. if (stat(path, &st) < 0)
  337. return -1;
  338. if (euid == 0) {
  339. /* Root can read or write any file. */
  340. if (mode != X_OK)
  341. return 0;
  342. /* Root can execute any file that has any one of the execute
  343. bits set. */
  344. if (st.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH))
  345. return 0;
  346. }
  347. if (st.st_uid == euid) /* owner */
  348. mode <<= 6;
  349. else if (is_a_group_member(st.st_gid))
  350. mode <<= 3;
  351. if (st.st_mode & mode)
  352. return 0;
  353. return -1;
  354. }
  355. static int filstat(char *nm, enum token mode)
  356. {
  357. struct stat s;
  358. int i = i; /* gcc 3.x thinks it can be used uninitialized */
  359. if (mode == FILSYM) {
  360. #ifdef S_IFLNK
  361. if (lstat(nm, &s) == 0) {
  362. i = S_IFLNK;
  363. goto filetype;
  364. }
  365. #endif
  366. return 0;
  367. }
  368. if (stat(nm, &s) != 0)
  369. return 0;
  370. if (mode == FILEXIST)
  371. return 1;
  372. if (is_file_access(mode)) {
  373. if (mode == FILRD)
  374. i = R_OK;
  375. if (mode == FILWR)
  376. i = W_OK;
  377. if (mode == FILEX)
  378. i = X_OK;
  379. return test_eaccess(nm, i) == 0;
  380. }
  381. if (is_file_type(mode)) {
  382. if (mode == FILREG)
  383. i = S_IFREG;
  384. if (mode == FILDIR)
  385. i = S_IFDIR;
  386. if (mode == FILCDEV)
  387. i = S_IFCHR;
  388. if (mode == FILBDEV)
  389. i = S_IFBLK;
  390. if (mode == FILFIFO) {
  391. #ifdef S_IFIFO
  392. i = S_IFIFO;
  393. #else
  394. return 0;
  395. #endif
  396. }
  397. if (mode == FILSOCK) {
  398. #ifdef S_IFSOCK
  399. i = S_IFSOCK;
  400. #else
  401. return 0;
  402. #endif
  403. }
  404. filetype:
  405. return ((s.st_mode & S_IFMT) == i);
  406. }
  407. if (is_file_bit(mode)) {
  408. if (mode == FILSUID)
  409. i = S_ISUID;
  410. if (mode == FILSGID)
  411. i = S_ISGID;
  412. if (mode == FILSTCK)
  413. i = S_ISVTX;
  414. return ((s.st_mode & i) != 0);
  415. }
  416. if (mode == FILGZ)
  417. return s.st_size > 0L;
  418. if (mode == FILUID)
  419. return s.st_uid == geteuid();
  420. if (mode == FILGID)
  421. return s.st_gid == getegid();
  422. return 1; /* NOTREACHED */
  423. }
  424. static arith_t nexpr(enum token n)
  425. {
  426. if (n == UNOT)
  427. return !nexpr(t_lex(*++t_wp));
  428. return primary(n);
  429. }
  430. static arith_t aexpr(enum token n)
  431. {
  432. arith_t res;
  433. res = nexpr(n);
  434. if (t_lex(*++t_wp) == BAND)
  435. return aexpr(t_lex(*++t_wp)) && res;
  436. t_wp--;
  437. return res;
  438. }
  439. static arith_t oexpr(enum token n)
  440. {
  441. arith_t res;
  442. res = aexpr(n);
  443. if (t_lex(*++t_wp) == BOR) {
  444. return oexpr(t_lex(*++t_wp)) || res;
  445. }
  446. t_wp--;
  447. return res;
  448. }
  449. static arith_t primary(enum token n)
  450. {
  451. arith_t res;
  452. if (n == EOI) {
  453. syntax(NULL, "argument expected");
  454. }
  455. if (n == LPAREN) {
  456. res = oexpr(t_lex(*++t_wp));
  457. if (t_lex(*++t_wp) != RPAREN)
  458. syntax(NULL, "closing paren expected");
  459. return res;
  460. }
  461. if (t_wp_op && t_wp_op->op_type == UNOP) {
  462. /* unary expression */
  463. if (*++t_wp == NULL)
  464. syntax(t_wp_op->op_text, "argument expected");
  465. if (n == STREZ)
  466. return t_wp[0][0] == '\0';
  467. if (n == STRNZ)
  468. return t_wp[0][0] != '\0';
  469. if (n == FILTT)
  470. return isatty(getn(*t_wp));
  471. return filstat(*t_wp, n);
  472. }
  473. t_lex(t_wp[1]);
  474. if (t_wp_op && t_wp_op->op_type == BINOP) {
  475. return binop();
  476. }
  477. return t_wp[0][0] != '\0';
  478. }
  479. int test_main(int argc, char **argv)
  480. {
  481. int res;
  482. const char *arg0;
  483. bool negate = 0;
  484. arg0 = bb_basename(argv[0]);
  485. if (arg0[0] == '[') {
  486. --argc;
  487. if (!arg0[1]) { /* "[" ? */
  488. if (NOT_LONE_CHAR(argv[argc], ']')) {
  489. bb_error_msg("missing ]");
  490. return 2;
  491. }
  492. } else { /* assuming "[[" */
  493. if (strcmp(argv[argc], "]]") != 0) {
  494. bb_error_msg("missing ]]");
  495. return 2;
  496. }
  497. }
  498. argv[argc] = NULL;
  499. }
  500. /* We must do DEINIT_S() prior to returning */
  501. INIT_S();
  502. res = setjmp(leaving);
  503. if (res)
  504. goto ret;
  505. /* resetting ngroups is probably unnecessary. it will
  506. * force a new call to getgroups(), which prevents using
  507. * group data fetched during a previous call. but the
  508. * only way the group data could be stale is if there's
  509. * been an intervening call to setgroups(), and this
  510. * isn't likely in the case of a shell. paranoia
  511. * prevails...
  512. */
  513. ngroups = 0;
  514. //argc--;
  515. argv++;
  516. /* Implement special cases from POSIX.2, section 4.62.4 */
  517. if (!argv[0]) { /* "test" */
  518. res = 1;
  519. goto ret;
  520. }
  521. if (LONE_CHAR(argv[0], '!') && argv[1]) {
  522. negate = 1;
  523. //argc--;
  524. argv++;
  525. }
  526. if (!argv[1]) { /* "test [!] arg" */
  527. res = (*argv[0] == '\0');
  528. goto ret;
  529. }
  530. if (argv[2] && !argv[3]) {
  531. t_lex(argv[1]);
  532. if (t_wp_op && t_wp_op->op_type == BINOP) {
  533. /* "test [!] arg1 <binary_op> arg2" */
  534. t_wp = &argv[0];
  535. res = (binop() == 0);
  536. goto ret;
  537. }
  538. }
  539. /* Some complex expression. Undo '!' removal */
  540. if (negate) {
  541. negate = 0;
  542. //argc++;
  543. argv--;
  544. }
  545. t_wp = &argv[0];
  546. res = !oexpr(t_lex(*t_wp));
  547. if (*t_wp != NULL && *++t_wp != NULL) {
  548. bb_error_msg("%s: unknown operand", *t_wp);
  549. res = 2;
  550. }
  551. ret:
  552. DEINIT_S();
  553. return negate ? !res : res;
  554. }