Apache Mesos
su.hpp
Go to the documentation of this file.
1 // Licensed to the Apache Software Foundation (ASF) under one
2 // or more contributor license agreements. See the NOTICE file
3 // distributed with this work for additional information
4 // regarding copyright ownership. The ASF licenses this file
5 // to you under the Apache License, Version 2.0 (the
6 // "License"); you may not use this file except in compliance
7 // with the License. You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 
17 #ifndef __STOUT_OS_WINDOWS_SU_HPP__
18 #define __STOUT_OS_WINDOWS_SU_HPP__
19 
20 #include <string>
21 #include <vector>
22 
23 #include <stout/error.hpp>
24 #include <stout/nothing.hpp>
25 #include <stout/result.hpp>
26 #include <stout/try.hpp>
27 
28 #include <stout/windows.hpp>
29 
30 // Include for `GetUserNameEx`. `SECURITY_WIN32` or `SECURITY_KERNEL` must be
31 // defined to include `SecExt.h`, which defines `GetUserNameEx` (the choice
32 // depends on whether you want the functions defined to be usermode or kernel
33 // operations). We include `security.h` instead of `SecExt.h` because comments
34 // in this header indicate that it should only be included from `security.h`.
35 // Finally, we `#undef` to avoid accidentally interfering with Windows headers
36 // that might be sensitive to `SECURITY_WIN32`.
37 #if defined(SECURITY_WIN32) || defined(SECURITY_KERNEL)
38 #include <security.h>
39 #else
40 #define SECURITY_WIN32
41 #include <security.h>
42 #undef SECURITY_WIN32
43 #endif // SECURITY_WIN32 || SECURITY_KERNEL
44 
45 
46 namespace os {
47 
48 // NOTE: We delete these functions because they are not meaningful on Windows.
49 // `su` and `user` are the most important of these functions. The POSIX code
50 // uses them prodigiously, but in Windows we have been able to divest ourselves
51 // of all uses.
52 //
53 // `su` is important to the launcher API; if the `user` flag (not to be
54 // confused with the `user` function, which we delete below) is present, we
55 // will `su` to that user before launching the command. On Windows we avoid
56 // this problem by simply conditionally compiling out the `user` flag
57 // altogether, which means that we never have to call `su`.
58 //
59 // The `user` function itself is already mostly conditionally compiled out of
60 // every platform except linux. So in this case it is simply safe to return an
61 // error on Windows.
62 
63 
64 inline Result<uid_t> getuid(const Option<std::string>& user = None()) = delete;
65 
66 
67 inline Result<gid_t> getgid(const Option<std::string>& user = None()) = delete;
68 
69 
70 // Returns the SAM account name for the current user. This username is
71 // unprocessed, meaning it contains punctuation, possibly including '\'.
72 // NOTE: The `uid` parameter is unsupported on Windows, and will result in an
73 // error.
75 {
76  if (uid.isSome()) {
77  return Error(
78  "os::user: Retrieving user information via uid "
79  "is not supported on Windows");
80  }
81 
82  EXTENDED_NAME_FORMAT username_format = NameSamCompatible;
83  ULONG buffer_size = 0;
84  if (::GetUserNameExW(username_format, nullptr, &buffer_size) == FALSE) {
85  if (::GetLastError() != ERROR_MORE_DATA) {
86  return WindowsError("os::user: Failed to get buffer size for username");
87  }
88  }
89 
90  std::vector<wchar_t> user_name;
91  user_name.reserve(buffer_size);
92  if (::GetUserNameExW(username_format, user_name.data(), &buffer_size)
93  == FALSE) {
94  return WindowsError("os::user: Failed to get username from OS");
95  }
96 
97  return stringify(std::wstring(user_name.data()));
98 }
99 
100 
101 inline Try<Nothing> su(const std::string& user) = delete;
102 
103 } // namespace os {
104 
105 #endif // __STOUT_OS_WINDOWS_SU_HPP__
Try< uid_t > uid(const std::string &path, const FollowSymlink follow=FollowSymlink::FOLLOW_SYMLINK)
Definition: stat.hpp:224
Definition: errorbase.hpp:36
Definition: check.hpp:33
Result< std::string > user(Option< uid_t > uid=None())
Definition: su.hpp:284
Definition: error.hpp:108
Definition: posix_signalhandler.hpp:23
Definition: check.hpp:30
Result< uid_t > getuid(const Option< std::string > &user=None())
Definition: su.hpp:41
Result< gid_t > getgid(const Option< std::string > &user=None())
Definition: su.hpp:118
Try< Nothing > su(const std::string &user)
Definition: su.hpp:326
Definition: none.hpp:27
std::string stringify(int flags)