00001 #!/bin/sh
00002 #
00003 # $Id: add-imscore-user.sh 186 2007-03-09 12:58:35Z jsbach $
00004 #
00005 # add-imscore-user.sh
00006 # Version: 0.2
00007 # Released: 02/06/07
00008 # Author: Sven Bornemann -at- materna de
00009 #
00010 # History:
00011 # 0.3 (03/09/07(:
00012 # * sip2ims transactions are commented out.
00013 # 0.2 (02/06/07):
00014 # * Changed parameter handling (getopt).
00015 # * Allow direct mysql import.
00016 # * Remove temporary password file after usage.
00017 # 0.1 (02/02/07):
00018 # * Initial version
00019 #
00020 # Script for generating two SQL scripts for creating/deleting IMS Core users
00021 # in the HSS and the SIP2IMS gateway tables.
00022 #
00023 # Usage for add-imscore-user.sh: See Usage() procedure below
00024 #
00025 # Example for creating user 'brooke' with password 'brooke' for realm
00026 # 'open-ims.test':
00027 #
00028 # # ./add-imscore-user.sh -u brooke -a
00029 # Successfully wrote add-user-brooke.sql
00030 # Successfully wrote delete-user-brooke.sql
00031 # Apply add-user-brooke.sql...
00032 # Enter password:
00033 # Successfully applied add-user-brooke.sql
00034 #
00035 # After applying the add script, you should be able to register with IMS Core
00036 # with SIP clients (e.g. as 'brooke') via SIP2IMS. Use delete script or -d
00037 # option for removing the user from IMS Core database tables.
00038 #
00039 # Known limits:
00040 # * IMS Core installation in /opt/OpenIMSCore required.
00041 # * Password is limited to 16 characters.
00042 #
00043
00044
00045 Usage()
00046 {
00047 echo "ERROR: Invalid parameters"
00048 echo "add-imscore-user.sh -u <user> [-r <realm> -p <password>] [-a|-d]"
00049 echo " -u <user>: The username (e.g. 'brooke')"
00050 echo " -r <realm>: The realm. Default is 'open-ims.test'"
00051 echo " -p <password>: The password. Default is value of -u option"
00052 echo " -a: Automatically apply created add script (script will not be deleted afterwards)"
00053 echo " -d: Automatically apply created delete script (script will be deleted afterwards)"
00054 exit -1
00055 }
00056
00057 OPTION_ADD=0
00058 OPTION_DELETE=0
00059 SCRIPT=
00060 EXIT_CODE=0
00061 DBUSER=root
00062
00063 while getopts u:r:p:o:ad? option;
00064 do
00065 case $option in
00066 u) IMSUSER=$OPTARG;;
00067 r) REALM=$OPTARG;;
00068 p) PASSWORD=$OPTARG;;
00069 a) OPTION_ADD=1;;
00070 d) OPTION_DELETE=1;;
00071 esac
00072 done
00073
00074 [ -z "$IMSUSER" ] && Usage
00075 [ -z "$PASSWORD" ] && PASSWORD=$IMSUSER
00076 [ -z "$REALM" ] && REALM=open-ims.test
00077
00078 # Some checks
00079 [ $OPTION_ADD -eq 1 ] && [ $OPTION_DELETE -eq 1 ] && Usage;
00080 [ -z "$IMSUSER" ] && Usage;
00081
00082 KEY=`/opt/OpenIMSCore/ser_ims/utils/gen_ha1/gen_ha1 $IMSUSER@$REALM $REALM $PASSWORD`
00083
00084 CREATE_SCRIPT="add-user-$IMSUSER.sql"
00085 DELETE_SCRIPT="delete-user-$IMSUSER.sql"
00086 SED_SCRIPT="s/<USER>/$IMSUSER/g"
00087 PASSWORD_FILE=~temp~password~
00088
00089 echo -n $PASSWORD > $PASSWORD_FILE
00090 ENCODED_PASSWORD=`hexdump -C < $PASSWORD_FILE|cut -b 10-60|sed 's/
00091 ENCODED_PASSWORD=`echo $ENCODED_PASSWORD|cut -b 1-32`
00092 rm $PASSWORD_FILE
00093
00094 CREATE_SCRIPT_TEMPLATE="insert into hssdb.imsu(name) values ('<USER>_imsu');
00095
00096 --add Private Identity
00097
00098 --Add <USER>@$REALM
00099 insert into hssdb.impi(
00100 impi_string,
00101 imsu_id,
00102 imsi,
00103 scscf_name,
00104 s_key,
00105 chrg_id,
00106 sqn)
00107 values( '<USER>@$REALM',
00108 (select imsu_id from hssdb.imsu where hssdb.imsu.name='<USER>_imsu'),
00109 '<USER>_ISDN_User_part_ID',
00110 'sip:scscf.$REALM:6060',
00111 '$ENCODED_PASSWORD',
00112 (select chrg_id from hssdb.chrginfo where hssdb.chrginfo.name='default_chrg'),
00113 '000000000000');
00114
00115 --add Public Identity
00116 insert into hssdb.impu(sip_url, tel_url, svp_id) values ('sip:<USER>@$REALM', '', (select svp_id from hssdb.svp where hssdb.svp.name='default_sp'));
00117
00118 --add Public Identity to Private Identity
00119 insert into hssdb.impu2impi(impi_id, impu_id) values ((select impi_id from hssdb.impi where hssdb.impi.impi_string='<USER>@$REALM'), (select impu_id from hssdb.impu where hssdb.impu.sip_url='sip:<USER>@$REALM'));
00120
00121 --add roaming network
00122 insert into hssdb.roam(impi_id, nw_id) values((select impi_id from hssdb.impi where hssdb.impi.impi_string='<USER>@$REALM'), (select nw_id from hssdb.networks where hssdb.networks.network_string='$REALM'));
00123
00124 -- add SIP2IMS credentials
00125 -- insert into sip2ims.credentials values ('<USER>', '_none', '$REALM', '$PASSWORD',1,'','$KEY',(select imsu_id from hssdb.imsu where hssdb.imsu.name='<USER>_imsu'));"
00126
00127
00128 DELETE_SCRIPT_TEMPLATE=" --delete from sip2ims.credentials where auth_username='<USER>';
00129
00130 delete from hssdb.roam where impi_id = (select impi_id from hssdb.impi where hssdb.impi.impi_string='<USER>@$REALM');
00131
00132 delete from hssdb.impu2impi where impi_id = (select impi_id from hssdb.impi where hssdb.impi.impi_string='<USER>@$REALM');
00133
00134 delete from hssdb.impi where imsu_id = (select imsu_id from hssdb.imsu where hssdb.imsu.name='<USER>_imsu');
00135
00136 delete from hssdb.impu where sip_url = 'sip:<USER>@$REALM';
00137
00138 delete from hssdb.imsu where name = '<USER>_imsu';"
00139
00140 # Create SQL add script
00141 echo "$CREATE_SCRIPT_TEMPLATE" | sed $SED_SCRIPT > $CREATE_SCRIPT
00142 if [ $? -ne 0 ]; then
00143 echo "Failed to write $CREATE_SCRIPT"
00144 exit -1
00145 fi
00146 echo "Successfully wrote $CREATE_SCRIPT"
00147
00148 # Create SQL delete script
00149 echo "$DELETE_SCRIPT_TEMPLATE" | sed $SED_SCRIPT > $DELETE_SCRIPT
00150 if [ $? -ne 0 ]; then
00151 echo "Failed to write $DELETE_SCRIPT"
00152 exit -1
00153 fi
00154 echo "Successfully wrote $DELETE_SCRIPT"
00155
00156 # Apply scripts directly?
00157 if [ $OPTION_ADD -eq 1 ]; then
00158 echo Apply $CREATE_SCRIPT as user $DBUSER...
00159 mysql -u $DBUSER -p < $CREATE_SCRIPT
00160 EXIT_CODE=$?
00161 SCRIPT=$CREATE_SCRIPT
00162 elif [ $OPTION_DELETE -eq 1 ]; then
00163 echo Apply $DELETE_SCRIPT as user $DBUSER...
00164 mysql -u $DBUSER -p < $DELETE_SCRIPT
00165 EXIT_CODE=$?
00166 SCRIPT=$DELETE_SCRIPT
00167 fi
00168
00169 # Evaluate exit code
00170 if [ ! -z "$SCRIPT" ]; then
00171 if [ $EXIT_CODE -ne 0 ]; then
00172 echo "ERROR: Failed to apply $SCRIPT"
00173 else
00174 echo "Successfully applied $SCRIPT"
00175 fi
00176 fi
00177 exit $EXIT_CODE