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