8322 nl: misleading-indentation
[unleashed/tickless.git] / usr / src / cmd / print / bsd-sysv-commands / in.lpd.c
blob1b058ac2919f3bf285f1c3a08308017113c6a42b
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
23 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
28 /* $Id: in.lpd.c 170 2006-05-20 05:58:49Z njacobs $ */
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <unistd.h>
33 #include <fcntl.h>
34 #include <stdarg.h>
35 #include <string.h>
36 #include <ctype.h>
37 #include <errno.h>
38 #include <syslog.h>
39 #include <libintl.h>
40 #include <pwd.h>
41 #include <grp.h>
42 #include <sys/types.h>
43 #include <sys/stat.h>
44 #include <sys/socket.h>
45 #include <netinet/in.h>
46 #include <arpa/inet.h>
47 #include <netdb.h>
48 #include <sys/systeminfo.h>
50 #include <papi.h>
51 #include <uri.h>
52 #include "common.h"
54 #define ACK(fp) { (void) fputc('\0', fp); (void) fflush(fp); }
55 #define NACK(fp) { (void) fputc('\1', fp); (void) fflush(fp); }
58 * This file contains the front-end of the BSD Print Protocol adaptor. This
59 * code assumes a BSD Socket interface to the networking side.
62 static char *
63 remote_host_name(FILE *fp)
65 struct hostent *hp;
66 struct sockaddr_in6 peer;
67 socklen_t peer_len = sizeof (peer);
68 int fd = fileno(fp);
69 int error_num;
70 char tmp_buf[INET6_ADDRSTRLEN];
71 char *hostname;
73 /* who is our peer ? */
74 if (getpeername(fd, (struct sockaddr *)&peer, &peer_len) < 0) {
75 if ((errno != ENOTSOCK) && (errno != EINVAL))
76 return (NULL);
77 else
78 return (strdup("localhost"));
81 /* get their name or return a string containing their address */
82 if ((hp = getipnodebyaddr((const char *)&peer.sin6_addr,
83 sizeof (struct in6_addr), AF_INET6,
84 &error_num)) == NULL) {
85 return (strdup(inet_ntop(peer.sin6_family,
86 &peer.sin6_addr, tmp_buf, sizeof (tmp_buf))));
89 hostname = strdup(hp->h_name);
90 if (is_localhost(hp->h_name) != 0)
91 return (strdup("localhost"));
93 /* It must be someone else */
94 return (hostname);
97 static void
98 fatal(FILE *fp, char *fmt, ...)
100 va_list ap;
102 va_start(ap, fmt);
103 vsyslog(LOG_DEBUG, fmt, ap);
104 vfprintf(fp, fmt, ap);
105 va_end(ap);
106 exit(1);
109 static void
110 cleanup(char ***files, char **cf)
112 if (*files != NULL) {
113 int i;
115 for (i = 0; (*files)[i] != NULL; i++) {
116 (void) unlink((*files)[i]);
117 free((*files)[i]);
119 free(*files);
120 *files = NULL;
123 if (*cf != NULL) {
124 free(*cf);
125 *cf = NULL;
129 static papi_attribute_t **
130 parse_cf(papi_service_t svc, char *cf, char **files)
132 papi_attribute_t **list = NULL;
133 char previous = NULL;
134 char *entry;
135 int copies_set = 0;
136 int copies = 0;
138 for (entry = strtok(cf, "\n"); entry != NULL;
139 entry = strtok(NULL, "\n")) {
140 char *format = NULL;
142 /* count the copies */
143 if ((entry[0] >= 'a') && (entry[0] <= 'z') &&
144 (copies_set == 0) && (previous == entry[0]))
145 copies++;
146 else if ((previous >= 'a') && (previous <= 'z'))
147 copies_set = 1;
148 previous = entry[0];
150 /* process the control message */
151 switch (entry[0]) {
152 /* RFC-1179 options */
153 case 'J': /* RFC-1179 Banner Job Name */
154 papiAttributeListAddString(&list, PAPI_ATTR_EXCL,
155 "job-name", ++entry);
156 break;
157 case 'C': /* RFC-1179 Banner Class Name */
158 papiAttributeListAddString(&list, PAPI_ATTR_EXCL,
159 "rfc-1179-class", ++entry);
160 break;
161 case 'L': /* RFC-1179 Banner toggle */
162 papiAttributeListAddString(&list, PAPI_ATTR_EXCL,
163 "job-sheets", "standard");
164 break;
165 case 'T': /* RFC-1179 Title (pr) */
166 papiAttributeListAddString(&list, PAPI_ATTR_EXCL,
167 "pr-title", ++entry);
168 break;
169 case 'H': /* RFC-1179 Host */
171 * use the host as known by us, not by them
173 * papiAttributeListAddString(&list, PAPI_ATTR_EXCL,
174 * "job-originating-host-name", ++entry);
176 break;
177 case 'P': /* RFC-1179 User */
178 ++entry;
179 papiAttributeListAddString(&list, PAPI_ATTR_EXCL,
180 "job-originating-user-name", entry);
181 papiServiceSetUserName(svc, entry);
182 break;
183 case 'M': /* RFC-1179 Mail to User */
184 papiAttributeListAddBoolean(&list, PAPI_ATTR_EXCL,
185 "rfc-1179-mail", 1);
186 break;
187 case 'W': /* RFC-1179 Width (pr) */
188 papiAttributeListAddInteger(&list, PAPI_ATTR_EXCL,
189 "pr-width", atoi(++entry));
190 break;
191 case 'I': /* RFC-1179 Indent (pr) */
192 papiAttributeListAddInteger(&list, PAPI_ATTR_EXCL,
193 "pr-indent", atoi(++entry));
194 break;
195 case 'N': /* RFC-1179 Filename */
196 /* could have HPUX extension embedded */
197 if (entry[1] != ' ') { /* real pathname */
198 #ifdef DEBUG
199 papiAttributeListAddString(&list,
200 PAPI_ATTR_EXCL,
201 "flist", ++entry);
202 #endif
203 } else if (entry[2] == 'O') /* HPUX lp -o options */
204 papiAttributeListFromString(&list,
205 PAPI_ATTR_APPEND, ++entry);
206 break;
207 case 'U': /* RFC-1179 Unlink */
208 break; /* ignored */
209 case '1': /* RFC-1179 TROFF Font R */
210 papiAttributeListAddString(&list, PAPI_ATTR_EXCL,
211 "rfc-1179-font-r", ++entry);
212 break;
213 case '2': /* RFC-1179 TROFF Font I */
214 papiAttributeListAddString(&list, PAPI_ATTR_EXCL,
215 "rfc-1179-font-i", ++entry);
216 break;
217 case '3': /* RFC-1179 TROFF Font B */
218 papiAttributeListAddString(&list, PAPI_ATTR_EXCL,
219 "rfc-1179-font-b", ++entry);
220 break;
221 case '4': /* RFC-1179 TROFF Font S */
222 papiAttributeListAddString(&list, PAPI_ATTR_EXCL,
223 "rfc-1179-font-s", ++entry);
224 break;
225 case 'f': /* RFC-1179 ASCII file (print) */
226 format = "text/plain";
227 if (is_postscript(files[0]) == 1)
228 format = "application/postscript";
229 break;
230 case 'l': /* RFC-1179 CATV file (print) */
231 format = "application/octet-stream";
232 if (is_postscript(files[0]) == 1)
233 format = "application/postscript";
234 break;
235 case 'o': /* RFC-1179 Postscript file (print) */
236 format = "application/postscript";
237 break;
238 case 'p': /* RFC-1179 PR file (print) */
239 format = "application/x-pr";
240 papiAttributeListAddBoolean(&list, PAPI_ATTR_EXCL,
241 "pr-filter", 1);
242 break;
243 case 't': /* RFC-1179 TROFF file (print) */
244 format = "application/x-troff";
245 break;
246 case 'n': /* RFC-1179 DITROFF file (print) */
247 format = "application/x-ditroff";
248 break;
249 case 'd': /* RFC-1179 DVI file (print) */
250 format = "application/x-dvi";
251 break;
252 case 'g': /* RFC-1179 GRAPH file (print) */
253 format = "application/x-plot";
254 break;
255 case 'c': /* RFC-1179 CIF file (print) */
256 format = "application/x-cif";
257 break;
258 case 'v': /* RFC-1179 RASTER file (print) */
259 format = "application/x-raster";
260 break;
261 case 'r': /* RFC-1179 FORTRAN file (print) */
262 format = "application/x-fortran";
263 break;
264 /* Sun Solaris Extensions */
265 case 'O':
266 ++entry;
268 int rd, wr;
270 for (rd = wr = 0; entry[rd] != '\0'; rd++) {
271 if (entry[rd] == '"')
272 continue;
273 if (rd != wr)
274 entry[wr] = entry[rd];
275 wr++;
277 entry[wr] = '\0';
279 papiAttributeListFromString(&list,
280 PAPI_ATTR_APPEND, entry);
282 break;
283 case '5':
284 ++entry;
285 switch (entry[0]) {
286 case 'f': /* Solaris form */
287 papiAttributeListAddString(&list,
288 PAPI_ATTR_EXCL,
289 "form", ++entry);
290 break;
291 case 'H': /* Solaris handling */
292 ++entry;
293 if (strcasecmp(entry, "hold") == 0)
294 papiAttributeListAddString(&list,
295 PAPI_ATTR_EXCL,
296 "job-hold-until", "indefinite");
297 else if (strcasecmp(entry, "immediate") == 0)
298 papiAttributeListAddString(&list,
299 PAPI_ATTR_EXCL,
300 "job-hold-until", "no-hold");
301 else
302 papiAttributeListAddString(&list,
303 PAPI_ATTR_EXCL,
304 "job-hold-until", entry);
305 break;
306 case 'p': /* Solaris notification */
307 papiAttributeListAddBoolean(&list,
308 PAPI_ATTR_EXCL, "rfc-1179-mail", 1);
309 break;
310 case 'P': { /* Solaris page list */
311 char buf[BUFSIZ];
313 snprintf(buf, sizeof (buf), "page-ranges=%s",
314 ++entry);
315 papiAttributeListFromString(&list,
316 PAPI_ATTR_EXCL, buf);
318 break;
319 case 'q': { /* Solaris priority */
320 int i = atoi(++entry);
322 i = 100 - (i * 2.5);
323 if ((i < 1) || (i > 100))
324 i = 50;
325 papiAttributeListAddInteger(&list,
326 PAPI_ATTR_EXCL, "job-priority", i);
328 break;
329 case 'S': /* Solaris character set */
330 papiAttributeListAddString(&list,
331 PAPI_ATTR_EXCL, "lp-charset",
332 ++entry);
333 break;
334 case 'T': /* Solaris type */
335 format = lp_type_to_mime_type(++entry);
336 break;
337 case 'y': /* Solaris mode */
338 papiAttributeListAddString(&list,
339 PAPI_ATTR_APPEND, "lp-modes", ++entry);
340 break;
341 default:
342 syslog(LOG_INFO|LOG_DEBUG,
343 "Warning: cf message (%s) ignored",
344 entry);
345 break;
347 break;
348 /* Undefined Extensions: SCO, Ultrix, AIX, ... */
350 default:
351 syslog(LOG_INFO|LOG_DEBUG,
352 "Warning: cf message (%s) ignored", entry);
353 break;
356 if (format != NULL)
357 papiAttributeListAddString(&list, PAPI_ATTR_EXCL,
358 "document-format", format);
361 papiAttributeListAddInteger(&list, PAPI_ATTR_EXCL,
362 "copies", ++copies);
363 papiAttributeListAddString(&list, PAPI_ATTR_EXCL,
364 "job-sheets", "none");
366 return (list);
369 static papi_status_t
370 submit_job(papi_service_t svc, FILE *ifp, char *printer, int rid, char *cf,
371 char **files)
373 papi_attribute_t **list = NULL;
374 papi_status_t status;
375 papi_job_t job = NULL;
376 char *format = "";
378 if ((list = parse_cf(svc, cf, files)) != NULL) {
379 /* use the host as known by us, not by them */
380 char *host = remote_host_name(ifp);
382 if (host != NULL) {
383 papiAttributeListAddString(&list, PAPI_ATTR_REPLACE,
384 "job-originating-host-name", host);
385 free(host);
387 if (rid >= 0) {
388 papiAttributeListAddInteger(&list, PAPI_ATTR_EXCL,
389 "job-id-requested", rid);
393 status = papiJobSubmit(svc, printer, list, NULL, files, &job);
394 syslog(LOG_DEBUG, "submit: %s", papiStatusString(status));
395 if (status != PAPI_OK) {
396 char *tmp = papiServiceGetStatusMessage(svc);
398 syslog(LOG_DEBUG, "submit-detail: %s", tmp ? tmp : "none");
400 papiJobFree(job);
402 return (status);
405 static char *
406 receive_control_file(papi_service_t svc, FILE *ifp, FILE *ofp, int size)
408 char *ptr, *cf_data;
410 if ((ptr = cf_data = calloc(1, size + 1)) == NULL) {
411 NACK(ofp);
412 return (NULL);
413 } else
414 ACK(ofp);
416 while (size > 0) {
417 int rc;
419 if (((rc = fread(ptr, 1, size, ifp)) == 0) &&
420 (feof(ifp) != 0)) {
421 free(cf_data);
422 return (NULL);
423 } else {
424 ptr += rc;
425 size -= rc;
428 syslog(LOG_DEBUG, "cf_data(%s)", cf_data);
430 if (fgetc(ifp) != 0) {
431 free(cf_data);
432 return (NULL);
434 ACK(ofp);
436 return (cf_data);
439 static char *
440 receive_data_file(FILE *ifp, FILE *ofp, int size)
442 char file[] = "lpdXXXXXX";
443 char buf[BUFSIZ];
444 int fd;
446 if ((fd = mkstemp(file)) < 0) {
447 NACK(ofp);
448 return (NULL);
449 } else
450 ACK(ofp);
452 while (size > 0) {
453 int rc = ((size > BUFSIZ) ? BUFSIZ : size);
455 if (((rc = fread(buf, 1, rc, ifp)) == 0) &&
456 (feof(ifp) != 0)) {
457 close(fd);
458 unlink(file);
459 return (NULL);
460 } else {
461 char *ptr = buf;
463 while (rc > 0) {
464 int wrc = write(fd, ptr, rc);
466 if (wrc < 0) {
467 close(fd);
468 unlink(file);
469 return (NULL);
472 ptr += wrc;
473 size -= wrc;
474 rc -= wrc;
478 close(fd);
479 if (fgetc(ifp) != 0) {
480 unlink(file);
481 return (NULL);
483 ACK(ofp);
485 return (strdup(file));
488 static papi_status_t
489 berkeley_receive_files(papi_service_t svc, FILE *ifp, FILE *ofp, char *printer)
491 papi_status_t status = PAPI_OK;
492 char *file, **files = NULL; /* the job data files */
493 char *cf = NULL;
494 int rid = 0;
495 char buf[BUFSIZ];
497 while (fgets(buf, sizeof (buf), ifp) != NULL) {
498 int size;
500 syslog(LOG_DEBUG, "XFER CMD: (%d)%s\n", buf[0], &buf[1]);
501 #ifdef DEBUG /* translate [1-3]... messages to \[1-3] to run by hand */
502 if ((buf[0] > '0') && (buf[0] < '4'))
503 buf[0] -= '0';
504 #endif
505 switch (buf[0]) {
506 case 0x01: /* Abort */
507 cleanup(&files, &cf);
508 break;
509 case 0x02: { /* Receive control file */
510 if (((cf = strchr(buf, ' ')) != NULL) &&
511 (strlen(cf) > 4)) {
512 while ((*cf != NULL) && (isdigit(*cf) == 0))
513 cf++;
514 rid = atoi(cf);
516 cf = receive_control_file(svc, ifp, ofp, atoi(&buf[1]));
517 if (cf == NULL) {
518 cleanup(&files, &cf);
519 return (PAPI_BAD_REQUEST);
520 } else if (files != NULL) {
521 status = submit_job(svc, ifp, printer, rid, cf,
522 files);
523 cleanup(&files, &cf);
526 break;
527 case 0x03: { /* Receive data file */
528 file = receive_data_file(ifp, ofp, atoi(&buf[1]));
529 if (file == NULL) {
530 cleanup(&files, &cf);
531 return (PAPI_TEMPORARY_ERROR);
533 list_append(&files, file);
535 break;
536 default:
537 cleanup(&files, &cf);
538 fatal(ofp, "protocol screwup");
539 break;
543 if ((cf != NULL) && (files != NULL))
544 status = submit_job(svc, ifp, printer, rid, cf, files);
546 cleanup(&files, &cf);
548 return (status);
551 static papi_status_t
552 berkeley_transfer_files(papi_service_t svc, FILE *ifp, FILE *ofp,
553 char *printer)
555 papi_status_t status;
556 papi_printer_t p = NULL;
557 char *keys[] = { "printer-is-accepting-jobs", NULL };
559 status = papiPrinterQuery(svc, printer, keys, NULL, &p);
560 if ((status == PAPI_OK) && (p != NULL)) {
561 papi_attribute_t **attrs = papiPrinterGetAttributeList(p);
562 char accepting = PAPI_FALSE;
564 papiAttributeListGetBoolean(attrs, NULL,
565 "printer-is-accepting-jobs", &accepting);
567 if (accepting == PAPI_TRUE) {
568 ACK(ofp);
569 status = berkeley_receive_files(svc, ifp, ofp, printer);
570 } else
571 NACK(ofp);
573 papiPrinterFree(p);
574 } else
575 NACK(ofp);
577 return (status);
580 static int
581 cyclical_service_check(char *svc_name)
583 papi_attribute_t **list;
584 uri_t *uri = NULL;
585 char *s = NULL;
587 /* was there a printer? */
588 if (svc_name == NULL)
589 return (0);
591 if ((list = getprinterbyname(svc_name, NULL)) == NULL)
592 return (0); /* if it doesnt' resolve, we will fail later */
594 papiAttributeListGetString(list, NULL, "printer-uri-supported", &s);
595 if ((s == NULL) || (strcasecmp(svc_name, s) != 0))
596 return (0); /* they don't match */
598 /* is it in uri form? */
599 if (uri_from_string(s, &uri) < 0)
600 return (0);
602 if ((uri == NULL) || (uri->scheme == NULL) || (uri->host == NULL)) {
603 uri_free(uri);
604 return (0);
607 /* is it in lpd form? */
608 if (strcasecmp(uri->scheme, "lpd") != 0) {
609 uri_free(uri);
610 return (0);
613 /* is it the local host? */
614 if (is_localhost(uri->host) != 0) {
615 uri_free(uri);
616 return (0);
619 uri_free(uri);
620 return (1);
625 * This is the entry point for this program. The program takes the
626 * following options:
627 * (none)
630 main(int ac, char *av[])
632 papi_status_t status;
633 papi_service_t svc = NULL;
634 papi_encryption_t encryption = PAPI_ENCRYPT_NEVER;
635 FILE *ifp = stdin;
636 FILE *ofp = stdout;
637 int c;
638 char buf[BUFSIZ];
639 char **args;
640 char *printer;
641 char *run_dir = "/var/run/in.lpd";
642 char *run_user = NULL;
643 struct passwd *pw = NULL;
645 (void) chdir("/tmp"); /* run in /tmp by default */
646 openlog("bsd-gw", LOG_PID, LOG_LPR);
648 while ((c = getopt(ac, av, "Ed:u:")) != EOF)
649 switch (c) {
650 case 'E':
651 encryption = PAPI_ENCRYPT_ALWAYS;
652 break;
653 case 'd': /* run where they tell you */
654 run_dir = optarg;
655 break;
656 case 'u': /* run as */
657 run_user = optarg;
658 break;
659 default:
663 if (run_user != NULL) /* get the requested user info */
664 pw = getpwnam(run_user);
666 if (run_dir != NULL) { /* setup the run_dir */
667 (void) mkdir(run_dir, 0700);
668 if (pw != NULL)
669 (void) chown(run_dir, pw->pw_uid, pw->pw_gid);
672 if (pw != NULL) { /* run as the requested user */
673 syslog(LOG_DEBUG, "name: %s, uid: %d, gid: %d",
674 pw->pw_name, pw->pw_uid, pw->pw_gid);
675 initgroups(pw->pw_name, pw->pw_gid);
676 setgid(pw->pw_gid);
677 setuid(pw->pw_uid);
680 if (run_dir != NULL) /* move to the run_dir */
681 if (chdir(run_dir) < 0) {
682 syslog(LOG_DEBUG, "failed to chdir(%s)", run_dir);
683 exit(1);
686 syslog(LOG_DEBUG, "$CWD = %s", getwd(NULL));
688 if (fgets(buf, sizeof (buf), ifp) == NULL) {
689 if (feof(ifp) == 0)
690 syslog(LOG_ERR, "Error reading from connection: %s",
691 strerror(errno));
692 exit(1);
695 syslog(LOG_DEBUG, "CMD: (%d)%s\n", buf[0], &buf[1]);
697 #ifdef DEBUG /* translate [1-5]... messages to \[1-5] to run by hand */
698 if ((buf[0] > '0') && (buf[0] < '6'))
699 buf[0] -= '0';
700 #endif
702 if ((buf[0] < 1) || (buf[0] > 5)) {
703 fatal(ofp, "Invalid protocol request (%d): %c%s\n",
704 buf[0], buf[0], buf);
705 exit(1);
708 args = strsplit(&buf[1], "\t\n ");
709 printer = *args++;
711 if (printer == NULL) {
712 fatal(ofp, "Can't determine requested printer");
713 exit(1);
716 if (cyclical_service_check(printer) != 0) {
717 fatal(ofp, "%s is cyclical\n", printer);
718 exit(1);
721 status = papiServiceCreate(&svc, printer, NULL, NULL, NULL,
722 encryption, NULL);
723 if (status != PAPI_OK) {
724 fatal(ofp, "Failed to contact service for %s: %s\n", printer,
725 verbose_papi_message(svc, status));
726 exit(1);
730 * Trusted Solaris can't be trusting of intermediaries. Pass
731 * the socket connection to the print service to retrieve the
732 * sensativity label off of a multi-level port.
734 (void) papiServiceSetPeer(svc, fileno(ifp));
736 switch (buf[0]) {
737 case '\1': /* restart printer */
738 ACK(ofp); /* there is no equivalent */
739 break;
740 case '\2': /* transfer job(s) */
741 status = berkeley_transfer_files(svc, ifp, ofp, printer);
742 break;
743 case '\3': /* show queue (short) */
744 case '\4': { /* show queue (long) */
745 int count;
747 for (count = 0; args[count] != 0; count++) {}
749 berkeley_queue_report(svc, ofp, printer, buf[0], count, args);
751 break;
752 case '\5': { /* cancel job(s) */
753 char *user = *args++;
754 char *host = remote_host_name(ifp);
755 int count;
757 if (host != NULL) {
758 char buf[BUFSIZ];
760 snprintf(buf, sizeof (buf), "%s@%s", user, host);
761 status = papiServiceSetUserName(svc, buf);
762 } else
763 status = papiServiceSetUserName(svc, user);
765 for (count = 0; args[count] != 0; count++) {}
767 berkeley_cancel_request(svc, ofp, printer, count, args);
769 break;
770 default:
771 fatal(ofp, "unsupported protocol request (%c), %s",
772 buf[0], &buf[1]);
775 (void) fflush(ofp);
777 syslog(LOG_DEBUG, "protocol request(%d) for %s completed: %s",
778 buf[0], printer, papiStatusString(status));
779 if (status != PAPI_OK)
780 syslog(LOG_DEBUG, "detail: %s",
781 verbose_papi_message(svc, status));
783 papiServiceDestroy(svc);
785 return (0);