xref: /titanic_50/usr/src/cmd/lp/lib/secure/secure.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
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 
31 #pragma ident	"%Z%%M%	%I%	%E% SMI"	/* SVr4.0 1.14	*/
32 /* EMACS_MODES: !fill, lnumb, !overwrite, !nodelete, !picture */
33 
34 #include "string.h"
35 #include "sys/param.h"
36 #include "stdlib.h"
37 
38 #include "lp.h"
39 #include "secure.h"
40 
41 /**
42  ** getsecure() - EXTRACT SECURE REQUEST STRUCTURE FROM DISK FILE
43  **/
44 
45 SECURE *
46 getsecure(char *file)
47 {
48 	static SECURE		secbuf;
49 
50 	char			buf[BUFSIZ],
51 				*path;
52 
53 	int fd;
54 
55 	int			fld;
56 
57 
58 	if (*file == '/')
59 		path = Strdup(file);
60 	else
61 		path = makepath(Lp_Requests, file, (char *)0);
62 	if (!path)
63 		return (0);
64 
65 	if ((fd = open_locked(path, "r", MODE_NOREAD)) < 0) {
66 		Free (path);
67 		return (0);
68 	}
69 	Free (path);
70 
71 	secbuf.user = 0;
72 	errno = 0;
73 	for (
74 		fld = 0;
75 		fld < SC_MAX && fdgets(buf, BUFSIZ, fd);
76 		fld++
77 	) {
78 		buf[strlen(buf) - 1] = 0;
79 		switch (fld) {
80 
81 		case SC_REQID:
82 			secbuf.req_id = Strdup(buf);
83 			break;
84 
85 		case SC_UID:
86 			secbuf.uid = (uid_t)atol(buf);
87 			break;
88 
89 		case SC_USER:
90 			secbuf.user = Strdup(buf);
91 			break;
92 
93 		case SC_GID:
94 			secbuf.gid = (gid_t)atol(buf);
95 			break;
96 
97 		case SC_SIZE:
98 			secbuf.size = (size_t)atol(buf);
99 			break;
100 
101 		case SC_DATE:
102 			secbuf.date = (time_t)atol(buf);
103 			break;
104 
105 		case SC_SYSTEM:
106 			secbuf.system = Strdup(buf);
107 			break;
108 		}
109 	}
110 	if (errno != 0 || fld != SC_MAX) {
111 		int			save_errno = errno;
112 
113 		freesecure (&secbuf);
114 		close(fd);
115 		errno = save_errno;
116 		return (0);
117 	}
118 	close(fd);
119 
120 	/*
121 	 * Now go through the structure and see if we have
122 	 * anything strange.
123 	 */
124 	if (
125 	        secbuf.uid > MAXUID || secbuf.uid < -1
126 	     || !secbuf.user
127 	     || secbuf.gid > MAXUID || secbuf.gid < -1
128 	     || secbuf.size == 0
129 	     || secbuf.date <= 0
130 	) {
131 		freesecure (&secbuf);
132 		errno = EBADF;
133 		return (0);
134 	}
135 
136 	return (&secbuf);
137 }
138 
139 /**
140  ** putsecure() - WRITE SECURE REQUEST STRUCTURE TO DISK FILE
141  **/
142 
143 int
144 putsecure(char *file, SECURE *secbufp)
145 {
146 	char			*path;
147 
148 	int fd;
149 
150 	int			fld;
151 
152 	if (*file == '/')
153 		path = Strdup(file);
154 	else
155 		path = makepath(Lp_Requests, file, (char *)0);
156 	if (!path)
157 		return (-1);
158 
159 	if ((fd = open_locked(path, "w", MODE_NOREAD)) < 0) {
160 		Free (path);
161 		return (-1);
162 	}
163 	Free (path);
164 
165 	if (
166 		!secbufp->req_id ||
167 		!secbufp->user
168 	)
169 		return (-1);
170 
171 	for (fld = 0; fld < SC_MAX; fld++)
172 
173 		switch (fld) {
174 
175 		case SC_REQID:
176 			(void)fdprintf(fd, "%s\n", secbufp->req_id);
177 			break;
178 
179 		case SC_UID:
180 			(void)fdprintf(fd, "%ld\n", secbufp->uid);
181 			break;
182 
183 		case SC_USER:
184 			(void)fdprintf(fd, "%s\n", secbufp->user);
185 			break;
186 
187 		case SC_GID:
188 			(void)fdprintf(fd, "%ld\n", secbufp->gid);
189 			break;
190 
191 		case SC_SIZE:
192 			(void)fdprintf(fd, "%lu\n", secbufp->size);
193 			break;
194 
195 		case SC_DATE:
196 			(void)fdprintf(fd, "%ld\n", secbufp->date);
197 			break;
198 
199 		case SC_SYSTEM:
200 			(void)fdprintf(fd, "%s\n", secbufp->system);
201 			break;
202 		}
203 
204 	close(fd);
205 
206 	return (0);
207 }
208 
209 /*
210 **  rmsecure ()
211 **
212 **	o  'reqfilep' is of the form 'node-name/request-file'
213 **	   e.g. 'sfcalv/123-0'.
214 */
215 int
216 rmsecure (char *reqfilep)
217 {
218 	int	n;
219 	char *	pathp;
220 
221 	pathp = makepath (Lp_Requests, reqfilep, (char *) 0);
222 	if (! pathp)
223 		return	-1;
224 
225 	n = Unlink (pathp);
226 	Free (pathp);
227 
228 	return	n;
229 }
230 
231 /**
232  ** freesecure() - FREE A SECURE STRUCTURE
233  **/
234 
235 void
236 freesecure(SECURE *secbufp)
237 {
238 	if (!secbufp)
239 		return;
240 	if (secbufp->req_id)
241 		Free (secbufp->req_id);
242 	if (secbufp->user)
243 		Free (secbufp->user);
244 	if (secbufp->system)
245 		Free (secbufp->system);
246 	return;
247 }
248 
249