]> granicus.if.org Git - linux-pam/blob - libpam_misc/help_env.c
Relevant BUGIDs:
[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 "config.h"
9
10 #include <stdlib.h>
11 #include <stdio.h>
12 #include <string.h>
13 #include <security/pam_misc.h>
14
15 /*
16  * This function should be used to carefully dispose of the copied
17  * environment.
18  *
19  *     usage:     env = pam_misc_drop_env(env);
20  */
21
22 char **pam_misc_drop_env(char **dump)
23 {
24     int i;
25
26     for (i=0; dump[i] != NULL; ++i) {
27         D(("dump[%d]=`%s'", i, dump[i]));
28         _pam_overwrite(dump[i]);
29         _pam_drop(dump[i]);
30     }
31     _pam_drop(dump);
32
33     return NULL;
34 }
35
36 /*
37  *  This function takes the supplied environment and uploads it to be
38  *  the PAM one.
39  */
40
41 int pam_misc_paste_env(pam_handle_t *pamh, const char * const * user_env)
42 {
43     for (; user_env && *user_env; ++user_env) {
44         int retval;
45
46         D(("uploading: %s", *user_env));
47         retval = pam_putenv(pamh, *user_env);
48         if (retval != PAM_SUCCESS) {
49             D(("error setting %s: %s", *user_env, pam_strerror(pamh,retval)));
50             return retval;
51         }
52     }
53     D(("done."));
54     return PAM_SUCCESS;
55 }
56
57 /*
58  * This is a wrapper to make pam behave in the way that setenv() does.
59  */
60
61 int pam_misc_setenv(pam_handle_t *pamh, const char *name
62                     , const char *value, int readonly)
63 {
64     char *tmp;
65     int retval;
66
67     if (readonly) {
68         const char *etmp;
69
70         /* we check if the variable is there already */
71         etmp = pam_getenv(pamh, name);
72         if (etmp != NULL) {
73             D(("failed to set readonly variable: %s", name));
74             return PAM_PERM_DENIED;          /* not allowed to overwrite */
75         }
76     }
77     if (asprintf(&tmp, "%s=%s", name, value) >= 0) {
78         D(("pam_putt()ing: %s", tmp));
79         retval = pam_putenv(pamh, tmp);
80         _pam_overwrite(tmp);                 /* purge */
81         _pam_drop(tmp);                      /* forget */
82     } else {
83         D(("malloc failure"));
84         retval = PAM_BUF_ERR;
85     }
86
87     return retval;
88 }