lpc.c revision 50042
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 36static 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 43static char sccsid[] = "@(#)lpc.c 8.3 (Berkeley) 4/28/95"; 44#endif 45static const char rcsid[] = 46 "$Id: lpc.c,v 1.9 1999/08/19 03:29:13 mdodd Exp $"; 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 78static int fromatty; 79 80static char cmdline[MAX_CMDLINE]; 81static int margc; 82static char *margv[MAX_MARGV]; 83uid_t uid, euid; 84 85int main __P((int, char *[])); 86static void cmdscanner __P((void)); 87static struct cmd *getcmd __P((char *)); 88static void intr __P((int)); 89static void makeargv __P((void)); 90static int ingroup __P((char *)); 91 92int 93main(argc, argv) 94 int argc; 95 char *argv[]; 96{ 97 register struct cmd *c; 98 99 euid = geteuid(); 100 uid = getuid(); 101 seteuid(uid); 102 name = argv[0]; 103 openlog("lpd", 0, LOG_LPR); 104 105 if (--argc > 0) { 106 c = getcmd(*++argv); 107 if (c == (struct cmd *)-1) { 108 printf("?Ambiguous command\n"); 109 exit(1); 110 } 111 if (c == 0) { 112 printf("?Invalid command\n"); 113 exit(1); 114 } 115 if (c->c_priv && getuid() && ingroup(LPR_OPER) == 0) { 116 printf("?Privileged command\n"); 117 exit(1); 118 } 119 if (c->c_generic != 0) 120 generic(c->c_generic, argc, argv); 121 else 122 (*c->c_handler)(argc, argv); 123 exit(0); 124 } 125 fromatty = isatty(fileno(stdin)); 126 if (!fromatty) 127 signal(SIGINT, intr); 128 for (;;) { 129 cmdscanner(); 130 } 131} 132 133static void 134intr(signo) 135 int signo; 136{ 137 exit(0); 138} 139 140static char * 141lpc_prompt() 142{ 143 return("lpc> "); 144} 145 146/* 147 * Command parser. 148 */ 149static void 150cmdscanner() 151{ 152 register struct cmd *c; 153 static EditLine *el = NULL; 154 static History *hist = NULL; 155 int num = 0; 156 const char *bp = NULL; 157 158 for (;;) { 159 if (fromatty) { 160 if (!el) { 161 el = el_init("lpc", stdin, stdout); 162 hist = history_init(); 163 history(hist, H_EVENT, 100); 164 el_set(el, EL_HIST, history, hist); 165 el_set(el, EL_EDITOR, "emacs"); 166 el_set(el, EL_PROMPT, lpc_prompt); 167 el_set(el, EL_SIGNAL, 1); 168 el_source(el, NULL); 169 } 170 if ((bp = el_gets(el, &num)) == NULL || num == 0) 171 return; 172 173 memcpy(cmdline, bp, (MAX_CMDLINE > num ? MAX_CMDLINE : num)); 174 cmdline[num] = 0; 175 history(hist, H_ENTER, bp); 176 177 } else { 178 if (fgets(cmdline, MAX_CMDLINE, stdin) == 0) 179 quit(0, NULL); 180 if (cmdline[0] == 0 || cmdline[0] == '\n') 181 break; 182 } 183 184 makeargv(); 185 if (margc == 0) 186 continue; 187 if (el_parse(el, margc, margv) != -1) 188 continue; 189 190 c = getcmd(margv[0]); 191 if (c == (struct cmd *)-1) { 192 printf("?Ambiguous command\n"); 193 continue; 194 } 195 if (c == 0) { 196 printf("?Invalid command\n"); 197 continue; 198 } 199 if (c->c_priv && getuid() && ingroup(LPR_OPER) == 0) { 200 printf("?Privileged command\n"); 201 continue; 202 } 203 if (c->c_generic != 0) 204 generic(c->c_generic, margc, margv); 205 else 206 (*c->c_handler)(margc, margv); 207 } 208} 209 210static struct cmd * 211getcmd(name) 212 register char *name; 213{ 214 register char *p, *q; 215 register struct cmd *c, *found; 216 register int nmatches, longest; 217 218 longest = 0; 219 nmatches = 0; 220 found = 0; 221 for (c = cmdtab; (p = c->c_name); c++) { 222 for (q = name; *q == *p++; q++) 223 if (*q == 0) /* exact match? */ 224 return(c); 225 if (!*q) { /* the name was a prefix */ 226 if (q - name > longest) { 227 longest = q - name; 228 nmatches = 1; 229 found = c; 230 } else if (q - name == longest) 231 nmatches++; 232 } 233 } 234 if (nmatches > 1) 235 return((struct cmd *)-1); 236 return(found); 237} 238 239/* 240 * Slice a string up into argc/argv. 241 */ 242static void 243makeargv() 244{ 245 register char *cp; 246 register char **argp = margv; 247 register int n = 0; 248 249 margc = 0; 250 for (cp = cmdline; *cp && (cp - cmdline) < sizeof(cmdline) && 251 n < MAX_MARGV; n++) { 252 while (isspace(*cp)) 253 cp++; 254 if (*cp == '\0') 255 break; 256 *argp++ = cp; 257 margc += 1; 258 while (*cp != '\0' && !isspace(*cp)) 259 cp++; 260 if (*cp == '\0') 261 break; 262 *cp++ = '\0'; 263 } 264 *argp++ = 0; 265} 266 267#define HELPINDENT (sizeof ("directory")) 268 269/* 270 * Help command. 271 */ 272void 273help(argc, argv) 274 int argc; 275 char *argv[]; 276{ 277 register struct cmd *c; 278 279 if (argc == 1) { 280 register int i, j, w; 281 int columns, width = 0, lines; 282 283 printf("Commands may be abbreviated. Commands are:\n\n"); 284 for (c = cmdtab; c->c_name; c++) { 285 int len = strlen(c->c_name); 286 287 if (len > width) 288 width = len; 289 } 290 width = (width + 8) &~ 7; 291 columns = 80 / width; 292 if (columns == 0) 293 columns = 1; 294 lines = (NCMDS + columns - 1) / columns; 295 for (i = 0; i < lines; i++) { 296 for (j = 0; j < columns; j++) { 297 c = cmdtab + j * lines + i; 298 if (c->c_name) 299 printf("%s", c->c_name); 300 if (c + lines >= &cmdtab[NCMDS]) { 301 printf("\n"); 302 break; 303 } 304 w = strlen(c->c_name); 305 while (w < width) { 306 w = (w + 8) &~ 7; 307 putchar('\t'); 308 } 309 } 310 } 311 return; 312 } 313 while (--argc > 0) { 314 register char *arg; 315 arg = *++argv; 316 c = getcmd(arg); 317 if (c == (struct cmd *)-1) 318 printf("?Ambiguous help command %s\n", arg); 319 else if (c == (struct cmd *)0) 320 printf("?Invalid help command %s\n", arg); 321 else 322 printf("%-*s\t%s\n", (int) HELPINDENT, 323 c->c_name, c->c_help); 324 } 325} 326 327/* 328 * return non-zero if the user is a member of the given group 329 */ 330static int 331ingroup(grname) 332 char *grname; 333{ 334 static struct group *gptr=NULL; 335 static gid_t groups[NGROUPS]; 336 register gid_t gid; 337 register int i; 338 339 if (gptr == NULL) { 340 if ((gptr = getgrnam(grname)) == NULL) { 341 warnx("warning: unknown group '%s'", grname); 342 return(0); 343 } 344 if (getgroups(NGROUPS, groups) < 0) 345 err(1, "getgroups"); 346 } 347 gid = gptr->gr_gid; 348 for (i = 0; i < NGROUPS; i++) 349 if (gid == groups[i]) 350 return(1); 351 return(0); 352} 353