OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <pwd.h> | 5 #include <pwd.h> |
6 | 6 |
7 #include "chrome/browser/policy/policy_path_parser.h" | 7 #include "chrome/browser/policy/policy_path_parser.h" |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 | 10 |
11 namespace policy { | 11 namespace policy { |
12 | 12 |
13 namespace path_parser { | 13 namespace path_parser { |
14 | 14 |
15 const char* kMachineNamePolicyVarName = "${machine_name}"; | 15 const char* kMachineNamePolicyVarName = "${machine_name}"; |
16 const char* kUserNamePolicyVarName = "${user_name}"; | 16 const char* kUserNamePolicyVarName = "${user_name}"; |
17 | 17 |
18 // Replaces all variable occurrences in the policy string with the respective | 18 // Replaces all variable occurrences in the policy string with the respective |
19 // system settings values. | 19 // system settings values. |
20 FilePath::StringType ExpandPathVariables( | 20 FilePath::StringType ExpandPathVariables( |
21 const FilePath::StringType& untranslated_string) { | 21 const FilePath::StringType& untranslated_string) { |
22 FilePath::StringType result(untranslated_string); | 22 FilePath::StringType result(untranslated_string); |
| 23 if (result.length() == 0) |
| 24 return result; |
| 25 // Sanitize quotes in case of any around the whole string. |
| 26 if (result.length() > 1 && |
| 27 ((result[0] == '"' && result[result.length() - 1] == '"') || |
| 28 (result[0] == '\'' && result[result.length() - 1] == '\''))) { |
| 29 // Strip first and last char which should be matching quotes now. |
| 30 result = result.substr(1, result.length() - 2); |
| 31 } |
23 // Translate two special variables ${user_name} and ${machine_name} | 32 // Translate two special variables ${user_name} and ${machine_name} |
24 size_t position = result.find(kUserNamePolicyVarName); | 33 size_t position = result.find(kUserNamePolicyVarName); |
25 if (position != std::string::npos) { | 34 if (position != std::string::npos) { |
26 struct passwd* user = getpwuid(geteuid()); | 35 struct passwd* user = getpwuid(geteuid()); |
27 if (user) { | 36 if (user) { |
28 result.replace(position, strlen(kUserNamePolicyVarName), user->pw_name); | 37 result.replace(position, strlen(kUserNamePolicyVarName), user->pw_name); |
29 } else { | 38 } else { |
30 LOG(ERROR) << "Username variable can not be resolved. "; | 39 LOG(ERROR) << "Username variable can not be resolved. "; |
31 } | 40 } |
32 } | 41 } |
33 position = result.find(kMachineNamePolicyVarName); | 42 position = result.find(kMachineNamePolicyVarName); |
34 if (position != std::string::npos) { | 43 if (position != std::string::npos) { |
35 char machinename[255]; | 44 char machinename[255]; |
36 if (gethostname(machinename, 255) == 0) { | 45 if (gethostname(machinename, 255) == 0) { |
37 result.replace(position, strlen(kMachineNamePolicyVarName), machinename); | 46 result.replace(position, strlen(kMachineNamePolicyVarName), machinename); |
38 } else { | 47 } else { |
39 LOG(ERROR) << "Machine name variable can not be resolved."; | 48 LOG(ERROR) << "Machine name variable can not be resolved."; |
40 } | 49 } |
41 } | 50 } |
42 return result; | 51 return result; |
43 } | 52 } |
44 | 53 |
45 } // namespace path_parser | 54 } // namespace path_parser |
46 | 55 |
47 } // namespace policy | 56 } // namespace policy |
OLD | NEW |