17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
50a44ef6dSjacobs  * Common Development and Distribution License (the "License").
60a44ef6dSjacobs  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
210a44ef6dSjacobs 
227c478bd9Sstevel@tonic-gate /*
230a44ef6dSjacobs  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
287c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate /* EMACS_MODES: !fill, lnumb, !overwrite, !nodelete, !picture */
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate #include "stdio.h"
337c478bd9Sstevel@tonic-gate #include "string.h"
347c478bd9Sstevel@tonic-gate #include "errno.h"
357c478bd9Sstevel@tonic-gate #include "sys/types.h"
367c478bd9Sstevel@tonic-gate #include "stdlib.h"
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate #include "lp.h"
397c478bd9Sstevel@tonic-gate #include "requests.h"
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate extern struct {
427c478bd9Sstevel@tonic-gate 	char			*v;
437c478bd9Sstevel@tonic-gate 	short			len;
447c478bd9Sstevel@tonic-gate }			reqheadings[];
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate /**
477c478bd9Sstevel@tonic-gate  ** getrequest() - EXTRACT REQUEST STRUCTURE FROM DISK FILE
487c478bd9Sstevel@tonic-gate  **/
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate REQUEST *
517c478bd9Sstevel@tonic-gate #if	defined(__STDC__)
getrequest(char * file)527c478bd9Sstevel@tonic-gate getrequest (
537c478bd9Sstevel@tonic-gate 	char *			file
547c478bd9Sstevel@tonic-gate )
557c478bd9Sstevel@tonic-gate #else
567c478bd9Sstevel@tonic-gate getrequest (file)
577c478bd9Sstevel@tonic-gate 	char			*file;
587c478bd9Sstevel@tonic-gate #endif
597c478bd9Sstevel@tonic-gate {
600a44ef6dSjacobs 	REQUEST		*reqp;
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate 	char			buf[BUFSIZ],
637c478bd9Sstevel@tonic-gate 				*path,
647c478bd9Sstevel@tonic-gate 				*p;
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate 	int fd;
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate 	int			fld;
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate 	/*
727c478bd9Sstevel@tonic-gate 	 * Full pathname? If so the file must lie in LP's
737c478bd9Sstevel@tonic-gate 	 * regular temporary directory.
747c478bd9Sstevel@tonic-gate 	 */
757c478bd9Sstevel@tonic-gate 	if (*file == '/') {
767c478bd9Sstevel@tonic-gate 		if (!STRNEQU(file, Lp_Tmp, strlen(Lp_Tmp))) {
777c478bd9Sstevel@tonic-gate 			errno = EINVAL;
787c478bd9Sstevel@tonic-gate 			return (0);
797c478bd9Sstevel@tonic-gate 		}
807c478bd9Sstevel@tonic-gate 		path = Strdup(file);
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate 	/*
837c478bd9Sstevel@tonic-gate 	 * A relative pathname (such as system/name)?
847c478bd9Sstevel@tonic-gate 	 * If so we'll locate it under LP's regular temporary
857c478bd9Sstevel@tonic-gate 	 * directory.
867c478bd9Sstevel@tonic-gate 	 */
877c478bd9Sstevel@tonic-gate 	} else if (strchr(file, '/')) {
887c478bd9Sstevel@tonic-gate 		if (!(path = makepath(Lp_Tmp, file, (char *)0)))
897c478bd9Sstevel@tonic-gate 			return (0);
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate 	/*
927c478bd9Sstevel@tonic-gate 	 * It must be a simple name. Locate this under the
937c478bd9Sstevel@tonic-gate 	 * special temporary directory that is linked to the
947c478bd9Sstevel@tonic-gate 	 * regular place for the local system.
957c478bd9Sstevel@tonic-gate 	 */
967c478bd9Sstevel@tonic-gate 	} else if (!(path = makepath(Lp_Temp, file, (char *)0)))
977c478bd9Sstevel@tonic-gate 		return (0);
98*2a8bcb4eSToomas Soome 
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate 	if ((fd = open_locked(path, "r", 0)) < 0) {
1017c478bd9Sstevel@tonic-gate 		Free (path);
1027c478bd9Sstevel@tonic-gate 		return (0);
1037c478bd9Sstevel@tonic-gate 	}
1047c478bd9Sstevel@tonic-gate 	Free (path);
1057c478bd9Sstevel@tonic-gate 
1060a44ef6dSjacobs 	reqp = calloc(sizeof (*reqp), 1);
1070a44ef6dSjacobs 	reqp->copies		= 1;
1080a44ef6dSjacobs 	reqp->priority		= -1;
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate 	errno = 0;
1117c478bd9Sstevel@tonic-gate 	while (fdgets(buf, BUFSIZ, fd)) {
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate 		buf[strlen(buf) - 1] = 0;
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate 		for (fld = 0; fld < RQ_MAX; fld++)
1167c478bd9Sstevel@tonic-gate 			if (
1177c478bd9Sstevel@tonic-gate 				reqheadings[fld].v
1187c478bd9Sstevel@tonic-gate 			     && reqheadings[fld].len
1197c478bd9Sstevel@tonic-gate 			     && STRNEQU(
1207c478bd9Sstevel@tonic-gate 					buf,
1217c478bd9Sstevel@tonic-gate 					reqheadings[fld].v,
1227c478bd9Sstevel@tonic-gate 					reqheadings[fld].len
1237c478bd9Sstevel@tonic-gate 				)
1247c478bd9Sstevel@tonic-gate 			) {
1257c478bd9Sstevel@tonic-gate 				p = buf + reqheadings[fld].len;
1267c478bd9Sstevel@tonic-gate 				break;
1277c478bd9Sstevel@tonic-gate 			}
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate 		/*
1307c478bd9Sstevel@tonic-gate 		 * To allow future extensions to not impact applications
1317c478bd9Sstevel@tonic-gate 		 * using old versions of this routine, ignore strange
1327c478bd9Sstevel@tonic-gate 		 * fields.
1337c478bd9Sstevel@tonic-gate 		 */
1347c478bd9Sstevel@tonic-gate 		if (fld >= RQ_MAX)
1357c478bd9Sstevel@tonic-gate 			continue;
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate 		switch (fld) {
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate 		case RQ_COPIES:
1400a44ef6dSjacobs 			reqp->copies = atoi(p);
1417c478bd9Sstevel@tonic-gate 			break;
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate 		case RQ_DEST:
1440a44ef6dSjacobs 			reqp->destination = Strdup(p);
1457c478bd9Sstevel@tonic-gate 			break;
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate 		case RQ_FILE:
1480a44ef6dSjacobs 			appendlist (&reqp->file_list, p);
1497c478bd9Sstevel@tonic-gate 			break;
1507c478bd9Sstevel@tonic-gate 
1517c478bd9Sstevel@tonic-gate 		case RQ_FORM:
1527c478bd9Sstevel@tonic-gate 			if (!STREQU(p, NAME_ANY))
1530a44ef6dSjacobs 				reqp->form = Strdup(p);
1547c478bd9Sstevel@tonic-gate 			break;
1557c478bd9Sstevel@tonic-gate 
1567c478bd9Sstevel@tonic-gate 		case RQ_HANDL:
1577c478bd9Sstevel@tonic-gate 			if (STREQU(p, NAME_RESUME))
1580a44ef6dSjacobs 				reqp->actions |= ACT_RESUME;
1597c478bd9Sstevel@tonic-gate 			else if (STREQU(p, NAME_HOLD))
1600a44ef6dSjacobs 				reqp->actions |= ACT_HOLD;
1617c478bd9Sstevel@tonic-gate 			else if (STREQU(p, NAME_IMMEDIATE))
1620a44ef6dSjacobs 				reqp->actions |= ACT_IMMEDIATE;
1637c478bd9Sstevel@tonic-gate 			break;
1647c478bd9Sstevel@tonic-gate 
1657c478bd9Sstevel@tonic-gate 		case RQ_NOTIFY:
1667c478bd9Sstevel@tonic-gate 			if (STREQU(p, "M"))
1670a44ef6dSjacobs 				reqp->actions |= ACT_MAIL;
1687c478bd9Sstevel@tonic-gate 			else if (STREQU(p, "W"))
1690a44ef6dSjacobs 				reqp->actions |= ACT_WRITE;
1707c478bd9Sstevel@tonic-gate 			else if (STREQU(p, "N"))
1710a44ef6dSjacobs 				reqp->actions |= ACT_NOTIFY;
1727c478bd9Sstevel@tonic-gate 			else
1730a44ef6dSjacobs 				reqp->alert = Strdup(p);
1747c478bd9Sstevel@tonic-gate 			break;
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate 		case RQ_OPTS:
1770a44ef6dSjacobs 			reqp->options = Strdup(p);
1787c478bd9Sstevel@tonic-gate 			break;
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate 		case RQ_PRIOR:
1810a44ef6dSjacobs 			reqp->priority = atoi(p);
1827c478bd9Sstevel@tonic-gate 			break;
1837c478bd9Sstevel@tonic-gate 
1847c478bd9Sstevel@tonic-gate 		case RQ_PAGES:
1850a44ef6dSjacobs 			reqp->pages = Strdup(p);
1867c478bd9Sstevel@tonic-gate 			break;
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate 		case RQ_CHARS:
1897c478bd9Sstevel@tonic-gate 			if (!STREQU(p, NAME_ANY))
1900a44ef6dSjacobs 				reqp->charset = Strdup(p);
1917c478bd9Sstevel@tonic-gate 			break;
1927c478bd9Sstevel@tonic-gate 
1937c478bd9Sstevel@tonic-gate 		case RQ_TITLE:
1940a44ef6dSjacobs 			reqp->title = Strdup(p);
1957c478bd9Sstevel@tonic-gate 			break;
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate 		case RQ_MODES:
1980a44ef6dSjacobs 			reqp->modes = Strdup(p);
1997c478bd9Sstevel@tonic-gate 			break;
2007c478bd9Sstevel@tonic-gate 
2017c478bd9Sstevel@tonic-gate 		case RQ_TYPE:
2020a44ef6dSjacobs 			reqp->input_type = Strdup(p);
2037c478bd9Sstevel@tonic-gate 			break;
2047c478bd9Sstevel@tonic-gate 
2057c478bd9Sstevel@tonic-gate 		case RQ_USER:
2060a44ef6dSjacobs 			reqp->user = Strdup(p);
2077c478bd9Sstevel@tonic-gate 			break;
2087c478bd9Sstevel@tonic-gate 
2097c478bd9Sstevel@tonic-gate 		case RQ_RAW:
2100a44ef6dSjacobs 			reqp->actions |= ACT_RAW;
2117c478bd9Sstevel@tonic-gate 			break;
2127c478bd9Sstevel@tonic-gate 
2137c478bd9Sstevel@tonic-gate 		case RQ_FAST:
2140a44ef6dSjacobs 			reqp->actions |= ACT_FAST;
2157c478bd9Sstevel@tonic-gate 			break;
2167c478bd9Sstevel@tonic-gate 
2177c478bd9Sstevel@tonic-gate 		case RQ_STAT:
2180a44ef6dSjacobs 			reqp->outcome = (ushort)strtol(p, (char **)0, 16);
2197c478bd9Sstevel@tonic-gate 			break;
2207c478bd9Sstevel@tonic-gate 
2217c478bd9Sstevel@tonic-gate 		}
2227c478bd9Sstevel@tonic-gate 
2237c478bd9Sstevel@tonic-gate 	}
2247c478bd9Sstevel@tonic-gate 	if (errno != 0) {
2257c478bd9Sstevel@tonic-gate 		int			save_errno = errno;
2267c478bd9Sstevel@tonic-gate 
2277c478bd9Sstevel@tonic-gate 		close(fd);
2287c478bd9Sstevel@tonic-gate 		errno = save_errno;
2297c478bd9Sstevel@tonic-gate 		return (0);
2307c478bd9Sstevel@tonic-gate 	}
2317c478bd9Sstevel@tonic-gate 	close(fd);
2327c478bd9Sstevel@tonic-gate 
2337c478bd9Sstevel@tonic-gate 	/*
2347c478bd9Sstevel@tonic-gate 	 * Now go through the structure and see if we have
2357c478bd9Sstevel@tonic-gate 	 * anything strange.
2367c478bd9Sstevel@tonic-gate 	 */
2377c478bd9Sstevel@tonic-gate 	if (
2380a44ef6dSjacobs 		reqp->copies <= 0
2390a44ef6dSjacobs 	     || !reqp->file_list || !*(reqp->file_list)
2400a44ef6dSjacobs 	     || reqp->priority < -1 || 39 < reqp->priority
2410a44ef6dSjacobs 	     || STREQU(reqp->input_type, NAME_ANY)
2420a44ef6dSjacobs 	     || STREQU(reqp->input_type, NAME_TERMINFO)
2437c478bd9Sstevel@tonic-gate 	) {
2440a44ef6dSjacobs 		freerequest (reqp);
2457c478bd9Sstevel@tonic-gate 		errno = EBADF;
2467c478bd9Sstevel@tonic-gate 		return (0);
2477c478bd9Sstevel@tonic-gate 	}
2487c478bd9Sstevel@tonic-gate 
2497c478bd9Sstevel@tonic-gate 	/*
2507c478bd9Sstevel@tonic-gate 	 * Guarantee some return values won't be null or empty.
2517c478bd9Sstevel@tonic-gate 	 */
2520a44ef6dSjacobs 	if (!reqp->destination || !*reqp->destination) {
2530a44ef6dSjacobs 		if (reqp->destination)
2540a44ef6dSjacobs 			Free (reqp->destination);
2550a44ef6dSjacobs 		reqp->destination = Strdup(NAME_ANY);
2567c478bd9Sstevel@tonic-gate 	}
2570a44ef6dSjacobs 	if (!reqp->input_type || !*reqp->input_type) {
2580a44ef6dSjacobs 		if (reqp->input_type)
2590a44ef6dSjacobs 			Free (reqp->input_type);
2600a44ef6dSjacobs 		reqp->input_type = Strdup(NAME_SIMPLE);
2617c478bd9Sstevel@tonic-gate 	}
2627c478bd9Sstevel@tonic-gate 
2630a44ef6dSjacobs 	return (reqp);
2647c478bd9Sstevel@tonic-gate }
265