190792Sgshapiro/*
2261363Sgshapiro * Copyright (c) 2000-2003, 2007 Proofpoint, Inc. and its suppliers.
390792Sgshapiro *	All rights reserved.
490792Sgshapiro *
590792Sgshapiro * By using this file, you agree to the terms and conditions set
690792Sgshapiro * forth in the LICENSE file which can be found at the top level of
790792Sgshapiro * the sendmail distribution.
890792Sgshapiro *
990792Sgshapiro */
1090792Sgshapiro
1190792Sgshapiro#include <sm/gen.h>
12266692SgshapiroSM_RCSID("@(#)$Id: config.c,v 1.32 2013-11-22 20:51:42 ca Exp $")
1390792Sgshapiro
1490792Sgshapiro#include <stdlib.h>
1590792Sgshapiro#include <sm/heap.h>
1690792Sgshapiro#include <sm/string.h>
1790792Sgshapiro#include <sm/conf.h>
1890792Sgshapiro
1990792Sgshapiro/*
2090792Sgshapiro**  PUTENV -- emulation of putenv() in terms of setenv()
2190792Sgshapiro**
2290792Sgshapiro**	Not needed on Posix-compliant systems.
2390792Sgshapiro**	This doesn't have full Posix semantics, but it's good enough
2490792Sgshapiro**		for sendmail.
2590792Sgshapiro**
2690792Sgshapiro**	Parameter:
2790792Sgshapiro**		env -- the environment to put.
2890792Sgshapiro**
2990792Sgshapiro**	Returns:
3090792Sgshapiro**		0 on success, < 0 on failure.
3190792Sgshapiro*/
3290792Sgshapiro
3390792Sgshapiro#if NEEDPUTENV
3490792Sgshapiro
3590792Sgshapiro# if NEEDPUTENV == 2		/* no setenv(3) call available */
3690792Sgshapiro
3790792Sgshapiroint
3890792Sgshapiroputenv(str)
3990792Sgshapiro	char *str;
4090792Sgshapiro{
4190792Sgshapiro	char **current;
4290792Sgshapiro	int matchlen, envlen = 0;
4390792Sgshapiro	char *tmp;
4490792Sgshapiro	char **newenv;
4590792Sgshapiro	static bool first = true;
4690792Sgshapiro	extern char **environ;
4790792Sgshapiro
4890792Sgshapiro	/*
4990792Sgshapiro	**  find out how much of str to match when searching
5090792Sgshapiro	**  for a string to replace.
5190792Sgshapiro	*/
5290792Sgshapiro
5390792Sgshapiro	if ((tmp = strchr(str, '=')) == NULL || tmp == str)
5490792Sgshapiro		matchlen = strlen(str);
5590792Sgshapiro	else
5690792Sgshapiro		matchlen = (int) (tmp - str);
5790792Sgshapiro	++matchlen;
5890792Sgshapiro
5990792Sgshapiro	/*
6090792Sgshapiro	**  Search for an existing string in the environment and find the
6190792Sgshapiro	**  length of environ.  If found, replace and exit.
6290792Sgshapiro	*/
6390792Sgshapiro
6490792Sgshapiro	for (current = environ; *current != NULL; current++)
6590792Sgshapiro	{
6690792Sgshapiro		++envlen;
6790792Sgshapiro
6890792Sgshapiro		if (strncmp(str, *current, matchlen) == 0)
6990792Sgshapiro		{
7090792Sgshapiro			/* found it, now insert the new version */
7190792Sgshapiro			*current = (char *) str;
7290792Sgshapiro			return 0;
7390792Sgshapiro		}
7490792Sgshapiro	}
7590792Sgshapiro
7690792Sgshapiro	/*
7790792Sgshapiro	**  There wasn't already a slot so add space for a new slot.
7890792Sgshapiro	**  If this is our first time through, use malloc(), else realloc().
7990792Sgshapiro	*/
8090792Sgshapiro
8190792Sgshapiro	if (first)
8290792Sgshapiro	{
8390792Sgshapiro		newenv = (char **) sm_malloc(sizeof(char *) * (envlen + 2));
8490792Sgshapiro		if (newenv == NULL)
8590792Sgshapiro			return -1;
8690792Sgshapiro
8790792Sgshapiro		first = false;
8890792Sgshapiro		(void) memcpy(newenv, environ, sizeof(char *) * envlen);
8990792Sgshapiro	}
9090792Sgshapiro	else
9190792Sgshapiro	{
9290792Sgshapiro		newenv = (char **) sm_realloc((char *) environ,
9390792Sgshapiro					      sizeof(char *) * (envlen + 2));
9490792Sgshapiro		if (newenv == NULL)
9590792Sgshapiro			return -1;
9690792Sgshapiro	}
9790792Sgshapiro
9890792Sgshapiro	/* actually add in the new entry */
9990792Sgshapiro	environ = newenv;
10090792Sgshapiro	environ[envlen] = (char *) str;
10190792Sgshapiro	environ[envlen + 1] = NULL;
10290792Sgshapiro
10390792Sgshapiro	return 0;
10490792Sgshapiro}
10590792Sgshapiro
10690792Sgshapiro# else /* NEEDPUTENV == 2 */
10790792Sgshapiro
10890792Sgshapiroint
10990792Sgshapiroputenv(env)
11090792Sgshapiro	char *env;
11190792Sgshapiro{
11290792Sgshapiro	char *p;
11390792Sgshapiro	int l;
11490792Sgshapiro	char nbuf[100];
11590792Sgshapiro
11690792Sgshapiro	p = strchr(env, '=');
11790792Sgshapiro	if (p == NULL)
11890792Sgshapiro		return 0;
11990792Sgshapiro	l = p - env;
12090792Sgshapiro	if (l > sizeof nbuf - 1)
12190792Sgshapiro		l = sizeof nbuf - 1;
12290792Sgshapiro	memmove(nbuf, env, l);
12390792Sgshapiro	nbuf[l] = '\0';
12490792Sgshapiro	return setenv(nbuf, ++p, 1);
12590792Sgshapiro}
12690792Sgshapiro
12790792Sgshapiro# endif /* NEEDPUTENV == 2 */
12890792Sgshapiro#endif /* NEEDPUTENV */
12990792Sgshapiro/*
13090792Sgshapiro**  UNSETENV -- remove a variable from the environment
13190792Sgshapiro**
13290792Sgshapiro**	Not needed on newer systems.
13390792Sgshapiro**
13490792Sgshapiro**	Parameters:
13590792Sgshapiro**		name -- the string name of the environment variable to be
13690792Sgshapiro**			deleted from the current environment.
13790792Sgshapiro**
13890792Sgshapiro**	Returns:
13990792Sgshapiro**		none.
14090792Sgshapiro**
14190792Sgshapiro**	Globals:
14290792Sgshapiro**		environ -- a pointer to the current environment.
14390792Sgshapiro**
14490792Sgshapiro**	Side Effects:
14590792Sgshapiro**		Modifies environ.
14690792Sgshapiro*/
14790792Sgshapiro
14890792Sgshapiro#if !HASUNSETENV
14990792Sgshapiro
15090792Sgshapirovoid
15190792Sgshapirounsetenv(name)
15290792Sgshapiro	char *name;
15390792Sgshapiro{
15490792Sgshapiro	extern char **environ;
15590792Sgshapiro	register char **pp;
15690792Sgshapiro	int len = strlen(name);
15790792Sgshapiro
15890792Sgshapiro	for (pp = environ; *pp != NULL; pp++)
15990792Sgshapiro	{
16090792Sgshapiro		if (strncmp(name, *pp, len) == 0 &&
16190792Sgshapiro		    ((*pp)[len] == '=' || (*pp)[len] == '\0'))
16290792Sgshapiro			break;
16390792Sgshapiro	}
16490792Sgshapiro
16590792Sgshapiro	for (; *pp != NULL; pp++)
16690792Sgshapiro		*pp = pp[1];
16790792Sgshapiro}
16890792Sgshapiro
16990792Sgshapiro#endif /* !HASUNSETENV */
17090792Sgshapiro
17190792Sgshapirochar *SmCompileOptions[] =
17290792Sgshapiro{
17390792Sgshapiro#if SM_CONF_BROKEN_STRTOD
17490792Sgshapiro	"SM_CONF_BROKEN_STRTOD",
17590792Sgshapiro#endif /* SM_CONF_BROKEN_STRTOD */
17690792Sgshapiro#if SM_CONF_GETOPT
17790792Sgshapiro	"SM_CONF_GETOPT",
17890792Sgshapiro#endif /* SM_CONF_GETOPT */
179132943Sgshapiro#if SM_CONF_LDAP_INITIALIZE
180132943Sgshapiro	"SM_CONF_LDAP_INITIALIZE",
181132943Sgshapiro#endif /* SM_CONF_LDAP_INITIALIZE */
18294334Sgshapiro#if SM_CONF_LDAP_MEMFREE
18394334Sgshapiro	"SM_CONF_LDAP_MEMFREE",
18494334Sgshapiro#endif /* SM_CONF_LDAP_MEMFREE */
18590792Sgshapiro#if SM_CONF_LONGLONG
18690792Sgshapiro	"SM_CONF_LONGLONG",
18790792Sgshapiro#endif /* SM_CONF_LONGLONG */
18890792Sgshapiro#if SM_CONF_MEMCHR
18990792Sgshapiro	"SM_CONF_MEMCHR",
19090792Sgshapiro#endif /* SM_CONF_MEMCHR */
19190792Sgshapiro#if SM_CONF_MSG
19290792Sgshapiro	"SM_CONF_MSG",
19390792Sgshapiro#endif /* SM_CONF_MSG */
19490792Sgshapiro#if SM_CONF_QUAD_T
19590792Sgshapiro	"SM_CONF_QUAD_T",
19690792Sgshapiro#endif /* SM_CONF_QUAD_T */
19790792Sgshapiro#if SM_CONF_SEM
19890792Sgshapiro	"SM_CONF_SEM",
19990792Sgshapiro#endif /* SM_CONF_SEM */
20090792Sgshapiro#if SM_CONF_SETITIMER
20190792Sgshapiro	"SM_CONF_SETITIMER",
20290792Sgshapiro#endif /* SM_CONF_SETITIMER */
203112810Sgshapiro#if SM_CONF_SIGSETJMP
204112810Sgshapiro	"SM_CONF_SIGSETJMP",
205112810Sgshapiro#endif /* SM_CONF_SIGSETJMP */
20690792Sgshapiro#if SM_CONF_SHM
20790792Sgshapiro	"SM_CONF_SHM",
20890792Sgshapiro#endif /* SM_CONF_SHM */
20990792Sgshapiro#if SM_CONF_SHM_DELAY
21090792Sgshapiro	"SM_CONF_SHM_DELAY",
21190792Sgshapiro#endif /* SM_CONF_SHM_DELAY */
21290792Sgshapiro#if SM_CONF_SSIZE_T
21390792Sgshapiro	"SM_CONF_SSIZE_T",
21490792Sgshapiro#endif /* SM_CONF_SSIZE_T */
21590792Sgshapiro#if SM_CONF_STDBOOL_H
21690792Sgshapiro	"SM_CONF_STDBOOL_H",
21790792Sgshapiro#endif /* SM_CONF_STDBOOL_H */
21890792Sgshapiro#if SM_CONF_STDDEF_H
21990792Sgshapiro	"SM_CONF_STDDEF_H",
22090792Sgshapiro#endif /* SM_CONF_STDDEF_H */
22190792Sgshapiro
22290792Sgshapiro#if 0
22390792Sgshapiro/* XXX this is always enabled (for now) */
22490792Sgshapiro#if SM_CONF_STRL
22590792Sgshapiro	"SM_CONF_STRL",
22690792Sgshapiro#endif /* SM_CONF_STRL */
22790792Sgshapiro#endif /* 0 */
22890792Sgshapiro
22990792Sgshapiro#if SM_CONF_SYS_CDEFS_H
23090792Sgshapiro	"SM_CONF_SYS_CDEFS_H",
23190792Sgshapiro#endif /* SM_CONF_SYS_CDEFS_H */
23290792Sgshapiro#if SM_CONF_SYSEXITS_H
23390792Sgshapiro	"SM_CONF_SYSEXITS_H",
23490792Sgshapiro#endif /* SM_CONF_SYSEXITS_H */
23590792Sgshapiro#if SM_CONF_UID_GID
23690792Sgshapiro	"SM_CONF_UID_GID",
23790792Sgshapiro#endif /* SM_CONF_UID_GID */
238132943Sgshapiro#if DO_NOT_USE_STRCPY
239132943Sgshapiro	"DO_NOT_USE_STRCPY",
240132943Sgshapiro#endif /* DO_NOT_USE_STRCPY */
24190792Sgshapiro#if SM_HEAP_CHECK
24290792Sgshapiro	"SM_HEAP_CHECK",
24390792Sgshapiro#endif /* SM_HEAP_CHECK */
24490792Sgshapiro#if defined(SM_OS_NAME) && defined(__STDC__)
24590792Sgshapiro	"SM_OS=sm_os_" SM_OS_NAME,
24690792Sgshapiro#endif /* defined(SM_OS_NAME) && defined(__STDC__) */
24790792Sgshapiro#if SM_VA_STD
24890792Sgshapiro	"SM_VA_STD",
24990792Sgshapiro#endif /* SM_VA_STD */
250168515Sgshapiro#if USEKSTAT
251168515Sgshapiro	"USEKSTAT",
252168515Sgshapiro#endif /* USEKSTAT */
253168515Sgshapiro#if USEPROCMEMINFO
254168515Sgshapiro	"USEPROCMEMINFO",
255168515Sgshapiro#endif /* USEPROCMEMINFO */
256168515Sgshapiro#if USESWAPCTL
257168515Sgshapiro	"USESWAPCTL",
258168515Sgshapiro#endif /* USESWAPCTL */
25990792Sgshapiro	NULL
26090792Sgshapiro};
261