1 /*
2 * CDDL HEADER START
3 *
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.
7 *
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.
12 *
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]
18 *
19 * CDDL HEADER END
20 */
21
22 /*
23 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
25 */
26
27 #include <stdlib.h>
28 #include <sys/types.h>
29 #include <string.h>
30 #include "rtc.h"
31 #include "_conv.h"
32 #include "config_msg.h"
33
34 #define FEATSZ CONV_EXPN_FIELD_DEF_PREFIX_SIZE + \
35 MSG_CONF_EDLIBPATH_SIZE + CONV_EXPN_FIELD_DEF_SEP_SIZE + \
36 MSG_CONF_ESLIBPATH_SIZE + CONV_EXPN_FIELD_DEF_SEP_SIZE + \
37 MSG_CONF_ADLIBPATH_SIZE + CONV_EXPN_FIELD_DEF_SEP_SIZE + \
38 MSG_CONF_ASLIBPATH_SIZE + CONV_EXPN_FIELD_DEF_SEP_SIZE + \
39 MSG_CONF_DIRCFG_SIZE + CONV_EXPN_FIELD_DEF_SEP_SIZE + \
40 MSG_CONF_OBJALT_SIZE + CONV_EXPN_FIELD_DEF_SEP_SIZE + \
41 MSG_CONF_MEMRESV_SIZE + CONV_EXPN_FIELD_DEF_SEP_SIZE + \
42 MSG_CONF_ENVS_SIZE + CONV_EXPN_FIELD_DEF_SEP_SIZE + \
43 MSG_CONF_FLTR_SIZE + CONV_EXPN_FIELD_DEF_SEP_SIZE + \
44 CONV_INV_BUFSIZE + CONV_EXPN_FIELD_DEF_SUFFIX_SIZE
45
46 /*
47 * Ensure that Conv_config_feat_buf_t is large enough:
48 *
49 * FEATSZ is the real minimum size of the buffer required by conv_config_feat().
50 * However, Conv_config_feat_buf_t uses CONV_CONFIG_FEAT_BUFSIZE to set the
51 * buffer size. We do things this way because the definition of FEATSZ uses
52 * information that is not available in the environment of other programs
53 * that include the conv.h header file.
54 */
55 #if (CONV_CONFIG_FEAT_BUFSIZE != FEATSZ) && !defined(__lint)
56 #define REPORT_BUFSIZE FEATSZ
57 #include "report_bufsize.h"
58 #error "CONV_CONFIG_FEAT_BUFSIZE does not match FEATSZ"
59 #endif
60
61 /*
62 * String conversion routine for configuration file information.
63 */
64 const char *
conv_config_feat(int features,Conv_config_feat_buf_t * config_feat_buf)65 conv_config_feat(int features, Conv_config_feat_buf_t *config_feat_buf)
66 {
67 static const Val_desc vda[] = {
68 { CONF_EDLIBPATH, MSG_CONF_EDLIBPATH },
69 { CONF_ESLIBPATH, MSG_CONF_ESLIBPATH },
70 { CONF_ADLIBPATH, MSG_CONF_ADLIBPATH },
71 { CONF_ASLIBPATH, MSG_CONF_ASLIBPATH },
72 { CONF_DIRCFG, MSG_CONF_DIRCFG },
73 { CONF_OBJALT, MSG_CONF_OBJALT },
74 { CONF_MEMRESV, MSG_CONF_MEMRESV },
75 { CONF_ENVS, MSG_CONF_ENVS },
76 { CONF_FLTR, MSG_CONF_FLTR },
77 { 0, 0 }
78 };
79 static CONV_EXPN_FIELD_ARG conv_arg = {
80 NULL, sizeof (config_feat_buf->buf) };
81
82 conv_arg.buf = config_feat_buf->buf;
83 conv_arg.oflags = conv_arg.rflags = features;
84 (void) conv_expn_field(&conv_arg, vda, 0);
85
86 return ((const char *)config_feat_buf->buf);
87 }
88
89 #define FLAGSZ CONV_EXPN_FIELD_DEF_PREFIX_SIZE + \
90 MSG_CONF_DIRENT_SIZE + CONV_EXPN_FIELD_DEF_SEP_SIZE + \
91 MSG_CONF_ALLENTS_SIZE + CONV_EXPN_FIELD_DEF_SEP_SIZE + \
92 MSG_CONF_NOEXIST_SIZE + CONV_EXPN_FIELD_DEF_SEP_SIZE + \
93 MSG_CONF_EXEC_SIZE + CONV_EXPN_FIELD_DEF_SEP_SIZE + \
94 MSG_CONF_ALTER_SIZE + CONV_EXPN_FIELD_DEF_SEP_SIZE + \
95 MSG_CONF_OPTIONAL_SIZE + CONV_EXPN_FIELD_DEF_SEP_SIZE + \
96 MSG_CONF_DUMP_SIZE + CONV_EXPN_FIELD_DEF_SEP_SIZE + \
97 MSG_CONF_REALPATH_SIZE + CONV_EXPN_FIELD_DEF_SEP_SIZE + \
98 MSG_CONF_NOALTER_SIZE + CONV_EXPN_FIELD_DEF_SEP_SIZE + \
99 MSG_CONF_GROUP_SIZE + CONV_EXPN_FIELD_DEF_SEP_SIZE + \
100 MSG_CONF_APP_SIZE + CONV_EXPN_FIELD_DEF_SEP_SIZE + \
101 MSG_CONF_CMDLINE_SIZE + CONV_EXPN_FIELD_DEF_SEP_SIZE + \
102 MSG_CONF_FILTER_SIZE + CONV_EXPN_FIELD_DEF_SEP_SIZE + \
103 MSG_CONF_FILTEE_SIZE + CONV_EXPN_FIELD_DEF_SEP_SIZE + \
104 CONV_INV_BUFSIZE + CONV_EXPN_FIELD_DEF_SUFFIX_SIZE
105
106 /*
107 * Ensure that Conv_config_obj_buf_t is large enough:
108 *
109 * FLAGSZ is the real minimum size of the buffer required by conv_config_obj().
110 * However, Conv_config_obj_buf_t uses CONV_CONFIG_OBJ_BUFSIZE to set the
111 * buffer size. We do things this way because the definition of FLAGSZ uses
112 * information that is not available in the environment of other programs
113 * that include the conv.h header file.
114 */
115 #if (CONV_CONFIG_OBJ_BUFSIZE != FLAGSZ) && !defined(__lint)
116 #define REPORT_BUFSIZE FLAGSZ
117 #include "report_bufsize.h"
118 #error "CONV_CONFIG_OBJ_BUFSIZE does not match FLAGSZ"
119 #endif
120
121 /*
122 * String conversion routine for object flags.
123 */
124 const char *
conv_config_obj(ushort_t flags,Conv_config_obj_buf_t * config_obj_buf)125 conv_config_obj(ushort_t flags, Conv_config_obj_buf_t *config_obj_buf)
126 {
127 static const Val_desc vda[] = {
128 { RTC_OBJ_DIRENT, MSG_CONF_DIRENT },
129 { RTC_OBJ_ALLENTS, MSG_CONF_ALLENTS },
130 { RTC_OBJ_NOEXIST, MSG_CONF_NOEXIST },
131 { RTC_OBJ_EXEC, MSG_CONF_EXEC },
132 { RTC_OBJ_ALTER, MSG_CONF_ALTER },
133 { RTC_OBJ_DUMP, MSG_CONF_DUMP },
134 { RTC_OBJ_NOALTER, MSG_CONF_NOALTER },
135 { RTC_OBJ_REALPTH, MSG_CONF_REALPATH },
136 { RTC_OBJ_GROUP, MSG_CONF_GROUP },
137 { RTC_OBJ_APP, MSG_CONF_APP },
138 { RTC_OBJ_CMDLINE, MSG_CONF_CMDLINE },
139 { RTC_OBJ_FILTER, MSG_CONF_FILTER },
140 { RTC_OBJ_FILTEE, MSG_CONF_FILTEE },
141 { 0, 0 }
142 };
143 static const char *leading_str_arr[2];
144 static CONV_EXPN_FIELD_ARG conv_arg = {
145 NULL, sizeof (config_obj_buf->buf), leading_str_arr };
146
147 const char **lstr = leading_str_arr;
148
149 if ((flags == 0) || (flags == RTC_OBJ_OPTINAL))
150 return (MSG_ORIG(MSG_GBL_NULL));
151
152 conv_arg.buf = config_obj_buf->buf;
153 conv_arg.rflags = flags;
154
155 /*
156 * Print an alternative-optional object simply as optional.
157 */
158 if ((flags & (RTC_OBJ_ALTER | RTC_OBJ_OPTINAL)) ==
159 (RTC_OBJ_ALTER | RTC_OBJ_OPTINAL)) {
160 *lstr++ = MSG_ORIG(MSG_CONF_OPTIONAL);
161 conv_arg.rflags &= ~(RTC_OBJ_ALTER | RTC_OBJ_OPTINAL);
162 }
163 *lstr = NULL;
164 conv_arg.oflags = conv_arg.rflags &= ~RTC_OBJ_OPTINAL;
165
166 (void) conv_expn_field(&conv_arg, vda, 0);
167
168 return ((const char *)config_obj_buf->buf);
169 }
170
171 /*
172 * Determine whether and old pathname exists within a search path string,
173 * without a new pathname, i.e., does the search path string contain "/usr/lib"
174 * but not "/lib". If so, add the new pathname before the old pathname. For
175 * example, convert:
176 *
177 * /local/lib:/opt/sfw/lib:/usr/lib
178 * to:
179 * /local/lib:/opt/sfw/lib:/lib:/usr/lib
180 */
181 const char *
conv_config_upm(const char * str,const char * old,const char * new,size_t newlen)182 conv_config_upm(const char *str, const char *old, const char *new,
183 size_t newlen)
184 {
185 const char *curstr, *ptr;
186 const char *curold = 0, *curnew = 0;
187 const char *ptrold = old, * ptrnew = new;
188 int chkold = 1, chknew = 1;
189
190 for (curstr = ptr = str; *ptr; ptr++) {
191 if (*ptr == ':') {
192 /*
193 * We've come to the end of a token within the string.
194 */
195 if ((uintptr_t)ptr - (uintptr_t)curstr) {
196 /*
197 * If the old or new string checking is still
198 * enabled, we've found a match.
199 */
200 if (chkold)
201 curold = curstr;
202 if (chknew)
203 curnew = curstr;
204 }
205 curstr = (char *)(ptr + 1);
206
207 /*
208 * If an old or new string hasn't yet been matched,
209 * re-enable the checking for either.
210 */
211 if (curold == 0) {
212 ptrold = old;
213 chkold = 1;
214 }
215 if (curnew == 0) {
216 ptrnew = new;
217 chknew = 1;
218 }
219 continue;
220 }
221
222 /*
223 * Determine if the current token matches the old or new string.
224 * If not, disable the checking for each string.
225 */
226 if (chkold && (*ptr != *ptrold++))
227 chkold = 0;
228 if (chknew && (*ptr != *ptrnew++))
229 chknew = 0;
230 }
231
232 /*
233 * We've come to the end of the string, if the old or new string
234 * checking is still enabled, we've found a match.
235 */
236 if ((uintptr_t)ptr - (uintptr_t)curstr) {
237 if (chkold)
238 curold = curstr;
239 if (chknew)
240 curnew = curstr;
241 }
242
243 /*
244 * If an old string hasn't been found, or it has and a new string has
245 * been found, return the original string.
246 */
247 if ((curold == 0) || curnew)
248 return (str);
249 else {
250 char *newstr;
251 size_t len;
252
253 /*
254 * Allocate a new string, enlarged to accommodate the new string
255 * that will be inserted, and an associated separator.
256 */
257 if ((curstr = malloc(newlen + 2 +
258 (uintptr_t)ptr - (uintptr_t)str)) == 0)
259 return (str);
260
261 newstr = (char *)curstr;
262 for (len = (uintptr_t)curold - (uintptr_t)str; len; len--)
263 *(newstr++) = *(str++); /* copy up to */
264 /* insertion point */
265 for (len = newlen; len; len--)
266 *(newstr++) = *(new++); /* add new string and */
267 *(newstr++) = ':'; /* separator */
268 for (len = (uintptr_t)ptr - (uintptr_t)str; len; len--)
269 *(newstr++) = *(str++); /* add remaining */
270 *(newstr++) = '\0'; /* string */
271
272 return (curstr);
273 }
274 }
275