1216294Ssyrinx/*-
2216294Ssyrinx * Copyright (c) 2002-2003 Networks Associates Technology, Inc.
3216294Ssyrinx * Copyright (c) 2004-2011 Dag-Erling Sm��rgrav
4216294Ssyrinx * All rights reserved.
5216294Ssyrinx *
6216294Ssyrinx * This software was developed for the FreeBSD Project by ThinkSec AS and
7216294Ssyrinx * Network Associates Laboratories, the Security Research Division of
8216294Ssyrinx * Network Associates, Inc.  under DARPA/SPAWAR contract N66001-01-C-8035
9216294Ssyrinx * ("CBOSS"), as part of the DARPA CHATS research program.
10216294Ssyrinx *
11216294Ssyrinx * Redistribution and use in source and binary forms, with or without
12216294Ssyrinx * modification, are permitted provided that the following conditions
13216294Ssyrinx * are met:
14216294Ssyrinx * 1. Redistributions of source code must retain the above copyright
15216294Ssyrinx *    notice, this list of conditions and the following disclaimer.
16216294Ssyrinx * 2. Redistributions in binary form must reproduce the above copyright
17216294Ssyrinx *    notice, this list of conditions and the following disclaimer in the
18216294Ssyrinx *    documentation and/or other materials provided with the distribution.
19216294Ssyrinx * 3. The name of the author may not be used to endorse or promote
20216294Ssyrinx *    products derived from this software without specific prior written
21216294Ssyrinx *    permission.
22216294Ssyrinx *
23216294Ssyrinx * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
24216294Ssyrinx * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25216294Ssyrinx * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26216294Ssyrinx * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
27216294Ssyrinx * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28216294Ssyrinx * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29216294Ssyrinx * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30216294Ssyrinx * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31216294Ssyrinx * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32216294Ssyrinx * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33216294Ssyrinx * SUCH DAMAGE.
34216294Ssyrinx *
35216294Ssyrinx * $Id: pam_getenvlist.c 648 2013-03-05 17:54:27Z des $
36237193Sjoel */
37216294Ssyrinx
38216294Ssyrinx#ifdef HAVE_CONFIG_H
39216294Ssyrinx# include "config.h"
40216294Ssyrinx#endif
41216294Ssyrinx
42216294Ssyrinx#include <stdlib.h>
43216294Ssyrinx#include <string.h>
44216294Ssyrinx
45216294Ssyrinx#include <security/pam_appl.h>
46216294Ssyrinx
47216294Ssyrinx#include "openpam_impl.h"
48216294Ssyrinx
49216294Ssyrinx/*
50216294Ssyrinx * XSSO 4.2.1
51216294Ssyrinx * XSSO 6 page 45
52216294Ssyrinx *
53216294Ssyrinx * Returns a list of all the PAM environment variables
54216294Ssyrinx */
55216294Ssyrinx
56216294Ssyrinxchar **
57216294Ssyrinxpam_getenvlist(pam_handle_t *pamh)
58216294Ssyrinx{
59216294Ssyrinx	char **envlist;
60235286Sgjb	int i;
61216294Ssyrinx
62216294Ssyrinx	ENTER();
63216294Ssyrinx	if (pamh == NULL)
64216294Ssyrinx		RETURNP(NULL);
65216294Ssyrinx	envlist = malloc(sizeof(char *) * (pamh->env_count + 1));
66216294Ssyrinx	if (envlist == NULL) {
67216294Ssyrinx		openpam_log(PAM_LOG_ERROR, "%s",
68216294Ssyrinx			pam_strerror(pamh, PAM_BUF_ERR));
69216294Ssyrinx		RETURNP(NULL);
70216294Ssyrinx	}
71216294Ssyrinx	for (i = 0; i < pamh->env_count; ++i) {
72216294Ssyrinx		if ((envlist[i] = strdup(pamh->env[i])) == NULL) {
73216294Ssyrinx			while (i) {
74216294Ssyrinx				--i;
75216294Ssyrinx				FREE(envlist[i]);
76216294Ssyrinx			}
77216294Ssyrinx			FREE(envlist);
78216294Ssyrinx			openpam_log(PAM_LOG_ERROR, "%s",
79216294Ssyrinx				pam_strerror(pamh, PAM_BUF_ERR));
80216294Ssyrinx			RETURNP(NULL);
81216294Ssyrinx		}
82237194Sjoel	}
83216294Ssyrinx	envlist[i] = NULL;
84216294Ssyrinx	RETURNP(envlist);
85216294Ssyrinx}
86216294Ssyrinx
87216294Ssyrinx/**
88216294Ssyrinx * The =pam_getenvlist function returns a copy of the given PAM context's
89216294Ssyrinx * environment list as a pointer to an array of strings.
90216294Ssyrinx * The last element in the array is =NULL.
91216294Ssyrinx * The pointer is suitable for assignment to {Va environ}.
92216294Ssyrinx *
93216294Ssyrinx * The array and the strings it lists are allocated using =malloc, and
94237194Sjoel * should be released using =free after use:
95216294Ssyrinx *
96216294Ssyrinx *     char **envlist, **env;
97216294Ssyrinx *
98216294Ssyrinx *     envlist = environ;
99216294Ssyrinx *     environ = pam_getenvlist(pamh);
100216294Ssyrinx *     \/\* do something nifty \*\/
101216294Ssyrinx *     for (env = environ; *env != NULL; env++)
102216294Ssyrinx *         free(*env);
103216294Ssyrinx *     free(environ);
104216294Ssyrinx *     environ = envlist;
105235286Sgjb *
106216294Ssyrinx * >environ 7
107216294Ssyrinx * >pam_getenv
108216294Ssyrinx * >pam_putenv
109216294Ssyrinx * >pam_setenv
110216294Ssyrinx */
111216294Ssyrinx