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> 38*36615d24Sjacobs #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; 720a44ef6dSjacobs char myname[MAXHOSTNAMELEN], 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 /* is it "localhost" ? */ 920a44ef6dSjacobs if (strcasecmp(hp->h_name, "localhost") == 0) 930a44ef6dSjacobs return (strdup("localhost")); 940a44ef6dSjacobs 950a44ef6dSjacobs /* duplicate the name because gethostbyXXXX() is not reentrant */ 960a44ef6dSjacobs hostname = strdup(hp->h_name); 970a44ef6dSjacobs (void) sysinfo(SI_HOSTNAME, myname, sizeof (myname)); 980a44ef6dSjacobs 990a44ef6dSjacobs /* is it from one of my addresses ? */ 1000a44ef6dSjacobs if ((hp = getipnodebyname(myname, AF_INET6, AI_ALL|AI_V4MAPPED, 1010a44ef6dSjacobs &error_num)) != NULL) { 1020a44ef6dSjacobs struct in6_addr **tmp = (struct in6_addr **)hp->h_addr_list; 1030a44ef6dSjacobs int i = 0; 1040a44ef6dSjacobs 1050a44ef6dSjacobs while (tmp[i] != NULL) { 1060a44ef6dSjacobs if (memcmp(tmp[i++], &peer.sin6_addr, hp->h_length) 1070a44ef6dSjacobs == 0) { 1080a44ef6dSjacobs free(hostname); 1090a44ef6dSjacobs return (strdup("localhost")); 1100a44ef6dSjacobs } 1110a44ef6dSjacobs } 1120a44ef6dSjacobs } 1130a44ef6dSjacobs 1140a44ef6dSjacobs /* It must be someone else */ 1150a44ef6dSjacobs return (hostname); 1160a44ef6dSjacobs } 1170a44ef6dSjacobs 1180a44ef6dSjacobs static void 119355b4669Sjacobs fatal(FILE *fp, char *fmt, ...) 120355b4669Sjacobs { 121355b4669Sjacobs va_list ap; 122355b4669Sjacobs 123355b4669Sjacobs va_start(ap, fmt); 124355b4669Sjacobs vsyslog(LOG_DEBUG, fmt, ap); 125355b4669Sjacobs vfprintf(fp, fmt, ap); 126355b4669Sjacobs va_end(ap); 1270a44ef6dSjacobs exit(1); 128355b4669Sjacobs } 129355b4669Sjacobs 130355b4669Sjacobs static void 1310a44ef6dSjacobs cleanup(char ***files, char **cf) 132355b4669Sjacobs { 1330a44ef6dSjacobs if (*files != NULL) { 134355b4669Sjacobs int i; 135355b4669Sjacobs 1360a44ef6dSjacobs for (i = 0; (*files)[i] != NULL; i++) { 1370a44ef6dSjacobs (void) unlink((*files)[i]); 1380a44ef6dSjacobs free((*files)[i]); 1390a44ef6dSjacobs } 1400a44ef6dSjacobs free(*files); 1410a44ef6dSjacobs *files = NULL; 1420a44ef6dSjacobs } 1430a44ef6dSjacobs 1440a44ef6dSjacobs if (*cf != NULL) { 1450a44ef6dSjacobs free(*cf); 1460a44ef6dSjacobs *cf = NULL; 147355b4669Sjacobs } 148355b4669Sjacobs } 149355b4669Sjacobs 1500a44ef6dSjacobs static papi_attribute_t ** 1510a44ef6dSjacobs parse_cf(papi_service_t svc, char *cf, char **files) 152355b4669Sjacobs { 1530a44ef6dSjacobs papi_attribute_t **list = NULL; 1540a44ef6dSjacobs char previous = NULL, 1550a44ef6dSjacobs *entry, 1560a44ef6dSjacobs *s, 1570a44ef6dSjacobs text[BUFSIZ]; 1580a44ef6dSjacobs int count = 0, 1590a44ef6dSjacobs copies_set = 0, 1600a44ef6dSjacobs copies = 0; 161355b4669Sjacobs 1620a44ef6dSjacobs for (entry = strtok(cf, "\n"); entry != NULL; 1630a44ef6dSjacobs entry = strtok(NULL, "\n")) { 1640a44ef6dSjacobs char *format = NULL; 1650a44ef6dSjacobs 1660a44ef6dSjacobs /* count the copies */ 1670a44ef6dSjacobs if ((entry[0] >= 'a') && (entry[0] <= 'z') && 1680a44ef6dSjacobs (copies_set == 0) && (previous == entry[0])) 1690a44ef6dSjacobs copies++; 1700a44ef6dSjacobs else if ((previous >= 'a') && (previous <= 'z')) 1710a44ef6dSjacobs copies_set = 1; 1720a44ef6dSjacobs previous = entry[0]; 1730a44ef6dSjacobs 1740a44ef6dSjacobs /* process the control message */ 1750a44ef6dSjacobs switch (entry[0]) { 1760a44ef6dSjacobs /* RFC-1179 options */ 1770a44ef6dSjacobs case 'J': /* RFC-1179 Banner Job Name */ 1780a44ef6dSjacobs papiAttributeListAddString(&list, PAPI_ATTR_EXCL, 1790a44ef6dSjacobs "job-name", ++entry); 1800a44ef6dSjacobs break; 1810a44ef6dSjacobs case 'C': /* RFC-1179 Banner Class Name */ 1820a44ef6dSjacobs papiAttributeListAddString(&list, PAPI_ATTR_EXCL, 1830a44ef6dSjacobs "rfc-1179-class", ++entry); 1840a44ef6dSjacobs break; 1850a44ef6dSjacobs case 'L': /* RFC-1179 Banner toggle */ 1860a44ef6dSjacobs papiAttributeListAddString(&list, PAPI_ATTR_EXCL, 1870a44ef6dSjacobs "job-sheets", "standard"); 1880a44ef6dSjacobs break; 1890a44ef6dSjacobs case 'T': /* RFC-1179 Title (pr) */ 1900a44ef6dSjacobs papiAttributeListAddString(&list, PAPI_ATTR_EXCL, 1910a44ef6dSjacobs "pr-title", ++entry); 1920a44ef6dSjacobs break; 1930a44ef6dSjacobs case 'H': /* RFC-1179 Host */ 1940a44ef6dSjacobs /* 1950a44ef6dSjacobs * use the host as known by us, not by them 1960a44ef6dSjacobs * 1970a44ef6dSjacobs * papiAttributeListAddString(&list, PAPI_ATTR_EXCL, 1980a44ef6dSjacobs * "job-originating-host-name", ++entry); 1990a44ef6dSjacobs */ 2000a44ef6dSjacobs break; 2010a44ef6dSjacobs case 'P': /* RFC-1179 User */ 2020a44ef6dSjacobs ++entry; 2030a44ef6dSjacobs papiAttributeListAddString(&list, PAPI_ATTR_EXCL, 2040a44ef6dSjacobs "requesting-user-name", entry); 2050a44ef6dSjacobs papiServiceSetUserName(svc, entry); 2060a44ef6dSjacobs break; 2070a44ef6dSjacobs case 'M': /* RFC-1179 Mail to User */ 2080a44ef6dSjacobs papiAttributeListAddBoolean(&list, PAPI_ATTR_EXCL, 2090a44ef6dSjacobs "rfc-1179-mail", 1); 2100a44ef6dSjacobs break; 2110a44ef6dSjacobs case 'W': /* RFC-1179 Width (pr) */ 2120a44ef6dSjacobs papiAttributeListAddInteger(&list, PAPI_ATTR_EXCL, 2130a44ef6dSjacobs "pr-width", atoi(++entry)); 2140a44ef6dSjacobs break; 2150a44ef6dSjacobs case 'I': /* RFC-1179 Indent (pr) */ 2160a44ef6dSjacobs papiAttributeListAddInteger(&list, PAPI_ATTR_EXCL, 2170a44ef6dSjacobs "pr-indent", atoi(++entry)); 2180a44ef6dSjacobs break; 2190a44ef6dSjacobs case 'N': /* RFC-1179 Filename */ 2200a44ef6dSjacobs /* could have HPUX extension embedded */ 2210a44ef6dSjacobs if (entry[1] != ' ') { /* real pathname */ 2220a44ef6dSjacobs #ifdef DEBUG 2230a44ef6dSjacobs papiAttributeListAddString(&list, 2240a44ef6dSjacobs PAPI_ATTR_EXCL, 2250a44ef6dSjacobs "flist", ++entry); 2260a44ef6dSjacobs #endif 2270a44ef6dSjacobs } else if (entry[2] == 'O') /* HPUX lp -o options */ 2280a44ef6dSjacobs papiAttributeListFromString(&list, 2290a44ef6dSjacobs PAPI_ATTR_APPEND, ++entry); 2300a44ef6dSjacobs break; 2310a44ef6dSjacobs case 'U': /* RFC-1179 Unlink */ 2320a44ef6dSjacobs break; /* ignored */ 2330a44ef6dSjacobs case '1': /* RFC-1179 TROFF Font R */ 2340a44ef6dSjacobs papiAttributeListAddString(&list, PAPI_ATTR_EXCL, 2350a44ef6dSjacobs "rfc-1179-font-r", ++entry); 2360a44ef6dSjacobs break; 2370a44ef6dSjacobs case '2': /* RFC-1179 TROFF Font I */ 2380a44ef6dSjacobs papiAttributeListAddString(&list, PAPI_ATTR_EXCL, 2390a44ef6dSjacobs "rfc-1179-font-i", ++entry); 2400a44ef6dSjacobs break; 2410a44ef6dSjacobs case '3': /* RFC-1179 TROFF Font B */ 2420a44ef6dSjacobs papiAttributeListAddString(&list, PAPI_ATTR_EXCL, 2430a44ef6dSjacobs "rfc-1179-font-b", ++entry); 2440a44ef6dSjacobs break; 2450a44ef6dSjacobs case '4': /* RFC-1179 TROFF Font S */ 2460a44ef6dSjacobs papiAttributeListAddString(&list, PAPI_ATTR_EXCL, 2470a44ef6dSjacobs "rfc-1179-font-s", ++entry); 2480a44ef6dSjacobs break; 2490a44ef6dSjacobs case 'f': /* RFC-1179 ASCII file (print) */ 2500a44ef6dSjacobs format = "text/plain"; 2510a44ef6dSjacobs if (is_postscript(files[0]) == 1) 2520a44ef6dSjacobs format = "application/postscript"; 2530a44ef6dSjacobs break; 2540a44ef6dSjacobs case 'l': /* RFC-1179 CATV file (print) */ 2550a44ef6dSjacobs format = "application/octet-stream"; 2560a44ef6dSjacobs if (is_postscript(files[0]) == 1) 2570a44ef6dSjacobs format = "application/postscript"; 2580a44ef6dSjacobs break; 2590a44ef6dSjacobs case 'o': /* RFC-1179 Postscript file (print) */ 2600a44ef6dSjacobs format = "application/postscript"; 2610a44ef6dSjacobs break; 2620a44ef6dSjacobs case 'p': /* RFC-1179 PR file (print) */ 2630a44ef6dSjacobs format = "application/x-pr"; 2640a44ef6dSjacobs papiAttributeListAddBoolean(&list, PAPI_ATTR_EXCL, 2650a44ef6dSjacobs "pr-filter", 1); 2660a44ef6dSjacobs break; 2670a44ef6dSjacobs case 't': /* RFC-1179 TROFF file (print) */ 2680a44ef6dSjacobs format = "application/x-troff"; 2690a44ef6dSjacobs break; 2700a44ef6dSjacobs case 'n': /* RFC-1179 DITROFF file (print) */ 2710a44ef6dSjacobs format = "application/x-ditroff"; 2720a44ef6dSjacobs break; 2730a44ef6dSjacobs case 'd': /* RFC-1179 DVI file (print) */ 2740a44ef6dSjacobs format = "application/x-dvi"; 2750a44ef6dSjacobs break; 2760a44ef6dSjacobs case 'g': /* RFC-1179 GRAPH file (print) */ 2770a44ef6dSjacobs format = "application/x-plot"; 2780a44ef6dSjacobs break; 2790a44ef6dSjacobs case 'c': /* RFC-1179 CIF file (print) */ 2800a44ef6dSjacobs format = "application/x-cif"; 2810a44ef6dSjacobs break; 2820a44ef6dSjacobs case 'v': /* RFC-1179 RASTER file (print) */ 2830a44ef6dSjacobs format = "application/x-raster"; 2840a44ef6dSjacobs break; 2850a44ef6dSjacobs case 'r': /* RFC-1179 FORTRAN file (print) */ 2860a44ef6dSjacobs format = "application/x-fortran"; 2870a44ef6dSjacobs break; 2880a44ef6dSjacobs /* Sun Solaris Extensions */ 2890a44ef6dSjacobs case 'O': 2900a44ef6dSjacobs ++entry; 2910a44ef6dSjacobs do { 2920a44ef6dSjacobs if (*entry != '"') 2930a44ef6dSjacobs text[count++] = *entry; 2940a44ef6dSjacobs } while (*entry++); 2950a44ef6dSjacobs papiAttributeListFromString(&list, PAPI_ATTR_APPEND, 2960a44ef6dSjacobs text); 2970a44ef6dSjacobs break; 2980a44ef6dSjacobs case '5': 2990a44ef6dSjacobs ++entry; 3000a44ef6dSjacobs switch (entry[0]) { 3010a44ef6dSjacobs case 'f': /* Solaris form */ 3020a44ef6dSjacobs papiAttributeListAddString(&list, 3030a44ef6dSjacobs PAPI_ATTR_EXCL, 3040a44ef6dSjacobs "form", ++entry); 3050a44ef6dSjacobs break; 3060a44ef6dSjacobs case 'H': /* Solaris handling */ 3070a44ef6dSjacobs ++entry; 3080a44ef6dSjacobs if (strcasecmp(entry, "hold") == 0) 3090a44ef6dSjacobs papiAttributeListAddString(&list, 3100a44ef6dSjacobs PAPI_ATTR_EXCL, 3110a44ef6dSjacobs "job-hold-until", "indefinite"); 31243b9c050Sjacobs else if (strcasecmp(entry, "immediate") == 0) 3130a44ef6dSjacobs papiAttributeListAddString(&list, 3140a44ef6dSjacobs PAPI_ATTR_EXCL, 3150a44ef6dSjacobs "job-hold-until", "no-hold"); 3160a44ef6dSjacobs else 3170a44ef6dSjacobs papiAttributeListAddString(&list, 3180a44ef6dSjacobs PAPI_ATTR_EXCL, 3190a44ef6dSjacobs "job-hold-until", entry); 3200a44ef6dSjacobs break; 3210a44ef6dSjacobs case 'p': /* Solaris notification */ 3220a44ef6dSjacobs papiAttributeListAddBoolean(&list, 3230a44ef6dSjacobs PAPI_ATTR_EXCL, "rfc-1179-mail", 1); 3240a44ef6dSjacobs break; 32543b9c050Sjacobs case 'P': { /* Solaris page list */ 32643b9c050Sjacobs char buf[BUFSIZ]; 32743b9c050Sjacobs 32843b9c050Sjacobs snprintf(buf, sizeof (buf), "page-ranges=%s", 32943b9c050Sjacobs ++entry); 33043b9c050Sjacobs papiAttributeListFromString(&list, 33143b9c050Sjacobs PAPI_ATTR_EXCL, buf); 33243b9c050Sjacobs } 3330a44ef6dSjacobs break; 3340a44ef6dSjacobs case 'q': { /* Solaris priority */ 3350a44ef6dSjacobs int i = atoi(optarg); 3360a44ef6dSjacobs 33743b9c050Sjacobs i = 100 - (i * 2.5); 3380a44ef6dSjacobs if ((i < 1) || (i > 100)) 3390a44ef6dSjacobs i = 50; 3400a44ef6dSjacobs papiAttributeListAddInteger(&list, 34143b9c050Sjacobs PAPI_ATTR_EXCL, "job-priority", i); 3420a44ef6dSjacobs } 3430a44ef6dSjacobs break; 3440a44ef6dSjacobs case 'S': /* Solaris character set */ 3450a44ef6dSjacobs papiAttributeListAddString(&list, 3460a44ef6dSjacobs PAPI_ATTR_EXCL, "lp-charset", 3470a44ef6dSjacobs ++entry); 3480a44ef6dSjacobs break; 3490a44ef6dSjacobs case 'T': /* Solaris type */ 3500a44ef6dSjacobs format = lp_type_to_mime_type(++entry); 3510a44ef6dSjacobs break; 3520a44ef6dSjacobs case 'y': /* Solaris mode */ 3530a44ef6dSjacobs papiAttributeListAddString(&list, 3540a44ef6dSjacobs PAPI_ATTR_APPEND, "lp-modes", ++entry); 3550a44ef6dSjacobs break; 3560a44ef6dSjacobs default: 3570a44ef6dSjacobs syslog(LOG_INFO|LOG_DEBUG, 3580a44ef6dSjacobs "Warning: cf message (%s) ignored", 3590a44ef6dSjacobs entry); 3600a44ef6dSjacobs break; 3610a44ef6dSjacobs } 3620a44ef6dSjacobs break; 3630a44ef6dSjacobs /* Undefined Extensions: SCO, Ultrix, AIX, ... */ 3640a44ef6dSjacobs 3650a44ef6dSjacobs default: 3660a44ef6dSjacobs syslog(LOG_INFO|LOG_DEBUG, 3670a44ef6dSjacobs "Warning: cf message (%s) ignored", entry); 3680a44ef6dSjacobs break; 3690a44ef6dSjacobs } 3700a44ef6dSjacobs 3710a44ef6dSjacobs if (format != NULL) 3720a44ef6dSjacobs papiAttributeListAddString(&list, PAPI_ATTR_EXCL, 3730a44ef6dSjacobs "document-format", format); 3740a44ef6dSjacobs } 3750a44ef6dSjacobs 3760a44ef6dSjacobs papiAttributeListAddInteger(&list, PAPI_ATTR_EXCL, 3770a44ef6dSjacobs "copies", ++copies); 3780a44ef6dSjacobs papiAttributeListAddString(&list, PAPI_ATTR_EXCL, 3790a44ef6dSjacobs "job-sheets", "none"); 3800a44ef6dSjacobs 3810a44ef6dSjacobs return (list); 3820a44ef6dSjacobs } 3830a44ef6dSjacobs 3840a44ef6dSjacobs static papi_status_t 385*36615d24Sjacobs submit_job(papi_service_t svc, FILE *ifp, char *printer, int rid, char *cf, 386*36615d24Sjacobs char **files) 3870a44ef6dSjacobs { 3880a44ef6dSjacobs papi_attribute_t **list = NULL; 3890a44ef6dSjacobs papi_status_t status; 3900a44ef6dSjacobs papi_job_t job = NULL; 3910a44ef6dSjacobs char *format = ""; 3920a44ef6dSjacobs 3930a44ef6dSjacobs if ((list = parse_cf(svc, cf, files)) != NULL) { 3940a44ef6dSjacobs /* use the host as known by us, not by them */ 3950a44ef6dSjacobs char *host = remote_host_name(ifp); 3960a44ef6dSjacobs 3970a44ef6dSjacobs if (host != NULL) { 3980a44ef6dSjacobs papiAttributeListAddString(&list, PAPI_ATTR_REPLACE, 3990a44ef6dSjacobs "job-originating-host-name", host); 4000a44ef6dSjacobs free(host); 4010a44ef6dSjacobs } 402*36615d24Sjacobs if (rid > 0) { 403*36615d24Sjacobs papiAttributeListAddInteger(&list, PAPI_ATTR_EXCL, 404*36615d24Sjacobs "job-id-requested", rid); 405*36615d24Sjacobs } 4060a44ef6dSjacobs } 4070a44ef6dSjacobs 4080a44ef6dSjacobs status = papiJobSubmit(svc, printer, list, NULL, files, &job); 4090a44ef6dSjacobs syslog(LOG_DEBUG, "submit: %s", papiStatusString(status)); 4100a44ef6dSjacobs if (status != PAPI_OK) { 4110a44ef6dSjacobs char *tmp = papiServiceGetStatusMessage(svc); 4120a44ef6dSjacobs 4130a44ef6dSjacobs syslog(LOG_DEBUG, "submit-detail: %s", tmp ? tmp : "none"); 4140a44ef6dSjacobs } 4150a44ef6dSjacobs papiJobFree(job); 4160a44ef6dSjacobs 4170a44ef6dSjacobs return (status); 4180a44ef6dSjacobs } 4190a44ef6dSjacobs 4200a44ef6dSjacobs static char * 4210a44ef6dSjacobs receive_control_file(papi_service_t svc, FILE *ifp, FILE *ofp, int size) 4220a44ef6dSjacobs { 4230a44ef6dSjacobs char *ptr, *cf_data; 4240a44ef6dSjacobs 4250a44ef6dSjacobs if ((ptr = cf_data = calloc(1, size + 1)) == NULL) { 4260a44ef6dSjacobs NACK(ofp); 4270a44ef6dSjacobs return (NULL); 4280a44ef6dSjacobs } else 429355b4669Sjacobs ACK(ofp); 430355b4669Sjacobs 4310a44ef6dSjacobs while (size > 0) { 4320a44ef6dSjacobs int rc; 433355b4669Sjacobs 4340a44ef6dSjacobs if (((rc = fread(ptr, 1, size, ifp)) == 0) && 4350a44ef6dSjacobs (feof(ifp) != 0)) { 4360a44ef6dSjacobs free(cf_data); 4370a44ef6dSjacobs return (NULL); 4380a44ef6dSjacobs } else { 4390a44ef6dSjacobs ptr += rc; 4400a44ef6dSjacobs size -= rc; 4410a44ef6dSjacobs } 4420a44ef6dSjacobs } 4430a44ef6dSjacobs syslog(LOG_DEBUG, "cf_data(%s)", cf_data); 4440a44ef6dSjacobs 4450a44ef6dSjacobs if (fgetc(ifp) != 0) { 4460a44ef6dSjacobs free(cf_data); 4470a44ef6dSjacobs return (NULL); 4480a44ef6dSjacobs } 4490a44ef6dSjacobs ACK(ofp); 4500a44ef6dSjacobs 4510a44ef6dSjacobs return (cf_data); 4520a44ef6dSjacobs } 4530a44ef6dSjacobs 4540a44ef6dSjacobs static char * 4550a44ef6dSjacobs receive_data_file(FILE *ifp, FILE *ofp, int size) 4560a44ef6dSjacobs { 457355b4669Sjacobs char file[] = "lpdXXXXXX"; 4580a44ef6dSjacobs char buf[BUFSIZ]; 459355b4669Sjacobs int fd; 460355b4669Sjacobs 4610a44ef6dSjacobs if ((fd = mkstemp(file)) < 0) { 4620a44ef6dSjacobs NACK(ofp); 4630a44ef6dSjacobs return (NULL); 4640a44ef6dSjacobs } else 4650a44ef6dSjacobs ACK(ofp); 466355b4669Sjacobs 4670a44ef6dSjacobs while (size > 0) { 4680a44ef6dSjacobs int rc = ((size > BUFSIZ) ? BUFSIZ : size); 4690a44ef6dSjacobs 4700a44ef6dSjacobs if (((rc = fread(buf, 1, rc, ifp)) == 0) && 4710a44ef6dSjacobs (feof(ifp) != 0)) { 4720a44ef6dSjacobs close(fd); 4730a44ef6dSjacobs unlink(file); 4740a44ef6dSjacobs return (NULL); 4750a44ef6dSjacobs } else { 4760a44ef6dSjacobs char *ptr = buf; 4770a44ef6dSjacobs 4780a44ef6dSjacobs while (rc > 0) { 4790a44ef6dSjacobs int wrc = write(fd, ptr, rc); 4800a44ef6dSjacobs 4810a44ef6dSjacobs if (wrc < 0) { 4820a44ef6dSjacobs close(fd); 4830a44ef6dSjacobs unlink(file); 4840a44ef6dSjacobs return (NULL); 4850a44ef6dSjacobs } 4860a44ef6dSjacobs 4870a44ef6dSjacobs ptr += wrc; 4880a44ef6dSjacobs size -= wrc; 4890a44ef6dSjacobs rc -= wrc; 4900a44ef6dSjacobs } 4910a44ef6dSjacobs } 4920a44ef6dSjacobs } 4930a44ef6dSjacobs close(fd); 4940a44ef6dSjacobs if (fgetc(ifp) != 0) { 4950a44ef6dSjacobs unlink(file); 4960a44ef6dSjacobs return (NULL); 4970a44ef6dSjacobs } 4980a44ef6dSjacobs ACK(ofp); 4990a44ef6dSjacobs 5000a44ef6dSjacobs return (strdup(file)); 5010a44ef6dSjacobs } 5020a44ef6dSjacobs 5030a44ef6dSjacobs static papi_status_t 5040a44ef6dSjacobs berkeley_receive_files(papi_service_t svc, FILE *ifp, FILE *ofp, char *printer) 5050a44ef6dSjacobs { 5060a44ef6dSjacobs papi_status_t status = PAPI_OK; 5070a44ef6dSjacobs char *file, **files = NULL; /* the job data files */ 5080a44ef6dSjacobs char *cf = NULL; 509*36615d24Sjacobs int rid = 0; 5100a44ef6dSjacobs char buf[BUFSIZ]; 5110a44ef6dSjacobs 5120a44ef6dSjacobs while (fgets(buf, sizeof (buf), ifp) != NULL) { 5130a44ef6dSjacobs int size; 5140a44ef6dSjacobs 5150a44ef6dSjacobs syslog(LOG_DEBUG, "XFER CMD: (%d)%s\n", buf[0], &buf[1]); 5160a44ef6dSjacobs #ifdef DEBUG /* translate [1-3]... messages to \[1-3] to run by hand */ 5170a44ef6dSjacobs if ((buf[0] > '0') && (buf[0] < '4')) 5180a44ef6dSjacobs buf[0] -= '0'; 5190a44ef6dSjacobs #endif 5200a44ef6dSjacobs switch (buf[0]) { 5210a44ef6dSjacobs case 0x01: /* Abort */ 5220a44ef6dSjacobs cleanup(&files, &cf); 5230a44ef6dSjacobs break; 5240a44ef6dSjacobs case 0x02: { /* Receive control file */ 525*36615d24Sjacobs if (((cf = strchr(buf, ' ')) != NULL) && 526*36615d24Sjacobs (strlen(cf) > 4)) { 527*36615d24Sjacobs while ((*cf != NULL) && (isdigit(*cf) == 0)) 528*36615d24Sjacobs cf++; 529*36615d24Sjacobs rid = atoi(cf); 530*36615d24Sjacobs } 5310a44ef6dSjacobs cf = receive_control_file(svc, ifp, ofp, atoi(&buf[1])); 5320a44ef6dSjacobs if (cf == NULL) { 5330a44ef6dSjacobs cleanup(&files, &cf); 5340a44ef6dSjacobs return (PAPI_BAD_REQUEST); 5350a44ef6dSjacobs } else if (files != NULL) { 536*36615d24Sjacobs status = submit_job(svc, ifp, printer, rid, cf, 5370a44ef6dSjacobs files); 5380a44ef6dSjacobs cleanup(&files, &cf); 5390a44ef6dSjacobs } 5400a44ef6dSjacobs } 5410a44ef6dSjacobs break; 5420a44ef6dSjacobs case 0x03: { /* Receive data file */ 5430a44ef6dSjacobs file = receive_data_file(ifp, ofp, atoi(&buf[1])); 5440a44ef6dSjacobs if (file == NULL) { 5450a44ef6dSjacobs cleanup(&files, &cf); 5460a44ef6dSjacobs return (PAPI_TEMPORARY_ERROR); 5470a44ef6dSjacobs } 5480a44ef6dSjacobs list_append(&files, file); 549355b4669Sjacobs } 550355b4669Sjacobs break; 551355b4669Sjacobs default: 5520a44ef6dSjacobs cleanup(&files, &cf); 553355b4669Sjacobs fatal(ofp, "protocol screwup"); 554355b4669Sjacobs break; 555355b4669Sjacobs } 556355b4669Sjacobs } 557355b4669Sjacobs 5580a44ef6dSjacobs if ((cf != NULL) && (files != NULL)) 559*36615d24Sjacobs status = submit_job(svc, ifp, printer, rid, cf, files); 5600a44ef6dSjacobs 5610a44ef6dSjacobs cleanup(&files, &cf); 5620a44ef6dSjacobs 5630a44ef6dSjacobs return (status); 564355b4669Sjacobs } 565355b4669Sjacobs 5660a44ef6dSjacobs static papi_status_t 567355b4669Sjacobs berkeley_transfer_files(papi_service_t svc, FILE *ifp, FILE *ofp, 568355b4669Sjacobs char *printer) 569355b4669Sjacobs { 570355b4669Sjacobs papi_status_t status; 571355b4669Sjacobs papi_printer_t p = NULL; 5720a44ef6dSjacobs char *keys[] = { "printer-is-accepting-jobs", NULL }; 573355b4669Sjacobs 574355b4669Sjacobs status = papiPrinterQuery(svc, printer, keys, NULL, &p); 575355b4669Sjacobs if ((status == PAPI_OK) && (p != NULL)) { 576355b4669Sjacobs papi_attribute_t **attrs = papiPrinterGetAttributeList(p); 577355b4669Sjacobs char accepting = PAPI_FALSE; 578355b4669Sjacobs 579355b4669Sjacobs papiAttributeListGetBoolean(attrs, NULL, 5800a44ef6dSjacobs "printer-is-accepting-jobs", &accepting); 581355b4669Sjacobs 5820a44ef6dSjacobs if (accepting == PAPI_TRUE) { 5830a44ef6dSjacobs ACK(ofp); 5840a44ef6dSjacobs status = berkeley_receive_files(svc, ifp, ofp, printer); 5850a44ef6dSjacobs } else 586355b4669Sjacobs NACK(ofp); 587355b4669Sjacobs 588355b4669Sjacobs papiPrinterFree(p); 589355b4669Sjacobs } else 590355b4669Sjacobs NACK(ofp); 5910a44ef6dSjacobs 5920a44ef6dSjacobs return (status); 593355b4669Sjacobs } 594355b4669Sjacobs 5950a44ef6dSjacobs static int 5960a44ef6dSjacobs cyclical_service_check(char *svc_name) 5970a44ef6dSjacobs { 5980a44ef6dSjacobs papi_attribute_t **list; 5990a44ef6dSjacobs char buf[BUFSIZ]; 6000a44ef6dSjacobs uri_t *uri = NULL; 6010a44ef6dSjacobs char *s = NULL; 6020a44ef6dSjacobs 6030a44ef6dSjacobs /* was there a printer? */ 6040a44ef6dSjacobs if (svc_name == NULL) 6050a44ef6dSjacobs return (0); 6060a44ef6dSjacobs 6070a44ef6dSjacobs if ((list = getprinterbyname(svc_name, NULL)) == NULL) 6080a44ef6dSjacobs return (0); /* if it doesnt' resolve, we will fail later */ 6090a44ef6dSjacobs 6100a44ef6dSjacobs papiAttributeListGetString(list, NULL, "printer-uri-supported", &s); 6110a44ef6dSjacobs if ((s == NULL) || (strcasecmp(svc_name, s) != 0)) 6120a44ef6dSjacobs return (0); /* they don't match */ 6130a44ef6dSjacobs 6140a44ef6dSjacobs /* is it in uri form? */ 6150a44ef6dSjacobs if (uri_from_string(s, &uri) < 0) 6160a44ef6dSjacobs return (0); 6170a44ef6dSjacobs 6180a44ef6dSjacobs if ((uri == NULL) || (uri->scheme == NULL) || (uri->host == NULL)) { 6190a44ef6dSjacobs uri_free(uri); 6200a44ef6dSjacobs return (0); 6210a44ef6dSjacobs } 6220a44ef6dSjacobs 6230a44ef6dSjacobs /* is it in lpd form? */ 6240a44ef6dSjacobs if (strcasecmp(uri->scheme, "lpd") != 0) { 6250a44ef6dSjacobs uri_free(uri); 6260a44ef6dSjacobs return (0); 6270a44ef6dSjacobs } 6280a44ef6dSjacobs 6290a44ef6dSjacobs /* is it the local host? */ 6300a44ef6dSjacobs sysinfo(SI_HOSTNAME, buf, sizeof (buf)); 6310a44ef6dSjacobs if ((strcasecmp(uri->host, "localhost") != 0) && 6320a44ef6dSjacobs (strcasecmp(uri->host, buf) != 0)) { 6330a44ef6dSjacobs uri_free(uri); 6340a44ef6dSjacobs return (0); 6350a44ef6dSjacobs } 6360a44ef6dSjacobs 6370a44ef6dSjacobs uri_free(uri); 6380a44ef6dSjacobs return (1); 6390a44ef6dSjacobs } 6400a44ef6dSjacobs 6410a44ef6dSjacobs 642355b4669Sjacobs /* 643355b4669Sjacobs * This is the entry point for this program. The program takes the 644355b4669Sjacobs * following options: 645355b4669Sjacobs * (none) 646355b4669Sjacobs */ 647355b4669Sjacobs int 648355b4669Sjacobs main(int ac, char *av[]) 649355b4669Sjacobs { 650355b4669Sjacobs papi_status_t status; 651355b4669Sjacobs papi_service_t svc = NULL; 652355b4669Sjacobs papi_encryption_t encryption = PAPI_ENCRYPT_NEVER; 653355b4669Sjacobs FILE *ifp = stdin, 654355b4669Sjacobs *ofp = stdout; 655355b4669Sjacobs int c; 656355b4669Sjacobs char buf[BUFSIZ], 657355b4669Sjacobs **args, 6580a44ef6dSjacobs *printer, 6590a44ef6dSjacobs *run_dir = "/var/run/in.lpd", 6600a44ef6dSjacobs *run_user = NULL; 6610a44ef6dSjacobs struct passwd *pw = NULL; 662355b4669Sjacobs 6630a44ef6dSjacobs (void) chdir("/tmp"); /* run in /tmp by default */ 664355b4669Sjacobs openlog("bsd-gw", LOG_PID, LOG_LPR); 665355b4669Sjacobs 6660a44ef6dSjacobs while ((c = getopt(ac, av, "Ed:u:")) != EOF) 667355b4669Sjacobs switch (c) { 668355b4669Sjacobs case 'E': 669355b4669Sjacobs encryption = PAPI_ENCRYPT_ALWAYS; 670355b4669Sjacobs break; 6710a44ef6dSjacobs case 'd': /* run where they tell you */ 6720a44ef6dSjacobs run_dir = optarg; 6730a44ef6dSjacobs break; 6740a44ef6dSjacobs case 'u': /* run as */ 6750a44ef6dSjacobs run_user = optarg; 6760a44ef6dSjacobs break; 677355b4669Sjacobs default: 678355b4669Sjacobs ; 679355b4669Sjacobs } 680355b4669Sjacobs 6810a44ef6dSjacobs if (run_user != NULL) /* get the requested user info */ 6820a44ef6dSjacobs pw = getpwnam(run_user); 6830a44ef6dSjacobs 6840a44ef6dSjacobs if (run_dir != NULL) { /* setup the run_dir */ 6850a44ef6dSjacobs (void) mkdir(run_dir, 0700); 6860a44ef6dSjacobs if (pw != NULL) 6870a44ef6dSjacobs (void) chown(run_dir, pw->pw_uid, pw->pw_gid); 6880a44ef6dSjacobs } 6890a44ef6dSjacobs 6900a44ef6dSjacobs if (pw != NULL) { /* run as the requested user */ 6910a44ef6dSjacobs syslog(LOG_DEBUG, "name: %s, uid: %d, gid: %d", 6920a44ef6dSjacobs pw->pw_name, pw->pw_uid, pw->pw_gid); 6930a44ef6dSjacobs initgroups(pw->pw_name, pw->pw_gid); 6940a44ef6dSjacobs setgid(pw->pw_gid); 6950a44ef6dSjacobs setuid(pw->pw_uid); 6960a44ef6dSjacobs } 6970a44ef6dSjacobs 6980a44ef6dSjacobs if (run_dir != NULL) /* move to the run_dir */ 6990a44ef6dSjacobs if (chdir(run_dir) < 0) { 7000a44ef6dSjacobs syslog(LOG_DEBUG, "failed to chdir(%s)", run_dir); 7010a44ef6dSjacobs exit(1); 7020a44ef6dSjacobs } 7030a44ef6dSjacobs 7040a44ef6dSjacobs syslog(LOG_DEBUG, "$CWD = %s", getwd(NULL)); 7050a44ef6dSjacobs 706355b4669Sjacobs if (fgets(buf, sizeof (buf), ifp) == NULL) { 707355b4669Sjacobs if (feof(ifp) == 0) 708355b4669Sjacobs syslog(LOG_ERR, "Error reading from connection: %s", 709355b4669Sjacobs strerror(errno)); 710355b4669Sjacobs exit(1); 711355b4669Sjacobs } 712355b4669Sjacobs 7130a44ef6dSjacobs syslog(LOG_DEBUG, "CMD: (%d)%s\n", buf[0], &buf[1]); 7140a44ef6dSjacobs 7150a44ef6dSjacobs #ifdef DEBUG /* translate [1-5]... messages to \[1-5] to run by hand */ 7160a44ef6dSjacobs if ((buf[0] > '0') && (buf[0] < '6')) 7170a44ef6dSjacobs buf[0] -= '0'; 7180a44ef6dSjacobs #endif 7190a44ef6dSjacobs 720355b4669Sjacobs if ((buf[0] < 1) || (buf[0] > 5)) { 721355b4669Sjacobs fatal(ofp, "Invalid protocol request (%d): %c%s\n", 722355b4669Sjacobs buf[0], buf[0], buf); 723355b4669Sjacobs exit(1); 724355b4669Sjacobs } 725355b4669Sjacobs 726355b4669Sjacobs args = strsplit(&buf[1], "\t\n "); 727355b4669Sjacobs printer = *args++; 728355b4669Sjacobs 729355b4669Sjacobs if (printer == NULL) { 730355b4669Sjacobs fatal(ofp, "Can't determine requested printer"); 731355b4669Sjacobs exit(1); 732355b4669Sjacobs } 733355b4669Sjacobs 7340a44ef6dSjacobs if (cyclical_service_check(printer) != 0) { 7350a44ef6dSjacobs fatal(ofp, "%s is cyclical\n", printer); 7360a44ef6dSjacobs exit(1); 7370a44ef6dSjacobs } 7380a44ef6dSjacobs 739355b4669Sjacobs status = papiServiceCreate(&svc, printer, NULL, NULL, NULL, 740355b4669Sjacobs encryption, NULL); 741355b4669Sjacobs if (status != PAPI_OK) { 742355b4669Sjacobs fatal(ofp, "Failed to contact service for %s: %s\n", printer, 743355b4669Sjacobs verbose_papi_message(svc, status)); 744355b4669Sjacobs exit(1); 745355b4669Sjacobs } 746355b4669Sjacobs 7470a44ef6dSjacobs /* 7480a44ef6dSjacobs * Trusted Solaris can't be trusting of intermediaries. Pass 7490a44ef6dSjacobs * the socket connection to the print service to retrieve the 7500a44ef6dSjacobs * sensativity label off of a multi-level port. 7510a44ef6dSjacobs */ 7520a44ef6dSjacobs (void) papiServiceSetPeer(svc, fileno(ifp)); 753355b4669Sjacobs 754355b4669Sjacobs switch (buf[0]) { 755355b4669Sjacobs case '\1': /* restart printer */ 756355b4669Sjacobs ACK(ofp); /* there is no equivalent */ 757355b4669Sjacobs break; 758355b4669Sjacobs case '\2': /* transfer job(s) */ 7590a44ef6dSjacobs status = berkeley_transfer_files(svc, ifp, ofp, printer); 760355b4669Sjacobs break; 761355b4669Sjacobs case '\3': /* show queue (short) */ 762355b4669Sjacobs case '\4': { /* show queue (long) */ 763355b4669Sjacobs int count; 764355b4669Sjacobs 765355b4669Sjacobs for (count = 0; args[count] != 0; count++); 766355b4669Sjacobs 767355b4669Sjacobs berkeley_queue_report(svc, ofp, printer, buf[0], count, args); 768355b4669Sjacobs } 769355b4669Sjacobs break; 770355b4669Sjacobs case '\5': { /* cancel job(s) */ 7710a44ef6dSjacobs char *user = *args++; 7720a44ef6dSjacobs char *host = remote_host_name(ifp); 773355b4669Sjacobs int count; 774355b4669Sjacobs 7750a44ef6dSjacobs if (host != NULL) { 7760a44ef6dSjacobs char buf[BUFSIZ]; 7770a44ef6dSjacobs 7780a44ef6dSjacobs snprintf(buf, sizeof (buf), "%s@%s", user, host); 7790a44ef6dSjacobs status = papiServiceSetUserName(svc, buf); 7800a44ef6dSjacobs } else 7810a44ef6dSjacobs status = papiServiceSetUserName(svc, user); 7820a44ef6dSjacobs 783355b4669Sjacobs for (count = 0; args[count] != 0; count++); 784355b4669Sjacobs 785355b4669Sjacobs berkeley_cancel_request(svc, ofp, printer, count, args); 786355b4669Sjacobs } 787355b4669Sjacobs break; 788355b4669Sjacobs default: 789355b4669Sjacobs fatal(ofp, "unsupported protocol request (%c), %s", 790355b4669Sjacobs buf[0], &buf[1]); 791355b4669Sjacobs } 792355b4669Sjacobs 793355b4669Sjacobs (void) fflush(ofp); 794355b4669Sjacobs 795355b4669Sjacobs syslog(LOG_DEBUG, "protocol request(%d) for %s completed: %s", 796355b4669Sjacobs buf[0], printer, papiStatusString(status)); 7970a44ef6dSjacobs if (status != PAPI_OK) 7980a44ef6dSjacobs syslog(LOG_DEBUG, "detail: %s", 7990a44ef6dSjacobs verbose_papi_message(svc, status)); 800355b4669Sjacobs 801355b4669Sjacobs papiServiceDestroy(svc); 802355b4669Sjacobs 803355b4669Sjacobs return (0); 804355b4669Sjacobs } 805