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
3 // found in the LICENSE file.
5 #include "chrome/browser/policy/policy_path_parser.h"
7 #include "base/basictypes.h"
8 #include "base/files/file_path.h"
9 #include "base/logging.h"
10 #import "base/mac/scoped_nsautorelease_pool.h"
11 #include "base/strings/sys_string_conversions.h"
12 #include "policy/policy_constants.h"
14 #import <Cocoa/Cocoa.h>
15 #import <SystemConfiguration/SCDynamicStore.h>
16 #import <SystemConfiguration/SCDynamicStoreCopySpecific.h>
17 #import <SystemConfiguration/SystemConfiguration.h>
23 namespace path_parser {
25 const char* kUserNamePolicyVarName = "${user_name}";
26 const char* kMachineNamePolicyVarName = "${machine_name}";
27 const char* kMacUsersDirectory = "${users}";
28 const char* kMacDocumentsFolderVarName = "${documents}";
30 struct MacFolderNamesToSPDMaping {
32 NSSearchPathDirectory id;
35 // Mapping from variable names to MacOS NSSearchPathDirectory ids.
36 const MacFolderNamesToSPDMaping mac_folder_mapping[] = {
37 { kMacUsersDirectory, NSUserDirectory},
38 { kMacDocumentsFolderVarName, NSDocumentDirectory}
41 // Replaces all variable occurrences in the policy string with the respective
42 // system settings values.
43 base::FilePath::StringType ExpandPathVariables(
44 const base::FilePath::StringType& untranslated_string) {
45 base::FilePath::StringType result(untranslated_string);
46 if (result.length() == 0)
48 // Sanitize quotes in case of any around the whole string.
49 if (result.length() > 1 &&
50 ((result[0] == '"' && result[result.length() - 1] == '"') ||
51 (result[0] == '\'' && result[result.length() - 1] == '\''))) {
52 // Strip first and last char which should be matching quotes now.
53 result = result.substr(1, result.length() - 2);
55 // First translate all path variables we recognize.
56 for (size_t i = 0; i < arraysize(mac_folder_mapping); ++i) {
57 size_t position = result.find(mac_folder_mapping[i].name);
58 if (position != std::string::npos) {
59 NSArray* searchpaths = NSSearchPathForDirectoriesInDomains(
60 mac_folder_mapping[i].id, NSAllDomainsMask, true);
61 if ([searchpaths count] > 0) {
62 NSString *variable_value = [searchpaths objectAtIndex:0];
63 result.replace(position, strlen(mac_folder_mapping[i].name),
64 base::SysNSStringToUTF8(variable_value));
68 // Next translate two special variables ${user_name} and ${machine_name}
69 size_t position = result.find(kUserNamePolicyVarName);
70 if (position != std::string::npos) {
71 NSString* username = NSUserName();
73 result.replace(position, strlen(kUserNamePolicyVarName),
74 base::SysNSStringToUTF8(username));
76 LOG(ERROR) << "Username variable can not be resolved.";
79 position = result.find(kMachineNamePolicyVarName);
80 if (position != std::string::npos) {
81 SCDynamicStoreContext context = { 0, NULL, NULL, NULL };
82 SCDynamicStoreRef store = SCDynamicStoreCreate(kCFAllocatorDefault,
83 CFSTR("policy_subsystem"),
85 CFStringRef machinename = SCDynamicStoreCopyLocalHostName(store);
87 result.replace(position, strlen(kMachineNamePolicyVarName),
88 base::SysCFStringRefToUTF8(machinename));
89 CFRelease(machinename);
91 int error = SCError();
92 LOG(ERROR) << "Machine name variable can not be resolved. Error: "
93 << error << " - " << SCErrorString(error);
100 void CheckUserDataDirPolicy(base::FilePath* user_data_dir) {
101 base::mac::ScopedNSAutoreleasePool pool;
103 // Since the configuration management infrastructure is not initialized when
104 // this code runs, read the policy preference directly.
105 NSString* key = base::SysUTF8ToNSString(policy::key::kUserDataDir);
106 NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
107 NSString* value = [defaults stringForKey:key];
108 if (value && [defaults objectIsForcedForKey:key]) {
109 std::string string_value = base::SysNSStringToUTF8(value);
110 // Now replace any vars the user might have used.
111 string_value = policy::path_parser::ExpandPathVariables(string_value);
112 *user_data_dir = base::FilePath(string_value);
116 } // namespace path_parser
118 } // namespace policy