1
/* $OpenBSD: lpc.c,v 1.5 1997/01/17 16:12:37 millert Exp $ */
4
* Copyright (c) 1983, 1993
5
* The Regents of the University of California. All rights reserved.
8
* Redistribution and use in source and binary forms, with or without
9
* modification, are permitted provided that the following conditions
11
* 1. Redistributions of source code must retain the above copyright
12
* notice, this list of conditions and the following disclaimer.
13
* 2. Redistributions in binary form must reproduce the above copyright
14
* notice, this list of conditions and the following disclaimer in the
15
* documentation and/or other materials provided with the distribution.
16
* 3. All advertising materials mentioning features or use of this software
17
* must display the following acknowledgement:
18
* This product includes software developed by the University of
19
* California, Berkeley and its contributors.
20
* 4. Neither the name of the University nor the names of its contributors
21
* may be used to endorse or promote products derived from this software
22
* without specific prior written permission.
24
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38
static char copyright[] =
39
"@(#) Copyright (c) 1983, 1993\n\
40
The Regents of the University of California. All rights reserved.\n";
45
static char sccsid[] = "@(#)lpc.c 8.3 (Berkeley) 4/28/95";
47
static char rcsid[] = "$OpenBSD: lpc.c,v 1.5 1997/01/17 16:12:37 millert Exp $";
51
#include <sys/param.h>
63
#include <sys/param.h>
69
#define LPR_OPER "operator" /* group name of lpr operators */
73
* lpc -- line printer control program
76
#define MAX_CMDLINE 200
80
char cmdline[MAX_CMDLINE];
82
char *margv[MAX_MARGV];
88
static void cmdscanner __P((int));
89
static struct cmd *getcmd __P((char *));
90
static void intr __P((int));
91
static void makeargv __P((void));
92
static int ingroup __P((char *));
99
register struct cmd *c;
105
openlog("lpd", 0, LOG_LPR);
109
if (c == (struct cmd *)-1) {
110
printf("?Ambiguous command\n");
114
printf("?Invalid command\n");
117
if (c->c_priv && getuid() && ingroup(LPR_OPER) == 0) {
118
printf("?Privileged command\n");
121
(*c->c_handler)(argc, argv);
124
fromatty = isatty(fileno(stdin));
125
top = setjmp(toplevel) == 0;
127
signal(SIGINT, intr);
140
longjmp(toplevel, 1);
150
register struct cmd *c;
159
if (fgets(cmdline, MAX_CMDLINE, stdin) == 0)
161
if (cmdline[0] == 0 || cmdline[0] == '\n')
164
c = getcmd(margv[0]);
165
if (c == (struct cmd *)-1) {
166
printf("?Ambiguous command\n");
170
printf("?Invalid command\n");
173
if (c->c_priv && getuid() && ingroup(LPR_OPER) == 0) {
174
printf("?Privileged command\n");
177
(*c->c_handler)(margc, margv);
179
longjmp(toplevel, 0);
186
register char *p, *q;
187
register struct cmd *c, *found;
188
register int nmatches, longest;
193
for (c = cmdtab; (p = c->c_name); c++) {
194
for (q = name; *q == *p++; q++)
195
if (*q == 0) /* exact match? */
197
if (!*q) { /* the name was a prefix */
198
if (q - name > longest) {
202
} else if (q - name == longest)
207
return((struct cmd *)-1);
212
* Slice a string up into argc/argv.
218
register char **argp = margv;
222
for (cp = cmdline; *cp && (cp - cmdline) < sizeof(cmdline) &&
223
n < MAX_MARGV; n++) {
230
while (*cp != '\0' && !isspace(*cp))
239
#define HELPINDENT (sizeof ("directory"))
249
register struct cmd *c;
252
register int i, j, w;
253
int columns, width = 0, lines;
256
printf("Commands may be abbreviated. Commands are:\n\n");
257
for (c = cmdtab; c->c_name; c++) {
258
int len = strlen(c->c_name);
263
width = (width + 8) &~ 7;
264
columns = 80 / width;
267
lines = (NCMDS + columns - 1) / columns;
268
for (i = 0; i < lines; i++) {
269
for (j = 0; j < columns; j++) {
270
c = cmdtab + j * lines + i;
272
printf("%s", c->c_name);
273
if (c + lines >= &cmdtab[NCMDS]) {
277
w = strlen(c->c_name);
290
if (c == (struct cmd *)-1)
291
printf("?Ambiguous help command %s\n", arg);
292
else if (c == (struct cmd *)0)
293
printf("?Invalid help command %s\n", arg);
295
printf("%-*s\t%s\n", HELPINDENT,
296
c->c_name, c->c_help);
301
* return non-zero if the user is a member of the given group
307
static struct group *gptr=NULL;
308
static gid_t groups[NGROUPS];
313
if ((gptr = getgrnam(grname)) == NULL) {
314
fprintf(stderr, "Warning: unknown group '%s'\n",
318
if (getgroups(NGROUPS, groups) < 0) {
324
for (i = 0; i < NGROUPS; i++)
325
if (gid == groups[i])