10Sstevel@tonic-gate /* 20Sstevel@tonic-gate * CDDL HEADER START 30Sstevel@tonic-gate * 40Sstevel@tonic-gate * The contents of this file are subject to the terms of the 51618Srie * Common Development and Distribution License (the "License"). 61618Srie * You may not use this file except in compliance with the License. 70Sstevel@tonic-gate * 80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 100Sstevel@tonic-gate * See the License for the specific language governing permissions 110Sstevel@tonic-gate * and limitations under the License. 120Sstevel@tonic-gate * 130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 180Sstevel@tonic-gate * 190Sstevel@tonic-gate * CDDL HEADER END 200Sstevel@tonic-gate */ 211618Srie 220Sstevel@tonic-gate /* 234734Sab196087 * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 240Sstevel@tonic-gate * Use is subject to license terms. 250Sstevel@tonic-gate */ 260Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 270Sstevel@tonic-gate 280Sstevel@tonic-gate #include <stdlib.h> 290Sstevel@tonic-gate #include <sys/types.h> 300Sstevel@tonic-gate #include <string.h> 310Sstevel@tonic-gate #include "rtc.h" 320Sstevel@tonic-gate #include "_conv.h" 330Sstevel@tonic-gate #include "config_msg.h" 340Sstevel@tonic-gate 352352Sab196087 #define FEATSZ CONV_EXPN_FIELD_DEF_PREFIX_SIZE + \ 362352Sab196087 MSG_CONF_EDLIBPATH_SIZE + CONV_EXPN_FIELD_DEF_SEP_SIZE + \ 372352Sab196087 MSG_CONF_ESLIBPATH_SIZE + CONV_EXPN_FIELD_DEF_SEP_SIZE + \ 382352Sab196087 MSG_CONF_ADLIBPATH_SIZE + CONV_EXPN_FIELD_DEF_SEP_SIZE + \ 392352Sab196087 MSG_CONF_ASLIBPATH_SIZE + CONV_EXPN_FIELD_DEF_SEP_SIZE + \ 402352Sab196087 MSG_CONF_DIRCFG_SIZE + CONV_EXPN_FIELD_DEF_SEP_SIZE + \ 412352Sab196087 MSG_CONF_OBJALT_SIZE + CONV_EXPN_FIELD_DEF_SEP_SIZE + \ 422352Sab196087 MSG_CONF_MEMRESV_SIZE + CONV_EXPN_FIELD_DEF_SEP_SIZE + \ 432352Sab196087 MSG_CONF_ENVS_SIZE + CONV_EXPN_FIELD_DEF_SEP_SIZE + \ 442352Sab196087 MSG_CONF_FLTR_SIZE + CONV_EXPN_FIELD_DEF_SEP_SIZE + \ 454734Sab196087 CONV_INV_BUFSIZE + CONV_EXPN_FIELD_DEF_SUFFIX_SIZE 464734Sab196087 474734Sab196087 /* 484734Sab196087 * Ensure that Conv_config_feat_buf_t is large enough: 494734Sab196087 * 504734Sab196087 * FEATSZ is the real minimum size of the buffer required by conv_config_feat(). 514734Sab196087 * However, Conv_config_feat_buf_t uses CONV_CONFIG_FEAT_BUFSIZE to set the 524734Sab196087 * buffer size. We do things this way because the definition of FEATSZ uses 534734Sab196087 * information that is not available in the environment of other programs 544734Sab196087 * that include the conv.h header file. 554734Sab196087 */ 564734Sab196087 #if (CONV_CONFIG_FEAT_BUFSIZE < FEATSZ) && !defined(__lint) 574734Sab196087 #error "CONV_CONFIG_FEAT_BUFSIZE is not large enough" 584734Sab196087 #endif 590Sstevel@tonic-gate 600Sstevel@tonic-gate /* 610Sstevel@tonic-gate * String conversion routine for configuration file information. 620Sstevel@tonic-gate */ 630Sstevel@tonic-gate const char * 644734Sab196087 conv_config_feat(int features, Conv_config_feat_buf_t *config_feat_buf) 650Sstevel@tonic-gate { 661618Srie static Val_desc vda[] = { 671618Srie { CONF_EDLIBPATH, MSG_ORIG(MSG_CONF_EDLIBPATH) }, 681618Srie { CONF_ESLIBPATH, MSG_ORIG(MSG_CONF_ESLIBPATH) }, 691618Srie { CONF_ADLIBPATH, MSG_ORIG(MSG_CONF_ADLIBPATH) }, 701618Srie { CONF_ASLIBPATH, MSG_ORIG(MSG_CONF_ASLIBPATH) }, 711618Srie { CONF_DIRCFG, MSG_ORIG(MSG_CONF_DIRCFG) }, 721618Srie { CONF_OBJALT, MSG_ORIG(MSG_CONF_OBJALT) }, 731618Srie { CONF_MEMRESV, MSG_ORIG(MSG_CONF_MEMRESV) }, 741618Srie { CONF_ENVS, MSG_ORIG(MSG_CONF_ENVS) }, 751618Srie { CONF_FLTR, MSG_ORIG(MSG_CONF_FLTR) }, 761618Srie { 0, 0 } 771618Srie }; 784734Sab196087 static CONV_EXPN_FIELD_ARG conv_arg = { 794734Sab196087 NULL, sizeof (config_feat_buf->buf), vda }; 800Sstevel@tonic-gate 814734Sab196087 conv_arg.buf = config_feat_buf->buf; 822352Sab196087 conv_arg.oflags = conv_arg.rflags = features; 83*5088Sab196087 (void) conv_expn_field(&conv_arg, 0); 840Sstevel@tonic-gate 854734Sab196087 return ((const char *)config_feat_buf->buf); 860Sstevel@tonic-gate } 870Sstevel@tonic-gate 882352Sab196087 #define FLAGSZ CONV_EXPN_FIELD_DEF_PREFIX_SIZE + \ 892352Sab196087 MSG_CONF_DIRENT_SIZE + CONV_EXPN_FIELD_DEF_SEP_SIZE + \ 902352Sab196087 MSG_CONF_ALLENTS_SIZE + CONV_EXPN_FIELD_DEF_SEP_SIZE + \ 912352Sab196087 MSG_CONF_NOEXIST_SIZE + CONV_EXPN_FIELD_DEF_SEP_SIZE + \ 922352Sab196087 MSG_CONF_EXEC_SIZE + CONV_EXPN_FIELD_DEF_SEP_SIZE + \ 932352Sab196087 MSG_CONF_ALTER_SIZE + CONV_EXPN_FIELD_DEF_SEP_SIZE + \ 942352Sab196087 MSG_CONF_OPTIONAL_SIZE + CONV_EXPN_FIELD_DEF_SEP_SIZE + \ 952352Sab196087 MSG_CONF_DUMP_SIZE + CONV_EXPN_FIELD_DEF_SEP_SIZE + \ 962352Sab196087 MSG_CONF_REALPATH_SIZE + CONV_EXPN_FIELD_DEF_SEP_SIZE + \ 972352Sab196087 MSG_CONF_NOALTER_SIZE + CONV_EXPN_FIELD_DEF_SEP_SIZE + \ 982352Sab196087 MSG_CONF_GROUP_SIZE + CONV_EXPN_FIELD_DEF_SEP_SIZE + \ 992352Sab196087 MSG_CONF_APP_SIZE + CONV_EXPN_FIELD_DEF_SEP_SIZE + \ 1002352Sab196087 MSG_CONF_CMDLINE_SIZE + CONV_EXPN_FIELD_DEF_SEP_SIZE + \ 1012352Sab196087 MSG_CONF_FILTER_SIZE + CONV_EXPN_FIELD_DEF_SEP_SIZE + \ 1022352Sab196087 MSG_CONF_FILTEE_SIZE + CONV_EXPN_FIELD_DEF_SEP_SIZE + \ 1034734Sab196087 CONV_INV_BUFSIZE + CONV_EXPN_FIELD_DEF_SUFFIX_SIZE 1044734Sab196087 1054734Sab196087 /* 1064734Sab196087 * Ensure that Conv_config_obj_buf_t is large enough: 1074734Sab196087 * 1084734Sab196087 * FLAGSZ is the real minimum size of the buffer required by conv_config_obj(). 1094734Sab196087 * However, Conv_config_obj_buf_t uses CONV_CONFIG_OBJ_BUFSIZE to set the 1104734Sab196087 * buffer size. We do things this way because the definition of FLAGSZ uses 1114734Sab196087 * information that is not available in the environment of other programs 1124734Sab196087 * that include the conv.h header file. 1134734Sab196087 */ 1144734Sab196087 #if (CONV_CONFIG_OBJ_BUFSIZE < FLAGSZ) && !defined(__lint) 1154734Sab196087 #error "CONV_CONFIG_OBJ_BUFSIZE is not large enough" 1164734Sab196087 #endif 1170Sstevel@tonic-gate 1180Sstevel@tonic-gate /* 1190Sstevel@tonic-gate * String conversion routine for object flags. 1200Sstevel@tonic-gate */ 1210Sstevel@tonic-gate const char * 1224734Sab196087 conv_config_obj(ushort_t flags, Conv_config_obj_buf_t *config_obj_buf) 1230Sstevel@tonic-gate { 1241618Srie static Val_desc vda[] = { 1251618Srie { RTC_OBJ_DIRENT, MSG_ORIG(MSG_CONF_DIRENT) }, 1261618Srie { RTC_OBJ_ALLENTS, MSG_ORIG(MSG_CONF_ALLENTS) }, 1271618Srie { RTC_OBJ_NOEXIST, MSG_ORIG(MSG_CONF_NOEXIST) }, 1281618Srie { RTC_OBJ_EXEC, MSG_ORIG(MSG_CONF_EXEC) }, 1291618Srie { RTC_OBJ_ALTER, MSG_ORIG(MSG_CONF_ALTER) }, 1301618Srie { RTC_OBJ_DUMP, MSG_ORIG(MSG_CONF_DUMP) }, 1311618Srie { RTC_OBJ_NOALTER, MSG_ORIG(MSG_CONF_NOALTER) }, 1321618Srie { RTC_OBJ_REALPTH, MSG_ORIG(MSG_CONF_REALPATH) }, 1331618Srie { RTC_OBJ_GROUP, MSG_ORIG(MSG_CONF_GROUP) }, 1341618Srie { RTC_OBJ_APP, MSG_ORIG(MSG_CONF_APP) }, 1351618Srie { RTC_OBJ_CMDLINE, MSG_ORIG(MSG_CONF_CMDLINE) }, 1361618Srie { RTC_OBJ_FILTER, MSG_ORIG(MSG_CONF_FILTER) }, 1371618Srie { RTC_OBJ_FILTEE, MSG_ORIG(MSG_CONF_FILTEE) }, 1381618Srie { 0, 0 } 1391618Srie }; 1402352Sab196087 static const char *leading_str_arr[2]; 1414734Sab196087 static CONV_EXPN_FIELD_ARG conv_arg = { 1424734Sab196087 NULL, sizeof (config_obj_buf->buf), vda, leading_str_arr }; 1432352Sab196087 1442352Sab196087 const char **lstr = leading_str_arr; 1450Sstevel@tonic-gate 1461618Srie if ((flags == 0) || (flags == RTC_OBJ_OPTINAL)) 1471618Srie return (MSG_ORIG(MSG_GBL_NULL)); 1481618Srie 1494734Sab196087 conv_arg.buf = config_obj_buf->buf; 1502352Sab196087 conv_arg.rflags = flags; 1511618Srie 1521618Srie /* 1531618Srie * Print an alternative-optional object simply as optional. 1541618Srie */ 1551618Srie if ((flags & (RTC_OBJ_ALTER | RTC_OBJ_OPTINAL)) == 1561618Srie (RTC_OBJ_ALTER | RTC_OBJ_OPTINAL)) { 1572352Sab196087 *lstr++ = MSG_ORIG(MSG_CONF_OPTIONAL); 1582352Sab196087 conv_arg.rflags &= ~(RTC_OBJ_ALTER | RTC_OBJ_OPTINAL); 1590Sstevel@tonic-gate } 1602352Sab196087 *lstr = NULL; 1612352Sab196087 conv_arg.oflags = conv_arg.rflags &= ~RTC_OBJ_OPTINAL; 1620Sstevel@tonic-gate 163*5088Sab196087 (void) conv_expn_field(&conv_arg, 0); 1640Sstevel@tonic-gate 1654734Sab196087 return ((const char *)config_obj_buf->buf); 1660Sstevel@tonic-gate } 1670Sstevel@tonic-gate 1680Sstevel@tonic-gate /* 1690Sstevel@tonic-gate * Determine whether and old pathname exists within a search path string, 1700Sstevel@tonic-gate * without a new pathname, i.e., does the search path string contain "/usr/lib" 1710Sstevel@tonic-gate * but not "/lib". If so, add the new pathname before the old pathname. For 1720Sstevel@tonic-gate * example, convert: 1730Sstevel@tonic-gate * 1740Sstevel@tonic-gate * /local/lib:/opt/sfw/lib:/usr/lib 1750Sstevel@tonic-gate * to: 1760Sstevel@tonic-gate * /local/lib:/opt/sfw/lib:/lib:/usr/lib 1770Sstevel@tonic-gate */ 1780Sstevel@tonic-gate const char * 1791618Srie conv_config_upm(const char *str, const char *old, const char *new, 1800Sstevel@tonic-gate size_t newlen) 1810Sstevel@tonic-gate { 1820Sstevel@tonic-gate const char *curstr, *ptr; 1830Sstevel@tonic-gate const char *curold = 0, *curnew = 0; 1840Sstevel@tonic-gate const char *ptrold = old, * ptrnew = new; 1850Sstevel@tonic-gate int chkold = 1, chknew = 1; 1860Sstevel@tonic-gate 1870Sstevel@tonic-gate for (curstr = ptr = str; *ptr; ptr++) { 1880Sstevel@tonic-gate if (*ptr == ':') { 1890Sstevel@tonic-gate /* 1900Sstevel@tonic-gate * We've come to the end of a token within the string. 1910Sstevel@tonic-gate */ 1920Sstevel@tonic-gate if ((uintptr_t)ptr - (uintptr_t)curstr) { 1930Sstevel@tonic-gate /* 1940Sstevel@tonic-gate * If the old or new string checking is still 1950Sstevel@tonic-gate * enabled, we've found a match. 1960Sstevel@tonic-gate */ 1970Sstevel@tonic-gate if (chkold) 1980Sstevel@tonic-gate curold = curstr; 1990Sstevel@tonic-gate if (chknew) 2000Sstevel@tonic-gate curnew = curstr; 2010Sstevel@tonic-gate } 2020Sstevel@tonic-gate curstr = (char *)(ptr + 1); 2030Sstevel@tonic-gate 2040Sstevel@tonic-gate /* 2050Sstevel@tonic-gate * If an old or new string hasn't yet been matched, 2060Sstevel@tonic-gate * re-enable the checking for either. 2070Sstevel@tonic-gate */ 2080Sstevel@tonic-gate if (curold == 0) { 2090Sstevel@tonic-gate ptrold = old; 2100Sstevel@tonic-gate chkold = 1; 2110Sstevel@tonic-gate } 2120Sstevel@tonic-gate if (curnew == 0) { 2130Sstevel@tonic-gate ptrnew = new; 2140Sstevel@tonic-gate chknew = 1; 2150Sstevel@tonic-gate } 2160Sstevel@tonic-gate continue; 2170Sstevel@tonic-gate } 2180Sstevel@tonic-gate 2190Sstevel@tonic-gate /* 2200Sstevel@tonic-gate * Determine if the current token matches the old or new string. 2210Sstevel@tonic-gate * If not, disable the checking for each string. 2220Sstevel@tonic-gate */ 2230Sstevel@tonic-gate if (chkold && (*ptr != *ptrold++)) 2240Sstevel@tonic-gate chkold = 0; 2250Sstevel@tonic-gate if (chknew && (*ptr != *ptrnew++)) 2260Sstevel@tonic-gate chknew = 0; 2270Sstevel@tonic-gate } 2280Sstevel@tonic-gate 2290Sstevel@tonic-gate /* 2300Sstevel@tonic-gate * We've come to the end of the string, if the old or new string 2310Sstevel@tonic-gate * checking is still enabled, we've found a match. 2320Sstevel@tonic-gate */ 2330Sstevel@tonic-gate if ((uintptr_t)ptr - (uintptr_t)curstr) { 2340Sstevel@tonic-gate if (chkold) 2350Sstevel@tonic-gate curold = curstr; 2360Sstevel@tonic-gate if (chknew) 2370Sstevel@tonic-gate curnew = curstr; 2380Sstevel@tonic-gate } 2390Sstevel@tonic-gate 2400Sstevel@tonic-gate /* 2410Sstevel@tonic-gate * If an old string hasn't been found, or it has and a new string has 2420Sstevel@tonic-gate * been found, return the original string. 2430Sstevel@tonic-gate */ 2440Sstevel@tonic-gate if ((curold == 0) || curnew) 2450Sstevel@tonic-gate return (str); 2460Sstevel@tonic-gate else { 2470Sstevel@tonic-gate char *newstr; 2480Sstevel@tonic-gate size_t len; 2490Sstevel@tonic-gate 2500Sstevel@tonic-gate /* 2510Sstevel@tonic-gate * Allocate a new string, enlarged to accommodate the new string 2520Sstevel@tonic-gate * that will be inserted, and an associated separator. 2530Sstevel@tonic-gate */ 2540Sstevel@tonic-gate if ((curstr = malloc(newlen + 2 + 2550Sstevel@tonic-gate (uintptr_t)ptr - (uintptr_t)str)) == 0) 2560Sstevel@tonic-gate return (str); 2570Sstevel@tonic-gate 2580Sstevel@tonic-gate newstr = (char *)curstr; 2590Sstevel@tonic-gate for (len = (uintptr_t)curold - (uintptr_t)str; len; len--) 2600Sstevel@tonic-gate *(newstr++) = *(str++); /* copy up to */ 2610Sstevel@tonic-gate /* insertion point */ 2620Sstevel@tonic-gate for (len = newlen; len; len--) 2630Sstevel@tonic-gate *(newstr++) = *(new++); /* add new string and */ 2640Sstevel@tonic-gate *(newstr++) = ':'; /* separator */ 2650Sstevel@tonic-gate for (len = (uintptr_t)ptr - (uintptr_t)str; len; len--) 2660Sstevel@tonic-gate *(newstr++) = *(str++); /* add remaining */ 2670Sstevel@tonic-gate *(newstr++) = '\0'; /* string */ 2680Sstevel@tonic-gate 2690Sstevel@tonic-gate return (curstr); 2700Sstevel@tonic-gate } 2710Sstevel@tonic-gate } 272