summaryrefslogtreecommitdiff
path: root/usr/src/cmd/lp/model/netpr/netpr.c
blob: 243200a4cfcf321301b204462f6dfc99b075e512 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#include <stdio.h>
#include <stdlib.h>
#include <libintl.h>
#include <locale.h>
#include <signal.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <sys/mman.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <fcntl.h>
#include <syslog.h>
#include <sys/utsname.h>
#include "netpr.h"


static void usage_exit();

static void pipehandler(int);
char data_file_type = 0;

/*
 *  null() is to be used as a signal handler that does nothing.  It is used in
 *      place of SIG_IGN, because we want the signal to be delivered and
 *      interupt the current system call.
 */
static void
null(int i)
{
	syslog(LOG_DEBUG, "null(%d)", i);
}

/*
 *  net_open() opens a tcp connection to the printer port on the host specified
 *      in the arguments passed in.  If the connection is not made in the
 *      timeout (in seconds) passed in, an error it returned.  If the host is
 *      unknown, an error is returned.  If all is well, a file descriptor is
 *      returned to be used for future communications.
 */
int
net_open(char *host, int timeout)
{
	struct hostent *hp;
	struct servent *sp;
	struct sockaddr_in6 sin;
	void (*old_handler)();
	static struct utsname uts;

	int s;
	int lport;
	int err;
	int error_num;
	unsigned timo = 1;

	syslog(LOG_DEBUG, "net_open(%s, %d)", (host != NULL ? host : "NULL"),
	    timeout);
	/*
	 * Get the host address and port number to connect to.
	 */
	if (host == NULL) {
		return (-1);
	}

	(void) memset((char *)&sin, 0, sizeof (sin));
	if ((hp = getipnodebyname(host, AF_INET6, AI_DEFAULT,
	    &error_num)) == NULL) {
		syslog(LOG_DEBUG|LOG_ERR, "unknown host %s "
		    "getipnodebyname() returned %d", host, error_num);
		return (NETWORK_ERROR_HOST);
	}
	(void) memcpy((caddr_t)&sin.sin6_addr, hp->h_addr, hp->h_length);
	sin.sin6_family = hp->h_addrtype;
	freehostent(hp);

	if ((sp = getservbyname("printer", "tcp")) == NULL) {
		syslog(LOG_DEBUG|LOG_ERR, "printer/tcp: unknown service");
		return (NETWORK_ERROR_SERVICE);
	}
	sin.sin6_port = sp->s_port;

retry:
	/*
	 * Try connecting to the server.
	 *
	 * Use 0 as lport means that rresvport_af() will bind to a port in
	 * the anonymous privileged port range.
	 */
	lport = 0;
	s = rresvport_af(&lport, AF_INET6);
	if (s < 0)
		return (NETWORK_ERROR_PORT);

	old_handler = signal(SIGALRM, null);
	(void) alarm(timeout);
	if (connect(s, (struct sockaddr *)&sin, sizeof (sin)) < 0) {
		(void) alarm(0);
		(void) signal(SIGALRM, old_handler);
		err = errno;
		(void) close(s);
		errno = err;
		if (errno == EADDRINUSE) {
			goto retry;
		}
		/*
		 * If connecting to the local system fails, try
		 * again with "localhost" address instead.
		 */
		if (uts.nodename[0] == '\0')
			(void) uname(&uts);
		if (strcmp(host, uts.nodename) == 0) {
			IN6_IPADDR_TO_V4MAPPED(htonl(INADDR_LOOPBACK),
			    &sin.sin6_addr);
			sin.sin6_family = AF_INET6;
			goto retry;
		}
		if (errno == ECONNREFUSED && timo <= 16) {
			(void) sleep(timo);
			timo *= 2;
			goto retry;
		}
		return (NETWORK_ERROR_UNKNOWN);
	}
	(void) alarm(0);
	(void) signal(SIGALRM, old_handler);
	return (s);
}

int
main(int argc, char *argv[])
{
	extern char *optarg;
	extern int optind;
	int opt;
	np_job_t *job_data;
	char *destination = NULL;
	np_bsdjob_t *bsdjob;
	np_tcpjob_t *tcpjob;
	int sockfd;
	int pr_order = CONTROL_FIRST;
	char *vendor_pr_name = NULL;
	char *tcp_port = NULL;
	size_t filesize;
	int fd;
	caddr_t pa;
	int jobstatus;
	int exit_status = 0;
	int on = 1;


	(void) setlocale(LC_ALL, "");
#if	!defined(TEXT_DOMAIN)   /* Should be defined by cc -D */
#define	TEXT_DOMAIN "SYS_TEST"  /* Use this only if it weren't */
#endif
	(void) textdomain(TEXT_DOMAIN);

	openlog("netpr", LOG_PID, LOG_LPR);
	(void) signal(SIGPIPE, pipehandler);

	/* reduce privileges until needed to open reserved port */
	if (seteuid(getuid())) {
		syslog(LOG_DEBUG, "seteuid failed, exiting netpr");
		exit(E_FAILURE);
	}

	if ((job_data = init_job()) == NULL) {
		fprintf(stderr, gettext("init_job(): out of memory\n"));
		exit(E_RETRY);
	}

	while ((opt = getopt(argc, argv, "f:I:p:d:T:P:t:U:c:b")) != EOF)
		switch (opt) {
		case 'f':
			data_file_type = optarg[0];
			break;
		case 'I': /* foo-49 */
			job_data->request_id = alloc_str((char *)optarg);
			syslog(LOG_DEBUG, "request_id: %s",
			    job_data->request_id);
			break;
		case 'U': /* awe172-126!wendyp */
			job_data->username = alloc_str((char *)optarg);
			syslog(LOG_DEBUG, "username: %s", job_data->username);
			break;
		case 'p': /* foo */
			job_data->printer = alloc_str((char *)optarg);
			syslog(LOG_DEBUG, "printer: %s", job_data->printer);
			break;
		case 'd': /* server for printer */
			job_data->dest = alloc_str((char *)optarg);
			syslog(LOG_DEBUG, "dest: %s", job_data->dest);
			break;
		case 'T': /* /tmp/file2 */
			job_data->title = alloc_str((char *)optarg);
			syslog(LOG_DEBUG, "title: %s", job_data->title);
			break;
		case 'P':
			if ((strcmp(optarg, "bsd")) == 0)
				job_data->protocol = BSD;
			else if ((strcmp(optarg, "tcp")) == 0)
				job_data->protocol = TCP;
			else
				usage_exit();

			syslog(LOG_DEBUG, "protocol: %d", job_data->protocol);
			break;
		case 't':
			job_data->timeout = atoi(optarg);
			if (job_data->timeout < 0)
				usage_exit();
			break;
		case 'c':
			if ((strcmp(optarg, "first")) == 0)
				pr_order = CONTROL_FIRST;
			else if ((strcmp(optarg, "last")) == 0)
				pr_order = DATA_FIRST;
			else
				usage_exit();

			syslog(LOG_DEBUG, "bsd print order: %d", pr_order);
			break;
		case 'b':
			job_data->banner = NOBANNER;
			syslog(LOG_DEBUG, "banner : %d", job_data->banner);
			break;
		case '?':
			usage_exit();
		}


	if ((job_data->dest == NULL) || (job_data->request_id == NULL) ||
	    (job_data->printer == NULL) || (job_data->username == NULL))
		usage_exit();

	/*
	 * Check that there is a file
	 */
	if (optind == argc) {
		usage_exit();
	}

	job_data->filename = alloc_str(argv[optind]);
	syslog(LOG_DEBUG, "filename : %s", job_data->filename);


	/*
	 * Sanity check the file
	 * returns filesize
	 */

	if ((filesize = check_file(job_data->filename)) == -1) {
		syslog(LOG_DEBUG, "Skipping file %s",
		    job_data->filename ?
		    job_data->filename : "Error NULL file");

		switch (errno) {
		case EISDIR:
			(void) fprintf(stderr,
			    gettext("Netpr: %s: Not a regular file\n"),
			    job_data->filename ?
			    job_data->filename : "Noname");
			syslog(LOG_DEBUG, "Not a regular file");
			break;
		case ESRCH:
			(void) fprintf(stderr,
			    gettext("Netpr: %s: Empty file\n"),
			    job_data->filename ?
			    job_data->filename : "Noname");
			syslog(LOG_DEBUG, "Empty file");
			break;
		default:
			perror(job_data->filename);
			(void) fprintf(stderr,
			    gettext("Netpr: Cannot access file %s\n"),
			    job_data->filename ?
			    job_data->filename : "Noname");
			syslog(LOG_DEBUG, "Cannot access file.");
			break;

		}

		/*
		 * This file not valid, so bail
		 * Exit with zero so system will keep printing
		 */
		exit(0);
	}

	/*
	 * file looks ok, open and mmap it
	 */
	if ((fd = open(job_data->filename, O_RDONLY)) < 0) {
		(void) fprintf(stderr, gettext("Netpr: Cannot open file %s\n"),
		    job_data->filename ?
		    job_data->filename : "Error: NULL file");
		syslog(LOG_DEBUG, "Cannot open file: %s",
		    job_data->filename ?
		    job_data->filename : "Error NULL file");
		exit(E_BAD_FILE);
	}

	if ((pa = mmap((caddr_t)0, filesize, PROT_READ,
	    (MAP_SHARED | MAP_NORESERVE), fd, (off_t)0)) == MAP_FAILED) {

		(void) close(fd);
		(void) fprintf(stderr, gettext("Netpr: Cannot mmap file %s"),
		    job_data->filename ?
		    job_data->filename : "Error: NULL file");

		syslog(LOG_DEBUG, "Cannot mmap file: %s",
		    job_data->filename ?
		    job_data->filename : "Error NULL file");

		exit(E_RETRY);
	}


	if (job_data->protocol == BSD) {
		bsdjob = (np_bsdjob_t *)
		    create_bsd_job(job_data, pr_order, filesize);
		if (bsdjob == NULL)
			exit(E_FAILURE);
	} else {
		tcpjob = (np_tcpjob_t *)create_tcp_job(job_data, filesize);
		if (tcpjob == NULL)
			exit(E_FAILURE);
	}

	/*
	 * Parse destination
	 */

	if ((strpbrk(job_data->dest, DEST_SEP)) != NULL) {
		if (job_data->protocol == BSD) {
			parse_dest(job_data->dest, &destination,
			    &vendor_pr_name, DEST_SEP);
			if (vendor_pr_name != NULL) {
				bsdjob->np_printer = vendor_pr_name;
				syslog(LOG_DEBUG, "bsd vendor name: %s",
				    bsdjob->np_printer);
			}
		} else {
			parse_dest(job_data->dest, &destination, &tcp_port,
			    DEST_SEP);
			if (tcp_port != NULL)
				tcpjob->np_port = tcp_port;
			syslog(LOG_DEBUG, "tcp_port %s", tcpjob->np_port);
		}
		if (destination == NULL ||
		    (job_data->protocol == TCP && tcp_port == NULL)) {
			(void) fprintf(stderr, gettext("Netpr: system error "
			    "parsing destination %s\n"), job_data->dest);
			syslog(LOG_DEBUG, "system error parsing destination %s",
			    job_data->dest);

			exit(E_FAILURE);
		}

	} else {
		destination = job_data->dest;
	}
	syslog(LOG_DEBUG, "destination : %s", destination);

	/*
	 * We are now ready to open a connection to the printer
	 * and print each of the files
	 */

	if (job_data->protocol == BSD) {

		/* set privileges to get reserved port */
		if (seteuid(0)) {
			syslog(LOG_DEBUG, "seteuid(0) failed, exiting netpr");
			exit(E_FAILURE);
		}
		if ((sockfd =  net_open(destination, 20)) < 0) {
			(void) fprintf(stderr,
			    gettext("Netpr: Cannot open connection to <%s>\n"),
			    destination);
			syslog(LOG_DEBUG,
			    "Cannot open connection to %s: retrying",
			    destination);
			exit(E_RETRY);
		}
	} else {
		if ((sockfd = tcp_open(destination, tcpjob, 20)) == -1) {
			exit(E_RETRY);
		}
	}

	/* lower privileges as we now have the reserved port */
	if (setuid(getuid())) {
		syslog(LOG_DEBUG, "setuid() failed, exiting netpr");
		exit(E_FAILURE);
	}


	/* Set SO_KEEPALIVE on socket to keep open */
	if ((setsockopt(sockfd, SOL_SOCKET, SO_KEEPALIVE,
	    (char *)&on, sizeof (on))) < 0) {
		syslog(LOG_DEBUG, "setsocket (SO_KEEPALIVE): %m");
	}

	if (job_data->protocol == BSD) {
		if ((jobstatus = bsd_print(sockfd, pa,  bsdjob)) != 0) {
			(void) fprintf(stderr, gettext("Netpr: Error return "
			    "from bsd_print <%d>\n"), jobstatus);
			syslog(LOG_DEBUG,
			    "Error return from bsd_print <%d>", jobstatus);
			exit_status = E_RETRY;
		}
	} else {
		if ((jobstatus =
		    tcp_print(sockfd, pa, tcpjob)) != 0) {
			(void) fprintf(stderr, gettext("Netpr: Error return "
			    "from tcp_print <%d>\n"), jobstatus);
			syslog(LOG_DEBUG,
			    "Error return from tcp_print <%d>", jobstatus);
			exit_status = E_RETRY;
		}
	}

	(void) close(fd);
	(void) close(sockfd);
	(void) munmap(pa, filesize);

	syslog(LOG_DEBUG, "exit status: %d", exit_status);
	return (exit_status);
}

static void
usage_exit()
{
	(void) fprintf(stderr,
	gettext("Usage: netpr -I request_id -p printer -d destination\n"));
	(void) fprintf(stderr,
	gettext("\t\t-U username [ -f type ] [ -T title ] [ -P protocol ]\n"));
	(void) fprintf(stderr,
	    gettext("\t\t[-t timeout] [ -c ] [ -b ]\n"));
	(void) fprintf(stderr, gettext("\t\tfiles\n"));
	exit(E_BAD_INPUT);
}

/*ARGSUSED*/
void
pipehandler(int i)
{
	(void) signal(SIGPIPE, pipehandler);
	syslog(LOG_DEBUG, "Received SIGPIPE, connection to printer broken");
	exit(E_SIGPIPE);
}