summaryrefslogtreecommitdiff
path: root/libpam_misc/help_env.c
blob: d52b3a024c5b2b954e02aeaccae65fb89d41eff3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/*
 * $Id$
 *
 * This file was written by Andrew G. Morgan <morgan@parc.power.net>
 *
 * $Log$
 * Revision 1.1  2000/06/20 22:11:24  agmorgan
 * Initial revision
 *
 * Revision 1.1.1.1  1998/07/12 05:17:15  morgan
 * Linux PAM sources pre-0.66
 *
 * Revision 1.2  1997/01/04 20:19:20  morgan
 * added a prototype (no warning) and fixed paste function
 *
 * Revision 1.1  1996/12/01 03:25:37  morgan
 * Initial revision
 *
 */

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <security/pam_misc.h>

/*
 * This is a useful function for dumping the Linux-PAM environment
 * into some local memory, prior to it all getting lost when pam_end()
 * is called.
 *
 * Initially it was assumed that libpam did not do this part correctly
 * (based on a loose email definition).  The X/Open XSSO spec makes it
 * clear that this function is a duplicate of the one already in
 * libpam and therefore unnecessary.  IT WILL BE COMPLETELY REMOVED
 * IN libpam_misc 1.0 */

char **pam_misc_copy_env(pam_handle_t *pamh);
char **pam_misc_copy_env(pam_handle_t *pamh)
{
    return pam_getenvlist(pamh);
}

/*
 * This function should be used to carefully dispose of the copied
 * environment.
 *
 *     usage:     env = pam_misc_drop_env(env);
 */

char **pam_misc_drop_env(char **dump)
{
    int i;

    for (i=0; dump[i] != NULL; ++i) {
	D(("dump[%d]=`%s'", i, dump[i]));
	_pam_overwrite(dump[i]);
	_pam_drop(dump[i]);
    }
    _pam_drop(dump);

    return NULL;
}

/*
 *  This function takes the supplied environment and uploads it to be
 *  the PAM one.
 */

int pam_misc_paste_env(pam_handle_t *pamh, const char * const * user_env)
{
    for (; user_env && *user_env; ++user_env) {
	int retval;

	D(("uploading: %s", *user_env));
	retval = pam_putenv(pamh, *user_env);
	if (retval != PAM_SUCCESS) {
	    D(("error setting %s: %s", *user_env, pam_strerror(pamh,retval)));
	    return retval;
	}
    }
    D(("done."));
    return PAM_SUCCESS;
}

/*
 * This is a wrapper to make pam behave in the way that setenv() does.
 */

int pam_misc_setenv(pam_handle_t *pamh, const char *name
		    , const char *value, int readonly)
{
    char *tmp;
    int retval;

    if (readonly) {
	const char *etmp;

	/* we check if the variable is there already */
	etmp = pam_getenv(pamh, name);
	if (etmp != NULL) {
	    D(("failed to set readonly variable: %s", name));
	    return PAM_PERM_DENIED;          /* not allowed to overwrite */
	}
    }
    tmp = malloc(2+strlen(name)+strlen(value));
    if (tmp != NULL) {
	sprintf(tmp,"%s=%s",name,value);
	D(("pam_putt()ing: %s", tmp));
	retval = pam_putenv(pamh, tmp);
	_pam_overwrite(tmp);                 /* purge */
	_pam_drop(tmp);                      /* forget */
    } else {
	D(("malloc failure"));
	retval = PAM_BUF_ERR;
    }

    return retval;
}