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 "chrome/browser/policy/policy_path_parser.h" | 5 #include "chrome/browser/policy/policy_path_parser.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/basictypes.h" |
8 #include "base/files/file_path.h" | 8 #include "base/files/file_path.h" |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #import "base/mac/scoped_nsautorelease_pool.h" | 10 #import "base/mac/scoped_nsautorelease_pool.h" |
11 #include "base/strings/sys_string_conversions.h" | 11 #include "base/strings/sys_string_conversions.h" |
12 #include "policy/policy_constants.h" | 12 #include "policy/policy_constants.h" |
13 | 13 |
14 #import <Cocoa/Cocoa.h> | 14 #import <Cocoa/Cocoa.h> |
15 #import <SystemConfiguration/SCDynamicStore.h> | 15 #import <SystemConfiguration/SCDynamicStore.h> |
16 #import <SystemConfiguration/SCDynamicStoreCopySpecific.h> | 16 #import <SystemConfiguration/SCDynamicStoreCopySpecific.h> |
17 | 17 |
18 #include <string> | 18 #include <string> |
19 | 19 |
20 namespace policy { | 20 namespace policy { |
21 | 21 |
22 namespace path_parser { | 22 namespace path_parser { |
23 | 23 |
24 namespace internal { | |
25 | |
26 bool GetFolder(NSSearchPathDirectory id, base::FilePath::StringType* value) { | |
27 NSArray* searchpaths = | |
28 NSSearchPathForDirectoriesInDomains(id, NSAllDomainsMask, true); | |
29 if ([searchpaths count] > 0) { | |
30 NSString* variable_value = [searchpaths objectAtIndex:0]; | |
31 *value = base::SysNSStringToUTF8(variable_value); | |
32 return true; | |
33 } | |
34 return false; | |
35 } | |
36 | |
37 // The wrapper is used instead of callbacks since these function are | |
38 // initialized in a global table and using callbacks in the table results in | |
39 // the increase in size of static initializers. | |
40 #define WRAP_GET_FORLDER_FUNCTION(FunctionName, FolderId) \ | |
41 bool FunctionName(base::FilePath::StringType* value) { \ | |
42 return GetFolder(FolderId, value); \ | |
43 } | |
44 | |
45 WRAP_GET_FORLDER_FUNCTION(GetMacUserFolderPath, NSUserDirectory) | |
46 WRAP_GET_FORLDER_FUNCTION(GetMacDocumentsFolderPath, NSDocumentDirectory) | |
47 | |
48 bool GetUserName(base::FilePath::StringType* value) { | |
49 NSString* username = NSUserName(); | |
50 if (username) | |
51 *value = base::SysNSStringToUTF8(username); | |
52 else | |
53 LOG(ERROR) << "Username variable can not be resolved."; | |
54 return (username != NULL); | |
55 } | |
56 | |
57 bool GetMachineName(base::FilePath::StringType* value) { | |
58 SCDynamicStoreContext context = {0, NULL, NULL, NULL}; | |
59 SCDynamicStoreRef store = SCDynamicStoreCreate( | |
60 kCFAllocatorDefault, CFSTR("policy_subsystem"), NULL, &context); | |
61 CFStringRef machinename = SCDynamicStoreCopyLocalHostName(store); | |
62 if (machinename) { | |
63 *value = base::SysCFStringRefToUTF8(machinename); | |
64 CFRelease(machinename); | |
65 } else { | |
66 LOG(ERROR) << "Machine name variable can not be resolved."; | |
67 } | |
68 CFRelease(store); | |
69 return (machinename != NULL); | |
70 } | |
71 | |
72 const char* kUserNamePolicyVarName = "${user_name}"; | 24 const char* kUserNamePolicyVarName = "${user_name}"; |
73 const char* kMachineNamePolicyVarName = "${machine_name}"; | 25 const char* kMachineNamePolicyVarName = "${machine_name}"; |
74 const char* kMacUsersDirectory = "${users}"; | 26 const char* kMacUsersDirectory = "${users}"; |
75 const char* kMacDocumentsFolderVarName = "${documents}"; | 27 const char* kMacDocumentsFolderVarName = "${documents}"; |
76 | 28 |
77 // A table mapping variable names to their respective get value function | 29 struct MacFolderNamesToSPDMaping { |
78 // pointers. | 30 const char* name; |
79 const VariableNameAndValueCallback kVariableNameAndValueCallbacks[] = { | 31 NSSearchPathDirectory id; |
80 {kUserNamePolicyVarName, &GetUserName}, | 32 }; |
81 {kMachineNamePolicyVarName, &GetMachineName}, | |
82 {kMacUsersDirectory, &GetMacUserFolderPath}, | |
83 {kMacDocumentsFolderVarName, &GetMacDocumentsFolderPath}}; | |
84 | 33 |
85 // Total number of entries in the mapping table. | 34 // Mapping from variable names to MacOS NSSearchPathDirectory ids. |
86 const int kNoOfVariables = arraysize(kVariableNameAndValueCallbacks); | 35 const MacFolderNamesToSPDMaping mac_folder_mapping[] = { |
| 36 { kMacUsersDirectory, NSUserDirectory}, |
| 37 { kMacDocumentsFolderVarName, NSDocumentDirectory} |
| 38 }; |
87 | 39 |
88 } // namespace internal | 40 // Replaces all variable occurrences in the policy string with the respective |
| 41 // system settings values. |
| 42 base::FilePath::StringType ExpandPathVariables( |
| 43 const base::FilePath::StringType& untranslated_string) { |
| 44 base::FilePath::StringType result(untranslated_string); |
| 45 if (result.length() == 0) |
| 46 return result; |
| 47 // Sanitize quotes in case of any around the whole string. |
| 48 if (result.length() > 1 && |
| 49 ((result[0] == '"' && result[result.length() - 1] == '"') || |
| 50 (result[0] == '\'' && result[result.length() - 1] == '\''))) { |
| 51 // Strip first and last char which should be matching quotes now. |
| 52 result = result.substr(1, result.length() - 2); |
| 53 } |
| 54 // First translate all path variables we recognize. |
| 55 for (size_t i = 0; i < arraysize(mac_folder_mapping); ++i) { |
| 56 size_t position = result.find(mac_folder_mapping[i].name); |
| 57 if (position != std::string::npos) { |
| 58 NSArray* searchpaths = NSSearchPathForDirectoriesInDomains( |
| 59 mac_folder_mapping[i].id, NSAllDomainsMask, true); |
| 60 if ([searchpaths count] > 0) { |
| 61 NSString *variable_value = [searchpaths objectAtIndex:0]; |
| 62 result.replace(position, strlen(mac_folder_mapping[i].name), |
| 63 base::SysNSStringToUTF8(variable_value)); |
| 64 } |
| 65 } |
| 66 } |
| 67 // Next translate two special variables ${user_name} and ${machine_name} |
| 68 size_t position = result.find(kUserNamePolicyVarName); |
| 69 if (position != std::string::npos) { |
| 70 NSString* username = NSUserName(); |
| 71 if (username) { |
| 72 result.replace(position, strlen(kUserNamePolicyVarName), |
| 73 base::SysNSStringToUTF8(username)); |
| 74 } else { |
| 75 LOG(ERROR) << "Username variable can not be resolved."; |
| 76 } |
| 77 } |
| 78 position = result.find(kMachineNamePolicyVarName); |
| 79 if (position != std::string::npos) { |
| 80 SCDynamicStoreContext context = { 0, NULL, NULL, NULL }; |
| 81 SCDynamicStoreRef store = SCDynamicStoreCreate(kCFAllocatorDefault, |
| 82 CFSTR("policy_subsystem"), |
| 83 NULL, &context); |
| 84 CFStringRef machinename = SCDynamicStoreCopyLocalHostName(store); |
| 85 if (machinename) { |
| 86 result.replace(position, strlen(kMachineNamePolicyVarName), |
| 87 base::SysCFStringRefToUTF8(machinename)); |
| 88 CFRelease(machinename); |
| 89 } else { |
| 90 LOG(ERROR) << "Machine name variable can not be resolved."; |
| 91 } |
| 92 CFRelease(store); |
| 93 } |
| 94 return result; |
| 95 } |
89 | 96 |
90 void CheckUserDataDirPolicy(base::FilePath* user_data_dir) { | 97 void CheckUserDataDirPolicy(base::FilePath* user_data_dir) { |
91 base::mac::ScopedNSAutoreleasePool pool; | 98 base::mac::ScopedNSAutoreleasePool pool; |
92 | 99 |
93 // Since the configuration management infrastructure is not initialized when | 100 // Since the configuration management infrastructure is not initialized when |
94 // this code runs, read the policy preference directly. | 101 // this code runs, read the policy preference directly. |
95 NSString* key = base::SysUTF8ToNSString(policy::key::kUserDataDir); | 102 NSString* key = base::SysUTF8ToNSString(policy::key::kUserDataDir); |
96 NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults]; | 103 NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults]; |
97 NSString* value = [defaults stringForKey:key]; | 104 NSString* value = [defaults stringForKey:key]; |
98 if (value && [defaults objectIsForcedForKey:key]) { | 105 if (value && [defaults objectIsForcedForKey:key]) { |
99 std::string string_value = base::SysNSStringToUTF8(value); | 106 std::string string_value = base::SysNSStringToUTF8(value); |
100 // Now replace any vars the user might have used. | 107 // Now replace any vars the user might have used. |
101 string_value = policy::path_parser::ExpandPathVariables(string_value); | 108 string_value = policy::path_parser::ExpandPathVariables(string_value); |
102 *user_data_dir = base::FilePath(string_value); | 109 *user_data_dir = base::FilePath(string_value); |
103 } | 110 } |
104 } | 111 } |
105 | 112 |
106 } // namespace path_parser | 113 } // namespace path_parser |
107 | 114 |
108 } // namespace policy | 115 } // namespace policy |
OLD | NEW |