xref: /onnv-gate/usr/src/cmd/lp/lib/papi/job.c (revision 12091)
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
51676Sjpk  * Common Development and Distribution License (the "License").
61676Sjpk  * 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 /*
22*12091SSonam.Gupta@Sun.COM  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
230Sstevel@tonic-gate  */
240Sstevel@tonic-gate 
250Sstevel@tonic-gate /*LINTLIBRARY*/
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #include <stdlib.h>
280Sstevel@tonic-gate #include <string.h>
290Sstevel@tonic-gate #include <unistd.h>
300Sstevel@tonic-gate #include <libintl.h>
310Sstevel@tonic-gate #include <pwd.h>
320Sstevel@tonic-gate #include <sys/stat.h>
330Sstevel@tonic-gate #include <papi_impl.h>
340Sstevel@tonic-gate 
352264Sjacobs /*
362264Sjacobs  * for an older application that may have been linked with a pre-v1.0
372264Sjacobs  * PAPI implementation.
382264Sjacobs  */
392264Sjacobs papi_status_t
402264Sjacobs papiAttributeListAdd(papi_attribute_t ***attrs, int flags, char *name,
412264Sjacobs 		papi_attribute_value_type_t type, papi_attribute_value_t *value)
422264Sjacobs {
432264Sjacobs 	return (papiAttributeListAddValue(attrs, flags, name, type, value));
442264Sjacobs }
452264Sjacobs 
460Sstevel@tonic-gate #ifdef LP_USE_PAPI_ATTR
472264Sjacobs static papi_status_t psm_modifyAttrsFile(papi_attribute_t **attrs, char *file);
482264Sjacobs static papi_status_t psm_modifyAttrsList(char *file, papi_attribute_t **attrs,
492264Sjacobs 					papi_attribute_t ***newAttrs);
500Sstevel@tonic-gate #endif
510Sstevel@tonic-gate 
52*12091SSonam.Gupta@Sun.COM int32_t
53*12091SSonam.Gupta@Sun.COM check_job_id(papi_service_t svc, char *printer, int32_t id)
54*12091SSonam.Gupta@Sun.COM {
55*12091SSonam.Gupta@Sun.COM 	papi_job_t *jobs = NULL;
56*12091SSonam.Gupta@Sun.COM 	papi_status_t status;
57*12091SSonam.Gupta@Sun.COM 	int ret = -1;
58*12091SSonam.Gupta@Sun.COM 	char *jattrs[] = { "job-id",
59*12091SSonam.Gupta@Sun.COM 	    "job-id-requested", NULL };
60*12091SSonam.Gupta@Sun.COM 
61*12091SSonam.Gupta@Sun.COM 	status = papiPrinterListJobs(svc, printer, jattrs, PAPI_LIST_JOBS_ALL,
62*12091SSonam.Gupta@Sun.COM 	    0, &jobs);
63*12091SSonam.Gupta@Sun.COM 
64*12091SSonam.Gupta@Sun.COM 	if (status != PAPI_OK) {
65*12091SSonam.Gupta@Sun.COM 		detailed_error(svc,
66*12091SSonam.Gupta@Sun.COM 		    gettext("Failed to query service for %s: %s\n"),
67*12091SSonam.Gupta@Sun.COM 		    printer, lpsched_status_string(status));
68*12091SSonam.Gupta@Sun.COM 		return (-1);
69*12091SSonam.Gupta@Sun.COM 	}
70*12091SSonam.Gupta@Sun.COM 
71*12091SSonam.Gupta@Sun.COM 	if (jobs != NULL) {
72*12091SSonam.Gupta@Sun.COM 		int i = 0;
73*12091SSonam.Gupta@Sun.COM 
74*12091SSonam.Gupta@Sun.COM 		for (i = 0; jobs[i] != NULL; i++) {
75*12091SSonam.Gupta@Sun.COM 			int32_t rid = -1;
76*12091SSonam.Gupta@Sun.COM 			int32_t jid = -1;
77*12091SSonam.Gupta@Sun.COM 			papi_attribute_t **list =
78*12091SSonam.Gupta@Sun.COM 			    papiJobGetAttributeList(jobs[i]);
79*12091SSonam.Gupta@Sun.COM 
80*12091SSonam.Gupta@Sun.COM 			papiAttributeListGetInteger(list, NULL,
81*12091SSonam.Gupta@Sun.COM 			    "job-id-requested", &rid);
82*12091SSonam.Gupta@Sun.COM 			papiAttributeListGetInteger(list, NULL,
83*12091SSonam.Gupta@Sun.COM 			    "job-id", &jid);
84*12091SSonam.Gupta@Sun.COM 
85*12091SSonam.Gupta@Sun.COM 			/*
86*12091SSonam.Gupta@Sun.COM 			 * check if id matches with either rid or jid
87*12091SSonam.Gupta@Sun.COM 			 */
88*12091SSonam.Gupta@Sun.COM 			if (rid == id) {
89*12091SSonam.Gupta@Sun.COM 				/* get the actual id and return it */
90*12091SSonam.Gupta@Sun.COM 				papiAttributeListGetInteger(list, NULL,
91*12091SSonam.Gupta@Sun.COM 				    "job-id", &id);
92*12091SSonam.Gupta@Sun.COM 				return (id);
93*12091SSonam.Gupta@Sun.COM 			} else if (jid == id) {
94*12091SSonam.Gupta@Sun.COM 				if (rid != -1) {
95*12091SSonam.Gupta@Sun.COM 					/*
96*12091SSonam.Gupta@Sun.COM 					 * It is a remote lpd job.
97*12091SSonam.Gupta@Sun.COM 					 * It cannot be modified based on job-id
98*12091SSonam.Gupta@Sun.COM 					 * or spool number
99*12091SSonam.Gupta@Sun.COM 					 */
100*12091SSonam.Gupta@Sun.COM 					return (-1);
101*12091SSonam.Gupta@Sun.COM 				} else {
102*12091SSonam.Gupta@Sun.COM 					/*
103*12091SSonam.Gupta@Sun.COM 					 * It is either local job or
104*12091SSonam.Gupta@Sun.COM 					 * remote ipp job
105*12091SSonam.Gupta@Sun.COM 					 */
106*12091SSonam.Gupta@Sun.COM 					return (id);
107*12091SSonam.Gupta@Sun.COM 				}
108*12091SSonam.Gupta@Sun.COM 			}
109*12091SSonam.Gupta@Sun.COM 		}
110*12091SSonam.Gupta@Sun.COM 	}
111*12091SSonam.Gupta@Sun.COM 	return (id);
112*12091SSonam.Gupta@Sun.COM }
1130Sstevel@tonic-gate 
1140Sstevel@tonic-gate void
1150Sstevel@tonic-gate papiJobFree(papi_job_t job)
1160Sstevel@tonic-gate {
1170Sstevel@tonic-gate 	job_t *tmp = (job_t *)job;
1180Sstevel@tonic-gate 
1190Sstevel@tonic-gate 	if (tmp != NULL) {
1200Sstevel@tonic-gate 		papiAttributeListFree(tmp->attributes);
1210Sstevel@tonic-gate 		free(tmp);
1220Sstevel@tonic-gate 	}
1230Sstevel@tonic-gate }
1240Sstevel@tonic-gate 
1250Sstevel@tonic-gate void
1260Sstevel@tonic-gate papiJobListFree(papi_job_t *jobs)
1270Sstevel@tonic-gate {
1280Sstevel@tonic-gate 	if (jobs != NULL) {
1290Sstevel@tonic-gate 		int i;
1300Sstevel@tonic-gate 
1310Sstevel@tonic-gate 		for (i = 0; jobs[i] != NULL; i++) {
1320Sstevel@tonic-gate 			papiJobFree(jobs[i]);
1330Sstevel@tonic-gate 		}
1340Sstevel@tonic-gate 		free(jobs);
1350Sstevel@tonic-gate 	}
1360Sstevel@tonic-gate }
1370Sstevel@tonic-gate 
1380Sstevel@tonic-gate papi_attribute_t **
1390Sstevel@tonic-gate papiJobGetAttributeList(papi_job_t job)
1400Sstevel@tonic-gate {
1410Sstevel@tonic-gate 	job_t *tmp = (job_t *)job;
1420Sstevel@tonic-gate 
1430Sstevel@tonic-gate 	if (tmp != NULL)
1440Sstevel@tonic-gate 		return (tmp->attributes);
1450Sstevel@tonic-gate 
1460Sstevel@tonic-gate 	return (NULL);
1470Sstevel@tonic-gate }
1480Sstevel@tonic-gate 
1490Sstevel@tonic-gate char *
1500Sstevel@tonic-gate papiJobGetPrinterName(papi_job_t job)
1510Sstevel@tonic-gate {
1520Sstevel@tonic-gate 	job_t *tmp = (job_t *)job;
1530Sstevel@tonic-gate 	char *result = NULL;
1540Sstevel@tonic-gate 
1550Sstevel@tonic-gate 	if (tmp != NULL)
1560Sstevel@tonic-gate 		papiAttributeListGetString(tmp->attributes, NULL,
1578569SJonathan.Ca@Sun.COM 		    "printer-name", &result);
1580Sstevel@tonic-gate 
1590Sstevel@tonic-gate 	return (result);
1600Sstevel@tonic-gate }
1610Sstevel@tonic-gate 
1620Sstevel@tonic-gate int32_t
1630Sstevel@tonic-gate papiJobGetId(papi_job_t job)
1640Sstevel@tonic-gate {
1650Sstevel@tonic-gate 	job_t *tmp = (job_t *)job;
1660Sstevel@tonic-gate 	int result = -1;
1670Sstevel@tonic-gate 
1680Sstevel@tonic-gate 	if (tmp != NULL)
1690Sstevel@tonic-gate 		papiAttributeListGetInteger(tmp->attributes, NULL, "job-id",
1708569SJonathan.Ca@Sun.COM 		    &result);
1710Sstevel@tonic-gate 
1720Sstevel@tonic-gate 	return (result);
1730Sstevel@tonic-gate }
1740Sstevel@tonic-gate 
1750Sstevel@tonic-gate static REQUEST *
1760Sstevel@tonic-gate create_request(papi_service_t svc, char *printer, papi_attribute_t **attributes)
1770Sstevel@tonic-gate {
1787253Sjacobs 	REQUEST *r;
1790Sstevel@tonic-gate 
1807253Sjacobs 	if ((r = calloc(1, sizeof (*r))) != NULL) {
1819152SSonam.Gupta@Sun.COM 		char *hostname = NULL;
1829152SSonam.Gupta@Sun.COM 
1837253Sjacobs 		r->priority = -1;
1847253Sjacobs 		r->destination = printer_name_from_uri_id(printer, -1);
1859152SSonam.Gupta@Sun.COM 
1869152SSonam.Gupta@Sun.COM 		papiAttributeListGetString(attributes, NULL,
1879152SSonam.Gupta@Sun.COM 		    "job-originating-host-name", &hostname);
1889152SSonam.Gupta@Sun.COM 
1899152SSonam.Gupta@Sun.COM 		if (hostname == NULL) {
1909152SSonam.Gupta@Sun.COM 			char host[BUFSIZ];
1919152SSonam.Gupta@Sun.COM 
1929152SSonam.Gupta@Sun.COM 			if (gethostname(host, sizeof (host)) == 0)
1939152SSonam.Gupta@Sun.COM 				papiAttributeListAddString(&attributes,
1949152SSonam.Gupta@Sun.COM 				    PAPI_ATTR_REPLACE,
1959152SSonam.Gupta@Sun.COM 				    "job-originating-host-name",
1969152SSonam.Gupta@Sun.COM 				    host);
1979152SSonam.Gupta@Sun.COM 		}
1989152SSonam.Gupta@Sun.COM 
1997253Sjacobs 		job_attributes_to_lpsched_request(svc, r, attributes);
2007253Sjacobs 	}
2010Sstevel@tonic-gate 
2027253Sjacobs 	return (r);
2030Sstevel@tonic-gate }
2040Sstevel@tonic-gate 
2050Sstevel@tonic-gate static papi_status_t
2060Sstevel@tonic-gate authorized(service_t *svc, int32_t id)
2070Sstevel@tonic-gate {
2080Sstevel@tonic-gate 	papi_status_t result = PAPI_NOT_AUTHORIZED;	/* assume the worst */
2090Sstevel@tonic-gate 	char file[32];
2100Sstevel@tonic-gate 	REQUEST *r;
2110Sstevel@tonic-gate 
2120Sstevel@tonic-gate 	snprintf(file, sizeof (file), "%d-0", id);
2130Sstevel@tonic-gate 	if ((r = getrequest(file)) != NULL) {
2140Sstevel@tonic-gate 		uid_t uid = getuid();
2150Sstevel@tonic-gate 		struct passwd *pw = NULL;
2160Sstevel@tonic-gate 		char *user = "intruder";	/* assume an intruder */
2170Sstevel@tonic-gate 
2180Sstevel@tonic-gate 		if ((pw = getpwuid(uid)) != NULL)
2190Sstevel@tonic-gate 			user = pw->pw_name;	/* use the process owner */
2200Sstevel@tonic-gate 
2210Sstevel@tonic-gate 		if ((uid == 0) || (uid == 71)) { /* root/lp can forge this */
2220Sstevel@tonic-gate 			papi_status_t s;
2230Sstevel@tonic-gate 			s = papiAttributeListGetString(svc->attributes, NULL,
2248569SJonathan.Ca@Sun.COM 			    "user-name", &user);
2250Sstevel@tonic-gate 			if (s != PAPI_OK)	/* true root/lp are almighty */
2260Sstevel@tonic-gate 				result = PAPI_OK;
2270Sstevel@tonic-gate 		}
2280Sstevel@tonic-gate 
2298966SSonam.Gupta@Sun.COM 		if (result != PAPI_OK) {
2308966SSonam.Gupta@Sun.COM 			if (strcmp(user, r->user) == 0)
2318966SSonam.Gupta@Sun.COM 				result = PAPI_OK;
2328966SSonam.Gupta@Sun.COM 			else {
2338966SSonam.Gupta@Sun.COM 				/*
2349742SSonam.Gupta@Sun.COM 				 * user and r->user might contain the
2358966SSonam.Gupta@Sun.COM 				 * host info also
2368966SSonam.Gupta@Sun.COM 				 */
2379742SSonam.Gupta@Sun.COM 				char *token1 = strtok(r->user, "@");
2389742SSonam.Gupta@Sun.COM 				char *token2 = strtok(NULL, "@");
2399742SSonam.Gupta@Sun.COM 				char *token3 = strtok(user, "@");
2409742SSonam.Gupta@Sun.COM 				char *token4 = strtok(NULL, "@");
2418966SSonam.Gupta@Sun.COM 
2429742SSonam.Gupta@Sun.COM 				/*
2439742SSonam.Gupta@Sun.COM 				 * token1 and token3 contain usernames
2449742SSonam.Gupta@Sun.COM 				 * token2 and token4 contain hostnames
2459742SSonam.Gupta@Sun.COM 				 */
2469742SSonam.Gupta@Sun.COM 				if ((token1 == NULL) || (token3 == NULL))
2479742SSonam.Gupta@Sun.COM 					result = PAPI_NOT_AUTHORIZED;
2489742SSonam.Gupta@Sun.COM 				else if ((token4 != NULL) &&
2499742SSonam.Gupta@Sun.COM 				    (strcmp(token4, "localhost") == 0) &&
2509742SSonam.Gupta@Sun.COM 				    (strcmp(token3, "root") == 0) ||
2519742SSonam.Gupta@Sun.COM 				    (strcmp(token3, "lp") == 0)) {
2529742SSonam.Gupta@Sun.COM 					/*
2539742SSonam.Gupta@Sun.COM 					 * root/lp user on server can
2549742SSonam.Gupta@Sun.COM 					 * cancel any requset
2559742SSonam.Gupta@Sun.COM 					 */
2569742SSonam.Gupta@Sun.COM 					result = PAPI_OK;
2579742SSonam.Gupta@Sun.COM 				} else if (strcmp(token1, token3) == 0) {
2589742SSonam.Gupta@Sun.COM 					/*
2599742SSonam.Gupta@Sun.COM 					 * usernames are same
2609742SSonam.Gupta@Sun.COM 					 * compare the hostnames
2619742SSonam.Gupta@Sun.COM 					 */
2629742SSonam.Gupta@Sun.COM 					if ((token4 != NULL) &&
2639742SSonam.Gupta@Sun.COM 					    (token2 != NULL) &&
2649742SSonam.Gupta@Sun.COM 					    (strcmp(token4, "localhost") ==
2659742SSonam.Gupta@Sun.COM 					    0)) {
2669742SSonam.Gupta@Sun.COM 						/*
2679742SSonam.Gupta@Sun.COM 						 * Its server machine
2689742SSonam.Gupta@Sun.COM 						 */
2699742SSonam.Gupta@Sun.COM 						static char host[256];
2709742SSonam.Gupta@Sun.COM 						if (gethostname(host,
2719742SSonam.Gupta@Sun.COM 						    sizeof (host)) == 0) {
2729742SSonam.Gupta@Sun.COM 							if ((host != NULL) &&
2739742SSonam.Gupta@Sun.COM 							    (strcmp(host,
2749742SSonam.Gupta@Sun.COM 							    token2) == 0))
2759742SSonam.Gupta@Sun.COM 								result =
2769742SSonam.Gupta@Sun.COM 								    PAPI_OK;
2779742SSonam.Gupta@Sun.COM 						}
2789742SSonam.Gupta@Sun.COM 
2799742SSonam.Gupta@Sun.COM 					} else if ((token4 != NULL) &&
2809742SSonam.Gupta@Sun.COM 					    (token2 != NULL) &&
2819742SSonam.Gupta@Sun.COM 					    (strcmp(token4, token2) == 0)) {
2828966SSonam.Gupta@Sun.COM 						result = PAPI_OK;
2839742SSonam.Gupta@Sun.COM 					} else if ((token4 == NULL) &&
2849742SSonam.Gupta@Sun.COM 					    (token2 != NULL)) {
2859742SSonam.Gupta@Sun.COM 						/*
2869742SSonam.Gupta@Sun.COM 						 * When the request is sent from
2879742SSonam.Gupta@Sun.COM 						 * client to server using ipp
2889742SSonam.Gupta@Sun.COM 						 * token4 is NULL
2899742SSonam.Gupta@Sun.COM 						 */
2909742SSonam.Gupta@Sun.COM 						result = PAPI_OK;
2919742SSonam.Gupta@Sun.COM 					}
2928966SSonam.Gupta@Sun.COM 				}
2938966SSonam.Gupta@Sun.COM 			}
2948966SSonam.Gupta@Sun.COM 		}
2950Sstevel@tonic-gate 
2960Sstevel@tonic-gate 		freerequest(r);
2970Sstevel@tonic-gate 	} else
2980Sstevel@tonic-gate 		result = PAPI_NOT_FOUND;
2990Sstevel@tonic-gate 
3000Sstevel@tonic-gate 	return (result);
3010Sstevel@tonic-gate }
3020Sstevel@tonic-gate 
3030Sstevel@tonic-gate static papi_status_t
3042264Sjacobs copy_file(char *from, char *to)
3050Sstevel@tonic-gate {
3060Sstevel@tonic-gate 	int ifd, ofd;
3070Sstevel@tonic-gate 	char buf[BUFSIZ];
3080Sstevel@tonic-gate 	int rc;
3090Sstevel@tonic-gate 
3100Sstevel@tonic-gate 	if ((ifd = open(from, O_RDONLY)) < 0)
3110Sstevel@tonic-gate 		return (PAPI_DOCUMENT_ACCESS_ERROR);
3120Sstevel@tonic-gate 
3130Sstevel@tonic-gate 	if ((ofd = open(to, O_WRONLY)) < 0) {
3140Sstevel@tonic-gate 		close(ifd);
3150Sstevel@tonic-gate 		return (PAPI_NOT_POSSIBLE);
3160Sstevel@tonic-gate 	}
3170Sstevel@tonic-gate 
3180Sstevel@tonic-gate 	while ((rc = read(ifd, buf, sizeof (buf))) > 0)
3190Sstevel@tonic-gate 		write(ofd, buf, rc);
3200Sstevel@tonic-gate 
3210Sstevel@tonic-gate 	close(ifd);
3220Sstevel@tonic-gate 	close(ofd);
3230Sstevel@tonic-gate 
3240Sstevel@tonic-gate 	return (PAPI_OK);
3250Sstevel@tonic-gate }
3260Sstevel@tonic-gate 
3270Sstevel@tonic-gate 
3280Sstevel@tonic-gate #ifdef LP_USE_PAPI_ATTR
3290Sstevel@tonic-gate /*
3300Sstevel@tonic-gate  * *****************************************************************************
3310Sstevel@tonic-gate  *
3320Sstevel@tonic-gate  * Description: Create a file containing all the attributes in the attribute
3330Sstevel@tonic-gate  *              list passed to this function.
3340Sstevel@tonic-gate  *              This file is then passed through lpsched and given to either
3350Sstevel@tonic-gate  *              a slow-filter or to the printer's interface script to process
3360Sstevel@tonic-gate  *              the attributes.
3370Sstevel@tonic-gate  *
3380Sstevel@tonic-gate  * Parameters:  attrs - list of attributes and their values
3390Sstevel@tonic-gate  *              file  - file pathname to create and put the attributes into.
3400Sstevel@tonic-gate  *
3410Sstevel@tonic-gate  * *****************************************************************************
3420Sstevel@tonic-gate  */
3430Sstevel@tonic-gate 
3440Sstevel@tonic-gate static papi_status_t
3452264Sjacobs psm_copy_attrsToFile(papi_attribute_t **attrs, char *file)
3460Sstevel@tonic-gate 
3470Sstevel@tonic-gate {
3480Sstevel@tonic-gate 	papi_status_t result = PAPI_OK;
3492264Sjacobs 
3502264Sjacobs 	if ((attrs != NULL) && (*attrs != NULL)) {
3512264Sjacobs 		FILE *out = NULL;
3520Sstevel@tonic-gate 
3532264Sjacobs 		if ((out = fopen(file, "w")) != NULL) {
3542264Sjacobs 			papiAttributeListPrint(out, attrs, "");
3550Sstevel@tonic-gate 			fclose(out);
3562264Sjacobs 		} else {
3570Sstevel@tonic-gate 			result = PAPI_NOT_POSSIBLE;
3580Sstevel@tonic-gate 		}
3590Sstevel@tonic-gate 	}
3600Sstevel@tonic-gate 
3610Sstevel@tonic-gate 	return (result);
3620Sstevel@tonic-gate } /* psm_copy_attrsToFile */
3630Sstevel@tonic-gate 
3640Sstevel@tonic-gate 
3650Sstevel@tonic-gate /*
3660Sstevel@tonic-gate  * *****************************************************************************
3670Sstevel@tonic-gate  *
3680Sstevel@tonic-gate  * Description: Modify the given attribute 'file' with the attributes from the
3690Sstevel@tonic-gate  *              'attrs' list. Attributes already in the file will be replaced
3700Sstevel@tonic-gate  *              with the new value. New attributes will be added into the file.
3710Sstevel@tonic-gate  *
3720Sstevel@tonic-gate  * Parameters:  attrs - list of attributes and their values
3730Sstevel@tonic-gate  *              file  - file pathname to create and put the attributes into.
3740Sstevel@tonic-gate  *
3750Sstevel@tonic-gate  * *****************************************************************************
3760Sstevel@tonic-gate  */
3770Sstevel@tonic-gate 
3780Sstevel@tonic-gate static papi_status_t
3792264Sjacobs psm_modifyAttrsFile(papi_attribute_t **attrs, char *file)
3800Sstevel@tonic-gate 
3810Sstevel@tonic-gate {
3820Sstevel@tonic-gate 	papi_status_t result = PAPI_OK;
3830Sstevel@tonic-gate 	papi_attribute_t **newAttrs = NULL;
3840Sstevel@tonic-gate 	struct stat   tmpBuf;
3850Sstevel@tonic-gate 	FILE *fd = NULL;
3860Sstevel@tonic-gate 
3872264Sjacobs 	if ((attrs != NULL) && (*attrs != NULL) && (file != NULL)) {
3880Sstevel@tonic-gate 
3890Sstevel@tonic-gate 		/*
3900Sstevel@tonic-gate 		 * check file exist before try to modify it, if it doesn't
3910Sstevel@tonic-gate 		 * exist assume there is an error
3920Sstevel@tonic-gate 		 */
3932264Sjacobs 		if (stat(file, &tmpBuf) == 0) {
3940Sstevel@tonic-gate 			/*
3950Sstevel@tonic-gate 			 * if file is currently empty just write the given
3960Sstevel@tonic-gate 			 * attributes to the file otherwise exact the attributes
3970Sstevel@tonic-gate 			 * from the file and modify them accordingly before
3980Sstevel@tonic-gate 			 * writing them back to the file
3990Sstevel@tonic-gate 			 */
4002264Sjacobs 			if (tmpBuf.st_size == 0) {
4010Sstevel@tonic-gate 				newAttrs = (papi_attribute_t **)attrs;
4020Sstevel@tonic-gate 
4030Sstevel@tonic-gate 				fd = fopen(file, "w");
4042264Sjacobs 				if (fd != NULL) {
4050Sstevel@tonic-gate 					papiAttributeListPrint(fd,
4062264Sjacobs 							newAttrs, "");
4070Sstevel@tonic-gate 					fclose(fd);
4082264Sjacobs 				} else {
4090Sstevel@tonic-gate 					result = PAPI_NOT_POSSIBLE;
4100Sstevel@tonic-gate 				}
4112264Sjacobs 			} else {
4120Sstevel@tonic-gate 				result =
4130Sstevel@tonic-gate 				    psm_modifyAttrsList(file, attrs, &newAttrs);
4140Sstevel@tonic-gate 
4150Sstevel@tonic-gate 				fd = fopen(file, "w");
4162264Sjacobs 				if (fd != NULL) {
4170Sstevel@tonic-gate 					papiAttributeListPrint(fd,
4182264Sjacobs 								newAttrs, "");
4190Sstevel@tonic-gate 					fclose(fd);
4202264Sjacobs 				} else {
4210Sstevel@tonic-gate 					result = PAPI_NOT_POSSIBLE;
4220Sstevel@tonic-gate 				}
4230Sstevel@tonic-gate 
4240Sstevel@tonic-gate 				papiAttributeListFree(newAttrs);
4250Sstevel@tonic-gate 			}
4262264Sjacobs 		} else {
4270Sstevel@tonic-gate 			result = PAPI_NOT_POSSIBLE;
4280Sstevel@tonic-gate 		}
4290Sstevel@tonic-gate 	}
4300Sstevel@tonic-gate 
4310Sstevel@tonic-gate 	return (result);
4320Sstevel@tonic-gate } /* psm_modifyAttrsFile */
4330Sstevel@tonic-gate 
4340Sstevel@tonic-gate 
4350Sstevel@tonic-gate /*
4360Sstevel@tonic-gate  * *****************************************************************************
4370Sstevel@tonic-gate  *
4380Sstevel@tonic-gate  * Description: Extracts the attributes in the given attribute 'file' and
4390Sstevel@tonic-gate  *              creates a new list 'newAttrs' containing the modified list of
4400Sstevel@tonic-gate  *              attributes.
4410Sstevel@tonic-gate  *
4420Sstevel@tonic-gate  * Parameters:  file  - pathname of file containing attributes to be modified
4430Sstevel@tonic-gate  *              attrs - list of attributes and their values to modify
4440Sstevel@tonic-gate  *              newAttrs - returns the modified list of attributes
4450Sstevel@tonic-gate  *
4460Sstevel@tonic-gate  * *****************************************************************************
4470Sstevel@tonic-gate  */
4480Sstevel@tonic-gate 
4490Sstevel@tonic-gate static papi_status_t
4502264Sjacobs psm_modifyAttrsList(char *file, papi_attribute_t **attrs,
4510Sstevel@tonic-gate     papi_attribute_t ***newAttrs)
4520Sstevel@tonic-gate 
4530Sstevel@tonic-gate {
4540Sstevel@tonic-gate 	papi_status_t result = PAPI_OK;
4550Sstevel@tonic-gate 	papi_attribute_t  *nextAttr = NULL;
4560Sstevel@tonic-gate 	papi_attribute_value_t  **values = NULL;
4570Sstevel@tonic-gate 	void *iter = NULL;
4580Sstevel@tonic-gate 	FILE *fd = NULL;
4590Sstevel@tonic-gate 	register int fD = 0;
4600Sstevel@tonic-gate 	char aBuff[200];
4610Sstevel@tonic-gate 	char *a = NULL;
4620Sstevel@tonic-gate 	char *p = NULL;
4630Sstevel@tonic-gate 	int count = 0;
4640Sstevel@tonic-gate 	int n = 0;
4650Sstevel@tonic-gate 
4660Sstevel@tonic-gate 	fd = fopen(file, "r");
4672264Sjacobs 	if (fd != NULL) {
4680Sstevel@tonic-gate 		fD = fileno(fd);
4690Sstevel@tonic-gate 		a = &aBuff[0];
4700Sstevel@tonic-gate 		p = &aBuff[0];
4710Sstevel@tonic-gate 		count = read(fD, &aBuff[0], sizeof (aBuff) - 1);
4722264Sjacobs 		while ((result == PAPI_OK) && (count > 0)) {
4730Sstevel@tonic-gate 			aBuff[count+n] = '\0';
4742264Sjacobs 			if (count == sizeof (aBuff) - n - 1) {
4750Sstevel@tonic-gate 				p = strrchr(aBuff, '\n');
4762264Sjacobs 				if (p != NULL) {
4770Sstevel@tonic-gate 					/* terminate at last complete line */
4780Sstevel@tonic-gate 					*p = '\0';
4790Sstevel@tonic-gate 				}
4800Sstevel@tonic-gate 			}
4810Sstevel@tonic-gate 			result = papiAttributeListFromString(
4820Sstevel@tonic-gate 				newAttrs, PAPI_ATTR_EXCL, aBuff);
4830Sstevel@tonic-gate 
4842264Sjacobs 			if (result == PAPI_OK) {
4850Sstevel@tonic-gate 				/*
4860Sstevel@tonic-gate 				 * handle any part lines and then read the next
4870Sstevel@tonic-gate 				 * buffer from the file
4880Sstevel@tonic-gate 				 */
4890Sstevel@tonic-gate 				n = 0;
4902264Sjacobs 				if (p != a) {
4910Sstevel@tonic-gate 					p++; /* skip NL */
4920Sstevel@tonic-gate 					n = sizeof (aBuff) - 1 - (p - a);
4930Sstevel@tonic-gate 					strncpy(aBuff, p, n);
4940Sstevel@tonic-gate 				}
4950Sstevel@tonic-gate 				count = read(fD, &aBuff[n],
4960Sstevel@tonic-gate 					sizeof (aBuff) - n - 1);
4970Sstevel@tonic-gate 				p = &aBuff[0];
4980Sstevel@tonic-gate 			}
4990Sstevel@tonic-gate 		}
5000Sstevel@tonic-gate 		fclose(fd);
5010Sstevel@tonic-gate 	}
5020Sstevel@tonic-gate 
5030Sstevel@tonic-gate 	/* now modify the attribute list with the new attributes in 'attrs' */
5040Sstevel@tonic-gate 
5050Sstevel@tonic-gate 	nextAttr = papiAttributeListGetNext((papi_attribute_t **)attrs, &iter);
5062264Sjacobs 	while ((result == PAPI_OK) && (nextAttr != NULL)) {
5070Sstevel@tonic-gate 		values = nextAttr->values;
5080Sstevel@tonic-gate 
5092264Sjacobs 		if ((values != NULL) && (*values != NULL)) {
5102264Sjacobs 			result = papiAttributeListAddValue(newAttrs,
5110Sstevel@tonic-gate 						    PAPI_ATTR_REPLACE,
5120Sstevel@tonic-gate 						    nextAttr->name,
5130Sstevel@tonic-gate 						    nextAttr->type, *values);
5140Sstevel@tonic-gate 			values++;
5150Sstevel@tonic-gate 		}
5160Sstevel@tonic-gate 
5170Sstevel@tonic-gate 		while ((result == PAPI_OK) &&
5182264Sjacobs 			(values != NULL) && (*values != NULL)) {
5192264Sjacobs 			result = papiAttributeListAddValue(newAttrs,
5200Sstevel@tonic-gate 						    PAPI_ATTR_APPEND,
5210Sstevel@tonic-gate 						    nextAttr->name,
5220Sstevel@tonic-gate 						    nextAttr->type, *values);
5230Sstevel@tonic-gate 			values++;
5240Sstevel@tonic-gate 		}
5250Sstevel@tonic-gate 		nextAttr =
5260Sstevel@tonic-gate 		    papiAttributeListGetNext((papi_attribute_t **)attrs, &iter);
5270Sstevel@tonic-gate 	}
5280Sstevel@tonic-gate 
5290Sstevel@tonic-gate 	return (result);
5300Sstevel@tonic-gate } /* papi_modifyAttrsList() */
5310Sstevel@tonic-gate #endif
5320Sstevel@tonic-gate 
5330Sstevel@tonic-gate 
5340Sstevel@tonic-gate papi_status_t
5352264Sjacobs papiJobSubmit(papi_service_t handle, char *printer,
5362264Sjacobs 		papi_attribute_t **job_attributes,
5372264Sjacobs 		papi_job_ticket_t *job_ticket,
5382264Sjacobs 		char **files, papi_job_t *job)
5390Sstevel@tonic-gate {
5400Sstevel@tonic-gate 	papi_status_t status;
5410Sstevel@tonic-gate 	service_t *svc = handle;
5428569SJonathan.Ca@Sun.COM 	struct stat statbuf;
5430Sstevel@tonic-gate 	job_t *j;
5440Sstevel@tonic-gate 	int file_no;
5450Sstevel@tonic-gate 	char *request_id = NULL;
5460Sstevel@tonic-gate 	REQUEST *request;
5470Sstevel@tonic-gate 	int i;
5480Sstevel@tonic-gate 	char *c;
5490Sstevel@tonic-gate 	char *tmp = NULL;
5500Sstevel@tonic-gate 	char lpfile[BUFSIZ];
5510Sstevel@tonic-gate 
5520Sstevel@tonic-gate 	if ((svc == NULL) || (printer == NULL) || (files == NULL) ||
5530Sstevel@tonic-gate 	    (job == NULL))
5540Sstevel@tonic-gate 		return (PAPI_BAD_ARGUMENT);
5550Sstevel@tonic-gate 
5560Sstevel@tonic-gate 	if (job_ticket != NULL)
5570Sstevel@tonic-gate 		return (PAPI_OPERATION_NOT_SUPPORTED);
5580Sstevel@tonic-gate 
5590Sstevel@tonic-gate 	if (files != NULL)
5608569SJonathan.Ca@Sun.COM 		for (file_no = 0; files[file_no] != NULL; file_no++) {
5610Sstevel@tonic-gate 			if (access(files[file_no], R_OK) < 0) {
5620Sstevel@tonic-gate 				detailed_error(svc,
5638569SJonathan.Ca@Sun.COM 				    gettext("Cannot access file: %s: %s"),
5648569SJonathan.Ca@Sun.COM 				    files[file_no], strerror(errno));
5650Sstevel@tonic-gate 				return (PAPI_BAD_ARGUMENT);
5660Sstevel@tonic-gate 			}
5679224SJonathan.Ca@Sun.COM 			if (stat(files[file_no], &statbuf) < 0) {
5689224SJonathan.Ca@Sun.COM 				detailed_error(svc,
5699224SJonathan.Ca@Sun.COM 				    gettext("Cannot access file: %s: %s"),
5709224SJonathan.Ca@Sun.COM 				    files[file_no], strerror(errno));
5719224SJonathan.Ca@Sun.COM 				return (PAPI_DOCUMENT_ACCESS_ERROR);
5729224SJonathan.Ca@Sun.COM 			}
5738569SJonathan.Ca@Sun.COM 			if (statbuf.st_size == 0) {
5748569SJonathan.Ca@Sun.COM 				detailed_error(svc,
5758569SJonathan.Ca@Sun.COM 				    gettext("Zero byte (empty) file: %s"),
5768569SJonathan.Ca@Sun.COM 				    files[file_no]);
5778569SJonathan.Ca@Sun.COM 				return (PAPI_BAD_ARGUMENT);
5788569SJonathan.Ca@Sun.COM 			}
5798569SJonathan.Ca@Sun.COM 		}
5800Sstevel@tonic-gate 
5810Sstevel@tonic-gate 	if ((*job = j = calloc(1, sizeof (*j))) == NULL)
5820Sstevel@tonic-gate 		return (PAPI_TEMPORARY_ERROR);
5830Sstevel@tonic-gate 
5840Sstevel@tonic-gate 	/* file_no + 1 for the control file (-0) */
5850Sstevel@tonic-gate 	status = lpsched_alloc_files(svc, file_no + 1, &request_id);
5860Sstevel@tonic-gate 	if (status != PAPI_OK)
5870Sstevel@tonic-gate 		return (status);
5880Sstevel@tonic-gate 
5890Sstevel@tonic-gate 	request = create_request(svc, (char *)printer,
5908569SJonathan.Ca@Sun.COM 	    (papi_attribute_t **)job_attributes);
5910Sstevel@tonic-gate 
5920Sstevel@tonic-gate 	for (i = 0; files[i] != NULL; i++) {
5930Sstevel@tonic-gate 		papi_status_t status;
5940Sstevel@tonic-gate 		snprintf(lpfile, sizeof (lpfile), "%s%s-%d",
5958569SJonathan.Ca@Sun.COM 		    "/var/spool/lp/temp/", request_id, i+1);
5960Sstevel@tonic-gate 		status = copy_file(files[i], lpfile);
5970Sstevel@tonic-gate 		if (status != PAPI_OK) {
5980Sstevel@tonic-gate 			detailed_error(svc,
5998569SJonathan.Ca@Sun.COM 			    gettext("unable to copy: %s -> %s: %s"),
6008569SJonathan.Ca@Sun.COM 			    files[i], lpfile, strerror(errno));
6010Sstevel@tonic-gate 				freerequest(request);
6020Sstevel@tonic-gate 			return (PAPI_DEVICE_ERROR);
6030Sstevel@tonic-gate 		}
6040Sstevel@tonic-gate 		addlist(&(request->file_list), lpfile);
6050Sstevel@tonic-gate 	}
6060Sstevel@tonic-gate 
6070Sstevel@tonic-gate #ifdef LP_USE_PAPI_ATTR
6080Sstevel@tonic-gate 	/*
6090Sstevel@tonic-gate 	 * store the job attributes in the PAPI job attribute file that was
6100Sstevel@tonic-gate 	 * created by lpsched_alloc_files(), the attributes will then pass
6110Sstevel@tonic-gate 	 * through lpsched and be given to the slow-filters and the printer's
6120Sstevel@tonic-gate 	 * interface script to process them
6130Sstevel@tonic-gate 	 */
6140Sstevel@tonic-gate 	snprintf(lpfile, sizeof (lpfile), "%s%s-%s",
6158569SJonathan.Ca@Sun.COM 	    "/var/spool/lp/temp/", request_id, LP_PAPIATTRNAME);
6160Sstevel@tonic-gate 	status = psm_copy_attrsToFile(job_attributes, lpfile);
6170Sstevel@tonic-gate 	if (status != PAPI_OK) {
6180Sstevel@tonic-gate 		detailed_error(svc, "unable to copy attributes to file: %s: %s",
6198569SJonathan.Ca@Sun.COM 		    lpfile, strerror(errno));
6200Sstevel@tonic-gate 		return (PAPI_DEVICE_ERROR);
6210Sstevel@tonic-gate 	}
6220Sstevel@tonic-gate #endif
6230Sstevel@tonic-gate 
6240Sstevel@tonic-gate 	/* store the meta-data file */
6250Sstevel@tonic-gate 	snprintf(lpfile, sizeof (lpfile), "%s-0", request_id);
6260Sstevel@tonic-gate 	if (putrequest(lpfile, request) < 0) {
6270Sstevel@tonic-gate 		detailed_error(svc, gettext("unable to save request: %s: %s"),
6288569SJonathan.Ca@Sun.COM 		    lpfile, strerror(errno));
6290Sstevel@tonic-gate 		freerequest(request);
6300Sstevel@tonic-gate 		return (PAPI_DEVICE_ERROR);
6310Sstevel@tonic-gate 	}
6320Sstevel@tonic-gate 
6330Sstevel@tonic-gate 	status = lpsched_commit_job(svc, lpfile, &tmp);
6340Sstevel@tonic-gate 	if (status != PAPI_OK) {
6350Sstevel@tonic-gate 		unlink(lpfile);
6360Sstevel@tonic-gate 		freerequest(request);
6370Sstevel@tonic-gate 		return (status);
6380Sstevel@tonic-gate 	}
6390Sstevel@tonic-gate 
6400Sstevel@tonic-gate 	lpsched_request_to_job_attributes(request, j);
6410Sstevel@tonic-gate 	freerequest(request);
6420Sstevel@tonic-gate 
6430Sstevel@tonic-gate 	if ((c = strrchr(tmp, '-')) != NULL)
6440Sstevel@tonic-gate 		c++;
6450Sstevel@tonic-gate 	papiAttributeListAddInteger(&j->attributes, PAPI_ATTR_REPLACE,
6468569SJonathan.Ca@Sun.COM 	    "job-id", atoi(c));
6470Sstevel@tonic-gate 	papiAttributeListAddString(&j->attributes, PAPI_ATTR_REPLACE,
6488569SJonathan.Ca@Sun.COM 	    "job-uri", tmp);
6490Sstevel@tonic-gate 
6500Sstevel@tonic-gate 	return (PAPI_OK);
6510Sstevel@tonic-gate }
6520Sstevel@tonic-gate 
6530Sstevel@tonic-gate papi_status_t
6542264Sjacobs papiJobSubmitByReference(papi_service_t handle, char *printer,
6552264Sjacobs 		papi_attribute_t **job_attributes,
6562264Sjacobs 		papi_job_ticket_t *job_ticket,
6572264Sjacobs 		char **files, papi_job_t *job)
6580Sstevel@tonic-gate {
6590Sstevel@tonic-gate 	service_t *svc = handle;
6608569SJonathan.Ca@Sun.COM 	struct stat statbuf;
6610Sstevel@tonic-gate 	job_t *j;
6620Sstevel@tonic-gate 	int file_no;
6630Sstevel@tonic-gate 	short status;
6640Sstevel@tonic-gate 	char *request_id = NULL;
6650Sstevel@tonic-gate 	REQUEST *request;
6660Sstevel@tonic-gate 	char *c;
6670Sstevel@tonic-gate 	char *tmp = NULL;
6680Sstevel@tonic-gate 	char lpfile[BUFSIZ];
6690Sstevel@tonic-gate 	char **file_list = NULL;
6700Sstevel@tonic-gate 
6710Sstevel@tonic-gate 	if ((svc == NULL) || (printer == NULL) || (files == NULL) ||
6720Sstevel@tonic-gate 	    (job == NULL))
6730Sstevel@tonic-gate 		return (PAPI_BAD_ARGUMENT);
6740Sstevel@tonic-gate 
6750Sstevel@tonic-gate 	if (job_ticket != NULL)
6760Sstevel@tonic-gate 		return (PAPI_OPERATION_NOT_SUPPORTED);
6770Sstevel@tonic-gate 
6780Sstevel@tonic-gate 	if (files != NULL)
6790Sstevel@tonic-gate 		for (file_no = 0; files[file_no] != NULL; file_no++) {
6800Sstevel@tonic-gate 			if (access(files[file_no], R_OK) < 0) {
6810Sstevel@tonic-gate 				detailed_error(svc,
6828569SJonathan.Ca@Sun.COM 				    gettext("Cannot access file: %s: %s"),
6838569SJonathan.Ca@Sun.COM 				    files[file_no], strerror(errno));
6842660Sjacobs 				return (PAPI_DOCUMENT_ACCESS_ERROR);
6850Sstevel@tonic-gate 			}
6869224SJonathan.Ca@Sun.COM 			if (stat(files[file_no], &statbuf) < 0) {
6879224SJonathan.Ca@Sun.COM 				detailed_error(svc,
6889224SJonathan.Ca@Sun.COM 				    gettext("Cannot access file: %s: %s"),
6899224SJonathan.Ca@Sun.COM 				    files[file_no], strerror(errno));
6909224SJonathan.Ca@Sun.COM 				return (PAPI_DOCUMENT_ACCESS_ERROR);
6919224SJonathan.Ca@Sun.COM 			}
6928569SJonathan.Ca@Sun.COM 			if (statbuf.st_size == 0) {
6938569SJonathan.Ca@Sun.COM 				detailed_error(svc,
6948569SJonathan.Ca@Sun.COM 				    gettext("Zero byte (empty) file: %s"),
6958569SJonathan.Ca@Sun.COM 				    files[file_no]);
6968569SJonathan.Ca@Sun.COM 				return (PAPI_BAD_ARGUMENT);
6978569SJonathan.Ca@Sun.COM 			}
6988569SJonathan.Ca@Sun.COM 
6992660Sjacobs 			if (files[file_no][0] != '/') {
7002660Sjacobs 				char path[MAXPATHLEN];
7012660Sjacobs 
7022660Sjacobs 				if (getcwd(path, sizeof (path)) == NULL) {
7032660Sjacobs 					detailed_error(svc, gettext(
7048569SJonathan.Ca@Sun.COM 					    "getcwd for file: %s: %s"),
7058569SJonathan.Ca@Sun.COM 					    files[file_no],
7068569SJonathan.Ca@Sun.COM 					    strerror(errno));
7072660Sjacobs 					return (PAPI_DOCUMENT_ACCESS_ERROR);
7082660Sjacobs 				}
7092660Sjacobs 				strlcat(path, "/", sizeof (path));
7102660Sjacobs 				if (strlcat(path, files[file_no], sizeof (path))
7118569SJonathan.Ca@Sun.COM 				    >= sizeof (path)) {
7122660Sjacobs 					detailed_error(svc, gettext(
7138569SJonathan.Ca@Sun.COM 					    "pathname too long: %s"),
7148569SJonathan.Ca@Sun.COM 					    files[file_no]);
7152660Sjacobs 					return (PAPI_DOCUMENT_ACCESS_ERROR);
7162660Sjacobs 				}
7172660Sjacobs 				addlist(&file_list, path);
7182660Sjacobs 			} else
7192660Sjacobs 				addlist(&file_list, (char *)files[file_no]);
7200Sstevel@tonic-gate 		}
7210Sstevel@tonic-gate 
7220Sstevel@tonic-gate 	if ((*job = j = calloc(1, sizeof (*j))) == NULL)
7230Sstevel@tonic-gate 		return (PAPI_TEMPORARY_ERROR);
7240Sstevel@tonic-gate 
7250Sstevel@tonic-gate 	/* 1 for the control file (-0) */
7260Sstevel@tonic-gate 	status = lpsched_alloc_files(svc, 1, &request_id);
7270Sstevel@tonic-gate 	if (status != PAPI_OK)
7280Sstevel@tonic-gate 		return (status);
7290Sstevel@tonic-gate 
7300Sstevel@tonic-gate 	request = create_request(svc, (char *)printer,
7318569SJonathan.Ca@Sun.COM 	    (papi_attribute_t **)job_attributes);
7320Sstevel@tonic-gate 	request->file_list = file_list;
7330Sstevel@tonic-gate 
7340Sstevel@tonic-gate #ifdef LP_USE_PAPI_ATTR
7350Sstevel@tonic-gate 	/*
7360Sstevel@tonic-gate 	 * store the job attributes in the PAPI job attribute file that was
7370Sstevel@tonic-gate 	 * created by lpsched_alloc_files(), the attributes will then pass
7380Sstevel@tonic-gate 	 * through lpsched and be given to the slow-filters and the printer's
7390Sstevel@tonic-gate 	 * interface script to process them
7400Sstevel@tonic-gate 	 */
7410Sstevel@tonic-gate 	snprintf(lpfile, sizeof (lpfile), "%s%s-%s",
7428569SJonathan.Ca@Sun.COM 	    "/var/spool/lp/temp/", request_id, LP_PAPIATTRNAME);
7430Sstevel@tonic-gate 	status = psm_copy_attrsToFile(job_attributes, lpfile);
7440Sstevel@tonic-gate 	if (status != PAPI_OK) {
7450Sstevel@tonic-gate 		detailed_error(svc, "unable to copy attributes to file: %s: %s",
7468569SJonathan.Ca@Sun.COM 		    lpfile, strerror(errno));
7470Sstevel@tonic-gate 		return (PAPI_DEVICE_ERROR);
7480Sstevel@tonic-gate 	}
7490Sstevel@tonic-gate #endif
7500Sstevel@tonic-gate 
7510Sstevel@tonic-gate 	/* store the meta-data file */
7520Sstevel@tonic-gate 	snprintf(lpfile, sizeof (lpfile), "%s-0", request_id);
7530Sstevel@tonic-gate 	if (putrequest(lpfile, request) < 0) {
7540Sstevel@tonic-gate 		detailed_error(svc, gettext("unable to save request: %s: %s"),
7558569SJonathan.Ca@Sun.COM 		    lpfile, strerror(errno));
7560Sstevel@tonic-gate 		freerequest(request);
7570Sstevel@tonic-gate 		return (PAPI_DEVICE_ERROR);
7580Sstevel@tonic-gate 	}
7590Sstevel@tonic-gate 
7600Sstevel@tonic-gate 	status = lpsched_commit_job(svc, lpfile, &tmp);
7610Sstevel@tonic-gate 	if (status != PAPI_OK) {
7620Sstevel@tonic-gate 		unlink(lpfile);
7630Sstevel@tonic-gate 		freerequest(request);
7640Sstevel@tonic-gate 		return (status);
7650Sstevel@tonic-gate 	}
7660Sstevel@tonic-gate 
7670Sstevel@tonic-gate 	lpsched_request_to_job_attributes(request, j);
7680Sstevel@tonic-gate 
7690Sstevel@tonic-gate 	freerequest(request);
7700Sstevel@tonic-gate 
7710Sstevel@tonic-gate 	if ((c = strrchr(tmp, '-')) != NULL)
7720Sstevel@tonic-gate 		c++;
7730Sstevel@tonic-gate 	papiAttributeListAddInteger(&j->attributes, PAPI_ATTR_REPLACE,
7748569SJonathan.Ca@Sun.COM 	    "job-id", atoi(c));
7750Sstevel@tonic-gate 	papiAttributeListAddString(&j->attributes, PAPI_ATTR_REPLACE,
7768569SJonathan.Ca@Sun.COM 	    "job-uri", tmp);
7770Sstevel@tonic-gate 
7780Sstevel@tonic-gate 	return (PAPI_OK);
7790Sstevel@tonic-gate }
7800Sstevel@tonic-gate 
7810Sstevel@tonic-gate papi_status_t
7822264Sjacobs papiJobValidate(papi_service_t handle, char *printer,
7832264Sjacobs 		papi_attribute_t **job_attributes,
7842264Sjacobs 		papi_job_ticket_t *job_ticket,
7852264Sjacobs 		char **files, papi_job_t *job)
7860Sstevel@tonic-gate {
7870Sstevel@tonic-gate 	papi_status_t status;
7880Sstevel@tonic-gate 	papi_attribute_t **attributes = NULL;
7890Sstevel@tonic-gate 	int i;
7900Sstevel@tonic-gate 
7910Sstevel@tonic-gate 	papiAttributeListAddString(&attributes, PAPI_ATTR_REPLACE,
7928569SJonathan.Ca@Sun.COM 	    "job-hold-until", "indefinite");
7930Sstevel@tonic-gate 	for (i = 0; job_attributes[i]; i++)
7940Sstevel@tonic-gate 		list_append(&attributes, job_attributes[i]);
7950Sstevel@tonic-gate 
7960Sstevel@tonic-gate 	status = papiJobSubmitByReference(handle, printer,
7978569SJonathan.Ca@Sun.COM 	    (papi_attribute_t **)attributes,
7988569SJonathan.Ca@Sun.COM 	    job_ticket, files, job);
7990Sstevel@tonic-gate 	if (status == PAPI_OK) {
8000Sstevel@tonic-gate 		int id = papiJobGetId(*job);
8010Sstevel@tonic-gate 
8020Sstevel@tonic-gate 		if (id != -1)
8030Sstevel@tonic-gate 			papiJobCancel(handle, printer, id);
8040Sstevel@tonic-gate 	}
8050Sstevel@tonic-gate 
8060Sstevel@tonic-gate 	attributes[1] = NULL;	/* after attr[0], they are in another list */
8070Sstevel@tonic-gate 	papiAttributeListFree(attributes);
8080Sstevel@tonic-gate 
8090Sstevel@tonic-gate 	return (status);
8100Sstevel@tonic-gate }
8110Sstevel@tonic-gate 
8120Sstevel@tonic-gate papi_status_t
8132264Sjacobs papiJobStreamOpen(papi_service_t handle, char *printer,
8142264Sjacobs 		papi_attribute_t **job_attributes,
8152264Sjacobs 		papi_job_ticket_t *job_ticket, papi_stream_t *stream)
8160Sstevel@tonic-gate {
8170Sstevel@tonic-gate 	papi_status_t status;
8180Sstevel@tonic-gate 	service_t *svc = handle;
8190Sstevel@tonic-gate 	job_stream_t *s = NULL;
8200Sstevel@tonic-gate 	char *request_id = NULL;
8210Sstevel@tonic-gate 	char lpfile[BUFSIZ];
8220Sstevel@tonic-gate 
8230Sstevel@tonic-gate 	if ((svc == NULL) || (printer == NULL) || (stream == NULL))
8240Sstevel@tonic-gate 		return (PAPI_BAD_ARGUMENT);
8250Sstevel@tonic-gate 
8260Sstevel@tonic-gate 	if (job_ticket != NULL)
8270Sstevel@tonic-gate 		return (PAPI_OPERATION_NOT_SUPPORTED);
8280Sstevel@tonic-gate 
8290Sstevel@tonic-gate 	if ((*stream = s = calloc(1, sizeof (*s))) == NULL)
8300Sstevel@tonic-gate 		return (PAPI_TEMPORARY_ERROR);
8310Sstevel@tonic-gate 
8320Sstevel@tonic-gate 	/* 1 for data, 1 for the meta-data (-0) */
8330Sstevel@tonic-gate 	status = lpsched_alloc_files(svc, 2, &request_id);
8340Sstevel@tonic-gate 	if (status != PAPI_OK)
8350Sstevel@tonic-gate 		return (status);
8360Sstevel@tonic-gate 
8370Sstevel@tonic-gate 	s->request = create_request(svc, (char *)printer,
8388569SJonathan.Ca@Sun.COM 	    (papi_attribute_t **)job_attributes);
8390Sstevel@tonic-gate 	snprintf(lpfile, sizeof (lpfile), "/var/spool/lp/temp/%s-1",
8408569SJonathan.Ca@Sun.COM 	    request_id);
8410Sstevel@tonic-gate 	s->fd = open(lpfile, O_WRONLY);
8420Sstevel@tonic-gate 	addlist(&(s->request->file_list), lpfile);
8430Sstevel@tonic-gate 
8440Sstevel@tonic-gate #ifdef LP_USE_PAPI_ATTR
8450Sstevel@tonic-gate 	/*
8460Sstevel@tonic-gate 	 * store the job attributes in the PAPI job attribute file that was
8470Sstevel@tonic-gate 	 * created by lpsched_alloc_files(), the attributes will then pass
8480Sstevel@tonic-gate 	 * through lpsched and be given to the slow-filters and the printer's
8490Sstevel@tonic-gate 	 * interface script to process them
8500Sstevel@tonic-gate 	 */
8510Sstevel@tonic-gate 	snprintf(lpfile, sizeof (lpfile), "%s%s-%s",
8528569SJonathan.Ca@Sun.COM 	    "/var/spool/lp/temp/", request_id, LP_PAPIATTRNAME);
8530Sstevel@tonic-gate 	status = psm_copy_attrsToFile(job_attributes, lpfile);
8540Sstevel@tonic-gate 	if (status != PAPI_OK) {
8550Sstevel@tonic-gate 		detailed_error(svc, "unable to copy attributes to file: %s: %s",
8568569SJonathan.Ca@Sun.COM 		    lpfile, strerror(errno));
8570Sstevel@tonic-gate 		close(s->fd);
8580Sstevel@tonic-gate 		free(s);
8590Sstevel@tonic-gate 		return (PAPI_DEVICE_ERROR);
8600Sstevel@tonic-gate 	}
8610Sstevel@tonic-gate #endif
8620Sstevel@tonic-gate 
8630Sstevel@tonic-gate 	/* store the meta-data file */
8640Sstevel@tonic-gate 	snprintf(lpfile, sizeof (lpfile), "%s-0", request_id);
8650Sstevel@tonic-gate 	s->meta_data_file = strdup(lpfile);
8660Sstevel@tonic-gate 	if (putrequest(lpfile, s->request) < 0) {
8670Sstevel@tonic-gate 		detailed_error(svc, gettext("unable to save request: %s: %s"),
8688569SJonathan.Ca@Sun.COM 		    lpfile, strerror(errno));
8690Sstevel@tonic-gate 		s->request = NULL;
8700Sstevel@tonic-gate 		return (PAPI_DEVICE_ERROR);
8710Sstevel@tonic-gate 	}
8720Sstevel@tonic-gate 
8730Sstevel@tonic-gate 	return (PAPI_OK);
8740Sstevel@tonic-gate }
8750Sstevel@tonic-gate 
8760Sstevel@tonic-gate papi_status_t
8770Sstevel@tonic-gate papiJobStreamWrite(papi_service_t handle,
8782264Sjacobs 		papi_stream_t stream, void *buffer, size_t buflen)
8790Sstevel@tonic-gate {
8800Sstevel@tonic-gate 	service_t *svc = handle;
8810Sstevel@tonic-gate 	job_stream_t *s = stream;
8820Sstevel@tonic-gate 
8830Sstevel@tonic-gate 	if ((svc == NULL) || (stream == NULL) || (buffer == NULL))
8840Sstevel@tonic-gate 		return (PAPI_BAD_ARGUMENT);
8850Sstevel@tonic-gate 
8860Sstevel@tonic-gate 	if (write(s->fd, buffer, buflen) != buflen)
8870Sstevel@tonic-gate 		return (PAPI_DEVICE_ERROR);
8880Sstevel@tonic-gate 
8890Sstevel@tonic-gate 	return (PAPI_OK);
8900Sstevel@tonic-gate }
8910Sstevel@tonic-gate papi_status_t
8920Sstevel@tonic-gate papiJobStreamClose(papi_service_t handle,
8930Sstevel@tonic-gate 		papi_stream_t stream, papi_job_t *job)
8940Sstevel@tonic-gate {
8950Sstevel@tonic-gate 	papi_status_t status = PAPI_OK;
8960Sstevel@tonic-gate 	service_t *svc = handle;
8970Sstevel@tonic-gate 	job_stream_t *s = stream;
8980Sstevel@tonic-gate 	job_t *j = NULL;
8990Sstevel@tonic-gate 	char *tmp = NULL, *c;
9000Sstevel@tonic-gate 
9010Sstevel@tonic-gate 	if ((svc == NULL) || (stream == NULL) || (job == NULL))
9020Sstevel@tonic-gate 		return (PAPI_BAD_ARGUMENT);
9030Sstevel@tonic-gate 
9040Sstevel@tonic-gate 	if ((*job = j = calloc(1, sizeof (*j))) == NULL)
9050Sstevel@tonic-gate 		return (PAPI_TEMPORARY_ERROR);
9060Sstevel@tonic-gate 
9070Sstevel@tonic-gate 	close(s->fd);
9080Sstevel@tonic-gate 
9090Sstevel@tonic-gate 	lpsched_request_to_job_attributes(s->request, j);
9100Sstevel@tonic-gate 
9110Sstevel@tonic-gate 	if (s->meta_data_file != NULL) {
9120Sstevel@tonic-gate 		status = lpsched_commit_job(svc, s->meta_data_file, &tmp);
9130Sstevel@tonic-gate 		if (status != PAPI_OK) {
9140Sstevel@tonic-gate 			unlink(s->meta_data_file);
9150Sstevel@tonic-gate 			return (status);
9160Sstevel@tonic-gate 		}
9170Sstevel@tonic-gate 		if ((c = strrchr(tmp, '-')) != NULL)
9180Sstevel@tonic-gate 			c++;
9190Sstevel@tonic-gate 		papiAttributeListAddInteger(&j->attributes, PAPI_ATTR_REPLACE,
9208569SJonathan.Ca@Sun.COM 		    "job-id", atoi(c));
9210Sstevel@tonic-gate 		papiAttributeListAddString(&j->attributes, PAPI_ATTR_REPLACE,
9228569SJonathan.Ca@Sun.COM 		    "job-uri", tmp);
9230Sstevel@tonic-gate 		free(s->meta_data_file);
9240Sstevel@tonic-gate 	}
9257253Sjacobs 	freerequest(s->request);
9260Sstevel@tonic-gate 	free(s);
9270Sstevel@tonic-gate 
9280Sstevel@tonic-gate 	return (PAPI_OK);
9290Sstevel@tonic-gate }
9300Sstevel@tonic-gate 
9310Sstevel@tonic-gate papi_status_t
9322264Sjacobs papiJobQuery(papi_service_t handle, char *printer, int32_t job_id,
9332264Sjacobs 		char **requested_attrs,
9340Sstevel@tonic-gate 		papi_job_t *job)
9350Sstevel@tonic-gate {
9360Sstevel@tonic-gate 	service_t *svc = handle;
9370Sstevel@tonic-gate 	job_t *j;
9380Sstevel@tonic-gate 	char *dest;
9390Sstevel@tonic-gate 	char req_id[32];
9400Sstevel@tonic-gate 	short rc;
9410Sstevel@tonic-gate 	char *form = NULL,
9428569SJonathan.Ca@Sun.COM 	    *request_id = NULL,
9438569SJonathan.Ca@Sun.COM 	    *charset = NULL,
9448569SJonathan.Ca@Sun.COM 	    *user = NULL,
9458569SJonathan.Ca@Sun.COM 	    *slabel = NULL,
9468569SJonathan.Ca@Sun.COM 	    *file = NULL;
9470Sstevel@tonic-gate 	time_t date = 0;
9480Sstevel@tonic-gate 	size_t size = 0;
9490Sstevel@tonic-gate 	short  rank = 0,
9508569SJonathan.Ca@Sun.COM 	    state = 0;
9510Sstevel@tonic-gate 
9520Sstevel@tonic-gate 	if ((handle == NULL) || (printer == NULL) || (job_id < 0))
9530Sstevel@tonic-gate 		return (PAPI_BAD_ARGUMENT);
9540Sstevel@tonic-gate 
9550Sstevel@tonic-gate 	dest = printer_name_from_uri_id(printer, job_id);
9560Sstevel@tonic-gate 	snprintf(req_id, sizeof (req_id), "%s-%d", dest, job_id);
9570Sstevel@tonic-gate 	free(dest);
9580Sstevel@tonic-gate 
9590Sstevel@tonic-gate 	rc = snd_msg(svc, S_INQUIRE_REQUEST_RANK, 0, "", "", req_id, "", "");
9600Sstevel@tonic-gate 	if (rc < 0)
9610Sstevel@tonic-gate 		return (PAPI_SERVICE_UNAVAILABLE);
9620Sstevel@tonic-gate 
9630Sstevel@tonic-gate 	if (rcv_msg(svc, R_INQUIRE_REQUEST_RANK, &rc, &request_id,
9648569SJonathan.Ca@Sun.COM 	    &user, &slabel, &size, &date, &state, &dest, &form,
9658569SJonathan.Ca@Sun.COM 	    &charset, &rank, &file) < 0) {
9660Sstevel@tonic-gate 		detailed_error(svc,
9678569SJonathan.Ca@Sun.COM 		    gettext("failed to read response from scheduler"));
9680Sstevel@tonic-gate 		return (PAPI_DEVICE_ERROR);
9690Sstevel@tonic-gate 	}
9700Sstevel@tonic-gate 
9710Sstevel@tonic-gate 	if ((request_id == NULL) || (request_id[0] == NULL))
9720Sstevel@tonic-gate 		return (PAPI_NOT_FOUND);
9730Sstevel@tonic-gate 
9740Sstevel@tonic-gate 	if ((*job = j = calloc(1, sizeof (*j))) == NULL)
9750Sstevel@tonic-gate 		return (PAPI_TEMPORARY_ERROR);
9760Sstevel@tonic-gate 
97710781SGowtham.Thommandra@Sun.COM 	snprintf(req_id, sizeof (req_id), "%d-0", job_id);
97810781SGowtham.Thommandra@Sun.COM 	lpsched_read_job_configuration(svc, j, req_id);
97910781SGowtham.Thommandra@Sun.COM 
9801676Sjpk 	job_status_to_attributes(j, request_id, user, slabel, size, date, state,
9818569SJonathan.Ca@Sun.COM 	    dest, form, charset, rank, file);
9820Sstevel@tonic-gate 
9830Sstevel@tonic-gate 	return (PAPI_OK);
9840Sstevel@tonic-gate }
9850Sstevel@tonic-gate 
9860Sstevel@tonic-gate papi_status_t
9872264Sjacobs papiJobMove(papi_service_t handle, char *printer, int32_t job_id,
9882264Sjacobs 		char *destination)
9892264Sjacobs {
9902264Sjacobs 	papi_status_t result = PAPI_OK;
9918266SNagaraj.Yedathore@Sun.COM 	long bits;
9922264Sjacobs 	service_t *svc = handle;
9932264Sjacobs 	char req_id[64];
9942264Sjacobs 	char *queue;
9952264Sjacobs 	char *user = NULL;
9962264Sjacobs 
9972264Sjacobs 	if ((svc == NULL) || (printer == NULL) || (job_id < 0) ||
9982264Sjacobs 	    (destination == NULL))
9992264Sjacobs 		return (PAPI_BAD_ARGUMENT);
10002264Sjacobs 
10012264Sjacobs 	queue = printer_name_from_uri_id(printer, job_id);
10022264Sjacobs 	snprintf(req_id, sizeof (req_id), "%s-%d", queue, job_id);
10032264Sjacobs 	free(queue);
10042264Sjacobs 
10052264Sjacobs 	if (papiAttributeListGetString(svc->attributes, NULL, "user-name",
10068569SJonathan.Ca@Sun.COM 	    &user) == PAPI_OK) {
10072264Sjacobs 		REQUEST *r = getrequest(req_id);
10082264Sjacobs 
10092264Sjacobs 		if ((r != NULL) && (r->user != NULL) &&
10102264Sjacobs 		    (strcmp(r->user, user) != 0))
10112264Sjacobs 			result = PAPI_NOT_AUTHORIZED;
10122264Sjacobs 		freerequest(r);
10132264Sjacobs 	}
10142264Sjacobs 
10152264Sjacobs 	if (result == PAPI_OK) {
10162264Sjacobs 		short status = MOK;
10172264Sjacobs 		char *dest = printer_name_from_uri_id(destination, -1);
10182264Sjacobs 
10192264Sjacobs 		if ((snd_msg(svc, S_MOVE_REQUEST, req_id, dest) < 0) ||
10208266SNagaraj.Yedathore@Sun.COM 		    (rcv_msg(svc, R_MOVE_REQUEST, &status, &bits) < 0))
10212264Sjacobs 			status = MTRANSMITERR;
10222264Sjacobs 
10232264Sjacobs 		free(dest);
10242264Sjacobs 
10252264Sjacobs 		result = lpsched_status_to_papi_status(status);
10262264Sjacobs 	}
10272264Sjacobs 
10282264Sjacobs 	return (result);
10292264Sjacobs }
10302264Sjacobs 
10312264Sjacobs papi_status_t
10322264Sjacobs papiJobCancel(papi_service_t handle, char *printer, int32_t job_id)
10330Sstevel@tonic-gate {
10340Sstevel@tonic-gate 	papi_status_t result = PAPI_OK;
10350Sstevel@tonic-gate 	service_t *svc = handle;
10360Sstevel@tonic-gate 	char req_id[64];
10370Sstevel@tonic-gate 	char *dest;
10380Sstevel@tonic-gate 	char *user = NULL;
10390Sstevel@tonic-gate 
10400Sstevel@tonic-gate 	if ((svc == NULL) || (printer == NULL) || (job_id < 0))
10410Sstevel@tonic-gate 		return (PAPI_BAD_ARGUMENT);
10420Sstevel@tonic-gate 
10430Sstevel@tonic-gate 	dest = printer_name_from_uri_id(printer, job_id);
10440Sstevel@tonic-gate 	snprintf(req_id, sizeof (req_id), "%s-%d", dest, job_id);
10450Sstevel@tonic-gate 	free(dest);
10460Sstevel@tonic-gate 
10470Sstevel@tonic-gate 	if (papiAttributeListGetString(svc->attributes, NULL, "user-name",
10488569SJonathan.Ca@Sun.COM 	    &user) == PAPI_OK) {
10490Sstevel@tonic-gate 		REQUEST *r = getrequest(req_id);
10500Sstevel@tonic-gate 
10518966SSonam.Gupta@Sun.COM 		if ((result = authorized(handle, job_id)) != PAPI_OK)
10528966SSonam.Gupta@Sun.COM 			result = PAPI_NOT_AUTHORIZED;
10538966SSonam.Gupta@Sun.COM 
10542264Sjacobs 		if ((r != NULL) && (r->user != NULL) &&
10552264Sjacobs 		    (strcmp(r->user, user) != 0))
10560Sstevel@tonic-gate 			result = PAPI_NOT_AUTHORIZED;
10570Sstevel@tonic-gate 		freerequest(r);
10580Sstevel@tonic-gate 	}
10590Sstevel@tonic-gate 
10600Sstevel@tonic-gate 	if (result == PAPI_OK) {
10610Sstevel@tonic-gate 		short status = MOK;
10620Sstevel@tonic-gate 
10630Sstevel@tonic-gate 		if ((snd_msg(svc, S_CANCEL_REQUEST, req_id) < 0) ||
10640Sstevel@tonic-gate 		    (rcv_msg(svc, R_CANCEL_REQUEST, &status) < 0))
10650Sstevel@tonic-gate 			status = MTRANSMITERR;
10660Sstevel@tonic-gate 
10670Sstevel@tonic-gate 		result = lpsched_status_to_papi_status(status);
10680Sstevel@tonic-gate 	}
10690Sstevel@tonic-gate 
10700Sstevel@tonic-gate 	return (result);
10710Sstevel@tonic-gate }
10720Sstevel@tonic-gate 
10730Sstevel@tonic-gate papi_status_t
10742264Sjacobs hold_release_job(papi_service_t handle, char *printer,
10752264Sjacobs 		int32_t job_id, int flag)
10760Sstevel@tonic-gate {
10770Sstevel@tonic-gate 	papi_status_t status;
10780Sstevel@tonic-gate 	service_t *svc = handle;
10790Sstevel@tonic-gate 	REQUEST *r = NULL;
10800Sstevel@tonic-gate 	char *file;
10810Sstevel@tonic-gate 	char *dest;
10820Sstevel@tonic-gate 
10830Sstevel@tonic-gate 	if ((svc == NULL) || (printer == NULL) || (job_id < 0))
10840Sstevel@tonic-gate 		return (PAPI_BAD_ARGUMENT);
10850Sstevel@tonic-gate 
10860Sstevel@tonic-gate 	if ((status = authorized(svc, job_id)) != PAPI_OK)
10870Sstevel@tonic-gate 		return (status);
10880Sstevel@tonic-gate 
10890Sstevel@tonic-gate 	dest = printer_name_from_uri_id(printer, job_id);
10900Sstevel@tonic-gate 	status = lpsched_start_change(svc, dest, job_id, &file);
10910Sstevel@tonic-gate 	if (status != PAPI_OK)
10920Sstevel@tonic-gate 		return (status);
10930Sstevel@tonic-gate 
10940Sstevel@tonic-gate 	if ((r = getrequest(file)) != NULL) {
10950Sstevel@tonic-gate 		r->actions &= ~ACT_RESUME;
10962264Sjacobs 		switch (flag) {
10972264Sjacobs 		case 0:
10980Sstevel@tonic-gate 			r->actions |= ACT_HOLD;
10992264Sjacobs 			break;
11002264Sjacobs 		case 1:
11010Sstevel@tonic-gate 			r->actions |= ACT_RESUME;
11022264Sjacobs 			break;
11032264Sjacobs 		case 2:
11042264Sjacobs 			r->actions |= ACT_IMMEDIATE;
11052264Sjacobs 			break;
11062264Sjacobs 		}
11070Sstevel@tonic-gate 		if (putrequest(file, r) < 0) {
11080Sstevel@tonic-gate 			detailed_error(svc,
11098569SJonathan.Ca@Sun.COM 			    gettext("failed to write job: %s: %s"),
11108569SJonathan.Ca@Sun.COM 			    file, strerror(errno));
11112264Sjacobs 			freerequest(r);
11120Sstevel@tonic-gate 			return (PAPI_DEVICE_ERROR);
11130Sstevel@tonic-gate 		}
11142264Sjacobs 		freerequest(r);
11150Sstevel@tonic-gate 	} else {
11160Sstevel@tonic-gate 		detailed_error(svc, gettext("failed to read job: %s: %s"),
11178569SJonathan.Ca@Sun.COM 		    file, strerror(errno));
11180Sstevel@tonic-gate 		return (PAPI_DEVICE_ERROR);
11190Sstevel@tonic-gate 	}
11200Sstevel@tonic-gate 
11210Sstevel@tonic-gate 	status = lpsched_end_change(svc, dest, job_id);
11220Sstevel@tonic-gate 
11230Sstevel@tonic-gate 	return (status);
11240Sstevel@tonic-gate }
11250Sstevel@tonic-gate 
11260Sstevel@tonic-gate papi_status_t
11272264Sjacobs papiJobHold(papi_service_t handle, char *printer, int32_t job_id)
11280Sstevel@tonic-gate {
11290Sstevel@tonic-gate 	return (hold_release_job(handle, printer, job_id, 0));
11300Sstevel@tonic-gate }
11310Sstevel@tonic-gate 
11320Sstevel@tonic-gate papi_status_t
11332264Sjacobs papiJobRelease(papi_service_t handle, char *printer, int32_t job_id)
11340Sstevel@tonic-gate {
11350Sstevel@tonic-gate 	return (hold_release_job(handle, printer, job_id, 1));
11360Sstevel@tonic-gate }
11370Sstevel@tonic-gate 
11380Sstevel@tonic-gate papi_status_t
11392264Sjacobs papiJobPromote(papi_service_t handle, char *printer, int32_t job_id)
11400Sstevel@tonic-gate {
11412264Sjacobs 	return (hold_release_job(handle, printer, job_id, 2));
11420Sstevel@tonic-gate }
11430Sstevel@tonic-gate 
11440Sstevel@tonic-gate papi_status_t
11452264Sjacobs papiJobModify(papi_service_t handle, char *printer, int32_t job_id,
11462264Sjacobs 		papi_attribute_t **attributes, papi_job_t *job)
11470Sstevel@tonic-gate {
11480Sstevel@tonic-gate 	papi_status_t status;
11490Sstevel@tonic-gate 	job_t *j = NULL;
11500Sstevel@tonic-gate 	service_t *svc = handle;
11510Sstevel@tonic-gate 	char *file = NULL;
11520Sstevel@tonic-gate 	char *dest;
11530Sstevel@tonic-gate 	REQUEST *r = NULL;
11540Sstevel@tonic-gate 	char lpfile[BUFSIZ];
1155*12091SSonam.Gupta@Sun.COM 	int32_t job_id_actual;
11560Sstevel@tonic-gate 
11570Sstevel@tonic-gate 	if ((svc == NULL) || (printer == NULL) || (job_id < 0) ||
11580Sstevel@tonic-gate 	    (attributes == NULL))
11590Sstevel@tonic-gate 		return (PAPI_BAD_ARGUMENT);
11600Sstevel@tonic-gate 
11610Sstevel@tonic-gate 	if ((*job = j = calloc(1, sizeof (*j))) == NULL)
11620Sstevel@tonic-gate 		return (PAPI_TEMPORARY_ERROR);
11630Sstevel@tonic-gate 
11640Sstevel@tonic-gate 	dest = printer_name_from_uri_id(printer, job_id);
1165*12091SSonam.Gupta@Sun.COM 
1166*12091SSonam.Gupta@Sun.COM 	/*
1167*12091SSonam.Gupta@Sun.COM 	 * job-id might be job-id-requested
1168*12091SSonam.Gupta@Sun.COM 	 * If it is job-id-requested then we need to
1169*12091SSonam.Gupta@Sun.COM 	 * look for corresponding job-id
1170*12091SSonam.Gupta@Sun.COM 	 */
1171*12091SSonam.Gupta@Sun.COM 	job_id_actual = check_job_id(svc, printer, job_id);
1172*12091SSonam.Gupta@Sun.COM 
1173*12091SSonam.Gupta@Sun.COM 	if (job_id_actual < 0) {
1174*12091SSonam.Gupta@Sun.COM 		status = PAPI_NOT_FOUND;
1175*12091SSonam.Gupta@Sun.COM 		detailed_error(svc,
1176*12091SSonam.Gupta@Sun.COM 		    "failed to initiate change for job (%s-%d): %s",
1177*12091SSonam.Gupta@Sun.COM 		    dest, job_id, "no such resource");
1178*12091SSonam.Gupta@Sun.COM 		return (status);
1179*12091SSonam.Gupta@Sun.COM 	}
1180*12091SSonam.Gupta@Sun.COM 
1181*12091SSonam.Gupta@Sun.COM 	status = lpsched_start_change(svc, dest, job_id_actual, &file);
11820Sstevel@tonic-gate 	if (status != PAPI_OK)
11830Sstevel@tonic-gate 		return (status);
11840Sstevel@tonic-gate 
11850Sstevel@tonic-gate 	if ((r = getrequest(file)) != NULL) {
11860Sstevel@tonic-gate 		job_attributes_to_lpsched_request(handle, r,
11878569SJonathan.Ca@Sun.COM 		    (papi_attribute_t **)attributes);
11880Sstevel@tonic-gate #ifdef LP_USE_PAPI_ATTR
11890Sstevel@tonic-gate 		/*
11900Sstevel@tonic-gate 		 * store the job attributes in the PAPI job attribute file
1191*12091SSonam.Gupta@Sun.COM 		 * that was created by the original job request. We need to
11920Sstevel@tonic-gate 		 * modify the attributes in the file as per the new attributes
11930Sstevel@tonic-gate 		 */
11940Sstevel@tonic-gate 		snprintf(lpfile, sizeof (lpfile), "%s%d-%s",
1195*12091SSonam.Gupta@Sun.COM 		    "/var/spool/lp/temp/", job_id_actual, LP_PAPIATTRNAME);
11960Sstevel@tonic-gate 		status = psm_modifyAttrsFile(attributes, lpfile);
11970Sstevel@tonic-gate 		if (status != PAPI_OK) {
11980Sstevel@tonic-gate 			detailed_error(svc,
11998569SJonathan.Ca@Sun.COM 			    "unable to modify the attributes file: %s: %s",
12008569SJonathan.Ca@Sun.COM 			    lpfile, strerror(errno));
12010Sstevel@tonic-gate 			return (PAPI_DEVICE_ERROR);
12020Sstevel@tonic-gate 		}
12030Sstevel@tonic-gate #endif
12040Sstevel@tonic-gate 
12050Sstevel@tonic-gate 		if (putrequest(file, r) < 0) {
12060Sstevel@tonic-gate 			detailed_error(svc,
12078569SJonathan.Ca@Sun.COM 			    gettext("failed to write job: %s: %s"),
12088569SJonathan.Ca@Sun.COM 			    file, strerror(errno));
12090Sstevel@tonic-gate 			freerequest(r);
12100Sstevel@tonic-gate 			return (PAPI_DEVICE_ERROR);
12110Sstevel@tonic-gate 		}
12120Sstevel@tonic-gate 	} else {
12130Sstevel@tonic-gate 		detailed_error(svc, gettext("failed to read job: %s: %s"),
12148569SJonathan.Ca@Sun.COM 		    file, strerror(errno));
12150Sstevel@tonic-gate 		return (PAPI_DEVICE_ERROR);
12160Sstevel@tonic-gate 	}
12170Sstevel@tonic-gate 
1218*12091SSonam.Gupta@Sun.COM 	status = lpsched_end_change(svc, dest, job_id_actual);
12190Sstevel@tonic-gate 	lpsched_request_to_job_attributes(r, j);
12206725Sjacobs 
12216725Sjacobs 	papiAttributeListAddInteger(&j->attributes, PAPI_ATTR_REPLACE,
1222*12091SSonam.Gupta@Sun.COM 	    "job-id", job_id_actual);
12236725Sjacobs 
12240Sstevel@tonic-gate 	freerequest(r);
12250Sstevel@tonic-gate 
12260Sstevel@tonic-gate 	return (status);
12270Sstevel@tonic-gate }
12280Sstevel@tonic-gate 
12290Sstevel@tonic-gate /*
12300Sstevel@tonic-gate  * Extension to PAPI, a variation of this is slated for post-1.0
12310Sstevel@tonic-gate  */
12320Sstevel@tonic-gate #define	DUMMY_FILE	"/var/spool/lp/fifos/FIFO"
12330Sstevel@tonic-gate 
12340Sstevel@tonic-gate papi_status_t
12352264Sjacobs papiJobCreate(papi_service_t handle, char *printer,
12362264Sjacobs 		papi_attribute_t **job_attributes,
12372264Sjacobs 		papi_job_ticket_t *job_ticket, papi_job_t *job)
12380Sstevel@tonic-gate {
12390Sstevel@tonic-gate 	papi_status_t status;
12400Sstevel@tonic-gate 	service_t *svc = handle;
12410Sstevel@tonic-gate 	job_t *j = NULL;
12420Sstevel@tonic-gate 	REQUEST *request;
12430Sstevel@tonic-gate 	char *request_id = NULL;
12440Sstevel@tonic-gate 	char *c;
12450Sstevel@tonic-gate 	char *tmp = NULL;
12460Sstevel@tonic-gate 	char metadata_file[MAXPATHLEN];
12470Sstevel@tonic-gate 
12480Sstevel@tonic-gate 	if ((svc == NULL) || (printer == NULL) || (job == NULL))
12490Sstevel@tonic-gate 		return (PAPI_BAD_ARGUMENT);
12500Sstevel@tonic-gate 
12510Sstevel@tonic-gate 	if (job_ticket != NULL)
12520Sstevel@tonic-gate 		return (PAPI_JOB_TICKET_NOT_SUPPORTED);
12530Sstevel@tonic-gate 
12540Sstevel@tonic-gate 	if ((*job = j = calloc(1, sizeof (*j))) == NULL)
12550Sstevel@tonic-gate 		return (PAPI_TEMPORARY_ERROR);
12560Sstevel@tonic-gate 
12570Sstevel@tonic-gate 	/* 1 for the control file (-0) */
12580Sstevel@tonic-gate 	status = lpsched_alloc_files(svc, 1, &request_id);
12590Sstevel@tonic-gate 	if (status != PAPI_OK)
12600Sstevel@tonic-gate 		return (status);
12610Sstevel@tonic-gate 
12620Sstevel@tonic-gate 	/* convert the attributes to an lpsched REQUEST structure */
12630Sstevel@tonic-gate 	request = create_request(svc, (char *)printer,
12648569SJonathan.Ca@Sun.COM 	    (papi_attribute_t **)job_attributes);
12652264Sjacobs 	if (request == NULL)
12662264Sjacobs 		return (PAPI_TEMPORARY_ERROR);
12670Sstevel@tonic-gate 	addlist(&request->file_list, DUMMY_FILE);	/* add a dummy file */
12680Sstevel@tonic-gate 	request->actions |= ACT_HOLD;			/* hold the job */
12690Sstevel@tonic-gate 
12700Sstevel@tonic-gate #ifdef LP_USE_PAPI_ATTR
12710Sstevel@tonic-gate 	/*
12720Sstevel@tonic-gate 	 * store the job attributes in the PAPI job attribute file that was
12730Sstevel@tonic-gate 	 * created by lpsched_alloc_files(), the attributes will then pass
12740Sstevel@tonic-gate 	 * through lpsched and be given to the slow-filters and the printer's
12750Sstevel@tonic-gate 	 * interface script to process them
12760Sstevel@tonic-gate 	 */
12770Sstevel@tonic-gate 	snprintf(metadata_file, sizeof (metadata_file), "%s%s-%s",
12788569SJonathan.Ca@Sun.COM 	    "/var/spool/lp/temp/", request_id, LP_PAPIATTRNAME);
12790Sstevel@tonic-gate 	status = psm_copy_attrsToFile(job_attributes, metadata_file);
12800Sstevel@tonic-gate 	if (status != PAPI_OK) {
12810Sstevel@tonic-gate 		detailed_error(svc, "unable to copy attributes to file: %s: %s",
12828569SJonathan.Ca@Sun.COM 		    metadata_file, strerror(errno));
12832264Sjacobs 		free(request_id);
12840Sstevel@tonic-gate 		return (PAPI_DEVICE_ERROR);
12850Sstevel@tonic-gate 	}
12860Sstevel@tonic-gate #endif
12870Sstevel@tonic-gate 
12880Sstevel@tonic-gate 	/* store the REQUEST on disk */
12890Sstevel@tonic-gate 	snprintf(metadata_file, sizeof (metadata_file), "%s-0", request_id);
12902264Sjacobs 	free(request_id);
12910Sstevel@tonic-gate 	if (putrequest(metadata_file, request) < 0) {
12920Sstevel@tonic-gate 		detailed_error(svc, gettext("unable to save request: %s: %s"),
12938569SJonathan.Ca@Sun.COM 		    metadata_file, strerror(errno));
12940Sstevel@tonic-gate 		return (PAPI_DEVICE_ERROR);
12950Sstevel@tonic-gate 	}
12960Sstevel@tonic-gate 
12970Sstevel@tonic-gate 	status = lpsched_commit_job(svc, metadata_file, &tmp);
12980Sstevel@tonic-gate 	if (status != PAPI_OK) {
12990Sstevel@tonic-gate 		unlink(metadata_file);
13000Sstevel@tonic-gate 		return (status);
13010Sstevel@tonic-gate 	}
13020Sstevel@tonic-gate 
13030Sstevel@tonic-gate 	lpsched_request_to_job_attributes(request, j);
13040Sstevel@tonic-gate 
13050Sstevel@tonic-gate 	if ((c = strrchr(tmp, '-')) != NULL)
13060Sstevel@tonic-gate 		c++;
13070Sstevel@tonic-gate 	papiAttributeListAddInteger(&j->attributes, PAPI_ATTR_REPLACE,
13088569SJonathan.Ca@Sun.COM 	    "job-id", atoi(c));
13090Sstevel@tonic-gate 	papiAttributeListAddString(&j->attributes, PAPI_ATTR_REPLACE,
13108569SJonathan.Ca@Sun.COM 	    "job-uri", tmp);
13110Sstevel@tonic-gate 
13120Sstevel@tonic-gate 	return (PAPI_OK);
13130Sstevel@tonic-gate }
13140Sstevel@tonic-gate 
13150Sstevel@tonic-gate papi_status_t
13160Sstevel@tonic-gate papiJobCommit(papi_service_t handle, char *printer, int32_t id)
13170Sstevel@tonic-gate {
13180Sstevel@tonic-gate 	papi_status_t status = PAPI_OK;
13190Sstevel@tonic-gate 	service_t *svc = handle;
13200Sstevel@tonic-gate 	REQUEST *r = NULL;
13210Sstevel@tonic-gate 	char *metadata_file;
13220Sstevel@tonic-gate 	char *dest;
13230Sstevel@tonic-gate 
13240Sstevel@tonic-gate 	if ((svc == NULL) || (printer == NULL))
13250Sstevel@tonic-gate 		return (PAPI_BAD_ARGUMENT);
13260Sstevel@tonic-gate 
13270Sstevel@tonic-gate 	dest = printer_name_from_uri_id(printer, id);
13280Sstevel@tonic-gate 	/* tell the scheduler that we want to change the job */
13290Sstevel@tonic-gate 	status = lpsched_start_change(svc, dest, id, &metadata_file);
13300Sstevel@tonic-gate 	if (status != PAPI_OK)
13310Sstevel@tonic-gate 		return (status);
13320Sstevel@tonic-gate 
13330Sstevel@tonic-gate 	if ((r = getrequest(metadata_file)) != NULL) {
13340Sstevel@tonic-gate 		r->actions &= ~ACT_RESUME;
13350Sstevel@tonic-gate 		r->actions |= ACT_RESUME;
13360Sstevel@tonic-gate 		dellist(&r->file_list, DUMMY_FILE);
13370Sstevel@tonic-gate 
13380Sstevel@tonic-gate 		if (putrequest(metadata_file, r) < 0) {
13390Sstevel@tonic-gate 			detailed_error(svc,
13408569SJonathan.Ca@Sun.COM 			    gettext("failed to write job: %s: %s"),
13418569SJonathan.Ca@Sun.COM 			    metadata_file, strerror(errno));
13422264Sjacobs 			freerequest(r);
13430Sstevel@tonic-gate 			return (PAPI_DEVICE_ERROR);
13440Sstevel@tonic-gate 		}
13450Sstevel@tonic-gate 	} else {
13460Sstevel@tonic-gate 		detailed_error(svc, gettext("failed to read job: %s: %s"),
13478569SJonathan.Ca@Sun.COM 		    metadata_file, strerror(errno));
13480Sstevel@tonic-gate 		return (PAPI_DEVICE_ERROR);
13490Sstevel@tonic-gate 	}
13500Sstevel@tonic-gate 
13510Sstevel@tonic-gate 	status = lpsched_end_change(svc, dest, id);
13520Sstevel@tonic-gate 	freerequest(r);
13530Sstevel@tonic-gate 
13540Sstevel@tonic-gate 	return (status);
13550Sstevel@tonic-gate }
13560Sstevel@tonic-gate 
13570Sstevel@tonic-gate papi_status_t
13580Sstevel@tonic-gate papiJobStreamAdd(papi_service_t handle, char *printer, int32_t id,
13590Sstevel@tonic-gate 		papi_stream_t *stream)
13600Sstevel@tonic-gate {
13610Sstevel@tonic-gate 	papi_status_t status;
13620Sstevel@tonic-gate 	service_t *svc = handle;
13630Sstevel@tonic-gate 	job_stream_t *s = NULL;
13640Sstevel@tonic-gate 	char *metadata_file = NULL;
13650Sstevel@tonic-gate 	char *dest;
13660Sstevel@tonic-gate 	char path[MAXPATHLEN];
13670Sstevel@tonic-gate 
13680Sstevel@tonic-gate 	/* allocate space for the stream */
13690Sstevel@tonic-gate 	if ((*stream = s = calloc(1, sizeof (*s))) == NULL)
13700Sstevel@tonic-gate 		return (PAPI_TEMPORARY_ERROR);
13710Sstevel@tonic-gate 
13720Sstevel@tonic-gate 	dest = printer_name_from_uri_id(printer, id);
13730Sstevel@tonic-gate 	/* create/open data file (only root or lp can really do this */
13740Sstevel@tonic-gate 	snprintf(path, sizeof (path), "/var/spool/lp/temp/%d-XXXXXX", id);
13750Sstevel@tonic-gate 	if ((s->fd = mkstemp(path)) < 0) {
13760Sstevel@tonic-gate 		detailed_error(svc, gettext("unable to create sink (%s): %s"),
13778569SJonathan.Ca@Sun.COM 		    path, strerror(errno));
13780Sstevel@tonic-gate 		free(s);
13790Sstevel@tonic-gate 		return (PAPI_NOT_AUTHORIZED);
13800Sstevel@tonic-gate 	}
13810Sstevel@tonic-gate 
13820Sstevel@tonic-gate 	/* add data file to job */
13830Sstevel@tonic-gate 	status = lpsched_start_change(svc, dest, id, &metadata_file);
13840Sstevel@tonic-gate 	if (status != PAPI_OK) {
13850Sstevel@tonic-gate 		close(s->fd);
13860Sstevel@tonic-gate 		free(s);
13870Sstevel@tonic-gate 		unlink(path);
13880Sstevel@tonic-gate 		return (status);
13890Sstevel@tonic-gate 	}
13900Sstevel@tonic-gate 
13910Sstevel@tonic-gate 	if ((s->request = getrequest(metadata_file)) == NULL) {
13920Sstevel@tonic-gate 		detailed_error(svc, gettext("unable to load request: %s: %s"),
13938569SJonathan.Ca@Sun.COM 		    metadata_file, strerror(errno));
13940Sstevel@tonic-gate 		close(s->fd);
13950Sstevel@tonic-gate 		free(s);
13960Sstevel@tonic-gate 		unlink(path);
13970Sstevel@tonic-gate 		return (PAPI_NOT_POSSIBLE);
13980Sstevel@tonic-gate 	}
13990Sstevel@tonic-gate 
14000Sstevel@tonic-gate 	addlist(&(s->request->file_list), path);
14010Sstevel@tonic-gate 
14020Sstevel@tonic-gate 	if (putrequest(metadata_file, s->request) < 0) {
14030Sstevel@tonic-gate 		detailed_error(svc, gettext("unable to save request: %s: %s"),
14048569SJonathan.Ca@Sun.COM 		    metadata_file, strerror(errno));
14050Sstevel@tonic-gate 		close(s->fd);
14060Sstevel@tonic-gate 		free(s);
14070Sstevel@tonic-gate 		unlink(path);
14080Sstevel@tonic-gate 		return (PAPI_NOT_POSSIBLE);
14090Sstevel@tonic-gate 	}
14100Sstevel@tonic-gate 
14110Sstevel@tonic-gate 	status = lpsched_end_change(svc, dest, id);
14120Sstevel@tonic-gate 
14130Sstevel@tonic-gate 	if (status != PAPI_OK)
14140Sstevel@tonic-gate 		return (status);
14150Sstevel@tonic-gate 
14160Sstevel@tonic-gate 	return (PAPI_OK);
14170Sstevel@tonic-gate }
1418