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" |
11 #include "base/logging.h" | 12 #include "base/logging.h" |
12 | 13 |
13 namespace policy { | 14 namespace policy { |
14 | 15 |
15 namespace path_parser { | 16 namespace path_parser { |
16 | 17 |
| 18 namespace internal { |
| 19 |
17 const char* kMachineNamePolicyVarName = "${machine_name}"; | 20 const char* kMachineNamePolicyVarName = "${machine_name}"; |
18 const char* kUserNamePolicyVarName = "${user_name}"; | 21 const char* kUserNamePolicyVarName = "${user_name}"; |
19 | 22 |
20 // Replaces all variable occurrences in the policy string with the respective | 23 bool GetUserName(base::FilePath::StringType* value) { |
21 // system settings values. | 24 struct passwd* user = getpwuid(geteuid()); |
22 base::FilePath::StringType ExpandPathVariables( | 25 if (user) |
23 const base::FilePath::StringType& untranslated_string) { | 26 *value = base::FilePath::StringType(user->pw_name); |
24 base::FilePath::StringType result(untranslated_string); | 27 else |
25 if (result.length() == 0) | 28 LOG(ERROR) << "Username variable can not be resolved. "; |
26 return result; | 29 return (user != NULL); |
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; | |
54 } | 30 } |
55 | 31 |
| 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 |
56 void CheckUserDataDirPolicy(base::FilePath* user_data_dir) { | 52 void CheckUserDataDirPolicy(base::FilePath* user_data_dir) { |
57 // This function is not implemented in Linux because we don't support the | 53 // This function is not implemented in Linux because we don't support the |
58 // policy on this platform. | 54 // policy on this platform. |
59 NOTREACHED(); | 55 NOTREACHED(); |
60 } | 56 } |
61 | 57 |
62 } // namespace path_parser | 58 } // namespace path_parser |
63 | 59 |
64 } // namespace policy | 60 } // namespace policy |
OLD | NEW |