29 #ifdef CVMFS_NAMESPACE_GUARD
30 namespace CVMFS_NAMESPACE_GUARD {
35 for (
unsigned i = 0, l = raw.length(); i < l; ++i) {
36 if (!(((raw[i] >=
'0') && (raw[i] <=
'9')) ||
37 ((raw[i] >=
'A') && (raw[i] <=
'Z')) ||
38 ((raw[i] >=
'a') && (raw[i] <=
'z')) ||
39 (raw[i] ==
'/') || (raw[i] ==
':') || (raw[i] ==
'.') ||
40 (raw[i] ==
'_') || (raw[i] ==
'-') || (raw[i] ==
',')))
42 goto escape_shell_quote;
49 for (
unsigned i = 0, l = raw.length(); i < l; ++i) {
60 string result =
Trim(parameter);
62 if (result.find(
"readonly ") == 0) {
63 result = result.substr(9);
64 result =
Trim(result);
65 }
else if (result.find(
"export ") == 0) {
66 result = result.substr(7);
67 result =
Trim(result);
68 }
else if (result.find(
"eval ") == 0) {
69 result = result.substr(5);
70 result =
Trim(result);
76 vector <string> *tokens) {
77 size_t comment_idx = line->find(
"#");
78 if (comment_idx != string::npos)
79 *line = line->substr(0, comment_idx);
84 if (tokens->size() < 2)
86 string parameter = TrimParameter((*tokens)[0]);
87 if (parameter.find(
" ") != string::npos)
94 delete opt_templ_mgr_;
95 if (opt_templ_mgr_param != NULL) {
96 opt_templ_mgr_ = opt_templ_mgr_param;
100 for (std::map<std::string, std::string>::iterator it
101 = templatable_values_.begin();
102 it != templatable_values_.end();
104 config_[it->first].value = it->second;
105 opt_templ_mgr_->ParseString(&(config_[it->first].value));
106 UpdateEnvironment(it->first, config_[it->first]);
112 config_file.c_str());
114 FILE *fconfig = fopen(config_file.c_str(),
"r");
120 vector <string> tokens;
121 string parameter = SanitizeParameterAssignment(&line, &tokens);
122 if (parameter.empty())
126 tokens.erase(tokens.begin());
128 unsigned value_length = value.length();
129 if (value_length > 2) {
130 if ( ((value[0] ==
'"') && ((value[value_length - 1] ==
'"'))) ||
131 ((value[0] ==
'\'') && ((value[value_length - 1] ==
'\''))) )
133 value = value.substr(1, value_length - 2);
138 config_value.
source = config_file;
139 config_value.
value = value;
140 PopulateParameter(parameter, config_value);
147 const bool external) {
161 switch (pid_child = fork()) {
168 if (getpgrp() != getpid()) {
169 pid_t new_session = setsid();
170 assert(new_session != (pid_t)-1);
172 (void)open(config_file.c_str(), O_RDONLY);
175 retval = read(pipe_quit[0], &ready, 1);
188 FILE *fconfig = fopen(config_file.c_str(),
"r");
193 waitpid(pid_child, &statloc, 0);
198 string repo_required;
199 if (GetValue(
"CVMFS_CONFIG_REPO_REQUIRED", &repo_required) &&
200 IsOn(repo_required)) {
202 "required configuration repository directory does not exist: %s",
203 config_path.c_str());
210 "configuration repository directory does not exist: %s",
211 config_path.c_str());
219 retval =
Shell(&fd_stdin, &fd_stdout, &fd_stderr);
224 const string newline =
"\n";
225 const string cd =
"cd \"" + ((config_path ==
"") ?
"/" : config_path) +
"\"" +
227 WritePipe(fd_stdin, cd.data(), cd.length());
229 WritePipe(fd_stdin, line.data(), line.length());
230 WritePipe(fd_stdin, newline.data(), newline.length());
236 vector <string> tokens;
237 string parameter = SanitizeParameterAssignment(&line, &tokens);
238 if (parameter.empty())
242 value.
source = config_file;
243 const string sh_echo =
"echo $" + parameter +
"\n";
244 WritePipe(fd_stdin, sh_echo.data(), sh_echo.length());
246 PopulateParameter(parameter, value);
257 string *config_path) {
258 string cvmfs_mount_dir;
259 if (!GetValue(
"CVMFS_MOUNT_DIR", &cvmfs_mount_dir)) {
264 string config_repository;
265 if (GetValue(
"CVMFS_CONFIG_REPOSITORY", &config_repository)) {
266 if (config_repository.empty() || (config_repository == fqrn))
269 if (!repository_sanitizer.
IsValid(config_repository)) {
271 "invalid CVMFS_CONFIG_REPOSITORY: %s",
272 config_repository.c_str());
275 *config_path = cvmfs_mount_dir +
"/" + config_repository +
"/etc/cvmfs/";
283 if (taint_environment_) {
284 int retval = setenv(
"CVMFS_FQRN", fqrn.c_str(), 1);
288 protected_parameters_.clear();
289 ParsePath(
"/etc/cvmfs/default.conf",
false);
290 vector<string> dist_defaults =
292 for (
unsigned i = 0; i < dist_defaults.size(); ++i) {
293 ParsePath(dist_defaults[i],
false);
295 ProtectParameter(
"CVMFS_CONFIG_REPOSITORY");
296 string external_config_path;
297 if ((fqrn !=
"") && HasConfigRepository(fqrn, &external_config_path))
298 ParsePath(external_config_path +
"default.conf",
true);
299 ParsePath(
"/etc/cvmfs/default.local",
false);
304 assert(tokens.size() > 1);
305 tokens.erase(tokens.begin());
308 if (HasConfigRepository(fqrn, &external_config_path))
309 ParsePath(external_config_path+
"domain.d/" + domain +
".conf",
311 ParsePath(
"/etc/cvmfs/domain.d/" + domain +
".conf",
false);
312 ParsePath(
"/etc/cvmfs/domain.d/" + domain +
".local",
false);
314 if (HasConfigRepository(fqrn, &external_config_path))
315 ParsePath(external_config_path +
"config.d/" + fqrn +
".conf",
true);
316 ParsePath(
"/etc/cvmfs/config.d/" + fqrn +
".conf",
false);
317 ParsePath(
"/etc/cvmfs/config.d/" + fqrn +
".local",
false);
325 map<string, string>::const_iterator iter = protected_parameters_.find(param);
326 if ((iter != protected_parameters_.end()) && (iter->second != val.
value)) {
328 "error in cvmfs configuration: attempt to change protected %s "
330 param.c_str(), iter->second.c_str(), val.
value.c_str());
333 ParseValue(param, &val);
334 config_[param] = val;
335 UpdateEnvironment(param, val);
341 if (taint_environment_) {
342 int retval = setenv(param.c_str(), val.
value.c_str(), 1);
348 string orig = val->
value;
349 bool has_templ = opt_templ_mgr_->ParseString(&(val->
value));
351 templatable_values_[param] = orig;
360 (void) GetValue(param, &value);
361 protected_parameters_[param] = value;
371 map<string, ConfigValue>::const_iterator iter = config_.find(key);
372 return iter != config_.end();
377 map<string, ConfigValue>::const_iterator iter = config_.find(key);
378 if (iter != config_.end()) {
379 *value = iter->second.value;
389 bool retval = GetValue(key, &value);
392 "%s configuration parameter missing", key.c_str());
399 map<string, ConfigValue>::const_iterator iter = config_.find(key);
400 if (iter != config_.end()) {
401 *value = iter->second.source;
410 const string uppercase =
ToUpper(param_value);
411 return ((uppercase ==
"YES") || (uppercase ==
"ON") || (uppercase ==
"1") ||
412 (uppercase ==
"TRUE"));
417 const string uppercase =
ToUpper(param_value);
418 return ((uppercase ==
"NO") || (uppercase ==
"OFF") || (uppercase ==
"0") ||
419 (uppercase ==
"FALSE"));
424 vector<string> result;
425 for (map<string, ConfigValue>::const_iterator i = config_.begin(),
426 iEnd = config_.end(); i != iEnd; ++i)
428 result.push_back(i->first);
435 const string &key_prefix,
438 vector<string> result;
439 for (map<string, ConfigValue>::const_iterator i = config_.begin(),
440 iEnd = config_.end(); i != iEnd; ++i)
442 const bool ignore_prefix =
false;
443 if (
HasPrefix(i->first, key_prefix, ignore_prefix)) {
444 const string output_key = strip_prefix
445 ? i->first.substr(key_prefix.length())
447 result.push_back(output_key +
"=" + i->second.value);
456 vector<string> keys = GetAllKeys();
457 for (
unsigned i = 0, l = keys.size(); i < l; ++i) {
462 retval = GetValue(keys[i], &value);
464 retval = GetSource(keys[i], &source);
467 " # from " + source +
"\n";
474 config_value.
source =
"cvmfs_talk";
475 config_value.
value = value;
476 PopulateParameter(key, config_value);
481 config_value.
source =
"@INTERNAL@";
482 config_value.
value = value;
483 PopulateParameter(key, config_value);
488 protected_parameters_.erase(key);
490 if (taint_environment_)
491 unsetenv(key.c_str());
502 SetTemplate(kTemplateIdentFqrn, fqrn);
503 vector<string> fqrn_parts =
SplitString(fqrn,
'.');
504 SetTemplate(kTemplateIdentOrg, fqrn_parts[0]);
508 templates_[name] = val;
512 if (templates_.count(name)) {
513 return templates_[name];
515 std::string var_name =
"@" + name +
"@";
524 std::string in = *input;
525 bool has_vars =
false;
528 for (std::string::size_type i = 0; i < in.size(); i++) {
540 result += GetTemplate(stock);
550 result +=
"@" + stock;
557 return templates_.count(name);
560 #ifdef CVMFS_NAMESPACE_GUARD
std::string GetValueOrDie(const std::string &key)
std::string GetTemplate(std::string name)
static const char * kTemplateIdentOrg
static const char * kTemplateIdentFqrn
string Trim(const string &raw, bool trim_newline)
bool GetSource(const std::string &key, std::string *value)
string JoinStrings(const vector< string > &strings, const string &joint)
static string EscapeShell(const std::string &raw)
DefaultOptionsTemplateManager(std::string fqrn)
bool Shell(int *fd_stdin, int *fd_stdout, int *fd_stderr)
bool IsOn(const std::string ¶m_value) const
std::vector< std::string > GetEnvironmentSubset(const std::string &key_prefix, bool strip_prefix)
void PopulateParameter(const std::string ¶m, const ConfigValue val)
std::string SanitizeParameterAssignment(std::string *line, std::vector< std::string > *tokens)
assert((mem||(size==0))&&"Out Of Memory")
bool IsOff(const std::string ¶m_value) const
void ParseDefault(const std::string &fqrn)
bool HasConfigRepository(const std::string &fqrn, std::string *config_path)
void MakePipe(int pipe_fd[2])
void SwitchTemplateManager(OptionsTemplateManager *opt_templ_mgr_param)
void SetValue(const std::string &key, const std::string &value)
bool GetLineFile(FILE *f, std::string *line)
string ToUpper(const string &mixed_case)
bool TryParsePath(const std::string &config_file)
vector< string > SplitString(const string &str, char delim)
void SetValueFromTalk(const std::string &key, const std::string &value)
std::vector< std::string > GetAllKeys()
void ParseValue(const std::string param, ConfigValue *val)
bool HasPrefix(const string &str, const string &prefix, const bool ignore_case)
bool GetValue(const std::string &key, std::string *value) const
bool DirectoryExists(const std::string &path)
std::string TrimParameter(const std::string ¶meter)
void ParsePath(const std::string &config_file, const bool external)
bool GetLineFd(const int fd, std::string *line)
void SetTemplate(std::string name, std::string val)
void UpdateEnvironment(const std::string ¶m, ConfigValue val)
bool ParseString(std::string *input)
PathString GetParentPath(const PathString &path)
bool IsDefined(const std::string &key)
void WritePipe(int fd, const void *buf, size_t nbyte)
void ReadPipe(int fd, void *buf, size_t nbyte)
std::vector< std::string > FindFilesBySuffix(const std::string &dir, const std::string &suffix)
void ProtectParameter(const std::string ¶m)
void UnsetValue(const std::string &key)
bool HasTemplate(std::string name)
CVMFS_EXPORT void LogCvmfs(const LogSource source, const int mask, const char *format,...)