summaryrefslogtreecommitdiff
path: root/usr/src/cmd/print/bsd-sysv-commands/in.lpd.c
diff options
context:
space:
mode:
authorjacobs <none@none>2006-11-15 19:19:07 -0800
committerjacobs <none@none>2006-11-15 19:19:07 -0800
commit0a44ef6d9afbfe052a7e975f55ea0d2954b62a82 (patch)
treeafca18ba6ad0fb467b29e368f05399e53a19ec4a /usr/src/cmd/print/bsd-sysv-commands/in.lpd.c
parent3383b6dda001791704e0e66f7b78dd0dfe74f547 (diff)
downloadillumos-joyent-0a44ef6d9afbfe052a7e975f55ea0d2954b62a82.tar.gz
4634017 Lpadmin's new printer limit needs to be increased.
4941143 lpsched leaks memory in freeprinter() 6480891 lpsched fixes unused files 6486772 in.lpd should use libpapi 6486967 print-svc refers to /usr/sbin/cut 6487898 lpadmin doesn't add remote access 6488443 printmr/lpadmin should prefer IPP support when it's available 6490445 psm-lpd has incorrect mime type in it's translation table 6490453 lpr uses the wrong default mime type 6491971 in.lpd and the ipp listener should detect cycles 6491978 libhttp-core should build when -DDEBUG is specified 6492414 lpstat -o -l should report on requested form when available --HG-- rename : usr/src/cmd/lp/cmd/adaptor/Makefile => deleted_files/usr/src/cmd/lp/cmd/adaptor/Makefile rename : usr/src/cmd/lp/cmd/adaptor/cancel_job.c => deleted_files/usr/src/cmd/lp/cmd/adaptor/cancel_job.c rename : usr/src/cmd/lp/cmd/adaptor/misc.c => deleted_files/usr/src/cmd/lp/cmd/adaptor/misc.c rename : usr/src/cmd/lp/cmd/adaptor/misc.h => deleted_files/usr/src/cmd/lp/cmd/adaptor/misc.h rename : usr/src/cmd/lp/cmd/adaptor/show_queue.c => deleted_files/usr/src/cmd/lp/cmd/adaptor/show_queue.c rename : usr/src/cmd/lp/cmd/adaptor/submit_job.c => deleted_files/usr/src/cmd/lp/cmd/adaptor/submit_job.c rename : usr/src/cmd/print/gateway/Makefile => deleted_files/usr/src/cmd/print/gateway/Makefile rename : usr/src/cmd/print/gateway/adaptor.c => deleted_files/usr/src/cmd/print/gateway/adaptor.c rename : usr/src/cmd/print/gateway/adaptor.h => deleted_files/usr/src/cmd/print/gateway/adaptor.h rename : usr/src/cmd/print/gateway/cleanup.xml => deleted_files/usr/src/cmd/print/gateway/cleanup.xml rename : usr/src/cmd/print/gateway/main.c => deleted_files/usr/src/cmd/print/gateway/main.c rename : usr/src/cmd/print/gateway/print-cleanup => deleted_files/usr/src/cmd/print/gateway/print-cleanup rename : usr/src/cmd/print/gateway/printd.c => deleted_files/usr/src/cmd/print/gateway/printd.c rename : usr/src/cmd/print/gateway/translators/Makefile => deleted_files/usr/src/cmd/print/gateway/translators/Makefile rename : usr/src/cmd/print/gateway/translators/cascade/Makefile => deleted_files/usr/src/cmd/print/gateway/translators/cascade/Makefile rename : usr/src/cmd/print/gateway/translators/cascade/cascade.c => deleted_files/usr/src/cmd/print/gateway/translators/cascade/cascade.c rename : usr/src/cmd/print/gateway/translators/test/Makefile => deleted_files/usr/src/cmd/print/gateway/translators/test/Makefile rename : usr/src/cmd/print/gateway/translators/test/README => deleted_files/usr/src/cmd/print/gateway/translators/test/README rename : usr/src/cmd/print/gateway/translators/test/test.c => deleted_files/usr/src/cmd/print/gateway/translators/test/test.c rename : usr/src/cmd/print/gateway/rfc1179.xml => usr/src/cmd/print/bsd-sysv-commands/rfc1179.xml
Diffstat (limited to 'usr/src/cmd/print/bsd-sysv-commands/in.lpd.c')
-rw-r--r--usr/src/cmd/print/bsd-sysv-commands/in.lpd.c635
1 files changed, 592 insertions, 43 deletions
diff --git a/usr/src/cmd/print/bsd-sysv-commands/in.lpd.c b/usr/src/cmd/print/bsd-sysv-commands/in.lpd.c
index 465189daf1..18487c51d8 100644
--- a/usr/src/cmd/print/bsd-sysv-commands/in.lpd.c
+++ b/usr/src/cmd/print/bsd-sysv-commands/in.lpd.c
@@ -31,13 +31,25 @@
#include <stdio.h>
#include <stdlib.h>
+#include <unistd.h>
+#include <fcntl.h>
#include <stdarg.h>
#include <string.h>
#include <errno.h>
#include <syslog.h>
#include <libintl.h>
+#include <pwd.h>
+#include <grp.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
+#include <netdb.h>
+#include <sys/systeminfo.h>
#include <papi.h>
+#include <uri.h>
#include "common.h"
#define ACK(fp) { (void) fputc('\0', fp); (void) fflush(fp); }
@@ -48,7 +60,61 @@
* code assumes a BSD Socket interface to the networking side.
*/
-void
+static char *
+remote_host_name(FILE *fp)
+{
+ struct hostent *hp;
+ struct sockaddr_in6 peer;
+ socklen_t peer_len = sizeof (peer);
+ int fd = fileno(fp);
+ int error_num;
+ char myname[MAXHOSTNAMELEN], tmp_buf[INET6_ADDRSTRLEN];
+ char *hostname;
+
+ /* who is our peer ? */
+ if (getpeername(fd, (struct sockaddr *)&peer, &peer_len) < 0) {
+ if ((errno != ENOTSOCK) && (errno != EINVAL))
+ return (NULL);
+ else
+ return (strdup("localhost"));
+ }
+
+ /* get their name or return a string containing their address */
+ if ((hp = getipnodebyaddr((const char *)&peer.sin6_addr,
+ sizeof (struct in6_addr), AF_INET6,
+ &error_num)) == NULL) {
+ return (strdup(inet_ntop(peer.sin6_family,
+ &peer.sin6_addr, tmp_buf, sizeof (tmp_buf))));
+ }
+
+ /* is it "localhost" ? */
+ if (strcasecmp(hp->h_name, "localhost") == 0)
+ return (strdup("localhost"));
+
+ /* duplicate the name because gethostbyXXXX() is not reentrant */
+ hostname = strdup(hp->h_name);
+ (void) sysinfo(SI_HOSTNAME, myname, sizeof (myname));
+
+ /* is it from one of my addresses ? */
+ if ((hp = getipnodebyname(myname, AF_INET6, AI_ALL|AI_V4MAPPED,
+ &error_num)) != NULL) {
+ struct in6_addr **tmp = (struct in6_addr **)hp->h_addr_list;
+ int i = 0;
+
+ while (tmp[i] != NULL) {
+ if (memcmp(tmp[i++], &peer.sin6_addr, hp->h_length)
+ == 0) {
+ free(hostname);
+ return (strdup("localhost"));
+ }
+ }
+ }
+
+ /* It must be someone else */
+ return (hostname);
+}
+
+static void
fatal(FILE *fp, char *fmt, ...)
{
va_list ap;
@@ -57,62 +123,440 @@ fatal(FILE *fp, char *fmt, ...)
vsyslog(LOG_DEBUG, fmt, ap);
vfprintf(fp, fmt, ap);
va_end(ap);
+ exit(1);
}
static void
-cleanup(char **files)
+cleanup(char ***files, char **cf)
{
- if (files != NULL) {
+ if (*files != NULL) {
int i;
- for (i = 0; files[i] != NULL; i++)
- unlink(files[i]);
+ for (i = 0; (*files)[i] != NULL; i++) {
+ (void) unlink((*files)[i]);
+ free((*files)[i]);
+ }
+ free(*files);
+ *files = NULL;
+ }
+
+ if (*cf != NULL) {
+ free(*cf);
+ *cf = NULL;
}
}
-static void
-berkeley_receive_files(papi_service_t svc, FILE *ifp, FILE *ofp)
+static papi_attribute_t **
+parse_cf(papi_service_t svc, char *cf, char **files)
{
- char line[BUFSIZ];
- char **files = NULL; /* the job data files */
+ papi_attribute_t **list = NULL;
+ char previous = NULL,
+ *entry,
+ *s,
+ text[BUFSIZ];
+ int count = 0,
+ copies_set = 0,
+ copies = 0;
+
+ for (entry = strtok(cf, "\n"); entry != NULL;
+ entry = strtok(NULL, "\n")) {
+ char *format = NULL;
+
+ /* count the copies */
+ if ((entry[0] >= 'a') && (entry[0] <= 'z') &&
+ (copies_set == 0) && (previous == entry[0]))
+ copies++;
+ else if ((previous >= 'a') && (previous <= 'z'))
+ copies_set = 1;
+ previous = entry[0];
+
+ /* process the control message */
+ switch (entry[0]) {
+ /* RFC-1179 options */
+ case 'J': /* RFC-1179 Banner Job Name */
+ papiAttributeListAddString(&list, PAPI_ATTR_EXCL,
+ "job-name", ++entry);
+ break;
+ case 'C': /* RFC-1179 Banner Class Name */
+ papiAttributeListAddString(&list, PAPI_ATTR_EXCL,
+ "rfc-1179-class", ++entry);
+ break;
+ case 'L': /* RFC-1179 Banner toggle */
+ papiAttributeListAddString(&list, PAPI_ATTR_EXCL,
+ "job-sheets", "standard");
+ break;
+ case 'T': /* RFC-1179 Title (pr) */
+ papiAttributeListAddString(&list, PAPI_ATTR_EXCL,
+ "pr-title", ++entry);
+ break;
+ case 'H': /* RFC-1179 Host */
+ /*
+ * use the host as known by us, not by them
+ *
+ * papiAttributeListAddString(&list, PAPI_ATTR_EXCL,
+ * "job-originating-host-name", ++entry);
+ */
+ break;
+ case 'P': /* RFC-1179 User */
+ ++entry;
+ papiAttributeListAddString(&list, PAPI_ATTR_EXCL,
+ "requesting-user-name", entry);
+ papiServiceSetUserName(svc, entry);
+ break;
+ case 'M': /* RFC-1179 Mail to User */
+ papiAttributeListAddBoolean(&list, PAPI_ATTR_EXCL,
+ "rfc-1179-mail", 1);
+ break;
+ case 'W': /* RFC-1179 Width (pr) */
+ papiAttributeListAddInteger(&list, PAPI_ATTR_EXCL,
+ "pr-width", atoi(++entry));
+ break;
+ case 'I': /* RFC-1179 Indent (pr) */
+ papiAttributeListAddInteger(&list, PAPI_ATTR_EXCL,
+ "pr-indent", atoi(++entry));
+ break;
+ case 'N': /* RFC-1179 Filename */
+ /* could have HPUX extension embedded */
+ if (entry[1] != ' ') { /* real pathname */
+#ifdef DEBUG
+ papiAttributeListAddString(&list,
+ PAPI_ATTR_EXCL,
+ "flist", ++entry);
+#endif
+ } else if (entry[2] == 'O') /* HPUX lp -o options */
+ papiAttributeListFromString(&list,
+ PAPI_ATTR_APPEND, ++entry);
+ break;
+ case 'U': /* RFC-1179 Unlink */
+ break; /* ignored */
+ case '1': /* RFC-1179 TROFF Font R */
+ papiAttributeListAddString(&list, PAPI_ATTR_EXCL,
+ "rfc-1179-font-r", ++entry);
+ break;
+ case '2': /* RFC-1179 TROFF Font I */
+ papiAttributeListAddString(&list, PAPI_ATTR_EXCL,
+ "rfc-1179-font-i", ++entry);
+ break;
+ case '3': /* RFC-1179 TROFF Font B */
+ papiAttributeListAddString(&list, PAPI_ATTR_EXCL,
+ "rfc-1179-font-b", ++entry);
+ break;
+ case '4': /* RFC-1179 TROFF Font S */
+ papiAttributeListAddString(&list, PAPI_ATTR_EXCL,
+ "rfc-1179-font-s", ++entry);
+ break;
+ case 'f': /* RFC-1179 ASCII file (print) */
+ format = "text/plain";
+ if (is_postscript(files[0]) == 1)
+ format = "application/postscript";
+ break;
+ case 'l': /* RFC-1179 CATV file (print) */
+ format = "application/octet-stream";
+ if (is_postscript(files[0]) == 1)
+ format = "application/postscript";
+ break;
+ case 'o': /* RFC-1179 Postscript file (print) */
+ format = "application/postscript";
+ break;
+ case 'p': /* RFC-1179 PR file (print) */
+ format = "application/x-pr";
+ papiAttributeListAddBoolean(&list, PAPI_ATTR_EXCL,
+ "pr-filter", 1);
+ break;
+ case 't': /* RFC-1179 TROFF file (print) */
+ format = "application/x-troff";
+ break;
+ case 'n': /* RFC-1179 DITROFF file (print) */
+ format = "application/x-ditroff";
+ break;
+ case 'd': /* RFC-1179 DVI file (print) */
+ format = "application/x-dvi";
+ break;
+ case 'g': /* RFC-1179 GRAPH file (print) */
+ format = "application/x-plot";
+ break;
+ case 'c': /* RFC-1179 CIF file (print) */
+ format = "application/x-cif";
+ break;
+ case 'v': /* RFC-1179 RASTER file (print) */
+ format = "application/x-raster";
+ break;
+ case 'r': /* RFC-1179 FORTRAN file (print) */
+ format = "application/x-fortran";
+ break;
+ /* Sun Solaris Extensions */
+ case 'O':
+ ++entry;
+ do {
+ if (*entry != '"')
+ text[count++] = *entry;
+ } while (*entry++);
+ papiAttributeListFromString(&list, PAPI_ATTR_APPEND,
+ text);
+ break;
+ case '5':
+ ++entry;
+ switch (entry[0]) {
+ case 'f': /* Solaris form */
+ papiAttributeListAddString(&list,
+ PAPI_ATTR_EXCL,
+ "form", ++entry);
+ break;
+ case 'H': /* Solaris handling */
+ ++entry;
+ if (strcasecmp(entry, "hold") == 0)
+ papiAttributeListAddString(&list,
+ PAPI_ATTR_EXCL,
+ "job-hold-until", "indefinite");
+ else if (strcasecmp(entry, "release") == 0)
+ papiAttributeListAddString(&list,
+ PAPI_ATTR_EXCL,
+ "job-hold-until", "no-hold");
+ else if (strcasecmp(entry, "immediate") == 0)
+ papiAttributeListAddInteger(&list,
+ PAPI_ATTR_EXCL,
+ "job-priority", 100);
+ else
+ papiAttributeListAddString(&list,
+ PAPI_ATTR_EXCL,
+ "job-hold-until", entry);
+ break;
+ case 'p': /* Solaris notification */
+ papiAttributeListAddBoolean(&list,
+ PAPI_ATTR_EXCL, "rfc-1179-mail", 1);
+ break;
+ case 'P': /* Solaris page list */
+ papiAttributeListAddString(&list,
+ PAPI_ATTR_EXCL,
+ "page-ranges", ++entry);
+ break;
+ case 'q': { /* Solaris priority */
+ int i = atoi(optarg);
+
+ i = 99 * (39 - i) / 39 + 1;
+ if ((i < 1) || (i > 100))
+ i = 50;
+ papiAttributeListAddInteger(&list,
+ PAPI_ATTR_EXCL, "priority", i);
+ }
+ break;
+ case 'S': /* Solaris character set */
+ papiAttributeListAddString(&list,
+ PAPI_ATTR_EXCL, "lp-charset",
+ ++entry);
+ break;
+ case 'T': /* Solaris type */
+ format = lp_type_to_mime_type(++entry);
+ break;
+ case 'y': /* Solaris mode */
+ papiAttributeListAddString(&list,
+ PAPI_ATTR_APPEND, "lp-modes", ++entry);
+ break;
+ default:
+ syslog(LOG_INFO|LOG_DEBUG,
+ "Warning: cf message (%s) ignored",
+ entry);
+ break;
+ }
+ break;
+ /* Undefined Extensions: SCO, Ultrix, AIX, ... */
+
+ default:
+ syslog(LOG_INFO|LOG_DEBUG,
+ "Warning: cf message (%s) ignored", entry);
+ break;
+ }
+
+ if (format != NULL)
+ papiAttributeListAddString(&list, PAPI_ATTR_EXCL,
+ "document-format", format);
+ }
- /* This should actually implement transfer job from RFC-1179 */
+ papiAttributeListAddInteger(&list, PAPI_ATTR_EXCL,
+ "copies", ++copies);
+ papiAttributeListAddString(&list, PAPI_ATTR_EXCL,
+ "job-sheets", "none");
+
+ return (list);
+}
+
+static papi_status_t
+submit_job(papi_service_t svc, FILE *ifp, char *printer, char *cf, char **files)
+{
+ papi_attribute_t **list = NULL;
+ papi_status_t status;
+ papi_job_t job = NULL;
+ char *format = "";
+
+ if ((list = parse_cf(svc, cf, files)) != NULL) {
+ /* use the host as known by us, not by them */
+ char *host = remote_host_name(ifp);
+
+ if (host != NULL) {
+ papiAttributeListAddString(&list, PAPI_ATTR_REPLACE,
+ "job-originating-host-name", host);
+ free(host);
+ }
+ }
+
+ status = papiJobSubmit(svc, printer, list, NULL, files, &job);
+ syslog(LOG_DEBUG, "submit: %s", papiStatusString(status));
+ if (status != PAPI_OK) {
+ char *tmp = papiServiceGetStatusMessage(svc);
+
+ syslog(LOG_DEBUG, "submit-detail: %s", tmp ? tmp : "none");
+ }
+ papiJobFree(job);
+
+ return (status);
+}
+
+static char *
+receive_control_file(papi_service_t svc, FILE *ifp, FILE *ofp, int size)
+{
+ char *ptr, *cf_data;
+
+ if ((ptr = cf_data = calloc(1, size + 1)) == NULL) {
+ NACK(ofp);
+ return (NULL);
+ } else
+ ACK(ofp);
+
+ while (size > 0) {
+ int rc;
+
+ if (((rc = fread(ptr, 1, size, ifp)) == 0) &&
+ (feof(ifp) != 0)) {
+ free(cf_data);
+ return (NULL);
+ } else {
+ ptr += rc;
+ size -= rc;
+ }
+ }
+ syslog(LOG_DEBUG, "cf_data(%s)", cf_data);
+
+ if (fgetc(ifp) != 0) {
+ free(cf_data);
+ return (NULL);
+ }
+ ACK(ofp);
+
+ return (cf_data);
+}
+
+static char *
+receive_data_file(FILE *ifp, FILE *ofp, int size)
+{
+ char file[] = "lpdXXXXXX";
+ char buf[BUFSIZ];
+ int fd;
+
+ if ((fd = mkstemp(file)) < 0) {
+ NACK(ofp);
+ return (NULL);
+ } else
+ ACK(ofp);
+
+ while (size > 0) {
+ int rc = ((size > BUFSIZ) ? BUFSIZ : size);
+
+ if (((rc = fread(buf, 1, rc, ifp)) == 0) &&
+ (feof(ifp) != 0)) {
+ close(fd);
+ unlink(file);
+ return (NULL);
+ } else {
+ char *ptr = buf;
+
+ while (rc > 0) {
+ int wrc = write(fd, ptr, rc);
+
+ if (wrc < 0) {
+ close(fd);
+ unlink(file);
+ return(NULL);
+ }
+
+ ptr += wrc;
+ size -= wrc;
+ rc -= wrc;
+ }
+ }
+ }
+ close(fd);
+ if (fgetc(ifp) != 0) {
+ unlink(file);
+ return (NULL);
+ }
ACK(ofp);
- while (fgets(line, sizeof (line), ifp) != NULL) {
- switch (line[0]) {
+ return (strdup(file));
+}
+
+static papi_status_t
+berkeley_receive_files(papi_service_t svc, FILE *ifp, FILE *ofp, char *printer)
+{
+ papi_status_t status = PAPI_OK;
+ char *file, **files = NULL; /* the job data files */
+ char *cf = NULL;
+ char buf[BUFSIZ];
+
+ while (fgets(buf, sizeof (buf), ifp) != NULL) {
+ int size;
+
+ syslog(LOG_DEBUG, "XFER CMD: (%d)%s\n", buf[0], &buf[1]);
+#ifdef DEBUG /* translate [1-3]... messages to \[1-3] to run by hand */
+ if ((buf[0] > '0') && (buf[0] < '4'))
+ buf[0] -= '0';
+#endif
+ switch (buf[0]) {
case 0x01: /* Abort */
- cleanup(files);
+ cleanup(&files, &cf);
break;
- case 0x02: /* Receive control file */
-
+ case 0x02: { /* Receive control file */
+ cf = receive_control_file(svc, ifp, ofp, atoi(&buf[1]));
+ if (cf == NULL) {
+ cleanup(&files, &cf);
+ return (PAPI_BAD_REQUEST);
+ } else if (files != NULL) {
+ status = submit_job(svc, ifp, printer, cf,
+ files);
+ cleanup(&files, &cf);
+ }
+ }
break;
case 0x03: { /* Receive data file */
- char file[] = "lpdXXXXXX";
- int fd;
-
- fd = mkstemp(file);
-
- list_append(&files, strdup(file));
+ file = receive_data_file(ifp, ofp, atoi(&buf[1]));
+ if (file == NULL) {
+ cleanup(&files, &cf);
+ return (PAPI_TEMPORARY_ERROR);
+ }
+ list_append(&files, file);
}
break;
default:
+ cleanup(&files, &cf);
fatal(ofp, "protocol screwup");
- cleanup(files);
break;
}
}
- cleanup(files);
+ if ((cf != NULL) && (files != NULL))
+ status = submit_job(svc, ifp, printer, cf, files);
+
+ cleanup(&files, &cf);
+
+ return (status);
}
-static void
+static papi_status_t
berkeley_transfer_files(papi_service_t svc, FILE *ifp, FILE *ofp,
char *printer)
{
papi_status_t status;
papi_printer_t p = NULL;
- char *keys[] = { "printer-is-accepting", NULL };
+ char *keys[] = { "printer-is-accepting-jobs", NULL };
status = papiPrinterQuery(svc, printer, keys, NULL, &p);
if ((status == PAPI_OK) && (p != NULL)) {
@@ -120,18 +564,68 @@ berkeley_transfer_files(papi_service_t svc, FILE *ifp, FILE *ofp,
char accepting = PAPI_FALSE;
papiAttributeListGetBoolean(attrs, NULL,
- "printer-is-accepting", &accepting);
+ "printer-is-accepting-jobs", &accepting);
- if (accepting == PAPI_TRUE)
- berkeley_receive_files(svc, ifp, ofp);
- else
+ if (accepting == PAPI_TRUE) {
+ ACK(ofp);
+ status = berkeley_receive_files(svc, ifp, ofp, printer);
+ } else
NACK(ofp);
papiPrinterFree(p);
} else
NACK(ofp);
+
+ return (status);
+}
+
+static int
+cyclical_service_check(char *svc_name)
+{
+ papi_attribute_t **list;
+ char buf[BUFSIZ];
+ uri_t *uri = NULL;
+ char *s = NULL;
+
+ /* was there a printer? */
+ if (svc_name == NULL)
+ return (0);
+
+ if ((list = getprinterbyname(svc_name, NULL)) == NULL)
+ return (0); /* if it doesnt' resolve, we will fail later */
+
+ papiAttributeListGetString(list, NULL, "printer-uri-supported", &s);
+ if ((s == NULL) || (strcasecmp(svc_name, s) != 0))
+ return (0); /* they don't match */
+
+ /* is it in uri form? */
+ if (uri_from_string(s, &uri) < 0)
+ return (0);
+
+ if ((uri == NULL) || (uri->scheme == NULL) || (uri->host == NULL)) {
+ uri_free(uri);
+ return (0);
+ }
+
+ /* is it in lpd form? */
+ if (strcasecmp(uri->scheme, "lpd") != 0) {
+ uri_free(uri);
+ return (0);
+ }
+
+ /* is it the local host? */
+ sysinfo(SI_HOSTNAME, buf, sizeof (buf));
+ if ((strcasecmp(uri->host, "localhost") != 0) &&
+ (strcasecmp(uri->host, buf) != 0)) {
+ uri_free(uri);
+ return (0);
+ }
+
+ uri_free(uri);
+ return (1);
}
+
/*
* This is the entry point for this program. The program takes the
* following options:
@@ -148,20 +642,54 @@ main(int ac, char *av[])
int c;
char buf[BUFSIZ],
**args,
- *printer;
+ *printer,
+ *run_dir = "/var/run/in.lpd",
+ *run_user = NULL;
+ struct passwd *pw = NULL;
+ (void) chdir("/tmp"); /* run in /tmp by default */
openlog("bsd-gw", LOG_PID, LOG_LPR);
- while ((c = getopt(ac, av, "d")) != EOF)
+ while ((c = getopt(ac, av, "Ed:u:")) != EOF)
switch (c) {
case 'E':
encryption = PAPI_ENCRYPT_ALWAYS;
break;
- case 'd':
+ case 'd': /* run where they tell you */
+ run_dir = optarg;
+ break;
+ case 'u': /* run as */
+ run_user = optarg;
+ break;
default:
;
}
+ if (run_user != NULL) /* get the requested user info */
+ pw = getpwnam(run_user);
+
+ if (run_dir != NULL) { /* setup the run_dir */
+ (void) mkdir(run_dir, 0700);
+ if (pw != NULL)
+ (void) chown(run_dir, pw->pw_uid, pw->pw_gid);
+ }
+
+ if (pw != NULL) { /* run as the requested user */
+ syslog(LOG_DEBUG, "name: %s, uid: %d, gid: %d",
+ pw->pw_name, pw->pw_uid, pw->pw_gid);
+ initgroups(pw->pw_name, pw->pw_gid);
+ setgid(pw->pw_gid);
+ setuid(pw->pw_uid);
+ }
+
+ if (run_dir != NULL) /* move to the run_dir */
+ if (chdir(run_dir) < 0) {
+ syslog(LOG_DEBUG, "failed to chdir(%s)", run_dir);
+ exit(1);
+ }
+
+ syslog(LOG_DEBUG, "$CWD = %s", getwd(NULL));
+
if (fgets(buf, sizeof (buf), ifp) == NULL) {
if (feof(ifp) == 0)
syslog(LOG_ERR, "Error reading from connection: %s",
@@ -169,6 +697,13 @@ main(int ac, char *av[])
exit(1);
}
+ syslog(LOG_DEBUG, "CMD: (%d)%s\n", buf[0], &buf[1]);
+
+#ifdef DEBUG /* translate [1-5]... messages to \[1-5] to run by hand */
+ if ((buf[0] > '0') && (buf[0] < '6'))
+ buf[0] -= '0';
+#endif
+
if ((buf[0] < 1) || (buf[0] > 5)) {
fatal(ofp, "Invalid protocol request (%d): %c%s\n",
buf[0], buf[0], buf);
@@ -183,6 +718,11 @@ main(int ac, char *av[])
exit(1);
}
+ if (cyclical_service_check(printer) != 0) {
+ fatal(ofp, "%s is cyclical\n", printer);
+ exit(1);
+ }
+
status = papiServiceCreate(&svc, printer, NULL, NULL, NULL,
encryption, NULL);
if (status != PAPI_OK) {
@@ -191,20 +731,19 @@ main(int ac, char *av[])
exit(1);
}
-#ifdef HAVE_IS_SYSTEM_LABELED
- if (is_system_labeled()) {
- int fd = fileno(ifp);
-
- (void) papiServiceSetPeer(svc, fd);
- }
-#endif
+ /*
+ * Trusted Solaris can't be trusting of intermediaries. Pass
+ * the socket connection to the print service to retrieve the
+ * sensativity label off of a multi-level port.
+ */
+ (void) papiServiceSetPeer(svc, fileno(ifp));
switch (buf[0]) {
case '\1': /* restart printer */
ACK(ofp); /* there is no equivalent */
break;
case '\2': /* transfer job(s) */
- berkeley_transfer_files(svc, ifp, ofp, printer);
+ status = berkeley_transfer_files(svc, ifp, ofp, printer);
break;
case '\3': /* show queue (short) */
case '\4': { /* show queue (long) */
@@ -216,10 +755,18 @@ main(int ac, char *av[])
}
break;
case '\5': { /* cancel job(s) */
- char *requestor = *args++;
+ char *user = *args++;
+ char *host = remote_host_name(ifp);
int count;
- status = papiServiceSetUserName(svc, requestor);
+ if (host != NULL) {
+ char buf[BUFSIZ];
+
+ snprintf(buf, sizeof (buf), "%s@%s", user, host);
+ status = papiServiceSetUserName(svc, buf);
+ } else
+ status = papiServiceSetUserName(svc, user);
+
for (count = 0; args[count] != 0; count++);
berkeley_cancel_request(svc, ofp, printer, count, args);
@@ -234,7 +781,9 @@ main(int ac, char *av[])
syslog(LOG_DEBUG, "protocol request(%d) for %s completed: %s",
buf[0], printer, papiStatusString(status));
- syslog(LOG_DEBUG, "detail: %s", verbose_papi_message(svc, status));
+ if (status != PAPI_OK)
+ syslog(LOG_DEBUG, "detail: %s",
+ verbose_papi_message(svc, status));
papiServiceDestroy(svc);