kill tsol ("Trusted Solaris") aka TX ("Trusted Extensions")
[unleashed.git] / usr / src / cmd / lp / lib / secure / secure.c
blob59b1404a8af16d95f82d387404d30201b536cdb0
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
23 * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
28 /* All Rights Reserved */
31 #pragma ident "%Z%%M% %I% %E% SMI"
32 /* EMACS_MODES: !fill, lnumb, !overwrite, !nodelete, !picture */
34 #include "string.h"
35 #include "sys/param.h"
36 #include "stdlib.h"
38 #include "lp.h"
39 #include "secure.h"
41 /**
42 ** getsecure() - EXTRACT SECURE REQUEST STRUCTURE FROM DISK FILE
43 **/
45 SECURE *
46 getsecure(char *file)
48 SECURE *secp;
50 char buf[BUFSIZ],
51 *path;
53 int fd;
55 int fld;
58 if (*file == '/')
59 path = Strdup(file);
60 else
61 path = makepath(Lp_Requests, file, (char *)0);
62 if (!path)
63 return (0);
65 if ((fd = open_locked(path, "r", MODE_NOREAD)) < 0) {
66 Free (path);
67 return (0);
69 Free (path);
71 secp = calloc(sizeof (*secp), 1);
73 secp->user = 0;
74 errno = 0;
75 for (
76 fld = 0;
77 fld < SC_MAX && fdgets(buf, BUFSIZ, fd);
78 fld++
79 ) {
80 buf[strlen(buf) - 1] = 0;
81 switch (fld) {
83 case SC_REQID:
84 secp->req_id = Strdup(buf);
85 break;
87 case SC_UID:
88 secp->uid = (uid_t)atol(buf);
89 break;
91 case SC_USER:
92 secp->user = Strdup(buf);
93 break;
95 case SC_GID:
96 secp->gid = (gid_t)atol(buf);
97 break;
99 case SC_SIZE:
100 secp->size = (size_t)atol(buf);
101 break;
103 case SC_DATE:
104 secp->date = (time_t)atol(buf);
105 break;
109 if (errno != 0 || fld != SC_MAX) {
110 int save_errno = errno;
112 freesecure (secp);
113 close(fd);
114 errno = save_errno;
115 return (0);
117 close(fd);
120 * Now go through the structure and see if we have
121 * anything strange.
123 if (
124 secp->uid > MAXUID
125 || !secp->user
126 || secp->gid > MAXUID
127 || secp->size == 0
128 || secp->date <= 0
130 freesecure (secp);
131 errno = EBADF;
132 return (0);
135 return (secp);
139 ** putsecure() - WRITE SECURE REQUEST STRUCTURE TO DISK FILE
143 putsecure(char *file, SECURE *secbufp)
145 char *path;
147 int fd;
149 int fld;
151 if (*file == '/')
152 path = Strdup(file);
153 else
154 path = makepath(Lp_Requests, file, (char *)0);
155 if (!path)
156 return (-1);
158 if ((fd = open_locked(path, "w", MODE_NOREAD)) < 0) {
159 Free (path);
160 return (-1);
162 Free (path);
164 if (
165 !secbufp->req_id ||
166 !secbufp->user
168 return (-1);
170 for (fld = 0; fld < SC_MAX; fld++)
172 switch (fld) {
174 case SC_REQID:
175 (void)fdprintf(fd, "%s\n", secbufp->req_id);
176 break;
178 case SC_UID:
179 (void)fdprintf(fd, "%u\n", secbufp->uid);
180 break;
182 case SC_USER:
183 (void)fdprintf(fd, "%s\n", secbufp->user);
184 break;
186 case SC_GID:
187 (void)fdprintf(fd, "%u\n", secbufp->gid);
188 break;
190 case SC_SIZE:
191 (void)fdprintf(fd, "%lu\n", secbufp->size);
192 break;
194 case SC_DATE:
195 (void)fdprintf(fd, "%ld\n", secbufp->date);
196 break;
198 close(fd);
200 return (0);
204 ** rmsecure ()
206 ** o 'reqfilep' is of the form 'node-name/request-file'
207 ** e.g. 'sfcalv/123-0'.
210 rmsecure (char *reqfilep)
212 int n;
213 char * pathp;
215 pathp = makepath (Lp_Requests, reqfilep, (char *) 0);
216 if (! pathp)
217 return -1;
219 n = Unlink (pathp);
220 Free (pathp);
222 return n;
226 ** freesecure() - FREE A SECURE STRUCTURE
229 void
230 freesecure(SECURE *secbufp)
232 if (!secbufp)
233 return;
234 if (secbufp->req_id)
235 Free (secbufp->req_id);
236 if (secbufp->user)
237 Free (secbufp->user);
238 Free (secbufp);
240 return;