191094Sdes/*-
2115619Sdes * Copyright (c) 2002-2003 Networks Associates Technology, Inc.
3228690Sdes * Copyright (c) 2004-2011 Dag-Erling Sm��rgrav
491094Sdes * All rights reserved.
591094Sdes *
691094Sdes * This software was developed for the FreeBSD Project by ThinkSec AS and
799158Sdes * Network Associates Laboratories, the Security Research Division of
899158Sdes * Network Associates, Inc.  under DARPA/SPAWAR contract N66001-01-C-8035
999158Sdes * ("CBOSS"), as part of the DARPA CHATS research program.
1091094Sdes *
1191094Sdes * Redistribution and use in source and binary forms, with or without
1291094Sdes * modification, are permitted provided that the following conditions
1391094Sdes * are met:
1491094Sdes * 1. Redistributions of source code must retain the above copyright
1591094Sdes *    notice, this list of conditions and the following disclaimer.
1691094Sdes * 2. Redistributions in binary form must reproduce the above copyright
1791094Sdes *    notice, this list of conditions and the following disclaimer in the
1891094Sdes *    documentation and/or other materials provided with the distribution.
1991094Sdes * 3. The name of the author may not be used to endorse or promote
2091094Sdes *    products derived from this software without specific prior written
2191094Sdes *    permission.
2291094Sdes *
2391094Sdes * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
2491094Sdes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2591094Sdes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2691094Sdes * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2791094Sdes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2891094Sdes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2991094Sdes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3091094Sdes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3191094Sdes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3291094Sdes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3391094Sdes * SUCH DAMAGE.
3491094Sdes *
35255376Sdes * $Id: pam_setenv.c 648 2013-03-05 17:54:27Z des $
3691094Sdes */
3791094Sdes
38228690Sdes#ifdef HAVE_CONFIG_H
39228690Sdes# include "config.h"
40228690Sdes#endif
41228690Sdes
4291094Sdes#include <stdlib.h>
4391094Sdes#include <stdio.h>
4491094Sdes#include <string.h>
4591094Sdes
4691094Sdes#include <security/pam_appl.h>
4791094Sdes
4891094Sdes#include "openpam_impl.h"
49255376Sdes#include "openpam_asprintf.h"
5091094Sdes
5191094Sdes/*
5291094Sdes * OpenPAM extension
5391094Sdes *
5491094Sdes * Set the value of an environment variable
5591094Sdes * Mirrors setenv(3)
5691094Sdes */
5791094Sdes
5891094Sdesint
5991094Sdespam_setenv(pam_handle_t *pamh,
6091094Sdes	const char *name,
6191094Sdes	const char *value,
6291094Sdes	int overwrite)
6391094Sdes{
6491094Sdes	char *env;
6591094Sdes	int r;
6691094Sdes
67107937Sdes	ENTER();
6891094Sdes	if (pamh == NULL)
69107937Sdes		RETURNC(PAM_SYSTEM_ERR);
7091094Sdes
7191094Sdes	/* sanity checks */
7291094Sdes	if (name == NULL || value == NULL || strchr(name, '=') != NULL)
73107937Sdes		RETURNC(PAM_SYSTEM_ERR);
7491094Sdes
7591094Sdes	/* is it already there? */
76115619Sdes	if (!overwrite && openpam_findenv(pamh, name, strlen(name)) >= 0)
77107937Sdes		RETURNC(PAM_SUCCESS);
7891094Sdes
7991094Sdes	/* set it... */
80115619Sdes	if (asprintf(&env, "%s=%s", name, value) < 0)
81107937Sdes		RETURNC(PAM_BUF_ERR);
8291094Sdes	r = pam_putenv(pamh, env);
83115619Sdes	FREE(env);
84107937Sdes	RETURNC(r);
8591094Sdes}
8691100Sdes
8791100Sdes/*
8891100Sdes * Error codes:
8991100Sdes *
9091100Sdes *	=pam_putenv
9191100Sdes *	PAM_SYSTEM_ERR
9291100Sdes *	PAM_BUF_ERR
9391100Sdes */
9491100Sdes
9591100Sdes/**
96236099Sdes * The =pam_setenv function sets an environment variable.
9791100Sdes * Its semantics are similar to those of =setenv, but it modifies the PAM
9891100Sdes * context's environment list instead of the application's.
9991100Sdes *
10091100Sdes * >pam_getenv
10191100Sdes * >pam_getenvlist
10291100Sdes * >pam_putenv
10391100Sdes */
104