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 /* 23286caa64SKeerthi Kondaka * Copyright 2009 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 #include <stdio.h> 31355b4669Sjacobs #include <stdlib.h> 320a44ef6dSjacobs #include <unistd.h> 330a44ef6dSjacobs #include <fcntl.h> 34355b4669Sjacobs #include <stdarg.h> 35355b4669Sjacobs #include <string.h> 3636615d24Sjacobs #include <ctype.h> 37355b4669Sjacobs #include <errno.h> 38355b4669Sjacobs #include <syslog.h> 39355b4669Sjacobs #include <libintl.h> 400a44ef6dSjacobs #include <pwd.h> 410a44ef6dSjacobs #include <grp.h> 420a44ef6dSjacobs #include <sys/types.h> 430a44ef6dSjacobs #include <sys/stat.h> 440a44ef6dSjacobs #include <sys/socket.h> 450a44ef6dSjacobs #include <netinet/in.h> 460a44ef6dSjacobs #include <arpa/inet.h> 470a44ef6dSjacobs #include <netdb.h> 480a44ef6dSjacobs #include <sys/systeminfo.h> 49355b4669Sjacobs 50355b4669Sjacobs #include <papi.h> 510a44ef6dSjacobs #include <uri.h> 52355b4669Sjacobs #include "common.h" 53355b4669Sjacobs 54355b4669Sjacobs #define ACK(fp) { (void) fputc('\0', fp); (void) fflush(fp); } 55355b4669Sjacobs #define NACK(fp) { (void) fputc('\1', fp); (void) fflush(fp); } 56355b4669Sjacobs 57355b4669Sjacobs /* 58355b4669Sjacobs * This file contains the front-end of the BSD Print Protocol adaptor. This 59355b4669Sjacobs * code assumes a BSD Socket interface to the networking side. 60355b4669Sjacobs */ 61355b4669Sjacobs 620a44ef6dSjacobs static char * 630a44ef6dSjacobs remote_host_name(FILE *fp) 640a44ef6dSjacobs { 650a44ef6dSjacobs struct hostent *hp; 660a44ef6dSjacobs struct sockaddr_in6 peer; 670a44ef6dSjacobs socklen_t peer_len = sizeof (peer); 680a44ef6dSjacobs int fd = fileno(fp); 690a44ef6dSjacobs int error_num; 70a18dc42fSps29005 char tmp_buf[INET6_ADDRSTRLEN]; 710a44ef6dSjacobs char *hostname; 720a44ef6dSjacobs 730a44ef6dSjacobs /* who is our peer ? */ 740a44ef6dSjacobs if (getpeername(fd, (struct sockaddr *)&peer, &peer_len) < 0) { 750a44ef6dSjacobs if ((errno != ENOTSOCK) && (errno != EINVAL)) 760a44ef6dSjacobs return (NULL); 770a44ef6dSjacobs else 780a44ef6dSjacobs return (strdup("localhost")); 790a44ef6dSjacobs } 800a44ef6dSjacobs 810a44ef6dSjacobs /* get their name or return a string containing their address */ 820a44ef6dSjacobs if ((hp = getipnodebyaddr((const char *)&peer.sin6_addr, 830a44ef6dSjacobs sizeof (struct in6_addr), AF_INET6, 840a44ef6dSjacobs &error_num)) == NULL) { 850a44ef6dSjacobs return (strdup(inet_ntop(peer.sin6_family, 860a44ef6dSjacobs &peer.sin6_addr, tmp_buf, sizeof (tmp_buf)))); 870a44ef6dSjacobs } 880a44ef6dSjacobs 890a44ef6dSjacobs hostname = strdup(hp->h_name); 90a18dc42fSps29005 if (is_localhost(hp->h_name) != 0) 910a44ef6dSjacobs return (strdup("localhost")); 920a44ef6dSjacobs 930a44ef6dSjacobs /* It must be someone else */ 940a44ef6dSjacobs return (hostname); 950a44ef6dSjacobs } 960a44ef6dSjacobs 970a44ef6dSjacobs static void 98355b4669Sjacobs fatal(FILE *fp, char *fmt, ...) 99355b4669Sjacobs { 100355b4669Sjacobs va_list ap; 101355b4669Sjacobs 102355b4669Sjacobs va_start(ap, fmt); 103355b4669Sjacobs vsyslog(LOG_DEBUG, fmt, ap); 104355b4669Sjacobs vfprintf(fp, fmt, ap); 105355b4669Sjacobs va_end(ap); 1060a44ef6dSjacobs exit(1); 107355b4669Sjacobs } 108355b4669Sjacobs 109355b4669Sjacobs static void 1100a44ef6dSjacobs cleanup(char ***files, char **cf) 111355b4669Sjacobs { 1120a44ef6dSjacobs if (*files != NULL) { 113355b4669Sjacobs int i; 114355b4669Sjacobs 1150a44ef6dSjacobs for (i = 0; (*files)[i] != NULL; i++) { 1160a44ef6dSjacobs (void) unlink((*files)[i]); 1170a44ef6dSjacobs free((*files)[i]); 1180a44ef6dSjacobs } 1190a44ef6dSjacobs free(*files); 1200a44ef6dSjacobs *files = NULL; 1210a44ef6dSjacobs } 1220a44ef6dSjacobs 1230a44ef6dSjacobs if (*cf != NULL) { 1240a44ef6dSjacobs free(*cf); 1250a44ef6dSjacobs *cf = NULL; 126355b4669Sjacobs } 127355b4669Sjacobs } 128355b4669Sjacobs 1290a44ef6dSjacobs static papi_attribute_t ** 1300a44ef6dSjacobs parse_cf(papi_service_t svc, char *cf, char **files) 131355b4669Sjacobs { 1320a44ef6dSjacobs papi_attribute_t **list = NULL; 133*dcf1b443Ssonam gupta - Sun Microsystems - Bangalore India char previous = NULL; 134*dcf1b443Ssonam gupta - Sun Microsystems - Bangalore India char *entry; 135*dcf1b443Ssonam gupta - Sun Microsystems - Bangalore India int copies_set = 0; 136*dcf1b443Ssonam gupta - Sun Microsystems - Bangalore India int copies = 0; 137355b4669Sjacobs 1380a44ef6dSjacobs for (entry = strtok(cf, "\n"); entry != NULL; 1390a44ef6dSjacobs entry = strtok(NULL, "\n")) { 1400a44ef6dSjacobs char *format = NULL; 1410a44ef6dSjacobs 1420a44ef6dSjacobs /* count the copies */ 1430a44ef6dSjacobs if ((entry[0] >= 'a') && (entry[0] <= 'z') && 1440a44ef6dSjacobs (copies_set == 0) && (previous == entry[0])) 1450a44ef6dSjacobs copies++; 1460a44ef6dSjacobs else if ((previous >= 'a') && (previous <= 'z')) 1470a44ef6dSjacobs copies_set = 1; 1480a44ef6dSjacobs previous = entry[0]; 1490a44ef6dSjacobs 1500a44ef6dSjacobs /* process the control message */ 1510a44ef6dSjacobs switch (entry[0]) { 1520a44ef6dSjacobs /* RFC-1179 options */ 1530a44ef6dSjacobs case 'J': /* RFC-1179 Banner Job Name */ 1540a44ef6dSjacobs papiAttributeListAddString(&list, PAPI_ATTR_EXCL, 1550a44ef6dSjacobs "job-name", ++entry); 1560a44ef6dSjacobs break; 1570a44ef6dSjacobs case 'C': /* RFC-1179 Banner Class Name */ 1580a44ef6dSjacobs papiAttributeListAddString(&list, PAPI_ATTR_EXCL, 1590a44ef6dSjacobs "rfc-1179-class", ++entry); 1600a44ef6dSjacobs break; 1610a44ef6dSjacobs case 'L': /* RFC-1179 Banner toggle */ 1620a44ef6dSjacobs papiAttributeListAddString(&list, PAPI_ATTR_EXCL, 1630a44ef6dSjacobs "job-sheets", "standard"); 1640a44ef6dSjacobs break; 1650a44ef6dSjacobs case 'T': /* RFC-1179 Title (pr) */ 1660a44ef6dSjacobs papiAttributeListAddString(&list, PAPI_ATTR_EXCL, 1670a44ef6dSjacobs "pr-title", ++entry); 1680a44ef6dSjacobs break; 1690a44ef6dSjacobs case 'H': /* RFC-1179 Host */ 1700a44ef6dSjacobs /* 1710a44ef6dSjacobs * use the host as known by us, not by them 1720a44ef6dSjacobs * 1730a44ef6dSjacobs * papiAttributeListAddString(&list, PAPI_ATTR_EXCL, 1740a44ef6dSjacobs * "job-originating-host-name", ++entry); 1750a44ef6dSjacobs */ 1760a44ef6dSjacobs break; 1770a44ef6dSjacobs case 'P': /* RFC-1179 User */ 1780a44ef6dSjacobs ++entry; 1790a44ef6dSjacobs papiAttributeListAddString(&list, PAPI_ATTR_EXCL, 180286caa64SKeerthi Kondaka "job-originating-user-name", entry); 1810a44ef6dSjacobs papiServiceSetUserName(svc, entry); 1820a44ef6dSjacobs break; 1830a44ef6dSjacobs case 'M': /* RFC-1179 Mail to User */ 1840a44ef6dSjacobs papiAttributeListAddBoolean(&list, PAPI_ATTR_EXCL, 1850a44ef6dSjacobs "rfc-1179-mail", 1); 1860a44ef6dSjacobs break; 1870a44ef6dSjacobs case 'W': /* RFC-1179 Width (pr) */ 1880a44ef6dSjacobs papiAttributeListAddInteger(&list, PAPI_ATTR_EXCL, 1890a44ef6dSjacobs "pr-width", atoi(++entry)); 1900a44ef6dSjacobs break; 1910a44ef6dSjacobs case 'I': /* RFC-1179 Indent (pr) */ 1920a44ef6dSjacobs papiAttributeListAddInteger(&list, PAPI_ATTR_EXCL, 1930a44ef6dSjacobs "pr-indent", atoi(++entry)); 1940a44ef6dSjacobs break; 1950a44ef6dSjacobs case 'N': /* RFC-1179 Filename */ 1960a44ef6dSjacobs /* could have HPUX extension embedded */ 1970a44ef6dSjacobs if (entry[1] != ' ') { /* real pathname */ 1980a44ef6dSjacobs #ifdef DEBUG 1990a44ef6dSjacobs papiAttributeListAddString(&list, 2000a44ef6dSjacobs PAPI_ATTR_EXCL, 2010a44ef6dSjacobs "flist", ++entry); 2020a44ef6dSjacobs #endif 2030a44ef6dSjacobs } else if (entry[2] == 'O') /* HPUX lp -o options */ 2040a44ef6dSjacobs papiAttributeListFromString(&list, 2050a44ef6dSjacobs PAPI_ATTR_APPEND, ++entry); 2060a44ef6dSjacobs break; 2070a44ef6dSjacobs case 'U': /* RFC-1179 Unlink */ 2080a44ef6dSjacobs break; /* ignored */ 2090a44ef6dSjacobs case '1': /* RFC-1179 TROFF Font R */ 2100a44ef6dSjacobs papiAttributeListAddString(&list, PAPI_ATTR_EXCL, 2110a44ef6dSjacobs "rfc-1179-font-r", ++entry); 2120a44ef6dSjacobs break; 2130a44ef6dSjacobs case '2': /* RFC-1179 TROFF Font I */ 2140a44ef6dSjacobs papiAttributeListAddString(&list, PAPI_ATTR_EXCL, 2150a44ef6dSjacobs "rfc-1179-font-i", ++entry); 2160a44ef6dSjacobs break; 2170a44ef6dSjacobs case '3': /* RFC-1179 TROFF Font B */ 2180a44ef6dSjacobs papiAttributeListAddString(&list, PAPI_ATTR_EXCL, 2190a44ef6dSjacobs "rfc-1179-font-b", ++entry); 2200a44ef6dSjacobs break; 2210a44ef6dSjacobs case '4': /* RFC-1179 TROFF Font S */ 2220a44ef6dSjacobs papiAttributeListAddString(&list, PAPI_ATTR_EXCL, 2230a44ef6dSjacobs "rfc-1179-font-s", ++entry); 2240a44ef6dSjacobs break; 2250a44ef6dSjacobs case 'f': /* RFC-1179 ASCII file (print) */ 2260a44ef6dSjacobs format = "text/plain"; 2270a44ef6dSjacobs if (is_postscript(files[0]) == 1) 2280a44ef6dSjacobs format = "application/postscript"; 2290a44ef6dSjacobs break; 2300a44ef6dSjacobs case 'l': /* RFC-1179 CATV file (print) */ 2310a44ef6dSjacobs format = "application/octet-stream"; 2320a44ef6dSjacobs if (is_postscript(files[0]) == 1) 2330a44ef6dSjacobs format = "application/postscript"; 2340a44ef6dSjacobs break; 2350a44ef6dSjacobs case 'o': /* RFC-1179 Postscript file (print) */ 2360a44ef6dSjacobs format = "application/postscript"; 2370a44ef6dSjacobs break; 2380a44ef6dSjacobs case 'p': /* RFC-1179 PR file (print) */ 2390a44ef6dSjacobs format = "application/x-pr"; 2400a44ef6dSjacobs papiAttributeListAddBoolean(&list, PAPI_ATTR_EXCL, 2410a44ef6dSjacobs "pr-filter", 1); 2420a44ef6dSjacobs break; 2430a44ef6dSjacobs case 't': /* RFC-1179 TROFF file (print) */ 2440a44ef6dSjacobs format = "application/x-troff"; 2450a44ef6dSjacobs break; 2460a44ef6dSjacobs case 'n': /* RFC-1179 DITROFF file (print) */ 2470a44ef6dSjacobs format = "application/x-ditroff"; 2480a44ef6dSjacobs break; 2490a44ef6dSjacobs case 'd': /* RFC-1179 DVI file (print) */ 2500a44ef6dSjacobs format = "application/x-dvi"; 2510a44ef6dSjacobs break; 2520a44ef6dSjacobs case 'g': /* RFC-1179 GRAPH file (print) */ 2530a44ef6dSjacobs format = "application/x-plot"; 2540a44ef6dSjacobs break; 2550a44ef6dSjacobs case 'c': /* RFC-1179 CIF file (print) */ 2560a44ef6dSjacobs format = "application/x-cif"; 2570a44ef6dSjacobs break; 2580a44ef6dSjacobs case 'v': /* RFC-1179 RASTER file (print) */ 2590a44ef6dSjacobs format = "application/x-raster"; 2600a44ef6dSjacobs break; 2610a44ef6dSjacobs case 'r': /* RFC-1179 FORTRAN file (print) */ 2620a44ef6dSjacobs format = "application/x-fortran"; 2630a44ef6dSjacobs break; 2640a44ef6dSjacobs /* Sun Solaris Extensions */ 2650a44ef6dSjacobs case 'O': 2660a44ef6dSjacobs ++entry; 267a18dc42fSps29005 { 268a18dc42fSps29005 int rd, wr; 269a18dc42fSps29005 270a18dc42fSps29005 for (rd = wr = 0; entry[rd] != '\0'; rd++) { 271a18dc42fSps29005 if (entry[rd] == '"') 272a18dc42fSps29005 continue; 273a18dc42fSps29005 if (rd != wr) 274a18dc42fSps29005 entry[wr] = entry[rd]; 275a18dc42fSps29005 wr++; 276a18dc42fSps29005 } 277a18dc42fSps29005 entry[wr] = '\0'; 278a18dc42fSps29005 279a18dc42fSps29005 papiAttributeListFromString(&list, 280a18dc42fSps29005 PAPI_ATTR_APPEND, entry); 281a18dc42fSps29005 } 2820a44ef6dSjacobs break; 2830a44ef6dSjacobs case '5': 2840a44ef6dSjacobs ++entry; 2850a44ef6dSjacobs switch (entry[0]) { 2860a44ef6dSjacobs case 'f': /* Solaris form */ 2870a44ef6dSjacobs papiAttributeListAddString(&list, 2880a44ef6dSjacobs PAPI_ATTR_EXCL, 2890a44ef6dSjacobs "form", ++entry); 2900a44ef6dSjacobs break; 2910a44ef6dSjacobs case 'H': /* Solaris handling */ 2920a44ef6dSjacobs ++entry; 2930a44ef6dSjacobs if (strcasecmp(entry, "hold") == 0) 2940a44ef6dSjacobs papiAttributeListAddString(&list, 2950a44ef6dSjacobs PAPI_ATTR_EXCL, 2960a44ef6dSjacobs "job-hold-until", "indefinite"); 29743b9c050Sjacobs else if (strcasecmp(entry, "immediate") == 0) 2980a44ef6dSjacobs papiAttributeListAddString(&list, 2990a44ef6dSjacobs PAPI_ATTR_EXCL, 3000a44ef6dSjacobs "job-hold-until", "no-hold"); 3010a44ef6dSjacobs else 3020a44ef6dSjacobs papiAttributeListAddString(&list, 3030a44ef6dSjacobs PAPI_ATTR_EXCL, 3040a44ef6dSjacobs "job-hold-until", entry); 3050a44ef6dSjacobs break; 3060a44ef6dSjacobs case 'p': /* Solaris notification */ 3070a44ef6dSjacobs papiAttributeListAddBoolean(&list, 3080a44ef6dSjacobs PAPI_ATTR_EXCL, "rfc-1179-mail", 1); 3090a44ef6dSjacobs break; 31043b9c050Sjacobs case 'P': { /* Solaris page list */ 31143b9c050Sjacobs char buf[BUFSIZ]; 31243b9c050Sjacobs 31343b9c050Sjacobs snprintf(buf, sizeof (buf), "page-ranges=%s", 31443b9c050Sjacobs ++entry); 31543b9c050Sjacobs papiAttributeListFromString(&list, 31643b9c050Sjacobs PAPI_ATTR_EXCL, buf); 31743b9c050Sjacobs } 3180a44ef6dSjacobs break; 3190a44ef6dSjacobs case 'q': { /* Solaris priority */ 3200a44ef6dSjacobs int i = atoi(optarg); 3210a44ef6dSjacobs 32243b9c050Sjacobs i = 100 - (i * 2.5); 3230a44ef6dSjacobs if ((i < 1) || (i > 100)) 3240a44ef6dSjacobs i = 50; 3250a44ef6dSjacobs papiAttributeListAddInteger(&list, 32643b9c050Sjacobs PAPI_ATTR_EXCL, "job-priority", i); 3270a44ef6dSjacobs } 3280a44ef6dSjacobs break; 3290a44ef6dSjacobs case 'S': /* Solaris character set */ 3300a44ef6dSjacobs papiAttributeListAddString(&list, 3310a44ef6dSjacobs PAPI_ATTR_EXCL, "lp-charset", 3320a44ef6dSjacobs ++entry); 3330a44ef6dSjacobs break; 3340a44ef6dSjacobs case 'T': /* Solaris type */ 3350a44ef6dSjacobs format = lp_type_to_mime_type(++entry); 3360a44ef6dSjacobs break; 3370a44ef6dSjacobs case 'y': /* Solaris mode */ 3380a44ef6dSjacobs papiAttributeListAddString(&list, 3390a44ef6dSjacobs PAPI_ATTR_APPEND, "lp-modes", ++entry); 3400a44ef6dSjacobs break; 3410a44ef6dSjacobs default: 3420a44ef6dSjacobs syslog(LOG_INFO|LOG_DEBUG, 3430a44ef6dSjacobs "Warning: cf message (%s) ignored", 3440a44ef6dSjacobs entry); 3450a44ef6dSjacobs break; 3460a44ef6dSjacobs } 3470a44ef6dSjacobs break; 3480a44ef6dSjacobs /* Undefined Extensions: SCO, Ultrix, AIX, ... */ 3490a44ef6dSjacobs 3500a44ef6dSjacobs default: 3510a44ef6dSjacobs syslog(LOG_INFO|LOG_DEBUG, 3520a44ef6dSjacobs "Warning: cf message (%s) ignored", entry); 3530a44ef6dSjacobs break; 3540a44ef6dSjacobs } 3550a44ef6dSjacobs 3560a44ef6dSjacobs if (format != NULL) 3570a44ef6dSjacobs papiAttributeListAddString(&list, PAPI_ATTR_EXCL, 3580a44ef6dSjacobs "document-format", format); 3590a44ef6dSjacobs } 3600a44ef6dSjacobs 3610a44ef6dSjacobs papiAttributeListAddInteger(&list, PAPI_ATTR_EXCL, 3620a44ef6dSjacobs "copies", ++copies); 3630a44ef6dSjacobs papiAttributeListAddString(&list, PAPI_ATTR_EXCL, 3640a44ef6dSjacobs "job-sheets", "none"); 3650a44ef6dSjacobs 3660a44ef6dSjacobs return (list); 3670a44ef6dSjacobs } 3680a44ef6dSjacobs 3690a44ef6dSjacobs static papi_status_t 37036615d24Sjacobs submit_job(papi_service_t svc, FILE *ifp, char *printer, int rid, char *cf, 37136615d24Sjacobs char **files) 3720a44ef6dSjacobs { 3730a44ef6dSjacobs papi_attribute_t **list = NULL; 3740a44ef6dSjacobs papi_status_t status; 3750a44ef6dSjacobs papi_job_t job = NULL; 3760a44ef6dSjacobs char *format = ""; 3770a44ef6dSjacobs 3780a44ef6dSjacobs if ((list = parse_cf(svc, cf, files)) != NULL) { 3790a44ef6dSjacobs /* use the host as known by us, not by them */ 3800a44ef6dSjacobs char *host = remote_host_name(ifp); 3810a44ef6dSjacobs 3820a44ef6dSjacobs if (host != NULL) { 3830a44ef6dSjacobs papiAttributeListAddString(&list, PAPI_ATTR_REPLACE, 3840a44ef6dSjacobs "job-originating-host-name", host); 3850a44ef6dSjacobs free(host); 3860a44ef6dSjacobs } 387*dcf1b443Ssonam gupta - Sun Microsystems - Bangalore India if (rid >= 0) { 38836615d24Sjacobs papiAttributeListAddInteger(&list, PAPI_ATTR_EXCL, 38936615d24Sjacobs "job-id-requested", rid); 39036615d24Sjacobs } 3910a44ef6dSjacobs } 3920a44ef6dSjacobs 3930a44ef6dSjacobs status = papiJobSubmit(svc, printer, list, NULL, files, &job); 3940a44ef6dSjacobs syslog(LOG_DEBUG, "submit: %s", papiStatusString(status)); 3950a44ef6dSjacobs if (status != PAPI_OK) { 3960a44ef6dSjacobs char *tmp = papiServiceGetStatusMessage(svc); 3970a44ef6dSjacobs 3980a44ef6dSjacobs syslog(LOG_DEBUG, "submit-detail: %s", tmp ? tmp : "none"); 3990a44ef6dSjacobs } 4000a44ef6dSjacobs papiJobFree(job); 4010a44ef6dSjacobs 4020a44ef6dSjacobs return (status); 4030a44ef6dSjacobs } 4040a44ef6dSjacobs 4050a44ef6dSjacobs static char * 4060a44ef6dSjacobs receive_control_file(papi_service_t svc, FILE *ifp, FILE *ofp, int size) 4070a44ef6dSjacobs { 4080a44ef6dSjacobs char *ptr, *cf_data; 4090a44ef6dSjacobs 4100a44ef6dSjacobs if ((ptr = cf_data = calloc(1, size + 1)) == NULL) { 4110a44ef6dSjacobs NACK(ofp); 4120a44ef6dSjacobs return (NULL); 4130a44ef6dSjacobs } else 414355b4669Sjacobs ACK(ofp); 415355b4669Sjacobs 4160a44ef6dSjacobs while (size > 0) { 4170a44ef6dSjacobs int rc; 418355b4669Sjacobs 4190a44ef6dSjacobs if (((rc = fread(ptr, 1, size, ifp)) == 0) && 4200a44ef6dSjacobs (feof(ifp) != 0)) { 4210a44ef6dSjacobs free(cf_data); 4220a44ef6dSjacobs return (NULL); 4230a44ef6dSjacobs } else { 4240a44ef6dSjacobs ptr += rc; 4250a44ef6dSjacobs size -= rc; 4260a44ef6dSjacobs } 4270a44ef6dSjacobs } 4280a44ef6dSjacobs syslog(LOG_DEBUG, "cf_data(%s)", cf_data); 4290a44ef6dSjacobs 4300a44ef6dSjacobs if (fgetc(ifp) != 0) { 4310a44ef6dSjacobs free(cf_data); 4320a44ef6dSjacobs return (NULL); 4330a44ef6dSjacobs } 4340a44ef6dSjacobs ACK(ofp); 4350a44ef6dSjacobs 4360a44ef6dSjacobs return (cf_data); 4370a44ef6dSjacobs } 4380a44ef6dSjacobs 4390a44ef6dSjacobs static char * 4400a44ef6dSjacobs receive_data_file(FILE *ifp, FILE *ofp, int size) 4410a44ef6dSjacobs { 442355b4669Sjacobs char file[] = "lpdXXXXXX"; 4430a44ef6dSjacobs char buf[BUFSIZ]; 444355b4669Sjacobs int fd; 445355b4669Sjacobs 4460a44ef6dSjacobs if ((fd = mkstemp(file)) < 0) { 4470a44ef6dSjacobs NACK(ofp); 4480a44ef6dSjacobs return (NULL); 4490a44ef6dSjacobs } else 4500a44ef6dSjacobs ACK(ofp); 451355b4669Sjacobs 4520a44ef6dSjacobs while (size > 0) { 4530a44ef6dSjacobs int rc = ((size > BUFSIZ) ? BUFSIZ : size); 4540a44ef6dSjacobs 4550a44ef6dSjacobs if (((rc = fread(buf, 1, rc, ifp)) == 0) && 4560a44ef6dSjacobs (feof(ifp) != 0)) { 4570a44ef6dSjacobs close(fd); 4580a44ef6dSjacobs unlink(file); 4590a44ef6dSjacobs return (NULL); 4600a44ef6dSjacobs } else { 4610a44ef6dSjacobs char *ptr = buf; 4620a44ef6dSjacobs 4630a44ef6dSjacobs while (rc > 0) { 4640a44ef6dSjacobs int wrc = write(fd, ptr, rc); 4650a44ef6dSjacobs 4660a44ef6dSjacobs if (wrc < 0) { 4670a44ef6dSjacobs close(fd); 4680a44ef6dSjacobs unlink(file); 4690a44ef6dSjacobs return (NULL); 4700a44ef6dSjacobs } 4710a44ef6dSjacobs 4720a44ef6dSjacobs ptr += wrc; 4730a44ef6dSjacobs size -= wrc; 4740a44ef6dSjacobs rc -= wrc; 4750a44ef6dSjacobs } 4760a44ef6dSjacobs } 4770a44ef6dSjacobs } 4780a44ef6dSjacobs close(fd); 4790a44ef6dSjacobs if (fgetc(ifp) != 0) { 4800a44ef6dSjacobs unlink(file); 4810a44ef6dSjacobs return (NULL); 4820a44ef6dSjacobs } 4830a44ef6dSjacobs ACK(ofp); 4840a44ef6dSjacobs 4850a44ef6dSjacobs return (strdup(file)); 4860a44ef6dSjacobs } 4870a44ef6dSjacobs 4880a44ef6dSjacobs static papi_status_t 4890a44ef6dSjacobs berkeley_receive_files(papi_service_t svc, FILE *ifp, FILE *ofp, char *printer) 4900a44ef6dSjacobs { 4910a44ef6dSjacobs papi_status_t status = PAPI_OK; 4920a44ef6dSjacobs char *file, **files = NULL; /* the job data files */ 4930a44ef6dSjacobs char *cf = NULL; 49436615d24Sjacobs int rid = 0; 4950a44ef6dSjacobs char buf[BUFSIZ]; 4960a44ef6dSjacobs 4970a44ef6dSjacobs while (fgets(buf, sizeof (buf), ifp) != NULL) { 4980a44ef6dSjacobs int size; 4990a44ef6dSjacobs 5000a44ef6dSjacobs syslog(LOG_DEBUG, "XFER CMD: (%d)%s\n", buf[0], &buf[1]); 5010a44ef6dSjacobs #ifdef DEBUG /* translate [1-3]... messages to \[1-3] to run by hand */ 5020a44ef6dSjacobs if ((buf[0] > '0') && (buf[0] < '4')) 5030a44ef6dSjacobs buf[0] -= '0'; 5040a44ef6dSjacobs #endif 5050a44ef6dSjacobs switch (buf[0]) { 5060a44ef6dSjacobs case 0x01: /* Abort */ 5070a44ef6dSjacobs cleanup(&files, &cf); 5080a44ef6dSjacobs break; 5090a44ef6dSjacobs case 0x02: { /* Receive control file */ 51036615d24Sjacobs if (((cf = strchr(buf, ' ')) != NULL) && 51136615d24Sjacobs (strlen(cf) > 4)) { 51236615d24Sjacobs while ((*cf != NULL) && (isdigit(*cf) == 0)) 51336615d24Sjacobs cf++; 51436615d24Sjacobs rid = atoi(cf); 51536615d24Sjacobs } 5160a44ef6dSjacobs cf = receive_control_file(svc, ifp, ofp, atoi(&buf[1])); 5170a44ef6dSjacobs if (cf == NULL) { 5180a44ef6dSjacobs cleanup(&files, &cf); 5190a44ef6dSjacobs return (PAPI_BAD_REQUEST); 5200a44ef6dSjacobs } else if (files != NULL) { 52136615d24Sjacobs status = submit_job(svc, ifp, printer, rid, cf, 5220a44ef6dSjacobs files); 5230a44ef6dSjacobs cleanup(&files, &cf); 5240a44ef6dSjacobs } 5250a44ef6dSjacobs } 5260a44ef6dSjacobs break; 5270a44ef6dSjacobs case 0x03: { /* Receive data file */ 5280a44ef6dSjacobs file = receive_data_file(ifp, ofp, atoi(&buf[1])); 5290a44ef6dSjacobs if (file == NULL) { 5300a44ef6dSjacobs cleanup(&files, &cf); 5310a44ef6dSjacobs return (PAPI_TEMPORARY_ERROR); 5320a44ef6dSjacobs } 5330a44ef6dSjacobs list_append(&files, file); 534355b4669Sjacobs } 535355b4669Sjacobs break; 536355b4669Sjacobs default: 5370a44ef6dSjacobs cleanup(&files, &cf); 538355b4669Sjacobs fatal(ofp, "protocol screwup"); 539355b4669Sjacobs break; 540355b4669Sjacobs } 541355b4669Sjacobs } 542355b4669Sjacobs 5430a44ef6dSjacobs if ((cf != NULL) && (files != NULL)) 54436615d24Sjacobs status = submit_job(svc, ifp, printer, rid, cf, files); 5450a44ef6dSjacobs 5460a44ef6dSjacobs cleanup(&files, &cf); 5470a44ef6dSjacobs 5480a44ef6dSjacobs return (status); 549355b4669Sjacobs } 550355b4669Sjacobs 5510a44ef6dSjacobs static papi_status_t 552355b4669Sjacobs berkeley_transfer_files(papi_service_t svc, FILE *ifp, FILE *ofp, 553355b4669Sjacobs char *printer) 554355b4669Sjacobs { 555355b4669Sjacobs papi_status_t status; 556355b4669Sjacobs papi_printer_t p = NULL; 5570a44ef6dSjacobs char *keys[] = { "printer-is-accepting-jobs", NULL }; 558355b4669Sjacobs 559355b4669Sjacobs status = papiPrinterQuery(svc, printer, keys, NULL, &p); 560355b4669Sjacobs if ((status == PAPI_OK) && (p != NULL)) { 561355b4669Sjacobs papi_attribute_t **attrs = papiPrinterGetAttributeList(p); 562355b4669Sjacobs char accepting = PAPI_FALSE; 563355b4669Sjacobs 564355b4669Sjacobs papiAttributeListGetBoolean(attrs, NULL, 5650a44ef6dSjacobs "printer-is-accepting-jobs", &accepting); 566355b4669Sjacobs 5670a44ef6dSjacobs if (accepting == PAPI_TRUE) { 5680a44ef6dSjacobs ACK(ofp); 5690a44ef6dSjacobs status = berkeley_receive_files(svc, ifp, ofp, printer); 5700a44ef6dSjacobs } else 571355b4669Sjacobs NACK(ofp); 572355b4669Sjacobs 573355b4669Sjacobs papiPrinterFree(p); 574355b4669Sjacobs } else 575355b4669Sjacobs NACK(ofp); 5760a44ef6dSjacobs 5770a44ef6dSjacobs return (status); 578355b4669Sjacobs } 579355b4669Sjacobs 5800a44ef6dSjacobs static int 5810a44ef6dSjacobs cyclical_service_check(char *svc_name) 5820a44ef6dSjacobs { 5830a44ef6dSjacobs papi_attribute_t **list; 5840a44ef6dSjacobs uri_t *uri = NULL; 5850a44ef6dSjacobs char *s = NULL; 5860a44ef6dSjacobs 5870a44ef6dSjacobs /* was there a printer? */ 5880a44ef6dSjacobs if (svc_name == NULL) 5890a44ef6dSjacobs return (0); 5900a44ef6dSjacobs 5910a44ef6dSjacobs if ((list = getprinterbyname(svc_name, NULL)) == NULL) 5920a44ef6dSjacobs return (0); /* if it doesnt' resolve, we will fail later */ 5930a44ef6dSjacobs 5940a44ef6dSjacobs papiAttributeListGetString(list, NULL, "printer-uri-supported", &s); 5950a44ef6dSjacobs if ((s == NULL) || (strcasecmp(svc_name, s) != 0)) 5960a44ef6dSjacobs return (0); /* they don't match */ 5970a44ef6dSjacobs 5980a44ef6dSjacobs /* is it in uri form? */ 5990a44ef6dSjacobs if (uri_from_string(s, &uri) < 0) 6000a44ef6dSjacobs return (0); 6010a44ef6dSjacobs 6020a44ef6dSjacobs if ((uri == NULL) || (uri->scheme == NULL) || (uri->host == NULL)) { 6030a44ef6dSjacobs uri_free(uri); 6040a44ef6dSjacobs return (0); 6050a44ef6dSjacobs } 6060a44ef6dSjacobs 6070a44ef6dSjacobs /* is it in lpd form? */ 6080a44ef6dSjacobs if (strcasecmp(uri->scheme, "lpd") != 0) { 6090a44ef6dSjacobs uri_free(uri); 6100a44ef6dSjacobs return (0); 6110a44ef6dSjacobs } 6120a44ef6dSjacobs 6130a44ef6dSjacobs /* is it the local host? */ 614a18dc42fSps29005 if (is_localhost(uri->host) != 0) { 6150a44ef6dSjacobs uri_free(uri); 6160a44ef6dSjacobs return (0); 6170a44ef6dSjacobs } 6180a44ef6dSjacobs 6190a44ef6dSjacobs uri_free(uri); 6200a44ef6dSjacobs return (1); 6210a44ef6dSjacobs } 6220a44ef6dSjacobs 6230a44ef6dSjacobs 624355b4669Sjacobs /* 625355b4669Sjacobs * This is the entry point for this program. The program takes the 626355b4669Sjacobs * following options: 627355b4669Sjacobs * (none) 628355b4669Sjacobs */ 629355b4669Sjacobs int 630355b4669Sjacobs main(int ac, char *av[]) 631355b4669Sjacobs { 632355b4669Sjacobs papi_status_t status; 633355b4669Sjacobs papi_service_t svc = NULL; 634355b4669Sjacobs papi_encryption_t encryption = PAPI_ENCRYPT_NEVER; 635*dcf1b443Ssonam gupta - Sun Microsystems - Bangalore India FILE *ifp = stdin; 636*dcf1b443Ssonam gupta - Sun Microsystems - Bangalore India FILE *ofp = stdout; 637355b4669Sjacobs int c; 638*dcf1b443Ssonam gupta - Sun Microsystems - Bangalore India char buf[BUFSIZ]; 639*dcf1b443Ssonam gupta - Sun Microsystems - Bangalore India char **args; 640*dcf1b443Ssonam gupta - Sun Microsystems - Bangalore India char *printer; 641*dcf1b443Ssonam gupta - Sun Microsystems - Bangalore India char *run_dir = "/var/run/in.lpd"; 642*dcf1b443Ssonam gupta - Sun Microsystems - Bangalore India char *run_user = NULL; 6430a44ef6dSjacobs struct passwd *pw = NULL; 644355b4669Sjacobs 6450a44ef6dSjacobs (void) chdir("/tmp"); /* run in /tmp by default */ 646355b4669Sjacobs openlog("bsd-gw", LOG_PID, LOG_LPR); 647355b4669Sjacobs 6480a44ef6dSjacobs while ((c = getopt(ac, av, "Ed:u:")) != EOF) 649355b4669Sjacobs switch (c) { 650355b4669Sjacobs case 'E': 651355b4669Sjacobs encryption = PAPI_ENCRYPT_ALWAYS; 652355b4669Sjacobs break; 6530a44ef6dSjacobs case 'd': /* run where they tell you */ 6540a44ef6dSjacobs run_dir = optarg; 6550a44ef6dSjacobs break; 6560a44ef6dSjacobs case 'u': /* run as */ 6570a44ef6dSjacobs run_user = optarg; 6580a44ef6dSjacobs break; 659355b4669Sjacobs default: 660355b4669Sjacobs ; 661355b4669Sjacobs } 662355b4669Sjacobs 6630a44ef6dSjacobs if (run_user != NULL) /* get the requested user info */ 6640a44ef6dSjacobs pw = getpwnam(run_user); 6650a44ef6dSjacobs 6660a44ef6dSjacobs if (run_dir != NULL) { /* setup the run_dir */ 6670a44ef6dSjacobs (void) mkdir(run_dir, 0700); 6680a44ef6dSjacobs if (pw != NULL) 6690a44ef6dSjacobs (void) chown(run_dir, pw->pw_uid, pw->pw_gid); 6700a44ef6dSjacobs } 6710a44ef6dSjacobs 6720a44ef6dSjacobs if (pw != NULL) { /* run as the requested user */ 6730a44ef6dSjacobs syslog(LOG_DEBUG, "name: %s, uid: %d, gid: %d", 6740a44ef6dSjacobs pw->pw_name, pw->pw_uid, pw->pw_gid); 6750a44ef6dSjacobs initgroups(pw->pw_name, pw->pw_gid); 6760a44ef6dSjacobs setgid(pw->pw_gid); 6770a44ef6dSjacobs setuid(pw->pw_uid); 6780a44ef6dSjacobs } 6790a44ef6dSjacobs 6800a44ef6dSjacobs if (run_dir != NULL) /* move to the run_dir */ 6810a44ef6dSjacobs if (chdir(run_dir) < 0) { 6820a44ef6dSjacobs syslog(LOG_DEBUG, "failed to chdir(%s)", run_dir); 6830a44ef6dSjacobs exit(1); 6840a44ef6dSjacobs } 6850a44ef6dSjacobs 6860a44ef6dSjacobs syslog(LOG_DEBUG, "$CWD = %s", getwd(NULL)); 6870a44ef6dSjacobs 688355b4669Sjacobs if (fgets(buf, sizeof (buf), ifp) == NULL) { 689355b4669Sjacobs if (feof(ifp) == 0) 690355b4669Sjacobs syslog(LOG_ERR, "Error reading from connection: %s", 691355b4669Sjacobs strerror(errno)); 692355b4669Sjacobs exit(1); 693355b4669Sjacobs } 694355b4669Sjacobs 6950a44ef6dSjacobs syslog(LOG_DEBUG, "CMD: (%d)%s\n", buf[0], &buf[1]); 6960a44ef6dSjacobs 6970a44ef6dSjacobs #ifdef DEBUG /* translate [1-5]... messages to \[1-5] to run by hand */ 6980a44ef6dSjacobs if ((buf[0] > '0') && (buf[0] < '6')) 6990a44ef6dSjacobs buf[0] -= '0'; 7000a44ef6dSjacobs #endif 7010a44ef6dSjacobs 702355b4669Sjacobs if ((buf[0] < 1) || (buf[0] > 5)) { 703355b4669Sjacobs fatal(ofp, "Invalid protocol request (%d): %c%s\n", 704355b4669Sjacobs buf[0], buf[0], buf); 705355b4669Sjacobs exit(1); 706355b4669Sjacobs } 707355b4669Sjacobs 708355b4669Sjacobs args = strsplit(&buf[1], "\t\n "); 709355b4669Sjacobs printer = *args++; 710355b4669Sjacobs 711355b4669Sjacobs if (printer == NULL) { 712355b4669Sjacobs fatal(ofp, "Can't determine requested printer"); 713355b4669Sjacobs exit(1); 714355b4669Sjacobs } 715355b4669Sjacobs 7160a44ef6dSjacobs if (cyclical_service_check(printer) != 0) { 7170a44ef6dSjacobs fatal(ofp, "%s is cyclical\n", printer); 7180a44ef6dSjacobs exit(1); 7190a44ef6dSjacobs } 7200a44ef6dSjacobs 721355b4669Sjacobs status = papiServiceCreate(&svc, printer, NULL, NULL, NULL, 722355b4669Sjacobs encryption, NULL); 723355b4669Sjacobs if (status != PAPI_OK) { 724355b4669Sjacobs fatal(ofp, "Failed to contact service for %s: %s\n", printer, 725355b4669Sjacobs verbose_papi_message(svc, status)); 726355b4669Sjacobs exit(1); 727355b4669Sjacobs } 728355b4669Sjacobs 7290a44ef6dSjacobs /* 7300a44ef6dSjacobs * Trusted Solaris can't be trusting of intermediaries. Pass 7310a44ef6dSjacobs * the socket connection to the print service to retrieve the 7320a44ef6dSjacobs * sensativity label off of a multi-level port. 7330a44ef6dSjacobs */ 7340a44ef6dSjacobs (void) papiServiceSetPeer(svc, fileno(ifp)); 735355b4669Sjacobs 736355b4669Sjacobs switch (buf[0]) { 737355b4669Sjacobs case '\1': /* restart printer */ 738355b4669Sjacobs ACK(ofp); /* there is no equivalent */ 739355b4669Sjacobs break; 740355b4669Sjacobs case '\2': /* transfer job(s) */ 7410a44ef6dSjacobs status = berkeley_transfer_files(svc, ifp, ofp, printer); 742355b4669Sjacobs break; 743355b4669Sjacobs case '\3': /* show queue (short) */ 744355b4669Sjacobs case '\4': { /* show queue (long) */ 745355b4669Sjacobs int count; 746355b4669Sjacobs 747*dcf1b443Ssonam gupta - Sun Microsystems - Bangalore India for (count = 0; args[count] != 0; count++) {} 748355b4669Sjacobs 749355b4669Sjacobs berkeley_queue_report(svc, ofp, printer, buf[0], count, args); 750355b4669Sjacobs } 751355b4669Sjacobs break; 752355b4669Sjacobs case '\5': { /* cancel job(s) */ 7530a44ef6dSjacobs char *user = *args++; 7540a44ef6dSjacobs char *host = remote_host_name(ifp); 755355b4669Sjacobs int count; 756355b4669Sjacobs 7570a44ef6dSjacobs if (host != NULL) { 7580a44ef6dSjacobs char buf[BUFSIZ]; 7590a44ef6dSjacobs 7600a44ef6dSjacobs snprintf(buf, sizeof (buf), "%s@%s", user, host); 7610a44ef6dSjacobs status = papiServiceSetUserName(svc, buf); 7620a44ef6dSjacobs } else 7630a44ef6dSjacobs status = papiServiceSetUserName(svc, user); 7640a44ef6dSjacobs 765*dcf1b443Ssonam gupta - Sun Microsystems - Bangalore India for (count = 0; args[count] != 0; count++) {} 766355b4669Sjacobs 767355b4669Sjacobs berkeley_cancel_request(svc, ofp, printer, count, args); 768355b4669Sjacobs } 769355b4669Sjacobs break; 770355b4669Sjacobs default: 771355b4669Sjacobs fatal(ofp, "unsupported protocol request (%c), %s", 772355b4669Sjacobs buf[0], &buf[1]); 773355b4669Sjacobs } 774355b4669Sjacobs 775355b4669Sjacobs (void) fflush(ofp); 776355b4669Sjacobs 777355b4669Sjacobs syslog(LOG_DEBUG, "protocol request(%d) for %s completed: %s", 778355b4669Sjacobs buf[0], printer, papiStatusString(status)); 7790a44ef6dSjacobs if (status != PAPI_OK) 7800a44ef6dSjacobs syslog(LOG_DEBUG, "detail: %s", 7810a44ef6dSjacobs verbose_papi_message(svc, status)); 782355b4669Sjacobs 783355b4669Sjacobs papiServiceDestroy(svc); 784355b4669Sjacobs 785355b4669Sjacobs return (0); 786355b4669Sjacobs } 787