test.c 11 KB

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