xref: /titanic_50/usr/src/lib/libbsm/common/getdment.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate #include <stdio.h>
30*7c478bd9Sstevel@tonic-gate #include <string.h>
31*7c478bd9Sstevel@tonic-gate #include <malloc.h>
32*7c478bd9Sstevel@tonic-gate #include <bsm/devices.h>
33*7c478bd9Sstevel@tonic-gate #include <sys/errno.h>
34*7c478bd9Sstevel@tonic-gate 
35*7c478bd9Sstevel@tonic-gate #define	MAXINT 0x7fffffff;
36*7c478bd9Sstevel@tonic-gate 
37*7c478bd9Sstevel@tonic-gate static struct _dmapbuff {
38*7c478bd9Sstevel@tonic-gate 	devmap_t _NULLDM;
39*7c478bd9Sstevel@tonic-gate 	FILE *_dmapf;	/* pointer into /etc/security/device_maps */
40*7c478bd9Sstevel@tonic-gate 	devmap_t _interpdevmap;
41*7c478bd9Sstevel@tonic-gate 	char _interpline[BUFSIZ + 1];
42*7c478bd9Sstevel@tonic-gate 	char *_DEVMAP;
43*7c478bd9Sstevel@tonic-gate } *__dmapbuff;
44*7c478bd9Sstevel@tonic-gate 
45*7c478bd9Sstevel@tonic-gate #define	NULLDM (_dmap->_NULLDM)
46*7c478bd9Sstevel@tonic-gate #define	dmapf (_dmap->_dmapf)
47*7c478bd9Sstevel@tonic-gate #define	interpdevmap (_dmap->_interpdevmap)
48*7c478bd9Sstevel@tonic-gate #define	interpline (_dmap->_interpline)
49*7c478bd9Sstevel@tonic-gate #define	DEVMAP (_dmap->_DEVMAP)
50*7c478bd9Sstevel@tonic-gate static devmap_t  *interpret();
51*7c478bd9Sstevel@tonic-gate static int matchdev();
52*7c478bd9Sstevel@tonic-gate static int matchname();
53*7c478bd9Sstevel@tonic-gate /*
54*7c478bd9Sstevel@tonic-gate  * trim_white(ptr) trims off leading and trailing white space from a NULL
55*7c478bd9Sstevel@tonic-gate  * terminated string pointed to by "ptr". The leading white space is skipped
56*7c478bd9Sstevel@tonic-gate  * by moving the pointer forward. The trailing white space is removed by
57*7c478bd9Sstevel@tonic-gate  * nulling the white space characters.  The pointer is returned to the white
58*7c478bd9Sstevel@tonic-gate  * string. If the resulting string is null in length then a NULL pointer is
59*7c478bd9Sstevel@tonic-gate  * returned. If "ptr" is NULL then a NULL pointer is returned.
60*7c478bd9Sstevel@tonic-gate  */
61*7c478bd9Sstevel@tonic-gate static char *
62*7c478bd9Sstevel@tonic-gate trim_white(char *ptr)
63*7c478bd9Sstevel@tonic-gate {
64*7c478bd9Sstevel@tonic-gate 	register char  *tptr;
65*7c478bd9Sstevel@tonic-gate 	register int    cnt;
66*7c478bd9Sstevel@tonic-gate 	if (ptr == NULL)
67*7c478bd9Sstevel@tonic-gate 		return (NULL);
68*7c478bd9Sstevel@tonic-gate 	while ((*ptr == ' ') || (*ptr == '\t')) {
69*7c478bd9Sstevel@tonic-gate 		ptr++;
70*7c478bd9Sstevel@tonic-gate 	}
71*7c478bd9Sstevel@tonic-gate 	cnt = strlen(ptr);
72*7c478bd9Sstevel@tonic-gate 	if (cnt != 0) {
73*7c478bd9Sstevel@tonic-gate 		tptr = ptr + cnt - 1;
74*7c478bd9Sstevel@tonic-gate 		while ((*tptr == ' ') || (*tptr == '\t')) {
75*7c478bd9Sstevel@tonic-gate 			*tptr = '\0';
76*7c478bd9Sstevel@tonic-gate 			tptr--;
77*7c478bd9Sstevel@tonic-gate 		}
78*7c478bd9Sstevel@tonic-gate 	}
79*7c478bd9Sstevel@tonic-gate 	if (*ptr == NULL)
80*7c478bd9Sstevel@tonic-gate 		return (NULL);
81*7c478bd9Sstevel@tonic-gate 	return (ptr);
82*7c478bd9Sstevel@tonic-gate }
83*7c478bd9Sstevel@tonic-gate /*
84*7c478bd9Sstevel@tonic-gate  * scan string pointed to by pointer "p"
85*7c478bd9Sstevel@tonic-gate  * find next colin or end of line. Null it and
86*7c478bd9Sstevel@tonic-gate  * return pointer to next char.
87*7c478bd9Sstevel@tonic-gate  */
88*7c478bd9Sstevel@tonic-gate static char *
89*7c478bd9Sstevel@tonic-gate dmapskip(register char *p)
90*7c478bd9Sstevel@tonic-gate {
91*7c478bd9Sstevel@tonic-gate 	while (*p && *p != ':' && *p != '\n')
92*7c478bd9Sstevel@tonic-gate 		++p;
93*7c478bd9Sstevel@tonic-gate 	if (*p == '\n')
94*7c478bd9Sstevel@tonic-gate 		*p = '\0';
95*7c478bd9Sstevel@tonic-gate 	else if (*p != '\0')
96*7c478bd9Sstevel@tonic-gate 		*p++ = '\0';
97*7c478bd9Sstevel@tonic-gate 	return (p);
98*7c478bd9Sstevel@tonic-gate }
99*7c478bd9Sstevel@tonic-gate /*
100*7c478bd9Sstevel@tonic-gate  * scan string pointed to by pointer "p"
101*7c478bd9Sstevel@tonic-gate  * find next colin or end of line. Null it and
102*7c478bd9Sstevel@tonic-gate  * return pointer to next char.
103*7c478bd9Sstevel@tonic-gate  */
104*7c478bd9Sstevel@tonic-gate static char *
105*7c478bd9Sstevel@tonic-gate dmapdskip(register char *p)
106*7c478bd9Sstevel@tonic-gate {
107*7c478bd9Sstevel@tonic-gate 	while (*p && *p != ' ' && *p != '\n')
108*7c478bd9Sstevel@tonic-gate 		++p;
109*7c478bd9Sstevel@tonic-gate 	if (*p != '\0')
110*7c478bd9Sstevel@tonic-gate 		*p++ = '\0';
111*7c478bd9Sstevel@tonic-gate 	return (p);
112*7c478bd9Sstevel@tonic-gate }
113*7c478bd9Sstevel@tonic-gate 
114*7c478bd9Sstevel@tonic-gate /*
115*7c478bd9Sstevel@tonic-gate  * _dmapalloc() allocates common buffers and structures used by the device
116*7c478bd9Sstevel@tonic-gate  * maps library routines. Then returns a pointer to a structure.  The
117*7c478bd9Sstevel@tonic-gate  * returned pointer will be null if there is an error condition.
118*7c478bd9Sstevel@tonic-gate  */
119*7c478bd9Sstevel@tonic-gate static struct _dmapbuff *
120*7c478bd9Sstevel@tonic-gate _dmapalloc(void)
121*7c478bd9Sstevel@tonic-gate {
122*7c478bd9Sstevel@tonic-gate 	register struct _dmapbuff *_dmap = __dmapbuff;
123*7c478bd9Sstevel@tonic-gate 
124*7c478bd9Sstevel@tonic-gate 	if (_dmap == 0) {
125*7c478bd9Sstevel@tonic-gate 		_dmap = (struct _dmapbuff *)
126*7c478bd9Sstevel@tonic-gate 			calloc((unsigned)1, (unsigned)sizeof (*__dmapbuff));
127*7c478bd9Sstevel@tonic-gate 		if (_dmap == 0)
128*7c478bd9Sstevel@tonic-gate 			return (0);
129*7c478bd9Sstevel@tonic-gate 		DEVMAP = "/etc/security/device_maps";
130*7c478bd9Sstevel@tonic-gate 		__dmapbuff = _dmap;
131*7c478bd9Sstevel@tonic-gate 	}
132*7c478bd9Sstevel@tonic-gate 	return (__dmapbuff);
133*7c478bd9Sstevel@tonic-gate }
134*7c478bd9Sstevel@tonic-gate /*
135*7c478bd9Sstevel@tonic-gate  * getdmapline(buff,len,stream) reads one device maps line from "stream" into
136*7c478bd9Sstevel@tonic-gate  * "buff" on "len" bytes.  Continued lines from "stream" are concatinated
137*7c478bd9Sstevel@tonic-gate  * into one line in "buff". Comments are removed from "buff". The number of
138*7c478bd9Sstevel@tonic-gate  * characters in "buff" is returned.  If no characters are read or an error
139*7c478bd9Sstevel@tonic-gate  * occured then "0" is returned
140*7c478bd9Sstevel@tonic-gate  */
141*7c478bd9Sstevel@tonic-gate static int
142*7c478bd9Sstevel@tonic-gate getdmapline(char *buff, int len, FILE *stream)
143*7c478bd9Sstevel@tonic-gate {
144*7c478bd9Sstevel@tonic-gate 	register struct _dmapbuff *_dmap = _dmapalloc();
145*7c478bd9Sstevel@tonic-gate 	char *cp;
146*7c478bd9Sstevel@tonic-gate 	char *ccp;
147*7c478bd9Sstevel@tonic-gate 	size_t tmpcnt;
148*7c478bd9Sstevel@tonic-gate 	int charcnt = 0;
149*7c478bd9Sstevel@tonic-gate 	int fileerr = 0;
150*7c478bd9Sstevel@tonic-gate 	int contline;
151*7c478bd9Sstevel@tonic-gate 	if (_dmap == 0)
152*7c478bd9Sstevel@tonic-gate 		return (0);
153*7c478bd9Sstevel@tonic-gate 	do {
154*7c478bd9Sstevel@tonic-gate 		cp = buff;
155*7c478bd9Sstevel@tonic-gate 		*cp = NULL;
156*7c478bd9Sstevel@tonic-gate 		do {
157*7c478bd9Sstevel@tonic-gate 			if ((len - charcnt <= 1) ||
158*7c478bd9Sstevel@tonic-gate 			    (fgets(cp, len - charcnt, stream) == NULL)) {
159*7c478bd9Sstevel@tonic-gate 				fileerr = 1;
160*7c478bd9Sstevel@tonic-gate 				break;
161*7c478bd9Sstevel@tonic-gate 			}
162*7c478bd9Sstevel@tonic-gate 			ccp = strpbrk(cp, "\\\n");
163*7c478bd9Sstevel@tonic-gate 			if (ccp != NULL) {
164*7c478bd9Sstevel@tonic-gate 				if (*ccp == '\\')
165*7c478bd9Sstevel@tonic-gate 					contline = 1;
166*7c478bd9Sstevel@tonic-gate 				else
167*7c478bd9Sstevel@tonic-gate 					contline = 0;
168*7c478bd9Sstevel@tonic-gate 				*ccp = NULL;
169*7c478bd9Sstevel@tonic-gate 			}
170*7c478bd9Sstevel@tonic-gate 			tmpcnt = strlen(cp);
171*7c478bd9Sstevel@tonic-gate 			if (tmpcnt != 0) {
172*7c478bd9Sstevel@tonic-gate 				cp += tmpcnt;
173*7c478bd9Sstevel@tonic-gate 				charcnt += tmpcnt;
174*7c478bd9Sstevel@tonic-gate 			}
175*7c478bd9Sstevel@tonic-gate 		} while ((contline) || (charcnt == 0));
176*7c478bd9Sstevel@tonic-gate 		ccp = strpbrk(buff, "#");
177*7c478bd9Sstevel@tonic-gate 		if (ccp != NULL)
178*7c478bd9Sstevel@tonic-gate 			*ccp = NULL;
179*7c478bd9Sstevel@tonic-gate 		charcnt = strlen(buff);
180*7c478bd9Sstevel@tonic-gate 	} while ((fileerr == 0) && (charcnt == 0));
181*7c478bd9Sstevel@tonic-gate 	if (fileerr && !charcnt)
182*7c478bd9Sstevel@tonic-gate 		return (0);
183*7c478bd9Sstevel@tonic-gate 	else
184*7c478bd9Sstevel@tonic-gate 		return (charcnt);
185*7c478bd9Sstevel@tonic-gate }
186*7c478bd9Sstevel@tonic-gate char
187*7c478bd9Sstevel@tonic-gate *getdmapfield(char *ptr)
188*7c478bd9Sstevel@tonic-gate {
189*7c478bd9Sstevel@tonic-gate 	static	char	*tptr;
190*7c478bd9Sstevel@tonic-gate 	if (ptr == NULL)
191*7c478bd9Sstevel@tonic-gate 		ptr = tptr;
192*7c478bd9Sstevel@tonic-gate 	if (ptr == NULL)
193*7c478bd9Sstevel@tonic-gate 		return (NULL);
194*7c478bd9Sstevel@tonic-gate 	tptr = dmapskip(ptr);
195*7c478bd9Sstevel@tonic-gate 	ptr = trim_white(ptr);
196*7c478bd9Sstevel@tonic-gate 	if (ptr == NULL)
197*7c478bd9Sstevel@tonic-gate 		return (NULL);
198*7c478bd9Sstevel@tonic-gate 	if (*ptr == NULL)
199*7c478bd9Sstevel@tonic-gate 		return (NULL);
200*7c478bd9Sstevel@tonic-gate 	return (ptr);
201*7c478bd9Sstevel@tonic-gate }
202*7c478bd9Sstevel@tonic-gate char
203*7c478bd9Sstevel@tonic-gate *getdmapdfield(char *ptr)
204*7c478bd9Sstevel@tonic-gate {
205*7c478bd9Sstevel@tonic-gate 	static	char	*tptr;
206*7c478bd9Sstevel@tonic-gate 	if (ptr != NULL) {
207*7c478bd9Sstevel@tonic-gate 		ptr = trim_white(ptr);
208*7c478bd9Sstevel@tonic-gate 	} else {
209*7c478bd9Sstevel@tonic-gate 		ptr = tptr;
210*7c478bd9Sstevel@tonic-gate 	}
211*7c478bd9Sstevel@tonic-gate 	if (ptr == NULL)
212*7c478bd9Sstevel@tonic-gate 		return (NULL);
213*7c478bd9Sstevel@tonic-gate 	tptr = dmapdskip(ptr);
214*7c478bd9Sstevel@tonic-gate 	if (ptr == NULL)
215*7c478bd9Sstevel@tonic-gate 		return (NULL);
216*7c478bd9Sstevel@tonic-gate 	if (*ptr == NULL)
217*7c478bd9Sstevel@tonic-gate 		return (NULL);
218*7c478bd9Sstevel@tonic-gate 	return (ptr);
219*7c478bd9Sstevel@tonic-gate }
220*7c478bd9Sstevel@tonic-gate /*
221*7c478bd9Sstevel@tonic-gate  * getdmapdev(dev) searches from the beginning of the file until a logical
222*7c478bd9Sstevel@tonic-gate  * device matching "dev" is found and returns a pointer to the particular
223*7c478bd9Sstevel@tonic-gate  * structure in which it was found.  If an EOF or an error is encountered on
224*7c478bd9Sstevel@tonic-gate  * reading, these functions return a NULL pointer.
225*7c478bd9Sstevel@tonic-gate  */
226*7c478bd9Sstevel@tonic-gate devmap_t *
227*7c478bd9Sstevel@tonic-gate getdmapdev(register char  *name)
228*7c478bd9Sstevel@tonic-gate {
229*7c478bd9Sstevel@tonic-gate 	register struct _dmapbuff *_dmap = _dmapalloc();
230*7c478bd9Sstevel@tonic-gate 	devmap_t *dmap;
231*7c478bd9Sstevel@tonic-gate 	char line[BUFSIZ + 1];
232*7c478bd9Sstevel@tonic-gate 
233*7c478bd9Sstevel@tonic-gate 	if (_dmap == 0)
234*7c478bd9Sstevel@tonic-gate 		return (0);
235*7c478bd9Sstevel@tonic-gate 	setdmapent();
236*7c478bd9Sstevel@tonic-gate 	if (!dmapf)
237*7c478bd9Sstevel@tonic-gate 		return (NULL);
238*7c478bd9Sstevel@tonic-gate 	while (getdmapline(line, (int)sizeof (line), dmapf) != 0) {
239*7c478bd9Sstevel@tonic-gate 		if ((dmap = interpret(line)) == NULL)
240*7c478bd9Sstevel@tonic-gate 			continue;
241*7c478bd9Sstevel@tonic-gate 		if (matchdev(&dmap, name)) {
242*7c478bd9Sstevel@tonic-gate 			enddmapent();
243*7c478bd9Sstevel@tonic-gate 			return (dmap);
244*7c478bd9Sstevel@tonic-gate 		}
245*7c478bd9Sstevel@tonic-gate 	}
246*7c478bd9Sstevel@tonic-gate 	enddmapent();
247*7c478bd9Sstevel@tonic-gate 	return (NULL);
248*7c478bd9Sstevel@tonic-gate }
249*7c478bd9Sstevel@tonic-gate /*
250*7c478bd9Sstevel@tonic-gate  * getdmapnam(name) searches from the beginning of the file until a audit-name
251*7c478bd9Sstevel@tonic-gate  * matching "name" is found and returns a pointer to the particular structure
252*7c478bd9Sstevel@tonic-gate  * in which it was found.  If an EOF or an error is encountered on reading,
253*7c478bd9Sstevel@tonic-gate  * these functions return a NULL pointer.
254*7c478bd9Sstevel@tonic-gate  */
255*7c478bd9Sstevel@tonic-gate devmap_t *
256*7c478bd9Sstevel@tonic-gate getdmapnam(register char  *name)
257*7c478bd9Sstevel@tonic-gate {
258*7c478bd9Sstevel@tonic-gate 	register struct _dmapbuff *_dmap = _dmapalloc();
259*7c478bd9Sstevel@tonic-gate 	devmap_t *dmap;
260*7c478bd9Sstevel@tonic-gate 	char line[BUFSIZ + 1];
261*7c478bd9Sstevel@tonic-gate 
262*7c478bd9Sstevel@tonic-gate 	if (_dmap == 0)
263*7c478bd9Sstevel@tonic-gate 		return (0);
264*7c478bd9Sstevel@tonic-gate 	setdmapent();
265*7c478bd9Sstevel@tonic-gate 	if (!dmapf)
266*7c478bd9Sstevel@tonic-gate 		return (NULL);
267*7c478bd9Sstevel@tonic-gate 	while (getdmapline(line, (int)sizeof (line), dmapf) != 0) {
268*7c478bd9Sstevel@tonic-gate 		if ((dmap = interpret(line)) == NULL)
269*7c478bd9Sstevel@tonic-gate 			continue;
270*7c478bd9Sstevel@tonic-gate 		if (matchname(&dmap, name)) {
271*7c478bd9Sstevel@tonic-gate 			enddmapent();
272*7c478bd9Sstevel@tonic-gate 			return (dmap);
273*7c478bd9Sstevel@tonic-gate 		}
274*7c478bd9Sstevel@tonic-gate 	}
275*7c478bd9Sstevel@tonic-gate 	enddmapent();
276*7c478bd9Sstevel@tonic-gate 	return (NULL);
277*7c478bd9Sstevel@tonic-gate }
278*7c478bd9Sstevel@tonic-gate 
279*7c478bd9Sstevel@tonic-gate /*
280*7c478bd9Sstevel@tonic-gate  * setdmapent() essentially rewinds the device_maps file to the begining.
281*7c478bd9Sstevel@tonic-gate  */
282*7c478bd9Sstevel@tonic-gate 
283*7c478bd9Sstevel@tonic-gate void
284*7c478bd9Sstevel@tonic-gate setdmapent(void)
285*7c478bd9Sstevel@tonic-gate {
286*7c478bd9Sstevel@tonic-gate 	register struct _dmapbuff *_dmap = _dmapalloc();
287*7c478bd9Sstevel@tonic-gate 
288*7c478bd9Sstevel@tonic-gate 
289*7c478bd9Sstevel@tonic-gate 	if (_dmap == 0)
290*7c478bd9Sstevel@tonic-gate 		return;
291*7c478bd9Sstevel@tonic-gate 
292*7c478bd9Sstevel@tonic-gate 	if (dmapf == NULL) {
293*7c478bd9Sstevel@tonic-gate 		dmapf = fopen(DEVMAP, "r");
294*7c478bd9Sstevel@tonic-gate 	} else
295*7c478bd9Sstevel@tonic-gate 		rewind(dmapf);
296*7c478bd9Sstevel@tonic-gate }
297*7c478bd9Sstevel@tonic-gate 
298*7c478bd9Sstevel@tonic-gate 
299*7c478bd9Sstevel@tonic-gate /*
300*7c478bd9Sstevel@tonic-gate  * enddmapent() may be called to close the device_maps file when processing
301*7c478bd9Sstevel@tonic-gate  * is complete.
302*7c478bd9Sstevel@tonic-gate  */
303*7c478bd9Sstevel@tonic-gate 
304*7c478bd9Sstevel@tonic-gate void
305*7c478bd9Sstevel@tonic-gate enddmapent(void)
306*7c478bd9Sstevel@tonic-gate {
307*7c478bd9Sstevel@tonic-gate 	register struct _dmapbuff *_dmap = _dmapalloc();
308*7c478bd9Sstevel@tonic-gate 
309*7c478bd9Sstevel@tonic-gate 	if (_dmap == 0)
310*7c478bd9Sstevel@tonic-gate 		return;
311*7c478bd9Sstevel@tonic-gate 	if (dmapf != NULL) {
312*7c478bd9Sstevel@tonic-gate 		(void) fclose(dmapf);
313*7c478bd9Sstevel@tonic-gate 		dmapf = NULL;
314*7c478bd9Sstevel@tonic-gate 	}
315*7c478bd9Sstevel@tonic-gate }
316*7c478bd9Sstevel@tonic-gate 
317*7c478bd9Sstevel@tonic-gate 
318*7c478bd9Sstevel@tonic-gate /*
319*7c478bd9Sstevel@tonic-gate  * setdmapfile(name) changes the default device_maps file to "name" thus
320*7c478bd9Sstevel@tonic-gate  * allowing alternate device_maps files to be used.  Note: it does not
321*7c478bd9Sstevel@tonic-gate  * close the previous file . If this is desired, enddmapent should be called
322*7c478bd9Sstevel@tonic-gate  * prior to it.
323*7c478bd9Sstevel@tonic-gate  */
324*7c478bd9Sstevel@tonic-gate void
325*7c478bd9Sstevel@tonic-gate setdmapfile(char *file)
326*7c478bd9Sstevel@tonic-gate {
327*7c478bd9Sstevel@tonic-gate 	register struct _dmapbuff *_dmap = _dmapalloc();
328*7c478bd9Sstevel@tonic-gate 
329*7c478bd9Sstevel@tonic-gate 	if (_dmap == 0)
330*7c478bd9Sstevel@tonic-gate 		return;
331*7c478bd9Sstevel@tonic-gate 	if (dmapf != NULL) {
332*7c478bd9Sstevel@tonic-gate 		(void) fclose(dmapf);
333*7c478bd9Sstevel@tonic-gate 		dmapf = NULL;
334*7c478bd9Sstevel@tonic-gate 	}
335*7c478bd9Sstevel@tonic-gate 	DEVMAP = file;
336*7c478bd9Sstevel@tonic-gate }
337*7c478bd9Sstevel@tonic-gate /*
338*7c478bd9Sstevel@tonic-gate  * getdmaptype(tp) When first called, returns a pointer to the
339*7c478bd9Sstevel@tonic-gate  * first devmap_t structure in the file with device-type matching
340*7c478bd9Sstevel@tonic-gate  * "tp"; thereafter, it returns a pointer to the next devmap_t
341*7c478bd9Sstevel@tonic-gate  * structure in the file with device-type matching "tp".
342*7c478bd9Sstevel@tonic-gate  * Thus successive calls can be used to search the
343*7c478bd9Sstevel@tonic-gate  * entire file for entries having device-type matching "tp".
344*7c478bd9Sstevel@tonic-gate  * A null pointer is returned on error.
345*7c478bd9Sstevel@tonic-gate  */
346*7c478bd9Sstevel@tonic-gate devmap_t *
347*7c478bd9Sstevel@tonic-gate getdmaptype(char *tp)
348*7c478bd9Sstevel@tonic-gate {
349*7c478bd9Sstevel@tonic-gate 	register struct _dmapbuff *_dmap = _dmapalloc();
350*7c478bd9Sstevel@tonic-gate 	char line1[BUFSIZ + 1];
351*7c478bd9Sstevel@tonic-gate 	devmap_t *dmap;
352*7c478bd9Sstevel@tonic-gate 
353*7c478bd9Sstevel@tonic-gate 	if (_dmap == 0)
354*7c478bd9Sstevel@tonic-gate 		return (0);
355*7c478bd9Sstevel@tonic-gate 	if (dmapf == NULL && (dmapf = fopen(DEVMAP, "r")) == NULL) {
356*7c478bd9Sstevel@tonic-gate 		return (NULL);
357*7c478bd9Sstevel@tonic-gate 	}
358*7c478bd9Sstevel@tonic-gate 	do {
359*7c478bd9Sstevel@tonic-gate 		if (getdmapline(line1, (int)sizeof (line1), dmapf) == 0)
360*7c478bd9Sstevel@tonic-gate 			return (NULL);
361*7c478bd9Sstevel@tonic-gate 
362*7c478bd9Sstevel@tonic-gate 		if ((dmap = interpret(line1)) == NULL)
363*7c478bd9Sstevel@tonic-gate 			return (NULL);
364*7c478bd9Sstevel@tonic-gate 	} while (strcmp(tp, dmap->dmap_devtype) != 0);
365*7c478bd9Sstevel@tonic-gate 	return (dmap);
366*7c478bd9Sstevel@tonic-gate }
367*7c478bd9Sstevel@tonic-gate 
368*7c478bd9Sstevel@tonic-gate /*
369*7c478bd9Sstevel@tonic-gate  * getdmapent() When first called, returns a pointer to the first devmap_t
370*7c478bd9Sstevel@tonic-gate  * structure in the file; thereafter, it returns a pointer to the next devmap_t
371*7c478bd9Sstevel@tonic-gate  * structure in the file. Thus successive calls can be used to search the
372*7c478bd9Sstevel@tonic-gate  * entire file.  A null pointer is returned on error.
373*7c478bd9Sstevel@tonic-gate  */
374*7c478bd9Sstevel@tonic-gate devmap_t *
375*7c478bd9Sstevel@tonic-gate getdmapent(void)
376*7c478bd9Sstevel@tonic-gate {
377*7c478bd9Sstevel@tonic-gate 	register struct _dmapbuff *_dmap = _dmapalloc();
378*7c478bd9Sstevel@tonic-gate 	char line1[BUFSIZ + 1];
379*7c478bd9Sstevel@tonic-gate 	devmap_t *dmap;
380*7c478bd9Sstevel@tonic-gate 
381*7c478bd9Sstevel@tonic-gate 	if (_dmap == 0)
382*7c478bd9Sstevel@tonic-gate 		return (0);
383*7c478bd9Sstevel@tonic-gate 	if (dmapf == NULL && (dmapf = fopen(DEVMAP, "r")) == NULL) {
384*7c478bd9Sstevel@tonic-gate 		return (NULL);
385*7c478bd9Sstevel@tonic-gate 	}
386*7c478bd9Sstevel@tonic-gate 	if (getdmapline(line1, (int)sizeof (line1), dmapf) == 0)
387*7c478bd9Sstevel@tonic-gate 		return (NULL);
388*7c478bd9Sstevel@tonic-gate 
389*7c478bd9Sstevel@tonic-gate 	if ((dmap = interpret(line1)) == NULL)
390*7c478bd9Sstevel@tonic-gate 		return (NULL);
391*7c478bd9Sstevel@tonic-gate 	return (dmap);
392*7c478bd9Sstevel@tonic-gate }
393*7c478bd9Sstevel@tonic-gate /*
394*7c478bd9Sstevel@tonic-gate  * matchdev(dmapp,dev) The dev_list in the structure pointed to by "dmapp" is
395*7c478bd9Sstevel@tonic-gate  * searched for string "dev".  If a match occures then a "1" is returned
396*7c478bd9Sstevel@tonic-gate  * otherwise a "0" is returned.
397*7c478bd9Sstevel@tonic-gate  */
398*7c478bd9Sstevel@tonic-gate static int
399*7c478bd9Sstevel@tonic-gate matchdev(devmap_t **dmapp, char *dev)
400*7c478bd9Sstevel@tonic-gate {
401*7c478bd9Sstevel@tonic-gate 	register struct _dmapbuff *_dmap = _dmapalloc();
402*7c478bd9Sstevel@tonic-gate 	devmap_t *dmap = *dmapp;
403*7c478bd9Sstevel@tonic-gate 	char tmpdev[BUFSIZ + 1];
404*7c478bd9Sstevel@tonic-gate 	int charcnt;
405*7c478bd9Sstevel@tonic-gate 	int tmpcnt;
406*7c478bd9Sstevel@tonic-gate 	char *cp;
407*7c478bd9Sstevel@tonic-gate 	char *tcp;
408*7c478bd9Sstevel@tonic-gate 	char *last;
409*7c478bd9Sstevel@tonic-gate 	charcnt = strlen(dev);
410*7c478bd9Sstevel@tonic-gate 	if (_dmap == 0)
411*7c478bd9Sstevel@tonic-gate 		return (0);
412*7c478bd9Sstevel@tonic-gate 	if (dmap->dmap_devlist == NULL)
413*7c478bd9Sstevel@tonic-gate 		return (0);
414*7c478bd9Sstevel@tonic-gate 	(void) strcpy(tmpdev, dmap->dmap_devlist);
415*7c478bd9Sstevel@tonic-gate 	tcp = tmpdev;
416*7c478bd9Sstevel@tonic-gate 	while ((cp = strtok_r(tcp, " ", &last)) != NULL) {
417*7c478bd9Sstevel@tonic-gate 		tcp = NULL;
418*7c478bd9Sstevel@tonic-gate 		tmpcnt = strlen(cp);
419*7c478bd9Sstevel@tonic-gate 		if (tmpcnt != charcnt)
420*7c478bd9Sstevel@tonic-gate 			continue;
421*7c478bd9Sstevel@tonic-gate 		if (strcmp(cp, dev) == 0)
422*7c478bd9Sstevel@tonic-gate 			return (1);
423*7c478bd9Sstevel@tonic-gate 	}
424*7c478bd9Sstevel@tonic-gate 	return (0);
425*7c478bd9Sstevel@tonic-gate }
426*7c478bd9Sstevel@tonic-gate /*
427*7c478bd9Sstevel@tonic-gate  * matchname(dmapp,name) The audit-name in the structure pointed to by "dmapp"
428*7c478bd9Sstevel@tonic-gate  * is searched for string "name".  If a match occures then a "1" is returned
429*7c478bd9Sstevel@tonic-gate  * otherwise a "0" is returned.
430*7c478bd9Sstevel@tonic-gate  */
431*7c478bd9Sstevel@tonic-gate static int
432*7c478bd9Sstevel@tonic-gate matchname(devmap_t **dmapp, char *name)
433*7c478bd9Sstevel@tonic-gate {
434*7c478bd9Sstevel@tonic-gate 	register struct _dmapbuff *_dmap = _dmapalloc();
435*7c478bd9Sstevel@tonic-gate 	devmap_t *dmap = *dmapp;
436*7c478bd9Sstevel@tonic-gate 
437*7c478bd9Sstevel@tonic-gate 	if (_dmap == 0)
438*7c478bd9Sstevel@tonic-gate 		return (0);
439*7c478bd9Sstevel@tonic-gate 	if (dmap->dmap_devname == NULL)
440*7c478bd9Sstevel@tonic-gate 		return (0);
441*7c478bd9Sstevel@tonic-gate 	if (strlen(dmap->dmap_devname) != strlen(name))
442*7c478bd9Sstevel@tonic-gate 		return (0);
443*7c478bd9Sstevel@tonic-gate 	if (strcmp(dmap->dmap_devname, name) == 0)
444*7c478bd9Sstevel@tonic-gate 		return (1);
445*7c478bd9Sstevel@tonic-gate 	return (0);
446*7c478bd9Sstevel@tonic-gate }
447*7c478bd9Sstevel@tonic-gate /*
448*7c478bd9Sstevel@tonic-gate  * interpret(val) string "val" is parsed and the pointers in a devmap_t
449*7c478bd9Sstevel@tonic-gate  * structure are initialized to point to fields in "val". A pointer to this
450*7c478bd9Sstevel@tonic-gate  * structure is returned.
451*7c478bd9Sstevel@tonic-gate  */
452*7c478bd9Sstevel@tonic-gate static devmap_t  *
453*7c478bd9Sstevel@tonic-gate interpret(char *val)
454*7c478bd9Sstevel@tonic-gate {
455*7c478bd9Sstevel@tonic-gate 	register struct _dmapbuff *_dmap = _dmapalloc();
456*7c478bd9Sstevel@tonic-gate 
457*7c478bd9Sstevel@tonic-gate 	if (_dmap == 0)
458*7c478bd9Sstevel@tonic-gate 		return (0);
459*7c478bd9Sstevel@tonic-gate 	(void) strcpy(interpline, val);
460*7c478bd9Sstevel@tonic-gate 	interpdevmap.dmap_devname = getdmapfield(interpline);
461*7c478bd9Sstevel@tonic-gate 	interpdevmap.dmap_devtype = getdmapfield((char *)NULL);
462*7c478bd9Sstevel@tonic-gate 	interpdevmap.dmap_devlist = getdmapfield((char *)NULL);
463*7c478bd9Sstevel@tonic-gate 
464*7c478bd9Sstevel@tonic-gate 	return (&interpdevmap);
465*7c478bd9Sstevel@tonic-gate }
466