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