pam_putenv.c revision 228690
1265236Sken/*-
2265236Sken * Copyright (c) 2002-2003 Networks Associates Technology, Inc.
3265236Sken * Copyright (c) 2004-2011 Dag-Erling Sm��rgrav
4265236Sken * All rights reserved.
5265236Sken *
6265236Sken * This software was developed for the FreeBSD Project by ThinkSec AS and
7265236Sken * Network Associates Laboratories, the Security Research Division of
8265236Sken * Network Associates, Inc.  under DARPA/SPAWAR contract N66001-01-C-8035
9265236Sken * ("CBOSS"), as part of the DARPA CHATS research program.
10265236Sken *
11265236Sken * Redistribution and use in source and binary forms, with or without
12265236Sken * modification, are permitted provided that the following conditions
13265236Sken * are met:
14265236Sken * 1. Redistributions of source code must retain the above copyright
15265236Sken *    notice, this list of conditions and the following disclaimer.
16265236Sken * 2. Redistributions in binary form must reproduce the above copyright
17265236Sken *    notice, this list of conditions and the following disclaimer in the
18265236Sken *    documentation and/or other materials provided with the distribution.
19265236Sken * 3. The name of the author may not be used to endorse or promote
20265236Sken *    products derived from this software without specific prior written
21265236Sken *    permission.
22265236Sken *
23265236Sken * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
24265236Sken * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25265236Sken * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26265236Sken * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
27265236Sken * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28265236Sken * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29265236Sken * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30265236Sken * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31265236Sken * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32265236Sken * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33265236Sken * SUCH DAMAGE.
34265236Sken *
35265236Sken * $Id: pam_putenv.c 437 2011-09-13 12:00:13Z des $
36265236Sken */
37265236Sken
38265236Sken#ifdef HAVE_CONFIG_H
39265388Sken# include "config.h"
40265236Sken#endif
41265236Sken
42265236Sken#include <stdlib.h>
43265236Sken#include <string.h>
44265236Sken
45265236Sken#include <security/pam_appl.h>
46265236Sken
47265236Sken#include "openpam_impl.h"
48265236Sken
49265236Sken/*
50265236Sken * XSSO 4.2.1
51265236Sken * XSSO 6 page 56
52265236Sken *
53265236Sken * Set the value of an environment variable
54265388Sken */
55265388Sken
56265236Skenint
57265236Skenpam_putenv(pam_handle_t *pamh,
58265236Sken	const char *namevalue)
59265236Sken{
60265236Sken	char **env, *p;
61265236Sken	int i;
62265236Sken
63265236Sken	ENTER();
64265236Sken	if (pamh == NULL)
65265236Sken		RETURNC(PAM_SYSTEM_ERR);
66265236Sken
67265236Sken	/* sanity checks */
68265236Sken	if (namevalue == NULL || (p = strchr(namevalue, '=')) == NULL)
69265388Sken		RETURNC(PAM_SYSTEM_ERR);
70265236Sken
71265236Sken	/* see if the variable is already in the environment */
72265236Sken	if ((i = openpam_findenv(pamh, namevalue, p - namevalue)) >= 0) {
73265236Sken		if ((p = strdup(namevalue)) == NULL)
74265236Sken			RETURNC(PAM_BUF_ERR);
75265236Sken		FREE(pamh->env[i]);
76265236Sken		pamh->env[i] = p;
77265236Sken		RETURNC(PAM_SUCCESS);
78265236Sken	}
79265236Sken
80265236Sken	/* grow the environment list if necessary */
81265236Sken	if (pamh->env_count == pamh->env_size) {
82265236Sken		env = realloc(pamh->env,
83265236Sken		    sizeof(char *) * (pamh->env_size * 2 + 1));
84265236Sken		if (env == NULL)
85265236Sken			RETURNC(PAM_BUF_ERR);
86265236Sken		pamh->env = env;
87265236Sken		pamh->env_size = pamh->env_size * 2 + 1;
88265236Sken	}
89265236Sken
90265236Sken	/* add the variable at the end */
91265236Sken	if ((pamh->env[pamh->env_count] = strdup(namevalue)) == NULL)
92265236Sken		RETURNC(PAM_BUF_ERR);
93265236Sken	++pamh->env_count;
94265236Sken	RETURNC(PAM_SUCCESS);
95265236Sken}
96265236Sken
97265236Sken/*
98265236Sken * Error codes:
99265236Sken *
100265236Sken *	PAM_SYSTEM_ERR
101265236Sken *	PAM_BUF_ERR
102265236Sken */
103265236Sken
104265236Sken/**
105265236Sken * The =pam_putenv function sets a environment variable.
106265236Sken * Its semantics are similar to those of =putenv, but it modifies the PAM
107265236Sken * context's environment list instead of the application's.
108265236Sken *
109265236Sken * >pam_getenv
110265236Sken * >pam_getenvlist
111265236Sken * >pam_setenv
112265236Sken */
113265236Sken