• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
5 #include <pwd.h>
6 
7 #include "chrome/browser/policy/policy_path_parser.h"
8 
9 #include "base/logging.h"
10 
11 namespace policy {
12 
13 namespace path_parser {
14 
15 const char* kMachineNamePolicyVarName = "${machine_name}";
16 const char* kUserNamePolicyVarName = "${user_name}";
17 
18 // Replaces all variable occurrences in the policy string with the respective
19 // system settings values.
ExpandPathVariables(const FilePath::StringType & untranslated_string)20 FilePath::StringType ExpandPathVariables(
21     const FilePath::StringType& untranslated_string) {
22   FilePath::StringType result(untranslated_string);
23   // Translate two special variables ${user_name} and ${machine_name}
24   size_t position = result.find(kUserNamePolicyVarName);
25   if (position != std::string::npos) {
26     struct passwd* user = getpwuid(geteuid());
27     if (user) {
28       result.replace(position, strlen(kUserNamePolicyVarName), user->pw_name);
29     } else {
30       LOG(ERROR) << "Username variable can not be resolved. ";
31     }
32   }
33   position = result.find(kMachineNamePolicyVarName);
34   if (position != std::string::npos) {
35     char machinename[255];
36     if (gethostname(machinename, 255) == 0) {
37       result.replace(position, strlen(kMachineNamePolicyVarName), machinename);
38     } else {
39       LOG(ERROR) << "Machine name variable can not be resolved.";
40     }
41   }
42   return result;
43 }
44 
45 }  // namespace path_parser
46 
47 }  // namespace policy
48