lpc.c revision 100203
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  "$FreeBSD: head/usr.sbin/lpr/lpc/lpc.c 100203 2002-07-17 00:51:19Z gad $";
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(int _argc, char *_argv[]);
86static void		 cmdscanner(void);
87static struct cmd	*getcmd(const char *_name);
88static void		 intr(int _signo);
89static void		 makeargv(void);
90static int		 ingroup(const char *_grname);
91
92int
93main(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_opts & LPC_PRIVCMD) && getuid() &&
114		    ingroup(LPR_OPER) == 0) {
115			printf("?Privileged command\n");
116			exit(1);
117		}
118		if (c->c_generic != 0)
119			generic(c->c_generic, c->c_opts, c->c_handler,
120			    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(int signo __unused)
135{
136	/* (the '__unused' is just to avoid a compile-time warning) */
137	exit(0);
138}
139
140static const char *
141lpc_prompt(void)
142{
143	return ("lpc> ");
144}
145
146/*
147 * Command parser.
148 */
149static void
150cmdscanner(void)
151{
152	register struct cmd *c;
153	static EditLine *el;
154	static History *hist;
155	HistEvent he;
156	size_t len;
157	int num;
158	const char *bp;
159
160	num = 0;
161	bp = NULL;
162	el = NULL;
163	hist = NULL;
164	for (;;) {
165		if (fromatty) {
166			if (!el) {
167				el = el_init("lpc", stdin, stdout, stderr);
168				hist = history_init();
169				history(hist, &he, H_EVENT, 100);
170				el_set(el, EL_HIST, history, hist);
171				el_set(el, EL_EDITOR, "emacs");
172				el_set(el, EL_PROMPT, lpc_prompt);
173				el_set(el, EL_SIGNAL, 1);
174				el_source(el, NULL);
175				/*
176				 * EditLine init may call 'cgetset()' to set a
177				 * capability-db meant for termcap (eg: to set
178				 * terminal type 'xterm').  Reset that now, or
179				 * that same db-information will be used for
180				 * printcap (giving us an "xterm" printer, with
181				 * all kinds of invalid capabilities...).
182				 */
183				cgetset(NULL);
184			}
185			if ((bp = el_gets(el, &num)) == NULL || num == 0)
186				quit(0, NULL);
187
188			len = (num > MAX_CMDLINE) ? MAX_CMDLINE : num;
189			memcpy(cmdline, bp, len);
190			cmdline[len] = 0;
191			history(hist, &he, H_ENTER, bp);
192
193		} else {
194			if (fgets(cmdline, MAX_CMDLINE, stdin) == 0)
195				quit(0, NULL);
196			if (cmdline[0] == 0 || cmdline[0] == '\n')
197				break;
198		}
199
200		makeargv();
201		if (margc == 0)
202			continue;
203		if (el_parse(el, margc, margv) != -1)
204			continue;
205
206		c = getcmd(margv[0]);
207		if (c == (struct cmd *)-1) {
208			printf("?Ambiguous command\n");
209			continue;
210		}
211		if (c == 0) {
212			printf("?Invalid command\n");
213			continue;
214		}
215		if ((c->c_opts & LPC_PRIVCMD) && getuid() &&
216		    ingroup(LPR_OPER) == 0) {
217			printf("?Privileged command\n");
218			continue;
219		}
220
221		/*
222		 * Two different commands might have the same generic rtn
223		 * (eg: "clean" and "tclean"), and just use different
224		 * handler routines for distinct command-setup.  The handler
225		 * routine might also be set on a generic routine for
226		 * initial parameter processing.
227		 */
228		if (c->c_generic != 0)
229			generic(c->c_generic, c->c_opts, c->c_handler,
230			    margc, margv);
231		else
232			(*c->c_handler)(margc, margv);
233	}
234}
235
236static struct cmd *
237getcmd(const char *name)
238{
239	register const char *p, *q;
240	register struct cmd *c, *found;
241	register int nmatches, longest;
242
243	longest = 0;
244	nmatches = 0;
245	found = 0;
246	for (c = cmdtab; (p = c->c_name); c++) {
247		for (q = name; *q == *p++; q++)
248			if (*q == 0)		/* exact match? */
249				return(c);
250		if (!*q) {			/* the name was a prefix */
251			if (q - name > longest) {
252				longest = q - name;
253				nmatches = 1;
254				found = c;
255			} else if (q - name == longest)
256				nmatches++;
257		}
258	}
259	if (nmatches > 1)
260		return((struct cmd *)-1);
261	return(found);
262}
263
264/*
265 * Slice a string up into argc/argv.
266 */
267static void
268makeargv(void)
269{
270	register char *cp;
271	register char **argp = margv;
272	register int n = 0;
273
274	margc = 0;
275	for (cp = cmdline; *cp && (size_t)(cp - cmdline) < sizeof(cmdline) &&
276	    n < MAX_MARGV; n++) {
277		while (isspace(*cp))
278			cp++;
279		if (*cp == '\0')
280			break;
281		*argp++ = cp;
282		margc += 1;
283		while (*cp != '\0' && !isspace(*cp))
284			cp++;
285		if (*cp == '\0')
286			break;
287		*cp++ = '\0';
288	}
289	*argp++ = 0;
290}
291
292#define HELPINDENT (sizeof ("directory"))
293
294/*
295 * Help command.
296 */
297void
298help(int argc, char *argv[])
299{
300	register struct cmd *c;
301
302	if (argc == 1) {
303		register int i, j, w;
304		int columns, width = 0, lines;
305
306		printf("Commands may be abbreviated.  Commands are:\n\n");
307		for (c = cmdtab; c->c_name; c++) {
308			int len = strlen(c->c_name);
309
310			if (len > width)
311				width = len;
312		}
313		width = (width + 8) &~ 7;
314		columns = 80 / width;
315		if (columns == 0)
316			columns = 1;
317		lines = (NCMDS + columns - 1) / columns;
318		for (i = 0; i < lines; i++) {
319			for (j = 0; j < columns; j++) {
320				c = cmdtab + j * lines + i;
321				if (c->c_name)
322					printf("%s", c->c_name);
323				if (c + lines >= &cmdtab[NCMDS]) {
324					printf("\n");
325					break;
326				}
327				w = strlen(c->c_name);
328				while (w < width) {
329					w = (w + 8) &~ 7;
330					putchar('\t');
331				}
332			}
333		}
334		return;
335	}
336	while (--argc > 0) {
337		register char *arg;
338		arg = *++argv;
339		c = getcmd(arg);
340		if (c == (struct cmd *)-1)
341			printf("?Ambiguous help command %s\n", arg);
342		else if (c == (struct cmd *)0)
343			printf("?Invalid help command %s\n", arg);
344		else
345			printf("%-*s\t%s\n", (int) HELPINDENT,
346				c->c_name, c->c_help);
347	}
348}
349
350/*
351 * return non-zero if the user is a member of the given group
352 */
353static int
354ingroup(const char *grname)
355{
356	static struct group *gptr=NULL;
357	static int ngroups = 0;
358	static gid_t groups[NGROUPS];
359	register gid_t gid;
360	register int i;
361
362	if (gptr == NULL) {
363		if ((gptr = getgrnam(grname)) == NULL) {
364			warnx("warning: unknown group '%s'", grname);
365			return(0);
366		}
367		ngroups = getgroups(NGROUPS, groups);
368		if (ngroups < 0)
369			err(1, "getgroups");
370	}
371	gid = gptr->gr_gid;
372	for (i = 0; i < ngroups; i++)
373		if (gid == groups[i])
374			return(1);
375	return(0);
376}
377
378/*
379 * Routine to get the information for a single printer (which will be
380 * called by the routines which implement individual commands).
381 * Note: This is for commands operating on a *single* printer.
382 */
383struct printer *
384setup_myprinter(char *pwanted, struct printer *pp, int sump_opts)
385{
386	int cdres, cmdstatus;
387
388	init_printer(pp);
389	cmdstatus = getprintcap(pwanted, pp);
390	switch (cmdstatus) {
391	default:
392		fatal(pp, "%s", pcaperr(cmdstatus));
393		/* NOTREACHED */
394	case PCAPERR_NOTFOUND:
395		printf("unknown printer %s\n", pwanted);
396		return (NULL);
397	case PCAPERR_TCOPEN:
398		printf("warning: %s: unresolved tc= reference(s)", pwanted);
399		break;
400	case PCAPERR_SUCCESS:
401		break;
402	}
403	if ((sump_opts & SUMP_NOHEADER) == 0)
404		printf("%s:\n", pp->printer);
405
406	if (sump_opts & SUMP_CHDIR_SD) {
407		seteuid(euid);
408		cdres = chdir(pp->spool_dir);
409		seteuid(uid);
410		if (cdres < 0) {
411			printf("\tcannot chdir to %s\n", pp->spool_dir);
412			free_printer(pp);
413			return (NULL);
414		}
415	}
416
417	return (pp);
418}
419