Advertisement
Guest User

Untitled

a guest
Jun 1st, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 46.12 KB | None | 0 0
  1. /*
  2.  
  3. WASTE - util.cpp (General utility code)
  4.  
  5. Copyright (C) 2003 Nullsoft, Inc.
  6.  
  7. Copyright (C) 2004 WASTE Development Team
  8.  
  9.  
  10.  
  11. WASTE is free software; you can redistribute it and/or modify
  12.  
  13. it under the terms of the GNU General Public License as published by
  14.  
  15. the Free Software Foundation; either version 2 of the License, or
  16.  
  17. (at your option) any later version.
  18.  
  19.  
  20.  
  21. WASTE is distributed in the hope that it will be useful,
  22.  
  23. but WITHOUT ANY WARRANTY; without even the implied warranty of
  24.  
  25. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  26.  
  27. GNU General Public License for more details.
  28.  
  29.  
  30.  
  31. You should have received a copy of the GNU General Public License
  32.  
  33. along with WASTE; if not, write to the Free Software
  34.  
  35. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  36.  
  37. */
  38.  
  39.  
  40.  
  41. #include "stdafx.hpp"
  42.  
  43.  
  44.  
  45. #include "main.hpp"
  46.  
  47. #include "netkern.hpp"
  48.  
  49. #include "util.hpp"
  50.  
  51. #include "rsa/md5.hpp"
  52.  
  53. #include "rsa/r_random.hpp"
  54.  
  55.  
  56.  
  57. #if (defined(_WIN32) && defined(_CHECK_RSA_BLINDING))
  58.  
  59. #include "rsa/nn.hpp"
  60.  
  61. #include "rsa/rsa.hpp"
  62.  
  63. #endif
  64.  
  65.  
  66.  
  67. #ifdef _DEFINE_SRV
  68.  
  69. #include "resourcesrv.hpp"
  70.  
  71. #else
  72.  
  73. #include "resource.hpp"
  74.  
  75. #endif
  76.  
  77.  
  78.  
  79. #ifdef _DEFINE_WXUI
  80.  
  81. #include <wx/clipbrd.h>
  82.  
  83. #endif
  84.  
  85.  
  86.  
  87. //global strings
  88.  
  89.  
  90.  
  91. extern const char szDotWastestate[]=".WASTESTATE";
  92.  
  93. extern const char szWastestate[] ="WASTESTATE";
  94.  
  95.  
  96.  
  97. //////guid
  98.  
  99.  
  100.  
  101. void CreateID128(T_GUID *id)
  102.  
  103. {
  104.  
  105. R_GenerateBytes((unsigned char *)id->idc, 16, &g_random);
  106.  
  107. }
  108.  
  109.  
  110.  
  111. void MakeID128Str(T_GUID *id, char *str)
  112.  
  113. {
  114.  
  115. Bin2Hex(str,id->idc,sizeof(id->idc));
  116.  
  117. }
  118.  
  119.  
  120.  
  121. #if defined(_DEBUG) && defined(WIN32)
  122.  
  123. char* dbgstrdup(const char*str,const char *file, unsigned int line)
  124.  
  125. {
  126.  
  127. if (!str) return NULL;
  128.  
  129. int len=strlen(str);
  130.  
  131. char *dest=(char*)_malloc_dbg(len+1, _NORMAL_BLOCK, file, line);
  132.  
  133. safe_strncpy(dest,str,len+1);
  134.  
  135. return dest;
  136.  
  137. }
  138.  
  139. #endif
  140.  
  141.  
  142.  
  143. int MakeID128FromStr(const char *str, T_GUID *id)
  144.  
  145. {
  146.  
  147. int x;
  148.  
  149. const char *p=str;
  150.  
  151. for (x = 0; x < 16; x ++) {
  152.  
  153. int h=*p++;
  154.  
  155. int l=*p++;
  156.  
  157. if (l >= '0' && l <= '9') l-='0';
  158.  
  159. else if (l >= 'A' && l <= 'F') l -= 'A'-10;
  160.  
  161. else return 1;
  162.  
  163. if (h >= '0' && h <= '9') h-='0';
  164.  
  165. else if (h >= 'A' && h <= 'F') h -= 'A'-10;
  166.  
  167. else return 1;
  168.  
  169. id->idc[x]=(unsigned char)((l|(h<<4))&0xff);
  170.  
  171. };
  172.  
  173. return 0;
  174.  
  175. }
  176.  
  177.  
  178.  
  179. inline void Bin2Hex_Single(char* buf,char inp)
  180.  
  181. {
  182.  
  183. char c1,c2;
  184.  
  185. c1=((inp>>0)&0xf)+'0';if (c1>'9') c1=c1-'9'-1+'A';
  186.  
  187. c2=((inp>>4)&0xf)+'0';if (c2>'9') c2=c2-'9'-1+'A';
  188.  
  189. buf[1]=c1;buf[0]=c2;
  190.  
  191. }
  192.  
  193.  
  194.  
  195. char* Bin2Hex(char* output, unsigned char* input, int len)
  196.  
  197. {
  198.  
  199. while (len>0) {
  200.  
  201. Bin2Hex_Single(output,*input);
  202.  
  203. input+=1;
  204.  
  205. output+=2;
  206.  
  207. len--;
  208.  
  209. };
  210.  
  211. *output=0;
  212.  
  213. return output;
  214.  
  215. }
  216.  
  217.  
  218.  
  219. char* Bin2Hex_Lf(char* output, unsigned char* input, int len, int &perline, int maxperline, bool wantCrLf)
  220.  
  221. {
  222.  
  223. if (maxperline==0) maxperline=1;
  224.  
  225. while (len>0) {
  226.  
  227. if (perline>=maxperline) {
  228.  
  229. perline=0;
  230.  
  231. #ifdef _WIN32
  232.  
  233. if (wantCrLf) *output++='\r';
  234.  
  235. #endif
  236.  
  237. *output++='\n';
  238.  
  239. };
  240.  
  241. Bin2Hex_Single(output,*input);
  242.  
  243. input+=1;
  244.  
  245. output+=2;
  246.  
  247. len--;
  248.  
  249. perline++;
  250.  
  251. };
  252.  
  253. *output=0;
  254.  
  255. return output;
  256.  
  257. }
  258.  
  259.  
  260.  
  261. ///rng
  262.  
  263.  
  264.  
  265. R_RANDOM_STRUCT g_random;
  266.  
  267.  
  268.  
  269. #if defined(_WIN32)&&(!defined(_DEFINE_SRV))
  270.  
  271. static WNDPROC rng_oldWndProc;
  272.  
  273. static unsigned int rng_movebuf[7];
  274.  
  275. static int rng_movebuf_cnt;
  276.  
  277.  
  278.  
  279. static BOOL CALLBACK rng_newWndProc(HWND hwndDlg, UINT uMsg, WPARAM wParam,LPARAM lParam)
  280.  
  281. {
  282.  
  283. if (uMsg == WM_MOUSEMOVE) {
  284.  
  285. rng_movebuf[rng_movebuf_cnt%7]+=lParam;
  286.  
  287. rng_movebuf[(rng_movebuf_cnt+1)%7]+=GetTickCount();
  288.  
  289. rng_movebuf[(rng_movebuf_cnt+2)%7]+=GetMessageTime()+GetMessagePos();
  290.  
  291. if (++rng_movebuf_cnt >= 53) {
  292.  
  293. rng_movebuf_cnt=0;
  294.  
  295. R_RandomUpdate(&g_random,(unsigned char *)rng_movebuf,sizeof(rng_movebuf));
  296.  
  297.  
  298.  
  299. unsigned int bytesNeeded;
  300.  
  301. R_GetRandomBytesNeeded(&bytesNeeded, &g_random);
  302.  
  303. SendDlgItemMessage(hwndDlg,IDC_PROGRESS_RNG,PBM_SETPOS,(WPARAM)(64-bytesNeeded/4),0);
  304.  
  305. if (bytesNeeded<1) EndDialog(hwndDlg,1);
  306.  
  307. };
  308.  
  309. };
  310.  
  311. return CallWindowProc(rng_oldWndProc,hwndDlg,uMsg,wParam,lParam);
  312.  
  313. }
  314.  
  315.  
  316.  
  317. static BOOL WINAPI RndProc(HWND hwndDlg, UINT uMsg, WPARAM /*wParam*/, LPARAM /*lParam*/)
  318.  
  319. {
  320.  
  321. if (uMsg == WM_INITDIALOG) {
  322.  
  323. SetWindowText(hwndDlg,APP_NAME " Random Number Generator Initialization");
  324.  
  325. ShowWindow(GetDlgItem(hwndDlg,IDC_PROGRESS_RNG),SW_SHOWNA);
  326.  
  327. SendDlgItemMessage(hwndDlg,IDC_PROGRESS_RNG,PBM_SETRANGE,0,MAKELPARAM(0,64));
  328.  
  329. rng_oldWndProc=(WNDPROC) SetWindowLong(hwndDlg,GWL_WNDPROC,(LONG)rng_newWndProc);
  330.  
  331. };
  332.  
  333. return 0;
  334.  
  335. }
  336.  
  337.  
  338.  
  339. #endif//WIN32
  340.  
  341.  
  342.  
  343. void MYSRANDUPDATE(unsigned char *buf, int bufl)
  344.  
  345. {
  346.  
  347. static int last_srtime;
  348.  
  349. int t=GetTickCount()-last_srtime;
  350.  
  351. last_srtime=GetTickCount();
  352.  
  353.  
  354.  
  355. if (buf&&bufl) R_RandomUpdate(&g_random,buf,bufl);
  356.  
  357. R_RandomUpdate(&g_random, (unsigned char*)&t, sizeof(t));
  358.  
  359.  
  360.  
  361. //ADDED Md5Chap removed that nonworking code
  362.  
  363. }
  364.  
  365.  
  366.  
  367. void MYSRAND()
  368.  
  369. {
  370.  
  371. R_RandomInit(&g_random);
  372.  
  373. #ifdef _WIN32
  374.  
  375. if (!GetPrivateProfileStruct("config","rngseedn",g_random.state,16,g_config_mainini)) {
  376.  
  377. #ifndef _DEFINE_SRV
  378.  
  379. DialogBox(g_hInst,MAKEINTRESOURCE(IDD_RNGINIT),NULL,RndProc);
  380.  
  381. rng_movebuf_cnt=0;
  382.  
  383. memset(rng_movebuf,0,sizeof(rng_movebuf));
  384.  
  385. #else
  386.  
  387. log_printf(ds_Console,"CRITICAL WARNING! You need to run wastesrv configs at least once with WASTE. \"%s\" needs rngseedn value for security reasons!!! Exiting!",g_config_mainini);
  388.  
  389. g_exit=1;
  390.  
  391. return;
  392.  
  393. #endif
  394.  
  395. }
  396.  
  397. else {
  398.  
  399. g_random.outputAvailable=0;
  400.  
  401. g_random.bytesNeeded=0;
  402.  
  403. };
  404.  
  405. #else
  406.  
  407. log_printf(ds_Console,"initializing RNG");
  408.  
  409. FILE *fp=fopen("/dev/urandom","rb");
  410.  
  411. if (fp) {
  412.  
  413. fread(g_random.state,32,1,fp);
  414.  
  415. fclose(fp);
  416.  
  417. };
  418.  
  419. g_random.outputAvailable=0;
  420.  
  421. g_random.bytesNeeded=0;
  422.  
  423. log_printf(ds_Console,"done");
  424.  
  425. #endif
  426.  
  427.  
  428.  
  429. MYSRANDUPDATE();
  430.  
  431.  
  432.  
  433. unsigned char tmp[16];
  434.  
  435.  
  436.  
  437. R_GenerateBytes(tmp,sizeof(tmp), &g_random);
  438.  
  439.  
  440.  
  441. #ifdef _WIN32
  442.  
  443. WritePrivateProfileStruct("config","rngseedn",tmp,16,g_config_mainini);
  444.  
  445. #endif
  446.  
  447. }
  448.  
  449.  
  450.  
  451. ////misc string shit
  452.  
  453.  
  454.  
  455. char *extension(char *fn)
  456.  
  457. {
  458.  
  459. char *s = fn+strlen(fn);
  460.  
  461. while (s > fn && *s != '.'
  462.  
  463. #ifdef _WIN32
  464.  
  465. && *s != '\\'
  466.  
  467. #endif
  468.  
  469. && *s != '/') s=CharPrev(fn,s);
  470.  
  471. if (s == fn || *s != '.') return "";
  472.  
  473. return (s+1);
  474.  
  475. }
  476.  
  477.  
  478.  
  479. void removeInvalidFNChars(char *filename)
  480.  
  481. {
  482.  
  483. char *p=filename;
  484.  
  485. while (*p) {
  486.  
  487. if (*p == '?' || *p == '*' || *p == '>' || *p == '<' || *p == '|' || *p == ':') *p='_';
  488.  
  489. p=CharNext(p);
  490.  
  491. };
  492.  
  493. }
  494.  
  495.  
  496.  
  497. #if defined(_WIN32)&&(!defined(_DEFINE_SRV))
  498.  
  499. ////windows shit
  500.  
  501.  
  502.  
  503. int toolwnd_state;
  504.  
  505. int systray_state;
  506.  
  507.  
  508.  
  509. BOOL systray_add(HWND hwnd, HICON icon)
  510.  
  511. {
  512.  
  513. NOTIFYICONDATA tnid;
  514.  
  515. systray_state=1;
  516.  
  517. tnid.cbSize = sizeof(NOTIFYICONDATA);
  518.  
  519. tnid.hWnd = hwnd;
  520.  
  521. tnid.uID = 232;
  522.  
  523. tnid.uFlags = NIF_ICON | NIF_TIP | NIF_MESSAGE;
  524.  
  525. tnid.uCallbackMessage = WM_USER_SYSTRAY;
  526.  
  527. tnid.hIcon = icon;
  528.  
  529. GetWindowText(g_mainwnd,tnid.szTip,sizeof(tnid.szTip));
  530.  
  531. return (Shell_NotifyIcon(NIM_ADD, &tnid));
  532.  
  533. }
  534.  
  535.  
  536.  
  537. BOOL systray_del(HWND hwnd)
  538.  
  539. {
  540.  
  541. NOTIFYICONDATA tnid;
  542.  
  543. systray_state=0;
  544.  
  545. tnid.cbSize = sizeof(NOTIFYICONDATA);
  546.  
  547. tnid.hWnd = hwnd;
  548.  
  549. tnid.uID = 232;
  550.  
  551. return(Shell_NotifyIcon(NIM_DELETE, &tnid));
  552.  
  553. }
  554.  
  555.  
  556.  
  557. /* BSC Comment for compat
  558.  
  559. struct FLASHWINFO
  560.  
  561. {
  562.  
  563. UINT cbSize;
  564.  
  565. HWND hwnd;
  566.  
  567. DWORD dwFlags;
  568.  
  569. UINT uCount;
  570.  
  571. DWORD dwTimeout;
  572.  
  573. }
  574.  
  575. typedef FLASHWINFO *PFLASHWINFO;
  576.  
  577. */
  578.  
  579. //#define FLASHW_TRAY 2
  580.  
  581. //#define FLASHW_TIMERNOFG 12
  582.  
  583.  
  584.  
  585. static int (__stdcall *fflashWindowEx)(FLASHWINFO *);
  586.  
  587.  
  588.  
  589. void DoFlashWindow(HWND hwndParent, int timeoutval)
  590.  
  591. {
  592.  
  593. static HMODULE hUser32=NULL;
  594.  
  595. if (!hUser32) {
  596.  
  597. hUser32=LoadLibrary("USER32.DLL");
  598.  
  599. *((void**)&fflashWindowEx)=GetProcAddress(hUser32,"FlashWindowEx");
  600.  
  601. };
  602.  
  603.  
  604.  
  605. if (fflashWindowEx && GetForegroundWindow() != hwndParent && GetParent(GetForegroundWindow()) != hwndParent) {
  606.  
  607. FLASHWINFO fi;
  608.  
  609. fi.cbSize=sizeof(fi);
  610.  
  611. fi.hwnd=hwndParent;
  612.  
  613. fi.dwFlags=FLASHW_TRAY|FLASHW_TIMERNOFG;
  614.  
  615. fi.uCount=timeoutval;
  616.  
  617. fi.dwTimeout=0;
  618.  
  619. fflashWindowEx(&fi);
  620.  
  621. };
  622.  
  623. }
  624.  
  625.  
  626.  
  627. HWND CreateTooltip(HWND hWnd, LPSTR strTT)
  628.  
  629. {
  630.  
  631. HWND hWndTT; //handle to the ToolTip control
  632.  
  633. TOOLINFO ti;
  634.  
  635. unsigned int uid = 0; //for ti initialization
  636.  
  637. LPTSTR lptstr = strTT;
  638.  
  639. RECT rect; //for client area coordinates
  640.  
  641.  
  642.  
  643. /* CREATE A TOOLTIP WINDOW */
  644.  
  645. hWndTT = CreateWindowEx(
  646.  
  647. NULL,
  648.  
  649. TOOLTIPS_CLASS,
  650.  
  651. NULL,
  652.  
  653. WS_POPUP | TTS_NOPREFIX | TTS_ALWAYSTIP,
  654.  
  655. CW_USEDEFAULT,
  656.  
  657. CW_USEDEFAULT,
  658.  
  659. CW_USEDEFAULT,
  660.  
  661. CW_USEDEFAULT,
  662.  
  663. hWnd,
  664.  
  665. NULL,
  666.  
  667. g_hInst,
  668.  
  669. NULL
  670.  
  671. );
  672.  
  673.  
  674.  
  675. SetWindowPos(
  676.  
  677. hWndTT,
  678.  
  679. HWND_TOPMOST,
  680.  
  681. 0,
  682.  
  683. 0,
  684.  
  685. 0,
  686.  
  687. 0,
  688.  
  689. SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
  690.  
  691.  
  692.  
  693. /* GET COORDINATES OF THE MAIN CLIENT AREA */
  694.  
  695. GetClientRect(hWnd, &rect);
  696.  
  697.  
  698.  
  699. /* INITIALIZE MEMBERS OF THE TOOLINFO STRUCTURE */
  700.  
  701. ti.cbSize = sizeof(TOOLINFO);
  702.  
  703. ti.uFlags = TTF_SUBCLASS;
  704.  
  705. ti.hwnd = hWnd;
  706.  
  707. ti.hinst = g_hInst;
  708.  
  709. ti.uId = uid;
  710.  
  711. ti.lpszText = lptstr;
  712.  
  713. //ToolTip control will cover the whole window
  714.  
  715. ti.rect.left = rect.left;
  716.  
  717. ti.rect.top = rect.top;
  718.  
  719. ti.rect.right = rect.right;
  720.  
  721. ti.rect.bottom = rect.bottom;
  722.  
  723.  
  724.  
  725. /* SEND AN ADDTOOL MESSAGE TO THE TOOLTIP CONTROL WINDOW */
  726.  
  727. if(!SendMessage(hWndTT, TTM_ADDTOOL, 0, (LPARAM)&ti)){
  728.  
  729. #ifdef _DEBUG
  730.  
  731. MessageBox(0,"DEBUG: Couldn't create the ToolTip control.","Error",MB_OK);
  732.  
  733. return NULL;
  734.  
  735. #endif
  736.  
  737. };
  738.  
  739. return hWndTT;
  740.  
  741. }
  742.  
  743.  
  744.  
  745. void toolWindowSet(int twstate)
  746.  
  747. {
  748.  
  749. if (!toolwnd_state != !twstate) {
  750.  
  751. toolwnd_state=twstate;
  752.  
  753. int a=0;
  754.  
  755. if (IsWindowVisible(g_mainwnd)) {
  756.  
  757. ShowWindow(g_mainwnd,SW_HIDE);
  758.  
  759. a++;
  760.  
  761. };
  762.  
  763. if (toolwnd_state) {
  764.  
  765. SetWindowLong(g_mainwnd,GWL_EXSTYLE,(GetWindowLong(g_mainwnd,GWL_EXSTYLE)|WS_EX_TOOLWINDOW)&~WS_EX_APPWINDOW);
  766.  
  767. //SetWindowLong(g_mainwnd,GWL_STYLE,GetWindowLong(g_mainwnd,GWL_STYLE)(WS_MINIMIZEBOX|WS_SYSMENU));
  768.  
  769. }
  770.  
  771. else {
  772.  
  773. SetWindowLong(g_mainwnd,GWL_EXSTYLE,GetWindowLong(g_mainwnd,GWL_EXSTYLE)&~WS_EX_TOOLWINDOW);
  774.  
  775. //SetWindowLong(g_mainwnd,GWL_STYLE,GetWindowLong(g_mainwnd,GWL_STYLE)|(WS_MINIMIZEBOX|WS_SYSMENU));
  776.  
  777. };
  778.  
  779.  
  780.  
  781. if (a) {
  782.  
  783. ShowWindow(g_mainwnd,SW_SHOWNA);
  784.  
  785. SetWindowPos(g_mainwnd,NULL,0,0,0,0,SWP_NOMOVE|SWP_NOZORDER|SWP_NOSIZE|SWP_NOACTIVATE|SWP_DRAWFRAME);
  786.  
  787. };
  788.  
  789. };
  790.  
  791. }
  792.  
  793. #endif //WIN32
  794.  
  795. ///access control shit
  796.  
  797.  
  798.  
  799. ACitem *g_aclist=0;
  800.  
  801. int g_aclist_size;
  802.  
  803.  
  804.  
  805. bool allowIP(unsigned long addr)
  806.  
  807. {
  808.  
  809. if (!g_use_accesslist) return true;
  810.  
  811.  
  812.  
  813. ACitem *p=g_aclist;
  814.  
  815. int x;
  816.  
  817. if (!p) return 1;
  818.  
  819. for (x = 0; x < g_aclist_size; x ++) {
  820.  
  821. if (IPv4TestIpInMask(addr,htonl(p->ip),IPv4NetMask(p->maskbits))) {
  822.  
  823. return (p->allow!=0);
  824.  
  825. };
  826.  
  827. p++;
  828.  
  829. };
  830.  
  831. return true;
  832.  
  833. }
  834.  
  835.  
  836.  
  837. int ACStringToStruct(const char *t, ACitem *i)
  838.  
  839. {
  840.  
  841. char buf[64];
  842.  
  843. safe_strncpy(buf,t,64);
  844.  
  845. t=buf;
  846.  
  847. if (*t == 'A') i->allow=1;
  848.  
  849. else if (*t == 'D') i->allow=0;
  850.  
  851. else return 0;
  852.  
  853. t++;
  854.  
  855. char *p=strstr(t,"/");
  856.  
  857. if (!p || !p[1]) return 0;
  858.  
  859. i->maskbits=(char)(atoi(++p)&0xff);
  860.  
  861. if (*p < '0' || *p > '9') return 0;
  862.  
  863.  
  864.  
  865. p[-1]=0;
  866.  
  867. i->ip=inet_addr(t);
  868.  
  869. i->ip=ntohl(i->ip);
  870.  
  871. #if 0
  872.  
  873. dbg_printf(ds_Debug,"converted %s to %d:%08X/%d",t-1,i->allow,i->ip,i->maskbits);
  874.  
  875. #endif
  876.  
  877. return 1;
  878.  
  879. }
  880.  
  881.  
  882.  
  883. #if defined(_WIN32)&&(!defined(_DEFINE_SRV))
  884.  
  885. void updateACList(W_ListView *lv)
  886.  
  887. #else
  888.  
  889. void updateACList(void *lv)
  890.  
  891. #endif
  892.  
  893. {
  894.  
  895. free(g_aclist);
  896.  
  897. if (lv) {
  898.  
  899. #if defined(_WIN32)&&(!defined(_DEFINE_SRV))
  900.  
  901. g_aclist_size=lv->GetCount();
  902.  
  903. g_aclist=(ACitem *)malloc(sizeof(ACitem)*g_aclist_size);
  904.  
  905. g_config->WriteInt(CONFIG_ac_cnt,g_aclist_size);
  906.  
  907. int x;
  908.  
  909. int a=0;
  910.  
  911. for (x = 0; x < g_aclist_size; x ++) {
  912.  
  913. char b[512];
  914.  
  915. lv->GetText(x,0,b,sizeof(b));
  916.  
  917. lv->GetText(x,1,b+1,sizeof(b)-1);
  918.  
  919. char nstr[32];
  920.  
  921. sprintf(nstr,"ac_%d",x);
  922.  
  923. g_config->WriteString(nstr,b);
  924.  
  925. if (ACStringToStruct(b,g_aclist+a)) a++;
  926.  
  927. };
  928.  
  929. g_aclist_size=a;
  930.  
  931. #endif//WIN32
  932.  
  933. }
  934.  
  935. else //read it from config
  936.  
  937. {
  938.  
  939. g_aclist_size=g_config->ReadInt(CONFIG_ac_cnt,CONFIG_ac_cnt_DEFAULT);
  940.  
  941. g_aclist=(ACitem *)malloc(sizeof(ACitem)*g_aclist_size);
  942.  
  943. int x;
  944.  
  945. int a=0;
  946.  
  947. for (x = 0; x < g_aclist_size; x ++) {
  948.  
  949. char buf[64];
  950.  
  951. sprintf(buf,"ac_%d",x);
  952.  
  953. const char *t=g_config->ReadString(buf,"");
  954.  
  955. if (ACStringToStruct(t,g_aclist+a)) a++;
  956.  
  957. };
  958.  
  959. g_aclist_size=a;
  960.  
  961. };
  962.  
  963. }
  964.  
  965.  
  966.  
  967. ////key shit
  968.  
  969.  
  970.  
  971. static char tmp_passbuf[256];
  972.  
  973.  
  974.  
  975. #if defined(_WIN32)&&(!defined(_DEFINE_SRV))
  976.  
  977.  
  978.  
  979. BOOL WINAPI PassWordProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM /*lParam*/)
  980.  
  981. {
  982.  
  983. switch (uMsg)
  984.  
  985. {
  986.  
  987. case WM_INITDIALOG:
  988.  
  989. {
  990.  
  991. SetWindowText(hwndDlg,APP_NAME " : password");
  992.  
  993. memset(tmp_passbuf,0,256);
  994.  
  995. if (g_config->ReadInt(CONFIG_storepass,CONFIG_storepass_DEFAULT)) {
  996.  
  997. if (g_config->ReadString(CONFIG_keypass,CONFIG_keypass_DEFAULT)) {
  998.  
  999. safe_strncpy(tmp_passbuf,g_config->ReadString(CONFIG_keypass,CONFIG_keypass_DEFAULT),256);
  1000.  
  1001. };
  1002.  
  1003. CheckDlgButton(hwndDlg,IDC_CHECK_SAVE_PASSWD,BST_CHECKED);
  1004.  
  1005. };
  1006.  
  1007. SetDlgItemText(hwndDlg,IDC_EDIT_PASSWORD,tmp_passbuf);
  1008.  
  1009. memset(tmp_passbuf,0,256);
  1010.  
  1011. return 0;
  1012.  
  1013. };
  1014.  
  1015. case WM_CLOSE:
  1016.  
  1017. {
  1018.  
  1019. wParam=IDOK;
  1020.  
  1021. };
  1022.  
  1023. case WM_COMMAND:
  1024.  
  1025. {
  1026.  
  1027. if (LOWORD(wParam) == IDOK) {
  1028.  
  1029. GetDlgItemText(hwndDlg,IDC_EDIT_PASSWORD,tmp_passbuf,255);
  1030.  
  1031. tmp_passbuf[255]=0;
  1032.  
  1033. if (IsDlgButtonChecked(hwndDlg,IDC_CHECK_SAVE_PASSWD)) {
  1034.  
  1035. g_config->WriteInt(CONFIG_storepass,1);
  1036.  
  1037. g_config->WriteString(CONFIG_keypass,tmp_passbuf);
  1038.  
  1039. }
  1040.  
  1041. else {
  1042.  
  1043. g_config->WriteInt(CONFIG_storepass,0);
  1044.  
  1045. g_config->WriteString(CONFIG_keypass,"");
  1046.  
  1047. };
  1048.  
  1049. EndDialog(hwndDlg,0);
  1050.  
  1051. };
  1052.  
  1053. if (LOWORD(wParam) == IDCANCEL) {
  1054.  
  1055. EndDialog(hwndDlg,1);
  1056.  
  1057. };
  1058.  
  1059. return 0;
  1060.  
  1061. };
  1062.  
  1063. };
  1064.  
  1065. return 0;
  1066.  
  1067. }
  1068.  
  1069.  
  1070.  
  1071. #endif
  1072.  
  1073.  
  1074.  
  1075. static int readEncodedChar(FILE *in) //returns -1 on error
  1076.  
  1077. {
  1078.  
  1079. char buf[2];
  1080.  
  1081. int ret;
  1082.  
  1083. do buf[0]=(char)(fgetc(in)&0xff);
  1084.  
  1085. while (buf[0] == '\n' ||
  1086.  
  1087. buf[0] == '\r' ||
  1088.  
  1089. buf[0] == '\t' ||
  1090.  
  1091. buf[0] == ' ' ||
  1092.  
  1093. buf[0] == '-' ||
  1094.  
  1095. buf[0] == '_' ||
  1096.  
  1097. buf[0] == '>' ||
  1098.  
  1099. buf[0] == '<' ||
  1100.  
  1101. buf[0] == ':' ||
  1102.  
  1103. buf[0] == '|'
  1104.  
  1105. );
  1106.  
  1107. buf[1]=(char)(fgetc(in)&0xff);
  1108.  
  1109.  
  1110.  
  1111. if (buf[0] >= '0' && buf[0] <= '9') ret=(buf[0]-'0')<<4;
  1112.  
  1113. else if (buf[0] >= 'A' && buf[0] <= 'F') ret=(buf[0]-'A'+10)<<4;
  1114.  
  1115. else return -1;
  1116.  
  1117.  
  1118.  
  1119. if (buf[1] >= '0' && buf[1] <= '9') ret+=(buf[1]-'0');
  1120.  
  1121. else if (buf[1] >= 'A' && buf[1] <= 'F') ret+=(buf[1]-'A'+10);
  1122.  
  1123. else return -1;
  1124.  
  1125.  
  1126.  
  1127. return ret;
  1128.  
  1129. }
  1130.  
  1131.  
  1132.  
  1133. static int readBFdata(FILE *in, CBlowfish *bl, void *data, unsigned int len)
  1134.  
  1135. {
  1136.  
  1137. unsigned int x;
  1138.  
  1139. for (x = 0; x < len; x++) {
  1140.  
  1141. int c=readEncodedChar(in);
  1142.  
  1143. if (c<0) return 1;
  1144.  
  1145. ((unsigned char *)data)[x]=(unsigned char)c;
  1146.  
  1147. };
  1148.  
  1149. bl->DecryptCBC(data, len);
  1150.  
  1151. return 0;
  1152.  
  1153. }
  1154.  
  1155.  
  1156.  
  1157. #if defined(_WIN32)&&(!defined(_DEFINE_SRV))
  1158.  
  1159. int doLoadKey(HWND hwndParent, const char *pstr, const char *keyfn, R_RSA_PRIVATE_KEY *key)
  1160.  
  1161. #else
  1162.  
  1163. int doLoadKey(const char *pstr, const char *keyfn, R_RSA_PRIVATE_KEY *key)
  1164.  
  1165. #endif
  1166.  
  1167. {
  1168.  
  1169. int goagain=0;
  1170.  
  1171. SHAify m;
  1172.  
  1173. m.add((unsigned char *)pstr,strlen(pstr));
  1174.  
  1175. unsigned char tmp[SHA_OUTSIZE];
  1176.  
  1177. m.final(tmp);
  1178.  
  1179.  
  1180.  
  1181. CBlowfish bl;
  1182.  
  1183. //BLOWFISH_CTX bf;
  1184.  
  1185. bl.Init(tmp,SHA_OUTSIZE);
  1186.  
  1187.  
  1188.  
  1189. FILE *fp;
  1190.  
  1191. if ((fp=fopen(keyfn,"rt"))==NULL) {
  1192.  
  1193. key->bits=0;
  1194.  
  1195. #if defined(_WIN32)&&(!defined(_DEFINE_SRV))
  1196.  
  1197. MessageBox(hwndParent,"Error loading private key",APP_NAME " Error",MB_OK|MB_ICONSTOP);
  1198.  
  1199. #else
  1200.  
  1201. log_printf(ds_Console,APP_NAME " Error: Error loading private key");
  1202.  
  1203. #endif
  1204.  
  1205. return 1;
  1206.  
  1207. };
  1208.  
  1209.  
  1210.  
  1211. char *err=NULL;
  1212.  
  1213. char linebuf[1024];
  1214.  
  1215. while (!err) {
  1216.  
  1217. fgets(linebuf,1023,fp);
  1218.  
  1219. linebuf[1023]=0;
  1220.  
  1221. if (feof(fp) || !linebuf[0]) err="No private key found in file";
  1222.  
  1223. if (!strncmp(linebuf,"WASTE_PRIVATE_KEY ",strlen("WASTE_PRIVATE_KEY "))) break;
  1224.  
  1225. if (!strncmp(linebuf,"JSM_PRIVATE_KEY ",strlen("JSM_PRIVATE_KEY "))) break;
  1226.  
  1227. };
  1228.  
  1229.  
  1230.  
  1231. unsigned char tl[8]={0,};
  1232.  
  1233. if (!err) {
  1234.  
  1235. char *p=strstr(linebuf," ");
  1236.  
  1237. while (p && *p == ' ') p++;
  1238.  
  1239. if (p && atoi(p) >= 10 && atoi(p) < 20) {
  1240.  
  1241. p=strstr(p," ");
  1242.  
  1243. while (p && *p == ' ') p++;
  1244.  
  1245. if (p && (key->bits=atoi(p)) <= MAX_RSA_MODULUS_BITS && key->bits >= MIN_RSA_MODULUS_BITS) {
  1246.  
  1247. int x;
  1248.  
  1249. for (x = 0; x < 8 && !err; x ++) {
  1250.  
  1251. int c=readEncodedChar(fp);
  1252.  
  1253. if (c < 0) err="Private key corrupt";
  1254.  
  1255. else {
  1256.  
  1257. tl[x]=(unsigned char)(c&0xff);
  1258.  
  1259. //tl[x/4]|=c<<((x&3)*8);
  1260.  
  1261. };
  1262.  
  1263. };
  1264.  
  1265. if (!err) {
  1266.  
  1267. char buf[8];
  1268.  
  1269. bl.SetIV(CBlowfish::IV_BOTH,(unsigned long*)tl);
  1270.  
  1271. if (readBFdata(fp,&bl,buf,8)) {
  1272.  
  1273. err="Private key corrupt";
  1274.  
  1275. }
  1276.  
  1277. else if (memcmp(buf,"PASSWORD",8)) {
  1278.  
  1279. goagain++;
  1280.  
  1281. err="Invalid password for private key";
  1282.  
  1283. }
  1284.  
  1285. else {
  1286.  
  1287. int a=0;
  1288.  
  1289. //HACK md5chap g_key->key
  1290.  
  1291. #define WPK(x) if (!readBFdata(fp,&bl,key->x,sizeof(key->x)))
  1292.  
  1293. WPK(modulus)
  1294.  
  1295. WPK(publicExponent)
  1296.  
  1297. WPK(exponent)
  1298.  
  1299. WPK(prime)
  1300.  
  1301. WPK(primeExponent)
  1302.  
  1303. WPK(coefficient)
  1304.  
  1305. a++;
  1306.  
  1307. #undef WPK
  1308.  
  1309. if (!a) err="Private key corrupt";
  1310.  
  1311. };
  1312.  
  1313. };
  1314.  
  1315. //read key now
  1316.  
  1317. }
  1318.  
  1319. else
  1320.  
  1321. err="Private key found but size incorrect";
  1322.  
  1323. }
  1324.  
  1325. else err="Private key found but version incorrect";
  1326.  
  1327. };
  1328.  
  1329. fclose(fp);
  1330.  
  1331.  
  1332.  
  1333. if (err) {
  1334.  
  1335. key->bits=0;
  1336.  
  1337. #if defined(_WIN32)&&(!defined(_DEFINE_SRV))
  1338.  
  1339. MessageBox(hwndParent,err,APP_NAME " Error",MB_OK|MB_ICONSTOP);
  1340.  
  1341. #else
  1342.  
  1343. log_printf(ds_Console,APP_NAME " Error: %s",err);
  1344.  
  1345. #endif
  1346.  
  1347. return goagain?2:1;
  1348.  
  1349. };
  1350.  
  1351. return 0;
  1352.  
  1353. }
  1354.  
  1355.  
  1356.  
  1357. #if defined(_WIN32)&&(!defined(_DEFINE_SRV))
  1358.  
  1359. void reloadKey(const char *passstr, HWND hwndParent)
  1360.  
  1361. #else
  1362.  
  1363. void reloadKey(const char *passstr)
  1364.  
  1365. #endif
  1366.  
  1367. {
  1368.  
  1369. int retry=10;
  1370.  
  1371.  
  1372.  
  1373. char keyfn[1024];
  1374.  
  1375. sprintf(keyfn,"%s.pr4",g_config_prefix);
  1376.  
  1377.  
  1378.  
  1379. if (!passstr) {
  1380.  
  1381. giveitanothergo:
  1382.  
  1383. #if defined(_WIN32)&&(!defined(_DEFINE_SRV))
  1384.  
  1385. if (DialogBox(g_hInst,MAKEINTRESOURCE(IDD_PASSWD),hwndParent,PassWordProc)) {
  1386.  
  1387. g_key.bits=0;
  1388.  
  1389. memset(g_pubkeyhash,0,sizeof(g_pubkeyhash));
  1390.  
  1391. return;
  1392.  
  1393. };
  1394.  
  1395.  
  1396.  
  1397. #elif defined(_DEFINE_WXUI)
  1398.  
  1399. extern void wx_get_key_password(char *buf, int len);
  1400.  
  1401.  
  1402.  
  1403. wx_get_key_password(tmp_passbuf, sizeof(tmp_passbuf));
  1404.  
  1405. if (tmp_passbuf[0] == '\0') {
  1406.  
  1407. g_key.bits=0;
  1408.  
  1409. memset(g_pubkeyhash,0,sizeof(g_pubkeyhash));
  1410.  
  1411. return;
  1412.  
  1413. }
  1414.  
  1415.  
  1416.  
  1417. #else
  1418.  
  1419. log_printf(ds_Console,"\nPassword: ");
  1420.  
  1421. fgets(tmp_passbuf,sizeof(tmp_passbuf),stdin);
  1422.  
  1423. if (tmp_passbuf[0] && tmp_passbuf[strlen(tmp_passbuf)-1] == '\n') {
  1424.  
  1425. tmp_passbuf[strlen(tmp_passbuf)-1]=0;
  1426.  
  1427. };
  1428.  
  1429. #endif
  1430.  
  1431. passstr=tmp_passbuf;
  1432.  
  1433. };
  1434.  
  1435.  
  1436.  
  1437. #if defined(_WIN32)&&(!defined(_DEFINE_SRV))
  1438.  
  1439. int ret=doLoadKey(hwndParent,passstr,keyfn,&g_key);
  1440.  
  1441. #else
  1442.  
  1443. int ret=doLoadKey(passstr,keyfn,&g_key);
  1444.  
  1445. #endif
  1446.  
  1447.  
  1448.  
  1449. memset(tmp_passbuf,0,256);
  1450.  
  1451.  
  1452.  
  1453. if (ret) {
  1454.  
  1455. if (ret == 2) {
  1456.  
  1457. if (retry>0) {
  1458.  
  1459. retry--;
  1460.  
  1461. goto giveitanothergo;
  1462.  
  1463. }
  1464.  
  1465. else {
  1466.  
  1467. g_key.bits=0;
  1468.  
  1469. memset(g_pubkeyhash,0,sizeof(g_pubkeyhash));
  1470.  
  1471. return;
  1472.  
  1473. };
  1474.  
  1475. };
  1476.  
  1477. return;
  1478.  
  1479. };
  1480.  
  1481.  
  1482.  
  1483. SHAify m;
  1484.  
  1485.  
  1486.  
  1487. m.add((unsigned char *)g_key.modulus,MAX_RSA_MODULUS_LEN);
  1488.  
  1489. m.add((unsigned char *)g_key.publicExponent,MAX_RSA_MODULUS_LEN);
  1490.  
  1491. m.final(g_pubkeyhash);
  1492.  
  1493.  
  1494.  
  1495. #if 0
  1496.  
  1497. int x;
  1498.  
  1499. for (x=0; x < g_new_net.GetSize(); x ++) {
  1500.  
  1501. C_Connection *cc=g_new_net.Get(x);
  1502.  
  1503. cc->close(1);
  1504.  
  1505. };
  1506.  
  1507. if (g_mql) for (x = 0; x < g_mql->GetNumQueues(); x ++) {
  1508.  
  1509. g_mql->GetQueue(x)->get_con()->close(1);
  1510.  
  1511. };
  1512.  
  1513. #endif
  1514.  
  1515. }
  1516.  
  1517.  
  1518.  
  1519. C_ItemList<PKitem> g_pklist,g_pklist_pending;
  1520.  
  1521.  
  1522.  
  1523. void KillPkList()
  1524.  
  1525. {
  1526.  
  1527. int x=g_pklist.GetSize()-1;
  1528.  
  1529. while (x >= 0) {
  1530.  
  1531. free(g_pklist.Get(x));
  1532.  
  1533. g_pklist.Del(x);
  1534.  
  1535. x--;
  1536.  
  1537. };
  1538.  
  1539. x=g_pklist_pending.GetSize()-1;
  1540.  
  1541. while (x >= 0) {
  1542.  
  1543. free(g_pklist_pending.Get(x));
  1544.  
  1545. g_pklist_pending.Del(x);
  1546.  
  1547. x--;
  1548.  
  1549. };
  1550.  
  1551. }
  1552.  
  1553.  
  1554.  
  1555. int loadPKList(char *fn)
  1556.  
  1557. {
  1558.  
  1559. int err=0;
  1560.  
  1561. int cnt=0;
  1562.  
  1563. char str[1024+8];
  1564.  
  1565. if (!fn) sprintf(str,"%s.pr3",g_config_prefix);
  1566.  
  1567.  
  1568.  
  1569. FILE *fp=fopen(fn?fn:str,"rt");
  1570.  
  1571.  
  1572.  
  1573. if (fp) while (!err) {
  1574.  
  1575. char linebuf[1024];
  1576.  
  1577. char *lineptr=NULL;
  1578.  
  1579. PKitem item;
  1580.  
  1581. memset(&item,0,sizeof(PKitem));
  1582.  
  1583.  
  1584.  
  1585. int pending=0;
  1586.  
  1587. while (!err) {
  1588.  
  1589. lineptr=linebuf;
  1590.  
  1591. fgets(lineptr,1023,fp);
  1592.  
  1593. lineptr[1023]=0;
  1594.  
  1595. if (feof(fp) || !lineptr[0]) err++;
  1596.  
  1597. if (!strncmp(lineptr,"JSM_PENDING ",12) || !strncmp(lineptr,"WASTE_PENDING ",14)) {
  1598.  
  1599. pending=1;
  1600.  
  1601. lineptr+=(*lineptr == 'W')?14:12;
  1602.  
  1603. }
  1604.  
  1605. else pending=0;
  1606.  
  1607.  
  1608.  
  1609. if (!strncmp(lineptr,"JSM_PUBLIC_KEY ",strlen("JSM_PUBLIC_KEY "))) break;
  1610.  
  1611. if (!strncmp(lineptr,"WASTE_PUBLIC_KEY ",strlen("WASTE_PUBLIC_KEY "))) break;
  1612.  
  1613. };
  1614.  
  1615.  
  1616.  
  1617. if (!err) {
  1618.  
  1619. char *p=strstr(lineptr," ");
  1620.  
  1621. while (p && *p == ' ') p++;
  1622.  
  1623. if (p && atoi(p) >= 10 && atoi(p) <= 20) {
  1624.  
  1625. int newkeyfmt=0;
  1626.  
  1627. if (atoi(p) >= 20) newkeyfmt++;
  1628.  
  1629. p=strstr(p," ");
  1630.  
  1631. while (p && *p == ' ') p++;
  1632.  
  1633. if (p && (item.pk.bits=atoi(p)) <= MAX_RSA_MODULUS_BITS && item.pk.bits >= MIN_RSA_MODULUS_BITS) {
  1634.  
  1635. while (*p && *p != ' ') p++;
  1636.  
  1637. while (*p == ' ') p++;
  1638.  
  1639. //p is name\n now :)
  1640.  
  1641. while (*p && (p[strlen(p)-1] == '\n' || p[strlen(p)-1] == '\r' ||
  1642.  
  1643. p[strlen(p)-1] == '\t' || p[strlen(p)-1] == ' ')) p[strlen(p)-1]=0;
  1644.  
  1645.  
  1646.  
  1647. safe_strncpy(item.name,p,sizeof(item.name));
  1648.  
  1649.  
  1650.  
  1651. int x;
  1652.  
  1653. int ks=(item.pk.bits+7)/8;
  1654.  
  1655. for (x = 0; x < ks && !err; x ++) {
  1656.  
  1657. int c=readEncodedChar(fp);
  1658.  
  1659. if (c<0) err++;
  1660.  
  1661. item.pk.modulus[MAX_RSA_MODULUS_LEN-ks+x]=(unsigned char)(c&0xff);
  1662.  
  1663. };
  1664.  
  1665. if (newkeyfmt) {
  1666.  
  1667. int a=readEncodedChar(fp);
  1668.  
  1669. if (a<0) err++;
  1670.  
  1671. else {
  1672.  
  1673. int b=readEncodedChar(fp);
  1674.  
  1675. if (b < 0) err++;
  1676.  
  1677. else {
  1678.  
  1679. int expsize=(a<<8)|b;
  1680.  
  1681. if (expsize < 1 || expsize > ks) err++;
  1682.  
  1683. else ks=expsize;
  1684.  
  1685. };
  1686.  
  1687. };
  1688.  
  1689. };
  1690.  
  1691. for (x = 0; x < ks && !err; x ++) {
  1692.  
  1693. int c=readEncodedChar(fp);
  1694.  
  1695. if (c<0) err++;
  1696.  
  1697. item.pk.exponent[MAX_RSA_MODULUS_LEN-ks+x]=(unsigned char)(c&0xff);
  1698.  
  1699. };
  1700.  
  1701. }
  1702.  
  1703. else err++;
  1704.  
  1705. }
  1706.  
  1707. else err++;
  1708.  
  1709. };
  1710.  
  1711.  
  1712.  
  1713. if (err) break;
  1714.  
  1715.  
  1716.  
  1717. SHAify m;
  1718.  
  1719. m.add((unsigned char *)item.pk.modulus,MAX_RSA_MODULUS_LEN);
  1720.  
  1721. m.add((unsigned char *)item.pk.exponent,MAX_RSA_MODULUS_LEN);
  1722.  
  1723. m.final(item.hash);
  1724.  
  1725.  
  1726.  
  1727. PKitem *p=(PKitem *)malloc(sizeof(PKitem));
  1728.  
  1729. memcpy(p,&item,sizeof(item));
  1730.  
  1731. if (!pending) g_pklist.Add(p);
  1732.  
  1733. else g_pklist_pending.Add(p);
  1734.  
  1735.  
  1736.  
  1737. char buf[SHA_OUTSIZE*2+1];
  1738.  
  1739. Bin2Hex(buf,p->hash,SHA_OUTSIZE);
  1740.  
  1741. log_printf(ds_Informational,"PkLoad: %04i P%i %s %s",cnt+1,pending?1:0,buf,p->name);
  1742.  
  1743.  
  1744.  
  1745. cnt++;
  1746.  
  1747. };
  1748.  
  1749. if (fp) fclose(fp);
  1750.  
  1751. return cnt;
  1752.  
  1753. }
  1754.  
  1755.  
  1756.  
  1757. #if defined(_WIN32)&&(!defined(_DEFINE_SRV)) || defined(_DEFINE_WXUI)
  1758.  
  1759. void copyMyPubKey
  1760.  
  1761. #ifdef _DEFINE_WXUI
  1762.  
  1763. ()
  1764.  
  1765. #else
  1766.  
  1767. (HWND hwndOwner)
  1768.  
  1769. #endif
  1770.  
  1771. {
  1772.  
  1773. char buf[(MAX_RSA_MODULUS_LEN/8)*2*2+4096];
  1774.  
  1775. char *buf2;
  1776.  
  1777.  
  1778.  
  1779. #ifdef _WIN32
  1780.  
  1781. char *EOL = "\r\n";
  1782.  
  1783. bool wantCrLf = true;
  1784.  
  1785. #else
  1786.  
  1787. char *EOL = "\n";
  1788.  
  1789. bool wantCrLf = false;
  1790.  
  1791. #endif
  1792.  
  1793.  
  1794.  
  1795. buf[0]=0;buf2=buf;
  1796.  
  1797. int ks=(g_key.bits+7)/8;
  1798.  
  1799. if (!ks) return;
  1800.  
  1801.  
  1802.  
  1803. sprintf(buf2,"WASTE_PUBLIC_KEY 20 %d %s%s",g_key.bits,g_regnick[0]?g_regnick:"unknown", EOL);
  1804.  
  1805. buf2+=strlen(buf);
  1806.  
  1807.  
  1808.  
  1809. int lc=0, lcm=30;
  1810.  
  1811.  
  1812.  
  1813. buf2=Bin2Hex_Lf(buf2,&g_key.modulus[MAX_RSA_MODULUS_LEN-ks],ks,lc,lcm,wantCrLf);
  1814.  
  1815.  
  1816.  
  1817. int zeroes;
  1818.  
  1819. for (zeroes = MAX_RSA_MODULUS_LEN-ks; zeroes < MAX_RSA_MODULUS_LEN && !g_key.publicExponent[zeroes]; zeroes ++);
  1820.  
  1821.  
  1822.  
  1823. ks=MAX_RSA_MODULUS_LEN-zeroes;
  1824.  
  1825.  
  1826.  
  1827. unsigned char x;
  1828.  
  1829. x=(unsigned char)((ks>>8)&0xff);buf2=Bin2Hex_Lf(buf2,&x,1,lc,lcm,wantCrLf);
  1830.  
  1831. x=(unsigned char)((ks>>0)&0xff);buf2=Bin2Hex_Lf(buf2,&x,1,lc,lcm,wantCrLf);
  1832.  
  1833.  
  1834.  
  1835. buf2=Bin2Hex_Lf(buf2,&g_key.publicExponent[MAX_RSA_MODULUS_LEN-ks],ks,lc,lcm,wantCrLf);
  1836.  
  1837.  
  1838.  
  1839. sprintf(buf2,"%sWASTE_PUBLIC_KEY_END%s", EOL, EOL);
  1840.  
  1841.  
  1842.  
  1843. #ifdef _DEFINE_WXUI
  1844.  
  1845.  
  1846.  
  1847. if (wxTheClipboard->Open()) {
  1848.  
  1849. // clipboard will call delete()
  1850.  
  1851. wxTheClipboard->SetData(new
  1852.  
  1853. wxTextDataObject(cstr_to_wxstr(buf)));
  1854.  
  1855. wxTheClipboard->Close();
  1856.  
  1857. }
  1858.  
  1859.  
  1860.  
  1861. #else
  1862.  
  1863.  
  1864.  
  1865. HANDLE h=GlobalAlloc(GMEM_MOVEABLE,strlen(buf)+1);
  1866.  
  1867. void *t=GlobalLock(h);
  1868.  
  1869. memcpy(t,buf,strlen(buf)+1);
  1870.  
  1871. GlobalUnlock(h);
  1872.  
  1873. OpenClipboard(hwndOwner);
  1874.  
  1875. EmptyClipboard();
  1876.  
  1877. SetClipboardData(CF_TEXT,h);
  1878.  
  1879. CloseClipboard();
  1880.  
  1881. #endif
  1882.  
  1883. }
  1884.  
  1885. #endif
  1886.  
  1887.  
  1888.  
  1889. void savePKList()
  1890.  
  1891. {
  1892.  
  1893. char str[1024+8];
  1894.  
  1895. char buf[(MAX_RSA_MODULUS_LEN/8)*2*2+4096];
  1896.  
  1897. char *buf2;
  1898.  
  1899. sprintf(str,"%s.pr3",g_config_prefix);
  1900.  
  1901. FILE *fp=fopen(str,"wt");
  1902.  
  1903. if (fp) {
  1904.  
  1905. int x;
  1906.  
  1907. int total=g_pklist.GetSize()+g_pklist_pending.GetSize();
  1908.  
  1909. for (x = 0; x < total; x ++) {
  1910.  
  1911. PKitem *t;
  1912.  
  1913. bool pending=(x >= g_pklist.GetSize());
  1914.  
  1915. if (pending) t=g_pklist_pending.Get(x-g_pklist.GetSize());
  1916.  
  1917. else t=g_pklist.Get(x);
  1918.  
  1919.  
  1920.  
  1921. buf[0]=0;buf2=buf;
  1922.  
  1923. int ks=(t->pk.bits+7)/8;
  1924.  
  1925. if (!ks) continue;
  1926.  
  1927.  
  1928.  
  1929. sprintf(buf2,"%sWASTE_PUBLIC_KEY 20 %d %s\n",pending?"WASTE_PENDING ":"",t->pk.bits,t->name[0]?t->name:"unknown");
  1930.  
  1931. buf2+=strlen(buf);
  1932.  
  1933. int lc=0,lcm=30;
  1934.  
  1935.  
  1936.  
  1937. buf2=Bin2Hex_Lf(buf2,&t->pk.modulus[MAX_RSA_MODULUS_LEN-ks],ks,lc,lcm,false);
  1938.  
  1939.  
  1940.  
  1941. int zeroes;
  1942.  
  1943. for (zeroes = MAX_RSA_MODULUS_LEN-ks; zeroes < MAX_RSA_MODULUS_LEN && !t->pk.exponent[zeroes]; zeroes ++);
  1944.  
  1945. ks=MAX_RSA_MODULUS_LEN-zeroes;
  1946.  
  1947.  
  1948.  
  1949. unsigned char x;
  1950.  
  1951. x=(unsigned char)((ks>>8)&0xff);buf2=Bin2Hex_Lf(buf2,&x,1,lc,lcm,false);
  1952.  
  1953. x=(unsigned char)((ks>>0)&0xff);buf2=Bin2Hex_Lf(buf2,&x,1,lc,lcm,false);
  1954.  
  1955.  
  1956.  
  1957. buf2=Bin2Hex_Lf(buf2,&t->pk.exponent[MAX_RSA_MODULUS_LEN-ks],ks,lc,lcm,false);
  1958.  
  1959. sprintf(buf2,"\nWASTE_PUBLIC_KEY_END\n\n");
  1960.  
  1961. buf2+=strlen(buf);
  1962.  
  1963.  
  1964.  
  1965. fprintf(fp,"%s",buf);
  1966.  
  1967. };
  1968.  
  1969. fclose(fp);
  1970.  
  1971. };
  1972.  
  1973. }
  1974.  
  1975.  
  1976.  
  1977. int findPublicKeyFromKey(R_RSA_PUBLIC_KEY *key) // 1 on found, searches pending too!
  1978.  
  1979. {
  1980.  
  1981. int x;
  1982.  
  1983. for (x = 0; x < g_pklist.GetSize(); x ++) {
  1984.  
  1985. PKitem *pi=g_pklist.Get(x);
  1986.  
  1987. R_RSA_PUBLIC_KEY *t=&pi->pk;
  1988.  
  1989. if (key->bits == t->bits &&
  1990.  
  1991. !memcmp(key->exponent,t->exponent,MAX_RSA_MODULUS_LEN) &&
  1992.  
  1993. !memcmp(key->modulus,t->modulus,MAX_RSA_MODULUS_LEN))
  1994.  
  1995. return 1;
  1996.  
  1997. };
  1998.  
  1999. for (x = 0; x < g_pklist_pending.GetSize(); x ++) {
  2000.  
  2001. PKitem *pi=g_pklist_pending.Get(x);
  2002.  
  2003. R_RSA_PUBLIC_KEY *t=&pi->pk;
  2004.  
  2005. if (key->bits == t->bits &&
  2006.  
  2007. !memcmp(key->exponent,t->exponent,MAX_RSA_MODULUS_LEN) &&
  2008.  
  2009. !memcmp(key->modulus,t->modulus,MAX_RSA_MODULUS_LEN))
  2010.  
  2011. return 1;
  2012.  
  2013. };
  2014.  
  2015. if (key->bits == g_key.bits &&
  2016.  
  2017. !memcmp(key->exponent,g_key.publicExponent,MAX_RSA_MODULUS_LEN) &&
  2018.  
  2019. !memcmp(key->modulus,g_key.modulus,MAX_RSA_MODULUS_LEN))
  2020.  
  2021. return 1;
  2022.  
  2023.  
  2024.  
  2025. return 0; //no key found
  2026.  
  2027. }
  2028.  
  2029.  
  2030.  
  2031. char *findPublicKey(unsigned char *hash, R_RSA_PUBLIC_KEY *out)
  2032.  
  2033. {
  2034.  
  2035. int x;
  2036.  
  2037. for (x = 0; x < g_pklist.GetSize(); x ++) {
  2038.  
  2039. PKitem *pi=g_pklist.Get(x);
  2040.  
  2041. if (!memcmp(hash,pi->hash,SHA_OUTSIZE)) {
  2042.  
  2043. if (out) memcpy(out,&pi->pk,sizeof(R_RSA_PUBLIC_KEY));
  2044.  
  2045. return pi->name;
  2046.  
  2047. };
  2048.  
  2049. };
  2050.  
  2051. if (!memcmp(hash,g_pubkeyhash,SHA_OUTSIZE)) {
  2052.  
  2053. if (out) {
  2054.  
  2055. out->bits=g_key.bits;
  2056.  
  2057. memcpy(out->exponent,g_key.publicExponent,MAX_RSA_MODULUS_LEN);
  2058.  
  2059. memcpy(out->modulus,g_key.modulus,MAX_RSA_MODULUS_LEN);
  2060.  
  2061. };
  2062.  
  2063. return "(local)";
  2064.  
  2065. };
  2066.  
  2067.  
  2068.  
  2069. return NULL; //no key found
  2070.  
  2071. }
  2072.  
  2073.  
  2074.  
  2075. char *conspeed_strs[5]={"Modem","ISDN","Slow DSL/Cable","T1/Fast DSL/Cable","T3/LAN"};
  2076.  
  2077. int conspeed_speeds[5]={32,64,384,1600,20000};
  2078.  
  2079. int get_speedstr(int kbps, char *str)
  2080.  
  2081. {
  2082.  
  2083. int x;
  2084.  
  2085. for (x = 0; x < 5; x ++)
  2086.  
  2087. if (kbps <= conspeed_speeds[x]) break;
  2088.  
  2089. if (x == 5) x--;
  2090.  
  2091. if (str) strcpy(str,conspeed_strs[x]);
  2092.  
  2093. return x;
  2094.  
  2095. }
  2096.  
  2097.  
  2098.  
  2099. bool IPv4TestIpInMask(unsigned long addr,unsigned long subnet,unsigned long mask)
  2100.  
  2101. {
  2102.  
  2103. subnet&=mask;
  2104.  
  2105. addr&=mask;
  2106.  
  2107. return addr==subnet;
  2108.  
  2109. }
  2110.  
  2111.  
  2112.  
  2113. unsigned long IPv4Addr(unsigned char i1,unsigned char i2,unsigned char i3,unsigned char i4)
  2114.  
  2115. {
  2116.  
  2117. unsigned long t;
  2118.  
  2119. t=htonl(
  2120.  
  2121. (((unsigned long)i1)<<24)|
  2122.  
  2123. (((unsigned long)i2)<<16)|
  2124.  
  2125. (((unsigned long)i3)<< 8)|
  2126.  
  2127. (((unsigned long)i4)<< 0)
  2128.  
  2129. );
  2130.  
  2131. return t;
  2132.  
  2133. }
  2134.  
  2135.  
  2136.  
  2137. unsigned long IPv4NetMask(unsigned int networkbits)
  2138.  
  2139. {
  2140.  
  2141. unsigned long i;
  2142.  
  2143. if (!networkbits) return 0;
  2144.  
  2145. networkbits&=0x1f;
  2146.  
  2147. i=(unsigned long)-1;
  2148.  
  2149. i=i<<(32-(networkbits&31));
  2150.  
  2151. return htonl(i);
  2152.  
  2153. }
  2154.  
  2155.  
  2156.  
  2157. bool IPv4IsLoopback(unsigned long addr)
  2158.  
  2159. {
  2160.  
  2161. return IPv4TestIpInMask(addr,IPv4Addr(127, 0, 0, 0),IPv4NetMask( 8));
  2162.  
  2163. }
  2164.  
  2165.  
  2166.  
  2167. bool IPv4IsPrivateNet(unsigned long addr)
  2168.  
  2169. {
  2170.  
  2171. bool t;
  2172.  
  2173. t=IPv4TestIpInMask(addr,IPv4Addr( 10, 0, 0, 0),IPv4NetMask( 8));if (t) return true;
  2174.  
  2175. t=IPv4TestIpInMask(addr,IPv4Addr(172, 16, 0, 0),IPv4NetMask(12));if (t) return true;
  2176.  
  2177. t=IPv4TestIpInMask(addr,IPv4Addr(192,168, 0, 0),IPv4NetMask(16));if (t) return true;
  2178.  
  2179. return false;
  2180.  
  2181. }
  2182.  
  2183.  
  2184.  
  2185. #ifdef _WIN32
  2186.  
  2187. bool GetInterfaceInfoOnAddr(unsigned long addr,unsigned long &localaddr,unsigned long &localmask)
  2188.  
  2189. {
  2190.  
  2191. SOCKET s;
  2192.  
  2193. s=socket(AF_INET,SOCK_DGRAM,0);
  2194.  
  2195. if (s==INVALID_SOCKET){
  2196.  
  2197. log_printf(ds_Error,"Call to socket() failed with error!!");
  2198.  
  2199. localaddr=localmask=0;
  2200.  
  2201. return false;
  2202.  
  2203. };
  2204.  
  2205.  
  2206.  
  2207. INTERFACE_INFO ii[64];
  2208.  
  2209. DWORD len;
  2210.  
  2211. if (WSAIoctl(s,SIO_GET_INTERFACE_LIST,NULL,0,&ii,sizeof(ii),&len,NULL,NULL)==SOCKET_ERROR) {
  2212.  
  2213. log_printf(ds_Error,"Call to WSAIoctl() failed with error!!");
  2214.  
  2215. localaddr=localmask=0;
  2216.  
  2217. closesocket(s);
  2218.  
  2219. return false;
  2220.  
  2221. };
  2222.  
  2223.  
  2224.  
  2225. len=len/sizeof(ii[0]);
  2226.  
  2227. unsigned long laddr=0;
  2228.  
  2229. unsigned long lmask=0;
  2230.  
  2231. bool ret=false;
  2232.  
  2233. DWORD i;
  2234.  
  2235. for (i=0;i<len;i++) {
  2236.  
  2237. if ((ii[i].iiFlags & IFF_UP)!=0) {
  2238.  
  2239. if (ii[i].iiAddress.Address.sa_family==AF_INET) {
  2240.  
  2241. laddr=ii[i].iiAddress.AddressIn.sin_addr.s_addr;
  2242.  
  2243. lmask=ii[i].iiNetmask.AddressIn.sin_addr.s_addr;
  2244.  
  2245. if (IPv4TestIpInMask(addr,laddr,lmask)) {
  2246.  
  2247. ret=true;
  2248.  
  2249. break;
  2250.  
  2251. };
  2252.  
  2253. };
  2254.  
  2255. };
  2256.  
  2257. };
  2258.  
  2259. if (!ret) {
  2260.  
  2261. laddr=lmask=0;
  2262.  
  2263. };
  2264.  
  2265. localaddr=laddr;
  2266.  
  2267. localmask=lmask;
  2268.  
  2269. closesocket(s);
  2270.  
  2271. return false;
  2272.  
  2273. }
  2274.  
  2275. #else
  2276.  
  2277. bool GetInterfaceInfoOnAddr(unsigned long addr,unsigned long &localaddr,unsigned long &localmask)
  2278.  
  2279. {
  2280.  
  2281. struct ifaddrs *ifaces, *ifa;
  2282.  
  2283.  
  2284.  
  2285. if (getifaddrs(&ifaces) < 0)
  2286.  
  2287. {
  2288.  
  2289. if (errno != ENOSYS)
  2290.  
  2291. {
  2292.  
  2293. log_printf(ds_Error,"Call to getifaddrs() failed with error!!");
  2294.  
  2295. localaddr=localmask=0;
  2296.  
  2297. return false;
  2298.  
  2299. }
  2300.  
  2301. else {
  2302.  
  2303. log_printf(ds_Error,"Call to getifaddrs() not implemented!! Huh?");
  2304.  
  2305. localaddr=localmask=0;
  2306.  
  2307. return false;
  2308.  
  2309. };
  2310.  
  2311. };
  2312.  
  2313.  
  2314.  
  2315. unsigned long laddr=0;
  2316.  
  2317. unsigned long lmask=0;
  2318.  
  2319. bool ret=false;
  2320.  
  2321. for (ifa = ifaces; ifa != NULL; ifa = ifa->ifa_next)
  2322.  
  2323. {
  2324.  
  2325. if ((ifa->ifa_flags & IFF_UP)!=0 && ifa->ifa_addr != NULL)
  2326.  
  2327. {
  2328.  
  2329. if (ifa->ifa_addr->sa_family==AF_INET) {
  2330.  
  2331. sockaddr_in *in;
  2332.  
  2333. in=(sockaddr_in*)(ifa->ifa_addr);
  2334.  
  2335. unsigned long laddr=in->sin_addr.s_addr;
  2336.  
  2337. in=(sockaddr_in*)(ifa->ifa_netmask);
  2338.  
  2339. unsigned long lmask=in->sin_addr.s_addr;
  2340.  
  2341. if (IPv4TestIpInMask(addr,laddr,lmask)) {
  2342.  
  2343. ret=true;
  2344.  
  2345. break;
  2346.  
  2347. };
  2348.  
  2349. };
  2350.  
  2351. };
  2352.  
  2353. };
  2354.  
  2355. if (!ret) {
  2356.  
  2357. laddr=lmask=0;
  2358.  
  2359. };
  2360.  
  2361. localaddr=laddr;
  2362.  
  2363. localmask=lmask;
  2364.  
  2365. freeifaddrs(ifaces);
  2366.  
  2367. return false;
  2368.  
  2369. }
  2370.  
  2371. #endif
  2372.  
  2373.  
  2374.  
  2375. bool is_accessable_addr(unsigned long addr)
  2376.  
  2377. {
  2378.  
  2379. if (g_use_accesslist) return true;
  2380.  
  2381.  
  2382.  
  2383. if (addr==0) return false;
  2384.  
  2385. if (addr==INADDR_NONE) return false;
  2386.  
  2387. if (IPv4IsLoopback(addr)) return false;
  2388.  
  2389. if (!IPv4IsPrivateNet(addr)) return true;
  2390.  
  2391.  
  2392.  
  2393. //if (!g_mql->GetNumQueues()) return true;
  2394.  
  2395.  
  2396.  
  2397. unsigned long localaddr;
  2398.  
  2399. unsigned long localmask;
  2400.  
  2401. if (GetInterfaceInfoOnAddr(addr,localaddr,localmask)) {
  2402.  
  2403. if (addr!=localaddr) {
  2404.  
  2405. return true;
  2406.  
  2407. };
  2408.  
  2409. };
  2410.  
  2411. return false;
  2412.  
  2413. }
  2414.  
  2415.  
  2416.  
  2417. #if defined(_WIN32)&&(!defined(_DEFINE_SRV))
  2418.  
  2419. void SetWndTitle(HWND hwnd, char *titlebase)
  2420.  
  2421. {
  2422.  
  2423. char buf[1024];
  2424.  
  2425. if (g_appendprofiletitles && g_profile_name[0] && strlen(titlebase)+strlen(g_profile_name)+3 < sizeof(buf)) {
  2426.  
  2427. sprintf(buf,"%s/%s",titlebase,g_profile_name);
  2428.  
  2429. SetWindowText(hwnd,buf);
  2430.  
  2431. }
  2432.  
  2433. else
  2434.  
  2435. SetWindowText(hwnd,titlebase);
  2436.  
  2437. }
  2438.  
  2439. #endif
  2440.  
  2441.  
  2442.  
  2443. static const char* dSevStArr[]=
  2444.  
  2445. {
  2446.  
  2447. "Console ", //ds_Console
  2448.  
  2449. "Critical", //ds_Critical
  2450.  
  2451. "Error ", //ds_Error
  2452.  
  2453. "Warning ", //ds_Warning
  2454.  
  2455. "Inform. ", //ds_Informational
  2456.  
  2457. "Debug " //ds_Debug
  2458.  
  2459. };
  2460.  
  2461.  
  2462.  
  2463. const char* sevString(dSeverity sev)
  2464.  
  2465. {
  2466.  
  2467. static const char dummy[]="unknown!";
  2468.  
  2469. if ((sev >= 1) && (sev <= (int) sizeof(dSevStArr))) {
  2470.  
  2471. return dSevStArr[sev-1];
  2472.  
  2473. }
  2474.  
  2475. else {
  2476.  
  2477. return dummy;
  2478.  
  2479. };
  2480.  
  2481. };
  2482.  
  2483.  
  2484.  
  2485. void CLogfile::operator()(dSeverity sev,const char *text,...) const
  2486.  
  2487. {
  2488.  
  2489. if ((g_log_level>0)&&(sev<=g_log_level)) {
  2490.  
  2491. static int logidx;
  2492.  
  2493. static const int buflen=2048;
  2494.  
  2495. char str[buflen],*strc;
  2496.  
  2497. int len2;
  2498.  
  2499. time_t t;struct tm *tm;
  2500.  
  2501. t = time(NULL);
  2502.  
  2503. tm = localtime(&t);
  2504.  
  2505.  
  2506.  
  2507. va_list list;
  2508.  
  2509. va_start(list,text);
  2510.  
  2511.  
  2512.  
  2513. strc=str;strc[0]=0;
  2514.  
  2515. sprintf(strc,"(%i)",(int)sev);strc+=strlen(strc);
  2516.  
  2517. strcpy(strc,sevString(sev));strc+=strlen(strc);
  2518.  
  2519. strcpy(strc,":");strc+=1;
  2520.  
  2521. if (tm) sprintf(strc,"<%02d/%02d/%02d %02d:%02d:%02d:%d> ",tm->tm_year%100, tm->tm_mon+1, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec, logidx++);
  2522.  
  2523. else strcpy(strc,"<" "????" "> "); /* avoid interpreting as trigraphs! */
  2524.  
  2525.  
  2526.  
  2527. #if defined(_DEBUG) || defined(_DEFINE_WXUI)
  2528.  
  2529. strc+=strlen(strc);
  2530.  
  2531. strcpy(strc,"{");strc+=1;
  2532.  
  2533. strcpy(strc,m_szFilename);strc+=strlen(strc);
  2534.  
  2535. strcpy(strc,"(");strc+=1;
  2536.  
  2537. sprintf(strc,"%u",m_line);strc+=strlen(strc);
  2538.  
  2539. strcpy(strc,")");strc+=1;
  2540.  
  2541. strcpy(strc,"}");strc+=1;
  2542.  
  2543. strcpy(strc," ");
  2544.  
  2545. #endif
  2546.  
  2547.  
  2548.  
  2549. len2=strlen(str);
  2550.  
  2551. strc=str+len2;
  2552.  
  2553. if (len2<buflen) {
  2554.  
  2555. _vsnprintf(strc,buflen-len2,text,list);
  2556.  
  2557. strc+=strlen(strc);
  2558.  
  2559. };
  2560.  
  2561. va_end(list);
  2562.  
  2563.  
  2564.  
  2565. if (_logfile) {
  2566.  
  2567. fprintf(_logfile,"%s\n",str);
  2568.  
  2569. if (g_log_flush_auto) fflush(_logfile);
  2570.  
  2571. #if defined(_WIN32)&&(defined(_DEBUG))
  2572.  
  2573. static const char* _ret="\n";
  2574.  
  2575. len2=strc-str+strlen(_ret);
  2576.  
  2577. if (len2<buflen) {
  2578.  
  2579. strcpy(strc,_ret);strc+=strlen(strc);
  2580.  
  2581. OutputDebugString(str);
  2582.  
  2583. };
  2584.  
  2585. #endif
  2586.  
  2587. };
  2588.  
  2589. };
  2590.  
  2591. }
  2592.  
  2593.  
  2594.  
  2595. bool log_UpdatePath(const char *logpath, bool bIsFilename)
  2596.  
  2597. {
  2598.  
  2599. if (_logfile) {
  2600.  
  2601. if((_logfile!=stderr)&&(_logfile!=stdout)) {
  2602.  
  2603. fflush(_logfile);
  2604.  
  2605. fclose(_logfile);
  2606.  
  2607. };
  2608.  
  2609. _logfile=NULL;
  2610.  
  2611. };
  2612.  
  2613. if ((!logpath)||(!logpath[0])) return true;
  2614.  
  2615.  
  2616.  
  2617. static const int buflen=2048;
  2618.  
  2619. static const char slfn[]="Wastelog-";
  2620.  
  2621. static const char slext[]=".log";
  2622.  
  2623. char np[buflen];np[0]=0;
  2624.  
  2625. char *npc=np;
  2626.  
  2627. int len=buflen-1;
  2628.  
  2629. int tl;
  2630.  
  2631. FILE* tf;
  2632.  
  2633.  
  2634.  
  2635. tl=strlen(logpath);
  2636.  
  2637. if (tl>len) return false;
  2638.  
  2639. strcat(npc,logpath);len-=tl;
  2640.  
  2641. npc+=tl;
  2642.  
  2643.  
  2644.  
  2645. if (!bIsFilename) {
  2646.  
  2647. tl=1;
  2648.  
  2649. if (tl>len) return false;
  2650.  
  2651. *npc++=DIRCHAR;*npc=0;len-=tl;
  2652.  
  2653.  
  2654.  
  2655. tl=strlen(slfn);
  2656.  
  2657. if (tl>len) return false;
  2658.  
  2659. strcat(npc,slfn);len-=tl;
  2660.  
  2661. npc+=tl;
  2662.  
  2663.  
  2664.  
  2665. tl=strlen(g_profile_name);
  2666.  
  2667. if (tl>len) return false;
  2668.  
  2669. strcat(npc,g_profile_name);len-=tl;
  2670.  
  2671. npc+=tl;
  2672.  
  2673.  
  2674.  
  2675. tl=strlen(slext);
  2676.  
  2677. if (tl>len) return false;
  2678.  
  2679. strcat(npc,slext);len-=tl;
  2680.  
  2681. npc+=tl;
  2682.  
  2683. };
  2684.  
  2685.  
  2686.  
  2687. tf=fopen(np,"a");
  2688.  
  2689. if (!tf) return false;
  2690.  
  2691. _logfile=tf;
  2692.  
  2693. return true;
  2694.  
  2695. }
  2696.  
  2697.  
  2698.  
  2699. void update_forceip_dns_resolution()
  2700.  
  2701. {
  2702.  
  2703. if (g_forceip_dynip_mode==1) {
  2704.  
  2705. unsigned long ipold=g_forceip_dynip_addr;
  2706.  
  2707. unsigned long ip=inet_addr(g_forceip_name);
  2708.  
  2709. if (ip==INADDR_NONE) {
  2710.  
  2711. hostent* ent = gethostbyname(g_forceip_name);
  2712.  
  2713. if (ent) {
  2714.  
  2715. g_forceip_dynip_addr = *(long*)(ent->h_addr_list[0]);
  2716.  
  2717. }
  2718.  
  2719. else {
  2720.  
  2721. g_forceip_dynip_addr=INADDR_NONE;
  2722.  
  2723. };
  2724.  
  2725. }
  2726.  
  2727. else {
  2728.  
  2729. if (ip==0) ip=INADDR_NONE;
  2730.  
  2731. g_forceip_dynip_addr=ip;
  2732.  
  2733. };
  2734.  
  2735. if (ipold!=g_forceip_dynip_addr) {
  2736.  
  2737. char *ad1,ad2[64];
  2738.  
  2739. struct in_addr in;
  2740.  
  2741. in.s_addr=g_forceip_dynip_addr;
  2742.  
  2743. ad1=inet_ntoa(in);
  2744.  
  2745. safe_strncpy(ad2,ad1,64);
  2746.  
  2747. in.s_addr=ipold;
  2748.  
  2749. ad1=inet_ntoa(in);
  2750.  
  2751. log_printf(ds_Informational,"Ip Mode(force): Resolved new ip. Old Ip: %s -> New Ip: %s",ad1,ad2);
  2752.  
  2753. };
  2754.  
  2755. };
  2756.  
  2757. }
  2758.  
  2759.  
  2760.  
  2761. //ADDED Md5Chap Moved from srchwnd coz need in server for dbg
  2762.  
  2763. void FormatSizeStr64(char *out, unsigned int low, unsigned int high)
  2764.  
  2765. {
  2766.  
  2767. if (high) {
  2768.  
  2769. sprintf(out,"%u GB",high*4 + (low>>30));
  2770.  
  2771. }
  2772.  
  2773. else {
  2774.  
  2775. if (low < 1024*1024) sprintf(out,"%u.%u KB",low>>10,((low*10)>>10)%10);
  2776.  
  2777. else if (low < 256*1024*1024) sprintf(out,"%u.%u MB",low>>20,((low*10)>>20)%10);
  2778.  
  2779. else if (low < 1024*1024*1024) sprintf(out,"%u MB",low>>20);
  2780.  
  2781. else {
  2782.  
  2783. low>>=20;
  2784.  
  2785. sprintf(out,"%u.%u GB",low>>10,((low%1024)*10)/1024);
  2786.  
  2787. };
  2788.  
  2789. };
  2790.  
  2791. }
  2792.  
  2793.  
  2794.  
  2795. // return is position of (c) or NULL if not found
  2796.  
  2797. // maxlen is bufferlen
  2798.  
  2799. const char* GetNextOf(const char* buf,char c,int maxlen)
  2800.  
  2801. {
  2802.  
  2803. int tl=maxlen;
  2804.  
  2805. for (;;) {
  2806.  
  2807. if (tl<=0) {
  2808.  
  2809. return NULL;
  2810.  
  2811. };
  2812.  
  2813. if (*buf==c) {
  2814.  
  2815. return buf;
  2816.  
  2817. };
  2818.  
  2819. buf++;tl--;
  2820.  
  2821. };
  2822.  
  2823. }
  2824.  
  2825.  
  2826.  
  2827. // return value is position of (stopchar)+1 or NULL if not found
  2828.  
  2829. // len are bufferlen
  2830.  
  2831. // autolen is decremented by return-source
  2832.  
  2833. const char* CopySingleToken(char* dest,const char* source,char stopchar,int destLen,int sourceLen, int* autolen)
  2834.  
  2835. {
  2836.  
  2837. int maxlen=waste_min(destLen,sourceLen);
  2838.  
  2839.  
  2840.  
  2841. if (maxlen<=0) {
  2842.  
  2843. if (destLen>0) dest[0]=0;
  2844.  
  2845. return NULL;
  2846.  
  2847. };
  2848.  
  2849.  
  2850.  
  2851. const char *t=GetNextOf(source,stopchar,sourceLen);
  2852.  
  2853. if (t) {
  2854.  
  2855. int clen=t-source+1;
  2856.  
  2857. if (clen > destLen) clen=destLen;
  2858.  
  2859. memcpy(dest,source,clen-1);
  2860.  
  2861. dest[clen-1]=0;
  2862.  
  2863. if (autolen) *autolen=*autolen-clen;
  2864.  
  2865. return t+1;
  2866.  
  2867. }
  2868.  
  2869. else {
  2870.  
  2871. memcpy(dest,source,maxlen-1);
  2872.  
  2873. dest[maxlen-1]=0;
  2874.  
  2875. if (autolen) *autolen=0;
  2876.  
  2877. return NULL;
  2878.  
  2879. };
  2880.  
  2881. }
  2882.  
  2883.  
  2884.  
  2885. //len must be integral
  2886.  
  2887. void GenerateRandomSeed(void* target,unsigned int len,R_RANDOM_STRUCT *random)
  2888.  
  2889. {
  2890.  
  2891. //according to X9.17, afaik.
  2892.  
  2893. //ADDED md5chap using BFCBC for Ivector
  2894.  
  2895. //x9.17 uses 3des originally
  2896.  
  2897.  
  2898.  
  2899. CBlowfish bl;
  2900.  
  2901.  
  2902.  
  2903. unsigned char tmpkey[8];
  2904.  
  2905.  
  2906.  
  2907. R_GenerateBytes((unsigned char *)target,len,random);
  2908.  
  2909. R_GenerateBytes(tmpkey,sizeof(tmpkey),random);
  2910.  
  2911.  
  2912.  
  2913. bl.Init(tmpkey, sizeof(tmpkey));
  2914.  
  2915. memset(&tmpkey,0,sizeof(tmpkey));
  2916.  
  2917.  
  2918.  
  2919. unsigned long tm[2]={time(NULL),GetTickCount()};
  2920.  
  2921. bl.EncryptECB(tm,sizeof(tm));
  2922.  
  2923.  
  2924.  
  2925. bl.SetIV(CBlowfish::IV_ENC,tm);
  2926.  
  2927. memset(&tm,0,sizeof(tmpkey));
  2928.  
  2929.  
  2930.  
  2931. bl.EncryptCBC(target, len);
  2932.  
  2933.  
  2934.  
  2935. bl.Final();
  2936.  
  2937. }
  2938.  
  2939.  
  2940.  
  2941. // you need double size array if converting to crlf
  2942.  
  2943. // buflen Specifies the target size!
  2944.  
  2945. bool str_return_unpack(char *dst,const char* src,unsigned int dstbuflen,const char returnChar)
  2946.  
  2947. {
  2948.  
  2949. if (dstbuflen<=0) return false;
  2950.  
  2951. const char *p1;
  2952.  
  2953. char *p2;
  2954.  
  2955. p1=src;
  2956.  
  2957. p2=dst;
  2958.  
  2959. while (*p1) {
  2960.  
  2961. if (((unsigned int)(p2-dst))>=(dstbuflen-1)) {
  2962.  
  2963. *p2=0;
  2964.  
  2965. return false;
  2966.  
  2967. };
  2968.  
  2969. if (*p1==returnChar) {
  2970.  
  2971. *p2++=0x0d;*p2++=0x0a;
  2972.  
  2973. p1++;
  2974.  
  2975. }
  2976.  
  2977. else {
  2978.  
  2979. *p2++=*p1++;
  2980.  
  2981. };
  2982.  
  2983. };
  2984.  
  2985. *p2=0;
  2986.  
  2987. return true;
  2988.  
  2989. };
  2990.  
  2991.  
  2992.  
  2993. // you need double size array if converting to crlf
  2994.  
  2995. // buflen Specifies the target size!
  2996.  
  2997. // automatic removal of leading/tailing returns
  2998.  
  2999. bool str_return_pack(char *dst,const char* src,unsigned int dstbuflen,const char returnChar)
  3000.  
  3001. {
  3002.  
  3003. if (dstbuflen<=0) return false;
  3004.  
  3005. const char *p1;
  3006.  
  3007. char *p2, *p3;
  3008.  
  3009. bool rin=true;
  3010.  
  3011. p1=src;
  3012.  
  3013. p3=p2=dst;
  3014.  
  3015. while (*p1) {
  3016.  
  3017. if (((unsigned int)(p2-dst))>=(dstbuflen-1)) {
  3018.  
  3019. *p2=0;
  3020.  
  3021. return false;
  3022.  
  3023. };
  3024.  
  3025. if (*p1==0x0d) {
  3026.  
  3027. p1++;
  3028.  
  3029. }
  3030.  
  3031. else if (*p1==0x0a){
  3032.  
  3033. if (!rin) *p2++=returnChar;
  3034.  
  3035. p1++;
  3036.  
  3037. }
  3038.  
  3039. else {
  3040.  
  3041. *p2++=*p1++;
  3042.  
  3043. p3=p2;
  3044.  
  3045. rin=false;
  3046.  
  3047. };
  3048.  
  3049. };
  3050.  
  3051. *p3=0;
  3052.  
  3053. return true;
  3054.  
  3055. };
  3056.  
  3057.  
  3058.  
  3059. void RandomizePadding(void* buf,unsigned int bufsize,unsigned int datasize)
  3060.  
  3061. {
  3062.  
  3063. if (!VERIFY(bufsize>=datasize)) return;
  3064.  
  3065. unsigned int len=bufsize-datasize;
  3066.  
  3067. if (len==0) return;
  3068.  
  3069. unsigned char* cbuf=(unsigned char* )buf;
  3070.  
  3071. cbuf+=datasize;
  3072.  
  3073. R_GenerateBytes(cbuf, len, &g_random);
  3074.  
  3075. }
  3076.  
  3077.  
  3078.  
  3079. void RelpaceCr(char* st)
  3080.  
  3081. {
  3082.  
  3083. while (*st) {
  3084.  
  3085. if (*st=='\r') *st=' ';
  3086.  
  3087. st++;
  3088.  
  3089. };
  3090.  
  3091. }
  3092.  
  3093.  
  3094.  
  3095. //ADDED Md5Chap
  3096.  
  3097. //Hint: has is mandatory, strings optional, length=16+40+32=88
  3098.  
  3099. void MakeUserStringFromHash(unsigned char *hash,char* longstring, char*shortstring)
  3100.  
  3101. {
  3102.  
  3103. char *t;
  3104.  
  3105. bool nouser=false;
  3106.  
  3107. if (((t=findPublicKey(hash,NULL))!=0) && *t) {
  3108.  
  3109. if (shortstring) safe_strncpy(shortstring,t,16);
  3110.  
  3111. if (longstring) safe_strncpy(longstring,t,16);
  3112.  
  3113. }
  3114.  
  3115. else {
  3116.  
  3117. if (shortstring) shortstring[0]=0;
  3118.  
  3119. if (longstring) longstring[0]=0;
  3120.  
  3121. nouser=true;
  3122.  
  3123. };
  3124.  
  3125. if (!nouser) {
  3126.  
  3127. if (shortstring) strcat(shortstring," (");
  3128.  
  3129. if (longstring) strcat(longstring," (");
  3130.  
  3131. };
  3132.  
  3133. if (longstring) Bin2Hex(longstring+strlen(longstring), hash, SHA_OUTSIZE);
  3134.  
  3135. if (shortstring) {
  3136.  
  3137. Bin2Hex(shortstring+strlen(shortstring), hash, 4 );
  3138.  
  3139. strcat(shortstring,"...");
  3140.  
  3141. };
  3142.  
  3143. if (!nouser) {
  3144.  
  3145. if (shortstring) strcat(shortstring,")");
  3146.  
  3147. if (longstring) strcat(longstring,")");
  3148.  
  3149. };
  3150.  
  3151. };
  3152.  
  3153.  
  3154.  
  3155. //ADDED Md5Chap
  3156.  
  3157. #if (defined(_WIN32) && defined(_CHECK_RSA_BLINDING))
  3158.  
  3159. //----------------------------------------------------------------------------------
  3160.  
  3161. #define N 60
  3162.  
  3163. #define _UQ0 1
  3164.  
  3165. #define _UQ1 1
  3166.  
  3167. #define _UN1 1
  3168.  
  3169. #define _BP0 1
  3170.  
  3171. #define _BP1 1
  3172.  
  3173. #define _BN1 1
  3174.  
  3175. //----------------------------------------------------------------------------------
  3176.  
  3177. static unsigned long CheckRsa_Single(int enable,int n,unsigned char *inp,bool blind)
  3178.  
  3179. {
  3180.  
  3181. unsigned char out[4096/8];
  3182.  
  3183. unsigned int outlen;
  3184.  
  3185. int ret;
  3186.  
  3187. if (enable) {
  3188.  
  3189. WasteSleep(0);
  3190.  
  3191. DWORD tick=GetTickCount();
  3192.  
  3193. int i;
  3194.  
  3195. for (i=0;i<n;i++) {
  3196.  
  3197. outlen=0;
  3198.  
  3199. ret=RSAPrivateDecrypt(out, &outlen, inp, (g_key.bits + 7) / 8, &g_key, blind?&g_random:NULL);
  3200.  
  3201. };
  3202.  
  3203. tick=GetTickCount()-tick;
  3204.  
  3205. return tick;
  3206.  
  3207. };
  3208.  
  3209. return 0;
  3210.  
  3211. }
  3212.  
  3213.  
  3214.  
  3215. void CheckRsaBlinding()
  3216.  
  3217. {
  3218.  
  3219. NN_DIGIT q_0[MAX_NN_DIGITS];
  3220.  
  3221. NN_DIGIT q_1[MAX_NN_DIGITS];
  3222.  
  3223. NN_DIGIT n_1[MAX_NN_DIGITS];
  3224.  
  3225. NN_DIGIT one[MAX_NN_DIGITS];
  3226.  
  3227.  
  3228.  
  3229. unsigned char cq0[4096/8];
  3230.  
  3231. unsigned char cq1[4096/8];
  3232.  
  3233. unsigned char cn1[4096/8];
  3234.  
  3235. char buf[4096];
  3236.  
  3237. int pDigits;
  3238.  
  3239. int nDigits;
  3240.  
  3241.  
  3242.  
  3243. if (g_key.bits>0) {
  3244.  
  3245. NN_Decode(q_0, MAX_NN_DIGITS, g_key.prime[1], MAX_RSA_PRIME_LEN);
  3246.  
  3247. pDigits=NN_Digits(q_0,MAX_NN_DIGITS);
  3248.  
  3249. NN_Decode(n_1, MAX_NN_DIGITS, g_key.modulus, MAX_RSA_MODULUS_LEN);
  3250.  
  3251. nDigits=NN_Digits(n_1,MAX_NN_DIGITS);
  3252.  
  3253.  
  3254.  
  3255. NN_ASSIGN_DIGIT(one, 1, MAX_NN_DIGITS);
  3256.  
  3257.  
  3258.  
  3259. NN_Sub(q_1,q_0,one,MAX_NN_DIGITS);
  3260.  
  3261. NN_Add(q_0,q_0,one,MAX_NN_DIGITS);
  3262.  
  3263. NN_Sub(n_1,n_1,one,MAX_NN_DIGITS);
  3264.  
  3265.  
  3266.  
  3267. NN_Encode(cq0, (g_key.bits + 7) / 8, q_0, pDigits);
  3268.  
  3269. NN_Encode(cq1, (g_key.bits + 7) / 8, q_1, pDigits);
  3270.  
  3271. NN_Encode(cn1, (g_key.bits + 7) / 8, n_1, nDigits);
  3272.  
  3273.  
  3274.  
  3275. //------------------------------
  3276.  
  3277. int n=N;
  3278.  
  3279. unsigned long tick1u=CheckRsa_Single(_UQ0,n,cq0,false);
  3280.  
  3281. unsigned long tick2u=CheckRsa_Single(_UQ1,n,cq1,false);
  3282.  
  3283. unsigned long tick3u=CheckRsa_Single(_UN1,n,cn1,false);
  3284.  
  3285. unsigned long tick1b=CheckRsa_Single(_BP0,n,cq0,true);
  3286.  
  3287. unsigned long tick2b=CheckRsa_Single(_BP1,n,cq1,true);
  3288.  
  3289. unsigned long tick3b=CheckRsa_Single(_BN1,n,cn1,true);
  3290.  
  3291. //------------------------------
  3292.  
  3293. double dperitem1u=(double)tick1u/(double)n;
  3294.  
  3295. double dperitem2u=(double)tick2u/(double)n;
  3296.  
  3297. double dperitem3u=(double)tick3u/(double)n;
  3298.  
  3299. double dperitemavgu=((tick1u>0)?1:0)+((tick2u>0)?1:0)+((tick3u>0)?1:0);
  3300.  
  3301. double dfactoru=tick1u;
  3302.  
  3303. if (dperitemavgu>0) dperitemavgu=(dperitem1u+dperitem2u+dperitem3u)/dperitemavgu;
  3304.  
  3305. if (dfactoru!=0) dfactoru=(double)tick2u/dfactoru;
  3306.  
  3307. //------------------------------
  3308.  
  3309. double dperitem1b=(double)tick1b/(double)n;
  3310.  
  3311. double dperitem2b=(double)tick2b/(double)n;
  3312.  
  3313. double dperitem3b=(double)tick3b/(double)n;
  3314.  
  3315. double dperitemavgb=((tick1b>0)?1:0)+((tick2b>0)?1:0)+((tick3b>0)?1:0);
  3316.  
  3317. double dfactorb=tick1b;
  3318.  
  3319. if (dperitemavgb>0) dperitemavgb=(dperitem1b+dperitem2b+dperitem3b)/dperitemavgb;
  3320.  
  3321. if (dfactorb!=0) dfactorb=(double)tick2b/dfactorb;
  3322.  
  3323. //------------------------------
  3324.  
  3325. #pragma push_macro("sprintf")
  3326.  
  3327. #undef sprintf
  3328.  
  3329. sprintf(buf,
  3330.  
  3331. "Rounds n: %i\n"
  3332.  
  3333. "-------------------\n"
  3334.  
  3335. "Normal:\n"
  3336.  
  3337. "TickCount All : %10i\n"
  3338.  
  3339. "TickCount Avg : %10.4f\n"
  3340.  
  3341. #if _UQ0
  3342.  
  3343. "TickCount(q+1) : %10i\n"
  3344.  
  3345. "TickCount(q+1)/n : %10.4f\n"
  3346.  
  3347. #endif
  3348.  
  3349. #if _UQ1
  3350.  
  3351. "TickCount(q-1) : %10i\n"
  3352.  
  3353. "TickCount(q-1)/n : %10.4f\n"
  3354.  
  3355. #endif
  3356.  
  3357. #if _UN1
  3358.  
  3359. "TickCount(n-1) : %10i\n"
  3360.  
  3361. "TickCount(n-1)/n : %10.4f\n"
  3362.  
  3363. #endif
  3364.  
  3365. "Tick(q-1)/Tick(q): %10.4f\n"
  3366.  
  3367. "-------------------\n"
  3368.  
  3369. "Blinded:\n"
  3370.  
  3371. "TickCount All : %10i\n"
  3372.  
  3373. "TickCount Avg : %10.4f\n"
  3374.  
  3375. #if _BP0
  3376.  
  3377. "TickCount(q+1) : %10i\n"
  3378.  
  3379. "TickCount(q+1)/n : %10.4f\n"
  3380.  
  3381. #endif
  3382.  
  3383. #if _BP1
  3384.  
  3385. "TickCount(q-1) : %10i\n"
  3386.  
  3387. "TickCount(q-1)/n : %10.4f\n"
  3388.  
  3389. #endif
  3390.  
  3391. #if _BN1
  3392.  
  3393. "TickCount(n-1) : %10i\n"
  3394.  
  3395. "TickCount(n-1)/n : %10.4f\n"
  3396.  
  3397. #endif
  3398.  
  3399. "Tick(q-1)/Tick(q): %10.4f\n",
  3400.  
  3401. n,
  3402.  
  3403. tick1u+tick2u+tick3u,
  3404.  
  3405. dperitemavgu,
  3406.  
  3407. #if _UQ0
  3408.  
  3409. tick1u,
  3410.  
  3411. dperitem1u,
  3412.  
  3413. #endif
  3414.  
  3415. #if _UQ1
  3416.  
  3417. tick2u,
  3418.  
  3419. dperitem2u,
  3420.  
  3421. #endif
  3422.  
  3423. #if _UN1
  3424.  
  3425. tick3u,
  3426.  
  3427. dperitem3u,
  3428.  
  3429. #endif
  3430.  
  3431. dfactoru,
  3432.  
  3433. tick1b+tick2b+tick3b,
  3434.  
  3435. dperitemavgb,
  3436.  
  3437. #if _BP0
  3438.  
  3439. tick1b,
  3440.  
  3441. dperitem1b,
  3442.  
  3443. #endif
  3444.  
  3445. #if _BP1
  3446.  
  3447. tick2b,
  3448.  
  3449. dperitem2b,
  3450.  
  3451. #endif
  3452.  
  3453. #if _BN1
  3454.  
  3455. tick3b,
  3456.  
  3457. dperitem3b,
  3458.  
  3459. #endif
  3460.  
  3461. dfactorb
  3462.  
  3463. );
  3464.  
  3465. #pragma pop_macro("sprintf")
  3466.  
  3467. MessageBox(0,buf,"Info",0);
  3468.  
  3469. };
  3470.  
  3471. }
  3472.  
  3473. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement