xref: /onnv-gate/usr/src/cmd/lp/model/netpr/bsd_misc.c (revision 3913:661924bac568)
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
5*3913Swendyp  * Common Development and Distribution License (the "License").
6*3913Swendyp  * 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 /*
23*3913Swendyp  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
24*3913Swendyp  * 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 *);
410Sstevel@tonic-gate static char * create_cfname(np_bsdjob_t *);
420Sstevel@tonic-gate static char * create_dfname(np_bsdjob_t *);
430Sstevel@tonic-gate extern char data_file_type;
440Sstevel@tonic-gate 
450Sstevel@tonic-gate np_bsdjob_t *
460Sstevel@tonic-gate 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;
520Sstevel@tonic-gate 	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));
620Sstevel@tonic-gate 	/*
630Sstevel@tonic-gate 	 * request-id comes in as printer-number
640Sstevel@tonic-gate 	 * pull apart to create number
650Sstevel@tonic-gate 	 */
660Sstevel@tonic-gate 	if ((id = strrchr(injob->request_id, (int)'-')) == NULL) {
670Sstevel@tonic-gate 		(void) fprintf(stderr,
680Sstevel@tonic-gate 		gettext("Netpr: request_id in unknown format:<%s>\n"),
690Sstevel@tonic-gate 			injob->request_id);
700Sstevel@tonic-gate 		syslog(LOG_DEBUG, "request id in unknown format: %s",
710Sstevel@tonic-gate 			injob->request_id);
720Sstevel@tonic-gate 		return (NULL);
730Sstevel@tonic-gate 	}
740Sstevel@tonic-gate 
750Sstevel@tonic-gate 	id++;
760Sstevel@tonic-gate 
770Sstevel@tonic-gate 	/*
780Sstevel@tonic-gate 	 * 4261563 - A ID collides with an existing one, it plus
790Sstevel@tonic-gate 	 * 1,000 with the ID causes breaking
800Sstevel@tonic-gate 	 * Max job id for bsd is 999.
810Sstevel@tonic-gate 	 */
820Sstevel@tonic-gate 	job->np_request_id = malloc(4);
830Sstevel@tonic-gate 	ASSERT(job->np_request_id, MALLOC_ERR);
840Sstevel@tonic-gate 	errno = 0;
850Sstevel@tonic-gate 	x = atoi(id);
860Sstevel@tonic-gate 	if ((errno != 0) || (x < 0)) {
870Sstevel@tonic-gate 		x = 0;
880Sstevel@tonic-gate 	}
890Sstevel@tonic-gate 	(void) snprintf(job->np_request_id, (size_t) 4,
900Sstevel@tonic-gate 	    "%.3d", x % 1000);
910Sstevel@tonic-gate 
920Sstevel@tonic-gate 	/* seperate the user/host from host!user or user@host */
930Sstevel@tonic-gate 	if ((id = strchr(injob->username, '@')) != NULL) {
940Sstevel@tonic-gate 		*id++ = '\0';
950Sstevel@tonic-gate 		job->np_username = strdup(injob->username);
960Sstevel@tonic-gate 		job->np_host = strdup(id);
970Sstevel@tonic-gate 		*--id = '@';
980Sstevel@tonic-gate 	} else if ((id = strrchr(injob->username, '!')) != NULL) {
990Sstevel@tonic-gate 		*id++ = '\0';
1000Sstevel@tonic-gate 		job->np_username = strdup(id);
1010Sstevel@tonic-gate 		job->np_host = strdup(injob->username);
1020Sstevel@tonic-gate 		*--id = '!';
1030Sstevel@tonic-gate 	} else {
104*3913Swendyp 		syslog(LOG_DEBUG, "using localhost for user %s",
1050Sstevel@tonic-gate 			injob->username);
106*3913Swendyp 		job->np_username = strdup(injob->username);
107*3913Swendyp 		job->np_host = strdup("localhost");
1080Sstevel@tonic-gate 	}
1090Sstevel@tonic-gate 
1100Sstevel@tonic-gate 	job->np_printer = injob->printer;
1110Sstevel@tonic-gate 	job->np_filename = injob->filename;
1120Sstevel@tonic-gate 
1130Sstevel@tonic-gate 	job->np_df_letter = 'A';
1140Sstevel@tonic-gate 
1150Sstevel@tonic-gate 	/* build cfAfilename: (cfA)(np_request_id)(np_host) */
1160Sstevel@tonic-gate 	if ((job->np_cfAfilename = create_cfname(job)) == NULL) {
1170Sstevel@tonic-gate 		(void) fprintf(stderr,
1180Sstevel@tonic-gate 			gettext("Netpr: System error creating cfAfilename\n"));
1190Sstevel@tonic-gate 			syslog(LOG_DEBUG, "System error creating cfAfilename");
1200Sstevel@tonic-gate 		return (NULL);
1210Sstevel@tonic-gate 	}
1220Sstevel@tonic-gate 
1230Sstevel@tonic-gate 	job->np_timeout = injob->timeout;
1240Sstevel@tonic-gate 	job->np_banner = injob->banner;
1250Sstevel@tonic-gate 	job->np_print_order = pr_order;
1260Sstevel@tonic-gate 
1270Sstevel@tonic-gate 	if (injob->title == NULL)
1280Sstevel@tonic-gate 		job->np_title = injob->filename;
1290Sstevel@tonic-gate 	else
1300Sstevel@tonic-gate 		job->np_title = injob->title;
1310Sstevel@tonic-gate 
1320Sstevel@tonic-gate 	if ((create_cfA_file(job)) == -1) {
1330Sstevel@tonic-gate 		(void) fprintf(stderr,
1340Sstevel@tonic-gate 		gettext("Netpr: Cannot create bsd control file\n"));
1350Sstevel@tonic-gate 		syslog(LOG_DEBUG, "Cannot create bsd control file");
1360Sstevel@tonic-gate 		return (NULL);
1370Sstevel@tonic-gate 	}
1380Sstevel@tonic-gate 
1390Sstevel@tonic-gate 	/* Now we have a title, add to the control file */
1400Sstevel@tonic-gate 	if (injob->banner == BANNER) {
1410Sstevel@tonic-gate 		(void) job_primitive(job, 'C', job->np_host);
1420Sstevel@tonic-gate 		(void) job_primitive(job, 'J', job->np_title);
1430Sstevel@tonic-gate 		(void) job_primitive(job, 'L', job->np_username);
1440Sstevel@tonic-gate 	}
1450Sstevel@tonic-gate 
1460Sstevel@tonic-gate 
1470Sstevel@tonic-gate 	/* create dfname for this file */
1480Sstevel@tonic-gate 
1490Sstevel@tonic-gate 	/* allocate the jobdata and initialize what we have so far */
1500Sstevel@tonic-gate 	jobdata = malloc(sizeof (np_data_t));
1510Sstevel@tonic-gate 	ASSERT(jobdata, MALLOC_ERR);
1520Sstevel@tonic-gate 	(void) memset(jobdata, 0, sizeof (np_data_t));
1530Sstevel@tonic-gate 
1540Sstevel@tonic-gate 	jobdata->np_path_file = malloc(strlen(job->np_filename) + 1);
1550Sstevel@tonic-gate 	ASSERT(jobdata->np_path_file, MALLOC_ERR);
1560Sstevel@tonic-gate 	(void) strcpy(jobdata->np_path_file, job->np_filename);
1570Sstevel@tonic-gate 
1580Sstevel@tonic-gate 	jobdata->np_data_size = filesize;
1590Sstevel@tonic-gate 
1600Sstevel@tonic-gate 	if ((jobdata->np_dfAfilename = create_dfname(job)) == NULL) {
1610Sstevel@tonic-gate 		return (NULL);
1620Sstevel@tonic-gate 	}
1630Sstevel@tonic-gate 
1640Sstevel@tonic-gate 	/*
1650Sstevel@tonic-gate 	 * data_file_type should contain the RFC-1179 control file message
1660Sstevel@tonic-gate 	 * type for the control file.  The is is set via the "-f" option
1670Sstevel@tonic-gate 	 * to netpr, which get it from the "destination-full-control-file-type"
1680Sstevel@tonic-gate 	 * option passed in.  Normally this will be either 'l' or 'f'.
1690Sstevel@tonic-gate 	 */
1700Sstevel@tonic-gate 	if (data_file_type != 0) {
1710Sstevel@tonic-gate 		(void) job_primitive(job, data_file_type,
1720Sstevel@tonic-gate 				jobdata->np_dfAfilename);
1730Sstevel@tonic-gate 		(void) job_primitive(job, 'U', jobdata->np_dfAfilename);
1740Sstevel@tonic-gate 		(void) job_primitive(job, 'N', "print-data");
1750Sstevel@tonic-gate 	}
1760Sstevel@tonic-gate 
1770Sstevel@tonic-gate 	syslog(LOG_DEBUG, "data file info: %s", job->np_cfAfile);
1780Sstevel@tonic-gate 
1790Sstevel@tonic-gate 	/*
1800Sstevel@tonic-gate 	 * attach np_data to bsdjob
1810Sstevel@tonic-gate 	 */
1820Sstevel@tonic-gate 	job->np_data = jobdata;
1830Sstevel@tonic-gate 
1840Sstevel@tonic-gate 	return (job);
1850Sstevel@tonic-gate }
1860Sstevel@tonic-gate 
1870Sstevel@tonic-gate 
1880Sstevel@tonic-gate /*
1890Sstevel@tonic-gate  * Create df<x>name for this file
1900Sstevel@tonic-gate  * df<X><nnn><hostname>
1910Sstevel@tonic-gate  */
1920Sstevel@tonic-gate static char *
1930Sstevel@tonic-gate create_dfname(np_bsdjob_t *job)
1940Sstevel@tonic-gate {
1950Sstevel@tonic-gate 	char * dfname;
1960Sstevel@tonic-gate 
1970Sstevel@tonic-gate 	if (job == NULL)
1980Sstevel@tonic-gate 		return (NULL);
1990Sstevel@tonic-gate 
2000Sstevel@tonic-gate 	/* Trying to print too many files */
2010Sstevel@tonic-gate 	if (job->np_df_letter > 'z') {
2020Sstevel@tonic-gate 		errno = ENFILE;
2030Sstevel@tonic-gate 		return (NULL);
2040Sstevel@tonic-gate 	}
2050Sstevel@tonic-gate 
2060Sstevel@tonic-gate 	dfname = (char *)malloc(strlen(job->np_host) + 3 + 3 + 1);
2070Sstevel@tonic-gate 	ASSERT(dfname, MALLOC_ERR);
2080Sstevel@tonic-gate 	(void) memset(dfname, 0, strlen(job->np_host) + 3 + 3 + 1);
2090Sstevel@tonic-gate 	(void) sprintf(dfname, "%s%c%s%s", "df", job->np_df_letter,
2100Sstevel@tonic-gate 	    job->np_request_id, job->np_host);
2110Sstevel@tonic-gate 
2120Sstevel@tonic-gate 	/* udate np_df_letter for the next caller */
2130Sstevel@tonic-gate 	job->np_df_letter += 1;
2140Sstevel@tonic-gate 	if ((job->np_df_letter > 'Z') && (job->np_df_letter < 'a'))
2150Sstevel@tonic-gate 		job->np_df_letter = 'a';
2160Sstevel@tonic-gate 
2170Sstevel@tonic-gate 	return (dfname);
2180Sstevel@tonic-gate }
2190Sstevel@tonic-gate 
2200Sstevel@tonic-gate static char *
2210Sstevel@tonic-gate create_cfname(np_bsdjob_t * job)
2220Sstevel@tonic-gate {
2230Sstevel@tonic-gate 	char * cfname;
2240Sstevel@tonic-gate 
2250Sstevel@tonic-gate 	if (job == NULL)
2260Sstevel@tonic-gate 		return (NULL);
2270Sstevel@tonic-gate 
2280Sstevel@tonic-gate 	cfname = (char *)malloc(strlen(job->np_host) + 3 + 3 + 1);
2290Sstevel@tonic-gate 	ASSERT(cfname, MALLOC_ERR);
2300Sstevel@tonic-gate 	(void) memset(cfname, 0, strlen(job->np_host) + 3 + 3 + 1);
2310Sstevel@tonic-gate 	(void) sprintf(cfname, "%s%s%s", "cfA",
2320Sstevel@tonic-gate 	job->np_request_id, job->np_host);
2330Sstevel@tonic-gate 	return (cfname);
2340Sstevel@tonic-gate }
2350Sstevel@tonic-gate 
2360Sstevel@tonic-gate static int
2370Sstevel@tonic-gate create_cfA_file(np_bsdjob_t *job)
2380Sstevel@tonic-gate {
2390Sstevel@tonic-gate 	/*
2400Sstevel@tonic-gate 	 * Read through job structure, creating entries
2410Sstevel@tonic-gate 	 * in control file as appropriate
2420Sstevel@tonic-gate 	 */
2430Sstevel@tonic-gate 	if ((job->np_host == NULL) || (job->np_username == NULL)) {
2440Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
2450Sstevel@tonic-gate 		"Netpr: Missing required data, cannot build control file\n"));
2460Sstevel@tonic-gate 		return (-1);
2470Sstevel@tonic-gate 	}
2480Sstevel@tonic-gate 	(void) job_primitive(job, 'H', job->np_host);
2490Sstevel@tonic-gate 	(void) job_primitive(job, 'P', job->np_username);
2500Sstevel@tonic-gate 
2510Sstevel@tonic-gate 	return (0);
2520Sstevel@tonic-gate }
2530Sstevel@tonic-gate 
2540Sstevel@tonic-gate static int
2550Sstevel@tonic-gate job_primitive(np_bsdjob_t * job, char option, char *value)
2560Sstevel@tonic-gate {
2570Sstevel@tonic-gate 	char buf[BUFSIZ];
2580Sstevel@tonic-gate 
2590Sstevel@tonic-gate 	if ((job == NULL) || (value == NULL))
2600Sstevel@tonic-gate 		return (-1);
2610Sstevel@tonic-gate 
2620Sstevel@tonic-gate 	job->np_cfAfilesize += strlen(value) + 2; /* (opt)(value)\n */
2630Sstevel@tonic-gate 	if (job->np_cfAfile == NULL) {
2640Sstevel@tonic-gate 		/* Always allocate one greater than cfAfilesize for the \0 */
2650Sstevel@tonic-gate 		job->np_cfAfile = calloc(1, job->np_cfAfilesize + 1);
2660Sstevel@tonic-gate 		ASSERT(job->np_cfAfile, MALLOC_ERR);
2670Sstevel@tonic-gate 	} else {
2680Sstevel@tonic-gate 		job->np_cfAfile = realloc(job->np_cfAfile,
2690Sstevel@tonic-gate 			job->np_cfAfilesize + 1);
2700Sstevel@tonic-gate 		ASSERT(job->np_cfAfile, REALLOC_ERR);
2710Sstevel@tonic-gate 	}
2720Sstevel@tonic-gate 	(void) snprintf(buf, sizeof (buf),  "%c%s\n", option, value);
2730Sstevel@tonic-gate 	(void) strcat(job->np_cfAfile, buf);
2740Sstevel@tonic-gate 	syslog(LOG_DEBUG, "adding: %d %s", job->np_cfAfilesize, buf);
2750Sstevel@tonic-gate 
2760Sstevel@tonic-gate 	return (0);
2770Sstevel@tonic-gate }
278