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