xref: /onnv-gate/usr/src/cmd/lp/lib/papi/job.c (revision 10781)
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 /*
228569SJonathan.Ca@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate /*LINTLIBRARY*/
270Sstevel@tonic-gate 
280Sstevel@tonic-gate #include <stdlib.h>
290Sstevel@tonic-gate #include <string.h>
300Sstevel@tonic-gate #include <unistd.h>
310Sstevel@tonic-gate #include <libintl.h>
320Sstevel@tonic-gate #include <pwd.h>
330Sstevel@tonic-gate #include <sys/stat.h>
340Sstevel@tonic-gate #include <papi_impl.h>
350Sstevel@tonic-gate 
362264Sjacobs /*
372264Sjacobs  * for an older application that may have been linked with a pre-v1.0
382264Sjacobs  * PAPI implementation.
392264Sjacobs  */
402264Sjacobs papi_status_t
412264Sjacobs papiAttributeListAdd(papi_attribute_t ***attrs, int flags, char *name,
422264Sjacobs 		papi_attribute_value_type_t type, papi_attribute_value_t *value)
432264Sjacobs {
442264Sjacobs 	return (papiAttributeListAddValue(attrs, flags, name, type, value));
452264Sjacobs }
462264Sjacobs 
470Sstevel@tonic-gate #ifdef LP_USE_PAPI_ATTR
482264Sjacobs static papi_status_t psm_modifyAttrsFile(papi_attribute_t **attrs, char *file);
492264Sjacobs static papi_status_t psm_modifyAttrsList(char *file, papi_attribute_t **attrs,
502264Sjacobs 					papi_attribute_t ***newAttrs);
510Sstevel@tonic-gate #endif
520Sstevel@tonic-gate 
530Sstevel@tonic-gate 
540Sstevel@tonic-gate void
550Sstevel@tonic-gate papiJobFree(papi_job_t job)
560Sstevel@tonic-gate {
570Sstevel@tonic-gate 	job_t *tmp = (job_t *)job;
580Sstevel@tonic-gate 
590Sstevel@tonic-gate 	if (tmp != NULL) {
600Sstevel@tonic-gate 		papiAttributeListFree(tmp->attributes);
610Sstevel@tonic-gate 		free(tmp);
620Sstevel@tonic-gate 	}
630Sstevel@tonic-gate }
640Sstevel@tonic-gate 
650Sstevel@tonic-gate void
660Sstevel@tonic-gate papiJobListFree(papi_job_t *jobs)
670Sstevel@tonic-gate {
680Sstevel@tonic-gate 	if (jobs != NULL) {
690Sstevel@tonic-gate 		int i;
700Sstevel@tonic-gate 
710Sstevel@tonic-gate 		for (i = 0; jobs[i] != NULL; i++) {
720Sstevel@tonic-gate 			papiJobFree(jobs[i]);
730Sstevel@tonic-gate 		}
740Sstevel@tonic-gate 		free(jobs);
750Sstevel@tonic-gate 	}
760Sstevel@tonic-gate }
770Sstevel@tonic-gate 
780Sstevel@tonic-gate papi_attribute_t **
790Sstevel@tonic-gate papiJobGetAttributeList(papi_job_t job)
800Sstevel@tonic-gate {
810Sstevel@tonic-gate 	job_t *tmp = (job_t *)job;
820Sstevel@tonic-gate 
830Sstevel@tonic-gate 	if (tmp != NULL)
840Sstevel@tonic-gate 		return (tmp->attributes);
850Sstevel@tonic-gate 
860Sstevel@tonic-gate 	return (NULL);
870Sstevel@tonic-gate }
880Sstevel@tonic-gate 
890Sstevel@tonic-gate char *
900Sstevel@tonic-gate papiJobGetPrinterName(papi_job_t job)
910Sstevel@tonic-gate {
920Sstevel@tonic-gate 	job_t *tmp = (job_t *)job;
930Sstevel@tonic-gate 	char *result = NULL;
940Sstevel@tonic-gate 
950Sstevel@tonic-gate 	if (tmp != NULL)
960Sstevel@tonic-gate 		papiAttributeListGetString(tmp->attributes, NULL,
978569SJonathan.Ca@Sun.COM 		    "printer-name", &result);
980Sstevel@tonic-gate 
990Sstevel@tonic-gate 	return (result);
1000Sstevel@tonic-gate }
1010Sstevel@tonic-gate 
1020Sstevel@tonic-gate int32_t
1030Sstevel@tonic-gate papiJobGetId(papi_job_t job)
1040Sstevel@tonic-gate {
1050Sstevel@tonic-gate 	job_t *tmp = (job_t *)job;
1060Sstevel@tonic-gate 	int result = -1;
1070Sstevel@tonic-gate 
1080Sstevel@tonic-gate 	if (tmp != NULL)
1090Sstevel@tonic-gate 		papiAttributeListGetInteger(tmp->attributes, NULL, "job-id",
1108569SJonathan.Ca@Sun.COM 		    &result);
1110Sstevel@tonic-gate 
1120Sstevel@tonic-gate 	return (result);
1130Sstevel@tonic-gate }
1140Sstevel@tonic-gate 
1150Sstevel@tonic-gate static REQUEST *
1160Sstevel@tonic-gate create_request(papi_service_t svc, char *printer, papi_attribute_t **attributes)
1170Sstevel@tonic-gate {
1187253Sjacobs 	REQUEST *r;
1190Sstevel@tonic-gate 
1207253Sjacobs 	if ((r = calloc(1, sizeof (*r))) != NULL) {
1219152SSonam.Gupta@Sun.COM 		char *hostname = NULL;
1229152SSonam.Gupta@Sun.COM 
1237253Sjacobs 		r->priority = -1;
1247253Sjacobs 		r->destination = printer_name_from_uri_id(printer, -1);
1259152SSonam.Gupta@Sun.COM 
1269152SSonam.Gupta@Sun.COM 		papiAttributeListGetString(attributes, NULL,
1279152SSonam.Gupta@Sun.COM 		    "job-originating-host-name", &hostname);
1289152SSonam.Gupta@Sun.COM 
1299152SSonam.Gupta@Sun.COM 		if (hostname == NULL) {
1309152SSonam.Gupta@Sun.COM 			char host[BUFSIZ];
1319152SSonam.Gupta@Sun.COM 
1329152SSonam.Gupta@Sun.COM 			if (gethostname(host, sizeof (host)) == 0)
1339152SSonam.Gupta@Sun.COM 				papiAttributeListAddString(&attributes,
1349152SSonam.Gupta@Sun.COM 				    PAPI_ATTR_REPLACE,
1359152SSonam.Gupta@Sun.COM 				    "job-originating-host-name",
1369152SSonam.Gupta@Sun.COM 				    host);
1379152SSonam.Gupta@Sun.COM 		}
1389152SSonam.Gupta@Sun.COM 
1397253Sjacobs 		job_attributes_to_lpsched_request(svc, r, attributes);
1407253Sjacobs 	}
1410Sstevel@tonic-gate 
1427253Sjacobs 	return (r);
1430Sstevel@tonic-gate }
1440Sstevel@tonic-gate 
1450Sstevel@tonic-gate static papi_status_t
1460Sstevel@tonic-gate authorized(service_t *svc, int32_t id)
1470Sstevel@tonic-gate {
1480Sstevel@tonic-gate 	papi_status_t result = PAPI_NOT_AUTHORIZED;	/* assume the worst */
1490Sstevel@tonic-gate 	char file[32];
1500Sstevel@tonic-gate 	REQUEST *r;
1510Sstevel@tonic-gate 
1520Sstevel@tonic-gate 	snprintf(file, sizeof (file), "%d-0", id);
1530Sstevel@tonic-gate 	if ((r = getrequest(file)) != NULL) {
1540Sstevel@tonic-gate 		uid_t uid = getuid();
1550Sstevel@tonic-gate 		struct passwd *pw = NULL;
1560Sstevel@tonic-gate 		char *user = "intruder";	/* assume an intruder */
1570Sstevel@tonic-gate 
1580Sstevel@tonic-gate 		if ((pw = getpwuid(uid)) != NULL)
1590Sstevel@tonic-gate 			user = pw->pw_name;	/* use the process owner */
1600Sstevel@tonic-gate 
1610Sstevel@tonic-gate 		if ((uid == 0) || (uid == 71)) { /* root/lp can forge this */
1620Sstevel@tonic-gate 			papi_status_t s;
1630Sstevel@tonic-gate 			s = papiAttributeListGetString(svc->attributes, NULL,
1648569SJonathan.Ca@Sun.COM 			    "user-name", &user);
1650Sstevel@tonic-gate 			if (s != PAPI_OK)	/* true root/lp are almighty */
1660Sstevel@tonic-gate 				result = PAPI_OK;
1670Sstevel@tonic-gate 		}
1680Sstevel@tonic-gate 
1698966SSonam.Gupta@Sun.COM 		if (result != PAPI_OK) {
1708966SSonam.Gupta@Sun.COM 			if (strcmp(user, r->user) == 0)
1718966SSonam.Gupta@Sun.COM 				result = PAPI_OK;
1728966SSonam.Gupta@Sun.COM 			else {
1738966SSonam.Gupta@Sun.COM 				/*
1749742SSonam.Gupta@Sun.COM 				 * user and r->user might contain the
1758966SSonam.Gupta@Sun.COM 				 * host info also
1768966SSonam.Gupta@Sun.COM 				 */
1779742SSonam.Gupta@Sun.COM 				char *token1 = strtok(r->user, "@");
1789742SSonam.Gupta@Sun.COM 				char *token2 = strtok(NULL, "@");
1799742SSonam.Gupta@Sun.COM 				char *token3 = strtok(user, "@");
1809742SSonam.Gupta@Sun.COM 				char *token4 = strtok(NULL, "@");
1818966SSonam.Gupta@Sun.COM 
1829742SSonam.Gupta@Sun.COM 				/*
1839742SSonam.Gupta@Sun.COM 				 * token1 and token3 contain usernames
1849742SSonam.Gupta@Sun.COM 				 * token2 and token4 contain hostnames
1859742SSonam.Gupta@Sun.COM 				 */
1869742SSonam.Gupta@Sun.COM 				if ((token1 == NULL) || (token3 == NULL))
1879742SSonam.Gupta@Sun.COM 					result = PAPI_NOT_AUTHORIZED;
1889742SSonam.Gupta@Sun.COM 				else if ((token4 != NULL) &&
1899742SSonam.Gupta@Sun.COM 				    (strcmp(token4, "localhost") == 0) &&
1909742SSonam.Gupta@Sun.COM 				    (strcmp(token3, "root") == 0) ||
1919742SSonam.Gupta@Sun.COM 				    (strcmp(token3, "lp") == 0)) {
1929742SSonam.Gupta@Sun.COM 					/*
1939742SSonam.Gupta@Sun.COM 					 * root/lp user on server can
1949742SSonam.Gupta@Sun.COM 					 * cancel any requset
1959742SSonam.Gupta@Sun.COM 					 */
1969742SSonam.Gupta@Sun.COM 					result = PAPI_OK;
1979742SSonam.Gupta@Sun.COM 				} else if (strcmp(token1, token3) == 0) {
1989742SSonam.Gupta@Sun.COM 					/*
1999742SSonam.Gupta@Sun.COM 					 * usernames are same
2009742SSonam.Gupta@Sun.COM 					 * compare the hostnames
2019742SSonam.Gupta@Sun.COM 					 */
2029742SSonam.Gupta@Sun.COM 					if ((token4 != NULL) &&
2039742SSonam.Gupta@Sun.COM 					    (token2 != NULL) &&
2049742SSonam.Gupta@Sun.COM 					    (strcmp(token4, "localhost") ==
2059742SSonam.Gupta@Sun.COM 					    0)) {
2069742SSonam.Gupta@Sun.COM 						/*
2079742SSonam.Gupta@Sun.COM 						 * Its server machine
2089742SSonam.Gupta@Sun.COM 						 */
2099742SSonam.Gupta@Sun.COM 						static char host[256];
2109742SSonam.Gupta@Sun.COM 						if (gethostname(host,
2119742SSonam.Gupta@Sun.COM 						    sizeof (host)) == 0) {
2129742SSonam.Gupta@Sun.COM 							if ((host != NULL) &&
2139742SSonam.Gupta@Sun.COM 							    (strcmp(host,
2149742SSonam.Gupta@Sun.COM 							    token2) == 0))
2159742SSonam.Gupta@Sun.COM 								result =
2169742SSonam.Gupta@Sun.COM 								    PAPI_OK;
2179742SSonam.Gupta@Sun.COM 						}
2189742SSonam.Gupta@Sun.COM 
2199742SSonam.Gupta@Sun.COM 					} else if ((token4 != NULL) &&
2209742SSonam.Gupta@Sun.COM 					    (token2 != NULL) &&
2219742SSonam.Gupta@Sun.COM 					    (strcmp(token4, token2) == 0)) {
2228966SSonam.Gupta@Sun.COM 						result = PAPI_OK;
2239742SSonam.Gupta@Sun.COM 					} else if ((token4 == NULL) &&
2249742SSonam.Gupta@Sun.COM 					    (token2 != NULL)) {
2259742SSonam.Gupta@Sun.COM 						/*
2269742SSonam.Gupta@Sun.COM 						 * When the request is sent from
2279742SSonam.Gupta@Sun.COM 						 * client to server using ipp
2289742SSonam.Gupta@Sun.COM 						 * token4 is NULL
2299742SSonam.Gupta@Sun.COM 						 */
2309742SSonam.Gupta@Sun.COM 						result = PAPI_OK;
2319742SSonam.Gupta@Sun.COM 					}
2328966SSonam.Gupta@Sun.COM 				}
2338966SSonam.Gupta@Sun.COM 			}
2348966SSonam.Gupta@Sun.COM 		}
2350Sstevel@tonic-gate 
2360Sstevel@tonic-gate 		freerequest(r);
2370Sstevel@tonic-gate 	} else
2380Sstevel@tonic-gate 		result = PAPI_NOT_FOUND;
2390Sstevel@tonic-gate 
2400Sstevel@tonic-gate 	return (result);
2410Sstevel@tonic-gate }
2420Sstevel@tonic-gate 
2430Sstevel@tonic-gate static papi_status_t
2442264Sjacobs copy_file(char *from, char *to)
2450Sstevel@tonic-gate {
2460Sstevel@tonic-gate 	int ifd, ofd;
2470Sstevel@tonic-gate 	char buf[BUFSIZ];
2480Sstevel@tonic-gate 	int rc;
2490Sstevel@tonic-gate 
2500Sstevel@tonic-gate 	if ((ifd = open(from, O_RDONLY)) < 0)
2510Sstevel@tonic-gate 		return (PAPI_DOCUMENT_ACCESS_ERROR);
2520Sstevel@tonic-gate 
2530Sstevel@tonic-gate 	if ((ofd = open(to, O_WRONLY)) < 0) {
2540Sstevel@tonic-gate 		close(ifd);
2550Sstevel@tonic-gate 		return (PAPI_NOT_POSSIBLE);
2560Sstevel@tonic-gate 	}
2570Sstevel@tonic-gate 
2580Sstevel@tonic-gate 	while ((rc = read(ifd, buf, sizeof (buf))) > 0)
2590Sstevel@tonic-gate 		write(ofd, buf, rc);
2600Sstevel@tonic-gate 
2610Sstevel@tonic-gate 	close(ifd);
2620Sstevel@tonic-gate 	close(ofd);
2630Sstevel@tonic-gate 
2640Sstevel@tonic-gate 	return (PAPI_OK);
2650Sstevel@tonic-gate }
2660Sstevel@tonic-gate 
2670Sstevel@tonic-gate 
2680Sstevel@tonic-gate #ifdef LP_USE_PAPI_ATTR
2690Sstevel@tonic-gate /*
2700Sstevel@tonic-gate  * *****************************************************************************
2710Sstevel@tonic-gate  *
2720Sstevel@tonic-gate  * Description: Create a file containing all the attributes in the attribute
2730Sstevel@tonic-gate  *              list passed to this function.
2740Sstevel@tonic-gate  *              This file is then passed through lpsched and given to either
2750Sstevel@tonic-gate  *              a slow-filter or to the printer's interface script to process
2760Sstevel@tonic-gate  *              the attributes.
2770Sstevel@tonic-gate  *
2780Sstevel@tonic-gate  * Parameters:  attrs - list of attributes and their values
2790Sstevel@tonic-gate  *              file  - file pathname to create and put the attributes into.
2800Sstevel@tonic-gate  *
2810Sstevel@tonic-gate  * *****************************************************************************
2820Sstevel@tonic-gate  */
2830Sstevel@tonic-gate 
2840Sstevel@tonic-gate static papi_status_t
2852264Sjacobs psm_copy_attrsToFile(papi_attribute_t **attrs, char *file)
2860Sstevel@tonic-gate 
2870Sstevel@tonic-gate {
2880Sstevel@tonic-gate 	papi_status_t result = PAPI_OK;
2892264Sjacobs 
2902264Sjacobs 	if ((attrs != NULL) && (*attrs != NULL)) {
2912264Sjacobs 		FILE *out = NULL;
2920Sstevel@tonic-gate 
2932264Sjacobs 		if ((out = fopen(file, "w")) != NULL) {
2942264Sjacobs 			papiAttributeListPrint(out, attrs, "");
2950Sstevel@tonic-gate 			fclose(out);
2962264Sjacobs 		} else {
2970Sstevel@tonic-gate 			result = PAPI_NOT_POSSIBLE;
2980Sstevel@tonic-gate 		}
2990Sstevel@tonic-gate 	}
3000Sstevel@tonic-gate 
3010Sstevel@tonic-gate 	return (result);
3020Sstevel@tonic-gate } /* psm_copy_attrsToFile */
3030Sstevel@tonic-gate 
3040Sstevel@tonic-gate 
3050Sstevel@tonic-gate /*
3060Sstevel@tonic-gate  * *****************************************************************************
3070Sstevel@tonic-gate  *
3080Sstevel@tonic-gate  * Description: Modify the given attribute 'file' with the attributes from the
3090Sstevel@tonic-gate  *              'attrs' list. Attributes already in the file will be replaced
3100Sstevel@tonic-gate  *              with the new value. New attributes will be added into the file.
3110Sstevel@tonic-gate  *
3120Sstevel@tonic-gate  * Parameters:  attrs - list of attributes and their values
3130Sstevel@tonic-gate  *              file  - file pathname to create and put the attributes into.
3140Sstevel@tonic-gate  *
3150Sstevel@tonic-gate  * *****************************************************************************
3160Sstevel@tonic-gate  */
3170Sstevel@tonic-gate 
3180Sstevel@tonic-gate static papi_status_t
3192264Sjacobs psm_modifyAttrsFile(papi_attribute_t **attrs, char *file)
3200Sstevel@tonic-gate 
3210Sstevel@tonic-gate {
3220Sstevel@tonic-gate 	papi_status_t result = PAPI_OK;
3230Sstevel@tonic-gate 	papi_attribute_t **newAttrs = NULL;
3240Sstevel@tonic-gate 	struct stat   tmpBuf;
3250Sstevel@tonic-gate 	FILE *fd = NULL;
3260Sstevel@tonic-gate 
3272264Sjacobs 	if ((attrs != NULL) && (*attrs != NULL) && (file != NULL)) {
3280Sstevel@tonic-gate 
3290Sstevel@tonic-gate 		/*
3300Sstevel@tonic-gate 		 * check file exist before try to modify it, if it doesn't
3310Sstevel@tonic-gate 		 * exist assume there is an error
3320Sstevel@tonic-gate 		 */
3332264Sjacobs 		if (stat(file, &tmpBuf) == 0) {
3340Sstevel@tonic-gate 			/*
3350Sstevel@tonic-gate 			 * if file is currently empty just write the given
3360Sstevel@tonic-gate 			 * attributes to the file otherwise exact the attributes
3370Sstevel@tonic-gate 			 * from the file and modify them accordingly before
3380Sstevel@tonic-gate 			 * writing them back to the file
3390Sstevel@tonic-gate 			 */
3402264Sjacobs 			if (tmpBuf.st_size == 0) {
3410Sstevel@tonic-gate 				newAttrs = (papi_attribute_t **)attrs;
3420Sstevel@tonic-gate 
3430Sstevel@tonic-gate 				fd = fopen(file, "w");
3442264Sjacobs 				if (fd != NULL) {
3450Sstevel@tonic-gate 					papiAttributeListPrint(fd,
3462264Sjacobs 							newAttrs, "");
3470Sstevel@tonic-gate 					fclose(fd);
3482264Sjacobs 				} else {
3490Sstevel@tonic-gate 					result = PAPI_NOT_POSSIBLE;
3500Sstevel@tonic-gate 				}
3512264Sjacobs 			} else {
3520Sstevel@tonic-gate 				result =
3530Sstevel@tonic-gate 				    psm_modifyAttrsList(file, attrs, &newAttrs);
3540Sstevel@tonic-gate 
3550Sstevel@tonic-gate 				fd = fopen(file, "w");
3562264Sjacobs 				if (fd != NULL) {
3570Sstevel@tonic-gate 					papiAttributeListPrint(fd,
3582264Sjacobs 								newAttrs, "");
3590Sstevel@tonic-gate 					fclose(fd);
3602264Sjacobs 				} else {
3610Sstevel@tonic-gate 					result = PAPI_NOT_POSSIBLE;
3620Sstevel@tonic-gate 				}
3630Sstevel@tonic-gate 
3640Sstevel@tonic-gate 				papiAttributeListFree(newAttrs);
3650Sstevel@tonic-gate 			}
3662264Sjacobs 		} else {
3670Sstevel@tonic-gate 			result = PAPI_NOT_POSSIBLE;
3680Sstevel@tonic-gate 		}
3690Sstevel@tonic-gate 	}
3700Sstevel@tonic-gate 
3710Sstevel@tonic-gate 	return (result);
3720Sstevel@tonic-gate } /* psm_modifyAttrsFile */
3730Sstevel@tonic-gate 
3740Sstevel@tonic-gate 
3750Sstevel@tonic-gate /*
3760Sstevel@tonic-gate  * *****************************************************************************
3770Sstevel@tonic-gate  *
3780Sstevel@tonic-gate  * Description: Extracts the attributes in the given attribute 'file' and
3790Sstevel@tonic-gate  *              creates a new list 'newAttrs' containing the modified list of
3800Sstevel@tonic-gate  *              attributes.
3810Sstevel@tonic-gate  *
3820Sstevel@tonic-gate  * Parameters:  file  - pathname of file containing attributes to be modified
3830Sstevel@tonic-gate  *              attrs - list of attributes and their values to modify
3840Sstevel@tonic-gate  *              newAttrs - returns the modified list of attributes
3850Sstevel@tonic-gate  *
3860Sstevel@tonic-gate  * *****************************************************************************
3870Sstevel@tonic-gate  */
3880Sstevel@tonic-gate 
3890Sstevel@tonic-gate static papi_status_t
3902264Sjacobs psm_modifyAttrsList(char *file, papi_attribute_t **attrs,
3910Sstevel@tonic-gate     papi_attribute_t ***newAttrs)
3920Sstevel@tonic-gate 
3930Sstevel@tonic-gate {
3940Sstevel@tonic-gate 	papi_status_t result = PAPI_OK;
3950Sstevel@tonic-gate 	papi_attribute_t  *nextAttr = NULL;
3960Sstevel@tonic-gate 	papi_attribute_value_t  **values = NULL;
3970Sstevel@tonic-gate 	void *iter = NULL;
3980Sstevel@tonic-gate 	FILE *fd = NULL;
3990Sstevel@tonic-gate 	register int fD = 0;
4000Sstevel@tonic-gate 	char aBuff[200];
4010Sstevel@tonic-gate 	char *a = NULL;
4020Sstevel@tonic-gate 	char *p = NULL;
4030Sstevel@tonic-gate 	int count = 0;
4040Sstevel@tonic-gate 	int n = 0;
4050Sstevel@tonic-gate 
4060Sstevel@tonic-gate 	fd = fopen(file, "r");
4072264Sjacobs 	if (fd != NULL) {
4080Sstevel@tonic-gate 		fD = fileno(fd);
4090Sstevel@tonic-gate 		a = &aBuff[0];
4100Sstevel@tonic-gate 		p = &aBuff[0];
4110Sstevel@tonic-gate 		count = read(fD, &aBuff[0], sizeof (aBuff) - 1);
4122264Sjacobs 		while ((result == PAPI_OK) && (count > 0)) {
4130Sstevel@tonic-gate 			aBuff[count+n] = '\0';
4142264Sjacobs 			if (count == sizeof (aBuff) - n - 1) {
4150Sstevel@tonic-gate 				p = strrchr(aBuff, '\n');
4162264Sjacobs 				if (p != NULL) {
4170Sstevel@tonic-gate 					/* terminate at last complete line */
4180Sstevel@tonic-gate 					*p = '\0';
4190Sstevel@tonic-gate 				}
4200Sstevel@tonic-gate 			}
4210Sstevel@tonic-gate 			result = papiAttributeListFromString(
4220Sstevel@tonic-gate 				newAttrs, PAPI_ATTR_EXCL, aBuff);
4230Sstevel@tonic-gate 
4242264Sjacobs 			if (result == PAPI_OK) {
4250Sstevel@tonic-gate 				/*
4260Sstevel@tonic-gate 				 * handle any part lines and then read the next
4270Sstevel@tonic-gate 				 * buffer from the file
4280Sstevel@tonic-gate 				 */
4290Sstevel@tonic-gate 				n = 0;
4302264Sjacobs 				if (p != a) {
4310Sstevel@tonic-gate 					p++; /* skip NL */
4320Sstevel@tonic-gate 					n = sizeof (aBuff) - 1 - (p - a);
4330Sstevel@tonic-gate 					strncpy(aBuff, p, n);
4340Sstevel@tonic-gate 				}
4350Sstevel@tonic-gate 				count = read(fD, &aBuff[n],
4360Sstevel@tonic-gate 					sizeof (aBuff) - n - 1);
4370Sstevel@tonic-gate 				p = &aBuff[0];
4380Sstevel@tonic-gate 			}
4390Sstevel@tonic-gate 		}
4400Sstevel@tonic-gate 		fclose(fd);
4410Sstevel@tonic-gate 	}
4420Sstevel@tonic-gate 
4430Sstevel@tonic-gate 	/* now modify the attribute list with the new attributes in 'attrs' */
4440Sstevel@tonic-gate 
4450Sstevel@tonic-gate 	nextAttr = papiAttributeListGetNext((papi_attribute_t **)attrs, &iter);
4462264Sjacobs 	while ((result == PAPI_OK) && (nextAttr != NULL)) {
4470Sstevel@tonic-gate 		values = nextAttr->values;
4480Sstevel@tonic-gate 
4492264Sjacobs 		if ((values != NULL) && (*values != NULL)) {
4502264Sjacobs 			result = papiAttributeListAddValue(newAttrs,
4510Sstevel@tonic-gate 						    PAPI_ATTR_REPLACE,
4520Sstevel@tonic-gate 						    nextAttr->name,
4530Sstevel@tonic-gate 						    nextAttr->type, *values);
4540Sstevel@tonic-gate 			values++;
4550Sstevel@tonic-gate 		}
4560Sstevel@tonic-gate 
4570Sstevel@tonic-gate 		while ((result == PAPI_OK) &&
4582264Sjacobs 			(values != NULL) && (*values != NULL)) {
4592264Sjacobs 			result = papiAttributeListAddValue(newAttrs,
4600Sstevel@tonic-gate 						    PAPI_ATTR_APPEND,
4610Sstevel@tonic-gate 						    nextAttr->name,
4620Sstevel@tonic-gate 						    nextAttr->type, *values);
4630Sstevel@tonic-gate 			values++;
4640Sstevel@tonic-gate 		}
4650Sstevel@tonic-gate 		nextAttr =
4660Sstevel@tonic-gate 		    papiAttributeListGetNext((papi_attribute_t **)attrs, &iter);
4670Sstevel@tonic-gate 	}
4680Sstevel@tonic-gate 
4690Sstevel@tonic-gate 	return (result);
4700Sstevel@tonic-gate } /* papi_modifyAttrsList() */
4710Sstevel@tonic-gate #endif
4720Sstevel@tonic-gate 
4730Sstevel@tonic-gate 
4740Sstevel@tonic-gate papi_status_t
4752264Sjacobs papiJobSubmit(papi_service_t handle, char *printer,
4762264Sjacobs 		papi_attribute_t **job_attributes,
4772264Sjacobs 		papi_job_ticket_t *job_ticket,
4782264Sjacobs 		char **files, papi_job_t *job)
4790Sstevel@tonic-gate {
4800Sstevel@tonic-gate 	papi_status_t status;
4810Sstevel@tonic-gate 	service_t *svc = handle;
4828569SJonathan.Ca@Sun.COM 	struct stat statbuf;
4830Sstevel@tonic-gate 	job_t *j;
4840Sstevel@tonic-gate 	int file_no;
4850Sstevel@tonic-gate 	char *request_id = NULL;
4860Sstevel@tonic-gate 	REQUEST *request;
4870Sstevel@tonic-gate 	int i;
4880Sstevel@tonic-gate 	char *c;
4890Sstevel@tonic-gate 	char *tmp = NULL;
4900Sstevel@tonic-gate 	char lpfile[BUFSIZ];
4910Sstevel@tonic-gate 
4920Sstevel@tonic-gate 	if ((svc == NULL) || (printer == NULL) || (files == NULL) ||
4930Sstevel@tonic-gate 	    (job == NULL))
4940Sstevel@tonic-gate 		return (PAPI_BAD_ARGUMENT);
4950Sstevel@tonic-gate 
4960Sstevel@tonic-gate 	if (job_ticket != NULL)
4970Sstevel@tonic-gate 		return (PAPI_OPERATION_NOT_SUPPORTED);
4980Sstevel@tonic-gate 
4990Sstevel@tonic-gate 	if (files != NULL)
5008569SJonathan.Ca@Sun.COM 		for (file_no = 0; files[file_no] != NULL; file_no++) {
5010Sstevel@tonic-gate 			if (access(files[file_no], R_OK) < 0) {
5020Sstevel@tonic-gate 				detailed_error(svc,
5038569SJonathan.Ca@Sun.COM 				    gettext("Cannot access file: %s: %s"),
5048569SJonathan.Ca@Sun.COM 				    files[file_no], strerror(errno));
5050Sstevel@tonic-gate 				return (PAPI_BAD_ARGUMENT);
5060Sstevel@tonic-gate 			}
5079224SJonathan.Ca@Sun.COM 			if (stat(files[file_no], &statbuf) < 0) {
5089224SJonathan.Ca@Sun.COM 				detailed_error(svc,
5099224SJonathan.Ca@Sun.COM 				    gettext("Cannot access file: %s: %s"),
5109224SJonathan.Ca@Sun.COM 				    files[file_no], strerror(errno));
5119224SJonathan.Ca@Sun.COM 				return (PAPI_DOCUMENT_ACCESS_ERROR);
5129224SJonathan.Ca@Sun.COM 			}
5138569SJonathan.Ca@Sun.COM 			if (statbuf.st_size == 0) {
5148569SJonathan.Ca@Sun.COM 				detailed_error(svc,
5158569SJonathan.Ca@Sun.COM 				    gettext("Zero byte (empty) file: %s"),
5168569SJonathan.Ca@Sun.COM 				    files[file_no]);
5178569SJonathan.Ca@Sun.COM 				return (PAPI_BAD_ARGUMENT);
5188569SJonathan.Ca@Sun.COM 			}
5198569SJonathan.Ca@Sun.COM 		}
5200Sstevel@tonic-gate 
5210Sstevel@tonic-gate 	if ((*job = j = calloc(1, sizeof (*j))) == NULL)
5220Sstevel@tonic-gate 		return (PAPI_TEMPORARY_ERROR);
5230Sstevel@tonic-gate 
5240Sstevel@tonic-gate 	/* file_no + 1 for the control file (-0) */
5250Sstevel@tonic-gate 	status = lpsched_alloc_files(svc, file_no + 1, &request_id);
5260Sstevel@tonic-gate 	if (status != PAPI_OK)
5270Sstevel@tonic-gate 		return (status);
5280Sstevel@tonic-gate 
5290Sstevel@tonic-gate 	request = create_request(svc, (char *)printer,
5308569SJonathan.Ca@Sun.COM 	    (papi_attribute_t **)job_attributes);
5310Sstevel@tonic-gate 
5320Sstevel@tonic-gate 	for (i = 0; files[i] != NULL; i++) {
5330Sstevel@tonic-gate 		papi_status_t status;
5340Sstevel@tonic-gate 		snprintf(lpfile, sizeof (lpfile), "%s%s-%d",
5358569SJonathan.Ca@Sun.COM 		    "/var/spool/lp/temp/", request_id, i+1);
5360Sstevel@tonic-gate 		status = copy_file(files[i], lpfile);
5370Sstevel@tonic-gate 		if (status != PAPI_OK) {
5380Sstevel@tonic-gate 			detailed_error(svc,
5398569SJonathan.Ca@Sun.COM 			    gettext("unable to copy: %s -> %s: %s"),
5408569SJonathan.Ca@Sun.COM 			    files[i], lpfile, strerror(errno));
5410Sstevel@tonic-gate 				freerequest(request);
5420Sstevel@tonic-gate 			return (PAPI_DEVICE_ERROR);
5430Sstevel@tonic-gate 		}
5440Sstevel@tonic-gate 		addlist(&(request->file_list), lpfile);
5450Sstevel@tonic-gate 	}
5460Sstevel@tonic-gate 
5470Sstevel@tonic-gate #ifdef LP_USE_PAPI_ATTR
5480Sstevel@tonic-gate 	/*
5490Sstevel@tonic-gate 	 * store the job attributes in the PAPI job attribute file that was
5500Sstevel@tonic-gate 	 * created by lpsched_alloc_files(), the attributes will then pass
5510Sstevel@tonic-gate 	 * through lpsched and be given to the slow-filters and the printer's
5520Sstevel@tonic-gate 	 * interface script to process them
5530Sstevel@tonic-gate 	 */
5540Sstevel@tonic-gate 	snprintf(lpfile, sizeof (lpfile), "%s%s-%s",
5558569SJonathan.Ca@Sun.COM 	    "/var/spool/lp/temp/", request_id, LP_PAPIATTRNAME);
5560Sstevel@tonic-gate 	status = psm_copy_attrsToFile(job_attributes, lpfile);
5570Sstevel@tonic-gate 	if (status != PAPI_OK) {
5580Sstevel@tonic-gate 		detailed_error(svc, "unable to copy attributes to file: %s: %s",
5598569SJonathan.Ca@Sun.COM 		    lpfile, strerror(errno));
5600Sstevel@tonic-gate 		return (PAPI_DEVICE_ERROR);
5610Sstevel@tonic-gate 	}
5620Sstevel@tonic-gate #endif
5630Sstevel@tonic-gate 
5640Sstevel@tonic-gate 	/* store the meta-data file */
5650Sstevel@tonic-gate 	snprintf(lpfile, sizeof (lpfile), "%s-0", request_id);
5660Sstevel@tonic-gate 	if (putrequest(lpfile, request) < 0) {
5670Sstevel@tonic-gate 		detailed_error(svc, gettext("unable to save request: %s: %s"),
5688569SJonathan.Ca@Sun.COM 		    lpfile, strerror(errno));
5690Sstevel@tonic-gate 		freerequest(request);
5700Sstevel@tonic-gate 		return (PAPI_DEVICE_ERROR);
5710Sstevel@tonic-gate 	}
5720Sstevel@tonic-gate 
5730Sstevel@tonic-gate 	status = lpsched_commit_job(svc, lpfile, &tmp);
5740Sstevel@tonic-gate 	if (status != PAPI_OK) {
5750Sstevel@tonic-gate 		unlink(lpfile);
5760Sstevel@tonic-gate 		freerequest(request);
5770Sstevel@tonic-gate 		return (status);
5780Sstevel@tonic-gate 	}
5790Sstevel@tonic-gate 
5800Sstevel@tonic-gate 	lpsched_request_to_job_attributes(request, j);
5810Sstevel@tonic-gate 	freerequest(request);
5820Sstevel@tonic-gate 
5830Sstevel@tonic-gate 	if ((c = strrchr(tmp, '-')) != NULL)
5840Sstevel@tonic-gate 		c++;
5850Sstevel@tonic-gate 	papiAttributeListAddInteger(&j->attributes, PAPI_ATTR_REPLACE,
5868569SJonathan.Ca@Sun.COM 	    "job-id", atoi(c));
5870Sstevel@tonic-gate 	papiAttributeListAddString(&j->attributes, PAPI_ATTR_REPLACE,
5888569SJonathan.Ca@Sun.COM 	    "job-uri", tmp);
5890Sstevel@tonic-gate 
5900Sstevel@tonic-gate 	return (PAPI_OK);
5910Sstevel@tonic-gate }
5920Sstevel@tonic-gate 
5930Sstevel@tonic-gate papi_status_t
5942264Sjacobs papiJobSubmitByReference(papi_service_t handle, char *printer,
5952264Sjacobs 		papi_attribute_t **job_attributes,
5962264Sjacobs 		papi_job_ticket_t *job_ticket,
5972264Sjacobs 		char **files, papi_job_t *job)
5980Sstevel@tonic-gate {
5990Sstevel@tonic-gate 	service_t *svc = handle;
6008569SJonathan.Ca@Sun.COM 	struct stat statbuf;
6010Sstevel@tonic-gate 	job_t *j;
6020Sstevel@tonic-gate 	int file_no;
6030Sstevel@tonic-gate 	short status;
6040Sstevel@tonic-gate 	char *request_id = NULL;
6050Sstevel@tonic-gate 	REQUEST *request;
6060Sstevel@tonic-gate 	char *c;
6070Sstevel@tonic-gate 	char *tmp = NULL;
6080Sstevel@tonic-gate 	char lpfile[BUFSIZ];
6090Sstevel@tonic-gate 	char **file_list = NULL;
6100Sstevel@tonic-gate 
6110Sstevel@tonic-gate 	if ((svc == NULL) || (printer == NULL) || (files == NULL) ||
6120Sstevel@tonic-gate 	    (job == NULL))
6130Sstevel@tonic-gate 		return (PAPI_BAD_ARGUMENT);
6140Sstevel@tonic-gate 
6150Sstevel@tonic-gate 	if (job_ticket != NULL)
6160Sstevel@tonic-gate 		return (PAPI_OPERATION_NOT_SUPPORTED);
6170Sstevel@tonic-gate 
6180Sstevel@tonic-gate 	if (files != NULL)
6190Sstevel@tonic-gate 		for (file_no = 0; files[file_no] != NULL; file_no++) {
6200Sstevel@tonic-gate 			if (access(files[file_no], R_OK) < 0) {
6210Sstevel@tonic-gate 				detailed_error(svc,
6228569SJonathan.Ca@Sun.COM 				    gettext("Cannot access file: %s: %s"),
6238569SJonathan.Ca@Sun.COM 				    files[file_no], strerror(errno));
6242660Sjacobs 				return (PAPI_DOCUMENT_ACCESS_ERROR);
6250Sstevel@tonic-gate 			}
6269224SJonathan.Ca@Sun.COM 			if (stat(files[file_no], &statbuf) < 0) {
6279224SJonathan.Ca@Sun.COM 				detailed_error(svc,
6289224SJonathan.Ca@Sun.COM 				    gettext("Cannot access file: %s: %s"),
6299224SJonathan.Ca@Sun.COM 				    files[file_no], strerror(errno));
6309224SJonathan.Ca@Sun.COM 				return (PAPI_DOCUMENT_ACCESS_ERROR);
6319224SJonathan.Ca@Sun.COM 			}
6328569SJonathan.Ca@Sun.COM 			if (statbuf.st_size == 0) {
6338569SJonathan.Ca@Sun.COM 				detailed_error(svc,
6348569SJonathan.Ca@Sun.COM 				    gettext("Zero byte (empty) file: %s"),
6358569SJonathan.Ca@Sun.COM 				    files[file_no]);
6368569SJonathan.Ca@Sun.COM 				return (PAPI_BAD_ARGUMENT);
6378569SJonathan.Ca@Sun.COM 			}
6388569SJonathan.Ca@Sun.COM 
6392660Sjacobs 			if (files[file_no][0] != '/') {
6402660Sjacobs 				char path[MAXPATHLEN];
6412660Sjacobs 
6422660Sjacobs 				if (getcwd(path, sizeof (path)) == NULL) {
6432660Sjacobs 					detailed_error(svc, gettext(
6448569SJonathan.Ca@Sun.COM 					    "getcwd for file: %s: %s"),
6458569SJonathan.Ca@Sun.COM 					    files[file_no],
6468569SJonathan.Ca@Sun.COM 					    strerror(errno));
6472660Sjacobs 					return (PAPI_DOCUMENT_ACCESS_ERROR);
6482660Sjacobs 				}
6492660Sjacobs 				strlcat(path, "/", sizeof (path));
6502660Sjacobs 				if (strlcat(path, files[file_no], sizeof (path))
6518569SJonathan.Ca@Sun.COM 				    >= sizeof (path)) {
6522660Sjacobs 					detailed_error(svc, gettext(
6538569SJonathan.Ca@Sun.COM 					    "pathname too long: %s"),
6548569SJonathan.Ca@Sun.COM 					    files[file_no]);
6552660Sjacobs 					return (PAPI_DOCUMENT_ACCESS_ERROR);
6562660Sjacobs 				}
6572660Sjacobs 				addlist(&file_list, path);
6582660Sjacobs 			} else
6592660Sjacobs 				addlist(&file_list, (char *)files[file_no]);
6600Sstevel@tonic-gate 		}
6610Sstevel@tonic-gate 
6620Sstevel@tonic-gate 	if ((*job = j = calloc(1, sizeof (*j))) == NULL)
6630Sstevel@tonic-gate 		return (PAPI_TEMPORARY_ERROR);
6640Sstevel@tonic-gate 
6650Sstevel@tonic-gate 	/* 1 for the control file (-0) */
6660Sstevel@tonic-gate 	status = lpsched_alloc_files(svc, 1, &request_id);
6670Sstevel@tonic-gate 	if (status != PAPI_OK)
6680Sstevel@tonic-gate 		return (status);
6690Sstevel@tonic-gate 
6700Sstevel@tonic-gate 	request = create_request(svc, (char *)printer,
6718569SJonathan.Ca@Sun.COM 	    (papi_attribute_t **)job_attributes);
6720Sstevel@tonic-gate 	request->file_list = file_list;
6730Sstevel@tonic-gate 
6740Sstevel@tonic-gate #ifdef LP_USE_PAPI_ATTR
6750Sstevel@tonic-gate 	/*
6760Sstevel@tonic-gate 	 * store the job attributes in the PAPI job attribute file that was
6770Sstevel@tonic-gate 	 * created by lpsched_alloc_files(), the attributes will then pass
6780Sstevel@tonic-gate 	 * through lpsched and be given to the slow-filters and the printer's
6790Sstevel@tonic-gate 	 * interface script to process them
6800Sstevel@tonic-gate 	 */
6810Sstevel@tonic-gate 	snprintf(lpfile, sizeof (lpfile), "%s%s-%s",
6828569SJonathan.Ca@Sun.COM 	    "/var/spool/lp/temp/", request_id, LP_PAPIATTRNAME);
6830Sstevel@tonic-gate 	status = psm_copy_attrsToFile(job_attributes, lpfile);
6840Sstevel@tonic-gate 	if (status != PAPI_OK) {
6850Sstevel@tonic-gate 		detailed_error(svc, "unable to copy attributes to file: %s: %s",
6868569SJonathan.Ca@Sun.COM 		    lpfile, strerror(errno));
6870Sstevel@tonic-gate 		return (PAPI_DEVICE_ERROR);
6880Sstevel@tonic-gate 	}
6890Sstevel@tonic-gate #endif
6900Sstevel@tonic-gate 
6910Sstevel@tonic-gate 	/* store the meta-data file */
6920Sstevel@tonic-gate 	snprintf(lpfile, sizeof (lpfile), "%s-0", request_id);
6930Sstevel@tonic-gate 	if (putrequest(lpfile, request) < 0) {
6940Sstevel@tonic-gate 		detailed_error(svc, gettext("unable to save request: %s: %s"),
6958569SJonathan.Ca@Sun.COM 		    lpfile, strerror(errno));
6960Sstevel@tonic-gate 		freerequest(request);
6970Sstevel@tonic-gate 		return (PAPI_DEVICE_ERROR);
6980Sstevel@tonic-gate 	}
6990Sstevel@tonic-gate 
7000Sstevel@tonic-gate 	status = lpsched_commit_job(svc, lpfile, &tmp);
7010Sstevel@tonic-gate 	if (status != PAPI_OK) {
7020Sstevel@tonic-gate 		unlink(lpfile);
7030Sstevel@tonic-gate 		freerequest(request);
7040Sstevel@tonic-gate 		return (status);
7050Sstevel@tonic-gate 	}
7060Sstevel@tonic-gate 
7070Sstevel@tonic-gate 	lpsched_request_to_job_attributes(request, j);
7080Sstevel@tonic-gate 
7090Sstevel@tonic-gate 	freerequest(request);
7100Sstevel@tonic-gate 
7110Sstevel@tonic-gate 	if ((c = strrchr(tmp, '-')) != NULL)
7120Sstevel@tonic-gate 		c++;
7130Sstevel@tonic-gate 	papiAttributeListAddInteger(&j->attributes, PAPI_ATTR_REPLACE,
7148569SJonathan.Ca@Sun.COM 	    "job-id", atoi(c));
7150Sstevel@tonic-gate 	papiAttributeListAddString(&j->attributes, PAPI_ATTR_REPLACE,
7168569SJonathan.Ca@Sun.COM 	    "job-uri", tmp);
7170Sstevel@tonic-gate 
7180Sstevel@tonic-gate 	return (PAPI_OK);
7190Sstevel@tonic-gate }
7200Sstevel@tonic-gate 
7210Sstevel@tonic-gate papi_status_t
7222264Sjacobs papiJobValidate(papi_service_t handle, char *printer,
7232264Sjacobs 		papi_attribute_t **job_attributes,
7242264Sjacobs 		papi_job_ticket_t *job_ticket,
7252264Sjacobs 		char **files, papi_job_t *job)
7260Sstevel@tonic-gate {
7270Sstevel@tonic-gate 	papi_status_t status;
7280Sstevel@tonic-gate 	papi_attribute_t **attributes = NULL;
7290Sstevel@tonic-gate 	int i;
7300Sstevel@tonic-gate 
7310Sstevel@tonic-gate 	papiAttributeListAddString(&attributes, PAPI_ATTR_REPLACE,
7328569SJonathan.Ca@Sun.COM 	    "job-hold-until", "indefinite");
7330Sstevel@tonic-gate 	for (i = 0; job_attributes[i]; i++)
7340Sstevel@tonic-gate 		list_append(&attributes, job_attributes[i]);
7350Sstevel@tonic-gate 
7360Sstevel@tonic-gate 	status = papiJobSubmitByReference(handle, printer,
7378569SJonathan.Ca@Sun.COM 	    (papi_attribute_t **)attributes,
7388569SJonathan.Ca@Sun.COM 	    job_ticket, files, job);
7390Sstevel@tonic-gate 	if (status == PAPI_OK) {
7400Sstevel@tonic-gate 		int id = papiJobGetId(*job);
7410Sstevel@tonic-gate 
7420Sstevel@tonic-gate 		if (id != -1)
7430Sstevel@tonic-gate 			papiJobCancel(handle, printer, id);
7440Sstevel@tonic-gate 	}
7450Sstevel@tonic-gate 
7460Sstevel@tonic-gate 	attributes[1] = NULL;	/* after attr[0], they are in another list */
7470Sstevel@tonic-gate 	papiAttributeListFree(attributes);
7480Sstevel@tonic-gate 
7490Sstevel@tonic-gate 	return (status);
7500Sstevel@tonic-gate }
7510Sstevel@tonic-gate 
7520Sstevel@tonic-gate papi_status_t
7532264Sjacobs papiJobStreamOpen(papi_service_t handle, char *printer,
7542264Sjacobs 		papi_attribute_t **job_attributes,
7552264Sjacobs 		papi_job_ticket_t *job_ticket, papi_stream_t *stream)
7560Sstevel@tonic-gate {
7570Sstevel@tonic-gate 	papi_status_t status;
7580Sstevel@tonic-gate 	service_t *svc = handle;
7590Sstevel@tonic-gate 	job_stream_t *s = NULL;
7600Sstevel@tonic-gate 	char *request_id = NULL;
7610Sstevel@tonic-gate 	char lpfile[BUFSIZ];
7620Sstevel@tonic-gate 
7630Sstevel@tonic-gate 	if ((svc == NULL) || (printer == NULL) || (stream == NULL))
7640Sstevel@tonic-gate 		return (PAPI_BAD_ARGUMENT);
7650Sstevel@tonic-gate 
7660Sstevel@tonic-gate 	if (job_ticket != NULL)
7670Sstevel@tonic-gate 		return (PAPI_OPERATION_NOT_SUPPORTED);
7680Sstevel@tonic-gate 
7690Sstevel@tonic-gate 	if ((*stream = s = calloc(1, sizeof (*s))) == NULL)
7700Sstevel@tonic-gate 		return (PAPI_TEMPORARY_ERROR);
7710Sstevel@tonic-gate 
7720Sstevel@tonic-gate 	/* 1 for data, 1 for the meta-data (-0) */
7730Sstevel@tonic-gate 	status = lpsched_alloc_files(svc, 2, &request_id);
7740Sstevel@tonic-gate 	if (status != PAPI_OK)
7750Sstevel@tonic-gate 		return (status);
7760Sstevel@tonic-gate 
7770Sstevel@tonic-gate 	s->request = create_request(svc, (char *)printer,
7788569SJonathan.Ca@Sun.COM 	    (papi_attribute_t **)job_attributes);
7790Sstevel@tonic-gate 	snprintf(lpfile, sizeof (lpfile), "/var/spool/lp/temp/%s-1",
7808569SJonathan.Ca@Sun.COM 	    request_id);
7810Sstevel@tonic-gate 	s->fd = open(lpfile, O_WRONLY);
7820Sstevel@tonic-gate 	addlist(&(s->request->file_list), lpfile);
7830Sstevel@tonic-gate 
7840Sstevel@tonic-gate #ifdef LP_USE_PAPI_ATTR
7850Sstevel@tonic-gate 	/*
7860Sstevel@tonic-gate 	 * store the job attributes in the PAPI job attribute file that was
7870Sstevel@tonic-gate 	 * created by lpsched_alloc_files(), the attributes will then pass
7880Sstevel@tonic-gate 	 * through lpsched and be given to the slow-filters and the printer's
7890Sstevel@tonic-gate 	 * interface script to process them
7900Sstevel@tonic-gate 	 */
7910Sstevel@tonic-gate 	snprintf(lpfile, sizeof (lpfile), "%s%s-%s",
7928569SJonathan.Ca@Sun.COM 	    "/var/spool/lp/temp/", request_id, LP_PAPIATTRNAME);
7930Sstevel@tonic-gate 	status = psm_copy_attrsToFile(job_attributes, lpfile);
7940Sstevel@tonic-gate 	if (status != PAPI_OK) {
7950Sstevel@tonic-gate 		detailed_error(svc, "unable to copy attributes to file: %s: %s",
7968569SJonathan.Ca@Sun.COM 		    lpfile, strerror(errno));
7970Sstevel@tonic-gate 		close(s->fd);
7980Sstevel@tonic-gate 		free(s);
7990Sstevel@tonic-gate 		return (PAPI_DEVICE_ERROR);
8000Sstevel@tonic-gate 	}
8010Sstevel@tonic-gate #endif
8020Sstevel@tonic-gate 
8030Sstevel@tonic-gate 	/* store the meta-data file */
8040Sstevel@tonic-gate 	snprintf(lpfile, sizeof (lpfile), "%s-0", request_id);
8050Sstevel@tonic-gate 	s->meta_data_file = strdup(lpfile);
8060Sstevel@tonic-gate 	if (putrequest(lpfile, s->request) < 0) {
8070Sstevel@tonic-gate 		detailed_error(svc, gettext("unable to save request: %s: %s"),
8088569SJonathan.Ca@Sun.COM 		    lpfile, strerror(errno));
8090Sstevel@tonic-gate 		s->request = NULL;
8100Sstevel@tonic-gate 		return (PAPI_DEVICE_ERROR);
8110Sstevel@tonic-gate 	}
8120Sstevel@tonic-gate 
8130Sstevel@tonic-gate 	return (PAPI_OK);
8140Sstevel@tonic-gate }
8150Sstevel@tonic-gate 
8160Sstevel@tonic-gate papi_status_t
8170Sstevel@tonic-gate papiJobStreamWrite(papi_service_t handle,
8182264Sjacobs 		papi_stream_t stream, void *buffer, size_t buflen)
8190Sstevel@tonic-gate {
8200Sstevel@tonic-gate 	service_t *svc = handle;
8210Sstevel@tonic-gate 	job_stream_t *s = stream;
8220Sstevel@tonic-gate 
8230Sstevel@tonic-gate 	if ((svc == NULL) || (stream == NULL) || (buffer == NULL))
8240Sstevel@tonic-gate 		return (PAPI_BAD_ARGUMENT);
8250Sstevel@tonic-gate 
8260Sstevel@tonic-gate 	if (write(s->fd, buffer, buflen) != buflen)
8270Sstevel@tonic-gate 		return (PAPI_DEVICE_ERROR);
8280Sstevel@tonic-gate 
8290Sstevel@tonic-gate 	return (PAPI_OK);
8300Sstevel@tonic-gate }
8310Sstevel@tonic-gate papi_status_t
8320Sstevel@tonic-gate papiJobStreamClose(papi_service_t handle,
8330Sstevel@tonic-gate 		papi_stream_t stream, papi_job_t *job)
8340Sstevel@tonic-gate {
8350Sstevel@tonic-gate 	papi_status_t status = PAPI_OK;
8360Sstevel@tonic-gate 	service_t *svc = handle;
8370Sstevel@tonic-gate 	job_stream_t *s = stream;
8380Sstevel@tonic-gate 	job_t *j = NULL;
8390Sstevel@tonic-gate 	char *tmp = NULL, *c;
8400Sstevel@tonic-gate 
8410Sstevel@tonic-gate 	if ((svc == NULL) || (stream == NULL) || (job == NULL))
8420Sstevel@tonic-gate 		return (PAPI_BAD_ARGUMENT);
8430Sstevel@tonic-gate 
8440Sstevel@tonic-gate 	if ((*job = j = calloc(1, sizeof (*j))) == NULL)
8450Sstevel@tonic-gate 		return (PAPI_TEMPORARY_ERROR);
8460Sstevel@tonic-gate 
8470Sstevel@tonic-gate 	close(s->fd);
8480Sstevel@tonic-gate 
8490Sstevel@tonic-gate 	lpsched_request_to_job_attributes(s->request, j);
8500Sstevel@tonic-gate 
8510Sstevel@tonic-gate 	if (s->meta_data_file != NULL) {
8520Sstevel@tonic-gate 		status = lpsched_commit_job(svc, s->meta_data_file, &tmp);
8530Sstevel@tonic-gate 		if (status != PAPI_OK) {
8540Sstevel@tonic-gate 			unlink(s->meta_data_file);
8550Sstevel@tonic-gate 			return (status);
8560Sstevel@tonic-gate 		}
8570Sstevel@tonic-gate 		if ((c = strrchr(tmp, '-')) != NULL)
8580Sstevel@tonic-gate 			c++;
8590Sstevel@tonic-gate 		papiAttributeListAddInteger(&j->attributes, PAPI_ATTR_REPLACE,
8608569SJonathan.Ca@Sun.COM 		    "job-id", atoi(c));
8610Sstevel@tonic-gate 		papiAttributeListAddString(&j->attributes, PAPI_ATTR_REPLACE,
8628569SJonathan.Ca@Sun.COM 		    "job-uri", tmp);
8630Sstevel@tonic-gate 		free(s->meta_data_file);
8640Sstevel@tonic-gate 	}
8657253Sjacobs 	freerequest(s->request);
8660Sstevel@tonic-gate 	free(s);
8670Sstevel@tonic-gate 
8680Sstevel@tonic-gate 	return (PAPI_OK);
8690Sstevel@tonic-gate }
8700Sstevel@tonic-gate 
8710Sstevel@tonic-gate papi_status_t
8722264Sjacobs papiJobQuery(papi_service_t handle, char *printer, int32_t job_id,
8732264Sjacobs 		char **requested_attrs,
8740Sstevel@tonic-gate 		papi_job_t *job)
8750Sstevel@tonic-gate {
8760Sstevel@tonic-gate 	service_t *svc = handle;
8770Sstevel@tonic-gate 	job_t *j;
8780Sstevel@tonic-gate 	char *dest;
8790Sstevel@tonic-gate 	char req_id[32];
8800Sstevel@tonic-gate 	short rc;
8810Sstevel@tonic-gate 	char *form = NULL,
8828569SJonathan.Ca@Sun.COM 	    *request_id = NULL,
8838569SJonathan.Ca@Sun.COM 	    *charset = NULL,
8848569SJonathan.Ca@Sun.COM 	    *user = NULL,
8858569SJonathan.Ca@Sun.COM 	    *slabel = NULL,
8868569SJonathan.Ca@Sun.COM 	    *file = NULL;
8870Sstevel@tonic-gate 	time_t date = 0;
8880Sstevel@tonic-gate 	size_t size = 0;
8890Sstevel@tonic-gate 	short  rank = 0,
8908569SJonathan.Ca@Sun.COM 	    state = 0;
8910Sstevel@tonic-gate 
8920Sstevel@tonic-gate 	if ((handle == NULL) || (printer == NULL) || (job_id < 0))
8930Sstevel@tonic-gate 		return (PAPI_BAD_ARGUMENT);
8940Sstevel@tonic-gate 
8950Sstevel@tonic-gate 	dest = printer_name_from_uri_id(printer, job_id);
8960Sstevel@tonic-gate 	snprintf(req_id, sizeof (req_id), "%s-%d", dest, job_id);
8970Sstevel@tonic-gate 	free(dest);
8980Sstevel@tonic-gate 
8990Sstevel@tonic-gate 	rc = snd_msg(svc, S_INQUIRE_REQUEST_RANK, 0, "", "", req_id, "", "");
9000Sstevel@tonic-gate 	if (rc < 0)
9010Sstevel@tonic-gate 		return (PAPI_SERVICE_UNAVAILABLE);
9020Sstevel@tonic-gate 
9030Sstevel@tonic-gate 	if (rcv_msg(svc, R_INQUIRE_REQUEST_RANK, &rc, &request_id,
9048569SJonathan.Ca@Sun.COM 	    &user, &slabel, &size, &date, &state, &dest, &form,
9058569SJonathan.Ca@Sun.COM 	    &charset, &rank, &file) < 0) {
9060Sstevel@tonic-gate 		detailed_error(svc,
9078569SJonathan.Ca@Sun.COM 		    gettext("failed to read response from scheduler"));
9080Sstevel@tonic-gate 		return (PAPI_DEVICE_ERROR);
9090Sstevel@tonic-gate 	}
9100Sstevel@tonic-gate 
9110Sstevel@tonic-gate 	if ((request_id == NULL) || (request_id[0] == NULL))
9120Sstevel@tonic-gate 		return (PAPI_NOT_FOUND);
9130Sstevel@tonic-gate 
9140Sstevel@tonic-gate 	if ((*job = j = calloc(1, sizeof (*j))) == NULL)
9150Sstevel@tonic-gate 		return (PAPI_TEMPORARY_ERROR);
9160Sstevel@tonic-gate 
917*10781SGowtham.Thommandra@Sun.COM 	snprintf(req_id, sizeof (req_id), "%d-0", job_id);
918*10781SGowtham.Thommandra@Sun.COM 	lpsched_read_job_configuration(svc, j, req_id);
919*10781SGowtham.Thommandra@Sun.COM 
9201676Sjpk 	job_status_to_attributes(j, request_id, user, slabel, size, date, state,
9218569SJonathan.Ca@Sun.COM 	    dest, form, charset, rank, file);
9220Sstevel@tonic-gate 
9230Sstevel@tonic-gate 	return (PAPI_OK);
9240Sstevel@tonic-gate }
9250Sstevel@tonic-gate 
9260Sstevel@tonic-gate papi_status_t
9272264Sjacobs papiJobMove(papi_service_t handle, char *printer, int32_t job_id,
9282264Sjacobs 		char *destination)
9292264Sjacobs {
9302264Sjacobs 	papi_status_t result = PAPI_OK;
9318266SNagaraj.Yedathore@Sun.COM 	long bits;
9322264Sjacobs 	service_t *svc = handle;
9332264Sjacobs 	char req_id[64];
9342264Sjacobs 	char *queue;
9352264Sjacobs 	char *user = NULL;
9362264Sjacobs 
9372264Sjacobs 	if ((svc == NULL) || (printer == NULL) || (job_id < 0) ||
9382264Sjacobs 	    (destination == NULL))
9392264Sjacobs 		return (PAPI_BAD_ARGUMENT);
9402264Sjacobs 
9412264Sjacobs 	queue = printer_name_from_uri_id(printer, job_id);
9422264Sjacobs 	snprintf(req_id, sizeof (req_id), "%s-%d", queue, job_id);
9432264Sjacobs 	free(queue);
9442264Sjacobs 
9452264Sjacobs 	if (papiAttributeListGetString(svc->attributes, NULL, "user-name",
9468569SJonathan.Ca@Sun.COM 	    &user) == PAPI_OK) {
9472264Sjacobs 		REQUEST *r = getrequest(req_id);
9482264Sjacobs 
9492264Sjacobs 		if ((r != NULL) && (r->user != NULL) &&
9502264Sjacobs 		    (strcmp(r->user, user) != 0))
9512264Sjacobs 			result = PAPI_NOT_AUTHORIZED;
9522264Sjacobs 		freerequest(r);
9532264Sjacobs 	}
9542264Sjacobs 
9552264Sjacobs 	if (result == PAPI_OK) {
9562264Sjacobs 		short status = MOK;
9572264Sjacobs 		char *dest = printer_name_from_uri_id(destination, -1);
9582264Sjacobs 
9592264Sjacobs 		if ((snd_msg(svc, S_MOVE_REQUEST, req_id, dest) < 0) ||
9608266SNagaraj.Yedathore@Sun.COM 		    (rcv_msg(svc, R_MOVE_REQUEST, &status, &bits) < 0))
9612264Sjacobs 			status = MTRANSMITERR;
9622264Sjacobs 
9632264Sjacobs 		free(dest);
9642264Sjacobs 
9652264Sjacobs 		result = lpsched_status_to_papi_status(status);
9662264Sjacobs 	}
9672264Sjacobs 
9682264Sjacobs 	return (result);
9692264Sjacobs }
9702264Sjacobs 
9712264Sjacobs papi_status_t
9722264Sjacobs papiJobCancel(papi_service_t handle, char *printer, int32_t job_id)
9730Sstevel@tonic-gate {
9740Sstevel@tonic-gate 	papi_status_t result = PAPI_OK;
9750Sstevel@tonic-gate 	service_t *svc = handle;
9760Sstevel@tonic-gate 	char req_id[64];
9770Sstevel@tonic-gate 	char *dest;
9780Sstevel@tonic-gate 	char *user = NULL;
9790Sstevel@tonic-gate 
9800Sstevel@tonic-gate 	if ((svc == NULL) || (printer == NULL) || (job_id < 0))
9810Sstevel@tonic-gate 		return (PAPI_BAD_ARGUMENT);
9820Sstevel@tonic-gate 
9830Sstevel@tonic-gate 	dest = printer_name_from_uri_id(printer, job_id);
9840Sstevel@tonic-gate 	snprintf(req_id, sizeof (req_id), "%s-%d", dest, job_id);
9850Sstevel@tonic-gate 	free(dest);
9860Sstevel@tonic-gate 
9870Sstevel@tonic-gate 	if (papiAttributeListGetString(svc->attributes, NULL, "user-name",
9888569SJonathan.Ca@Sun.COM 	    &user) == PAPI_OK) {
9890Sstevel@tonic-gate 		REQUEST *r = getrequest(req_id);
9900Sstevel@tonic-gate 
9918966SSonam.Gupta@Sun.COM 		if ((result = authorized(handle, job_id)) != PAPI_OK)
9928966SSonam.Gupta@Sun.COM 			result = PAPI_NOT_AUTHORIZED;
9938966SSonam.Gupta@Sun.COM 
9942264Sjacobs 		if ((r != NULL) && (r->user != NULL) &&
9952264Sjacobs 		    (strcmp(r->user, user) != 0))
9960Sstevel@tonic-gate 			result = PAPI_NOT_AUTHORIZED;
9970Sstevel@tonic-gate 		freerequest(r);
9980Sstevel@tonic-gate 	}
9990Sstevel@tonic-gate 
10000Sstevel@tonic-gate 	if (result == PAPI_OK) {
10010Sstevel@tonic-gate 		short status = MOK;
10020Sstevel@tonic-gate 
10030Sstevel@tonic-gate 		if ((snd_msg(svc, S_CANCEL_REQUEST, req_id) < 0) ||
10040Sstevel@tonic-gate 		    (rcv_msg(svc, R_CANCEL_REQUEST, &status) < 0))
10050Sstevel@tonic-gate 			status = MTRANSMITERR;
10060Sstevel@tonic-gate 
10070Sstevel@tonic-gate 		result = lpsched_status_to_papi_status(status);
10080Sstevel@tonic-gate 	}
10090Sstevel@tonic-gate 
10100Sstevel@tonic-gate 	return (result);
10110Sstevel@tonic-gate }
10120Sstevel@tonic-gate 
10130Sstevel@tonic-gate papi_status_t
10142264Sjacobs hold_release_job(papi_service_t handle, char *printer,
10152264Sjacobs 		int32_t job_id, int flag)
10160Sstevel@tonic-gate {
10170Sstevel@tonic-gate 	papi_status_t status;
10180Sstevel@tonic-gate 	service_t *svc = handle;
10190Sstevel@tonic-gate 	REQUEST *r = NULL;
10200Sstevel@tonic-gate 	char *file;
10210Sstevel@tonic-gate 	char *dest;
10220Sstevel@tonic-gate 
10230Sstevel@tonic-gate 	if ((svc == NULL) || (printer == NULL) || (job_id < 0))
10240Sstevel@tonic-gate 		return (PAPI_BAD_ARGUMENT);
10250Sstevel@tonic-gate 
10260Sstevel@tonic-gate 	if ((status = authorized(svc, job_id)) != PAPI_OK)
10270Sstevel@tonic-gate 		return (status);
10280Sstevel@tonic-gate 
10290Sstevel@tonic-gate 	dest = printer_name_from_uri_id(printer, job_id);
10300Sstevel@tonic-gate 	status = lpsched_start_change(svc, dest, job_id, &file);
10310Sstevel@tonic-gate 	if (status != PAPI_OK)
10320Sstevel@tonic-gate 		return (status);
10330Sstevel@tonic-gate 
10340Sstevel@tonic-gate 	if ((r = getrequest(file)) != NULL) {
10350Sstevel@tonic-gate 		r->actions &= ~ACT_RESUME;
10362264Sjacobs 		switch (flag) {
10372264Sjacobs 		case 0:
10380Sstevel@tonic-gate 			r->actions |= ACT_HOLD;
10392264Sjacobs 			break;
10402264Sjacobs 		case 1:
10410Sstevel@tonic-gate 			r->actions |= ACT_RESUME;
10422264Sjacobs 			break;
10432264Sjacobs 		case 2:
10442264Sjacobs 			r->actions |= ACT_IMMEDIATE;
10452264Sjacobs 			break;
10462264Sjacobs 		}
10470Sstevel@tonic-gate 		if (putrequest(file, r) < 0) {
10480Sstevel@tonic-gate 			detailed_error(svc,
10498569SJonathan.Ca@Sun.COM 			    gettext("failed to write job: %s: %s"),
10508569SJonathan.Ca@Sun.COM 			    file, strerror(errno));
10512264Sjacobs 			freerequest(r);
10520Sstevel@tonic-gate 			return (PAPI_DEVICE_ERROR);
10530Sstevel@tonic-gate 		}
10542264Sjacobs 		freerequest(r);
10550Sstevel@tonic-gate 	} else {
10560Sstevel@tonic-gate 		detailed_error(svc, gettext("failed to read job: %s: %s"),
10578569SJonathan.Ca@Sun.COM 		    file, strerror(errno));
10580Sstevel@tonic-gate 		return (PAPI_DEVICE_ERROR);
10590Sstevel@tonic-gate 	}
10600Sstevel@tonic-gate 
10610Sstevel@tonic-gate 	status = lpsched_end_change(svc, dest, job_id);
10620Sstevel@tonic-gate 
10630Sstevel@tonic-gate 	return (status);
10640Sstevel@tonic-gate }
10650Sstevel@tonic-gate 
10660Sstevel@tonic-gate papi_status_t
10672264Sjacobs papiJobHold(papi_service_t handle, char *printer, int32_t job_id)
10680Sstevel@tonic-gate {
10690Sstevel@tonic-gate 	return (hold_release_job(handle, printer, job_id, 0));
10700Sstevel@tonic-gate }
10710Sstevel@tonic-gate 
10720Sstevel@tonic-gate papi_status_t
10732264Sjacobs papiJobRelease(papi_service_t handle, char *printer, int32_t job_id)
10740Sstevel@tonic-gate {
10750Sstevel@tonic-gate 	return (hold_release_job(handle, printer, job_id, 1));
10760Sstevel@tonic-gate }
10770Sstevel@tonic-gate 
10780Sstevel@tonic-gate papi_status_t
10792264Sjacobs papiJobPromote(papi_service_t handle, char *printer, int32_t job_id)
10800Sstevel@tonic-gate {
10812264Sjacobs 	return (hold_release_job(handle, printer, job_id, 2));
10820Sstevel@tonic-gate }
10830Sstevel@tonic-gate 
10840Sstevel@tonic-gate papi_status_t
10852264Sjacobs papiJobModify(papi_service_t handle, char *printer, int32_t job_id,
10862264Sjacobs 		papi_attribute_t **attributes, papi_job_t *job)
10870Sstevel@tonic-gate {
10880Sstevel@tonic-gate 	papi_status_t status;
10890Sstevel@tonic-gate 	job_t *j = NULL;
10900Sstevel@tonic-gate 	service_t *svc = handle;
10910Sstevel@tonic-gate 	char *file = NULL;
10920Sstevel@tonic-gate 	char *dest;
10930Sstevel@tonic-gate 	REQUEST *r = NULL;
10940Sstevel@tonic-gate 	char lpfile[BUFSIZ];
10950Sstevel@tonic-gate 
10960Sstevel@tonic-gate 	if ((svc == NULL) || (printer == NULL) || (job_id < 0) ||
10970Sstevel@tonic-gate 	    (attributes == NULL))
10980Sstevel@tonic-gate 		return (PAPI_BAD_ARGUMENT);
10990Sstevel@tonic-gate 
11000Sstevel@tonic-gate 	if ((*job = j = calloc(1, sizeof (*j))) == NULL)
11010Sstevel@tonic-gate 		return (PAPI_TEMPORARY_ERROR);
11020Sstevel@tonic-gate 
11030Sstevel@tonic-gate 	dest = printer_name_from_uri_id(printer, job_id);
11040Sstevel@tonic-gate 	status = lpsched_start_change(svc, dest, job_id, &file);
11050Sstevel@tonic-gate 	if (status != PAPI_OK)
11060Sstevel@tonic-gate 		return (status);
11070Sstevel@tonic-gate 
11080Sstevel@tonic-gate 	if ((r = getrequest(file)) != NULL) {
11090Sstevel@tonic-gate 		job_attributes_to_lpsched_request(handle, r,
11108569SJonathan.Ca@Sun.COM 		    (papi_attribute_t **)attributes);
11110Sstevel@tonic-gate #ifdef LP_USE_PAPI_ATTR
11120Sstevel@tonic-gate 		/*
11130Sstevel@tonic-gate 		 * store the job attributes in the PAPI job attribute file
11140Sstevel@tonic-gate 		 * that was created by the origonal job request. We need to
11150Sstevel@tonic-gate 		 * modify the attributes in the file as per the new attributes
11160Sstevel@tonic-gate 		 */
11170Sstevel@tonic-gate 		snprintf(lpfile, sizeof (lpfile), "%s%d-%s",
11188569SJonathan.Ca@Sun.COM 		    "/var/spool/lp/temp/", job_id, LP_PAPIATTRNAME);
11190Sstevel@tonic-gate 		status = psm_modifyAttrsFile(attributes, lpfile);
11200Sstevel@tonic-gate 		if (status != PAPI_OK) {
11210Sstevel@tonic-gate 			detailed_error(svc,
11228569SJonathan.Ca@Sun.COM 			    "unable to modify the attributes file: %s: %s",
11238569SJonathan.Ca@Sun.COM 			    lpfile, strerror(errno));
11240Sstevel@tonic-gate 			return (PAPI_DEVICE_ERROR);
11250Sstevel@tonic-gate 		}
11260Sstevel@tonic-gate #endif
11270Sstevel@tonic-gate 
11280Sstevel@tonic-gate 		if (putrequest(file, r) < 0) {
11290Sstevel@tonic-gate 			detailed_error(svc,
11308569SJonathan.Ca@Sun.COM 			    gettext("failed to write job: %s: %s"),
11318569SJonathan.Ca@Sun.COM 			    file, strerror(errno));
11320Sstevel@tonic-gate 			freerequest(r);
11330Sstevel@tonic-gate 			return (PAPI_DEVICE_ERROR);
11340Sstevel@tonic-gate 		}
11350Sstevel@tonic-gate 	} else {
11360Sstevel@tonic-gate 		detailed_error(svc, gettext("failed to read job: %s: %s"),
11378569SJonathan.Ca@Sun.COM 		    file, strerror(errno));
11380Sstevel@tonic-gate 		return (PAPI_DEVICE_ERROR);
11390Sstevel@tonic-gate 	}
11400Sstevel@tonic-gate 
11410Sstevel@tonic-gate 	status = lpsched_end_change(svc, dest, job_id);
11420Sstevel@tonic-gate 	lpsched_request_to_job_attributes(r, j);
11436725Sjacobs 
11446725Sjacobs 	papiAttributeListAddInteger(&j->attributes, PAPI_ATTR_REPLACE,
11458569SJonathan.Ca@Sun.COM 	    "job-id", job_id);
11466725Sjacobs 
11470Sstevel@tonic-gate 	freerequest(r);
11480Sstevel@tonic-gate 
11490Sstevel@tonic-gate 	return (status);
11500Sstevel@tonic-gate }
11510Sstevel@tonic-gate 
11520Sstevel@tonic-gate /*
11530Sstevel@tonic-gate  * Extension to PAPI, a variation of this is slated for post-1.0
11540Sstevel@tonic-gate  */
11550Sstevel@tonic-gate #define	DUMMY_FILE	"/var/spool/lp/fifos/FIFO"
11560Sstevel@tonic-gate 
11570Sstevel@tonic-gate papi_status_t
11582264Sjacobs papiJobCreate(papi_service_t handle, char *printer,
11592264Sjacobs 		papi_attribute_t **job_attributes,
11602264Sjacobs 		papi_job_ticket_t *job_ticket, papi_job_t *job)
11610Sstevel@tonic-gate {
11620Sstevel@tonic-gate 	papi_status_t status;
11630Sstevel@tonic-gate 	service_t *svc = handle;
11640Sstevel@tonic-gate 	job_t *j = NULL;
11650Sstevel@tonic-gate 	REQUEST *request;
11660Sstevel@tonic-gate 	char *request_id = NULL;
11670Sstevel@tonic-gate 	char *c;
11680Sstevel@tonic-gate 	char *tmp = NULL;
11690Sstevel@tonic-gate 	char metadata_file[MAXPATHLEN];
11700Sstevel@tonic-gate 
11710Sstevel@tonic-gate 	if ((svc == NULL) || (printer == NULL) || (job == NULL))
11720Sstevel@tonic-gate 		return (PAPI_BAD_ARGUMENT);
11730Sstevel@tonic-gate 
11740Sstevel@tonic-gate 	if (job_ticket != NULL)
11750Sstevel@tonic-gate 		return (PAPI_JOB_TICKET_NOT_SUPPORTED);
11760Sstevel@tonic-gate 
11770Sstevel@tonic-gate 	if ((*job = j = calloc(1, sizeof (*j))) == NULL)
11780Sstevel@tonic-gate 		return (PAPI_TEMPORARY_ERROR);
11790Sstevel@tonic-gate 
11800Sstevel@tonic-gate 	/* 1 for the control file (-0) */
11810Sstevel@tonic-gate 	status = lpsched_alloc_files(svc, 1, &request_id);
11820Sstevel@tonic-gate 	if (status != PAPI_OK)
11830Sstevel@tonic-gate 		return (status);
11840Sstevel@tonic-gate 
11850Sstevel@tonic-gate 	/* convert the attributes to an lpsched REQUEST structure */
11860Sstevel@tonic-gate 	request = create_request(svc, (char *)printer,
11878569SJonathan.Ca@Sun.COM 	    (papi_attribute_t **)job_attributes);
11882264Sjacobs 	if (request == NULL)
11892264Sjacobs 		return (PAPI_TEMPORARY_ERROR);
11900Sstevel@tonic-gate 	addlist(&request->file_list, DUMMY_FILE);	/* add a dummy file */
11910Sstevel@tonic-gate 	request->actions |= ACT_HOLD;			/* hold the job */
11920Sstevel@tonic-gate 
11930Sstevel@tonic-gate #ifdef LP_USE_PAPI_ATTR
11940Sstevel@tonic-gate 	/*
11950Sstevel@tonic-gate 	 * store the job attributes in the PAPI job attribute file that was
11960Sstevel@tonic-gate 	 * created by lpsched_alloc_files(), the attributes will then pass
11970Sstevel@tonic-gate 	 * through lpsched and be given to the slow-filters and the printer's
11980Sstevel@tonic-gate 	 * interface script to process them
11990Sstevel@tonic-gate 	 */
12000Sstevel@tonic-gate 	snprintf(metadata_file, sizeof (metadata_file), "%s%s-%s",
12018569SJonathan.Ca@Sun.COM 	    "/var/spool/lp/temp/", request_id, LP_PAPIATTRNAME);
12020Sstevel@tonic-gate 	status = psm_copy_attrsToFile(job_attributes, metadata_file);
12030Sstevel@tonic-gate 	if (status != PAPI_OK) {
12040Sstevel@tonic-gate 		detailed_error(svc, "unable to copy attributes to file: %s: %s",
12058569SJonathan.Ca@Sun.COM 		    metadata_file, strerror(errno));
12062264Sjacobs 		free(request_id);
12070Sstevel@tonic-gate 		return (PAPI_DEVICE_ERROR);
12080Sstevel@tonic-gate 	}
12090Sstevel@tonic-gate #endif
12100Sstevel@tonic-gate 
12110Sstevel@tonic-gate 	/* store the REQUEST on disk */
12120Sstevel@tonic-gate 	snprintf(metadata_file, sizeof (metadata_file), "%s-0", request_id);
12132264Sjacobs 	free(request_id);
12140Sstevel@tonic-gate 	if (putrequest(metadata_file, request) < 0) {
12150Sstevel@tonic-gate 		detailed_error(svc, gettext("unable to save request: %s: %s"),
12168569SJonathan.Ca@Sun.COM 		    metadata_file, strerror(errno));
12170Sstevel@tonic-gate 		return (PAPI_DEVICE_ERROR);
12180Sstevel@tonic-gate 	}
12190Sstevel@tonic-gate 
12200Sstevel@tonic-gate 	status = lpsched_commit_job(svc, metadata_file, &tmp);
12210Sstevel@tonic-gate 	if (status != PAPI_OK) {
12220Sstevel@tonic-gate 		unlink(metadata_file);
12230Sstevel@tonic-gate 		return (status);
12240Sstevel@tonic-gate 	}
12250Sstevel@tonic-gate 
12260Sstevel@tonic-gate 	lpsched_request_to_job_attributes(request, j);
12270Sstevel@tonic-gate 
12280Sstevel@tonic-gate 	if ((c = strrchr(tmp, '-')) != NULL)
12290Sstevel@tonic-gate 		c++;
12300Sstevel@tonic-gate 	papiAttributeListAddInteger(&j->attributes, PAPI_ATTR_REPLACE,
12318569SJonathan.Ca@Sun.COM 	    "job-id", atoi(c));
12320Sstevel@tonic-gate 	papiAttributeListAddString(&j->attributes, PAPI_ATTR_REPLACE,
12338569SJonathan.Ca@Sun.COM 	    "job-uri", tmp);
12340Sstevel@tonic-gate 
12350Sstevel@tonic-gate 	return (PAPI_OK);
12360Sstevel@tonic-gate }
12370Sstevel@tonic-gate 
12380Sstevel@tonic-gate papi_status_t
12390Sstevel@tonic-gate papiJobCommit(papi_service_t handle, char *printer, int32_t id)
12400Sstevel@tonic-gate {
12410Sstevel@tonic-gate 	papi_status_t status = PAPI_OK;
12420Sstevel@tonic-gate 	service_t *svc = handle;
12430Sstevel@tonic-gate 	REQUEST *r = NULL;
12440Sstevel@tonic-gate 	char *metadata_file;
12450Sstevel@tonic-gate 	char *dest;
12460Sstevel@tonic-gate 
12470Sstevel@tonic-gate 	if ((svc == NULL) || (printer == NULL))
12480Sstevel@tonic-gate 		return (PAPI_BAD_ARGUMENT);
12490Sstevel@tonic-gate 
12500Sstevel@tonic-gate 	dest = printer_name_from_uri_id(printer, id);
12510Sstevel@tonic-gate 	/* tell the scheduler that we want to change the job */
12520Sstevel@tonic-gate 	status = lpsched_start_change(svc, dest, id, &metadata_file);
12530Sstevel@tonic-gate 	if (status != PAPI_OK)
12540Sstevel@tonic-gate 		return (status);
12550Sstevel@tonic-gate 
12560Sstevel@tonic-gate 	if ((r = getrequest(metadata_file)) != NULL) {
12570Sstevel@tonic-gate 		r->actions &= ~ACT_RESUME;
12580Sstevel@tonic-gate 		r->actions |= ACT_RESUME;
12590Sstevel@tonic-gate 		dellist(&r->file_list, DUMMY_FILE);
12600Sstevel@tonic-gate 
12610Sstevel@tonic-gate 		if (putrequest(metadata_file, r) < 0) {
12620Sstevel@tonic-gate 			detailed_error(svc,
12638569SJonathan.Ca@Sun.COM 			    gettext("failed to write job: %s: %s"),
12648569SJonathan.Ca@Sun.COM 			    metadata_file, strerror(errno));
12652264Sjacobs 			freerequest(r);
12660Sstevel@tonic-gate 			return (PAPI_DEVICE_ERROR);
12670Sstevel@tonic-gate 		}
12680Sstevel@tonic-gate 	} else {
12690Sstevel@tonic-gate 		detailed_error(svc, gettext("failed to read job: %s: %s"),
12708569SJonathan.Ca@Sun.COM 		    metadata_file, strerror(errno));
12710Sstevel@tonic-gate 		return (PAPI_DEVICE_ERROR);
12720Sstevel@tonic-gate 	}
12730Sstevel@tonic-gate 
12740Sstevel@tonic-gate 	status = lpsched_end_change(svc, dest, id);
12750Sstevel@tonic-gate 	freerequest(r);
12760Sstevel@tonic-gate 
12770Sstevel@tonic-gate 	return (status);
12780Sstevel@tonic-gate }
12790Sstevel@tonic-gate 
12800Sstevel@tonic-gate papi_status_t
12810Sstevel@tonic-gate papiJobStreamAdd(papi_service_t handle, char *printer, int32_t id,
12820Sstevel@tonic-gate 		papi_stream_t *stream)
12830Sstevel@tonic-gate {
12840Sstevel@tonic-gate 	papi_status_t status;
12850Sstevel@tonic-gate 	service_t *svc = handle;
12860Sstevel@tonic-gate 	job_stream_t *s = NULL;
12870Sstevel@tonic-gate 	char *metadata_file = NULL;
12880Sstevel@tonic-gate 	char *dest;
12890Sstevel@tonic-gate 	char path[MAXPATHLEN];
12900Sstevel@tonic-gate 
12910Sstevel@tonic-gate 	/* allocate space for the stream */
12920Sstevel@tonic-gate 	if ((*stream = s = calloc(1, sizeof (*s))) == NULL)
12930Sstevel@tonic-gate 		return (PAPI_TEMPORARY_ERROR);
12940Sstevel@tonic-gate 
12950Sstevel@tonic-gate 	dest = printer_name_from_uri_id(printer, id);
12960Sstevel@tonic-gate 	/* create/open data file (only root or lp can really do this */
12970Sstevel@tonic-gate 	snprintf(path, sizeof (path), "/var/spool/lp/temp/%d-XXXXXX", id);
12980Sstevel@tonic-gate 	if ((s->fd = mkstemp(path)) < 0) {
12990Sstevel@tonic-gate 		detailed_error(svc, gettext("unable to create sink (%s): %s"),
13008569SJonathan.Ca@Sun.COM 		    path, strerror(errno));
13010Sstevel@tonic-gate 		free(s);
13020Sstevel@tonic-gate 		return (PAPI_NOT_AUTHORIZED);
13030Sstevel@tonic-gate 	}
13040Sstevel@tonic-gate 
13050Sstevel@tonic-gate 	/* add data file to job */
13060Sstevel@tonic-gate 	status = lpsched_start_change(svc, dest, id, &metadata_file);
13070Sstevel@tonic-gate 	if (status != PAPI_OK) {
13080Sstevel@tonic-gate 		close(s->fd);
13090Sstevel@tonic-gate 		free(s);
13100Sstevel@tonic-gate 		unlink(path);
13110Sstevel@tonic-gate 		return (status);
13120Sstevel@tonic-gate 	}
13130Sstevel@tonic-gate 
13140Sstevel@tonic-gate 	if ((s->request = getrequest(metadata_file)) == NULL) {
13150Sstevel@tonic-gate 		detailed_error(svc, gettext("unable to load request: %s: %s"),
13168569SJonathan.Ca@Sun.COM 		    metadata_file, strerror(errno));
13170Sstevel@tonic-gate 		close(s->fd);
13180Sstevel@tonic-gate 		free(s);
13190Sstevel@tonic-gate 		unlink(path);
13200Sstevel@tonic-gate 		return (PAPI_NOT_POSSIBLE);
13210Sstevel@tonic-gate 	}
13220Sstevel@tonic-gate 
13230Sstevel@tonic-gate 	addlist(&(s->request->file_list), path);
13240Sstevel@tonic-gate 
13250Sstevel@tonic-gate 	if (putrequest(metadata_file, s->request) < 0) {
13260Sstevel@tonic-gate 		detailed_error(svc, gettext("unable to save request: %s: %s"),
13278569SJonathan.Ca@Sun.COM 		    metadata_file, strerror(errno));
13280Sstevel@tonic-gate 		close(s->fd);
13290Sstevel@tonic-gate 		free(s);
13300Sstevel@tonic-gate 		unlink(path);
13310Sstevel@tonic-gate 		return (PAPI_NOT_POSSIBLE);
13320Sstevel@tonic-gate 	}
13330Sstevel@tonic-gate 
13340Sstevel@tonic-gate 	status = lpsched_end_change(svc, dest, id);
13350Sstevel@tonic-gate 
13360Sstevel@tonic-gate 	if (status != PAPI_OK)
13370Sstevel@tonic-gate 		return (status);
13380Sstevel@tonic-gate 
13390Sstevel@tonic-gate 	return (PAPI_OK);
13400Sstevel@tonic-gate }
1341