summaryrefslogtreecommitdiff
path: root/nsprpub/pr/src/misc/prenv.c
blob: b057a1c89ae27063a572065addcad99d001f1e8e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include <string.h>
#include <stdlib.h>
#include "primpl.h"
#include "prmem.h"

#if defined(XP_UNIX)
#include <unistd.h>
#if defined(DARWIN)
#if defined(HAVE_CRT_EXTERNS_H)
#include <crt_externs.h>
#endif /* HAVE_CRT_EXTERNS_H */
#else  /* DARWIN */
PR_IMPORT_DATA(char **) environ;
#endif /* DARWIN */
#endif /* XP_UNIX */

#if !defined(HAVE_SECURE_GETENV) && defined(HAVE___SECURE_GETENV)
#define secure_getenv __secure_getenv
#define HAVE_SECURE_GETENV 1
#endif

/* Lock used to lock the environment */
#if defined(_PR_NO_PREEMPT)
#define _PR_NEW_LOCK_ENV()
#define _PR_DELETE_LOCK_ENV()
#define _PR_LOCK_ENV()
#define _PR_UNLOCK_ENV()
#elif defined(_PR_LOCAL_THREADS_ONLY)
extern _PRCPU * _pr_primordialCPU;
static PRIntn _is;
#define _PR_NEW_LOCK_ENV()
#define _PR_DELETE_LOCK_ENV()
#define _PR_LOCK_ENV() if (_pr_primordialCPU) _PR_INTSOFF(_is);
#define _PR_UNLOCK_ENV() if (_pr_primordialCPU) _PR_INTSON(_is);
#else
static PRLock *_pr_envLock = NULL;
#define _PR_NEW_LOCK_ENV() {_pr_envLock = PR_NewLock();}
#define _PR_DELETE_LOCK_ENV() \
    { if (_pr_envLock) { PR_DestroyLock(_pr_envLock); _pr_envLock = NULL; } }
#define _PR_LOCK_ENV() { if (_pr_envLock) PR_Lock(_pr_envLock); }
#define _PR_UNLOCK_ENV() { if (_pr_envLock) PR_Unlock(_pr_envLock); }
#endif

/************************************************************************/

void _PR_InitEnv(void)
{
    _PR_NEW_LOCK_ENV();
}

void _PR_CleanupEnv(void)
{
    _PR_DELETE_LOCK_ENV();
}

PR_IMPLEMENT(char*) PR_GetEnv(const char *var)
{
    char *ev;

    if (!_pr_initialized) {
        _PR_ImplicitInitialization();
    }

    _PR_LOCK_ENV();
    ev = _PR_MD_GET_ENV(var);
    _PR_UNLOCK_ENV();
    return ev;
}

PR_IMPLEMENT(char*) PR_GetEnvSecure(const char *var)
{
#ifdef HAVE_SECURE_GETENV
    char *ev;

    if (!_pr_initialized) {
        _PR_ImplicitInitialization();
    }

    _PR_LOCK_ENV();
    ev = secure_getenv(var);
    _PR_UNLOCK_ENV();

    return ev;
#else
#ifdef XP_UNIX
    /*
    ** Fall back to checking uids and gids.  This won't detect any other
    ** privilege-granting mechanisms the platform may have.  This also
    ** can't detect the case where the process already called
    ** setuid(geteuid()) and/or setgid(getegid()).
    */
    if (getuid() != geteuid() || getgid() != getegid()) {
        return NULL;
    }
#endif /* XP_UNIX */
    return PR_GetEnv(var);
#endif /* HAVE_SECURE_GETENV */
}

PR_IMPLEMENT(PRStatus) PR_SetEnv(const char *string)
{
    PRIntn result;

    if (!_pr_initialized) {
        _PR_ImplicitInitialization();
    }

    if (!strchr(string, '=')) {
        return(PR_FAILURE);
    }

    _PR_LOCK_ENV();
    result = _PR_MD_PUT_ENV((char*)string);
    _PR_UNLOCK_ENV();
    return result ? PR_FAILURE : PR_SUCCESS;
}

#if defined(XP_UNIX) && (!defined(DARWIN) || defined(HAVE_CRT_EXTERNS_H))
PR_IMPLEMENT(char **) PR_DuplicateEnvironment(void)
{
    char **the_environ, **result, **end, **src, **dst;

    _PR_LOCK_ENV();
#ifdef DARWIN
    the_environ = *(_NSGetEnviron());
#else
    the_environ = environ;
#endif

    for (end = the_environ; *end != NULL; end++)
        /* empty loop body */;

    result = (char **)PR_Malloc(sizeof(char *) * (end - the_environ + 1));
    if (result != NULL) {
        for (src = the_environ, dst = result; src != end; src++, dst++) {
            size_t len;

            len = strlen(*src) + 1;
            *dst = PR_Malloc(len);
            if (*dst == NULL) {
                /* Allocation failed.  Must clean up the half-copied env. */
                char **to_delete;

                for (to_delete = result; to_delete != dst; to_delete++) {
                    PR_Free(*to_delete);
                }
                PR_Free(result);
                result = NULL;
                goto out;
            }
            memcpy(*dst, *src, len);
        }
        *dst = NULL;
    }
out:
    _PR_UNLOCK_ENV();
    return result;
}
#else
/* This platform doesn't support raw access to the environ block. */
PR_IMPLEMENT(char **) PR_DuplicateEnvironment(void)
{
    return NULL;
}
#endif