| 1 |
SystemD Socket Activation for Asterisk |
| 2 |
====================================== |
| 3 |
|
| 4 |
This folder contains sample unit files which can be used as the basis of a |
| 5 |
socket activated Asterisk deployment. Socket activation support currently |
| 6 |
extends to the following listeners: |
| 7 |
|
| 8 |
* Asterisk Command-line Interface |
| 9 |
* Asterisk Manager Interface (clear text and TLS) |
| 10 |
* Builtin HTTP / HTTPS server |
| 11 |
|
| 12 |
The primary use case of this feature is to allow Asterisk to be started by |
| 13 |
other services through use of AMI, CLI or REST API. |
| 14 |
|
| 15 |
The examples and documentation assume that Asterisk was linked to libsystemd |
| 16 |
when compiled. This integration is required for `Type=notify` and socket |
| 17 |
activation to work. |
| 18 |
|
| 19 |
Security |
| 20 |
======== |
| 21 |
|
| 22 |
Care must be take if enabling socket activation on any IP:PORT that is not |
| 23 |
protected by a firewall. Any user that can reach any socket activation |
| 24 |
port can start Asterisk, even if they do not have valid credentials to sign |
| 25 |
into the service in question. Enabling HTTP socket activation on a system |
| 26 |
which provides SIP over websockets would allow remote users to start Asterisk |
| 27 |
any time the HTTP socket is running. |
| 28 |
|
| 29 |
This functionality bypasses the normal restriction where only 'root' can start |
| 30 |
a service. Enabling AMI socket activation allows any user on the local server |
| 31 |
to start Asterisk by running 'telnet localhost 5038'. |
| 32 |
|
| 33 |
CLI activation is secured by the combination of SocketUser, SocketGroup and |
| 34 |
SocketMode settings in the systemd socket. Only local users with access will |
| 35 |
be able to start asterisk by using CLI. |
| 36 |
|
| 37 |
|
| 38 |
Separate .socket units or a single unit |
| 39 |
======================================= |
| 40 |
|
| 41 |
Asterisk is a complex system with many components which can be enabled or |
| 42 |
disabled individually. Using socket activation requires deciding to use |
| 43 |
a single socket file or multiple separate socket files. |
| 44 |
|
| 45 |
The remainder of this README assumes separate socket units are used for each |
| 46 |
listener. |
| 47 |
|
| 48 |
|
| 49 |
Service and Socket files |
| 50 |
======================== |
| 51 |
|
| 52 |
All .socket and .service examples in this folder use "reasonable" default |
| 53 |
paths for Linux. Depending on your distribution and ./configure options |
| 54 |
you may need to modify these before installing. The files are meant to |
| 55 |
be examples rather than files to be blindly installed. |
| 56 |
|
| 57 |
|
| 58 |
Installing and enabling socket units |
| 59 |
==================================== |
| 60 |
|
| 61 |
Modify socket files as desired. Install them to a location where systemd |
| 62 |
will find them. pkg-config can be used to determine an appropriate location. |
| 63 |
|
| 64 |
For socket files to be managed directly by the local administrator: |
| 65 |
pkg-config systemd --variable systemdsystemconfdir |
| 66 |
|
| 67 |
For socket files to be deployed by package manager: |
| 68 |
pkg-config systemd --variable systemdsystemunitdir |
| 69 |
|
| 70 |
|
| 71 |
After installing socket files you must run 'systemctl daemon-reload' for |
| 72 |
systemd to read the added/modified units. After this you can enable the |
| 73 |
desired sockets, for example to enable AMI: |
| 74 |
systemctl enable asterisk-ami.socket |
| 75 |
|
| 76 |
|
| 77 |
Socket Selection |
| 78 |
================ |
| 79 |
|
| 80 |
Asterisk configuration is unchanged by use of socket activation. When a |
| 81 |
component that supports socket activation starts a listener in Asterisk, |
| 82 |
any sockets provided by systemd are iterated. The systemd socket is used |
| 83 |
when the bound address configured by Asterisk is an exact match with the |
| 84 |
address given by the ListenStream setting in the systemd socket. |
| 85 |
|
| 86 |
|
| 87 |
Command-line Interface |
| 88 |
====================== |
| 89 |
|
| 90 |
Symbolic links do not appear to be resolved when checking the CLI listener. |
| 91 |
This may be of concern since /var/run is often a symbolic link to /run. Both |
| 92 |
Asterisk and systemd must use /var/run, or both must use /run. Mismatching |
| 93 |
will result in service startup failure. |
| 94 |
|
| 95 |
When socket activation is used for Asterisk CLI some asterisk.conf options |
| 96 |
are ignored. The following options from the [files] section are ignored |
| 97 |
and must instead be set by the systemd socket file. |
| 98 |
* astctlowner - use SocketUser |
| 99 |
* astctlgroup - use SocketGroup |
| 100 |
* astctlpermissions - use SocketMode |
| 101 |
|
| 102 |
See asterisk-cli.socket for an example of these settings. |
| 103 |
|
| 104 |
|
| 105 |
Stopping Asterisk |
| 106 |
================= |
| 107 |
|
| 108 |
Some existing asterisk.service files use CLI 'core stop now' for the ExecStop |
| 109 |
command. It is not recommended to use CLI to stop Asterisk on systems where |
| 110 |
CLI socket activation is enabled. If Asterisk fails to start systemd still |
| 111 |
tries running the ExecStop command. This can result in an loop where ExecStop |
| 112 |
causes CLI socket activation to start Asterisk again. A better way to deal |
| 113 |
with shutdown is to use Type=notify and do not specify an ExecStop command. |
| 114 |
See the example asterisk.service. |
| 115 |
|
| 116 |
|
| 117 |
Unused Sockets |
| 118 |
============== |
| 119 |
|
| 120 |
Asterisk makes no attempt to check for sockets provided by systemd that are not |
| 121 |
used. It is the users responsibility to only provide sockets which Asterisk is |
| 122 |
configured to use. |