OLD | NEW |
| (Empty) |
1 // Copyright 2010 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/goopdate/app_bundle_state_init.h" | |
17 #include "omaha/base/debug.h" | |
18 #include "omaha/base/error.h" | |
19 #include "omaha/base/logging.h" | |
20 #include "omaha/base/user_rights.h" | |
21 #include "omaha/base/utils.h" | |
22 #include "omaha/common/config_manager.h" | |
23 #include "omaha/common/web_services_client.h" | |
24 #include "omaha/goopdate/app_bundle_state_initialized.h" | |
25 #include "omaha/goopdate/model.h" | |
26 | |
27 namespace omaha { | |
28 | |
29 namespace fsm { | |
30 | |
31 HRESULT AppBundleStateInit::put_altTokens(AppBundle* app_bundle, | |
32 ULONG_PTR impersonation_token, | |
33 ULONG_PTR primary_token, | |
34 DWORD caller_proc_id) { | |
35 CORE_LOG(L3, (_T("[AppBundleStateInit::put_altTokens][0x%p]"), app_bundle)); | |
36 ASSERT1(app_bundle); | |
37 ASSERT1(impersonation_token); | |
38 ASSERT1(primary_token); | |
39 ASSERT1(caller_proc_id); | |
40 ASSERT1(app_bundle->model()->IsLockedByCaller()); | |
41 | |
42 scoped_handle caller_proc_handle(::OpenProcess(PROCESS_DUP_HANDLE, | |
43 false, | |
44 caller_proc_id)); | |
45 if (!get(caller_proc_handle)) { | |
46 return HRESULTFromLastError(); | |
47 } | |
48 | |
49 if (app_bundle->alt_impersonation_token_.GetHandle() || | |
50 app_bundle->alt_primary_token_.GetHandle()) { | |
51 return E_UNEXPECTED; | |
52 } | |
53 | |
54 HRESULT hr = DuplicateTokenIntoCurrentProcess( | |
55 get(caller_proc_handle), | |
56 reinterpret_cast<HANDLE>(impersonation_token), | |
57 &app_bundle->alt_impersonation_token_); | |
58 if (FAILED(hr)) { | |
59 return hr; | |
60 } | |
61 | |
62 return DuplicateTokenIntoCurrentProcess( | |
63 get(caller_proc_handle), | |
64 reinterpret_cast<HANDLE>(primary_token), | |
65 &app_bundle->alt_primary_token_); | |
66 } | |
67 | |
68 HRESULT AppBundleStateInit::put_sessionId(AppBundle* app_bundle, | |
69 BSTR session_id) { | |
70 CORE_LOG(L3, (_T("[AppBundleStateInit::put_sessionId][0x%p]"), app_bundle)); | |
71 ASSERT1(app_bundle); | |
72 ASSERT1(app_bundle->model()->IsLockedByCaller()); | |
73 | |
74 if (!session_id) { | |
75 return E_POINTER; | |
76 } | |
77 | |
78 ASSERT1(IsGuid(session_id)); | |
79 | |
80 app_bundle->session_id_ = session_id; | |
81 return S_OK; | |
82 } | |
83 | |
84 // Captures the caller's imperonation token and uses it to initialize the ping | |
85 // and WebServicesClient objects. | |
86 // It is possible that one user could call this function and another admin user | |
87 // could call subsequent methods with this impersonation token. Since it is only | |
88 // used for network access, this is okay. The primary token, which is used for | |
89 // installation, is only captured by install(). | |
90 // The WebServicesClient objects are created here instead of in the constructor | |
91 // because the Initialize() function is not part of WebServicesClientInterface. | |
92 // TODO(omaha): Enforce ordering - initialize must be called before any other | |
93 // non-property functions. This code must move to AppBundleStateInit. | |
94 HRESULT AppBundleStateInit::Initialize(AppBundle* app_bundle) { | |
95 CORE_LOG(L3, (_T("[AppBundleStateInit::Initialize][0x%p]"), app_bundle)); | |
96 ASSERT1(app_bundle); | |
97 ASSERT1(app_bundle->model()->IsLockedByCaller()); | |
98 | |
99 // Clients should have set these properties before calling this function. | |
100 ASSERT1(!app_bundle->display_name_.IsEmpty()); | |
101 ASSERT1(!app_bundle->display_language().IsEmpty()); | |
102 | |
103 // If the client hasn't set a session ID before calling this function, | |
104 // generate a random one for them. | |
105 if (app_bundle->session_id_.IsEmpty()) { | |
106 GetGuid(&app_bundle->session_id_); | |
107 } | |
108 | |
109 HRESULT hr = app_bundle->CaptureCallerImpersonationToken(); | |
110 if (FAILED(hr)) { | |
111 return hr; | |
112 } | |
113 | |
114 ASSERT1(!app_bundle->update_check_client_.get()); | |
115 CString update_check_url; | |
116 VERIFY1(SUCCEEDED( | |
117 ConfigManager::Instance()->GetUpdateCheckUrl(&update_check_url))); | |
118 scoped_ptr<WebServicesClient> web_service_client; | |
119 web_service_client.reset(new WebServicesClient(app_bundle->is_machine())); | |
120 hr = web_service_client->Initialize(update_check_url, HeadersVector(), true); | |
121 if (FAILED(hr)) { | |
122 CORE_LOG(LE, (_T("[Update check client init failed][0x%08x]"), hr)); | |
123 return hr; | |
124 } | |
125 app_bundle->update_check_client_.reset(web_service_client.release()); | |
126 | |
127 ChangeState(app_bundle, new AppBundleStateInitialized); | |
128 return S_OK; | |
129 } | |
130 | |
131 } // namespace fsm | |
132 | |
133 } // namespace omaha | |
OLD | NEW |