pam_getenvlist.c revision 99158
111394Sswallace/*-
211394Sswallace * Copyright (c) 2002 Networks Associates Technology, Inc.
311394Sswallace * All rights reserved.
411394Sswallace *
511394Sswallace * This software was developed for the FreeBSD Project by ThinkSec AS and
611394Sswallace * Network Associates Laboratories, the Security Research Division of
711394Sswallace * Network Associates, Inc.  under DARPA/SPAWAR contract N66001-01-C-8035
811394Sswallace * ("CBOSS"), as part of the DARPA CHATS research program.
911394Sswallace *
1011394Sswallace * Redistribution and use in source and binary forms, with or without
1111394Sswallace * modification, are permitted provided that the following conditions
1211394Sswallace * are met:
1311394Sswallace * 1. Redistributions of source code must retain the above copyright
1411394Sswallace *    notice, this list of conditions and the following disclaimer.
1511394Sswallace * 2. Redistributions in binary form must reproduce the above copyright
1611394Sswallace *    notice, this list of conditions and the following disclaimer in the
1711394Sswallace *    documentation and/or other materials provided with the distribution.
1811394Sswallace * 3. The name of the author may not be used to endorse or promote
1911394Sswallace *    products derived from this software without specific prior written
2011394Sswallace *    permission.
2111394Sswallace *
2211394Sswallace * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
2311394Sswallace * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2411394Sswallace * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2511394Sswallace * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2611394Sswallace * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2711394Sswallace * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2811394Sswallace * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2911394Sswallace * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3011394Sswallace * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3111394Sswallace * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3211394Sswallace * SUCH DAMAGE.
3311394Sswallace *
3411394Sswallace * $P4: //depot/projects/openpam/lib/pam_getenvlist.c#9 $
3511394Sswallace */
3611397Sswallace
3711394Sswallace#include <stdlib.h>
3811394Sswallace#include <string.h>
3911394Sswallace
4011394Sswallace#include <security/pam_appl.h>
4111394Sswallace
4211394Sswallace#include "openpam_impl.h"
4311394Sswallace
4411394Sswallace/*
4511394Sswallace * XSSO 4.2.1
4611394Sswallace * XSSO 6 page 45
4711394Sswallace *
4811394Sswallace * Returns a list of all the PAM environment variables
4911394Sswallace */
5011394Sswallace
51char **
52pam_getenvlist(pam_handle_t *pamh)
53{
54	char **envlist;
55	int i;
56
57	if (pamh == NULL)
58		return (NULL);
59
60	envlist = malloc(sizeof(char *) * (pamh->env_count + 1));
61	if (envlist == NULL) {
62		openpam_log(PAM_LOG_ERROR, "%s",
63			pam_strerror(pamh, PAM_BUF_ERR));
64		return (NULL);
65	}
66	for (i = 0; i < pamh->env_count; ++i) {
67		if ((envlist[i] = strdup(pamh->env[i])) == NULL) {
68			while (i)
69				free(envlist[--i]);
70			free(envlist);
71			openpam_log(PAM_LOG_ERROR, "%s",
72				pam_strerror(pamh, PAM_BUF_ERR));
73			return (NULL);
74		}
75	}
76	envlist[i] = NULL;
77	openpam_log(PAM_LOG_DEBUG, "returning %d variables\n", pamh->env_count);
78	return (envlist);
79}
80
81/**
82 * The =pam_getenvlist function returns a copy of the given PAM context's
83 * environment list as a pointer to an array of strings.
84 * The last element in the array is =NULL.
85 * The pointer is suitable for assignment to {Va environ}.
86 *
87 * The array and the strings it lists are allocated using =malloc, and
88 * should be released using =free after use:
89 *
90 *     char **envlist, **env;
91 *
92 *     envlist = environ;
93 *     environ = pam_getenvlist(pamh);
94 *     \/\* do something nifty \*\/
95 *     for (env = environ; *env != NULL; env++)
96 *         free(*env);
97 *     free(environ);
98 *     environ = envlist;
99 *
100 * >environ 7
101 * >pam_getenv
102 * >pam_putenv
103 * >pam_setenv
104 */
105