xref: /netbsd-src/usr.sbin/timed/timedc/timedc.c (revision 23c8222edbfb0f0932d88a8351d3a0cf817dfb9e)
1 /*	$NetBSD: timedc.c,v 1.12 2004/10/30 15:54:29 dsl 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.12 2004/10/30 15:54:29 dsl 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 #define MAX_MARGV	20
61 char	*margv[MAX_MARGV];
62 char	cmdline[200];
63 jmp_buf	toplevel;
64 static struct cmd *getcmd(char *);
65 
66 int
67 main(int argc, char *argv[])
68 {
69 	register struct cmd *c;
70 
71 	openlog("timedc", 0, LOG_AUTH);
72 
73 	/*
74 	 * security dictates!
75 	 */
76 	if (priv_resources() < 0) {
77 		fprintf(stderr, "Could not get privileged resources\n");
78 		exit(1);
79 	}
80 	(void) setuid(getuid());
81 
82 	if (--argc > 0) {
83 		c = getcmd(*++argv);
84 		if (c == (struct cmd *)-1) {
85 			printf("?Ambiguous command\n");
86 			exit(1);
87 		}
88 		if (c == 0) {
89 			printf("?Invalid command\n");
90 			exit(1);
91 		}
92 		if (c->c_priv && getuid()) {
93 			printf("?Privileged command\n");
94 			exit(1);
95 		}
96 		(*c->c_handler)(argc, argv);
97 		exit(0);
98 	}
99 
100 	fromatty = isatty(fileno(stdin));
101 	if (setjmp(toplevel))
102 		putchar('\n');
103 	(void) signal(SIGINT, intr);
104 	for (;;) {
105 		if (fromatty) {
106 			printf("timedc> ");
107 			(void) fflush(stdout);
108 		}
109 		if (fgets(cmdline, sizeof(cmdline), stdin) == 0)
110 			quit(0, NULL);
111 		if (cmdline[0] == 0)
112 			break;
113 		if (makeargv()) {
114 			printf("?Too many arguments\n");
115 			continue;
116 		}
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(int signo)
139 {
140 	if (!fromatty)
141 		exit(0);
142 	longjmp(toplevel, 1);
143 }
144 
145 
146 static struct cmd *
147 getcmd(char *name)
148 {
149 	register char *p, *q;
150 	register struct cmd *c, *found;
151 	register int nmatches, longest;
152 	extern struct cmd cmdtab[];
153 	extern int NCMDS;
154 
155 	longest = 0;
156 	nmatches = 0;
157 	found = 0;
158 	for (c = cmdtab; c < &cmdtab[NCMDS]; c++) {
159 		p = c->c_name;
160 		for (q = name; *q == *p++; q++)
161 			if (*q == 0)		/* exact match? */
162 				return(c);
163 		if (!*q) {			/* the name was a prefix */
164 			if (q - name > longest) {
165 				longest = q - name;
166 				nmatches = 1;
167 				found = c;
168 			} else if (q - name == longest)
169 				nmatches++;
170 		}
171 	}
172 	if (nmatches > 1)
173 		return((struct cmd *)-1);
174 	return(found);
175 }
176 
177 /*
178  * Slice a string up into argc/argv.
179  */
180 int
181 makeargv(void)
182 {
183 	register char *cp;
184 	register char **argp = margv;
185 
186 	margc = 0;
187 	for (cp = cmdline; cp < margv[MAX_MARGV - 1] && *cp;) {
188 		while (isspace((unsigned char)*cp))
189 			cp++;
190 		if (*cp == '\0')
191 			break;
192 		*argp++ = cp;
193 		margc += 1;
194 		while (*cp != '\0' && !isspace((unsigned char)*cp))
195 			cp++;
196 		if (*cp == '\0')
197 			break;
198 		*cp++ = '\0';
199 	}
200 	if (margc == MAX_MARGV - 1)
201 		return 1;
202 	*argp++ = 0;
203 	return 0;
204 }
205 
206 #define HELPINDENT (sizeof ("directory"))
207 
208 /*
209  * Help command.
210  */
211 void
212 help(int argc, 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