1 | /* |
---|
2 | * $Source: /afs/dev.mit.edu/source/repository/athena/bin/lpr/lpc.c,v $ |
---|
3 | * $Header: /afs/dev.mit.edu/source/repository/athena/bin/lpr/lpc.c,v 1.6 1991-02-09 13:15:37 epeisach Exp $ |
---|
4 | */ |
---|
5 | |
---|
6 | #ifndef lint |
---|
7 | static char *rcsid_lpc_c = "$Header: /afs/dev.mit.edu/source/repository/athena/bin/lpr/lpc.c,v 1.6 1991-02-09 13:15:37 epeisach Exp $"; |
---|
8 | #endif lint |
---|
9 | |
---|
10 | /* |
---|
11 | * Copyright (c) 1983 Regents of the University of California. |
---|
12 | * All rights reserved. The Berkeley software License Agreement |
---|
13 | * specifies the terms and conditions for redistribution. |
---|
14 | */ |
---|
15 | |
---|
16 | #ifndef lint |
---|
17 | char copyright[] = |
---|
18 | "@(#) Copyright (c) 1983 Regents of the University of California.\n\ |
---|
19 | All rights reserved.\n"; |
---|
20 | #endif not lint |
---|
21 | |
---|
22 | #ifndef lint |
---|
23 | static char sccsid[] = "@(#)lpc.c 5.2 (Berkeley) 11/17/85"; |
---|
24 | #endif not lint |
---|
25 | |
---|
26 | #ifdef OPERATOR |
---|
27 | #define GETUID geteuid |
---|
28 | #else |
---|
29 | #define GETUID getuid |
---|
30 | #endif |
---|
31 | |
---|
32 | /* |
---|
33 | * lpc -- line printer control program |
---|
34 | */ |
---|
35 | #include <stdio.h> |
---|
36 | #include <signal.h> |
---|
37 | #include <ctype.h> |
---|
38 | #include <setjmp.h> |
---|
39 | #include <syslog.h> |
---|
40 | #include <netdb.h> |
---|
41 | |
---|
42 | #ifdef OPERATOR |
---|
43 | #include <pwd.h> |
---|
44 | #endif |
---|
45 | |
---|
46 | |
---|
47 | #include "lpc.h" |
---|
48 | |
---|
49 | int fromatty; |
---|
50 | |
---|
51 | char cmdline[200]; |
---|
52 | int margc; |
---|
53 | char *margv[20]; |
---|
54 | int top; |
---|
55 | int intr(); |
---|
56 | struct cmd *getcmd(); |
---|
57 | extern struct cmd cmdtab[]; |
---|
58 | |
---|
59 | jmp_buf toplevel; |
---|
60 | |
---|
61 | main(argc, argv) |
---|
62 | char *argv[]; |
---|
63 | { |
---|
64 | register struct cmd *c; |
---|
65 | extern char *name; |
---|
66 | |
---|
67 | name = argv[0]; |
---|
68 | #ifdef LOG_AUTH |
---|
69 | #ifdef OPERATOR |
---|
70 | openlog("lpc", 0, LOG_AUTH); |
---|
71 | #else |
---|
72 | openlog("lpd", 0, LOG_LPR); |
---|
73 | #endif |
---|
74 | #else /* not LOG_AUTH */ |
---|
75 | #ifdef OPERATOR |
---|
76 | openlog("lpc", 0); |
---|
77 | #else |
---|
78 | openlog("lpd", 0); |
---|
79 | #endif |
---|
80 | #endif /* LOG_AUTH */ |
---|
81 | if (--argc > 0) { |
---|
82 | c = getcmd(*++argv); |
---|
83 | if (c == (struct cmd *)-1) { |
---|
84 | printf("?Ambiguous command\n"); |
---|
85 | exit(1); |
---|
86 | } |
---|
87 | if (c == 0) { |
---|
88 | printf("?Invalid command\n"); |
---|
89 | exit(1); |
---|
90 | } |
---|
91 | if (c->c_priv && GETUID()) { |
---|
92 | printf("?Privileged command\n"); |
---|
93 | exit(1); |
---|
94 | } |
---|
95 | #ifdef OPERATOR |
---|
96 | log_cmdline(argc, argv); |
---|
97 | #endif |
---|
98 | (*c->c_handler)(argc, argv); |
---|
99 | exit(0); |
---|
100 | } |
---|
101 | fromatty = isatty(fileno(stdin)); |
---|
102 | top = setjmp(toplevel) == 0; |
---|
103 | if (top) |
---|
104 | signal(SIGINT, intr); |
---|
105 | for (;;) { |
---|
106 | cmdscanner(top); |
---|
107 | top = 1; |
---|
108 | } |
---|
109 | } |
---|
110 | |
---|
111 | intr() |
---|
112 | { |
---|
113 | if (!fromatty) |
---|
114 | exit(0); |
---|
115 | longjmp(toplevel, 1); |
---|
116 | } |
---|
117 | |
---|
118 | /* |
---|
119 | * Command parser. |
---|
120 | */ |
---|
121 | cmdscanner(top) |
---|
122 | int top; |
---|
123 | { |
---|
124 | register struct cmd *c; |
---|
125 | extern int help(); |
---|
126 | |
---|
127 | if (!top) |
---|
128 | putchar('\n'); |
---|
129 | for (;;) { |
---|
130 | if (fromatty) { |
---|
131 | printf("lpc> "); |
---|
132 | fflush(stdout); |
---|
133 | } |
---|
134 | if (gets(cmdline) == 0) |
---|
135 | quit(); |
---|
136 | if (cmdline[0] == 0) |
---|
137 | break; |
---|
138 | makeargv(); |
---|
139 | c = getcmd(margv[0]); |
---|
140 | if (c == (struct cmd *)-1) { |
---|
141 | printf("?Ambiguous command\n"); |
---|
142 | continue; |
---|
143 | } |
---|
144 | if (c == 0) { |
---|
145 | printf("?Invalid command\n"); |
---|
146 | continue; |
---|
147 | } |
---|
148 | if (c->c_priv && GETUID()) { |
---|
149 | printf("?Privileged command\n"); |
---|
150 | continue; |
---|
151 | } |
---|
152 | #ifdef OPERATOR |
---|
153 | log_cmdline(margc, margv); |
---|
154 | #endif |
---|
155 | (*c->c_handler)(margc, margv); |
---|
156 | } |
---|
157 | longjmp(toplevel, 0); |
---|
158 | } |
---|
159 | |
---|
160 | struct cmd * |
---|
161 | getcmd(name) |
---|
162 | register char *name; |
---|
163 | { |
---|
164 | register char *p, *q; |
---|
165 | register struct cmd *c, *found; |
---|
166 | register int nmatches, longest; |
---|
167 | |
---|
168 | longest = 0; |
---|
169 | nmatches = 0; |
---|
170 | found = 0; |
---|
171 | for (c = cmdtab; p = c->c_name; c++) { |
---|
172 | for (q = name; *q == *p++; q++) |
---|
173 | if (*q == 0) /* exact match? */ |
---|
174 | return(c); |
---|
175 | if (!*q) { /* the name was a prefix */ |
---|
176 | if (q - name > longest) { |
---|
177 | longest = q - name; |
---|
178 | nmatches = 1; |
---|
179 | found = c; |
---|
180 | } else if (q - name == longest) |
---|
181 | nmatches++; |
---|
182 | } |
---|
183 | } |
---|
184 | if (nmatches > 1) |
---|
185 | return((struct cmd *)-1); |
---|
186 | return(found); |
---|
187 | } |
---|
188 | |
---|
189 | /* |
---|
190 | * Slice a string up into argc/argv. |
---|
191 | */ |
---|
192 | makeargv() |
---|
193 | { |
---|
194 | register char *cp; |
---|
195 | register char **argp = margv; |
---|
196 | |
---|
197 | margc = 0; |
---|
198 | for (cp = cmdline; *cp;) { |
---|
199 | while (isspace(*cp)) |
---|
200 | cp++; |
---|
201 | if (*cp == '\0') |
---|
202 | break; |
---|
203 | *argp++ = cp; |
---|
204 | margc += 1; |
---|
205 | while (*cp != '\0' && !isspace(*cp)) |
---|
206 | cp++; |
---|
207 | if (*cp == '\0') |
---|
208 | break; |
---|
209 | *cp++ = '\0'; |
---|
210 | } |
---|
211 | *argp++ = 0; |
---|
212 | } |
---|
213 | |
---|
214 | #define HELPINDENT (sizeof ("directory")) |
---|
215 | |
---|
216 | /* |
---|
217 | * Help command. |
---|
218 | */ |
---|
219 | help(argc, argv) |
---|
220 | int argc; |
---|
221 | char *argv[]; |
---|
222 | { |
---|
223 | register struct cmd *c; |
---|
224 | |
---|
225 | if (argc == 1) { |
---|
226 | register int i, j, w; |
---|
227 | int columns, width = 0, lines; |
---|
228 | extern int NCMDS; |
---|
229 | |
---|
230 | printf("Commands may be abbreviated. Commands are:\n\n"); |
---|
231 | for (c = cmdtab; c < &cmdtab[NCMDS]; c++) { |
---|
232 | int len; |
---|
233 | if (!c->c_name) |
---|
234 | continue; |
---|
235 | len = strlen(c->c_name); |
---|
236 | |
---|
237 | if (len > width) |
---|
238 | width = len; |
---|
239 | } |
---|
240 | width = (width + 8) &~ 7; |
---|
241 | columns = 80 / width; |
---|
242 | if (columns == 0) |
---|
243 | columns = 1; |
---|
244 | lines = (NCMDS + columns - 1) / columns; |
---|
245 | for (i = 0; i < lines; i++) { |
---|
246 | for (j = 0; j < columns; j++) { |
---|
247 | c = cmdtab + j * lines + i; |
---|
248 | if(c->c_name) printf("%s", c->c_name); |
---|
249 | if (c + lines >= &cmdtab[NCMDS]) { |
---|
250 | printf("\n"); |
---|
251 | break; |
---|
252 | } |
---|
253 | w = strlen(c->c_name); |
---|
254 | while (w < width) { |
---|
255 | w = (w + 8) &~ 7; |
---|
256 | putchar('\t'); |
---|
257 | } |
---|
258 | } |
---|
259 | } |
---|
260 | return; |
---|
261 | } |
---|
262 | while (--argc > 0) { |
---|
263 | register char *arg; |
---|
264 | arg = *++argv; |
---|
265 | c = getcmd(arg); |
---|
266 | if (c == (struct cmd *)-1) |
---|
267 | printf("?Ambiguous help command %s\n", arg); |
---|
268 | else if (c == (struct cmd *)0) |
---|
269 | printf("?Invalid help command %s\n", arg); |
---|
270 | else |
---|
271 | printf("%-*s\t%s\n", HELPINDENT, |
---|
272 | c->c_name, c->c_help); |
---|
273 | } |
---|
274 | return; |
---|
275 | } |
---|
276 | |
---|
277 | #ifdef OPERATOR |
---|
278 | log_cmdline(argc, argv) |
---|
279 | int argc; |
---|
280 | char *argv[]; |
---|
281 | { |
---|
282 | struct passwd *pwentry; |
---|
283 | char *name; |
---|
284 | char logbuf[512]; |
---|
285 | register int i; |
---|
286 | /* note getuid() returns real uid */ |
---|
287 | pwentry = getpwuid(getuid()); /* get password entry for invoker */ |
---|
288 | name = pwentry->pw_name; /* get his name */ |
---|
289 | |
---|
290 | sprintf(logbuf,"%s LPC: ",name); |
---|
291 | for (i=0 ;i < argc; i++) { |
---|
292 | strcat(logbuf,argv[i]); |
---|
293 | strcat(logbuf," "); |
---|
294 | } |
---|
295 | syslog(LOG_INFO, logbuf); |
---|
296 | return(0); |
---|
297 | } |
---|
298 | |
---|
299 | #endif |
---|