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