xref: /titanic_52/usr/src/lib/libbsm/common/getdaent.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 /*
24*7c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
25*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
26*7c478bd9Sstevel@tonic-gate  */
27*7c478bd9Sstevel@tonic-gate 
28*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
29*7c478bd9Sstevel@tonic-gate 
30*7c478bd9Sstevel@tonic-gate #include <stdio.h>
31*7c478bd9Sstevel@tonic-gate #include <string.h>
32*7c478bd9Sstevel@tonic-gate #include <malloc.h>
33*7c478bd9Sstevel@tonic-gate #include <bsm/devices.h>
34*7c478bd9Sstevel@tonic-gate 
35*7c478bd9Sstevel@tonic-gate #define	MAXINT 0x7fffffff;
36*7c478bd9Sstevel@tonic-gate #ifdef SunOS_CMW
37*7c478bd9Sstevel@tonic-gate extern char	*calloc();
38*7c478bd9Sstevel@tonic-gate #endif
39*7c478bd9Sstevel@tonic-gate 
40*7c478bd9Sstevel@tonic-gate static struct _dabuff {
41*7c478bd9Sstevel@tonic-gate 	devalloc_t _NULLDA;
42*7c478bd9Sstevel@tonic-gate 	FILE *_daf;	/* pointer into /etc/security/device_allocate */
43*7c478bd9Sstevel@tonic-gate 	devalloc_t _interpdevalloc;
44*7c478bd9Sstevel@tonic-gate 	char _interpline[BUFSIZ + 1];
45*7c478bd9Sstevel@tonic-gate 	char *_DEVALLOC;
46*7c478bd9Sstevel@tonic-gate } *__dabuff;
47*7c478bd9Sstevel@tonic-gate 
48*7c478bd9Sstevel@tonic-gate #define	NULLDA (_da->_NULLDA)
49*7c478bd9Sstevel@tonic-gate #define	daf (_da->_daf)
50*7c478bd9Sstevel@tonic-gate #define	interpdevalloc (_da->_interpdevalloc)
51*7c478bd9Sstevel@tonic-gate #define	interpline (_da->_interpline)
52*7c478bd9Sstevel@tonic-gate #define	DEVALLOC (_da->_DEVALLOC)
53*7c478bd9Sstevel@tonic-gate static devalloc_t  *interpret();
54*7c478bd9Sstevel@tonic-gate static int matchname();
55*7c478bd9Sstevel@tonic-gate 
56*7c478bd9Sstevel@tonic-gate /*
57*7c478bd9Sstevel@tonic-gate  * trim_white(ptr) trims off leading and trailing white space from a NULL
58*7c478bd9Sstevel@tonic-gate  * terminated string pointed to by "ptr". The leading white space is skipped
59*7c478bd9Sstevel@tonic-gate  * by moving the pointer forward. The trailing white space is removed by
60*7c478bd9Sstevel@tonic-gate  * nulling the white space characters.  The pointer is returned to the white
61*7c478bd9Sstevel@tonic-gate  * string. If the resulting string is null in length then a NULL pointer is
62*7c478bd9Sstevel@tonic-gate  * returned. If "ptr" is NULL then a NULL pointer is returned.
63*7c478bd9Sstevel@tonic-gate  */
64*7c478bd9Sstevel@tonic-gate static char	*
65*7c478bd9Sstevel@tonic-gate trim_white(ptr)
66*7c478bd9Sstevel@tonic-gate char	*ptr;
67*7c478bd9Sstevel@tonic-gate {
68*7c478bd9Sstevel@tonic-gate 	register char	*tptr;
69*7c478bd9Sstevel@tonic-gate 	register int	cnt;
70*7c478bd9Sstevel@tonic-gate 	if (ptr == NULL)
71*7c478bd9Sstevel@tonic-gate 		return (NULL);
72*7c478bd9Sstevel@tonic-gate 	while ((*ptr == ' ') || (*ptr == '\t')) {
73*7c478bd9Sstevel@tonic-gate 		ptr++;
74*7c478bd9Sstevel@tonic-gate 	}
75*7c478bd9Sstevel@tonic-gate 	cnt = strlen(ptr);
76*7c478bd9Sstevel@tonic-gate 	if (cnt != 0) {
77*7c478bd9Sstevel@tonic-gate 		tptr = ptr + cnt - 1;
78*7c478bd9Sstevel@tonic-gate 		while ((*tptr == ' ') || (*tptr == '\t')) {
79*7c478bd9Sstevel@tonic-gate 			*tptr = '\0';
80*7c478bd9Sstevel@tonic-gate 			tptr--;
81*7c478bd9Sstevel@tonic-gate 		}
82*7c478bd9Sstevel@tonic-gate 	}
83*7c478bd9Sstevel@tonic-gate 	if (*ptr == NULL)
84*7c478bd9Sstevel@tonic-gate 		return (NULL);
85*7c478bd9Sstevel@tonic-gate 	return (ptr);
86*7c478bd9Sstevel@tonic-gate }
87*7c478bd9Sstevel@tonic-gate 
88*7c478bd9Sstevel@tonic-gate 
89*7c478bd9Sstevel@tonic-gate /*
90*7c478bd9Sstevel@tonic-gate  * scan string pointed to by pointer "p"
91*7c478bd9Sstevel@tonic-gate  * find next colin or end of line. Null it and
92*7c478bd9Sstevel@tonic-gate  * return pointer to next char.
93*7c478bd9Sstevel@tonic-gate  */
94*7c478bd9Sstevel@tonic-gate static char	*
95*7c478bd9Sstevel@tonic-gate daskip(p)
96*7c478bd9Sstevel@tonic-gate register char	*p;
97*7c478bd9Sstevel@tonic-gate {
98*7c478bd9Sstevel@tonic-gate 	while (*p && *p != ';' && *p != '\n')
99*7c478bd9Sstevel@tonic-gate 		++p;
100*7c478bd9Sstevel@tonic-gate 	if (*p == '\n')
101*7c478bd9Sstevel@tonic-gate 		*p = '\0';
102*7c478bd9Sstevel@tonic-gate 	else if (*p != '\0')
103*7c478bd9Sstevel@tonic-gate 		*p++ = '\0';
104*7c478bd9Sstevel@tonic-gate 	return (p);
105*7c478bd9Sstevel@tonic-gate }
106*7c478bd9Sstevel@tonic-gate 
107*7c478bd9Sstevel@tonic-gate 
108*7c478bd9Sstevel@tonic-gate /*
109*7c478bd9Sstevel@tonic-gate  * scan string pointed to by pointer "p"
110*7c478bd9Sstevel@tonic-gate  * find next colin or end of line. Null it and
111*7c478bd9Sstevel@tonic-gate  * return pointer to next char.
112*7c478bd9Sstevel@tonic-gate  */
113*7c478bd9Sstevel@tonic-gate static char	*
114*7c478bd9Sstevel@tonic-gate dadskip(p)
115*7c478bd9Sstevel@tonic-gate register char	*p;
116*7c478bd9Sstevel@tonic-gate {
117*7c478bd9Sstevel@tonic-gate 	while (*p && *p != ' ' && *p != '\n')
118*7c478bd9Sstevel@tonic-gate 		++p;
119*7c478bd9Sstevel@tonic-gate 	if (*p != '\0')
120*7c478bd9Sstevel@tonic-gate 		*p++ = '\0';
121*7c478bd9Sstevel@tonic-gate 	return (p);
122*7c478bd9Sstevel@tonic-gate }
123*7c478bd9Sstevel@tonic-gate 
124*7c478bd9Sstevel@tonic-gate 
125*7c478bd9Sstevel@tonic-gate /*
126*7c478bd9Sstevel@tonic-gate  * _daalloc() allocates common buffers and structures used by the device
127*7c478bd9Sstevel@tonic-gate  * allocate library routines. Then returns a pointer to a structure.  The
128*7c478bd9Sstevel@tonic-gate  * returned pointer will be null if there is an error condition.
129*7c478bd9Sstevel@tonic-gate  */
130*7c478bd9Sstevel@tonic-gate static struct _dabuff *
131*7c478bd9Sstevel@tonic-gate _daalloc()
132*7c478bd9Sstevel@tonic-gate {
133*7c478bd9Sstevel@tonic-gate 	register struct _dabuff *_da = __dabuff;
134*7c478bd9Sstevel@tonic-gate 
135*7c478bd9Sstevel@tonic-gate 	if (_da == 0) {
136*7c478bd9Sstevel@tonic-gate 		_da = (struct _dabuff *)
137*7c478bd9Sstevel@tonic-gate 		calloc((size_t)1, sizeof (*__dabuff));
138*7c478bd9Sstevel@tonic-gate 		if (_da == 0)
139*7c478bd9Sstevel@tonic-gate 			return (0);
140*7c478bd9Sstevel@tonic-gate 		DEVALLOC = "/etc/security/device_allocate";
141*7c478bd9Sstevel@tonic-gate 		__dabuff = _da;
142*7c478bd9Sstevel@tonic-gate 	}
143*7c478bd9Sstevel@tonic-gate 	return (__dabuff);
144*7c478bd9Sstevel@tonic-gate }
145*7c478bd9Sstevel@tonic-gate 
146*7c478bd9Sstevel@tonic-gate 
147*7c478bd9Sstevel@tonic-gate /*
148*7c478bd9Sstevel@tonic-gate  * getdaline(buff,len,stream) reads one device allocate line from "stream" into
149*7c478bd9Sstevel@tonic-gate  * "buff" on "len" bytes.  Continued lines from "stream" are concatinated
150*7c478bd9Sstevel@tonic-gate  * into one line in "buff". Comments are removed from "buff". The number of
151*7c478bd9Sstevel@tonic-gate  * characters in "buff" is returned.  If no characters are read or an err or
152*7c478bd9Sstevel@tonic-gate  * occured then "0" is returned
153*7c478bd9Sstevel@tonic-gate  */
154*7c478bd9Sstevel@tonic-gate static int
155*7c478bd9Sstevel@tonic-gate getdaline(buff, len, stream)
156*7c478bd9Sstevel@tonic-gate 	char *buff;
157*7c478bd9Sstevel@tonic-gate 	int len;
158*7c478bd9Sstevel@tonic-gate 	FILE *stream;
159*7c478bd9Sstevel@tonic-gate {
160*7c478bd9Sstevel@tonic-gate 	register struct _dabuff *_da = _daalloc();
161*7c478bd9Sstevel@tonic-gate 	char	*cp;
162*7c478bd9Sstevel@tonic-gate 	char	*ccp;
163*7c478bd9Sstevel@tonic-gate 	int	tmpcnt;
164*7c478bd9Sstevel@tonic-gate 	int	charcnt = 0;
165*7c478bd9Sstevel@tonic-gate 	int	fileerr = 0;
166*7c478bd9Sstevel@tonic-gate 	int	contline;
167*7c478bd9Sstevel@tonic-gate 	if (_da == 0)
168*7c478bd9Sstevel@tonic-gate 		return (0);
169*7c478bd9Sstevel@tonic-gate 	do {
170*7c478bd9Sstevel@tonic-gate 		cp = buff;
171*7c478bd9Sstevel@tonic-gate 		*cp = NULL;
172*7c478bd9Sstevel@tonic-gate 		do {
173*7c478bd9Sstevel@tonic-gate 			if (fgets(cp, len - charcnt, stream) == NULL) {
174*7c478bd9Sstevel@tonic-gate 				fileerr = 1;
175*7c478bd9Sstevel@tonic-gate 				break;
176*7c478bd9Sstevel@tonic-gate 			}
177*7c478bd9Sstevel@tonic-gate 			ccp = strpbrk(cp, "\\\n");
178*7c478bd9Sstevel@tonic-gate 			if (ccp != NULL) {
179*7c478bd9Sstevel@tonic-gate 				if (*ccp == '\\')
180*7c478bd9Sstevel@tonic-gate 					contline = 1;
181*7c478bd9Sstevel@tonic-gate 					else
182*7c478bd9Sstevel@tonic-gate 					contline = 0;
183*7c478bd9Sstevel@tonic-gate 				*ccp = NULL;
184*7c478bd9Sstevel@tonic-gate 			}
185*7c478bd9Sstevel@tonic-gate 			tmpcnt = strlen(cp);
186*7c478bd9Sstevel@tonic-gate 			if (tmpcnt != 0) {
187*7c478bd9Sstevel@tonic-gate 				cp += tmpcnt;
188*7c478bd9Sstevel@tonic-gate 				charcnt += tmpcnt;
189*7c478bd9Sstevel@tonic-gate 			}
190*7c478bd9Sstevel@tonic-gate 		} while ((contline) || (charcnt == 0));
191*7c478bd9Sstevel@tonic-gate 		ccp = strpbrk(buff, "#");
192*7c478bd9Sstevel@tonic-gate 		if (ccp != NULL)
193*7c478bd9Sstevel@tonic-gate 			*ccp = NULL;
194*7c478bd9Sstevel@tonic-gate 		charcnt = strlen(buff);
195*7c478bd9Sstevel@tonic-gate 	} while ((fileerr == 0) && (charcnt == 0));
196*7c478bd9Sstevel@tonic-gate 	if (fileerr)
197*7c478bd9Sstevel@tonic-gate 		return (0);
198*7c478bd9Sstevel@tonic-gate 		else
199*7c478bd9Sstevel@tonic-gate 		return (charcnt);
200*7c478bd9Sstevel@tonic-gate }
201*7c478bd9Sstevel@tonic-gate 
202*7c478bd9Sstevel@tonic-gate char	*
203*7c478bd9Sstevel@tonic-gate getdafield(ptr)
204*7c478bd9Sstevel@tonic-gate char	*ptr;
205*7c478bd9Sstevel@tonic-gate {
206*7c478bd9Sstevel@tonic-gate 	static char	*tptr;
207*7c478bd9Sstevel@tonic-gate 	if (ptr == NULL)
208*7c478bd9Sstevel@tonic-gate 		ptr = tptr;
209*7c478bd9Sstevel@tonic-gate 	if (ptr == NULL)
210*7c478bd9Sstevel@tonic-gate 		return (NULL);
211*7c478bd9Sstevel@tonic-gate 	tptr = daskip(ptr);
212*7c478bd9Sstevel@tonic-gate 	ptr = trim_white(ptr);
213*7c478bd9Sstevel@tonic-gate 	if (ptr == NULL)
214*7c478bd9Sstevel@tonic-gate 		return (NULL);
215*7c478bd9Sstevel@tonic-gate 	if (*ptr == NULL)
216*7c478bd9Sstevel@tonic-gate 		return (NULL);
217*7c478bd9Sstevel@tonic-gate 	return (ptr);
218*7c478bd9Sstevel@tonic-gate }
219*7c478bd9Sstevel@tonic-gate 
220*7c478bd9Sstevel@tonic-gate char	*
221*7c478bd9Sstevel@tonic-gate getdadfield(ptr)
222*7c478bd9Sstevel@tonic-gate char	*ptr;
223*7c478bd9Sstevel@tonic-gate {
224*7c478bd9Sstevel@tonic-gate 	static char	*tptr;
225*7c478bd9Sstevel@tonic-gate 	if (ptr != NULL) {
226*7c478bd9Sstevel@tonic-gate 		ptr = trim_white(ptr);
227*7c478bd9Sstevel@tonic-gate 	} else {
228*7c478bd9Sstevel@tonic-gate 		ptr = tptr;
229*7c478bd9Sstevel@tonic-gate 	}
230*7c478bd9Sstevel@tonic-gate 	if (ptr == NULL)
231*7c478bd9Sstevel@tonic-gate 		return (NULL);
232*7c478bd9Sstevel@tonic-gate 	tptr = dadskip(ptr);
233*7c478bd9Sstevel@tonic-gate 	if (ptr == NULL)
234*7c478bd9Sstevel@tonic-gate 		return (NULL);
235*7c478bd9Sstevel@tonic-gate 	if (*ptr == NULL)
236*7c478bd9Sstevel@tonic-gate 		return (NULL);
237*7c478bd9Sstevel@tonic-gate 	return (ptr);
238*7c478bd9Sstevel@tonic-gate }
239*7c478bd9Sstevel@tonic-gate 
240*7c478bd9Sstevel@tonic-gate /*
241*7c478bd9Sstevel@tonic-gate  * getdadev(dev) searches from the beginning of the file until a logical
242*7c478bd9Sstevel@tonic-gate  * device matching "dev" is found and returns a pointer to the particular
243*7c478bd9Sstevel@tonic-gate  * structure in which it was found.  If an EOF or an error is encountered on
244*7c478bd9Sstevel@tonic-gate  * reading, these functions return a NULL pointer.
245*7c478bd9Sstevel@tonic-gate  */
246*7c478bd9Sstevel@tonic-gate #ifdef NOTDEF
247*7c478bd9Sstevel@tonic-gate devalloc_t *
248*7c478bd9Sstevel@tonic-gate getdadev(name)
249*7c478bd9Sstevel@tonic-gate 	register char	*name;
250*7c478bd9Sstevel@tonic-gate {
251*7c478bd9Sstevel@tonic-gate 	register struct _dabuff *_da = _daalloc();
252*7c478bd9Sstevel@tonic-gate 	devalloc_t *da;
253*7c478bd9Sstevel@tonic-gate 	char	line[BUFSIZ + 1];
254*7c478bd9Sstevel@tonic-gate 
255*7c478bd9Sstevel@tonic-gate 	if (_da == 0)
256*7c478bd9Sstevel@tonic-gate 		return (0);
257*7c478bd9Sstevel@tonic-gate 	setdaent();
258*7c478bd9Sstevel@tonic-gate 	if (!daf)
259*7c478bd9Sstevel@tonic-gate 		return ((devalloc_t *)NULL);
260*7c478bd9Sstevel@tonic-gate 	while (getdaline(line, sizeof (line), daf) != 0) {
261*7c478bd9Sstevel@tonic-gate 		if ((da = interpret(line)) == NULL)
262*7c478bd9Sstevel@tonic-gate 			continue;
263*7c478bd9Sstevel@tonic-gate 		if (matchdev(&da, name)) {
264*7c478bd9Sstevel@tonic-gate 			enddaent();
265*7c478bd9Sstevel@tonic-gate 			return (da);
266*7c478bd9Sstevel@tonic-gate 		}
267*7c478bd9Sstevel@tonic-gate 	}
268*7c478bd9Sstevel@tonic-gate 	enddaent();
269*7c478bd9Sstevel@tonic-gate 	return ((devalloc_t *)NULL);
270*7c478bd9Sstevel@tonic-gate }
271*7c478bd9Sstevel@tonic-gate 
272*7c478bd9Sstevel@tonic-gate 
273*7c478bd9Sstevel@tonic-gate #endif /* NOTDEF */
274*7c478bd9Sstevel@tonic-gate 
275*7c478bd9Sstevel@tonic-gate /*
276*7c478bd9Sstevel@tonic-gate  * getdanam(name) searches from the beginning of the file until a audit-name
277*7c478bd9Sstevel@tonic-gate  * matching "name" is found and returns a pointer to the particular structure
278*7c478bd9Sstevel@tonic-gate  * in which it was found.  If an EOF or an error is encountered on reading,
279*7c478bd9Sstevel@tonic-gate  * these functions return a NULL pointer.
280*7c478bd9Sstevel@tonic-gate  */
281*7c478bd9Sstevel@tonic-gate devalloc_t *
282*7c478bd9Sstevel@tonic-gate getdanam(name)
283*7c478bd9Sstevel@tonic-gate 	register char	*name;
284*7c478bd9Sstevel@tonic-gate {
285*7c478bd9Sstevel@tonic-gate 	register struct _dabuff *_da = _daalloc();
286*7c478bd9Sstevel@tonic-gate 	devalloc_t *da;
287*7c478bd9Sstevel@tonic-gate 	char line[BUFSIZ + 1];
288*7c478bd9Sstevel@tonic-gate 
289*7c478bd9Sstevel@tonic-gate 	if (_da == 0)
290*7c478bd9Sstevel@tonic-gate 		return (0);
291*7c478bd9Sstevel@tonic-gate 	setdaent();
292*7c478bd9Sstevel@tonic-gate 	if (!daf)
293*7c478bd9Sstevel@tonic-gate 		return ((devalloc_t *)NULL);
294*7c478bd9Sstevel@tonic-gate 	while (getdaline(line, (int)sizeof (line), daf) != 0) {
295*7c478bd9Sstevel@tonic-gate 		if ((da = interpret(line)) == NULL)
296*7c478bd9Sstevel@tonic-gate 			continue;
297*7c478bd9Sstevel@tonic-gate 		if (matchname(&da, name)) {
298*7c478bd9Sstevel@tonic-gate 			enddaent();
299*7c478bd9Sstevel@tonic-gate 			return (da);
300*7c478bd9Sstevel@tonic-gate 		}
301*7c478bd9Sstevel@tonic-gate 	}
302*7c478bd9Sstevel@tonic-gate 	enddaent();
303*7c478bd9Sstevel@tonic-gate 	return ((devalloc_t *)NULL);
304*7c478bd9Sstevel@tonic-gate }
305*7c478bd9Sstevel@tonic-gate 
306*7c478bd9Sstevel@tonic-gate 
307*7c478bd9Sstevel@tonic-gate /*
308*7c478bd9Sstevel@tonic-gate  * setdaent() essentially rewinds the device_allocate file to the begining.
309*7c478bd9Sstevel@tonic-gate  */
310*7c478bd9Sstevel@tonic-gate 
311*7c478bd9Sstevel@tonic-gate void
312*7c478bd9Sstevel@tonic-gate setdaent()
313*7c478bd9Sstevel@tonic-gate {
314*7c478bd9Sstevel@tonic-gate 	register struct _dabuff *_da = _daalloc();
315*7c478bd9Sstevel@tonic-gate 
316*7c478bd9Sstevel@tonic-gate 	if (_da == 0)
317*7c478bd9Sstevel@tonic-gate 		return;
318*7c478bd9Sstevel@tonic-gate 	if (daf == NULL) {
319*7c478bd9Sstevel@tonic-gate 		daf = fopen(DEVALLOC, "r");
320*7c478bd9Sstevel@tonic-gate 	} else
321*7c478bd9Sstevel@tonic-gate 		rewind(daf);
322*7c478bd9Sstevel@tonic-gate }
323*7c478bd9Sstevel@tonic-gate 
324*7c478bd9Sstevel@tonic-gate 
325*7c478bd9Sstevel@tonic-gate /*
326*7c478bd9Sstevel@tonic-gate  * enddaent() may be called to close the device_allocate file when processing
327*7c478bd9Sstevel@tonic-gate  * is complete.
328*7c478bd9Sstevel@tonic-gate  */
329*7c478bd9Sstevel@tonic-gate 
330*7c478bd9Sstevel@tonic-gate void
331*7c478bd9Sstevel@tonic-gate enddaent()
332*7c478bd9Sstevel@tonic-gate {
333*7c478bd9Sstevel@tonic-gate 	register struct _dabuff *_da = _daalloc();
334*7c478bd9Sstevel@tonic-gate 
335*7c478bd9Sstevel@tonic-gate 	if (_da == 0)
336*7c478bd9Sstevel@tonic-gate 		return;
337*7c478bd9Sstevel@tonic-gate 	if (daf != NULL) {
338*7c478bd9Sstevel@tonic-gate 		(void) fclose(daf);
339*7c478bd9Sstevel@tonic-gate 		daf = NULL;
340*7c478bd9Sstevel@tonic-gate 	}
341*7c478bd9Sstevel@tonic-gate }
342*7c478bd9Sstevel@tonic-gate 
343*7c478bd9Sstevel@tonic-gate 
344*7c478bd9Sstevel@tonic-gate /*
345*7c478bd9Sstevel@tonic-gate  * setdafile(name) changes the default device_allocate file to "name" thus
346*7c478bd9Sstevel@tonic-gate  * allowing alternate device_allocate files to be used.  Note: it does not
347*7c478bd9Sstevel@tonic-gate  * close the previous file . If this is desired, enddaent should be called
348*7c478bd9Sstevel@tonic-gate  * prior to it.
349*7c478bd9Sstevel@tonic-gate  */
350*7c478bd9Sstevel@tonic-gate void
351*7c478bd9Sstevel@tonic-gate setdafile(file)
352*7c478bd9Sstevel@tonic-gate char	*file;
353*7c478bd9Sstevel@tonic-gate {
354*7c478bd9Sstevel@tonic-gate 	register struct _dabuff *_da = _daalloc();
355*7c478bd9Sstevel@tonic-gate 
356*7c478bd9Sstevel@tonic-gate 	if (_da == 0)
357*7c478bd9Sstevel@tonic-gate 		return;
358*7c478bd9Sstevel@tonic-gate 	if (daf != NULL) {
359*7c478bd9Sstevel@tonic-gate 		(void) fclose(daf);
360*7c478bd9Sstevel@tonic-gate 		daf = NULL;
361*7c478bd9Sstevel@tonic-gate 	}
362*7c478bd9Sstevel@tonic-gate 	DEVALLOC = file;
363*7c478bd9Sstevel@tonic-gate }
364*7c478bd9Sstevel@tonic-gate 
365*7c478bd9Sstevel@tonic-gate 
366*7c478bd9Sstevel@tonic-gate /*
367*7c478bd9Sstevel@tonic-gate  * getdatype(tp) When first called, returns a pointer to the
368*7c478bd9Sstevel@tonic-gate  * first devalloc_t structure in the file with device-type matching
369*7c478bd9Sstevel@tonic-gate  * "tp"; thereafter, it returns a pointer to the next devalloc_t
370*7c478bd9Sstevel@tonic-gate  * structure in the file with device-type matching "tp".
371*7c478bd9Sstevel@tonic-gate  * Thus successive calls can be used to search the
372*7c478bd9Sstevel@tonic-gate  * entire file for entries having device-type matching "tp".
373*7c478bd9Sstevel@tonic-gate  * A null pointer is returned on error.
374*7c478bd9Sstevel@tonic-gate  */
375*7c478bd9Sstevel@tonic-gate devalloc_t *
376*7c478bd9Sstevel@tonic-gate getdatype(tp)
377*7c478bd9Sstevel@tonic-gate 	char	*tp;
378*7c478bd9Sstevel@tonic-gate {
379*7c478bd9Sstevel@tonic-gate 	register struct _dabuff *_da = _daalloc();
380*7c478bd9Sstevel@tonic-gate 	char line1[BUFSIZ + 1];
381*7c478bd9Sstevel@tonic-gate 	devalloc_t *da;
382*7c478bd9Sstevel@tonic-gate 
383*7c478bd9Sstevel@tonic-gate 	if (_da == 0)
384*7c478bd9Sstevel@tonic-gate 		return (0);
385*7c478bd9Sstevel@tonic-gate 	if (daf == NULL && (daf = fopen(DEVALLOC, "r")) == NULL) {
386*7c478bd9Sstevel@tonic-gate 		return (NULL);
387*7c478bd9Sstevel@tonic-gate 	}
388*7c478bd9Sstevel@tonic-gate 	do {
389*7c478bd9Sstevel@tonic-gate 		if (getdaline(line1, (int)sizeof (line1), daf) == 0)
390*7c478bd9Sstevel@tonic-gate 			return (NULL);
391*7c478bd9Sstevel@tonic-gate 
392*7c478bd9Sstevel@tonic-gate 		if ((da = interpret(line1)) == NULL)
393*7c478bd9Sstevel@tonic-gate 			return (NULL);
394*7c478bd9Sstevel@tonic-gate 	} while (strcmp(tp, da->da_devtype) != 0);
395*7c478bd9Sstevel@tonic-gate 	return (da);
396*7c478bd9Sstevel@tonic-gate }
397*7c478bd9Sstevel@tonic-gate 
398*7c478bd9Sstevel@tonic-gate 
399*7c478bd9Sstevel@tonic-gate /*
400*7c478bd9Sstevel@tonic-gate  * getdaent() When first called, returns a pointer to the first devalloc_t
401*7c478bd9Sstevel@tonic-gate  * structure in the file; thereafter, it returns a pointer to the next
402*7c478bd9Sstevel@tonic-gate  * devalloc_t structure in the file. Thus successive calls can be used to
403*7c478bd9Sstevel@tonic-gate  * search the entire file.  A null pointer is returned on error.
404*7c478bd9Sstevel@tonic-gate  */
405*7c478bd9Sstevel@tonic-gate devalloc_t *
406*7c478bd9Sstevel@tonic-gate getdaent()
407*7c478bd9Sstevel@tonic-gate {
408*7c478bd9Sstevel@tonic-gate 	register struct _dabuff *_da = _daalloc();
409*7c478bd9Sstevel@tonic-gate 	char line1[BUFSIZ + 1];
410*7c478bd9Sstevel@tonic-gate 	devalloc_t *da;
411*7c478bd9Sstevel@tonic-gate 
412*7c478bd9Sstevel@tonic-gate 	if (_da == 0)
413*7c478bd9Sstevel@tonic-gate 		return (0);
414*7c478bd9Sstevel@tonic-gate 	if (daf == NULL && (daf = fopen(DEVALLOC, "r")) == NULL) {
415*7c478bd9Sstevel@tonic-gate 		return (NULL);
416*7c478bd9Sstevel@tonic-gate 	}
417*7c478bd9Sstevel@tonic-gate 	if (getdaline(line1, (int)sizeof (line1), daf) == 0)
418*7c478bd9Sstevel@tonic-gate 		return (NULL);
419*7c478bd9Sstevel@tonic-gate 
420*7c478bd9Sstevel@tonic-gate 	if ((da = interpret(line1)) == NULL)
421*7c478bd9Sstevel@tonic-gate 		return (NULL);
422*7c478bd9Sstevel@tonic-gate 	return (da);
423*7c478bd9Sstevel@tonic-gate }
424*7c478bd9Sstevel@tonic-gate 
425*7c478bd9Sstevel@tonic-gate 
426*7c478bd9Sstevel@tonic-gate /*
427*7c478bd9Sstevel@tonic-gate  * matchdev(dap,dev) The dev_list in the structure pointed to by "dap" is
428*7c478bd9Sstevel@tonic-gate  * searched for string "dev".  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 #ifdef NOTDEF
432*7c478bd9Sstevel@tonic-gate static
433*7c478bd9Sstevel@tonic-gate matchdev(dap, dev)
434*7c478bd9Sstevel@tonic-gate 	devalloc_t **dap;
435*7c478bd9Sstevel@tonic-gate 	char	*dev;
436*7c478bd9Sstevel@tonic-gate {
437*7c478bd9Sstevel@tonic-gate 	register struct _dabuff *_da = _daalloc();
438*7c478bd9Sstevel@tonic-gate 	devalloc_t *da = *dap;
439*7c478bd9Sstevel@tonic-gate 	char tmpdev[BUFSIZ + 1];
440*7c478bd9Sstevel@tonic-gate 	int	charcnt;
441*7c478bd9Sstevel@tonic-gate 	int	tmpcnt;
442*7c478bd9Sstevel@tonic-gate 	char	*cp;
443*7c478bd9Sstevel@tonic-gate 	char	*tcp;
444*7c478bd9Sstevel@tonic-gate 	char	*last;
445*7c478bd9Sstevel@tonic-gate 
446*7c478bd9Sstevel@tonic-gate 	charcnt = strlen(dev);
447*7c478bd9Sstevel@tonic-gate 	if (_da == 0)
448*7c478bd9Sstevel@tonic-gate 		return (0);
449*7c478bd9Sstevel@tonic-gate 	if (da->da_devlist == NULL)
450*7c478bd9Sstevel@tonic-gate 		return (0);
451*7c478bd9Sstevel@tonic-gate 	(void) strcpy(tmpdev, da->da_devlist);
452*7c478bd9Sstevel@tonic-gate 	tcp = tmpdev;
453*7c478bd9Sstevel@tonic-gate 	while ((cp = strtok_r(tcp, " ", &last)) != NULL) {
454*7c478bd9Sstevel@tonic-gate 		tcp = NULL;
455*7c478bd9Sstevel@tonic-gate 		tmpcnt = strlen(cp);
456*7c478bd9Sstevel@tonic-gate 		if (tmpcnt != charcnt)
457*7c478bd9Sstevel@tonic-gate 			continue;
458*7c478bd9Sstevel@tonic-gate 		if (strcmp(cp, dev) == 0)
459*7c478bd9Sstevel@tonic-gate 			return (1);
460*7c478bd9Sstevel@tonic-gate 	}
461*7c478bd9Sstevel@tonic-gate 	return (0);
462*7c478bd9Sstevel@tonic-gate }
463*7c478bd9Sstevel@tonic-gate 
464*7c478bd9Sstevel@tonic-gate #endif /* NOTDEF */
465*7c478bd9Sstevel@tonic-gate /*
466*7c478bd9Sstevel@tonic-gate  * matchname(dap,name) The audit-name in the structure pointed to by "dap" is
467*7c478bd9Sstevel@tonic-gate  * searched for string "name".  If a match occures then a "1" is returned
468*7c478bd9Sstevel@tonic-gate  * otherwise a "0" is returned.
469*7c478bd9Sstevel@tonic-gate  */
470*7c478bd9Sstevel@tonic-gate static int
471*7c478bd9Sstevel@tonic-gate matchname(dap, name)
472*7c478bd9Sstevel@tonic-gate 	devalloc_t **dap;
473*7c478bd9Sstevel@tonic-gate 	char *name;
474*7c478bd9Sstevel@tonic-gate {
475*7c478bd9Sstevel@tonic-gate 	register struct _dabuff *_da = _daalloc();
476*7c478bd9Sstevel@tonic-gate 	devalloc_t *da = *dap;
477*7c478bd9Sstevel@tonic-gate 
478*7c478bd9Sstevel@tonic-gate 	if (_da == 0)
479*7c478bd9Sstevel@tonic-gate 		return (0);
480*7c478bd9Sstevel@tonic-gate 	if (da->da_devname == NULL)
481*7c478bd9Sstevel@tonic-gate 		return (0);
482*7c478bd9Sstevel@tonic-gate 	if (strlen(da->da_devname) != strlen(name))
483*7c478bd9Sstevel@tonic-gate 		return (0);
484*7c478bd9Sstevel@tonic-gate 	if (strcmp(da->da_devname, name) == 0)
485*7c478bd9Sstevel@tonic-gate 		return (1);
486*7c478bd9Sstevel@tonic-gate 	return (0);
487*7c478bd9Sstevel@tonic-gate }
488*7c478bd9Sstevel@tonic-gate 
489*7c478bd9Sstevel@tonic-gate 
490*7c478bd9Sstevel@tonic-gate /*
491*7c478bd9Sstevel@tonic-gate  * interpret(val) string "val" is parsed and the pointers in a devalloc_t
492*7c478bd9Sstevel@tonic-gate  * structure are initialized to point to fields in "val". A pointer to this
493*7c478bd9Sstevel@tonic-gate  * structure is returned.
494*7c478bd9Sstevel@tonic-gate  */
495*7c478bd9Sstevel@tonic-gate static devalloc_t  *
496*7c478bd9Sstevel@tonic-gate interpret(val)
497*7c478bd9Sstevel@tonic-gate char	*val;
498*7c478bd9Sstevel@tonic-gate {
499*7c478bd9Sstevel@tonic-gate 	register struct _dabuff *_da = _daalloc();
500*7c478bd9Sstevel@tonic-gate 
501*7c478bd9Sstevel@tonic-gate 	if (_da == 0)
502*7c478bd9Sstevel@tonic-gate 		return (0);
503*7c478bd9Sstevel@tonic-gate 	(void) strcpy(interpline, val);
504*7c478bd9Sstevel@tonic-gate 	interpdevalloc.da_devname = getdafield(interpline);
505*7c478bd9Sstevel@tonic-gate 	interpdevalloc.da_devtype = getdafield((char *)NULL);
506*7c478bd9Sstevel@tonic-gate 	interpdevalloc.da_devmin = getdafield((char *)NULL);
507*7c478bd9Sstevel@tonic-gate 	interpdevalloc.da_devmax = getdafield((char *)NULL);
508*7c478bd9Sstevel@tonic-gate 	interpdevalloc.da_devauth = getdafield((char *)NULL);
509*7c478bd9Sstevel@tonic-gate 	interpdevalloc.da_devexec = getdafield((char *)NULL);
510*7c478bd9Sstevel@tonic-gate 
511*7c478bd9Sstevel@tonic-gate 	return (&interpdevalloc);
512*7c478bd9Sstevel@tonic-gate }
513