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