xref: /titanic_53/usr/src/cmd/print/bsd-sysv-commands/in.lpd.c (revision a18dc42fc967d11feba9b8be61c6727dc6c56b48)
1355b4669Sjacobs /*
2355b4669Sjacobs  * CDDL HEADER START
3355b4669Sjacobs  *
4355b4669Sjacobs  * The contents of this file are subject to the terms of the
5355b4669Sjacobs  * Common Development and Distribution License (the "License").
6355b4669Sjacobs  * You may not use this file except in compliance with the License.
7355b4669Sjacobs  *
8355b4669Sjacobs  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9355b4669Sjacobs  * or http://www.opensolaris.org/os/licensing.
10355b4669Sjacobs  * See the License for the specific language governing permissions
11355b4669Sjacobs  * and limitations under the License.
12355b4669Sjacobs  *
13355b4669Sjacobs  * When distributing Covered Code, include this CDDL HEADER in each
14355b4669Sjacobs  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15355b4669Sjacobs  * If applicable, add the following below this CDDL HEADER, with the
16355b4669Sjacobs  * fields enclosed by brackets "[]" replaced with your own identifying
17355b4669Sjacobs  * information: Portions Copyright [yyyy] [name of copyright owner]
18355b4669Sjacobs  *
19355b4669Sjacobs  * CDDL HEADER END
20355b4669Sjacobs  */
21355b4669Sjacobs 
22355b4669Sjacobs /*
2343b9c050Sjacobs  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
24355b4669Sjacobs  * Use is subject to license terms.
25355b4669Sjacobs  *
26355b4669Sjacobs  */
27355b4669Sjacobs 
28355b4669Sjacobs /* $Id: in.lpd.c 170 2006-05-20 05:58:49Z njacobs $ */
29355b4669Sjacobs 
30355b4669Sjacobs #pragma ident	"%Z%%M%	%I%	%E% SMI"
31355b4669Sjacobs 
32355b4669Sjacobs #include <stdio.h>
33355b4669Sjacobs #include <stdlib.h>
340a44ef6dSjacobs #include <unistd.h>
350a44ef6dSjacobs #include <fcntl.h>
36355b4669Sjacobs #include <stdarg.h>
37355b4669Sjacobs #include <string.h>
3836615d24Sjacobs #include <ctype.h>
39355b4669Sjacobs #include <errno.h>
40355b4669Sjacobs #include <syslog.h>
41355b4669Sjacobs #include <libintl.h>
420a44ef6dSjacobs #include <pwd.h>
430a44ef6dSjacobs #include <grp.h>
440a44ef6dSjacobs #include <sys/types.h>
450a44ef6dSjacobs #include <sys/stat.h>
460a44ef6dSjacobs #include <sys/socket.h>
470a44ef6dSjacobs #include <netinet/in.h>
480a44ef6dSjacobs #include <arpa/inet.h>
490a44ef6dSjacobs #include <netdb.h>
500a44ef6dSjacobs #include <sys/systeminfo.h>
51355b4669Sjacobs 
52355b4669Sjacobs #include <papi.h>
530a44ef6dSjacobs #include <uri.h>
54355b4669Sjacobs #include "common.h"
55355b4669Sjacobs 
56355b4669Sjacobs #define	ACK(fp)		{ (void) fputc('\0', fp); (void) fflush(fp); }
57355b4669Sjacobs #define	NACK(fp)	{ (void) fputc('\1', fp); (void) fflush(fp); }
58355b4669Sjacobs 
59355b4669Sjacobs /*
60355b4669Sjacobs  * This file contains the front-end of the BSD Print Protocol adaptor.  This
61355b4669Sjacobs  * code assumes a BSD Socket interface to the networking side.
62355b4669Sjacobs  */
63355b4669Sjacobs 
640a44ef6dSjacobs static char *
650a44ef6dSjacobs remote_host_name(FILE *fp)
660a44ef6dSjacobs {
670a44ef6dSjacobs 	struct hostent *hp;
680a44ef6dSjacobs 	struct sockaddr_in6 peer;
690a44ef6dSjacobs 	socklen_t peer_len = sizeof (peer);
700a44ef6dSjacobs 	int fd = fileno(fp);
710a44ef6dSjacobs 	int error_num;
72*a18dc42fSps29005 	char tmp_buf[INET6_ADDRSTRLEN];
730a44ef6dSjacobs 	char *hostname;
740a44ef6dSjacobs 
750a44ef6dSjacobs 	/* who is our peer ? */
760a44ef6dSjacobs 	if (getpeername(fd, (struct sockaddr *)&peer, &peer_len) < 0) {
770a44ef6dSjacobs 		if ((errno != ENOTSOCK) && (errno != EINVAL))
780a44ef6dSjacobs 			return (NULL);
790a44ef6dSjacobs 		else
800a44ef6dSjacobs 			return (strdup("localhost"));
810a44ef6dSjacobs 	}
820a44ef6dSjacobs 
830a44ef6dSjacobs 	/* get their name or return a string containing their address */
840a44ef6dSjacobs 	if ((hp = getipnodebyaddr((const char *)&peer.sin6_addr,
850a44ef6dSjacobs 				sizeof (struct in6_addr), AF_INET6,
860a44ef6dSjacobs 				&error_num)) == NULL) {
870a44ef6dSjacobs 		return (strdup(inet_ntop(peer.sin6_family,
880a44ef6dSjacobs 			&peer.sin6_addr, tmp_buf, sizeof (tmp_buf))));
890a44ef6dSjacobs 	}
900a44ef6dSjacobs 
910a44ef6dSjacobs 	hostname = strdup(hp->h_name);
92*a18dc42fSps29005 	if (is_localhost(hp->h_name) != 0)
930a44ef6dSjacobs 		return (strdup("localhost"));
940a44ef6dSjacobs 
950a44ef6dSjacobs 	/* It must be someone else */
960a44ef6dSjacobs 	return (hostname);
970a44ef6dSjacobs }
980a44ef6dSjacobs 
990a44ef6dSjacobs static void
100355b4669Sjacobs fatal(FILE *fp, char *fmt, ...)
101355b4669Sjacobs {
102355b4669Sjacobs 	va_list ap;
103355b4669Sjacobs 
104355b4669Sjacobs 	va_start(ap, fmt);
105355b4669Sjacobs 	vsyslog(LOG_DEBUG, fmt, ap);
106355b4669Sjacobs 	vfprintf(fp, fmt, ap);
107355b4669Sjacobs 	va_end(ap);
1080a44ef6dSjacobs 	exit(1);
109355b4669Sjacobs }
110355b4669Sjacobs 
111355b4669Sjacobs static void
1120a44ef6dSjacobs cleanup(char ***files, char **cf)
113355b4669Sjacobs {
1140a44ef6dSjacobs 	if (*files != NULL) {
115355b4669Sjacobs 		int i;
116355b4669Sjacobs 
1170a44ef6dSjacobs 		for (i = 0; (*files)[i] != NULL; i++) {
1180a44ef6dSjacobs 			(void) unlink((*files)[i]);
1190a44ef6dSjacobs 			free((*files)[i]);
1200a44ef6dSjacobs 		}
1210a44ef6dSjacobs 		free(*files);
1220a44ef6dSjacobs 		*files = NULL;
1230a44ef6dSjacobs 	}
1240a44ef6dSjacobs 
1250a44ef6dSjacobs 	if (*cf != NULL) {
1260a44ef6dSjacobs 		free(*cf);
1270a44ef6dSjacobs 		*cf = NULL;
128355b4669Sjacobs 	}
129355b4669Sjacobs }
130355b4669Sjacobs 
1310a44ef6dSjacobs static papi_attribute_t **
1320a44ef6dSjacobs parse_cf(papi_service_t svc, char *cf, char **files)
133355b4669Sjacobs {
1340a44ef6dSjacobs 	papi_attribute_t **list = NULL;
1350a44ef6dSjacobs 	char	previous = NULL,
136*a18dc42fSps29005 		*entry;
137*a18dc42fSps29005 	int	copies_set = 0,
1380a44ef6dSjacobs 		copies = 0;
139355b4669Sjacobs 
1400a44ef6dSjacobs 	for (entry = strtok(cf, "\n"); entry != NULL;
1410a44ef6dSjacobs 	    entry = strtok(NULL, "\n")) {
1420a44ef6dSjacobs 		char *format = NULL;
1430a44ef6dSjacobs 
1440a44ef6dSjacobs 		/* count the copies */
1450a44ef6dSjacobs 		if ((entry[0] >= 'a') && (entry[0] <= 'z') &&
1460a44ef6dSjacobs 		    (copies_set == 0) && (previous == entry[0]))
1470a44ef6dSjacobs 			copies++;
1480a44ef6dSjacobs 		else if ((previous >= 'a') && (previous <= 'z'))
1490a44ef6dSjacobs 			copies_set = 1;
1500a44ef6dSjacobs 		previous = entry[0];
1510a44ef6dSjacobs 
1520a44ef6dSjacobs 		/* process the control message */
1530a44ef6dSjacobs 		switch (entry[0]) {
1540a44ef6dSjacobs 		/* RFC-1179 options */
1550a44ef6dSjacobs 		case 'J':	/* RFC-1179 Banner Job Name */
1560a44ef6dSjacobs 			papiAttributeListAddString(&list, PAPI_ATTR_EXCL,
1570a44ef6dSjacobs 					"job-name", ++entry);
1580a44ef6dSjacobs 			break;
1590a44ef6dSjacobs 		case 'C':	/* RFC-1179 Banner Class Name */
1600a44ef6dSjacobs 			papiAttributeListAddString(&list, PAPI_ATTR_EXCL,
1610a44ef6dSjacobs 					"rfc-1179-class", ++entry);
1620a44ef6dSjacobs 			break;
1630a44ef6dSjacobs 		case 'L':	/* RFC-1179 Banner toggle  */
1640a44ef6dSjacobs 			papiAttributeListAddString(&list, PAPI_ATTR_EXCL,
1650a44ef6dSjacobs 					"job-sheets", "standard");
1660a44ef6dSjacobs 			break;
1670a44ef6dSjacobs 		case 'T':	/* RFC-1179 Title (pr)  */
1680a44ef6dSjacobs 			papiAttributeListAddString(&list, PAPI_ATTR_EXCL,
1690a44ef6dSjacobs 					"pr-title", ++entry);
1700a44ef6dSjacobs 			break;
1710a44ef6dSjacobs 		case 'H':	/* RFC-1179 Host */
1720a44ef6dSjacobs 			/*
1730a44ef6dSjacobs 			 * use the host as known by us, not by them
1740a44ef6dSjacobs 			 *
1750a44ef6dSjacobs 			 * papiAttributeListAddString(&list, PAPI_ATTR_EXCL,
1760a44ef6dSjacobs 			 *		"job-originating-host-name", ++entry);
1770a44ef6dSjacobs 			 */
1780a44ef6dSjacobs 			break;
1790a44ef6dSjacobs 		case 'P':	/* RFC-1179 User */
1800a44ef6dSjacobs 			++entry;
1810a44ef6dSjacobs 			papiAttributeListAddString(&list, PAPI_ATTR_EXCL,
1820a44ef6dSjacobs 					"requesting-user-name", entry);
1830a44ef6dSjacobs 			papiServiceSetUserName(svc, entry);
1840a44ef6dSjacobs 			break;
1850a44ef6dSjacobs 		case 'M':	/* RFC-1179 Mail to User */
1860a44ef6dSjacobs 			papiAttributeListAddBoolean(&list, PAPI_ATTR_EXCL,
1870a44ef6dSjacobs 				"rfc-1179-mail", 1);
1880a44ef6dSjacobs 			break;
1890a44ef6dSjacobs 		case 'W':	/* RFC-1179 Width (pr) */
1900a44ef6dSjacobs 			papiAttributeListAddInteger(&list, PAPI_ATTR_EXCL,
1910a44ef6dSjacobs 					"pr-width", atoi(++entry));
1920a44ef6dSjacobs 			break;
1930a44ef6dSjacobs 		case 'I':	/* RFC-1179 Indent (pr) */
1940a44ef6dSjacobs 			papiAttributeListAddInteger(&list, PAPI_ATTR_EXCL,
1950a44ef6dSjacobs 					"pr-indent", atoi(++entry));
1960a44ef6dSjacobs 			break;
1970a44ef6dSjacobs 		case 'N':	/* RFC-1179 Filename */
1980a44ef6dSjacobs 			/* could have HPUX extension embedded */
1990a44ef6dSjacobs 			if (entry[1] != ' ') {	/* real pathname */
2000a44ef6dSjacobs #ifdef DEBUG
2010a44ef6dSjacobs 				papiAttributeListAddString(&list,
2020a44ef6dSjacobs 						PAPI_ATTR_EXCL,
2030a44ef6dSjacobs 						"flist", ++entry);
2040a44ef6dSjacobs #endif
2050a44ef6dSjacobs 			} else if (entry[2] == 'O') /* HPUX lp -o options */
2060a44ef6dSjacobs 				papiAttributeListFromString(&list,
2070a44ef6dSjacobs 						PAPI_ATTR_APPEND, ++entry);
2080a44ef6dSjacobs 			break;
2090a44ef6dSjacobs 		case 'U':	/* RFC-1179 Unlink */
2100a44ef6dSjacobs 			break;	/* ignored */
2110a44ef6dSjacobs 		case '1':	/* RFC-1179 TROFF Font R */
2120a44ef6dSjacobs 			papiAttributeListAddString(&list, PAPI_ATTR_EXCL,
2130a44ef6dSjacobs 				"rfc-1179-font-r", ++entry);
2140a44ef6dSjacobs 			break;
2150a44ef6dSjacobs 		case '2':	/* RFC-1179 TROFF Font I */
2160a44ef6dSjacobs 			papiAttributeListAddString(&list, PAPI_ATTR_EXCL,
2170a44ef6dSjacobs 				"rfc-1179-font-i", ++entry);
2180a44ef6dSjacobs 			break;
2190a44ef6dSjacobs 		case '3':	/* RFC-1179 TROFF Font B */
2200a44ef6dSjacobs 			papiAttributeListAddString(&list, PAPI_ATTR_EXCL,
2210a44ef6dSjacobs 				"rfc-1179-font-b", ++entry);
2220a44ef6dSjacobs 			break;
2230a44ef6dSjacobs 		case '4':	/* RFC-1179 TROFF Font S */
2240a44ef6dSjacobs 			papiAttributeListAddString(&list, PAPI_ATTR_EXCL,
2250a44ef6dSjacobs 				"rfc-1179-font-s", ++entry);
2260a44ef6dSjacobs 			break;
2270a44ef6dSjacobs 		case 'f':	/* RFC-1179 ASCII file (print) */
2280a44ef6dSjacobs 			format = "text/plain";
2290a44ef6dSjacobs 			if (is_postscript(files[0]) == 1)
2300a44ef6dSjacobs 				format = "application/postscript";
2310a44ef6dSjacobs 			break;
2320a44ef6dSjacobs 		case 'l':	/* RFC-1179 CATV file (print) */
2330a44ef6dSjacobs 			format = "application/octet-stream";
2340a44ef6dSjacobs 			if (is_postscript(files[0]) == 1)
2350a44ef6dSjacobs 				format = "application/postscript";
2360a44ef6dSjacobs 			break;
2370a44ef6dSjacobs 		case 'o':	/* RFC-1179 Postscript file (print) */
2380a44ef6dSjacobs 			format = "application/postscript";
2390a44ef6dSjacobs 			break;
2400a44ef6dSjacobs 		case 'p':	/* RFC-1179 PR file (print) */
2410a44ef6dSjacobs 			format = "application/x-pr";
2420a44ef6dSjacobs 			papiAttributeListAddBoolean(&list, PAPI_ATTR_EXCL,
2430a44ef6dSjacobs 					"pr-filter", 1);
2440a44ef6dSjacobs 			break;
2450a44ef6dSjacobs 		case 't':	/* RFC-1179 TROFF file (print) */
2460a44ef6dSjacobs 			format = "application/x-troff";
2470a44ef6dSjacobs 			break;
2480a44ef6dSjacobs 		case 'n':	/* RFC-1179 DITROFF file (print) */
2490a44ef6dSjacobs 			format = "application/x-ditroff";
2500a44ef6dSjacobs 			break;
2510a44ef6dSjacobs 		case 'd':	/* RFC-1179 DVI file (print) */
2520a44ef6dSjacobs 			format = "application/x-dvi";
2530a44ef6dSjacobs 			break;
2540a44ef6dSjacobs 		case 'g':	/* RFC-1179 GRAPH file (print) */
2550a44ef6dSjacobs 			format = "application/x-plot";
2560a44ef6dSjacobs 			break;
2570a44ef6dSjacobs 		case 'c':	/* RFC-1179 CIF file (print) */
2580a44ef6dSjacobs 			format = "application/x-cif";
2590a44ef6dSjacobs 			break;
2600a44ef6dSjacobs 		case 'v':	/* RFC-1179 RASTER file (print) */
2610a44ef6dSjacobs 			format = "application/x-raster";
2620a44ef6dSjacobs 			break;
2630a44ef6dSjacobs 		case 'r':	/* RFC-1179 FORTRAN file (print) */
2640a44ef6dSjacobs 			format = "application/x-fortran";
2650a44ef6dSjacobs 			break;
2660a44ef6dSjacobs 		/* Sun Solaris Extensions */
2670a44ef6dSjacobs 		case 'O':
2680a44ef6dSjacobs 			++entry;
269*a18dc42fSps29005 			{
270*a18dc42fSps29005 				int rd, wr;
271*a18dc42fSps29005 
272*a18dc42fSps29005 				for (rd = wr = 0; entry[rd] != '\0'; rd++) {
273*a18dc42fSps29005 					if (entry[rd] == '"')
274*a18dc42fSps29005 						continue;
275*a18dc42fSps29005 					if (rd != wr)
276*a18dc42fSps29005 						entry[wr] = entry[rd];
277*a18dc42fSps29005 					wr++;
278*a18dc42fSps29005 				}
279*a18dc42fSps29005 				entry[wr] = '\0';
280*a18dc42fSps29005 
281*a18dc42fSps29005 				papiAttributeListFromString(&list,
282*a18dc42fSps29005 				    PAPI_ATTR_APPEND, entry);
283*a18dc42fSps29005 			}
2840a44ef6dSjacobs 			break;
2850a44ef6dSjacobs 		case '5':
2860a44ef6dSjacobs 			++entry;
2870a44ef6dSjacobs 			switch (entry[0]) {
2880a44ef6dSjacobs 			case 'f':	/* Solaris form */
2890a44ef6dSjacobs 				papiAttributeListAddString(&list,
2900a44ef6dSjacobs 						PAPI_ATTR_EXCL,
2910a44ef6dSjacobs 						"form", ++entry);
2920a44ef6dSjacobs 				break;
2930a44ef6dSjacobs 			case 'H':	/* Solaris handling */
2940a44ef6dSjacobs 				++entry;
2950a44ef6dSjacobs 				if (strcasecmp(entry, "hold") == 0)
2960a44ef6dSjacobs 					papiAttributeListAddString(&list,
2970a44ef6dSjacobs 						PAPI_ATTR_EXCL,
2980a44ef6dSjacobs 						"job-hold-until", "indefinite");
29943b9c050Sjacobs 				else if (strcasecmp(entry, "immediate") == 0)
3000a44ef6dSjacobs 					papiAttributeListAddString(&list,
3010a44ef6dSjacobs 						PAPI_ATTR_EXCL,
3020a44ef6dSjacobs 						"job-hold-until", "no-hold");
3030a44ef6dSjacobs 				else
3040a44ef6dSjacobs 					papiAttributeListAddString(&list,
3050a44ef6dSjacobs 						PAPI_ATTR_EXCL,
3060a44ef6dSjacobs 						"job-hold-until", entry);
3070a44ef6dSjacobs 				break;
3080a44ef6dSjacobs 			case 'p':	/* Solaris notification */
3090a44ef6dSjacobs 				papiAttributeListAddBoolean(&list,
3100a44ef6dSjacobs 					PAPI_ATTR_EXCL, "rfc-1179-mail", 1);
3110a44ef6dSjacobs 				break;
31243b9c050Sjacobs 			case 'P': {	/* Solaris page list */
31343b9c050Sjacobs 				char buf[BUFSIZ];
31443b9c050Sjacobs 
31543b9c050Sjacobs 				snprintf(buf, sizeof (buf), "page-ranges=%s",
31643b9c050Sjacobs 						++entry);
31743b9c050Sjacobs 				papiAttributeListFromString(&list,
31843b9c050Sjacobs 						PAPI_ATTR_EXCL, buf);
31943b9c050Sjacobs 				}
3200a44ef6dSjacobs 				break;
3210a44ef6dSjacobs 			case 'q': {	/* Solaris priority */
3220a44ef6dSjacobs 				int i = atoi(optarg);
3230a44ef6dSjacobs 
32443b9c050Sjacobs 				i = 100 - (i * 2.5);
3250a44ef6dSjacobs 				if ((i < 1) || (i > 100))
3260a44ef6dSjacobs 					i = 50;
3270a44ef6dSjacobs 				papiAttributeListAddInteger(&list,
32843b9c050Sjacobs 					PAPI_ATTR_EXCL, "job-priority", i);
3290a44ef6dSjacobs 				}
3300a44ef6dSjacobs 				break;
3310a44ef6dSjacobs 			case 'S':	/* Solaris character set */
3320a44ef6dSjacobs 				papiAttributeListAddString(&list,
3330a44ef6dSjacobs 					PAPI_ATTR_EXCL, "lp-charset",
3340a44ef6dSjacobs 					++entry);
3350a44ef6dSjacobs 				break;
3360a44ef6dSjacobs 			case 'T':	/* Solaris type */
3370a44ef6dSjacobs 				format = lp_type_to_mime_type(++entry);
3380a44ef6dSjacobs 				break;
3390a44ef6dSjacobs 			case 'y':	/* Solaris mode */
3400a44ef6dSjacobs 				papiAttributeListAddString(&list,
3410a44ef6dSjacobs 					PAPI_ATTR_APPEND, "lp-modes", ++entry);
3420a44ef6dSjacobs 				break;
3430a44ef6dSjacobs 			default:
3440a44ef6dSjacobs 				syslog(LOG_INFO|LOG_DEBUG,
3450a44ef6dSjacobs 					"Warning: cf message (%s) ignored",
3460a44ef6dSjacobs 					entry);
3470a44ef6dSjacobs 				break;
3480a44ef6dSjacobs 			}
3490a44ef6dSjacobs 			break;
3500a44ef6dSjacobs 		/* Undefined Extensions: SCO, Ultrix, AIX, ... */
3510a44ef6dSjacobs 
3520a44ef6dSjacobs 		default:
3530a44ef6dSjacobs 			syslog(LOG_INFO|LOG_DEBUG,
3540a44ef6dSjacobs 				"Warning: cf message (%s) ignored", entry);
3550a44ef6dSjacobs 			break;
3560a44ef6dSjacobs 		}
3570a44ef6dSjacobs 
3580a44ef6dSjacobs 		if (format != NULL)
3590a44ef6dSjacobs 			papiAttributeListAddString(&list, PAPI_ATTR_EXCL,
3600a44ef6dSjacobs 				"document-format", format);
3610a44ef6dSjacobs 	}
3620a44ef6dSjacobs 
3630a44ef6dSjacobs 	papiAttributeListAddInteger(&list, PAPI_ATTR_EXCL,
3640a44ef6dSjacobs 					"copies", ++copies);
3650a44ef6dSjacobs 	papiAttributeListAddString(&list, PAPI_ATTR_EXCL,
3660a44ef6dSjacobs 					"job-sheets", "none");
3670a44ef6dSjacobs 
3680a44ef6dSjacobs 	return (list);
3690a44ef6dSjacobs }
3700a44ef6dSjacobs 
3710a44ef6dSjacobs static papi_status_t
37236615d24Sjacobs submit_job(papi_service_t svc, FILE *ifp, char *printer, int rid, char *cf,
37336615d24Sjacobs 		char **files)
3740a44ef6dSjacobs {
3750a44ef6dSjacobs 	papi_attribute_t **list = NULL;
3760a44ef6dSjacobs 	papi_status_t status;
3770a44ef6dSjacobs 	papi_job_t job = NULL;
3780a44ef6dSjacobs 	char *format = "";
3790a44ef6dSjacobs 
3800a44ef6dSjacobs 	if ((list = parse_cf(svc, cf, files)) != NULL) {
3810a44ef6dSjacobs 		/* use the host as known by us, not by them */
3820a44ef6dSjacobs 		char *host = remote_host_name(ifp);
3830a44ef6dSjacobs 
3840a44ef6dSjacobs 		if (host != NULL) {
3850a44ef6dSjacobs 			papiAttributeListAddString(&list, PAPI_ATTR_REPLACE,
3860a44ef6dSjacobs 					"job-originating-host-name", host);
3870a44ef6dSjacobs 			free(host);
3880a44ef6dSjacobs 		}
38936615d24Sjacobs 		if (rid > 0) {
39036615d24Sjacobs 			papiAttributeListAddInteger(&list, PAPI_ATTR_EXCL,
39136615d24Sjacobs 					"job-id-requested", rid);
39236615d24Sjacobs 		}
3930a44ef6dSjacobs 	}
3940a44ef6dSjacobs 
3950a44ef6dSjacobs 	status = papiJobSubmit(svc, printer, list, NULL, files, &job);
3960a44ef6dSjacobs 	syslog(LOG_DEBUG, "submit: %s", papiStatusString(status));
3970a44ef6dSjacobs 	if (status != PAPI_OK) {
3980a44ef6dSjacobs 		char *tmp = papiServiceGetStatusMessage(svc);
3990a44ef6dSjacobs 
4000a44ef6dSjacobs 		syslog(LOG_DEBUG, "submit-detail: %s", tmp ? tmp : "none");
4010a44ef6dSjacobs 	}
4020a44ef6dSjacobs 	papiJobFree(job);
4030a44ef6dSjacobs 
4040a44ef6dSjacobs 	return (status);
4050a44ef6dSjacobs }
4060a44ef6dSjacobs 
4070a44ef6dSjacobs static char *
4080a44ef6dSjacobs receive_control_file(papi_service_t svc, FILE *ifp, FILE *ofp, int size)
4090a44ef6dSjacobs {
4100a44ef6dSjacobs 	char *ptr, *cf_data;
4110a44ef6dSjacobs 
4120a44ef6dSjacobs 	if ((ptr = cf_data = calloc(1, size + 1)) == NULL) {
4130a44ef6dSjacobs 		NACK(ofp);
4140a44ef6dSjacobs 		return (NULL);
4150a44ef6dSjacobs 	} else
416355b4669Sjacobs 		ACK(ofp);
417355b4669Sjacobs 
4180a44ef6dSjacobs 	while (size > 0) {
4190a44ef6dSjacobs 		int rc;
420355b4669Sjacobs 
4210a44ef6dSjacobs 		if (((rc = fread(ptr, 1, size, ifp)) == 0) &&
4220a44ef6dSjacobs 		    (feof(ifp) != 0)) {
4230a44ef6dSjacobs 			free(cf_data);
4240a44ef6dSjacobs 			return (NULL);
4250a44ef6dSjacobs 		} else {
4260a44ef6dSjacobs 			ptr += rc;
4270a44ef6dSjacobs 			size -= rc;
4280a44ef6dSjacobs 		}
4290a44ef6dSjacobs 	}
4300a44ef6dSjacobs 	syslog(LOG_DEBUG, "cf_data(%s)", cf_data);
4310a44ef6dSjacobs 
4320a44ef6dSjacobs 	if (fgetc(ifp) != 0) {
4330a44ef6dSjacobs 		free(cf_data);
4340a44ef6dSjacobs 		return (NULL);
4350a44ef6dSjacobs 	}
4360a44ef6dSjacobs 	ACK(ofp);
4370a44ef6dSjacobs 
4380a44ef6dSjacobs 	return (cf_data);
4390a44ef6dSjacobs }
4400a44ef6dSjacobs 
4410a44ef6dSjacobs static char *
4420a44ef6dSjacobs receive_data_file(FILE *ifp, FILE *ofp, int size)
4430a44ef6dSjacobs {
444355b4669Sjacobs 	char file[] = "lpdXXXXXX";
4450a44ef6dSjacobs 	char buf[BUFSIZ];
446355b4669Sjacobs 	int fd;
447355b4669Sjacobs 
4480a44ef6dSjacobs 	if ((fd = mkstemp(file)) < 0) {
4490a44ef6dSjacobs 		NACK(ofp);
4500a44ef6dSjacobs 		return (NULL);
4510a44ef6dSjacobs 	} else
4520a44ef6dSjacobs 		ACK(ofp);
453355b4669Sjacobs 
4540a44ef6dSjacobs 	while (size > 0) {
4550a44ef6dSjacobs 		int rc = ((size > BUFSIZ) ? BUFSIZ : size);
4560a44ef6dSjacobs 
4570a44ef6dSjacobs 		if (((rc = fread(buf, 1, rc, ifp)) == 0) &&
4580a44ef6dSjacobs 		    (feof(ifp) != 0)) {
4590a44ef6dSjacobs 			close(fd);
4600a44ef6dSjacobs 			unlink(file);
4610a44ef6dSjacobs 			return (NULL);
4620a44ef6dSjacobs 		} else {
4630a44ef6dSjacobs 			char *ptr = buf;
4640a44ef6dSjacobs 
4650a44ef6dSjacobs 			while (rc > 0) {
4660a44ef6dSjacobs 				int wrc = write(fd, ptr, rc);
4670a44ef6dSjacobs 
4680a44ef6dSjacobs 				if (wrc < 0) {
4690a44ef6dSjacobs 					close(fd);
4700a44ef6dSjacobs 					unlink(file);
4710a44ef6dSjacobs 					return (NULL);
4720a44ef6dSjacobs 				}
4730a44ef6dSjacobs 
4740a44ef6dSjacobs 				ptr += wrc;
4750a44ef6dSjacobs 				size -= wrc;
4760a44ef6dSjacobs 				rc -= wrc;
4770a44ef6dSjacobs 			}
4780a44ef6dSjacobs 		}
4790a44ef6dSjacobs 	}
4800a44ef6dSjacobs 	close(fd);
4810a44ef6dSjacobs 	if (fgetc(ifp) != 0) {
4820a44ef6dSjacobs 		unlink(file);
4830a44ef6dSjacobs 		return (NULL);
4840a44ef6dSjacobs 	}
4850a44ef6dSjacobs 	ACK(ofp);
4860a44ef6dSjacobs 
4870a44ef6dSjacobs 	return (strdup(file));
4880a44ef6dSjacobs }
4890a44ef6dSjacobs 
4900a44ef6dSjacobs static papi_status_t
4910a44ef6dSjacobs berkeley_receive_files(papi_service_t svc, FILE *ifp, FILE *ofp, char *printer)
4920a44ef6dSjacobs {
4930a44ef6dSjacobs 	papi_status_t status = PAPI_OK;
4940a44ef6dSjacobs 	char *file, **files = NULL;	/* the job data files */
4950a44ef6dSjacobs 	char *cf = NULL;
49636615d24Sjacobs 	int rid = 0;
4970a44ef6dSjacobs 	char buf[BUFSIZ];
4980a44ef6dSjacobs 
4990a44ef6dSjacobs 	while (fgets(buf, sizeof (buf), ifp) != NULL) {
5000a44ef6dSjacobs 		int size;
5010a44ef6dSjacobs 
5020a44ef6dSjacobs 		syslog(LOG_DEBUG, "XFER CMD: (%d)%s\n", buf[0], &buf[1]);
5030a44ef6dSjacobs #ifdef DEBUG	/* translate [1-3]... messages to \[1-3] to run by hand */
5040a44ef6dSjacobs 		if ((buf[0] > '0') && (buf[0] < '4'))
5050a44ef6dSjacobs 			buf[0] -= '0';
5060a44ef6dSjacobs #endif
5070a44ef6dSjacobs 		switch (buf[0]) {
5080a44ef6dSjacobs 		case 0x01:	/* Abort */
5090a44ef6dSjacobs 			cleanup(&files, &cf);
5100a44ef6dSjacobs 			break;
5110a44ef6dSjacobs 		case 0x02: {	/* Receive control file */
51236615d24Sjacobs 			if (((cf = strchr(buf, ' ')) != NULL) &&
51336615d24Sjacobs 			    (strlen(cf) > 4)) {
51436615d24Sjacobs 				while ((*cf != NULL) && (isdigit(*cf) == 0))
51536615d24Sjacobs 					cf++;
51636615d24Sjacobs 				rid = atoi(cf);
51736615d24Sjacobs 			}
5180a44ef6dSjacobs 			cf = receive_control_file(svc, ifp, ofp, atoi(&buf[1]));
5190a44ef6dSjacobs 			if (cf == NULL) {
5200a44ef6dSjacobs 				cleanup(&files, &cf);
5210a44ef6dSjacobs 				return (PAPI_BAD_REQUEST);
5220a44ef6dSjacobs 			} else if (files != NULL) {
52336615d24Sjacobs 				status = submit_job(svc, ifp, printer, rid, cf,
5240a44ef6dSjacobs 							files);
5250a44ef6dSjacobs 				cleanup(&files, &cf);
5260a44ef6dSjacobs 			}
5270a44ef6dSjacobs 			}
5280a44ef6dSjacobs 			break;
5290a44ef6dSjacobs 		case 0x03: {	/* Receive data file */
5300a44ef6dSjacobs 			file = receive_data_file(ifp, ofp, atoi(&buf[1]));
5310a44ef6dSjacobs 			if (file == NULL) {
5320a44ef6dSjacobs 				cleanup(&files, &cf);
5330a44ef6dSjacobs 				return (PAPI_TEMPORARY_ERROR);
5340a44ef6dSjacobs 			}
5350a44ef6dSjacobs 			list_append(&files, file);
536355b4669Sjacobs 			}
537355b4669Sjacobs 			break;
538355b4669Sjacobs 		default:
5390a44ef6dSjacobs 			cleanup(&files, &cf);
540355b4669Sjacobs 			fatal(ofp, "protocol screwup");
541355b4669Sjacobs 			break;
542355b4669Sjacobs 		}
543355b4669Sjacobs 	}
544355b4669Sjacobs 
5450a44ef6dSjacobs 	if ((cf != NULL) && (files != NULL))
54636615d24Sjacobs 		status = submit_job(svc, ifp, printer, rid, cf, files);
5470a44ef6dSjacobs 
5480a44ef6dSjacobs 	cleanup(&files, &cf);
5490a44ef6dSjacobs 
5500a44ef6dSjacobs 	return (status);
551355b4669Sjacobs }
552355b4669Sjacobs 
5530a44ef6dSjacobs static papi_status_t
554355b4669Sjacobs berkeley_transfer_files(papi_service_t svc, FILE *ifp, FILE *ofp,
555355b4669Sjacobs 		char *printer)
556355b4669Sjacobs {
557355b4669Sjacobs 	papi_status_t status;
558355b4669Sjacobs 	papi_printer_t p = NULL;
5590a44ef6dSjacobs 	char *keys[] = { "printer-is-accepting-jobs", NULL };
560355b4669Sjacobs 
561355b4669Sjacobs 	status = papiPrinterQuery(svc, printer, keys, NULL, &p);
562355b4669Sjacobs 	if ((status == PAPI_OK) && (p != NULL)) {
563355b4669Sjacobs 		papi_attribute_t **attrs = papiPrinterGetAttributeList(p);
564355b4669Sjacobs 		char accepting = PAPI_FALSE;
565355b4669Sjacobs 
566355b4669Sjacobs 		papiAttributeListGetBoolean(attrs, NULL,
5670a44ef6dSjacobs 				"printer-is-accepting-jobs", &accepting);
568355b4669Sjacobs 
5690a44ef6dSjacobs 		if (accepting == PAPI_TRUE) {
5700a44ef6dSjacobs 			ACK(ofp);
5710a44ef6dSjacobs 			status = berkeley_receive_files(svc, ifp, ofp, printer);
5720a44ef6dSjacobs 		} else
573355b4669Sjacobs 			NACK(ofp);
574355b4669Sjacobs 
575355b4669Sjacobs 		papiPrinterFree(p);
576355b4669Sjacobs 	} else
577355b4669Sjacobs 		NACK(ofp);
5780a44ef6dSjacobs 
5790a44ef6dSjacobs 	return (status);
580355b4669Sjacobs }
581355b4669Sjacobs 
5820a44ef6dSjacobs static int
5830a44ef6dSjacobs cyclical_service_check(char *svc_name)
5840a44ef6dSjacobs {
5850a44ef6dSjacobs 	papi_attribute_t **list;
5860a44ef6dSjacobs 	uri_t *uri = NULL;
5870a44ef6dSjacobs 	char *s = NULL;
5880a44ef6dSjacobs 
5890a44ef6dSjacobs 	/* was there a printer? */
5900a44ef6dSjacobs 	if (svc_name == NULL)
5910a44ef6dSjacobs 		return (0);
5920a44ef6dSjacobs 
5930a44ef6dSjacobs 	if ((list = getprinterbyname(svc_name, NULL)) == NULL)
5940a44ef6dSjacobs 		return (0);	/* if it doesnt' resolve, we will fail later */
5950a44ef6dSjacobs 
5960a44ef6dSjacobs 	papiAttributeListGetString(list, NULL, "printer-uri-supported", &s);
5970a44ef6dSjacobs 	if ((s == NULL) || (strcasecmp(svc_name, s) != 0))
5980a44ef6dSjacobs 		return (0);	/* they don't match */
5990a44ef6dSjacobs 
6000a44ef6dSjacobs 	/* is it in uri form? */
6010a44ef6dSjacobs 	if (uri_from_string(s, &uri) < 0)
6020a44ef6dSjacobs 		return (0);
6030a44ef6dSjacobs 
6040a44ef6dSjacobs 	if ((uri == NULL) || (uri->scheme == NULL) || (uri->host == NULL)) {
6050a44ef6dSjacobs 		uri_free(uri);
6060a44ef6dSjacobs 		return (0);
6070a44ef6dSjacobs 	}
6080a44ef6dSjacobs 
6090a44ef6dSjacobs 	/* is it in lpd form? */
6100a44ef6dSjacobs 	if (strcasecmp(uri->scheme, "lpd") != 0) {
6110a44ef6dSjacobs 		uri_free(uri);
6120a44ef6dSjacobs 		return (0);
6130a44ef6dSjacobs 	}
6140a44ef6dSjacobs 
6150a44ef6dSjacobs 	/* is it the local host? */
616*a18dc42fSps29005 	if (is_localhost(uri->host) != 0) {
6170a44ef6dSjacobs 		uri_free(uri);
6180a44ef6dSjacobs 		return (0);
6190a44ef6dSjacobs 	}
6200a44ef6dSjacobs 
6210a44ef6dSjacobs 	uri_free(uri);
6220a44ef6dSjacobs 	return (1);
6230a44ef6dSjacobs }
6240a44ef6dSjacobs 
6250a44ef6dSjacobs 
626355b4669Sjacobs /*
627355b4669Sjacobs  * This is the entry point for this program.  The program takes the
628355b4669Sjacobs  * following options:
629355b4669Sjacobs  * 	(none)
630355b4669Sjacobs  */
631355b4669Sjacobs int
632355b4669Sjacobs main(int ac, char *av[])
633355b4669Sjacobs {
634355b4669Sjacobs 	papi_status_t status;
635355b4669Sjacobs 	papi_service_t svc = NULL;
636355b4669Sjacobs 	papi_encryption_t encryption = PAPI_ENCRYPT_NEVER;
637355b4669Sjacobs 	FILE	*ifp = stdin,
638355b4669Sjacobs 		*ofp = stdout;
639355b4669Sjacobs 	int	c;
640355b4669Sjacobs 	char	buf[BUFSIZ],
641355b4669Sjacobs 		**args,
6420a44ef6dSjacobs 		*printer,
6430a44ef6dSjacobs 		*run_dir = "/var/run/in.lpd",
6440a44ef6dSjacobs 		*run_user = NULL;
6450a44ef6dSjacobs 	struct passwd *pw = NULL;
646355b4669Sjacobs 
6470a44ef6dSjacobs 	(void) chdir("/tmp");		/* run in /tmp by default */
648355b4669Sjacobs 	openlog("bsd-gw", LOG_PID, LOG_LPR);
649355b4669Sjacobs 
6500a44ef6dSjacobs 	while ((c = getopt(ac, av, "Ed:u:")) != EOF)
651355b4669Sjacobs 		switch (c) {
652355b4669Sjacobs 		case 'E':
653355b4669Sjacobs 			encryption = PAPI_ENCRYPT_ALWAYS;
654355b4669Sjacobs 			break;
6550a44ef6dSjacobs 		case 'd':	/* run where they tell you */
6560a44ef6dSjacobs 			run_dir = optarg;
6570a44ef6dSjacobs 			break;
6580a44ef6dSjacobs 		case 'u':	/* run as */
6590a44ef6dSjacobs 			run_user = optarg;
6600a44ef6dSjacobs 			break;
661355b4669Sjacobs 		default:
662355b4669Sjacobs 			;
663355b4669Sjacobs 		}
664355b4669Sjacobs 
6650a44ef6dSjacobs 	if (run_user != NULL)	/* get the requested user info */
6660a44ef6dSjacobs 		pw = getpwnam(run_user);
6670a44ef6dSjacobs 
6680a44ef6dSjacobs 	if (run_dir != NULL) {	/* setup the run_dir */
6690a44ef6dSjacobs 		(void) mkdir(run_dir, 0700);
6700a44ef6dSjacobs 		if (pw != NULL)
6710a44ef6dSjacobs 			(void) chown(run_dir, pw->pw_uid, pw->pw_gid);
6720a44ef6dSjacobs 	}
6730a44ef6dSjacobs 
6740a44ef6dSjacobs 	if (pw != NULL) {	/* run as the requested user */
6750a44ef6dSjacobs 		syslog(LOG_DEBUG, "name: %s, uid: %d, gid: %d",
6760a44ef6dSjacobs 				pw->pw_name, pw->pw_uid, pw->pw_gid);
6770a44ef6dSjacobs 		initgroups(pw->pw_name, pw->pw_gid);
6780a44ef6dSjacobs 		setgid(pw->pw_gid);
6790a44ef6dSjacobs 		setuid(pw->pw_uid);
6800a44ef6dSjacobs 	}
6810a44ef6dSjacobs 
6820a44ef6dSjacobs 	if (run_dir != NULL)	/* move to the run_dir */
6830a44ef6dSjacobs 		if (chdir(run_dir) < 0) {
6840a44ef6dSjacobs 			syslog(LOG_DEBUG, "failed to chdir(%s)", run_dir);
6850a44ef6dSjacobs 			exit(1);
6860a44ef6dSjacobs 		}
6870a44ef6dSjacobs 
6880a44ef6dSjacobs 	syslog(LOG_DEBUG, "$CWD = %s", getwd(NULL));
6890a44ef6dSjacobs 
690355b4669Sjacobs 	if (fgets(buf, sizeof (buf), ifp) == NULL) {
691355b4669Sjacobs 		if (feof(ifp) == 0)
692355b4669Sjacobs 			syslog(LOG_ERR, "Error reading from connection: %s",
693355b4669Sjacobs 				strerror(errno));
694355b4669Sjacobs 		exit(1);
695355b4669Sjacobs 	}
696355b4669Sjacobs 
6970a44ef6dSjacobs 	syslog(LOG_DEBUG, "CMD: (%d)%s\n", buf[0], &buf[1]);
6980a44ef6dSjacobs 
6990a44ef6dSjacobs #ifdef DEBUG	/* translate [1-5]... messages to \[1-5] to run by hand */
7000a44ef6dSjacobs 	if ((buf[0] > '0') && (buf[0] < '6'))
7010a44ef6dSjacobs 		buf[0] -= '0';
7020a44ef6dSjacobs #endif
7030a44ef6dSjacobs 
704355b4669Sjacobs 	if ((buf[0] < 1) || (buf[0] > 5)) {
705355b4669Sjacobs 		fatal(ofp, "Invalid protocol request (%d): %c%s\n",
706355b4669Sjacobs 			buf[0], buf[0], buf);
707355b4669Sjacobs 		exit(1);
708355b4669Sjacobs 	}
709355b4669Sjacobs 
710355b4669Sjacobs 	args = strsplit(&buf[1], "\t\n ");
711355b4669Sjacobs 	printer = *args++;
712355b4669Sjacobs 
713355b4669Sjacobs 	if (printer == NULL) {
714355b4669Sjacobs 		fatal(ofp, "Can't determine requested printer");
715355b4669Sjacobs 		exit(1);
716355b4669Sjacobs 	}
717355b4669Sjacobs 
7180a44ef6dSjacobs 	if (cyclical_service_check(printer) != 0) {
7190a44ef6dSjacobs 		fatal(ofp, "%s is cyclical\n", printer);
7200a44ef6dSjacobs 		exit(1);
7210a44ef6dSjacobs 	}
7220a44ef6dSjacobs 
723355b4669Sjacobs 	status = papiServiceCreate(&svc, printer, NULL, NULL, NULL,
724355b4669Sjacobs 					encryption, NULL);
725355b4669Sjacobs 	if (status != PAPI_OK) {
726355b4669Sjacobs 		fatal(ofp, "Failed to contact service for %s: %s\n", printer,
727355b4669Sjacobs 			verbose_papi_message(svc, status));
728355b4669Sjacobs 		exit(1);
729355b4669Sjacobs 	}
730355b4669Sjacobs 
7310a44ef6dSjacobs 	/*
7320a44ef6dSjacobs 	 * Trusted Solaris can't be trusting of intermediaries.  Pass
7330a44ef6dSjacobs 	 * the socket connection to the print service to retrieve the
7340a44ef6dSjacobs 	 * sensativity label off of a multi-level port.
7350a44ef6dSjacobs 	 */
7360a44ef6dSjacobs 	(void) papiServiceSetPeer(svc, fileno(ifp));
737355b4669Sjacobs 
738355b4669Sjacobs 	switch (buf[0]) {
739355b4669Sjacobs 	case '\1':	/* restart printer */
740355b4669Sjacobs 		ACK(ofp);	/* there is no equivalent */
741355b4669Sjacobs 		break;
742355b4669Sjacobs 	case '\2':	/* transfer job(s) */
7430a44ef6dSjacobs 		status = berkeley_transfer_files(svc, ifp, ofp, printer);
744355b4669Sjacobs 		break;
745355b4669Sjacobs 	case '\3':	/* show queue (short) */
746355b4669Sjacobs 	case '\4': {	/* show queue (long) */
747355b4669Sjacobs 		int count;
748355b4669Sjacobs 
749355b4669Sjacobs 		for (count = 0; args[count] != 0; count++);
750355b4669Sjacobs 
751355b4669Sjacobs 		berkeley_queue_report(svc, ofp, printer, buf[0], count, args);
752355b4669Sjacobs 		}
753355b4669Sjacobs 		break;
754355b4669Sjacobs 	case '\5': {	/* cancel job(s) */
7550a44ef6dSjacobs 		char *user = *args++;
7560a44ef6dSjacobs 		char *host = remote_host_name(ifp);
757355b4669Sjacobs 		int count;
758355b4669Sjacobs 
7590a44ef6dSjacobs 		if (host != NULL) {
7600a44ef6dSjacobs 			char buf[BUFSIZ];
7610a44ef6dSjacobs 
7620a44ef6dSjacobs 			snprintf(buf, sizeof (buf), "%s@%s", user, host);
7630a44ef6dSjacobs 			status = papiServiceSetUserName(svc, buf);
7640a44ef6dSjacobs 		} else
7650a44ef6dSjacobs 			status = papiServiceSetUserName(svc, user);
7660a44ef6dSjacobs 
767355b4669Sjacobs 		for (count = 0; args[count] != 0; count++);
768355b4669Sjacobs 
769355b4669Sjacobs 		berkeley_cancel_request(svc, ofp, printer, count, args);
770355b4669Sjacobs 		}
771355b4669Sjacobs 		break;
772355b4669Sjacobs 	default:
773355b4669Sjacobs 		fatal(ofp, "unsupported protocol request (%c), %s",
774355b4669Sjacobs 			buf[0], &buf[1]);
775355b4669Sjacobs 	}
776355b4669Sjacobs 
777355b4669Sjacobs 	(void) fflush(ofp);
778355b4669Sjacobs 
779355b4669Sjacobs 	syslog(LOG_DEBUG, "protocol request(%d) for %s completed: %s",
780355b4669Sjacobs 		buf[0], printer, papiStatusString(status));
7810a44ef6dSjacobs 	if (status != PAPI_OK)
7820a44ef6dSjacobs 		syslog(LOG_DEBUG, "detail: %s",
7830a44ef6dSjacobs 				verbose_papi_message(svc, status));
784355b4669Sjacobs 
785355b4669Sjacobs 	papiServiceDestroy(svc);
786355b4669Sjacobs 
787355b4669Sjacobs 	return (0);
788355b4669Sjacobs }
789