def convert_uri_from_uri(dst, src): cur = src.cursor() if common.verbose: print "Converting source uri table into destination uri table" try: cur.execute("select username, domain, uri_user from uri") except MySQLdb.ProgrammingError: print "Error while querying source uri table, skipping" return uris = cur.fetchall() if cur.rowcount == 0: if common.verbose: print "Empty source uri table, nothing to convert" return for uri in uris: domain = extract_domain(uri[0], uri[1]) try: did = get_did_by_domain(dst, domain) except DidNotFoundException: did = common.DEFAULT_DID try: uid = get_uid_by_uri(dst, uri[0], domain, did) dst.cursor().execute("insert into uri (uid, did, username, flags) values" "(%s, %s, %s, %s)", (uid, did, uri[2], common.DB_LOAD_SER | common.DB_IS_FROM | common.DB_IS_TO | common.DB_FOR_SERWEB)) except UidNotFoundException: print "ERROR: Could not find UID for '%s@%s' from uri table" % (uri[0], domain) except MySQLdb.IntegrityError: print "Conflicting row found in uri table" print "Make sure that the table is empty and re-run the script" sys.exit(1)
def convert_uri_from_credentials(dst, src): cur = dst.cursor() if common.verbose: print "Converting usernames and realms from credentials table into URIs in uri table" try: cur.execute("select uid, auth_username, realm from credentials") except MySQLdb.ProgrammingError: print "Error while querying credentials table, skipping" return if cur.rowcount == 0: if common.verbose: print "credentials table is empty, nothing to convert" return users = cur.fetchall() for u in users: try: did = get_did_by_domain(dst, u[2]) dst.cursor().execute("insert into uri (uid, did, username, flags) values" "(%s, %s, %s, %s)", (u[0], did, u[1], common.DB_LOAD_SER | common.DB_IS_FROM | common.DB_IS_TO | common.DB_CANON | common.DB_FOR_SERWEB)) except DidNotFoundException: dst.cursor().execute("insert into uri (uid, did, username, flags) values" "(%s, %s, %s, %s)", (u[0], common.DEFAULT_DOMAIN, u[1], common.DB_LOAD_SER | common.DB_IS_FROM | common.DB_IS_TO | common.DB_CANON | common.DB_FOR_SERWEB)) except MySQLdb.IntegrityError: print "Confliting row found in target uri table" print "Make sure that the target database is empty and rerun the script" sys.exit(1)
def convert_uri_from_aliases(dst, src): cur = src.cursor() if common.verbose: print "Converting usernames and domains and contacts from aliases table into URIs in uri table" try: cur.execute("select username, domain, contact from aliases") except MySQLdb.ProgrammingError: print "Error while querying aliases table, skipping" return if cur.rowcount == 0: if common.verbose: print "Aliases table is empty, nothing to convert" return aliases = cur.fetchall() for alias in aliases: domain = extract_domain(alias[0], alias[1]) (scheme, username, password, host, port, params) = parse(alias[2]) if scheme is None: print "Malformed URI found in aliases table: '%s', skipping" % alias[2] continue try: did = get_did_by_domain(dst, domain) except DidNotFoundException: did = common.DEFAULT_DID try: uid = get_uid_by_uri(dst, username, host) dst.cursor().execute( "insert into uri (uid, did, username, flags) values" "(%s, %s, %s, %s)", (uid, did, alias[0], common.DB_LOAD_SER | common.DB_IS_FROM | common.DB_IS_TO | common.DB_FOR_SERWEB), ) except UidNotFoundException: print "ERROR: Could not find UID for alias target '%s@%s', skipping" % (username, host) except MySQdb.IntegrityError: print "Conflicting row found in uri table" print "Make sure that the table is empty and re-run the script" sys.exit(1)
def get_uid_by_uri(db, username, domain, did = None): # No username or domain given, that's bad, raise an exception if username is None or domain is None: raise UidNotFoundException(username, domain) if username.startswith("sip:"): username = username[4:] if username.startswith("sips:"): username = username[5:] if did is None: try: did = get_did_by_domain(db, domain) except DidNotFoundException: did = common.DEFAULT_DID cur = db.cursor(); cur.execute("select uid from uri where username=%s and did=%s", (username, did)) try: return cur.fetchone()[0] except: # Entry not found in URI table, try digest credentials return get_uid_by_credentials(db, username, domain)
def convert_acc(table, dst, src): if common.verbose: print "Converting %s table" % table start = 0 count = 1000 num = 1 while True: cur = src.cursor() try: cur.execute("select sip_from, sip_to, sip_status, sip_method, i_uri, o_uri, " "from_uri, to_uri, sip_callid, username, fromtag, totag, time, " "timestamp, domain from %s limit %s, %s" % (table, start, count)) except MySQLdb.ProgrammingError: print "Error while querying %s table, skipping" % table if cur.rowcount == 0: return acc = cur.fetchone() while acc: cols = [] vals = [] # time cols.append("request_timestamp") vals.append(acc[13]) cols.append("response_timestamp") vals.append(acc[13]) # sip_from cols.append("sip_from") vals.append(acc[0]) # from_uri did = None cols.append("from_uri") vals.append(acc[6]) # Parse the URI in from_uri column (scheme, user, passwd, domain, port, params) = parse(acc[6]) # Find out did try: did = get_did_by_domain(dst, domain) cols.append("from_did") vals.append(did) except DidNotFoundException: pass # Try to find out UID if we have did if did is not None and user is not None: try: uid = get_uid_by_uri(dst, user, domain, did) cols.append("from_uid") vals.append(uid) except UidNotFoundException: pass # sip_to cols.append("sip_to") vals.append(acc[1]) # sip_status cols.append("sip_status") vals.append(acc[2]) # sip_method cols.append("sip_method") vals.append(acc[3]) # i_uri did = None cols.append("in_ruri") vals.append(acc[4]) # Parse the URI in in_ruri column (scheme, user, passwd, domain, port, params) = parse(acc[4]) # Try to find DID try: did = get_did_by_domain(dst, domain) except DidNotFoundException: # We could not find DID, let's try to_uri instead of i_uri (scheme, user, passwd, domain, port, params) = parse(acc[7]) try: did = get_did_by_domain(dst, domain) except DidNotFoundException: pass # If we have DID try to find UID if did is not None: cols.append("to_did") vals.append(did) if user is not None: try: uid = get_uid_by_uri(dst, user, domain, did) cols.append("to_uid") vals.append(uid) except UidNotFoundException: pass # o_uri cols.append("out_ruri") vals.append(acc[5]) # to_uri cols.append("to_uri") vals.append(acc[7]) # sip_callid cols.append("sip_callid") vals.append(acc[8]) # username cols.append("digest_username") vals.append(acc[9]) # fromtag cols.append("from_tag") vals.append(acc[10]) # totag cols.append("to_tag") vals.append(acc[11]) # domain cols.append("digest_realm") vals.append(acc[14]) query = "insert into %s (" % table query += cols[0] for c in cols[1:]: query += ", " query += c query += ") values (" query += "%s" for c in cols[1:]: query += ", %s" query += ")" try: dst.cursor().execute(query, tuple(vals)) except: print "Error while executing:" print vals sys.exit(1) num = num + 1 acc = cur.fetchone() start += count