lpc.c revision 1.7 1 /* $NetBSD: lpc.c,v 1.7 1997/10/05 11:52:30 mrg Exp $ */
2 /*
3 * Copyright (c) 1983, 1993
4 * The Regents of the University of California. All rights reserved.
5 *
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36 #ifndef lint
37 static char copyright[] =
38 "@(#) Copyright (c) 1983, 1993\n\
39 The Regents of the University of California. All rights reserved.\n";
40 #endif /* not lint */
41
42 #ifndef lint
43 static char sccsid[] = "@(#)lpc.c 8.3 (Berkeley) 4/28/95";
44 #endif /* not lint */
45
46 #include <sys/param.h>
47
48 #include <dirent.h>
49 #include <signal.h>
50 #include <setjmp.h>
51 #include <syslog.h>
52 #include <unistd.h>
53 #include <stdlib.h>
54 #include <stdio.h>
55 #include <ctype.h>
56 #include <string.h>
57 #include <grp.h>
58 #include <sys/param.h>
59 #include "lp.h"
60 #include "lpc.h"
61 #include "extern.h"
62
63 #ifndef LPR_OPER
64 #define LPR_OPER "operator" /* group name of lpr operators */
65 #endif
66
67 /*
68 * lpc -- line printer control program
69 */
70
71 #define MAX_CMDLINE 200
72 #define MAX_MARGV 20
73 int fromatty;
74
75 char cmdline[MAX_CMDLINE];
76 int margc;
77 char *margv[MAX_MARGV];
78 int top;
79 uid_t uid, euid;
80
81 jmp_buf toplevel;
82
83 static void cmdscanner __P((int));
84 static struct cmd *getcmd __P((char *));
85 static void intr __P((int));
86 static void makeargv __P((void));
87 static int ingroup __P((char *));
88
89 int
90 main(argc, argv)
91 int argc;
92 char *argv[];
93 {
94 register struct cmd *c;
95
96 euid = geteuid();
97 uid = getuid();
98 seteuid(uid);
99 name = argv[0];
100 openlog("lpd", 0, LOG_LPR);
101
102 if (--argc > 0) {
103 c = getcmd(*++argv);
104 if (c == (struct cmd *)-1) {
105 printf("?Ambiguous command\n");
106 exit(1);
107 }
108 if (c == 0) {
109 printf("?Invalid command\n");
110 exit(1);
111 }
112 if (c->c_priv && getuid() && ingroup(LPR_OPER) == 0) {
113 printf("?Privileged command\n");
114 exit(1);
115 }
116 (*c->c_handler)(argc, argv);
117 exit(0);
118 }
119 fromatty = isatty(fileno(stdin));
120 top = setjmp(toplevel) == 0;
121 if (top)
122 signal(SIGINT, intr);
123 for (;;) {
124 cmdscanner(top);
125 top = 1;
126 }
127 }
128
129 static void
130 intr(signo)
131 int signo;
132 {
133 if (!fromatty)
134 exit(0);
135 longjmp(toplevel, 1);
136 }
137
138 /*
139 * Command parser.
140 */
141 static void
142 cmdscanner(top)
143 int top;
144 {
145 register struct cmd *c;
146
147 if (!top)
148 putchar('\n');
149 for (;;) {
150 if (fromatty) {
151 printf("lpc> ");
152 fflush(stdout);
153 }
154 if (fgets(cmdline, MAX_CMDLINE, stdin) == 0)
155 quit(0, NULL);
156 if (cmdline[0] == 0 || cmdline[0] == '\n')
157 break;
158 makeargv();
159 c = getcmd(margv[0]);
160 if (c == (struct cmd *)-1) {
161 printf("?Ambiguous command\n");
162 continue;
163 }
164 if (c == 0) {
165 printf("?Invalid command\n");
166 continue;
167 }
168 if (c->c_priv && getuid() && ingroup(LPR_OPER) == 0) {
169 printf("?Privileged command\n");
170 continue;
171 }
172 (*c->c_handler)(margc, margv);
173 }
174 longjmp(toplevel, 0);
175 }
176
177 static struct cmd *
178 getcmd(name)
179 register char *name;
180 {
181 register char *p, *q;
182 register struct cmd *c, *found;
183 register int nmatches, longest;
184
185 longest = 0;
186 nmatches = 0;
187 found = 0;
188 for (c = cmdtab; (p = c->c_name) != NULL; c++) {
189 for (q = name; *q == *p++; q++)
190 if (*q == 0) /* exact match? */
191 return(c);
192 if (!*q) { /* the name was a prefix */
193 if (q - name > longest) {
194 longest = q - name;
195 nmatches = 1;
196 found = c;
197 } else if (q - name == longest)
198 nmatches++;
199 }
200 }
201 if (nmatches > 1)
202 return((struct cmd *)-1);
203 return(found);
204 }
205
206 /*
207 * Slice a string up into argc/argv.
208 */
209 static void
210 makeargv()
211 {
212 register char *cp;
213 register char **argp = margv;
214 register int n = 0;
215
216 margc = 0;
217 for (cp = cmdline; *cp && n < MAX_MARGV; n++) {
218 while (isspace(*cp))
219 cp++;
220 if (*cp == '\0')
221 break;
222 *argp++ = cp;
223 margc += 1;
224 while (*cp != '\0' && !isspace(*cp))
225 cp++;
226 if (*cp == '\0')
227 break;
228 *cp++ = '\0';
229 }
230 *argp++ = 0;
231 }
232
233 #define HELPINDENT (sizeof ("directory"))
234
235 /*
236 * Help command.
237 */
238 void
239 help(argc, argv)
240 int argc;
241 char *argv[];
242 {
243 register struct cmd *c;
244
245 if (argc == 1) {
246 register int i, j, w;
247 int columns, width = 0, lines;
248 extern int NCMDS;
249
250 printf("Commands may be abbreviated. Commands are:\n\n");
251 for (c = cmdtab; c->c_name; c++) {
252 int len = strlen(c->c_name);
253
254 if (len > width)
255 width = len;
256 }
257 width = (width + 8) &~ 7;
258 columns = 80 / width;
259 if (columns == 0)
260 columns = 1;
261 lines = (NCMDS + columns - 1) / columns;
262 for (i = 0; i < lines; i++) {
263 for (j = 0; j < columns; j++) {
264 c = cmdtab + j * lines + i;
265 if (c->c_name)
266 printf("%s", c->c_name);
267 if (c + lines >= &cmdtab[NCMDS]) {
268 printf("\n");
269 break;
270 }
271 w = strlen(c->c_name);
272 while (w < width) {
273 w = (w + 8) &~ 7;
274 putchar('\t');
275 }
276 }
277 }
278 return;
279 }
280 while (--argc > 0) {
281 register char *arg;
282 arg = *++argv;
283 c = getcmd(arg);
284 if (c == (struct cmd *)-1)
285 printf("?Ambiguous help command %s\n", arg);
286 else if (c == (struct cmd *)0)
287 printf("?Invalid help command %s\n", arg);
288 else
289 printf("%-*s\t%s\n", HELPINDENT,
290 c->c_name, c->c_help);
291 }
292 }
293
294 /*
295 * return non-zero if the user is a member of the given group
296 */
297 static int
298 ingroup(grname)
299 char *grname;
300 {
301 static struct group *gptr=NULL;
302 static gid_t groups[NGROUPS];
303 register gid_t gid;
304 register int i;
305
306 if (gptr == NULL) {
307 if ((gptr = getgrnam(grname)) == NULL) {
308 fprintf(stderr, "Warning: unknown group '%s'\n",
309 grname);
310 return(0);
311 }
312 if (getgroups(NGROUPS, groups) < 0) {
313 perror("getgroups");
314 exit(1);
315 }
316 }
317 gid = gptr->gr_gid;
318 for (i = 0; i < NGROUPS; i++)
319 if (gid == groups[i])
320 return(1);
321 return(0);
322 }
323