xref: /onnv-gate/usr/src/cmd/lp/model/netpr/bsd_misc.c (revision 4062:c84209043eff)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
53913Swendyp  * Common Development and Distribution License (the "License").
63913Swendyp  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate 
220Sstevel@tonic-gate /*
233913Swendyp  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
243913Swendyp  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
280Sstevel@tonic-gate 
290Sstevel@tonic-gate #include <stdio.h>
300Sstevel@tonic-gate #include <stdlib.h>
310Sstevel@tonic-gate #include <libintl.h>
320Sstevel@tonic-gate #include <signal.h>
330Sstevel@tonic-gate #include <errno.h>
340Sstevel@tonic-gate #include <string.h>
350Sstevel@tonic-gate #include <syslog.h>
360Sstevel@tonic-gate #include "netpr.h"
370Sstevel@tonic-gate #include "netdebug.h"
380Sstevel@tonic-gate 
390Sstevel@tonic-gate static int job_primitive(np_bsdjob_t *, char, char *);
400Sstevel@tonic-gate static int create_cfA_file(np_bsdjob_t *);
41*4062Sjacobs static char *create_cfname(np_bsdjob_t *);
42*4062Sjacobs static char *create_dfname(np_bsdjob_t *);
430Sstevel@tonic-gate extern char data_file_type;
440Sstevel@tonic-gate 
450Sstevel@tonic-gate np_bsdjob_t *
create_bsd_job(np_job_t * injob,int pr_order,int filesize)46*4062Sjacobs create_bsd_job(np_job_t *injob, int pr_order, int filesize)
470Sstevel@tonic-gate {
480Sstevel@tonic-gate 
490Sstevel@tonic-gate 	np_bsdjob_t *job;
500Sstevel@tonic-gate 	char *id;
510Sstevel@tonic-gate 	int x;
52*4062Sjacobs 	np_data_t *jobdata;
530Sstevel@tonic-gate 
540Sstevel@tonic-gate 	if ((injob->request_id == NULL) || (injob->username == NULL) ||
550Sstevel@tonic-gate 	    (injob->dest == NULL) || (injob->printer ==  NULL)) {
560Sstevel@tonic-gate 		return (NULL);
570Sstevel@tonic-gate 	}
580Sstevel@tonic-gate 
590Sstevel@tonic-gate 	job = (np_bsdjob_t *)malloc(sizeof (np_bsdjob_t));
600Sstevel@tonic-gate 	ASSERT(job, MALLOC_ERR);
610Sstevel@tonic-gate 	(void) memset(job, 0, sizeof (np_bsdjob_t));
62*4062Sjacobs 	job->np_printer = "auto";	/* default "queue" */
630Sstevel@tonic-gate 	/*
640Sstevel@tonic-gate 	 * request-id comes in as printer-number
650Sstevel@tonic-gate 	 * pull apart to create number
660Sstevel@tonic-gate 	 */
670Sstevel@tonic-gate 	if ((id = strrchr(injob->request_id, (int)'-')) == NULL) {
680Sstevel@tonic-gate 		(void) fprintf(stderr,
690Sstevel@tonic-gate 		gettext("Netpr: request_id in unknown format:<%s>\n"),
700Sstevel@tonic-gate 			injob->request_id);
710Sstevel@tonic-gate 		syslog(LOG_DEBUG, "request id in unknown format: %s",
720Sstevel@tonic-gate 			injob->request_id);
730Sstevel@tonic-gate 		return (NULL);
740Sstevel@tonic-gate 	}
750Sstevel@tonic-gate 
760Sstevel@tonic-gate 	id++;
770Sstevel@tonic-gate 
780Sstevel@tonic-gate 	/*
790Sstevel@tonic-gate 	 * 4261563 - A ID collides with an existing one, it plus
800Sstevel@tonic-gate 	 * 1,000 with the ID causes breaking
810Sstevel@tonic-gate 	 * Max job id for bsd is 999.
820Sstevel@tonic-gate 	 */
830Sstevel@tonic-gate 	job->np_request_id = malloc(4);
840Sstevel@tonic-gate 	ASSERT(job->np_request_id, MALLOC_ERR);
850Sstevel@tonic-gate 	errno = 0;
860Sstevel@tonic-gate 	x = atoi(id);
870Sstevel@tonic-gate 	if ((errno != 0) || (x < 0)) {
880Sstevel@tonic-gate 		x = 0;
890Sstevel@tonic-gate 	}
90*4062Sjacobs 	(void) snprintf(job->np_request_id, (size_t)4,
910Sstevel@tonic-gate 	    "%.3d", x % 1000);
920Sstevel@tonic-gate 
930Sstevel@tonic-gate 	/* seperate the user/host from host!user or user@host */
940Sstevel@tonic-gate 	if ((id = strchr(injob->username, '@')) != NULL) {
950Sstevel@tonic-gate 		*id++ = '\0';
960Sstevel@tonic-gate 		job->np_username = strdup(injob->username);
970Sstevel@tonic-gate 		job->np_host = strdup(id);
980Sstevel@tonic-gate 		*--id = '@';
990Sstevel@tonic-gate 	} else if ((id = strrchr(injob->username, '!')) != NULL) {
1000Sstevel@tonic-gate 		*id++ = '\0';
1010Sstevel@tonic-gate 		job->np_username = strdup(id);
1020Sstevel@tonic-gate 		job->np_host = strdup(injob->username);
1030Sstevel@tonic-gate 		*--id = '!';
1040Sstevel@tonic-gate 	} else {
1053913Swendyp 		syslog(LOG_DEBUG, "using localhost for user %s",
1060Sstevel@tonic-gate 			injob->username);
1073913Swendyp 		job->np_username = strdup(injob->username);
1083913Swendyp 		job->np_host = strdup("localhost");
1090Sstevel@tonic-gate 	}
1100Sstevel@tonic-gate 
1110Sstevel@tonic-gate 	job->np_printer = injob->printer;
1120Sstevel@tonic-gate 	job->np_filename = injob->filename;
1130Sstevel@tonic-gate 
1140Sstevel@tonic-gate 	job->np_df_letter = 'A';
1150Sstevel@tonic-gate 
1160Sstevel@tonic-gate 	/* build cfAfilename: (cfA)(np_request_id)(np_host) */
1170Sstevel@tonic-gate 	if ((job->np_cfAfilename = create_cfname(job)) == NULL) {
1180Sstevel@tonic-gate 		(void) fprintf(stderr,
1190Sstevel@tonic-gate 			gettext("Netpr: System error creating cfAfilename\n"));
1200Sstevel@tonic-gate 			syslog(LOG_DEBUG, "System error creating cfAfilename");
1210Sstevel@tonic-gate 		return (NULL);
1220Sstevel@tonic-gate 	}
1230Sstevel@tonic-gate 
1240Sstevel@tonic-gate 	job->np_timeout = injob->timeout;
1250Sstevel@tonic-gate 	job->np_banner = injob->banner;
1260Sstevel@tonic-gate 	job->np_print_order = pr_order;
1270Sstevel@tonic-gate 
1280Sstevel@tonic-gate 	if (injob->title == NULL)
1290Sstevel@tonic-gate 		job->np_title = injob->filename;
1300Sstevel@tonic-gate 	else
1310Sstevel@tonic-gate 		job->np_title = injob->title;
1320Sstevel@tonic-gate 
1330Sstevel@tonic-gate 	if ((create_cfA_file(job)) == -1) {
1340Sstevel@tonic-gate 		(void) fprintf(stderr,
1350Sstevel@tonic-gate 		gettext("Netpr: Cannot create bsd control file\n"));
1360Sstevel@tonic-gate 		syslog(LOG_DEBUG, "Cannot create bsd control file");
1370Sstevel@tonic-gate 		return (NULL);
1380Sstevel@tonic-gate 	}
1390Sstevel@tonic-gate 
1400Sstevel@tonic-gate 	/* Now we have a title, add to the control file */
1410Sstevel@tonic-gate 	if (injob->banner == BANNER) {
1420Sstevel@tonic-gate 		(void) job_primitive(job, 'C', job->np_host);
1430Sstevel@tonic-gate 		(void) job_primitive(job, 'J', job->np_title);
1440Sstevel@tonic-gate 		(void) job_primitive(job, 'L', job->np_username);
1450Sstevel@tonic-gate 	}
1460Sstevel@tonic-gate 
1470Sstevel@tonic-gate 
1480Sstevel@tonic-gate 	/* create dfname for this file */
1490Sstevel@tonic-gate 
1500Sstevel@tonic-gate 	/* allocate the jobdata and initialize what we have so far */
1510Sstevel@tonic-gate 	jobdata = malloc(sizeof (np_data_t));
1520Sstevel@tonic-gate 	ASSERT(jobdata, MALLOC_ERR);
1530Sstevel@tonic-gate 	(void) memset(jobdata, 0, sizeof (np_data_t));
1540Sstevel@tonic-gate 
1550Sstevel@tonic-gate 	jobdata->np_path_file = malloc(strlen(job->np_filename) + 1);
1560Sstevel@tonic-gate 	ASSERT(jobdata->np_path_file, MALLOC_ERR);
1570Sstevel@tonic-gate 	(void) strcpy(jobdata->np_path_file, job->np_filename);
1580Sstevel@tonic-gate 
1590Sstevel@tonic-gate 	jobdata->np_data_size = filesize;
1600Sstevel@tonic-gate 
1610Sstevel@tonic-gate 	if ((jobdata->np_dfAfilename = create_dfname(job)) == NULL) {
1620Sstevel@tonic-gate 		return (NULL);
1630Sstevel@tonic-gate 	}
1640Sstevel@tonic-gate 
1650Sstevel@tonic-gate 	/*
1660Sstevel@tonic-gate 	 * data_file_type should contain the RFC-1179 control file message
1670Sstevel@tonic-gate 	 * type for the control file.  The is is set via the "-f" option
1680Sstevel@tonic-gate 	 * to netpr, which get it from the "destination-full-control-file-type"
1690Sstevel@tonic-gate 	 * option passed in.  Normally this will be either 'l' or 'f'.
1700Sstevel@tonic-gate 	 */
1710Sstevel@tonic-gate 	if (data_file_type != 0) {
1720Sstevel@tonic-gate 		(void) job_primitive(job, data_file_type,
1730Sstevel@tonic-gate 				jobdata->np_dfAfilename);
1740Sstevel@tonic-gate 		(void) job_primitive(job, 'U', jobdata->np_dfAfilename);
1750Sstevel@tonic-gate 		(void) job_primitive(job, 'N', "print-data");
1760Sstevel@tonic-gate 	}
1770Sstevel@tonic-gate 
1780Sstevel@tonic-gate 	syslog(LOG_DEBUG, "data file info: %s", job->np_cfAfile);
1790Sstevel@tonic-gate 
1800Sstevel@tonic-gate 	/*
1810Sstevel@tonic-gate 	 * attach np_data to bsdjob
1820Sstevel@tonic-gate 	 */
1830Sstevel@tonic-gate 	job->np_data = jobdata;
1840Sstevel@tonic-gate 
1850Sstevel@tonic-gate 	return (job);
1860Sstevel@tonic-gate }
1870Sstevel@tonic-gate 
1880Sstevel@tonic-gate 
1890Sstevel@tonic-gate /*
1900Sstevel@tonic-gate  * Create df<x>name for this file
1910Sstevel@tonic-gate  * df<X><nnn><hostname>
1920Sstevel@tonic-gate  */
1930Sstevel@tonic-gate static char *
create_dfname(np_bsdjob_t * job)1940Sstevel@tonic-gate create_dfname(np_bsdjob_t *job)
1950Sstevel@tonic-gate {
196*4062Sjacobs 	char *dfname;
1970Sstevel@tonic-gate 
1980Sstevel@tonic-gate 	if (job == NULL)
1990Sstevel@tonic-gate 		return (NULL);
2000Sstevel@tonic-gate 
2010Sstevel@tonic-gate 	/* Trying to print too many files */
2020Sstevel@tonic-gate 	if (job->np_df_letter > 'z') {
2030Sstevel@tonic-gate 		errno = ENFILE;
2040Sstevel@tonic-gate 		return (NULL);
2050Sstevel@tonic-gate 	}
2060Sstevel@tonic-gate 
2070Sstevel@tonic-gate 	dfname = (char *)malloc(strlen(job->np_host) + 3 + 3 + 1);
2080Sstevel@tonic-gate 	ASSERT(dfname, MALLOC_ERR);
2090Sstevel@tonic-gate 	(void) memset(dfname, 0, strlen(job->np_host) + 3 + 3 + 1);
2100Sstevel@tonic-gate 	(void) sprintf(dfname, "%s%c%s%s", "df", job->np_df_letter,
2110Sstevel@tonic-gate 	    job->np_request_id, job->np_host);
2120Sstevel@tonic-gate 
2130Sstevel@tonic-gate 	/* udate np_df_letter for the next caller */
2140Sstevel@tonic-gate 	job->np_df_letter += 1;
2150Sstevel@tonic-gate 	if ((job->np_df_letter > 'Z') && (job->np_df_letter < 'a'))
2160Sstevel@tonic-gate 		job->np_df_letter = 'a';
2170Sstevel@tonic-gate 
2180Sstevel@tonic-gate 	return (dfname);
2190Sstevel@tonic-gate }
2200Sstevel@tonic-gate 
2210Sstevel@tonic-gate static char *
create_cfname(np_bsdjob_t * job)222*4062Sjacobs create_cfname(np_bsdjob_t *job)
2230Sstevel@tonic-gate {
224*4062Sjacobs 	char *cfname;
2250Sstevel@tonic-gate 
2260Sstevel@tonic-gate 	if (job == NULL)
2270Sstevel@tonic-gate 		return (NULL);
2280Sstevel@tonic-gate 
2290Sstevel@tonic-gate 	cfname = (char *)malloc(strlen(job->np_host) + 3 + 3 + 1);
2300Sstevel@tonic-gate 	ASSERT(cfname, MALLOC_ERR);
2310Sstevel@tonic-gate 	(void) memset(cfname, 0, strlen(job->np_host) + 3 + 3 + 1);
2320Sstevel@tonic-gate 	(void) sprintf(cfname, "%s%s%s", "cfA",
2330Sstevel@tonic-gate 	job->np_request_id, job->np_host);
2340Sstevel@tonic-gate 	return (cfname);
2350Sstevel@tonic-gate }
2360Sstevel@tonic-gate 
2370Sstevel@tonic-gate static int
create_cfA_file(np_bsdjob_t * job)2380Sstevel@tonic-gate create_cfA_file(np_bsdjob_t *job)
2390Sstevel@tonic-gate {
2400Sstevel@tonic-gate 	/*
2410Sstevel@tonic-gate 	 * Read through job structure, creating entries
2420Sstevel@tonic-gate 	 * in control file as appropriate
2430Sstevel@tonic-gate 	 */
2440Sstevel@tonic-gate 	if ((job->np_host == NULL) || (job->np_username == NULL)) {
2450Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
2460Sstevel@tonic-gate 		"Netpr: Missing required data, cannot build control file\n"));
2470Sstevel@tonic-gate 		return (-1);
2480Sstevel@tonic-gate 	}
2490Sstevel@tonic-gate 	(void) job_primitive(job, 'H', job->np_host);
2500Sstevel@tonic-gate 	(void) job_primitive(job, 'P', job->np_username);
2510Sstevel@tonic-gate 
2520Sstevel@tonic-gate 	return (0);
2530Sstevel@tonic-gate }
2540Sstevel@tonic-gate 
2550Sstevel@tonic-gate static int
job_primitive(np_bsdjob_t * job,char option,char * value)256*4062Sjacobs job_primitive(np_bsdjob_t *job, char option, char *value)
2570Sstevel@tonic-gate {
2580Sstevel@tonic-gate 	char buf[BUFSIZ];
2590Sstevel@tonic-gate 
2600Sstevel@tonic-gate 	if ((job == NULL) || (value == NULL))
2610Sstevel@tonic-gate 		return (-1);
2620Sstevel@tonic-gate 
2630Sstevel@tonic-gate 	job->np_cfAfilesize += strlen(value) + 2; /* (opt)(value)\n */
2640Sstevel@tonic-gate 	if (job->np_cfAfile == NULL) {
2650Sstevel@tonic-gate 		/* Always allocate one greater than cfAfilesize for the \0 */
2660Sstevel@tonic-gate 		job->np_cfAfile = calloc(1, job->np_cfAfilesize + 1);
2670Sstevel@tonic-gate 		ASSERT(job->np_cfAfile, MALLOC_ERR);
2680Sstevel@tonic-gate 	} else {
2690Sstevel@tonic-gate 		job->np_cfAfile = realloc(job->np_cfAfile,
2700Sstevel@tonic-gate 			job->np_cfAfilesize + 1);
2710Sstevel@tonic-gate 		ASSERT(job->np_cfAfile, REALLOC_ERR);
2720Sstevel@tonic-gate 	}
2730Sstevel@tonic-gate 	(void) snprintf(buf, sizeof (buf),  "%c%s\n", option, value);
2740Sstevel@tonic-gate 	(void) strcat(job->np_cfAfile, buf);
2750Sstevel@tonic-gate 	syslog(LOG_DEBUG, "adding: %d %s", job->np_cfAfilesize, buf);
2760Sstevel@tonic-gate 
2770Sstevel@tonic-gate 	return (0);
2780Sstevel@tonic-gate }
279