OLD | NEW |
| (Empty) |
1 // Copyright 2008-2009 Google Inc. | |
2 // | |
3 // Licensed under the Apache License, Version 2.0 (the "License"); | |
4 // you may not use this file except in compliance with the License. | |
5 // You may obtain a copy of the License at | |
6 // | |
7 // http://www.apache.org/licenses/LICENSE-2.0 | |
8 // | |
9 // Unless required by applicable law or agreed to in writing, software | |
10 // distributed under the License is distributed on an "AS IS" BASIS, | |
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
12 // See the License for the specific language governing permissions and | |
13 // limitations under the License. | |
14 // ======================================================================== | |
15 | |
16 #include "omaha/core/google_update_core.h" | |
17 #include "omaha/base/debug.h" | |
18 #include "omaha/base/error.h" | |
19 #include "omaha/base/exception_barrier.h" | |
20 #include "omaha/base/logging.h" | |
21 #include "omaha/base/reg_key.h" | |
22 #include "omaha/base/scope_guard.h" | |
23 #include "omaha/base/scoped_any.h" | |
24 #include "omaha/base/scoped_ptr_address.h" | |
25 #include "omaha/base/system.h" | |
26 #include "omaha/base/utils.h" | |
27 #include "omaha/common/config_manager.h" | |
28 #include "omaha/goopdate/app_command.h" | |
29 | |
30 namespace omaha { | |
31 | |
32 GoogleUpdateCoreBase::GoogleUpdateCoreBase() : StdMarshalInfo(true) { | |
33 CORE_LOG(L3, (_T("[GoogleUpdateCoreBase::GoogleUpdateCoreBase]"))); | |
34 } | |
35 | |
36 GoogleUpdateCoreBase::~GoogleUpdateCoreBase() { | |
37 CORE_LOG(L3, (_T("[GoogleUpdateCoreBase::~GoogleUpdateCoreBase]"))); | |
38 } | |
39 | |
40 STDMETHODIMP GoogleUpdateCoreBase::LaunchCmdElevated(const WCHAR* app_guid, | |
41 const WCHAR* cmd_id, | |
42 DWORD caller_proc_id, | |
43 ULONG_PTR* proc_handle) { | |
44 CORE_LOG(L3, (_T("[GoogleUpdateCoreBase::LaunchCmdElevated]") | |
45 _T("[app %s][cmd %s][pid %d]"), | |
46 app_guid, cmd_id, caller_proc_id)); | |
47 | |
48 ExceptionBarrier barrier; | |
49 | |
50 ASSERT1(app_guid); | |
51 ASSERT1(cmd_id); | |
52 ASSERT1(proc_handle); | |
53 | |
54 if (!(IsGuid(app_guid) && cmd_id && _tcslen(cmd_id) && proc_handle)) { | |
55 return E_INVALIDARG; | |
56 } | |
57 | |
58 scoped_process caller_proc_handle; | |
59 HRESULT hr = OpenCallerProcessHandle(caller_proc_id, | |
60 address(caller_proc_handle)); | |
61 if (FAILED(hr)) { | |
62 CORE_LOG(LE, (_T("[failed to open caller's handle][0x%x]"), hr)); | |
63 return hr; | |
64 } | |
65 | |
66 // Allocate a session ID for the ping that this call will generate. (I'd | |
67 // really like to be able to pipe an external session ID through this API, | |
68 // but this is old and I don't feel comfortable changing the signature on it.) | |
69 CString session_id; | |
70 GetGuid(&session_id); | |
71 | |
72 scoped_ptr<AppCommand> app_command; | |
73 // true == machine level | |
74 hr = AppCommand::Load(app_guid, | |
75 true, | |
76 cmd_id, | |
77 session_id, | |
78 address(app_command)); | |
79 if (FAILED(hr)) { | |
80 CORE_LOG(LE, (_T("[failed to load command configuration][0x%x]"), hr)); | |
81 return hr; | |
82 } | |
83 | |
84 // This is a pseudo handle that must not be closed. | |
85 HANDLE this_process_handle = ::GetCurrentProcess(); | |
86 | |
87 scoped_process command_process; | |
88 scoped_process duplicate_proc_handle; | |
89 | |
90 hr = app_command->Execute(address(command_process)); | |
91 if (FAILED(hr)) { | |
92 CORE_LOG(LE, (_T("[failed to launch app command][0x%x]"), hr)); | |
93 return hr; | |
94 } | |
95 | |
96 DWORD desired_access = PROCESS_QUERY_INFORMATION | SYNCHRONIZE; | |
97 bool res = ::DuplicateHandle( | |
98 this_process_handle, // Current process. | |
99 get(command_process), // Process handle to duplicate. | |
100 get(caller_proc_handle), // Process receiving the handle. | |
101 address(duplicate_proc_handle), // Duplicated handle. | |
102 desired_access, // Access requested for the new handle. | |
103 false, // Don't inherit the new handle. | |
104 0) != 0; // Flags. | |
105 | |
106 if (!res) { | |
107 hr = HRESULTFromLastError(); | |
108 CORE_LOG(LE, (_T("[failed to duplicate the handle][0x%08x]"), hr)); | |
109 return hr; | |
110 } | |
111 | |
112 // Transfer the ownership of the new handle to the caller. The caller must | |
113 // close this handle. | |
114 *proc_handle = reinterpret_cast<ULONG_PTR>(release(duplicate_proc_handle)); | |
115 | |
116 return S_OK; | |
117 } | |
118 | |
119 HRESULT GoogleUpdateCoreBase::OpenCallerProcessHandle(DWORD proc_id, | |
120 HANDLE* proc_handle) { | |
121 ASSERT1(proc_handle); | |
122 *proc_handle = NULL; | |
123 | |
124 HRESULT hr = ::CoImpersonateClient(); | |
125 if (FAILED(hr)) { | |
126 return hr; | |
127 } | |
128 ON_SCOPE_EXIT(::CoRevertToSelf); | |
129 | |
130 *proc_handle = ::OpenProcess(PROCESS_DUP_HANDLE, false, proc_id); | |
131 return *proc_handle ? S_OK : HRESULTFromLastError(); | |
132 } | |
133 | |
134 } // namespace omaha | |
135 | |
OLD | NEW |