Kamailio multi tenant (domain) configuration





By default, Kamailio employs the user portion of the SIP Address for authentication purposes or to locate the contact header. This default configuration restricts its functionality to a single domain,For example here Alice @ CompanyA.omid.blog can register and call to Bob @ companyA.omid.blog

Kamailio MultiDomain Configuration

However, Kamailio's architecture is specifically designed to handle thousands of calls per second. Lets say I want to implement a multi-tenant environment, such as a hosted PBX or call center system and I want to service multiple customers with different domains.

In the context of our example, I am looking to provide services to both Company A and Company B, that some employees may have the same names in both companies. For instance, Alice and Bob are present in both organisations. However, it's important to clarify that even though they share a name, they are distinct individuals with unique SIP usernames and passwords.

Kamailio multi domain configuration

Multi tenant functionality can be provided in 2 ways . Statically in config by using aliases .. Or a more dynamic way by using a domain module that can read domains from a database such as mysql or postgresql.

Method 1: alias in kamailio.cfg

with alias keyword in kamailio configuration you can define multiple domains for your kamailio. It will be the preferred method for small installation that you may not have so many domains. It has two downside .

  • You need to restart your kamailio everytime you want to change or add a new domain
  • Its not very easy to to add or edit if you want to have web panel or some sort of API for managing the domains

In order to configure our domains we need to edit kamailio.cfg file and add our domains

/* add local domain aliases - it can be set many times */
# alias="sip.mydomain.com"
 alias="companyA.omid.blog"
 alias="companyB.omid.blog"

and restart our kamailio service

systemctl restart kamailio

Method 2: Domain Module

In order to enable Multi domain configuration using domain.so module we need to connect our kamailio to database. If you have installed the kamailio using my guide here : https://github.com/Omid-Mohajerani/Learn-Kamailio/wiki/Kamailio-installation-on-Debian-11 you have already installed your kamailio with MYSQL db support and User authentication with db. During the installation Kamailio domain table is also created.

 CREATE TABLE `domain` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `domain` varchar(64) NOT NULL,
  `did` varchar(64) DEFAULT NULL,
  `last_modified` datetime NOT NULL DEFAULT '2000-01-01 00:00:01',
  PRIMARY KEY (`id`),
  UNIQUE KEY `domain_idx` (`domain`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=latin1 COLLATE=latin1_swedish_ci

To enable the multi domain in default configuration you just need to add

#!define WITH_MULTIDOMAIN

in your configuration file

That will set the MULTIDOMAIN variable value to 1

#!ifdef WITH_MULTIDOMAIN
# - the value for 'use_domain' parameters
#!define MULTIDOMAIN 1
#!else
#!define MULTIDOMAIN 0
#!endif

It will load the domain module

#!ifdef WITH_MULTIDOMAIN
loadmodule "domain.so"
#!endif

It will set the domain parameters such as db_url

# ----- domain params -----
#!ifdef WITH_MULTIDOMAIN
modparam("domain", "db_url", DBURL)
/* register callback to match myself condition with domains list */
modparam("domain", "register_myself", 1)
#!endif

To add a new domain we can insert directly into db or use kamctl command

kamctl domain add companyC.omid.blog
kamctl domain reload
kamctl domain show

Also its very important to know that now that you have multi-domain configured you need to specify the domain name when you are adding or deleting a user.

kamctl add myuser@companyC.omid.blog Mypass

Comments