New FAMILUG

The PyMiers

Monday 24 June 2013

Cài đặt server chat XMPP - ejabberd

XMPP (Extensible Messaging and Presence Protocol): trước đây là Jabber, là giao thức mở dựa trên nền tảng XML dùng trong nhắn tin nhanh (instant messaging) và thông tin hiện diện trực tuyến (presence information).
(http://vi.wikipedia.org/wiki/XMPP)

Mạng XMPP có thể dùng đơn lẻ: các máy trong mạng chat với nhau, hoặc kết nối với các hệ thống nhắn tin (instant messaging systems) khác - thông qua các gateway: máy của mạng nay chat được với máy thuộc mạng kia .
Các hệ thống nhắn tin này có thể là 1 mạng XMPP khác hay mạng ICQ, google talk...





Mỗi user của XMPP server được xác định bằng JID (jabberd id), gồm các thành phần: user_name@server_name/resource
JID có phần giống 1 địa chỉ email: user_name@server_name
resource để xác định tài khoản của bạn ở các client khác nhau (ở nhà/công ty/app điện thoại...)

Ví dụ 1 jid của facebook:
d.tung.lam@chat.facebook.com/5864aa81_4DFDDEAC3F4A7
+ d.tung.lam là username facebook
+ chat.facebook.com là server chat của facebook
+ 5864aa81_4DFDDEAC3F4A7 là resource giúp facebook nhận diện, quản lý được các client mình đang vào. Nếu user đăng nhập tài khoản này ở 1 máy khác thì resource sẽ thay đổi.

message gửi đi ở dạng XML, có cấu trúc kiểu như:

<message from='test@mysvr.com/3977137943137258301563176'
         to='hihi@mysvr.com'
         type='chat'
         id='purpleb1ed9785'>
  <active xmlns='http://jabber.org/protocol/chatstates'/>
  <body>hello familug</body>
</message>
trong đó chỉ ra người gửi, người nhận, nội dung message, id message

ejabberd - là 1 XMPP server viết bằng Erlang
Mình sẽ viết hướng dẫn cài đặt, cấu hình, khởi tạo user và xác thực bằng LDAP với ejabberd.

Cài đặt

mình cài trên: Ubuntu 12.04.2 LTS
chọn domain: http://mysvr.com, nếu không để nguyên localhost cũng được
thêm vào file hosts:
127.0.0.1    mysvr.com

sudo -s
apt-get install ejabberd
vim /etc/ejabberd/ejabberd.cfg

Tìm dòng
{acl, admin, {user, "", "localhost"}}.
Thay bằng:
{acl, admin, {user, "lamdt", "mysvr.com"}}.
lamdt - tên admin

Tìm
{hosts, ["localhost"]}.
Thêm vào
{hosts, ["localhost","mysvr.com"]}.
restart lại service
service ejabberd restart

Tạo tài khoản admin:

ejabberdctl register lamdt mysvr.com 123@456
lamdt - tên admin đã điền ở trên
mysvr.com là domain
123@456 là password

Nếu báo User lamdt@mysvr.com successfully registered là bạn đã tạo thành công. Nếu chưa được kiểm tra lại hosts, username, đã restart service chưa?

Tạo user


Từ browser truy cập vào

http://mysvr.com:5280/admin
username và password đã tạo ở trên
lamdt - 123@456

Sau khi login, vào: Virtual Hosts > chọn host (mysvr.com) > User để tạo user chat.

Ở đây mình tạo 2 user:
hihi@mysvr.com
test@mysvr.com

Để test bạn đăng nhập cả 2 user test bằng 1 xmpp client - ở đây mình dùng pidgin, cấu hình như sau:




Ctrl + m: Tạo 1 tin mới gửi đi.



Kết quả:




Xác thực với LDAP

Giả sử bạn chạy server LDAP để xác thực cho các email mysvr.com tại ldap.domain.net

Bạn có thể cấu hình như sau trong file ejabberd.cfg
Cấu hình dưới này cho cây LDAP có cấu trúc:
 dc = mysvr, dc = com
|
|_ o=domains
       |
       |_ domainName=msvr.com
              |
              |_ ou=Users
                             |
                             | _mail=a_user@mysvr.com
                             |
                             | _mail=b_user@mysvr.com
     

 Phần Authentication, comment dòng

{auth_method, internal}. 
%%{auth_method, internal}.


bỏ comment

%%{auth_method, ldap}.
thành
{auth_method, ldap}.
 

Để bỏ từ xác thực bình thường sang xác thực ldap.
Bỏ comment và cấu hình như sau:

{ldap_servers, ["ldap.domain.net"]}.

Kết nối bình thường thì để
{ldap_encrypt, none}.
Kết nối bảo mật thì để
{ldap_encrypt, tls}.
Cái này phụ thuộc vào server ldap của bạn

dn quản trị ldap
{ldap_rootdn, "cn=Manager,dc=mysvr,dc=com"}.
mật khẩu quản trị
{ldap_password, "****"}.

Search base:
{ldap_base, "o=domains,dc=mysvr,dc=com"}.

Id của user trong LDAP, ở đây nhận diện = email
{ldap_uids, [{"mail", "%u@mysvr.net"}]}.

Cái này để tạo lọc ra email của user khi search trong LDAP
{ldap_filter, "(mail=*)"}.
Chú ý:
Nếu bạn muốn dùng account LDAP để quản trị trang admin thì thêm vào
{acl, admin, {user, "a_user", "mysvr.com"}}.

Lưu lại và restart ejabberd.


========
Để tìm hiểu các modules của ejabberd thì vào đây:
svn.process-one.net/ejabberd/tags/ejabberd-2.0.2/doc/guide.html#htoc35 

Chi tiết hơn ở đây, sau khi đã cài:
http://192.168.25.180:5280/admin/doc/guide.html#modoverview 

Các modules khuyên dùng:
mod_muc: Tạo chat room
mod_ping: giữ connection để không bị timeout khi inactive lâu

19 comments:

  1. tut hay quá :x

    mỗi tội ứ hiểu anh cho tag LDAP làm gì =.=

    ReplyDelete
    Replies
    1. Mình sẽ viết hướng dẫn cài đặt, cấu hình, khởi tạo user và xác thực bằng LDAP với ejabberd.

      thím đọc title rồi comment à, (e chưa viết xong :3 )

      Delete
  2. Hôm qua đọc về tạo contact list, mới chỉ biết tạo trên trang admin, chưa biết cho user tạo thế nào. lúc nào thành thạo sẽ bổ sung thêm @@

    ReplyDelete
  3. Update LDAP
    ========
    Để tìm hiểu các modules của ejabberd thì vào đây:
    svn.process-one.net/ejabberd/tags/ejabberd-2.0.2/doc/guide.html#htoc35

    Chi tiết hơn ở đây, sau khi đã cài:
    http://192.168.25.180:5280/admin/doc/guide.html#modoverview

    Các modules khuyên dùng:
    mod_muc: Tạo chat room
    mod_ping: giữ connection để không bị timeout khi inactive lâu

    ReplyDelete
    Replies
    1. EDIT: Chi tiết hơn ở doc này, chỉ đọc được sau khi đã cài ejabberd:
      http://localhost:5280/admin/doc/guide.html#modoverview

      Delete
  4. http://192.168.25.180:5280/admin/doc/guide.html#modoverview ????
    link này sống bao lâu? nếu là 1 file text thì copy paste sang pastebin ấy

    ReplyDelete
    Replies
    1. `Sau khi đã cài`
      Ai quan tâm đến cái này -> cài, cài rồi thì vào url xem.
      Xem chơi thì link trên cũng được

      Delete
  5. chuẩn bị viết bài cài XMPP server prosody, 5 phút là nhảy được :3

    ReplyDelete
  6. It is included in my habit that I often visit blogs in my free time, so after landing on your blog. I have thoroughly impressed with it and decided to take out some precious time to visit it again and again. Thanks. what is a chatbot

    ReplyDelete
  7. I am incapable of reading articles online very often, but I’m happy I did today. It is very well written, and your points are well-expressed. I request you warmly, please, don’t ever stop writing. how to create a chatbot

    ReplyDelete
  8. I exactly got what you mean, thanks for posting. And, I am too much happy to find this website on the world of Google. voice app company

    ReplyDelete
  9. Attractive, post. I just stumbled upon your weblog and wanted to say that I have liked browsing your blog posts. After all, I will surely subscribe to your feed, and I hope you will write again soon! what is a chatbot

    ReplyDelete
  10. My brother suggested I might like this website.

    He was entirely right. This post actually made my day.

    You cann’t imagine just how much time I had spent for this
    info! Thanks!
    where can i buy counterfeit money online

    ReplyDelete
  11. My brother suggested I might like this website.

    He was entirely right. This post actually made my day.

    You cann’t imagine just how much time I had spent for this
    info! Thanks! can you buy counterfeit money online

    ReplyDelete
  12. interesting post. click my website for more..
    fake bills

    ReplyDelete
  13. amazing check on my wesite for more like this tramadol online

    ReplyDelete
  14. Goodness, cool post. I might want to compose like this as well – setting aside time and genuine difficult work to make an extraordinary article… however I put things off something over the top and never appear to begin. check my website here for more
    where can i buy counterfeit money online

    ReplyDelete