XRootD
Loading...
Searching...
No Matches
XrdOucEnv.cc
Go to the documentation of this file.
1/******************************************************************************/
2/* */
3/* X r d O u c E n v . c c */
4/* */
5/* (c) 2003 by the Board of Trustees of the Leland Stanford, Jr., University */
6/* All Rights Reserved */
7/* Produced by Andrew Hanushevsky for Stanford University under contract */
8/* DE-AC02-76-SFO0515 with the Department of Energy */
9/* */
10/* This file is part of the XRootD software suite. */
11/* */
12/* XRootD is free software: you can redistribute it and/or modify it under */
13/* the terms of the GNU Lesser General Public License as published by the */
14/* Free Software Foundation, either version 3 of the License, or (at your */
15/* option) any later version. */
16/* */
17/* XRootD is distributed in the hope that it will be useful, but WITHOUT */
18/* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or */
19/* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public */
20/* License for more details. */
21/* */
22/* You should have received a copy of the GNU Lesser General Public License */
23/* along with XRootD in a file called COPYING.LESSER (LGPL license) and file */
24/* COPYING (GPL license). If not, see <http://www.gnu.org/licenses/>. */
25/* */
26/* The copyright holder's institutional names and contributor's names may not */
27/* be used to endorse or promote products derived from this software without */
28/* specific prior written permission of the institution or contributor. */
29/******************************************************************************/
30
31#include "string.h"
32#include "stdio.h"
33#include <cstdlib>
34
35#include "XrdOuc/XrdOucEnv.hh"
37
38/******************************************************************************/
39/* C o n s t r u c t o r */
40/******************************************************************************/
41
42XrdOucEnv::XrdOucEnv(const char *vardata, int varlen,
43 const XrdSecEntity *secent)
44 : env_Hash(8,13), secEntity(secent)
45{
46 char *vdp, varsave, *varname, *varvalu;
47
48 if (!vardata) {global_env = 0; global_len = 0; return;}
49
50// Get the length of the global information (don't rely on its being correct)
51//
52 if (!varlen) varlen = strlen(vardata);
53
54// We want our env copy to start with a single ampersand
55//
56 while(*vardata == '&' && varlen) {vardata++; varlen--;}
57 if (!varlen) {global_env = 0; global_len = 0; return;}
58 global_env = (char *)malloc(varlen+2);
59 *global_env = '&'; vdp = global_env+1;
60 memcpy((void *)vdp, (const void *)vardata, (size_t)varlen);
61 *(vdp+varlen) = '\0'; global_len = varlen+1;
62
63// scan through the string looking for '&'
64//
65 while(*vdp)
66 {while(*vdp == '&') vdp++;
67 varname = vdp;
68
69 while(*vdp && *vdp != '=' && *vdp != '&') vdp++; // &....=
70 if (!*vdp) break;
71 if (*vdp == '&') continue;
72 *vdp = '\0';
73 varvalu = ++vdp;
74
75 while(*vdp && *vdp != '&') vdp++; // &....=....&
76 varsave = *vdp; *vdp = '\0';
77
78 if (*varname && *varvalu)
79 env_Hash.Rep(varname, strdup(varvalu), 0, Hash_dofree);
80
81 *vdp = varsave; *(varvalu-1) = '=';
82 }
83 return;
84}
85
86/******************************************************************************/
87/* D e l i m i t */
88/******************************************************************************/
89
90char *XrdOucEnv::Delimit(char *value)
91{
92 while(*value) if (*value == ',') {*value = '\0'; return ++value;}
93 else value++;
94 return (char *)0;
95}
96
97/******************************************************************************/
98/* Private: E n v B u i l d T i d y */
99/******************************************************************************/
100
101#define TIDY_ENVVAR " Xrd Ouc Env "
102
103void XrdOucEnv::EnvBuildTidy()
104{
105 char *tidyEnv, *authInfo;
106 int aBeg, aEnd;
107
108// We need to sanitize the current env string by removing auth info. If there
109// is no auth informationn, then we can short cicuit this.
110//
111 if ((authInfo = strstr(global_env, "authz=")) == 0)
112 {Put(TIDY_ENVVAR, "");
113 return;
114 }
115
116// Get position of the auth string and check if we can do a fast deletion.
117// Otherwise, we must trudge along.
118//
119 aBeg = authInfo - global_env;
120 if (aBeg && global_env[aBeg-1] == '&') aBeg--;
121 if (!(tidyEnv = index(authInfo+6, '&')))
122 {char aSave = global_env[aBeg];
123 global_env[aBeg] = 0;
124 Put(TIDY_ENVVAR, global_env);
125 global_env[aBeg] = aSave;
126 } else {
127 XrdOucString tidyStr(global_env);
128 do{if ((aEnd = tidyStr.find('&', aBeg+6)) == STR_NPOS)
129 {tidyStr.erase(aBeg);
130 break;
131 }
132 tidyStr.erase(aBeg, aEnd-aBeg);
133 } while((aBeg = tidyStr.find("&authz=")) != STR_NPOS);
134 Put(TIDY_ENVVAR, tidyStr.c_str());
135 }
136}
137
138/******************************************************************************/
139/* E n v T i d y */
140/******************************************************************************/
141
142char *XrdOucEnv::EnvTidy(int &envlen)
143{
144 char *tidyEnv;
145 int tries = 1;
146
147// Check if there is no env
148//
149 if ((envlen = global_len) == 0 || global_env == 0) return 0;
150
151// Check if we have produced a tidy version. If noo build one and try again.
152//
153do{if ((tidyEnv = Get(TIDY_ENVVAR)))
154 {if (*tidyEnv == 0) break;
155 envlen = strlen(tidyEnv);
156 return tidyEnv;
157 }
158 EnvBuildTidy();
159 } while(tries--);
160
161// Return standard env
162//
163 return Env(envlen);
164}
165
166/******************************************************************************/
167/* E x p o r t */
168/******************************************************************************/
169
170int XrdOucEnv::Export(const char *Var, const char *Val)
171{
172 int vLen = strlen(Var);
173 char *eBuff;
174
175// If this is a null value then substitute a null string
176//
177 if (!Val) Val = "";
178
179// Allocate memory. Note that this memory will appear to be lost.
180//
181 eBuff = (char *)malloc(vLen+strlen(Val)+2); // +2 for '=' and '\0'
182
183// Set up envar
184//
185 strcpy(eBuff, Var);
186 *(eBuff+vLen) = '=';
187 strcpy(eBuff+vLen+1, Val);
188 return putenv(eBuff);
189}
190
191/******************************************************************************/
192
193int XrdOucEnv::Export(const char *Var, int Val)
194{
195 char buff[32];
196 sprintf(buff, "%d", Val);
197 return Export(Var, buff);
198}
199
200
201/******************************************************************************/
202/* I m p o r t */
203/******************************************************************************/
204bool XrdOucEnv::Import( const char *var, char *&val )
205{
206 char *value = getenv( var );
207 if( !value || !*value )
208 return false;
209
210 val = value;
211 return true;
212}
213
214/******************************************************************************/
215/* I m p o r t */
216/******************************************************************************/
217bool XrdOucEnv::Import( const char *var, long &val )
218{
219 char *value;
220 if( !Import( var, value ) )
221 return false;
222
223 char *status;
224 val = strtol( value, &status, 0 );
225
226 if( *status != 0 )
227 return false;
228 return true;
229}
230
231/******************************************************************************/
232/* G e t I n t */
233/******************************************************************************/
234
235long XrdOucEnv::GetInt(const char *varname)
236{
237 char *cP;
238
239// Retrieve a char* value from the Hash table and convert it into a long.
240// Return -999999999 if the varname does not exist
241//
242 if ((cP = env_Hash.Find(varname)) == NULL) return -999999999;
243 return atol(cP);
244}
245
246/******************************************************************************/
247/* P u t I n t */
248/******************************************************************************/
249
250void XrdOucEnv::PutInt(const char *varname, long value)
251{
252// Convert the long into a char* and the put it into the hash table
253//
254 char stringValue[24];
255 sprintf(stringValue, "%ld", value);
256 env_Hash.Rep(varname, strdup(stringValue), 0, Hash_dofree);
257}
258
259/******************************************************************************/
260/* G e t P t r */
261/******************************************************************************/
262
263void *XrdOucEnv::GetPtr(const char *varname)
264{
265 void *Valp;
266 char *cP, *Value = (char *)&Valp;
267 int cLen, n, i = 0, Odd = 0;
268
269// Retrieve the variable from the hash
270//
271 if ((cP = env_Hash.Find(varname)) == NULL) return (void *)0;
272
273// Verify that the string is not too long or too short
274//
275 if ((cLen = strlen(cP)) != (int)sizeof(void *)*2) return (void *)0;
276
277// Now convert the hex string back to its pointer value
278//
279 while(cLen--)
280 { if (*cP >= '0' && *cP <= '9') n = *cP-48;
281 else if (*cP >= 'a' && *cP <= 'f') n = *cP-87;
282 else if (*cP >= 'A' && *cP <= 'F') n = *cP-55;
283 else return (void *)0;
284 if (Odd) Value[i++] |= n;
285 else Value[i ] = n << 4;
286 cP++; Odd = ~Odd;
287 }
288
289// All done, return the actual pointer value
290//
291 return Valp;
292}
293
294/******************************************************************************/
295/* P u t P t r */
296/******************************************************************************/
297
298void XrdOucEnv::PutPtr(const char *varname, void *value)
299{
300 static char hv[] = "0123456789abcdef";
301 char Buff[sizeof(void *)*2+1], *Value = (char *)&value;
302 int i, j = 0;
303
304// Convert the pointer value to a hex string
305//
306 if (value) for (i = 0; i <(int)sizeof(void *); i++)
307 {Buff[j++] = hv[(Value[i] >> 4) & 0x0f];
308 Buff[j++] = hv[ Value[i] & 0x0f];
309 }
310 Buff[j] = '\0';
311
312// Replace the value in he hash
313//
314 env_Hash.Rep(varname, strdup(Buff), 0, Hash_dofree);
315}
#define TIDY_ENVVAR
Definition XrdOucEnv.cc:101
@ Hash_dofree
Definition XrdOucHash.hh:56
#define STR_NPOS
bool Import
void PutInt(const char *varname, long value)
Definition XrdOucEnv.cc:250
char * Env(int &envlen)
Definition XrdOucEnv.hh:48
long GetInt(const char *varname)
Definition XrdOucEnv.cc:235
char * EnvTidy(int &envlen)
Definition XrdOucEnv.cc:142
static bool Import(const char *var, char *&val)
Definition XrdOucEnv.cc:204
char * Get(const char *varname)
Definition XrdOucEnv.hh:69
static int Export(const char *Var, const char *Val)
Definition XrdOucEnv.cc:170
void * GetPtr(const char *varname)
Definition XrdOucEnv.cc:263
void PutPtr(const char *varname, void *value)
Definition XrdOucEnv.cc:298
void Put(const char *varname, const char *value)
Definition XrdOucEnv.hh:85
char * Delimit(char *value)
Definition XrdOucEnv.cc:90
XrdOucEnv(const char *vardata=0, int vardlen=0, const XrdSecEntity *secent=0)
Definition XrdOucEnv.cc:42
T * Rep(const char *KeyVal, T *KeyData, const int LifeTime=0, XrdOucHash_Options opt=Hash_default)
T * Find(const char *KeyVal, time_t *KeyTime=0)
int erase(int start=0, int size=0)
int find(const char c, int start=0, bool forward=1)
const char * c_str() const