17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
55aefb655Srie  * Common Development and Distribution License (the "License").
65aefb655Srie  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
215aefb655Srie 
227c478bd9Sstevel@tonic-gate /*
23*4f680cc6SAli Bahrami  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #include	<stdlib.h>
287c478bd9Sstevel@tonic-gate #include	<sys/types.h>
297c478bd9Sstevel@tonic-gate #include	<string.h>
307c478bd9Sstevel@tonic-gate #include	"rtc.h"
317c478bd9Sstevel@tonic-gate #include	"_conv.h"
327c478bd9Sstevel@tonic-gate #include	"config_msg.h"
337c478bd9Sstevel@tonic-gate 
34ba4e3c84Sab #define	FEATSZ	CONV_EXPN_FIELD_DEF_PREFIX_SIZE + \
35ba4e3c84Sab 		MSG_CONF_EDLIBPATH_SIZE + CONV_EXPN_FIELD_DEF_SEP_SIZE + \
36ba4e3c84Sab 		MSG_CONF_ESLIBPATH_SIZE + CONV_EXPN_FIELD_DEF_SEP_SIZE + \
37ba4e3c84Sab 		MSG_CONF_ADLIBPATH_SIZE + CONV_EXPN_FIELD_DEF_SEP_SIZE + \
38ba4e3c84Sab 		MSG_CONF_ASLIBPATH_SIZE + CONV_EXPN_FIELD_DEF_SEP_SIZE + \
39ba4e3c84Sab 		MSG_CONF_DIRCFG_SIZE	+ CONV_EXPN_FIELD_DEF_SEP_SIZE + \
40ba4e3c84Sab 		MSG_CONF_OBJALT_SIZE	+ CONV_EXPN_FIELD_DEF_SEP_SIZE + \
41ba4e3c84Sab 		MSG_CONF_MEMRESV_SIZE	+ CONV_EXPN_FIELD_DEF_SEP_SIZE + \
42ba4e3c84Sab 		MSG_CONF_ENVS_SIZE	+ CONV_EXPN_FIELD_DEF_SEP_SIZE + \
43ba4e3c84Sab 		MSG_CONF_FLTR_SIZE	+ CONV_EXPN_FIELD_DEF_SEP_SIZE + \
44de777a60Sab 		CONV_INV_BUFSIZE + CONV_EXPN_FIELD_DEF_SUFFIX_SIZE
45de777a60Sab 
46de777a60Sab /*
47de777a60Sab  * Ensure that Conv_config_feat_buf_t is large enough:
48de777a60Sab  *
49de777a60Sab  * FEATSZ is the real minimum size of the buffer required by conv_config_feat().
50de777a60Sab  * However, Conv_config_feat_buf_t uses CONV_CONFIG_FEAT_BUFSIZE to set the
51de777a60Sab  * buffer size. We do things this way because the definition of FEATSZ uses
52de777a60Sab  * information that is not available in the environment of other programs
53de777a60Sab  * that include the conv.h header file.
54de777a60Sab  */
556a074c93Sab #if (CONV_CONFIG_FEAT_BUFSIZE != FEATSZ) && !defined(__lint)
566a074c93Sab #define	REPORT_BUFSIZE FEATSZ
576a074c93Sab #include "report_bufsize.h"
586a074c93Sab #error "CONV_CONFIG_FEAT_BUFSIZE does not match FEATSZ"
59de777a60Sab #endif
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate /*
627c478bd9Sstevel@tonic-gate  * String conversion routine for configuration file information.
637c478bd9Sstevel@tonic-gate  */
647c478bd9Sstevel@tonic-gate const char *
conv_config_feat(int features,Conv_config_feat_buf_t * config_feat_buf)65de777a60Sab conv_config_feat(int features, Conv_config_feat_buf_t *config_feat_buf)
667c478bd9Sstevel@tonic-gate {
67*4f680cc6SAli Bahrami 	static const Val_desc	vda[] = {
68*4f680cc6SAli Bahrami 		{ CONF_EDLIBPATH,	MSG_CONF_EDLIBPATH },
69*4f680cc6SAli Bahrami 		{ CONF_ESLIBPATH,	MSG_CONF_ESLIBPATH },
70*4f680cc6SAli Bahrami 		{ CONF_ADLIBPATH,	MSG_CONF_ADLIBPATH },
71*4f680cc6SAli Bahrami 		{ CONF_ASLIBPATH,	MSG_CONF_ASLIBPATH },
72*4f680cc6SAli Bahrami 		{ CONF_DIRCFG,		MSG_CONF_DIRCFG },
73*4f680cc6SAli Bahrami 		{ CONF_OBJALT,		MSG_CONF_OBJALT },
74*4f680cc6SAli Bahrami 		{ CONF_MEMRESV,		MSG_CONF_MEMRESV },
75*4f680cc6SAli Bahrami 		{ CONF_ENVS,		MSG_CONF_ENVS },
76*4f680cc6SAli Bahrami 		{ CONF_FLTR,		MSG_CONF_FLTR },
775aefb655Srie 		{ 0,			0 }
785aefb655Srie 	};
79de777a60Sab 	static CONV_EXPN_FIELD_ARG conv_arg = {
80*4f680cc6SAli Bahrami 	    NULL, sizeof (config_feat_buf->buf) };
817c478bd9Sstevel@tonic-gate 
82de777a60Sab 	conv_arg.buf = config_feat_buf->buf;
83ba4e3c84Sab 	conv_arg.oflags = conv_arg.rflags = features;
84*4f680cc6SAli Bahrami 	(void) conv_expn_field(&conv_arg, vda, 0);
857c478bd9Sstevel@tonic-gate 
86de777a60Sab 	return ((const char *)config_feat_buf->buf);
877c478bd9Sstevel@tonic-gate }
887c478bd9Sstevel@tonic-gate 
89ba4e3c84Sab #define	FLAGSZ	CONV_EXPN_FIELD_DEF_PREFIX_SIZE + \
90ba4e3c84Sab 		MSG_CONF_DIRENT_SIZE	+ CONV_EXPN_FIELD_DEF_SEP_SIZE + \
91ba4e3c84Sab 		MSG_CONF_ALLENTS_SIZE	+ CONV_EXPN_FIELD_DEF_SEP_SIZE + \
92ba4e3c84Sab 		MSG_CONF_NOEXIST_SIZE	+ CONV_EXPN_FIELD_DEF_SEP_SIZE + \
93ba4e3c84Sab 		MSG_CONF_EXEC_SIZE	+ CONV_EXPN_FIELD_DEF_SEP_SIZE + \
94ba4e3c84Sab 		MSG_CONF_ALTER_SIZE	+ CONV_EXPN_FIELD_DEF_SEP_SIZE + \
95ba4e3c84Sab 		MSG_CONF_OPTIONAL_SIZE	+ CONV_EXPN_FIELD_DEF_SEP_SIZE + \
96ba4e3c84Sab 		MSG_CONF_DUMP_SIZE	+ CONV_EXPN_FIELD_DEF_SEP_SIZE + \
97ba4e3c84Sab 		MSG_CONF_REALPATH_SIZE	+ CONV_EXPN_FIELD_DEF_SEP_SIZE + \
98ba4e3c84Sab 		MSG_CONF_NOALTER_SIZE	+ CONV_EXPN_FIELD_DEF_SEP_SIZE + \
99ba4e3c84Sab 		MSG_CONF_GROUP_SIZE	+ CONV_EXPN_FIELD_DEF_SEP_SIZE + \
100ba4e3c84Sab 		MSG_CONF_APP_SIZE	+ CONV_EXPN_FIELD_DEF_SEP_SIZE + \
101ba4e3c84Sab 		MSG_CONF_CMDLINE_SIZE	+ CONV_EXPN_FIELD_DEF_SEP_SIZE + \
102ba4e3c84Sab 		MSG_CONF_FILTER_SIZE	+ CONV_EXPN_FIELD_DEF_SEP_SIZE + \
103ba4e3c84Sab 		MSG_CONF_FILTEE_SIZE	+ CONV_EXPN_FIELD_DEF_SEP_SIZE + \
104de777a60Sab 		CONV_INV_BUFSIZE + CONV_EXPN_FIELD_DEF_SUFFIX_SIZE
105de777a60Sab 
106de777a60Sab /*
107de777a60Sab  * Ensure that Conv_config_obj_buf_t is large enough:
108de777a60Sab  *
109de777a60Sab  * FLAGSZ is the real minimum size of the buffer required by conv_config_obj().
110de777a60Sab  * However, Conv_config_obj_buf_t uses CONV_CONFIG_OBJ_BUFSIZE to set the
111de777a60Sab  * buffer size. We do things this way because the definition of FLAGSZ uses
112de777a60Sab  * information that is not available in the environment of other programs
113de777a60Sab  * that include the conv.h header file.
114de777a60Sab  */
1156a074c93Sab #if (CONV_CONFIG_OBJ_BUFSIZE != FLAGSZ) && !defined(__lint)
1166a074c93Sab #define	REPORT_BUFSIZE FLAGSZ
1176a074c93Sab #include "report_bufsize.h"
1186a074c93Sab #error "CONV_CONFIG_OBJ_BUFSIZE does not match FLAGSZ"
119de777a60Sab #endif
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate /*
1227c478bd9Sstevel@tonic-gate  * String conversion routine for object flags.
1237c478bd9Sstevel@tonic-gate  */
1247c478bd9Sstevel@tonic-gate const char *
conv_config_obj(ushort_t flags,Conv_config_obj_buf_t * config_obj_buf)125de777a60Sab conv_config_obj(ushort_t flags, Conv_config_obj_buf_t *config_obj_buf)
1267c478bd9Sstevel@tonic-gate {
127*4f680cc6SAli Bahrami 	static const Val_desc vda[] = {
128*4f680cc6SAli Bahrami 		{ RTC_OBJ_DIRENT,	MSG_CONF_DIRENT },
129*4f680cc6SAli Bahrami 		{ RTC_OBJ_ALLENTS,	MSG_CONF_ALLENTS },
130*4f680cc6SAli Bahrami 		{ RTC_OBJ_NOEXIST,	MSG_CONF_NOEXIST },
131*4f680cc6SAli Bahrami 		{ RTC_OBJ_EXEC,		MSG_CONF_EXEC },
132*4f680cc6SAli Bahrami 		{ RTC_OBJ_ALTER,	MSG_CONF_ALTER },
133*4f680cc6SAli Bahrami 		{ RTC_OBJ_DUMP,		MSG_CONF_DUMP },
134*4f680cc6SAli Bahrami 		{ RTC_OBJ_NOALTER,	MSG_CONF_NOALTER },
135*4f680cc6SAli Bahrami 		{ RTC_OBJ_REALPTH,	MSG_CONF_REALPATH },
136*4f680cc6SAli Bahrami 		{ RTC_OBJ_GROUP,	MSG_CONF_GROUP },
137*4f680cc6SAli Bahrami 		{ RTC_OBJ_APP,		MSG_CONF_APP },
138*4f680cc6SAli Bahrami 		{ RTC_OBJ_CMDLINE,	MSG_CONF_CMDLINE },
139*4f680cc6SAli Bahrami 		{ RTC_OBJ_FILTER,	MSG_CONF_FILTER },
140*4f680cc6SAli Bahrami 		{ RTC_OBJ_FILTEE,	MSG_CONF_FILTEE },
1415aefb655Srie 		{ 0,			0 }
1425aefb655Srie 	};
143ba4e3c84Sab 	static const char *leading_str_arr[2];
144de777a60Sab 	static CONV_EXPN_FIELD_ARG conv_arg = {
145*4f680cc6SAli Bahrami 	    NULL, sizeof (config_obj_buf->buf), leading_str_arr };
146ba4e3c84Sab 
147ba4e3c84Sab 	const char **lstr = leading_str_arr;
1485aefb655Srie 
1495aefb655Srie 	if ((flags == 0) || (flags == RTC_OBJ_OPTINAL))
1505aefb655Srie 		return (MSG_ORIG(MSG_GBL_NULL));
1517c478bd9Sstevel@tonic-gate 
152de777a60Sab 	conv_arg.buf = config_obj_buf->buf;
153ba4e3c84Sab 	conv_arg.rflags = flags;
1547c478bd9Sstevel@tonic-gate 
1555aefb655Srie 	/*
1565aefb655Srie 	 * Print an alternative-optional object simply as optional.
1575aefb655Srie 	 */
1585aefb655Srie 	if ((flags & (RTC_OBJ_ALTER | RTC_OBJ_OPTINAL)) ==
1595aefb655Srie 	    (RTC_OBJ_ALTER | RTC_OBJ_OPTINAL)) {
160ba4e3c84Sab 		*lstr++ = MSG_ORIG(MSG_CONF_OPTIONAL);
161ba4e3c84Sab 		conv_arg.rflags &= ~(RTC_OBJ_ALTER | RTC_OBJ_OPTINAL);
1627c478bd9Sstevel@tonic-gate 	}
163ba4e3c84Sab 	*lstr = NULL;
164ba4e3c84Sab 	conv_arg.oflags = conv_arg.rflags &= ~RTC_OBJ_OPTINAL;
1657c478bd9Sstevel@tonic-gate 
166*4f680cc6SAli Bahrami 	(void) conv_expn_field(&conv_arg, vda, 0);
1677c478bd9Sstevel@tonic-gate 
168de777a60Sab 	return ((const char *)config_obj_buf->buf);
1697c478bd9Sstevel@tonic-gate }
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate /*
1727c478bd9Sstevel@tonic-gate  * Determine whether and old pathname exists within a search path string,
1737c478bd9Sstevel@tonic-gate  * without a new pathname, i.e., does the search path string contain "/usr/lib"
1747c478bd9Sstevel@tonic-gate  * but not "/lib".  If so, add the new pathname before the old pathname.  For
1757c478bd9Sstevel@tonic-gate  * example, convert:
1767c478bd9Sstevel@tonic-gate  *
1777c478bd9Sstevel@tonic-gate  *	/local/lib:/opt/sfw/lib:/usr/lib
1787c478bd9Sstevel@tonic-gate  * to:
1797c478bd9Sstevel@tonic-gate  *	/local/lib:/opt/sfw/lib:/lib:/usr/lib
1807c478bd9Sstevel@tonic-gate  */
1817c478bd9Sstevel@tonic-gate const char *
conv_config_upm(const char * str,const char * old,const char * new,size_t newlen)1825aefb655Srie conv_config_upm(const char *str, const char *old, const char *new,
1837c478bd9Sstevel@tonic-gate     size_t newlen)
1847c478bd9Sstevel@tonic-gate {
1857c478bd9Sstevel@tonic-gate 	const char	*curstr, *ptr;
1867c478bd9Sstevel@tonic-gate 	const char	*curold = 0, *curnew = 0;
1877c478bd9Sstevel@tonic-gate 	const char	*ptrold = old, * ptrnew = new;
1887c478bd9Sstevel@tonic-gate 	int		chkold = 1, chknew = 1;
1897c478bd9Sstevel@tonic-gate 
1907c478bd9Sstevel@tonic-gate 	for (curstr = ptr = str; *ptr; ptr++) {
1917c478bd9Sstevel@tonic-gate 		if (*ptr == ':') {
1927c478bd9Sstevel@tonic-gate 			/*
1937c478bd9Sstevel@tonic-gate 			 * We've come to the end of a token within the string.
1947c478bd9Sstevel@tonic-gate 			 */
1957c478bd9Sstevel@tonic-gate 			if ((uintptr_t)ptr - (uintptr_t)curstr) {
1967c478bd9Sstevel@tonic-gate 				/*
1977c478bd9Sstevel@tonic-gate 				 * If the old or new string checking is still
1987c478bd9Sstevel@tonic-gate 				 * enabled, we've found a match.
1997c478bd9Sstevel@tonic-gate 				 */
2007c478bd9Sstevel@tonic-gate 				if (chkold)
2017c478bd9Sstevel@tonic-gate 					curold = curstr;
2027c478bd9Sstevel@tonic-gate 				if (chknew)
2037c478bd9Sstevel@tonic-gate 					curnew = curstr;
2047c478bd9Sstevel@tonic-gate 			}
2057c478bd9Sstevel@tonic-gate 			curstr = (char *)(ptr + 1);
2067c478bd9Sstevel@tonic-gate 
2077c478bd9Sstevel@tonic-gate 			/*
2087c478bd9Sstevel@tonic-gate 			 * If an old or new string hasn't yet been matched,
2097c478bd9Sstevel@tonic-gate 			 * re-enable the checking for either.
2107c478bd9Sstevel@tonic-gate 			 */
2117c478bd9Sstevel@tonic-gate 			if (curold == 0) {
2127c478bd9Sstevel@tonic-gate 				ptrold = old;
2137c478bd9Sstevel@tonic-gate 				chkold = 1;
2147c478bd9Sstevel@tonic-gate 			}
2157c478bd9Sstevel@tonic-gate 			if (curnew == 0) {
2167c478bd9Sstevel@tonic-gate 				ptrnew = new;
2177c478bd9Sstevel@tonic-gate 				chknew = 1;
2187c478bd9Sstevel@tonic-gate 			}
2197c478bd9Sstevel@tonic-gate 			continue;
2207c478bd9Sstevel@tonic-gate 		}
2217c478bd9Sstevel@tonic-gate 
2227c478bd9Sstevel@tonic-gate 		/*
2237c478bd9Sstevel@tonic-gate 		 * Determine if the current token matches the old or new string.
2247c478bd9Sstevel@tonic-gate 		 * If not, disable the checking for each string.
2257c478bd9Sstevel@tonic-gate 		 */
2267c478bd9Sstevel@tonic-gate 		if (chkold && (*ptr != *ptrold++))
2277c478bd9Sstevel@tonic-gate 			chkold = 0;
2287c478bd9Sstevel@tonic-gate 		if (chknew && (*ptr != *ptrnew++))
2297c478bd9Sstevel@tonic-gate 			chknew = 0;
2307c478bd9Sstevel@tonic-gate 	}
2317c478bd9Sstevel@tonic-gate 
2327c478bd9Sstevel@tonic-gate 	/*
2337c478bd9Sstevel@tonic-gate 	 * We've come to the end of the string, if the old or new string
2347c478bd9Sstevel@tonic-gate 	 * checking is still enabled, we've found a match.
2357c478bd9Sstevel@tonic-gate 	 */
2367c478bd9Sstevel@tonic-gate 	if ((uintptr_t)ptr - (uintptr_t)curstr) {
2377c478bd9Sstevel@tonic-gate 		if (chkold)
2387c478bd9Sstevel@tonic-gate 			curold = curstr;
2397c478bd9Sstevel@tonic-gate 		if (chknew)
2407c478bd9Sstevel@tonic-gate 			curnew = curstr;
2417c478bd9Sstevel@tonic-gate 	}
2427c478bd9Sstevel@tonic-gate 
2437c478bd9Sstevel@tonic-gate 	/*
2447c478bd9Sstevel@tonic-gate 	 * If an old string hasn't been found, or it has and a new string has
2457c478bd9Sstevel@tonic-gate 	 * been found, return the original string.
2467c478bd9Sstevel@tonic-gate 	 */
2477c478bd9Sstevel@tonic-gate 	if ((curold == 0) || curnew)
2487c478bd9Sstevel@tonic-gate 		return (str);
2497c478bd9Sstevel@tonic-gate 	else {
2507c478bd9Sstevel@tonic-gate 		char	*newstr;
2517c478bd9Sstevel@tonic-gate 		size_t	len;
2527c478bd9Sstevel@tonic-gate 
2537c478bd9Sstevel@tonic-gate 		/*
2547c478bd9Sstevel@tonic-gate 		 * Allocate a new string, enlarged to accommodate the new string
2557c478bd9Sstevel@tonic-gate 		 * that will be inserted, and an associated separator.
2567c478bd9Sstevel@tonic-gate 		 */
2577c478bd9Sstevel@tonic-gate 		if ((curstr = malloc(newlen + 2 +
2587c478bd9Sstevel@tonic-gate 		    (uintptr_t)ptr - (uintptr_t)str)) == 0)
2597c478bd9Sstevel@tonic-gate 			return (str);
2607c478bd9Sstevel@tonic-gate 
2617c478bd9Sstevel@tonic-gate 		newstr = (char *)curstr;
2627c478bd9Sstevel@tonic-gate 		for (len = (uintptr_t)curold - (uintptr_t)str; len; len--)
2637c478bd9Sstevel@tonic-gate 			*(newstr++) = *(str++);		/* copy up to */
2647c478bd9Sstevel@tonic-gate 							/*    insertion point */
2657c478bd9Sstevel@tonic-gate 		for (len = newlen; len; len--)
2667c478bd9Sstevel@tonic-gate 			*(newstr++) = *(new++);		/* add new string and */
2677c478bd9Sstevel@tonic-gate 		*(newstr++) = ':';			/*    separator */
2687c478bd9Sstevel@tonic-gate 		for (len = (uintptr_t)ptr - (uintptr_t)str; len; len--)
2697c478bd9Sstevel@tonic-gate 			*(newstr++) = *(str++);		/* add remaining */
2707c478bd9Sstevel@tonic-gate 		*(newstr++) = '\0';			/*	string */
2717c478bd9Sstevel@tonic-gate 
2727c478bd9Sstevel@tonic-gate 		return (curstr);
2737c478bd9Sstevel@tonic-gate 	}
2747c478bd9Sstevel@tonic-gate }
275