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 2007 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 #pragma ident "%Z%%M% %I% %E% SMI"
29 * routine to read configuration file
32 #include "nscd_config.h"
40 strbreak(char *field
[], int array_size
, char *s
, char *sep
)
47 for (i
= 0; i
< array_size
&& (field
[i
] = strtok_r((i
?NULL
:s
),
56 while (++qp
< lasts
) {
61 inquote
= (inquote
== 0);
66 if (inquote
== 1 && *(qp
+ 1) == '"')
87 nscd_cfg_error_t
**errorp
)
89 char *me
= "_nscd_cfg_read_file";
95 nscd_rc_t rc
= NSCD_SUCCESS
;
96 nscd_cfg_handle_t
*h
= NULL
;
97 nscd_cfg_param_desc_t
*pdesc
;
101 char msg
[NSCD_CFG_MAX_ERR_MSG_LEN
];
108 if ((in
= fopen(filename
, "r")) == NULL
) {
110 (void) snprintf(msg
, sizeof (msg
),
111 gettext("open of configuration file \"%s\" failed: %s"),
112 filename
, strerror(errno
));
114 *errorp
= _nscd_cfg_make_error(
115 NSCD_CFG_FILE_OPEN_ERROR
, msg
);
117 _NSCD_LOG(NSCD_LOG_CONFIG
, NSCD_LOG_LEVEL_ERROR
)
120 return (NSCD_CFG_FILE_OPEN_ERROR
);
125 while (fgets(buffer
, sizeof (buffer
), in
) != NULL
) {
128 if ((fieldcnt
= strbreak(fields
, 128, buffer
, " \t\n")) ==
129 0 || *fields
[0] == '#') {
130 /* skip blank or comment lines */
148 (void) strlcpy(u
.data
, fields
[0], sizeof (u
.data
));
149 for (i
= 1; i
< fieldcnt
; i
++) {
150 (void) strlcat(u
.data
, " ",
152 (void) strlcat(u
.data
, fields
[i
],
156 (void) snprintf(msg
, sizeof (msg
),
157 gettext("Syntax error: line %d of configuration "
158 "file: %s : \"%s\""), linecnt
, filename
, u
.data
);
160 *errorp
= _nscd_cfg_make_error(
161 NSCD_CFG_SYNTAX_ERROR
, msg
);
163 _NSCD_LOG(NSCD_LOG_CONFIG
, NSCD_LOG_LEVEL_ERROR
)
166 rc
= NSCD_CFG_SYNTAX_ERROR
;
170 if (rc
!= NSCD_SUCCESS
)
173 rc
= _nscd_cfg_get_handle(fields
[0], dbname
, &h
, errorp
);
174 if (rc
!= NSCD_SUCCESS
)
177 pdesc
= _nscd_cfg_get_desc(h
);
179 /* convert string to data */
180 rc
= _nscd_cfg_str_to_data(pdesc
, str
, &u
.data
,
182 if (rc
!= NSCD_SUCCESS
)
185 /* do preliminary check based on data type */
186 rc
= _nscd_cfg_prelim_check(pdesc
, data_p
, errorp
);
187 if (rc
!= NSCD_SUCCESS
)
190 rc
= _nscd_cfg_set_linked(h
, data_p
, errorp
);
191 _nscd_cfg_free_handle(h
);
193 if (rc
!= NSCD_CFG_READ_ONLY
&& rc
!= NSCD_SUCCESS
)
196 _nscd_cfg_free_error(*errorp
);
200 /* NSCD_CFG_READ_ONLY is not fatal */
201 if (rc
== NSCD_CFG_READ_ONLY
)
205 _nscd_cfg_free_handle(h
);
209 if (msg
[0] == '\0' && rc
!= NSCD_SUCCESS
) {
211 _NSCD_LOG(NSCD_LOG_CONFIG
, NSCD_LOG_LEVEL_ERROR
)
212 (me
, "%s\n", NSCD_ERR2MSG(*errorp
));
219 _nscd_cfg_read_nsswitch_file(
221 nscd_cfg_error_t
**errorp
)
223 char *me
= "_nscd_cfg_read_nsswitch_file";
224 char *pname
= "nsw-config-string";
227 char *cc
, *ce
, *ce1
, *c1
, *c2
;
232 nscd_rc_t rc
= NSCD_SUCCESS
;
233 nscd_cfg_handle_t
*h
= NULL
;
234 nscd_cfg_param_desc_t
*pdesc
;
236 char msg
[NSCD_CFG_MAX_ERR_MSG_LEN
];
243 if ((in
= fopen(filename
, "r")) == NULL
) {
245 (void) snprintf(msg
, sizeof (msg
),
246 gettext("open of configuration file \"%s\" failed: %s"),
247 filename
, strerror(errno
));
249 *errorp
= _nscd_cfg_make_error(
250 NSCD_CFG_FILE_OPEN_ERROR
, msg
);
252 _NSCD_LOG(NSCD_LOG_CONFIG
, NSCD_LOG_LEVEL_ERROR
)
255 return (NSCD_CFG_FILE_OPEN_ERROR
);
260 while (fgets(buffer
, sizeof (buffer
), in
) != NULL
) {
264 /* skip blank or comment lines */
265 if (buffer
[0] == '#' || buffer
[0] == '\n')
267 /* skip end of line comment */
268 if ((ce
= strchr(buffer
, '\n')) != NULL
)
272 if ((ce1
= strchr(buffer
, '#')) != NULL
) {
276 if ((cc
= strchr(buffer
, ':')) == NULL
) {
278 while (isalpha(*c1
) && c1
< ce
)
282 else /* blank line */
286 * data name goes before ':',
287 * skip spaces on both ends
290 while (buffer
<= c2
&& isspace(*c2
))
293 while (c1
<= cc
&& isspace(*c1
))
302 * nss config goes after ':',
303 * skip spaces on both ends
306 while (c1
<= ce
&& isspace(*c1
))
309 while (cc
<= c2
&& isspace(*c2
))
312 /* no source specified, it's OK */
322 if (syntax_err
== 1) {
324 (void) snprintf(msg
, sizeof (msg
),
325 gettext("Syntax error: line %d of configuration "
326 "file: %s : \"%s\""), linecnt
, filename
, buffer
);
328 *errorp
= _nscd_cfg_make_error(
329 NSCD_CFG_SYNTAX_ERROR
, msg
);
331 _NSCD_LOG(NSCD_LOG_CONFIG
, NSCD_LOG_LEVEL_ERROR
)
334 rc
= NSCD_CFG_SYNTAX_ERROR
;
338 rc
= _nscd_cfg_get_handle(pname
, db
, &h
, errorp
);
339 if (rc
!= NSCD_SUCCESS
) {
340 /* ignore unsupported switch database */
341 if (rc
== NSCD_CFG_UNSUPPORTED_SWITCH_DB
) {
342 _nscd_cfg_free_error(*errorp
);
350 pdesc
= _nscd_cfg_get_desc(h
);
352 /* convert string to data */
353 rc
= _nscd_cfg_str_to_data(pdesc
, nsscfg
, &u
.data
,
355 if (rc
!= NSCD_SUCCESS
)
358 /* do preliminary check based on data type */
359 rc
= _nscd_cfg_prelim_check(pdesc
, data_p
, errorp
);
360 if (rc
!= NSCD_SUCCESS
)
363 rc
= _nscd_cfg_set_linked(h
, data_p
, errorp
);
364 _nscd_cfg_free_handle(h
);
366 if (rc
!= NSCD_CFG_READ_ONLY
&& rc
!= NSCD_SUCCESS
)
369 _nscd_cfg_free_error(*errorp
);
373 /* NSCD_CFG_READ_ONLY is not fatal */
374 if (rc
== NSCD_CFG_READ_ONLY
)
378 _nscd_cfg_free_handle(h
);
382 if (msg
[0] == '\0' && rc
!= NSCD_SUCCESS
) {
384 _NSCD_LOG(NSCD_LOG_CONFIG
, NSCD_LOG_LEVEL_ERROR
)
385 (me
, "%s\n", NSCD_ERR2MSG(*errorp
));