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