10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*7253Sjacobs * Common Development and Distribution License (the "License").
6*7253Sjacobs * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
210Sstevel@tonic-gate /*
22*7253Sjacobs * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
230Sstevel@tonic-gate * Use is subject to license terms.
240Sstevel@tonic-gate */
250Sstevel@tonic-gate
260Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
270Sstevel@tonic-gate
280Sstevel@tonic-gate #include <stdio.h>
290Sstevel@tonic-gate #include <stdlib.h>
300Sstevel@tonic-gate #include <libintl.h>
310Sstevel@tonic-gate #include <locale.h>
320Sstevel@tonic-gate #include <signal.h>
330Sstevel@tonic-gate #include <errno.h>
340Sstevel@tonic-gate #include <string.h>
350Sstevel@tonic-gate #include <unistd.h>
360Sstevel@tonic-gate #include <sys/mman.h>
370Sstevel@tonic-gate #include <sys/socket.h>
38*7253Sjacobs #include <netinet/in.h>
39*7253Sjacobs #include <arpa/inet.h>
40*7253Sjacobs #include <netdb.h>
410Sstevel@tonic-gate #include <fcntl.h>
420Sstevel@tonic-gate #include <syslog.h>
43*7253Sjacobs #include <sys/utsname.h>
440Sstevel@tonic-gate #include "netpr.h"
450Sstevel@tonic-gate
46*7253Sjacobs
470Sstevel@tonic-gate static void usage_exit();
480Sstevel@tonic-gate
490Sstevel@tonic-gate static void pipehandler(int);
500Sstevel@tonic-gate char data_file_type = 0;
510Sstevel@tonic-gate
52*7253Sjacobs /*
53*7253Sjacobs * null() is to be used as a signal handler that does nothing. It is used in
54*7253Sjacobs * place of SIG_IGN, because we want the signal to be delivered and
55*7253Sjacobs * interupt the current system call.
56*7253Sjacobs */
57*7253Sjacobs static void
null(int i)58*7253Sjacobs null(int i)
59*7253Sjacobs {
60*7253Sjacobs syslog(LOG_DEBUG, "null(%d)", i);
61*7253Sjacobs }
62*7253Sjacobs
63*7253Sjacobs /*
64*7253Sjacobs * net_open() opens a tcp connection to the printer port on the host specified
65*7253Sjacobs * in the arguments passed in. If the connection is not made in the
66*7253Sjacobs * timeout (in seconds) passed in, an error it returned. If the host is
67*7253Sjacobs * unknown, an error is returned. If all is well, a file descriptor is
68*7253Sjacobs * returned to be used for future communications.
69*7253Sjacobs */
70*7253Sjacobs int
net_open(char * host,int timeout)71*7253Sjacobs net_open(char *host, int timeout)
72*7253Sjacobs {
73*7253Sjacobs struct hostent *hp;
74*7253Sjacobs struct servent *sp;
75*7253Sjacobs struct sockaddr_in6 sin;
76*7253Sjacobs void (*old_handler)();
77*7253Sjacobs static struct utsname uts;
78*7253Sjacobs
79*7253Sjacobs int s,
80*7253Sjacobs lport,
81*7253Sjacobs err,
82*7253Sjacobs error_num;
83*7253Sjacobs unsigned timo = 1;
84*7253Sjacobs
85*7253Sjacobs syslog(LOG_DEBUG, "net_open(%s, %d)", (host != NULL ? host : "NULL"),
86*7253Sjacobs timeout);
87*7253Sjacobs /*
88*7253Sjacobs * Get the host address and port number to connect to.
89*7253Sjacobs */
90*7253Sjacobs if (host == NULL) {
91*7253Sjacobs return (-1);
92*7253Sjacobs }
93*7253Sjacobs
94*7253Sjacobs (void) memset((char *)&sin, NULL, sizeof (sin));
95*7253Sjacobs if ((hp = getipnodebyname(host, AF_INET6, AI_DEFAULT,
96*7253Sjacobs &error_num)) == NULL) {
97*7253Sjacobs syslog(LOG_DEBUG|LOG_ERR, "unknown host %s "
98*7253Sjacobs "getipnodebyname() returned %d", host, error_num);
99*7253Sjacobs return (NETWORK_ERROR_HOST);
100*7253Sjacobs }
101*7253Sjacobs (void) memcpy((caddr_t)&sin.sin6_addr, hp->h_addr, hp->h_length);
102*7253Sjacobs sin.sin6_family = hp->h_addrtype;
103*7253Sjacobs freehostent(hp);
104*7253Sjacobs
105*7253Sjacobs if ((sp = getservbyname("printer", "tcp")) == NULL) {
106*7253Sjacobs syslog(LOG_DEBUG|LOG_ERR, "printer/tcp: unknown service");
107*7253Sjacobs return (NETWORK_ERROR_SERVICE);
108*7253Sjacobs }
109*7253Sjacobs sin.sin6_port = sp->s_port;
110*7253Sjacobs
111*7253Sjacobs retry:
112*7253Sjacobs /*
113*7253Sjacobs * Try connecting to the server.
114*7253Sjacobs *
115*7253Sjacobs * Use 0 as lport means that rresvport_af() will bind to a port in
116*7253Sjacobs * the anonymous privileged port range.
117*7253Sjacobs */
118*7253Sjacobs lport = 0;
119*7253Sjacobs s = rresvport_af(&lport, AF_INET6);
120*7253Sjacobs if (s < 0)
121*7253Sjacobs return (NETWORK_ERROR_PORT);
122*7253Sjacobs
123*7253Sjacobs old_handler = signal(SIGALRM, null);
124*7253Sjacobs (void) alarm(timeout);
125*7253Sjacobs if (connect(s, (struct sockaddr *)&sin, sizeof (sin)) < 0) {
126*7253Sjacobs (void) alarm(0);
127*7253Sjacobs (void) signal(SIGALRM, old_handler);
128*7253Sjacobs err = errno;
129*7253Sjacobs (void) close(s);
130*7253Sjacobs errno = err;
131*7253Sjacobs if (errno == EADDRINUSE) {
132*7253Sjacobs goto retry;
133*7253Sjacobs }
134*7253Sjacobs /*
135*7253Sjacobs * If connecting to the local system fails, try
136*7253Sjacobs * again with "localhost" address instead.
137*7253Sjacobs */
138*7253Sjacobs if (uts.nodename[0] == '\0')
139*7253Sjacobs (void) uname(&uts);
140*7253Sjacobs if (strcmp(host, uts.nodename) == 0) {
141*7253Sjacobs IN6_IPADDR_TO_V4MAPPED(htonl(INADDR_LOOPBACK),
142*7253Sjacobs &sin.sin6_addr);
143*7253Sjacobs sin.sin6_family = AF_INET6;
144*7253Sjacobs goto retry;
145*7253Sjacobs }
146*7253Sjacobs if (errno == ECONNREFUSED && timo <= 16) {
147*7253Sjacobs (void) sleep(timo);
148*7253Sjacobs timo *= 2;
149*7253Sjacobs goto retry;
150*7253Sjacobs }
151*7253Sjacobs return (NETWORK_ERROR_UNKNOWN);
152*7253Sjacobs }
153*7253Sjacobs (void) alarm(0);
154*7253Sjacobs (void) signal(SIGALRM, old_handler);
155*7253Sjacobs return (s);
156*7253Sjacobs }
157*7253Sjacobs
158320Sceastha int
main(int argc,char * argv[])1590Sstevel@tonic-gate main(int argc, char *argv[])
1600Sstevel@tonic-gate {
1610Sstevel@tonic-gate extern char *optarg;
1620Sstevel@tonic-gate extern int optind;
1630Sstevel@tonic-gate int opt;
1640Sstevel@tonic-gate np_job_t *job_data;
1650Sstevel@tonic-gate char *destination = NULL;
1660Sstevel@tonic-gate np_bsdjob_t *bsdjob;
1670Sstevel@tonic-gate np_tcpjob_t *tcpjob;
1680Sstevel@tonic-gate int sockfd;
1690Sstevel@tonic-gate int pr_order = CONTROL_FIRST;
1700Sstevel@tonic-gate char *vendor_pr_name = NULL;
1710Sstevel@tonic-gate char *tcp_port = NULL;
1720Sstevel@tonic-gate size_t filesize;
1730Sstevel@tonic-gate int fd;
1740Sstevel@tonic-gate caddr_t pa;
1750Sstevel@tonic-gate int jobstatus;
1760Sstevel@tonic-gate int exit_status = 0;
1770Sstevel@tonic-gate int on = 1;
1780Sstevel@tonic-gate
1790Sstevel@tonic-gate
1800Sstevel@tonic-gate (void) setlocale(LC_ALL, "");
1810Sstevel@tonic-gate #if !defined(TEXT_DOMAIN) /* Should be defined by cc -D */
1820Sstevel@tonic-gate #define TEXT_DOMAIN "SYS_TEST" /* Use this only if it weren't */
1830Sstevel@tonic-gate #endif
1840Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN);
1850Sstevel@tonic-gate
1860Sstevel@tonic-gate openlog("netpr", LOG_PID, LOG_LPR);
1870Sstevel@tonic-gate (void) signal(SIGPIPE, pipehandler);
1880Sstevel@tonic-gate
1890Sstevel@tonic-gate /* reduce privileges until needed to open reserved port */
1900Sstevel@tonic-gate if (seteuid(getuid())) {
1910Sstevel@tonic-gate syslog(LOG_DEBUG, "seteuid failed, exiting netpr");
1920Sstevel@tonic-gate exit(E_FAILURE);
1930Sstevel@tonic-gate }
1940Sstevel@tonic-gate
1950Sstevel@tonic-gate if ((job_data = init_job()) == NULL) {
1960Sstevel@tonic-gate fprintf(stderr, gettext("init_job(): out of memory\n"));
1970Sstevel@tonic-gate exit(E_RETRY);
1980Sstevel@tonic-gate }
1990Sstevel@tonic-gate
2000Sstevel@tonic-gate while ((opt = getopt(argc, argv, "f:I:p:d:T:P:t:U:c:b")) != EOF)
2010Sstevel@tonic-gate switch (opt) {
2020Sstevel@tonic-gate case 'f':
2030Sstevel@tonic-gate data_file_type = optarg[0];
2040Sstevel@tonic-gate break;
2050Sstevel@tonic-gate case 'I': /* foo-49 */
2060Sstevel@tonic-gate job_data->request_id = alloc_str((char *)optarg);
2070Sstevel@tonic-gate syslog(LOG_DEBUG, "request_id: %s",
2080Sstevel@tonic-gate job_data->request_id);
2090Sstevel@tonic-gate break;
2100Sstevel@tonic-gate case 'U': /* awe172-126!wendyp */
2110Sstevel@tonic-gate job_data->username = alloc_str((char *)optarg);
2120Sstevel@tonic-gate syslog(LOG_DEBUG, "username: %s",
2130Sstevel@tonic-gate job_data->username);
2140Sstevel@tonic-gate break;
2150Sstevel@tonic-gate case 'p': /* foo */
2160Sstevel@tonic-gate job_data->printer = alloc_str((char *)optarg);
2170Sstevel@tonic-gate syslog(LOG_DEBUG, "printer: %s",
2180Sstevel@tonic-gate job_data->printer);
2190Sstevel@tonic-gate break;
2200Sstevel@tonic-gate case 'd': /* server for printer */
2210Sstevel@tonic-gate job_data->dest = alloc_str((char *)optarg);
2220Sstevel@tonic-gate syslog(LOG_DEBUG, "dest: %s",
2230Sstevel@tonic-gate job_data->dest);
2240Sstevel@tonic-gate break;
2250Sstevel@tonic-gate case 'T': /* /tmp/file2 */
2260Sstevel@tonic-gate job_data->title = alloc_str((char *)optarg);
2270Sstevel@tonic-gate syslog(LOG_DEBUG, "title: %s",
2280Sstevel@tonic-gate job_data->title);
2290Sstevel@tonic-gate break;
2300Sstevel@tonic-gate case 'P':
2310Sstevel@tonic-gate if ((strcmp(optarg, "bsd")) == 0)
2320Sstevel@tonic-gate job_data->protocol = BSD;
2330Sstevel@tonic-gate else if ((strcmp(optarg, "tcp")) == 0)
2340Sstevel@tonic-gate job_data->protocol = TCP;
2350Sstevel@tonic-gate else
2360Sstevel@tonic-gate usage_exit();
2370Sstevel@tonic-gate
2380Sstevel@tonic-gate syslog(LOG_DEBUG, "protocol: %d",
2390Sstevel@tonic-gate job_data->protocol);
2400Sstevel@tonic-gate break;
2410Sstevel@tonic-gate case 't':
2420Sstevel@tonic-gate job_data->timeout = atoi(optarg);
2430Sstevel@tonic-gate if (job_data->timeout < 0)
2440Sstevel@tonic-gate usage_exit();
2450Sstevel@tonic-gate break;
2460Sstevel@tonic-gate case 'c':
2470Sstevel@tonic-gate if ((strcmp(optarg, "first")) == 0)
2480Sstevel@tonic-gate pr_order = CONTROL_FIRST;
2490Sstevel@tonic-gate else if ((strcmp(optarg, "last")) == 0)
2500Sstevel@tonic-gate pr_order = DATA_FIRST;
2510Sstevel@tonic-gate else
2520Sstevel@tonic-gate usage_exit();
2530Sstevel@tonic-gate
2540Sstevel@tonic-gate syslog(LOG_DEBUG, "bsd print order: %d", pr_order);
2550Sstevel@tonic-gate break;
2560Sstevel@tonic-gate case 'b':
2570Sstevel@tonic-gate job_data->banner = NOBANNER;
2580Sstevel@tonic-gate syslog(LOG_DEBUG, "banner : %d",
2590Sstevel@tonic-gate job_data->banner);
2600Sstevel@tonic-gate break;
2610Sstevel@tonic-gate case '?':
2620Sstevel@tonic-gate usage_exit();
2630Sstevel@tonic-gate }
2640Sstevel@tonic-gate
2650Sstevel@tonic-gate
2660Sstevel@tonic-gate if ((job_data->dest == NULL) || (job_data->request_id == NULL) ||
2670Sstevel@tonic-gate (job_data->printer == NULL) || (job_data->username == NULL))
2680Sstevel@tonic-gate usage_exit();
2690Sstevel@tonic-gate
2700Sstevel@tonic-gate /*
2710Sstevel@tonic-gate * Check that there is a file
2720Sstevel@tonic-gate */
2730Sstevel@tonic-gate if (optind == argc) {
2740Sstevel@tonic-gate usage_exit();
2750Sstevel@tonic-gate }
2760Sstevel@tonic-gate
2770Sstevel@tonic-gate job_data->filename = alloc_str(argv[optind]);
2780Sstevel@tonic-gate syslog(LOG_DEBUG, "filename : %s", job_data->filename);
2790Sstevel@tonic-gate
2800Sstevel@tonic-gate
2810Sstevel@tonic-gate /*
2820Sstevel@tonic-gate * Sanity check the file
2830Sstevel@tonic-gate * returns filesize
2840Sstevel@tonic-gate */
2850Sstevel@tonic-gate
2860Sstevel@tonic-gate if ((filesize = check_file(job_data->filename)) == -1) {
2870Sstevel@tonic-gate syslog(LOG_DEBUG, "Skipping file %s",
2880Sstevel@tonic-gate job_data->filename ? job_data->filename : "Error NULL file");
2890Sstevel@tonic-gate
2900Sstevel@tonic-gate switch (errno) {
2910Sstevel@tonic-gate case EISDIR:
2920Sstevel@tonic-gate (void) fprintf(stderr,
2930Sstevel@tonic-gate gettext("Netpr: %s: Not a regular file\n"),
2940Sstevel@tonic-gate (job_data->filename ? job_data->filename : "Noname"));
2950Sstevel@tonic-gate syslog(LOG_DEBUG, "Not a regular file");
2960Sstevel@tonic-gate break;
2970Sstevel@tonic-gate case ESRCH:
2980Sstevel@tonic-gate (void) fprintf(stderr,
2990Sstevel@tonic-gate gettext("Netpr: %s: Empty file\n"),
3000Sstevel@tonic-gate (job_data->filename ? job_data->filename : "Noname"));
3010Sstevel@tonic-gate syslog(LOG_DEBUG, "Empty file");
3020Sstevel@tonic-gate break;
3030Sstevel@tonic-gate default:
3040Sstevel@tonic-gate perror(job_data->filename);
3050Sstevel@tonic-gate (void) fprintf(stderr,
3060Sstevel@tonic-gate gettext("Netpr: Cannot access file %s\n"),
3070Sstevel@tonic-gate (job_data->filename ? job_data->filename : "Noname"));
3080Sstevel@tonic-gate syslog(LOG_DEBUG, "Cannot access file.");
3090Sstevel@tonic-gate break;
3100Sstevel@tonic-gate
3110Sstevel@tonic-gate }
3120Sstevel@tonic-gate
3130Sstevel@tonic-gate /*
3140Sstevel@tonic-gate * This file not valid, so bail
3150Sstevel@tonic-gate * Exit with zero so system will keep printing
3160Sstevel@tonic-gate */
3170Sstevel@tonic-gate exit(0);
3180Sstevel@tonic-gate }
3190Sstevel@tonic-gate
3200Sstevel@tonic-gate /*
3210Sstevel@tonic-gate * file looks ok, open and mmap it
3220Sstevel@tonic-gate */
3230Sstevel@tonic-gate if ((fd = open(job_data->filename, O_RDONLY)) < 0) {
3240Sstevel@tonic-gate (void) fprintf(stderr, gettext("Netpr: Cannot open file %s\n"),
3250Sstevel@tonic-gate (job_data->filename ? job_data->filename : "Error: NULL file"));
3260Sstevel@tonic-gate syslog(LOG_DEBUG, "Cannot open file: %s",
3270Sstevel@tonic-gate job_data->filename ? job_data->filename : "Error NULL file");
3280Sstevel@tonic-gate exit(E_BAD_FILE);
3290Sstevel@tonic-gate }
3300Sstevel@tonic-gate
3310Sstevel@tonic-gate if ((pa = mmap((caddr_t)0, filesize, PROT_READ,
3320Sstevel@tonic-gate (MAP_SHARED | MAP_NORESERVE), fd, (off_t)0)) == MAP_FAILED) {
3330Sstevel@tonic-gate
3340Sstevel@tonic-gate (void) close(fd);
3350Sstevel@tonic-gate (void) fprintf(stderr, gettext("Netpr: Cannot mmap file %s"),
3360Sstevel@tonic-gate (job_data->filename ? job_data->filename : "Error: NULL file"));
3370Sstevel@tonic-gate
3380Sstevel@tonic-gate syslog(LOG_DEBUG, "Cannot mmap file: %s",
3390Sstevel@tonic-gate job_data->filename ? job_data->filename : "Error NULL file");
3400Sstevel@tonic-gate
3410Sstevel@tonic-gate exit(E_RETRY);
3420Sstevel@tonic-gate }
3430Sstevel@tonic-gate
3440Sstevel@tonic-gate
3450Sstevel@tonic-gate if (job_data->protocol == BSD) {
3460Sstevel@tonic-gate bsdjob = (np_bsdjob_t *)
3470Sstevel@tonic-gate create_bsd_job(job_data, pr_order, filesize);
3480Sstevel@tonic-gate if (bsdjob == NULL)
3490Sstevel@tonic-gate exit(E_FAILURE);
3500Sstevel@tonic-gate } else {
3510Sstevel@tonic-gate tcpjob = (np_tcpjob_t *)create_tcp_job(job_data, filesize);
3520Sstevel@tonic-gate if (tcpjob == NULL)
3530Sstevel@tonic-gate exit(E_FAILURE);
3540Sstevel@tonic-gate }
3550Sstevel@tonic-gate
3560Sstevel@tonic-gate /*
3570Sstevel@tonic-gate * Parse destination
3580Sstevel@tonic-gate */
3590Sstevel@tonic-gate
3600Sstevel@tonic-gate if ((strpbrk(job_data->dest, DEST_SEP)) != NULL) {
3610Sstevel@tonic-gate if (job_data->protocol == BSD) {
3620Sstevel@tonic-gate parse_dest(job_data->dest, &destination,
3630Sstevel@tonic-gate &vendor_pr_name, DEST_SEP);
3640Sstevel@tonic-gate if (vendor_pr_name != NULL) {
3650Sstevel@tonic-gate bsdjob->np_printer = vendor_pr_name;
3660Sstevel@tonic-gate syslog(LOG_DEBUG, "bsd vendor name: %s",
3670Sstevel@tonic-gate bsdjob->np_printer);
3680Sstevel@tonic-gate }
3690Sstevel@tonic-gate } else {
3700Sstevel@tonic-gate parse_dest(job_data->dest, &destination, &tcp_port,
3710Sstevel@tonic-gate DEST_SEP);
3720Sstevel@tonic-gate if (tcp_port != NULL)
3730Sstevel@tonic-gate tcpjob->np_port = tcp_port;
3740Sstevel@tonic-gate syslog(LOG_DEBUG, "tcp_port %s",
3750Sstevel@tonic-gate tcpjob->np_port);
3760Sstevel@tonic-gate }
3770Sstevel@tonic-gate if (destination == NULL ||
3780Sstevel@tonic-gate (job_data->protocol == TCP && tcp_port == NULL)) {
3790Sstevel@tonic-gate (void) fprintf(stderr,
3800Sstevel@tonic-gate gettext("Netpr: system error parsing destination %s\n"),
3810Sstevel@tonic-gate job_data->dest);
3820Sstevel@tonic-gate syslog(LOG_DEBUG, "system error parsing destination %s",
3830Sstevel@tonic-gate job_data->dest);
3840Sstevel@tonic-gate
3850Sstevel@tonic-gate exit(E_FAILURE);
3860Sstevel@tonic-gate }
3870Sstevel@tonic-gate
3880Sstevel@tonic-gate } else {
3890Sstevel@tonic-gate destination = job_data->dest;
3900Sstevel@tonic-gate }
3910Sstevel@tonic-gate syslog(LOG_DEBUG, "destination : %s", destination);
3920Sstevel@tonic-gate
3930Sstevel@tonic-gate /*
3940Sstevel@tonic-gate * We are now ready to open a connection to the printer
3950Sstevel@tonic-gate * and print each of the files
3960Sstevel@tonic-gate */
3970Sstevel@tonic-gate
3980Sstevel@tonic-gate if (job_data->protocol == BSD) {
3990Sstevel@tonic-gate
4000Sstevel@tonic-gate /* set privileges to get reserved port */
4010Sstevel@tonic-gate if (seteuid(0)) {
4020Sstevel@tonic-gate syslog(LOG_DEBUG, "seteuid(0) failed, exiting netpr");
4030Sstevel@tonic-gate exit(E_FAILURE);
4040Sstevel@tonic-gate }
4050Sstevel@tonic-gate if ((sockfd = net_open(destination, 20)) < 0) {
4060Sstevel@tonic-gate (void) fprintf(stderr,
4070Sstevel@tonic-gate gettext("Netpr: Cannot open connection to <%s>\n"),
4080Sstevel@tonic-gate destination);
4090Sstevel@tonic-gate syslog(LOG_DEBUG,
4100Sstevel@tonic-gate "Cannot open connection to %s: retrying",
4110Sstevel@tonic-gate destination);
4120Sstevel@tonic-gate exit(E_RETRY);
4130Sstevel@tonic-gate }
4140Sstevel@tonic-gate } else {
4150Sstevel@tonic-gate if ((sockfd = tcp_open(destination, tcpjob, 20)) == -1) {
4160Sstevel@tonic-gate exit(E_RETRY);
4170Sstevel@tonic-gate }
4180Sstevel@tonic-gate }
4190Sstevel@tonic-gate
4200Sstevel@tonic-gate /* lower privileges as we now have the reserved port */
4210Sstevel@tonic-gate if (setuid(getuid())) {
4220Sstevel@tonic-gate syslog(LOG_DEBUG, "setuid() failed, exiting netpr");
4230Sstevel@tonic-gate exit(E_FAILURE);
4240Sstevel@tonic-gate }
4250Sstevel@tonic-gate
4260Sstevel@tonic-gate
4270Sstevel@tonic-gate /* Set SO_KEEPALIVE on socket to keep open */
4280Sstevel@tonic-gate if ((setsockopt(sockfd, SOL_SOCKET, SO_KEEPALIVE,
4290Sstevel@tonic-gate (char *)&on, sizeof (on))) < 0) {
4300Sstevel@tonic-gate syslog(LOG_DEBUG, "setsocket (SO_KEEPALIVE): %m");
4310Sstevel@tonic-gate }
4320Sstevel@tonic-gate
4330Sstevel@tonic-gate if (job_data->protocol == BSD) {
4340Sstevel@tonic-gate if ((jobstatus = bsd_print(sockfd, pa, bsdjob)) != 0) {
4350Sstevel@tonic-gate (void) fprintf(stderr,
4360Sstevel@tonic-gate gettext("Netpr: Error return from bsd_print <%d>\n"),
4370Sstevel@tonic-gate jobstatus);
4380Sstevel@tonic-gate syslog(LOG_DEBUG,
4390Sstevel@tonic-gate "Error return from bsd_print <%d>", jobstatus);
4400Sstevel@tonic-gate exit_status = E_RETRY;
4410Sstevel@tonic-gate }
4420Sstevel@tonic-gate } else {
4430Sstevel@tonic-gate if ((jobstatus =
4440Sstevel@tonic-gate tcp_print(sockfd, pa, tcpjob)) != 0) {
4450Sstevel@tonic-gate (void) fprintf(stderr,
4460Sstevel@tonic-gate gettext("Netpr: Error return from tcp_print <%d>\n"),
4470Sstevel@tonic-gate jobstatus);
4480Sstevel@tonic-gate syslog(LOG_DEBUG,
4490Sstevel@tonic-gate "Error return from tcp_print <%d>", jobstatus);
4500Sstevel@tonic-gate exit_status = E_RETRY;
4510Sstevel@tonic-gate }
4520Sstevel@tonic-gate }
4530Sstevel@tonic-gate
4540Sstevel@tonic-gate (void) close(fd);
4550Sstevel@tonic-gate (void) close(sockfd);
4560Sstevel@tonic-gate (void) munmap(pa, filesize);
4570Sstevel@tonic-gate
4580Sstevel@tonic-gate syslog(LOG_DEBUG, "exit status: %d", exit_status);
4590Sstevel@tonic-gate return (exit_status);
4600Sstevel@tonic-gate }
4610Sstevel@tonic-gate
4620Sstevel@tonic-gate static void
usage_exit()4630Sstevel@tonic-gate usage_exit()
4640Sstevel@tonic-gate {
4650Sstevel@tonic-gate (void) fprintf(stderr,
4660Sstevel@tonic-gate gettext("Usage: netpr -I request_id -p printer -d destination\n"));
4670Sstevel@tonic-gate (void) fprintf(stderr,
4680Sstevel@tonic-gate gettext("\t\t-U username [ -f type ] [ -T title ] [ -P protocol ]\n"));
4690Sstevel@tonic-gate (void) fprintf(stderr,
4700Sstevel@tonic-gate gettext("\t\t[-t timeout] [ -c ] [ -b ]\n"));
4710Sstevel@tonic-gate (void) fprintf(stderr, gettext("\t\tfiles\n"));
4720Sstevel@tonic-gate exit(E_BAD_INPUT);
4730Sstevel@tonic-gate }
4740Sstevel@tonic-gate
4750Sstevel@tonic-gate /*ARGSUSED*/
4760Sstevel@tonic-gate void
pipehandler(int i)4770Sstevel@tonic-gate pipehandler(int i)
4780Sstevel@tonic-gate {
4790Sstevel@tonic-gate (void) signal(SIGPIPE, pipehandler);
4800Sstevel@tonic-gate syslog(LOG_DEBUG, "Received SIGPIPE, connection to printer broken");
4810Sstevel@tonic-gate exit(E_SIGPIPE);
4820Sstevel@tonic-gate }
483