Home | History | Annotate | Line # | Download | only in timedc
timedc.c revision 1.17
      1 /*	$NetBSD: timedc.c,v 1.17 2007/01/25 23:47:13 christos 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.17 2007/01/25 23:47:13 christos 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 #include <err.h>
     56 
     57 int trace = 0;
     58 FILE *fd = 0;
     59 int	margc;
     60 int	fromatty;
     61 #define MAX_MARGV	20
     62 char	*margv[MAX_MARGV];
     63 char	cmdline[200];
     64 jmp_buf	toplevel;
     65 static struct cmd *getcmd(char *);
     66 
     67 int
     68 main(int argc, char *argv[])
     69 {
     70 	struct cmd *c;
     71 
     72 	openlog("timedc", 0, LOG_AUTH);
     73 
     74 	/*
     75 	 * security dictates!
     76 	 */
     77 	if (priv_resources() < 0)
     78 		errx(1, "Could not get privileged resources");
     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 		if (makeargv()) {
    113 			printf("?Too many arguments\n");
    114 			continue;
    115 		}
    116 		if (margv[0] == 0)
    117 			continue;
    118 		c = getcmd(margv[0]);
    119 		if (c == (struct cmd *)-1) {
    120 			printf("?Ambiguous command\n");
    121 			continue;
    122 		}
    123 		if (c == 0) {
    124 			printf("?Invalid command\n");
    125 			continue;
    126 		}
    127 		if (c->c_priv && getuid()) {
    128 			printf("?Privileged command\n");
    129 			continue;
    130 		}
    131 		(*c->c_handler)(margc, margv);
    132 	}
    133 	return 0;
    134 }
    135 
    136 void
    137 intr(int signo)
    138 {
    139 	(void) signo;
    140 	if (!fromatty)
    141 		exit(0);
    142 	longjmp(toplevel, 1);
    143 }
    144 
    145 
    146 static struct cmd *
    147 getcmd(char *name)
    148 {
    149 	const char *p;
    150 	char *q;
    151 	struct cmd *c, *found;
    152 	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 int
    182 makeargv(void)
    183 {
    184 	char *cp;
    185 	char **argp = margv;
    186 
    187 	margc = 0;
    188 	for (cp = cmdline; argp < &margv[MAX_MARGV - 1] && *cp;) {
    189 		while (isspace((unsigned char)*cp))
    190 			cp++;
    191 		if (*cp == '\0')
    192 			break;
    193 		*argp++ = cp;
    194 		margc += 1;
    195 		while (*cp != '\0' && !isspace((unsigned char)*cp))
    196 			cp++;
    197 		if (*cp == '\0')
    198 			break;
    199 		*cp++ = '\0';
    200 	}
    201 	if (margc == MAX_MARGV - 1)
    202 		return 1;
    203 	*argp++ = 0;
    204 	return 0;
    205 }
    206 
    207 #define HELPINDENT (sizeof ("directory"))
    208 
    209 /*
    210  * Help command.
    211  */
    212 void
    213 help(int argc, char *argv[])
    214 {
    215 	struct cmd *c;
    216 	extern struct cmd cmdtab[];
    217 
    218 	if (argc == 1) {
    219 		int i, j, w;
    220 		int columns, width = 0, lines;
    221 		extern int NCMDS;
    222 
    223 		printf("Commands may be abbreviated.  Commands are:\n\n");
    224 		for (c = cmdtab; c < &cmdtab[NCMDS]; c++) {
    225 			int len = strlen(c->c_name);
    226 
    227 			if (len > width)
    228 				width = len;
    229 		}
    230 		width = (width + 8) &~ 7;
    231 		columns = 80 / width;
    232 		if (columns == 0)
    233 			columns = 1;
    234 		lines = (NCMDS + columns - 1) / columns;
    235 		for (i = 0; i < lines; i++) {
    236 			for (j = 0; j < columns; j++) {
    237 				c = cmdtab + j * lines + i;
    238 				printf("%s", c->c_name);
    239 				if (c + lines >= &cmdtab[NCMDS]) {
    240 					printf("\n");
    241 					break;
    242 				}
    243 				w = strlen(c->c_name);
    244 				while (w < width) {
    245 					w = (w + 8) &~ 7;
    246 					putchar('\t');
    247 				}
    248 			}
    249 		}
    250 		return;
    251 	}
    252 	while (--argc > 0) {
    253 		char *arg;
    254 		arg = *++argv;
    255 		c = getcmd(arg);
    256 		if (c == (struct cmd *)-1)
    257 			printf("?Ambiguous help command %s\n", arg);
    258 		else if (c == (struct cmd *)0)
    259 			printf("?Invalid help command %s\n", arg);
    260 		else
    261 			printf("%-*s\t%s\n", (int)HELPINDENT,
    262 				c->c_name, c->c_help);
    263 	}
    264 }
    265