1 /* $NetBSD: timedc.c,v 1.9 2002/07/06 22:01:41 wiz 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. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed by the University of 18 * California, Berkeley and its contributors. 19 * 4. Neither the name of the University nor the names of its contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 */ 35 36 #include <sys/cdefs.h> 37 #ifndef lint 38 __COPYRIGHT( 39 "@(#) Copyright (c) 1985, 1993 The Regents of the University of California.\n\ 40 All rights reserved.\n"); 41 #endif /* not lint */ 42 43 #ifndef lint 44 #if 0 45 static char sccsid[] = "@(#)timedc.c 8.1 (Berkeley) 6/6/93"; 46 #else 47 __RCSID("$NetBSD: timedc.c,v 1.9 2002/07/06 22:01:41 wiz Exp $"); 48 #endif 49 #endif /* not lint */ 50 51 #include "timedc.h" 52 #include <ctype.h> 53 #include <setjmp.h> 54 #include <signal.h> 55 #include <stdlib.h> 56 #include <string.h> 57 #include <syslog.h> 58 #include <unistd.h> 59 60 int trace = 0; 61 FILE *fd = 0; 62 int margc; 63 int fromatty; 64 char *margv[20]; 65 char cmdline[200]; 66 jmp_buf toplevel; 67 static struct cmd *getcmd(char *); 68 69 int 70 main(int argc, char *argv[]) 71 { 72 register struct cmd *c; 73 74 openlog("timedc", 0, LOG_AUTH); 75 76 /* 77 * security dictates! 78 */ 79 if (priv_resources() < 0) { 80 fprintf(stderr, "Could not get privileged resources\n"); 81 exit(1); 82 } 83 (void) setuid(getuid()); 84 85 if (--argc > 0) { 86 c = getcmd(*++argv); 87 if (c == (struct cmd *)-1) { 88 printf("?Ambiguous command\n"); 89 exit(1); 90 } 91 if (c == 0) { 92 printf("?Invalid command\n"); 93 exit(1); 94 } 95 if (c->c_priv && getuid()) { 96 printf("?Privileged command\n"); 97 exit(1); 98 } 99 (*c->c_handler)(argc, argv); 100 exit(0); 101 } 102 103 fromatty = isatty(fileno(stdin)); 104 if (setjmp(toplevel)) 105 putchar('\n'); 106 (void) signal(SIGINT, intr); 107 for (;;) { 108 if (fromatty) { 109 printf("timedc> "); 110 (void) fflush(stdout); 111 } 112 if (fgets(cmdline, sizeof(cmdline), stdin) == 0) 113 quit(0, NULL); 114 if (cmdline[0] == 0) 115 break; 116 makeargv(); 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 void 181 makeargv(void) 182 { 183 register char *cp; 184 register char **argp = margv; 185 186 margc = 0; 187 for (cp = cmdline; *cp;) { 188 while (isspace(*cp)) 189 cp++; 190 if (*cp == '\0') 191 break; 192 *argp++ = cp; 193 margc += 1; 194 while (*cp != '\0' && !isspace(*cp)) 195 cp++; 196 if (*cp == '\0') 197 break; 198 *cp++ = '\0'; 199 } 200 *argp++ = 0; 201 } 202 203 #define HELPINDENT (sizeof ("directory")) 204 205 /* 206 * Help command. 207 */ 208 void 209 help(int argc, char *argv[]) 210 { 211 register struct cmd *c; 212 extern struct cmd cmdtab[]; 213 214 if (argc == 1) { 215 register int i, j, w; 216 int columns, width = 0, lines; 217 extern int NCMDS; 218 219 printf("Commands may be abbreviated. Commands are:\n\n"); 220 for (c = cmdtab; c < &cmdtab[NCMDS]; c++) { 221 int len = strlen(c->c_name); 222 223 if (len > width) 224 width = len; 225 } 226 width = (width + 8) &~ 7; 227 columns = 80 / width; 228 if (columns == 0) 229 columns = 1; 230 lines = (NCMDS + columns - 1) / columns; 231 for (i = 0; i < lines; i++) { 232 for (j = 0; j < columns; j++) { 233 c = cmdtab + j * lines + i; 234 printf("%s", c->c_name); 235 if (c + lines >= &cmdtab[NCMDS]) { 236 printf("\n"); 237 break; 238 } 239 w = strlen(c->c_name); 240 while (w < width) { 241 w = (w + 8) &~ 7; 242 putchar('\t'); 243 } 244 } 245 } 246 return; 247 } 248 while (--argc > 0) { 249 register char *arg; 250 arg = *++argv; 251 c = getcmd(arg); 252 if (c == (struct cmd *)-1) 253 printf("?Ambiguous help command %s\n", arg); 254 else if (c == (struct cmd *)0) 255 printf("?Invalid help command %s\n", arg); 256 else 257 printf("%-*s\t%s\n", (int)HELPINDENT, 258 c->c_name, c->c_help); 259 } 260 } 261