timedc.c revision 1.21 1 1.21 lukem /* $NetBSD: timedc.c,v 1.21 2008/07/21 13:37:00 lukem Exp $ */
2 1.4 mrg
3 1.3 cgd /*-
4 1.3 cgd * Copyright (c) 1985, 1993 The Regents of the University of California.
5 1.1 cgd * All rights reserved.
6 1.1 cgd *
7 1.1 cgd * Redistribution and use in source and binary forms, with or without
8 1.1 cgd * modification, are permitted provided that the following conditions
9 1.1 cgd * are met:
10 1.1 cgd * 1. Redistributions of source code must retain the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer.
12 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 cgd * notice, this list of conditions and the following disclaimer in the
14 1.1 cgd * documentation and/or other materials provided with the distribution.
15 1.10 agc * 3. Neither the name of the University nor the names of its contributors
16 1.1 cgd * may be used to endorse or promote products derived from this software
17 1.1 cgd * without specific prior written permission.
18 1.1 cgd *
19 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 1.1 cgd * SUCH DAMAGE.
30 1.1 cgd */
31 1.1 cgd
32 1.5 lukem #include <sys/cdefs.h>
33 1.1 cgd #ifndef lint
34 1.21 lukem __COPYRIGHT("@(#) Copyright (c) 1985, 1993\
35 1.21 lukem The Regents of the University of California. All rights reserved.");
36 1.1 cgd #endif /* not lint */
37 1.1 cgd
38 1.1 cgd #ifndef lint
39 1.5 lukem #if 0
40 1.4 mrg static char sccsid[] = "@(#)timedc.c 8.1 (Berkeley) 6/6/93";
41 1.5 lukem #else
42 1.21 lukem __RCSID("$NetBSD: timedc.c,v 1.21 2008/07/21 13:37:00 lukem Exp $");
43 1.5 lukem #endif
44 1.1 cgd #endif /* not lint */
45 1.3 cgd
46 1.1 cgd #include "timedc.h"
47 1.1 cgd #include <ctype.h>
48 1.1 cgd #include <setjmp.h>
49 1.6 lukem #include <signal.h>
50 1.3 cgd #include <stdlib.h>
51 1.6 lukem #include <string.h>
52 1.1 cgd #include <syslog.h>
53 1.6 lukem #include <unistd.h>
54 1.18 cbiere #include <fcntl.h>
55 1.18 cbiere #include <pwd.h>
56 1.15 christos #include <err.h>
57 1.1 cgd
58 1.3 cgd int trace = 0;
59 1.3 cgd FILE *fd = 0;
60 1.1 cgd int margc;
61 1.1 cgd int fromatty;
62 1.11 itojun #define MAX_MARGV 20
63 1.11 itojun char *margv[MAX_MARGV];
64 1.1 cgd char cmdline[200];
65 1.1 cgd jmp_buf toplevel;
66 1.20 matt static const struct cmd *getcmd(char *);
67 1.18 cbiere static int drop_privileges(void);
68 1.1 cgd
69 1.3 cgd int
70 1.3 cgd main(int argc, char *argv[])
71 1.1 cgd {
72 1.20 matt const struct cmd *c;
73 1.1 cgd
74 1.18 cbiere fcntl(3, F_CLOSEM);
75 1.7 lukem openlog("timedc", 0, LOG_AUTH);
76 1.1 cgd
77 1.1 cgd /*
78 1.1 cgd * security dictates!
79 1.1 cgd */
80 1.15 christos if (priv_resources() < 0)
81 1.19 cbiere errx(EXIT_FAILURE, "Could not get privileged resources");
82 1.18 cbiere if (drop_privileges() < 0)
83 1.19 cbiere errx(EXIT_FAILURE, "Could not drop privileges");
84 1.1 cgd
85 1.1 cgd if (--argc > 0) {
86 1.1 cgd c = getcmd(*++argv);
87 1.1 cgd if (c == (struct cmd *)-1) {
88 1.1 cgd printf("?Ambiguous command\n");
89 1.19 cbiere exit(EXIT_FAILURE);
90 1.1 cgd }
91 1.1 cgd if (c == 0) {
92 1.1 cgd printf("?Invalid command\n");
93 1.19 cbiere exit(EXIT_FAILURE);
94 1.1 cgd }
95 1.1 cgd (*c->c_handler)(argc, argv);
96 1.19 cbiere exit(EXIT_SUCCESS);
97 1.1 cgd }
98 1.3 cgd
99 1.1 cgd fromatty = isatty(fileno(stdin));
100 1.3 cgd if (setjmp(toplevel))
101 1.1 cgd putchar('\n');
102 1.3 cgd (void) signal(SIGINT, intr);
103 1.1 cgd for (;;) {
104 1.1 cgd if (fromatty) {
105 1.1 cgd printf("timedc> ");
106 1.1 cgd (void) fflush(stdout);
107 1.1 cgd }
108 1.1 cgd if (fgets(cmdline, sizeof(cmdline), stdin) == 0)
109 1.5 lukem quit(0, NULL);
110 1.1 cgd if (cmdline[0] == 0)
111 1.1 cgd break;
112 1.11 itojun if (makeargv()) {
113 1.11 itojun printf("?Too many arguments\n");
114 1.11 itojun continue;
115 1.11 itojun }
116 1.3 cgd if (margv[0] == 0)
117 1.3 cgd continue;
118 1.1 cgd c = getcmd(margv[0]);
119 1.1 cgd if (c == (struct cmd *)-1) {
120 1.1 cgd printf("?Ambiguous command\n");
121 1.1 cgd continue;
122 1.1 cgd }
123 1.1 cgd if (c == 0) {
124 1.1 cgd printf("?Invalid command\n");
125 1.1 cgd continue;
126 1.1 cgd }
127 1.1 cgd (*c->c_handler)(margc, margv);
128 1.1 cgd }
129 1.3 cgd return 0;
130 1.3 cgd }
131 1.3 cgd
132 1.3 cgd void
133 1.9 wiz intr(int signo)
134 1.3 cgd {
135 1.16 cbiere (void) signo;
136 1.3 cgd if (!fromatty)
137 1.19 cbiere exit(EXIT_SUCCESS);
138 1.3 cgd longjmp(toplevel, 1);
139 1.1 cgd }
140 1.1 cgd
141 1.3 cgd
142 1.20 matt static const struct cmd *
143 1.3 cgd getcmd(char *name)
144 1.1 cgd {
145 1.17 christos const char *p;
146 1.17 christos char *q;
147 1.20 matt const struct cmd *c, *found;
148 1.13 perry int nmatches, longest;
149 1.20 matt extern const struct cmd cmdtab[];
150 1.1 cgd extern int NCMDS;
151 1.1 cgd
152 1.1 cgd longest = 0;
153 1.1 cgd nmatches = 0;
154 1.1 cgd found = 0;
155 1.1 cgd for (c = cmdtab; c < &cmdtab[NCMDS]; c++) {
156 1.1 cgd p = c->c_name;
157 1.1 cgd for (q = name; *q == *p++; q++)
158 1.1 cgd if (*q == 0) /* exact match? */
159 1.1 cgd return(c);
160 1.1 cgd if (!*q) { /* the name was a prefix */
161 1.1 cgd if (q - name > longest) {
162 1.1 cgd longest = q - name;
163 1.1 cgd nmatches = 1;
164 1.1 cgd found = c;
165 1.1 cgd } else if (q - name == longest)
166 1.1 cgd nmatches++;
167 1.1 cgd }
168 1.1 cgd }
169 1.1 cgd if (nmatches > 1)
170 1.1 cgd return((struct cmd *)-1);
171 1.1 cgd return(found);
172 1.1 cgd }
173 1.1 cgd
174 1.1 cgd /*
175 1.1 cgd * Slice a string up into argc/argv.
176 1.1 cgd */
177 1.11 itojun int
178 1.9 wiz makeargv(void)
179 1.1 cgd {
180 1.13 perry char *cp;
181 1.13 perry char **argp = margv;
182 1.1 cgd
183 1.1 cgd margc = 0;
184 1.14 christos for (cp = cmdline; argp < &margv[MAX_MARGV - 1] && *cp;) {
185 1.12 dsl while (isspace((unsigned char)*cp))
186 1.1 cgd cp++;
187 1.1 cgd if (*cp == '\0')
188 1.1 cgd break;
189 1.1 cgd *argp++ = cp;
190 1.1 cgd margc += 1;
191 1.12 dsl while (*cp != '\0' && !isspace((unsigned char)*cp))
192 1.1 cgd cp++;
193 1.1 cgd if (*cp == '\0')
194 1.1 cgd break;
195 1.1 cgd *cp++ = '\0';
196 1.1 cgd }
197 1.11 itojun if (margc == MAX_MARGV - 1)
198 1.11 itojun return 1;
199 1.1 cgd *argp++ = 0;
200 1.11 itojun return 0;
201 1.1 cgd }
202 1.1 cgd
203 1.1 cgd #define HELPINDENT (sizeof ("directory"))
204 1.1 cgd
205 1.1 cgd /*
206 1.1 cgd * Help command.
207 1.1 cgd */
208 1.3 cgd void
209 1.9 wiz help(int argc, char *argv[])
210 1.1 cgd {
211 1.20 matt const struct cmd *c;
212 1.20 matt extern const struct cmd cmdtab[];
213 1.1 cgd
214 1.1 cgd if (argc == 1) {
215 1.13 perry int i, j, w;
216 1.1 cgd int columns, width = 0, lines;
217 1.1 cgd extern int NCMDS;
218 1.1 cgd
219 1.1 cgd printf("Commands may be abbreviated. Commands are:\n\n");
220 1.1 cgd for (c = cmdtab; c < &cmdtab[NCMDS]; c++) {
221 1.1 cgd int len = strlen(c->c_name);
222 1.1 cgd
223 1.1 cgd if (len > width)
224 1.1 cgd width = len;
225 1.1 cgd }
226 1.1 cgd width = (width + 8) &~ 7;
227 1.1 cgd columns = 80 / width;
228 1.1 cgd if (columns == 0)
229 1.1 cgd columns = 1;
230 1.1 cgd lines = (NCMDS + columns - 1) / columns;
231 1.1 cgd for (i = 0; i < lines; i++) {
232 1.1 cgd for (j = 0; j < columns; j++) {
233 1.1 cgd c = cmdtab + j * lines + i;
234 1.1 cgd printf("%s", c->c_name);
235 1.1 cgd if (c + lines >= &cmdtab[NCMDS]) {
236 1.1 cgd printf("\n");
237 1.1 cgd break;
238 1.1 cgd }
239 1.1 cgd w = strlen(c->c_name);
240 1.1 cgd while (w < width) {
241 1.1 cgd w = (w + 8) &~ 7;
242 1.1 cgd putchar('\t');
243 1.1 cgd }
244 1.1 cgd }
245 1.1 cgd }
246 1.1 cgd return;
247 1.1 cgd }
248 1.1 cgd while (--argc > 0) {
249 1.13 perry char *arg;
250 1.1 cgd arg = *++argv;
251 1.1 cgd c = getcmd(arg);
252 1.1 cgd if (c == (struct cmd *)-1)
253 1.1 cgd printf("?Ambiguous help command %s\n", arg);
254 1.1 cgd else if (c == (struct cmd *)0)
255 1.1 cgd printf("?Invalid help command %s\n", arg);
256 1.1 cgd else
257 1.3 cgd printf("%-*s\t%s\n", (int)HELPINDENT,
258 1.1 cgd c->c_name, c->c_help);
259 1.1 cgd }
260 1.1 cgd }
261 1.18 cbiere
262 1.18 cbiere static int
263 1.18 cbiere drop_privileges(void)
264 1.18 cbiere {
265 1.19 cbiere static const char user[] = "_timedc";
266 1.18 cbiere const struct passwd *pw;
267 1.18 cbiere uid_t uid;
268 1.18 cbiere gid_t gid;
269 1.18 cbiere
270 1.19 cbiere if ((pw = getpwnam(user)) == NULL) {
271 1.19 cbiere warnx("getpwnam(\"%s\") failed", user);
272 1.18 cbiere return -1;
273 1.18 cbiere }
274 1.18 cbiere uid = pw->pw_uid;
275 1.18 cbiere gid = pw->pw_gid;
276 1.18 cbiere if (setgroups(1, &gid)) {
277 1.18 cbiere warn("setgroups");
278 1.18 cbiere return -1;
279 1.18 cbiere }
280 1.18 cbiere if (setgid(gid)) {
281 1.18 cbiere warn("setgid");
282 1.18 cbiere return -1;
283 1.18 cbiere }
284 1.18 cbiere if (setuid(uid)) {
285 1.18 cbiere warn("setuid");
286 1.18 cbiere return -1;
287 1.18 cbiere }
288 1.18 cbiere return 0;
289 1.18 cbiere }
290