xref: /illumos-gate/usr/src/cmd/lp/lib/forms/getform.c (revision 2a8bcb4efb45d99ac41c94a75c396b362c414f7f)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 1997 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
28 /*	  All Rights Reserved  	*/
29 
30 /* EMACS_MODES: !fill, lnumb, !overwrite, !nodelete, !picture */
31 
32 #include "stdio.h"
33 #include "string.h"
34 #include "errno.h"
35 #include "sys/types.h"
36 #include "stdlib.h"
37 
38 #include "lp.h"
39 #include "form.h"
40 
41 /*
42  * getform() - EXTRACT FORM STRUCTURE FROM DISK FILE
43  *
44  * The FILE **align_fp doesn't need to be changed for scalability, because
45  * it is always NULL when getform is called by lpsched.
46  */
47 int
getform(char * name,FORM * formp,FALERT * alertp,FILE ** align_fp)48 getform(char *name, FORM *formp, FALERT *alertp, FILE **align_fp)
49 {
50 	static long		lastdir		= -1;
51 
52 	int fd;
53 
54 	register char *		path;
55 
56 
57 	if (!name || !*name) {
58 		errno = EINVAL;
59 		return (-1);
60 	}
61 
62 	/*
63 	 * Getting ``all''? If so, jump into the directory
64 	 * wherever we left off.
65 	 */
66 	if (STREQU(NAME_ALL, name)) {
67 		if (!(name = next_dir(Lp_A_Forms, &lastdir)))
68 			return (-1);
69 	} else
70 		lastdir = -1;
71 
72 
73 	/*
74 	 * Get the form configuration information (?)
75 	 */
76 	if (formp) {
77 		path = getformfile(name, DESCRIBE);
78 		if (!path)
79 			return (-1);
80 		if ((fd = open_locked(path, "r", 0)) < 0) {
81 			Free (path);
82 			return (-1);
83 		}
84 		Free (path);
85 
86 		if (rdform(name, formp, fd, 0, (int *)0) == -1) {
87 			close(fd);
88 			return (-1);
89 		}
90 		close(fd);
91 	}
92 
93 	/*
94 	 * Get the alert information (?)
95 	 */
96 	if (alertp) {
97 
98 		FALERT *		pa = getalert(Lp_A_Forms, name);
99 
100 
101 		/*
102 		 * Don't fail if we can't read it because of access
103 		 * permission UNLESS we're "root" or "lp"
104 		 */
105 		if (!pa) {
106 
107 			if (errno == ENOENT) {
108 				alertp->shcmd = 0;
109 				alertp->Q = alertp->W = -1;
110 
111 			} else if (errno == ENOTDIR) {
112 				freeform (formp);
113 				errno = ENOENT;	/* form doesn't exist */
114 				return (-1);
115 
116 			} else if (
117 				errno != EACCES
118 			     || !getpid()		  /* we be root */
119 			     || STREQU(getname(), LPUSER) /* we be lp   */
120 			) {
121 				freeform (formp);
122 				return (-1);
123 			}
124 
125 		} else
126 			*alertp = *pa;
127 	}
128 
129 	/*
130 	 * Get the alignment pattern (?)
131 	 */
132 	if (align_fp) {
133 		path = getformfile(name, ALIGN_PTRN);
134 		if (!path) {
135 			freeform (formp);
136 			errno = ENOMEM;
137 			return (-1);
138 		}
139 		if (
140 			!(*align_fp = open_lpfile(path, "r", 0))
141 		     && errno != ENOENT
142 		) {
143 			Free (path);
144 			freeform (formp);
145 			return (-1);
146 		}
147 		Free (path);
148 	}
149 
150 	return (0);
151 }
152