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