1*32b1fd08SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*32b1fd08SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*32b1fd08SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*32b1fd08SAndrew Rist  * distributed with this work for additional information
6*32b1fd08SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*32b1fd08SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*32b1fd08SAndrew Rist  * "License"); you may not use this file except in compliance
9*32b1fd08SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*32b1fd08SAndrew Rist  *
11*32b1fd08SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*32b1fd08SAndrew Rist  *
13*32b1fd08SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*32b1fd08SAndrew Rist  * software distributed under the License is distributed on an
15*32b1fd08SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*32b1fd08SAndrew Rist  * KIND, either express or implied.  See the License for the
17*32b1fd08SAndrew Rist  * specific language governing permissions and limitations
18*32b1fd08SAndrew Rist  * under the License.
19*32b1fd08SAndrew Rist  *
20*32b1fd08SAndrew Rist  *************************************************************/
21*32b1fd08SAndrew Rist 
22*32b1fd08SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir #undef UNICODE
25cdf0e10cSrcweir #undef _UNICODE
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #define _WIN32_WINDOWS 0x0410
28cdf0e10cSrcweir 
29cdf0e10cSrcweir #ifdef _MSC_VER
30cdf0e10cSrcweir #pragma warning(push, 1) /* disable warnings within system headers */
31cdf0e10cSrcweir #endif
32cdf0e10cSrcweir #define WIN32_LEAN_AND_MEAN
33cdf0e10cSrcweir #include <windows.h>
34cdf0e10cSrcweir #include <msiquery.h>
35cdf0e10cSrcweir #ifdef _MSC_VER
36cdf0e10cSrcweir #pragma warning(pop)
37cdf0e10cSrcweir #endif
38cdf0e10cSrcweir 
39cdf0e10cSrcweir #include <malloc.h>
40cdf0e10cSrcweir #include <assert.h>
41cdf0e10cSrcweir 
42cdf0e10cSrcweir #include <tchar.h>
43cdf0e10cSrcweir #include <string>
44cdf0e10cSrcweir #include <systools/win32/uwinapi.h>
45cdf0e10cSrcweir 
46cdf0e10cSrcweir #include <../tools/seterror.hxx>
47cdf0e10cSrcweir 
48cdf0e10cSrcweir using namespace std;
49cdf0e10cSrcweir 
50cdf0e10cSrcweir namespace
51cdf0e10cSrcweir {
GetMsiProperty(MSIHANDLE handle,const string & sProperty)52cdf0e10cSrcweir     string GetMsiProperty(MSIHANDLE handle, const string& sProperty)
53cdf0e10cSrcweir     {
54cdf0e10cSrcweir         string  result;
55cdf0e10cSrcweir         TCHAR   szDummy[1] = TEXT("");
56cdf0e10cSrcweir         DWORD   nChars = 0;
57cdf0e10cSrcweir 
58cdf0e10cSrcweir         if (MsiGetProperty(handle, sProperty.c_str(), szDummy, &nChars) == ERROR_MORE_DATA)
59cdf0e10cSrcweir         {
60cdf0e10cSrcweir             DWORD nBytes = ++nChars * sizeof(TCHAR);
61cdf0e10cSrcweir             LPTSTR buffer = reinterpret_cast<LPTSTR>(_alloca(nBytes));
62cdf0e10cSrcweir             ZeroMemory( buffer, nBytes );
63cdf0e10cSrcweir             MsiGetProperty(handle, sProperty.c_str(), buffer, &nChars);
64cdf0e10cSrcweir             result = buffer;
65cdf0e10cSrcweir         }
66cdf0e10cSrcweir         return result;
67cdf0e10cSrcweir     }
68cdf0e10cSrcweir 
IsSetMsiProperty(MSIHANDLE handle,const string & sProperty)69cdf0e10cSrcweir     inline bool IsSetMsiProperty(MSIHANDLE handle, const string& sProperty)
70cdf0e10cSrcweir     {
71cdf0e10cSrcweir         return (GetMsiProperty(handle, sProperty).length() > 0);
72cdf0e10cSrcweir     }
73cdf0e10cSrcweir 
UnsetMsiProperty(MSIHANDLE handle,const string & sProperty)74cdf0e10cSrcweir     inline void UnsetMsiProperty(MSIHANDLE handle, const string& sProperty)
75cdf0e10cSrcweir     {
76cdf0e10cSrcweir         MsiSetProperty(handle, sProperty.c_str(), NULL);
77cdf0e10cSrcweir     }
78cdf0e10cSrcweir 
SetMsiProperty(MSIHANDLE handle,const string & sProperty,const string &)79cdf0e10cSrcweir     inline void SetMsiProperty(MSIHANDLE handle, const string& sProperty, const string&)
80cdf0e10cSrcweir     {
81cdf0e10cSrcweir         MsiSetProperty(handle, sProperty.c_str(), TEXT("1"));
82cdf0e10cSrcweir     }
83cdf0e10cSrcweir } // namespace
84cdf0e10cSrcweir 
GetUserInstallMode(MSIHANDLE handle)85cdf0e10cSrcweir extern "C" UINT __stdcall GetUserInstallMode(MSIHANDLE handle)
86cdf0e10cSrcweir {
87cdf0e10cSrcweir     string sOfficeInstallPath = GetMsiProperty(handle, TEXT("INSTALLLOCATION"));
88cdf0e10cSrcweir 
89cdf0e10cSrcweir     // MessageBox(NULL, sOfficeInstallPath.c_str(), "DEBUG", MB_OK);
90cdf0e10cSrcweir 
91cdf0e10cSrcweir     // unsetting all properties
92cdf0e10cSrcweir 
93cdf0e10cSrcweir     UnsetMsiProperty( handle, TEXT("INVALIDDIRECTORY") );
94cdf0e10cSrcweir     UnsetMsiProperty( handle, TEXT("ISWRONGPRODUCT") );
95cdf0e10cSrcweir     UnsetMsiProperty( handle, TEXT("PATCHISOLDER") );
96cdf0e10cSrcweir     UnsetMsiProperty( handle, TEXT("ALLUSERS") );
97cdf0e10cSrcweir 
98cdf0e10cSrcweir     // 1. Searching for "ProductCode" in setup.ini
99cdf0e10cSrcweir 
100cdf0e10cSrcweir     string sSetupiniPath = sOfficeInstallPath + TEXT("program\\setup.ini");
101cdf0e10cSrcweir 
102cdf0e10cSrcweir     TCHAR szValue[32767];
103cdf0e10cSrcweir 
104cdf0e10cSrcweir     GetPrivateProfileString(
105cdf0e10cSrcweir         TEXT("Bootstrap"),
106cdf0e10cSrcweir         TEXT("ProductCode"),
107cdf0e10cSrcweir         TEXT("INVALIDDIRECTORY"),
108cdf0e10cSrcweir         szValue,
109cdf0e10cSrcweir         elementsof(szValue),
110cdf0e10cSrcweir         sSetupiniPath.c_str()
111cdf0e10cSrcweir         );
112cdf0e10cSrcweir 
113cdf0e10cSrcweir     if ( !_tcsicmp( szValue, TEXT("INVALIDDIRECTORY") ) )
114cdf0e10cSrcweir     {
115cdf0e10cSrcweir     	// No setup.ini or no "ProductCode" in setup.ini. This is an invalid directory.
116cdf0e10cSrcweir         SetMsiProperty( handle, TEXT("INVALIDDIRECTORY"), TEXT("YES") );
117cdf0e10cSrcweir         // MessageBox(NULL, "INVALIDDIRECTORY set, no setup.ini or ProductCode in setup.ini.", "DEBUG", MB_OK);
118cdf0e10cSrcweir         SetMsiErrorCode( MSI_ERROR_INVALIDDIRECTORY );
119cdf0e10cSrcweir         return ERROR_SUCCESS;
120cdf0e10cSrcweir     }
121cdf0e10cSrcweir 
122cdf0e10cSrcweir     // 2. Comparing first three characters of "PRODUCTMAJOR" from property table and "buildid" from InfoFile
123cdf0e10cSrcweir 
124cdf0e10cSrcweir     szValue[0] = '\0';
125cdf0e10cSrcweir 
126cdf0e10cSrcweir     GetPrivateProfileString(
127cdf0e10cSrcweir         TEXT("Bootstrap"),
128cdf0e10cSrcweir         TEXT("buildid"),
129cdf0e10cSrcweir         TEXT("ISWRONGPRODUCT"),
130cdf0e10cSrcweir         szValue,
131cdf0e10cSrcweir         elementsof(szValue),
132cdf0e10cSrcweir         sSetupiniPath.c_str()
133cdf0e10cSrcweir         );
134cdf0e10cSrcweir 
135cdf0e10cSrcweir     if ( !_tcsicmp( szValue, TEXT("ISWRONGPRODUCT") ) )
136cdf0e10cSrcweir     {
137cdf0e10cSrcweir         SetMsiProperty( handle, TEXT("ISWRONGPRODUCT"), TEXT("YES") );
138cdf0e10cSrcweir         // MessageBox(NULL, "ISWRONGPRODUCT 1 set after searching buildid", "DEBUG", MB_OK);
139cdf0e10cSrcweir         SetMsiErrorCode( MSI_ERROR_ISWRONGPRODUCT );
140cdf0e10cSrcweir         return ERROR_SUCCESS;
141cdf0e10cSrcweir     }
142cdf0e10cSrcweir 
143cdf0e10cSrcweir     string ProductMajor = GetMsiProperty(handle, TEXT("PRODUCTMAJOR"));
144cdf0e10cSrcweir 
145cdf0e10cSrcweir     // Comparing the first three characters, for example "680"
146cdf0e10cSrcweir     // If not equal, this version is not suited for patch or language pack
147cdf0e10cSrcweir 
148cdf0e10cSrcweir     if (_tcsnicmp(ProductMajor.c_str(), szValue, 3))
149cdf0e10cSrcweir     {
150cdf0e10cSrcweir         SetMsiProperty( handle, TEXT("ISWRONGPRODUCT"), TEXT("YES") );
151cdf0e10cSrcweir         // MessageBox(NULL, "ISWRONGPRODUCT 2 set after searching PRODUCTMAJOR", "DEBUG", MB_OK);
152cdf0e10cSrcweir         SetMsiErrorCode( MSI_ERROR_ISWRONGPRODUCT );
153cdf0e10cSrcweir         return ERROR_SUCCESS;
154cdf0e10cSrcweir     }
155cdf0e10cSrcweir 
156cdf0e10cSrcweir     // 3. Only for patch: Comparing "PRODUCTMINOR from property table and "ProductMinor" from InfoFile
157cdf0e10cSrcweir 
158cdf0e10cSrcweir     string isPatch = GetMsiProperty(handle, TEXT("ISPATCH"));
159cdf0e10cSrcweir 
160cdf0e10cSrcweir     if (isPatch=="1")
161cdf0e10cSrcweir     {
162cdf0e10cSrcweir         string ProductMinor = GetMsiProperty(handle, TEXT("PRODUCTBUILDID"));
163cdf0e10cSrcweir         int PatchProductMinor = atoi(ProductMinor.c_str());
164cdf0e10cSrcweir 
165cdf0e10cSrcweir         szValue[0] = '\0';
166cdf0e10cSrcweir 
167cdf0e10cSrcweir         GetPrivateProfileString(
168cdf0e10cSrcweir             TEXT("Bootstrap"),
169cdf0e10cSrcweir             TEXT("ProductBuildid"),
170cdf0e10cSrcweir             TEXT("8918"),
171cdf0e10cSrcweir             szValue,
172cdf0e10cSrcweir             elementsof(szValue),
173cdf0e10cSrcweir             sSetupiniPath.c_str()
174cdf0e10cSrcweir             );
175cdf0e10cSrcweir 
176cdf0e10cSrcweir         int InstalledProductMinor = atoi(szValue);
177cdf0e10cSrcweir 
178cdf0e10cSrcweir         if ( InstalledProductMinor >= PatchProductMinor )
179cdf0e10cSrcweir         {
180cdf0e10cSrcweir             SetMsiProperty( handle, TEXT("PATCHISOLDER"), TEXT("YES") );
181cdf0e10cSrcweir             // MessageBox(NULL, "PATCHISOLDER set", "DEBUG", MB_OK);
182cdf0e10cSrcweir             SetMsiErrorCode( MSI_ERROR_PATCHISOLDER );
183cdf0e10cSrcweir             return ERROR_SUCCESS;
184cdf0e10cSrcweir         }
185cdf0e10cSrcweir     }
186cdf0e10cSrcweir 
187cdf0e10cSrcweir     // 4. Setting property ALLUSERS with value from "setup.ini"
188cdf0e10cSrcweir 
189cdf0e10cSrcweir     szValue[0] = '\0';
190cdf0e10cSrcweir 
191cdf0e10cSrcweir     GetPrivateProfileString(
192cdf0e10cSrcweir         TEXT("Bootstrap"),
193cdf0e10cSrcweir         TEXT("ALLUSERS"),
194cdf0e10cSrcweir         TEXT(""),
195cdf0e10cSrcweir         szValue,
196cdf0e10cSrcweir         elementsof(szValue),
197cdf0e10cSrcweir         sSetupiniPath.c_str()
198cdf0e10cSrcweir         );
199cdf0e10cSrcweir 
200cdf0e10cSrcweir     if ( szValue[0] )
201cdf0e10cSrcweir     {
202cdf0e10cSrcweir         SetMsiProperty( handle, TEXT("ALLUSERS"), szValue );
203cdf0e10cSrcweir         // MessageBox(NULL, "ALLUSERS set", "DEBUG", MB_OK);
204cdf0e10cSrcweir     }
205cdf0e10cSrcweir 
206cdf0e10cSrcweir     return ERROR_SUCCESS;
207cdf0e10cSrcweir }
208