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