login | register
Wed 09 of Jul, 2008 [05:18 UTC]

voip-info.org

Discuss [7] History

Asterisk non-root

Created by: JustRumours,Last modification on Mon 19 of May, 2008 [16:08 UTC] by tzafrir
By default Asterisk is configured to run as root, that is, the superuser. This page is about why it is not a good idea and how to set it up to run as a different user.


Summary

Can Asterisk run as non-root?

Yes, but it requires a little tweaking.

Why would this be useful?

So that if Asterisk has a remote security compromise, this cannot be used to take over the entire box. Ideally the compromise shouldn't even allow editing of the config files (possible if you don't edit them on-the-fly via the console).

Distro Packages

I installed Asterisk from a distro package. Any tweaks necessary?

Debian packages (as of Sarge) and Ubuntu packages (at least as of 5-10, not sure) will refuse to run as non-root and won't need any changes.
FreeBSD ports run as root. Not sure about SuSE packages, Fedora-Extra packages, Mandrake-contrib packages, DAG packages or anything else.
Fedora - ATrpms repo : Installs as user=asterisk, group=asterisk but will fail to start with the error, "unable to access /var/log/asterisk/messages"

SELinux causes problems. To disable selinux change in /etc/selinux/config SELINUX=enabled to disabled or permissive

Quick Start

No-frills Guide for Asterisk 1.4.* (RedHat flavour) as of 19 May 2008

/etc/init.d/asterisk stop
#> Shutting down asterisk:                                      OK  
#----------------------------------------------------
#
# It is safer to define a user called asterisk in group asterisk
# unless you want to make more changes to 2 files i.e. zaptel.rules and /etc/init.d/asterisk
#
/usr/sbin/groupadd asterisk
#----------------------------------------------------
/usr/sbin/useradd -d /var/lib/asterisk -g asterisk asterisk
#> useradd: warning: the home directory already exists.
#> Not copying any file from skel directory into it.
#
#----------------------------------------------------
chown --recursive asterisk:asterisk /var/lib/asterisk
chown --recursive asterisk:asterisk /var/log/asterisk
chown --recursive asterisk:asterisk /var/run/asterisk
chown --recursive asterisk:asterisk /var/spool/asterisk
chown --recursive asterisk:asterisk /usr/lib/asterisk
chown --recursive asterisk:asterisk /dev/zap
#----------------------------------------------------
chmod --recursive u=rwX,g=rX,o= /var/lib/asterisk
chmod --recursive u=rwX,g=rX,o= /var/log/asterisk
chmod --recursive u=rwX,g=rX,o= /var/run/asterisk
chmod --recursive u=rwX,g=rX,o= /var/spool/asterisk
chmod --recursive u=rwX,g=rX,o= /usr/lib/asterisk
chmod --recursive u=rwX,g=rX,o= /dev/zap
#----------------------------------------------------
chown --recursive root:asterisk /etc/asterisk
chmod --recursive u=rwX,g=rX,o= /etc/asterisk
#----------------------------------------------------
cp /etc/asterisk/asterisk.conf /etc/asterisk/asterisk.conf.org
vi /etc/asterisk/asterisk.conf

# Change the following line from:
#
# astrundir => /var/run
#
# to
#
# astrundir => /var/run/asterisk
#
#----------------------------------------------------
cp /etc/init.d/asterisk /etc/init.d/asterisk.org
#----------------------------------------------------
vi /etc/init.d/asterisk

# Change the following line from:
#
# #AST_USER="asterisk"
# #AST_GROUP="asterisk"
#
# to
#
# AST_USER="asterisk"
# AST_GROUP="asterisk"
#
#----------------------------------------------------

# Asterisk needs to write to voicemail.conf for password change.
#
chmod g+w /etc/asterisk/voicemail.conf
chmod g+w,+t /etc/asterisk
#----------------------------------------------------

# Let's restart Asterisk to ensure that both start up methods work.
#
#----------------------------------------------------
/etc/init.d/asterisk restart
#----------------------------------------------------
asterisk -U asterisk -G asterisk
#----------------------------------------------------


User Account

Use your system's preferred method of adding a new user. Examples:
  • Red Hat: adduser -c "Asterisk PBX" -d /var/lib/asterisk
  • Debian: adduser --system --group --home /var/lib/asterisk --no-create-home --gecos "Asterisk PBX" asterisk
  • Debian: adduser asterisk dialout
  • Debian: adduser asterisk audio

Note that recent debian packages do this for you, including chown and chmod.
Just take care not to start asterisk as root accidentally without '-U' .

Run Directory

Asterisk >= 1.4

Edit your Asterisk config file (/etc/asterisk/asterisk.conf):

   astrundir => /var/run/asterisk

Asterisk <=1.2

Edit /usr/src/asterisk/Makefile and change the definition of ASTVARRUNDIR like this:

   ASTVARRUNDIR=$(INSTALL_PREFIX)/var/run/asterisk

Recompile and reinstall Asterisk. See Compiling Asterisk for details of this process.

Permissions

Asterisk needs write permission for these directories and their contents:
  • /var/lib/asterisk
  • /var/log/asterisk
  • /var/run/asterisk
  • /var/spool/asterisk
  • /usr/lib/asterisk
  • /dev/zap/* (Though better done through udev rules for Zaptel. See the [http://zaptel.tzafrir.org.ilZaptel README.)

The files in the /var/spool/asterisk/outgoing directory need to be owned by the asterisk user as well as writable. Writable because asterisk appends lines to indicate retry status. Owner so that it can set the utime. Covered below.

   chown --recursive asterisk:asterisk /var/lib/asterisk
   chown --recursive asterisk:asterisk /var/log/asterisk
   chown --recursive asterisk:asterisk /var/run/asterisk
   chown --recursive asterisk:asterisk /var/spool/asterisk
   chown --recursive asterisk:asterisk /usr/lib/asterisk

   chmod --recursive u=rwX,g=rX,o= /var/lib/asterisk
   chmod --recursive u=rwX,g=rX,o= /var/log/asterisk
   chmod --recursive u=rwX,g=rX,o= /var/run/asterisk
   chmod --recursive u=rwX,g=rX,o= /var/spool/asterisk
   chmod --recursive u=rwX,g=rX,o= /usr/lib/asterisk

Also, make note that if you're running udev on your system (linux-2.6), the /dev directory
is dynamically populated with device nodes, meaning that any permissions you set on /dev/zap
will be lost on your next reboot, and you may get a nasty message such as "Asterisk ended with exit status 1"
when trying to start asterisk. Read the file /path/to/zaptel-src-1.2.x/README.udev for instructions on
how to change the user/group assigned to /dev/zap.
Asterisk needs read permission for these directories and their contents:
  • /etc/asterisk

   chown --recursive root:asterisk /etc/asterisk
   chmod --recursive u=rwX,g=rX,o= /etc/asterisk

You might also have to take a look at the permissions of the web voicemail cgi files/directory.


Other files and devices may also need to be tweaked depending on your exact setup.

e.g. If you use chan_oss:
   chown asterisk /dev/dsp

If running chan_capi for ISDN devices:
   chown asterisk /dev/capi20

If using the Sirrix cards :-
   chown asterisk /dev/bchdev /dev/dchdev

If using chan_alsa:
   chown --recursive asterisk /dev/snd

SUID root executables


If you're using musiconhold with mpg123, you'll probably need to set the suid bit on the executable like this:

   chmod u+s /usr/local/bin/mpg123

this allows mpg123 to run as root even though Asterisk is running as a non-root user. This appears to be necessary for mpg123 to work properly under Asterisk.

However recall that mpg123 has some known security issues. Don't use it to play arbitrary data from the web.

Using mpg123 as SUID root makes it even more insecure. As of Asterisk 1.2 it is only really needed for remote streaming media.

Starting Asterisk

Starting asterisk is covered elsewhere. However once you've done the above changes, you can make sure asterisk runs as user asterisk and group asterisk by issuing this command in your startup scripts:

   asterisk -U asterisk

Troubleshooting

Problems getting it to work?

As root run the command:
 strace -eopen asterisk -U asterisk
And look for failures to open files. Modify the ownership and permissions of the culprits and try again.

If you use the option -p (real-time priority) to Asterisk, Asterisk must be executed as root. Even if it later drops the root privileges. (using -U). Thus you should not
use 'su asterisk' to run safe_asterisk. Anybody who actually uses safe_asterisk with -p, please fix this.

Also, take care not to run Asterisk without -U asterisk . You may be tempted to do so for debugging. The Debian package (in Xorcom Rapid and in current Etch ) has /etc/init.d/asterisk debug for that.

Asterisk ended with exit status 1

See Also



Asterisk | Asterisk Installation | Compiling Asterisk

Comments

Comments Filter
222

333Problems with start-stop-daemon

by _Thomas_, Thursday 12 of April, 2007 [23:14:53 UTC]
I had some problems with * as non-root and the Debian start-stop-daemon (which is used by the /etc/init.d/asterisk script).

Using "asterisk -U asterisk" worked: asterisk ran as user "asterisk".
Using "start-stop-daemon -S --exec asterisk — -U asterisk" didn't work: asterisk ran as user "root"

After hours of searching the net I found a solution/workaround for this: Simply add the following to your /etc/asterisk/asterisk.conf:

[options]
runuser => asterisk
222

333

by msg, Saturday 09 of September, 2006 [16:48:46 UTC]
safe_asterisk has a variable called ASTARGS which can be assigned the value of "-U asterisk" to make safe_asterisk run the program as a non-root user without needing to su to execute the script.
222

333

by coatesg, Monday 20 of February, 2006 [16:09:26 UTC]
And 3.:

I'm sure asterisk needs a shell to run safe_asterisk in:

su -s /bin/bash -c /usr/sbin/safe_asterisk

It doesn't run on my system otherwise (I have /bin/false set as the shell for asterisk in /etc/passwd).

222

333

by coatesg, Monday 20 of February, 2006 [15:58:06 UTC]
Having followed the above using a Debian Sarge system, I found the following:

1. /usr/sbin/safe_asterisk fails if /usr/sbin is not in the asterisk user's $PATH - I fixed it by adding:

PATH=$PATH:/usr/sbin

to the top of safe_asterisk

2. The user asterisk is not in the group asterisk! So don't get yourself confused over group permissions...
222

333System user

by kleptog, Wednesday 16 of March, 2005 [12:18:06 UTC]
It's probably a good idea to add --system to the adduser line, since it's not actually a real user.
222

333TOS bits

by andrewid, Monday 21 of February, 2005 [15:11:54 UTC]
Asterisk can`t set tos bit in user mode, so you have to set it in iptables:

  1. Set TOS bit
iptables -A OUTPUT -t mangle -p udp -m udp --dport 5060 -j DSCP --set-dscp 0x28
iptables -A OUTPUT -t mangle -p udp -m udp --sport 10000:20000 -j DSCP --set-dscp 0x28

222

333Doesn't work for music on hold

by , Tuesday 18 of May, 2004 [23:43:38 UTC]
The debian packages use this method... the server fails to spawn the mpg123 executables correctly so music on hold fails.