1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
| #!/bin/bash # # $Id: add-imscore-user_newdb.sh 627 2021-04-21 15:38:55Z vingarzan $ # # add-imscore-user_newdb.sh Version='0.6' # Released: 2021-04-21 # Author: Sven Bornemann -at- materna de # # History: # 0.6 (04/21/21) # * Change "CREATE_SCRIPT_TEMPLATE" sql # * Add database password # 0.51 (04/08/08) # * added tel-URI support incl. implicit-set # 0.4 (06/21/07) # * upgraded to the new db structures # 0.3 (03/09/07): # * sip2ims transactions are commented out. # 0.2 (02/06/07): # * Changed parameter handling (getopt). # * Allow direct mysql import. # * Remove temporary password file after usage. # 0.1 (02/02/07): # * Initial version # # Script for generating two SQL scripts for creating/deleting IMS Core users # in the HSS and the SIP2IMS gateway tables. # # Usage for add-imscore-user.sh: See Usage() procedure below # # Example for creating user 'brooke' with password 'brooke' for realm # 'open-ims.test': # # # ./add-imscore-user.sh -u brooke -a # Successfully wrote add-user-brooke.sql # Successfully wrote delete-user-brooke.sql # Apply add-user-brooke.sql... # Enter password: # Successfully applied add-user-brooke.sql # # After applying the add script, you should be able to register with IMS Core # with SIP clients (e.g. as 'brooke') via SIP2IMS. Use delete script or -d # option for removing the user from IMS Core database tables. # # Known limits: # * IMS Core installation in /opt/OpenIMSCore required. # * Password is limited to 16 characters. #
Usage() { echo -e "\e[1;31m ERROR: Invalid parameters\e[0m" echo " -h <help> Display this help and exit" echo "Usage: add-imscore-user.sh -u <user> [-t <tel-URI>] [-r <realm> -p <password>] [-a|-d]" echo " -u <user>: The username (e.g. 'brooke')" echo " -t <tel-URI>: The tel-URI (e.g. 'tel:+49123456789'). If used with -a, -d requires" echo " this parameter too!" echo -e " -r <realm>: The realm. Default is \033[1;31m 'open-ims.test' \033[0m" echo " -i <impi>: The Private Identity (e.g. 'brooke@open-ims.test'). The -u option overrides this." echo " -b <impu>: The Public Identity (e.g. 'sip:brooke@open-ims.test') The -u option overrides this." echo " -p <password>: The password. Default is value of -u option" echo " -a: Automatically apply created add script" echo " -d: Automatically apply created delete script" echo " -c: Delete the scripts afterwards (by default they are not deleted)" echo " -v: Version information)" exit -1 }
OPTION_ADD=0 OPTION_DELETE=0 OPTION_CLEANUP=0 SCRIPT= EXIT_CODE=0 #GRANT SELECT, INSERT, UPDATE, DELETE ON icscf.* TO root@localhost #GRANT SELECT, INSERT, UPDATE, DELETE ON hss_db.* TO root@localhost DBUSER=root DBPASS='' #数据库密码,可保持为空,手动输入密码
while getopts u:r:i:b:p:o:c?:adt:? option; do case $option in u) IMSUSER=$OPTARG;; r) REALM=$OPTARG;; i) IMPI=$OPTARG;; b) IMPU=$OPTARG;; t) TELURI=$OPTARG;; p) PASSWORD=$OPTARG;; a) OPTION_ADD=1;; d) OPTION_DELETE=1;; c) OPTION_CLEANUP=1;; esac done
[ -z "$REALM" ] && REALM=test.com if [ -z "$IMSUSER" ]; then IMSUSER=${IMPI%%@*} else IMPI="$IMSUSER@$REALM" IMPU="sip:$IMSUSER@$REALM" fi [ -z "$PASSWORD" ] && PASSWORD=$IMSUSER [ -z "$IMSUSER" ] && Usage
# Some checks [ $OPTION_ADD -eq 1 ] && [ $OPTION_DELETE -eq 1 ] && Usage;
#KEY=`/opt/OpenIMSCore/ser_ims/utils/gen_ha1/gen_ha1 $IMSUSER@$REALM $REALM $PASSWORD`
CREATE_SCRIPT="add-user-$IMSUSER.sql" DELETE_SCRIPT="delete-user-$IMSUSER.sql" SED_SCRIPT="s/<USER>/$IMSUSER/g" PASSWORD_FILE=~temp~password~
echo -n $PASSWORD > $PASSWORD_FILE ENCODED_PASSWORD=`hexdump -C < $PASSWORD_FILE|cut -b 10-60|sed 's/ //g'|cut -b 1-32`00000000000000000000000000000000 ENCODED_PASSWORD=`echo $ENCODED_PASSWORD|cut -b 1-32` rm $PASSWORD_FILE
CREATE_SCRIPT_TEMPLATE=" insert into hss_db.imsu( name, scscf_name, diameter_name, id_capabilities_set, id_preferred_scscf_set) values ('<USER>', 'sip:scscf.$REALM:6060', 'scscf.$REALM', 1, 1);
-- add Private Identity
-- add <USER>@$REALM
insert into hss_db.impi( id_imsu, identity, k, auth_scheme, default_auth_scheme, amf, op) values((select id from hss_db.imsu where hss_db.imsu.name='<USER>'), '$IMPI', '$PASSWORD', 127, 1, '\0\0', '\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0');
-- add Public SIP Identity insert into hss_db.impu(identity,id_sp,id_charging_info) values ('$IMPU', (select id from hss_db.sp order by id limit 1),1); update hss_db.impu set id_implicit_set=id where hss_db.impu.identity='$IMPU';
-- add Public Identity to Private Identity 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'));
-- add roaming network 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')); "
CREATE_TELURI_IMPU_TEMPLATE=" -- add Public tel-URI Identity insert into hss_db.impu(identity,id_sp,id_charging_info) values ('$TELURI', (select id from hss_db.sp order by id limit 1),1); select @id:=id from hss_db.impu where hss_db.impu.identity='$IMPU'; update hss_db.impu set id_implicit_set=@id where hss_db.impu.identity='$TELURI';
-- add Public Identity to Private Identity 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'));
-- add roaming network 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')); "
DELETE_SCRIPT_TEMPLATE=" delete from hss_db.impu_visited_network where id_impu = (select id from hss_db.impu where hss_db.impu.identity='$IMPU'); delete from hss_db.impi_impu where id_impi = (select id from hss_db.impi where hss_db.impi.identity='$IMPI'); delete from hss_db.impu where identity = '$IMPU'; delete from hss_db.imsu where name = '<USER>'; "
DELETE_TELURI_IMPU_TEMPLATE=" delete from hss_db.impu_visited_network where id_impu = (select id from hss_db.impu where hss_db.impu.identity='$TELURI'); delete from hss_db.impi_impu where id_impi = (select id from hss_db.impi where hss_db.impi.identity='$IMPI'); delete from hss_db.impu where identity = '$TELURI'; "
DELETE_IMPI_TEMPLATE=" delete from hss_db.impi where identity = '$IMPI'; "
# Create SQL add script echo "$CREATE_SCRIPT_TEMPLATE" | sed $SED_SCRIPT > $CREATE_SCRIPT if [ $? -ne 0 ]; then echo -e "\e[1;31m Failed to write $CREATE_SCRIPT \e[0m" exit -1 fi if [ ! -z $TELURI ]; then echo "$CREATE_TELURI_IMPU_TEMPLATE" | sed $SED_SCRIPT >> $CREATE_SCRIPT if [ $? -ne 0 ]; then echo -e "\e[1;31m Failed to write $CREATE_SCRIPT \e[0m" exit -1 fi fi echo -e "\033[32m Successfully wrote $CREATE_SCRIPT \033[0m"
# Create SQL delete script echo "$DELETE_SCRIPT_TEMPLATE" | sed $SED_SCRIPT > $DELETE_SCRIPT if [ $? -ne 0 ]; then echo -e "\e[1;31m Failed to write $DELETE_SCRIPT \e[0m" exit -1 fi if [ ! -z $TELURI ]; then echo "$DELETE_TELURI_IMPU_TEMPLATE" | sed $SED_SCRIPT >> $DELETE_SCRIPT if [ $? -ne 0 ]; then echo -e "\e[1;31m Failed to write $DELETE_SCRIPT \e[0m" exit -1 fi fi echo "$DELETE_IMPI_TEMPLATE" | sed $SED_SCRIPT >> $DELETE_SCRIPT echo -e "\033[32m Successfully wrote $DELETE_SCRIPT \033[0m"
# Apply sql scripts directly if [ $OPTION_ADD -eq 1 ]; then echo Apply $CREATE_SCRIPT as user $DBUSER... #mysql -u $DBUSER -p < $CREATE_SCRIPT > /dev/null mysql -u $DBUSER -p$DBPASS < $CREATE_SCRIPT > /dev/null EXIT_CODE=$? SCRIPT=$CREATE_SCRIPT elif [ $OPTION_DELETE -eq 1 ]; then echo Apply $DELETE_SCRIPT as user $DBUSER... #mysql -u $DBUSER -p < $DELETE_SCRIPT mysql -u $DBUSER -p$DBPASS < $DELETE_SCRIPT EXIT_CODE=$? SCRIPT=$DELETE_SCRIPT fi
# Evaluate exit code if [ ! -z "$SCRIPT" ]; then if [ $EXIT_CODE -ne 0 ]; then echo -e "\033[1;31m ERROR: Failed to apply $SCRIPT \033[0m" else echo -e "\033[1;32m Successfully applied $SCRIPT \033[0m" fi fi
# Clean-up sql scripts if [ $OPTION_CLEANUP -eq 1 ]; then rm $CREATE_SCRIPT $DELETE_SCRIPT echo -e "\033[1;32m Deleted $CREATE_SCRIPT $DELETE_SCRIPT \033[0m" fi
exit $EXIT_CODE
|