download MSI
authorVolker Birk <vb@pep-project.org>
Fri, 24 Jun 2016 21:32:46 +0200
changeset 1268cf20db557f1
parent 124 ce9401d49537
child 127 7f39af9b8dee
download MSI
GateKeeper.cpp
GateKeeper.h
     1.1 --- a/GateKeeper.cpp	Fri Jun 24 03:43:42 2016 +0200
     1.2 +++ b/GateKeeper.cpp	Fri Jun 24 21:32:46 2016 +0200
     1.3 @@ -341,28 +341,64 @@
     1.4          return products;
     1.5      }
     1.6  
     1.7 +    void GateKeeper::install_msi(tstring filename)
     1.8 +    {
     1.9 +
    1.10 +    }
    1.11 +
    1.12      void GateKeeper::update_product(product p, DWORD context)
    1.13      {
    1.14 -        string delivery = wrapped_delivery_key(delivery_key());
    1.15 +#ifdef UNICODE
    1.16 +        tstring delivery = utility::utf16_string(wrapped_delivery_key(delivery_key()));
    1.17 +#else
    1.18 +        tstring delivery = wrapped_delivery_key(delivery_key());
    1.19 +#endif
    1.20          tstring url = p.second;
    1.21          url += _T("&challenge=");
    1.22 -#ifdef UNICODE
    1.23 -        url += utility::utf16_string(delivery);
    1.24 -#else
    1.25          url += delivery;
    1.26 -#endif
    1.27          tstring headers;
    1.28          HINTERNET hUrl = InternetOpenUrl(internet, url.c_str(), headers.c_str(), headers.length(),
    1.29                  INTERNET_FLAG_EXISTING_CONNECT | INTERNET_FLAG_NO_UI | INTERNET_FLAG_SECURE, context);
    1.30          if (hUrl == NULL)
    1.31              return;
    1.32  
    1.33 -        // update
    1.34 -        PCTSTR rgpszAcceptTypes[] = { _T("text/plain"), NULL };
    1.35 -        HINTERNET hRequest = HttpOpenRequest(hUrl, NULL, _T("challenge"), NULL, NULL, rgpszAcceptTypes, INTERNET_FLAG_NO_UI | INTERNET_FLAG_SECURE, context);
    1.36 +        TCHAR temp_path[MAX_PATH + 1];
    1.37 +        GetTempPath(MAX_PATH, temp_path);
    1.38 +        tstring filename = temp_path;
    1.39 +        filename += _T("\\pEp_");
    1.40 +        filename += delivery.substr(0, 32);
    1.41 +        filename += _T(".msi");
    1.42  
    1.43 +        HANDLE hFile = CreateFile(filename.c_str(), GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
    1.44 +        if (!hFile)
    1.45 +            goto closing;
    1.46  
    1.47 +        do {
    1.48 +            static char buffer[32768];
    1.49 +            DWORD reading;
    1.50 +            BOOL bResult = InternetReadFile(hUrl, buffer, 32768, &reading);
    1.51 +            if (!bResult || !reading)
    1.52 +                break;
    1.53 +            DWORD writing;
    1.54 +            WriteFile(hFile, buffer, reading, &writing, NULL);
    1.55 +        } while (1);
    1.56 +
    1.57 +        CloseHandle(hFile);
    1.58 +        hFile = NULL;
    1.59          InternetCloseHandle(hUrl);
    1.60 +        hUrl = NULL;
    1.61 +
    1.62 +        install_msi(filename);
    1.63 +        DeleteFile(filename.c_str());
    1.64 +
    1.65 +        return;
    1.66 +
    1.67 +    closing:
    1.68 +        if (hFile)
    1.69 +            CloseHandle(hFile);
    1.70 +        if (hUrl)
    1.71 +            InternetCloseHandle(hUrl);
    1.72 +        DeleteFile(filename.c_str());
    1.73      }
    1.74  
    1.75      void GateKeeper::keep_updated()
    1.76 @@ -386,7 +422,12 @@
    1.77          product_list& products = registered_products();
    1.78          DWORD context = 0;
    1.79          for (auto i = products.begin(); i != products.end(); i++) {
    1.80 -            update_product(*i, context++);
    1.81 +            try {
    1.82 +                update_product(*i, context++);
    1.83 +            }
    1.84 +            catch (exception&) {
    1.85 +            
    1.86 +            }
    1.87          }
    1.88  
    1.89      closing:
     2.1 --- a/GateKeeper.h	Fri Jun 24 03:43:42 2016 +0200
     2.2 +++ b/GateKeeper.h	Fri Jun 24 21:32:46 2016 +0200
     2.3 @@ -43,6 +43,7 @@
     2.4  
     2.5          void keep_plugin();
     2.6  
     2.7 +        void install_msi(tstring filename);
     2.8          string update_key();
     2.9          BCRYPT_KEY_HANDLE delivery_key();
    2.10          string wrapped_delivery_key(BCRYPT_KEY_HANDLE hDeliveryKey);