xref: /onnv-gate/usr/src/cmd/lp/model/netpr/bsd_misc.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate 
23*0Sstevel@tonic-gate /*
24*0Sstevel@tonic-gate  * Copyright (c) 1996 by Sun Microsystems, Inc.
25*0Sstevel@tonic-gate  * All rights reserved.
26*0Sstevel@tonic-gate  */
27*0Sstevel@tonic-gate 
28*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
29*0Sstevel@tonic-gate 
30*0Sstevel@tonic-gate #include <stdio.h>
31*0Sstevel@tonic-gate #include <stdlib.h>
32*0Sstevel@tonic-gate #include <libintl.h>
33*0Sstevel@tonic-gate #include <signal.h>
34*0Sstevel@tonic-gate #include <errno.h>
35*0Sstevel@tonic-gate #include <string.h>
36*0Sstevel@tonic-gate #include <syslog.h>
37*0Sstevel@tonic-gate #include "netpr.h"
38*0Sstevel@tonic-gate #include "netdebug.h"
39*0Sstevel@tonic-gate 
40*0Sstevel@tonic-gate static int job_primitive(np_bsdjob_t *, char, char *);
41*0Sstevel@tonic-gate static int create_cfA_file(np_bsdjob_t *);
42*0Sstevel@tonic-gate static char * create_cfname(np_bsdjob_t *);
43*0Sstevel@tonic-gate static char * create_dfname(np_bsdjob_t *);
44*0Sstevel@tonic-gate extern char data_file_type;
45*0Sstevel@tonic-gate 
46*0Sstevel@tonic-gate np_bsdjob_t *
47*0Sstevel@tonic-gate create_bsd_job(np_job_t * injob, int pr_order, int filesize)
48*0Sstevel@tonic-gate {
49*0Sstevel@tonic-gate 
50*0Sstevel@tonic-gate 	np_bsdjob_t *job;
51*0Sstevel@tonic-gate 	char *id;
52*0Sstevel@tonic-gate 	int x;
53*0Sstevel@tonic-gate 	np_data_t * jobdata;
54*0Sstevel@tonic-gate 
55*0Sstevel@tonic-gate 	if ((injob->request_id == NULL) || (injob->username == NULL) ||
56*0Sstevel@tonic-gate 	    (injob->dest == NULL) || (injob->printer ==  NULL)) {
57*0Sstevel@tonic-gate 		return (NULL);
58*0Sstevel@tonic-gate 	}
59*0Sstevel@tonic-gate 
60*0Sstevel@tonic-gate 	job = (np_bsdjob_t *)malloc(sizeof (np_bsdjob_t));
61*0Sstevel@tonic-gate 	ASSERT(job, MALLOC_ERR);
62*0Sstevel@tonic-gate 	(void) memset(job, 0, sizeof (np_bsdjob_t));
63*0Sstevel@tonic-gate 	/*
64*0Sstevel@tonic-gate 	 * request-id comes in as printer-number
65*0Sstevel@tonic-gate 	 * pull apart to create number
66*0Sstevel@tonic-gate 	 */
67*0Sstevel@tonic-gate 	if ((id = strrchr(injob->request_id, (int)'-')) == NULL) {
68*0Sstevel@tonic-gate 		(void) fprintf(stderr,
69*0Sstevel@tonic-gate 		gettext("Netpr: request_id in unknown format:<%s>\n"),
70*0Sstevel@tonic-gate 			injob->request_id);
71*0Sstevel@tonic-gate 		syslog(LOG_DEBUG, "request id in unknown format: %s",
72*0Sstevel@tonic-gate 			injob->request_id);
73*0Sstevel@tonic-gate 		return (NULL);
74*0Sstevel@tonic-gate 	}
75*0Sstevel@tonic-gate 
76*0Sstevel@tonic-gate 	id++;
77*0Sstevel@tonic-gate 
78*0Sstevel@tonic-gate 	/*
79*0Sstevel@tonic-gate 	 * 4261563 - A ID collides with an existing one, it plus
80*0Sstevel@tonic-gate 	 * 1,000 with the ID causes breaking
81*0Sstevel@tonic-gate 	 * Max job id for bsd is 999.
82*0Sstevel@tonic-gate 	 */
83*0Sstevel@tonic-gate 	job->np_request_id = malloc(4);
84*0Sstevel@tonic-gate 	ASSERT(job->np_request_id, MALLOC_ERR);
85*0Sstevel@tonic-gate 	errno = 0;
86*0Sstevel@tonic-gate 	x = atoi(id);
87*0Sstevel@tonic-gate 	if ((errno != 0) || (x < 0)) {
88*0Sstevel@tonic-gate 		x = 0;
89*0Sstevel@tonic-gate 	}
90*0Sstevel@tonic-gate 	(void) snprintf(job->np_request_id, (size_t) 4,
91*0Sstevel@tonic-gate 	    "%.3d", x % 1000);
92*0Sstevel@tonic-gate 
93*0Sstevel@tonic-gate 	/* seperate the user/host from host!user or user@host */
94*0Sstevel@tonic-gate 	if ((id = strchr(injob->username, '@')) != NULL) {
95*0Sstevel@tonic-gate 		*id++ = '\0';
96*0Sstevel@tonic-gate 		job->np_username = strdup(injob->username);
97*0Sstevel@tonic-gate 		job->np_host = strdup(id);
98*0Sstevel@tonic-gate 		*--id = '@';
99*0Sstevel@tonic-gate 	} else if ((id = strrchr(injob->username, '!')) != NULL) {
100*0Sstevel@tonic-gate 		*id++ = '\0';
101*0Sstevel@tonic-gate 		job->np_username = strdup(id);
102*0Sstevel@tonic-gate 		job->np_host = strdup(injob->username);
103*0Sstevel@tonic-gate 		*--id = '!';
104*0Sstevel@tonic-gate 	} else {
105*0Sstevel@tonic-gate 		(void) fprintf(stderr,
106*0Sstevel@tonic-gate 		gettext("Netpr: username in unknown format:<%s>\n"),
107*0Sstevel@tonic-gate 			injob->username);
108*0Sstevel@tonic-gate 		syslog(LOG_DEBUG, "username in unknown format: %s",
109*0Sstevel@tonic-gate 			injob->username);
110*0Sstevel@tonic-gate 		return (NULL);
111*0Sstevel@tonic-gate 	}
112*0Sstevel@tonic-gate 
113*0Sstevel@tonic-gate 	job->np_printer = injob->printer;
114*0Sstevel@tonic-gate 	job->np_filename = injob->filename;
115*0Sstevel@tonic-gate 
116*0Sstevel@tonic-gate 	job->np_df_letter = 'A';
117*0Sstevel@tonic-gate 
118*0Sstevel@tonic-gate 	/* build cfAfilename: (cfA)(np_request_id)(np_host) */
119*0Sstevel@tonic-gate 	if ((job->np_cfAfilename = create_cfname(job)) == NULL) {
120*0Sstevel@tonic-gate 		(void) fprintf(stderr,
121*0Sstevel@tonic-gate 			gettext("Netpr: System error creating cfAfilename\n"));
122*0Sstevel@tonic-gate 			syslog(LOG_DEBUG, "System error creating cfAfilename");
123*0Sstevel@tonic-gate 		return (NULL);
124*0Sstevel@tonic-gate 	}
125*0Sstevel@tonic-gate 
126*0Sstevel@tonic-gate 	job->np_timeout = injob->timeout;
127*0Sstevel@tonic-gate 	job->np_banner = injob->banner;
128*0Sstevel@tonic-gate 	job->np_print_order = pr_order;
129*0Sstevel@tonic-gate 
130*0Sstevel@tonic-gate 	if (injob->title == NULL)
131*0Sstevel@tonic-gate 		job->np_title = injob->filename;
132*0Sstevel@tonic-gate 	else
133*0Sstevel@tonic-gate 		job->np_title = injob->title;
134*0Sstevel@tonic-gate 
135*0Sstevel@tonic-gate 	if ((create_cfA_file(job)) == -1) {
136*0Sstevel@tonic-gate 		(void) fprintf(stderr,
137*0Sstevel@tonic-gate 		gettext("Netpr: Cannot create bsd control file\n"));
138*0Sstevel@tonic-gate 		syslog(LOG_DEBUG, "Cannot create bsd control file");
139*0Sstevel@tonic-gate 		return (NULL);
140*0Sstevel@tonic-gate 	}
141*0Sstevel@tonic-gate 
142*0Sstevel@tonic-gate 	/* Now we have a title, add to the control file */
143*0Sstevel@tonic-gate 	if (injob->banner == BANNER) {
144*0Sstevel@tonic-gate 		(void) job_primitive(job, 'C', job->np_host);
145*0Sstevel@tonic-gate 		(void) job_primitive(job, 'J', job->np_title);
146*0Sstevel@tonic-gate 		(void) job_primitive(job, 'L', job->np_username);
147*0Sstevel@tonic-gate 	}
148*0Sstevel@tonic-gate 
149*0Sstevel@tonic-gate 
150*0Sstevel@tonic-gate 	/* create dfname for this file */
151*0Sstevel@tonic-gate 
152*0Sstevel@tonic-gate 	/* allocate the jobdata and initialize what we have so far */
153*0Sstevel@tonic-gate 	jobdata = malloc(sizeof (np_data_t));
154*0Sstevel@tonic-gate 	ASSERT(jobdata, MALLOC_ERR);
155*0Sstevel@tonic-gate 	(void) memset(jobdata, 0, sizeof (np_data_t));
156*0Sstevel@tonic-gate 
157*0Sstevel@tonic-gate 	jobdata->np_path_file = malloc(strlen(job->np_filename) + 1);
158*0Sstevel@tonic-gate 	ASSERT(jobdata->np_path_file, MALLOC_ERR);
159*0Sstevel@tonic-gate 	(void) strcpy(jobdata->np_path_file, job->np_filename);
160*0Sstevel@tonic-gate 
161*0Sstevel@tonic-gate 	jobdata->np_data_size = filesize;
162*0Sstevel@tonic-gate 
163*0Sstevel@tonic-gate 	if ((jobdata->np_dfAfilename = create_dfname(job)) == NULL) {
164*0Sstevel@tonic-gate 		return (NULL);
165*0Sstevel@tonic-gate 	}
166*0Sstevel@tonic-gate 
167*0Sstevel@tonic-gate 	/*
168*0Sstevel@tonic-gate 	 * data_file_type should contain the RFC-1179 control file message
169*0Sstevel@tonic-gate 	 * type for the control file.  The is is set via the "-f" option
170*0Sstevel@tonic-gate 	 * to netpr, which get it from the "destination-full-control-file-type"
171*0Sstevel@tonic-gate 	 * option passed in.  Normally this will be either 'l' or 'f'.
172*0Sstevel@tonic-gate 	 */
173*0Sstevel@tonic-gate 	if (data_file_type != 0) {
174*0Sstevel@tonic-gate 		(void) job_primitive(job, data_file_type,
175*0Sstevel@tonic-gate 				jobdata->np_dfAfilename);
176*0Sstevel@tonic-gate 		(void) job_primitive(job, 'U', jobdata->np_dfAfilename);
177*0Sstevel@tonic-gate 		(void) job_primitive(job, 'N', "print-data");
178*0Sstevel@tonic-gate 	}
179*0Sstevel@tonic-gate 
180*0Sstevel@tonic-gate 	syslog(LOG_DEBUG, "data file info: %s", job->np_cfAfile);
181*0Sstevel@tonic-gate 
182*0Sstevel@tonic-gate 	/*
183*0Sstevel@tonic-gate 	 * attach np_data to bsdjob
184*0Sstevel@tonic-gate 	 */
185*0Sstevel@tonic-gate 	job->np_data = jobdata;
186*0Sstevel@tonic-gate 
187*0Sstevel@tonic-gate 	return (job);
188*0Sstevel@tonic-gate }
189*0Sstevel@tonic-gate 
190*0Sstevel@tonic-gate 
191*0Sstevel@tonic-gate /*
192*0Sstevel@tonic-gate  * Create df<x>name for this file
193*0Sstevel@tonic-gate  * df<X><nnn><hostname>
194*0Sstevel@tonic-gate  */
195*0Sstevel@tonic-gate static char *
196*0Sstevel@tonic-gate create_dfname(np_bsdjob_t *job)
197*0Sstevel@tonic-gate {
198*0Sstevel@tonic-gate 	char * dfname;
199*0Sstevel@tonic-gate 
200*0Sstevel@tonic-gate 	if (job == NULL)
201*0Sstevel@tonic-gate 		return (NULL);
202*0Sstevel@tonic-gate 
203*0Sstevel@tonic-gate 	/* Trying to print too many files */
204*0Sstevel@tonic-gate 	if (job->np_df_letter > 'z') {
205*0Sstevel@tonic-gate 		errno = ENFILE;
206*0Sstevel@tonic-gate 		return (NULL);
207*0Sstevel@tonic-gate 	}
208*0Sstevel@tonic-gate 
209*0Sstevel@tonic-gate 	dfname = (char *)malloc(strlen(job->np_host) + 3 + 3 + 1);
210*0Sstevel@tonic-gate 	ASSERT(dfname, MALLOC_ERR);
211*0Sstevel@tonic-gate 	(void) memset(dfname, 0, strlen(job->np_host) + 3 + 3 + 1);
212*0Sstevel@tonic-gate 	(void) sprintf(dfname, "%s%c%s%s", "df", job->np_df_letter,
213*0Sstevel@tonic-gate 	    job->np_request_id, job->np_host);
214*0Sstevel@tonic-gate 
215*0Sstevel@tonic-gate 	/* udate np_df_letter for the next caller */
216*0Sstevel@tonic-gate 	job->np_df_letter += 1;
217*0Sstevel@tonic-gate 	if ((job->np_df_letter > 'Z') && (job->np_df_letter < 'a'))
218*0Sstevel@tonic-gate 		job->np_df_letter = 'a';
219*0Sstevel@tonic-gate 
220*0Sstevel@tonic-gate 	return (dfname);
221*0Sstevel@tonic-gate }
222*0Sstevel@tonic-gate 
223*0Sstevel@tonic-gate static char *
224*0Sstevel@tonic-gate create_cfname(np_bsdjob_t * job)
225*0Sstevel@tonic-gate {
226*0Sstevel@tonic-gate 	char * cfname;
227*0Sstevel@tonic-gate 
228*0Sstevel@tonic-gate 	if (job == NULL)
229*0Sstevel@tonic-gate 		return (NULL);
230*0Sstevel@tonic-gate 
231*0Sstevel@tonic-gate 	cfname = (char *)malloc(strlen(job->np_host) + 3 + 3 + 1);
232*0Sstevel@tonic-gate 	ASSERT(cfname, MALLOC_ERR);
233*0Sstevel@tonic-gate 	(void) memset(cfname, 0, strlen(job->np_host) + 3 + 3 + 1);
234*0Sstevel@tonic-gate 	(void) sprintf(cfname, "%s%s%s", "cfA",
235*0Sstevel@tonic-gate 	job->np_request_id, job->np_host);
236*0Sstevel@tonic-gate 	return (cfname);
237*0Sstevel@tonic-gate }
238*0Sstevel@tonic-gate 
239*0Sstevel@tonic-gate static int
240*0Sstevel@tonic-gate create_cfA_file(np_bsdjob_t *job)
241*0Sstevel@tonic-gate {
242*0Sstevel@tonic-gate 	/*
243*0Sstevel@tonic-gate 	 * Read through job structure, creating entries
244*0Sstevel@tonic-gate 	 * in control file as appropriate
245*0Sstevel@tonic-gate 	 */
246*0Sstevel@tonic-gate 	if ((job->np_host == NULL) || (job->np_username == NULL)) {
247*0Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
248*0Sstevel@tonic-gate 		"Netpr: Missing required data, cannot build control file\n"));
249*0Sstevel@tonic-gate 		return (-1);
250*0Sstevel@tonic-gate 	}
251*0Sstevel@tonic-gate 	(void) job_primitive(job, 'H', job->np_host);
252*0Sstevel@tonic-gate 	(void) job_primitive(job, 'P', job->np_username);
253*0Sstevel@tonic-gate 
254*0Sstevel@tonic-gate 	return (0);
255*0Sstevel@tonic-gate }
256*0Sstevel@tonic-gate 
257*0Sstevel@tonic-gate static int
258*0Sstevel@tonic-gate job_primitive(np_bsdjob_t * job, char option, char *value)
259*0Sstevel@tonic-gate {
260*0Sstevel@tonic-gate 	char buf[BUFSIZ];
261*0Sstevel@tonic-gate 
262*0Sstevel@tonic-gate 	if ((job == NULL) || (value == NULL))
263*0Sstevel@tonic-gate 		return (-1);
264*0Sstevel@tonic-gate 
265*0Sstevel@tonic-gate 	job->np_cfAfilesize += strlen(value) + 2; /* (opt)(value)\n */
266*0Sstevel@tonic-gate 	if (job->np_cfAfile == NULL) {
267*0Sstevel@tonic-gate 		/* Always allocate one greater than cfAfilesize for the \0 */
268*0Sstevel@tonic-gate 		job->np_cfAfile = calloc(1, job->np_cfAfilesize + 1);
269*0Sstevel@tonic-gate 		ASSERT(job->np_cfAfile, MALLOC_ERR);
270*0Sstevel@tonic-gate 	} else {
271*0Sstevel@tonic-gate 		job->np_cfAfile = realloc(job->np_cfAfile,
272*0Sstevel@tonic-gate 			job->np_cfAfilesize + 1);
273*0Sstevel@tonic-gate 		ASSERT(job->np_cfAfile, REALLOC_ERR);
274*0Sstevel@tonic-gate 	}
275*0Sstevel@tonic-gate 	(void) snprintf(buf, sizeof (buf),  "%c%s\n", option, value);
276*0Sstevel@tonic-gate 	(void) strcat(job->np_cfAfile, buf);
277*0Sstevel@tonic-gate 	syslog(LOG_DEBUG, "adding: %d %s", job->np_cfAfilesize, buf);
278*0Sstevel@tonic-gate 
279*0Sstevel@tonic-gate 	return (0);
280*0Sstevel@tonic-gate }
281