Home | History | Annotate | Line # | Download | only in timedc
timedc.c revision 1.8
      1 /*	$NetBSD: timedc.c,v 1.8 2001/09/02 00:13:08 reinoud Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1985, 1993 The Regents of the University of California.
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by the University of
     18  *	California, Berkeley and its contributors.
     19  * 4. Neither the name of the University nor the names of its contributors
     20  *    may be used to endorse or promote products derived from this software
     21  *    without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  * SUCH DAMAGE.
     34  */
     35 
     36 #include <sys/cdefs.h>
     37 #ifndef lint
     38 __COPYRIGHT(
     39 "@(#) Copyright (c) 1985, 1993 The Regents of the University of California.\n\
     40  All rights reserved.\n");
     41 #endif /* not lint */
     42 
     43 #ifndef lint
     44 #if 0
     45 static char sccsid[] = "@(#)timedc.c	8.1 (Berkeley) 6/6/93";
     46 #else
     47 __RCSID("$NetBSD: timedc.c,v 1.8 2001/09/02 00:13:08 reinoud Exp $");
     48 #endif
     49 #endif /* not lint */
     50 
     51 #include "timedc.h"
     52 #include <ctype.h>
     53 #include <setjmp.h>
     54 #include <signal.h>
     55 #include <stdlib.h>
     56 #include <string.h>
     57 #include <syslog.h>
     58 #include <unistd.h>
     59 
     60 int trace = 0;
     61 FILE *fd = 0;
     62 int	margc;
     63 int	fromatty;
     64 char	*margv[20];
     65 char	cmdline[200];
     66 jmp_buf	toplevel;
     67 static struct cmd *getcmd(char *);
     68 
     69 int
     70 main(int argc, char *argv[])
     71 {
     72 	register struct cmd *c;
     73 
     74 	openlog("timedc", 0, LOG_AUTH);
     75 
     76 	/*
     77 	 * security dictates!
     78 	 */
     79 	if (priv_resources() < 0) {
     80 		fprintf(stderr, "Could not get privileged resources\n");
     81 		exit(1);
     82 	}
     83 	(void) setuid(getuid());
     84 
     85 	if (--argc > 0) {
     86 		c = getcmd(*++argv);
     87 		if (c == (struct cmd *)-1) {
     88 			printf("?Ambiguous command\n");
     89 			exit(1);
     90 		}
     91 		if (c == 0) {
     92 			printf("?Invalid command\n");
     93 			exit(1);
     94 		}
     95 		if (c->c_priv && getuid()) {
     96 			printf("?Privileged command\n");
     97 			exit(1);
     98 		}
     99 		(*c->c_handler)(argc, argv);
    100 		exit(0);
    101 	}
    102 
    103 	fromatty = isatty(fileno(stdin));
    104 	if (setjmp(toplevel))
    105 		putchar('\n');
    106 	(void) signal(SIGINT, intr);
    107 	for (;;) {
    108 		if (fromatty) {
    109 			printf("timedc> ");
    110 			(void) fflush(stdout);
    111 		}
    112 		if (fgets(cmdline, sizeof(cmdline), stdin) == 0)
    113 			quit(0, NULL);
    114 		if (cmdline[0] == 0)
    115 			break;
    116 		makeargv();
    117 		if (margv[0] == 0)
    118 			continue;
    119 		c = getcmd(margv[0]);
    120 		if (c == (struct cmd *)-1) {
    121 			printf("?Ambiguous command\n");
    122 			continue;
    123 		}
    124 		if (c == 0) {
    125 			printf("?Invalid command\n");
    126 			continue;
    127 		}
    128 		if (c->c_priv && getuid()) {
    129 			printf("?Privileged command\n");
    130 			continue;
    131 		}
    132 		(*c->c_handler)(margc, margv);
    133 	}
    134 	return 0;
    135 }
    136 
    137 void
    138 intr(signo)
    139 	int signo;
    140 {
    141 	if (!fromatty)
    142 		exit(0);
    143 	longjmp(toplevel, 1);
    144 }
    145 
    146 
    147 static struct cmd *
    148 getcmd(char *name)
    149 {
    150 	register char *p, *q;
    151 	register struct cmd *c, *found;
    152 	register int nmatches, longest;
    153 	extern struct cmd cmdtab[];
    154 	extern int NCMDS;
    155 
    156 	longest = 0;
    157 	nmatches = 0;
    158 	found = 0;
    159 	for (c = cmdtab; c < &cmdtab[NCMDS]; c++) {
    160 		p = c->c_name;
    161 		for (q = name; *q == *p++; q++)
    162 			if (*q == 0)		/* exact match? */
    163 				return(c);
    164 		if (!*q) {			/* the name was a prefix */
    165 			if (q - name > longest) {
    166 				longest = q - name;
    167 				nmatches = 1;
    168 				found = c;
    169 			} else if (q - name == longest)
    170 				nmatches++;
    171 		}
    172 	}
    173 	if (nmatches > 1)
    174 		return((struct cmd *)-1);
    175 	return(found);
    176 }
    177 
    178 /*
    179  * Slice a string up into argc/argv.
    180  */
    181 void
    182 makeargv()
    183 {
    184 	register char *cp;
    185 	register char **argp = margv;
    186 
    187 	margc = 0;
    188 	for (cp = cmdline; *cp;) {
    189 		while (isspace(*cp))
    190 			cp++;
    191 		if (*cp == '\0')
    192 			break;
    193 		*argp++ = cp;
    194 		margc += 1;
    195 		while (*cp != '\0' && !isspace(*cp))
    196 			cp++;
    197 		if (*cp == '\0')
    198 			break;
    199 		*cp++ = '\0';
    200 	}
    201 	*argp++ = 0;
    202 }
    203 
    204 #define HELPINDENT (sizeof ("directory"))
    205 
    206 /*
    207  * Help command.
    208  */
    209 void
    210 help(argc, argv)
    211 	int argc;
    212 	char *argv[];
    213 {
    214 	register struct cmd *c;
    215 	extern struct cmd cmdtab[];
    216 
    217 	if (argc == 1) {
    218 		register int i, j, w;
    219 		int columns, width = 0, lines;
    220 		extern int NCMDS;
    221 
    222 		printf("Commands may be abbreviated.  Commands are:\n\n");
    223 		for (c = cmdtab; c < &cmdtab[NCMDS]; c++) {
    224 			int len = strlen(c->c_name);
    225 
    226 			if (len > width)
    227 				width = len;
    228 		}
    229 		width = (width + 8) &~ 7;
    230 		columns = 80 / width;
    231 		if (columns == 0)
    232 			columns = 1;
    233 		lines = (NCMDS + columns - 1) / columns;
    234 		for (i = 0; i < lines; i++) {
    235 			for (j = 0; j < columns; j++) {
    236 				c = cmdtab + j * lines + i;
    237 				printf("%s", c->c_name);
    238 				if (c + lines >= &cmdtab[NCMDS]) {
    239 					printf("\n");
    240 					break;
    241 				}
    242 				w = strlen(c->c_name);
    243 				while (w < width) {
    244 					w = (w + 8) &~ 7;
    245 					putchar('\t');
    246 				}
    247 			}
    248 		}
    249 		return;
    250 	}
    251 	while (--argc > 0) {
    252 		register char *arg;
    253 		arg = *++argv;
    254 		c = getcmd(arg);
    255 		if (c == (struct cmd *)-1)
    256 			printf("?Ambiguous help command %s\n", arg);
    257 		else if (c == (struct cmd *)0)
    258 			printf("?Invalid help command %s\n", arg);
    259 		else
    260 			printf("%-*s\t%s\n", (int)HELPINDENT,
    261 				c->c_name, c->c_help);
    262 	}
    263 }
    264