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
5e8622743Swendyp * Common Development and Distribution License (the "License").
6e8622743Swendyp * 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
227c478bd9Sstevel@tonic-gate /*
23e8622743Swendyp * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
24e8622743Swendyp * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate */
267c478bd9Sstevel@tonic-gate
277c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
287c478bd9Sstevel@tonic-gate
297c478bd9Sstevel@tonic-gate #include <stdio.h>
307c478bd9Sstevel@tonic-gate #include <stdlib.h>
317c478bd9Sstevel@tonic-gate #include <libintl.h>
327c478bd9Sstevel@tonic-gate #include <signal.h>
337c478bd9Sstevel@tonic-gate #include <errno.h>
347c478bd9Sstevel@tonic-gate #include <string.h>
357c478bd9Sstevel@tonic-gate #include <syslog.h>
367c478bd9Sstevel@tonic-gate #include "netpr.h"
377c478bd9Sstevel@tonic-gate #include "netdebug.h"
387c478bd9Sstevel@tonic-gate
397c478bd9Sstevel@tonic-gate static int job_primitive(np_bsdjob_t *, char, char *);
407c478bd9Sstevel@tonic-gate static int create_cfA_file(np_bsdjob_t *);
417c478bd9Sstevel@tonic-gate static char *create_cfname(np_bsdjob_t *);
427c478bd9Sstevel@tonic-gate static char *create_dfname(np_bsdjob_t *);
437c478bd9Sstevel@tonic-gate extern char data_file_type;
447c478bd9Sstevel@tonic-gate
457c478bd9Sstevel@tonic-gate np_bsdjob_t *
create_bsd_job(np_job_t * injob,int pr_order,int filesize)467c478bd9Sstevel@tonic-gate create_bsd_job(np_job_t *injob, int pr_order, int filesize)
477c478bd9Sstevel@tonic-gate {
487c478bd9Sstevel@tonic-gate
497c478bd9Sstevel@tonic-gate np_bsdjob_t *job;
507c478bd9Sstevel@tonic-gate char *id;
517c478bd9Sstevel@tonic-gate int x;
527c478bd9Sstevel@tonic-gate np_data_t *jobdata;
537c478bd9Sstevel@tonic-gate
547c478bd9Sstevel@tonic-gate if ((injob->request_id == NULL) || (injob->username == NULL) ||
557c478bd9Sstevel@tonic-gate (injob->dest == NULL) || (injob->printer == NULL)) {
567c478bd9Sstevel@tonic-gate return (NULL);
577c478bd9Sstevel@tonic-gate }
587c478bd9Sstevel@tonic-gate
597c478bd9Sstevel@tonic-gate job = (np_bsdjob_t *)malloc(sizeof (np_bsdjob_t));
607c478bd9Sstevel@tonic-gate ASSERT(job, MALLOC_ERR);
617c478bd9Sstevel@tonic-gate (void) memset(job, 0, sizeof (np_bsdjob_t));
62*022ba35cSjacobs job->np_printer = "auto"; /* default "queue" */
637c478bd9Sstevel@tonic-gate /*
647c478bd9Sstevel@tonic-gate * request-id comes in as printer-number
657c478bd9Sstevel@tonic-gate * pull apart to create number
667c478bd9Sstevel@tonic-gate */
677c478bd9Sstevel@tonic-gate if ((id = strrchr(injob->request_id, (int)'-')) == NULL) {
687c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
697c478bd9Sstevel@tonic-gate gettext("Netpr: request_id in unknown format:<%s>\n"),
707c478bd9Sstevel@tonic-gate injob->request_id);
717c478bd9Sstevel@tonic-gate syslog(LOG_DEBUG, "request id in unknown format: %s",
727c478bd9Sstevel@tonic-gate injob->request_id);
737c478bd9Sstevel@tonic-gate return (NULL);
747c478bd9Sstevel@tonic-gate }
757c478bd9Sstevel@tonic-gate
767c478bd9Sstevel@tonic-gate id++;
777c478bd9Sstevel@tonic-gate
787c478bd9Sstevel@tonic-gate /*
797c478bd9Sstevel@tonic-gate * 4261563 - A ID collides with an existing one, it plus
807c478bd9Sstevel@tonic-gate * 1,000 with the ID causes breaking
817c478bd9Sstevel@tonic-gate * Max job id for bsd is 999.
827c478bd9Sstevel@tonic-gate */
837c478bd9Sstevel@tonic-gate job->np_request_id = malloc(4);
847c478bd9Sstevel@tonic-gate ASSERT(job->np_request_id, MALLOC_ERR);
857c478bd9Sstevel@tonic-gate errno = 0;
867c478bd9Sstevel@tonic-gate x = atoi(id);
877c478bd9Sstevel@tonic-gate if ((errno != 0) || (x < 0)) {
887c478bd9Sstevel@tonic-gate x = 0;
897c478bd9Sstevel@tonic-gate }
907c478bd9Sstevel@tonic-gate (void) snprintf(job->np_request_id, (size_t)4,
917c478bd9Sstevel@tonic-gate "%.3d", x % 1000);
927c478bd9Sstevel@tonic-gate
937c478bd9Sstevel@tonic-gate /* seperate the user/host from host!user or user@host */
947c478bd9Sstevel@tonic-gate if ((id = strchr(injob->username, '@')) != NULL) {
957c478bd9Sstevel@tonic-gate *id++ = '\0';
967c478bd9Sstevel@tonic-gate job->np_username = strdup(injob->username);
977c478bd9Sstevel@tonic-gate job->np_host = strdup(id);
987c478bd9Sstevel@tonic-gate *--id = '@';
997c478bd9Sstevel@tonic-gate } else if ((id = strrchr(injob->username, '!')) != NULL) {
1007c478bd9Sstevel@tonic-gate *id++ = '\0';
1017c478bd9Sstevel@tonic-gate job->np_username = strdup(id);
1027c478bd9Sstevel@tonic-gate job->np_host = strdup(injob->username);
1037c478bd9Sstevel@tonic-gate *--id = '!';
1047c478bd9Sstevel@tonic-gate } else {
105e8622743Swendyp syslog(LOG_DEBUG, "using localhost for user %s",
1067c478bd9Sstevel@tonic-gate injob->username);
107e8622743Swendyp job->np_username = strdup(injob->username);
108e8622743Swendyp job->np_host = strdup("localhost");
1097c478bd9Sstevel@tonic-gate }
1107c478bd9Sstevel@tonic-gate
1117c478bd9Sstevel@tonic-gate job->np_printer = injob->printer;
1127c478bd9Sstevel@tonic-gate job->np_filename = injob->filename;
1137c478bd9Sstevel@tonic-gate
1147c478bd9Sstevel@tonic-gate job->np_df_letter = 'A';
1157c478bd9Sstevel@tonic-gate
1167c478bd9Sstevel@tonic-gate /* build cfAfilename: (cfA)(np_request_id)(np_host) */
1177c478bd9Sstevel@tonic-gate if ((job->np_cfAfilename = create_cfname(job)) == NULL) {
1187c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
1197c478bd9Sstevel@tonic-gate gettext("Netpr: System error creating cfAfilename\n"));
1207c478bd9Sstevel@tonic-gate syslog(LOG_DEBUG, "System error creating cfAfilename");
1217c478bd9Sstevel@tonic-gate return (NULL);
1227c478bd9Sstevel@tonic-gate }
1237c478bd9Sstevel@tonic-gate
1247c478bd9Sstevel@tonic-gate job->np_timeout = injob->timeout;
1257c478bd9Sstevel@tonic-gate job->np_banner = injob->banner;
1267c478bd9Sstevel@tonic-gate job->np_print_order = pr_order;
1277c478bd9Sstevel@tonic-gate
1287c478bd9Sstevel@tonic-gate if (injob->title == NULL)
1297c478bd9Sstevel@tonic-gate job->np_title = injob->filename;
1307c478bd9Sstevel@tonic-gate else
1317c478bd9Sstevel@tonic-gate job->np_title = injob->title;
1327c478bd9Sstevel@tonic-gate
1337c478bd9Sstevel@tonic-gate if ((create_cfA_file(job)) == -1) {
1347c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
1357c478bd9Sstevel@tonic-gate gettext("Netpr: Cannot create bsd control file\n"));
1367c478bd9Sstevel@tonic-gate syslog(LOG_DEBUG, "Cannot create bsd control file");
1377c478bd9Sstevel@tonic-gate return (NULL);
1387c478bd9Sstevel@tonic-gate }
1397c478bd9Sstevel@tonic-gate
1407c478bd9Sstevel@tonic-gate /* Now we have a title, add to the control file */
1417c478bd9Sstevel@tonic-gate if (injob->banner == BANNER) {
1427c478bd9Sstevel@tonic-gate (void) job_primitive(job, 'C', job->np_host);
1437c478bd9Sstevel@tonic-gate (void) job_primitive(job, 'J', job->np_title);
1447c478bd9Sstevel@tonic-gate (void) job_primitive(job, 'L', job->np_username);
1457c478bd9Sstevel@tonic-gate }
1467c478bd9Sstevel@tonic-gate
1477c478bd9Sstevel@tonic-gate
1487c478bd9Sstevel@tonic-gate /* create dfname for this file */
1497c478bd9Sstevel@tonic-gate
1507c478bd9Sstevel@tonic-gate /* allocate the jobdata and initialize what we have so far */
1517c478bd9Sstevel@tonic-gate jobdata = malloc(sizeof (np_data_t));
1527c478bd9Sstevel@tonic-gate ASSERT(jobdata, MALLOC_ERR);
1537c478bd9Sstevel@tonic-gate (void) memset(jobdata, 0, sizeof (np_data_t));
1547c478bd9Sstevel@tonic-gate
1557c478bd9Sstevel@tonic-gate jobdata->np_path_file = malloc(strlen(job->np_filename) + 1);
1567c478bd9Sstevel@tonic-gate ASSERT(jobdata->np_path_file, MALLOC_ERR);
1577c478bd9Sstevel@tonic-gate (void) strcpy(jobdata->np_path_file, job->np_filename);
1587c478bd9Sstevel@tonic-gate
1597c478bd9Sstevel@tonic-gate jobdata->np_data_size = filesize;
1607c478bd9Sstevel@tonic-gate
1617c478bd9Sstevel@tonic-gate if ((jobdata->np_dfAfilename = create_dfname(job)) == NULL) {
1627c478bd9Sstevel@tonic-gate return (NULL);
1637c478bd9Sstevel@tonic-gate }
1647c478bd9Sstevel@tonic-gate
1657c478bd9Sstevel@tonic-gate /*
1667c478bd9Sstevel@tonic-gate * data_file_type should contain the RFC-1179 control file message
1677c478bd9Sstevel@tonic-gate * type for the control file. The is is set via the "-f" option
1687c478bd9Sstevel@tonic-gate * to netpr, which get it from the "destination-full-control-file-type"
1697c478bd9Sstevel@tonic-gate * option passed in. Normally this will be either 'l' or 'f'.
1707c478bd9Sstevel@tonic-gate */
1717c478bd9Sstevel@tonic-gate if (data_file_type != 0) {
1727c478bd9Sstevel@tonic-gate (void) job_primitive(job, data_file_type,
1737c478bd9Sstevel@tonic-gate jobdata->np_dfAfilename);
1747c478bd9Sstevel@tonic-gate (void) job_primitive(job, 'U', jobdata->np_dfAfilename);
1757c478bd9Sstevel@tonic-gate (void) job_primitive(job, 'N', "print-data");
1767c478bd9Sstevel@tonic-gate }
1777c478bd9Sstevel@tonic-gate
1787c478bd9Sstevel@tonic-gate syslog(LOG_DEBUG, "data file info: %s", job->np_cfAfile);
1797c478bd9Sstevel@tonic-gate
1807c478bd9Sstevel@tonic-gate /*
1817c478bd9Sstevel@tonic-gate * attach np_data to bsdjob
1827c478bd9Sstevel@tonic-gate */
1837c478bd9Sstevel@tonic-gate job->np_data = jobdata;
1847c478bd9Sstevel@tonic-gate
1857c478bd9Sstevel@tonic-gate return (job);
1867c478bd9Sstevel@tonic-gate }
1877c478bd9Sstevel@tonic-gate
1887c478bd9Sstevel@tonic-gate
1897c478bd9Sstevel@tonic-gate /*
1907c478bd9Sstevel@tonic-gate * Create df<x>name for this file
1917c478bd9Sstevel@tonic-gate * df<X><nnn><hostname>
1927c478bd9Sstevel@tonic-gate */
1937c478bd9Sstevel@tonic-gate static char *
create_dfname(np_bsdjob_t * job)1947c478bd9Sstevel@tonic-gate create_dfname(np_bsdjob_t *job)
1957c478bd9Sstevel@tonic-gate {
1967c478bd9Sstevel@tonic-gate char *dfname;
1977c478bd9Sstevel@tonic-gate
1987c478bd9Sstevel@tonic-gate if (job == NULL)
1997c478bd9Sstevel@tonic-gate return (NULL);
2007c478bd9Sstevel@tonic-gate
2017c478bd9Sstevel@tonic-gate /* Trying to print too many files */
2027c478bd9Sstevel@tonic-gate if (job->np_df_letter > 'z') {
2037c478bd9Sstevel@tonic-gate errno = ENFILE;
2047c478bd9Sstevel@tonic-gate return (NULL);
2057c478bd9Sstevel@tonic-gate }
2067c478bd9Sstevel@tonic-gate
2077c478bd9Sstevel@tonic-gate dfname = (char *)malloc(strlen(job->np_host) + 3 + 3 + 1);
2087c478bd9Sstevel@tonic-gate ASSERT(dfname, MALLOC_ERR);
2097c478bd9Sstevel@tonic-gate (void) memset(dfname, 0, strlen(job->np_host) + 3 + 3 + 1);
2107c478bd9Sstevel@tonic-gate (void) sprintf(dfname, "%s%c%s%s", "df", job->np_df_letter,
2117c478bd9Sstevel@tonic-gate job->np_request_id, job->np_host);
2127c478bd9Sstevel@tonic-gate
2137c478bd9Sstevel@tonic-gate /* udate np_df_letter for the next caller */
2147c478bd9Sstevel@tonic-gate job->np_df_letter += 1;
2157c478bd9Sstevel@tonic-gate if ((job->np_df_letter > 'Z') && (job->np_df_letter < 'a'))
2167c478bd9Sstevel@tonic-gate job->np_df_letter = 'a';
2177c478bd9Sstevel@tonic-gate
2187c478bd9Sstevel@tonic-gate return (dfname);
2197c478bd9Sstevel@tonic-gate }
2207c478bd9Sstevel@tonic-gate
2217c478bd9Sstevel@tonic-gate static char *
create_cfname(np_bsdjob_t * job)2227c478bd9Sstevel@tonic-gate create_cfname(np_bsdjob_t *job)
2237c478bd9Sstevel@tonic-gate {
2247c478bd9Sstevel@tonic-gate char *cfname;
2257c478bd9Sstevel@tonic-gate
2267c478bd9Sstevel@tonic-gate if (job == NULL)
2277c478bd9Sstevel@tonic-gate return (NULL);
2287c478bd9Sstevel@tonic-gate
2297c478bd9Sstevel@tonic-gate cfname = (char *)malloc(strlen(job->np_host) + 3 + 3 + 1);
2307c478bd9Sstevel@tonic-gate ASSERT(cfname, MALLOC_ERR);
2317c478bd9Sstevel@tonic-gate (void) memset(cfname, 0, strlen(job->np_host) + 3 + 3 + 1);
2327c478bd9Sstevel@tonic-gate (void) sprintf(cfname, "%s%s%s", "cfA",
2337c478bd9Sstevel@tonic-gate job->np_request_id, job->np_host);
2347c478bd9Sstevel@tonic-gate return (cfname);
2357c478bd9Sstevel@tonic-gate }
2367c478bd9Sstevel@tonic-gate
2377c478bd9Sstevel@tonic-gate static int
create_cfA_file(np_bsdjob_t * job)2387c478bd9Sstevel@tonic-gate create_cfA_file(np_bsdjob_t *job)
2397c478bd9Sstevel@tonic-gate {
2407c478bd9Sstevel@tonic-gate /*
2417c478bd9Sstevel@tonic-gate * Read through job structure, creating entries
2427c478bd9Sstevel@tonic-gate * in control file as appropriate
2437c478bd9Sstevel@tonic-gate */
2447c478bd9Sstevel@tonic-gate if ((job->np_host == NULL) || (job->np_username == NULL)) {
2457c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext(
2467c478bd9Sstevel@tonic-gate "Netpr: Missing required data, cannot build control file\n"));
2477c478bd9Sstevel@tonic-gate return (-1);
2487c478bd9Sstevel@tonic-gate }
2497c478bd9Sstevel@tonic-gate (void) job_primitive(job, 'H', job->np_host);
2507c478bd9Sstevel@tonic-gate (void) job_primitive(job, 'P', job->np_username);
2517c478bd9Sstevel@tonic-gate
2527c478bd9Sstevel@tonic-gate return (0);
2537c478bd9Sstevel@tonic-gate }
2547c478bd9Sstevel@tonic-gate
2557c478bd9Sstevel@tonic-gate static int
job_primitive(np_bsdjob_t * job,char option,char * value)2567c478bd9Sstevel@tonic-gate job_primitive(np_bsdjob_t *job, char option, char *value)
2577c478bd9Sstevel@tonic-gate {
2587c478bd9Sstevel@tonic-gate char buf[BUFSIZ];
2597c478bd9Sstevel@tonic-gate
2607c478bd9Sstevel@tonic-gate if ((job == NULL) || (value == NULL))
2617c478bd9Sstevel@tonic-gate return (-1);
2627c478bd9Sstevel@tonic-gate
2637c478bd9Sstevel@tonic-gate job->np_cfAfilesize += strlen(value) + 2; /* (opt)(value)\n */
2647c478bd9Sstevel@tonic-gate if (job->np_cfAfile == NULL) {
2657c478bd9Sstevel@tonic-gate /* Always allocate one greater than cfAfilesize for the \0 */
2667c478bd9Sstevel@tonic-gate job->np_cfAfile = calloc(1, job->np_cfAfilesize + 1);
2677c478bd9Sstevel@tonic-gate ASSERT(job->np_cfAfile, MALLOC_ERR);
2687c478bd9Sstevel@tonic-gate } else {
2697c478bd9Sstevel@tonic-gate job->np_cfAfile = realloc(job->np_cfAfile,
2707c478bd9Sstevel@tonic-gate job->np_cfAfilesize + 1);
2717c478bd9Sstevel@tonic-gate ASSERT(job->np_cfAfile, REALLOC_ERR);
2727c478bd9Sstevel@tonic-gate }
2737c478bd9Sstevel@tonic-gate (void) snprintf(buf, sizeof (buf), "%c%s\n", option, value);
2747c478bd9Sstevel@tonic-gate (void) strcat(job->np_cfAfile, buf);
2757c478bd9Sstevel@tonic-gate syslog(LOG_DEBUG, "adding: %d %s", job->np_cfAfilesize, buf);
2767c478bd9Sstevel@tonic-gate
2777c478bd9Sstevel@tonic-gate return (0);
2787c478bd9Sstevel@tonic-gate }
279