1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 1997 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
28*0Sstevel@tonic-gate /*	  All Rights Reserved  	*/
29*0Sstevel@tonic-gate 
30*0Sstevel@tonic-gate 
31*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"	/* SVr4.0 1.14	*/
32*0Sstevel@tonic-gate /* EMACS_MODES: !fill, lnumb, !overwrite, !nodelete, !picture */
33*0Sstevel@tonic-gate 
34*0Sstevel@tonic-gate #include "string.h"
35*0Sstevel@tonic-gate #include "sys/param.h"
36*0Sstevel@tonic-gate #include "stdlib.h"
37*0Sstevel@tonic-gate 
38*0Sstevel@tonic-gate #include "lp.h"
39*0Sstevel@tonic-gate #include "secure.h"
40*0Sstevel@tonic-gate 
41*0Sstevel@tonic-gate /**
42*0Sstevel@tonic-gate  ** getsecure() - EXTRACT SECURE REQUEST STRUCTURE FROM DISK FILE
43*0Sstevel@tonic-gate  **/
44*0Sstevel@tonic-gate 
45*0Sstevel@tonic-gate SECURE *
46*0Sstevel@tonic-gate getsecure(char *file)
47*0Sstevel@tonic-gate {
48*0Sstevel@tonic-gate 	static SECURE		secbuf;
49*0Sstevel@tonic-gate 
50*0Sstevel@tonic-gate 	char			buf[BUFSIZ],
51*0Sstevel@tonic-gate 				*path;
52*0Sstevel@tonic-gate 
53*0Sstevel@tonic-gate 	int fd;
54*0Sstevel@tonic-gate 
55*0Sstevel@tonic-gate 	int			fld;
56*0Sstevel@tonic-gate 
57*0Sstevel@tonic-gate 
58*0Sstevel@tonic-gate 	if (*file == '/')
59*0Sstevel@tonic-gate 		path = Strdup(file);
60*0Sstevel@tonic-gate 	else
61*0Sstevel@tonic-gate 		path = makepath(Lp_Requests, file, (char *)0);
62*0Sstevel@tonic-gate 	if (!path)
63*0Sstevel@tonic-gate 		return (0);
64*0Sstevel@tonic-gate 
65*0Sstevel@tonic-gate 	if ((fd = open_locked(path, "r", MODE_NOREAD)) < 0) {
66*0Sstevel@tonic-gate 		Free (path);
67*0Sstevel@tonic-gate 		return (0);
68*0Sstevel@tonic-gate 	}
69*0Sstevel@tonic-gate 	Free (path);
70*0Sstevel@tonic-gate 
71*0Sstevel@tonic-gate 	secbuf.user = 0;
72*0Sstevel@tonic-gate 	errno = 0;
73*0Sstevel@tonic-gate 	for (
74*0Sstevel@tonic-gate 		fld = 0;
75*0Sstevel@tonic-gate 		fld < SC_MAX && fdgets(buf, BUFSIZ, fd);
76*0Sstevel@tonic-gate 		fld++
77*0Sstevel@tonic-gate 	) {
78*0Sstevel@tonic-gate 		buf[strlen(buf) - 1] = 0;
79*0Sstevel@tonic-gate 		switch (fld) {
80*0Sstevel@tonic-gate 
81*0Sstevel@tonic-gate 		case SC_REQID:
82*0Sstevel@tonic-gate 			secbuf.req_id = Strdup(buf);
83*0Sstevel@tonic-gate 			break;
84*0Sstevel@tonic-gate 
85*0Sstevel@tonic-gate 		case SC_UID:
86*0Sstevel@tonic-gate 			secbuf.uid = (uid_t)atol(buf);
87*0Sstevel@tonic-gate 			break;
88*0Sstevel@tonic-gate 
89*0Sstevel@tonic-gate 		case SC_USER:
90*0Sstevel@tonic-gate 			secbuf.user = Strdup(buf);
91*0Sstevel@tonic-gate 			break;
92*0Sstevel@tonic-gate 
93*0Sstevel@tonic-gate 		case SC_GID:
94*0Sstevel@tonic-gate 			secbuf.gid = (gid_t)atol(buf);
95*0Sstevel@tonic-gate 			break;
96*0Sstevel@tonic-gate 
97*0Sstevel@tonic-gate 		case SC_SIZE:
98*0Sstevel@tonic-gate 			secbuf.size = (size_t)atol(buf);
99*0Sstevel@tonic-gate 			break;
100*0Sstevel@tonic-gate 
101*0Sstevel@tonic-gate 		case SC_DATE:
102*0Sstevel@tonic-gate 			secbuf.date = (time_t)atol(buf);
103*0Sstevel@tonic-gate 			break;
104*0Sstevel@tonic-gate 
105*0Sstevel@tonic-gate 		case SC_SYSTEM:
106*0Sstevel@tonic-gate 			secbuf.system = Strdup(buf);
107*0Sstevel@tonic-gate 			break;
108*0Sstevel@tonic-gate 		}
109*0Sstevel@tonic-gate 	}
110*0Sstevel@tonic-gate 	if (errno != 0 || fld != SC_MAX) {
111*0Sstevel@tonic-gate 		int			save_errno = errno;
112*0Sstevel@tonic-gate 
113*0Sstevel@tonic-gate 		freesecure (&secbuf);
114*0Sstevel@tonic-gate 		close(fd);
115*0Sstevel@tonic-gate 		errno = save_errno;
116*0Sstevel@tonic-gate 		return (0);
117*0Sstevel@tonic-gate 	}
118*0Sstevel@tonic-gate 	close(fd);
119*0Sstevel@tonic-gate 
120*0Sstevel@tonic-gate 	/*
121*0Sstevel@tonic-gate 	 * Now go through the structure and see if we have
122*0Sstevel@tonic-gate 	 * anything strange.
123*0Sstevel@tonic-gate 	 */
124*0Sstevel@tonic-gate 	if (
125*0Sstevel@tonic-gate 	        secbuf.uid > MAXUID || secbuf.uid < -1
126*0Sstevel@tonic-gate 	     || !secbuf.user
127*0Sstevel@tonic-gate 	     || secbuf.gid > MAXUID || secbuf.gid < -1
128*0Sstevel@tonic-gate 	     || secbuf.size == 0
129*0Sstevel@tonic-gate 	     || secbuf.date <= 0
130*0Sstevel@tonic-gate 	) {
131*0Sstevel@tonic-gate 		freesecure (&secbuf);
132*0Sstevel@tonic-gate 		errno = EBADF;
133*0Sstevel@tonic-gate 		return (0);
134*0Sstevel@tonic-gate 	}
135*0Sstevel@tonic-gate 
136*0Sstevel@tonic-gate 	return (&secbuf);
137*0Sstevel@tonic-gate }
138*0Sstevel@tonic-gate 
139*0Sstevel@tonic-gate /**
140*0Sstevel@tonic-gate  ** putsecure() - WRITE SECURE REQUEST STRUCTURE TO DISK FILE
141*0Sstevel@tonic-gate  **/
142*0Sstevel@tonic-gate 
143*0Sstevel@tonic-gate int
144*0Sstevel@tonic-gate putsecure(char *file, SECURE *secbufp)
145*0Sstevel@tonic-gate {
146*0Sstevel@tonic-gate 	char			*path;
147*0Sstevel@tonic-gate 
148*0Sstevel@tonic-gate 	int fd;
149*0Sstevel@tonic-gate 
150*0Sstevel@tonic-gate 	int			fld;
151*0Sstevel@tonic-gate 
152*0Sstevel@tonic-gate 	if (*file == '/')
153*0Sstevel@tonic-gate 		path = Strdup(file);
154*0Sstevel@tonic-gate 	else
155*0Sstevel@tonic-gate 		path = makepath(Lp_Requests, file, (char *)0);
156*0Sstevel@tonic-gate 	if (!path)
157*0Sstevel@tonic-gate 		return (-1);
158*0Sstevel@tonic-gate 
159*0Sstevel@tonic-gate 	if ((fd = open_locked(path, "w", MODE_NOREAD)) < 0) {
160*0Sstevel@tonic-gate 		Free (path);
161*0Sstevel@tonic-gate 		return (-1);
162*0Sstevel@tonic-gate 	}
163*0Sstevel@tonic-gate 	Free (path);
164*0Sstevel@tonic-gate 
165*0Sstevel@tonic-gate 	if (
166*0Sstevel@tonic-gate 		!secbufp->req_id ||
167*0Sstevel@tonic-gate 		!secbufp->user
168*0Sstevel@tonic-gate 	)
169*0Sstevel@tonic-gate 		return (-1);
170*0Sstevel@tonic-gate 
171*0Sstevel@tonic-gate 	for (fld = 0; fld < SC_MAX; fld++)
172*0Sstevel@tonic-gate 
173*0Sstevel@tonic-gate 		switch (fld) {
174*0Sstevel@tonic-gate 
175*0Sstevel@tonic-gate 		case SC_REQID:
176*0Sstevel@tonic-gate 			(void)fdprintf(fd, "%s\n", secbufp->req_id);
177*0Sstevel@tonic-gate 			break;
178*0Sstevel@tonic-gate 
179*0Sstevel@tonic-gate 		case SC_UID:
180*0Sstevel@tonic-gate 			(void)fdprintf(fd, "%ld\n", secbufp->uid);
181*0Sstevel@tonic-gate 			break;
182*0Sstevel@tonic-gate 
183*0Sstevel@tonic-gate 		case SC_USER:
184*0Sstevel@tonic-gate 			(void)fdprintf(fd, "%s\n", secbufp->user);
185*0Sstevel@tonic-gate 			break;
186*0Sstevel@tonic-gate 
187*0Sstevel@tonic-gate 		case SC_GID:
188*0Sstevel@tonic-gate 			(void)fdprintf(fd, "%ld\n", secbufp->gid);
189*0Sstevel@tonic-gate 			break;
190*0Sstevel@tonic-gate 
191*0Sstevel@tonic-gate 		case SC_SIZE:
192*0Sstevel@tonic-gate 			(void)fdprintf(fd, "%lu\n", secbufp->size);
193*0Sstevel@tonic-gate 			break;
194*0Sstevel@tonic-gate 
195*0Sstevel@tonic-gate 		case SC_DATE:
196*0Sstevel@tonic-gate 			(void)fdprintf(fd, "%ld\n", secbufp->date);
197*0Sstevel@tonic-gate 			break;
198*0Sstevel@tonic-gate 
199*0Sstevel@tonic-gate 		case SC_SYSTEM:
200*0Sstevel@tonic-gate 			(void)fdprintf(fd, "%s\n", secbufp->system);
201*0Sstevel@tonic-gate 			break;
202*0Sstevel@tonic-gate 		}
203*0Sstevel@tonic-gate 
204*0Sstevel@tonic-gate 	close(fd);
205*0Sstevel@tonic-gate 
206*0Sstevel@tonic-gate 	return (0);
207*0Sstevel@tonic-gate }
208*0Sstevel@tonic-gate 
209*0Sstevel@tonic-gate /*
210*0Sstevel@tonic-gate **  rmsecure ()
211*0Sstevel@tonic-gate **
212*0Sstevel@tonic-gate **	o  'reqfilep' is of the form 'node-name/request-file'
213*0Sstevel@tonic-gate **	   e.g. 'sfcalv/123-0'.
214*0Sstevel@tonic-gate */
215*0Sstevel@tonic-gate int
216*0Sstevel@tonic-gate rmsecure (char *reqfilep)
217*0Sstevel@tonic-gate {
218*0Sstevel@tonic-gate 	int	n;
219*0Sstevel@tonic-gate 	char *	pathp;
220*0Sstevel@tonic-gate 
221*0Sstevel@tonic-gate 	pathp = makepath (Lp_Requests, reqfilep, (char *) 0);
222*0Sstevel@tonic-gate 	if (! pathp)
223*0Sstevel@tonic-gate 		return	-1;
224*0Sstevel@tonic-gate 
225*0Sstevel@tonic-gate 	n = Unlink (pathp);
226*0Sstevel@tonic-gate 	Free (pathp);
227*0Sstevel@tonic-gate 
228*0Sstevel@tonic-gate 	return	n;
229*0Sstevel@tonic-gate }
230*0Sstevel@tonic-gate 
231*0Sstevel@tonic-gate /**
232*0Sstevel@tonic-gate  ** freesecure() - FREE A SECURE STRUCTURE
233*0Sstevel@tonic-gate  **/
234*0Sstevel@tonic-gate 
235*0Sstevel@tonic-gate void
236*0Sstevel@tonic-gate freesecure(SECURE *secbufp)
237*0Sstevel@tonic-gate {
238*0Sstevel@tonic-gate 	if (!secbufp)
239*0Sstevel@tonic-gate 		return;
240*0Sstevel@tonic-gate 	if (secbufp->req_id)
241*0Sstevel@tonic-gate 		Free (secbufp->req_id);
242*0Sstevel@tonic-gate 	if (secbufp->user)
243*0Sstevel@tonic-gate 		Free (secbufp->user);
244*0Sstevel@tonic-gate 	if (secbufp->system)
245*0Sstevel@tonic-gate 		Free (secbufp->system);
246*0Sstevel@tonic-gate 	return;
247*0Sstevel@tonic-gate }
248*0Sstevel@tonic-gate 
249