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