static int connect_local(CLI *c) { /* spawn local process */ int fd[2]; STARTUPINFO si; PROCESS_INFORMATION pi; LPTSTR execname_l, execargs_l; make_sockets(c, fd); memset(&si, 0, sizeof si); si.cb=sizeof si; si.wShowWindow=SW_HIDE; si.dwFlags=STARTF_USESHOWWINDOW|STARTF_USESTDHANDLES; si.hStdInput=si.hStdOutput=si.hStdError=(HANDLE)fd[1]; memset(&pi, 0, sizeof pi); execname_l=str2tstr(c->opt->execname); execargs_l=str2tstr(c->opt->execargs); CreateProcess(execname_l, execargs_l, NULL, NULL, TRUE, 0, NULL, NULL, &si, &pi); str_free(execname_l); str_free(execargs_l); closesocket(fd[1]); CloseHandle(pi.hProcess); CloseHandle(pi.hThread); return fd[0]; }
void win_log(LPSTR line) { /* Also used in log.c */ struct LIST *curr; int len; static int log_len=0; LPTSTR txt; txt=str2tstr(line); len=_tcslen(txt); curr=malloc(sizeof(struct LIST)+len*sizeof(TCHAR)); curr->len=len; _tcscpy(curr->txt, txt); free(txt); curr->next=NULL; enter_critical_section(CRIT_WIN_LOG); if(tail) tail->next=curr; tail=curr; if(!head) head=tail; log_len++; while(log_len>LOG_LINES) { curr=head; head=head->next; free(curr); log_len--; } leave_critical_section(CRIT_WIN_LOG); if(visible) { txt=log_txt(); SetWindowText(EditControl, txt); free(txt); } }
void win_log(LPSTR line) { /* Also used in log.c */ LPTSTR tstr; tstr=str2tstr(line); RETAILMSG(TRUE, (TEXT("%s\r\n"), tstr)); free(tstr); }
DISK_FILE *file_open(char *name, int wr) { DISK_FILE *df; #ifdef USE_WIN32 LPTSTR tstr; #endif /* USE_WIN32 */ df=calloc(1, sizeof(DISK_FILE)); if(!df) return NULL; #ifdef USE_WIN32 tstr=str2tstr(name); df->fh=CreateFile(tstr, wr ? GENERIC_WRITE : GENERIC_READ, FILE_SHARE_READ, NULL, wr ? OPEN_ALWAYS : OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, (HANDLE)NULL); free(tstr); if(df->fh!=INVALID_HANDLE_VALUE) { /* OK! */ if(wr) /* append */ SetFilePointer(df->fh, 0, NULL, FILE_END); return df; } #else /* USE_WIN32 */ df->fd=open(name, wr ? O_CREAT|O_WRONLY|O_APPEND : O_RDONLY, 0640); if(df->fd>=0) { /* OK! */ #ifndef __vms fcntl(df->fd, F_SETFD, FD_CLOEXEC); #endif /* ! __vms */ return df; } #endif /* USE_WIN32 */ /* failed to open the file */ free(df); ioerror(name); return NULL; }
NOEXPORT void win_log(LPSTR line) { struct LIST *curr; int len; static int log_len=0; LPTSTR txt; txt=str2tstr(line); len=_tcslen(txt); /* this list is shared between threads */ curr=str_alloc(sizeof(struct LIST)+len*sizeof(TCHAR)); curr->len=len; _tcscpy(curr->txt, txt); str_free(txt); curr->next=NULL; if(tail) tail->next=curr; tail=curr; if(!head) head=tail; log_len++; while(log_len>LOG_LINES) { curr=head; head=head->next; /* this list is shared between threads */ str_free(curr); log_len--; } new_logs=1; }
DISK_FILE *file_open(char *name, FILE_MODE mode) { DISK_FILE *df; LPTSTR tstr; HANDLE fh; DWORD desired_access, creation_disposition; /* open file */ switch(mode) { case FILE_MODE_READ: desired_access=FILE_READ_DATA; creation_disposition=OPEN_EXISTING; break; case FILE_MODE_APPEND: desired_access=FILE_APPEND_DATA; creation_disposition=OPEN_ALWAYS; break; case FILE_MODE_OVERWRITE: desired_access=FILE_WRITE_DATA; creation_disposition=CREATE_ALWAYS; break; default: /* invalid mode */ return NULL; } tstr=str2tstr(name); fh=CreateFile(tstr, desired_access, FILE_SHARE_READ, NULL, creation_disposition, FILE_ATTRIBUTE_NORMAL, (HANDLE)NULL); str_free(tstr); /* str_free() overwrites GetLastError() value */ if(fh==INVALID_HANDLE_VALUE) return NULL; /* setup df structure */ df=str_alloc(sizeof df); df->fh=fh; return df; }
/* TODO: port it to WCE */ NOEXPORT void edit_config(HWND main_window_handle) { TCHAR cwd[MAX_PATH]; LPTSTR conf_file, conf_path; DISK_FILE *df; conf_file=str2tstr(configuration_file); if(*conf_file==TEXT('\"')) { conf_path=conf_file; } else if(_tcschr(conf_file, TEXT('\\'))) { conf_path=str_tprintf(TEXT("\"%s\""), conf_file); str_free(conf_file); } else { GetCurrentDirectory(MAX_PATH, cwd); conf_path=str_tprintf(TEXT("\"%s\\%s\""), cwd, conf_file); str_free(conf_file); } df=file_open(configuration_file, FILE_MODE_APPEND); if(df) { /* the configuration file is writable */ file_close(df); ShellExecute(main_window_handle, TEXT("open"), TEXT("notepad.exe"), conf_path, NULL, SW_SHOWNORMAL); } else { /* UAC workaround */ ShellExecute(main_window_handle, TEXT("runas"), TEXT("notepad.exe"), conf_path, NULL, SW_SHOWNORMAL); } str_free(conf_path); }
void message_box(const LPSTR text, const UINT type) { LPTSTR tstr; if(cmdline.quiet) return; tstr=str2tstr(text); MessageBox(hwnd, tstr, win32_name, type); str_free(tstr); }
ICON_IMAGE load_icon_file(const char *name) { LPTSTR tname; ICON_IMAGE icon; tname=str2tstr((LPSTR)name); icon=LoadImage(NULL, tname, IMAGE_ICON, GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON), LR_LOADFROMFILE); str_free(tname); return icon; }
void ui_new_log(const char *line) { #ifdef _WIN32_WCE /* log to Windows CE debug output stream */ LPTSTR tstr; tstr=str2tstr(line); RETAILMSG(TRUE, (TEXT("%s\r\n"), tstr)); str_free(tstr); #else fputs(line, stderr); fflush(stderr); #endif }
void ui_new_log(const char *line) { LPTSTR tstr; tstr=str2tstr(line); #ifdef _WIN32_WCE /* log to Windows CE debug output stream */ RETAILMSG(TRUE, (TEXT("%s\r\n"), tstr)); #else /* use UTF-16 or native codepage rather than UTF-8 */ _ftprintf(stderr, TEXT("%s\r\n"), tstr); fflush(stderr); #endif str_free(tstr); }
ICON_IMAGE load_icon_file(const char *name) { LPTSTR tname; ICON_IMAGE icon; tname=str2tstr((LPSTR)name); #ifndef _WIN32_WCE icon=LoadImage(NULL, tname, IMAGE_ICON, GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON), LR_LOADFROMFILE); #else /* TODO: Implement a WCE version of LoadImage() */ /* icon=wceLoadIconFromFile(tname); */ s_log(LOG_ERR, "Loading image from file not implemented on WCE"); icon=NULL; #endif str_free(tname); return icon; }
DISK_FILE *file_open(char *name, FILE_MODE mode) { DISK_FILE *df; LPTSTR tname; HANDLE fh; DWORD desired_access, creation_disposition; /* open file */ switch(mode) { case FILE_MODE_READ: desired_access=GENERIC_READ; creation_disposition=OPEN_EXISTING; break; case FILE_MODE_APPEND: /* reportedly more compatible than FILE_APPEND_DATA */ desired_access=GENERIC_WRITE; creation_disposition=OPEN_ALWAYS; /* keep the data */ break; case FILE_MODE_OVERWRITE: desired_access=GENERIC_WRITE; creation_disposition=CREATE_ALWAYS; /* remove the data */ break; default: /* invalid mode */ return NULL; } tname=str2tstr(name); fh=CreateFile(tname, desired_access, FILE_SHARE_READ, NULL, creation_disposition, FILE_ATTRIBUTE_NORMAL, (HANDLE)NULL); str_free(tname); /* str_free() overwrites GetLastError() value */ if(fh==INVALID_HANDLE_VALUE) return NULL; if(mode==FILE_MODE_APPEND) /* workaround for FILE_APPEND_DATA */ SetFilePointer(fh, 0, NULL, FILE_END); /* setup df structure */ df=str_alloc(sizeof df); df->fh=fh; return df; }
DISK_FILE *file_open(char *name, int wr) { DISK_FILE *df; LPTSTR tstr; HANDLE fh; /* open file */ tstr=str2tstr(name); fh=CreateFile(tstr, wr ? GENERIC_WRITE : GENERIC_READ, FILE_SHARE_READ, NULL, wr ? OPEN_ALWAYS : OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, (HANDLE)NULL); str_free(tstr); if(fh==INVALID_HANDLE_VALUE) { ioerror(name); return NULL; } if(wr) /* append */ SetFilePointer(fh, 0, NULL, FILE_END); /* setup df structure */ df=str_alloc(sizeof df); df->fh=fh; return df; }
NOEXPORT void update_peer_menu(void) { SERVICE_OPTIONS *section; #ifndef _WIN32_WCE HMENU main_peer_list=NULL; #endif HMENU tray_peer_list=NULL; char *str; unsigned int section_number; MENUITEMINFO mii; /* purge menu peer lists */ #ifndef _WIN32_WCE if(main_menu_handle) main_peer_list=GetSubMenu(main_menu_handle, 2); /* 3rd submenu */ if(main_peer_list) while(GetMenuItemCount(main_peer_list)) /* purge old menu */ DeleteMenu(main_peer_list, 0, MF_BYPOSITION); #endif if(tray_menu_handle) tray_peer_list=GetSubMenu(GetSubMenu(tray_menu_handle, 0), 2); if(tray_peer_list) while(GetMenuItemCount(tray_peer_list)) /* purge old menu */ DeleteMenu(tray_peer_list, 0, MF_BYPOSITION); /* initialize data structures */ number_of_sections=0; for(section=service_options.next; section; section=section->next) section->section_number=number_of_sections++; section_number=0; for(section=service_options.next; section; section=section->next) { /* setup section->file */ str=str_printf("peer-%s.pem", section->servname); section->file=str2tstr(str); str_free(str); /* setup LPTSTR section->file */ str=str_printf("peer-%s.pem", section->servname); section->file=str2tstr(str); str_free(str); /* setup (char *) section->help */ section->help=str_printf( "Peer certificate chain has been saved.\n" "Add the following lines to section [%s]:\n" "\tCAfile = peer-%s.pem\n" "\tverify = 3\n" "to enable cryptographic authentication.\n" "Then reload stunnel configuration file.", section->servname, section->servname); /* setup section->chain */ section->chain=NULL; /* insert new menu item */ mii.cbSize=sizeof mii; mii.fMask=MIIM_STRING|MIIM_DATA|MIIM_ID|MIIM_STATE; mii.fType=MFT_STRING; mii.dwTypeData=section->file; mii.cch=_tcslen(mii.dwTypeData); mii.wID=IDM_PEER_MENU+section_number; mii.fState=MFS_GRAYED; #ifndef _WIN32_WCE if(main_peer_list) if(!InsertMenuItem(main_peer_list, section_number, TRUE, &mii)) ioerror("InsertMenuItem"); #endif if(tray_peer_list) if(!InsertMenuItem(tray_peer_list, section_number, TRUE, &mii)) ioerror("InsertMenuItem"); ++section_number; } if(hwnd) DrawMenuBar(hwnd); }
void ui_new_log(const char *line) { LPTSTR txt; txt=str2tstr(line); str_detach(txt); /* this allocation will be freed in the GUI thread */ PostMessage(hwnd, WM_LOG, (WPARAM)txt, 0); }
NOEXPORT void update_peer_menu(void) { SERVICE_OPTIONS *section; #ifndef _WIN32_WCE HMENU main_peer_list=NULL; #endif HMENU tray_peer_list=NULL; unsigned section_number; LPTSTR servname; /* purge menu peer lists */ #ifndef _WIN32_WCE if(main_menu_handle) main_peer_list=GetSubMenu(main_menu_handle, 2); /* 3rd submenu */ if(main_peer_list) while(GetMenuItemCount(main_peer_list)) /* purge old menu */ DeleteMenu(main_peer_list, 0, MF_BYPOSITION); #endif if(tray_menu_handle) tray_peer_list=GetSubMenu(GetSubMenu(tray_menu_handle, 0), 2); if(tray_peer_list) while(GetMenuItemCount(tray_peer_list)) /* purge old menu */ DeleteMenu(tray_peer_list, 0, MF_BYPOSITION); /* initialize data structures */ section_number=0; for(section=service_options.next; section; section=section->next) { servname=str2tstr(section->servname); /* setup LPTSTR section->file */ section->file=str_tprintf(TEXT("peer-%s.pem"), servname); /* setup section->help */ section->help=str_tprintf( TEXT("Peer certificate chain has been saved.\n") TEXT("Add the following lines to section [%s]:\n") TEXT("\tCAfile = peer-%s.pem\n") TEXT("\tverify = 3\n") TEXT("to enable cryptographic authentication.\n") TEXT("Then reload stunnel configuration file."), servname, servname); str_free(servname); /* setup section->chain */ section->chain=NULL; /* insert new menu item */ #ifndef _WIN32_WCE if(main_peer_list) if(!InsertMenu(main_peer_list, section_number, MF_BYPOSITION|MF_STRING|MF_GRAYED, IDM_PEER_MENU+section_number, section->file)) ioerror("InsertMenu"); #endif if(tray_peer_list) if(!InsertMenu(tray_peer_list, section_number, MF_BYPOSITION|MF_STRING|MF_GRAYED, IDM_PEER_MENU+section_number, section->file)) ioerror("InsertMenu"); ++section_number; } if(hwnd) DrawMenuBar(hwnd); }
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); }