Example #1
0
NOEXPORT void save_log() {
    TCHAR file_name[MAX_PATH];
    OPENFILENAME ofn;
    LPTSTR txt;
    LPSTR str;

    ZeroMemory(&ofn, sizeof ofn);
    file_name[0]='\0';

    ofn.lStructSize=sizeof ofn;
    ofn.hwndOwner=hwnd;
    ofn.lpstrFilter=TEXT("Log Files (*.log)\0*.log\0All Files (*.*)\0*.*\0\0");
    ofn.lpstrFile=file_name;
    ofn.nMaxFile=MAX_PATH;
    ofn.lpstrDefExt=TEXT("LOG");
    ofn.lpstrInitialDir=TEXT(".");

    ofn.lpstrTitle=TEXT("Save Log");
    ofn.Flags=OFN_EXPLORER|OFN_PATHMUSTEXIST|OFN_HIDEREADONLY|
        OFN_OVERWRITEPROMPT;
    if(!GetSaveFileName(&ofn))
        return;

    txt=log_txt(); /* need to convert the result to UTF-8 */
    str=tstr2str(txt);
    str_free(txt);
    save_text_file(file_name, str);
    str_free(str);
}
NOEXPORT void error_box(const LPSTR text) {
    char *errmsg, *fullmsg;
    LPTSTR tstr;
    long dw;

    dw=GetLastError();
    FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM,
        NULL, dw, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
        (LPTSTR)&tstr, 0, NULL);
    errmsg=tstr2str(tstr);
    LocalFree(tstr);
    fullmsg=str_printf("%s: error %ld: %s", text, dw, errmsg);
    str_free(errmsg);
    message_box(fullmsg, MB_ICONERROR);
    str_free(fullmsg);
}
Example #3
0
NOEXPORT void gui_cmdline() {
    char *line, *c, *opt;

    line=tstr2str(get_params());
    memset(&cmdline, 0, sizeof cmdline);
    c=line;
    while(*c && (*c=='/' || *c=='-')) {
        opt=c;
        while(*c && !isspace(*c)) /* skip non-whitespaces */
            c++;
        while(*c && isspace(*c)) /* replace whitespaces with '\0' */
            *c++='\0';
        if(!strcasecmp(opt+1, "install"))
            cmdline.install=1;
        else if(!strcasecmp(opt+1, "uninstall"))
            cmdline.uninstall=1;
        else if(!strcasecmp(opt+1, "reload"))
            cmdline.reload=1;
        else if(!strcasecmp(opt+1, "reopen"))
            cmdline.reopen=1;
        else if(!strcasecmp(opt+1, "start"))
            cmdline.start=1;
        else if(!strcasecmp(opt+1, "stop"))
            cmdline.stop=1;
        else if(!strcasecmp(opt+1, "service"))
            cmdline.service=1;
        else if(!strcasecmp(opt+1, "quiet"))
            cmdline.quiet=1;
        else if(!strcasecmp(opt+1, "exit"))
            cmdline.exit=1;
        else { /* an option to be processed in options.c */
            c=opt;
            break;
        }
    }
    if(*c=='\"') { /* the option is within double quotes */
        c++;
        opt=c;
        while(*c && *c!='\"') /* find the closing double quote */
            c++;
        *c='\0';
    } else /* the option is simply the rest of the line */
        opt=c;
    cmdline.config_file=*opt ? str_dup(opt) : NULL;
    str_free(line);
}
Example #4
0
File: gui.c Project: djs55/stunnel
static void save_file(HWND hwnd) {
    TCHAR szFileName[MAX_PATH];
    OPENFILENAME ofn;
    HANDLE hFile;
    BOOL bResult;
    LPTSTR txt;
    LPSTR str;
    DWORD nWritten;

    ZeroMemory(&ofn, sizeof(ofn));
    szFileName[0]='\0';

    ofn.lStructSize=sizeof(ofn);
    ofn.hwndOwner=hwnd;
    ofn.lpstrFilter=TEXT("Log Files (*.log)\0*.log\0All Files (*.*)\0*.*\0\0");
    ofn.lpstrFile=szFileName;
    ofn.nMaxFile=MAX_PATH;
    ofn.lpstrDefExt=TEXT("LOG");
    ofn.lpstrInitialDir=TEXT(".");

    ofn.lpstrTitle=TEXT("Save Log");
    ofn.Flags=OFN_EXPLORER | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY |
        OFN_OVERWRITEPROMPT;
    if(!GetSaveFileName(&ofn))
        return;

    if((hFile=CreateFile(szFileName, GENERIC_WRITE,
            0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL,
            (HANDLE) NULL))==INVALID_HANDLE_VALUE) {
        error_box(TEXT("CreateFile"));
        return;
    }

    txt=log_txt();
    str=tstr2str(txt);
    free(txt);
    bResult=WriteFile(hFile, str, strlen(str), &nWritten, NULL);
    free(str);
    if(!bResult)
        error_box(TEXT("WriteFile"));
    CloseHandle(hFile);
}
Example #5
0
File: gui.c Project: djs55/stunnel
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
#ifdef _WIN32_WCE
    LPWSTR lpCmdLine,
#else
    LPSTR lpCmdLine,
#endif
    int nCmdShow) {

    LPSTR command_line;

#ifdef _WIN32_WCE
    command_line=tstr2str(lpCmdLine);
#else
    command_line=lpCmdLine;
#endif

    ghInst=hInstance;

    parse_cmdline(command_line); /* setup global cmdline structure */
#ifndef _WIN32_WCE
    if(set_cwd()) /* set current working directory */
        return 1;
#endif

    /* setup the windo caption before reading the configuration file
     * options.win32_service is not available here and may not be used */
#ifdef _WIN32_WCE
    _tcscpy(win32_name, TEXT("stunnel ") TEXT(VERSION)
        TEXT(" on Windows CE (not configured)"));
#else
    _tcscpy(win32_name, TEXT("stunnel ") TEXT(VERSION)
        TEXT(" on Win32 (not configured)"));
#endif

    if(initialize_winsock())
        return 1;

    if(!setjmp(jump_buf)) { /* TRY */
        main_initialize(
            cmdline.config_file[0] ? cmdline.config_file : NULL, NULL);
#ifdef _WIN32_WCE
        _tcscpy(win32_name, TEXT("stunnel ") TEXT(VERSION)
            TEXT(" on Windows CE"));
#else
        _snprintf(win32_name, STRLEN, "stunnel %s on Win32 (%s)",
            VERSION, options.win32_service); /* update the information */
        if(!cmdline.service) {
            if(cmdline.install)
                return service_install(command_line);
            if(cmdline.uninstall)
                return service_uninstall();
            if(cmdline.start)
                return service_start();
            if(cmdline.stop)
                return service_stop();
        }
#endif
    }

    /* CATCH */
#ifndef _WIN32_WCE
    if(cmdline.service)
        return service_initialize();
    else
#endif
        return win_main(hInstance, hPrevInstance, command_line, nCmdShow);
}
Example #6
0
NOEXPORT LRESULT CALLBACK pass_proc(HWND dialog_handle, UINT message,
        WPARAM wParam, LPARAM lParam) {
    LPTSTR titlebar;
    union {
        TCHAR txt[PEM_BUFSIZE];
        WORD len;
    } pass_dialog;
    WORD pass_len;
    char* pass_txt;
    LPTSTR key_file_name;

    switch(message) {
    case WM_INITDIALOG:
        /* set the default push button to "Cancel" */
        SendMessage(dialog_handle, DM_SETDEFID, (WPARAM)IDCANCEL, (LPARAM)0);

        key_file_name=str2tstr(ui_data->section->key);
        titlebar=str_tprintf(TEXT("Private key: %s"), key_file_name);
        str_free(key_file_name);
        SetWindowText(dialog_handle, titlebar);
        str_free(titlebar);
        return TRUE;

    case WM_COMMAND:
        /* set the default push button to "OK" when the user enters text */
        if(HIWORD(wParam)==EN_CHANGE && LOWORD(wParam)==IDE_PASSEDIT)
            SendMessage(dialog_handle, DM_SETDEFID, (WPARAM)IDOK, (LPARAM)0);
        switch(wParam) {
        case IDOK:
            /* get number of characters */
            pass_len=(WORD)SendDlgItemMessage(dialog_handle,
                IDE_PASSEDIT, EM_LINELENGTH, (WPARAM)0, (LPARAM)0);
            if(!pass_len || pass_len>=PEM_BUFSIZE) {
                EndDialog(dialog_handle, FALSE);
                return FALSE;
            }

            /* put the number of characters into first word of buffer */
            pass_dialog.len=pass_len;

            /* get the characters */
            SendDlgItemMessage(dialog_handle, IDE_PASSEDIT, EM_GETLINE,
                (WPARAM)0 /* line 0 */, (LPARAM)pass_dialog.txt);
            pass_dialog.txt[pass_len]='\0'; /* null-terminate the string */

            /* convert input passphrase to UTF-8 string (as ui_data->pass) */
            pass_txt=tstr2str(pass_dialog.txt);
            strcpy(ui_data->pass, pass_txt);
            str_free(pass_txt);

            EndDialog(dialog_handle, TRUE);
            return TRUE;

        case IDCANCEL:
            EndDialog(dialog_handle, FALSE);
            return TRUE;
        }
        return 0;
    }
    return FALSE;

    UNREFERENCED_PARAMETER(lParam);
}
int WINAPI WinMain(HINSTANCE this_instance, HINSTANCE prev_instance,
#ifdef _WIN32_WCE
        LPWSTR lpCmdLine,
#else
        LPSTR lpCmdLine,
#endif
        int nCmdShow) {
    LPSTR command_line;
    char *c, stunnel_exe_path[MAX_PATH];
#ifndef _WIN32_WCE
    char *errmsg;
#endif

    (void)prev_instance; /* skip warning about unused parameter */
    (void)nCmdShow; /* skip warning about unused parameter */

    str_init(); /* initialize per-thread string management */
    ghInst=this_instance;
#ifdef _WIN32_WCE
    command_line=tstr2str(lpCmdLine);
#else
    command_line=lpCmdLine;
#endif

    parse_cmdline(command_line); /* setup global cmdline structure */

    GetModuleFileName(0, stunnel_exe_path, MAX_PATH);

#ifndef _WIN32_WCE
    /* find previous instances of the same executable */
    if(!cmdline.service && !cmdline.install && !cmdline.uninstall &&
            !cmdline.start && !cmdline.stop) {
        EnumWindows(enum_windows, (LPARAM)stunnel_exe_path);
        if(cmdline.exit)
            return 0; /* in case EnumWindows didn't find a previous instance */
    }
#endif

    /* set current working directory and engine path */
    c=strrchr(stunnel_exe_path, '\\'); /* last backslash */
    if(c) /* found */
        c[1]='\0'; /* truncate program name */
#ifndef _WIN32_WCE
    if(!SetCurrentDirectory(stunnel_exe_path)) {
        errmsg=str_printf("Cannot set directory to %s", stunnel_exe_path);
        message_box(errmsg, MB_ICONERROR);
        str_free(errmsg);
        return 1;
    }
#endif
    _putenv_s("OPENSSL_ENGINES", stunnel_exe_path);

    if(initialize_winsock())
        return 1;

#ifndef _WIN32_WCE
    if(cmdline.service) /* "-service" must be processed before "-install" */
        return service_initialize();
    if(cmdline.install)
        return service_install(command_line);
    if(cmdline.uninstall)
        return service_uninstall();
    if(cmdline.start)
        return service_start();
    if(cmdline.stop)
        return service_stop();
#endif
    return gui_loop();
}