IMPORT openssh-9.8p1
[dragonfly.git] / usr.sbin / lpr / lpc / lpc.c
blobd7d97b4e05e7d492af503c411350d28e9c34efdd
1 /*
2 * Copyright (c) 1983, 1993
3 * The Regents of the University of California. All rights reserved.
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. Neither the name of the University nor the names of its contributors
15 * may be used to endorse or promote products derived from this software
16 * without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
30 * @(#) Copyright (c) 1983, 1993 The Regents of the University of California. All rights reserved.
31 * @(#)lpc.c 8.3 (Berkeley) 4/28/95
32 * $FreeBSD: src/usr.sbin/lpr/lpc/lpc.c,v 1.13.2.11 2002/07/26 03:12:07 gad Exp $
35 #include <sys/param.h>
37 #include <ctype.h>
38 #include <dirent.h>
39 #include <err.h>
40 #include <grp.h>
41 #include <setjmp.h>
42 #include <signal.h>
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <syslog.h>
46 #include <string.h>
47 #include <unistd.h>
48 #include <histedit.h>
50 #include "lp.h"
51 #include "lpc.h"
52 #include "extern.h"
54 #ifndef LPR_OPER
55 #define LPR_OPER "operator" /* group name of lpr operators */
56 #endif
59 * lpc -- line printer control program
62 #define MAX_CMDLINE 200
63 #define MAX_MARGV 20
64 static int fromatty;
66 static char cmdline[MAX_CMDLINE];
67 static int margc;
68 static char *margv[MAX_MARGV];
69 uid_t uid, euid;
71 static void cmdscanner(void);
72 static struct cmd *getcmd(const char *_name);
73 static void intr(int _signo);
74 static void makeargv(void);
75 static int ingroup(const char *_grname);
77 int
78 main(int argc, char **argv)
80 struct cmd *c;
82 euid = geteuid();
83 uid = getuid();
84 seteuid(uid);
85 progname = argv[0];
86 openlog("lpd", 0, LOG_LPR);
88 if (--argc > 0) {
89 c = getcmd(*++argv);
90 if (c == (struct cmd *)-1) {
91 printf("?Ambiguous command\n");
92 exit(1);
94 if (c == NULL) {
95 printf("?Invalid command\n");
96 exit(1);
98 if ((c->c_opts & LPC_PRIVCMD) && getuid() &&
99 ingroup(LPR_OPER) == 0) {
100 printf("?Privileged command\n");
101 exit(1);
103 if (c->c_generic != 0)
104 generic(c->c_generic, c->c_opts, c->c_handler,
105 argc, argv);
106 else
107 (*c->c_handler)(argc, argv);
108 exit(0);
110 fromatty = isatty(fileno(stdin));
111 if (!fromatty)
112 signal(SIGINT, intr);
113 for (;;) {
114 cmdscanner();
118 static void
119 intr(int signo __unused)
121 /* (the '__unused' is just to avoid a compile-time warning) */
122 exit(0);
125 static const char *
126 lpc_prompt(void)
128 return ("lpc> ");
132 * Command parser.
134 static void
135 cmdscanner(void)
137 struct cmd *c;
138 static EditLine *el;
139 static History *hist;
140 static HistEvent he;
141 size_t len;
142 int num;
143 const char *bp;
145 num = 0;
146 bp = NULL;
147 el = NULL;
148 hist = NULL;
149 for (;;) {
150 if (fromatty) {
151 if (!el) {
152 el = el_init("lpc", stdin, stdout, stderr);
153 hist = history_init();
154 history(hist, &he, H_SETSIZE, 100);
155 el_set(el, EL_HIST, history, hist);
156 el_set(el, EL_EDITOR, "emacs");
157 el_set(el, EL_PROMPT, lpc_prompt);
158 el_set(el, EL_SIGNAL, 1);
159 el_source(el, NULL);
161 * EditLine init may call 'cgetset()' to set a
162 * capability-db meant for termcap (eg: to set
163 * terminal type 'xterm'). Reset that now, or
164 * that same db-information will be used for
165 * printcap (giving us an "xterm" printer, with
166 * all kinds of invalid capabilities...).
168 cgetset(NULL);
170 if ((bp = el_gets(el, &num)) == NULL || num == 0)
171 quit(0, NULL);
173 len = (num > MAX_CMDLINE -1) ? MAX_CMDLINE -1 : num;
174 memcpy(cmdline, bp, len);
175 cmdline[len] = 0;
176 history(hist, &he, H_ENTER, bp);
178 } else {
179 if (fgets(cmdline, MAX_CMDLINE, stdin) == 0)
180 quit(0, NULL);
181 if (cmdline[0] == 0 || cmdline[0] == '\n')
182 break;
185 makeargv();
186 if (margc == 0)
187 continue;
188 if (el != NULL && el_parse(el, margc, (const char **)margv) != -1)
189 continue;
191 c = getcmd(margv[0]);
192 if (c == (struct cmd *)-1) {
193 printf("?Ambiguous command\n");
194 continue;
196 if (c == NULL) {
197 printf("?Invalid command\n");
198 continue;
200 if ((c->c_opts & LPC_PRIVCMD) && getuid() &&
201 ingroup(LPR_OPER) == 0) {
202 printf("?Privileged command\n");
203 continue;
207 * Two different commands might have the same generic rtn
208 * (eg: "clean" and "tclean"), and just use different
209 * handler routines for distinct command-setup. The handler
210 * routine might also be set on a generic routine for
211 * initial parameter processing.
213 if (c->c_generic != 0)
214 generic(c->c_generic, c->c_opts, c->c_handler,
215 margc, margv);
216 else
217 (*c->c_handler)(margc, margv);
221 static struct cmd *
222 getcmd(const char *name)
224 const char *p, *q;
225 struct cmd *c, *found;
226 int nmatches, longest;
228 longest = 0;
229 nmatches = 0;
230 found = NULL;
231 for (c = cmdtab; (p = c->c_name); c++) {
232 for (q = name; *q == *p++; q++)
233 if (*q == 0) /* exact match? */
234 return(c);
235 if (!*q) { /* the name was a prefix */
236 if (q - name > longest) {
237 longest = q - name;
238 nmatches = 1;
239 found = c;
240 } else if (q - name == longest)
241 nmatches++;
244 if (nmatches > 1)
245 return((struct cmd *)-1);
246 return(found);
250 * Slice a string up into argc/argv.
252 static void
253 makeargv(void)
255 char *cp;
256 char **argp = margv;
257 int n = 0;
259 margc = 0;
260 for (cp = cmdline; *cp && (size_t)(cp - cmdline) < sizeof(cmdline) &&
261 n < MAX_MARGV -1; n++) {
262 while (isspace(*cp))
263 cp++;
264 if (*cp == '\0')
265 break;
266 *argp++ = cp;
267 margc += 1;
268 while (*cp != '\0' && !isspace(*cp))
269 cp++;
270 if (*cp == '\0')
271 break;
272 *cp++ = '\0';
274 *argp++ = NULL;
277 #define HELPINDENT (sizeof ("directory"))
280 * Help command.
282 void
283 help(int argc, char **argv)
285 struct cmd *c;
287 if (argc == 1) {
288 int i, j, w;
289 int columns, width = 0, lines;
291 printf("Commands may be abbreviated. Commands are:\n\n");
292 for (c = cmdtab; c->c_name; c++) {
293 int len = strlen(c->c_name);
295 if (len > width)
296 width = len;
298 width = (width + 8) &~ 7;
299 columns = 80 / width;
300 if (columns == 0)
301 columns = 1;
302 lines = (NCMDS + columns - 1) / columns;
303 for (i = 0; i < lines; i++) {
304 for (j = 0; j < columns; j++) {
305 c = cmdtab + j * lines + i;
306 if (c->c_name)
307 printf("%s", c->c_name);
308 if (c + lines >= &cmdtab[NCMDS]) {
309 printf("\n");
310 break;
312 w = strlen(c->c_name);
313 while (w < width) {
314 w = (w + 8) &~ 7;
315 putchar('\t');
319 return;
321 while (--argc > 0) {
322 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 == NULL)
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);
337 * return non-zero if the user is a member of the given group
339 static int
340 ingroup(const char *grname)
342 static struct group *gptr=NULL;
343 static int ngroups = 0;
344 static gid_t groups[NGROUPS];
345 gid_t gid;
346 int i;
348 if (gptr == NULL) {
349 if ((gptr = getgrnam(grname)) == NULL) {
350 warnx("warning: unknown group '%s'", grname);
351 return(0);
353 ngroups = getgroups(NGROUPS, groups);
354 if (ngroups < 0)
355 err(1, "getgroups");
357 gid = gptr->gr_gid;
358 for (i = 0; i < ngroups; i++)
359 if (gid == groups[i])
360 return(1);
361 return(0);
365 * Routine to get the information for a single printer (which will be
366 * called by the routines which implement individual commands).
367 * Note: This is for commands operating on a *single* printer.
369 struct printer *
370 setup_myprinter(char *pwanted, struct printer *pp, int sump_opts)
372 int cdres, cmdstatus;
374 init_printer(pp);
375 cmdstatus = getprintcap(pwanted, pp);
376 switch (cmdstatus) {
377 default:
378 fatal(pp, "%s", pcaperr(cmdstatus));
379 /* NOTREACHED */
380 case PCAPERR_NOTFOUND:
381 printf("unknown printer %s\n", pwanted);
382 return (NULL);
383 case PCAPERR_TCOPEN:
384 printf("warning: %s: unresolved tc= reference(s)", pwanted);
385 break;
386 case PCAPERR_SUCCESS:
387 break;
389 if ((sump_opts & SUMP_NOHEADER) == 0)
390 printf("%s:\n", pp->printer);
392 if (sump_opts & SUMP_CHDIR_SD) {
393 seteuid(euid);
394 cdres = chdir(pp->spool_dir);
395 seteuid(uid);
396 if (cdres < 0) {
397 printf("\tcannot chdir to %s\n", pp->spool_dir);
398 free_printer(pp);
399 return (NULL);
403 return (pp);