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