17c478bdstevel@tonic-gate/*
27c478bdstevel@tonic-gate * CDDL HEADER START
37c478bdstevel@tonic-gate *
47c478bdstevel@tonic-gate * The contents of this file are subject to the terms of the
57c478bdstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
67c478bdstevel@tonic-gate * (the "License").  You may not use this file except in compliance
77c478bdstevel@tonic-gate * with the License.
87c478bdstevel@tonic-gate *
97c478bdstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bdstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
117c478bdstevel@tonic-gate * See the License for the specific language governing permissions
127c478bdstevel@tonic-gate * and limitations under the License.
137c478bdstevel@tonic-gate *
147c478bdstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
157c478bdstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bdstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
177c478bdstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
187c478bdstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bdstevel@tonic-gate *
207c478bdstevel@tonic-gate * CDDL HEADER END
217c478bdstevel@tonic-gate */
227c478bdstevel@tonic-gate/*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
237c478bdstevel@tonic-gate/*	  All Rights Reserved  	*/
247c478bdstevel@tonic-gate
257c478bdstevel@tonic-gate
267c478bdstevel@tonic-gate/*
277c478bdstevel@tonic-gate * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
287c478bdstevel@tonic-gate * Use is subject to license terms.
297c478bdstevel@tonic-gate */
307c478bdstevel@tonic-gate
317c478bdstevel@tonic-gate#pragma ident	"%Z%%M%	%I%	%E% SMI"
327c478bdstevel@tonic-gate
337c478bdstevel@tonic-gate/* EMACS_MODES: !fill, lnumb, !overwrite, !nodelete, !picture */
347c478bdstevel@tonic-gate
357c478bdstevel@tonic-gate#include "sys/types.h"
367c478bdstevel@tonic-gate#include "sys/stat.h"
377c478bdstevel@tonic-gate#include "stdio.h"
387c478bdstevel@tonic-gate#include "string.h"
397c478bdstevel@tonic-gate#include "errno.h"
407c478bdstevel@tonic-gate#include "stdlib.h"
417c478bdstevel@tonic-gate
427c478bdstevel@tonic-gate#include "lp.h"
437c478bdstevel@tonic-gate#include "printers.h"
447c478bdstevel@tonic-gate
457c478bdstevel@tonic-gate#include <unistd.h>
467c478bdstevel@tonic-gate#include <sys/wait.h>
477c478bdstevel@tonic-gate
487c478bdstevel@tonic-gate#define	SHELL "/bin/sh"
497c478bdstevel@tonic-gate#define	PPDZIP ".gz"
507c478bdstevel@tonic-gate
517c478bdstevel@tonic-gateextern struct {
527c478bdstevel@tonic-gate	char			*v;
537c478bdstevel@tonic-gate	short			len,
547c478bdstevel@tonic-gate				okremote;
557c478bdstevel@tonic-gate}			prtrheadings[];
567c478bdstevel@tonic-gate
577c478bdstevel@tonic-gate#if	defined(__STDC__)
587c478bdstevel@tonic-gate
597c478bdstevel@tonic-gatestatic void		print_sdn (int, char *, SCALED);
607c478bdstevel@tonic-gatestatic void		print_l (int, char *, char **);
617c478bdstevel@tonic-gatestatic void		print_str (int, char *, char *);
627c478bdstevel@tonic-gate
637c478bdstevel@tonic-gate#ifdef LP_USE_PAPI_ATTR
647c478bdstevel@tonic-gatestatic int addPrintersPPD(char *name, PRINTER *prbufp);
657c478bdstevel@tonic-gatestatic int copyPPDFile(char *ppd, char *printersPPD);
667c478bdstevel@tonic-gatestatic int unzipPPDFile(char *ppd, char *printersPPD);
677c478bdstevel@tonic-gate#endif
687c478bdstevel@tonic-gate
697c478bdstevel@tonic-gate#else
707c478bdstevel@tonic-gate
717c478bdstevel@tonic-gatestatic void		print_sdn(),
727c478bdstevel@tonic-gate			print_l(),
737c478bdstevel@tonic-gate			print_str();
747c478bdstevel@tonic-gate
757c478bdstevel@tonic-gate#ifdef LP_USE_PAPI_ATTR
767c478bdstevel@tonic-gatestatic int addPrintersPPD();
777c478bdstevel@tonic-gatestatic int copyPPDFile();
787c478bdstevel@tonic-gatestatic int unzipPPDFile();
797c478bdstevel@tonic-gate#endif
807c478bdstevel@tonic-gate
817c478bdstevel@tonic-gate#endif
827c478bdstevel@tonic-gate
837c478bdstevel@tonic-gateunsigned long		ignprinter	= 0;
847c478bdstevel@tonic-gateint			ppdopt		= 0;
857c478bdstevel@tonic-gate
867c478bdstevel@tonic-gate/**
877c478bdstevel@tonic-gate ** putprinter() - WRITE PRINTER STRUCTURE TO DISK FILES
887c478bdstevel@tonic-gate **/
897c478bdstevel@tonic-gate
907c478bdstevel@tonic-gateint
917c478bdstevel@tonic-gateputprinter(char *name, PRINTER *prbufp)
927c478bdstevel@tonic-gate{
937c478bdstevel@tonic-gate	register char *		path;
947c478bdstevel@tonic-gate	register char *		stty;
957c478bdstevel@tonic-gate	register char *		speed;
967c478bdstevel@tonic-gate
977c478bdstevel@tonic-gate	int fdin, fdout;
987c478bdstevel@tonic-gate
997c478bdstevel@tonic-gate	int			fld;
1007c478bdstevel@tonic-gate
1017c478bdstevel@tonic-gate	char			buf[BUFSIZ];
1027c478bdstevel@tonic-gate
1037c478bdstevel@tonic-gate	struct stat		statbuf1,
1047c478bdstevel@tonic-gate				statbuf2;
1057c478bdstevel@tonic-gate
1067c478bdstevel@tonic-gate
1077c478bdstevel@tonic-gate	badprinter = 0;
1087c478bdstevel@tonic-gate
1097c478bdstevel@tonic-gate	if (!name || !*name) {
1107c478bdstevel@tonic-gate		errno = EINVAL;
1117c478bdstevel@tonic-gate		return (-1);
1127c478bdstevel@tonic-gate	}
1137c478bdstevel@tonic-gate
1147c478bdstevel@tonic-gate	if (STREQU(NAME_ALL, name)) {
1157c478bdstevel@tonic-gate		errno = EINVAL;
1167c478bdstevel@tonic-gate		return (-1);
1177c478bdstevel@tonic-gate	}
1187c478bdstevel@tonic-gate
1197c478bdstevel@tonic-gate	/*
1207c478bdstevel@tonic-gate	 * First go through the structure and see if we have
1217c478bdstevel@tonic-gate	 * anything strange.
1227c478bdstevel@tonic-gate	 */
1237c478bdstevel@tonic-gate	if (!okprinter(name, prbufp, 1)) {
1247c478bdstevel@tonic-gate		errno = EINVAL;
1257c478bdstevel@tonic-gate		return (-1);
1267c478bdstevel@tonic-gate	}
1277c478bdstevel@tonic-gate
1287c478bdstevel@tonic-gate	if (!Lp_A_Printers || !Lp_A_Interfaces) {
1297c478bdstevel@tonic-gate		getadminpaths (LPUSER);
1307c478bdstevel@tonic-gate		if (!Lp_A_Printers || !Lp_A_Interfaces)
1317c478bdstevel@tonic-gate			return (0);
1327c478bdstevel@tonic-gate	}
1337c478bdstevel@tonic-gate
1347c478bdstevel@tonic-gate	/*
1357c478bdstevel@tonic-gate	 * Create the parent directory for this printer
1367c478bdstevel@tonic-gate	 * if it doesn't yet exist.
1377c478bdstevel@tonic-gate	 */
1387c478bdstevel@tonic-gate	if (!(path = getprinterfile(name, (char *)0)))
1397c478bdstevel@tonic-gate		return (-1);
1407c478bdstevel@tonic-gate	if (Stat(path, &statbuf1) == 0) {
1414bc0a2ecasper		if (!S_ISDIR(statbuf1.st_mode)) {
1427c478bdstevel@tonic-gate			Free (path);
1437c478bdstevel@tonic-gate			errno = ENOTDIR;
1447c478bdstevel@tonic-gate			return (-1);
1457c478bdstevel@tonic-gate		}
1467c478bdstevel@tonic-gate	} else if (errno != ENOENT || mkdir_lpdir(path, MODE_DIR) == -1) {
1477c478bdstevel@tonic-gate		Free (path);
1487c478bdstevel@tonic-gate		return (-1);
1497c478bdstevel@tonic-gate	}
1507c478bdstevel@tonic-gate	Free (path);
1517c478bdstevel@tonic-gate
1527c478bdstevel@tonic-gate	/*
1537c478bdstevel@tonic-gate	 * Create the copy of the interface program, unless
1547c478bdstevel@tonic-gate	 * that would be silly or not desired.
1557c478bdstevel@tonic-gate	 * Conversely, make sure the interface program doesn't
1567c478bdstevel@tonic-gate	 * exist for a remote printer.
1577c478bdstevel@tonic-gate	 */
1587c478bdstevel@tonic-gate	if (prbufp->remote) {
1597c478bdstevel@tonic-gate		if (!(path = makepath(Lp_A_Interfaces, name, (char *)0)))
1607c478bdstevel@tonic-gate			return (-1);
1617c478bdstevel@tonic-gate		(void)rmfile (path);
1627c478bdstevel@tonic-gate		Free (path);
1637c478bdstevel@tonic-gate	}
1647c478bdstevel@tonic-gate	if (prbufp->interface && (ignprinter & BAD_INTERFACE) == 0) {
1657c478bdstevel@tonic-gate		if (Stat(prbufp->interface, &statbuf1) == -1)
1667c478bdstevel@tonic-gate			return (-1);
1677c478bdstevel@tonic-gate		if (!(path = makepath(Lp_A_Interfaces, name, (char *)0)))
1687c478bdstevel@tonic-gate			return (-1);
1697c478bdstevel@tonic-gate		if (
1707c478bdstevel@tonic-gate			Stat(path, &statbuf2) == -1
1717c478bdstevel@tonic-gate		     || statbuf1.st_dev != statbuf2.st_dev
1727c478bdstevel@tonic-gate		     || statbuf1.st_ino != statbuf2.st_ino
1737c478bdstevel@tonic-gate		) {
1747c478bdstevel@tonic-gate			register int		n;
1757c478bdstevel@tonic-gate
1767c478bdstevel@tonic-gate			if ((fdin = open_locked(prbufp->interface, "r", 0)) < 0) {
1777c478bdstevel@tonic-gate				Free (path);
1787c478bdstevel@tonic-gate				return (-1);
1797c478bdstevel@tonic-gate			}
1807c478bdstevel@tonic-gate			if ((fdout = open_locked(path, "w", MODE_EXEC)) < 0) {
1817c478bdstevel@tonic-gate				Free (path);
1827c478bdstevel@tonic-gate				close(fdin);
1837c478bdstevel@tonic-gate				return (-1);
1847c478bdstevel@tonic-gate			}
1857c478bdstevel@tonic-gate			while ((n = read(fdin, buf, BUFSIZ)) > 0)
1867c478bdstevel@tonic-gate				write (fdout, buf,  n);
1877c478bdstevel@tonic-gate			close(fdout);
1887c478bdstevel@tonic-gate			close(fdin);
1897c478bdstevel@tonic-gate		}
1907c478bdstevel@tonic-gate		Free (path);
1917c478bdstevel@tonic-gate	}
1927c478bdstevel@tonic-gate
1937c478bdstevel@tonic-gate#ifdef LP_USE_PAPI_ATTR
1947c478bdstevel@tonic-gate	/*
1957c478bdstevel@tonic-gate	 * Handle PPD (Postscript Printer Definition) file for printer
1967c478bdstevel@tonic-gate	 * if this printer has been configured with one
1977c478bdstevel@tonic-gate	 */
1987c478bdstevel@tonic-gate	if ((prbufp->ppd != NULL) && (ppdopt))
1997c478bdstevel@tonic-gate	{
2007c478bdstevel@tonic-gate		if (addPrintersPPD(name, prbufp) != 0)
2017c478bdstevel@tonic-gate		{
2027c478bdstevel@tonic-gate			/* failed to added the printers PPD file */
2037c478bdstevel@tonic-gate			return (-1);
2047c478bdstevel@tonic-gate		}
2057c478bdstevel@tonic-gate	}
2067c478bdstevel@tonic-gate#endif
2077c478bdstevel@tonic-gate
2087c478bdstevel@tonic-gate	/*
2097c478bdstevel@tonic-gate	 * If this printer is dialed up, remove any baud rates
2107c478bdstevel@tonic-gate	 * from the stty option list and move the last one to
2117c478bdstevel@tonic-gate	 * the ".speed" member if the ".speed" member isn't already
2127c478bdstevel@tonic-gate	 * set. Conversely, if this printer is directly connected,
2137c478bdstevel@tonic-gate	 * move any value from the ".speed" member to the stty list.
2147c478bdstevel@tonic-gate	 */
2157c478bdstevel@tonic-gate
2167c478bdstevel@tonic-gate	stty = (prbufp->stty? Strdup(prbufp->stty) : 0);
2177c478bdstevel@tonic-gate	if (prbufp->speed)
2187c478bdstevel@tonic-gate		speed = Strdup(prbufp->speed);
2197c478bdstevel@tonic-gate	else
2207c478bdstevel@tonic-gate		speed = 0;
2217c478bdstevel@tonic-gate
2227c478bdstevel@tonic-gate	if (prbufp->dial_info && stty) {
2237c478bdstevel@tonic-gate		register char		*newstty,
2247c478bdstevel@tonic-gate					*p,
2257c478bdstevel@tonic-gate					*q;
2267c478bdstevel@tonic-gate
2277c478bdstevel@tonic-gate		register int		len;
2287c478bdstevel@tonic-gate
2297c478bdstevel@tonic-gate		if (!(q = newstty = Malloc(strlen(stty) + 1))) {
2307c478bdstevel@tonic-gate			Free (stty);
2317c478bdstevel@tonic-gate			errno = ENOMEM;
2327c478bdstevel@tonic-gate			return (-1);
2337c478bdstevel@tonic-gate		}
2347c478bdstevel@tonic-gate		newstty[0] = 0;	/* start with empty copy */
2357c478bdstevel@tonic-gate
2367c478bdstevel@tonic-gate		for (
2377c478bdstevel@tonic-gate			p = strtok(stty, " ");
2387c478bdstevel@tonic-gate			p;
2397c478bdstevel@tonic-gate			p = strtok((char *)0, " ")
2407c478bdstevel@tonic-gate		) {
2417c478bdstevel@tonic-gate			len = strlen(p);
2427c478bdstevel@tonic-gate			if (strspn(p, "0123456789") == len) {
2437c478bdstevel@tonic-gate				/*
2447c478bdstevel@tonic-gate				 * If "prbufp->speed" isn't set, then
2457c478bdstevel@tonic-gate				 * use the speed we just found. Don't
2467c478bdstevel@tonic-gate				 * check "speed", because if more than
2477c478bdstevel@tonic-gate				 * one speed was given in the list, we
2487c478bdstevel@tonic-gate				 * want the last one.
2497c478bdstevel@tonic-gate				 */
2507c478bdstevel@tonic-gate				if (!prbufp->speed) {
2517c478bdstevel@tonic-gate					if (speed)
2527c478bdstevel@tonic-gate						Free (speed);
2537c478bdstevel@tonic-gate					speed = Strdup(p);
2547c478bdstevel@tonic-gate				}
2557c478bdstevel@tonic-gate
2567c478bdstevel@tonic-gate			} else {
2577c478bdstevel@tonic-gate				/*
2587c478bdstevel@tonic-gate				 * Not a speed, so copy it to the
2597c478bdstevel@tonic-gate				 * new stty string.
2607c478bdstevel@tonic-gate				 */
2617c478bdstevel@tonic-gate				if (q != newstty)
2627c478bdstevel@tonic-gate					*q++ = ' ';
2637c478bdstevel@tonic-gate				strcpy (q, p);
2647c478bdstevel@tonic-gate				q += len;
2657c478bdstevel@tonic-gate			}
2667c478bdstevel@tonic-gate		}
2677c478bdstevel@tonic-gate
2687c478bdstevel@tonic-gate		Free (stty);
2697c478bdstevel@tonic-gate		stty = newstty;
2707c478bdstevel@tonic-gate
2717c478bdstevel@tonic-gate	} else if (!prbufp->dial_info && speed) {
2727c478bdstevel@tonic-gate		register char		*newstty;
2737c478bdstevel@tonic-gate
2747c478bdstevel@tonic-gate		newstty = Malloc(strlen(stty) + 1 + strlen(speed) + 1);
2757c478bdstevel@tonic-gate		if (!newstty) {
2767c478bdstevel@tonic-gate			if (stty)
2777c478bdstevel@tonic-gate				Free (stty);
2787c478bdstevel@tonic-gate			errno = ENOMEM;
2797c478bdstevel@tonic-gate			return (-1);
2807c478bdstevel@tonic-gate		}
2817c478bdstevel@tonic-gate
2827c478bdstevel@tonic-gate		if (stty) {
2837c478bdstevel@tonic-gate			strcpy (newstty, stty);
2847c478bdstevel@tonic-gate			strcat (newstty, " ");
2857c478bdstevel@tonic-gate			strcat (newstty, speed);
2867c478bdstevel@tonic-gate			Free (stty);
2877c478bdstevel@tonic-gate		} else
2887c478bdstevel@tonic-gate			strcpy (newstty, speed);
2897c478bdstevel@tonic-gate		Free (speed);
2907c478bdstevel@tonic-gate		speed = 0;
2917c478bdstevel@tonic-gate
2927c478bdstevel@tonic-gate		stty = newstty;
2937c478bdstevel@tonic-gate
2947c478bdstevel@tonic-gate	}
2957c478bdstevel@tonic-gate
2967c478bdstevel@tonic-gate	/*
2977c478bdstevel@tonic-gate	 * Open the configuration file and write out the printer
2987c478bdstevel@tonic-gate	 * configuration.
2997c478bdstevel@tonic-gate	 */
3007c478bdstevel@tonic-gate
3017c478bdstevel@tonic-gate	if (!(path = getprinterfile(name, CONFIGFILE))) {
3027c478bdstevel@tonic-gate		if (stty)
3037c478bdstevel@tonic-gate			Free (stty);
3047c478bdstevel@tonic-gate		if (speed)
3057c478bdstevel@tonic-gate			Free (speed);
3067c478bdstevel@tonic-gate		return (-1);
3077c478bdstevel@tonic-gate	}
3087c478bdstevel@tonic-gate	if ((fdout = open_locked(path, "w", MODE_READ)) < 0) {
3097c478bdstevel@tonic-gate		Free (path);
3107c478bdstevel@tonic-gate		if (stty)
3117c478bdstevel@tonic-gate			Free (stty);
3127c478bdstevel@tonic-gate		if (speed)
3137c478bdstevel@tonic-gate			Free (speed);
3147c478bdstevel@tonic-gate		return (-1);
3157c478bdstevel@tonic-gate	}
3167c478bdstevel@tonic-gate	Free (path);
3177c478bdstevel@tonic-gate
3187c478bdstevel@tonic-gate	errno = 0;
3197c478bdstevel@tonic-gate	for (fld = 0; fld < PR_MAX; fld++) {
3207c478bdstevel@tonic-gate		if (prbufp->remote && !prtrheadings[fld].okremote)
3217c478bdstevel@tonic-gate			continue;
3227c478bdstevel@tonic-gate
3237c478bdstevel@tonic-gate		switch (fld) {
3247c478bdstevel@tonic-gate
3257c478bdstevel@tonic-gate#define HEAD	prtrheadings[fld].v
3267c478bdstevel@tonic-gate
3277c478bdstevel@tonic-gate		case PR_BAN:
3287c478bdstevel@tonic-gate			{
3297c478bdstevel@tonic-gate				char *ptr = NAME_ON;
3307c478bdstevel@tonic-gate
3317c478bdstevel@tonic-gate				switch (prbufp->banner) {
3327c478bdstevel@tonic-gate				case BAN_ALWAYS:
3337c478bdstevel@tonic-gate					ptr = NAME_ON;
3347c478bdstevel@tonic-gate					break;
3357c478bdstevel@tonic-gate				case BAN_NEVER:
3367c478bdstevel@tonic-gate					ptr = NAME_OFF;
3377c478bdstevel@tonic-gate					break;
3387c478bdstevel@tonic-gate				case BAN_OPTIONAL:
3397c478bdstevel@tonic-gate					ptr = NAME_OPTIONAL;
3407c478bdstevel@tonic-gate					break;
3417c478bdstevel@tonic-gate				}
3427c478bdstevel@tonic-gate				(void)fdprintf(fdout, "%s %s\n", HEAD, ptr);
3437c478bdstevel@tonic-gate			}
3447c478bdstevel@tonic-gate			break;
3457c478bdstevel@tonic-gate
3467c478bdstevel@tonic-gate		case PR_CPI:
3477c478bdstevel@tonic-gate			print_sdn(fdout, HEAD, prbufp->cpi);
3487c478bdstevel@tonic-gate			break;
3497c478bdstevel@tonic-gate
3507c478bdstevel@tonic-gate		case PR_CS:
3517c478bdstevel@tonic-gate			if (!emptylist(prbufp->char_sets))
3527c478bdstevel@tonic-gate				print_l(fdout, HEAD, prbufp->char_sets);
3537c478bdstevel@tonic-gate			break;
3547c478bdstevel@tonic-gate
3557c478bdstevel@tonic-gate		case PR_ITYPES:
3567c478bdstevel@tonic-gate			/*
3577c478bdstevel@tonic-gate			 * Put out the header even if the list is empty,
3587c478bdstevel@tonic-gate			 * to distinguish no input types from the default.
3597c478bdstevel@tonic-gate			 */
3607c478bdstevel@tonic-gate			print_l(fdout, HEAD, prbufp->input_types);
3617c478bdstevel@tonic-gate			break;
3627c478bdstevel@tonic-gate
3637c478bdstevel@tonic-gate		case PR_DEV:
3647c478bdstevel@tonic-gate			print_str(fdout, HEAD, prbufp->device);
3657c478bdstevel@tonic-gate			break;
3667c478bdstevel@tonic-gate
3677c478bdstevel@tonic-gate		case PR_DIAL:
3687c478bdstevel@tonic-gate			print_str(fdout, HEAD, prbufp->dial_info);
3697c478bdstevel@tonic-gate			break;
3707c478bdstevel@tonic-gate
3717c478bdstevel@tonic-gate		case PR_RECOV:
3727c478bdstevel@tonic-gate			print_str(fdout, HEAD, prbufp->fault_rec);
3737c478bdstevel@tonic-gate			break;
3747c478bdstevel@tonic-gate
3757c478bdstevel@tonic-gate		case PR_INTFC:
3767c478bdstevel@tonic-gate			print_str(fdout, HEAD, prbufp->interface);
3777c478bdstevel@tonic-gate			break;
3787c478bdstevel@tonic-gate
3797c478bdstevel@tonic-gate		case PR_LPI:
3807c478bdstevel@tonic-gate			print_sdn(fdout, HEAD, prbufp->lpi);
3817c478bdstevel@tonic-gate			break;
3827c478bdstevel@tonic-gate
3837c478bdstevel@tonic-gate		case PR_LEN:
3847c478bdstevel@tonic-gate			print_sdn(fdout, HEAD, prbufp->plen);
3857c478bdstevel@tonic-gate			break;
3867c478bdstevel@tonic-gate
3877c478bdstevel@tonic-gate		case PR_LOGIN:
3887c478bdstevel@tonic-gate			if (prbufp->login & LOG_IN)
3897c478bdstevel@tonic-gate				(void)fdprintf(fdout, "%s\n", HEAD);
3907c478bdstevel@tonic-gate			break;
3917c478bdstevel@tonic-gate
3927c478bdstevel@tonic-gate		case PR_PTYPE:
3937c478bdstevel@tonic-gate		{
3947c478bdstevel@tonic-gate			char			**printer_types;
3957c478bdstevel@tonic-gate
3967c478bdstevel@tonic-gate			/*
3977c478bdstevel@tonic-gate			 * For backward compatibility for those who
3987c478bdstevel@tonic-gate			 * use only "->printer_type", we have to play
3997c478bdstevel@tonic-gate			 * some games here.
4007c478bdstevel@tonic-gate			 */
4017c478bdstevel@tonic-gate			if (prbufp->printer_type && !prbufp->printer_types)
4027c478bdstevel@tonic-gate				printer_types = getlist(
4037c478bdstevel@tonic-gate					prbufp->printer_type,
4047c478bdstevel@tonic-gate					LP_WS,
4057c478bdstevel@tonic-gate					LP_SEP
4067c478bdstevel@tonic-gate				);
4077c478bdstevel@tonic-gate			else
4087c478bdstevel@tonic-gate				printer_types = prbufp->printer_types;
4097c478bdstevel@tonic-gate
4107c478bdstevel@tonic-gate			if (!printer_types || !*printer_types)
4117c478bdstevel@tonic-gate				print_str(fdout, HEAD, NAME_UNKNOWN);
4127c478bdstevel@tonic-gate			else
4137c478bdstevel@tonic-gate				print_l(fdout, HEAD, printer_types);
4147c478bdstevel@tonic-gate
4157c478bdstevel@tonic-gate			if (printer_types != prbufp->printer_types)
4167c478bdstevel@tonic-gate				freelist (printer_types);
4177c478bdstevel@tonic-gate			break;
4187c478bdstevel@tonic-gate		}
4197c478bdstevel@tonic-gate
4207c478bdstevel@tonic-gate		case PR_REMOTE:
4217c478bdstevel@tonic-gate			print_str(fdout, HEAD, prbufp->remote);
4227c478bdstevel@tonic-gate			break;
4237c478bdstevel@tonic-gate
4247c478bdstevel@tonic-gate		case PR_SPEED:
4257c478bdstevel@tonic-gate			print_str(fdout, HEAD, speed);
4267c478bdstevel@tonic-gate			break;
4277c478bdstevel@tonic-gate
4287c478bdstevel@tonic-gate		case PR_STTY:
4297c478bdstevel@tonic-gate			print_str(fdout, HEAD, stty);
4307c478bdstevel@tonic-gate			break;
4317c478bdstevel@tonic-gate
4327c478bdstevel@tonic-gate		case PR_WIDTH:
4337c478bdstevel@tonic-gate			print_sdn(fdout, HEAD, prbufp->pwid);
4347c478bdstevel@tonic-gate			break;
4357c478bdstevel@tonic-gate
4367c478bdstevel@tonic-gate#if	defined(CAN_DO_MODULES)
4377c478bdstevel@tonic-gate		case PR_MODULES:
4387c478bdstevel@tonic-gate			/*
4397c478bdstevel@tonic-gate			 * Put out the header even if the list is empty,
4407c478bdstevel@tonic-gate			 * to distinguish no modules from the default.
4417c478bdstevel@tonic-gate			 */
4427c478bdstevel@tonic-gate			print_l(fdout, HEAD, prbufp->modules);
4437c478bdstevel@tonic-gate			break;
4447c478bdstevel@tonic-gate#endif
4457c478bdstevel@tonic-gate
4467c478bdstevel@tonic-gate		case PR_OPTIONS:
4477c478bdstevel@tonic-gate			print_l(fdout, HEAD, prbufp->options);
4487c478bdstevel@tonic-gate			break;
4497c478bdstevel@tonic-gate
4507c478bdstevel@tonic-gate		case PR_PPD:
4517c478bdstevel@tonic-gate		{
4527c478bdstevel@tonic-gate			print_str(fdout, HEAD, prbufp->ppd);
4537c478bdstevel@tonic-gate			break;
4547c478bdstevel@tonic-gate		}
4557c478bdstevel@tonic-gate		}
4567c478bdstevel@tonic-gate
4577c478bdstevel@tonic-gate	}
4587c478bdstevel@tonic-gate	if (stty)
4597c478bdstevel@tonic-gate		Free (stty);
4607c478bdstevel@tonic-gate	if (speed)
4617c478bdstevel@tonic-gate		Free (speed);
4627c478bdstevel@tonic-gate	if (errno != 0) {
4637c478bdstevel@tonic-gate		close(fdout);
4647c478bdstevel@tonic-gate		return (-1);
4657c478bdstevel@tonic-gate	}
4667c478bdstevel@tonic-gate	close(fdout);
4677c478bdstevel@tonic-gate
4687c478bdstevel@tonic-gate	/*
4697c478bdstevel@tonic-gate	 * If we have a description of the printer,
4707c478bdstevel@tonic-gate	 * write it out to a separate file.
4717c478bdstevel@tonic-gate	 */
4727c478bdstevel@tonic-gate	if (prbufp->description) {
4737c478bdstevel@tonic-gate
4747c478bdstevel@tonic-gate		if (!(path = getprinterfile(name, COMMENTFILE)))
4757c478bdstevel@tonic-gate			return (-1);
4767c478bdstevel@tonic-gate
4777c478bdstevel@tonic-gate		if (dumpstring(path, prbufp->description) == -1) {
4787c478bdstevel@tonic-gate			Free (path);
4797c478bdstevel@tonic-gate			return (-1);
4807c478bdstevel@tonic-gate		}
4817c478bdstevel@tonic-gate		Free (path);
4827c478bdstevel@tonic-gate
4837c478bdstevel@tonic-gate	}
4847c478bdstevel@tonic-gate
4857c478bdstevel@tonic-gate	/*
4867c478bdstevel@tonic-gate	 * Now write out the alert condition.
4877c478bdstevel@tonic-gate	 */
4887c478bdstevel@tonic-gate	if (
4897c478bdstevel@tonic-gate		prbufp->fault_alert.shcmd
4907c478bdstevel@tonic-gate	     && putalert(Lp_A_Printers, name, &(prbufp->fault_alert)) == -1
4917c478bdstevel@tonic-gate	)
4927c478bdstevel@tonic-gate		return (-1);
4937c478bdstevel@tonic-gate
4947c478bdstevel@tonic-gate	return (0);
4957c478bdstevel@tonic-gate}
4967c478bdstevel@tonic-gate
4977c478bdstevel@tonic-gate/**
4987c478bdstevel@tonic-gate ** print_sdn() - PRINT SCALED DECIMAL NUMBER WITH HEADER
4997c478bdstevel@tonic-gate ** print_l() - PRINT (char **) LIST WITH HEADER
5007c478bdstevel@tonic-gate ** print_str() - PRINT STRING WITH HEADER
5017c478bdstevel@tonic-gate **/
5027c478bdstevel@tonic-gate
5037c478bdstevel@tonic-gatestatic void
5047c478bdstevel@tonic-gateprint_sdn(int fd, char *head, SCALED sdn)
5057c478bdstevel@tonic-gate{
5067c478bdstevel@tonic-gate	if (sdn.val <= 0)
5077c478bdstevel@tonic-gate		return;
5087c478bdstevel@tonic-gate
5097c478bdstevel@tonic-gate	(void)fdprintf (fd, "%s ", head);
5107c478bdstevel@tonic-gate	fdprintsdn (fd, sdn);
5117c478bdstevel@tonic-gate
5127c478bdstevel@tonic-gate	return;
5137c478bdstevel@tonic-gate}
5147c478bdstevel@tonic-gate
5157c478bdstevel@tonic-gatestatic void
5167c478bdstevel@tonic-gateprint_l(int fd, char *head, char **list)
5177c478bdstevel@tonic-gate{
5187c478bdstevel@tonic-gate	(void)fdprintf (fd, "%s ", head);
5197c478bdstevel@tonic-gate	printlist_setup (0, 0, LP_SEP, 0);
5207c478bdstevel@tonic-gate	fdprintlist (fd, list);
5217c478bdstevel@tonic-gate	printlist_unsetup ();
5227c478bdstevel@tonic-gate
5237c478bdstevel@tonic-gate	return;
5247c478bdstevel@tonic-gate}
5257c478bdstevel@tonic-gate
5267c478bdstevel@tonic-gatestatic void
5277c478bdstevel@tonic-gateprint_str(int fd, char *head, char *str)
5287c478bdstevel@tonic-gate{
5297c478bdstevel@tonic-gate	if (!str || !*str)
5307c478bdstevel@tonic-gate		return;
5317c478bdstevel@tonic-gate
5327c478bdstevel@tonic-gate	(void)fdprintf (fd, "%s %s\n", head, str);
5337c478bdstevel@tonic-gate
5347c478bdstevel@tonic-gate	return;
5357c478bdstevel@tonic-gate}
5367c478bdstevel@tonic-gate
5377c478bdstevel@tonic-gate
5387c478bdstevel@tonic-gate#ifdef LP_USE_PAPI_ATTR
5397c478bdstevel@tonic-gate/*
5407c478bdstevel@tonic-gate * Function:     addPrintersPPD()
5417c478bdstevel@tonic-gate *
5427c478bdstevel@tonic-gate * Description:  Handle PPD (Postscript Printer Definition) file for this
5437c478bdstevel@tonic-gate *               printer if it has been configured with one
5447c478bdstevel@tonic-gate *
5457c478bdstevel@tonic-gate */
5467c478bdstevel@tonic-gate
5477c478bdstevel@tonic-gatestatic int
5487c478bdstevel@tonic-gateaddPrintersPPD(char *name, PRINTER *prbufp)
5497c478bdstevel@tonic-gate
5507c478bdstevel@tonic-gate{
5517c478bdstevel@tonic-gate	int result = 0;
5527c478bdstevel@tonic-gate	char *path = NULL;
5537c478bdstevel@tonic-gate	char *ppd = NULL;
5547c478bdstevel@tonic-gate	char  buf[BUFSIZ];
5557c478bdstevel@tonic-gate	struct stat statbuf;
5567c478bdstevel@tonic-gate
5577c478bdstevel@tonic-gate	(void) snprintf(buf, sizeof (buf), "%s.ppd", name);
5587c478bdstevel@tonic-gate	if (prbufp->remote)
5597c478bdstevel@tonic-gate	{
5607c478bdstevel@tonic-gate		/* make sure the PPD file doesn't exist for a remote printer */
5617c478bdstevel@tonic-gate		if (!(path = makepath(ETCDIR, "ppd", buf, (char *)0)))
5627c478bdstevel@tonic-gate		{
5637c478bdstevel@tonic-gate			result = -1;
5647c478bdstevel@tonic-gate		}
5657c478bdstevel@tonic-gate		else
5667c478bdstevel@tonic-gate		{
5677c478bdstevel@tonic-gate			(void) rmfile(path);
5687c478bdstevel@tonic-gate		}
5697c478bdstevel@tonic-gate	}
5707c478bdstevel@tonic-gate
5717c478bdstevel@tonic-gate	if ((result == 0) && (prbufp->ppd != NULL))
5727c478bdstevel@tonic-gate	{
5737c478bdstevel@tonic-gate		ppd = strdup(prbufp->ppd);
5747c478bdstevel@tonic-gate
5757c478bdstevel@tonic-gate		if (ppd == NULL)
5767c478bdstevel@tonic-gate		{
5777c478bdstevel@tonic-gate			result = -1;
5787c478bdstevel@tonic-gate		}
5797c478bdstevel@tonic-gate		else
5807c478bdstevel@tonic-gate		{
5817c478bdstevel@tonic-gate			/* Check the PPD file given exists */
5827c478bdstevel@tonic-gate
5837c478bdstevel@tonic-gate			if (Stat(ppd, &statbuf) == -1)
5847c478bdstevel@tonic-gate			{
5857c478bdstevel@tonic-gate				/*
5867c478bdstevel@tonic-gate				 * The given ppd files does not exist, but
5877c478bdstevel@tonic-gate				 * check if there is a zipped version of the
5887c478bdstevel@tonic-gate				 * file that we can use instead
5897c478bdstevel@tonic-gate				 */
5907c478bdstevel@tonic-gate				if (strstr(ppd, PPDZIP) != NULL)
5917c478bdstevel@tonic-gate				{
5927c478bdstevel@tonic-gate					/* this is a zipped file so exit */
5937c478bdstevel@tonic-gate					result = -1;
5947c478bdstevel@tonic-gate				}
5957c478bdstevel@tonic-gate				else
5967c478bdstevel@tonic-gate				{
5977c478bdstevel@tonic-gate					ppd = Realloc(ppd,
5987c478bdstevel@tonic-gate						strlen(ppd)+strlen(PPDZIP)+2);
5997c478bdstevel@tonic-gate					if (ppd != NULL)
6007c478bdstevel@tonic-gate					{
6017c478bdstevel@tonic-gate						ppd = strcat(ppd, PPDZIP);
6027c478bdstevel@tonic-gate						if (Stat(ppd, &statbuf) == -1)
6037c478bdstevel@tonic-gate						{
6047c478bdstevel@tonic-gate							/*
6057c478bdstevel@tonic-gate							 * this zipped version
6067c478bdstevel@tonic-gate							 * of the file does not
6077c478bdstevel@tonic-gate							 * exist either
6087c478bdstevel@tonic-gate							 */
6097c478bdstevel@tonic-gate							result = -1;
6107c478bdstevel@tonic-gate						}
6117c478bdstevel@tonic-gate					}
6127c478bdstevel@tonic-gate					else
6137c478bdstevel@tonic-gate					{
6147c478bdstevel@tonic-gate						result = -1;
6157c478bdstevel@tonic-gate					}
6167c478bdstevel@tonic-gate				}
6177c478bdstevel@tonic-gate			}
6187c478bdstevel@tonic-gate		}
6197c478bdstevel@tonic-gate
6207c478bdstevel@tonic-gate		/*
6217c478bdstevel@tonic-gate		 * Create the copy of the PPD file for this printer
6227c478bdstevel@tonic-gate		 * unless that would be silly or not desired
6237c478bdstevel@tonic-gate		 */
6247c478bdstevel@tonic-gate
6257c478bdstevel@tonic-gate		if (result == 0)
6267c478bdstevel@tonic-gate		{
6277c478bdstevel@tonic-gate			if (!(path = makepath(ETCDIR, "ppd", buf, (char *)0)))
6287c478bdstevel@tonic-gate			{
6297c478bdstevel@tonic-gate				result = -1;
6307c478bdstevel@tonic-gate			}
6317c478bdstevel@tonic-gate		}
6327c478bdstevel@tonic-gate
6337c478bdstevel@tonic-gate		/*
6347c478bdstevel@tonic-gate		 * At this point we may have a zipped or unzipped ppd file, if
6357c478bdstevel@tonic-gate		 * it's unzipped just copy it otherwise unzip it to the
6367c478bdstevel@tonic-gate		 * printer's ppd file (/etc/lp/ppd/<printer>.ppd)
6377c478bdstevel@tonic-gate		 */
6387c478bdstevel@tonic-gate
6397c478bdstevel@tonic-gate		if (result == 0)
6407c478bdstevel@tonic-gate		{
6417c478bdstevel@tonic-gate			if (strstr(ppd, PPDZIP) == NULL)
6427c478bdstevel@tonic-gate			{
6437c478bdstevel@tonic-gate				result = copyPPDFile(ppd, path);
6447c478bdstevel@tonic-gate			}
6457c478bdstevel@tonic-gate			else
6467c478bdstevel@tonic-gate			{
6477c478bdstevel@tonic-gate				result = unzipPPDFile(ppd, path);
6487c478bdstevel@tonic-gate			}
6497c478bdstevel@tonic-gate
6507c478bdstevel@tonic-gate			(void) chown_lppath(path);
6517c478bdstevel@tonic-gate			(void) chmod(path, 0644);
6527c478bdstevel@tonic-gate		}
6537c478bdstevel@tonic-gate
6547c478bdstevel@tonic-gate		if (ppd != NULL)
6557c478bdstevel@tonic-gate		{
6567c478bdstevel@tonic-gate			Free(ppd);
6577c478bdstevel@tonic-gate		}
6587c478bdstevel@tonic-gate		if (path != NULL)
6597c478bdstevel@tonic-gate		{
6607c478bdstevel@tonic-gate			Free(path);
6617c478bdstevel@tonic-gate		}
6627c478bdstevel@tonic-gate	}
6637c478bdstevel@tonic-gate
6647c478bdstevel@tonic-gate	return (result);
6657c478bdstevel@tonic-gate} /* addPrintersPPD() */
6667c478bdstevel@tonic-gate
6677c478bdstevel@tonic-gate
6687c478bdstevel@tonic-gate/*
6697c478bdstevel@tonic-gate * Function:     copyPPDFile()
6707c478bdstevel@tonic-gate *
6717c478bdstevel@tonic-gate * Description:  Copy the given ppd file to the printer's file in /etc/lp/ppd
6727c478bdstevel@tonic-gate *
6737c478bdstevel@tonic-gate */
6747c478bdstevel@tonic-gate
6757c478bdstevel@tonic-gatestatic int
6767c478bdstevel@tonic-gatecopyPPDFile(char *ppd, char *printersPPD)
6777c478bdstevel@tonic-gate
6787c478bdstevel@tonic-gate{
6797c478bdstevel@tonic-gate	int  result = 0;
6807c478bdstevel@tonic-gate	register int n = 0;
6817c478bdstevel@tonic-gate	int  fdin  = 0;
6827c478bdstevel@tonic-gate	int  fdout = 0;
6837c478bdstevel@tonic-gate	char buf[BUFSIZ];
6847c478bdstevel@tonic-gate
6857c478bdstevel@tonic-gate	if ((ppd != NULL) && (printersPPD != NULL))
6867c478bdstevel@tonic-gate	{
6877c478bdstevel@tonic-gate		if ((fdin = open_locked(ppd, "r", 0)) < 0)
6887c478bdstevel@tonic-gate		{
6897c478bdstevel@tonic-gate			result = -1;
6907c478bdstevel@tonic-gate		}
6917c478bdstevel@tonic-gate		else
6927c478bdstevel@tonic-gate		{
6937c478bdstevel@tonic-gate			fdout = open_locked(printersPPD, "w", MODE_EXEC);
6947c478bdstevel@tonic-gate			if (fdout < 0)
6957c478bdstevel@tonic-gate			{
6967c478bdstevel@tonic-gate				close(fdin);
6977c478bdstevel@tonic-gate				result = -1;
6987c478bdstevel@tonic-gate			}
6997c478bdstevel@tonic-gate		}
7007c478bdstevel@tonic-gate
7017c478bdstevel@tonic-gate		if (result == 0)
7027c478bdstevel@tonic-gate		{
7037c478bdstevel@tonic-gate			while ((n = read(fdin, buf, BUFSIZ)) > 0)
7047c478bdstevel@tonic-gate			{
7057c478bdstevel@tonic-gate				write(fdout, buf,  n);
7067c478bdstevel@tonic-gate			}
7077c478bdstevel@tonic-gate			close(fdout);
7087c478bdstevel@tonic-gate			close(fdin);
7097c478bdstevel@tonic-gate		}
7107c478bdstevel@tonic-gate	}
7117c478bdstevel@tonic-gate	else
7127c478bdstevel@tonic-gate	{
7137c478bdstevel@tonic-gate		result = -1;
7147c478bdstevel@tonic-gate	}
7157c478bdstevel@tonic-gate
7167c478bdstevel@tonic-gate	return (result);
7177c478bdstevel@tonic-gate} /* copyPPDFile() */
7187c478bdstevel@tonic-gate
7197c478bdstevel@tonic-gate
7207c478bdstevel@tonic-gate
7217c478bdstevel@tonic-gate/*
7227c478bdstevel@tonic-gate * Function:     unzipPPDFile()
7237c478bdstevel@tonic-gate *
7247c478bdstevel@tonic-gate * Description:  Unzip the given ppd file to the printer's file in /etc/lp/ppd.
7257c478bdstevel@tonic-gate *               This is done by forking and running the unzip utility on the
7267c478bdstevel@tonic-gate *               zipped ppd file.
7277c478bdstevel@tonic-gate *
7287c478bdstevel@tonic-gate */
7297c478bdstevel@tonic-gate
7307c478bdstevel@tonic-gatestatic int
7317c478bdstevel@tonic-gateunzipPPDFile(char *ppd, char *printersPPD)
7327c478bdstevel@tonic-gate
7337c478bdstevel@tonic-gate{
7347c478bdstevel@tonic-gate	int  result = -1;
7357c478bdstevel@tonic-gate	char *cmdLine = NULL;
7367c478bdstevel@tonic-gate	pid_t childPID = 0;
7377c478bdstevel@tonic-gate	int   stat = 0;
7387c478bdstevel@tonic-gate	int   clSize = 0;
7397c478bdstevel@tonic-gate
7407c478bdstevel@tonic-gate
7417c478bdstevel@tonic-gate	if ((ppd != NULL) && (printersPPD != NULL))
7427c478bdstevel@tonic-gate	{
7437c478bdstevel@tonic-gate		childPID = fork();
7447c478bdstevel@tonic-gate
7457c478bdstevel@tonic-gate		switch (childPID)
7467c478bdstevel@tonic-gate		{
7477c478bdstevel@tonic-gate			case -1:
7487c478bdstevel@tonic-gate			{
7497c478bdstevel@tonic-gate				/* return error */
7507c478bdstevel@tonic-gate				break;
7517c478bdstevel@tonic-gate			}
7527c478bdstevel@tonic-gate
7537c478bdstevel@tonic-gate			case 0:
7547c478bdstevel@tonic-gate			{
7557c478bdstevel@tonic-gate				/* child process  - so execute something */
7567c478bdstevel@tonic-gate
7577c478bdstevel@tonic-gate				clSize = strlen("/usr/bin/rm -f ") +
7587c478bdstevel@tonic-gate						strlen(printersPPD) +
7597c478bdstevel@tonic-gate						strlen("/usr/bin/gzip -dc ") +
7607c478bdstevel@tonic-gate						strlen(ppd) +
7617c478bdstevel@tonic-gate						strlen(printersPPD) + 20;
7627c478bdstevel@tonic-gate				cmdLine = malloc(clSize);
7637c478bdstevel@tonic-gate				if (cmdLine != NULL)
7647c478bdstevel@tonic-gate				{
7657c478bdstevel@tonic-gate
7667c478bdstevel@tonic-gate					(void) snprintf(cmdLine, clSize,
7677c478bdstevel@tonic-gate				"/usr/bin/rm -f %s; /usr/bin/gzip -dc %s > %s",
7687c478bdstevel@tonic-gate							printersPPD, ppd,
7697c478bdstevel@tonic-gate							printersPPD);
7707c478bdstevel@tonic-gate					result = execl(SHELL, SHELL, "-c",
7717c478bdstevel@tonic-gate							cmdLine, NULL);
7727c478bdstevel@tonic-gate					exit(result);
7737c478bdstevel@tonic-gate				}
7747c478bdstevel@tonic-gate				break;
7757c478bdstevel@tonic-gate			}
7767c478bdstevel@tonic-gate
7777c478bdstevel@tonic-gate			default:
7787c478bdstevel@tonic-gate			{
7797c478bdstevel@tonic-gate				/* parent process, child pid is in childPID */
7807c478bdstevel@tonic-gate
7817c478bdstevel@tonic-gate				while (wait(&stat) != childPID);
7827c478bdstevel@tonic-gate
7837c478bdstevel@tonic-gate				if ((stat & 0xff00) == 0)
7847c478bdstevel@tonic-gate				{
7857c478bdstevel@tonic-gate					result = 0;
7867c478bdstevel@tonic-gate				}
7877c478bdstevel@tonic-gate				break;
7887c478bdstevel@tonic-gate			}
7897c478bdstevel@tonic-gate		}
7907c478bdstevel@tonic-gate	}
7917c478bdstevel@tonic-gate
7927c478bdstevel@tonic-gate	return (result);
7937c478bdstevel@tonic-gate} /* unzipPPDFile() */
7947c478bdstevel@tonic-gate#endif
795