pam_putenv.c revision 256281
11592Srgrimes/*-
21592Srgrimes * Copyright (c) 2002-2003 Networks Associates Technology, Inc.
31592Srgrimes * Copyright (c) 2004-2011 Dag-Erling Sm��rgrav
41592Srgrimes * All rights reserved.
51592Srgrimes *
61592Srgrimes * This software was developed for the FreeBSD Project by ThinkSec AS and
71592Srgrimes * Network Associates Laboratories, the Security Research Division of
81592Srgrimes * Network Associates, Inc.  under DARPA/SPAWAR contract N66001-01-C-8035
91592Srgrimes * ("CBOSS"), as part of the DARPA CHATS research program.
101592Srgrimes *
111592Srgrimes * Redistribution and use in source and binary forms, with or without
121592Srgrimes * modification, are permitted provided that the following conditions
131592Srgrimes * are met:
141592Srgrimes * 1. Redistributions of source code must retain the above copyright
151592Srgrimes *    notice, this list of conditions and the following disclaimer.
161592Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
171592Srgrimes *    notice, this list of conditions and the following disclaimer in the
181592Srgrimes *    documentation and/or other materials provided with the distribution.
191592Srgrimes * 3. The name of the author may not be used to endorse or promote
201592Srgrimes *    products derived from this software without specific prior written
211592Srgrimes *    permission.
221592Srgrimes *
231592Srgrimes * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
241592Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
251592Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
261592Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
271592Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
281592Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
291592Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
301592Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
311592Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
321592Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
331592Srgrimes * SUCH DAMAGE.
341592Srgrimes *
351592Srgrimes * $Id: pam_putenv.c 648 2013-03-05 17:54:27Z des $
361592Srgrimes */
371592Srgrimes
381592Srgrimes#ifdef HAVE_CONFIG_H
391592Srgrimes# include "config.h"
401592Srgrimes#endif
411592Srgrimes
421592Srgrimes#include <stdlib.h>
431592Srgrimes#include <string.h>
441592Srgrimes
451592Srgrimes#include <security/pam_appl.h>
461592Srgrimes
471592Srgrimes#include "openpam_impl.h"
481592Srgrimes
491592Srgrimes/*
501592Srgrimes * XSSO 4.2.1
511592Srgrimes * XSSO 6 page 56
521592Srgrimes *
531592Srgrimes * Set the value of an environment variable
541592Srgrimes */
551592Srgrimes
561592Srgrimesint
571592Srgrimespam_putenv(pam_handle_t *pamh,
581592Srgrimes	const char *namevalue)
591592Srgrimes{
601592Srgrimes	char **env, *p;
611592Srgrimes	int i;
621592Srgrimes
631592Srgrimes	ENTER();
641592Srgrimes	if (pamh == NULL)
651592Srgrimes		RETURNC(PAM_SYSTEM_ERR);
661592Srgrimes
671592Srgrimes	/* sanity checks */
681592Srgrimes	if (namevalue == NULL || (p = strchr(namevalue, '=')) == NULL)
691592Srgrimes		RETURNC(PAM_SYSTEM_ERR);
701592Srgrimes
711592Srgrimes	/* see if the variable is already in the environment */
721592Srgrimes	if ((i = openpam_findenv(pamh, namevalue, p - namevalue)) >= 0) {
731592Srgrimes		if ((p = strdup(namevalue)) == NULL)
741592Srgrimes			RETURNC(PAM_BUF_ERR);
751592Srgrimes		FREE(pamh->env[i]);
761592Srgrimes		pamh->env[i] = p;
771592Srgrimes		RETURNC(PAM_SUCCESS);
781592Srgrimes	}
791592Srgrimes
801592Srgrimes	/* grow the environment list if necessary */
811592Srgrimes	if (pamh->env_count == pamh->env_size) {
821592Srgrimes		env = realloc(pamh->env,
831592Srgrimes		    sizeof(char *) * (pamh->env_size * 2 + 1));
841592Srgrimes		if (env == NULL)
851592Srgrimes			RETURNC(PAM_BUF_ERR);
861592Srgrimes		pamh->env = env;
871592Srgrimes		pamh->env_size = pamh->env_size * 2 + 1;
881592Srgrimes	}
891592Srgrimes
901592Srgrimes	/* add the variable at the end */
911592Srgrimes	if ((pamh->env[pamh->env_count] = strdup(namevalue)) == NULL)
921592Srgrimes		RETURNC(PAM_BUF_ERR);
931592Srgrimes	++pamh->env_count;
941592Srgrimes	RETURNC(PAM_SUCCESS);
951592Srgrimes}
961592Srgrimes
971592Srgrimes/*
981592Srgrimes * Error codes:
991592Srgrimes *
1001592Srgrimes *	PAM_SYSTEM_ERR
1011592Srgrimes *	PAM_BUF_ERR
1021592Srgrimes */
1031592Srgrimes
1041592Srgrimes/**
1051592Srgrimes * The =pam_putenv function sets an environment variable.
1061592Srgrimes * Its semantics are similar to those of =putenv, but it modifies the PAM
1071592Srgrimes * context's environment list instead of the application's.
1081592Srgrimes *
1091592Srgrimes * >pam_getenv
1101592Srgrimes * >pam_getenvlist
1111592Srgrimes * >pam_setenv
1121592Srgrimes */
1131592Srgrimes