Tentang IT dan Komputer

Teknologi Informasi

Qmail Rock

Posted by intrik on May 3, 2007

1stalasi qmail
Sebagai informasi, p Inada tutorial ini menggunakan:
[admin@www ~]$ cat /proc/meminfo |grep MemTotal
MemTotal: 970748 kB
[admin@www ~]$ cat /proc/cpuinfo
processor : 0
vendor_id : AuthenticAMD
cpu family : 15
model : 44
model name : AMD Sempron(tm) Processor 2800+
stepping : 2
cpu MHz : 1608.421
cache size : 256 KB
[admin@www ~]$ uname -a
Linux http://www.my.domain 2.6.16-1.2111_FC4 #1 Sat May 20 19:59:40 EDT 2006 i686 athlon i386 GNU/Linux
[admin@www ~]$ gcc -v
Using built-in specs.
Target: i386-redhat-linux
Thread model: posix
gcc version 4.0.2 20051125 (Red Hat 4.0.2-8)
1.2 Download dan ekstrak paket qmailrocks
Pertama, buat folder di /downloads. Dalam langkah-langkah instalasi menggunakan folder /downloads. Untuk
mempermudah mengikuti instalasi ini, gunakan folder dan cara yang sama dengan tutorial ini.
[root@server02 /]# mkdir downloads
[root@server02 /]# cd /downloads/
[root@server02 downloads]# wget http://www.qmailrocks.org/downloads/qmailrocks.tar.gz
[root@server02 downloads]# tar zxvf qmailrocks.tar.gz
]# pwd;ls
/downloads
qmailrocks qmailrocks.tar.gz
[root@server02 downloads]# /downloads/qmailrocks/scripts/install/qmr_install_linux-s1.script
[root@server02 downloads]# /downloads/qmailrocks/scripts/util/qmail_big_patches.script
[root@server02 downloads]# cd /usr/src/qmail/qmail-1.03
[root@server02 qmail-1.03]# make man && make setup check
[root@server02 qmail-1.03]# ./config-fast your_hostname (contoh: ./config-fast sakura4.prilog.net)
Selanjutnya membuat sertivikat keamaanan (sesuaikan dengan kondisi domain Anda):
[root@server02 qmail-1.03]# make cert
Country Name (2 letter code) [GB]:JP
State or Province Name (full name) [Berkshire]:Tky
Locality Name (eg, city) [Newbury]:Tky
Organization Name (eg, company) [My Company Ltd]:prilog.net
Organizational Unit Name (eg, section) []:mail
Common Name (eg, your name or your server’s hostname) []:mail.prilog.net
Email Address []:postmaster@prilog.net
[root@server02qmail-1.03]# chown -R vpopmail:qmail /var/qmail/control/clientcert.pem
/var/qmail/control/servercert.pem
1.3 Install ucspi-tcp
[root@server02 qmail-1.03]# cd /usr/src/qmail/ucspi-tcp-0.88/
[root@server02 ucspi-tcp-0.88]# patch < /downloads/qmailrocks/patches/ucspi-tcp-0.88.errno.patch
[root@server02 ucspi-tcp-0.88]# make && make setup check
1.4 Install daemontools
[root@server02 daemontools-0.76]# cd /package/admin/daemontools-0.76/src
[root@server02 src]# patch < /downloads/qmailrocks/patches/daemontools-0.76.errno.patch
[root@server02 src]# cd /package/admin/daemontools-0.76
[root@server02 daemontools-0.76]# package/install
1.5 Install Ezmlm
root@server02 daemontools-0.76]# cd /downloads/qmailrocks/
[root@server02 qmailrocks]# tar zxvf ezmlm-0.53-idx-0.41.tar.gz
[root@server02 ezmlm-0.53-idx-0.41]# cd ezmlm-0.53-idx-0.41
[root@server02 ezmlm-0.53-idx-0.41]# make && make setup
1.6 Install autorespon
[root@server02 ezmlm-0.53-idx-0.41]# cd /downloads/qmailrocks
[root@server02 qmailrocks]# tar zxvf autorespond-2.0.5.tar.gz
[root@server02 qmailrocks]# cd autorespond-2.0.5
[root@server02 autorespond-2.0.5]# make && make install
1.7 Install Vpopmail
[root@server02 autorespond-2.0.5]# cd /downloads/qmailrocks
[root@server02 qmailrocks]# tar zxvf vpopmail-5.4.13.tar.gz
[root@server02 qmailrocks]# cd vpopmail-5.4.13
[root@server02 vpopmail-5.4.13]# ./configure –enable-logging=p
[root@server02 vpopmail-5.4.13]# make && make install-strip
1.8 Install Vqadmin
[root@server02 qmailrocks]# cd /downloads/qmailrocks
[root@server02 qmailrocks]# tar zxvf vqadmin-2.3.6.tar.gz
[root@server02 qmailrocks]# cd vqadmin-2.3.6
Sesuaikan dengan kondisi letak folder cgi-bin dan DocumentRoot (lihat httpd.conf) Anda , secara devault cgi-bin di
/var/www/cgi-bin dan documentRoot di /var/www/html.
[root@server02 vqadmin-2.3.6]# ./configure –enable-cgibindir=/home/htdocs/cgi-bin –enablehtmldir=/
home/htdocs/www
[root@server02 vqadmin-2.3.6]# make && make install-strip
Tambahkan script berikut pada httpd.conf (ingat; sesuaikan dengan letak folder cgi-bin Anda):

deny from all
Options ExecCGI
AllowOverride AuthConfig
Order deny,allow

Untuk membatasi pengaksesas vqadmin melalui web kita perlu membuat password:
[root@server02 vqadmin-2.3.6]# cd /home/htdocs/cgi-bin/vqadmin/
[root@server02 vqadmin]# vi .htaccess
Tambah atau modifikasi menjadi:
AuthType Basic
#(Silahkan disesuikan)
AuthUserFile /etc/httpd/conf/vqadmin.passwd
AuthName vQadmin
require valid-user
satisfy any
Setelah disimpan, kemudian lanjutkan perintah:
[root@server02 vqadmin]# touch /etc/httpd/conf/vqadmin.passwd (Silahkan disesuikan)
[root@server02 vqadmin]# chown apache .htaccess
[root@server02 vqadmin]# chmod 644 .htaccess
[root@server02 vqadmin]# htpasswd -bc /etc/httpd/conf/vqadmin.passwd admin passwdadminnya
Adding password for user admin
[root@server02 vqadmin]# chmod 644 /etc/httpd/conf/vqadmin.passwd
Restart apache:
[root@server02 vqadmin]# /etc/rc.d/init.d/httpd stop
httpd を停止中: [ OK ]
[root@server02 vqadmin]# /etc/rc.d/init.d/httpd start
httpd を起動中: [ OK ]
Jika Berjalan dengan baik, Anda bisa mengunjungi via web http://hostname_anda/cgi-bin/vqadmin/vqadmin.cgi.
Anda akan ditanyai user name dan password, silahkan masukan sesuai perintah yang Anda lakukan tadi.
1.9 Install Maildrop
[root@server02 cgi-bin]# cd /downloads/qmailrocks
[root@server02 qmailrocks]# tar zxvf maildrop-1.6.3.tar.gz
[root@server02 qmailrocks]# cd maildrop-1.6.3
[root@server02 maildrop-1.6.3]# ./configure –prefix=/usr/local –exec-prefix=/usr/local –enable-maildropuid=
root –enable-maildrop-gid=vchkpw –enable-maildirquota
[root@server02 maildrop-1.6.3]# make && make install-strip && make install-man
1.9 Install qmailadmin
[root@server02 maildrop-1.6.3]# cd /downloads/qmailrocks
[root@server02 qmailrocks]# tar zxvf qmailadmin-1.2.9.tar.gz
[root@server02 qmailrocks]# cd qmailadmin-1.2.9
Sesuaikan dengan letak cgi dir Anda dan documentRoot:
[root@server02 qmailadmin-1.2.9]# ./configure –enable-cgibindir=/home/htdocs/cgi-bin –enablehtmldir=/
home/htdocs/www
[root@server02 qmailadmin-1.2.9]# make && make install-strip
1.9 Finishing…….
[root@server02 qmailadmin-1.2.9]# /downloads/qmailrocks/scripts/finalize/linux/finalize_linux.script
Ubah isi file menyesusaikan domain Anda (pada file diset devault mail.example.com:
[root@server02 qmailadmin-1.2.9]# vi /var/qmail/supervise/qmail-pop3d/run
[root@server02 qmailadmin-1.2.9]# vi /var/qmail/supervise/qmail-smtpd/run
Lanjutkan dengan:
[root@server02 qmailadmin-1.2.9]# qmailctl stop
[root@server02 qmailadmin-1.2.9]# echo ‘127.:allow,RELAYCLIENT=””‘ >> /etc/tcp.smtp
[root@server02 qmailadmin-1.2.9]# qmailctl cdb
[root@server02 qmailadmin-1.2.9]# echo root@server02.com > /var/qmail/alias/.qmail-root
[root@server02 qmailadmin-1.2.9]# echo postmaster@server02.com > /var/qmail/alias/.qmail-postmaster
[root@server02 qmailadmin-1.2.9]# echo root@server02.com > /var/qmail/alias/.qmail-mailer-daemon
[root@server02 qmailadmin-1.2.9]# ln -s /var/qmail/alias/.qmail-root /var/qmail/alias/.qmail-anonymous
[root@server02 qmailadmin-1.2.9]# chmod 644 /var/qmail/alias/.qmail*
Anda bisa kunjungi dengan browser di http://domain.anda/cgi-bin/qmailadmin
1.10 Uninstall sendmail
Unintall sendmail Anda, cek dengan:
rpm -qa|grep sendmail
Semua yang muncul di uninstall.
1.11 Cek intalasi
[root@server02 qmailadmin-1.2.9]#
Congratulations, your Qmailrocks.
1.12 Jalankan qmail
[root@server02 qmailadmin-1.2.9]#
Starting qmail…
Starting qmail-send
Starting qmail-smtpd
Starting qmail-pop3d
Untuk menguji kita dapat menggunakan email client seperti Thunderbird. Oke… Semoga Bermanfaat…!!!
END

Instalasi Qmail + Vpopmail + Qmailadmin + Vqadmin + Squirrelmail
Termasuk Bagian Stuff ditulis 25 May 2006 8:41 am oleh asep
Quick & Dirty on Redhat/Fedora/Centos
Instalasi Redhat/Fedora/Centos seperti biasa baik menggunakan X-Windows atau Command Line Interface :
Kelompok paket yang di install :
– X-Window (Optional)
– KDE atau Gnome, pilih salah satu saja (Optional)
– Text Editor
– Graphical Internet (Optional)
– Text base Internet
– Server Configurations Tools
– Web Server
– Network Server
– Development Tools
– Legacy Software Development
– Administrator Tools
– System Tools

Persiapan instalasi Qmail
Uninstall sendmail
[root@alton qmail]# service sendmail stop
Shutting down sendmail: [ OK ]
Shutting down sm-client: [ OK ]
[root@alton qmail]# for a in $(rpm -qa | grep sendmail); do rpm -e –nodeps $a;done
Download paket-paket & patch qmail,daemontools dan ucspi-tcp & ekstrak filenya:
[root@alton qmail]# wget http://cr.yp.to/software/qmail-1.03.tar.gz
[root@alton qmail]# wget http://cr.yp.to/ucspi-tcp/ucspi-tcp-0.88.tar.gz
[root@alton qmail]# wget http://cr.yp.to/daemontools/daemontools-0.76.tar.gz
[root@alton qmail]# wget http://qmail.cbn.net.id/moni.csi.hu/pub/glibc-2.3.1/qmail-1.03.errno.patch
[root@alton qmail]# wget http://qmail.cbn.net.id/moni.csi.hu/pub/glibc-2.3.1/ucspi-tcp-0.88.errno.patch
[root@alton qmail]# wget http://qmail.cbn.net.id/moni.csi.hu/pub/glibc-2.3.1/daemontools-0.76.errno.patch
[root@alton qmail]# tar -xzvf qmail-1.03.tar.gz
[root@alton qmail]# tar -xzvf daemontools-0.76.tar.gz
[root@alton qmail]# tar -xzvf ucspi-tcp-0.88.tar.gz
Buat user-user untuk qmail
[root@alton qmail]# mkdir /var/qmail
[root@alton qmail]# groupadd nofiles
[root@alton qmail]# useradd -g nofiles -d /var/qmail/alias alias
[root@alton qmail]# useradd -g nofiles -d /var/qmail qmaild
[root@alton qmail]# useradd -g nofiles -d /var/qmail qmaill
[root@alton qmail]# useradd -g nofiles -d /var/qmail qmailp
[root@alton qmail]# groupadd qmail
[root@alton qmail]# useradd -g qmail -d /var/qmail qmailq
[root@alton qmail]# useradd -g qmail -d /var/qmail qmailr
[root@alton qmail]# useradd -g qmail -d /var/qmail qmails
Patch qmail & Install Qmail
[root@alton qmail]# cd qmail-1.03
[root@alton qmail-1.03]# patch -p1 < ../qmail-1.03.errno.patch
[root@alton qmail-1.03]# patch -p1 < ../qmailqueue-patch
[root@alton qmail-1.03]# patch -p1 < ../qmail-1.03-quotas-1.1.patch
Configure untuk domain alton.co.id
[root@alton qmail-1.03]# make setup check
[root@alton qmail-1.03]# ./config-fast alton.co.id
Install ucspi-tcp-0.88
[root@alton qmail-1.03]# cd ../ucspi-tcp-0.88
[root@alton ucspi-tcp-0.88]# patch -p1 < ../ucspi-tcp-0.88.errno.patch
Install Daemontools
[root@alton ucspi-tcp-0.88]# cd ../admin/daemontools-0.76/
[root@alton daemontools-0.76]# patch -p1 < ../../daemontools-0.76.errno.patch
[root@alton daemontools-0.76]# ./package/install
Ganti direktori /command hasil install dengan yang ada di pake daemontools
[root@alton daemontools-0.76]# rm -rf /command/
[root@alton daemontools-0.76]# mv command/ /
Buat qmail boot script
[root@alton daemontools-0.76]# cp /var/qmail/boot/home /var/qmail/rc
[root@alton daemontools-0.76]# perl -pi -e “s/Mailbox splogger qmail/Maildir\//g” /var/qmail/rc
Buat Supervise untuk qmail-send & qmail-smtpd
[root@alton daemontools-0.76]# mkdir -p /var/qmail/supervise/qmail-send/log
[root@alton daemontools-0.76]# mkdir -p /var/qmail/supervise/qmail-smtpd/log
[root@alton daemontools-0.76]# vi /var/qmail/supervise/qmail-send/run
Isinya :
#!/bin/sh
exec /var/qmail/rc
[root@alton daemontools-0.76]# vi /var/qmail/supervise/qmail-send/log/run
Isinya :
#!/bin/sh
exec /usr/local/bin/setuidgid qmaill /usr/local/bin/multilog t s1048576 /var/log/qmail
[root@alton daemontools-0.76]# vi /var/qmail/supervise/qmail-smtpd/run
Isinya :
#!/bin/sh
PATH=$PATH:/usr/local/bin:/var/qmail/bin
export PATH
QMAILDUID=`id -u qmaild`
NOFILESGID=`id -g qmaild`
MAXSMTPD=30
exec /usr/local/bin/softlimit -m 5000000 \
tcpserver -H -R -v -x /etc/tcp.smtp.cdb -c “$MAXSMTPD” -u “$QMAILDUID” -g “$NOFILESGID” \
0 25 qmail-smtpd 2>&1
[root@alton daemontools-0.76]# vi /var/qmail/supervise/qmail-smtpd/log/run
Isinya :
#!/bin/sh
exec /usr/local/bin/setuidgid qmaill /usr/local/bin/multilog t s1048576 /var/log/qmail/smtpd
Rubah mode filenya :
[root@alton daemontools-0.76]# chmod 755 /var/qmail/supervise/qmail-send/run
[root@alton daemontools-0.76]# chmod 755 /var/qmail/supervise/qmail-send/log/run
[root@alton daemontools-0.76]# chmod 755 /var/qmail/supervise/qmail-smtpd/run
[root@alton daemontools-0.76]# chmod 755 /var/qmail/supervise/qmail-smtpd/log/run
Buat direktory untuk lognya
[root@alton daemontools-0.76]# mkdir -p /var/log/qmail/smtpd
[root@alton daemontools-0.76]# chown qmaill /var/log/qmail /var/log/qmail/smtpd
Set relayclient
[root@alton daemontools-0.76]# echo ‘127.0.0.1:allow,RELAYCLIENT=””‘ > /etc/tcp.smtp
Jangan lupa, setiap selesai penambahan untuk selalu membuat database baru dengan :
[root@alton daemontools-0.76]# tcprules /etc/tcp.smtp.cdb /etc/tcp.smtp.tmp < /etc/tcp.smtp
Buat symlink ke direktory services
[root@alton daemontools-0.76]# ln -s /var/qmail/supervise/qmail-send /var/qmail/supervise/qmail-smtpd /service
Cek proses apakah sudah jalan :
[root@alton daemontools-0.76]# ps ax | grep qmail
7902 ? S 0:00 supervise qmail-send
7904 ? S 0:00 /usr/local/bin/multilog t s1048576 /var/log/qmail
7905 ? S 0:00 supervise qmail-smtpd
7906 ? S 0:00 tcpserver -H -R -v -x /etc/tcp.smtp.cdb -c 30 -u 502 -g 501 0 25 qmail-smtpd
7908 ? S 0:00 /usr/local/bin/multilog t s1048576 /var/log/qmail/smtpd
7909 ? S 0:00 qmail-send
7910 ? S 0:00 qmail-lspawn ./Maildir/
7911 ? S 0:00 qmail-rspawn
7912 ? S 0:00 qmail-clean
7916 pts/4 R+ 0:00 grep qmail
Buat email untuk postmaster, root & mailer-daemon
[root@alton alias]# echo admin@alton.co.id > .qmail-root
[root@alton alias]# echo admin@alton.co.id > .qmail-postmaster
[root@alton alias]# echo admin@alton.co.id > .qmail-mailer-daemon
Buat symlink untuk sendmailnya
[root@alton alias]# ln -s /var/qmail/bin/sendmail /usr/sbin/sendmail
[root@alton alias]# ln -s /var/qmail/bin/sendmail /usr/bin/sendmail
Install Vpopmail
Download Vpopmail dari http://www.inter7.com
[root@alton qmail]# tar -xzvf vpopmail-5.2.1.tar.gz
[root@alton qmail]# cd vpopmail-5.2.1
[root@alton vpopmail-5.2.1]# groupadd vchkpw
[root@alton vpopmail-5.2.1]# useradd -g vchkpw vpopmail
[root@alton vpopmail-5.2.1]# ./configure –enable-logging=p
[root@alton vpopmail-5.2.1]# make && make install-strip
Buat domainnya
[root@alton vpopmail-5.2.1]# cd /home/vpopmail/bin
[root@alton bin]# ./vadddomain alton.co.id
Please enter password for postmaster:
enter password again:
user untuk domain tersebut
[root@alton bin]# ./vadduser asep@alton.co.id
Please enter password for asep@alton.co.id:
enter password again:
Setting POP3
[root@alton bin]# mkdir -p /var/qmail/supervise/qmail-pop3d
[root@alton bin]# vi /var/qmail/supervise/qmail-pop3d/run
#!/bin/sh
PATH=$PATH:/usr/local/bin:/var/qmail/bin
export PATH
MAXPOP3D=30
exec /usr/local/bin/softlimit -m 5000000 \
tcpserver -H -v -c “$MAXPOP3D” 0 110 qmail-popup dursosono.kurowo.edu /home/vpopmail/bin/vchkpw \
qmail-pop3d Maildir/ 2>&1
[root@alton bin]# mkdir -p /var/qmail/supervise/qmail-pop3d/log
[root@alton bin]# vi /var/qmail/supervise/qmail-pop3d/log/run
#!/bin/sh
exec /usr/local/bin/setuidgid qmaill /usr/local/bin/multilog t s1048576 /var/log/qmail/pop3d
[root@alton bin]# mkdir /var/log/qmail/pop3d
[root@alton bin]# chown qmaill /var/log/qmail/pop3d
[root@alton bin]# chmod 755 /var/qmail/supervise/qmail-pop3d/run
[root@alton bin]# chmod 755 /var/qmail/supervise/qmail-pop3d/log/run
Buat link ke /service
[root@alton bin]# ln -s /var/qmail/supervise/qmail-pop3d /service
Download qmailctl script (untuk yg tidak menggunakan pop3)
[root@alton qmail]# wget http://www.lifewithqmail.org/qmailctl-script-dt70
Jika menggunakan pop3 maka qmailctlnya download di
[root@alton qmail]# wget http://people.cakraweb.com/~asfik/qmailctl
Simpan script qmailctl
[root@alton qmail]# cp qmailctl-script-dt70 /var/qmail/bin/qmailctl
[root@alton qmail]# chmod 755 /var/qmail/bin/qmailctl
[root@alton qmail]# ln -s /var/qmail/bin/qmailctl /usr/bin
[root@alton qmail]# ln -s /var/qmail/bin/qmailctl /etc/rc.d/init.d/qmail
[root@alton qmail]# ln -s /etc/rc.d/init.d/qmail/qmail /etc/rc.d/rc1.d/K30qmail
[root@alton qmail]# ln -s /etc/rc.d/init.d/qmail/qmail /etc/rc.d/rc2.d/S80qmail
[root@alton qmail]# ln -s /etc/rc.d/init.d/qmail/qmail /etc/rc.d/rc3.d/S80qmail
[root@alton qmail]# ln -s /etc/rc.d/init.d/qmail/qmail /etc/rc.d/rc4.d/S80qmail
[root@alton qmail]# ln -s /etc/rc.d/init.d/qmail/qmail /etc/rc.d/rc5.d/S80qmail
[root@alton qmail]# ln -s /etc/rc.d/init.d/qmail/qmail /etc/rc.d/rc6.d/K30qmail
Instalasi ezmlm, autorespond & qmailadmin
Download ezmlm beserta patchnya :
[root@alton qmail]# wget http://cr.yp.to/software/ezmlm-0.53.tar.gz
[root@alton qmail]# wget http://gd.tuwien.ac.at/infosys/mail/qmail/ezmlm-patches/ezmlm-idx-0.40.tar.gz
Download qmailadmin & autorespond dari http://www.inter7.com
[root@alton qmail]# tar -xzvf ezmlm-0.53.tar.gz
[root@alton qmail]# tar -xzvf ezmlm-idx-0.40.tar.gz
[root@alton qmail]# tar -xzvf autorespond-2.0.2.tar.gz
[root@alton qmail]# mv ezmlm-idx-0.40/* ezmlm-0.53/
[root@alton qmail]# cd ezmlm-0.53
[root@alton ezmlm-0.53]# patch < idx.patch
[root@alton ezmlm-0.53]# patch -p1 < ../ezmlm-idx-0.53.400.unified_41.patch
[root@alton ezmlm-0.53]# make
[root@alton ezmlm-0.53]# make man
[root@alton ezmlm-0.53]# make setup
[root@alton ezmlm-0.53]# cd ../autorespond-2.0.2
[root@alton autorespond-2.0.2]# make
[root@alton autorespond-2.0.2]# make install
Configurasi qmail admin
[root@alton qmail]# tar -xzvf qmailadmin-1.2.9.tar.gz
[root@alton qmail]# cd qmailadmin-1.2.9
[root@alton qmailadmin-1.2.9]# ./configure –enable-htmldir=/var/www/html –enable-cgibindir=/var/www/cgi-bin
[root@alton qmailadmin-1.2.9]# make
[root@alton qmailadmin-1.2.9]# make install
Install vqadmin untuk administrasi mail dengan quota
Download vqadmin dari http://www.inter7.com kemudian install
[root@alton vqadmin-2.3.2]# ./configure –enable-htmldir=/var/www/html –enable-cgibindir=/var/www/cgi-bin
[root@alton vqadmin-2.3.2]# make && make install-strip
Edit /etc/httpd/conf/httpd.conf
Httpd.conf

deny from all
Options ExecCGI
AllowOverride AuthConfig
Order deny,allow
Edit file htaccess vqadmin
[root@alton vqadmin-2.3.2]# cd /var/www/cgi-bin/vqadmin/
[root@alton vqadmin]# vi .htaccess
Isinya :
AuthType Basic
AuthUserFile /etc/httpd/conf/vqadmin.passwd
AuthName vQadmin
require valid-user
satisfy any
[root@alton vqadmin]# chown apache .htaccess
[root@alton vqadmin]# chmod 644 .htaccess
[root@alton vqadmin]# htpasswd -bc /etc/httpd/conf/vqadmin.passwd admin emailalton
[root@alton vqadmin]# chmod 644 /etc/httpd/conf/vqadmin.passwd
Installasi imap server
Download paket-paket courier di http://www.courier-mta.org/?download.php
[root@alton qmail]# tar -jxvf courier-authlib-0.55.tar.bz2
[root@alton courier-authlib-0.55]# ./configure –prefix=/usr/local –exec-prefix=/usr/local –with-authvchkpw –without-authldap –without-authmysql –disable-root-check –with-authchangepwdir=/usr/local/libexec/authlib –with-redhat
[root@alton courier-authlib-0.55]# make && make check
[root@alton courier-authlib-0.55]# make install-strip && make install-configure
Simpan di rc.local
[root@alton courier-authlib-0.55]# vi /etc/rc.local
/usr/local/sbin/authdaemond start
[root@alton qmail]# tar -jxvf courier-imap-4.0.2.tar.bz2
[root@alton asep]# chown -R asep.root courier-imap-4.0.2/
[asep@alton courier-imap-4.0.2]$ ./configure –prefix=/usr/local –exec-prefix=/usr/local –with-authvchkpw –without-authldap –without-authmysql –disable-root-check –with-authchangepwdir=/usr/local/libexec/authlib –with-redhat
[asep@alton courier-imap-4.0.2]$ make && make check
[asep@alton courier-imap-4.0.2]$ su –
Password:
[root@alton ~]# make install-strip && make install-configure
Edit file-file yang diperlukan :
[root@alton courier-imap-4.0.2]# vi /usr/local/etc/imapd.cnf
Ganti postmaser@example.com menjadi postmaser@alton.co.id
[root@alton courier-imap-4.0.2]# vi /usr/local/etc/imapd
IMAPDSTART=YES
[root@alton courier-imap-4.0.2]# vi /usr/local/etc/authlib/authdaemonrc
authmodulelist=”authvchkpw”
[root@alton courier-imap-4.0.2]# cp /usr/local/libexec/imapd.rc /etc/rc.d/init.d/imap
[root@alton courier-imap-4.0.2]# /usr/local/sbin/authdaemond stop
[root@alton courier-imap-4.0.2]# /usr/local/sbin/authdaemond start
[root@alton courier-imap-4.0.2]# /etc/rc.d/init.d/imap stop
[root@alton courier-imap-4.0.2]# /etc/rc.d/init.d/imap start
[root@alton supervise]# vi /etc/rc.d/rc.local
/etc/rc.d/init.d/imap start
Untuk merubah password di squirrelmail
Download paketnya di
http://www.arda.homeunix.net/store/
[root@alton qmail]# tar -xzvf courierpassd-1.1.0-RC1.tar.gz
[root@alton courierpassd-1.1.0-RC1]# ./configure
[root@alton courierpassd-1.1.0-RC1]# make && make install
Buat service untuk membuka port 106 di xinetd
[root@alton courierpassd-1.1.0-RC1]# cd /etc/xinetd.d
[root@alton xinetd.d]# vi courierpassd
service courierpassd
{
port = 106
socket_type = stream
protocol = tcp
user = root
server = /usr/local/sbin/courierpassd
server_args = -s imap
wait = no
only_from = 127.0.0.1
instances = 4
disable = no
}
Tambahkan di /etc/services port 106
[root@alton xinetd.d]# vi /etc/services
courierpassd 106/tcp #for /etc/xinetd.d/courierpassd
Installasi Squirrelmail
Download paket squirrelmail dari http://www.squirrelmail.org
[root@alton qmail]# tar -jxvf squirrelmail-1.4.6.tar.bz2
[root@alton qmail]# mv squirrelmail-1.4.6 /var/www/html/webmail
[root@alton qmail]# mkdir /var/sqattachements
[root@alton qmail]# chown -R apache:apache /var/sqattachements
[root@alton webmail]# cd /var/www/html/webmail/
[root@alton webmail]# chown -R apache:apache data
[root@alton webmail]# cd /var/www/html/webmail/config
[root@alton config]# ./conf.pl
Pilih dan sesuaikan domain :
2. Server Settings
Plugins untuk merubah password di squirrelmail
wget http://squirrelmail.org/countdl.php?fileurl=http%3A%2F%2Fwww.squirrelmail.org%2Fplugins%2Fchange_pass-2.7-1.4.x.tar.gz
[root@alton qmail]# tar -xzvf change_pass-2.7-1.4.x.tar.gz
[root@alton qmail]# mv change_pass /var/www/html/webmail/plugins/
[root@alton qmail]# cd /var/www/html/webmail/config
[root@alton config]# ./conf.pl
Pilih Plugins kemudian pilih change_pass
8. Plugins
Sumber :
1. http://efnet.linux.or.id
2. http://www.layangan.com/asfik/writings/qmail-vpopmail.html
3. http://www.inter7.com
4. http://www.qmailrocks.org
5. http://lifewithqmail.org
6.http://www.courier-mta.org

http://forums.devshed.com/scripts-94/?s=&daysprune=

Virtual email server Qmail/VPOPMAIL/COURIER-IMAP with spam and virus protection
MYSQL INSTALLATION – http://www.mysql.org/
Create database
cd /usr/local/src/
wget http://www.devforge.net/virtualemail/mysql-4.0.21.tar.gz
tar zxf mysql-4.0.21.tar.gz
cd mysql-4.0.21

./configure –prefix=/usr/local/mysql \
–localstatedir=/usr/local/mysql/data \
–libexecdir=/usr/local/mysql/bin \
–with-extra-charsets=complex \
–enable-thread-safe-client \
–enable-local-infile \
–disable-shared \
–with-innodb

make
make install

groupadd mysql
useradd -g mysql mysql
cp support-files/mysql.server /etc/init.d/mysql
chmod +x /etc/init.d/mysql
cp support-files/my-medium.cnf /etc/my.cnf
cd /usr/local/mysql
bin/mysql_install_db –user=mysql
chown -R root .
chown -R mysql:mysql data
chgrp -R mysql .

cd /usr/local/mysql/
./bin/mysqld_safe –user=mysql &
./bin/mysqladmin -u root -p password ”

./bin/mysql -u root -p
create database email;
quit;

APACHE INSTALLATION – http://httpd.apache.org/
cd /usr/local/src/
wget http://www.devforge.net/virtualemail/httpd-2.0.52.tar.gz
tar zxf httpd-2.0.52.tar.gz
cd httpd-2.0.52
./configure –prefix=/usr/local/apache2 \
–enable-mods-shared=most

make
make install

ln -s /usr/local/apache2 /usr/local/apache
cp /usr/local/apache2/bin/apachectl /etc/init.d/apache
chmod +x /etc/init.d/apache

mkdir /home/sites
mkdir /home/cgi-bin
mkdir /home/sites/virtualemail
mkdir /home/sites/virtualemail/logs
mkdir /home/sites/virtualemail/web
chown -R nobody /home/sites
chown -R nobody /home/cgi-bin

Add the following to /usr/local/apache2/conf/httpd.conf replace with your domain name

AllowOverride None
Options ExecCGI
Order allow,deny
Allow from all

deny from all
Options ExecCGI
AllowOverride AuthConfig
Order deny,allow

ServerAdmin postmaster@
DocumentRoot /home/sites/virtualemail/web
ServerName
ScriptAlias /cgi-bin/ /home/cgi-bin/
AddType application/x-httpd-php .php
DirectoryIndex index.html index.html.var index.php
ErrorLog /home/sites/virtualemail/logs/error.log
CustomLog /home/sites/virtualemail/logs/access.log common

PHP INSTALLATION – http://www.php.net/
cd /usr/local/src/
wget http://www.devforge.net/virtualemail/php-4.3.9.tar.gz
tar zxf php-4.3.9.tar.gz
cd php-4.3.9
./configure –prefix=/usr/local/php \
–with-apxs2=/usr/local/apache2/bin/apxs \
–with-mysql=/usr/local/mysql

make
make install

QMAIL INSTALLATION – http://www.qmail.org/
PATH=/bin:/sbin:/usr/bin:/usr/sbin
groupadd nofiles -g 81
groupadd qmail -g 82
useradd alias -u 81 -g nofiles -s /nonexistent -d /var/qmail/alias -M
useradd qmaild -u 82 -g nofiles -s /nonexistent -d /var/qmail -M
useradd qmaill -u 83 -g nofiles -s /nonexistent -d /var/qmail -M
useradd qmailp -u 84 -g nofiles -s /nonexistent -d /var/qmail -M
useradd qmailq -u 85 -g qmail -s /nonexistent -d /var/qmail -M
useradd qmailr -u 86 -g qmail -s /nonexistent -d /var/qmail -M
useradd qmails -u 87 -g qmail -s /nonexistent -d /var/qmail -M

cd /usr/local/src
wget http://www.devforge.net/virtualemail/qmail-1.03.tar.gz
wget http://www.devforge.net/virtualemail/patch-qmail-queue
wget http://www.devforge.net/virtualemail/patch-qmail-103.patch
wget http://www.devforge.net/virtualemail/patch-qmail-sendmail-flagf.patch
wget http://www.devforge.net/virtualemail/patch-qmail-badmailfrom-wildcard
wget http://www.devforge.net/virtualemail/patch-qmail-capa-pop3d
wget http://www.devforge.net/virtualemail/patch-qmail-capa-popup
wget http://www.devforge.net/virtualemail/patch-qmail-remote-auth
wget http://www.devforge.net/virtualemail/patch-qmail-smtpd-auth
wget http://www.devforge.net/virtualemail/patch-qmail-smtpd-auth-log

tar xzf qmail-1.03.tar.gz
cd qmail-1.03
for d in ../patch-qmail-*; do patch <“$d”; done

cd /var/qmail/control
echo “” > me
cp me locals
cp me rcpthosts

cd /var/qmail/alias
echo postmaster@ >.qmail-root
cp .qmail-{root,mailer-daemon}
cp .qmail-{root,postmaster}
cp .qmail-{root,hostmaster}
cp .qmail-{root,abuse}

TCPRULES INSTALLATION – http://cr.yp.to/ucspi-tcp.html
cd /usr/local/src
wget http://www.devforge.net/virtualemail/ucspi-tcp-0.88.tar.gz
tar xzf ucspi-tcp-0.88.tar.gz
cd ucspi-tcp-0.88
make setup check

echo “127.0.0.:allow,RELAYCLIENT=\”\”” > /etc/tcp.smtp
tcprules /etc/tcp.smtp.cdb /etc/tcp.smtp.tmp < /etc/tcp.smtp

QMAIL AUTORESPONDER INSTALLATION
cd /usr/local/src/
wget http://www.devforge.net/virtualemail/autorespond-2.0.2.tar.gz
cd autorespond-2.0.2
make
make install

EZMLM MAILING LIST INSTALLATION – http://www.ezmlm.org/
cd /usr/local/src/
wget http://www.devforge.net/virtualemail/ezmlm-0.53.tar.gz
wget http://www.devforge.net/virtualemail/ezmlm-idx-0.42.tar.gz
tar zxf ezmlm-0.53.tar.gz
tar zxf ezmlm-idx-0.42.tar.gz

mv ezmlm-idx-0.42/* ezmlm-0.53/
cd ezmlm-0.53
patch < idx.patch
chmod 755 makelang

echo “-I/usr/local/mysql/include/mysql” > sub_mysql/conf-sqlcc
echo “-L/usr/local/mysql/lib/mysql -lmysqlclient -lm” > sub_mysql/conf-sqlld

make mysql
make clean
make
make man
make en_US
./ezmlm-mktab -d list | mysql -hlocalhost -u -p -f email
make setup

VPOPMAIL INSTALLATION – http://www.inter7.com/
cd /usr/local/src/
wget http://www.devforge.net/virtualemail/vpopmail-5.4.7.tar.gz
tar zxf vpopmail-5.4.7.tar.gz
cd vpopmail-5.4.7

groupadd -g 89 vchkpw
useradd -g vchkpw -u 89 -d /var/vpopmail vpopmail

./configure \
–enable-tcprules-prog=/usr/local/bin/tcprules \
–enable-tcpserver-file=/etc/tcp.smtp \
–enable-roaming-users=y \
–enable-relay-clear-minutes=60 \
–enable-logging=p \
–disable-ip-alias-domains \
–disable-passwd \
–enable-clear-passwd \
–disable-domain-quotas \
–enable-auth-module=mysql \
–disable-many-domains \
–enable-auth-logging \
–enable-logging=y \
–enable-mysql-logging \
–enable-valias \
–disable-mysql-limits

make
make install-strip

echo “localhost|0|||email” > ~vpopmail/etc/vpopmail.mysql

chown vpopmail.vchkpw ~vpopmail/etc/vpopmail.mysql
chmod 640 ~vpopmail/etc/vpopmail.mysql

COURIER-IMAP INSTALLATION – http://www.courier-mta.org/
cd /usr/local/src/
wget http://www.devforge.net/virtualemail/courier-imap-3.0.8.tar.bz2
tar jxf courier-imap-3.0.8.tar.bz2
cd courier-imap-3.0.8

export CFLAGS=”-DHAVE_OPEN_SMTP_RELAY -DHAVE_VLOGAUTH”
./configure –prefix=/usr/local/courier-imap \
–disable-root-check \
–without-authpam \
–without-authldap \
–without-authpwd \
–without-authmysql \
–without-authpgsql \
–without-authshadow \
–without-authuserdb \
–without-authcustom \
–without-authcram \
–without-authdaemon \
–with-authvchkpw \
–with-ssl

make
make install
make install-configure

cp courier-imap.sysvinit /etc/init.d/courier-imap
chmod +x /etc/init.d/courier-imap
mkdir -p /var/lock/subsys/
touch /usr/local/courier-imap/share/imapd.pem
touch /usr/local/courier-imap/share/pop3d.pem
chown vpopmail:vchkpw /usr/local/courier-imap/var/
chown vpopmail:vchkpw /usr/local/courier-imap/share/imapd.pem
chown vpopmail:vchkpw /usr/local/courier-imap/share/pop3d.pem

Edit /usr/local/courier-imap/etc/imapd and change the 2 values to equal the below
IMAPDSTART=YES
TCPDOPTS=”-nodnslookup -noidentlookup -user=vpopmail -group=vchkpw”

SQUIRRELMAIL INSTALLATION – http://www.squirrelmail.org/
cd /usr/local/src/
cd /home/sites/virtualemail/web
wget http://www.devforge.net/virtualemail/squirrelmail-1.4.3a.tar.gz
tar zxf squirrelmail-1.4.3a.tar.gz
ln -s squirrelmail-1.4.3a/ webmail
cd squirrelmail-1.4.3a

mkdir attachments
chown -R nobody data attachments
chmod go-w data attachments

The following you will be required to modify a few things, including your attachments dir
cd config
./conf.pl

CLAMAV ANTI-VIRUS INSTALLATION – http://www.clamav.net/
cd /usr/local/src/
wget http://www.devforge.net/virtualemail/clamav-0.80rc3.tar.gz
tar zxf clamav-0.80rc3.tar.gz
cd clamav-0.80rc3
groupadd clamav
useradd -g clamav -s /bin/false -c “Clam AntiVirus” clamav
./configure
make
make install
cat etc/clamd.conf | grep -v Example > /usr/local/etc/clamd.conf
cat etc/freshclam.conf | grep -v Example > /usr/local/etc/freshclam.conf
/usr/local/bin/freshclam –quiet

SPAMASSASSIN INSTALLATION – http://spamassassin.apache.org/

First you need to run the cpan shell.
You might be asked to configure if if you have not done this before.
When configuring just press enter unless asked to specify a mirror then chose the closest mirror
to your location.
perl -MCPAN -e shell

Under the cpan shell install the following 1 by 1
install Digest::SHA1
install HTML::Parser
install Storable
install DB_File
install Net::DNS
install Net::SMTP
install Mail::SPF::Query
install IP::Country::Fast
install IO::Socket::SSL
install Time::HiRes
install DBI
force install DBD::mysql

cd /usr/local/src/
wget http://www.devforge.net/virtualemail/Mail-SpamAssassin-current.tar.gz
tar zxf Mail-SpamAssassin-current.tar.gz
cd Mail-SpamAssassin-3.0.0/
perl Makefile.PL
make
make install
cp spamd/slackware-rc-script.sh /etc/init.d/spamassassin
chmod +x /etc/init.d/spamassassin

MAILDROP INSTALLATION – http://www.flounder.net/~mrsam/maildrop/
cd /usr/local/src/
wget http://www.devforge.net/virtualemail/maildrop-1.7.0.tar.bz2
tar jxf maildrop-1.7.0.tar.bz2
cd maildrop-1.7.0
./configure
make
make install

QMAIL-SCANNER INSTALLATION – http://qmail-scanner.sourceforge.net/
cd /usr/local/src/
wget http://www.devforge.net/virtualemail/qmail-scanner-1.23.tgz
wget http://www.devforge.net/virtualemail/q-s-1.23st-20040819.patch
gunzip q-s-1.23st-20040819.patch.gz
tar zxf qmail-scanner-1.23.tgz
cd qmail-scanner-1.23
patch -p1 < ../q-s-1.23st-20040819.patch
groupadd qscand
useradd -c “Qmail-Scanner Account” -g qscand -s /bin/false qscand
spamd -d -c -m5 -H -v -r /var/qmail/spamassassin.pid

./configure \
–qs-user qscand \
–qs-group qscand \
–spooldir /var/spool/qmailscan \
–qmaildir /var/qmail \
–bindir /var/qmail/bin \
–qmail-queue-binary /var/qmail/bin/qmail-queue \
–admin-fromname “Mail admin” \
–admin postmaster \
–domain `cat /var/qmail/control/me` \
–notify sender,admin,recips \
–local-domains `cat /var/qmail/control/rcpthosts | tr “\n” ” “` \
–silent-viruses auto \
–unzip 1 \
–add-dscr-hdrs 1 \
–archive 0 \
–redundant yes \
–log-crypto 0 \
–scanners “clamscan,fast_spamassassin” \
–scanners-per-domain yes \
–sa-subject “*****SPAM*****” \
–sa-delete 5 \
–sa-reject 1 \
–sa-quarantine 1 \
–install

kill `cat /var/qmail/spamassassin.pid`

VQADMIN INSTALLATION – http://www.inter7.com/
cd /usr/local/src/
wget http://www.devforge.net/virtualemail/vqadmin-2.3.2.tar.gz
tar zxf vqadmin-2.3.2.tar.gz
cd vqadmin-2.3.2
./configure –enable-cgibindir=/home/cgi-bin
make
make install-strip

/usr/local/apache/bin/htpasswd -bc /usr/local/apache2/conf/vqadmin.passwd postmaster PASSWORD
echo “postmaster * postmaster” > /home/cgi-bin/vqadmin/vqadmin.acl

QMAILADMIN INSTALLATION – http://www.inter7.com/
cd /usr/local/src/
wget http://www.devforge.net/virtualemail/qmailadmin-1.2.3.tar.gz
tar zxf qmailadmin-1.2.3.tar.gz
cd qmailadmin-1.2.3

./configure \
–enable-htmldir=/home/sites/virtualemail/qmailadminhtml \
–enable-imagedir=/home/sites/virtualemail/web/qmailadmin/images \
–enable-imageurl=/qmailadmin/images \
–enable-cgibindir=/home/cgi-bin/ \
–enable-autoresponder-bin=/usr/local/bin \
–enable-vpopuser=vpopmail \
–enable-ezmlmdir=/usr/local/bin/ezmlm/ \
–enable-ezmlmidx=y \
–enable-modify-quota \
–disable-ipauth \
–enable-help

DAEMONTOOLS INSTALLATION – http://cr.yp.to/daemontools.html
cd /usr/local/src/
wget http://www.devforge.net/virtualemail/daemontools-0.76.tar.gz
tar zxf daemontools-0.76.tar.gz
cd admin
wget http://www.devforge.net/virtualemail/daemontools-0.76.errno.patch
patch -p0 < daemontools-0.76.errno.patch
cd daemontools-0.76
./package/install

QMAILMRTG INSTALLATION – http://www.inter7.com/
mkdir /var/log/qmail
mkdir /var/log/pop3
mkdir /var/log/smtp
mkdir /var/log/spamd
mkdir /var/log/clamd
chown qmaill /var/log/qmail
chown qmaill /var/log/pop3
chown qmaill /var/log/smtp
chown qmaill /var/log/spamd
chown qmaill /var/log/clamd

cd /usr/local/src/
wget http://www.devforge.net/virtualemail/qmailmrtg7-4.0.tar.gz
tar zxf qmailmrtg7-4.0.tar.gz
cd qmailmrtg7-4.0
make
make install
mkdir -p /home/sites/virtualemail/web/mrtg/
cp index.html /home/sites/virtualemail/web/mrtg/
cp qmail.mrtg.cfg /etc/
/usr/bin/mrtg /etc/qmail.mrtg.cfg
/usr/bin/mrtg /etc/qmail.mrtg.cfg
/usr/bin/mrtg /etc/qmail.mrtg.cfg

Edit qmail.mrtg.cfg where applicable

SETUP CRON JOBS
cd ~
crontab -l > cron

Now edit the file cron and add the following
1 * * * * /usr/local/bin/freshclam –quiet &
01,15,31,46 * * * * /var/vpopmail/bin/clearopensmtp &
2-57/5 * * * * /usr/bin/mrtg /etc/qmail.mrtg.cfg > /dev/null

Now load the new cron file
crontab cron

START UP SCRIPT
cd /etc/init.d
wget http://www.devforge.net/virtualemail/virtualemail
wget http://www.devforge.net/virtualemail/qmail
chmod +x virtualemail
chmod +x qmail

Start MySQL/Apache/Qmail
/etc/init.d/virtualemail start all

Stop all MySQL/Apache/Qmail
/etc/init.d/virtualemail stop all

Start MySQL
/etc/init.d/mysql start

Stop MySQL
/etc/init.d/mysql stop

Start Apache
/etc/init.d/apache start

Stop Apache
/etc/init.d/apache stop

Start Qmail
/etc/init.d/qmail start

Stop Qmail
/etc/init.d/qmail stop

(2004-10-09 14:17:44.0) Permalink
Saturday September 04, 2004
C Programming – Basic chat server tutorial
Tutorial: Basic chat server chatserver-tutorial-v0.1.tar.gz

_____
/ ___\ ___ _ _ ____ _ _ ___ _____
| | / _ \ | \ / | / __\ | | | | / _ \ |_ _|
| | | | | | | \/ | | | | |__| | ||_| | | |
| |____ ||_| | | |\__/| | | |___ | ,–, | | _ | | |
\_____/ \ ___ /|_| |_| \____/|_| |_||_||_| |_|

This application is based on comchat – http://www.comchat.net/
Developed by the Devforge network – http://www.devforge.net/
Copyright (C) 2004 Benjamin Charles Tehan – benjamin@devforge.net

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Library General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place – Suite 330, Boston, MA 02111-1307, USA.

Please see http://www.gnu.org/licenses/gpl.txt for License terms.

This application may or may not work on your platform.

It was written for Linux and requires strncasecmp() and fcntl() which may not be
supported by your OS.

Under certain conditions this application will crash.

example, If client load exceeds FD_SETSIZE minus any other file descriptors you use.

This is only a brief out line at what you require to build a basic chat server.

Step 1.

You will be required to have a method of storing details about each connection and user information.

To make things easier we will use double linked lists from my previous Tutorial.
Please see this tutorial on howto write functions to insert/delete/list/clear data in the below linked list, or alternatively download chatserver-tutorial-v0.1.tar.gz.
struct UsersConnections;
typedef struct UsersConnections UsersConnections;
typedef UsersConnections *UsersConnectionsPTR;

struct Users;
typedef struct Users Users;
typedef Users *UsersPTR;

// List of users connections to server
struct UsersConnections {
int sockfd;
char IP[16];
UsersPTR User;
struct UsersConnections *Prev;
struct UsersConnections *Next;
};
UsersConnectionsPTR UsersConnectionsStart;

// List of users information
struct Users {
UsersConnectionsPTR Connection;
int status;
char Nick[36];
struct Users *Prev;
struct Users *Next;
};
UsersPTR UsersStart;

Step 2.

As a server daemon there are a few things we need to do:

a) Fork the process to the background
b) Bind the daemon to a port and listen for new connections
c) Check for new connections
d) Accept new connetions
e) Check for incomming data on each connection, read if any and process
Fork the process to the background
int main() {
pid_t pid;

if(!(pid = fork())) {
pid=getpid();

/* code goes here */
}
}

Bind the daemon to a port and listen for new connections
int main() {
int clientfd;
pid_t pid;

UsersStart = NULL;
UsersConnectionsStart = NULL;

// Listen for connections on port 9000
clientfd = ListenConnection(9000);

if(!(pid = fork())) {
pid=getpid();

/* code goes here */
}
}

int ListenConnection(int port) {

int sockfd;
struct sockaddr_in myaddr;

myaddr.sin_family = AF_INET;
myaddr.sin_addr.s_addr = INADDR_ANY;
myaddr.sin_port = htons(port);

memset(myaddr.sin_zero, 0, sizeof(myaddr.sin_zero));

if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
perror(“socket”);
exit(1);
}

if (bind(sockfd, (struct sockaddr *)&myaddr, sizeof(struct sockaddr)) == -1) {
perror(“bind”);
exit(1);
}

// This will listen to the port but we need another function to check this
if (listen(sockfd, 100) == -1) {
perror(“listen”);
exit(1);
}
return sockfd;
}

Check and accept new connections
int running;

int main() {
int clientfd;
pid_t pid;

UsersStart = NULL;
UsersConnectionsStart = NULL;
running = 1;

// Listen for connections on port 9000
clientfd = ListenConnection(9000);

if(!(pid = fork())) {
pid=getpid();

// loop while running = 1
while(running) {

// Check for new client connections
if (CheckSocketRead(clientfd) > 0) {
// Accept the connection and add user to client list
AcceptClient(clientfd);
}

// this is a temp solution to save cpu resources
usleep(200);
}
}
}

int CheckSocketRead(int sockfd) {
int rc;
struct timeval tv;
fd_set read_fd;

tv.tv_sec=0;
tv.tv_usec=1;

FD_ZERO(&read_fd);
FD_SET(sockfd, &read_fd);

rc = select(sockfd+1, &read_fd, NULL, NULL, &tv);
FD_ZERO(&read_fd);
return rc;
}

int AcceptClient(int sockfd) {
int newfd, sinsize;
struct sockaddr_in clientaddr;
UsersPTR From;
UsersConnectionsPTR Connection;

sinsize = sizeof(struct sockaddr_in);
newfd = accept(sockfd, (struct sockaddr *)&clientaddr, &sinsize);
if (newfd == -1) {
// you don’t realy want to exit here as it’s only 1 client that can’t be accepted
// however you will be required to investigate why accept returned -1
perror(“accept”);
exit(1);
} else if (newfd) {
printf(“New CLIENT connection from IP %s on socket %d\n”, \
inet_ntoa(clientaddr.sin_addr), newfd);

// Enable non-blocking socket for select()
fcntl(newfd, F_SETFL, O_NONBLOCK);

// Add user to connection list (This function is not in the example, please see
// http://www.devforge.net/downloads/chatserver-tutorial-v0.1.tar.gz
Connection = UsersConnectionsInsert(&UsersConnectionsStart, newfd, \
inet_ntoa(clientaddr.sin_addr));

// Add user to client list (This function is not in the example, please see
// http://www.devforge.net/downloads/chatserver-tutorial-v0.1.tar.gz
Connection->User = UsersInsert(&UsersStart, &Connection, “”);
}
}

Check for incomming data on each connection, read and process
int running;

int main() {
int clientfd;
pid_t pid;

UsersStart = NULL;
UsersConnectionsStart = NULL;
running = 1;

// Listen for connections on port 9000
clientfd = ListenConnection(9000);

if(!(pid = fork())) {
pid=getpid();

// loop while running = 1
while(running) {

// Check for new client connections
if (CheckSocketRead(clientfd) > 0) {
// Accept the connection and add user to client list
AcceptClient(clientfd);
}

// Check for any incomming data from clients
CheckClientData();

// this is a temp solution to save cpu resources
usleep(200);
}
}
}

int CheckClientData() {
int rc, max;
struct timeval tv;
UsersConnectionsPTR Connection, Next;
char output[1024];
fd_set usersfd;

tv.tv_sec=0;
tv.tv_usec=0;

Connection = UsersConnectionsStart;
while (Connection != NULL) {
Next = Connection->Next;

FD_ZERO(&usersfd);
FD_SET(Connection->sockfd, &usersfd);

errno = 0;
rc = select(Connection->sockfd+1, &usersfd, NULL, NULL, &tv);
if (rc > 0) {
ReadClientData(&Connection);
} else if (rc == -1) {
sprintf(output,”%m\n”,errno);
Quit(&Connection, output);
}
Connection = Next;
}
}

// This is a ugly function in that it only reads 1 char at a time from the client
// You may want to add a buffer in your Users structure.
int ReadClientData(UsersConnectionsPTR *sConnection) {
int i, ch, rc;
char data[1024], output[1024], buffer[1024];
UsersConnectionsPTR Connection;
UsersPTR From;

Connection = *sConnection;
From = (*sConnection)->User;

*buffer=”;

while (1) {
*data = ”;
memset(data, 0, sizeof data);
errno = 0;
rc = recv(Connection->sockfd,data,1,0);
data[rc] = ”;

if (errno) {
if (errno != EAGAIN && errno != EWOULDBLOCK) {
sprintf(output,”%m\n”,errno);
Quit(&Connection, output);
return;
}
} else if (rc > 0) {

ch = data[0];
if (ch != ‘\n’ && ch != ‘\r’) {
sprintf(buffer,”%s%c”,buffer,ch);
} else if (ch != ‘\r’) {
Process(&Connection, buffer);
*buffer=”;
break;
}

} else if (rc == 0) {
// closed successfully
Quit(&Connection, “Disconnected”);
return;
} else {
// connection terminated
Quit(&Connection, “Connection terminated”);
return;
}
}
}

int Process(UsersConnectionsPTR *sConnection, char *sData) {
UsersConnectionsPTR Connection;
UsersPTR From;

Connection = *sConnection;
From = (*sConnection)->User;

// This is the function where we process all data
}

int Quit(UsersConnectionsPTR *sConnection, char *sReason) {
char buffer[1024];
UsersPTR From;
UsersConnectionsPTR Connection;

Connection = *sConnection;
From = (*sConnection)->User;

// close file descriptor
close(Connection->sockfd);

// Delete user (This function is not in the example, please see
// http://www.devforge.net/downloads/chatserver-tutorial-v0.1.tar.gz
UsersDelete(&From);

// Delete connection (This function is not in the example, please see
// http://www.devforge.net/downloads/chatserver-tutorial-v0.1.tar.gz
UsersConnectionsDelete(&Connection);
}

Step 3.

Now that we have a working server we need to process the data and send data back to the client.

As this is a chat server the client requires a nickname, so we will have to add functionality to Process() that enables the client to set a nickname and do other things like message other users

We do not want the client to be able to message any one untill they have chosen a nickname, and we also don’t want the client to be able to chose a nickname that is already in use.

The below Process() does all the above (Please note that function lengthafterchar() is not in this example, please see http://www.devforge.net/downloads/chatserver-tutorial-v0.1.tar.gz
Process() functionality
int Process(UsersConnectionsPTR *sConnection, char *sData) {
int i;
char action[36], buffer[1024], temp[36];
UsersConnectionsPTR Connection;
UsersPTR From, To, currentPtr;

*buffer=”;

Connection = *sConnection;
From = (*sConnection)->User;

if (lengthafterchar(&sData[0], ‘ ‘)) {
snprintf(action,lengthafterchar(&sData[0], ‘ ‘),&sData[0]);
i = lengthafterchar(&sData[0], ‘ ‘);
} else {
strcpy(action,&sData[0]);
}

if (From->status) {
if (!lengthafterchar(&sData[0], ‘ ‘)) {
SendMSG(Connection->sockfd, “Unknown command\n”);

// Global messages to all authorized users
} else if (strcasecmp(action,”GLOBAL”)==0) {
if (!lengthafterchar(&sData[0], ‘ ‘)) {
SendMSG(Connection->sockfd, “Incorrect syntax for command GLOBAL: GLOBAL MESSAGE\n”);
} else {
sprintf(buffer,”GLOBAL %s %s\n”,From->Nick, &sData[i]);
printf(“<%s> GLOBAL %s\n”,From->Nick, &sData[i]);
SendALL(&UsersConnectionsStart, &Connection, buffer);
}

// Private messages to other users
} else if (strcasecmp(action,”MSG”)==0) {
if (!lengthafterchar(&sData[0], ‘ ‘)) {
SendMSG(Connection->sockfd, “Incorrect syntax for command MSG: MSG NICK MESSAGE\n”);
} else {
snprintf(temp,lengthafterchar(&sData[i], ‘ ‘),&sData[i]);
i = i + lengthafterchar(&sData[i], ‘ ‘);
To = GetUser(&UsersStart, temp);
if (To != NULL) {
sprintf(buffer,”MSG %s %s\n”,From->Nick, &sData[i]);
printf(“<%s> MSG %s %s\n”,From->Nick, To->Nick, &sData[i]);
SendMSG(To->Connection->sockfd, buffer);
} else {
sprintf(buffer,”No such nick %s\n”,temp);
SendMSG(Connection->sockfd, buffer);
}
}
} else {
SendMSG(Connection->sockfd, “Unknown command\n”);
}
} else if (strcasecmp(action,”NICK”)==0) {
if (GetUser(&UsersStart, &sData[i]) != NULL) {
SendMSG(Connection->sockfd, “Nickname is already in use, please try another.\n”);
} else {
strcpy(From->Nick, &sData[i]);
From->status = 1;
sprintf(buffer,”Welcome %s\n”,From->Nick);
SendMSG(Connection->sockfd, buffer);

sprintf(buffer,”CONNECTED %s\n”,&sData[i]);
printf(“%s”,buffer);
SendALL(&UsersConnectionsStart, &Connection, buffer);
}
} else {
SendMSG(Connection->sockfd, “You must select a nick by typing: NICK yournickname\n”);
}
}

int SendALL(UsersConnectionsPTR *sConnection, UsersConnectionsPTR *sFrom, char *sMsg) {
UsersConnectionsPTR Connection, nextPtr, currentPtr;
UsersPTR From;

currentPtr = *sConnection;
From = (*sFrom)->User;

while (currentPtr != NULL) {
nextPtr = currentPtr->Next;
if (strcasecmp(currentPtr->User->Nick,From->Nick)!=0 && currentPtr->User->status > 0) {
SendMSG(currentPtr->sockfd, sMsg);
}
currentPtr = nextPtr;
}
}

int SendMSG(int sockfd, char *msg) {
return send(sockfd, msg, strlen(msg), 0);
}

Step 4.

Time to test, compiled and started server on port 9000

I connected 2 clients to the server by telnet to localhost on port 9000
Server output
admin@notebook:~/Projects/Tutorials/chatserver$ make
gcc -O2 -g -o src/main.o -c src/main.c -Iinclude
gcc -O2 -g -o src/socket.o -c src/socket.c -Iinclude
gcc -O2 -g -o src/lists.o -c src/lists.c -Iinclude
gcc -O2 -g -o src/string.o -c src/string.c -Iinclude
gcc -O2 -g -o chatserver src/main.o src/socket.o src/lists.o src/string.o -Iinclude
Done!
admin@notebook:~/Projects/Tutorials/chatserver$ ./chatserver

_____
/ ___\ ___ _ _ ____ _ _ ___ _____
| | / _ \ | \ / | / __\ | | | | / _ \ |_ _|
| | | | | | | \/ | | | | |__| | ||_| | | |
| |____ ||_| | | |\__/| | | |___ | ,–, | | _ | | |
\_____/ \ ___ /|_| |_| \____/|_| |_||_||_| |_|

This application is based on comchat – http://www.comchat.net/
Developed by the Devforge network – http://www.devforge.net/
Copyright (C) 2004 Benjamin Charles Tehan – benjamin@devforge.net

This application will crash if more then 1024 clients are connected.
Please read up on FD_SETSIZE and restrict the client load appriopriatly

Chat server has forked to the background as pid 3441

New CLIENT connection from 127.0.0.1 on socket 4
CONNECTED bableon
New CLIENT connection from 127.0.0.1 on socket 5
CONNECTED vordi
GLOBAL hey vordi
GLOBAL hey bableon
MSG vordi heehee…
MSG bableon haahaa…

Client 1
admin@notebook:~/Projects/comchat$ telnet localhost 9000
Trying 127.0.0.1…
Connected to localhost.
Escape character is ‘^]’.
test
You must select a nick by typing: NICK yournickname
nick bableon
Welcome bableon
CONNECTED vordi
global hey vordi
GLOBAL vordi hey bableon
msg vordi heehee…
MSG vordi haahaa…

Client 1
admin@notebook:~/Projects/comchat$ telnet localhost 9000
Trying 127.0.0.1…
Connected to localhost.
Escape character is ‘^]’.
nick vordi
Welcome vordi
GLOBAL bableon hey vordi
global hey bableon
MSG bableon heehee…
msn bableon haahaa…
Unknown command
msg bableon haahaa…

Step 5.

Improving the above code.

The problem with this chat client is under extreme loads of incomming/outgoing connections it will segfault due to signals not being processed, especially when trying to send large amounts of data to users that have disconnected and have yet to be processed as quit

To solve these issues you can add 2 things to help avoid such issues,
Signal handling, ignore sigpipe
void handle_signal(int signum);

int running;

int main() {
int clientfd;
pid_t pid;

UsersStart = NULL;
UsersConnectionsStart = NULL;
running = 1;

signal (SIGSEGV, handle_signal);
signal (SIGINT, handle_signal);
signal (SIGUSR1, handle_signal);
signal (SIGUSR2, handle_signal);
signal (SIGPIPE, SIG_IGN);

// Listen for connections on port 9000
clientfd = ListenConnection(9000);

if(!(pid = fork())) {
pid=getpid();

// loop while running = 1
while(running) {

// Check for new client connections
if (CheckSocketRead(clientfd) > 0) {
// Accept the connection and add user to client list
AcceptClient(clientfd);
}

// Check for any incomming data from clients
CheckClientData();

// this is a temp solution to save cpu resources
usleep(200);
}
}
}

void handle_signal(int signum) {
// This will require you to research more on signal handling
printf(“SIGNAL %d\n”, signum);
if (signum == 11 || signum == 2) exit(0);
}

Using CheckSocketWrite(Connection->sockfd) to see if we can write to that socket
int CheckSocketWrite(int sockfd) {
int rc;
struct timeval tv;
fd_set write_fd;

tv.tv_sec=0;
tv.tv_usec=1;

FD_ZERO(&write_fd);
FD_SET(sockfd, &write_fd);

rc = select(sockfd+1, NULL, &write_fd, NULL, &tv);
FD_ZERO(&write_fd);
return rc;
}

Please download chatserver-tutorial-v0.1.tar.gz

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: