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