3
0

test.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  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 (LONE_CHAR(argv[0], '[')) {
  167. --argc;
  168. if (NOT_LONE_CHAR(argv[argc], ']')) {
  169. bb_error_msg("missing ]");
  170. return 2;
  171. }
  172. argv[argc] = NULL;
  173. } else if (strcmp(argv[0], "[[") == 0) {
  174. --argc;
  175. if (strcmp(argv[argc], "]]")) {
  176. bb_error_msg("missing ]]");
  177. return 2;
  178. }
  179. argv[argc] = NULL;
  180. }
  181. res = setjmp(leaving);
  182. if (res)
  183. return res;
  184. /* resetting ngroups is probably unnecessary. it will
  185. * force a new call to getgroups(), which prevents using
  186. * group data fetched during a previous call. but the
  187. * only way the group data could be stale is if there's
  188. * been an intervening call to setgroups(), and this
  189. * isn't likely in the case of a shell. paranoia
  190. * prevails...
  191. */
  192. ngroups = 0;
  193. /* Implement special cases from POSIX.2, section 4.62.4 */
  194. switch (argc) {
  195. case 1:
  196. return 1;
  197. case 2:
  198. return *argv[1] == '\0';
  199. case 3:
  200. if (argv[1][0] == '!' && argv[1][1] == '\0') {
  201. return *argv[2] != '\0';
  202. }
  203. break;
  204. case 4:
  205. if (argv[1][0] != '!' || argv[1][1] != '\0') {
  206. if (t_lex(argv[2]), t_wp_op && t_wp_op->op_type == BINOP) {
  207. t_wp = &argv[1];
  208. return binop() == 0;
  209. }
  210. }
  211. break;
  212. case 5:
  213. if (argv[1][0] == '!' && argv[1][1] == '\0') {
  214. if (t_lex(argv[3]), t_wp_op && t_wp_op->op_type == BINOP) {
  215. t_wp = &argv[2];
  216. return binop() != 0;
  217. }
  218. }
  219. break;
  220. }
  221. t_wp = &argv[1];
  222. res = !oexpr(t_lex(*t_wp));
  223. if (*t_wp != NULL && *++t_wp != NULL) {
  224. bb_error_msg("%s: unknown operand", *t_wp);
  225. return 2;
  226. }
  227. return res;
  228. }
  229. static void syntax(const char *op, const char *msg)
  230. {
  231. if (op && *op) {
  232. bb_error_msg("%s: %s", op, msg);
  233. } else {
  234. bb_error_msg("%s", msg);
  235. }
  236. longjmp(leaving, 2);
  237. }
  238. static arith_t oexpr(enum token n)
  239. {
  240. arith_t res;
  241. res = aexpr(n);
  242. if (t_lex(*++t_wp) == BOR) {
  243. return oexpr(t_lex(*++t_wp)) || res;
  244. }
  245. t_wp--;
  246. return res;
  247. }
  248. static arith_t aexpr(enum token n)
  249. {
  250. arith_t res;
  251. res = nexpr(n);
  252. if (t_lex(*++t_wp) == BAND)
  253. return aexpr(t_lex(*++t_wp)) && res;
  254. t_wp--;
  255. return res;
  256. }
  257. static arith_t nexpr(enum token n)
  258. {
  259. if (n == UNOT)
  260. return !nexpr(t_lex(*++t_wp));
  261. return primary(n);
  262. }
  263. static arith_t primary(enum token n)
  264. {
  265. arith_t res;
  266. if (n == EOI) {
  267. syntax(NULL, "argument expected");
  268. }
  269. if (n == LPAREN) {
  270. res = oexpr(t_lex(*++t_wp));
  271. if (t_lex(*++t_wp) != RPAREN)
  272. syntax(NULL, "closing paren expected");
  273. return res;
  274. }
  275. if (t_wp_op && t_wp_op->op_type == UNOP) {
  276. /* unary expression */
  277. if (*++t_wp == NULL)
  278. syntax(t_wp_op->op_text, "argument expected");
  279. switch (n) {
  280. case STREZ:
  281. return strlen(*t_wp) == 0;
  282. case STRNZ:
  283. return strlen(*t_wp) != 0;
  284. case FILTT:
  285. return isatty(getn(*t_wp));
  286. default:
  287. return filstat(*t_wp, n);
  288. }
  289. }
  290. if (t_lex(t_wp[1]), t_wp_op && t_wp_op->op_type == BINOP) {
  291. return binop();
  292. }
  293. return strlen(*t_wp) > 0;
  294. }
  295. static int binop(void)
  296. {
  297. const char *opnd1, *opnd2;
  298. struct t_op const *op;
  299. opnd1 = *t_wp;
  300. (void) t_lex(*++t_wp);
  301. op = t_wp_op;
  302. if ((opnd2 = *++t_wp) == (char *) 0)
  303. syntax(op->op_text, "argument expected");
  304. switch (op->op_num) {
  305. case STREQ:
  306. return strcmp(opnd1, opnd2) == 0;
  307. case STRNE:
  308. return strcmp(opnd1, opnd2) != 0;
  309. case STRLT:
  310. return strcmp(opnd1, opnd2) < 0;
  311. case STRGT:
  312. return strcmp(opnd1, opnd2) > 0;
  313. case INTEQ:
  314. return getn(opnd1) == getn(opnd2);
  315. case INTNE:
  316. return getn(opnd1) != getn(opnd2);
  317. case INTGE:
  318. return getn(opnd1) >= getn(opnd2);
  319. case INTGT:
  320. return getn(opnd1) > getn(opnd2);
  321. case INTLE:
  322. return getn(opnd1) <= getn(opnd2);
  323. case INTLT:
  324. return getn(opnd1) < getn(opnd2);
  325. case FILNT:
  326. return newerf(opnd1, opnd2);
  327. case FILOT:
  328. return olderf(opnd1, opnd2);
  329. case FILEQ:
  330. return equalf(opnd1, opnd2);
  331. }
  332. /* NOTREACHED */
  333. return 1;
  334. }
  335. static int filstat(char *nm, enum token mode)
  336. {
  337. struct stat s;
  338. unsigned int i;
  339. if (mode == FILSYM) {
  340. #ifdef S_IFLNK
  341. if (lstat(nm, &s) == 0) {
  342. i = S_IFLNK;
  343. goto filetype;
  344. }
  345. #endif
  346. return 0;
  347. }
  348. if (stat(nm, &s) != 0)
  349. return 0;
  350. switch (mode) {
  351. case FILRD:
  352. return test_eaccess(nm, R_OK) == 0;
  353. case FILWR:
  354. return test_eaccess(nm, W_OK) == 0;
  355. case FILEX:
  356. return test_eaccess(nm, X_OK) == 0;
  357. case FILEXIST:
  358. return 1;
  359. case FILREG:
  360. i = S_IFREG;
  361. goto filetype;
  362. case FILDIR:
  363. i = S_IFDIR;
  364. goto filetype;
  365. case FILCDEV:
  366. i = S_IFCHR;
  367. goto filetype;
  368. case FILBDEV:
  369. i = S_IFBLK;
  370. goto filetype;
  371. case FILFIFO:
  372. #ifdef S_IFIFO
  373. i = S_IFIFO;
  374. goto filetype;
  375. #else
  376. return 0;
  377. #endif
  378. case FILSOCK:
  379. #ifdef S_IFSOCK
  380. i = S_IFSOCK;
  381. goto filetype;
  382. #else
  383. return 0;
  384. #endif
  385. case FILSUID:
  386. i = S_ISUID;
  387. goto filebit;
  388. case FILSGID:
  389. i = S_ISGID;
  390. goto filebit;
  391. case FILSTCK:
  392. i = S_ISVTX;
  393. goto filebit;
  394. case FILGZ:
  395. return s.st_size > 0L;
  396. case FILUID:
  397. return s.st_uid == geteuid();
  398. case FILGID:
  399. return s.st_gid == getegid();
  400. default:
  401. return 1;
  402. }
  403. filetype:
  404. return ((s.st_mode & S_IFMT) == i);
  405. filebit:
  406. return ((s.st_mode & i) != 0);
  407. }
  408. static enum token t_lex(char *s)
  409. {
  410. struct t_op const *op = ops;
  411. if (s == 0) {
  412. t_wp_op = (struct t_op *) 0;
  413. return EOI;
  414. }
  415. while (op->op_text) {
  416. if (strcmp(s, op->op_text) == 0) {
  417. t_wp_op = op;
  418. return op->op_num;
  419. }
  420. op++;
  421. }
  422. t_wp_op = (struct t_op *) 0;
  423. return OPERAND;
  424. }
  425. /* atoi with error detection */
  426. static arith_t getn(const char *s)
  427. {
  428. char *p;
  429. #ifdef CONFIG_FEATURE_TEST_64
  430. long long r;
  431. #else
  432. long r;
  433. #endif
  434. errno = 0;
  435. #ifdef CONFIG_FEATURE_TEST_64
  436. r = strtoll(s, &p, 10);
  437. #else
  438. r = strtol(s, &p, 10);
  439. #endif
  440. if (errno != 0)
  441. syntax(s, "out of range");
  442. if (*(skip_whitespace(p)))
  443. syntax(s, "bad number");
  444. return r;
  445. }
  446. static int newerf(const char *f1, const char *f2)
  447. {
  448. struct stat b1, b2;
  449. return (stat(f1, &b1) == 0 &&
  450. stat(f2, &b2) == 0 && b1.st_mtime > b2.st_mtime);
  451. }
  452. static int olderf(const char *f1, const char *f2)
  453. {
  454. struct stat b1, b2;
  455. return (stat(f1, &b1) == 0 &&
  456. stat(f2, &b2) == 0 && b1.st_mtime < b2.st_mtime);
  457. }
  458. static int equalf(const char *f1, const char *f2)
  459. {
  460. struct stat b1, b2;
  461. return (stat(f1, &b1) == 0 &&
  462. stat(f2, &b2) == 0 &&
  463. b1.st_dev == b2.st_dev && b1.st_ino == b2.st_ino);
  464. }
  465. /* Do the same thing access(2) does, but use the effective uid and gid,
  466. and don't make the mistake of telling root that any file is
  467. executable. */
  468. static int test_eaccess(char *path, int mode)
  469. {
  470. struct stat st;
  471. unsigned int euid = geteuid();
  472. if (stat(path, &st) < 0)
  473. return -1;
  474. if (euid == 0) {
  475. /* Root can read or write any file. */
  476. if (mode != X_OK)
  477. return 0;
  478. /* Root can execute any file that has any one of the execute
  479. bits set. */
  480. if (st.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH))
  481. return 0;
  482. }
  483. if (st.st_uid == euid) /* owner */
  484. mode <<= 6;
  485. else if (is_a_group_member(st.st_gid))
  486. mode <<= 3;
  487. if (st.st_mode & mode)
  488. return 0;
  489. return -1;
  490. }
  491. static void initialize_group_array(void)
  492. {
  493. ngroups = getgroups(0, NULL);
  494. if (ngroups > 0) {
  495. group_array = xmalloc(ngroups * sizeof(gid_t));
  496. getgroups(ngroups, group_array);
  497. }
  498. }
  499. /* Return non-zero if GID is one that we have in our groups list. */
  500. static int is_a_group_member(gid_t gid)
  501. {
  502. int i;
  503. /* Short-circuit if possible, maybe saving a call to getgroups(). */
  504. if (gid == getgid() || gid == getegid())
  505. return 1;
  506. if (ngroups == 0)
  507. initialize_group_array();
  508. /* Search through the list looking for GID. */
  509. for (i = 0; i < ngroups; i++)
  510. if (gid == group_array[i])
  511. return 1;
  512. return 0;
  513. }
  514. /* applet entry point */
  515. int test_main(int argc, char **argv)
  516. {
  517. return bb_test(argc, argv);
  518. }