blob: 818b3db1279ba208762708eae5d9d90c29bf2fd9 [file] [log] [blame]
Erik Andersen13456d12000-03-16 08:09:57 +00001/* vi: set sw=4 ts=4: */
2/*
Erik Andersen5e1189e2000-04-15 16:34:54 +00003 * test implementation for busybox
Erik Andersen13456d12000-03-16 08:09:57 +00004 *
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 <andersee@debian.org> to be used
14 * in busybox.
15 *
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 2 of the License, or
19 * (at your option) any later version.
20 *
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
24 * General Public License for more details.
25 *
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, write to the Free Software
28 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 *
30 * Original copyright notice states:
31 * "This program is in the Public Domain."
32 */
33
34#include "internal.h"
35#include <sys/types.h>
36#include <sys/stat.h>
37#include <unistd.h>
38#include <ctype.h>
39#include <errno.h>
40#include <stdlib.h>
41#include <string.h>
Erik Andersen7ab9c7e2000-05-12 19:41:47 +000042#define BB_DECLARE_EXTERN
43#define bb_need_help
44#include "messages.c"
Erik Andersen13456d12000-03-16 08:09:57 +000045
46/* test(1) accepts the following grammar:
47 oexpr ::= aexpr | aexpr "-o" oexpr ;
48 aexpr ::= nexpr | nexpr "-a" aexpr ;
49 nexpr ::= primary | "!" primary
50 primary ::= unary-operator operand
51 | operand binary-operator operand
52 | operand
53 | "(" oexpr ")"
54 ;
55 unary-operator ::= "-r"|"-w"|"-x"|"-f"|"-d"|"-c"|"-b"|"-p"|
56 "-u"|"-g"|"-k"|"-s"|"-t"|"-z"|"-n"|"-o"|"-O"|"-G"|"-L"|"-S";
57
58 binary-operator ::= "="|"!="|"-eq"|"-ne"|"-ge"|"-gt"|"-le"|"-lt"|
59 "-nt"|"-ot"|"-ef";
60 operand ::= <any legal UNIX file name>
61*/
62
63enum token {
64 EOI,
65 FILRD,
66 FILWR,
67 FILEX,
68 FILEXIST,
69 FILREG,
70 FILDIR,
71 FILCDEV,
72 FILBDEV,
73 FILFIFO,
74 FILSOCK,
75 FILSYM,
76 FILGZ,
77 FILTT,
78 FILSUID,
79 FILSGID,
80 FILSTCK,
81 FILNT,
82 FILOT,
83 FILEQ,
84 FILUID,
85 FILGID,
86 STREZ,
87 STRNZ,
88 STREQ,
89 STRNE,
90 STRLT,
91 STRGT,
92 INTEQ,
93 INTNE,
94 INTGE,
95 INTGT,
96 INTLE,
97 INTLT,
98 UNOT,
99 BAND,
100 BOR,
101 LPAREN,
102 RPAREN,
103 OPERAND
104};
105
106enum token_types {
107 UNOP,
108 BINOP,
109 BUNOP,
110 BBINOP,
111 PAREN
112};
113
114struct t_op {
115 const char *op_text;
116 short op_num, op_type;
117} const ops [] = {
118 {"-r", FILRD, UNOP},
119 {"-w", FILWR, UNOP},
120 {"-x", FILEX, UNOP},
121 {"-e", FILEXIST,UNOP},
122 {"-f", FILREG, UNOP},
123 {"-d", FILDIR, UNOP},
124 {"-c", FILCDEV,UNOP},
125 {"-b", FILBDEV,UNOP},
126 {"-p", FILFIFO,UNOP},
127 {"-u", FILSUID,UNOP},
128 {"-g", FILSGID,UNOP},
129 {"-k", FILSTCK,UNOP},
130 {"-s", FILGZ, UNOP},
131 {"-t", FILTT, UNOP},
132 {"-z", STREZ, UNOP},
133 {"-n", STRNZ, UNOP},
134 {"-h", FILSYM, UNOP}, /* for backwards compat */
135 {"-O", FILUID, UNOP},
136 {"-G", FILGID, UNOP},
137 {"-L", FILSYM, UNOP},
138 {"-S", FILSOCK,UNOP},
139 {"=", STREQ, BINOP},
140 {"!=", STRNE, BINOP},
141 {"<", STRLT, BINOP},
142 {">", STRGT, BINOP},
143 {"-eq", INTEQ, BINOP},
144 {"-ne", INTNE, BINOP},
145 {"-ge", INTGE, BINOP},
146 {"-gt", INTGT, BINOP},
147 {"-le", INTLE, BINOP},
148 {"-lt", INTLT, BINOP},
149 {"-nt", FILNT, BINOP},
150 {"-ot", FILOT, BINOP},
151 {"-ef", FILEQ, BINOP},
152 {"!", UNOT, BUNOP},
153 {"-a", BAND, BBINOP},
154 {"-o", BOR, BBINOP},
155 {"(", LPAREN, PAREN},
156 {")", RPAREN, PAREN},
157 {0, 0, 0}
158};
159
160char **t_wp;
161struct t_op const *t_wp_op;
162static gid_t *group_array = NULL;
163static int ngroups;
164
165static enum token t_lex();
166static int oexpr();
167static int aexpr();
168static int nexpr();
169static int binop();
170static int primary();
171static int filstat();
172static int getn();
173static int newerf();
174static int olderf();
175static int equalf();
176static void syntax();
177static int test_eaccess();
178static int is_a_group_member();
179static void initialize_group_array();
180
181extern int
182test_main(int argc, char** argv)
183{
184 int res;
185
Matt Kraaie58771e2000-07-12 15:38:49 +0000186 if (strcmp(applet_name, "[") == 0) {
Erik Andersen13456d12000-03-16 08:09:57 +0000187 if (strcmp(argv[--argc], "]"))
Eric Andersen86ab8a32000-06-02 03:21:42 +0000188 fatalError("missing ]\n");
Erik Andersen13456d12000-03-16 08:09:57 +0000189 argv[argc] = NULL;
190 }
Erik Andersen13456d12000-03-16 08:09:57 +0000191 /* Implement special cases from POSIX.2, section 4.62.4 */
192 switch (argc) {
193 case 1:
194 exit( 1);
195 case 2:
196 exit (*argv[1] == '\0');
197 case 3:
198 if (argv[1][0] == '!' && argv[1][1] == '\0') {
199 exit (!(*argv[2] == '\0'));
200 }
201 break;
202 case 4:
203 if (argv[1][0] != '!' || argv[1][1] != '\0') {
204 if (t_lex(argv[2]),
205 t_wp_op && t_wp_op->op_type == BINOP) {
206 t_wp = &argv[1];
207 exit (binop() == 0);
208 }
209 }
210 break;
211 case 5:
212 if (argv[1][0] == '!' && argv[1][1] == '\0') {
213 if (t_lex(argv[3]),
214 t_wp_op && t_wp_op->op_type == BINOP) {
215 t_wp = &argv[2];
216 exit (!(binop() == 0));
217 }
218 }
219 break;
220 }
221
222 t_wp = &argv[1];
223 res = !oexpr(t_lex(*t_wp));
224
225 if (*t_wp != NULL && *++t_wp != NULL)
226 syntax(*t_wp, "unknown operand");
227
Eric Andersenb6106152000-06-19 17:25:40 +0000228 return( res);
Erik Andersen13456d12000-03-16 08:09:57 +0000229}
230
231static void
232syntax(op, msg)
233 char *op;
234 char *msg;
235{
236 if (op && *op)
Eric Andersen86ab8a32000-06-02 03:21:42 +0000237 fatalError("%s: %s\n", op, msg);
Erik Andersen13456d12000-03-16 08:09:57 +0000238 else
Eric Andersen86ab8a32000-06-02 03:21:42 +0000239 fatalError("%s\n", msg);
Erik Andersen13456d12000-03-16 08:09:57 +0000240}
241
242static int
243oexpr(n)
244 enum token n;
245{
246 int res;
247
248 res = aexpr(n);
249 if (t_lex(*++t_wp) == BOR)
250 return oexpr(t_lex(*++t_wp)) || res;
251 t_wp--;
252 return res;
253}
254
255static int
256aexpr(n)
257 enum token n;
258{
259 int res;
260
261 res = nexpr(n);
262 if (t_lex(*++t_wp) == BAND)
263 return aexpr(t_lex(*++t_wp)) && res;
264 t_wp--;
265 return res;
266}
267
268static int
269nexpr(n)
270 enum token n; /* token */
271{
272 if (n == UNOT)
273 return !nexpr(t_lex(*++t_wp));
274 return primary(n);
275}
276
277static int
278primary(n)
279 enum token n;
280{
281 int res;
282
283 if (n == EOI)
284 syntax(NULL, "argument expected");
285 if (n == LPAREN) {
286 res = oexpr(t_lex(*++t_wp));
287 if (t_lex(*++t_wp) != RPAREN)
288 syntax(NULL, "closing paren expected");
289 return res;
290 }
291 if (t_wp_op && t_wp_op->op_type == UNOP) {
292 /* unary expression */
293 if (*++t_wp == NULL)
294 syntax(t_wp_op->op_text, "argument expected");
295 switch (n) {
296 case STREZ:
297 return strlen(*t_wp) == 0;
298 case STRNZ:
299 return strlen(*t_wp) != 0;
300 case FILTT:
301 return isatty(getn(*t_wp));
302 default:
303 return filstat(*t_wp, n);
304 }
305 }
306
307 if (t_lex(t_wp[1]), t_wp_op && t_wp_op->op_type == BINOP) {
308 return binop();
309 }
310
311 return strlen(*t_wp) > 0;
312}
313
314static int
315binop()
316{
317 const char *opnd1, *opnd2;
318 struct t_op const *op;
319
320 opnd1 = *t_wp;
321 (void) t_lex(*++t_wp);
322 op = t_wp_op;
323
324 if ((opnd2 = *++t_wp) == (char *)0)
325 syntax(op->op_text, "argument expected");
326
327 switch (op->op_num) {
328 case STREQ:
329 return strcmp(opnd1, opnd2) == 0;
330 case STRNE:
331 return strcmp(opnd1, opnd2) != 0;
332 case STRLT:
333 return strcmp(opnd1, opnd2) < 0;
334 case STRGT:
335 return strcmp(opnd1, opnd2) > 0;
336 case INTEQ:
337 return getn(opnd1) == getn(opnd2);
338 case INTNE:
339 return getn(opnd1) != getn(opnd2);
340 case INTGE:
341 return getn(opnd1) >= getn(opnd2);
342 case INTGT:
343 return getn(opnd1) > getn(opnd2);
344 case INTLE:
345 return getn(opnd1) <= getn(opnd2);
346 case INTLT:
347 return getn(opnd1) < getn(opnd2);
348 case FILNT:
349 return newerf (opnd1, opnd2);
350 case FILOT:
351 return olderf (opnd1, opnd2);
352 case FILEQ:
353 return equalf (opnd1, opnd2);
354 }
355 /* NOTREACHED */
356 return 1;
357}
358
359static int
360filstat(nm, mode)
361 char *nm;
362 enum token mode;
363{
364 struct stat s;
Eric Andersenfad04fd2000-07-14 06:49:52 +0000365 unsigned int i;
Erik Andersen13456d12000-03-16 08:09:57 +0000366
367 if (mode == FILSYM) {
368#ifdef S_IFLNK
369 if (lstat(nm, &s) == 0) {
370 i = S_IFLNK;
371 goto filetype;
372 }
373#endif
374 return 0;
375 }
376
377 if (stat(nm, &s) != 0)
378 return 0;
379
380 switch (mode) {
381 case FILRD:
382 return test_eaccess(nm, R_OK) == 0;
383 case FILWR:
384 return test_eaccess(nm, W_OK) == 0;
385 case FILEX:
386 return test_eaccess(nm, X_OK) == 0;
387 case FILEXIST:
388 return 1;
389 case FILREG:
390 i = S_IFREG;
391 goto filetype;
392 case FILDIR:
393 i = S_IFDIR;
394 goto filetype;
395 case FILCDEV:
396 i = S_IFCHR;
397 goto filetype;
398 case FILBDEV:
399 i = S_IFBLK;
400 goto filetype;
401 case FILFIFO:
402#ifdef S_IFIFO
403 i = S_IFIFO;
404 goto filetype;
405#else
406 return 0;
407#endif
408 case FILSOCK:
409#ifdef S_IFSOCK
410 i = S_IFSOCK;
411 goto filetype;
412#else
413 return 0;
414#endif
415 case FILSUID:
416 i = S_ISUID;
417 goto filebit;
418 case FILSGID:
419 i = S_ISGID;
420 goto filebit;
421 case FILSTCK:
422 i = S_ISVTX;
423 goto filebit;
424 case FILGZ:
425 return s.st_size > 0L;
426 case FILUID:
427 return s.st_uid == geteuid();
428 case FILGID:
429 return s.st_gid == getegid();
430 default:
431 return 1;
432 }
433
434filetype:
435 return ((s.st_mode & S_IFMT) == i);
436
437filebit:
438 return ((s.st_mode & i) != 0);
439}
440
441static enum token
442t_lex(s)
443 char *s;
444{
445 struct t_op const *op = ops;
446
447 if (s == 0) {
448 t_wp_op = (struct t_op *)0;
449 return EOI;
450 }
451 while (op->op_text) {
452 if (strcmp(s, op->op_text) == 0) {
453 t_wp_op = op;
454 return op->op_num;
455 }
456 op++;
457 }
458 t_wp_op = (struct t_op *)0;
459 return OPERAND;
460}
461
462/* atoi with error detection */
463static int
464getn(s)
465 char *s;
466{
467 char *p;
468 long r;
469
470 errno = 0;
471 r = strtol(s, &p, 10);
472
473 if (errno != 0)
Eric Andersen86ab8a32000-06-02 03:21:42 +0000474 fatalError("%s: out of range\n", s);
Erik Andersen13456d12000-03-16 08:09:57 +0000475
476 while (isspace(*p))
477 p++;
478
479 if (*p)
Eric Andersen86ab8a32000-06-02 03:21:42 +0000480 fatalError("%s: bad number\n", s);
Erik Andersen13456d12000-03-16 08:09:57 +0000481
482 return (int) r;
483}
484
485static int
486newerf (f1, f2)
487char *f1, *f2;
488{
489 struct stat b1, b2;
490
491 return (stat (f1, &b1) == 0 &&
492 stat (f2, &b2) == 0 &&
493 b1.st_mtime > b2.st_mtime);
494}
495
496static int
497olderf (f1, f2)
498char *f1, *f2;
499{
500 struct stat b1, b2;
501
502 return (stat (f1, &b1) == 0 &&
503 stat (f2, &b2) == 0 &&
504 b1.st_mtime < b2.st_mtime);
505}
506
507static int
508equalf (f1, f2)
509char *f1, *f2;
510{
511 struct stat b1, b2;
512
513 return (stat (f1, &b1) == 0 &&
514 stat (f2, &b2) == 0 &&
515 b1.st_dev == b2.st_dev &&
516 b1.st_ino == b2.st_ino);
517}
518
519/* Do the same thing access(2) does, but use the effective uid and gid,
520 and don't make the mistake of telling root that any file is
521 executable. */
522static int
523test_eaccess (path, mode)
524char *path;
525int mode;
526{
527 struct stat st;
Eric Andersenfad04fd2000-07-14 06:49:52 +0000528 unsigned int euid = geteuid();
Erik Andersen13456d12000-03-16 08:09:57 +0000529
530 if (stat (path, &st) < 0)
531 return (-1);
532
533 if (euid == 0) {
534 /* Root can read or write any file. */
535 if (mode != X_OK)
536 return (0);
537
538 /* Root can execute any file that has any one of the execute
539 bits set. */
540 if (st.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH))
541 return (0);
542 }
543
544 if (st.st_uid == euid) /* owner */
545 mode <<= 6;
546 else if (is_a_group_member (st.st_gid))
547 mode <<= 3;
548
549 if (st.st_mode & mode)
550 return (0);
551
552 return (-1);
553}
554
555static void
556initialize_group_array ()
557{
558 ngroups = getgroups(0, NULL);
559 if ((group_array = realloc(group_array, ngroups * sizeof(gid_t))) == NULL)
Eric Andersen86ab8a32000-06-02 03:21:42 +0000560 fatalError("Out of space\n");
Erik Andersen13456d12000-03-16 08:09:57 +0000561
562 getgroups(ngroups, group_array);
563}
564
565/* Return non-zero if GID is one that we have in our groups list. */
566static int
567is_a_group_member (gid)
568gid_t gid;
569{
570 register int i;
571
572 /* Short-circuit if possible, maybe saving a call to getgroups(). */
573 if (gid == getgid() || gid == getegid())
574 return (1);
575
576 if (ngroups == 0)
577 initialize_group_array ();
578
579 /* Search through the list looking for GID. */
580 for (i = 0; i < ngroups; i++)
581 if (gid == group_array[i])
582 return (1);
583
584 return (0);
585}