tohokuaikiのチラシの裏

技術的ネタとか。

vsftpdでuse_localtimeをYESにした場合、時刻が9時間ずれると言われた件でuse_localtimeオプションとは何であるかを考察した

結論から

言うと、このuse_localtimeをYESにすると9時間のズレはなくなるよっていう記事はわんさか見たけど、自分の場合は逆でuse_localtimeをNOにしたら直ったのである。

症状は・・・

vsftpdで、use_localtimeをYESにすると9時間のズレが起こる。

情報

サーバーのタイムゾーンJST日本標準時)。サーバーでtimedatectlを実行すると下記の通り。

$ timedatectl
      Local time: 金 2017-12-22 10:48:17 JST
  Universal time: 金 2017-12-22 01:48:17 UTC
        RTC time: 金 2017-12-22 01:48:16
       Time zone: Asia/Tokyo (JST, +0900)
 Network time on: yes
NTP synchronized: yes
 RTC in local TZ: no

WinSCPでログインして、ファイルを置くとこうなる。
f:id:tohokuaiki:20171222105027p:plain

一見問題ないように見える。先ほどのtimedatectlと同じ時刻を返している(1分違うけどな)。

9時間遅い…

が、サーバー側でlsコマンドを打つとこうなる。

$ ls -l
合計 4
-rw-r--r-- 1 tohokuaiki tohokuaiki 1 12月 22 01:49 hoge

あれっ?9時間遅い?これが症状。

chrootしてるからか?と思ったけど違うみたい

vsftpd - use_localtime
とか見ると、chrootしてると/etc/localtimeを見られないので・・・というエントリーがたくさんあったけど、chrootを外しても症状は同じだったのでこれではないようだ。

WinSCPで通信ログを見てみる。

f:id:tohokuaiki:20171222104014p:plain

でログチェックをONにしてファイルでやり取りを見てみる。

use_localtimeとは?

そもそもuse_localtimeとは何かというと、vsftpd.confには以下のように書いてある。

If enabled, vsftpd will display directory listings with the time
in  your  local  time  zone.  The default is to display GMT. The
times returned by the MDTM FTP command are also affected by this option.

この「in your local time zone」というののyourって誰やねん?サーバ側かFTPクライアント側かっていうのがってのが分からない。

普通に考えるとFTPでつなぐクライアント側だと思うのだけど、さてWinSCPのログを見るとタイムゾーンの情報なんて全く送っていない。以下、接続した時点でのWinSCPのログ。

.Working directory: F:\WinSCPPortable
.Process ID: 18424
.Command-line: "F:\WinSCPPortable\WinSCP.exe" 
.Time zone: Current: GMT+9 (東京 (標準時)), No DST
.Login time: 2017年12月22日 10:57:33
.--------------------------------------------------------------------------
.Session name: tohokuaiki@vsftpd.example.com(FTP) (Site)
.Host name: vsftpd.example.com (Port: 21)
.User name: tohokuaiki (Password: Yes, Key file: No, Passphrase: No)
.Transfer Protocol: FTP
.Ping type: Dummy, Ping interval: 30 sec; Timeout: 15 sec
.Disable Nagle: No
.Proxy: None
.Send buffer: 262144
.UTF: Auto
.FTPS: None [Client certificate: No]
.FTP: Passive: Yes [Force IP: Auto]; MLSD: Auto [List all: Auto]; HOST: Auto
.Local directory: C:\Users\itoh\Documents, Remote directory: /work/20171222, Update: Yes, Cache: Yes
.Cache directory changes: Yes, Permanent: Yes
.Recycle bin: Delete to: No, Overwritten to: No, Bin path: 
.Timezone offset: 0h 0m
.--------------------------------------------------------------------------
.Session upkeep
.Connecting to vsftpd.example.com ...
.Connected with vsftpd.example.com. Waiting for welcome message...
<220 (vsFTPd 3.0.3)
>USER tohokuaiki
<331 Please specify the password.
>PASS ***********
<230 Login successful.
>SYST
<215 UNIX Type: L8
>FEAT
<211-Features:
< EPRT
< EPSV
< MDTM
< PASV
< REST STREAM
< SIZE
< TVFS
<211 End
.Connected
.Got reply 1 to the command 1
.--------------------------------------------------------------------------
.Using FTP protocol.
.Doing startup conversation with host.
>PWD
<257 "/" is the current directory
.Got reply 1 to the command 16
.Changing directory to "/work/20171222".
>CWD /work/20171222
<250 Directory successfully changed.
.Got reply 1 to the command 16
.Getting current directory name.
>PWD
<257 "/work/20171222" is the current directory
.Got reply 1 to the command 16
.Session upkeep
.Retrieving directory listing...
>TYPE A
<200 Switching to ASCII mode.
>PASV
<227 Entering Passive Mode (160,16,132,242,238,127).
>LIST -a
.Connecting to 160.16.132.242:61055 ...
.Data connection opened
<150 Here comes the directory listing.
.Data connection closed
.drwxr-xr-x    2 1000     1000         4096 Dec 22 10:49 .
.drwx------    7 1000     1000         4096 Dec 22 10:49 ..
.-rw-r--r--    1 1000     1000            1 Dec 22 01:49 hoge
<226 Directory send OK.
.Directory listing successful
.Got reply 1 to the command 2
.Detecting timezone difference...
.Retrieving file information...
>PWD
<257 "/work/20171222" is the current directory
>CWD /work/20171222/hoge
<550 Failed to change directory.
>TYPE I
<200 Switching to Binary mode.
>SIZE /work/20171222/hoge
<213 1
>MDTM /work/20171222/hoge
<213 20171222014942
.Retrieving file information successful
.Got reply 1 to the command 1024
.Timezone difference of -9 detected using file /work/20171222/hoge (Listing: 2017-12-21T16:49:00.000Z, UTF: 2017-12-22T01:49:00.000Z)
...;D;0;1899-12-30T09:00:00.000Z;0;"" [0];"" [0];---------;0
.hoge;-;1;2017-12-22T01:49:00.000Z;1;"1000" [0];"1000" [0];rw-r--r--;0

ログで違うなーと思ったのは

use_localtime=YES

Timezone difference of -9 detected using file /work/20171222/hoge (Listing: 2017-12-21T16:49:00.000Z, UTF: 2017-12-22T01:49:00.000Z)

use_localtime=NO

Timezone difference of -9 detected using file /work/20171222/hoge (Listing: 2017-12-21T07:49:00.000Z, UTF: 2017-12-21T16:49:00.000Z)

となっているところ。しかし、これはWinSCPから送信している情報ではない。サーバーから受け取っている情報である。

考察

こう考えると都合はあう。

おそらく、use_localtimeのlocalとはFTPクライアントタイムゾーンであろう。ただし、これをどうやってvsftpdが受け取っているのかが分からない。

基本的にvsftpdはGMT世界標準時)でファイルのタイムスタンプを扱うようになっているらしい。なので、ファイルをFTPソフトアップする際に、クライアントが+9のJST日本標準時)であるなら、vsftpdはそのファイルをのタイムスタンプを-9してサーバー上に保存する。

ただ、今回のサーバーはそもそもサーバーのタイムゾーンJST日本標準時)なのである。そこで-9されてしまってはずれが生じてしまう。

そこで、use_localtime=NOとすることで私のケースではズレが収まった。

結論

サーバーのタイムゾーンJSTにしている場合、use_localtime=NOにしておく。