lpc.c revision 50039
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.8 1998/09/11 18:49:31 wollman 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			}
169			if ((bp = el_gets(el, &num)) == NULL || num == 0)
170				return;
171
172			memcpy(cmdline, bp, (MAX_CMDLINE > num ? MAX_CMDLINE : num));
173			cmdline[num] = 0;
174			history(hist, H_ENTER, bp);
175
176		} else {
177			if (fgets(cmdline, MAX_CMDLINE, stdin) == 0)
178				quit(0, NULL);
179			if (cmdline[0] == 0 || cmdline[0] == '\n')
180				break;
181		}
182
183		makeargv();
184		if (margc == 0)
185			continue;
186		if (el_parse(el, margc, margv) != -1)
187			continue;
188
189		c = getcmd(margv[0]);
190		if (c == (struct cmd *)-1) {
191			printf("?Ambiguous command\n");
192			continue;
193		}
194		if (c == 0) {
195			printf("?Invalid command\n");
196			continue;
197		}
198		if (c->c_priv && getuid() && ingroup(LPR_OPER) == 0) {
199			printf("?Privileged command\n");
200			continue;
201		}
202		if (c->c_generic != 0)
203			generic(c->c_generic, margc, margv);
204		else
205			(*c->c_handler)(margc, margv);
206	}
207}
208
209static struct cmd *
210getcmd(name)
211	register char *name;
212{
213	register char *p, *q;
214	register struct cmd *c, *found;
215	register int nmatches, longest;
216
217	longest = 0;
218	nmatches = 0;
219	found = 0;
220	for (c = cmdtab; (p = c->c_name); c++) {
221		for (q = name; *q == *p++; q++)
222			if (*q == 0)		/* exact match? */
223				return(c);
224		if (!*q) {			/* the name was a prefix */
225			if (q - name > longest) {
226				longest = q - name;
227				nmatches = 1;
228				found = c;
229			} else if (q - name == longest)
230				nmatches++;
231		}
232	}
233	if (nmatches > 1)
234		return((struct cmd *)-1);
235	return(found);
236}
237
238/*
239 * Slice a string up into argc/argv.
240 */
241static void
242makeargv()
243{
244	register char *cp;
245	register char **argp = margv;
246	register int n = 0;
247
248	margc = 0;
249	for (cp = cmdline; *cp && (cp - cmdline) < sizeof(cmdline) &&
250	    n < MAX_MARGV; n++) {
251		while (isspace(*cp))
252			cp++;
253		if (*cp == '\0')
254			break;
255		*argp++ = cp;
256		margc += 1;
257		while (*cp != '\0' && !isspace(*cp))
258			cp++;
259		if (*cp == '\0')
260			break;
261		*cp++ = '\0';
262	}
263	*argp++ = 0;
264}
265
266#define HELPINDENT (sizeof ("directory"))
267
268/*
269 * Help command.
270 */
271void
272help(argc, argv)
273	int argc;
274	char *argv[];
275{
276	register struct cmd *c;
277
278	if (argc == 1) {
279		register int i, j, w;
280		int columns, width = 0, lines;
281
282		printf("Commands may be abbreviated.  Commands are:\n\n");
283		for (c = cmdtab; c->c_name; c++) {
284			int len = strlen(c->c_name);
285
286			if (len > width)
287				width = len;
288		}
289		width = (width + 8) &~ 7;
290		columns = 80 / width;
291		if (columns == 0)
292			columns = 1;
293		lines = (NCMDS + columns - 1) / columns;
294		for (i = 0; i < lines; i++) {
295			for (j = 0; j < columns; j++) {
296				c = cmdtab + j * lines + i;
297				if (c->c_name)
298					printf("%s", c->c_name);
299				if (c + lines >= &cmdtab[NCMDS]) {
300					printf("\n");
301					break;
302				}
303				w = strlen(c->c_name);
304				while (w < width) {
305					w = (w + 8) &~ 7;
306					putchar('\t');
307				}
308			}
309		}
310		return;
311	}
312	while (--argc > 0) {
313		register char *arg;
314		arg = *++argv;
315		c = getcmd(arg);
316		if (c == (struct cmd *)-1)
317			printf("?Ambiguous help command %s\n", arg);
318		else if (c == (struct cmd *)0)
319			printf("?Invalid help command %s\n", arg);
320		else
321			printf("%-*s\t%s\n", (int) HELPINDENT,
322				c->c_name, c->c_help);
323	}
324}
325
326/*
327 * return non-zero if the user is a member of the given group
328 */
329static int
330ingroup(grname)
331	char *grname;
332{
333	static struct group *gptr=NULL;
334	static gid_t groups[NGROUPS];
335	register gid_t gid;
336	register int i;
337
338	if (gptr == NULL) {
339		if ((gptr = getgrnam(grname)) == NULL) {
340			warnx("warning: unknown group '%s'", grname);
341			return(0);
342		}
343		if (getgroups(NGROUPS, groups) < 0)
344			err(1, "getgroups");
345	}
346	gid = gptr->gr_gid;
347	for (i = 0; i < NGROUPS; i++)
348		if (gid == groups[i])
349			return(1);
350	return(0);
351}
352