OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 #include <sys/types.h> | 6 #include <sys/types.h> |
7 #include <unistd.h> | 7 #include <unistd.h> |
8 | 8 |
9 #include "chrome/browser/policy/policy_path_parser.h" | 9 #include "chrome/browser/policy/policy_path_parser.h" |
10 | 10 |
11 #include "base/bind.h" | |
12 #include "base/logging.h" | 11 #include "base/logging.h" |
13 | 12 |
14 namespace policy { | 13 namespace policy { |
15 | 14 |
16 namespace path_parser { | 15 namespace path_parser { |
17 | 16 |
18 namespace internal { | |
19 | |
20 const char* kMachineNamePolicyVarName = "${machine_name}"; | 17 const char* kMachineNamePolicyVarName = "${machine_name}"; |
21 const char* kUserNamePolicyVarName = "${user_name}"; | 18 const char* kUserNamePolicyVarName = "${user_name}"; |
22 | 19 |
23 bool GetUserName(base::FilePath::StringType* value) { | 20 // Replaces all variable occurrences in the policy string with the respective |
24 struct passwd* user = getpwuid(geteuid()); | 21 // system settings values. |
25 if (user) | 22 base::FilePath::StringType ExpandPathVariables( |
26 *value = base::FilePath::StringType(user->pw_name); | 23 const base::FilePath::StringType& untranslated_string) { |
27 else | 24 base::FilePath::StringType result(untranslated_string); |
28 LOG(ERROR) << "Username variable can not be resolved. "; | 25 if (result.length() == 0) |
29 return (user != NULL); | 26 return result; |
| 27 // Sanitize quotes in case of any around the whole string. |
| 28 if (result.length() > 1 && |
| 29 ((result[0] == '"' && result[result.length() - 1] == '"') || |
| 30 (result[0] == '\'' && result[result.length() - 1] == '\''))) { |
| 31 // Strip first and last char which should be matching quotes now. |
| 32 result = result.substr(1, result.length() - 2); |
| 33 } |
| 34 // Translate two special variables ${user_name} and ${machine_name} |
| 35 size_t position = result.find(kUserNamePolicyVarName); |
| 36 if (position != std::string::npos) { |
| 37 struct passwd* user = getpwuid(geteuid()); |
| 38 if (user) { |
| 39 result.replace(position, strlen(kUserNamePolicyVarName), user->pw_name); |
| 40 } else { |
| 41 LOG(ERROR) << "Username variable can not be resolved. "; |
| 42 } |
| 43 } |
| 44 position = result.find(kMachineNamePolicyVarName); |
| 45 if (position != std::string::npos) { |
| 46 char machinename[255]; |
| 47 if (gethostname(machinename, 255) == 0) { |
| 48 result.replace(position, strlen(kMachineNamePolicyVarName), machinename); |
| 49 } else { |
| 50 LOG(ERROR) << "Machine name variable can not be resolved."; |
| 51 } |
| 52 } |
| 53 return result; |
30 } | 54 } |
31 | 55 |
32 bool GetMachineName(base::FilePath::StringType* value) { | |
33 char machinename[255]; | |
34 int status; | |
35 if ((status = gethostname(machinename, 255)) == 0) | |
36 *value = base::FilePath::StringType(machinename); | |
37 else | |
38 LOG(ERROR) << "Machine name variable can not be resolved."; | |
39 return (status == 0); | |
40 } | |
41 | |
42 // A table mapping variable name to their respective getvalue callbacks. | |
43 const VariableNameAndValueCallback kVariableNameAndValueCallbacks[] = { | |
44 {kUserNamePolicyVarName, &GetUserName}, | |
45 {kMachineNamePolicyVarName, &GetMachineName}}; | |
46 | |
47 // Total number of entries in the mapping table. | |
48 const int kNoOfVariables = arraysize(kVariableNameAndValueCallbacks); | |
49 | |
50 } // namespace internal | |
51 | |
52 void CheckUserDataDirPolicy(base::FilePath* user_data_dir) { | 56 void CheckUserDataDirPolicy(base::FilePath* user_data_dir) { |
53 // This function is not implemented in Linux because we don't support the | 57 // This function is not implemented in Linux because we don't support the |
54 // policy on this platform. | 58 // policy on this platform. |
55 NOTREACHED(); | 59 NOTREACHED(); |
56 } | 60 } |
57 | 61 |
58 } // namespace path_parser | 62 } // namespace path_parser |
59 | 63 |
60 } // namespace policy | 64 } // namespace policy |
OLD | NEW |