Category Archive電腦資訊



作業系統 dada on 四月 18, 2008

Bypass the 2GB file size limit on 32-bit Linux

Bypass the 2GB file size limit on 32-bit Linux (在 Linux 上面突破 2GB 的檔案大小限制)

在 32 位元的 Linux 上面寫超過 2GB 的檔案會發生錯誤,甚至導致程式終止執行

這是因為 Linux 的系統內部處理檔案時用的指標定義為 long,而 long 在 32 位元的系統上的大小為 32 位元,因此最大只能支援 2^31-1 = 2,147,483,647 bytes 等於是 2GB 扣掉 1 byte 的檔案大小

64 位元的系統 (例如 AMD64IA64) 則因為 long 定義成 64 位元,所以不會有問題..

#  if __WORDSIZE == 64
typedef long int int64_t;
# endif

不過在 FreeBSD 上面,即使是 32 位元的系統,也不會有 2GB 檔案大小的限制,這是因為 FreeBSD 內部處理檔案時,本來就是使用 64 位元的數字當作指標,所以不會有問題

因此在 32 位元的 Linux 上面,程式需要作一些額外處理才能正確寫超過 2GB 的檔案

我們先寫一個小程式來測試一下 (large.c)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <signal.h>
#include <unistd.h>
#include <errno.h>
void sig_xfsz(int sig)
{
        printf("ERROR: SIGXFSZ (%d) signal received!\n", sig);
}
int main()
{
        int     i, fd;
        char    dummy[4096];
 
        signal( SIGXFSZ, sig_xfsz );
 
        unlink("large.log");
        fd = open("large.log", O_CREAT|O_WRONLY, 0644 );
 
        bzero( dummy, 4096 );
        /* 2GB = 4KB x 524288 */
        for( i = 0 ; i < 524287 ; i++ )
                write( fd, dummy, 4096 );
        write( fd, dummy, 4095 );
        printf("large.log: 2147483647 bytes\n");
 
        if( write( fd, dummy, 1 ) < 0 )
                printf("ERROR: %s [errno:%d]\n",strerror(errno),errno);
        else
                printf("large.log: 2147483648 bytes\n");
 
        close(fd);
        exit(0);
}

在 32 位元的 Linux 下面,以上程式編譯後若沒有特殊處理,執行結果如下:

# gcc -o large32 large.c
# ./large32
large.log: 2147483647 bytes
ERROR: SIGXFSZ (25) signal received!
ERROR: File too large [errno:27]

在寫第 2147483648 byte 的時候,程式會收到 signal SIGXFSZ,同時 write() 會回傳 -1 錯誤,errno 則為 27 (File too large)。更甚者,如果程式沒有像上面一樣去處理 SIGXFSZ 的話,內定的 signal handler 甚至會造成程式停止執行並產生 core dump

接下來,我們在編譯同一個程式的時候加入 -D_FILE_OFFSET_BITS=64 再試看看:

# gcc -D_FILE_OFFSET_BITS=64 -o large64 large.c
# ./large64
large.log: 2147483647 bytes
large.log: 2147483648 bytes

果然順利突破 2GB 的限制了!

而同樣的程式在 32 位元的 FreeBSD 下面,不論有沒有加這個定義,跑起來都是正確的

不過處理這些大檔案的時候,除了編譯程式時的參數不同外,有些函數的使用上也要作一些調整,例如 fseek() 與 ftell() 這兩個原本使用到 long integer 當作 offset 的函數:

1
2
int fseek(FILE *stream, long offset, int whence);
long ftell(FILE *stream);

只要系統是 32 位元,即使是在 FreeBSD 下面,都需要改為使用 off_t 的版本:

1
2
int fseeko(FILE *stream, off_t offset, int whence);
off_t ftello(FILE *stream);

在 Linux 下面,如果 _FILE_OFFSET_BITS 定義為 64,則 off_t 這個型態會自動轉成 64 位元的大小(在 FreeBSD 上面,off_t 本來就是 64 位元的大小)

每種系統支援大於 2GB 的檔案讀寫所需要的編譯選項都會有一些差異,即使是同樣是 Linux 也會因為 32 位元或 64 位元而有不同。有一個簡單的方法可以判斷,就是利用 glibc 提供的 getconf 來取得編譯(compile)以及連結(linking)時所需的參數:

# getconf LFS_CFLAGS
-D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64
# getconf LFS_LDFLAGS 
 
#

上面是在 32 位元的 Redhat Linux 上面跑出來的結果,代表的是在這個系統上,若要讓程式支援 2GB 的檔案讀寫,編譯(compile)時需要加上 -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 這兩個參數,連結(linking)時則不用加任何參數

參考資料:

硬體 dada on 一月 19, 2008

SDHC support on Thinkpad X60

Thinkpad X60 原本並沒有 支援 SDHC 卡,把 SDHC 卡插入 X60 左方的 SD 插槽會讀不到

本來以為 X60 對於 SDHC 的支援是無望了,沒想到現在竟然可以了,而且還只要更新作業系統即可... 詳情請參考微軟發佈的 Hotfix 923293
(標題: 在 Windows XP Hotfix 923293 為高容量的 SD 記憶卡加入支援)

不過這個 Hotfix 微軟還沒有釋放出來讓大家下載安裝,未來新的 Service Pack 3 才會放進去,有需要的人必須自己去線上申請,選擇好你的作業系統版本及語言即可

申請之後,微軟就會把 Hotfix 的下載連結寄給你(還有個密碼來解壓縮),正體中文 WinXP 的 Hotfix 檔名為: WindowsXP-KB923293-v4-x86-CHT.exe,怕麻煩的話網路上搜尋一下應該找得到.. 或者寄信給我吧...

安裝此 Hotfix、重新開機後,果然可以讀到 SDHC 卡了! 同事的華碩 ASUS W5 (WinXP) 原本也是不能讀 SDHC 卡,服用這帖 Hotfix 後也 OK 了

Yeah! 這樣方便多了

2008/04/30 update: Windows XP SP3 已經包含這個修正了,不需要再特別安裝 hotfix 了...

資訊安全 dada on 一月 07, 2008

SSH Keyboard-Interactive Authentication

有些 SSH clients (例如: SecureCRT)允許使用者「儲存」密碼,這對系統安全是個大忌,一旦 client 遭受入侵,server 也會暴露在危險之中。

建議直接在伺服器端取消 SSH 的 password authentication 功能,改用 keyboard interactive 的方式認證,通常這樣的話,client 就無法儲存密碼了:

1. OpenSSH - /etc/ssh/sshd_config

PasswordAuthentication no
ChallengeResponseAuthentication yes

2. SSH2 (Tectia) - /etc/ssh2/sshd2_config

AllowedAuthentications          publickey,keyboard-interactive
AuthKbdInt.Optional             pam,password
AuthKbdInt.Required             password

修改好上述 sshd 的設定檔後,送個 HUP 給 sshd 即可(注意不要不小心把目前用的 session 也砍了,不然改錯的話只好到 console 前面去救了)

# ps ax | grep sbin/sshd | grep Ss
 9767 ?        Ss     0:00 /usr/sbin/sshd
# kill -HUP 9767

然後就可以請大家改用 keyboard-interactive authentication 了:

SSH keyboard-interactive authentication

資訊安全 dada on 一月 03, 2008

SSH2 vs OpenSSH

常見的 SSH Implementation 有兩種,ssh.com 的 SSH 以及 OpenSSH,詳細發展原由可參考 OpenSSH History

由於歷史因素,所以提到 SSH 相關用語時,常因不明確而混淆。建議可以參考 O'Reilly book 出版的 SSH, The Secure Shell: The Definitive Guide 一書中對於 SSH 各種詞彙的精確定義:

Terminology: SSH Protocols and Products

SSH
    A generic term referring to SSH protocols or software products.
SSH-1
    The SSH protocol, Version 1. This protocol went through several
    revisions, of which 1.3 and 1.5 are the best known, and we will
    write SSH-1.3 and SSH-1.5 should the distinction be necessary.
SSH-2
    The SSH protocol, Version 2, as defined by several draft standards
    documents of the IETF SECSH working group.
SSH1
    Tatu Ylönen's software implementing the SSH-1 protocol; the original
    SSH. Now distributed and maintained (minimally) by SSH
    Communications Security, Inc.
SSH2
    The "SSH Secure Shell" product from SSH Communications Security, Inc.
    This is a commercial SSH-2 protocol implementation, though it is
    licensed free of charge in some circumstances.
ssh (all lowercase letters)
    A client program included in SSH1, SSH2, OpenSSH, F-Secure SSH, and
    other products, for running secure terminal sessions and remote
    commands. In SSH1 and SSH2, it is also named ssh1/ssh2, respectively.
OpenSSH
    The product OpenSSH from the OpenBSD project,
    which implements both the SSH-1 and SSH-2 protocols.
OpenSSH/1
    OpenSSH, referring specifically to its behavior
    when using the SSH-1 protocol.
OpenSSH/2
    OpenSSH, referring specifically to its behavior
    when using the SSH-2 protocol.

OpenSSH 基於相容 BSD license 的 ssh 1.2.12 來開發,後續又加上了對 SSH-2 的支援

但 OpenSSH 對 SSH-2 的實作與 ssh.com 後來對 SSH-2 的實作已經有了一些差異。

現今大部分的 open source 作業系統都已經改用 OpenSSH 了,不過有些地方還是可以看到 SSH2 的蹤跡,可能是商業版本或是非商業版本,例如:

# ssh -V
ssh: SSH Secure Shell 3.2.9.1 (non-commercial version) on i686-pc-linux-gnu

SSH Communications Security, Inc. (www.ssh.com) 的商業版本 SSH (SSH2) 正式名稱叫做 Tectia SSH client/server

熟悉 OpenSSH 的人初次接觸 SSH2 最常遇到的問題就是 Public-Key Authentication 的作法以及 Key format 都不太一樣。

OpenSSH/1 與 SSH1 的 authorization file 都是放在 ~/.ssh/authorized_keys 這個檔案之內,identity 則是放在 ~/.ssh/identity 這個檔案內

OpenSSH/2 的 authorization file 一樣是 ~/.ssh/authorized_keys,identity 則是在 ~/.ssh/id_dsa 或 ~/.ssh/id_rsa 內,依據 key type 而異。但其實因為 OpenSSH/2 有向下相容 OpenSSH/1 的功能,所以事實上也會參考 ~/.ssh/identity 來當作 SSH-1 的 identity

SSH2 就很不一樣了。SSH2 允許有多個 identity,因此會有一個檔案 ~/.ssh/identification 可以設定有哪些 identity 可用,例如:

IdKey identity-1
IdKey identity-2
IdKey identity-3

真正的 private key 放在 identity-1, identity-2, identity-3 這三個檔案內,而使用 ssh -i 指定 identity 時,不同於 OpenSSH 是直接指到 key 本身,SSH2 必須指定這個間接的 identification file

SSH2 的 public key format 也與 OpenSSH 不同,不再是一行而已,也因此 authorization file 沒辦法跟 ~/.ssh/authorized_keys 一樣一行放一個 key 值。

SSH2 也是一樣使用一個檔案來設定允許的 public key 有哪些,檔案放在 ~/.ssh/authorization

Key identity-1.pub
Key identity-2.pub
Key identity-3.pub

這個檔案表示可接受三個 public key 分別為 identity-1.pub, identity-2.pub, identity-3.pub

而如同 OpenSSH 一樣,SSH2 的 authorization 檔案內也可以指定各種 options,例如:

Key identity-1.pub
Options no-port-forwarding,no-pty
Key identity-2.pub
Options command="rsync -az --server . /home/backup-x",no-pty

詳細資訊可參考 SSH2 的 manual

SSH2 的 key format 與 OpenSSH 不同,這在兩者之間要作 public key authentication 時造成了一些麻煩。OpenSSH 提供的 ssh-keygen 可以用 -e 和 -i 兩個選項來轉換兩者的 key format:

# ssh-keygen -e -f openssh-key.pub > ssh2-key.pub
# ssh-keygen -i -f ssh2-key.pub > openssh-key.pub

OpenSSH 與 SSH2 之間的問題還不只這樣,SCP 的實作方式也不同

OpenSSH 與 SSH1 的 SCP 是 RCP over SSH,而 SSH2 附的 SCP2 背地裡則是用 SFTP 實作的,因此兩者互相 copy 東西時有時會 copy 不過去,建議還是一律改用有經過 IETF 標準認證的 SFTP 就好了

如果可以的話,全部用 OpenSSH 是最單純的,不過有時不是自己能控制的,碰到使用 SSH2 的系統,其實只要稍微了解一下差異也很容易適應就是了

參考資料:

軟體 &資訊安全 dada on 一月 02, 2008

SSH Escape Character

SSH client 有一個 Escape Character (跳脫字元),事實上 rsh/rlogin 也有支援 Escape Character (~),就如同 Ctrl-] 之於 telnet 一樣...

SSH client 的 Escape Character 一般跟 rsh/rlogin 一樣設定成 ~,詳細用法可於命令列按 ~? 來取得說明,要注意的是,為了避免影響一般正常的輸入,Escape Character 必須是命令列換行後的第一個按鍵,如果你已經輸入別的按鍵,即使按 backspace 把游標移回開頭處再輸入 Escape Character 也是無效的!請先按 Enter 換行後再輸入吧!

myhost# ~?
Supported escape sequences:
~.  - terminate connection
~B  - send a BREAK to the remote system
~C  - open a command line
~R  - Request rekey (SSH protocol 2 only)
~^Z - suspend ssh
~#  - list forwarded connections
~&  - background ssh (when waiting for connections to terminate)
~?  - this message
~~  - send the escape character by typing it twice
(Note that escapes are only recognized immediately after newline.)

最常用的 Escape Character 就是 ~. 以及 ~^Z 這兩個:

~. 是直接切斷目前的 SSH 連線,如果你遇到遠端機器沒有反應時,可用此方式來切斷連線

~^Z (~ 及 Ctrl-Z) 則是把目前的 SSH 連線先 suspend,一般可用來回到原來機器上處理一些事情,若要再回去,輸入 fg 即可

問題來了,假設你從 HOST1 先 SSH 到 HOST2,然後再從 HOST2 SSH 到 HOST3

HOST1 ===1===> HOST2 ===2===> HOST3

如果你在 HOST3 的 terminal 下輸入 ~. 的話,你會直接切斷上面的第一段連線回到 HOST1 的 terminal,當然這樣的話第二段連線也一併 bye-bye 了!如果只是想切斷第二段連線回到 HOST2 該怎麼作呢?

這時候 ~~ 就發揮作用了,你只要輸入 ~~. 就可以切斷第二段連線了

以此累推,輸入 N 個 Escape Character 代表對第 N 段連線送出 Escape Character

最後,若不想用 ~ 當作跳脫字元,可於命令列用 -e 修改,例如把 SSH 跳脫字元改成 #:

ssh -e "#" myhost.mydomain.com

« 前一頁後一頁 »