1 /* $NetBSD: timedc.c,v 1.6 1997/10/18 07:13:35 lukem 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.6 1997/10/18 07:13:35 lukem Exp $"); 48 #endif 49 #endif /* not lint */ 50 51 #ifdef sgi 52 #ident "$Revision: 1.6 $" 53 #endif 54 55 #include "timedc.h" 56 #include <ctype.h> 57 #include <setjmp.h> 58 #include <signal.h> 59 #include <stdlib.h> 60 #include <string.h> 61 #include <syslog.h> 62 #include <unistd.h> 63 64 int trace = 0; 65 FILE *fd = 0; 66 int margc; 67 int fromatty; 68 char *margv[20]; 69 char cmdline[200]; 70 jmp_buf toplevel; 71 static struct cmd *getcmd(char *); 72 73 int 74 main(int argc, char *argv[]) 75 { 76 register struct cmd *c; 77 78 openlog("timedc", LOG_ODELAY, LOG_AUTH); 79 80 /* 81 * security dictates! 82 */ 83 if (priv_resources() < 0) { 84 fprintf(stderr, "Could not get privileged resources\n"); 85 exit(1); 86 } 87 (void) setuid(getuid()); 88 89 if (--argc > 0) { 90 c = getcmd(*++argv); 91 if (c == (struct cmd *)-1) { 92 printf("?Ambiguous command\n"); 93 exit(1); 94 } 95 if (c == 0) { 96 printf("?Invalid command\n"); 97 exit(1); 98 } 99 if (c->c_priv && getuid()) { 100 printf("?Privileged command\n"); 101 exit(1); 102 } 103 (*c->c_handler)(argc, argv); 104 exit(0); 105 } 106 107 fromatty = isatty(fileno(stdin)); 108 if (setjmp(toplevel)) 109 putchar('\n'); 110 (void) signal(SIGINT, intr); 111 for (;;) { 112 if (fromatty) { 113 printf("timedc> "); 114 (void) fflush(stdout); 115 } 116 if (fgets(cmdline, sizeof(cmdline), stdin) == 0) 117 quit(0, NULL); 118 if (cmdline[0] == 0) 119 break; 120 makeargv(); 121 if (margv[0] == 0) 122 continue; 123 c = getcmd(margv[0]); 124 if (c == (struct cmd *)-1) { 125 printf("?Ambiguous command\n"); 126 continue; 127 } 128 if (c == 0) { 129 printf("?Invalid command\n"); 130 continue; 131 } 132 if (c->c_priv && getuid()) { 133 printf("?Privileged command\n"); 134 continue; 135 } 136 (*c->c_handler)(margc, margv); 137 } 138 return 0; 139 } 140 141 void 142 intr(signo) 143 int signo; 144 { 145 if (!fromatty) 146 exit(0); 147 longjmp(toplevel, 1); 148 } 149 150 151 static struct cmd * 152 getcmd(char *name) 153 { 154 register char *p, *q; 155 register struct cmd *c, *found; 156 register int nmatches, longest; 157 extern struct cmd cmdtab[]; 158 extern int NCMDS; 159 160 longest = 0; 161 nmatches = 0; 162 found = 0; 163 for (c = cmdtab; c < &cmdtab[NCMDS]; c++) { 164 p = c->c_name; 165 for (q = name; *q == *p++; q++) 166 if (*q == 0) /* exact match? */ 167 return(c); 168 if (!*q) { /* the name was a prefix */ 169 if (q - name > longest) { 170 longest = q - name; 171 nmatches = 1; 172 found = c; 173 } else if (q - name == longest) 174 nmatches++; 175 } 176 } 177 if (nmatches > 1) 178 return((struct cmd *)-1); 179 return(found); 180 } 181 182 /* 183 * Slice a string up into argc/argv. 184 */ 185 void 186 makeargv() 187 { 188 register char *cp; 189 register char **argp = margv; 190 191 margc = 0; 192 for (cp = cmdline; *cp;) { 193 while (isspace(*cp)) 194 cp++; 195 if (*cp == '\0') 196 break; 197 *argp++ = cp; 198 margc += 1; 199 while (*cp != '\0' && !isspace(*cp)) 200 cp++; 201 if (*cp == '\0') 202 break; 203 *cp++ = '\0'; 204 } 205 *argp++ = 0; 206 } 207 208 #define HELPINDENT (sizeof ("directory")) 209 210 /* 211 * Help command. 212 */ 213 void 214 help(argc, argv) 215 int argc; 216 char *argv[]; 217 { 218 register struct cmd *c; 219 extern struct cmd cmdtab[]; 220 221 if (argc == 1) { 222 register int i, j, w; 223 int columns, width = 0, lines; 224 extern int NCMDS; 225 226 printf("Commands may be abbreviated. Commands are:\n\n"); 227 for (c = cmdtab; c < &cmdtab[NCMDS]; c++) { 228 int len = strlen(c->c_name); 229 230 if (len > width) 231 width = len; 232 } 233 width = (width + 8) &~ 7; 234 columns = 80 / width; 235 if (columns == 0) 236 columns = 1; 237 lines = (NCMDS + columns - 1) / columns; 238 for (i = 0; i < lines; i++) { 239 for (j = 0; j < columns; j++) { 240 c = cmdtab + j * lines + i; 241 printf("%s", c->c_name); 242 if (c + lines >= &cmdtab[NCMDS]) { 243 printf("\n"); 244 break; 245 } 246 w = strlen(c->c_name); 247 while (w < width) { 248 w = (w + 8) &~ 7; 249 putchar('\t'); 250 } 251 } 252 } 253 return; 254 } 255 while (--argc > 0) { 256 register char *arg; 257 arg = *++argv; 258 c = getcmd(arg); 259 if (c == (struct cmd *)-1) 260 printf("?Ambiguous help command %s\n", arg); 261 else if (c == (struct cmd *)0) 262 printf("?Invalid help command %s\n", arg); 263 else 264 printf("%-*s\t%s\n", (int)HELPINDENT, 265 c->c_name, c->c_help); 266 } 267 } 268