2015年11月4日水曜日

レンタル サーバーを dual boot (debian + openbsd) にする



5)レンタル サーバーを dual boot(debian + openbsd)にする。

20GB分だけ まずdebianをいれます。

debian を起動し / に bsd.rd を保存します。
また fdisk で openbsd areaを確保ておきます。


/boot/grub/grub.cfg の
menuentry 'Debian GNU/Linux, with Linux 3.2.0-4-amd64 (recovery mode)' --class debian --class gnu-linux --class gnu
 --class os {
    load_video
    insmod gzio
    insmod part_msdos
    insmod ext2
    set root='(hd0,msdos1)'
    search --no-floppy --fs-uuid --set=root 48513cc3-0824-41d2-b0e2-33cefa6dfbc7
    echo    'Loading Linux 3.2.0-4-amd64 ...'
    linux    /boot/vmlinuz-3.2.0-4-amd64 root=UUID=48513cc3-0824-41d2-b0e2-33cefa6dfbc7 ro single  
    echo    'Loading initial ramdisk ...'
    initrd    /boot/initrd.img-3.2.0-4-amd64
}
部分を



menuentry 'Debian GNU/Linux, with Linux 3.2.0-4-amd64 (recovery mode)' --class debian --class gnu-linux --class gnu
 --class os {
    load_video
    insmod gzio
    insmod part_msdos
    insmod ext2
    set root='(hd0,msdos1)'
    search --no-floppy --fs-uuid --set=root 48513cc3-0824-41d2-b0e2-33cefa6dfbc7
    echo    'Loading Linux 3.2.0-4-amd64 ...'
    kopenbsd /bsd.rd
    echo    'Loading initial ramdisk ...'
    boot
}

と書き換え この 'Debian GNU/Linux, with Linux 3.2.0-4-amd64 (recovery mode)'メ ニューで起動すると OPENBSDインストーラが動きます。 http での sd1へ の インストールとなります。 

その後もう一度 debian で立ち上げ

 cat /etc/grub.d/40_custom を
ーーーーーーーーーーーーー
#!/bin/sh
exec tail -n +3 $0
# This file provides an easy way to add custom menu entries.  Simply type the
# menu entries you want to add after this comment.  Be careful not to change
# the 'exec tail' line above.

menuentry "OpenBSD" {
    set root=(hd0,4)
    chainloader +1
}

としてから update-grub するだけです。




ARCH linuxの最終の /boot/grub/grub.cfg は

menuentry 'Arch Linux, with Linux core repo kernel' --class arch --class gnu-linux --class gnu --class os $menuentry_id_option 'gnulinux-core repo kernel-true-ad4103fa-d940-47ca-8506-301d8071d467' {
    load_video
    set gfxpayload=keep
    insmod gzio
    insmod part_msdos
    insmod ext2
    set root='hd0,msdos2'
    linux    /boot/vmlinuz-linux root=/dev/sdb2 rw  quiet

    initrd    /boot/initramfs-linux.img
}

menuentry 'openbsd' {
set  root='hd0,msdos1'
chainloader +1
}

menuentry 'INSTALL openbsd' {
kopenbsd /bsd.rd-57
boot
}








番外編) このサーバーは レン タルサーバーで動いてます。
お名前.comで メモリ1G HDD100GBです。

load averages:  0.07,  0.08,  0.12                                           aoiyuma.mydns.jp 15:16:43
46 processes: 45 idle, 1 on processor
CPU0 states:  0.0% user,  0.0% nice,  0.2% system,  0.0% interrupt, 99.8% idle
CPU1 states:  0.0% user,  0.0% nice,  0.0% system,  0.0% interrupt,  100% idle

Memory: Real: 271M/551M act/tot Free: 435M Cache: 197M Swap: 0K/2055M
  PID USERNAME PRI NICE  SIZE   RES STATE     WAIT      TIME    CPU COMMAND
 2817 _snort     4    0  502M  244M sleep     bpf       0:33  0.00% snort
 5383 root       2    0 1028K 1504K idle      select    0:21  0.00% sshd
24488 _syslogd   2    0 1120K 1484K idle      kqread    0:19  0.00% syslogd
 6203 tuyosi     2    0 3880K 2756K sleep     select    0:11  0.00% sshd
    1 root      10    0  524K  540K idle      wait      0:04  0.00% init
20705 _pflogd    4    0  684K  392K sleep     bpf       0:03  0.00% pflogd
16968 tuyosi     2    0 1436K 3780K sleep     kqread    0:01  0.00% imap
 3272 tuyosi     2    0 1444K 3848K sleep     kqread    0:01  0.00% imap
14486 _dovenul   2    0  948K 4372K sleep     kqread    0:01  0.00% imap-login

と スワップは2Gありますが、CPUは遊んでるような状態です。



# dmesg | grep wd
wd0 at pciide0 channel 1 drive 0: <QEMU HARDDISK>
wd0: 16-sector PIO, LBA48, 20480MB, 41943040 sectors

wd1 at pciide0 channel 1 drive 1: <QEMU HARDDISK>
wd1: 16-sector PIO, LBA48, 81920MB, 167772160 sectors

とハードディスクは QEMU HARDDISKで 想像するに実HDDではなくて
たぶん 数TBのハードディスクの一部なのでしょう。




# fdisk  wd0  
Disk: wd0       geometry: 2610/255/63 [41943040 Sectors]
Offset: 0       Signature: 0xAA55
            Starting         Ending         LBA Info:
 #: id      C   H   S -      C   H   S [       start:        size ]
-------------------------------------------------------------------------------
 0: 83    261  53  49 -    768  20  20 [     4196352:     8142848 ] Linux files* openbsdからもアクセスするためext2
*1: 83      0  32  33 -    261  53  48 [        2048:     4194304 ] Linux files* 
openbsdからもアクセスするため ext2
 2: 00      0   0   0 -      0   0   0 [           0:           0 ] unused     
 3: A6    768  20  21 -   2610 212  34 [    12339200:    29603840 ] OpenBSD    

と wd0は
4GBの arch (ここからもブート可)
2GBの arch ここに grub.cfgはある。もちろんブート可 /dev/wd0i      4128416   2299984   1618720    59%    /mnt-debian-sda2 と1Gちょっとしかいらない
menuentry 'openbsd install-5.7' {
kopenbsd /bsd.rd-57
}


menuentry 'openbsd install-snap' {
kopenbsd /bsd.rd-snap
}


menuentry 'Arch Linux sda111111' {
        load_video
        set gfxpayload=keep
        insmod gzio
        insmod part_msdos
        insmod ext2
        set root='hd0,msdos1'
        linux    /boot/vmlinuz-linux root=/dev/sda1 rw  quiet
        initrd   /boot/initramfs-linux.img
}
arch linuxは そのイメージを 
sftp -i vps1006-001.key  iu1-00008@sftp1001.myvps.jp
な形で転送して、これが仮想CDとなってお名前でOインストールをします。

arch linuxは そのgrubブートローダーを使うだけですから、スワップもいりませんし GUIもいりません。
だから2GBのHDDへも十分入ります。
そしてこのext2に openbsdの必要最小限をバックアップしてます。
/etc, /var/www, /home/tuyosi/*.bat ですね。
そうして万一のクラッシュに備えてます。
80G openbsd クラッシュ -> 15B openbsd クラッシュ -> arch のブートローダでkopenbsd を利用して再インストのあと/etc, /var/www, /home/tuyosi/*.batを
を書き戻して早い普及をはかる。
15GBのopenbsd   (ここからもブート可)

# fdisk  wd1
Disk: wd1       geometry: 10443/255/63 [167772160 Sectors]
Offset: 0       Signature: 0xAA55
            Starting         Ending         LBA Info:
 #: id      C   H   S -      C   H   S [       start:        size ]
-------------------------------------------------------------------------------
 0: 00      0   0   0 -      0   0   0 [           0:           0 ] unused     
 1: 00      0   0   0 -      0   0   0 [           0:           0 ] unused     
 2: 00      0   0   0 -      0   0   0 [           0:           0 ] unused     
*3: A6      0   1   2 -  10442 254  63 [          64:   167766731 ] OpenBSD  

80GBあって 通常はここから起動している。
今もこの状態。

Filesystem  512-blocks      Used     Avail Capacity  Mounted on
/dev/wd1a    160966480  39509296 113408860    26%    /
と十分な余裕があります。












openbsd で teamviewer を ブロックする

3) BLOCK teamviewer by openbsd

 eamviewer は 通 常のfirewallを乗り越え内部に侵入していきます。


openbsd
のpfだ けでは防御不能です。
例えば会社の内部の一台で teamviewerが 起動しているとIDとPASSWDで そのPCに入られま す。
そのPCを から他のPCに 接続して、中の情報を抜き取られます。
そのために 以下の方法でteamviewer をブロックします。
つまり squidを あわせて使います。






BLOCK  teamviewer by openbsd
teamviewer は 通 常のfirewallを乗り越え内部に侵入していきます。

openbsd
のpfだけでは防御不能です。
例えば会社の内部の一台で teamviewerが起動しているとIDとPASSWDで そのPCに入られます。
そのPCをから他のPCに 接続して、中の情報を抜き取られます。
そのために 以下の方法でteamviewer をブロックします。
つまり squidをあわせて使います。


下のような構成で windows81 が linuxの teamserver にはいていくのを
openbsdが止めます。

internet
|
wifi router---windows81
|
urtwn0 dhcp
openbsd:(fire wall=pf+squid)
fxp0 192.168.64.1/24(dhcpd)
|
dhcp
linux




in order to access internet by linux PC
(firefox edit>preference>adavance>setting>http proxy 192.168.64.1 port 3128




# ./comment-out.bat  /etc/squid/squid.conf                                                                  
acl localnet src 10.0.0.0/8     # RFC1918 possible internal network
acl localnet src 172.16.0.0/12  # RFC1918 possible internal network
acl localnet src 192.168.0.0/16 # RFC1918 possible internal network
acl localnet src fc00::/7       # RFC 4193 local private network range
acl localnet src fe80::/10      # RFC 4291 link-local (directly plugged) machines
acl SSL_ports port 443
acl Safe_ports port 80          # http
acl Safe_ports port 21          # ftp
acl Safe_ports port 443         # https
acl Safe_ports port 70          # gopher
acl Safe_ports port 210         # wais
acl Safe_ports port 1025-65535  # unregistered ports
acl Safe_ports port 280         # http-mgmt
acl Safe_ports port 488         # gss-http
acl Safe_ports port 591         # filemaker
acl Safe_ports port 777         # multiling http
acl CONNECT method CONNECT
http_access deny !Safe_ports
http_access deny CONNECT !SSL_ports
http_access allow localhost manager
http_access deny manager
http_access allow localnet
http_access allow localhost
http_access deny all
http_port 3128
coredump_dir /var/squid/cache
refresh_pattern ^ftp:           1440    20%     10080
refresh_pattern ^gopher:        1440    0%      1440
refresh_pattern -i (/cgi-bin/|\?) 0     0%      0
refresh_pattern .               0       20%     4320



# ./comment-out.bat  /etc/pf.conf                                                                           
ext_if="urtwn0"
int_if="fxp0"
tcp_services="{ 22, 113 }"
icmp_types="echoreq"
set block-policy return
set loginterface $ext_if
set skip on lo
set reassemble yes no-df
pass in quick log on $ext_if inet proto tcp to port 80 divert-to 127.0.0.1 port 3128
pass out quick from 127.0.0.1 divert-reply
anchor "ftp-proxy/*"
pass in quick on $int_if inet proto tcp to any port ftp \
    divert-to 127.0.0.1 port 8021
block in log
pass out quick
antispoof quick for { lo $int_if }
pass in  on  $ext_if   inet proto tcp from any to  ( $ext_if:0 ) port  $tcp_services
pass in inet proto icmp all icmp-type $icmp_types
pass in on $int_if






すると win81  のteamviewer から linuxのteamviewer に接続しよ うとしても
パー トナーへの接続はありません
となって接続を拒否される。
つまり openbsd firewall が侵入を食い止めた。



ということでサーバーとして使うなら やはりopenbsdが簡単で 堅牢 です。
特に ftpサーバー設定でその違い がわかるでしょう。
ただしGUIは下手です。

そのため自分はarch linuxを 使ってます。
これなら 2015/7/5の 時点で カーネルは
uname -r
4.0.7-2-ARCH
と最新になってます。










私見ですが、teamviewerをしこまれたPCは 「私はここよ。 私の声に乗って私を連れに来て」と 80番ポー ト?で声をあげます。 従ってどこにいてもteamviewerでそこに入っていけます。
も し万一このPCに起爆装置がつながっていたら、遠隔操作で爆発します。
も しカメラがつながっていたら盗撮ですし、 マイクならば盗聴されます。
便 利ですが ドローン並みに危ない面があります。

unbound について

4)  unbound
/etc/resolv.conf を nameserver 127.0.0.1 にして /etc/rc.d/unbound -f restart だけで動きます。
http://gihyo.jp/admin/feature/01/unbound/0001  によると以下です。
Unbound は本来はキャシュサーバーなのですが、簡易的なコンテンツサーバとしての機能を持ち,LAN内 のホストの名前の解決などに利用できます。 
なおDNSコ ンテンツサーバは権威サーバとも呼ばれ,自身が権威を持っているゾーンに対する問い合わせのみに回答します。
参考: http://murasaki.cocolog-nifty.com/cloud/2009/07/unbound-1f2d.html




全体の状況は

internet
|
|
|
run0:dhcp
<openbsd(mail+dhcpd server)>
bge0:192.168.11.1
|
|
|
fxp0:dhcp
<PC>

PC send and revieve mail by sylpheed .



サーバーの /etc/dhcpd.intefaces                                       
bge0


サーバーの /etc/dhcpd.conf                                            
option  domain-name-servers 192.168.11.1;
subnet 192.168.11.0 netmask 255.255.255.0 {
        option routers 192.168.11.1;
        range 192.168.11.50 192.168.11.57;
}


サーバーの /etc/resolv.conf                                           
nameserver 127.0.0.1

############# 

以下は不安定ですが 一応ローカルLANでメールの送受信ができます。


/var/unbound/etc/unbound.conf                                        <
-----------------
server:
        interface: 127.0.0.1
        interface: ::1
        access-control: 0.0.0.0/0 refuse
        access-control: 127.0.0.0/8 allow
        access-control: ::0/0 refuse
        access-control: ::1 allow
        hide-identity: yes
        hide-version: yes
###############################--->join name & adress
        interface: 192.168.11.1
        access-control: 192.168.11.0/24 allow
        local-zone: "home." static
        local-data: "server.home.  IN A 192.168.11.1"
        local-data: "kerai.home. IN A 192.168.11.50"
        local-data-ptr: "192.168.11.1   server.home."
        local-data-ptr: "192.168.11.50   kerai.home."
###############################--->to build mail server
 local-data: "home. IN MX 10 server.home."
 local-data-ptr: "192.168.11.1   server.home."



#  /etc/hosts                                                 
-------------------------
127.0.0.1       localhost
::1             localhost
192.168.100.101 aoiyuma.mydns.jp
192.168.11.1    server.home


 
# /etc/myname                                                
-------------------------
aoiyuma.mydns.jp



# /etc/postfix/main.cf                                       
------------------------------------------
myhostname = server.home
mydomain = home
myorigin = $mydomain
inet_interfaces = all
mydestination = $myhostname localhost.$mydomain
home_mailbox = Maildir/
mynetworks = 192.168.11.0/24, 127.0.0.0/8
queue_directory = /var/spool/postfix
command_directory = /usr/local/sbin
daemon_directory = /usr/local/libexec/postfix
data_directory = /var/postfix
mail_owner = _postfix
inet_protocols = all
unknown_local_recipient_reject_code = 550
debug_peer_level = 2
debugger_command =
         PATH=/bin:/usr/bin:/usr/local/bin:/usr/X11R6/bin
         ddd $daemon_directory/$process_name $process_id & sleep 5
sendmail_path = /usr/local/sbin/sendmail
newaliases_path = /usr/local/sbin/newaliases
mailq_path = /usr/local/sbin/mailq
setgid_group = _postdrop
html_directory = /usr/local/share/doc/postfix/html
manpage_directory = /usr/local/man
sample_directory = /etc/postfix
readme_directory = /usr/local/share/doc/postfix/readme
meta_directory = /etc/postfix
shlib_directory = no





/etc/postfix/master.cf                                        
--------------------------------------
smtp      inet  n       -       -       -       -       smtpd
pickup    unix  n       -       -       60      1       pickup
cleanup   unix  n       -       -       -       0       cleanup
qmgr      unix  n       -       -       300     1       qmgr
tlsmgr    unix  -       -       -       1000?   1       tlsmgr
rewrite   unix  -       -       -       -       -       trivial-rewrite
bounce    unix  -       -       -       -       0       bounce
defer     unix  -       -       -       -       0       bounce
trace     unix  -       -       -       -       0       bounce
verify    unix  -       -       -       -       1       verify
flush     unix  n       -       -       1000?   0       flush
proxymap  unix  -       -       n       -       -       proxymap
proxywrite unix -       -       n       -       1       proxymap
smtp      unix  -       -       -       -       -       smtp
relay     unix  -       -       -       -       -       smtp
showq     unix  n       -       -       -       -       showq
error     unix  -       -       -       -       -       error
retry     unix  -       -       -       -       -       error
discard   unix  -       -       -       -       -       discard
local     unix  -       n       n       -       -       local
virtual   unix  -       n       n       -       -       virtual
lmtp      unix  -       -       -       -       -       lmtp
anvil     unix  -       -       -       -       1       anvil
scache    unix  -       -       -       -       1       scache



/etc/dovecot/dovecot.conf                                        <
protocols = imap
listen = *
!include conf.d/*.conf




/etc/dovecot/conf.d/10-mail.conf  
   mail_location = maildir:~/Maildir
namespace inbox {
  inbox = yes
}
mmap_disable = yes
first_valid_uid = 1000
mail_plugin_dir = /usr/local/lib/dovecot
mbox_write_locks = fcntl





/etc/dovecot/conf.d/10-master.conf                                        
<
service imap-login {
  inet_listener imap {
  }
  inet_listener imaps {
  }
}
service pop3-login {
  inet_listener pop3 {
  }
  inet_listener pop3s {
  }
}
service lmtp {
  unix_listener lmtp {
  }
}
service imap {
}
service pop3 {
}
service auth {
  unix_listener auth-userdb {
  }
}
service auth-worker {
}
service dict {
  unix_listener dict {
  }
}





 /etc/dovecot/conf.d/10-auth.conf 
auth_mechanisms = plain login
!include auth-system.conf.ext
disable_plaintext_auth = no





/etc/dovecot/conf.d/10-ssl.conf                                        <
ssl = yes
ssl_cert = </etc/ssl/dovecotcert.pem
ssl_key = </etc/ssl/private/dovecot.pem






あとは sylpheed で
メールアドレス t@server.home
imap
送受信さーばー ともに server.home
高度な設定でポートを25と143
にするだけです。

intranet で unbound と opensmtpd で mail server をつくる

2) 
intranet で unbound + opensmtpd で作る 
               mail server

    本来の権威ネームサーバ NSD管理の代わりに、 キャシュサーバとして生まれたunboubを 簡易的に権威サーバーとして代用します 
                                     (http://gihyo.jp/admin /feature/01/unbound /0001)



 
全体の状況は

internet
|
|
wifi router
dhcpd|
|
run0:dhcpcd
<openbsd(opensmtpf + dovecot + dhcpd server)>
bge0:192.168.11.1
|
|
fxp0:dhcp
<LINUX thunderbird>



1) setting of openbsd server

a) about dhcpd
/etc/dhcpd.intefaces                                       
bge0


/etc/dhcpd.conf                                            
option  domain-name-servers 192.168.11.1;
subnet 192.168.11.0 netmask 255.255.255.0 {
        option routers 192.168.11.1;
        range 192.168.11.50 192.168.11.57;
}



b) 
/etc/hosts                                                 
-------------------------
127.0.0.1       localhost
::1             localhost
192.168.100.101 aoiyuma.mydns.jp
192.168.11.1    server.home

/etc/myname                                                
-------------------------
aoiyuma.mydns.jp


c)dovecot -n
--------------------------
# 2.2.15: /etc/dovecot/dovecot.conf
# OS: OpenBSD 5.7 i386  
auth_mechanisms = plain login
disable_plaintext_auth = no
first_valid_uid = 1000
imap_client_workarounds = delay-newmail tb-extra-mailbox-sep tb-lsub-flags
listen = *
mail_location = maildir:~/Maildir
mbox_write_locks = fcntl
mmap_disable = yes
namespace inbox {
  inbox = yes
  location = 
  mailbox Drafts {
    special_use = \Drafts
  }
  mailbox Junk {
    special_use = \Junk
  }
  mailbox Sent {
    special_use = \Sent
  }
  mailbox "Sent Messages" {
    special_use = \Sent
  }
  mailbox Trash {
    special_use = \Trash
  }
  prefix = 
}
passdb {
  driver = bsdauth
}
pop3_client_workarounds = outlook-no-nuls oe-ns-eoh
protocols = imap
ssl_cert = </etc/ssl/dovecotcert.pem
ssl_key = </etc/ssl/private/dovecot.pem
userdb {
  driver = passwd


 /etc/dovecot/dovecot.conf                                        <
------------------------
protocols = imap
listen = *
!include conf.d/*.conf


/etc/dovecot/conf.d/10-mail.conf  
-------------------------
   mail_location = maildir:~/Maildir
namespace inbox {
  inbox = yes
}
mmap_disable = yes
first_valid_uid = 1000
mail_plugin_dir = /usr/local/lib/dovecot
mbox_write_locks = fcntl


/etc/dovecot/conf.d/10-master.conf                                        
----------------------------------
service imap-login {
  inet_listener imap {
  }
  inet_listener imaps {
  }
}
service pop3-login {
  inet_listener pop3 {
  }
  inet_listener pop3s {
  }
}
service lmtp {
  unix_listener lmtp {
  }
}
service imap {
}
service pop3 {
}
service auth {
  unix_listener auth-userdb {
  }
}
service auth-worker {
}
service dict {
  unix_listener dict {
  }
}



/etc/dovecot/conf.d/10-auth.conf 
--------------------------------
auth_mechanisms = plain login
!include auth-system.conf.ext
disable_plaintext_auth = no


/etc/dovecot/conf.d/10-ssl.conf                                        <
--------------------------------
ssl = yes
ssl_cert = </etc/ssl/dovecotcert.pem
ssl_key = </etc/ssl/private/dovecot.pem




d)
first openbsd server runs dhcpcd under wifi-router(dhcpd)
so

/etc/hostname.run0                                                                                   
------------------
nwid URoad-662EA0
wpakey 04607271
dhcp

but when i start openbsd as serve , i run the next script .
 mail-server.bat
------------------------------------------------                                                                       
cp /etc/resolv.conf-127    /etc/resolv.conf
cp /etc/hostname.run0-fix  /etc/hostname.run0

/etc/rc.d/unbound restart

/etc/rc.d/smtpd   restart
/etc/rc.d/dovecot  restart


/etc/resolv.conf-127
----------------------------
nameserver 127.0.0.1



/etc/hostname.run0-fix                                                                               
-----------------------
inet 192.168.100.101 255.255.255.0


e)UNBOUND

/var/unbound/etc/unbound.conf
-------------------------------                                      
server:
        interface: 127.0.0.1
        interface: ::1
        access-control: 0.0.0.0/0 refuse
        access-control: 127.0.0.0/8 allow
        access-control: ::0/0 refuse
        access-control: ::1 allow
        hide-identity: yes
        hide-version: yes

        interface: 192.168.11.1
        access-control: 192.168.11.0/24 allow
        local-zone: "home." static
        local-data: "server.home.  IN A 192.168.11.1"
        local-data: "kerai.home. IN A 192.168.11.50"
        local-data-ptr: "192.168.11.1   server.home."
        local-data-ptr: "192.168.11.50   kerai.home."
        local-data: "home. IN MX 10 server.home."
        local-data-ptr: "192.168.11.1   server.home."
/




2) linux thunderbird
smtp
server name : server.home
port 25
authentification method: password, transmitted insecure
conection security     : none


imap
server name : server.home
port 143

authentification method: normal password
conection security     : STARTTLS



X) in this setting

i send mail to X@
server.home and recieve mail from X@server.home .

but
i cannot send mail from   Y@gmx.com      to   
X@server.home
and
i cannot send mail from   X@server.home  to    Y@gmx.com .

this is safe setting considering of virus from internet .
when i want to send or recieve mail with Z@gmail.com , i use Y@gmx.com .

さらに
/etc /pf.confを 以下のようにすると 

ext_if="run0"
int_if="bge0"
tcp_services="{ 22, 80 }"
icmp_types="echoreq"
set block-policy return
set loginterface $ext_if
set skip on lo
match out on $ext_if inet from ($int_if:network) to any nat-to ($ext_if:0)
set reassemble yes no-df
block in log
pass out quick
antispoof quick for { lo $int_if }
pass in  on  $ext_if   inet proto tcp from any to  ( $ext_if:0 ) port  $tcp_services
pass in inet proto icmp all icmp-type $icmp_types
pass in on $int_if
外からのメールはgmx.comを利用しても入ってこなくなります。
ただ
 gmx.comを利用して @internet.com に秘密をおくれます。

もちろん
ext_if="run0"
int_if="bge0"
tcp_services="{ 22, 80 }"
icmp_types="echoreq"
set block-policy return
set loginterface $ext_if
set skip on lo

###  match out on $ext_if inet from ($int_if:network) to any nat-to ($ext_if:0)
set reassemble yes no-df
block in log
pass out quick
antispoof quick for { lo $int_if }
pass in  on  $ext_if   inet proto tcp from any to  ( $ext_if:0 ) port  $tcp_services
pass in inet proto icmp all icmp-type $icmp_types
pass in on $int_if

とすれば intraネット内からメールも遅れなくなりますが、wwwも見れなくなります。
でも web mailの存在を考えると ここまでしなくてはいけないかもしれません。






opensmtpd と  dovecot  で mail server をつくる

1)レンタルサーバーで無料の mydns.jpを利用し opensmtpd +   dovecot  で mail server




  1)全体の状況は以下です。

openbsd:mailserverは いわゆるレンタルサーバーで す。                 mailserver is build on called rental server .
ここにメールサーバーを立ち上げて 
linux : thunderbird から使います。i use this server on linux's thunderbird .

linux : thunderbird
1.2.3.4
|
|
|
internet-----em0:openbsd:mailserver( opensmtpd + dovecot)




2)MXを設定するために                                                              i set mx by the following procedure

mydns



きちんと設定できれば以下となります
host aoiyuma.mydns.jp
then
aoiyuma.mydns.jp has address 157.7.208.141
aoiyuma.mydns.jp mail is handled by 10 mail.aoiyuma.mydns.jp.



3)dovecot -n の 結果は、
# 2.2.15: /etc/dovecot/dovecot.conf
# OS: OpenBSD 5.7 amd64 
auth_mechanisms = plain login
disable_plaintext_auth = no
first_valid_uid = 1000
imap_client_workarounds = delay-newmail tb-extra-mailbox-sep tb-lsub-flags
listen = *
mail_location = maildir:~/Maildir
mbox_write_locks = fcntl
mmap_disable = yes
namespace inbox {
  inbox = yes
  location =
  mailbox Drafts {
    special_use = \Drafts
  }
  mailbox Junk {
    special_use = \Junk
  }
  mailbox Sent {
    special_use = \Sent
  }
  mailbox "Sent Messages" {
    special_use = \Sent
  }
  mailbox Trash {
    special_use = \Trash
  }
  prefix =
}
passdb {
  driver = bsdauth
}
pop3_client_workarounds = outlook-no-nuls oe-ns-eoh
protocols = imap
ssl_cert = </etc/ssl/dovecotcert.pem
ssl_key = </etc/ssl/private/dovecot.pem
userdb {
  driver = passwd
}

こうなるためには


/etc/dovecot/dovecot.conf          
protocols = imap imaps
listen = *
!include conf.d/*.conf





/etc/dovecot/conf.d/10-master.conf 
service imap-login {
  inet_listener imap {
  }
  inet_listener imaps {
    port = 993
    ssl = yes
  }
}
service pop3-login {
  inet_listener pop3 {
  }
  inet_listener pop3s {
  }
}
service lmtp {
  unix_listener lmtp {
  }
}
service imap {
}
service pop3 {
}
service auth {
  unix_listener auth-userdb {
  }
}
service auth-worker {
}
service dict {
  unix_listener dict {
  }
}




/etc/dovecot/conf.d/10-mail.conf                                        <
   mail_location = maildir:~/Maildir
namespace inbox {
  inbox = yes
}
mmap_disable = yes
first_valid_uid = 1000
mail_plugin_dir = /usr/local/lib/dovecot
mbox_write_locks = fcntl



 /etc/dovecot/conf.d/10-ssl.conf                                        <
ssl = yes
ssl_cert = </etc/ssl/dovecotcert.pem
ssl_key = </etc/ssl/private/dovecot.pem


4) /etc/login.conf                                                                              
auth-defaults:auth=passwd,skey:
auth-ftp-defaults:auth-ftp=passwd:
default:\
        :path=/usr/bin /bin /usr/sbin /sbin /usr/X11R6/bin /usr/local/bin /usr/local/sbin:\
        :umask=022:\
        :datasize-max=512M:\
        :datasize-cur=512M:\
        :maxproc-max=256:\
        :maxproc-cur=128:\
        :openfiles-cur=512:\
        :stacksize-cur=4M:\
        :localcipher=blowfish,8:\
        :ypcipher=old:\
        :tc=auth-defaults:\
        :tc=auth-ftp-defaults:
daemon:\
        :ignorenologin:\
        :datasize=infinity:\
        :maxproc=infinity:\
        :openfiles-cur=128:\
        :stacksize-cur=8M:\
        :localcipher=blowfish,9:\
        :tc=default:
staff:\
        :datasize-cur=1536M:\
        :datasize-max=infinity:\
        :maxproc-max=512:\
        :maxproc-cur=256:\
        :ignorenologin:\
        :requirehome@:\
        :tc=default:
authpf:\
        :welcome=/etc/motd.authpf:\
        :shell=/usr/sbin/authpf:\
        :tc=default:
bgpd:\
        :openfiles-cur=512:\
        :tc=daemon:
unbound:\
        :openfiles-cur=512:\
        :tc=daemon:
dovecot:\
        :openfiles-cur=2048:\
        :openfiles-max=4096:\
        :tc=daemon:



http://cvsweb.openbsd.org/cgi-bin/cvsweb/~checkout~/ports/mail/dovecot/pkg/README-server?rev=1.2
では
dovecot:\
    :openfiles-cur=512:\
    :openfiles-max=2048:\
    :tc=daemon:

5)/etc/pf.conf                                                                    
ext_if="em0"
tcp_services="{ 22, 25, 80, 110, 143, 465, 587 }"
icmp_types="echoreq"
set block-policy return
set loginterface $ext_if
set skip on lo
set reassemble yes no-df
block in log
pass out quick
antispoof quick for { lo }
pass in  on  $ext_if   inet proto tcp from any to  ( $ext_if:0 ) port  $tcp_services
pass in inet proto icmp all icmp-type $icmp_types
pass in on $ext_if proto tcp to port 21
pass in on $ext_if proto tcp to port > 4915




6)/etc/mail/smtpd.conf
 send mail to anything but your local domains.

鎖国?
A@openbsd.link と B@openbsd.link の間の送受信は可能。
しかし X@gmail.comとの送受信は不可能。

だから 安全 
開 国

X@gmail.comとの送受信は可能
でも ウィルスメールがはいってもこれます。
listen on lo0
listen on em0 port 587

table aliases db:/etc/mail/aliases.db

accept  from any for domain "aoiyuma.mydns.jp" alias <aliases> deliver to maildir
accept from any for domain "aoiyuma.mydns.jp"        deliver to maildir

accept for local alias <aliases> deliver to maildir
accept for local deliver to maildir

reject from any for any



gmailは sendingに465を使っている。
587にしたのでgmailからのが受信できないとも考えられる。
じゃあ 465にしたらどうかというと 
こうしてもgmailからのは受信できないし、
更に悪いことにはaoiyuma.mydns.jpか らのも受信できなくなってしまった。

で  587にした。
ただし
送信に587を使うメールサーバーから実際に受信できないかは未確認
なので、心配は残るが、 gmail 465のことを考えると大丈夫かな?


listen on lo0
listen on em0 port 25 
listen on em0 port 465
listen on em0 port 587

table aliases db:/etc/mail/aliases.db

accept      from any           for domain "aoiyuma.mydns.jp"      alias <aliases>      deliver to maildir

accept from any   for domain "aoiyuma.mydns.jp"   deliver to maildir

accept for local           alias <aliases>   deliver to maildir
accept for local                              deliver to maildir


mynetwork1 = "14.22.25.247/32"
mynetwork2 = "6.2.6.2/32"
accept from source $mynetwork1 for any relay
accept from source $mynetwork2 for any relay
accept from local for any relay
d

# nano /etc/mail/aliases
# makemap /etc/mail/aliases
  は newaliasesに相当。

 STARTTLS にする。
openssl genrsa -out /etc/ssl/private/mail.aoiyuma.mydns.jp.key 4096
openssl req -new -x509 -key /etc/ssl/private/mail.aoiyuma.mydns.jp.key -out /etc/ssl/mail.aoiyuma.mydns.jp.crt -days 365
chmod 600 /etc/ssl/mail.aoiyuma.mydns.jp.crt
chmod 600 /etc/ssl/private/mail.aoiyuma.mydns.jp.key


cat jp3
6.2.6.2/32 114.22.25.247/32

makemap jp3

from http://technoquarter.blogspot.jp/2015/02/openbsd-mail-server-part-2-opensmtpd.html

so it listens
on egress with tls (for incoming mail) and
egress port 587 (submission) with tls and authentication (for outgoing mail),
accepts mail for virtual users and virtual domains, and delivers this mail to Maildir.


smtpd.conf
は以下です

pki mail.aoiyuma.mydns.jp certificate "/etc/ssl/mail.aoiyuma.mydns.jp.crt"
pki mail.aoiyuma.mydns.jp key "/etc/ssl/private/mail.aoiyuma.mydns.jp.key"

listen on em0                 tls          pki   mail.aoiyuma.mydns.jp   auth-optional
listen on em0 port submission tls-require pki   mail.aoiyuma.mydns.jp   auth

table aliases db:/etc/mail/aliases.db
table jp3 db:/etc/mail/jp3.db

accept from any         for domain "aoiyuma.mydns.jp"   alias <aliases> deliver to maildir
accept from any         for domain "aoiyuma.mydns.jp"                   deliver to maildir

accept for local                                        alias <aliases> deliver to maildir
accept for local                                                        deliver to maildir

accept from source <jp3>        for any relay
accept from local               for any relay


あと /home/fogeがあったとしたら それのshellがたとえfalse でも
mkdir -p /home/foge/Mairdir/{new,cur,tmp}
chown -R foge.foge /home/foge/Mairdir/
chmod -R 400 /home/foge/Mairdir/
して fogeがメールを利用できないようにした。
https://ipv4.fetus.jp/
tr '\n' ' ' <jp.tx

STARTTLS + spamd です,しかし不安定。
mail+nginx-server.bat-openSMTPD-spam   を実行するだけです。                                                                 
pfctl -f /etc/mail/black.pf
pfctl -sr
/etc/rc.d/smtpd         restart
/etc/rc.d/dovecot       restart
echo '----------------'
echo '----------------'
echo '----------------'
echo '----------------'
/etc/rc.d/spamd         -f restart                                                                                                           
ps ax| grep spam
/etc/rc.d/spamlogd      -f restart
ps ax| grep spam
/usr/libexec/spamd-setup


ここは http://technoquarter.blogspot.jp にすっかりお世話になりました。
また opensmtpのmailing listからもたくさんの助言をいただきました。


Incoming mail:
pf -> relay to spamd -> send to opensmtpd on lo0 -> deliver to maildir

Outoing mail:
opensmtpd on lo0 -> relay out
とあるので

openssl genrsa -out /etc/ssl/private/mail.aoiyuma.mydns.jp.key 4096
openssl req -new -x509 -key /etc/ssl/private/mail.aoiyuma.mydns.jp.key -out /etc/ssl/mail.aoiyuma.mydns.jp.crt -days 365
chmod 600 /etc/ssl/mail.aoiyuma.mydns.jp.crt
chmod 600 /etc/ssl/private/mail.aoiyuma.mydns.jp.key
してから
                                                         
smtpd.conf  
pki mail.aoiyuma.mydns.jp certificate "/etc/ssl/mail.aoiyuma.mydns.jp.crt"
pki mail.aoiyuma.mydns.jp key "/etc/ssl/private/mail.aoiyuma.mydns.jp.key"
listen on lo0
listen on em0                 tls         pki   mail.aoiyuma.mydns.jp   auth-optional
listen on em0 port submission tls-require pki   mail.aoiyuma.mydns.jp   auth
table aliases db:/etc/mail/aliases.db
accept from any         for domain "aoiyuma.mydns.jp"                   deliver to maildir
accept for local                                        alias <aliases> deliver to maildir
accept from local



 /etc/mail/black.pf 
ext_if="em0"
tcp_services="{  22,     80,      143 }"
icmp_types="echoreq"
set block-policy return
set loginterface $ext_if
set skip on lo
set reassemble yes no-df
block in log
pass out quick
antispoof quick for { lo }
pass in on $ext_if proto tcp to any port submission
table <spamd-white> persist
table <nospamd> persist file "/etc/mail/nospamd"
pass in         on $ext_if proto tcp from any to any port smtp rdr-to 127.0.0.1 port spamd
pass in on $ext_if proto tcp from <nospamd> to any port  smtp
pass in log     on $ext_if proto tcp from any to any port smtp
pass out log on $ext_if proto tcp to any port smtp
pass in  on  $ext_if   inet proto tcp from any to  ( $ext_if:0 ) port  $tcp_services
pass in inet proto icmp all icmp-type $icmp_types
pass in on $ext_if proto tcp to port 21
pass in on $ext_if proto tcp to port > 4915




/etc/rc.conf.local                                                                                      
ftpd_flags="-llUSA"
smtpd_flags=""
pf=YES                  # Packet filter / NAT
pf_rules=/etc/pf.conf           # Packet filter rules file



cat /etc/mail/nospamd                                                                                                     
157.7.208.141   #自分自身




一応
Return to RBL.JP home page は以下のように合格。

Relay test 19

>>> RSET
relay NOT accepted!!
Closing connection ...

>>> QUIT

Relay test result

All tests performed, no relays accepted.

http://www.noroi.jp/?date=20070415
で は
spamd の運用では、pf <spamd> と pf <spamd-white> を維持する必要がある。
前者は spamd-setup を定期的に実行して、 最近のブラックリストをネットから持ってくる。
後者は spamd 自身が /var/db/spamdb の WHITE データで定期的に置き替える。
 netstat -na -f inet
/usr/libexec/spamd-setup


# ls -l /var/db/spamd
-rw-r--r--  1 _spamd  _spamd  65536 Aug  1 15:03 /var/db/spamd




7) thunderbird setting of google mail
arch linux's thunderbird automatically detect them (openbsd's one cannot).

smtp.googlemail.com
465
auth              : Oauht2
conectio security : SSK/TLS
imap.googlemail.com
993
auth              : Oauht2
conectio security : SSK/TLS


smtp mail.aoiyuma.mydns.jp 587
auth              : normal password
conectio security : STARTTLS          
imap mail.aoiyuma.mydns.jp 143
auth              : normal password
conectio security : STARTTLS
abobe is the same setting of gmx.com



付録1)開国していて カフェにはいったと きの対処方法

カフェで
shllscript
curl ifconfig.me > ifconfigme.txt
scp  ifconfigme.txt tu@aoiyuma.mydns.jp:/home/tu






aoiyuma.mydns.jp にsshで入って
shllscript
y=`cat ./ifconfigme.txt`
cp /etc/mail/smtpd.conf /etc/mail/smtpd.conf-back
sed "s/xxx/$y\/32/g" kt  > /etc/mail/smtpd.conf
/etc/rc.d/smtpd restart




ただし ktは以下です

listen on lo0
listen on em0 port 25 
listen on em0 port 465
listen on em0 port 587
table aliases db:/etc/mail/aliases.db
accept from any         for domain "aoiyuma.mydns.jp"   alias <aliases> deliver to maildir
accept from any         for domain "aoiyuma.mydns.jp"                   deliver to maildir
accept for local                                        alias <aliases> deliver to maildir
accept for local                                                        deliver to maildir
mynetwork = "xxx"
accept from source $mynetwork for any relay
reject from any for any