xref: /illumos-gate/usr/src/lib/libbsm/common/getdevicerange.c (revision 7a6d80f1660abd4755c68cbd094d4a914681d26e)
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 (the "License").
6  * You may not use this file except in compliance with the License.
7  *
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.
12  *
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]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #include <stdlib.h>
28 #include <errno.h>
29 #include <tsol/label.h>
30 #include <bsm/devices.h>
31 
32 
33 /*
34  * getdevicerange
35  *	Gets the minimum and maximum labels within which the device can
36  *	be used. If label range is not specified for the device in
37  *	device_allocate, defaults to admin_low and admin_high.
38  *	Returns malloc'ed blrange pointer, or NULL on any error.
39  */
40 blrange_t *
41 getdevicerange(const char *dev)
42 {
43 	int		err;
44 	char		*lstr;
45 	devalloc_t	*da;
46 	devmap_t	*dm;
47 	blrange_t	*range;
48 
49 	errno = 0;
50 	if ((range = malloc(sizeof (blrange_t))) == NULL)
51 		return (NULL);
52 	if ((range->lower_bound = blabel_alloc()) == NULL) {
53 		free(range);
54 		return (NULL);
55 	}
56 	if ((range->upper_bound = blabel_alloc()) == NULL) {
57 		blabel_free(range->lower_bound);
58 		free(range);
59 		return (NULL);
60 	}
61 
62 	/*
63 	 * If an entry is found for the named device,
64 	 * return its label range.
65 	 */
66 	setdaent();
67 	if ((da = getdanam((char *)dev)) == NULL) {
68 		setdmapent();
69 		/* check for an actual device file */
70 		if ((dm = getdmapdev((char *)dev)) != NULL) {
71 			da = getdanam(dm->dmap_devname);
72 			freedmapent(dm);
73 		}
74 		enddmapent();
75 	}
76 	enddaent();
77 	if (da == NULL) {
78 		bsllow(range->lower_bound);
79 		bslhigh(range->upper_bound);
80 	} else {
81 		lstr = kva_match(da->da_devopts, DAOPT_MINLABEL);
82 		if (lstr == NULL) {
83 			bsllow(range->lower_bound);
84 		} else if (stobsl(lstr, range->lower_bound, NO_CORRECTION,
85 		    &err) == 0) {
86 			blabel_free(range->lower_bound);
87 			blabel_free(range->upper_bound);
88 			free(range);
89 			errno = ENOTSUP;
90 			return (NULL);
91 		}
92 		lstr = kva_match(da->da_devopts, DAOPT_MAXLABEL);
93 		if (lstr == NULL) {
94 			bslhigh(range->upper_bound);
95 		} else if (stobsl(lstr, range->upper_bound, NO_CORRECTION,
96 		    &err) == 0) {
97 			blabel_free(range->lower_bound);
98 			blabel_free(range->upper_bound);
99 			free(range);
100 			errno = ENOTSUP;
101 			return (NULL);
102 		}
103 		freedaent(da);
104 	}
105 
106 	return (range);
107 }
108