Help - Search - Members - Calendar
Full Version: Manual management of FreeBSD with help of some scripts.
The Planet Forums > Operating Systems > BSD
X-Istence
xscript-addusr
CODE
#!/bin/sh



DNSDOMAIN='example.net'

MAILIP='0.0.0.0'

MAINIP='0.0.0.0'

DNSDATALOCATION='/usr/local/djbdns/tinydns-229/root'

DNSDATADIR='/usr/local/djbdns/data/'

MYSQLPASSWORD='mysqlpassword' # Note: EXTREME SECURITY RISK IF FILE IS NOT CHMODDED CORRECTLY



echo "This script will add the records for Apache, MySQL and DNS"



echo "Please enter a username (No spaces):"

USERNAME=`perl -e '$r=<STDIN>; chomp($r); print $r'`

echo "Please enter a password:"

PASSWORD=`perl -e '$r=<STDIN>; chomp($r); print $r'`

echo "Please enter the domain (Sub-domains allowed):"

DOMAIN=`perl -e '$r=<STDIN>; chomp($r); print $r'`



echo "$PASSWORD" | pw user add $USERNAME -m -s /bin/sh -h 0

mkdir /usr/home/$USERNAME/public_html/

chmod 755 /usr/home/$USERNAME/public_html/

chown $USERNAME:$USERNAME /usr/home/$USERNAME/public_html/





cat << EOF >> /usr/local/etc/apache2/Includes/$DOMAIN.conf

<VirtualHost *:80>

       DocumentRoot "/usr/home/$USERNAME/public_html"

       ServerName $DOMAIN

       ServerAlias www.$DOMAIN

       <Directory "/usr/home/$USERNAME/public_html">

               Options Indexes FollowSymLinks MultiViews

               AllowOverride All

               Order allow,deny

               Allow from all

       </Directory>

       ErrorLog logs/$DOMAIN-error_log

</VirtualHost>

EOF



touch /usr/local/logs/$DOMAIN-error_log

touch /usr/local/logs/$DOMAIN-access_log



echo "Is $DOMAIN a sub-domain (1 = Yes; 0 = No):"

DOMAINEXIST=`perl -e '$r=<STDIN>; chomp($r); print $r'`

if [ 1 -eq $DOMAINEXIST ]; then

       echo "Domain will not be added to DNS"

else

       echo "Domain will be added to DNS"

cat << EOF > /usr/local/djbdns/data/$DOMAIN





# --

# $DOMAIN

# --



# NS entries

.$DOMAIN::a.ns.$DNSDOMAIN

.$DOMAIN::b.ns.$DNSDOMAIN



# Hosts

=$DOMAIN:$MAINIP



# Aliases

+www.$DOMAIN:$MAINIP

+ftp.$DOMAIN:$MAINIP

+*.$DOMAIN:$MAINIP



# MX entries

@$DOMAIN:$MAINIP:a



# SPF

'$DOMAIN:v=spf1 ip4072$MAILIP mx -all:3600

'a.mx.$DOMAIN:v=spf1 a -all:3600

EOF



OLDPWD=`pwd`

cd $DNSDATADIR

cat * > $DNSDATALOCATION/data

cd $DNSDATALOCATION

make

cd $OLDPWD



fi



echo "Would you like to add this user to MySQL (1 = Yes; 0 = No):"

ADDSQL=`perl -e '$r=<STDIN>; chomp($r); print $r'`

if [ 1 -eq $ADDSQL ]; then

       echo "User will be added to MySQL."

cat << EOF | mysql -u root -p${MYSQLPASSWORD}

GRANT USAGE ON *.* TO $USERNAME@localhost IDENTIFIED BY "$PASSWORD" WITH MAX_QUERIES_PER_HOUR 0 MAX_CONNECTIONS_PER_HOUR 0 MAX_UPDATES_PER_HOUR 0;

GRANT SELECT , INSERT , UPDATE , DELETE , CREATE , DROP , INDEX , ALTER , CREATE TEMPORARY TABLES , LOCK TABLES ON `${USERNAME}_%` . * TO $USERNAME@localhost;

CREATE DATABASE ${USERNAME}_test;

EOF



else

       echo "User will not be added to MySQL."

fi



/usr/local/sbin/xscript-awstatsrun /usr/local/etc/apache2/Includes/$DOMAIN.conf



apachectl graceful


This script creates the following files when ran with the input of:

username: hello
password: none
domain: example.com

/usr/local/etc/apache2/Includes/example.com.conf
/usr/local/djbdns/data/example.com

And it adds the user to MySQL with the appropriate permissions, and creates them a sample database.

As can be seen, i use Apache 2, MySQL, and djbdns.

CODE
Include etc/apache2/Includes/*.conf


That is put at the bottom of httpd.conf so that Apache reads the .conf files the script creates. Removing a domain is as easy as moving the file to example.com.conf -> example.com.disabled. Also makes keeping track of domains easier, as you don't have to look through your entire httpd.conf anymore.

The data dir for djbdns is filled with files, which are then concatted together, and put into the data file, and then "make" is run to copy it to the second tinydns running on the second IP.

CODE
sedated# cat /usr/local/djbdns/tinydns-229/root/Makefile

data.cdb: data

       /usr/local/bin/tinydns-data

       cp -P data.cdb /usr/local/djbdns/tinydns-230/root/


This way removing a domain is as easy as removing it from the data dir, reconcatting the stuff into the file data, and then running make.

Users are added with standard pw, with some options, just check the script.

xscript-rebuilddns:
CODE
#!/bin/sh



DNSDATALOCATION='/usr/local/djbdns/tinydns-229/root'

DNSDATADIR='/usr/local/djbdns/data/'



OLDPWD=`pwd`

cd $DNSDATADIR

cat * > $DNSDATALOCATION/data

cd $DNSDATALOCATION

make

cd $OLDPWD


This is an external program so that you don't need to run xscrip-addusr to update DNS. Simple in use. That way you can add your own entries to /usr/local/djbdns/data/example.com without having to add a user.

xscript-awstatsrun:
CODE
#!/bin/sh



DOMAIN=`grep ServerName $1 | sed "/.*ServerName /s///"`

ALIASES=`grep ServerAlias $1 | sed "/.*ServerAlias /s///"`



cat << EOF > /usr/local/etc/awstats/awstats.$DOMAIN.conf

LogFile="/usr/local/logs/$DOMAIN-access_log"

SiteDomain="$DOMAIN"

HostAliases="$ALIASES"

Include "/usr/local/etc/awstats/include.awstats"

EOF


This creates the config file for awstats at /usr/local/etc/awstats/awstats.example.com.conf you pass the script one paramater, and that is the location of the config file for httpd.conf, which in this case just contains one domain name. It could be redone to accept all the paramters, but this was done in this way on purpose, that way if you add more aliases to the config file, rerunning this command will add the new aliases to the awstats config file anyways.

I include /usr/local/etc/awstats/include.awstats as that is where all the default values are set for awstats. So that for all domains that are hosted, they are the same. Means less configuration work if i need to make a change for something, or want less stats, or more stats.

Hope this helps someone, otherwise, ignore it.
facecake
quite the handy, i have a similar set of scripts my self, however runs a lighttpd instance for the user, adds a apache1 vhost, a qmail (ok vpopmail) domain, and a few other things
facecake
also, just out of interest

you have your /usr/local/djbdns/tinydns-229/root/Makefile copying the data file between tinydns-instance/root/

why not just have them as symlinks?
X-Istence
QUOTE (facecake)
also, just out of interest

you have your /usr/local/djbdns/tinydns-229/root/Makefile copying the data file between tinydns-instance/root/

why not just have them as symlinks?


because symlinks do not get linked to the new updated data.cdb, as it is renamed from the temporary file. This means the two tinydns's will not have the same data, and thus return bad results. Try it for yourself.
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2009 Invision Power Services, Inc.