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