Securing MythWeb

From MythTV Official Wiki
Revision as of 20:23, 10 December 2014 by Darcy (talk | contribs) ({{FedoraVersion}}: Added bar of port 80)

Jump to: navigation, search

Time.png Outdated: Inconsistent and confusing information

This assumes you have decided to open your web server to the internet, and now you need to secure it. This guide will take you through setting up simple authentication with an Apache server. Please see the Apache [1] for more specifics or more secure methods.

There are several ways to secure MythWeb:

  • Modify the MythWeb .htaccess file to require password authentication
  • Modify the Apache configuration to require password authentication
  • Setup an SSH tunnel for remote access to MythWeb (assuming that you do not open your MythWeb server to the internet except for port-forwarding SSH from your router/firewall)

These instructions assume MythWeb is installed in the default path (/var/www/html/mythweb).

MythWeb .htaccess

The simplest option is to edit /var/www/html/mythweb/.htaccess and read the comments there about what to uncomment to secure MythWeb. This option does not require Apache to be restarted, but is slightly less efficient than adding the options to the Apache config.

You may use either AuthType Basic or AuthType digest -- "Basic" is more compatible, "Digest" is more secure. If you use "Basic", you must use htpasswd to create users. If you use "Digest", you must use htdigest to create users.

openSUSE 10.2

Geeko head48.pngThese steps are for openSUSE 10.2. The instructions are written using "htpasswd2" instead of "htdigest2" because that is what I got to work. I'm sure you could use "htdigest2", I'm just now sure how. These steps should work fine though.

1. create a new password file with htpasswd22
WARNING: the "-c" will wipe out the file if it already exists

/usr/bin/htpasswd2 -c /etc/apache2/conf.d/httpd-passwords userid

You will be prompted to put in a password for the userid
2. create any additional user IDs (no "-c")

/usr/bin/htpasswd2 /etc/apache2/conf.d/httpd-passwords another_userid

3. Find the user:group that apache2 is running as

cat /etc/apache2/uid.conf

You file will probably look like this

User wwwrun
Group www

4. Change the ownership of the password file (change "wwwrun:www" to match the "User:Group" from the previous step)

chown wwwrun:www /etc/apache2/conf.d/httpd-passwords

5. If the .htaccess exists, which it probably does if you have mythweb installed, back it up. This is just in case you mess it up and it need to start over.

cd /srv/www/htdocs/mythweb/
cp ./.htaccess ./.htaccess.orig

6. Open .htaccess for modification with vi - or whatever floats your boat

vi ./.htaccess

7. The following lines should be present but commented. Remove the "#" from the start of each line, modify them to fit your setup, and save the file. Changes are immediate. Pay special attention to the bolded lines

AuthType           Basic
AuthName           "MythTV"
AuthUserFile       /etc/apache2/conf.d/httpd-passwords
Require            valid-user
BrowserMatch       "MSIE"      AuthDigestEnableQueryStringHack=On

Save the file and authentication should begin immediately.

8. If you don't want to bother with authentication when you are on the same network, add the following lines and authentication will be bypassed (assuming 192.168.1.0 is your local network).

Allow from 192.168.1.
Satisfy any

If you aren't sure what your network is, if you are using a router purchased from a store, you could probably get away with specifying "Allow from 192.168.". This would also cover you if you move your box from one location to another using a different third octet in the IP. With this in place, though, you should not be prompted now for authentication while on the local network.

UPDATE: I'm not 100% sure that this last step works. I tested from a remote site and was able to access MythWeb without being prompted for a password. The remote site also used the 192.168.1 network range. After removing the last two entries above, it prompted right away.

Fedora 21

When installing the mythtv package, the mythweb package is installed along side it. The default installation handles most of the SELinux contexts and permissions under /usr/share/mythweb/. To turn MythWeb on, simply start up the Apache server and allow to run on boot:

# systemctl enable httpd.service
# systemctl restart httpd.service

At this stage, it's probably wise to turn on https so that you have an extra layer of encryption when viewing from the outside web. Install the mod_ssl package with:

# yum install mod_ssl

This should create a ssl.conf file in /etc/httpd/conf.d/. Create a new certificate using these [2]. Make the necessary changes to your ssl.conf file to point to the newly generated key and certificate at the following location:

Script.png ssl.conf
...
#   Server Certificate:
# Point SSLCertificateFile at a PEM encoded certificate.  If
# the certificate is encrypted, then you will be prompted for a
# pass phrase.  Note that a kill -HUP will prompt again.  A new
# certificate can be generated using the genkey(1) command.
SSLCertificateFile /etc/pki/tls/certs/YOURDOMAIN.com.crt

#   Server Private Key:
#   If the key is not combined with the certificate, use this
#   directive to point at the key file.  Keep in mind that if
#   you've both a RSA and a DSA private key you can configure
#   both in parallel (to also allow the use of DSA ciphers, etc.)
SSLCertificateKeyFile /etc/pki/tls/private/YOURDOMAIN.com.key
...

Optional - bar access to port 80 by commenting out Listen 80 in /etc/httpd/conf/:

Script.png httpd.conf

...
# Change this to Listen on specific IP addresses as shown below to
# prevent Apache from glomming onto all bound IP addresses.
#
#Listen 12.34.56.78:80
#Listen 80

#
# Dynamic Shared Object (DSO) Support
...

Ubuntu 9.10 (Karmic)

There are a couple of differences for Karmic (and possibly other Ubuntu flavours) to get this to work.

First problem is that auth_digest module for Apache isn't loaded by default in this distro. Get it to load using:

sudo a2enmod auth_digest

Next problem is that the MythWeb configuration isn't in the .htaccess file, but is in /etc/apache2/sites-enabled/mythweb.conf file. So when the instructions tell you to uncomment the relevent sections in .htaccess, do it in the mythweb.conf file.

Create a Password File

You will need to create an htdigest password file -- a suggested location is /etc/httpd/conf, but it should be a location that gets backed up regularly.

htdigest -c /etc/httpd/conf/httpd-passwords MythTV MYUSER1

Create additional users as needed:

htdigest /etc/httpd/conf/httpd-passwords MythTV MYUSER2

Please make sure you DO NOT use the -c after the initial user, as this will overwrite the file and start from scratch.

Modify ownership of the file to match the user and group Apache is running as -- try

grep -A 2 ^User /etc/httpd/conf/httpd.conf 
chown apache.apache /etc/httpd/conf/httpd-passwords
chmod 640 /etc/httpd/conf/httpd-passwords

Modify .htaccess

(added 4/3/08. using .htaccess isn't supported by MythTV .21. see below.)

nano /var/www/html/mythweb/.htaccess

The following lines should be present but commented. Remove the "#" from the start of each line, modify them to fit your setup, and save the file. Changes are immediate.

    AuthType           Digest
    AuthName           "MythTV"
    AuthUserFile       /etc/httpd/conf/httpd-passwords
    Require            valid-user
    BrowserMatch       "MSIE"      AuthDigestEnableQueryStringHack=On

Adding the following optional lines will enforce authentication from anywhere outside your local network (assuming 192.168.1.0 is your local network):

    Order deny,allow
    Allow from 192.168.1. 127.
    Deny from all
    Satisfy any

Authentication should begin working immediately.

The following is what I got from the Mailing List for securing Mythweb in MythTV .21 Get rid of your .htaccess files. Edit /etc/httpd/conf.d/mythweb.conf like this: Uncomment the htdigest section:

AuthType Digest
AuthName "MythTV"
AuthUserFile /var/www/htdigest
Require valid-user
BrowserMatch "MSIE" AuthDigestEnableQueryStringHack=On
Order allow,deny
Satisfy any


Then use htdigest (need to be root to do this or sudo the command) to create your digest file: htdigest -c /var/www/htdigest MythTV username

Then it will ask for your password.

Restart apache and it should work.

Apache Configuration

This method is slightly more efficient than the .htaccess method, but is complicated by the fact that you must restart Apache and mis-configuring the Apache configuration file will keep Apache from restarting.

Apache password file

We will start with creating an Apache password file. I put this in /etc/httpd/conf, as I back up this directory every night to one of my other systems.

htpasswd -c /etc/httpd/conf/httpd-passwords MYUSER1

Create additional users as needed:

htpasswd /etc/httpd/conf/httpd-passwords MYUSER2

Please make sure you DO NOT use the -c after the initial user, as this will overwrite the file and start from scratch.

Modify ownership of the file as follows:

chown apache.apache /etc/httpd/conf/httpd-passwords
chmod 640 /etc/httpd/conf/httpd-passwords

Editing Apache Config

Now edit /etc/httpd/conf/httpd.conf, and add the following section:

<Directory "/var/www/html/mythweb">
    Options Indexes FollowSymLinks
    AuthType Basic
    AuthName "MythTV"
    AuthUserFile /etc/httpd/conf/httpd-passwords
    require user MYUSER1 MYUSER2 MYUSER3
    Order allow,deny
    Allow from all
</Directory>

Modify the password file location and required users per your needs.

If you have created a link from your music storage area to /var/www/html/mythweb/music, you can add the following to separately secure web access to this:

#MythWeb music configuration
<Directory "/var/www/html/mythweb/music">
    Options Indexes FollowSymLinks
    AuthType Basic
    AuthName "MythTV-Music"
    AuthUserFile /etc/httpd/conf/httpd-passwords
    Require user MYUSER4 MYUSER5
    Order allow,deny
    Allow from all
</Directory>

Again, tailor your password file and users to your needs. You can even use separate password files if you wish!

Restart Apache

Now restart Apache, and you're done:

service httpd restart

(this may be different on your distro)

Other options

You can do a lot more with these configuration sections, such as secure by ip address, and more. Please see the Apache docs[3].

Questions: email me at johanreinalda at yahoo dot com. Preferred is an email to Myth Users list, however. Here's the configuration I used to allow users on my LAN to access MythWeb without a password, and require outside users to authenticate via pw

<Directory "/var/www/html/mythweb">
  Options Indexes FollowSymLinks
  AuthType Basic
  AuthName "MythTV"
  AuthUserFile /etc/httpd/conf/httpd-passwords
  Require valid-user
  Order allow,deny
  Allow from 192.168.1.
  Satisfy any
</Directory>