xref: /titanic_44/usr/src/cmd/lp/lib/papi/lpsched-jobs.c (revision 03494a9880d80f834bec10a1e8f0a2f8f7c97bf4)
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 (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 /*LINTLIBRARY*/
27 
28 #include <stdlib.h>
29 #include <string.h>
30 #include <unistd.h>
31 #include <libintl.h>
32 #include <pwd.h>
33 
34 /* lpsched include files */
35 #include "lp.h"
36 #include "requests.h"
37 #include "printers.h"
38 
39 #include <papi_impl.h>
40 
41 papi_status_t
42 job_attributes_to_lpsched_request(papi_service_t svc, REQUEST *r,
43 		papi_attribute_t **attributes)
44 {
45 	papi_status_t status = PAPI_OK;
46 	papi_attribute_t *attr;
47 	papi_attribute_t **unmapped = NULL;
48 	papi_attribute_t *tmp[2];
49 	int i;
50 	char *s;
51 
52 	char **options = NULL;
53 	char **modes = NULL;
54 
55 	char pr_filter = 0;
56 	char *pr_title = NULL;
57 	int pr_width = -1;
58 	int pr_indent = -1;
59 	int numberUp = 0;
60 	int orientation = 0;
61 	int lower = 0;
62 	int upper = 0;
63 	char buf[256];
64 	void *iterator = NULL;
65 	char *mapped_keys[] = { "copies", "document-format", "form",
66 			"job-class", "job-hold-until", "job-host", "job-name",
67 			"job-originating-user-name", "job-printer",
68 			"job-sheets", "lp-charset", "lp-modes", "number-up",
69 			"orienttation-requested", "page-ranges", "pr-filter",
70 			"pr-indent", "pr-title", "pr-width", "job-priority",
71 			"requesting-user-name", "job-originating-host-name",
72 			NULL };
73 
74 	if (attributes == NULL)
75 		return (PAPI_BAD_ARGUMENT);
76 
77 	/* replace the current destination */
78 	papiAttributeListGetLPString(attributes,
79 			"job-printer", &r->destination);
80 
81 	/* set the copies.  We need at least 1 */
82 	i = r->copies;
83 	papiAttributeListGetInteger(attributes, NULL, "copies", &i);
84 	if (i <= 0)
85 		i = 1;
86 	r->copies = i;
87 
88 	/*
89 	 * set the priority.  PAPI/IPP uses 1-100, lpsched use 0-39, so we
90 	 * have to convert it.
91 	 */
92 	if (papiAttributeListGetInteger(attributes, NULL, "job-priority", &i)
93 			== PAPI_OK) {
94 		if ((i < 1) || (i > 100))
95 			i = 50;
96 		i = 40 - (i / 2.5);
97 		r->priority = i;
98 	}
99 	if ((r->priority < 0) || (r->priority > 39))
100 		r->priority = 20;
101 
102 	/* set the requested form to print on */
103 	papiAttributeListGetLPString(attributes, "form", &r->form);
104 
105 	/* set the page range */
106 	memset(tmp, NULL, sizeof (tmp));
107 	tmp[0] = papiAttributeListFind(attributes, "page-ranges");
108 	if (tmp[0] != NULL) {
109 		char buf[BUFSIZ];
110 
111 		papiAttributeListToString(tmp, " ", buf, sizeof (buf));
112 		if ((s = strchr(buf, '=')) != NULL)
113 			r->pages = (char *)strdup(++s);
114 	}
115 
116 	/*
117 	 * set the document format, converting to old format names as
118 	 * as needed.
119 	 */
120 	s = NULL;
121 	papiAttributeListGetString(attributes, NULL, "document-format", &s);
122 	if (s != NULL)
123 		r->input_type = strdup(mime_type_to_lp_type(s));
124 
125 
126 	/*
127 	 * If we don't have an owner, set one.
128 	 */
129 	if (r->user == NULL) {
130 		uid_t uid = getuid();
131 		struct passwd *pw;
132 		char *user = "intruder";
133 		char *host = NULL;
134 		char buf[256];
135 
136 		if ((pw = getpwuid(uid)) != NULL)
137 			user = pw->pw_name; /* default to the process owner */
138 
139 		if ((uid == 0) || (uid == 71)) { /* root/lp can forge this */
140 			papiAttributeListGetString(attributes, NULL,
141 					"job-originating-host-name", &host);
142 			papiAttributeListGetString(attributes, NULL,
143 					"job-host", &host);
144 			papiAttributeListGetString(attributes, NULL,
145 					"job-originating-user-name", &user);
146 			papiAttributeListGetString(attributes, NULL,
147 					"requesting-user-name", &user);
148 
149 			snprintf(buf, sizeof (buf), "%s%s%s", user,
150 					(host ? "@" : ""), (host ? host : ""));
151 			user = buf;
152 		}
153 
154 		r->user = strdup(user);
155 	}
156 
157 	/* set any held state */
158 	s = NULL;
159 	papiAttributeListGetString(attributes, NULL, "job-hold-until", &s);
160 	if (s != NULL) {
161 		r->actions &= ~(ACT_SPECIAL); /* strip immediate/hold/resume */
162 		if (strcmp(s, "resume") == 0)
163 			r->actions |= ACT_RESUME;
164 		else if ((strcmp(s, "immediate") == 0) ||
165 			    (strcmp(s, "no-hold") == 0))
166 			r->actions |= ACT_IMMEDIATE;
167 		else if ((strcmp(s, "indefinite") == 0) ||
168 			    (strcmp(s, "hold") == 0))
169 			r->actions |= ACT_HOLD;
170 	}
171 
172 	/* set lp charset/printwheel */
173 	papiAttributeListGetLPString(attributes, "lp-charset", &r->charset);
174 
175 	/* legacy pr(1) filter related garbage "lpr -p" */
176 	papiAttributeListGetBoolean(attributes, NULL, "pr-filter", &pr_filter);
177 	papiAttributeListGetString(attributes, NULL, "pr-title", &pr_title);
178 	papiAttributeListGetInteger(attributes, NULL, "pr-width", &pr_width);
179 	papiAttributeListGetInteger(attributes, NULL, "pr-indent", &pr_indent);
180 
181 	if (pr_filter != 0) {
182 		char buf[128];
183 
184 		if (pr_title != NULL) {
185 			snprintf(buf, sizeof (buf), "prtitle='%s'", pr_title);
186 			appendlist(&modes, buf);
187 		}
188 
189 		if (pr_width > 0) {
190 			snprintf(buf, sizeof (buf), "prwidth=%d", pr_width);
191 			appendlist(&modes, buf);
192 		}
193 
194 		if (pr_indent > 0) {
195 			snprintf(buf, sizeof (buf), "indent=%d", pr_indent);
196 			appendlist(&modes, buf);
197 		}
198 	} else if ((pr_title != NULL) || (pr_width >= 0) || (pr_indent >= 0))
199 		detailed_error(svc, gettext(
200 	"pr(1) filter options specified without enabling pr(1) filter"));
201 
202 	/* add burst page information */
203 	s = NULL;
204 	papiAttributeListGetString(attributes, NULL, "job-sheets", &s);
205 	if ((s != NULL) && (strcasecmp(s, "none") != 0)) {
206 		char buf[128];
207 		char *class = NULL;
208 		char *job_name = NULL;
209 
210 		papiAttributeListGetLPString(attributes, "job-class", &class);
211 		papiAttributeListGetLPString(attributes, "job-name", &job_name);
212 
213 		/* burst page is enabled by default, add the title */
214 		snprintf(buf, sizeof (buf), "%s%s%s",
215 			(job_name ? job_name : ""),
216 			(job_name && class ? "\\n#####\\n#####\\t\\t " : ""),
217 			(class ? class : ""));
218 		if (buf[0] != '\0') {
219 			if (r->title != NULL)
220 				free(r->title);
221 			r->title = strdup(buf);
222 		}
223 	} else if ((s != NULL) && (strcasecmp(s, "none") == 0)) {
224 		/* burst page is disabled via lp "option" */
225 		appendlist(&options, "nobanner");
226 	}
227 
228 	/* Convert attribute "number-up" to mode group=n */
229 	papiAttributeListGetInteger(attributes, NULL, "number-up", &numberUp);
230 	if ((numberUp >= 2) && ((numberUp % 2) == 0)) {
231 		snprintf(buf, sizeof (buf), "group=%d", numberUp);
232 		appendlist(&modes, buf);
233 	}
234 
235 	/*
236 	 * Convert attribute "orientation-requested" to modes
237 	 * 'landscape', 'portrait', etc.
238 	 */
239 	papiAttributeListGetInteger(attributes, NULL,
240 				    "orientation-requested", &orientation);
241 	if ((orientation >= 3) && (orientation <= 6)) {
242 		switch (orientation) {
243 		case 4:	/* landscape */
244 		case 5:	/* reverse-landscape, use landscape instead */
245 			appendlist(&modes, "landscape");
246 			break;
247 		case 3:	/* portrait */
248 		case 6: /* reverse-portrait, use portrait instead */
249 		default:
250 			appendlist(&modes, "portrait");
251 			break;
252 		}
253 	}
254 
255 	/* add "lp -y" modes */
256 	attr = papiAttributeListFind(attributes, "lp-modes");
257 	if ((attr != NULL) && (attr->type == PAPI_STRING) &&
258 	    (attr->values != NULL)) {
259 		int i;
260 
261 		for (i = 0; attr->values[i] != NULL; i++)
262 			appendlist(&modes, attr->values[i]->string);
263 	}
264 
265 	if (modes != NULL) {
266 		if (r->modes == NULL)
267 			free(r->modes);
268 		r->modes = sprintlist(modes);
269 		freelist(modes);
270 	}
271 
272 	/* add any unconsumed attributes to the "options" list */
273 	split_and_copy_attributes(mapped_keys, attributes, NULL, &unmapped);
274 	if (unmapped != NULL) {	/* convert them to lp options */
275 		char *buf = malloc(1024);
276 		ssize_t size = 1024;
277 
278 		while (papiAttributeListToString(unmapped, " ", buf, size)
279 					!= PAPI_OK) {
280 			size += 1024;
281 			buf = realloc(buf, size);
282 		}
283 		appendlist(&options, buf);
284 		free(buf);
285 		papiAttributeListFree(unmapped);
286 	}
287 
288 	if (options != NULL) {
289 		if (r->options != NULL)
290 			free(r->options);
291 		r->options = sprintlist(options);
292 		freelist(options);
293 	}
294 
295 	return (PAPI_OK);
296 }
297 
298 /*
299  * Convert REQUEST->outcome (or R_REQUEST_* state) to the equivalent
300  * PAPI attribute representation.
301  */
302 static void
303 lpsched_request_outcome_to_attributes(papi_attribute_t ***attributes,
304 		unsigned short state)
305 {
306 	if (attributes == NULL)
307 		return;
308 
309 	if (state & (RS_HELD|RS_ADMINHELD)) {
310 		papiAttributeListAddInteger(attributes, PAPI_ATTR_REPLACE,
311 			"job-state", 0x04);	/* held */
312 		papiAttributeListAddString(attributes, PAPI_ATTR_REPLACE,
313 			"job-state-reasons", "job-hold-until-specified");
314 	} else if (state & RS_ACTIVE) {
315 		papiAttributeListAddInteger(attributes, PAPI_ATTR_REPLACE,
316 			"job-state", 0x05);
317 		if (state & RS_FILTERING)
318 			papiAttributeListAddString(attributes,
319 				PAPI_ATTR_REPLACE,
320 				"job-state-reasons", "job-transforming");
321 		else if (state & RS_PRINTING)
322 			papiAttributeListAddString(attributes,
323 				PAPI_ATTR_REPLACE,
324 				"job-state-reasons", "job-printing");
325 		else
326 			papiAttributeListAddString(attributes,
327 				PAPI_ATTR_REPLACE,
328 				"job-state-reasons", "job-processing");
329 	} else if (state & RS_CANCELLED) {
330 		papiAttributeListAddInteger(attributes, PAPI_ATTR_REPLACE,
331 			"job-state", 0x07);
332 		papiAttributeListAddString(attributes, PAPI_ATTR_REPLACE,
333 			"job-state-reasons", "job-canceled-by-user");
334 	} else if (state & RS_PRINTED) {
335 		papiAttributeListAddInteger(attributes, PAPI_ATTR_REPLACE,
336 			"job-state", 0x09);
337 		papiAttributeListAddString(attributes, PAPI_ATTR_REPLACE,
338 			"job-state-reasons", "job-complete");
339 	} else {
340 		papiAttributeListAddInteger(attributes, PAPI_ATTR_REPLACE,
341 			"job-state", 0x03);
342 		papiAttributeListAddString(attributes, PAPI_ATTR_REPLACE,
343 			"job-state-reasons", "job-queued");
344 	}
345 	papiAttributeListAddString(attributes, PAPI_ATTR_REPLACE,
346 				"job-hold-until",
347 		((state & RS_HELD) ? "indefinite" : "no-hold"));
348 }
349 
350 /*
351  * convert user[@host] to papi attributes
352  */
353 static void
354 lpsched_user_to_job_attributes(papi_attribute_t ***list, char *user)
355 {
356 	if ((list != NULL) && (user != NULL) && (user[0] != NULL)) {
357 		char *host = strrchr(user, '@');
358 
359 		if (host != NULL) {
360 			*host = NULL;
361 			papiAttributeListAddString(list, PAPI_ATTR_REPLACE,
362 					"job-originating-user-name", user);
363 			papiAttributeListAddString(list, PAPI_ATTR_REPLACE,
364 					"job-originating-host-name", host + 1);
365 			*host = '@';
366 		} else
367 			papiAttributeListAddString(list, PAPI_ATTR_REPLACE,
368 					"job-originating-user-name", user);
369 	}
370 }
371 
372 /*
373  * Convert REQUEST structure to the equivalent PAPI attribute representation.
374  */
375 void
376 lpsched_request_to_job_attributes(REQUEST *r, job_t *j)
377 {
378 	char *tmp;
379 	int i;
380 
381 	/* copies */
382 	papiAttributeListAddInteger(&j->attributes, PAPI_ATTR_REPLACE,
383 				"copies", r->copies);
384 
385 	/* destination */
386 	papiAttributeListAddLPString(&j->attributes, PAPI_ATTR_REPLACE,
387 				"printer-name", r->destination);
388 
389 	/* form */
390 	papiAttributeListAddLPString(&j->attributes, PAPI_ATTR_REPLACE,
391 				"form", r->form);
392 
393 	/* options */
394 	papiAttributeListFromString(&j->attributes, PAPI_ATTR_APPEND,
395 				r->options);
396 
397 	tmp = (((r->options != NULL) && (strstr(r->options, "nobanner")
398 		!= NULL)) ? "none" : "standard");
399 	papiAttributeListAddString(&j->attributes, PAPI_ATTR_REPLACE,
400 				"job-sheets", tmp);
401 
402 	tmp = (((r->options != NULL) && (strstr(r->options, "duplex")
403 		!= NULL)) ? "two-sized" : "one-sided");
404 	papiAttributeListAddString(&j->attributes, PAPI_ATTR_REPLACE,
405 				"sides", tmp);
406 
407 	i = (((r->options != NULL) && (strstr(r->options, "landscape")
408 		!= NULL)) ? 4 : 3);
409 	papiAttributeListAddInteger(&j->attributes, PAPI_ATTR_REPLACE,
410 				"orientation-requested", i);
411 
412 	/* priority (map 0-39 to 1-100) */
413 	papiAttributeListAddInteger(&j->attributes, PAPI_ATTR_REPLACE,
414 				"job-priority",
415 				(int)(100 - (r->priority * 2.5)));
416 
417 	/* pages */
418 	papiAttributeListAddLPString(&j->attributes, PAPI_ATTR_REPLACE,
419 				"page-ranges", r->pages);
420 
421 	/* charset */
422 	papiAttributeListAddLPString(&j->attributes, PAPI_ATTR_REPLACE,
423 				"lp-charset", r->charset);
424 
425 	/* modes */
426 	papiAttributeListAddLPString(&j->attributes, PAPI_ATTR_REPLACE,
427 				"lp-modes", r->modes);
428 
429 	/* title */
430 	papiAttributeListAddLPString(&j->attributes, PAPI_ATTR_REPLACE,
431 				"job-name", r->title);
432 
433 	/* input_type */
434 
435 	/* user */
436 	lpsched_user_to_job_attributes(&j->attributes, r->user);
437 
438 	/* outcome */
439 	lpsched_request_outcome_to_attributes(&j->attributes, r->outcome);
440 
441 	/* constants, (should be derived from options) */
442 	papiAttributeListAddInteger(&j->attributes, PAPI_ATTR_REPLACE,
443 				"number-up", 1);
444 
445 	papiAttributeListAddString(&j->attributes, PAPI_ATTR_REPLACE,
446 				"multiple-document-handling",
447 				"seperate-documents-collated-copies");
448 }
449 
450 /*
451  * Convert R_REQUEST_* results to the equivalent PAPI attribute representation.
452  */
453 void
454 job_status_to_attributes(job_t *job, char *req_id, char *user, char *slabel,
455 		size_t size, time_t date, short state, char *destination,
456 		char *form, char *charset, short rank, char *file)
457 {
458 	char buf[BUFSIZ];
459 	char *p;
460 
461 	lpsched_user_to_job_attributes(&job->attributes, user);
462 	papiAttributeListAddInteger(&job->attributes, PAPI_ATTR_REPLACE,
463 				"job-k-octets", size/1024);
464 	papiAttributeListAddInteger(&job->attributes, PAPI_ATTR_REPLACE,
465 				"job-octets", size);
466 	if ((p = strrchr(req_id, '-')) != NULL) {
467 		papiAttributeListAddInteger(&job->attributes, PAPI_ATTR_REPLACE,
468 				"job-id", atoi(++p));
469 	}
470 	snprintf(buf, sizeof (buf), "lpsched://localhost/printers/%s/%d",
471 			destination, atoi(p));
472 	papiAttributeListAddString(&job->attributes, PAPI_ATTR_REPLACE,
473 				"job-uri", buf);
474 	snprintf(buf, sizeof (buf), "lpsched://localhost/printers/%s",
475 			destination);
476 	papiAttributeListAddString(&job->attributes, PAPI_ATTR_REPLACE,
477 				"job-printer-uri", buf);
478 	papiAttributeListAddInteger(&job->attributes, PAPI_ATTR_REPLACE,
479 				"job-printer-up-time", time(NULL));
480 	papiAttributeListAddString(&job->attributes, PAPI_ATTR_REPLACE,
481 				"output-device-assigned", destination);
482 	papiAttributeListAddString(&job->attributes, PAPI_ATTR_REPLACE,
483 				"printer-name", destination);
484 	papiAttributeListAddLPString(&job->attributes, PAPI_ATTR_REPLACE,
485 				"form", form);
486 
487 	lpsched_request_outcome_to_attributes(&job->attributes, state);
488 
489 	papiAttributeListAddInteger(&job->attributes, PAPI_ATTR_REPLACE,
490 				"time-at-creation", date);
491 	papiAttributeListAddLPString(&job->attributes, PAPI_ATTR_REPLACE,
492 				"lpsched-request-id", req_id);
493 	papiAttributeListAddLPString(&job->attributes, PAPI_ATTR_REPLACE,
494 				"lp-charset", charset);
495 	papiAttributeListAddInteger(&job->attributes, PAPI_ATTR_REPLACE,
496 				"lpsched-job-state", state);
497 	papiAttributeListAddInteger(&job->attributes, PAPI_ATTR_REPLACE,
498 				"number-of-intervening-jobs", rank - 1);
499 	papiAttributeListAddLPString(&job->attributes, PAPI_ATTR_REPLACE,
500 				"lpsched-file", file);
501 	papiAttributeListAddLPString(&job->attributes, PAPI_ATTR_EXCL,
502 				"job-name", file);
503 	papiAttributeListAddLPString(&job->attributes, PAPI_ATTR_EXCL,
504 				"tsol-sensitivity-label", slabel);
505 }
506 
507 void
508 lpsched_read_job_configuration(service_t *svc, job_t *j, char *file)
509 {
510 	REQUEST *r;
511 
512 	if ((r = getrequest(file)) == NULL) {
513 		detailed_error(svc, gettext("unable to read job data: %s"),
514 			file);
515 		return;
516 	}
517 
518 	lpsched_request_to_job_attributes(r, j);
519 }
520