2.5 Rights Administration with Contexts
So far, we have been able to call any extension from any telephone. For a small, private system, this is sufficient. Large systems and systems connecting to other telephone networks need some way to manage calling rights (that is, the rules that determine which phones and users are allowed to make calls to where). Asterisk does this via contexts.
Think of a context as a kind of category. Each category contains rules about what can be dialed. To access the rules in a context, the phone must be a member of that context or have been directed to that context by another rule.
2.5.1 The Originating Context
We haven't yet specified a context for the SIP phones we configured. In this case, Asterisk assumes the [default] context. If we want to specify specific contexts for specific telephones, we use the following syntax in sip.conf:
context = ContextName
We can set a context for all the phones in the [general] section, but this context can be overwritten on a per-telephone basis. Let's take a look at a couple of examples.
2.5.1.1 A Context Example
ABC Co. has the SIP phones 10 and 11. They are both in the [internal] context. The sip.conf file looks like this:
[general] port=5060 bindaddr=0.0.0.0 [10] type=friend secret=1234 host=dynamic context=internal ; <-- context [11] type=friend secret=1234 host=dynamic context=internal ; <-- context
You can achieve the same result more simply, though:
[general] port=5060 bindaddr=0.0.0.0 context=internal ; <-- context [10] type=friend secret=1234 host=dynamic [11] type=friend secret=1234 host=dynamic
2.5.1.2 Example with Multiple Phones
ABC Co. has the SIP phones 10, 11, 12, and 20. The phones 10, 11, and 12 are standard staff phones in the [abc] context, and 20 is a courtesy phone at reception in the [visitor] context:
[general] port=5060 bindaddr=0.0.0.0 context=abc ; <-- context [10] type=friend secret=1234 host=dynamic [11] type=friend secret=1234 host=dynamic [12] type=friend secret=1234 host=dynamic [20] type=friend secret=1234 host=dynamic context=visitor ; <-- this device-specific context ; overrides the default context in the [general] section.
2.5.2 Call Destination: Contexts in extensions.conf
The dialplan, extensions.conf, is broken up into sections, or contexts. Each context is specified by a name in square brackets.
The following is a sample extensions.conf containing the three contexts, [default], [building-mgr], and [apple-pie]:
[default] exten => 1001,1,Answer() exten => 1001,2,Playback(hello-world) exten => 1001,3,Hangup() [building-mgr] exten => 2000,1,Dial(SIP/2000,20) exten => 2000,2,VoiceMail(2000,u) exten => 2001,1,Dial(SIP/2001) exten => 2001,2,VoiceMail(2001,u) [apple-pie] exten => 2999,1,VoiceMailMain(${CALLERID(num)},s)
If the context is not specified for a SIP phone in sip.conf, Asterisk assumes the [default] context in extensions.conf.