xref: /onnv-gate/usr/src/cmd/lp/lib/printers/getprinter.c (revision 3125:084bca4d4623)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*3125Sjacobs  * Common Development and Distribution License (the "License").
6*3125Sjacobs  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
21*3125Sjacobs 
22*3125Sjacobs /*
23*3125Sjacobs  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
24*3125Sjacobs  * Use is subject to license terms.
25*3125Sjacobs  */
26*3125Sjacobs 
270Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
280Sstevel@tonic-gate /*	  All Rights Reserved  	*/
290Sstevel@tonic-gate 
300Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
310Sstevel@tonic-gate 
320Sstevel@tonic-gate /* EMACS_MODES: !fill, lnumb, !overwrite, !nodelete, !picture */
330Sstevel@tonic-gate 
340Sstevel@tonic-gate #include "stdio.h"
350Sstevel@tonic-gate #include "string.h"
360Sstevel@tonic-gate #include "errno.h"
370Sstevel@tonic-gate #include "sys/types.h"
380Sstevel@tonic-gate #include "stdlib.h"
39*3125Sjacobs #include <syslog.h>
400Sstevel@tonic-gate 
410Sstevel@tonic-gate #include "lp.h"
420Sstevel@tonic-gate #include "printers.h"
430Sstevel@tonic-gate 
440Sstevel@tonic-gate extern struct {
450Sstevel@tonic-gate 	char			*v;
460Sstevel@tonic-gate 	short			len,
470Sstevel@tonic-gate 				okremote;
480Sstevel@tonic-gate }			prtrheadings[];
490Sstevel@tonic-gate 
500Sstevel@tonic-gate /**
510Sstevel@tonic-gate  ** getprinter() - EXTRACT PRINTER STRUCTURE FROM DISK FILE
520Sstevel@tonic-gate  **/
530Sstevel@tonic-gate 
540Sstevel@tonic-gate PRINTER *
getprinter(char * name)550Sstevel@tonic-gate getprinter(char *name)
560Sstevel@tonic-gate {
570Sstevel@tonic-gate 	static long		lastdir		= -1;
580Sstevel@tonic-gate 
59*3125Sjacobs 	PRINTER		*prp;
600Sstevel@tonic-gate 
610Sstevel@tonic-gate 	char			buf[BUFSIZ];
620Sstevel@tonic-gate 
630Sstevel@tonic-gate 	short			daisy;
640Sstevel@tonic-gate 
650Sstevel@tonic-gate 	int			fld;
660Sstevel@tonic-gate 
670Sstevel@tonic-gate 	int fd;
680Sstevel@tonic-gate 
690Sstevel@tonic-gate 	FALERT			*pa;
700Sstevel@tonic-gate 
710Sstevel@tonic-gate 	register char *		p;
720Sstevel@tonic-gate 	register char **	pp;
730Sstevel@tonic-gate 	register char ***	ppp;
740Sstevel@tonic-gate 	register char *		path;
750Sstevel@tonic-gate 	int			isNameAll;
760Sstevel@tonic-gate 
770Sstevel@tonic-gate 
780Sstevel@tonic-gate 
790Sstevel@tonic-gate 	if (!name || !*name) {
800Sstevel@tonic-gate 		errno = EINVAL;
810Sstevel@tonic-gate 		return (0);
820Sstevel@tonic-gate 	}
830Sstevel@tonic-gate 
84*3125Sjacobs 	syslog(LOG_DEBUG, "getprinter(%s)", name ? name : "");
850Sstevel@tonic-gate 	/*
860Sstevel@tonic-gate 	 * Getting ``all''? If so, jump into the directory
870Sstevel@tonic-gate 	 * wherever we left off.
880Sstevel@tonic-gate 	 */
890Sstevel@tonic-gate 	isNameAll = STREQU(NAME_ALL, name);
900Sstevel@tonic-gate 	for (; ; ) {
910Sstevel@tonic-gate 		/* fix for bug 1117241
920Sstevel@tonic-gate 		 * occasionally when a printer is removed, a printer directory
930Sstevel@tonic-gate 		 * is left behind, but the CONFIGFILE is removed.  In this
940Sstevel@tonic-gate 		 * case this directory terminates the search for additional
950Sstevel@tonic-gate 		 * printers as we have been returning 0 in this case.
960Sstevel@tonic-gate 		 * Now, we loop back and try the next directory until
970Sstevel@tonic-gate 		 * we have no more directories or we find a directory with
980Sstevel@tonic-gate 		 * a CONFIGFILE
990Sstevel@tonic-gate 		 */
1000Sstevel@tonic-gate 		if (isNameAll) {
1010Sstevel@tonic-gate 			if (!(name = next_dir(Lp_A_Printers, &lastdir)))
1020Sstevel@tonic-gate 				return (0);
1030Sstevel@tonic-gate 		} else
1040Sstevel@tonic-gate 			lastdir = -1;
1050Sstevel@tonic-gate 
1060Sstevel@tonic-gate 		/*
1070Sstevel@tonic-gate 		 * Get the printer configuration information.
1080Sstevel@tonic-gate 		 */
1090Sstevel@tonic-gate 
1100Sstevel@tonic-gate 		path = getprinterfile(name, CONFIGFILE);
1110Sstevel@tonic-gate 		if (!path) {
1120Sstevel@tonic-gate 			if (isNameAll)
1130Sstevel@tonic-gate 				Free(name);
1140Sstevel@tonic-gate 			return (0);
1150Sstevel@tonic-gate 		}
1160Sstevel@tonic-gate 
1170Sstevel@tonic-gate 		if ((fd = open_locked(path, "r", 0)) < 0) {
1180Sstevel@tonic-gate 			Free(path);	/*
1190Sstevel@tonic-gate 					 * go around to loop again for
1200Sstevel@tonic-gate 					 * NAME_ALL case
1210Sstevel@tonic-gate 					 */
1220Sstevel@tonic-gate 
1230Sstevel@tonic-gate 			if (!isNameAll) /* fix for bug 1117241 */
1240Sstevel@tonic-gate 				return(0);
1250Sstevel@tonic-gate 			else
1260Sstevel@tonic-gate 				Free(name);
1270Sstevel@tonic-gate 		}
1280Sstevel@tonic-gate 		else
1290Sstevel@tonic-gate 			break;
1300Sstevel@tonic-gate 	}
1310Sstevel@tonic-gate 	Free (path);
1320Sstevel@tonic-gate 
1330Sstevel@tonic-gate 	/*
1340Sstevel@tonic-gate 	 * Initialize the entire structure, to ensure no random
1350Sstevel@tonic-gate 	 * values get in it. However, make sure some values won't
1360Sstevel@tonic-gate 	 * be null or empty. Do the latter here as opposed to
1370Sstevel@tonic-gate 	 * after reading the file, because sometimes the file
1380Sstevel@tonic-gate 	 * contains an empty header to FORCE a null/empty value.
1390Sstevel@tonic-gate 	 */
140*3125Sjacobs 	prp = calloc(sizeof (*prp), 1);
141*3125Sjacobs 	prp->name = Strdup(name);
1420Sstevel@tonic-gate 	if (isNameAll)
1430Sstevel@tonic-gate 		Free(name);
144*3125Sjacobs 	prp->printer_types = getlist(NAME_UNKNOWN, LP_WS, LP_SEP);
145*3125Sjacobs 	prp->input_types = getlist(NAME_SIMPLE, LP_WS, LP_SEP);
1460Sstevel@tonic-gate #if	defined(CAN_DO_MODULES)
147*3125Sjacobs 	prp->modules = getlist(NAME_DEFAULT, LP_WS, LP_SEP);
1480Sstevel@tonic-gate #endif
1490Sstevel@tonic-gate 
1500Sstevel@tonic-gate 	/*
1510Sstevel@tonic-gate 	 * Read the file.
1520Sstevel@tonic-gate 	 */
1530Sstevel@tonic-gate 	errno = 0;
1540Sstevel@tonic-gate 	while (fdgets(buf, BUFSIZ, fd) != NULL) {
1550Sstevel@tonic-gate 
1560Sstevel@tonic-gate 		buf[strlen(buf) - 1] = 0;
1570Sstevel@tonic-gate 
1580Sstevel@tonic-gate 		for (fld = 0; fld < PR_MAX; fld++)
1590Sstevel@tonic-gate 			if (
1600Sstevel@tonic-gate 				prtrheadings[fld].v
1610Sstevel@tonic-gate 			     && prtrheadings[fld].len
1620Sstevel@tonic-gate 			     && STRNEQU(
1630Sstevel@tonic-gate 					buf,
1640Sstevel@tonic-gate 					prtrheadings[fld].v,
1650Sstevel@tonic-gate 					prtrheadings[fld].len
1660Sstevel@tonic-gate 				)
1670Sstevel@tonic-gate 			) {
1680Sstevel@tonic-gate 				p = buf + prtrheadings[fld].len;
1690Sstevel@tonic-gate 				while (*p && *p == ' ')
1700Sstevel@tonic-gate 					p++;
1710Sstevel@tonic-gate 				break;
1720Sstevel@tonic-gate 			}
1730Sstevel@tonic-gate 
1740Sstevel@tonic-gate 		/*
1750Sstevel@tonic-gate 		 * To allow future extensions to not impact applications
1760Sstevel@tonic-gate 		 * using old versions of this routine, ignore strange
1770Sstevel@tonic-gate 		 * fields.
1780Sstevel@tonic-gate 		 */
1790Sstevel@tonic-gate 		if (fld >= PR_MAX)
1800Sstevel@tonic-gate 			continue;
1810Sstevel@tonic-gate 
1820Sstevel@tonic-gate 		switch (fld) {
1830Sstevel@tonic-gate 
1840Sstevel@tonic-gate 		case PR_BAN:
1850Sstevel@tonic-gate 			if ((pp = getlist(p, LP_WS, ":"))) {
1860Sstevel@tonic-gate 				if (pp[0] != NULL) {
1870Sstevel@tonic-gate 					if (strcmp(pp[0], NAME_OPTIONAL) == 0)
188*3125Sjacobs 						prp->banner = BAN_OPTIONAL;
1890Sstevel@tonic-gate 					else if (strcmp(pp[0], NAME_OFF) == 0)
190*3125Sjacobs 						prp->banner = BAN_NEVER;
1910Sstevel@tonic-gate 					else if (strcmp(pp[0], NAME_ON) == 0)
192*3125Sjacobs 						prp->banner = BAN_ALWAYS;
1930Sstevel@tonic-gate 					else /* default to the LP default */
194*3125Sjacobs 						prp->banner = BAN_ALWAYS;
1950Sstevel@tonic-gate 				}
1960Sstevel@tonic-gate 				if (pp[1] && CS_STREQU(pp[1], NAME_ALWAYS))
197*3125Sjacobs 					prp->banner |= BAN_ALWAYS;
1980Sstevel@tonic-gate 				freelist (pp);
1990Sstevel@tonic-gate 			}
2000Sstevel@tonic-gate 			break;
2010Sstevel@tonic-gate 
2020Sstevel@tonic-gate 		case PR_LOGIN:
203*3125Sjacobs 			prp->login = LOG_IN;
2040Sstevel@tonic-gate 			break;
2050Sstevel@tonic-gate 
2060Sstevel@tonic-gate 		case PR_CPI:
207*3125Sjacobs 			prp->cpi = getcpi(p);
2080Sstevel@tonic-gate 			break;
2090Sstevel@tonic-gate 
2100Sstevel@tonic-gate 		case PR_LPI:
211*3125Sjacobs 			prp->lpi = getsdn(p);
2120Sstevel@tonic-gate 			break;
2130Sstevel@tonic-gate 
2140Sstevel@tonic-gate 		case PR_LEN:
215*3125Sjacobs 			prp->plen = getsdn(p);
2160Sstevel@tonic-gate 			break;
2170Sstevel@tonic-gate 
2180Sstevel@tonic-gate 		case PR_WIDTH:
219*3125Sjacobs 			prp->pwid = getsdn(p);
2200Sstevel@tonic-gate 			break;
2210Sstevel@tonic-gate 
2220Sstevel@tonic-gate 		case PR_CS:
223*3125Sjacobs 			ppp = &(prp->char_sets);
2240Sstevel@tonic-gate 			goto CharStarStar;
2250Sstevel@tonic-gate 
2260Sstevel@tonic-gate 		case PR_ITYPES:
227*3125Sjacobs 			ppp = &(prp->input_types);
2280Sstevel@tonic-gate CharStarStar:		if (*ppp)
2290Sstevel@tonic-gate 				freelist (*ppp);
2300Sstevel@tonic-gate 			*ppp = getlist(p, LP_WS, LP_SEP);
2310Sstevel@tonic-gate 			break;
2320Sstevel@tonic-gate 
2330Sstevel@tonic-gate 		case PR_DEV:
234*3125Sjacobs 			pp = &(prp->device);
2350Sstevel@tonic-gate 			goto CharStar;
2360Sstevel@tonic-gate 
2370Sstevel@tonic-gate 		case PR_DIAL:
238*3125Sjacobs 			pp = &(prp->dial_info);
2390Sstevel@tonic-gate 			goto CharStar;
2400Sstevel@tonic-gate 
2410Sstevel@tonic-gate 		case PR_RECOV:
242*3125Sjacobs 			pp = &(prp->fault_rec);
2430Sstevel@tonic-gate 			goto CharStar;
2440Sstevel@tonic-gate 
2450Sstevel@tonic-gate 		case PR_INTFC:
246*3125Sjacobs 			pp = &(prp->interface);
2470Sstevel@tonic-gate 			goto CharStar;
2480Sstevel@tonic-gate 
2490Sstevel@tonic-gate 		case PR_PTYPE:
250*3125Sjacobs 			ppp = &(prp->printer_types);
2510Sstevel@tonic-gate 			goto CharStarStar;
2520Sstevel@tonic-gate 
2530Sstevel@tonic-gate 		case PR_REMOTE:
254*3125Sjacobs 			pp = &(prp->remote);
2550Sstevel@tonic-gate 			goto CharStar;
2560Sstevel@tonic-gate 
2570Sstevel@tonic-gate 		case PR_SPEED:
258*3125Sjacobs 			pp = &(prp->speed);
2590Sstevel@tonic-gate 			goto CharStar;
2600Sstevel@tonic-gate 
2610Sstevel@tonic-gate 		case PR_STTY:
262*3125Sjacobs 			pp = &(prp->stty);
2630Sstevel@tonic-gate CharStar:		if (*pp)
2640Sstevel@tonic-gate 				Free (*pp);
2650Sstevel@tonic-gate 			*pp = Strdup(p);
2660Sstevel@tonic-gate 			break;
2670Sstevel@tonic-gate 
2680Sstevel@tonic-gate #if	defined(CAN_DO_MODULES)
2690Sstevel@tonic-gate 		case PR_MODULES:
270*3125Sjacobs 			ppp = &(prp->modules);
2710Sstevel@tonic-gate 			goto CharStarStar;
2720Sstevel@tonic-gate #endif
2730Sstevel@tonic-gate 
2740Sstevel@tonic-gate 		case PR_OPTIONS:
275*3125Sjacobs 			ppp = &(prp->options);
2760Sstevel@tonic-gate 			goto CharStarStar;
2770Sstevel@tonic-gate 			break;
2780Sstevel@tonic-gate 
2790Sstevel@tonic-gate 		case PR_PPD:
2800Sstevel@tonic-gate 		{
281*3125Sjacobs 			pp = &(prp->ppd);
2820Sstevel@tonic-gate 			goto CharStar;
2830Sstevel@tonic-gate 		}
2840Sstevel@tonic-gate 		}
2850Sstevel@tonic-gate 
2860Sstevel@tonic-gate 	}
2870Sstevel@tonic-gate 	if (errno != 0) {
2880Sstevel@tonic-gate 		int			save_errno = errno;
2890Sstevel@tonic-gate 
290*3125Sjacobs 		freeprinter (prp);
2910Sstevel@tonic-gate 		close(fd);
2920Sstevel@tonic-gate 		errno = save_errno;
2930Sstevel@tonic-gate 		return (0);
2940Sstevel@tonic-gate 	}
2950Sstevel@tonic-gate 	close(fd);
2960Sstevel@tonic-gate 
2970Sstevel@tonic-gate 	/*
2980Sstevel@tonic-gate 	 * Get the printer description (if it exists).
2990Sstevel@tonic-gate 	 */
300*3125Sjacobs 	if (!(path = getprinterfile(prp->name, COMMENTFILE)))
3010Sstevel@tonic-gate 		return (0);
302*3125Sjacobs 	if (!(prp->description = loadstring(path)) && errno != ENOENT) {
3030Sstevel@tonic-gate 		Free (path);
304*3125Sjacobs 		freeprinter (prp);
3050Sstevel@tonic-gate 		return (0);
3060Sstevel@tonic-gate 	}
3070Sstevel@tonic-gate 	Free (path);
3080Sstevel@tonic-gate 
3090Sstevel@tonic-gate 	/*
3100Sstevel@tonic-gate 	 * Get the information for the alert. Don't fail if we can't
3110Sstevel@tonic-gate 	 * read it because of access permission UNLESS we're "root"
3120Sstevel@tonic-gate 	 * or "lp"
3130Sstevel@tonic-gate 	 */
314*3125Sjacobs 	if (!(pa = getalert(Lp_A_Printers, prp->name))) {
3150Sstevel@tonic-gate 		if (
3160Sstevel@tonic-gate 			errno != ENOENT
3170Sstevel@tonic-gate 		     && (
3180Sstevel@tonic-gate 				errno != EACCES
3190Sstevel@tonic-gate 			     || !getpid()		  /* we be root */
3200Sstevel@tonic-gate 			     || STREQU(getname(), LPUSER) /* we be lp   */
3210Sstevel@tonic-gate 			)
3220Sstevel@tonic-gate 		) {
323*3125Sjacobs 			freeprinter (prp);
3240Sstevel@tonic-gate 			return (0);
3250Sstevel@tonic-gate 		}
3260Sstevel@tonic-gate 	} else
327*3125Sjacobs 		prp->fault_alert = *pa;
3280Sstevel@tonic-gate 
3290Sstevel@tonic-gate 	/*
3300Sstevel@tonic-gate 	 * Now go through the structure and see if we have
3310Sstevel@tonic-gate 	 * anything strange.
3320Sstevel@tonic-gate 	 */
333*3125Sjacobs 	if (!okprinter(prp->name, prp, 0)) {
334*3125Sjacobs 		freeprinter (prp);
3350Sstevel@tonic-gate 		errno = EBADF;
3360Sstevel@tonic-gate 		return (0);
3370Sstevel@tonic-gate 	}
3380Sstevel@tonic-gate 
3390Sstevel@tonic-gate 	/*
3400Sstevel@tonic-gate 	 * Just in case somebody tried to pull a fast one
3410Sstevel@tonic-gate 	 * by giving a printer type header by itself....
3420Sstevel@tonic-gate 	 */
343*3125Sjacobs 	if (!prp->printer_types)
344*3125Sjacobs 		prp->printer_types = getlist(NAME_UNKNOWN, LP_WS, LP_SEP);
3450Sstevel@tonic-gate 
3460Sstevel@tonic-gate 	/*
3470Sstevel@tonic-gate 	 * If there are more than one printer type, then we can't
3480Sstevel@tonic-gate 	 * have any input types, except perhaps ``simple''.
3490Sstevel@tonic-gate 	 */
3500Sstevel@tonic-gate 	if (
351*3125Sjacobs 		lenlist(prp->printer_types) > 1
352*3125Sjacobs 	     && prp->input_types
3530Sstevel@tonic-gate 	     && (
354*3125Sjacobs 			lenlist(prp->input_types) > 1
355*3125Sjacobs 		     || !STREQU(NAME_SIMPLE, *prp->input_types)
3560Sstevel@tonic-gate 		)
3570Sstevel@tonic-gate 	) {
358*3125Sjacobs 		freeprinter (prp);
3590Sstevel@tonic-gate 		badprinter = BAD_ITYPES;
3600Sstevel@tonic-gate 		errno = EBADF;
3610Sstevel@tonic-gate 		return (0);
3620Sstevel@tonic-gate 	}
3630Sstevel@tonic-gate 
3640Sstevel@tonic-gate 	/*
3650Sstevel@tonic-gate 	 * If there are more than one printer types, none can
3660Sstevel@tonic-gate 	 * be ``unknown''.
3670Sstevel@tonic-gate 	 */
3680Sstevel@tonic-gate 	if (
369*3125Sjacobs 		lenlist(prp->printer_types) > 1
370*3125Sjacobs 	     && searchlist(NAME_UNKNOWN, prp->printer_types)
3710Sstevel@tonic-gate 	) {
372*3125Sjacobs 		freeprinter (prp);
3730Sstevel@tonic-gate 		badprinter = BAD_PTYPES;
3740Sstevel@tonic-gate 		errno = EBADF;
3750Sstevel@tonic-gate 		return (0);
3760Sstevel@tonic-gate 	}
3770Sstevel@tonic-gate 
3780Sstevel@tonic-gate 	/*
3790Sstevel@tonic-gate 	 * All the printer types had better agree on whether the
3800Sstevel@tonic-gate 	 * printer takes print wheels!
3810Sstevel@tonic-gate 	 */
382*3125Sjacobs 	prp->daisy = -1;
383*3125Sjacobs 	for (pp = prp->printer_types; *pp; pp++) {
3840Sstevel@tonic-gate 		tidbit (*pp, "daisy", &daisy);
3850Sstevel@tonic-gate 		if (daisy == -1)
3860Sstevel@tonic-gate 			daisy = 0;
387*3125Sjacobs 		if (prp->daisy == -1)
388*3125Sjacobs 			prp->daisy = daisy;
389*3125Sjacobs 		else if (prp->daisy != daisy) {
390*3125Sjacobs 			freeprinter (prp);
3910Sstevel@tonic-gate 			badprinter = BAD_DAISY;
3920Sstevel@tonic-gate 			errno = EBADF;
3930Sstevel@tonic-gate 			return (0);
3940Sstevel@tonic-gate 		}
3950Sstevel@tonic-gate 	}
3960Sstevel@tonic-gate 
3970Sstevel@tonic-gate 	/*
3980Sstevel@tonic-gate 	 * Help out those who are still using the obsolete
3990Sstevel@tonic-gate 	 * "printer_type" member.
4000Sstevel@tonic-gate 	 */
401*3125Sjacobs 	prp->printer_type = Strdup(*prp->printer_types);
4020Sstevel@tonic-gate 
403*3125Sjacobs 	return (prp);
4040Sstevel@tonic-gate }
405