xref: /freebsd/usr.sbin/lpr/lpc/lpc.c (revision 6adf353a56a161443406b44a45d00c688ca7b857)
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 			if ((bp = el_gets(el, &num)) == NULL || num == 0)
174 				quit(0, NULL);
175 
176 			len = (num > MAX_CMDLINE) ? MAX_CMDLINE : num;
177 			memcpy(cmdline, bp, len);
178 			cmdline[len] = 0;
179 			history(hist, H_ENTER, bp);
180 
181 		} else {
182 			if (fgets(cmdline, MAX_CMDLINE, stdin) == 0)
183 				quit(0, NULL);
184 			if (cmdline[0] == 0 || cmdline[0] == '\n')
185 				break;
186 		}
187 
188 		makeargv();
189 		if (margc == 0)
190 			continue;
191 		if (el_parse(el, margc, margv) != -1)
192 			continue;
193 
194 		c = getcmd(margv[0]);
195 		if (c == (struct cmd *)-1) {
196 			printf("?Ambiguous command\n");
197 			continue;
198 		}
199 		if (c == 0) {
200 			printf("?Invalid command\n");
201 			continue;
202 		}
203 		if (c->c_priv && getuid() && ingroup(LPR_OPER) == 0) {
204 			printf("?Privileged command\n");
205 			continue;
206 		}
207 
208 		/*
209 		 * Two different commands might have the same generic rtn
210 		 * (eg: "clean" and "tclean"), and just use different
211 		 * handler routines for distinct command-setup.  The handler
212 		 * routine might also be set on a generic routine for
213 		 * initial parameter processing.
214 		 */
215 		if (c->c_generic != 0)
216 			generic(c->c_generic, c->c_handler, margc, margv);
217 		else
218 			(*c->c_handler)(margc, margv);
219 	}
220 }
221 
222 static struct cmd *
223 getcmd(const char *name)
224 {
225 	register const char *p, *q;
226 	register struct cmd *c, *found;
227 	register int nmatches, longest;
228 
229 	longest = 0;
230 	nmatches = 0;
231 	found = 0;
232 	for (c = cmdtab; (p = c->c_name); c++) {
233 		for (q = name; *q == *p++; q++)
234 			if (*q == 0)		/* exact match? */
235 				return(c);
236 		if (!*q) {			/* the name was a prefix */
237 			if (q - name > longest) {
238 				longest = q - name;
239 				nmatches = 1;
240 				found = c;
241 			} else if (q - name == longest)
242 				nmatches++;
243 		}
244 	}
245 	if (nmatches > 1)
246 		return((struct cmd *)-1);
247 	return(found);
248 }
249 
250 /*
251  * Slice a string up into argc/argv.
252  */
253 static void
254 makeargv(void)
255 {
256 	register char *cp;
257 	register char **argp = margv;
258 	register int n = 0;
259 
260 	margc = 0;
261 	for (cp = cmdline; *cp && (size_t)(cp - cmdline) < sizeof(cmdline) &&
262 	    n < MAX_MARGV; n++) {
263 		while (isspace(*cp))
264 			cp++;
265 		if (*cp == '\0')
266 			break;
267 		*argp++ = cp;
268 		margc += 1;
269 		while (*cp != '\0' && !isspace(*cp))
270 			cp++;
271 		if (*cp == '\0')
272 			break;
273 		*cp++ = '\0';
274 	}
275 	*argp++ = 0;
276 }
277 
278 #define HELPINDENT (sizeof ("directory"))
279 
280 /*
281  * Help command.
282  */
283 void
284 help(int argc, char *argv[])
285 {
286 	register struct cmd *c;
287 
288 	if (argc == 1) {
289 		register int i, j, w;
290 		int columns, width = 0, lines;
291 
292 		printf("Commands may be abbreviated.  Commands are:\n\n");
293 		for (c = cmdtab; c->c_name; c++) {
294 			int len = strlen(c->c_name);
295 
296 			if (len > width)
297 				width = len;
298 		}
299 		width = (width + 8) &~ 7;
300 		columns = 80 / width;
301 		if (columns == 0)
302 			columns = 1;
303 		lines = (NCMDS + columns - 1) / columns;
304 		for (i = 0; i < lines; i++) {
305 			for (j = 0; j < columns; j++) {
306 				c = cmdtab + j * lines + i;
307 				if (c->c_name)
308 					printf("%s", c->c_name);
309 				if (c + lines >= &cmdtab[NCMDS]) {
310 					printf("\n");
311 					break;
312 				}
313 				w = strlen(c->c_name);
314 				while (w < width) {
315 					w = (w + 8) &~ 7;
316 					putchar('\t');
317 				}
318 			}
319 		}
320 		return;
321 	}
322 	while (--argc > 0) {
323 		register char *arg;
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 == (struct cmd *)0)
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 	register gid_t gid;
346 	register 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