2009年6月8日 星期一

FreeTDS的加密傳輸(Secure Connection)

最近遇到的鐵板是使用FreeTDS連接SQL Server會出現模稜兩可的Msg 20017錯誤訊息,
Msg 20017, Level 9, State -1, Server OpenClient, Line -1
Unexpected EOF from the server

使用TDSDUMP看錯誤訊息後發現在傳輸建立初期就已經被中斷,
util.c:334:tdserror(0x997e0a8, 0x997ea50, 20017, 0)
util.c:368:tdserror: client library returned TDS_INT_CANCEL(2)

在來回比對之後才發現遇到了有加密(Force Encryption)的SQL Serve,

需以--with-openssl=/src/openssl --with-gnutls的設定重新安裝FreeTDS, 並且在FreeTDS的設定檔中加上 encryption = required或request的參數,

看似美好, 但重新安裝後仍遇到一樣的錯誤訊息, 再度以TDSDUMP檢查後發現訊息有所不同了, 是在送出表頭前出錯的...
net.c:779:Sending packet
0000 12 01 00 34 00 00 00 00-00 00 15 00 06 01 00 1b |...4.... ........|
0010 00 01 02 00 1c 00 0c 03-00 28 00 04 ff 08 00 01 |........ .(......|
0020 55 00 00 00 4d 53 53 51-4c 53 65 72 76 65 72 00 |U...MSSQ LServer.|
0030 f2 31 00 00 - |.1..|

原來因為一開始安裝機器時不想裝進一些亂七八糟的東西, 所以只選了確定要的套件, 因此少裝了某些加密模組, 最後還是只好拿出光碟片乖乖的把base的東西補齊,

終於可以快快樂樂的以FreeTDS進行加密傳輸了...

2009年5月19日 星期二

在Linux環境下以PHP連結SQL Server

若在Windows環境下安裝PHP, 通常可以很輕鬆的連結SQL Server, 但在Linux環境下就需要額外安裝FreeTDS, 詳細安裝步驟可以參考這篇這篇, 我也順便再記錄一次操作步驟吧...

1) 安裝FreeTDS:
  • 先下載FreeTDS的source,
  • 編譯參數: --prefix=/usr/local/freetds --enable-msdblib
  • 完成安裝後, 修改/etc/ld.so.conf, 加入/usr/local/freetds/lib; 以ldconfig重新載入函式
2) 修改/usr/local/freetds/etc/freetds.conf:
[foo]
host = your.mssql.server.ip
port = 1433
client charset = UTF-8 # 以UTF-8編碼方式連線
tds version = 8.0
#tds version: 4.2 (適用 MS SQL Server 6.x); 7.0 (適用 7.x); 8.0 (適用 2000/2005)
3) 測試連線
  • # /usr/local/freetds/bin/tsql -S foo -U sa
  • 1> use my_db
  • 2> select * from my_table
  • 3> go
4) 重新編譯PHP
  • 新增編譯參數: --with-mssql=/usr/local/freetds

在存取SQL Server 2005時, 若遇到欄位型態為uniqueidentifier, PHP無法直接取得其欄位值, 需在SQL查詢以SELECT CONVERT(VARCHAR(36), field_name) AS 'field_name'轉換型態才可使用, 而要新增uniqueidentifier類型的欄位時, 須以SQL Server內建之NewID()函式產生唯一的GUID

2009年5月12日 星期二

在Linux環境安裝Subversion

最近由於工作需要, 必須在Linux下安裝版本控制系統, 經過比較之後, 雖然Subversion(SVN)在commit的速度不如Git, 而且branch的管理也較差, 但用順手後也就習慣了, 在此紀錄一下安裝過程...
在Linux環境下先抓取Apache和SVN的source後編譯, 由於習慣使用webdav的方式透過http更新, 因此在Apache編譯前要先設定
# ./configure --enable-dav --enable-so --enable-maintainer-mode

根據Apache2.0x的預設安裝路徑, 繼續SVN的編譯
# ./configure --with-apxs=/usr/local/apache2/bin/apxs --with-apr=/usr/local/apr/bin/

在安裝SVN的過程中有時會出現'configure: error: Subversion requires SQLite'的錯誤訊息, 這時就須先下載sqlite-amalgamation, 解開壓縮檔後將sqlite3.c複製到subversion-x.x.x/sqlite-amalgamation的目錄下再行安裝,

安裝完成後開始建立Repository, 注意不要建立在web server文件下的目錄,
# svnadmin create /var/svn

有時也會遇到'txn-current-lock': Permission denied的問題, 就必須修改權限給Apache的使用群組(可見httpd.conf)
# chown -R daemon:daemon /var/svn

修改httpd.conf設定,
# vim /usr/local/apache2/conf/httpd.conf
[add]
<location /myprj>
Dav svn
SVNPath /var/svn/myprj
AuthType Basic
AuthName "Subversion Repository"
AuthUserFile /usr/local/apache2/conf/auth-file
Require valid-user
<//Location>
[/add]

加上Apache基本認證
# /usr/local/apache2/bin/htpasswd -c /usr/local/apache2/conf/auth-file USERNAME

確認是否成功, 重新啟動Apache
# /usr/local/apache2/bin/apachectl restart

開啟瀏覽器輸入以下網址
http://127.0.0.1/myprj

如果在登入帳號密碼後看到下列訊息, 表示成功安裝以webdav存取的的svn server
myprj - Revision 0: /

進一步的設定可參考Subversion系統, Subversion FAQVersion Control with Subversion, 而在Window環境下安裝可參考這裡

2009年3月20日 星期五

The Difference Between include() and require() in PHP

When the handle are failed,
include() will produce a Warning and the script will continue regardless.
require() results in a Fatal Error and halts the script.

2009年2月28日 星期六

Install VMware Tools on Linux

# yum install gcc kernel-headers kernel-devel

How to fix the problem when "What is the location of the directory of C header files that match your running kernel" error occurred?

The location of the directory of C header files is: /usr/src/kernels/`uname -r`/include/

If the error message was like
The directory of kernel headers (version @@VMWARE@@ UTS_RELEASE) does not match your running kernel...

The vmware-config-tools.pl is looking for define UTS_RELEASE within either version.sh or utsrelease.h under ./linux/ Unfortunately it wasn't there yet.
# cd /usr/src/kernels/`uname -r`/include
# cp -p generated/utsrelease.h linux/utsrelease.h

In the environment of VMWare Workstation 6.5.4 with Linux kernel 2.6.33.*, there is somehow difficult... If the "Virtual Network Device" was compiled failed in "Vmware Kernel Module Updater", the steps below were worked for me.
cd /usr/src/kernels/`uname -r`/include/linux/
# ln -sf ../generated/autoconf.h .
# ln -sf ../generated/utsrelease.h .
# tar xf /usr/lib/vmware/modules/source/vmnet.tar
# cd vmnet-only
In vnetUserListener,c, add the line:
#include
after
#include "vnetInt.h"
# cd ..
# tar cf vmnet.tar vmnet-only
# mv vmnet.tar /usr/lib/vmware/modules/source/
# tar xf /usr/lib/vmware/modules/source/vmci.tar
# cd vmci-only/include
In pgtbl.h, add the line:
#include
after
#include "compat_page.h"
# cd ../..
# tar cf vmci.tar vmci-only
# mv vmci.tar /usr/lib/vmware/modules/source/

reference:
VMWare Workstation 6.5.4 with Linux kernel 2.6.33.4
VMware Tools on 64-bit Ubuntu 9.10
圖文並茂,VMware 6.5.1for linux下載安裝、註冊機、VM tools及虛擬系統應用安裝教程

[轉錄]Enable Multiple Concurrent Remote Desktop Connections in Windows XP

1.Download the patched termsrv.dll:
Windows XP RTM, SP1 and SP2: termsrv.dll (version 5.1.2600.2055)
Windows XP SP2: termsrv.dll (version 5.1.2600.2180)
Windows XP SP3: termsrv.dll (version 5.1.2600.5512)

2.In safe mode, replace the patched termsrv.dll in the %windir%\System32, %windir%\ServicePackFiles\i386 and %windir%\System32\dllcach folder

3.Modify/Add the registry as follows:
[HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Control\Terminal Server\Licensing Core]
"EnableConcurrentSessions"=dword:00000001
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon]
"EnableConcurrentSessions"=dword:00000001
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon]
"AllowMultipleTSSessions"=dword:00000001

4.Modify the Group Policy Editor by gpedit.msc:
Computer Configuration -> Administrative Templates -> Windows Components -> Terminal Services -> Limit Number of Connections = 5
Computer Configuration -> Administrative Templates -> Windows Components -> Terminal Services -> Allow users to connect remotely to this computer = enabled
Fast User Switching in Control Panel -> User Accounts -> Change the way users log on or off = enabled

5.Restart and enjoy it

see here

Linux Trouble Shooting

忘記密碼:
1.重新開機後, 按ESC進入開機管理程式(GRUB)
2.輸入e進入編輯模式
3.選擇 kernel /boot/vmlinuz 這行
4.輸入e進入編輯模式
5.輸入runlevel (若要修改密碼, 輸入1或輸入single), 按下enter退出編輯模式
6.輸入b並重開機則進入single user mode without password

MBR壞掉:
1.使用rescue光碟開機: linux rescue
2.grub-install /DEVICE ( 例如: /dev/hda )
3.若 /sbin/grub: not found.
4.進入grub模式: grub
5.grub> root (hd0, 0) # (hd0, 0) 第二個0是linux boot partition mark
6.grub> setup (hd0)
7.grub> quit

/etc/fstab壞掉
1.使用single user mode登入系統
2.mount -n -o remount,rw /
3.重新修改/etc/fstab