예제 #1
0
    def GetSubscriberInfo(self, imsi):
        subscriber_details = {}
  
        try:
            mydoc = self.QueryDB(imsi)
        except:
            DBLogger.debug("Failed to pull subscriber info")
            raise ValueError("Failed to pull subscriber details for IMSI " + str(imsi) + " from MongoDB")

        #If query was completed Successfully extract data
        for x in mydoc:
            DBLogger.debug("Got result from MongoDB")
            DBLogger.debug(x)
            subscriber_details['K'] = x['security']['k'].replace(' ', '')
            try:
                subscriber_details['OP'] = x['security']['op'].replace(' ', '')
                DBLogger.debug("Database has OP stored - Converting to OPc")
                ##Convert to OPc
                subscriber_details['OPc'] = S6a_crypt.generate_opc(subscriber_details['K'], subscriber_details['OP'])
                #Remove OP reference from dict
                subscriber_details.pop('OP', None)
            except:
                subscriber_details['OPc'] = x['security']['opc'].replace(' ', '')
            subscriber_details['AMF'] = x['security']['amf'].replace(' ', '')
            try:
                subscriber_details['RAND'] = x['security']['rand'].replace(' ', '')
                subscriber_details['SQN'] = int(x['security']['sqn'])
            except:
                DBLogger.debug("Subscriber " + str() + " has not attached before - Generating new SQN and RAND")
                subscriber_details['SQN'] = 1
                subscriber_details['RAND'] = ''
            apn_list = ''
            for keys in x['slice'][0]['session']:
                apn_list += keys['name'] + ";"
            subscriber_details['APN_list'] = apn_list[:-1]      #Remove last semicolon
            DBLogger.debug("APN list is: " + str(subscriber_details['APN_list']))
            subscriber_details['pdn'] = x['slice'][0]['session']
            i = 0
            while i < len(subscriber_details['pdn']):
                #Rename from "name" to "apn"
                subscriber_details['pdn'][i]['apn'] = subscriber_details['pdn'][i]['name']
                #Store QCI data
                subscriber_details['pdn'][i]['qos']['qci'] = subscriber_details['pdn'][i]['qos']['index']
                #Map static P-GW Address
                if 'smf' in subscriber_details['pdn'][i]:
                    subscriber_details['pdn'][i]['ue'] = {}
                    subscriber_details['pdn'][i]['ue']['ip'] = subscriber_details['pdn'][i]['smf']['addr']
                i += 1
            DBLogger.debug(subscriber_details)
            return subscriber_details
        
        #if no results returned raise error
        raise ValueError("Mongodb has no matching subscriber details for IMSI " + str(imsi) + " from MongoDB")
예제 #2
0
    def GetSubscriberInfo(self, imsi):
        subscriber_details = {}

        try:
            mydoc = self.QueryDB(imsi)
        except:
            DBLogger.debug("Failed to pull subscriber info")
            raise ValueError("Failed to pull subscriber details for IMSI " +
                             str(imsi) + " from MongoDB")

        #If query was completed Successfully extract data
        for x in mydoc:
            DBLogger.debug("Got result from MongoDB")
            subscriber_details['K'] = x['security']['k'].replace(' ', '')
            try:
                subscriber_details['OP'] = x['security']['op'].replace(' ', '')
                DBLogger.debug("Database has OP stored - Converting to OPc")
                ##Convert to OPc
                subscriber_details['OPc'] = S6a_crypt.generate_opc(
                    subscriber_details['K'], subscriber_details['OP'])
                #Remove OP reference from dict
                subscriber_details.pop('OP', None)
            except:
                subscriber_details['OPc'] = x['security']['opc'].replace(
                    ' ', '')
            subscriber_details['AMF'] = x['security']['amf'].replace(' ', '')
            try:
                subscriber_details['RAND'] = x['security']['rand'].replace(
                    ' ', '')
                subscriber_details['SQN'] = int(x['security']['sqn'])
            except:
                DBLogger.debug(
                    "Subscriber " + str() +
                    " has not attached before - Generating new SQN and RAND")
                subscriber_details['SQN'] = 1
                subscriber_details['RAND'] = ''
            apn_list = ''
            for keys in x['pdn']:
                apn_list += keys['apn'] + ";"
            subscriber_details[
                'APN_list'] = apn_list[:-1]  #Remove last semicolon
            subscriber_details['pdn'] = x['pdn']
            DBLogger.debug(subscriber_details)
            return subscriber_details

        #if no results returned raise error
        raise ValueError(
            "Mongodb has no matching subscriber details for IMSI " +
            str(imsi) + " from MongoDB")
예제 #3
0
    def GetSubscriberInfo(self, imsi):
        with self._lock:
            try:
                DBLogger.debug("Getting subscriber info from MSSQL for IMSI " + str(imsi))
                subscriber_details = {}
                sql = "hss_imsi_known_check @imsi=" + str(imsi)
                DBLogger.debug(sql)
                self.conn.execute_query(sql)
                DBLogger.debug("Ran hss_imsi_known_check OK - Checking results")
                DBLogger.debug("Parsing results to var")
                result = [ row for row in self.conn ]
                DBLogger.debug("Result total is " + str(result))
                DBLogger.debug("Getting first entry in result")
                result = result[0]
                DBLogger.debug("printing final result:")
                DBLogger.debug(str(result))
            except Exception as e:
                DBLogger.error("failed to run " + str(sql))
                DBLogger.error(e)
                logtool.RedisIncrimenter('AIR_hss_imsi_known_check_SQL_Fail')
                raise Exception("Failed to query MSSQL server with query: " + str(sql))

            try:
                #known_imsi: IMSI attached with sim returns 1 else returns 0
                if str(result['known_imsi']) != '1':
                    logtool.RedisIncrimenter('AIR_hss_imsi_known_check_IMSI_unattached_w_SIM')
                    raise ValueError("MSSQL reports IMSI " + str(imsi) + " not attached with SIM")

                #subscriber_status: -1 –Blocked or 0-Active
                if str(result['subscriber_status']) != '0':
                    logtool.RedisIncrimenter('AIR_hss_imsi_known_check_IMSI_Blocked')
                    raise ValueError("MSSQL reports Subscriber Blocked for IMSI " + str(imsi))

                apn_id = result['apn_configuration']


                DBLogger.debug("Running hss_get_subscriber_data_v2 for imsi " + str(imsi))
                sql = 'hss_get_subscriber_data_v2 @imsi="' + str(imsi) + '";'
                DBLogger.debug("SQL: " + str(sql))
                self.conn.execute_query(sql)
                result = [ row for row in self.conn ][0]

                DBLogger.debug("\nResult of hss_get_subscriber_data_v2_v2: " + str(result))
                #subscriber_status: -1 –Blocked or 0-Active (Again)
                if str(result['subscriber_status']) != '0':
                    logtool.RedisIncrimenter('AIR_hss_get_subscriber_data_v2_v2_IMSI_Blocked')
                    raise ValueError("MSSQL reports Subscriber Blocked for IMSI " + str(imsi))
                
                #Get data output and put it into structure PyHSS uses
                subscriber_details['RAT_freq_priorityID'] = result['RAT_freq_priorityID']
                subscriber_details['APN_OI_replacement'] = result['APN_OI_replacement']
                subscriber_details['3gpp_charging_ch'] = result['_3gpp_charging_ch']
                subscriber_details['ue_ambr_ul'] = result['MAX_REQUESTED_BANDWIDTH_UL']
                subscriber_details['ue_ambr_dl'] = result['MAX_REQUESTED_BANDWIDTH_DL']
                subscriber_details['K'] = result['ki']
                subscriber_details['SQN'] = result['seqno']
                subscriber_details['RAT_freq_priorityID'] = result['RAT_freq_priorityID']
                subscriber_details['3gpp-charging-characteristics'] = result['_3gpp_charging_ch']
                
                #Harcoding AMF as it is the same for all SIMs and not returned by DB
                subscriber_details['AMF'] = '8000'

                #Set dummy RAND value (No need to store it)
                subscriber_details['RAND'] = ""

                #Format MSISDN
                subscriber_details['msisdn'] = str(result['region_subscriber_zone_code']) + str(result['msisdn'])
                subscriber_details['msisdn'] = subscriber_details['msisdn'].split(';')[-1]

                #Convert OP to OPc
                subscriber_details['OP'] = result['op_key']
                DBLogger.debug("Generating OPc with input K: " + str(subscriber_details['K']) + " and OP: " + str(subscriber_details['OP']))
                subscriber_details['OPc'] = S6a_crypt.generate_opc(subscriber_details['K'], subscriber_details['OP'])
                subscriber_details.pop('OP', None)
                DBLogger.debug("Generated OPc " + str(subscriber_details['OPc']))


                DBLogger.debug("Getting APN info")
                sql = 'hss_get_apn_info @apn_profileId=' + str(apn_id)
                DBLogger.debug(sql)
                self.conn.execute_query(sql)
                DBLogger.debug("Ran query")
                subscriber_details['pdn'] = []
                DBLogger.debug("Parsing results to var")
                result = [ row for row in self.conn ][0]
                DBLogger.debug("Got results")
                DBLogger.debug("Results are: " + str(result))
                apn = {'apn': str(result['Service_Selection']),\
                        'pcc_rule': [], 'qos': {'qci': int(result['QOS_CLASS_IDENTIFIER']), \
                        'arp': {'priority_level': int(result['QOS_PRIORITY_LEVEL']), 'pre_emption_vulnerability': int(result['QOS_PRE_EMP_VULNERABILITY']), 'pre_emption_capability': int(result['QOS_PRE_EMP_CAPABILITY'])}},\
                        'ambr' : {'apn_ambr_ul' : int(result['MAX_REQUESTED_BANDWIDTH_UL']), 'apn_ambr_Dl' : int(result['MAX_REQUESTED_BANDWIDTH_DL'])},
                        'PDN_GW_Allocation_Type' : int(result['PDN_GW_Allocation_Type']),
                        'VPLMN_Dynamic_Address_Allowed' : int(result['VPLMN_Dynamic_Address_Allowed']),
                        'type': 2, 'MIP6-Agent-Info' : {'MIP6_DESTINATION_HOST' : result['MIP6_DESTINATION_HOST'], 'MIP6_DESTINATION_REALM' : result['MIP6_DESTINATION_REALM']}}
                subscriber_details['pdn'].append(apn)

                DBLogger.debug("Final subscriber data for IMSI " + str(imsi) + " is: " + str(subscriber_details))
                return subscriber_details
            except Exception as e:
                logtool.RedisIncrimenter('AIR_general')
                DBLogger.error("General MSSQL Error")
                DBLogger.error(e)
                raise ValueError("MSSQL failed to return valid data for IMSI " + str(imsi))