00001 #!/bin/sh
00002 #
00003 # $Id: add-imscore-user_newdb.sh 549 2008-04-09 07:55:11Z vingarzan $
00004 #
00005 # add-imscore-user_newdb.sh
00006 # Version: 0.5
00007 # Released: 02/06/07
00008 # Author: Sven Bornemann -at- materna de
00009 #
00010 # History:
00011 # 0.51 (04/08/08)
00012 # * added tel-URI support incl. implicit-set
00013 # 0.4 (06/21/07)
00014 # * upgraded to the new db structures
00015 # 0.3 (03/09/07):
00016 # * sip2ims transactions are commented out.
00017 # 0.2 (02/06/07):
00018 # * Changed parameter handling (getopt).
00019 # * Allow direct mysql import.
00020 # * Remove temporary password file after usage.
00021 # 0.1 (02/02/07):
00022 # * Initial version
00023 #
00024 # Script for generating two SQL scripts for creating/deleting IMS Core users
00025 # in the HSS and the SIP2IMS gateway tables.
00026 #
00027 # Usage for add-imscore-user.sh: See Usage() procedure below
00028 #
00029 # Example for creating user 'brooke' with password 'brooke' for realm
00030 # 'open-ims.test':
00031 #
00032 # # ./add-imscore-user.sh -u brooke -a
00033 # Successfully wrote add-user-brooke.sql
00034 # Successfully wrote delete-user-brooke.sql
00035 # Apply add-user-brooke.sql...
00036 # Enter password:
00037 # Successfully applied add-user-brooke.sql
00038 #
00039 # After applying the add script, you should be able to register with IMS Core
00040 # with SIP clients (e.g. as 'brooke') via SIP2IMS. Use delete script or -d
00041 # option for removing the user from IMS Core database tables.
00042 #
00043 # Known limits:
00044 # * IMS Core installation in /opt/OpenIMSCore required.
00045 # * Password is limited to 16 characters.
00046 #
00047
00048
00049 Usage()
00050 {
00051 echo "ERROR: Invalid parameters"
00052 echo "add-imscore-user.sh -u <user> [-t <tel-URI>] [-r <realm> -p <password>] [-a|-d]"
00053 echo " -u <user>: The username (e.g. 'brooke')"
00054 echo " -t <tel-URI>: The tel-URI (e.g. 'tel:+49123456789'). If used with -a, -d requires"
00055 echo " this parameter too!"
00056 echo " -r <realm>: The realm. Default is 'open-ims.test'"
00057 echo " -i <impi>: The Private Identity (e.g. 'brooke@open-ims.test'). The -u option overrides this."
00058 echo " -b <impu>: The Public Identity (e.g. 'sip:brooke@open-ims.test') The -u option overrides this."
00059 echo " -p <password>: The password. Default is value of -u option"
00060 echo " -a: Automatically apply created add script"
00061 echo " -d: Automatically apply created delete script"
00062 echo " -c: Delete the scripts afterwards (by default they are not deleted)"
00063 exit -1
00064 }
00065
00066 OPTION_ADD=0
00067 OPTION_DELETE=0
00068 OPTION_CLEANUP=0
00069 SCRIPT=
00070 EXIT_CODE=0
00071 DBUSER=root
00072
00073 while getopts u:r:i:b:p:o:c?:adt:? option;
00074 do
00075 case $option in
00076 u) IMSUSER=$OPTARG;;
00077 r) REALM=$OPTARG;;
00078 i) IMPI=$OPTARG;;
00079 b) IMPU=$OPTARG;;
00080 t) TELURI=$OPTARG;;
00081 p) PASSWORD=$OPTARG;;
00082 a) OPTION_ADD=1;;
00083 d) OPTION_DELETE=1;;
00084 c) OPTION_CLEANUP=1;;
00085 esac
00086 done
00087
00088 [ -z "$REALM" ] && REALM=open-ims.test
00089 if [ -z "$IMSUSER" ];
00090 then
00091 IMSUSER=${IMPI%%@*}
00092 else
00093 IMPI="$IMSUSER@$REALM"
00094 IMPU="sip:$IMSUSER@$REALM"
00095 fi
00096 [ -z "$PASSWORD" ] && PASSWORD=$IMSUSER
00097 [ -z "$IMSUSER" ] && Usage
00098
00099 # Some checks
00100 [ $OPTION_ADD -eq 1 ] && [ $OPTION_DELETE -eq 1 ] && Usage;
00101
00102
00103 #KEY=`/opt/OpenIMSCore/ser_ims/utils/gen_ha1/gen_ha1 $IMSUSER@$REALM $REALM $PASSWORD`
00104
00105 CREATE_SCRIPT="add-user-$IMSUSER.sql"
00106 DELETE_SCRIPT="delete-user-$IMSUSER.sql"
00107 SED_SCRIPT="s/<USER>/$IMSUSER/g"
00108 PASSWORD_FILE=~temp~password~
00109
00110 echo -n $PASSWORD > $PASSWORD_FILE
00111 ENCODED_PASSWORD=`hexdump -C < $PASSWORD_FILE|cut -b 10-60|sed 's/
00112 ENCODED_PASSWORD=`echo $ENCODED_PASSWORD|cut -b 1-32`
00113 rm $PASSWORD_FILE
00114
00115 CREATE_SCRIPT_TEMPLATE="insert into hss_db.imsu(name) values ('<USER>_imsu');
00116
00117 --add Private Identity
00118
00119 --Add <USER>@$REALM
00120 insert into hss_db.impi(
00121 identity,
00122 id_imsu,
00123 k,
00124 auth_scheme,
00125 default_auth_scheme,
00126 amf,
00127 op)
00128 values( '$IMPI',
00129 (select id from hss_db.imsu where hss_db.imsu.name='<USER>_imsu'),
00130 '$PASSWORD',
00131 127,
00132 1,
00133 '\0\0',
00134 '\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0');
00135
00136 --add Public SIP Identity
00137 insert into hss_db.impu(identity,id_sp) values ('$IMPU', (select id from hss_db.sp order by id limit 1));
00138 update hss_db.impu set id_implicit_set=id where hss_db.impu.identity='$IMPU';
00139
00140 --add Public Identity to Private Identity
00141 insert into hss_db.impi_impu(id_impi,id_impu) values ((select id from hss_db.impi where hss_db.impi.identity='$IMPI'), (select id from hss_db.impu where hss_db.impu.identity='$IMPU'));
00142
00143 --add roaming network
00144 insert into hss_db.impu_visited_network(id_impu, id_visited_network) values((select id from hss_db.impu where hss_db.impu.identity='$IMPU'), (select id from hss_db.visited_network where hss_db.visited_network.identity='$REALM'));
00145 "
00146
00147 CREATE_TELURI_IMPU_TEMPLATE="
00148 --add Public tel-URI Identity
00149 insert into hss_db.impu(identity,id_sp) values ('$TELURI', (select id from hss_db.sp order by id limit 1));
00150 select @id:=id from hss_db.impu where hss_db.impu.identity='$IMPU';
00151 update hss_db.impu set id_implicit_set=@id where hss_db.impu.identity='$TELURI';
00152
00153 --add Public Identity to Private Identity
00154 insert into hss_db.impi_impu(id_impi,id_impu) values ((select id from hss_db.impi where hss_db.impi.identity='$IMPI'), (select id from hss_db.impu where hss_db.impu.identity='$TELURI'));
00155
00156 --add roaming network
00157 insert into hss_db.impu_visited_network(id_impu, id_visited_network) values((select id from hss_db.impu where hss_db.impu.identity='$TELURI'), (select id from hss_db.visited_network where hss_db.visited_network.identity='$REALM'));
00158 "
00159
00160 DELETE_SCRIPT_TEMPLATE="
00161 delete from hss_db.impu_visited_network where id_impu = (select id from hss_db.impu where hss_db.impu.identity='$IMPU');
00162 delete from hss_db.impi_impu where id_impi = (select id from hss_db.impi where hss_db.impi.identity='$IMPI');
00163 delete from hss_db.impu where identity = '$IMPU';
00164 delete from hss_db.imsu where name = '<USER>_imsu';
00165 "
00166
00167 DELETE_TELURI_IMPU_TEMPLATE="
00168 delete from hss_db.impu_visited_network where id_impu = (select id from hss_db.impu where hss_db.impu.identity='$TELURI');
00169 delete from hss_db.impi_impu where id_impi = (select id from hss_db.impi where hss_db.impi.identity='$IMPI');
00170 delete from hss_db.impu where identity = '$TELURI';
00171 "
00172
00173 DELETE_IMPI_TEMPLATE="
00174 delete from hss_db.impi where identity = '$IMPI';
00175 "
00176
00177 # Create SQL add script
00178 echo "$CREATE_SCRIPT_TEMPLATE" | sed $SED_SCRIPT > $CREATE_SCRIPT
00179 if [ $? -ne 0 ]; then
00180 echo "Failed to write $CREATE_SCRIPT"
00181 exit -1
00182 fi
00183 if [ ! -z $TELURI ]; then
00184 echo "$CREATE_TELURI_IMPU_TEMPLATE" | sed $SED_SCRIPT >> $CREATE_SCRIPT
00185 if [ $? -ne 0 ]; then
00186 echo "Failed to write $CREATE_SCRIPT"
00187 exit -1
00188 fi
00189 fi
00190 echo "Successfully wrote $CREATE_SCRIPT"
00191
00192 # Create SQL delete script
00193 echo "$DELETE_SCRIPT_TEMPLATE" | sed $SED_SCRIPT > $DELETE_SCRIPT
00194 if [ $? -ne 0 ]; then
00195 echo "Failed to write $DELETE_SCRIPT"
00196 exit -1
00197 fi
00198 if [ ! -z $TELURI ]; then
00199 echo "$DELETE_TELURI_IMPU_TEMPLATE" | sed $SED_SCRIPT >> $DELETE_SCRIPT
00200 if [ $? -ne 0 ]; then
00201 echo "Failed to write $DELETE_SCRIPT"
00202 exit -1
00203 fi
00204 fi
00205 echo "$DELETE_IMPI_TEMPLATE" | sed $SED_SCRIPT >> $DELETE_SCRIPT
00206 echo "Successfully wrote $DELETE_SCRIPT"
00207
00208 # Apply scripts directly?
00209 if [ $OPTION_ADD -eq 1 ]; then
00210 echo Apply $CREATE_SCRIPT as user $DBUSER...
00211 mysql -u $DBUSER -p < $CREATE_SCRIPT > /dev/null
00212 EXIT_CODE=$?
00213 SCRIPT=$CREATE_SCRIPT
00214 elif [ $OPTION_DELETE -eq 1 ]; then
00215 echo Apply $DELETE_SCRIPT as user $DBUSER...
00216 mysql -u $DBUSER -p < $DELETE_SCRIPT
00217 EXIT_CODE=$?
00218 SCRIPT=$DELETE_SCRIPT
00219 fi
00220
00221 # Evaluate exit code
00222 if [ ! -z "$SCRIPT" ]; then
00223 if [ $EXIT_CODE -ne 0 ]; then
00224 echo "ERROR: Failed to apply $SCRIPT"
00225 else
00226 echo "Successfully applied $SCRIPT"
00227 fi
00228 fi
00229
00230 # Clean-up?
00231 if [ $OPTION_CLEANUP -eq 1 ]; then
00232 rm $CREATE_SCRIPT $DELETE_SCRIPT
00233 echo "Deleted $CREATE_SCRIPT $DELETE_SCRIPT"
00234 fi
00235
00236
00237 exit $EXIT_CODE