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