xref: /illumos-gate/usr/src/cmd/lp/lib/secure/secure.c (revision 2a8bcb4efb45d99ac41c94a75c396b362c414f7f)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
545916cd2Sjpk  * Common Development and Distribution License (the "License").
645916cd2Sjpk  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
210a44ef6dSjacobs 
227c478bd9Sstevel@tonic-gate /*
23*f48205beScasper  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
287c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate /* EMACS_MODES: !fill, lnumb, !overwrite, !nodelete, !picture */
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate #include "string.h"
337c478bd9Sstevel@tonic-gate #include "sys/param.h"
347c478bd9Sstevel@tonic-gate #include "stdlib.h"
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate #include "lp.h"
377c478bd9Sstevel@tonic-gate #include "secure.h"
3845916cd2Sjpk #include <tsol/label.h>
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate /**
417c478bd9Sstevel@tonic-gate  ** getsecure() - EXTRACT SECURE REQUEST STRUCTURE FROM DISK FILE
427c478bd9Sstevel@tonic-gate  **/
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate SECURE *
getsecure(char * file)457c478bd9Sstevel@tonic-gate getsecure(char *file)
467c478bd9Sstevel@tonic-gate {
470a44ef6dSjacobs 	SECURE		*secp;
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate 	char			buf[BUFSIZ],
507c478bd9Sstevel@tonic-gate 				*path;
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate 	int fd;
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate 	int			fld;
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate 	if (*file == '/')
587c478bd9Sstevel@tonic-gate 		path = Strdup(file);
597c478bd9Sstevel@tonic-gate 	else
607c478bd9Sstevel@tonic-gate 		path = makepath(Lp_Requests, file, (char *)0);
617c478bd9Sstevel@tonic-gate 	if (!path)
627c478bd9Sstevel@tonic-gate 		return (0);
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate 	if ((fd = open_locked(path, "r", MODE_NOREAD)) < 0) {
657c478bd9Sstevel@tonic-gate 		Free (path);
667c478bd9Sstevel@tonic-gate 		return (0);
677c478bd9Sstevel@tonic-gate 	}
687c478bd9Sstevel@tonic-gate 	Free (path);
697c478bd9Sstevel@tonic-gate 
700a44ef6dSjacobs 	secp = calloc(sizeof (*secp), 1);
710a44ef6dSjacobs 
720a44ef6dSjacobs 	secp->user = 0;
737c478bd9Sstevel@tonic-gate 	errno = 0;
747c478bd9Sstevel@tonic-gate 	for (
757c478bd9Sstevel@tonic-gate 		fld = 0;
767c478bd9Sstevel@tonic-gate 		fld < SC_MAX && fdgets(buf, BUFSIZ, fd);
777c478bd9Sstevel@tonic-gate 		fld++
787c478bd9Sstevel@tonic-gate 	) {
797c478bd9Sstevel@tonic-gate 		buf[strlen(buf) - 1] = 0;
807c478bd9Sstevel@tonic-gate 		switch (fld) {
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate 		case SC_REQID:
830a44ef6dSjacobs 			secp->req_id = Strdup(buf);
847c478bd9Sstevel@tonic-gate 			break;
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate 		case SC_UID:
870a44ef6dSjacobs 			secp->uid = (uid_t)atol(buf);
887c478bd9Sstevel@tonic-gate 			break;
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate 		case SC_USER:
910a44ef6dSjacobs 			secp->user = Strdup(buf);
927c478bd9Sstevel@tonic-gate 			break;
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate 		case SC_GID:
950a44ef6dSjacobs 			secp->gid = (gid_t)atol(buf);
967c478bd9Sstevel@tonic-gate 			break;
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate 		case SC_SIZE:
990a44ef6dSjacobs 			secp->size = (size_t)atol(buf);
1007c478bd9Sstevel@tonic-gate 			break;
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate 		case SC_DATE:
1030a44ef6dSjacobs 			secp->date = (time_t)atol(buf);
1047c478bd9Sstevel@tonic-gate 			break;
10545916cd2Sjpk 
10645916cd2Sjpk 		case SC_SLABEL:
1070a44ef6dSjacobs 			secp->slabel = Strdup(buf);
10845916cd2Sjpk 			break;
1097c478bd9Sstevel@tonic-gate 		}
1107c478bd9Sstevel@tonic-gate 	}
1117c478bd9Sstevel@tonic-gate 	if (errno != 0 || fld != SC_MAX) {
1127c478bd9Sstevel@tonic-gate 		int			save_errno = errno;
1137c478bd9Sstevel@tonic-gate 
1140a44ef6dSjacobs 		freesecure (secp);
1157c478bd9Sstevel@tonic-gate 		close(fd);
1167c478bd9Sstevel@tonic-gate 		errno = save_errno;
1177c478bd9Sstevel@tonic-gate 		return (0);
1187c478bd9Sstevel@tonic-gate 	}
1197c478bd9Sstevel@tonic-gate 	close(fd);
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate 	/*
1227c478bd9Sstevel@tonic-gate 	 * Now go through the structure and see if we have
1237c478bd9Sstevel@tonic-gate 	 * anything strange.
1247c478bd9Sstevel@tonic-gate 	 */
1257c478bd9Sstevel@tonic-gate 	if (
126*f48205beScasper 	        secp->uid > MAXUID
1270a44ef6dSjacobs 	     || !secp->user
128*f48205beScasper 	     || secp->gid > MAXUID
1290a44ef6dSjacobs 	     || secp->size == 0
1300a44ef6dSjacobs 	     || secp->date <= 0
1317c478bd9Sstevel@tonic-gate 	) {
1320a44ef6dSjacobs 		freesecure (secp);
1337c478bd9Sstevel@tonic-gate 		errno = EBADF;
1347c478bd9Sstevel@tonic-gate 		return (0);
1357c478bd9Sstevel@tonic-gate 	}
1367c478bd9Sstevel@tonic-gate 
1370a44ef6dSjacobs 	return (secp);
1387c478bd9Sstevel@tonic-gate }
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate /**
1417c478bd9Sstevel@tonic-gate  ** putsecure() - WRITE SECURE REQUEST STRUCTURE TO DISK FILE
1427c478bd9Sstevel@tonic-gate  **/
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate int
putsecure(char * file,SECURE * secbufp)1457c478bd9Sstevel@tonic-gate putsecure(char *file, SECURE *secbufp)
1467c478bd9Sstevel@tonic-gate {
1477c478bd9Sstevel@tonic-gate 	char			*path;
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate 	int fd;
1507c478bd9Sstevel@tonic-gate 
1517c478bd9Sstevel@tonic-gate 	int			fld;
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate 	if (*file == '/')
1547c478bd9Sstevel@tonic-gate 		path = Strdup(file);
1557c478bd9Sstevel@tonic-gate 	else
1567c478bd9Sstevel@tonic-gate 		path = makepath(Lp_Requests, file, (char *)0);
1577c478bd9Sstevel@tonic-gate 	if (!path)
1587c478bd9Sstevel@tonic-gate 		return (-1);
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate 	if ((fd = open_locked(path, "w", MODE_NOREAD)) < 0) {
1617c478bd9Sstevel@tonic-gate 		Free (path);
1627c478bd9Sstevel@tonic-gate 		return (-1);
1637c478bd9Sstevel@tonic-gate 	}
1647c478bd9Sstevel@tonic-gate 	Free (path);
1657c478bd9Sstevel@tonic-gate 
1667c478bd9Sstevel@tonic-gate 	if (
1677c478bd9Sstevel@tonic-gate 		!secbufp->req_id ||
1687c478bd9Sstevel@tonic-gate 		!secbufp->user
1697c478bd9Sstevel@tonic-gate 	)
1707c478bd9Sstevel@tonic-gate 		return (-1);
1717c478bd9Sstevel@tonic-gate 
1727c478bd9Sstevel@tonic-gate 	for (fld = 0; fld < SC_MAX; fld++)
1737c478bd9Sstevel@tonic-gate 
1747c478bd9Sstevel@tonic-gate 		switch (fld) {
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate 		case SC_REQID:
1777c478bd9Sstevel@tonic-gate 			(void)fdprintf(fd, "%s\n", secbufp->req_id);
1787c478bd9Sstevel@tonic-gate 			break;
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate 		case SC_UID:
181*f48205beScasper 			(void)fdprintf(fd, "%u\n", secbufp->uid);
1827c478bd9Sstevel@tonic-gate 			break;
1837c478bd9Sstevel@tonic-gate 
1847c478bd9Sstevel@tonic-gate 		case SC_USER:
1857c478bd9Sstevel@tonic-gate 			(void)fdprintf(fd, "%s\n", secbufp->user);
1867c478bd9Sstevel@tonic-gate 			break;
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate 		case SC_GID:
189*f48205beScasper 			(void)fdprintf(fd, "%u\n", secbufp->gid);
1907c478bd9Sstevel@tonic-gate 			break;
1917c478bd9Sstevel@tonic-gate 
1927c478bd9Sstevel@tonic-gate 		case SC_SIZE:
1937c478bd9Sstevel@tonic-gate 			(void)fdprintf(fd, "%lu\n", secbufp->size);
1947c478bd9Sstevel@tonic-gate 			break;
1957c478bd9Sstevel@tonic-gate 
1967c478bd9Sstevel@tonic-gate 		case SC_DATE:
1977c478bd9Sstevel@tonic-gate 			(void)fdprintf(fd, "%ld\n", secbufp->date);
1987c478bd9Sstevel@tonic-gate 			break;
1997c478bd9Sstevel@tonic-gate 
20045916cd2Sjpk 		case SC_SLABEL:
20145916cd2Sjpk 			if (secbufp->slabel == NULL) {
20245916cd2Sjpk 				if (is_system_labeled()) {
20345916cd2Sjpk 					m_label_t *sl;
20445916cd2Sjpk 
20545916cd2Sjpk 					sl = m_label_alloc(MAC_LABEL);
20645916cd2Sjpk 					(void) getplabel(sl);
20745916cd2Sjpk 					if (label_to_str(sl, &(secbufp->slabel),
20845916cd2Sjpk 					    M_INTERNAL, DEF_NAMES) != 0) {
20945916cd2Sjpk 						perror("label_to_str");
21045916cd2Sjpk 						secbufp->slabel =
21145916cd2Sjpk 						    strdup("bad_label");
21245916cd2Sjpk 					}
21345916cd2Sjpk 					m_label_free(sl);
21445916cd2Sjpk 					(void) fdprintf(fd, "%s\n",
21545916cd2Sjpk 					    secbufp->slabel);
21645916cd2Sjpk 				} else {
21745916cd2Sjpk 					(void) fdprintf(fd, "none\n");
21845916cd2Sjpk 				}
21945916cd2Sjpk 			} else {
22045916cd2Sjpk 				(void) fdprintf(fd, "%s\n", secbufp->slabel);
22145916cd2Sjpk 			}
22245916cd2Sjpk 			break;
22345916cd2Sjpk 		}
2247c478bd9Sstevel@tonic-gate 	close(fd);
2257c478bd9Sstevel@tonic-gate 
2267c478bd9Sstevel@tonic-gate 	return (0);
2277c478bd9Sstevel@tonic-gate }
2287c478bd9Sstevel@tonic-gate 
2297c478bd9Sstevel@tonic-gate /*
2307c478bd9Sstevel@tonic-gate **  rmsecure ()
2317c478bd9Sstevel@tonic-gate **
2327c478bd9Sstevel@tonic-gate **	o  'reqfilep' is of the form 'node-name/request-file'
2337c478bd9Sstevel@tonic-gate **	   e.g. 'sfcalv/123-0'.
2347c478bd9Sstevel@tonic-gate */
2357c478bd9Sstevel@tonic-gate int
rmsecure(char * reqfilep)2367c478bd9Sstevel@tonic-gate rmsecure (char *reqfilep)
2377c478bd9Sstevel@tonic-gate {
2387c478bd9Sstevel@tonic-gate 	int	n;
2397c478bd9Sstevel@tonic-gate 	char *	pathp;
2407c478bd9Sstevel@tonic-gate 
2417c478bd9Sstevel@tonic-gate 	pathp = makepath (Lp_Requests, reqfilep, (char *) 0);
2427c478bd9Sstevel@tonic-gate 	if (! pathp)
2437c478bd9Sstevel@tonic-gate 		return	-1;
2447c478bd9Sstevel@tonic-gate 
2457c478bd9Sstevel@tonic-gate 	n = Unlink (pathp);
2467c478bd9Sstevel@tonic-gate 	Free (pathp);
2477c478bd9Sstevel@tonic-gate 
2487c478bd9Sstevel@tonic-gate 	return	n;
2497c478bd9Sstevel@tonic-gate }
2507c478bd9Sstevel@tonic-gate 
2517c478bd9Sstevel@tonic-gate /**
2527c478bd9Sstevel@tonic-gate  ** freesecure() - FREE A SECURE STRUCTURE
2537c478bd9Sstevel@tonic-gate  **/
2547c478bd9Sstevel@tonic-gate 
2557c478bd9Sstevel@tonic-gate void
freesecure(SECURE * secbufp)2567c478bd9Sstevel@tonic-gate freesecure(SECURE *secbufp)
2577c478bd9Sstevel@tonic-gate {
2587c478bd9Sstevel@tonic-gate 	if (!secbufp)
2597c478bd9Sstevel@tonic-gate 		return;
2607c478bd9Sstevel@tonic-gate 	if (secbufp->req_id)
2617c478bd9Sstevel@tonic-gate 		Free (secbufp->req_id);
2627c478bd9Sstevel@tonic-gate 	if (secbufp->user)
2637c478bd9Sstevel@tonic-gate 		Free (secbufp->user);
2640a44ef6dSjacobs 	Free (secbufp);
2650a44ef6dSjacobs 
2667c478bd9Sstevel@tonic-gate 	return;
2677c478bd9Sstevel@tonic-gate }
268