1996-05-05 14:04:33 +00:00
|
|
|
/*
|
|
|
|
* 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. 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef lint
|
1997-09-24 06:48:24 +00:00
|
|
|
static const char copyright[] =
|
1996-05-05 14:04:33 +00:00
|
|
|
"@(#) Copyright (c) 1983, 1993\n\
|
|
|
|
The Regents of the University of California. All rights reserved.\n";
|
|
|
|
#endif /* not lint */
|
|
|
|
|
1997-09-24 06:48:24 +00:00
|
|
|
#if 0
|
2003-07-14 22:24:28 +00:00
|
|
|
#ifndef lint
|
1996-05-05 14:04:33 +00:00
|
|
|
static char sccsid[] = "@(#)lpc.c 8.3 (Berkeley) 4/28/95";
|
|
|
|
#endif /* not lint */
|
2003-07-14 22:24:28 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "lp.cdefs.h" /* A cross-platform version of <sys/cdefs.h> */
|
|
|
|
__FBSDID("$FreeBSD$");
|
1996-05-05 14:04:33 +00:00
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
|
1997-09-24 06:48:24 +00:00
|
|
|
#include <ctype.h>
|
1996-05-05 14:04:33 +00:00
|
|
|
#include <dirent.h>
|
1997-12-02 20:46:22 +00:00
|
|
|
#include <err.h>
|
1997-09-24 06:48:24 +00:00
|
|
|
#include <grp.h>
|
1996-05-05 14:04:33 +00:00
|
|
|
#include <setjmp.h>
|
1997-09-24 06:48:24 +00:00
|
|
|
#include <signal.h>
|
1996-05-05 14:04:33 +00:00
|
|
|
#include <stdio.h>
|
1997-09-24 06:48:24 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <syslog.h>
|
1996-05-05 14:04:33 +00:00
|
|
|
#include <string.h>
|
1997-09-24 06:48:24 +00:00
|
|
|
#include <unistd.h>
|
1999-08-19 03:29:15 +00:00
|
|
|
#include <histedit.h>
|
1997-12-02 20:46:22 +00:00
|
|
|
|
1996-05-05 14:04:33 +00:00
|
|
|
#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
|
|
|
|
*/
|
|
|
|
|
1997-07-23 00:49:46 +00:00
|
|
|
#define MAX_CMDLINE 200
|
|
|
|
#define MAX_MARGV 20
|
1998-09-11 18:49:33 +00:00
|
|
|
static int fromatty;
|
1996-05-05 14:04:33 +00:00
|
|
|
|
1998-09-11 18:49:33 +00:00
|
|
|
static char cmdline[MAX_CMDLINE];
|
|
|
|
static int margc;
|
|
|
|
static char *margv[MAX_MARGV];
|
|
|
|
uid_t uid, euid;
|
1996-05-05 14:04:33 +00:00
|
|
|
|
2001-06-12 16:38:20 +00:00
|
|
|
int main(int _argc, char *_argv[]);
|
|
|
|
static void cmdscanner(void);
|
|
|
|
static struct cmd *getcmd(const char *_name);
|
|
|
|
static void intr(int _signo);
|
|
|
|
static void makeargv(void);
|
|
|
|
static int ingroup(const char *_grname);
|
1996-05-05 14:04:33 +00:00
|
|
|
|
|
|
|
int
|
2001-06-12 16:38:20 +00:00
|
|
|
main(int argc, char *argv[])
|
1996-05-05 14:04:33 +00:00
|
|
|
{
|
|
|
|
register struct cmd *c;
|
|
|
|
|
1997-07-23 00:49:46 +00:00
|
|
|
euid = geteuid();
|
|
|
|
uid = getuid();
|
|
|
|
seteuid(uid);
|
2001-06-15 16:28:37 +00:00
|
|
|
progname = argv[0];
|
1996-05-05 14:04:33 +00:00
|
|
|
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);
|
|
|
|
}
|
2002-06-15 22:51:58 +00:00
|
|
|
if ((c->c_opts & LPC_PRIVCMD) && getuid() &&
|
|
|
|
ingroup(LPR_OPER) == 0) {
|
1996-05-05 14:04:33 +00:00
|
|
|
printf("?Privileged command\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
1997-12-02 20:46:22 +00:00
|
|
|
if (c->c_generic != 0)
|
2002-06-15 22:51:58 +00:00
|
|
|
generic(c->c_generic, c->c_opts, c->c_handler,
|
|
|
|
argc, argv);
|
1997-12-02 20:46:22 +00:00
|
|
|
else
|
|
|
|
(*c->c_handler)(argc, argv);
|
1996-05-05 14:04:33 +00:00
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
fromatty = isatty(fileno(stdin));
|
1999-08-19 03:29:15 +00:00
|
|
|
if (!fromatty)
|
1996-05-05 14:04:33 +00:00
|
|
|
signal(SIGINT, intr);
|
|
|
|
for (;;) {
|
1999-08-19 03:29:15 +00:00
|
|
|
cmdscanner();
|
1996-05-05 14:04:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2001-06-12 16:38:20 +00:00
|
|
|
intr(int signo __unused)
|
1996-05-05 14:04:33 +00:00
|
|
|
{
|
2001-06-12 16:38:20 +00:00
|
|
|
/* (the '__unused' is just to avoid a compile-time warning) */
|
1999-08-19 03:29:15 +00:00
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
2001-06-12 16:38:20 +00:00
|
|
|
static const char *
|
2000-11-06 19:19:49 +00:00
|
|
|
lpc_prompt(void)
|
1999-08-19 03:29:15 +00:00
|
|
|
{
|
2000-11-06 19:19:49 +00:00
|
|
|
return ("lpc> ");
|
1996-05-05 14:04:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Command parser.
|
|
|
|
*/
|
|
|
|
static void
|
2001-06-12 16:38:20 +00:00
|
|
|
cmdscanner(void)
|
1996-05-05 14:04:33 +00:00
|
|
|
{
|
|
|
|
register struct cmd *c;
|
2001-07-15 03:10:54 +00:00
|
|
|
static EditLine *el;
|
|
|
|
static History *hist;
|
2001-10-01 08:43:58 +00:00
|
|
|
HistEvent he;
|
2001-07-15 03:10:54 +00:00
|
|
|
size_t len;
|
|
|
|
int num;
|
|
|
|
const char *bp;
|
|
|
|
|
|
|
|
num = 0;
|
|
|
|
bp = NULL;
|
|
|
|
el = NULL;
|
|
|
|
hist = NULL;
|
1996-05-05 14:04:33 +00:00
|
|
|
for (;;) {
|
|
|
|
if (fromatty) {
|
1999-08-19 03:29:15 +00:00
|
|
|
if (!el) {
|
2001-10-01 08:43:58 +00:00
|
|
|
el = el_init("lpc", stdin, stdout, stderr);
|
1999-08-19 03:29:15 +00:00
|
|
|
hist = history_init();
|
2001-10-01 08:43:58 +00:00
|
|
|
history(hist, &he, H_EVENT, 100);
|
1999-08-19 03:29:15 +00:00
|
|
|
el_set(el, EL_HIST, history, hist);
|
|
|
|
el_set(el, EL_EDITOR, "emacs");
|
|
|
|
el_set(el, EL_PROMPT, lpc_prompt);
|
|
|
|
el_set(el, EL_SIGNAL, 1);
|
1999-08-19 04:10:33 +00:00
|
|
|
el_source(el, NULL);
|
2001-09-17 02:35:34 +00:00
|
|
|
/*
|
|
|
|
* EditLine init may call 'cgetset()' to set a
|
|
|
|
* capability-db meant for termcap (eg: to set
|
|
|
|
* terminal type 'xterm'). Reset that now, or
|
|
|
|
* that same db-information will be used for
|
|
|
|
* printcap (giving us an "xterm" printer, with
|
|
|
|
* all kinds of invalid capabilities...).
|
|
|
|
*/
|
|
|
|
cgetset(NULL);
|
1999-08-19 03:29:15 +00:00
|
|
|
}
|
|
|
|
if ((bp = el_gets(el, &num)) == NULL || num == 0)
|
2000-06-30 20:05:21 +00:00
|
|
|
quit(0, NULL);
|
1999-08-19 03:29:15 +00:00
|
|
|
|
1999-08-20 07:14:46 +00:00
|
|
|
len = (num > MAX_CMDLINE) ? MAX_CMDLINE : num;
|
1999-08-20 01:24:35 +00:00
|
|
|
memcpy(cmdline, bp, len);
|
|
|
|
cmdline[len] = 0;
|
2001-10-01 08:43:58 +00:00
|
|
|
history(hist, &he, H_ENTER, bp);
|
1999-08-19 03:29:15 +00:00
|
|
|
|
|
|
|
} else {
|
|
|
|
if (fgets(cmdline, MAX_CMDLINE, stdin) == 0)
|
|
|
|
quit(0, NULL);
|
|
|
|
if (cmdline[0] == 0 || cmdline[0] == '\n')
|
|
|
|
break;
|
1996-05-05 14:04:33 +00:00
|
|
|
}
|
1999-08-19 03:29:15 +00:00
|
|
|
|
1996-05-05 14:04:33 +00:00
|
|
|
makeargv();
|
1999-08-19 03:29:15 +00:00
|
|
|
if (margc == 0)
|
|
|
|
continue;
|
|
|
|
if (el_parse(el, margc, margv) != -1)
|
|
|
|
continue;
|
|
|
|
|
1996-05-05 14:04:33 +00:00
|
|
|
c = getcmd(margv[0]);
|
|
|
|
if (c == (struct cmd *)-1) {
|
|
|
|
printf("?Ambiguous command\n");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (c == 0) {
|
|
|
|
printf("?Invalid command\n");
|
|
|
|
continue;
|
|
|
|
}
|
2002-06-15 22:51:58 +00:00
|
|
|
if ((c->c_opts & LPC_PRIVCMD) && getuid() &&
|
|
|
|
ingroup(LPR_OPER) == 0) {
|
1996-05-05 14:04:33 +00:00
|
|
|
printf("?Privileged command\n");
|
|
|
|
continue;
|
|
|
|
}
|
2001-06-25 02:05:03 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Two different commands might have the same generic rtn
|
|
|
|
* (eg: "clean" and "tclean"), and just use different
|
|
|
|
* handler routines for distinct command-setup. The handler
|
|
|
|
* routine might also be set on a generic routine for
|
|
|
|
* initial parameter processing.
|
|
|
|
*/
|
1997-12-02 20:46:22 +00:00
|
|
|
if (c->c_generic != 0)
|
2002-06-15 22:51:58 +00:00
|
|
|
generic(c->c_generic, c->c_opts, c->c_handler,
|
|
|
|
margc, margv);
|
1997-12-02 20:46:22 +00:00
|
|
|
else
|
|
|
|
(*c->c_handler)(margc, margv);
|
1996-05-05 14:04:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct cmd *
|
2001-06-12 16:38:20 +00:00
|
|
|
getcmd(const char *name)
|
1996-05-05 14:04:33 +00:00
|
|
|
{
|
2001-06-12 16:38:20 +00:00
|
|
|
register const char *p, *q;
|
1996-05-05 14:04:33 +00:00
|
|
|
register struct cmd *c, *found;
|
|
|
|
register int nmatches, longest;
|
|
|
|
|
|
|
|
longest = 0;
|
|
|
|
nmatches = 0;
|
|
|
|
found = 0;
|
1996-10-27 03:06:52 +00:00
|
|
|
for (c = cmdtab; (p = c->c_name); c++) {
|
1996-05-05 14:04:33 +00: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.
|
|
|
|
*/
|
|
|
|
static void
|
2001-06-12 16:38:20 +00:00
|
|
|
makeargv(void)
|
1996-05-05 14:04:33 +00:00
|
|
|
{
|
|
|
|
register char *cp;
|
|
|
|
register char **argp = margv;
|
1997-07-23 00:49:46 +00:00
|
|
|
register int n = 0;
|
1996-05-05 14:04:33 +00:00
|
|
|
|
|
|
|
margc = 0;
|
2001-07-23 01:13:20 +00:00
|
|
|
for (cp = cmdline; *cp && (size_t)(cp - cmdline) < sizeof(cmdline) &&
|
1997-07-23 00:49:46 +00:00
|
|
|
n < MAX_MARGV; n++) {
|
1996-05-05 14:04:33 +00: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.
|
|
|
|
*/
|
|
|
|
void
|
2001-06-12 16:38:20 +00:00
|
|
|
help(int argc, char *argv[])
|
1996-05-05 14:04:33 +00:00
|
|
|
{
|
|
|
|
register struct cmd *c;
|
|
|
|
|
|
|
|
if (argc == 1) {
|
|
|
|
register 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) {
|
|
|
|
register 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
|
1998-03-22 20:19:27 +00:00
|
|
|
printf("%-*s\t%s\n", (int) HELPINDENT,
|
1996-05-05 14:04:33 +00:00
|
|
|
c->c_name, c->c_help);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* return non-zero if the user is a member of the given group
|
|
|
|
*/
|
|
|
|
static int
|
2001-06-12 16:38:20 +00:00
|
|
|
ingroup(const char *grname)
|
1996-05-05 14:04:33 +00:00
|
|
|
{
|
|
|
|
static struct group *gptr=NULL;
|
2000-08-23 23:36:32 +00:00
|
|
|
static int ngroups = 0;
|
1996-05-05 14:04:33 +00:00
|
|
|
static gid_t groups[NGROUPS];
|
|
|
|
register gid_t gid;
|
|
|
|
register int i;
|
|
|
|
|
|
|
|
if (gptr == NULL) {
|
|
|
|
if ((gptr = getgrnam(grname)) == NULL) {
|
1997-09-24 06:48:24 +00:00
|
|
|
warnx("warning: unknown group '%s'", grname);
|
1996-05-05 14:04:33 +00:00
|
|
|
return(0);
|
|
|
|
}
|
2000-08-23 23:36:32 +00:00
|
|
|
ngroups = getgroups(NGROUPS, groups);
|
|
|
|
if (ngroups < 0)
|
1997-09-24 06:48:24 +00:00
|
|
|
err(1, "getgroups");
|
1996-05-05 14:04:33 +00:00
|
|
|
}
|
|
|
|
gid = gptr->gr_gid;
|
2000-08-23 23:36:32 +00:00
|
|
|
for (i = 0; i < ngroups; i++)
|
1996-05-05 14:04:33 +00:00
|
|
|
if (gid == groups[i])
|
|
|
|
return(1);
|
|
|
|
return(0);
|
|
|
|
}
|
2002-07-17 00:51:19 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Routine to get the information for a single printer (which will be
|
|
|
|
* called by the routines which implement individual commands).
|
|
|
|
* Note: This is for commands operating on a *single* printer.
|
|
|
|
*/
|
|
|
|
struct printer *
|
|
|
|
setup_myprinter(char *pwanted, struct printer *pp, int sump_opts)
|
|
|
|
{
|
|
|
|
int cdres, cmdstatus;
|
|
|
|
|
|
|
|
init_printer(pp);
|
|
|
|
cmdstatus = getprintcap(pwanted, pp);
|
|
|
|
switch (cmdstatus) {
|
|
|
|
default:
|
|
|
|
fatal(pp, "%s", pcaperr(cmdstatus));
|
|
|
|
/* NOTREACHED */
|
|
|
|
case PCAPERR_NOTFOUND:
|
|
|
|
printf("unknown printer %s\n", pwanted);
|
|
|
|
return (NULL);
|
|
|
|
case PCAPERR_TCOPEN:
|
|
|
|
printf("warning: %s: unresolved tc= reference(s)", pwanted);
|
|
|
|
break;
|
|
|
|
case PCAPERR_SUCCESS:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if ((sump_opts & SUMP_NOHEADER) == 0)
|
|
|
|
printf("%s:\n", pp->printer);
|
|
|
|
|
|
|
|
if (sump_opts & SUMP_CHDIR_SD) {
|
|
|
|
seteuid(euid);
|
|
|
|
cdres = chdir(pp->spool_dir);
|
|
|
|
seteuid(uid);
|
|
|
|
if (cdres < 0) {
|
|
|
|
printf("\tcannot chdir to %s\n", pp->spool_dir);
|
|
|
|
free_printer(pp);
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return (pp);
|
|
|
|
}
|