1 /* 2 * Copyright (c) 1983, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 3. All advertising materials mentioning features or use of this software 15 * must display the following acknowledgement: 16 * This product includes software developed by the University of 17 * California, Berkeley and its contributors. 18 * 4. Neither the name of the University nor the names of its contributors 19 * may be used to endorse or promote products derived from this software 20 * without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 */ 34 35 #ifndef lint 36 static const char copyright[] = 37 "@(#) Copyright (c) 1983, 1993\n\ 38 The Regents of the University of California. All rights reserved.\n"; 39 #endif /* not lint */ 40 41 #ifndef lint 42 #if 0 43 static char sccsid[] = "@(#)lpc.c 8.3 (Berkeley) 4/28/95"; 44 #endif 45 static const char rcsid[] = 46 "$FreeBSD$"; 47 #endif /* not lint */ 48 49 #include <sys/param.h> 50 51 #include <ctype.h> 52 #include <dirent.h> 53 #include <err.h> 54 #include <grp.h> 55 #include <setjmp.h> 56 #include <signal.h> 57 #include <stdio.h> 58 #include <stdlib.h> 59 #include <syslog.h> 60 #include <string.h> 61 #include <unistd.h> 62 #include <histedit.h> 63 64 #include "lp.h" 65 #include "lpc.h" 66 #include "extern.h" 67 68 #ifndef LPR_OPER 69 #define LPR_OPER "operator" /* group name of lpr operators */ 70 #endif 71 72 /* 73 * lpc -- line printer control program 74 */ 75 76 #define MAX_CMDLINE 200 77 #define MAX_MARGV 20 78 static int fromatty; 79 80 static char cmdline[MAX_CMDLINE]; 81 static int margc; 82 static char *margv[MAX_MARGV]; 83 uid_t uid, euid; 84 85 int main(int _argc, char *_argv[]); 86 static void cmdscanner(void); 87 static struct cmd *getcmd(const char *_name); 88 static void intr(int _signo); 89 static void makeargv(void); 90 static int ingroup(const char *_grname); 91 92 int 93 main(int argc, char *argv[]) 94 { 95 register struct cmd *c; 96 97 euid = geteuid(); 98 uid = getuid(); 99 seteuid(uid); 100 progname = argv[0]; 101 openlog("lpd", 0, LOG_LPR); 102 103 if (--argc > 0) { 104 c = getcmd(*++argv); 105 if (c == (struct cmd *)-1) { 106 printf("?Ambiguous command\n"); 107 exit(1); 108 } 109 if (c == 0) { 110 printf("?Invalid command\n"); 111 exit(1); 112 } 113 if (c->c_priv && getuid() && ingroup(LPR_OPER) == 0) { 114 printf("?Privileged command\n"); 115 exit(1); 116 } 117 if (c->c_generic != 0) 118 generic(c->c_generic, c->c_handler, argc, argv); 119 else 120 (*c->c_handler)(argc, argv); 121 exit(0); 122 } 123 fromatty = isatty(fileno(stdin)); 124 if (!fromatty) 125 signal(SIGINT, intr); 126 for (;;) { 127 cmdscanner(); 128 } 129 } 130 131 static void 132 intr(int signo __unused) 133 { 134 /* (the '__unused' is just to avoid a compile-time warning) */ 135 exit(0); 136 } 137 138 static const char * 139 lpc_prompt(void) 140 { 141 return ("lpc> "); 142 } 143 144 /* 145 * Command parser. 146 */ 147 static void 148 cmdscanner(void) 149 { 150 register struct cmd *c; 151 static EditLine *el; 152 static History *hist; 153 size_t len; 154 int num; 155 const char *bp; 156 157 num = 0; 158 bp = NULL; 159 el = NULL; 160 hist = NULL; 161 for (;;) { 162 if (fromatty) { 163 if (!el) { 164 el = el_init("lpc", stdin, stdout); 165 hist = history_init(); 166 history(hist, H_EVENT, 100); 167 el_set(el, EL_HIST, history, hist); 168 el_set(el, EL_EDITOR, "emacs"); 169 el_set(el, EL_PROMPT, lpc_prompt); 170 el_set(el, EL_SIGNAL, 1); 171 el_source(el, NULL); 172 /* 173 * EditLine init may call 'cgetset()' to set a 174 * capability-db meant for termcap (eg: to set 175 * terminal type 'xterm'). Reset that now, or 176 * that same db-information will be used for 177 * printcap (giving us an "xterm" printer, with 178 * all kinds of invalid capabilities...). 179 */ 180 cgetset(NULL); 181 } 182 if ((bp = el_gets(el, &num)) == NULL || num == 0) 183 quit(0, NULL); 184 185 len = (num > MAX_CMDLINE) ? MAX_CMDLINE : num; 186 memcpy(cmdline, bp, len); 187 cmdline[len] = 0; 188 history(hist, H_ENTER, bp); 189 190 } else { 191 if (fgets(cmdline, MAX_CMDLINE, stdin) == 0) 192 quit(0, NULL); 193 if (cmdline[0] == 0 || cmdline[0] == '\n') 194 break; 195 } 196 197 makeargv(); 198 if (margc == 0) 199 continue; 200 if (el_parse(el, margc, margv) != -1) 201 continue; 202 203 c = getcmd(margv[0]); 204 if (c == (struct cmd *)-1) { 205 printf("?Ambiguous command\n"); 206 continue; 207 } 208 if (c == 0) { 209 printf("?Invalid command\n"); 210 continue; 211 } 212 if (c->c_priv && getuid() && ingroup(LPR_OPER) == 0) { 213 printf("?Privileged command\n"); 214 continue; 215 } 216 217 /* 218 * Two different commands might have the same generic rtn 219 * (eg: "clean" and "tclean"), and just use different 220 * handler routines for distinct command-setup. The handler 221 * routine might also be set on a generic routine for 222 * initial parameter processing. 223 */ 224 if (c->c_generic != 0) 225 generic(c->c_generic, c->c_handler, margc, margv); 226 else 227 (*c->c_handler)(margc, margv); 228 } 229 } 230 231 static struct cmd * 232 getcmd(const char *name) 233 { 234 register const char *p, *q; 235 register struct cmd *c, *found; 236 register int nmatches, longest; 237 238 longest = 0; 239 nmatches = 0; 240 found = 0; 241 for (c = cmdtab; (p = c->c_name); c++) { 242 for (q = name; *q == *p++; q++) 243 if (*q == 0) /* exact match? */ 244 return(c); 245 if (!*q) { /* the name was a prefix */ 246 if (q - name > longest) { 247 longest = q - name; 248 nmatches = 1; 249 found = c; 250 } else if (q - name == longest) 251 nmatches++; 252 } 253 } 254 if (nmatches > 1) 255 return((struct cmd *)-1); 256 return(found); 257 } 258 259 /* 260 * Slice a string up into argc/argv. 261 */ 262 static void 263 makeargv(void) 264 { 265 register char *cp; 266 register char **argp = margv; 267 register int n = 0; 268 269 margc = 0; 270 for (cp = cmdline; *cp && (size_t)(cp - cmdline) < sizeof(cmdline) && 271 n < MAX_MARGV; n++) { 272 while (isspace(*cp)) 273 cp++; 274 if (*cp == '\0') 275 break; 276 *argp++ = cp; 277 margc += 1; 278 while (*cp != '\0' && !isspace(*cp)) 279 cp++; 280 if (*cp == '\0') 281 break; 282 *cp++ = '\0'; 283 } 284 *argp++ = 0; 285 } 286 287 #define HELPINDENT (sizeof ("directory")) 288 289 /* 290 * Help command. 291 */ 292 void 293 help(int argc, char *argv[]) 294 { 295 register struct cmd *c; 296 297 if (argc == 1) { 298 register int i, j, w; 299 int columns, width = 0, lines; 300 301 printf("Commands may be abbreviated. Commands are:\n\n"); 302 for (c = cmdtab; c->c_name; c++) { 303 int len = strlen(c->c_name); 304 305 if (len > width) 306 width = len; 307 } 308 width = (width + 8) &~ 7; 309 columns = 80 / width; 310 if (columns == 0) 311 columns = 1; 312 lines = (NCMDS + columns - 1) / columns; 313 for (i = 0; i < lines; i++) { 314 for (j = 0; j < columns; j++) { 315 c = cmdtab + j * lines + i; 316 if (c->c_name) 317 printf("%s", c->c_name); 318 if (c + lines >= &cmdtab[NCMDS]) { 319 printf("\n"); 320 break; 321 } 322 w = strlen(c->c_name); 323 while (w < width) { 324 w = (w + 8) &~ 7; 325 putchar('\t'); 326 } 327 } 328 } 329 return; 330 } 331 while (--argc > 0) { 332 register char *arg; 333 arg = *++argv; 334 c = getcmd(arg); 335 if (c == (struct cmd *)-1) 336 printf("?Ambiguous help command %s\n", arg); 337 else if (c == (struct cmd *)0) 338 printf("?Invalid help command %s\n", arg); 339 else 340 printf("%-*s\t%s\n", (int) HELPINDENT, 341 c->c_name, c->c_help); 342 } 343 } 344 345 /* 346 * return non-zero if the user is a member of the given group 347 */ 348 static int 349 ingroup(const char *grname) 350 { 351 static struct group *gptr=NULL; 352 static int ngroups = 0; 353 static gid_t groups[NGROUPS]; 354 register gid_t gid; 355 register int i; 356 357 if (gptr == NULL) { 358 if ((gptr = getgrnam(grname)) == NULL) { 359 warnx("warning: unknown group '%s'", grname); 360 return(0); 361 } 362 ngroups = getgroups(NGROUPS, groups); 363 if (ngroups < 0) 364 err(1, "getgroups"); 365 } 366 gid = gptr->gr_gid; 367 for (i = 0; i < ngroups; i++) 368 if (gid == groups[i]) 369 return(1); 370 return(0); 371 } 372