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.
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.
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]
22 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 #pragma ident "%Z%%M% %I% %E% SMI"
30 #include <bsm/devices.h>
31 #include <bsm/devalloc.h>
33 char *strtok_r(char *, const char *, char **);
35 /* externs from getdaent.c */
36 extern char *trim_white(char *);
37 extern int pack_white(char *);
38 extern char *getdadmfield(char *, char *);
39 extern int getdadmline(char *, int, FILE *);
41 extern char *_strdup_null(char *);
43 static struct _dadefbuff
{
45 /* pointer into /etc/security/tsol/devalloc_defaults */
46 da_defs_t _interpdadefs
;
47 char _interpdadefline
[DA_BUFSIZE
+ 1];
51 #define dadeff (_df->_dadeff)
52 #define interpdadefs (_df->_interpdadefs)
53 #define interpdadefline (_df->_interpdadefline)
54 #define DADEFS_FILE (_df->_DADEFS)
56 static da_defs_t
*dadef_interpret(char *);
57 int dadef_matchtype(da_defs_t
*, char *);
61 * allocates common buffers and structures.
62 * returns pointer to the new structure, else returns NULL on error.
64 static struct _dadefbuff
*
67 struct _dadefbuff
*_df
= __dadefbuff
;
70 _df
= (struct _dadefbuff
*)calloc((unsigned)1,
71 (unsigned)sizeof (*__dadefbuff
));
74 DADEFS_FILE
= "/etc/security/tsol/devalloc_defaults";
83 * rewinds devalloc_defaults file to the begining.
89 struct _dadefbuff
*_df
= _dadefalloc();
94 dadeff
= fopen(DADEFS_FILE
, "rF");
101 * closes devalloc_defaults file.
107 struct _dadefbuff
*_df
= _dadefalloc();
111 if (dadeff
!= NULL
) {
112 (void) fclose(dadeff
);
118 freedadefent(da_defs_t
*da_def
)
122 _kva_free(da_def
->devopts
);
123 da_def
->devopts
= NULL
;
128 * When first called, returns a pointer to the first da_defs_t
129 * structure in devalloc_defaults; thereafter, it returns a pointer to the
130 * next da_defs_t structure in the file. Thus, successive calls can be
131 * used to search the entire file.
132 * call to getdadefent should be bracketed by setdadefent and enddadefent.
133 * returns NULL on error.
138 char line1
[DA_BUFSIZE
+ 1];
140 struct _dadefbuff
*_df
= _dadefalloc();
142 if ((_df
== 0) || (dadeff
== NULL
))
145 while (getdadmline(line1
, (int)sizeof (line1
), dadeff
) != 0) {
146 if ((da_def
= dadef_interpret(line1
)) == NULL
)
156 * searches from the beginning of devalloc_defaults for the device
157 * specified by its type.
158 * call to getdadeftype should be bracketed by setdadefent and enddadefent.
159 * returns pointer to da_defs_t for the device if it is found, else
160 * returns NULL if device not found or in case of error.
163 getdadeftype(char *type
)
165 char line1
[DA_BUFSIZE
+ 1];
167 struct _dadefbuff
*_df
= _dadefalloc();
169 if ((type
== NULL
) || (_df
== NULL
) || (dadeff
== NULL
))
172 while (getdadmline(line1
, (int)sizeof (line1
), dadeff
) != 0) {
173 if (strstr(line1
, type
) == NULL
)
175 if ((da_def
= dadef_interpret(line1
)) == NULL
)
177 if (dadef_matchtype(da_def
, type
))
179 freedadefent(da_def
);
187 * checks if the specified da_defs_t is for the device type specified.
188 * returns 1 if match found, else, returns 0.
191 dadef_matchtype(da_defs_t
*da_def
, char *type
)
193 if (da_def
->devtype
== NULL
)
196 return ((strcmp(da_def
->devtype
, type
) == 0));
201 * parses val and initializes pointers in da_defs_t.
202 * returns pointer to parsed da_defs_t entry, else returns NULL on error.
205 dadef_interpret(char *val
)
207 struct _dadefbuff
*_df
= _dadefalloc();
216 (void) strcpy(interpdadefline
, val
);
217 interpdadefs
.devtype
= getdadmfield(interpdadefline
, KV_TOKEN_DELIMIT
);
218 opts
= getdadmfield(NULL
, KV_TOKEN_DELIMIT
);
219 interpdadefs
.devopts
= NULL
;
220 if (interpdadefs
.devtype
== NULL
)
223 interpdadefs
.devopts
=
224 _str2kva(opts
, KV_ASSIGN
, KV_DELIMITER
);
225 /* remove any extraneous whitespace in the options */
226 if ((kvap
= interpdadefs
.devopts
) != NULL
) {
227 for (i
= 0, kvp
= kvap
->data
; i
< kvap
->length
; i
++, kvp
++) {
228 (void) pack_white(kvp
->key
);
229 (void) pack_white(kvp
->value
);
233 return (&interpdadefs
);