NetBSD/usr.sbin/lpr/lpc/lpc.c

324 lines
6.9 KiB
C
Raw Normal View History

1997-10-05 20:45:43 +04:00
/* $NetBSD: lpc.c,v 1.9 1997/10/05 16:45:44 mrg Exp $ */
1993-03-21 12:45:37 +03:00
/*
1994-05-18 05:25:37 +04:00
* Copyright (c) 1983, 1993
* The Regents of the University of California. All rights reserved.
*
1993-03-21 12:45:37 +03:00
*
* 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. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. 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.
*/
#include <sys/cdefs.h>
1993-03-21 12:45:37 +03:00
#ifndef lint
__COPYRIGHT("@(#) Copyright (c) 1983, 1993\n\
The Regents of the University of California. All rights reserved.\n");
#if 0
1997-10-05 15:52:17 +04:00
static char sccsid[] = "@(#)lpc.c 8.3 (Berkeley) 4/28/95";
#else
1997-10-05 20:45:43 +04:00
__RCSID("$NetBSD: lpc.c,v 1.9 1997/10/05 16:45:44 mrg Exp $");
#endif
1993-03-21 12:45:37 +03:00
#endif /* not lint */
1994-05-18 05:25:37 +04:00
#include <sys/param.h>
#include <dirent.h>
1993-03-21 12:45:37 +03:00
#include <signal.h>
#include <setjmp.h>
#include <syslog.h>
1994-05-18 05:25:37 +04:00
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
1997-10-05 15:52:17 +04:00
#include <grp.h>
#include <err.h>
1994-05-18 05:25:37 +04:00
#include "lp.h"
1993-03-21 12:45:37 +03:00
#include "lpc.h"
1994-05-18 05:25:37 +04:00
#include "extern.h"
1997-10-05 15:52:17 +04:00
#ifndef LPR_OPER
#define LPR_OPER "operator" /* group name of lpr operators */
#endif
1994-05-18 05:25:37 +04:00
/*
* lpc -- line printer control program
*/
1993-03-21 12:45:37 +03:00
#define MAX_CMDLINE 200
#define MAX_MARGV 20
1993-03-21 12:45:37 +03:00
int fromatty;
char cmdline[MAX_CMDLINE];
1993-03-21 12:45:37 +03:00
int margc;
char *margv[MAX_MARGV];
1993-03-21 12:45:37 +03:00
int top;
uid_t uid, euid;
1993-03-21 12:45:37 +03:00
jmp_buf toplevel;
1994-05-18 05:25:37 +04:00
static void cmdscanner __P((int));
static struct cmd *getcmd __P((char *));
static void intr __P((int));
static void makeargv __P((void));
1997-10-05 15:52:17 +04:00
static int ingroup __P((char *));
int main __P((int, char *p[]));
1994-05-18 05:25:37 +04:00
int
1993-03-21 12:45:37 +03:00
main(argc, argv)
1994-05-18 05:25:37 +04:00
int argc;
1993-03-21 12:45:37 +03:00
char *argv[];
{
struct cmd *c;
1993-03-21 12:45:37 +03:00
euid = geteuid();
uid = getuid();
seteuid(uid);
1993-03-21 12:45:37 +03:00
name = argv[0];
openlog("lpd", 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);
}
1997-10-05 15:52:17 +04:00
if (c->c_priv && getuid() && ingroup(LPR_OPER) == 0) {
1993-03-21 12:45:37 +03:00
printf("?Privileged command\n");
exit(1);
}
(*c->c_handler)(argc, argv);
exit(0);
}
fromatty = isatty(fileno(stdin));
top = setjmp(toplevel) == 0;
if (top)
signal(SIGINT, intr);
for (;;) {
cmdscanner(top);
top = 1;
}
}
1994-05-18 05:25:37 +04:00
static void
intr(signo)
int signo;
1993-03-21 12:45:37 +03:00
{
if (!fromatty)
exit(0);
longjmp(toplevel, 1);
}
/*
* Command parser.
*/
1994-05-18 05:25:37 +04:00
static void
1993-03-21 12:45:37 +03:00
cmdscanner(top)
int top;
{
struct cmd *c;
1993-03-21 12:45:37 +03:00
if (!top)
putchar('\n');
for (;;) {
if (fromatty) {
printf("lpc> ");
fflush(stdout);
}
if (fgets(cmdline, MAX_CMDLINE, stdin) == 0)
1994-05-18 05:25:37 +04:00
quit(0, NULL);
1993-03-21 12:45:37 +03:00
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;
}
1997-10-05 15:52:17 +04:00
if (c->c_priv && getuid() && ingroup(LPR_OPER) == 0) {
1993-03-21 12:45:37 +03:00
printf("?Privileged command\n");
continue;
}
(*c->c_handler)(margc, margv);
}
longjmp(toplevel, 0);
}
1997-10-05 15:52:17 +04:00
static struct cmd *
1993-03-21 12:45:37 +03:00
getcmd(name)
char *name;
1993-03-21 12:45:37 +03:00
{
char *p, *q;
struct cmd *c, *found;
int nmatches, longest;
1993-03-21 12:45:37 +03:00
longest = 0;
nmatches = 0;
found = 0;
for (c = cmdtab; (p = c->c_name) != NULL; c++) {
1993-03-21 12:45:37 +03:00
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.
*/
1994-05-18 05:25:37 +04:00
static void
1993-03-21 12:45:37 +03:00
makeargv()
{
char *cp;
char **argp = margv;
int n = 0;
1993-03-21 12:45:37 +03:00
margc = 0;
for (cp = cmdline; *cp && (cp - cmdline) < sizeof(cmdline) &&
n < MAX_MARGV; n++) {
1993-03-21 12:45:37 +03:00
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 (sizeof ("directory"))
/*
* Help command.
*/
1994-05-18 05:25:37 +04:00
void
1993-03-21 12:45:37 +03:00
help(argc, argv)
int argc;
char *argv[];
{
struct cmd *c;
1993-03-21 12:45:37 +03:00
if (argc == 1) {
int i, j, w;
1993-03-21 12:45:37 +03:00
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;
1993-03-21 12:45:37 +03:00
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
1997-10-05 20:45:43 +04:00
printf("%-*s\t%s\n", (int)HELPINDENT,
1993-03-21 12:45:37 +03:00
c->c_name, c->c_help);
}
}
1997-10-05 15:52:17 +04:00
/*
* return non-zero if the user is a member of the given group
*/
static int
ingroup(grname)
char *grname;
{
static struct group *gptr=NULL;
static gid_t groups[NGROUPS];
gid_t gid;
int i;
1997-10-05 15:52:17 +04:00
if (gptr == NULL) {
if ((gptr = getgrnam(grname)) == NULL) {
warnx("Warning: unknown group `%s'\n",
1997-10-05 15:52:17 +04:00
grname);
return(0);
}
if (getgroups(NGROUPS, groups) < 0)
err(1, "getgroups");
1997-10-05 15:52:17 +04:00
}
gid = gptr->gr_gid;
for (i = 0; i < NGROUPS; i++)
if (gid == groups[i])
return(1);
return(0);
}