2013年11月2日 星期六

[SA] Hw2 ftp

SA HW2 DEMO 流程說明

===================
注意: 先在下列位置事先放置檔案供 demo 測試
-----------------------------------------
/home/ftp/public/
/home/ftp/hidden/
/home/ftp/hidden/treasure

使用的 ftp 是 proftpd,FreeBSD 9.2-RELEASE

ZFS

ZFS on /home/ftp
Create a zfs file system with mirror
Do zfs snapshot
Do zfs rollback
Set compression
Reboot and everything is fine (zfs still mount)






(1) Set compression: 對 zfs file system 設定 compression

    # sudo zpool create storage mirror /dev/da1 /dev/da2
    create storage that uses mirror (當時 spec 沒看清楚沒加參數就被扣分惹 QQ)

    # sudo zfs create storage/ftp
    # sudo zfs set compression=gzip storage/ftp
    compression 設定成 gzip

    # sudo ln -s /storage/ftp/ /home/ftp                                          
    我是用建 soft link 的方式讓他進到 /home/ftp 時會跑到 /storage/ftp,但是正常的作法好像應該是要 mount 上去,指令:
    # zfs set mountpoint=/home/ftp storage/ftp

    Demo 的時候助教問了兩個問題,一個是如何證明你有做 mirror:
    #zpool status
   

   第二個是要怎麼知道你有做壓縮:
   # zfs get compression




(2) Snapshot: 對 zfs file system 建立一份 snapshot

    # sudo zfs snapshot storage/ftp@demo

     如何看到 snapshot 有哪些:
     # man zpool
         listsnaps=on | off
         Controls whether information about snapshots associated with this
         pool is output when "zfs list" is run without the -t option. The
         default value is off.


    # sudo zpool listsnaps=on storage
    # zfs list   


Reference:  
Man page


FTP

(如果因為 IP 問題沒有辦法被直接連線的,請使用 port forwarding 的方式)
  • Install a FTP server for FreeBSD
    • Pure-FTPd: /usr/ports/ftp/pure-ftpd/
    • ProFTPD: /usr/ports/ftp/proftpd/
  • Settings
    • Anonymous Login
      • Chrooted (/home/ftp is your root directory)
      • Can download from /home/ftp/public
      • Can upload & mkdir from /home/ftp/upload
      • Can’t delete from /home/ftp/upload
      • Can only download the files which owner isn’t “ftp”
      • Hidden directory /home/ftp/hidden
        • There is a directory called "treasure" inside
        • Client can’t list in /home/ftp/hidden/ but can in hidden/treasure
    • Create a virtual user "ftp-vip"
      • Chrooted (/home/ftp is your root directory)
      • Password is your student-id
      • Can login from 140.113.17.225 & your ip only
      • Full access to /home/ftp
      •  Hidden directory is visible to ftp-vip
    •  Create a system user "sysadm"
      • Could connect by sftp, ftp, ftpes.
      • Password is your student id
      • Permission is same as ftp-vip.
    •  Your ftp server should support ftp over TLS
(1) Anonymous
    1.檢查是否 chroot
    Fisrt of all, 要先手動建一個叫作 ftp 的 user。
    在 proftpd 裏面,Anonymous 是否能登入只要修改一下 proftpd.conf 把裏面幾行的 comment 拿掉:



 chroot 只要在 Global 的標籤裏面加上你要給他的 root 的位置:
   


成功了的話就會像這樣子:



    2.可否從 /home/ftp/public 下載檔案
    下載的話是關係到 read 權限,所以 public 資料夾的 read 權限要打開

    3.上傳檔案並建立一資料夾至/home/ftp/upload
     上傳的話是需要資料夾的 write 權限,所以 upload 資料夾的 write 權限要打開,而且要記得拿掉或是註解掉:
   

 

    4.嘗試刪除、改名/home/ftp/upload的檔案(皆失敗)
     在 Anonymous 的 block 加入:




在 Limit block 裏面常用的參數:
  • CWD :Change Working Directory,變換目錄之意;
  • MKD :MaKe Directory,可建立目錄與否;
  • RNFR :ReName FRom,可更改檔名與否;
  • DELE :DELEt,可刪除檔案語法;
  • RMD :ReMove Directory,可移除目錄與否;
  • RETR :RETRieve,下載之意!由 Server 傳送資料到 Client;
  • READ :可讀取與否
  • WRITE:可寫入與否
  • STOR :STORe,上傳之意,由 Client 傳送資料到 Server !
  • ALL :全部的動作!



    5.下載/home/ftp/upload的檔案(擁有者是”ftp”的檔案不能下載,其餘可以)
    這個就有點有趣了,因為 anonymous 登入的時候在系統中是以 ftp 的身分,所以變成說 owner 的 read 權限要拿掉,而且因為 anonymous 自己也會上傳東西,上傳的東西 owner 會是 ftp 所以還要在 anonymous 的 block 裏面改他的 umask,目前暫時改成 700,and it works!

    6.可以進去 /home/ftp/hidden,但是看不到底下有任何東西
    hidden owner (ftp) 的 read 權限拔掉

    7.直接輸入/home/ftp/hidden/treasure/,可以進入並且看到底下有東西



(2) Virtual user
    1.Demo 用電腦以 “ftp-vip”登入無法,改用 140.113.17.225 登入成功。
     首先要先創個 virtual user 出來:  
  • 先設定好 Virtual user 及 Virtual Group: 
  • 查詢 ftp 這個 user 的 uid 及其 group 的 gid 
       $ cat /etc/passwd | grep ftp
       $ cat /etc/group | grep ftp

  • 語法:建立新 ftp 使用者,並儲存到 proftpd.passwd 
       # sudo ftpasswd --passwd --name=$virtual_user --gid=$real_user_gid --uid=$real_user_uid --home=$virtual_user_home --shell=/bin/false --file=/output_file/proftpd.passwd

        $ sudo ftpasswd --passwd --name=ftpvip --uid=1004 --gid=14 --file=/usr/local/etc/proftpd/proftpd.passwd
  • 語法:建立新ftp群組,並儲存到proftpd.group 
      # sudo ftpasswd --group --name=$virtual_group --gid=$real_user_gid --member=virtual_user --file=/output_file/proftpd.group
 
  • 接著就來設定 /etc/proftpd/proftpd.conf,確認以下的設定:
    # 使用自製的passwd檔
    AuthUserFile            /usr/local/etc/proftpd/proftpd.passwd

    # 使用自製的group檔
    AuthGroupFile            /usr/local/etc/proftpd/proftpd.group

    # 不需要有效的shell也能登入FTP,方便Virtual user登入
    RequireValidShell        off

     如果是虛擬機的話這邊判斷 ip 會有問題(所以後來這個 spec 就取消了),要做的話,在 proftpd 裏面可以利用 IfUser 和 Limit LOGIN 的 tag 來做:

    <IfUser ftp-vip>
        <Limit LOGIN>
            Order allow, deny   #first match
            Allow from 140.113.17.225
            DenyAll
        < /Limit>
    </IfUser>

如果噴語法錯誤,可能是沒有 load module 進來,在設定檔上面加上:

    LoadModule mod_ifsession.c 

    2.檢查是否 chroot
    3.可以再/home/ftp/hidden/ 看到任何東西
    剛剛是把 hidden 的 owner read 權限拔掉,這時候因為剛剛設定 ftp-vip 登入的 uid 和 ftp 一樣,所以不管這裡怎麼改都看不到,所以後來我就讓 ftp-vip 是以別人的 uid 登入,然後 gid 是 ftp 的,所以判定時就可以順利看到了(要記得開 group 的 read 權限) 

    4.可以對/home/ftp/upload 的任何檔案進行(上傳、下載、刪除、更名、建立資料夾)
    5.以 ftp over TLS 的方式登入
    根據這篇的作法即可成功。
   

(3) Systemuser
    1.以 sftp 的方式登入 sysadm 該帳號。
    sftp.conf:
 


     Virtualhost 後面接主機的 IP,Port 後面為連線時要開的 port
     然後在 proftpd.conf 裏面把他 Include 進來

    2.檢查 chroot 之目錄為 /home/ftp
    3.權限與 ftp-vip 相同, 相同操作都要再進行一次。


Reference:  
Wikipedia
http://linux.vbird.org/linux_server/0400wuftp.php#server_ftpaccess_basic
http://proftpd.open-source-solution.org/docs/howto/AuthFiles.htm 
http://linux.vbird.org/linux_server/0410proftpd.php
www.proftpd.org/docs/
http://gisanfu.pixnet.net/blog/post/6501095-proftpd-%E8%99%9B%E6%93%AC%E5%B8%B3%E8%99%9F
http://justicer.pixnet.net/blog/post/29774037-proftpd%E6%9E%B6%E7%AB%99%E7%B4%80%E9%8C%84
http://wiki.linux.org.hk/w/Setup_anonymous_FTP_server_with_ProFTPd 
http://www.freebsddiary.org/users.php 
http://www.howtoforge.com/setting-up-proftpd-tls-on-debian-squeeze
http://forums.freebsd.org/showthread.php?t=33570
https://forums.proftpd.org/smf/index.php?topic=3225.0
http://blog.endpoint.com/2012/12/sftp-virtual-users-with-proftpd-and.html
http://roshamboot.org/main/configuring-a-proftpd-ftpsftp-chroot-on-slessuse-11/ 

 

BT

(1) Torrent
    1.以 ftp-vip 的帳號登入。
    2.把助教準備的 torrent 上傳至/home/ftp/public,啟動自動下載,檔案存放於同目錄下。

用 FreeBSD 裡面的 net-p2p/transmission,安裝完後開始改設定,然後資料夾要記得給 transmission 可以編輯的權限。

# sudo vim /etc/rc.conf 

transmission_enable="YES" 
transmission_watch_dir="/home/ftp/public" transmission_download_dir="/home/ftp/public"


# sudo service transmission start

在 /usr/local/etc/transmission/home/torrents 中可以看到被加入的 torrents,然後下載中的 torrents file 會被加上 .added

Reference:  
https://trac.transmissionbt.com/wiki/EditConfigFiles
https://forum.transmissionbt.com/viewtopic.php?f=2&t=7680
http://pynej.blogspot.tw/2010/02/set-up-transmission-deamon-bittorrent.html

Samba

(盡量自己多準備一台 windows 來進行 samba 的相關操作)
  •  Install Samba
    • /usr/ports/net/samba36
  • Location
    • Samba share /home/ftp/public to windows
    • Mount windows directory at /home/ftp/windows
  • You should create at least 4 account.
  • Admin
    • full access ( read, download, delete, rename, mkdir, upload ) to all files and directory
  • Guest
    • Can only list the files and directory.
    • Can’t perform ( read, download, delete, rename, mkdir, upload ) to all files and directory
  • Test1
    • Can ( read, rename, delete, download) to Test1’s files.
    • Can mkdir.
    • Can upload files to Test1’s directory and root directory /home/ftp/public/
    • Can’t do ( read, rename, delete, download) to others files and directory.
  • Test2
    • Can ( read, rename, delete, download) to Test2’s files.
    • Can mkdir.
    • Can upload files to Test2’s directory and root directory /home/ftp/public/
    • Can’t do ( read, rename, delete, download) to others files and directory.

(1) Test1:
    1.用 "test1" 帳號登入
        command line 連進去:
       # smbclient //140.113.27.44/public -Utest1

    2.建立資料夾並且上傳檔案進去
    3.打開剛剛上傳的檔案

(2) Test2:
    1.用 “test2”帳號登入
    2.進入 “test1”創立的資料夾
    3.嘗試刪除 test1 的檔案 (失敗)
    4.嘗試打開 test1 的檔案 (失敗)
    5.嘗試上傳檔案到 test1 創立的資料夾 (失敗)
    6.在最外面創立資料夾
    7.上傳.exe 的檔案至 test2 的資料夾 (失敗)
    在 /usr/local/etc/smb.conf 加上:
    veto files = /*.exe/    

    8.上傳.txt 的檔案至 test2 的資料夾 (成功)
   


(3) Guest:
    1.用 “guest”帳號登入
    2.可以看到所有資料夾以及檔案
    3.無法對所有的檔案資料夾進行下列動作 (讀取、下載、刪除、改名、創立資料夾、上傳)
    利用 group 的概念,guest 沒有在 smbusr 這個 group 裏面,所以無法寫入,然後因為 create mode 是 600 所以也沒辦法對他做什麼。


(4) Admin:
    1.用 “admin”帳號登入
    2.可以對所有的檔案資料夾進行下列動做 (讀取、下載、刪除、改名、創立資料夾、上傳)
    在 smb.conf 裏面加上:
    admin users = admin 

    3.把剛剛 bt 載完的影片從/home/ftp/public/打開並能夠順利播放
    

(5) FreeBSD:
    1.掛載 windows 分享的資料夾於 /home/ftp/windows
    # sudo mount_smbfs -Eutf-8:cp866 '//joyqul@qulqul/sa' /home/ftp/windows/

    2.以常用帳號前往/home/ftp/windows,嘗試寫入一個檔案,看 windows 那端有沒有對應檔案出現。
   
    然後有一個很可愛的指令叫作 smbtree,可以看到同個區網底下的 workgroup 唷
  

Reference:  
Wikipedia
-- Basic concept --
http://linux.vbird.org/linux_server/0370samba.php
http://www.twbsd.org/cht/book/index.php?ch=20
http://phorum.vbird.org/viewtopic.php?t=22001
http://www2.lsps.tp.edu.tw/~gsyan/freebsd2001/samba.html
-- Samba command --
http://forums.freebsd.org/showthread.php?t=34508 
-- mount smbfs --
http://delphi.ktop.com.tw/board.php?cid=165&fid=900&tid=87113 
http://www.weithenn.org/cgi-bin/wiki.pl?Mount_smbfs-%E6%8E%9B%E8%BC%89_Samba_%E5%8F%8A_Windows_%E6%89%80%E5%88%86%E4%BA%AB%E7%9A%84%E7%9B%AE%E9%8C%84
http://osdir.com/ml/freebsd-bugs/2007-10/msg00247.html
http://forums.freebsd.org/showthread.php?t=27139 
-- Windows setting --
http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/sysdm_ident_join_workgroup.mspx?mfr=true
http://system.cyut.edu.tw/tech/share1/share.html (big 5)
-- User management --
http://www.freebsd.org/cgi/man.cgi?query=pw&sektion=8
http://www.twbsd.org/cht/book/index.php?ch=04
--Misc --
http://home.csjs.tc.edu.tw/phpbbinf/viewtopic.php?f=6&t=810&sid=e096cc0437d88ce221fe1cc2d607248d

 

ZFS

(1) Reboot: 電腦重新開機
(2) Rollback: 將 zfs file system 回覆到最開始,此時/home/ftp/下的檔案都要跟一開始完全一樣。
     # sudo zfs rollback storage/ftp@demo


沒有留言:

張貼留言