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 /* 23*43b9c050Sjacobs * 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> 38355b4669Sjacobs #include <errno.h> 39355b4669Sjacobs #include <syslog.h> 40355b4669Sjacobs #include <libintl.h> 410a44ef6dSjacobs #include <pwd.h> 420a44ef6dSjacobs #include <grp.h> 430a44ef6dSjacobs #include <sys/types.h> 440a44ef6dSjacobs #include <sys/stat.h> 450a44ef6dSjacobs #include <sys/socket.h> 460a44ef6dSjacobs #include <netinet/in.h> 470a44ef6dSjacobs #include <arpa/inet.h> 480a44ef6dSjacobs #include <netdb.h> 490a44ef6dSjacobs #include <sys/systeminfo.h> 50355b4669Sjacobs 51355b4669Sjacobs #include <papi.h> 520a44ef6dSjacobs #include <uri.h> 53355b4669Sjacobs #include "common.h" 54355b4669Sjacobs 55355b4669Sjacobs #define ACK(fp) { (void) fputc('\0', fp); (void) fflush(fp); } 56355b4669Sjacobs #define NACK(fp) { (void) fputc('\1', fp); (void) fflush(fp); } 57355b4669Sjacobs 58355b4669Sjacobs /* 59355b4669Sjacobs * This file contains the front-end of the BSD Print Protocol adaptor. This 60355b4669Sjacobs * code assumes a BSD Socket interface to the networking side. 61355b4669Sjacobs */ 62355b4669Sjacobs 630a44ef6dSjacobs static char * 640a44ef6dSjacobs remote_host_name(FILE *fp) 650a44ef6dSjacobs { 660a44ef6dSjacobs struct hostent *hp; 670a44ef6dSjacobs struct sockaddr_in6 peer; 680a44ef6dSjacobs socklen_t peer_len = sizeof (peer); 690a44ef6dSjacobs int fd = fileno(fp); 700a44ef6dSjacobs int error_num; 710a44ef6dSjacobs char myname[MAXHOSTNAMELEN], tmp_buf[INET6_ADDRSTRLEN]; 720a44ef6dSjacobs char *hostname; 730a44ef6dSjacobs 740a44ef6dSjacobs /* who is our peer ? */ 750a44ef6dSjacobs if (getpeername(fd, (struct sockaddr *)&peer, &peer_len) < 0) { 760a44ef6dSjacobs if ((errno != ENOTSOCK) && (errno != EINVAL)) 770a44ef6dSjacobs return (NULL); 780a44ef6dSjacobs else 790a44ef6dSjacobs return (strdup("localhost")); 800a44ef6dSjacobs } 810a44ef6dSjacobs 820a44ef6dSjacobs /* get their name or return a string containing their address */ 830a44ef6dSjacobs if ((hp = getipnodebyaddr((const char *)&peer.sin6_addr, 840a44ef6dSjacobs sizeof (struct in6_addr), AF_INET6, 850a44ef6dSjacobs &error_num)) == NULL) { 860a44ef6dSjacobs return (strdup(inet_ntop(peer.sin6_family, 870a44ef6dSjacobs &peer.sin6_addr, tmp_buf, sizeof (tmp_buf)))); 880a44ef6dSjacobs } 890a44ef6dSjacobs 900a44ef6dSjacobs /* is it "localhost" ? */ 910a44ef6dSjacobs if (strcasecmp(hp->h_name, "localhost") == 0) 920a44ef6dSjacobs return (strdup("localhost")); 930a44ef6dSjacobs 940a44ef6dSjacobs /* duplicate the name because gethostbyXXXX() is not reentrant */ 950a44ef6dSjacobs hostname = strdup(hp->h_name); 960a44ef6dSjacobs (void) sysinfo(SI_HOSTNAME, myname, sizeof (myname)); 970a44ef6dSjacobs 980a44ef6dSjacobs /* is it from one of my addresses ? */ 990a44ef6dSjacobs if ((hp = getipnodebyname(myname, AF_INET6, AI_ALL|AI_V4MAPPED, 1000a44ef6dSjacobs &error_num)) != NULL) { 1010a44ef6dSjacobs struct in6_addr **tmp = (struct in6_addr **)hp->h_addr_list; 1020a44ef6dSjacobs int i = 0; 1030a44ef6dSjacobs 1040a44ef6dSjacobs while (tmp[i] != NULL) { 1050a44ef6dSjacobs if (memcmp(tmp[i++], &peer.sin6_addr, hp->h_length) 1060a44ef6dSjacobs == 0) { 1070a44ef6dSjacobs free(hostname); 1080a44ef6dSjacobs return (strdup("localhost")); 1090a44ef6dSjacobs } 1100a44ef6dSjacobs } 1110a44ef6dSjacobs } 1120a44ef6dSjacobs 1130a44ef6dSjacobs /* It must be someone else */ 1140a44ef6dSjacobs return (hostname); 1150a44ef6dSjacobs } 1160a44ef6dSjacobs 1170a44ef6dSjacobs static void 118355b4669Sjacobs fatal(FILE *fp, char *fmt, ...) 119355b4669Sjacobs { 120355b4669Sjacobs va_list ap; 121355b4669Sjacobs 122355b4669Sjacobs va_start(ap, fmt); 123355b4669Sjacobs vsyslog(LOG_DEBUG, fmt, ap); 124355b4669Sjacobs vfprintf(fp, fmt, ap); 125355b4669Sjacobs va_end(ap); 1260a44ef6dSjacobs exit(1); 127355b4669Sjacobs } 128355b4669Sjacobs 129355b4669Sjacobs static void 1300a44ef6dSjacobs cleanup(char ***files, char **cf) 131355b4669Sjacobs { 1320a44ef6dSjacobs if (*files != NULL) { 133355b4669Sjacobs int i; 134355b4669Sjacobs 1350a44ef6dSjacobs for (i = 0; (*files)[i] != NULL; i++) { 1360a44ef6dSjacobs (void) unlink((*files)[i]); 1370a44ef6dSjacobs free((*files)[i]); 1380a44ef6dSjacobs } 1390a44ef6dSjacobs free(*files); 1400a44ef6dSjacobs *files = NULL; 1410a44ef6dSjacobs } 1420a44ef6dSjacobs 1430a44ef6dSjacobs if (*cf != NULL) { 1440a44ef6dSjacobs free(*cf); 1450a44ef6dSjacobs *cf = NULL; 146355b4669Sjacobs } 147355b4669Sjacobs } 148355b4669Sjacobs 1490a44ef6dSjacobs static papi_attribute_t ** 1500a44ef6dSjacobs parse_cf(papi_service_t svc, char *cf, char **files) 151355b4669Sjacobs { 1520a44ef6dSjacobs papi_attribute_t **list = NULL; 1530a44ef6dSjacobs char previous = NULL, 1540a44ef6dSjacobs *entry, 1550a44ef6dSjacobs *s, 1560a44ef6dSjacobs text[BUFSIZ]; 1570a44ef6dSjacobs int count = 0, 1580a44ef6dSjacobs copies_set = 0, 1590a44ef6dSjacobs copies = 0; 160355b4669Sjacobs 1610a44ef6dSjacobs for (entry = strtok(cf, "\n"); entry != NULL; 1620a44ef6dSjacobs entry = strtok(NULL, "\n")) { 1630a44ef6dSjacobs char *format = NULL; 1640a44ef6dSjacobs 1650a44ef6dSjacobs /* count the copies */ 1660a44ef6dSjacobs if ((entry[0] >= 'a') && (entry[0] <= 'z') && 1670a44ef6dSjacobs (copies_set == 0) && (previous == entry[0])) 1680a44ef6dSjacobs copies++; 1690a44ef6dSjacobs else if ((previous >= 'a') && (previous <= 'z')) 1700a44ef6dSjacobs copies_set = 1; 1710a44ef6dSjacobs previous = entry[0]; 1720a44ef6dSjacobs 1730a44ef6dSjacobs /* process the control message */ 1740a44ef6dSjacobs switch (entry[0]) { 1750a44ef6dSjacobs /* RFC-1179 options */ 1760a44ef6dSjacobs case 'J': /* RFC-1179 Banner Job Name */ 1770a44ef6dSjacobs papiAttributeListAddString(&list, PAPI_ATTR_EXCL, 1780a44ef6dSjacobs "job-name", ++entry); 1790a44ef6dSjacobs break; 1800a44ef6dSjacobs case 'C': /* RFC-1179 Banner Class Name */ 1810a44ef6dSjacobs papiAttributeListAddString(&list, PAPI_ATTR_EXCL, 1820a44ef6dSjacobs "rfc-1179-class", ++entry); 1830a44ef6dSjacobs break; 1840a44ef6dSjacobs case 'L': /* RFC-1179 Banner toggle */ 1850a44ef6dSjacobs papiAttributeListAddString(&list, PAPI_ATTR_EXCL, 1860a44ef6dSjacobs "job-sheets", "standard"); 1870a44ef6dSjacobs break; 1880a44ef6dSjacobs case 'T': /* RFC-1179 Title (pr) */ 1890a44ef6dSjacobs papiAttributeListAddString(&list, PAPI_ATTR_EXCL, 1900a44ef6dSjacobs "pr-title", ++entry); 1910a44ef6dSjacobs break; 1920a44ef6dSjacobs case 'H': /* RFC-1179 Host */ 1930a44ef6dSjacobs /* 1940a44ef6dSjacobs * use the host as known by us, not by them 1950a44ef6dSjacobs * 1960a44ef6dSjacobs * papiAttributeListAddString(&list, PAPI_ATTR_EXCL, 1970a44ef6dSjacobs * "job-originating-host-name", ++entry); 1980a44ef6dSjacobs */ 1990a44ef6dSjacobs break; 2000a44ef6dSjacobs case 'P': /* RFC-1179 User */ 2010a44ef6dSjacobs ++entry; 2020a44ef6dSjacobs papiAttributeListAddString(&list, PAPI_ATTR_EXCL, 2030a44ef6dSjacobs "requesting-user-name", entry); 2040a44ef6dSjacobs papiServiceSetUserName(svc, entry); 2050a44ef6dSjacobs break; 2060a44ef6dSjacobs case 'M': /* RFC-1179 Mail to User */ 2070a44ef6dSjacobs papiAttributeListAddBoolean(&list, PAPI_ATTR_EXCL, 2080a44ef6dSjacobs "rfc-1179-mail", 1); 2090a44ef6dSjacobs break; 2100a44ef6dSjacobs case 'W': /* RFC-1179 Width (pr) */ 2110a44ef6dSjacobs papiAttributeListAddInteger(&list, PAPI_ATTR_EXCL, 2120a44ef6dSjacobs "pr-width", atoi(++entry)); 2130a44ef6dSjacobs break; 2140a44ef6dSjacobs case 'I': /* RFC-1179 Indent (pr) */ 2150a44ef6dSjacobs papiAttributeListAddInteger(&list, PAPI_ATTR_EXCL, 2160a44ef6dSjacobs "pr-indent", atoi(++entry)); 2170a44ef6dSjacobs break; 2180a44ef6dSjacobs case 'N': /* RFC-1179 Filename */ 2190a44ef6dSjacobs /* could have HPUX extension embedded */ 2200a44ef6dSjacobs if (entry[1] != ' ') { /* real pathname */ 2210a44ef6dSjacobs #ifdef DEBUG 2220a44ef6dSjacobs papiAttributeListAddString(&list, 2230a44ef6dSjacobs PAPI_ATTR_EXCL, 2240a44ef6dSjacobs "flist", ++entry); 2250a44ef6dSjacobs #endif 2260a44ef6dSjacobs } else if (entry[2] == 'O') /* HPUX lp -o options */ 2270a44ef6dSjacobs papiAttributeListFromString(&list, 2280a44ef6dSjacobs PAPI_ATTR_APPEND, ++entry); 2290a44ef6dSjacobs break; 2300a44ef6dSjacobs case 'U': /* RFC-1179 Unlink */ 2310a44ef6dSjacobs break; /* ignored */ 2320a44ef6dSjacobs case '1': /* RFC-1179 TROFF Font R */ 2330a44ef6dSjacobs papiAttributeListAddString(&list, PAPI_ATTR_EXCL, 2340a44ef6dSjacobs "rfc-1179-font-r", ++entry); 2350a44ef6dSjacobs break; 2360a44ef6dSjacobs case '2': /* RFC-1179 TROFF Font I */ 2370a44ef6dSjacobs papiAttributeListAddString(&list, PAPI_ATTR_EXCL, 2380a44ef6dSjacobs "rfc-1179-font-i", ++entry); 2390a44ef6dSjacobs break; 2400a44ef6dSjacobs case '3': /* RFC-1179 TROFF Font B */ 2410a44ef6dSjacobs papiAttributeListAddString(&list, PAPI_ATTR_EXCL, 2420a44ef6dSjacobs "rfc-1179-font-b", ++entry); 2430a44ef6dSjacobs break; 2440a44ef6dSjacobs case '4': /* RFC-1179 TROFF Font S */ 2450a44ef6dSjacobs papiAttributeListAddString(&list, PAPI_ATTR_EXCL, 2460a44ef6dSjacobs "rfc-1179-font-s", ++entry); 2470a44ef6dSjacobs break; 2480a44ef6dSjacobs case 'f': /* RFC-1179 ASCII file (print) */ 2490a44ef6dSjacobs format = "text/plain"; 2500a44ef6dSjacobs if (is_postscript(files[0]) == 1) 2510a44ef6dSjacobs format = "application/postscript"; 2520a44ef6dSjacobs break; 2530a44ef6dSjacobs case 'l': /* RFC-1179 CATV file (print) */ 2540a44ef6dSjacobs format = "application/octet-stream"; 2550a44ef6dSjacobs if (is_postscript(files[0]) == 1) 2560a44ef6dSjacobs format = "application/postscript"; 2570a44ef6dSjacobs break; 2580a44ef6dSjacobs case 'o': /* RFC-1179 Postscript file (print) */ 2590a44ef6dSjacobs format = "application/postscript"; 2600a44ef6dSjacobs break; 2610a44ef6dSjacobs case 'p': /* RFC-1179 PR file (print) */ 2620a44ef6dSjacobs format = "application/x-pr"; 2630a44ef6dSjacobs papiAttributeListAddBoolean(&list, PAPI_ATTR_EXCL, 2640a44ef6dSjacobs "pr-filter", 1); 2650a44ef6dSjacobs break; 2660a44ef6dSjacobs case 't': /* RFC-1179 TROFF file (print) */ 2670a44ef6dSjacobs format = "application/x-troff"; 2680a44ef6dSjacobs break; 2690a44ef6dSjacobs case 'n': /* RFC-1179 DITROFF file (print) */ 2700a44ef6dSjacobs format = "application/x-ditroff"; 2710a44ef6dSjacobs break; 2720a44ef6dSjacobs case 'd': /* RFC-1179 DVI file (print) */ 2730a44ef6dSjacobs format = "application/x-dvi"; 2740a44ef6dSjacobs break; 2750a44ef6dSjacobs case 'g': /* RFC-1179 GRAPH file (print) */ 2760a44ef6dSjacobs format = "application/x-plot"; 2770a44ef6dSjacobs break; 2780a44ef6dSjacobs case 'c': /* RFC-1179 CIF file (print) */ 2790a44ef6dSjacobs format = "application/x-cif"; 2800a44ef6dSjacobs break; 2810a44ef6dSjacobs case 'v': /* RFC-1179 RASTER file (print) */ 2820a44ef6dSjacobs format = "application/x-raster"; 2830a44ef6dSjacobs break; 2840a44ef6dSjacobs case 'r': /* RFC-1179 FORTRAN file (print) */ 2850a44ef6dSjacobs format = "application/x-fortran"; 2860a44ef6dSjacobs break; 2870a44ef6dSjacobs /* Sun Solaris Extensions */ 2880a44ef6dSjacobs case 'O': 2890a44ef6dSjacobs ++entry; 2900a44ef6dSjacobs do { 2910a44ef6dSjacobs if (*entry != '"') 2920a44ef6dSjacobs text[count++] = *entry; 2930a44ef6dSjacobs } while (*entry++); 2940a44ef6dSjacobs papiAttributeListFromString(&list, PAPI_ATTR_APPEND, 2950a44ef6dSjacobs text); 2960a44ef6dSjacobs break; 2970a44ef6dSjacobs case '5': 2980a44ef6dSjacobs ++entry; 2990a44ef6dSjacobs switch (entry[0]) { 3000a44ef6dSjacobs case 'f': /* Solaris form */ 3010a44ef6dSjacobs papiAttributeListAddString(&list, 3020a44ef6dSjacobs PAPI_ATTR_EXCL, 3030a44ef6dSjacobs "form", ++entry); 3040a44ef6dSjacobs break; 3050a44ef6dSjacobs case 'H': /* Solaris handling */ 3060a44ef6dSjacobs ++entry; 3070a44ef6dSjacobs if (strcasecmp(entry, "hold") == 0) 3080a44ef6dSjacobs papiAttributeListAddString(&list, 3090a44ef6dSjacobs PAPI_ATTR_EXCL, 3100a44ef6dSjacobs "job-hold-until", "indefinite"); 311*43b9c050Sjacobs else if (strcasecmp(entry, "immediate") == 0) 3120a44ef6dSjacobs papiAttributeListAddString(&list, 3130a44ef6dSjacobs PAPI_ATTR_EXCL, 3140a44ef6dSjacobs "job-hold-until", "no-hold"); 3150a44ef6dSjacobs else 3160a44ef6dSjacobs papiAttributeListAddString(&list, 3170a44ef6dSjacobs PAPI_ATTR_EXCL, 3180a44ef6dSjacobs "job-hold-until", entry); 3190a44ef6dSjacobs break; 3200a44ef6dSjacobs case 'p': /* Solaris notification */ 3210a44ef6dSjacobs papiAttributeListAddBoolean(&list, 3220a44ef6dSjacobs PAPI_ATTR_EXCL, "rfc-1179-mail", 1); 3230a44ef6dSjacobs break; 324*43b9c050Sjacobs case 'P': { /* Solaris page list */ 325*43b9c050Sjacobs char buf[BUFSIZ]; 326*43b9c050Sjacobs 327*43b9c050Sjacobs snprintf(buf, sizeof (buf), "page-ranges=%s", 328*43b9c050Sjacobs ++entry); 329*43b9c050Sjacobs papiAttributeListFromString(&list, 330*43b9c050Sjacobs PAPI_ATTR_EXCL, buf); 331*43b9c050Sjacobs } 3320a44ef6dSjacobs break; 3330a44ef6dSjacobs case 'q': { /* Solaris priority */ 3340a44ef6dSjacobs int i = atoi(optarg); 3350a44ef6dSjacobs 336*43b9c050Sjacobs i = 100 - (i * 2.5); 3370a44ef6dSjacobs if ((i < 1) || (i > 100)) 3380a44ef6dSjacobs i = 50; 3390a44ef6dSjacobs papiAttributeListAddInteger(&list, 340*43b9c050Sjacobs PAPI_ATTR_EXCL, "job-priority", i); 3410a44ef6dSjacobs } 3420a44ef6dSjacobs break; 3430a44ef6dSjacobs case 'S': /* Solaris character set */ 3440a44ef6dSjacobs papiAttributeListAddString(&list, 3450a44ef6dSjacobs PAPI_ATTR_EXCL, "lp-charset", 3460a44ef6dSjacobs ++entry); 3470a44ef6dSjacobs break; 3480a44ef6dSjacobs case 'T': /* Solaris type */ 3490a44ef6dSjacobs format = lp_type_to_mime_type(++entry); 3500a44ef6dSjacobs break; 3510a44ef6dSjacobs case 'y': /* Solaris mode */ 3520a44ef6dSjacobs papiAttributeListAddString(&list, 3530a44ef6dSjacobs PAPI_ATTR_APPEND, "lp-modes", ++entry); 3540a44ef6dSjacobs break; 3550a44ef6dSjacobs default: 3560a44ef6dSjacobs syslog(LOG_INFO|LOG_DEBUG, 3570a44ef6dSjacobs "Warning: cf message (%s) ignored", 3580a44ef6dSjacobs entry); 3590a44ef6dSjacobs break; 3600a44ef6dSjacobs } 3610a44ef6dSjacobs break; 3620a44ef6dSjacobs /* Undefined Extensions: SCO, Ultrix, AIX, ... */ 3630a44ef6dSjacobs 3640a44ef6dSjacobs default: 3650a44ef6dSjacobs syslog(LOG_INFO|LOG_DEBUG, 3660a44ef6dSjacobs "Warning: cf message (%s) ignored", entry); 3670a44ef6dSjacobs break; 3680a44ef6dSjacobs } 3690a44ef6dSjacobs 3700a44ef6dSjacobs if (format != NULL) 3710a44ef6dSjacobs papiAttributeListAddString(&list, PAPI_ATTR_EXCL, 3720a44ef6dSjacobs "document-format", format); 3730a44ef6dSjacobs } 3740a44ef6dSjacobs 3750a44ef6dSjacobs papiAttributeListAddInteger(&list, PAPI_ATTR_EXCL, 3760a44ef6dSjacobs "copies", ++copies); 3770a44ef6dSjacobs papiAttributeListAddString(&list, PAPI_ATTR_EXCL, 3780a44ef6dSjacobs "job-sheets", "none"); 3790a44ef6dSjacobs 3800a44ef6dSjacobs return (list); 3810a44ef6dSjacobs } 3820a44ef6dSjacobs 3830a44ef6dSjacobs static papi_status_t 3840a44ef6dSjacobs submit_job(papi_service_t svc, FILE *ifp, char *printer, char *cf, char **files) 3850a44ef6dSjacobs { 3860a44ef6dSjacobs papi_attribute_t **list = NULL; 3870a44ef6dSjacobs papi_status_t status; 3880a44ef6dSjacobs papi_job_t job = NULL; 3890a44ef6dSjacobs char *format = ""; 3900a44ef6dSjacobs 3910a44ef6dSjacobs if ((list = parse_cf(svc, cf, files)) != NULL) { 3920a44ef6dSjacobs /* use the host as known by us, not by them */ 3930a44ef6dSjacobs char *host = remote_host_name(ifp); 3940a44ef6dSjacobs 3950a44ef6dSjacobs if (host != NULL) { 3960a44ef6dSjacobs papiAttributeListAddString(&list, PAPI_ATTR_REPLACE, 3970a44ef6dSjacobs "job-originating-host-name", host); 3980a44ef6dSjacobs free(host); 3990a44ef6dSjacobs } 4000a44ef6dSjacobs } 4010a44ef6dSjacobs 4020a44ef6dSjacobs status = papiJobSubmit(svc, printer, list, NULL, files, &job); 4030a44ef6dSjacobs syslog(LOG_DEBUG, "submit: %s", papiStatusString(status)); 4040a44ef6dSjacobs if (status != PAPI_OK) { 4050a44ef6dSjacobs char *tmp = papiServiceGetStatusMessage(svc); 4060a44ef6dSjacobs 4070a44ef6dSjacobs syslog(LOG_DEBUG, "submit-detail: %s", tmp ? tmp : "none"); 4080a44ef6dSjacobs } 4090a44ef6dSjacobs papiJobFree(job); 4100a44ef6dSjacobs 4110a44ef6dSjacobs return (status); 4120a44ef6dSjacobs } 4130a44ef6dSjacobs 4140a44ef6dSjacobs static char * 4150a44ef6dSjacobs receive_control_file(papi_service_t svc, FILE *ifp, FILE *ofp, int size) 4160a44ef6dSjacobs { 4170a44ef6dSjacobs char *ptr, *cf_data; 4180a44ef6dSjacobs 4190a44ef6dSjacobs if ((ptr = cf_data = calloc(1, size + 1)) == NULL) { 4200a44ef6dSjacobs NACK(ofp); 4210a44ef6dSjacobs return (NULL); 4220a44ef6dSjacobs } else 423355b4669Sjacobs ACK(ofp); 424355b4669Sjacobs 4250a44ef6dSjacobs while (size > 0) { 4260a44ef6dSjacobs int rc; 427355b4669Sjacobs 4280a44ef6dSjacobs if (((rc = fread(ptr, 1, size, ifp)) == 0) && 4290a44ef6dSjacobs (feof(ifp) != 0)) { 4300a44ef6dSjacobs free(cf_data); 4310a44ef6dSjacobs return (NULL); 4320a44ef6dSjacobs } else { 4330a44ef6dSjacobs ptr += rc; 4340a44ef6dSjacobs size -= rc; 4350a44ef6dSjacobs } 4360a44ef6dSjacobs } 4370a44ef6dSjacobs syslog(LOG_DEBUG, "cf_data(%s)", cf_data); 4380a44ef6dSjacobs 4390a44ef6dSjacobs if (fgetc(ifp) != 0) { 4400a44ef6dSjacobs free(cf_data); 4410a44ef6dSjacobs return (NULL); 4420a44ef6dSjacobs } 4430a44ef6dSjacobs ACK(ofp); 4440a44ef6dSjacobs 4450a44ef6dSjacobs return (cf_data); 4460a44ef6dSjacobs } 4470a44ef6dSjacobs 4480a44ef6dSjacobs static char * 4490a44ef6dSjacobs receive_data_file(FILE *ifp, FILE *ofp, int size) 4500a44ef6dSjacobs { 451355b4669Sjacobs char file[] = "lpdXXXXXX"; 4520a44ef6dSjacobs char buf[BUFSIZ]; 453355b4669Sjacobs int fd; 454355b4669Sjacobs 4550a44ef6dSjacobs if ((fd = mkstemp(file)) < 0) { 4560a44ef6dSjacobs NACK(ofp); 4570a44ef6dSjacobs return (NULL); 4580a44ef6dSjacobs } else 4590a44ef6dSjacobs ACK(ofp); 460355b4669Sjacobs 4610a44ef6dSjacobs while (size > 0) { 4620a44ef6dSjacobs int rc = ((size > BUFSIZ) ? BUFSIZ : size); 4630a44ef6dSjacobs 4640a44ef6dSjacobs if (((rc = fread(buf, 1, rc, ifp)) == 0) && 4650a44ef6dSjacobs (feof(ifp) != 0)) { 4660a44ef6dSjacobs close(fd); 4670a44ef6dSjacobs unlink(file); 4680a44ef6dSjacobs return (NULL); 4690a44ef6dSjacobs } else { 4700a44ef6dSjacobs char *ptr = buf; 4710a44ef6dSjacobs 4720a44ef6dSjacobs while (rc > 0) { 4730a44ef6dSjacobs int wrc = write(fd, ptr, rc); 4740a44ef6dSjacobs 4750a44ef6dSjacobs if (wrc < 0) { 4760a44ef6dSjacobs close(fd); 4770a44ef6dSjacobs unlink(file); 4780a44ef6dSjacobs return (NULL); 4790a44ef6dSjacobs } 4800a44ef6dSjacobs 4810a44ef6dSjacobs ptr += wrc; 4820a44ef6dSjacobs size -= wrc; 4830a44ef6dSjacobs rc -= wrc; 4840a44ef6dSjacobs } 4850a44ef6dSjacobs } 4860a44ef6dSjacobs } 4870a44ef6dSjacobs close(fd); 4880a44ef6dSjacobs if (fgetc(ifp) != 0) { 4890a44ef6dSjacobs unlink(file); 4900a44ef6dSjacobs return (NULL); 4910a44ef6dSjacobs } 4920a44ef6dSjacobs ACK(ofp); 4930a44ef6dSjacobs 4940a44ef6dSjacobs return (strdup(file)); 4950a44ef6dSjacobs } 4960a44ef6dSjacobs 4970a44ef6dSjacobs static papi_status_t 4980a44ef6dSjacobs berkeley_receive_files(papi_service_t svc, FILE *ifp, FILE *ofp, char *printer) 4990a44ef6dSjacobs { 5000a44ef6dSjacobs papi_status_t status = PAPI_OK; 5010a44ef6dSjacobs char *file, **files = NULL; /* the job data files */ 5020a44ef6dSjacobs char *cf = NULL; 5030a44ef6dSjacobs char buf[BUFSIZ]; 5040a44ef6dSjacobs 5050a44ef6dSjacobs while (fgets(buf, sizeof (buf), ifp) != NULL) { 5060a44ef6dSjacobs int size; 5070a44ef6dSjacobs 5080a44ef6dSjacobs syslog(LOG_DEBUG, "XFER CMD: (%d)%s\n", buf[0], &buf[1]); 5090a44ef6dSjacobs #ifdef DEBUG /* translate [1-3]... messages to \[1-3] to run by hand */ 5100a44ef6dSjacobs if ((buf[0] > '0') && (buf[0] < '4')) 5110a44ef6dSjacobs buf[0] -= '0'; 5120a44ef6dSjacobs #endif 5130a44ef6dSjacobs switch (buf[0]) { 5140a44ef6dSjacobs case 0x01: /* Abort */ 5150a44ef6dSjacobs cleanup(&files, &cf); 5160a44ef6dSjacobs break; 5170a44ef6dSjacobs case 0x02: { /* Receive control file */ 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) { 5230a44ef6dSjacobs status = submit_job(svc, ifp, printer, 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)) 5460a44ef6dSjacobs status = submit_job(svc, ifp, printer, 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 char buf[BUFSIZ]; 5870a44ef6dSjacobs uri_t *uri = NULL; 5880a44ef6dSjacobs char *s = NULL; 5890a44ef6dSjacobs 5900a44ef6dSjacobs /* was there a printer? */ 5910a44ef6dSjacobs if (svc_name == NULL) 5920a44ef6dSjacobs return (0); 5930a44ef6dSjacobs 5940a44ef6dSjacobs if ((list = getprinterbyname(svc_name, NULL)) == NULL) 5950a44ef6dSjacobs return (0); /* if it doesnt' resolve, we will fail later */ 5960a44ef6dSjacobs 5970a44ef6dSjacobs papiAttributeListGetString(list, NULL, "printer-uri-supported", &s); 5980a44ef6dSjacobs if ((s == NULL) || (strcasecmp(svc_name, s) != 0)) 5990a44ef6dSjacobs return (0); /* they don't match */ 6000a44ef6dSjacobs 6010a44ef6dSjacobs /* is it in uri form? */ 6020a44ef6dSjacobs if (uri_from_string(s, &uri) < 0) 6030a44ef6dSjacobs return (0); 6040a44ef6dSjacobs 6050a44ef6dSjacobs if ((uri == NULL) || (uri->scheme == NULL) || (uri->host == NULL)) { 6060a44ef6dSjacobs uri_free(uri); 6070a44ef6dSjacobs return (0); 6080a44ef6dSjacobs } 6090a44ef6dSjacobs 6100a44ef6dSjacobs /* is it in lpd form? */ 6110a44ef6dSjacobs if (strcasecmp(uri->scheme, "lpd") != 0) { 6120a44ef6dSjacobs uri_free(uri); 6130a44ef6dSjacobs return (0); 6140a44ef6dSjacobs } 6150a44ef6dSjacobs 6160a44ef6dSjacobs /* is it the local host? */ 6170a44ef6dSjacobs sysinfo(SI_HOSTNAME, buf, sizeof (buf)); 6180a44ef6dSjacobs if ((strcasecmp(uri->host, "localhost") != 0) && 6190a44ef6dSjacobs (strcasecmp(uri->host, buf) != 0)) { 6200a44ef6dSjacobs uri_free(uri); 6210a44ef6dSjacobs return (0); 6220a44ef6dSjacobs } 6230a44ef6dSjacobs 6240a44ef6dSjacobs uri_free(uri); 6250a44ef6dSjacobs return (1); 6260a44ef6dSjacobs } 6270a44ef6dSjacobs 6280a44ef6dSjacobs 629355b4669Sjacobs /* 630355b4669Sjacobs * This is the entry point for this program. The program takes the 631355b4669Sjacobs * following options: 632355b4669Sjacobs * (none) 633355b4669Sjacobs */ 634355b4669Sjacobs int 635355b4669Sjacobs main(int ac, char *av[]) 636355b4669Sjacobs { 637355b4669Sjacobs papi_status_t status; 638355b4669Sjacobs papi_service_t svc = NULL; 639355b4669Sjacobs papi_encryption_t encryption = PAPI_ENCRYPT_NEVER; 640355b4669Sjacobs FILE *ifp = stdin, 641355b4669Sjacobs *ofp = stdout; 642355b4669Sjacobs int c; 643355b4669Sjacobs char buf[BUFSIZ], 644355b4669Sjacobs **args, 6450a44ef6dSjacobs *printer, 6460a44ef6dSjacobs *run_dir = "/var/run/in.lpd", 6470a44ef6dSjacobs *run_user = NULL; 6480a44ef6dSjacobs struct passwd *pw = NULL; 649355b4669Sjacobs 6500a44ef6dSjacobs (void) chdir("/tmp"); /* run in /tmp by default */ 651355b4669Sjacobs openlog("bsd-gw", LOG_PID, LOG_LPR); 652355b4669Sjacobs 6530a44ef6dSjacobs while ((c = getopt(ac, av, "Ed:u:")) != EOF) 654355b4669Sjacobs switch (c) { 655355b4669Sjacobs case 'E': 656355b4669Sjacobs encryption = PAPI_ENCRYPT_ALWAYS; 657355b4669Sjacobs break; 6580a44ef6dSjacobs case 'd': /* run where they tell you */ 6590a44ef6dSjacobs run_dir = optarg; 6600a44ef6dSjacobs break; 6610a44ef6dSjacobs case 'u': /* run as */ 6620a44ef6dSjacobs run_user = optarg; 6630a44ef6dSjacobs break; 664355b4669Sjacobs default: 665355b4669Sjacobs ; 666355b4669Sjacobs } 667355b4669Sjacobs 6680a44ef6dSjacobs if (run_user != NULL) /* get the requested user info */ 6690a44ef6dSjacobs pw = getpwnam(run_user); 6700a44ef6dSjacobs 6710a44ef6dSjacobs if (run_dir != NULL) { /* setup the run_dir */ 6720a44ef6dSjacobs (void) mkdir(run_dir, 0700); 6730a44ef6dSjacobs if (pw != NULL) 6740a44ef6dSjacobs (void) chown(run_dir, pw->pw_uid, pw->pw_gid); 6750a44ef6dSjacobs } 6760a44ef6dSjacobs 6770a44ef6dSjacobs if (pw != NULL) { /* run as the requested user */ 6780a44ef6dSjacobs syslog(LOG_DEBUG, "name: %s, uid: %d, gid: %d", 6790a44ef6dSjacobs pw->pw_name, pw->pw_uid, pw->pw_gid); 6800a44ef6dSjacobs initgroups(pw->pw_name, pw->pw_gid); 6810a44ef6dSjacobs setgid(pw->pw_gid); 6820a44ef6dSjacobs setuid(pw->pw_uid); 6830a44ef6dSjacobs } 6840a44ef6dSjacobs 6850a44ef6dSjacobs if (run_dir != NULL) /* move to the run_dir */ 6860a44ef6dSjacobs if (chdir(run_dir) < 0) { 6870a44ef6dSjacobs syslog(LOG_DEBUG, "failed to chdir(%s)", run_dir); 6880a44ef6dSjacobs exit(1); 6890a44ef6dSjacobs } 6900a44ef6dSjacobs 6910a44ef6dSjacobs syslog(LOG_DEBUG, "$CWD = %s", getwd(NULL)); 6920a44ef6dSjacobs 693355b4669Sjacobs if (fgets(buf, sizeof (buf), ifp) == NULL) { 694355b4669Sjacobs if (feof(ifp) == 0) 695355b4669Sjacobs syslog(LOG_ERR, "Error reading from connection: %s", 696355b4669Sjacobs strerror(errno)); 697355b4669Sjacobs exit(1); 698355b4669Sjacobs } 699355b4669Sjacobs 7000a44ef6dSjacobs syslog(LOG_DEBUG, "CMD: (%d)%s\n", buf[0], &buf[1]); 7010a44ef6dSjacobs 7020a44ef6dSjacobs #ifdef DEBUG /* translate [1-5]... messages to \[1-5] to run by hand */ 7030a44ef6dSjacobs if ((buf[0] > '0') && (buf[0] < '6')) 7040a44ef6dSjacobs buf[0] -= '0'; 7050a44ef6dSjacobs #endif 7060a44ef6dSjacobs 707355b4669Sjacobs if ((buf[0] < 1) || (buf[0] > 5)) { 708355b4669Sjacobs fatal(ofp, "Invalid protocol request (%d): %c%s\n", 709355b4669Sjacobs buf[0], buf[0], buf); 710355b4669Sjacobs exit(1); 711355b4669Sjacobs } 712355b4669Sjacobs 713355b4669Sjacobs args = strsplit(&buf[1], "\t\n "); 714355b4669Sjacobs printer = *args++; 715355b4669Sjacobs 716355b4669Sjacobs if (printer == NULL) { 717355b4669Sjacobs fatal(ofp, "Can't determine requested printer"); 718355b4669Sjacobs exit(1); 719355b4669Sjacobs } 720355b4669Sjacobs 7210a44ef6dSjacobs if (cyclical_service_check(printer) != 0) { 7220a44ef6dSjacobs fatal(ofp, "%s is cyclical\n", printer); 7230a44ef6dSjacobs exit(1); 7240a44ef6dSjacobs } 7250a44ef6dSjacobs 726355b4669Sjacobs status = papiServiceCreate(&svc, printer, NULL, NULL, NULL, 727355b4669Sjacobs encryption, NULL); 728355b4669Sjacobs if (status != PAPI_OK) { 729355b4669Sjacobs fatal(ofp, "Failed to contact service for %s: %s\n", printer, 730355b4669Sjacobs verbose_papi_message(svc, status)); 731355b4669Sjacobs exit(1); 732355b4669Sjacobs } 733355b4669Sjacobs 7340a44ef6dSjacobs /* 7350a44ef6dSjacobs * Trusted Solaris can't be trusting of intermediaries. Pass 7360a44ef6dSjacobs * the socket connection to the print service to retrieve the 7370a44ef6dSjacobs * sensativity label off of a multi-level port. 7380a44ef6dSjacobs */ 7390a44ef6dSjacobs (void) papiServiceSetPeer(svc, fileno(ifp)); 740355b4669Sjacobs 741355b4669Sjacobs switch (buf[0]) { 742355b4669Sjacobs case '\1': /* restart printer */ 743355b4669Sjacobs ACK(ofp); /* there is no equivalent */ 744355b4669Sjacobs break; 745355b4669Sjacobs case '\2': /* transfer job(s) */ 7460a44ef6dSjacobs status = berkeley_transfer_files(svc, ifp, ofp, printer); 747355b4669Sjacobs break; 748355b4669Sjacobs case '\3': /* show queue (short) */ 749355b4669Sjacobs case '\4': { /* show queue (long) */ 750355b4669Sjacobs int count; 751355b4669Sjacobs 752355b4669Sjacobs for (count = 0; args[count] != 0; count++); 753355b4669Sjacobs 754355b4669Sjacobs berkeley_queue_report(svc, ofp, printer, buf[0], count, args); 755355b4669Sjacobs } 756355b4669Sjacobs break; 757355b4669Sjacobs case '\5': { /* cancel job(s) */ 7580a44ef6dSjacobs char *user = *args++; 7590a44ef6dSjacobs char *host = remote_host_name(ifp); 760355b4669Sjacobs int count; 761355b4669Sjacobs 7620a44ef6dSjacobs if (host != NULL) { 7630a44ef6dSjacobs char buf[BUFSIZ]; 7640a44ef6dSjacobs 7650a44ef6dSjacobs snprintf(buf, sizeof (buf), "%s@%s", user, host); 7660a44ef6dSjacobs status = papiServiceSetUserName(svc, buf); 7670a44ef6dSjacobs } else 7680a44ef6dSjacobs status = papiServiceSetUserName(svc, user); 7690a44ef6dSjacobs 770355b4669Sjacobs for (count = 0; args[count] != 0; count++); 771355b4669Sjacobs 772355b4669Sjacobs berkeley_cancel_request(svc, ofp, printer, count, args); 773355b4669Sjacobs } 774355b4669Sjacobs break; 775355b4669Sjacobs default: 776355b4669Sjacobs fatal(ofp, "unsupported protocol request (%c), %s", 777355b4669Sjacobs buf[0], &buf[1]); 778355b4669Sjacobs } 779355b4669Sjacobs 780355b4669Sjacobs (void) fflush(ofp); 781355b4669Sjacobs 782355b4669Sjacobs syslog(LOG_DEBUG, "protocol request(%d) for %s completed: %s", 783355b4669Sjacobs buf[0], printer, papiStatusString(status)); 7840a44ef6dSjacobs if (status != PAPI_OK) 7850a44ef6dSjacobs syslog(LOG_DEBUG, "detail: %s", 7860a44ef6dSjacobs verbose_papi_message(svc, status)); 787355b4669Sjacobs 788355b4669Sjacobs papiServiceDestroy(svc); 789355b4669Sjacobs 790355b4669Sjacobs return (0); 791355b4669Sjacobs } 792