]> granicus.if.org Git - linux-pam/blob - libpam_misc/help_env.c
Relevant BUGIDs: 116380
[linux-pam] / libpam_misc / help_env.c
1 /*
2  * $Id$
3  *
4  * This file was written by Andrew G. Morgan <morgan@parc.power.net>
5  *
6  * $Log$
7  * Revision 1.1  2000/06/20 22:11:24  agmorgan
8  * Initial revision
9  *
10  * Revision 1.1.1.1  1998/07/12 05:17:15  morgan
11  * Linux PAM sources pre-0.66
12  *
13  * Revision 1.2  1997/01/04 20:19:20  morgan
14  * added a prototype (no warning) and fixed paste function
15  *
16  * Revision 1.1  1996/12/01 03:25:37  morgan
17  * Initial revision
18  *
19  */
20
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <string.h>
24 #include <security/pam_misc.h>
25
26 /*
27  * This is a useful function for dumping the Linux-PAM environment
28  * into some local memory, prior to it all getting lost when pam_end()
29  * is called.
30  *
31  * Initially it was assumed that libpam did not do this part correctly
32  * (based on a loose email definition).  The X/Open XSSO spec makes it
33  * clear that this function is a duplicate of the one already in
34  * libpam and therefore unnecessary.  IT WILL BE COMPLETELY REMOVED
35  * IN libpam_misc 1.0 */
36
37 char **pam_misc_copy_env(pam_handle_t *pamh);
38 char **pam_misc_copy_env(pam_handle_t *pamh)
39 {
40     return pam_getenvlist(pamh);
41 }
42
43 /*
44  * This function should be used to carefully dispose of the copied
45  * environment.
46  *
47  *     usage:     env = pam_misc_drop_env(env);
48  */
49
50 char **pam_misc_drop_env(char **dump)
51 {
52     int i;
53
54     for (i=0; dump[i] != NULL; ++i) {
55         D(("dump[%d]=`%s'", i, dump[i]));
56         _pam_overwrite(dump[i]);
57         _pam_drop(dump[i]);
58     }
59     _pam_drop(dump);
60
61     return NULL;
62 }
63
64 /*
65  *  This function takes the supplied environment and uploads it to be
66  *  the PAM one.
67  */
68
69 int pam_misc_paste_env(pam_handle_t *pamh, const char * const * user_env)
70 {
71     for (; user_env && *user_env; ++user_env) {
72         int retval;
73
74         D(("uploading: %s", *user_env));
75         retval = pam_putenv(pamh, *user_env);
76         if (retval != PAM_SUCCESS) {
77             D(("error setting %s: %s", *user_env, pam_strerror(pamh,retval)));
78             return retval;
79         }
80     }
81     D(("done."));
82     return PAM_SUCCESS;
83 }
84
85 /*
86  * This is a wrapper to make pam behave in the way that setenv() does.
87  */
88
89 int pam_misc_setenv(pam_handle_t *pamh, const char *name
90                     , const char *value, int readonly)
91 {
92     char *tmp;
93     int retval;
94
95     if (readonly) {
96         const char *etmp;
97
98         /* we check if the variable is there already */
99         etmp = pam_getenv(pamh, name);
100         if (etmp != NULL) {
101             D(("failed to set readonly variable: %s", name));
102             return PAM_PERM_DENIED;          /* not allowed to overwrite */
103         }
104     }
105     tmp = malloc(2+strlen(name)+strlen(value));
106     if (tmp != NULL) {
107         sprintf(tmp,"%s=%s",name,value);
108         D(("pam_putt()ing: %s", tmp));
109         retval = pam_putenv(pamh, tmp);
110         _pam_overwrite(tmp);                 /* purge */
111         _pam_drop(tmp);                      /* forget */
112     } else {
113         D(("malloc failure"));
114         retval = PAM_BUF_ERR;
115     }
116
117     return retval;
118 }