Ticket #10845: 0001-Put-the-Tor-subdirectory-at-the-head-of-PATH-for-DLL.patch

File 0001-Put-the-Tor-subdirectory-at-the-head-of-PATH-for-DLL.patch, 4.8 KB (added by dcf, 5 years ago)
  • RelativeLink/RelativeLink.c

    From 029bc9d831f0187c14371b806e45e721ee91e172 Mon Sep 17 00:00:00 2001
    From: David Fifield <david@bamsoftware.com>
    Date: Thu, 20 Feb 2014 19:15:17 +0000
    Subject: [PATCH] Put the "Tor" subdirectory at the head of PATH for DLL
     access.
    
    Pluggable transport executables are in their own subdirectory,
    PluggableTransports, but they need to have access to the OpenSSL DLLs.
    Adding the directory containing the DLLs to PATH makes them visible. I
    put the directory at the front of PATH and not the end, just in case
    there is a libeay32.dll or ssleay32.dll somewhere else on the system.
    
    https://trac.torproject.org/projects/tor/ticket/10845
    ---
     RelativeLink/RelativeLink.c |  121 +++++++++++++++++++++++++++++++++++++++++--
     1 file changed, 118 insertions(+), 3 deletions(-)
    
    diff --git a/RelativeLink/RelativeLink.c b/RelativeLink/RelativeLink.c
    index bae4647..9cd4c64 100755
    a b  
    11#include <windows.h>
     2#include <stdio.h>
     3#include <tchar.h>
    24
    35//
    46// RelativeLink.c
     
    2224// Put it in the proper place.
    2325//
    2426
    25 int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
     27// Get the absolute path to a subdirectory of the current directory; i.e.
     28// "C:\cwd\reldir". Returns a malloc-allocated string or NULL on error.
     29LPTSTR GetSubdirectory(LPCTSTR reldir)
     30{
     31    DWORD n, len;
     32    LPTSTR cwd, subdir;
     33
     34    cwd = NULL;
     35    subdir = NULL;
     36
     37    // The first call to GetCurrentDirectory gets the buffer size; the second
     38    // fills the buffer.
     39    n = GetCurrentDirectory(0, NULL);
     40    if (n == 0)
     41        goto bail;
     42    cwd = (LPTSTR) malloc(n * sizeof(TCHAR));
     43    if (cwd == NULL)
     44        goto bail;
     45    n = GetCurrentDirectory(n, cwd);
     46    if (n == 0)
     47        goto bail;
     48
     49    n = n + 1 + _tcslen(reldir) + 1;
     50    subdir = (LPTSTR) malloc(n * sizeof(TCHAR));
     51    if (subdir == NULL)
     52        goto bail;
     53    len = _sntprintf(subdir, n, "%s\\%s", cwd, reldir);
     54    if (len >= n || len < 0)
     55        goto bail;
     56
     57    free(cwd);
     58    return subdir;
     59
     60bail:
     61    if (cwd != NULL)
     62        free(cwd);
     63    if (subdir != NULL)
     64        free(subdir);
     65    return NULL;
     66}
     67
     68// Add a directory to the beginning of the PATH environment variable. Returns 0
     69// on failure, nonzero on success.
     70// http://msdn.microsoft.com/en-us/library/windows/desktop/ms682009%28v=vs.85%29.aspx
     71DWORD PrependToPath(LPCTSTR dir)
     72{
     73    DWORD n, len, rc, err;
     74    LPTSTR path, value;
     75
     76    path = NULL;
     77    value = NULL;
     78
     79    // First find out how big a buffer we need.
     80    n = GetEnvironmentVariable(TEXT ("PATH"), NULL, 0);
     81    if (n == 0)
     82    {
     83        err = GetLastError();
     84        if (err == ERROR_ENVVAR_NOT_FOUND)
     85            // If the variable doesn't yet exist, just set it and return.
     86            return SetEnvironmentVariable(TEXT ("PATH"), dir);
     87        else
     88            goto bail;
     89    }
     90    // Now that we know the buffer size, get the value.
     91    path = (LPTSTR) malloc(n * sizeof(TCHAR));
     92    if (path == NULL)
     93        goto bail;
     94    n = GetEnvironmentVariable(TEXT ("PATH"), path, n);
     95    if (n == 0)
     96        goto bail;
     97
     98    n = _tcslen(dir) + 1 + n + 1;
     99    value = (LPTSTR) malloc(n * sizeof(TCHAR));
     100    if (value == NULL)
     101        goto bail;
     102    len = _sntprintf(value, n, "%s;%s", dir, path);
     103    if (len >= n || len < 0)
     104        goto bail;
     105
     106    rc = SetEnvironmentVariable(TEXT ("PATH"), value);
     107    if (rc == 0)
     108        goto bail;
     109
     110    free(path);
     111    free(value);
     112    return 1;
     113
     114bail:
     115    if (path != NULL)
     116        free(path);
     117    if (value != NULL)
     118        free(value);
     119    return 0;
     120}
     121
     122// Returns 0 on failure and nonzero on success.
     123DWORD StartTorBrowser(void)
    26124{
     125    TCHAR *TorDir;
     126    DWORD rc;
     127    // Put the Tor subdirectory at the beginning of PATH so that pluggable
     128    // transports (in their own subdirectory) can access DLLs.
     129    // https://trac.torproject.org/projects/tor/ticket/10845
     130    TorDir = GetSubdirectory(TEXT ("Tor"));
     131    if (TorDir == NULL)
     132        return 0;
     133    rc = PrependToPath(TorDir);
     134    free(TorDir);
     135    if (rc == 0)
     136        return 0;
     137
    27138    STARTUPINFO si;
    28139    PROCESS_INFORMATION pi;
    29140   
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine 
    34145    TCHAR *ProgramToStart;
    35146    ProgramToStart = TEXT ("Browser\\firefox.exe -no-remote -profile .\\Data\\Browser\\profile.default\\");
    36147
    37     if( !CreateProcess(
    38         NULL, ProgramToStart, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi ))
     148    return CreateProcess( NULL, ProgramToStart, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi );
     149}
     150
     151int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
     152{
     153    if (!StartTorBrowser())
    39154    {
    40155         MessageBox ( NULL, TEXT ("Unable to start Tor Browser"), NULL, MB_OK);
    41156         return -1;