xref: /onnv-gate/usr/src/cmd/lp/lib/secure/secure.c (revision 4321:a8930ec16e52)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
51676Sjpk  * Common Development and Distribution License (the "License").
61676Sjpk  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
213125Sjacobs 
220Sstevel@tonic-gate /*
23*4321Scasper  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
280Sstevel@tonic-gate /*	  All Rights Reserved  	*/
290Sstevel@tonic-gate 
300Sstevel@tonic-gate 
313125Sjacobs #pragma ident	"%Z%%M%	%I%	%E% SMI"
320Sstevel@tonic-gate /* EMACS_MODES: !fill, lnumb, !overwrite, !nodelete, !picture */
330Sstevel@tonic-gate 
340Sstevel@tonic-gate #include "string.h"
350Sstevel@tonic-gate #include "sys/param.h"
360Sstevel@tonic-gate #include "stdlib.h"
370Sstevel@tonic-gate 
380Sstevel@tonic-gate #include "lp.h"
390Sstevel@tonic-gate #include "secure.h"
401676Sjpk #include <tsol/label.h>
410Sstevel@tonic-gate 
420Sstevel@tonic-gate /**
430Sstevel@tonic-gate  ** getsecure() - EXTRACT SECURE REQUEST STRUCTURE FROM DISK FILE
440Sstevel@tonic-gate  **/
450Sstevel@tonic-gate 
460Sstevel@tonic-gate SECURE *
getsecure(char * file)470Sstevel@tonic-gate getsecure(char *file)
480Sstevel@tonic-gate {
493125Sjacobs 	SECURE		*secp;
500Sstevel@tonic-gate 
510Sstevel@tonic-gate 	char			buf[BUFSIZ],
520Sstevel@tonic-gate 				*path;
530Sstevel@tonic-gate 
540Sstevel@tonic-gate 	int fd;
550Sstevel@tonic-gate 
560Sstevel@tonic-gate 	int			fld;
570Sstevel@tonic-gate 
580Sstevel@tonic-gate 
590Sstevel@tonic-gate 	if (*file == '/')
600Sstevel@tonic-gate 		path = Strdup(file);
610Sstevel@tonic-gate 	else
620Sstevel@tonic-gate 		path = makepath(Lp_Requests, file, (char *)0);
630Sstevel@tonic-gate 	if (!path)
640Sstevel@tonic-gate 		return (0);
650Sstevel@tonic-gate 
660Sstevel@tonic-gate 	if ((fd = open_locked(path, "r", MODE_NOREAD)) < 0) {
670Sstevel@tonic-gate 		Free (path);
680Sstevel@tonic-gate 		return (0);
690Sstevel@tonic-gate 	}
700Sstevel@tonic-gate 	Free (path);
710Sstevel@tonic-gate 
723125Sjacobs 	secp = calloc(sizeof (*secp), 1);
733125Sjacobs 
743125Sjacobs 	secp->user = 0;
750Sstevel@tonic-gate 	errno = 0;
760Sstevel@tonic-gate 	for (
770Sstevel@tonic-gate 		fld = 0;
780Sstevel@tonic-gate 		fld < SC_MAX && fdgets(buf, BUFSIZ, fd);
790Sstevel@tonic-gate 		fld++
800Sstevel@tonic-gate 	) {
810Sstevel@tonic-gate 		buf[strlen(buf) - 1] = 0;
820Sstevel@tonic-gate 		switch (fld) {
830Sstevel@tonic-gate 
840Sstevel@tonic-gate 		case SC_REQID:
853125Sjacobs 			secp->req_id = Strdup(buf);
860Sstevel@tonic-gate 			break;
870Sstevel@tonic-gate 
880Sstevel@tonic-gate 		case SC_UID:
893125Sjacobs 			secp->uid = (uid_t)atol(buf);
900Sstevel@tonic-gate 			break;
910Sstevel@tonic-gate 
920Sstevel@tonic-gate 		case SC_USER:
933125Sjacobs 			secp->user = Strdup(buf);
940Sstevel@tonic-gate 			break;
950Sstevel@tonic-gate 
960Sstevel@tonic-gate 		case SC_GID:
973125Sjacobs 			secp->gid = (gid_t)atol(buf);
980Sstevel@tonic-gate 			break;
990Sstevel@tonic-gate 
1000Sstevel@tonic-gate 		case SC_SIZE:
1013125Sjacobs 			secp->size = (size_t)atol(buf);
1020Sstevel@tonic-gate 			break;
1030Sstevel@tonic-gate 
1040Sstevel@tonic-gate 		case SC_DATE:
1053125Sjacobs 			secp->date = (time_t)atol(buf);
1060Sstevel@tonic-gate 			break;
1071676Sjpk 
1081676Sjpk 		case SC_SLABEL:
1093125Sjacobs 			secp->slabel = Strdup(buf);
1101676Sjpk 			break;
1110Sstevel@tonic-gate 		}
1120Sstevel@tonic-gate 	}
1130Sstevel@tonic-gate 	if (errno != 0 || fld != SC_MAX) {
1140Sstevel@tonic-gate 		int			save_errno = errno;
1150Sstevel@tonic-gate 
1163125Sjacobs 		freesecure (secp);
1170Sstevel@tonic-gate 		close(fd);
1180Sstevel@tonic-gate 		errno = save_errno;
1190Sstevel@tonic-gate 		return (0);
1200Sstevel@tonic-gate 	}
1210Sstevel@tonic-gate 	close(fd);
1220Sstevel@tonic-gate 
1230Sstevel@tonic-gate 	/*
1240Sstevel@tonic-gate 	 * Now go through the structure and see if we have
1250Sstevel@tonic-gate 	 * anything strange.
1260Sstevel@tonic-gate 	 */
1270Sstevel@tonic-gate 	if (
128*4321Scasper 	        secp->uid > MAXUID
1293125Sjacobs 	     || !secp->user
130*4321Scasper 	     || secp->gid > MAXUID
1313125Sjacobs 	     || secp->size == 0
1323125Sjacobs 	     || secp->date <= 0
1330Sstevel@tonic-gate 	) {
1343125Sjacobs 		freesecure (secp);
1350Sstevel@tonic-gate 		errno = EBADF;
1360Sstevel@tonic-gate 		return (0);
1370Sstevel@tonic-gate 	}
1380Sstevel@tonic-gate 
1393125Sjacobs 	return (secp);
1400Sstevel@tonic-gate }
1410Sstevel@tonic-gate 
1420Sstevel@tonic-gate /**
1430Sstevel@tonic-gate  ** putsecure() - WRITE SECURE REQUEST STRUCTURE TO DISK FILE
1440Sstevel@tonic-gate  **/
1450Sstevel@tonic-gate 
1460Sstevel@tonic-gate int
putsecure(char * file,SECURE * secbufp)1470Sstevel@tonic-gate putsecure(char *file, SECURE *secbufp)
1480Sstevel@tonic-gate {
1490Sstevel@tonic-gate 	char			*path;
1500Sstevel@tonic-gate 
1510Sstevel@tonic-gate 	int fd;
1520Sstevel@tonic-gate 
1530Sstevel@tonic-gate 	int			fld;
1540Sstevel@tonic-gate 
1550Sstevel@tonic-gate 	if (*file == '/')
1560Sstevel@tonic-gate 		path = Strdup(file);
1570Sstevel@tonic-gate 	else
1580Sstevel@tonic-gate 		path = makepath(Lp_Requests, file, (char *)0);
1590Sstevel@tonic-gate 	if (!path)
1600Sstevel@tonic-gate 		return (-1);
1610Sstevel@tonic-gate 
1620Sstevel@tonic-gate 	if ((fd = open_locked(path, "w", MODE_NOREAD)) < 0) {
1630Sstevel@tonic-gate 		Free (path);
1640Sstevel@tonic-gate 		return (-1);
1650Sstevel@tonic-gate 	}
1660Sstevel@tonic-gate 	Free (path);
1670Sstevel@tonic-gate 
1680Sstevel@tonic-gate 	if (
1690Sstevel@tonic-gate 		!secbufp->req_id ||
1700Sstevel@tonic-gate 		!secbufp->user
1710Sstevel@tonic-gate 	)
1720Sstevel@tonic-gate 		return (-1);
1730Sstevel@tonic-gate 
1740Sstevel@tonic-gate 	for (fld = 0; fld < SC_MAX; fld++)
1750Sstevel@tonic-gate 
1760Sstevel@tonic-gate 		switch (fld) {
1770Sstevel@tonic-gate 
1780Sstevel@tonic-gate 		case SC_REQID:
1790Sstevel@tonic-gate 			(void)fdprintf(fd, "%s\n", secbufp->req_id);
1800Sstevel@tonic-gate 			break;
1810Sstevel@tonic-gate 
1820Sstevel@tonic-gate 		case SC_UID:
183*4321Scasper 			(void)fdprintf(fd, "%u\n", secbufp->uid);
1840Sstevel@tonic-gate 			break;
1850Sstevel@tonic-gate 
1860Sstevel@tonic-gate 		case SC_USER:
1870Sstevel@tonic-gate 			(void)fdprintf(fd, "%s\n", secbufp->user);
1880Sstevel@tonic-gate 			break;
1890Sstevel@tonic-gate 
1900Sstevel@tonic-gate 		case SC_GID:
191*4321Scasper 			(void)fdprintf(fd, "%u\n", secbufp->gid);
1920Sstevel@tonic-gate 			break;
1930Sstevel@tonic-gate 
1940Sstevel@tonic-gate 		case SC_SIZE:
1950Sstevel@tonic-gate 			(void)fdprintf(fd, "%lu\n", secbufp->size);
1960Sstevel@tonic-gate 			break;
1970Sstevel@tonic-gate 
1980Sstevel@tonic-gate 		case SC_DATE:
1990Sstevel@tonic-gate 			(void)fdprintf(fd, "%ld\n", secbufp->date);
2000Sstevel@tonic-gate 			break;
2010Sstevel@tonic-gate 
2021676Sjpk 		case SC_SLABEL:
2031676Sjpk 			if (secbufp->slabel == NULL) {
2041676Sjpk 				if (is_system_labeled()) {
2051676Sjpk 					m_label_t *sl;
2061676Sjpk 
2071676Sjpk 					sl = m_label_alloc(MAC_LABEL);
2081676Sjpk 					(void) getplabel(sl);
2091676Sjpk 					if (label_to_str(sl, &(secbufp->slabel),
2101676Sjpk 					    M_INTERNAL, DEF_NAMES) != 0) {
2111676Sjpk 						perror("label_to_str");
2121676Sjpk 						secbufp->slabel =
2131676Sjpk 						    strdup("bad_label");
2141676Sjpk 					}
2151676Sjpk 					m_label_free(sl);
2161676Sjpk 					(void) fdprintf(fd, "%s\n",
2171676Sjpk 					    secbufp->slabel);
2181676Sjpk 				} else {
2191676Sjpk 					(void) fdprintf(fd, "none\n");
2201676Sjpk 				}
2211676Sjpk 			} else {
2221676Sjpk 				(void) fdprintf(fd, "%s\n", secbufp->slabel);
2231676Sjpk 			}
2241676Sjpk 			break;
2250Sstevel@tonic-gate 		}
2260Sstevel@tonic-gate 	close(fd);
2270Sstevel@tonic-gate 
2280Sstevel@tonic-gate 	return (0);
2290Sstevel@tonic-gate }
2300Sstevel@tonic-gate 
2310Sstevel@tonic-gate /*
2320Sstevel@tonic-gate **  rmsecure ()
2330Sstevel@tonic-gate **
2340Sstevel@tonic-gate **	o  'reqfilep' is of the form 'node-name/request-file'
2350Sstevel@tonic-gate **	   e.g. 'sfcalv/123-0'.
2360Sstevel@tonic-gate */
2370Sstevel@tonic-gate int
rmsecure(char * reqfilep)2380Sstevel@tonic-gate rmsecure (char *reqfilep)
2390Sstevel@tonic-gate {
2400Sstevel@tonic-gate 	int	n;
2410Sstevel@tonic-gate 	char *	pathp;
2420Sstevel@tonic-gate 
2430Sstevel@tonic-gate 	pathp = makepath (Lp_Requests, reqfilep, (char *) 0);
2440Sstevel@tonic-gate 	if (! pathp)
2450Sstevel@tonic-gate 		return	-1;
2460Sstevel@tonic-gate 
2470Sstevel@tonic-gate 	n = Unlink (pathp);
2480Sstevel@tonic-gate 	Free (pathp);
2490Sstevel@tonic-gate 
2500Sstevel@tonic-gate 	return	n;
2510Sstevel@tonic-gate }
2520Sstevel@tonic-gate 
2530Sstevel@tonic-gate /**
2540Sstevel@tonic-gate  ** freesecure() - FREE A SECURE STRUCTURE
2550Sstevel@tonic-gate  **/
2560Sstevel@tonic-gate 
2570Sstevel@tonic-gate void
freesecure(SECURE * secbufp)2580Sstevel@tonic-gate freesecure(SECURE *secbufp)
2590Sstevel@tonic-gate {
2600Sstevel@tonic-gate 	if (!secbufp)
2610Sstevel@tonic-gate 		return;
2620Sstevel@tonic-gate 	if (secbufp->req_id)
2630Sstevel@tonic-gate 		Free (secbufp->req_id);
2640Sstevel@tonic-gate 	if (secbufp->user)
2650Sstevel@tonic-gate 		Free (secbufp->user);
2663125Sjacobs 	Free (secbufp);
2673125Sjacobs 
2680Sstevel@tonic-gate 	return;
2690Sstevel@tonic-gate }
270