17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*c1ecd8b9Sjacobs * Common Development and Distribution License (the "License").
6*c1ecd8b9Sjacobs * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate *
87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate * and limitations under the License.
127c478bd9Sstevel@tonic-gate *
137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate *
197c478bd9Sstevel@tonic-gate * CDDL HEADER END
207c478bd9Sstevel@tonic-gate */
217c478bd9Sstevel@tonic-gate /*
22*c1ecd8b9Sjacobs * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
237c478bd9Sstevel@tonic-gate * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate */
257c478bd9Sstevel@tonic-gate
267c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
277c478bd9Sstevel@tonic-gate
287c478bd9Sstevel@tonic-gate #include <stdio.h>
297c478bd9Sstevel@tonic-gate #include <stdlib.h>
307c478bd9Sstevel@tonic-gate #include <libintl.h>
317c478bd9Sstevel@tonic-gate #include <locale.h>
327c478bd9Sstevel@tonic-gate #include <signal.h>
337c478bd9Sstevel@tonic-gate #include <errno.h>
347c478bd9Sstevel@tonic-gate #include <string.h>
357c478bd9Sstevel@tonic-gate #include <unistd.h>
367c478bd9Sstevel@tonic-gate #include <sys/mman.h>
377c478bd9Sstevel@tonic-gate #include <sys/socket.h>
38*c1ecd8b9Sjacobs #include <netinet/in.h>
39*c1ecd8b9Sjacobs #include <arpa/inet.h>
40*c1ecd8b9Sjacobs #include <netdb.h>
417c478bd9Sstevel@tonic-gate #include <fcntl.h>
427c478bd9Sstevel@tonic-gate #include <syslog.h>
43*c1ecd8b9Sjacobs #include <sys/utsname.h>
447c478bd9Sstevel@tonic-gate #include "netpr.h"
457c478bd9Sstevel@tonic-gate
46*c1ecd8b9Sjacobs
477c478bd9Sstevel@tonic-gate static void usage_exit();
487c478bd9Sstevel@tonic-gate
497c478bd9Sstevel@tonic-gate static void pipehandler(int);
507c478bd9Sstevel@tonic-gate char data_file_type = 0;
517c478bd9Sstevel@tonic-gate
52*c1ecd8b9Sjacobs /*
53*c1ecd8b9Sjacobs * null() is to be used as a signal handler that does nothing. It is used in
54*c1ecd8b9Sjacobs * place of SIG_IGN, because we want the signal to be delivered and
55*c1ecd8b9Sjacobs * interupt the current system call.
56*c1ecd8b9Sjacobs */
57*c1ecd8b9Sjacobs static void
null(int i)58*c1ecd8b9Sjacobs null(int i)
59*c1ecd8b9Sjacobs {
60*c1ecd8b9Sjacobs syslog(LOG_DEBUG, "null(%d)", i);
61*c1ecd8b9Sjacobs }
62*c1ecd8b9Sjacobs
63*c1ecd8b9Sjacobs /*
64*c1ecd8b9Sjacobs * net_open() opens a tcp connection to the printer port on the host specified
65*c1ecd8b9Sjacobs * in the arguments passed in. If the connection is not made in the
66*c1ecd8b9Sjacobs * timeout (in seconds) passed in, an error it returned. If the host is
67*c1ecd8b9Sjacobs * unknown, an error is returned. If all is well, a file descriptor is
68*c1ecd8b9Sjacobs * returned to be used for future communications.
69*c1ecd8b9Sjacobs */
70*c1ecd8b9Sjacobs int
net_open(char * host,int timeout)71*c1ecd8b9Sjacobs net_open(char *host, int timeout)
72*c1ecd8b9Sjacobs {
73*c1ecd8b9Sjacobs struct hostent *hp;
74*c1ecd8b9Sjacobs struct servent *sp;
75*c1ecd8b9Sjacobs struct sockaddr_in6 sin;
76*c1ecd8b9Sjacobs void (*old_handler)();
77*c1ecd8b9Sjacobs static struct utsname uts;
78*c1ecd8b9Sjacobs
79*c1ecd8b9Sjacobs int s,
80*c1ecd8b9Sjacobs lport,
81*c1ecd8b9Sjacobs err,
82*c1ecd8b9Sjacobs error_num;
83*c1ecd8b9Sjacobs unsigned timo = 1;
84*c1ecd8b9Sjacobs
85*c1ecd8b9Sjacobs syslog(LOG_DEBUG, "net_open(%s, %d)", (host != NULL ? host : "NULL"),
86*c1ecd8b9Sjacobs timeout);
87*c1ecd8b9Sjacobs /*
88*c1ecd8b9Sjacobs * Get the host address and port number to connect to.
89*c1ecd8b9Sjacobs */
90*c1ecd8b9Sjacobs if (host == NULL) {
91*c1ecd8b9Sjacobs return (-1);
92*c1ecd8b9Sjacobs }
93*c1ecd8b9Sjacobs
94*c1ecd8b9Sjacobs (void) memset((char *)&sin, NULL, sizeof (sin));
95*c1ecd8b9Sjacobs if ((hp = getipnodebyname(host, AF_INET6, AI_DEFAULT,
96*c1ecd8b9Sjacobs &error_num)) == NULL) {
97*c1ecd8b9Sjacobs syslog(LOG_DEBUG|LOG_ERR, "unknown host %s "
98*c1ecd8b9Sjacobs "getipnodebyname() returned %d", host, error_num);
99*c1ecd8b9Sjacobs return (NETWORK_ERROR_HOST);
100*c1ecd8b9Sjacobs }
101*c1ecd8b9Sjacobs (void) memcpy((caddr_t)&sin.sin6_addr, hp->h_addr, hp->h_length);
102*c1ecd8b9Sjacobs sin.sin6_family = hp->h_addrtype;
103*c1ecd8b9Sjacobs freehostent(hp);
104*c1ecd8b9Sjacobs
105*c1ecd8b9Sjacobs if ((sp = getservbyname("printer", "tcp")) == NULL) {
106*c1ecd8b9Sjacobs syslog(LOG_DEBUG|LOG_ERR, "printer/tcp: unknown service");
107*c1ecd8b9Sjacobs return (NETWORK_ERROR_SERVICE);
108*c1ecd8b9Sjacobs }
109*c1ecd8b9Sjacobs sin.sin6_port = sp->s_port;
110*c1ecd8b9Sjacobs
111*c1ecd8b9Sjacobs retry:
112*c1ecd8b9Sjacobs /*
113*c1ecd8b9Sjacobs * Try connecting to the server.
114*c1ecd8b9Sjacobs *
115*c1ecd8b9Sjacobs * Use 0 as lport means that rresvport_af() will bind to a port in
116*c1ecd8b9Sjacobs * the anonymous privileged port range.
117*c1ecd8b9Sjacobs */
118*c1ecd8b9Sjacobs lport = 0;
119*c1ecd8b9Sjacobs s = rresvport_af(&lport, AF_INET6);
120*c1ecd8b9Sjacobs if (s < 0)
121*c1ecd8b9Sjacobs return (NETWORK_ERROR_PORT);
122*c1ecd8b9Sjacobs
123*c1ecd8b9Sjacobs old_handler = signal(SIGALRM, null);
124*c1ecd8b9Sjacobs (void) alarm(timeout);
125*c1ecd8b9Sjacobs if (connect(s, (struct sockaddr *)&sin, sizeof (sin)) < 0) {
126*c1ecd8b9Sjacobs (void) alarm(0);
127*c1ecd8b9Sjacobs (void) signal(SIGALRM, old_handler);
128*c1ecd8b9Sjacobs err = errno;
129*c1ecd8b9Sjacobs (void) close(s);
130*c1ecd8b9Sjacobs errno = err;
131*c1ecd8b9Sjacobs if (errno == EADDRINUSE) {
132*c1ecd8b9Sjacobs goto retry;
133*c1ecd8b9Sjacobs }
134*c1ecd8b9Sjacobs /*
135*c1ecd8b9Sjacobs * If connecting to the local system fails, try
136*c1ecd8b9Sjacobs * again with "localhost" address instead.
137*c1ecd8b9Sjacobs */
138*c1ecd8b9Sjacobs if (uts.nodename[0] == '\0')
139*c1ecd8b9Sjacobs (void) uname(&uts);
140*c1ecd8b9Sjacobs if (strcmp(host, uts.nodename) == 0) {
141*c1ecd8b9Sjacobs IN6_IPADDR_TO_V4MAPPED(htonl(INADDR_LOOPBACK),
142*c1ecd8b9Sjacobs &sin.sin6_addr);
143*c1ecd8b9Sjacobs sin.sin6_family = AF_INET6;
144*c1ecd8b9Sjacobs goto retry;
145*c1ecd8b9Sjacobs }
146*c1ecd8b9Sjacobs if (errno == ECONNREFUSED && timo <= 16) {
147*c1ecd8b9Sjacobs (void) sleep(timo);
148*c1ecd8b9Sjacobs timo *= 2;
149*c1ecd8b9Sjacobs goto retry;
150*c1ecd8b9Sjacobs }
151*c1ecd8b9Sjacobs return (NETWORK_ERROR_UNKNOWN);
152*c1ecd8b9Sjacobs }
153*c1ecd8b9Sjacobs (void) alarm(0);
154*c1ecd8b9Sjacobs (void) signal(SIGALRM, old_handler);
155*c1ecd8b9Sjacobs return (s);
156*c1ecd8b9Sjacobs }
157*c1ecd8b9Sjacobs
158f928ce67Sceastha int
main(int argc,char * argv[])1597c478bd9Sstevel@tonic-gate main(int argc, char *argv[])
1607c478bd9Sstevel@tonic-gate {
1617c478bd9Sstevel@tonic-gate extern char *optarg;
1627c478bd9Sstevel@tonic-gate extern int optind;
1637c478bd9Sstevel@tonic-gate int opt;
1647c478bd9Sstevel@tonic-gate np_job_t *job_data;
1657c478bd9Sstevel@tonic-gate char *destination = NULL;
1667c478bd9Sstevel@tonic-gate np_bsdjob_t *bsdjob;
1677c478bd9Sstevel@tonic-gate np_tcpjob_t *tcpjob;
1687c478bd9Sstevel@tonic-gate int sockfd;
1697c478bd9Sstevel@tonic-gate int pr_order = CONTROL_FIRST;
1707c478bd9Sstevel@tonic-gate char *vendor_pr_name = NULL;
1717c478bd9Sstevel@tonic-gate char *tcp_port = NULL;
1727c478bd9Sstevel@tonic-gate size_t filesize;
1737c478bd9Sstevel@tonic-gate int fd;
1747c478bd9Sstevel@tonic-gate caddr_t pa;
1757c478bd9Sstevel@tonic-gate int jobstatus;
1767c478bd9Sstevel@tonic-gate int exit_status = 0;
1777c478bd9Sstevel@tonic-gate int on = 1;
1787c478bd9Sstevel@tonic-gate
1797c478bd9Sstevel@tonic-gate
1807c478bd9Sstevel@tonic-gate (void) setlocale(LC_ALL, "");
1817c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN) /* Should be defined by cc -D */
1827c478bd9Sstevel@tonic-gate #define TEXT_DOMAIN "SYS_TEST" /* Use this only if it weren't */
1837c478bd9Sstevel@tonic-gate #endif
1847c478bd9Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN);
1857c478bd9Sstevel@tonic-gate
1867c478bd9Sstevel@tonic-gate openlog("netpr", LOG_PID, LOG_LPR);
1877c478bd9Sstevel@tonic-gate (void) signal(SIGPIPE, pipehandler);
1887c478bd9Sstevel@tonic-gate
1897c478bd9Sstevel@tonic-gate /* reduce privileges until needed to open reserved port */
1907c478bd9Sstevel@tonic-gate if (seteuid(getuid())) {
1917c478bd9Sstevel@tonic-gate syslog(LOG_DEBUG, "seteuid failed, exiting netpr");
1927c478bd9Sstevel@tonic-gate exit(E_FAILURE);
1937c478bd9Sstevel@tonic-gate }
1947c478bd9Sstevel@tonic-gate
1957c478bd9Sstevel@tonic-gate if ((job_data = init_job()) == NULL) {
1967c478bd9Sstevel@tonic-gate fprintf(stderr, gettext("init_job(): out of memory\n"));
1977c478bd9Sstevel@tonic-gate exit(E_RETRY);
1987c478bd9Sstevel@tonic-gate }
1997c478bd9Sstevel@tonic-gate
2007c478bd9Sstevel@tonic-gate while ((opt = getopt(argc, argv, "f:I:p:d:T:P:t:U:c:b")) != EOF)
2017c478bd9Sstevel@tonic-gate switch (opt) {
2027c478bd9Sstevel@tonic-gate case 'f':
2037c478bd9Sstevel@tonic-gate data_file_type = optarg[0];
2047c478bd9Sstevel@tonic-gate break;
2057c478bd9Sstevel@tonic-gate case 'I': /* foo-49 */
2067c478bd9Sstevel@tonic-gate job_data->request_id = alloc_str((char *)optarg);
2077c478bd9Sstevel@tonic-gate syslog(LOG_DEBUG, "request_id: %s",
2087c478bd9Sstevel@tonic-gate job_data->request_id);
2097c478bd9Sstevel@tonic-gate break;
2107c478bd9Sstevel@tonic-gate case 'U': /* awe172-126!wendyp */
2117c478bd9Sstevel@tonic-gate job_data->username = alloc_str((char *)optarg);
2127c478bd9Sstevel@tonic-gate syslog(LOG_DEBUG, "username: %s",
2137c478bd9Sstevel@tonic-gate job_data->username);
2147c478bd9Sstevel@tonic-gate break;
2157c478bd9Sstevel@tonic-gate case 'p': /* foo */
2167c478bd9Sstevel@tonic-gate job_data->printer = alloc_str((char *)optarg);
2177c478bd9Sstevel@tonic-gate syslog(LOG_DEBUG, "printer: %s",
2187c478bd9Sstevel@tonic-gate job_data->printer);
2197c478bd9Sstevel@tonic-gate break;
2207c478bd9Sstevel@tonic-gate case 'd': /* server for printer */
2217c478bd9Sstevel@tonic-gate job_data->dest = alloc_str((char *)optarg);
2227c478bd9Sstevel@tonic-gate syslog(LOG_DEBUG, "dest: %s",
2237c478bd9Sstevel@tonic-gate job_data->dest);
2247c478bd9Sstevel@tonic-gate break;
2257c478bd9Sstevel@tonic-gate case 'T': /* /tmp/file2 */
2267c478bd9Sstevel@tonic-gate job_data->title = alloc_str((char *)optarg);
2277c478bd9Sstevel@tonic-gate syslog(LOG_DEBUG, "title: %s",
2287c478bd9Sstevel@tonic-gate job_data->title);
2297c478bd9Sstevel@tonic-gate break;
2307c478bd9Sstevel@tonic-gate case 'P':
2317c478bd9Sstevel@tonic-gate if ((strcmp(optarg, "bsd")) == 0)
2327c478bd9Sstevel@tonic-gate job_data->protocol = BSD;
2337c478bd9Sstevel@tonic-gate else if ((strcmp(optarg, "tcp")) == 0)
2347c478bd9Sstevel@tonic-gate job_data->protocol = TCP;
2357c478bd9Sstevel@tonic-gate else
2367c478bd9Sstevel@tonic-gate usage_exit();
2377c478bd9Sstevel@tonic-gate
2387c478bd9Sstevel@tonic-gate syslog(LOG_DEBUG, "protocol: %d",
2397c478bd9Sstevel@tonic-gate job_data->protocol);
2407c478bd9Sstevel@tonic-gate break;
2417c478bd9Sstevel@tonic-gate case 't':
2427c478bd9Sstevel@tonic-gate job_data->timeout = atoi(optarg);
2437c478bd9Sstevel@tonic-gate if (job_data->timeout < 0)
2447c478bd9Sstevel@tonic-gate usage_exit();
2457c478bd9Sstevel@tonic-gate break;
2467c478bd9Sstevel@tonic-gate case 'c':
2477c478bd9Sstevel@tonic-gate if ((strcmp(optarg, "first")) == 0)
2487c478bd9Sstevel@tonic-gate pr_order = CONTROL_FIRST;
2497c478bd9Sstevel@tonic-gate else if ((strcmp(optarg, "last")) == 0)
2507c478bd9Sstevel@tonic-gate pr_order = DATA_FIRST;
2517c478bd9Sstevel@tonic-gate else
2527c478bd9Sstevel@tonic-gate usage_exit();
2537c478bd9Sstevel@tonic-gate
2547c478bd9Sstevel@tonic-gate syslog(LOG_DEBUG, "bsd print order: %d", pr_order);
2557c478bd9Sstevel@tonic-gate break;
2567c478bd9Sstevel@tonic-gate case 'b':
2577c478bd9Sstevel@tonic-gate job_data->banner = NOBANNER;
2587c478bd9Sstevel@tonic-gate syslog(LOG_DEBUG, "banner : %d",
2597c478bd9Sstevel@tonic-gate job_data->banner);
2607c478bd9Sstevel@tonic-gate break;
2617c478bd9Sstevel@tonic-gate case '?':
2627c478bd9Sstevel@tonic-gate usage_exit();
2637c478bd9Sstevel@tonic-gate }
2647c478bd9Sstevel@tonic-gate
2657c478bd9Sstevel@tonic-gate
2667c478bd9Sstevel@tonic-gate if ((job_data->dest == NULL) || (job_data->request_id == NULL) ||
2677c478bd9Sstevel@tonic-gate (job_data->printer == NULL) || (job_data->username == NULL))
2687c478bd9Sstevel@tonic-gate usage_exit();
2697c478bd9Sstevel@tonic-gate
2707c478bd9Sstevel@tonic-gate /*
2717c478bd9Sstevel@tonic-gate * Check that there is a file
2727c478bd9Sstevel@tonic-gate */
2737c478bd9Sstevel@tonic-gate if (optind == argc) {
2747c478bd9Sstevel@tonic-gate usage_exit();
2757c478bd9Sstevel@tonic-gate }
2767c478bd9Sstevel@tonic-gate
2777c478bd9Sstevel@tonic-gate job_data->filename = alloc_str(argv[optind]);
2787c478bd9Sstevel@tonic-gate syslog(LOG_DEBUG, "filename : %s", job_data->filename);
2797c478bd9Sstevel@tonic-gate
2807c478bd9Sstevel@tonic-gate
2817c478bd9Sstevel@tonic-gate /*
2827c478bd9Sstevel@tonic-gate * Sanity check the file
2837c478bd9Sstevel@tonic-gate * returns filesize
2847c478bd9Sstevel@tonic-gate */
2857c478bd9Sstevel@tonic-gate
2867c478bd9Sstevel@tonic-gate if ((filesize = check_file(job_data->filename)) == -1) {
2877c478bd9Sstevel@tonic-gate syslog(LOG_DEBUG, "Skipping file %s",
2887c478bd9Sstevel@tonic-gate job_data->filename ? job_data->filename : "Error NULL file");
2897c478bd9Sstevel@tonic-gate
2907c478bd9Sstevel@tonic-gate switch (errno) {
2917c478bd9Sstevel@tonic-gate case EISDIR:
2927c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
2937c478bd9Sstevel@tonic-gate gettext("Netpr: %s: Not a regular file\n"),
2947c478bd9Sstevel@tonic-gate (job_data->filename ? job_data->filename : "Noname"));
2957c478bd9Sstevel@tonic-gate syslog(LOG_DEBUG, "Not a regular file");
2967c478bd9Sstevel@tonic-gate break;
2977c478bd9Sstevel@tonic-gate case ESRCH:
2987c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
2997c478bd9Sstevel@tonic-gate gettext("Netpr: %s: Empty file\n"),
3007c478bd9Sstevel@tonic-gate (job_data->filename ? job_data->filename : "Noname"));
3017c478bd9Sstevel@tonic-gate syslog(LOG_DEBUG, "Empty file");
3027c478bd9Sstevel@tonic-gate break;
3037c478bd9Sstevel@tonic-gate default:
3047c478bd9Sstevel@tonic-gate perror(job_data->filename);
3057c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
3067c478bd9Sstevel@tonic-gate gettext("Netpr: Cannot access file %s\n"),
3077c478bd9Sstevel@tonic-gate (job_data->filename ? job_data->filename : "Noname"));
3087c478bd9Sstevel@tonic-gate syslog(LOG_DEBUG, "Cannot access file.");
3097c478bd9Sstevel@tonic-gate break;
3107c478bd9Sstevel@tonic-gate
3117c478bd9Sstevel@tonic-gate }
3127c478bd9Sstevel@tonic-gate
3137c478bd9Sstevel@tonic-gate /*
3147c478bd9Sstevel@tonic-gate * This file not valid, so bail
3157c478bd9Sstevel@tonic-gate * Exit with zero so system will keep printing
3167c478bd9Sstevel@tonic-gate */
3177c478bd9Sstevel@tonic-gate exit(0);
3187c478bd9Sstevel@tonic-gate }
3197c478bd9Sstevel@tonic-gate
3207c478bd9Sstevel@tonic-gate /*
3217c478bd9Sstevel@tonic-gate * file looks ok, open and mmap it
3227c478bd9Sstevel@tonic-gate */
3237c478bd9Sstevel@tonic-gate if ((fd = open(job_data->filename, O_RDONLY)) < 0) {
3247c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("Netpr: Cannot open file %s\n"),
3257c478bd9Sstevel@tonic-gate (job_data->filename ? job_data->filename : "Error: NULL file"));
3267c478bd9Sstevel@tonic-gate syslog(LOG_DEBUG, "Cannot open file: %s",
3277c478bd9Sstevel@tonic-gate job_data->filename ? job_data->filename : "Error NULL file");
3287c478bd9Sstevel@tonic-gate exit(E_BAD_FILE);
3297c478bd9Sstevel@tonic-gate }
3307c478bd9Sstevel@tonic-gate
3317c478bd9Sstevel@tonic-gate if ((pa = mmap((caddr_t)0, filesize, PROT_READ,
3327c478bd9Sstevel@tonic-gate (MAP_SHARED | MAP_NORESERVE), fd, (off_t)0)) == MAP_FAILED) {
3337c478bd9Sstevel@tonic-gate
3347c478bd9Sstevel@tonic-gate (void) close(fd);
3357c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("Netpr: Cannot mmap file %s"),
3367c478bd9Sstevel@tonic-gate (job_data->filename ? job_data->filename : "Error: NULL file"));
3377c478bd9Sstevel@tonic-gate
3387c478bd9Sstevel@tonic-gate syslog(LOG_DEBUG, "Cannot mmap file: %s",
3397c478bd9Sstevel@tonic-gate job_data->filename ? job_data->filename : "Error NULL file");
3407c478bd9Sstevel@tonic-gate
3417c478bd9Sstevel@tonic-gate exit(E_RETRY);
3427c478bd9Sstevel@tonic-gate }
3437c478bd9Sstevel@tonic-gate
3447c478bd9Sstevel@tonic-gate
3457c478bd9Sstevel@tonic-gate if (job_data->protocol == BSD) {
3467c478bd9Sstevel@tonic-gate bsdjob = (np_bsdjob_t *)
3477c478bd9Sstevel@tonic-gate create_bsd_job(job_data, pr_order, filesize);
3487c478bd9Sstevel@tonic-gate if (bsdjob == NULL)
3497c478bd9Sstevel@tonic-gate exit(E_FAILURE);
3507c478bd9Sstevel@tonic-gate } else {
3517c478bd9Sstevel@tonic-gate tcpjob = (np_tcpjob_t *)create_tcp_job(job_data, filesize);
3527c478bd9Sstevel@tonic-gate if (tcpjob == NULL)
3537c478bd9Sstevel@tonic-gate exit(E_FAILURE);
3547c478bd9Sstevel@tonic-gate }
3557c478bd9Sstevel@tonic-gate
3567c478bd9Sstevel@tonic-gate /*
3577c478bd9Sstevel@tonic-gate * Parse destination
3587c478bd9Sstevel@tonic-gate */
3597c478bd9Sstevel@tonic-gate
3607c478bd9Sstevel@tonic-gate if ((strpbrk(job_data->dest, DEST_SEP)) != NULL) {
3617c478bd9Sstevel@tonic-gate if (job_data->protocol == BSD) {
3627c478bd9Sstevel@tonic-gate parse_dest(job_data->dest, &destination,
3637c478bd9Sstevel@tonic-gate &vendor_pr_name, DEST_SEP);
3647c478bd9Sstevel@tonic-gate if (vendor_pr_name != NULL) {
3657c478bd9Sstevel@tonic-gate bsdjob->np_printer = vendor_pr_name;
3667c478bd9Sstevel@tonic-gate syslog(LOG_DEBUG, "bsd vendor name: %s",
3677c478bd9Sstevel@tonic-gate bsdjob->np_printer);
3687c478bd9Sstevel@tonic-gate }
3697c478bd9Sstevel@tonic-gate } else {
3707c478bd9Sstevel@tonic-gate parse_dest(job_data->dest, &destination, &tcp_port,
3717c478bd9Sstevel@tonic-gate DEST_SEP);
3727c478bd9Sstevel@tonic-gate if (tcp_port != NULL)
3737c478bd9Sstevel@tonic-gate tcpjob->np_port = tcp_port;
3747c478bd9Sstevel@tonic-gate syslog(LOG_DEBUG, "tcp_port %s",
3757c478bd9Sstevel@tonic-gate tcpjob->np_port);
3767c478bd9Sstevel@tonic-gate }
3777c478bd9Sstevel@tonic-gate if (destination == NULL ||
3787c478bd9Sstevel@tonic-gate (job_data->protocol == TCP && tcp_port == NULL)) {
3797c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
3807c478bd9Sstevel@tonic-gate gettext("Netpr: system error parsing destination %s\n"),
3817c478bd9Sstevel@tonic-gate job_data->dest);
3827c478bd9Sstevel@tonic-gate syslog(LOG_DEBUG, "system error parsing destination %s",
3837c478bd9Sstevel@tonic-gate job_data->dest);
3847c478bd9Sstevel@tonic-gate
3857c478bd9Sstevel@tonic-gate exit(E_FAILURE);
3867c478bd9Sstevel@tonic-gate }
3877c478bd9Sstevel@tonic-gate
3887c478bd9Sstevel@tonic-gate } else {
3897c478bd9Sstevel@tonic-gate destination = job_data->dest;
3907c478bd9Sstevel@tonic-gate }
3917c478bd9Sstevel@tonic-gate syslog(LOG_DEBUG, "destination : %s", destination);
3927c478bd9Sstevel@tonic-gate
3937c478bd9Sstevel@tonic-gate /*
3947c478bd9Sstevel@tonic-gate * We are now ready to open a connection to the printer
3957c478bd9Sstevel@tonic-gate * and print each of the files
3967c478bd9Sstevel@tonic-gate */
3977c478bd9Sstevel@tonic-gate
3987c478bd9Sstevel@tonic-gate if (job_data->protocol == BSD) {
3997c478bd9Sstevel@tonic-gate
4007c478bd9Sstevel@tonic-gate /* set privileges to get reserved port */
4017c478bd9Sstevel@tonic-gate if (seteuid(0)) {
4027c478bd9Sstevel@tonic-gate syslog(LOG_DEBUG, "seteuid(0) failed, exiting netpr");
4037c478bd9Sstevel@tonic-gate exit(E_FAILURE);
4047c478bd9Sstevel@tonic-gate }
4057c478bd9Sstevel@tonic-gate if ((sockfd = net_open(destination, 20)) < 0) {
4067c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
4077c478bd9Sstevel@tonic-gate gettext("Netpr: Cannot open connection to <%s>\n"),
4087c478bd9Sstevel@tonic-gate destination);
4097c478bd9Sstevel@tonic-gate syslog(LOG_DEBUG,
4107c478bd9Sstevel@tonic-gate "Cannot open connection to %s: retrying",
4117c478bd9Sstevel@tonic-gate destination);
4127c478bd9Sstevel@tonic-gate exit(E_RETRY);
4137c478bd9Sstevel@tonic-gate }
4147c478bd9Sstevel@tonic-gate } else {
4157c478bd9Sstevel@tonic-gate if ((sockfd = tcp_open(destination, tcpjob, 20)) == -1) {
4167c478bd9Sstevel@tonic-gate exit(E_RETRY);
4177c478bd9Sstevel@tonic-gate }
4187c478bd9Sstevel@tonic-gate }
4197c478bd9Sstevel@tonic-gate
4207c478bd9Sstevel@tonic-gate /* lower privileges as we now have the reserved port */
4217c478bd9Sstevel@tonic-gate if (setuid(getuid())) {
4227c478bd9Sstevel@tonic-gate syslog(LOG_DEBUG, "setuid() failed, exiting netpr");
4237c478bd9Sstevel@tonic-gate exit(E_FAILURE);
4247c478bd9Sstevel@tonic-gate }
4257c478bd9Sstevel@tonic-gate
4267c478bd9Sstevel@tonic-gate
4277c478bd9Sstevel@tonic-gate /* Set SO_KEEPALIVE on socket to keep open */
4287c478bd9Sstevel@tonic-gate if ((setsockopt(sockfd, SOL_SOCKET, SO_KEEPALIVE,
4297c478bd9Sstevel@tonic-gate (char *)&on, sizeof (on))) < 0) {
4307c478bd9Sstevel@tonic-gate syslog(LOG_DEBUG, "setsocket (SO_KEEPALIVE): %m");
4317c478bd9Sstevel@tonic-gate }
4327c478bd9Sstevel@tonic-gate
4337c478bd9Sstevel@tonic-gate if (job_data->protocol == BSD) {
4347c478bd9Sstevel@tonic-gate if ((jobstatus = bsd_print(sockfd, pa, bsdjob)) != 0) {
4357c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
4367c478bd9Sstevel@tonic-gate gettext("Netpr: Error return from bsd_print <%d>\n"),
4377c478bd9Sstevel@tonic-gate jobstatus);
4387c478bd9Sstevel@tonic-gate syslog(LOG_DEBUG,
4397c478bd9Sstevel@tonic-gate "Error return from bsd_print <%d>", jobstatus);
4407c478bd9Sstevel@tonic-gate exit_status = E_RETRY;
4417c478bd9Sstevel@tonic-gate }
4427c478bd9Sstevel@tonic-gate } else {
4437c478bd9Sstevel@tonic-gate if ((jobstatus =
4447c478bd9Sstevel@tonic-gate tcp_print(sockfd, pa, tcpjob)) != 0) {
4457c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
4467c478bd9Sstevel@tonic-gate gettext("Netpr: Error return from tcp_print <%d>\n"),
4477c478bd9Sstevel@tonic-gate jobstatus);
4487c478bd9Sstevel@tonic-gate syslog(LOG_DEBUG,
4497c478bd9Sstevel@tonic-gate "Error return from tcp_print <%d>", jobstatus);
4507c478bd9Sstevel@tonic-gate exit_status = E_RETRY;
4517c478bd9Sstevel@tonic-gate }
4527c478bd9Sstevel@tonic-gate }
4537c478bd9Sstevel@tonic-gate
4547c478bd9Sstevel@tonic-gate (void) close(fd);
4557c478bd9Sstevel@tonic-gate (void) close(sockfd);
4567c478bd9Sstevel@tonic-gate (void) munmap(pa, filesize);
4577c478bd9Sstevel@tonic-gate
4587c478bd9Sstevel@tonic-gate syslog(LOG_DEBUG, "exit status: %d", exit_status);
4597c478bd9Sstevel@tonic-gate return (exit_status);
4607c478bd9Sstevel@tonic-gate }
4617c478bd9Sstevel@tonic-gate
4627c478bd9Sstevel@tonic-gate static void
usage_exit()4637c478bd9Sstevel@tonic-gate usage_exit()
4647c478bd9Sstevel@tonic-gate {
4657c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
4667c478bd9Sstevel@tonic-gate gettext("Usage: netpr -I request_id -p printer -d destination\n"));
4677c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
4687c478bd9Sstevel@tonic-gate gettext("\t\t-U username [ -f type ] [ -T title ] [ -P protocol ]\n"));
4697c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
4707c478bd9Sstevel@tonic-gate gettext("\t\t[-t timeout] [ -c ] [ -b ]\n"));
4717c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("\t\tfiles\n"));
4727c478bd9Sstevel@tonic-gate exit(E_BAD_INPUT);
4737c478bd9Sstevel@tonic-gate }
4747c478bd9Sstevel@tonic-gate
4757c478bd9Sstevel@tonic-gate /*ARGSUSED*/
4767c478bd9Sstevel@tonic-gate void
pipehandler(int i)4777c478bd9Sstevel@tonic-gate pipehandler(int i)
4787c478bd9Sstevel@tonic-gate {
4797c478bd9Sstevel@tonic-gate (void) signal(SIGPIPE, pipehandler);
4807c478bd9Sstevel@tonic-gate syslog(LOG_DEBUG, "Received SIGPIPE, connection to printer broken");
4817c478bd9Sstevel@tonic-gate exit(E_SIGPIPE);
4827c478bd9Sstevel@tonic-gate }
483