xref: /illumos-gate/usr/src/cmd/lp/lib/papi/lpsched-printers.c (revision 355b4669e025ff377602b6fc7caaf30dbc218371)
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 2006 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 /*LINTLIBRARY*/
29 
30 #include <stdlib.h>
31 #include <libintl.h>
32 #include <unistd.h>
33 #include <string.h>
34 #include <sys/utsname.h>
35 #include <papi_impl.h>
36 
37 #include "class.h"
38 
39 void
40 lpsched_printer_status_to_attributes(papi_attribute_t ***attrs,
41 	unsigned short status)
42 {
43 	if (attrs == NULL)
44 		return;
45 
46 	if (status & (PS_DISABLED|PS_LATER|PS_FAULTED|PS_FORM_FAULT)) {
47 		papiAttributeListAddInteger(attrs, PAPI_ATTR_REPLACE,
48 				"printer-state", 0x05); /* stopped */
49 		if (status & PS_LATER)
50 			papiAttributeListAddString(attrs, PAPI_ATTR_REPLACE,
51 				"printer-state-reasons", "moving-to-paused");
52 		else if (status & PS_FAULTED)
53 			papiAttributeListAddString(attrs, PAPI_ATTR_REPLACE,
54 				"printer-state-reasons", "none");
55 		else if (status & PS_FORM_FAULT)
56 			papiAttributeListAddString(attrs, PAPI_ATTR_REPLACE,
57 				"printer-state-reasons",
58 				"interpreter-resource-unavailable");
59 		else
60 			papiAttributeListAddString(attrs, PAPI_ATTR_REPLACE,
61 				"printer-state-reasons", "paused");
62 	} else if (status & PS_BUSY) {
63 		papiAttributeListAddInteger(attrs, PAPI_ATTR_REPLACE,
64 				"printer-state", 0x04); /* processing */
65 		papiAttributeListAddString(attrs, PAPI_ATTR_REPLACE,
66 				"printer-state-reasons", "moving-to-paused");
67 	} else {
68 		papiAttributeListAddInteger(attrs, PAPI_ATTR_REPLACE,
69 				"printer-state", 0x03); /* idle */
70 	}
71 
72 	papiAttributeListAddBoolean(attrs, PAPI_ATTR_REPLACE,
73 			"printer-is-accepting-jobs",
74 			((status & PS_REJECTED) != PS_REJECTED));
75 	papiAttributeListAddBoolean(attrs, PAPI_ATTR_REPLACE,
76 			"printer-is-processing-jobs",
77 			((status & PS_DISABLED) != PS_DISABLED));
78 }
79 
80 void
81 lpsched_printer_defaults(papi_attribute_t ***attributes)
82 {
83 	if (attributes == NULL)
84 		return;
85 
86 	papiAttributeListAddBoolean(attributes, PAPI_ATTR_REPLACE,
87 			"multiple-document-jobs-supported", PAPI_TRUE);
88 	papiAttributeListAddString(attributes, PAPI_ATTR_REPLACE,
89 			"multiple-document-handling-supported",
90 			"seperate-documents-colated-copies");
91 	papiAttributeListAddString(attributes, PAPI_ATTR_REPLACE,
92 			"pdl-override-supported", "not-attempted");
93 	papiAttributeListAddInteger(attributes, PAPI_ATTR_REPLACE,
94 			"job-priority-supported", 40);
95 	papiAttributeListAddInteger(attributes, PAPI_ATTR_REPLACE,
96 			"job-priority-default", 20);
97 	papiAttributeListAddRange(attributes, PAPI_ATTR_REPLACE,
98 			"copies-supported", 1, 65535);
99 	papiAttributeListAddInteger(attributes, PAPI_ATTR_REPLACE,
100 			"copies-default", 1);
101 	papiAttributeListAddBoolean(attributes, PAPI_ATTR_REPLACE,
102 			"page-ranges-supported", PAPI_TRUE);
103 	papiAttributeListAddInteger(attributes, PAPI_ATTR_REPLACE,
104 			"number-up-supported", 1);
105 	papiAttributeListAddInteger(attributes, PAPI_ATTR_REPLACE,
106 			"number-up-default", 1);
107 	papiAttributeListAddString(attributes, PAPI_ATTR_REPLACE,
108 			"job-hold-until-supported", "no-hold");
109 	papiAttributeListAddString(attributes, PAPI_ATTR_APPEND,
110 			"job-hold-until-supported", "indefinite");
111 	papiAttributeListAddString(attributes, PAPI_ATTR_REPLACE,
112 			"job-hold-until-default", "no-hold");
113 	papiAttributeListAddString(attributes, PAPI_ATTR_REPLACE,
114 			"document-format-default", "application/octet-stream");
115 
116 }
117 
118 papi_status_t
119 lpsched_printer_configuration_to_attributes(service_t *svc, printer_t *p,
120 	char *dest)
121 {
122 	PRINTER *tmp;
123 	char buf[BUFSIZ+1];
124 	struct utsname sysname;
125 	char **allowed = NULL, **denied = NULL;
126 
127 	if ((svc == NULL) || (p == NULL) || (dest == NULL))
128 		return (PAPI_BAD_ARGUMENT);
129 
130 	/* get the configuration DB data */
131 	if ((tmp = getprinter(dest)) == NULL) {
132 		detailed_error(svc,
133 			gettext("unable to read configuration data"));
134 		return (PAPI_DEVICE_ERROR);
135 	}
136 
137 	/* name */
138 	papiAttributeListAddLPString(&p->attributes, PAPI_ATTR_REPLACE,
139 				"printer-name", tmp->name);
140 	if (tmp->name != NULL) {
141 		char uri[BUFSIZ];
142 
143 		snprintf(uri, sizeof (uri), "lpsched://localhost/printers/%s",
144 				tmp->name);
145 		papiAttributeListAddString(&p->attributes, PAPI_ATTR_REPLACE,
146 				"printer-uri-supported", uri);
147 	}
148 
149 	/* banner */
150 	if ((tmp->banner & BAN_OPTIONAL) == BAN_OPTIONAL)
151 		papiAttributeListAddString(&p->attributes, PAPI_ATTR_APPEND,
152 				"job-sheets-supported", "optional");
153 	else if (tmp->banner & BAN_NEVER)
154 		papiAttributeListAddString(&p->attributes, PAPI_ATTR_APPEND,
155 				"job-sheets-supported", "none");
156 	else if (tmp->banner & BAN_ALWAYS)
157 		papiAttributeListAddString(&p->attributes, PAPI_ATTR_APPEND,
158 				"job-sheets-supported", "standard");
159 
160 	/* input_types */
161 	if (tmp->input_types != NULL) {
162 		int i;
163 
164 		for (i = 0; tmp->input_types[i] != NULL; i++)
165 			papiAttributeListAddLPString(&p->attributes,
166 				PAPI_ATTR_APPEND, "document-format-supported",
167 				lp_type_to_mime_type(tmp->input_types[i]));
168 	}
169 
170 	/* description */
171 	papiAttributeListAddLPString(&p->attributes, PAPI_ATTR_REPLACE,
172 			"printer-info", tmp->description);
173 
174 	/* add lpsched specific attributes */
175 	papiAttributeListAddLPString(&p->attributes, PAPI_ATTR_REPLACE,
176 				"device-uri", tmp->device);
177 	papiAttributeListAddLPString(&p->attributes, PAPI_ATTR_REPLACE,
178 				"lpsched-dial-info", tmp->dial_info);
179 	papiAttributeListAddLPString(&p->attributes, PAPI_ATTR_REPLACE,
180 				"lpsched-fault-recovery", tmp->fault_rec);
181 	papiAttributeListAddLPString(&p->attributes, PAPI_ATTR_REPLACE,
182 				"lpsched-interface-script", tmp->interface);
183 	papiAttributeListAddLPString(&p->attributes, PAPI_ATTR_REPLACE,
184 				"lpsched-data-rate", tmp->speed);
185 	papiAttributeListAddLPString(&p->attributes, PAPI_ATTR_REPLACE,
186 				"lpsched-stty", tmp->stty);
187 	papiAttributeListAddBoolean(&p->attributes, PAPI_ATTR_REPLACE,
188 				"lpsched-login-term", tmp->login);
189 	papiAttributeListAddBoolean(&p->attributes, PAPI_ATTR_REPLACE,
190 				"lpsched-daisy", tmp->daisy);
191 	papiAttributeListAddLPStrings(&p->attributes, PAPI_ATTR_REPLACE,
192 				"lpsched-charsets", tmp->char_sets);
193 #ifdef CAN_DO_MODULES
194 	papiAttributeListAddLPStrings(&p->attributes, PAPI_ATTR_REPLACE,
195 				"lpsched-modules", tmp->modules);
196 #endif /* CAN_DO_MODULES */
197 	papiAttributeListAddLPStrings(&p->attributes, PAPI_ATTR_REPLACE,
198 				"lpsched-options", tmp->options);
199 	papiAttributeListAddLPStrings(&p->attributes, PAPI_ATTR_REPLACE,
200 				"lpsched-printer-type", tmp->printer_types);
201 	if (tmp->fault_alert.shcmd != NULL) {
202 		papiAttributeListAddLPString(&p->attributes, PAPI_ATTR_REPLACE,
203 				"lpsched-fault-alert-command",
204 				tmp->fault_alert.shcmd);
205 		papiAttributeListAddInteger(&p->attributes, PAPI_ATTR_REPLACE,
206 				"lpsched-fault-alert-threshold",
207 				tmp->fault_alert.Q);
208 		papiAttributeListAddInteger(&p->attributes, PAPI_ATTR_REPLACE,
209 				"lpsched-fault-alert-interval",
210 				tmp->fault_alert.W);
211 	}
212 	papiAttributeListAddInteger(&p->attributes, PAPI_ATTR_REPLACE,
213 				"lpsched-cpi-value", tmp->cpi.val);
214 	papiAttributeListAddInteger(&p->attributes, PAPI_ATTR_REPLACE,
215 				"lpsched-cpi-unit", tmp->cpi.sc);
216 	papiAttributeListAddInteger(&p->attributes, PAPI_ATTR_REPLACE,
217 				"lpsched-lpi-value", tmp->lpi.val);
218 	papiAttributeListAddInteger(&p->attributes, PAPI_ATTR_REPLACE,
219 				"lpsched-lpi-unit", tmp->lpi.sc);
220 	papiAttributeListAddInteger(&p->attributes, PAPI_ATTR_REPLACE,
221 				"lpsched-plen-value", tmp->plen.val);
222 	papiAttributeListAddInteger(&p->attributes, PAPI_ATTR_REPLACE,
223 				"lpsched-plen-unit", tmp->plen.sc);
224 	papiAttributeListAddInteger(&p->attributes, PAPI_ATTR_REPLACE,
225 				"lpsched-pwid-value", tmp->pwid.val);
226 	papiAttributeListAddInteger(&p->attributes, PAPI_ATTR_REPLACE,
227 				"lpsched-pwid-unit", tmp->pwid.sc);
228 
229 	/* allow/deny list */
230 	load_userprinter_access(dest, &allowed, &denied);
231 	papiAttributeListAddLPStrings(&p->attributes, PAPI_ATTR_REPLACE,
232 				"requesting-user-name-allowed", allowed);
233 	papiAttributeListAddLPStrings(&p->attributes, PAPI_ATTR_REPLACE,
234 				"requesting-user-name-denied", denied);
235 
236 #ifdef LP_USE_PAPI_ATTR
237 	if (tmp->ppd != NULL) {
238 		int fd;
239 		struct stat sbuf;
240 
241 		/* construct the two URIs for the printer's PPD file */
242 		if (uname(&sysname) < 0) {
243 			/* failed to get systen name */
244 			sysname.nodename[0] = 0;
245 		}
246 		snprintf(buf, sizeof (buf), "file://%s%s/ppd/%s.ppd",
247 			sysname.nodename, ETCDIR, tmp->name);
248 		papiAttributeListAddString(&p->attributes, PAPI_ATTR_REPLACE,
249 			"ppd-file-uri", buf);
250 
251 		snprintf(buf, sizeof (buf), "file://%s%s",
252 			sysname.nodename, tmp->ppd);
253 		papiAttributeListAddString(&p->attributes, PAPI_ATTR_REPLACE,
254 			"lpsched-printer-configure-ppd-uri", buf);
255 		papiAttributeListAddString(&p->attributes, PAPI_ATTR_REPLACE,
256 			"lpsched-ppd-source-path", tmp->ppd);
257 
258 		snprintf(buf, sizeof (buf), "%s/ppd/%s.ppd", ETCDIR, tmp->name);
259 
260 		/*
261 		 * We don't return error on any of the error conditions, we just
262 		 * silently return without adding the attribute.
263 		 */
264 		PPDFileToAttributesList(&p->attributes, buf);
265 	}
266 #endif
267 
268 	freeprinter(tmp);
269 
270 	return (PAPI_OK);
271 }
272 
273 papi_status_t
274 printer_status_to_attributes(printer_t *p, char *printer, char *form,
275 		char *character_set, char *disable_reason, char *reject_reason,
276 		short status, char *request_id,
277 		long disable_date, long reject_date)
278 {
279 	if (p == NULL)
280 		return (PAPI_BAD_ARGUMENT);
281 
282 	papiAttributeListAddLPString(&p->attributes, PAPI_ATTR_REPLACE,
283 			"form-ready", form);
284 	papiAttributeListAddLPString(&p->attributes, PAPI_ATTR_REPLACE,
285 			"lpsched-active-job", request_id);
286 	papiAttributeListAddLPString(&p->attributes, PAPI_ATTR_REPLACE,
287 			"lpsched-mounted-char-set", character_set);
288 	papiAttributeListAddLPString(&p->attributes, PAPI_ATTR_REPLACE,
289 			"lpsched-disable-reason", disable_reason);
290 	papiAttributeListAddDatetime(&p->attributes, PAPI_ATTR_REPLACE,
291 			"lpsched-disable-date", disable_date);
292 	papiAttributeListAddLPString(&p->attributes, PAPI_ATTR_REPLACE,
293 			"lpsched-reject-reason", reject_reason);
294 	papiAttributeListAddDatetime(&p->attributes, PAPI_ATTR_REPLACE,
295 			"lpsched-reject-date", reject_date);
296 
297 	/* add the current system time */
298 	papiAttributeListAddDatetime(&p->attributes, PAPI_ATTR_REPLACE,
299 			"printer-current-time", time(NULL));
300 
301 	/* add the time since last enabled */
302 	papiAttributeListAddInteger(&p->attributes, PAPI_ATTR_REPLACE,
303 			"printer-up-time", time(NULL));
304 
305 	/* add the status information */
306 	lpsched_printer_status_to_attributes(&p->attributes, status);
307 
308 	papiAttributeListAddString(&p->attributes, PAPI_ATTR_EXCL,
309 			"printer-state-reasons", "none");
310 
311 	lpsched_printer_defaults(&p->attributes);
312 
313 	return (PAPI_OK);
314 }
315 
316 
317 /*
318  * This puts the class information in only.  It could create a hybrid
319  * printer object to return, but that is problematic at best.
320  */
321 papi_status_t
322 lpsched_class_configuration_to_attributes(service_t *svc, printer_t *p,
323 	char *dest)
324 {
325 	CLASS *tmp;
326 
327 	if ((svc == NULL) || (p == NULL))
328 		return (PAPI_BAD_ARGUMENT);
329 
330 	/* get the configuration DB data */
331 	if ((tmp = getclass(dest)) == NULL) {
332 		detailed_error(svc,
333 			gettext("unable to read configuration data"));
334 		return (PAPI_DEVICE_ERROR);
335 	}
336 
337 	/* name */
338 	papiAttributeListAddLPString(&p->attributes, PAPI_ATTR_REPLACE,
339 				"printer-name", tmp->name);
340 	if (tmp->name != NULL) {
341 		char uri[BUFSIZ];
342 
343 		snprintf(uri, sizeof (uri), "lpsched://localhost/printers/%s",
344 				tmp->name);
345 		papiAttributeListAddString(&p->attributes, PAPI_ATTR_REPLACE,
346 				"printer-uri-supported", uri);
347 	}
348 
349 	if (tmp->members != NULL) {
350 		char **members = tmp->members;
351 		int i;
352 
353 		for (i = 0; members[i] != NULL; i++)
354 			papiAttributeListAddString(&p->attributes,
355 					PAPI_ATTR_APPEND,
356 					"member-names", members[i]);
357 	}
358 
359 	freeclass(tmp);
360 
361 	return (PAPI_OK);
362 }
363 
364 papi_status_t
365 class_status_to_attributes(printer_t *p, char *printer, short status,
366 		char *reject_reason, long reject_date)
367 {
368 	if (p == NULL)
369 		return (PAPI_BAD_ARGUMENT);
370 
371 	papiAttributeListAddLPString(&p->attributes, PAPI_ATTR_REPLACE,
372 			"lpsched-reject-reason", reject_reason);
373 	papiAttributeListAddDatetime(&p->attributes, PAPI_ATTR_REPLACE,
374 			"lpsched-reject-date", reject_date);
375 
376 	/* add the current system time */
377 	papiAttributeListAddDatetime(&p->attributes, PAPI_ATTR_REPLACE,
378 			"printer-current-time", time(NULL));
379 
380 	papiAttributeListAddInteger(&p->attributes, PAPI_ATTR_REPLACE,
381 			"printer-up-time", time(NULL));
382 
383 	/* add the status information */
384 	lpsched_printer_status_to_attributes(&p->attributes, status);
385 
386 	papiAttributeListAddString(&p->attributes, PAPI_ATTR_EXCL,
387 			"printer-state-reasons", "none");
388 
389 	lpsched_printer_defaults(&p->attributes);
390 
391 	return (PAPI_OK);
392 }
393 
394 papi_status_t
395 attributes_to_printer(papi_attribute_t **attributes, PRINTER *tmp)
396 {
397 	papi_status_t status;
398 	void *iter = NULL;
399 	char *string = NULL;
400 	int flags;
401 	char **list = NULL;
402 
403 	/* banner needs some conversion to the bitfield */
404 	iter = NULL, string = NULL; flags = 0;
405 	for (status = papiAttributeListGetString(attributes, &iter,
406 				"job-sheets-supported", &string);
407 	    status == PAPI_OK;
408 	    status = papiAttributeListGetString(attributes, &iter,
409 				NULL, &string))
410 		if (strcasecmp(string, "none") == 0)
411 			flags |= BAN_NEVER;
412 		else if (strcasecmp(string, "standard") == 0)
413 			flags |= BAN_ALWAYS;
414 	if (flags != 0)
415 		tmp->banner = flags;
416 
417 	/* input_types needs mime-type conversion */
418 	iter = NULL, string = NULL; list = NULL;
419 	for (status = papiAttributeListGetString(attributes, &iter,
420 				"document-format-supported", &string);
421 	    status == PAPI_OK;
422 	    status = papiAttributeListGetString(attributes, &iter,
423 				NULL, &string))
424 		addlist(&list, mime_type_to_lp_type(string));
425 	if (list != NULL) {
426 		if (tmp->input_types != NULL)
427 			freelist(tmp->input_types);
428 		tmp->input_types = list;
429 	}
430 
431 	papiAttributeListGetLPString(attributes,
432 				"device-uri", &tmp->device);
433 	papiAttributeListGetLPString(attributes,
434 				"printer-info", &tmp->description);
435 	papiAttributeListGetLPString(attributes,
436 				"lpsched-dial-info", &tmp->dial_info);
437 	papiAttributeListGetLPString(attributes,
438 				"lpsched-fault-recovery", &tmp->fault_rec);
439 	papiAttributeListGetLPString(attributes,
440 				"lpsched-interface-script", &tmp->interface);
441 	papiAttributeListGetLPString(attributes,
442 				"lpsched-data-rate", &tmp->speed);
443 	papiAttributeListGetLPString(attributes,
444 				"lpsched-stty", &tmp->stty);
445 	papiAttributeListGetLPStrings(attributes,
446 				"lpsched-charsets", &tmp->char_sets);
447 	papiAttributeListGetLPStrings(attributes,
448 				"lpsched-printer-types", &tmp->printer_types);
449 	papiAttributeListGetLPStrings(attributes,
450 				"lpsched-options", &tmp->options);
451 	papiAttributeListGetLPStrings(attributes,
452 				"lpsched-modules", &tmp->modules);
453 #ifdef LP_USE_PAPI_ATTR
454 	papiAttributeListGetLPString(attributes,
455 				"lpsched-printer-ppd-uri", &tmp->ppd);
456 #endif
457 
458 	return (PAPI_OK);
459 }
460