xref: /csrg-svn/usr.sbin/lpr/lpc/lpc.c (revision 69063)
122428Sdist /*
261843Sbostic  * Copyright (c) 1983, 1993
361843Sbostic  *	The Regents of the University of California.  All rights reserved.
434203Sbostic  *
556121Selan  *
656250Selan  * %sccs.include.redist.c%
722428Sdist  */
822428Sdist 
912380Sralph #ifndef lint
1056264Selan static char copyright[] =
1161843Sbostic "@(#) Copyright (c) 1983, 1993\n\
1261843Sbostic 	The Regents of the University of California.  All rights reserved.\n";
1334203Sbostic #endif /* not lint */
1412380Sralph 
1522428Sdist #ifndef lint
16*69063Stef static char sccsid[] = "@(#)lpc.c	8.3 (Berkeley) 04/28/95";
1734203Sbostic #endif /* not lint */
1822428Sdist 
1955472Sbostic #include <sys/param.h>
2055472Sbostic 
2155472Sbostic #include <dirent.h>
2212380Sralph #include <signal.h>
2312380Sralph #include <setjmp.h>
2425494Seric #include <syslog.h>
2555472Sbostic #include <unistd.h>
2655472Sbostic #include <stdlib.h>
2755472Sbostic #include <stdio.h>
2855472Sbostic #include <ctype.h>
2955472Sbostic #include <string.h>
30*69063Stef #include <grp.h>
3169027Stef #include <sys/param.h>
3255472Sbostic #include "lp.h"
3312380Sralph #include "lpc.h"
3455472Sbostic #include "extern.h"
3512380Sralph 
3669027Stef #ifndef LPR_OPER
3769027Stef #define LPR_OPER	"operator"	/* group name of lpr operators */
3869027Stef #endif
3969027Stef 
4055472Sbostic /*
4155472Sbostic  * lpc -- line printer control program
4255472Sbostic  */
4355472Sbostic 
4412380Sralph int	fromatty;
4512380Sralph 
4612380Sralph char	cmdline[200];
4712380Sralph int	margc;
4812380Sralph char	*margv[20];
4912380Sralph int	top;
5012380Sralph 
5112380Sralph jmp_buf	toplevel;
5212380Sralph 
5355472Sbostic static void		 cmdscanner __P((int));
5455472Sbostic static struct cmd	*getcmd __P((char *));
5555472Sbostic static void		 intr __P((int));
5655472Sbostic static void		 makeargv __P((void));
5769027Stef static int		 ingroup __P((char *));
5855472Sbostic 
5955472Sbostic int
main(argc,argv)6012380Sralph main(argc, argv)
6155472Sbostic 	int argc;
6212380Sralph 	char *argv[];
6312380Sralph {
6412380Sralph 	register struct cmd *c;
6512380Sralph 
6612744Sralph 	name = argv[0];
6725494Seric 	openlog("lpd", 0, LOG_LPR);
6812744Sralph 
6912380Sralph 	if (--argc > 0) {
7012380Sralph 		c = getcmd(*++argv);
7112380Sralph 		if (c == (struct cmd *)-1) {
7212380Sralph 			printf("?Ambiguous command\n");
7312380Sralph 			exit(1);
7412380Sralph 		}
7512380Sralph 		if (c == 0) {
7612380Sralph 			printf("?Invalid command\n");
7712380Sralph 			exit(1);
7812380Sralph 		}
7969027Stef 		if (c->c_priv && getuid() && ingroup(LPR_OPER) == 0) {
8012380Sralph 			printf("?Privileged command\n");
8112380Sralph 			exit(1);
8212380Sralph 		}
8312380Sralph 		(*c->c_handler)(argc, argv);
8412380Sralph 		exit(0);
8512380Sralph 	}
8612380Sralph 	fromatty = isatty(fileno(stdin));
8712380Sralph 	top = setjmp(toplevel) == 0;
8812380Sralph 	if (top)
8913147Ssam 		signal(SIGINT, intr);
9012380Sralph 	for (;;) {
9112380Sralph 		cmdscanner(top);
9212380Sralph 		top = 1;
9312380Sralph 	}
9412380Sralph }
9512380Sralph 
9655472Sbostic static void
intr(signo)9755472Sbostic intr(signo)
9855472Sbostic 	int signo;
9912380Sralph {
10012380Sralph 	if (!fromatty)
10112380Sralph 		exit(0);
10212380Sralph 	longjmp(toplevel, 1);
10312380Sralph }
10412380Sralph 
10512380Sralph /*
10612380Sralph  * Command parser.
10712380Sralph  */
10855472Sbostic static void
cmdscanner(top)10912380Sralph cmdscanner(top)
11012380Sralph 	int top;
11112380Sralph {
11212380Sralph 	register struct cmd *c;
11312380Sralph 
11412380Sralph 	if (!top)
11512380Sralph 		putchar('\n');
11612380Sralph 	for (;;) {
11712380Sralph 		if (fromatty) {
11812380Sralph 			printf("lpc> ");
11912380Sralph 			fflush(stdout);
12012380Sralph 		}
12136247Sbostic 		if (fgets(cmdline, sizeof(cmdline), stdin) == 0)
12255472Sbostic 			quit(0, NULL);
12340549Stef 		if (cmdline[0] == 0 || cmdline[0] == '\n')
12412380Sralph 			break;
12512380Sralph 		makeargv();
12612380Sralph 		c = getcmd(margv[0]);
12712380Sralph 		if (c == (struct cmd *)-1) {
12812380Sralph 			printf("?Ambiguous command\n");
12912380Sralph 			continue;
13012380Sralph 		}
13112380Sralph 		if (c == 0) {
13212380Sralph 			printf("?Invalid command\n");
13312380Sralph 			continue;
13412380Sralph 		}
13569027Stef 		if (c->c_priv && getuid() && ingroup(LPR_OPER) == 0) {
13612380Sralph 			printf("?Privileged command\n");
13712380Sralph 			continue;
13812380Sralph 		}
13912380Sralph 		(*c->c_handler)(margc, margv);
14012380Sralph 	}
14112380Sralph 	longjmp(toplevel, 0);
14212380Sralph }
14312380Sralph 
14469027Stef static struct cmd *
getcmd(name)14512380Sralph getcmd(name)
14612380Sralph 	register char *name;
14712380Sralph {
14812380Sralph 	register char *p, *q;
14912380Sralph 	register struct cmd *c, *found;
15012380Sralph 	register int nmatches, longest;
15112380Sralph 
15212380Sralph 	longest = 0;
15312380Sralph 	nmatches = 0;
15412380Sralph 	found = 0;
15512380Sralph 	for (c = cmdtab; p = c->c_name; c++) {
15612380Sralph 		for (q = name; *q == *p++; q++)
15712380Sralph 			if (*q == 0)		/* exact match? */
15812380Sralph 				return(c);
15912380Sralph 		if (!*q) {			/* the name was a prefix */
16012380Sralph 			if (q - name > longest) {
16112380Sralph 				longest = q - name;
16212380Sralph 				nmatches = 1;
16312380Sralph 				found = c;
16412380Sralph 			} else if (q - name == longest)
16512380Sralph 				nmatches++;
16612380Sralph 		}
16712380Sralph 	}
16812380Sralph 	if (nmatches > 1)
16912380Sralph 		return((struct cmd *)-1);
17012380Sralph 	return(found);
17112380Sralph }
17212380Sralph 
17312380Sralph /*
17412380Sralph  * Slice a string up into argc/argv.
17512380Sralph  */
17655472Sbostic static void
makeargv()17712380Sralph makeargv()
17812380Sralph {
17912380Sralph 	register char *cp;
18012380Sralph 	register char **argp = margv;
18112380Sralph 
18212380Sralph 	margc = 0;
18312380Sralph 	for (cp = cmdline; *cp;) {
18412380Sralph 		while (isspace(*cp))
18512380Sralph 			cp++;
18612380Sralph 		if (*cp == '\0')
18712380Sralph 			break;
18812380Sralph 		*argp++ = cp;
18912380Sralph 		margc += 1;
19012380Sralph 		while (*cp != '\0' && !isspace(*cp))
19112380Sralph 			cp++;
19212380Sralph 		if (*cp == '\0')
19312380Sralph 			break;
19412380Sralph 		*cp++ = '\0';
19512380Sralph 	}
19612380Sralph 	*argp++ = 0;
19712380Sralph }
19812380Sralph 
19912380Sralph #define HELPINDENT (sizeof ("directory"))
20012380Sralph 
20112380Sralph /*
20212380Sralph  * Help command.
20312380Sralph  */
20455472Sbostic void
help(argc,argv)20512380Sralph help(argc, argv)
20612380Sralph 	int argc;
20712380Sralph 	char *argv[];
20812380Sralph {
20912380Sralph 	register struct cmd *c;
21012380Sralph 
21112380Sralph 	if (argc == 1) {
21212380Sralph 		register int i, j, w;
21312380Sralph 		int columns, width = 0, lines;
21412380Sralph 		extern int NCMDS;
21512380Sralph 
21612380Sralph 		printf("Commands may be abbreviated.  Commands are:\n\n");
21739727Sbostic 		for (c = cmdtab; c->c_name; c++) {
21812380Sralph 			int len = strlen(c->c_name);
21912380Sralph 
22012380Sralph 			if (len > width)
22112380Sralph 				width = len;
22212380Sralph 		}
22312380Sralph 		width = (width + 8) &~ 7;
22412380Sralph 		columns = 80 / width;
22512380Sralph 		if (columns == 0)
22612380Sralph 			columns = 1;
22712380Sralph 		lines = (NCMDS + columns - 1) / columns;
22812380Sralph 		for (i = 0; i < lines; i++) {
22912380Sralph 			for (j = 0; j < columns; j++) {
23012380Sralph 				c = cmdtab + j * lines + i;
23139727Sbostic 				if (c->c_name)
23239727Sbostic 					printf("%s", c->c_name);
23312380Sralph 				if (c + lines >= &cmdtab[NCMDS]) {
23412380Sralph 					printf("\n");
23512380Sralph 					break;
23612380Sralph 				}
23712380Sralph 				w = strlen(c->c_name);
23812380Sralph 				while (w < width) {
23912380Sralph 					w = (w + 8) &~ 7;
24012380Sralph 					putchar('\t');
24112380Sralph 				}
24212380Sralph 			}
24312380Sralph 		}
24412380Sralph 		return;
24512380Sralph 	}
24612380Sralph 	while (--argc > 0) {
24712380Sralph 		register char *arg;
24812380Sralph 		arg = *++argv;
24912380Sralph 		c = getcmd(arg);
25012380Sralph 		if (c == (struct cmd *)-1)
25112380Sralph 			printf("?Ambiguous help command %s\n", arg);
25212380Sralph 		else if (c == (struct cmd *)0)
25312380Sralph 			printf("?Invalid help command %s\n", arg);
25412380Sralph 		else
25512380Sralph 			printf("%-*s\t%s\n", HELPINDENT,
25612380Sralph 				c->c_name, c->c_help);
25712380Sralph 	}
25812380Sralph }
25969027Stef 
26069027Stef /*
26169027Stef  * return non-zero if the user is a member of the given group
26269027Stef  */
26369027Stef static int
ingroup(grname)26469027Stef ingroup(grname)
26569027Stef 	char *grname;
26669027Stef {
26769027Stef 	static struct group *gptr=NULL;
26869027Stef 	static gid_t groups[NGROUPS];
26969027Stef 	register gid_t gid;
27069027Stef 	register int i;
27169027Stef 
27269027Stef 	if (gptr == NULL) {
27369027Stef 		if ((gptr = getgrnam(grname)) == NULL) {
27469027Stef 			fprintf(stderr, "Warning: unknown group '%s'\n",
27569027Stef 				grname);
27669027Stef 			return(0);
27769027Stef 		}
27869027Stef 		if (getgroups(NGROUPS, groups) < 0) {
27969027Stef 			perror("getgroups");
28069027Stef 			exit(1);
28169027Stef 		}
28269027Stef 	}
28369027Stef 	gid = gptr->gr_gid;
28469027Stef 	for (i = 0; i < NGROUPS; i++)
28569027Stef 		if (gid == groups[i])
28669027Stef 			return(1);
28769027Stef 	return(0);
28869027Stef }
289