1 /* $OpenBSD: timedc.c,v 1.13 2004/01/23 21:07:38 deraadt 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 #ifndef lint
33 char copyright[] =
34 "@(#) Copyright (c) 1985, 1993 The Regents of the University of California.\n\
35 All rights reserved.\n";
36 #endif /* not lint */
37
38 #ifndef lint
39 static char sccsid[] = "@(#)timedc.c 5.1 (Berkeley) 5/11/93";
40 #endif /* not lint */
41
42 #include "timedc.h"
43 #include <string.h>
44 #include <signal.h>
45 #include <ctype.h>
46 #include <unistd.h>
47 #include <stdlib.h>
48 #include <syslog.h>
49
50 int trace = 0;
51 FILE *fd = NULL;
52 int margc;
53 int fromatty;
54 #define MAX_MARGV 20
55 char *margv[MAX_MARGV];
56 char cmdline[200];
57
58 static struct cmd *getcmd(char *);
59 volatile sig_atomic_t gotintr;
60
61 int
main(int argc,char * argv[])62 main(int argc, char *argv[])
63 {
64 extern int sock_raw, sock;
65 struct sockaddr_in sin;
66 struct cmd *c;
67
68 sock_raw = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
69 if (sock_raw < 0) {
70 perror("opening raw socket");
71 exit(1);
72 }
73
74 openlog("timedc", LOG_ODELAY, LOG_AUTH);
75
76 sock = socket(AF_INET, SOCK_DGRAM, 0);
77 if (sock < 0) {
78 perror("opening socket");
79 (void)close(sock_raw);
80 return (-1);
81 }
82
83 memset(&sin, 0, sizeof sin);
84 sin.sin_family = AF_INET;
85 sin.sin_addr.s_addr = INADDR_ANY;
86 if (bind(sock, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
87 fprintf(stderr, "all reserved ports in use\n");
88 (void)close(sock_raw);
89 exit(1);
90 }
91
92 if (--argc > 0) {
93 c = getcmd(*++argv);
94 if (c == (struct cmd *)-1) {
95 printf("?Ambiguous command\n");
96 exit(1);
97 }
98 if (c == 0) {
99 printf("?Invalid command\n");
100 exit(1);
101 }
102 if (c->c_priv && getuid()) {
103 printf("?Privileged command\n");
104 exit(1);
105 }
106 (*c->c_handler)(argc, argv);
107 exit(0);
108 }
109
110 fromatty = isatty(fileno(stdin));
111 (void) signal(SIGINT, sigintr);
112 for (;;) {
113 if (gotintr) {
114 putchar('\n');
115 gotintr = 0;
116 }
117 if (fromatty) {
118 printf("timedc> ");
119 (void) fflush(stdout);
120 }
121
122 siginterrupt(SIGINT, 1);
123 if (fgets(cmdline, sizeof(cmdline), stdin) == NULL) {
124 if (errno == EINTR && gotintr) {
125 siginterrupt(SIGINT, 0);
126 continue;
127 }
128 quit(0, NULL);
129 }
130 siginterrupt(SIGINT, 0);
131
132 if (cmdline[0] == 0)
133 break;
134 if (makeargv()) {
135 printf("?Too many arguments\n");
136 continue;
137 }
138 if (margv[0] == 0)
139 continue;
140 c = getcmd(margv[0]);
141 if (c == (struct cmd *)-1) {
142 printf("?Ambiguous command\n");
143 continue;
144 }
145 if (c == 0) {
146 printf("?Invalid command\n");
147 continue;
148 }
149 if (c->c_priv && getuid()) {
150 printf("?Privileged command\n");
151 continue;
152 }
153 (*c->c_handler)(margc, margv);
154 }
155 return 0;
156 }
157
158 void
sigintr(int signo)159 sigintr(int signo)
160 {
161 if (!fromatty)
162 _exit(0);
163 gotintr = 1;
164 }
165
166 static struct cmd *
getcmd(char * name)167 getcmd(char *name)
168 {
169 char *p, *q;
170 struct cmd *c, *found;
171 int nmatches, longest;
172 extern struct cmd cmdtab[];
173 extern int NCMDS;
174
175 longest = 0;
176 nmatches = 0;
177 found = 0;
178 for (c = cmdtab; c < &cmdtab[NCMDS]; c++) {
179 p = c->c_name;
180 for (q = name; *q == *p++; q++)
181 if (*q == 0) /* exact match? */
182 return (c);
183 if (!*q) { /* the name was a prefix */
184 if (q - name > longest) {
185 longest = q - name;
186 nmatches = 1;
187 found = c;
188 } else if (q - name == longest)
189 nmatches++;
190 }
191 }
192 if (nmatches > 1)
193 return ((struct cmd *)-1);
194 return (found);
195 }
196
197 /*
198 * Slice a string up into argc/argv.
199 */
200 int
makeargv(void)201 makeargv(void)
202 {
203 char **argp = margv;
204 char *cp;
205
206 margc = 0;
207 for (cp = cmdline; margc < MAX_MARGV - 1 && *cp; ) {
208 while (isspace(*cp))
209 cp++;
210 if (*cp == '\0')
211 break;
212 *argp++ = cp;
213 margc += 1;
214 while (*cp != '\0' && !isspace(*cp))
215 cp++;
216 if (*cp == '\0')
217 break;
218 *cp++ = '\0';
219 }
220 if (margc == MAX_MARGV - 1)
221 return 1;
222 *argp++ = 0;
223 return 0;
224 }
225
226 #define HELPINDENT (sizeof ("directory"))
227
228 /*
229 * Help command.
230 */
231 void
help(int argc,char * argv[])232 help(int argc, char *argv[])
233 {
234 extern struct cmd cmdtab[];
235 struct cmd *c;
236
237 if (argc == 1) {
238 int columns, width = 0, lines;
239 extern int NCMDS;
240 int i, j, w;
241
242 printf("Commands may be abbreviated. Commands are:\n\n");
243 for (c = cmdtab; c < &cmdtab[NCMDS]; c++) {
244 int len = strlen(c->c_name);
245
246 if (len > width)
247 width = len;
248 }
249 width = (width + 8) &~ 7;
250 columns = 80 / width;
251 if (columns == 0)
252 columns = 1;
253 lines = (NCMDS + columns - 1) / columns;
254 for (i = 0; i < lines; i++) {
255 for (j = 0; j < columns; j++) {
256 c = cmdtab + j * lines + i;
257 printf("%s", c->c_name);
258 if (c + lines >= &cmdtab[NCMDS]) {
259 printf("\n");
260 break;
261 }
262 w = strlen(c->c_name);
263 while (w < width) {
264 w = (w + 8) &~ 7;
265 putchar('\t');
266 }
267 }
268 }
269 return;
270 }
271 while (--argc > 0) {
272 char *arg;
273 arg = *++argv;
274 c = getcmd(arg);
275 if (c == (struct cmd *)-1)
276 printf("?Ambiguous help command %s\n", arg);
277 else if (c == (struct cmd *)0)
278 printf("?Invalid help command %s\n", arg);
279 else
280 printf("%-*s\t%s\n", (int)HELPINDENT,
281 c->c_name, c->c_help);
282 }
283 }
284