| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169 | #!/bin/bash
#
# .---.                  .              .
# |                      |              |
# |--- .--. .-.  .-.  .-.|  .-. .--.--. |.-.  .-. .--.  .-.
# |    |   (.-' (.-' (   | (   )|  |  | |   )(   )|  | (.-'
# '    '     --'  --'  -' -  -' '  '   -' -'   -' '   -  --'
#
#                    Freedom in the Cloud
#
# Adds a SIP phone user to the system
# License
# =======
#
# Copyright (C) 2015-2016 Bob Mottram <bob@freedombone.net>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
PROJECT_NAME='freedombone'
export TEXTDOMAIN=${PROJECT_NAME}-addsipuser
export TEXTDOMAINDIR="/usr/share/locale"
MY_USERNAME=
EXTENSION=
PASSWORD=
CONFIG_FILE=/etc/sipwitch.conf
USER_EXISTS="no"
function show_help {
    echo ''
    echo $"${PROJECT_NAME}-addsipuser -u [username] -e [extension] -p [password]"
    echo ''
    exit 0
}
function sip_user_exists {
    IFS=''
    while read line; do
    if [[ "$line" == *"<user id=\"$MY_USERNAME\">" ]]; then
        USER_EXISTS="yes"
        return
    fi
    done < $CONFIG_FILE
}
function update_sip_user {
    USER_FOUND=
    NEW_CONFIG_FILE="${CONFIG_FILE}.new"
    if [ -f $NEW_CONFIG_FILE ]; then
    rm -f $NEW_CONFIG_FILE
    fi
    touch $NEW_CONFIG_FILE
    IFS=''
    while read line; do
    if [ ! $USER_FOUND ]; then
        if [[ "$line" == *"<user id=\"$MY_USERNAME\">" ]]; then
        USER_FOUND="yes"
        fi
    else
        if [[ "$line" == *"<extension>"* ]]; then
        line="      <extension>$EXTENSION</extension>"
        fi
        if [[ "$line" == *"<secret>"* ]]; then
        line="      <secret>$PASSWORD</secret>"
        fi
        if [[ "$line" == *"<display>"* ]]; then
        line="      <display>$MY_USERNAME $EXTENSION</display>"
        USER_FOUND=
        fi
    fi
    echo $line >> $NEW_CONFIG_FILE
    done < $CONFIG_FILE
    mv $NEW_CONFIG_FILE $CONFIG_FILE
}
function add_sip_user {
    NEW_CONFIG_FILE="${CONFIG_FILE}.new"
    if [ -f $NEW_CONFIG_FILE ]; then
    rm -f $NEW_CONFIG_FILE
    fi
    touch $NEW_CONFIG_FILE
    IFS=''
    while read line; do
    if [[ "$line" == *'</provision>' ]]; then
        echo "    <user id=\"$MY_USERNAME\">" >> $NEW_CONFIG_FILE
        echo "      <extension>$EXTENSION</extension>" >> $NEW_CONFIG_FILE
        echo "      <secret>$PASSWORD</secret>" >> $NEW_CONFIG_FILE
        echo "      <display>$MY_USERNAME $EXTENSION</display>" >> $NEW_CONFIG_FILE
        echo '    </user>' >> $NEW_CONFIG_FILE
    fi
    echo $line >> $NEW_CONFIG_FILE
    done < $CONFIG_FILE
    mv $NEW_CONFIG_FILE $CONFIG_FILE
    chmod 600 /etc/shadow
    chmod 600 /etc/gshadow
    usermod -aG sipwitch $MY_USERNAME
    chmod 0000 /etc/shadow
    chmod 0000 /etc/gshadow
}
while [[ $# > 1 ]]
do
    key="$1"
    case $key in
    -h|--help)
        show_help
        ;;
    -u|--user)
        shift
        MY_USERNAME="$1"
        ;;
    -e|--extension)
        shift
        EXTENSION="$1"
        ;;
    -p|--password)
        shift
        PASSWORD="$1"
        ;;
    *)
        # unknown option
        ;;
    esac
    shift
done
if ! [[ $MY_USERNAME && $EXTENSION && $PASSWORD ]]; then
    show_help
fi
if [ ! -f $CONFIG_FILE ]; then
    echo $"SIP configuration file not found"
    exit 1
fi
# the user must already exist on the system
if [ ! -d /home/$MY_USERNAME ]; then
    echo $"User $MY_USERNAME not found"
    exit 2
fi
sip_user_exists
if [[ $USER_EXISTS == "yes" ]]; then
    update_sip_user
    echo $"SIP user $MY_USERNAME amended"
else
    add_sip_user
    echo $"SIP user $MY_USERNAME added"
fi
systemctl restart sipwitch
exit 0
 |