Example #1
0
def convert_phonebook(dst, src):
    if common.verbose: print "Converting phonebook table"
    
    cur = src.cursor();

    try:
        cur.execute("select username, domain, fname, lname, sip_uri from phonebook")
    except MySQLdb.ProgrammingError:
        print "Error while querying phonebook table, skipping"
        return
        
    pbs = cur.fetchall()

    if common.verbose and cur.rowcount == 0:
        print "Source phonebook table is empty, skipping"
        return

    for pb in pbs:
        domain = extract_domain(pb[0], pb[1])
        try:
            uid = get_uid_by_uri(dst, pb[0], domain)

            dst.cursor().execute("insert into phonebook (uid, fname, lname, sip_uri) "
                                 "values (%s, %s, %s, %s)",
                                 (uid, pb[2], pb[3], pb[4]))
                
        except UidNotFoundException:
            print "ERROR: Cannot find UID for phonebook entry for '%s@%s'" % (pb[0], domain)

        except MySQLdb.IntegrityError:
            print "Conflicting row found in target phonebook table"
            print "Make sure that the target phonebook table is empty and re-run the script"
            sys.exit(1)
Example #2
0
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)
Example #3
0
def convert_location(dst, src):

    if common.verbose: print "Converting user location table entries"

    cur = src.cursor();

    try:
        cur.execute("select username, domain, contact, expires, q, callid, "
                    "cseq, flags, received, user_agent from location")
        contacts = cur.fetchall()
    except MySQLdb.ProgrammingError:
        print "Error while querying source location table, skipping"
        return

    if cur.rowcount == 0 and common.verbose:
        print "Source location table is empty, nothing to convert"
        return

    for contact in contacts:
        domain = extract_domain(contact[0], contact[1])
        try:
            uid = get_uid_by_uri(dst, contact[0], domain)
            dst.cursor().execute("insert into location (uid, contact, expires, q, callid, cseq, flags, received, user_agent) "
                                 "values (%s, %s, %s, %s, %s, %s, %s, %s, %s)",
                                 (uid, contact[2], contact[3], contact[4], contact[5], contact[6],
                                  contact[7], contact[8], contact[9]))
        except UidNotFoundException:
            print "ERROR: Could not find UID for location entry '%s@%s', skipping" % (contact[0], domain)
Example #4
0
def convert_cpl(dst, src):
    cur = src.cursor();

    if common.verbose: print "Converting cpl table"

    try:
        cur.execute("select user, cpl_xml, cpl_bin from cpl")
        cpls = cur.fetchall()
    except MySQLdb.ProgrammingError:
        print "Error while querying cpl table, skipping"
        return

    for cpl in cpls:
        domain = extract_domain(cpl[0])
        try:
            uid = get_uid_by_uri(dst, cpl[0], domain)
            try:
                dst.cursor().execute("insert into cpl (uid, cpl_xml, cpl_bin) values (%s, %s, %s)",
                                     (uid, cpl[1], cpl[2]))
            except MySQLdb.IntegrityViolation:
                print "Conflicting row found in target cpl table"
                print "Make sure that the target table is empty and re-run the script"
                sys.exit(1)
                
        except UidNotFoundException:
            print "ERROR: Could not find UID for cpl table user '%s'" % (cpl[0])
Example #5
0
def convert_msilo(dst, src):

    if common.verbose: print "Converting msilo table"
    
    cur = src.cursor();

    try:
        cur.execute("select username, domain, src_addr, dst_addr, r_uri, inc_time, exp_time, ctype, body from silo")
    except MySQLdb.ProgrammingError:
        print "Error while querying msilo table, skipping"
        return
    
    msgs = cur.fetchall()

    if common.verbose and cur.rowcount == 0:
        print "Source msilo table is empty, skipping"
        return

    for msg in msgs:
        domain = extract_domain(msg[0], msg[1])
        inc_time = strftime("%Y-%m-%d %H:%M:%S", gmtime(msg[5]))
        exp_time = strftime("%Y-%m-%d %H:%M:%S", gmtime(msg[6]))
        try:
            uid = get_uid_by_uri(dst, msg[0], domain)
            dst.cursor().execute("insert into silo (uid, ruri, from_hdr, to_hdr, inc_time, exp_time, ctype, body) "
                                 "values (%s, %s, %s, %s, %s, %s, %s, %s)",
                                 (uid, msg[4], msg[2], msg[3], inc_time, exp_time, msg[7], msg[8]))
            
        except UidNotFoundException:
            print "ERROR: Cannot find UID for stored message for '%s@%s'" % (msg[0], domain)

        except MySQLdb.IntegrityError:
            print "Conflicting row found in target msilo table"
            print "Make sure that the target msilo table is empty and re-run the script"
            sys.exit(1)
Example #6
0
def convert_grp(dst, src):

    if common.verbose:
        print "Converting grp table into user attributes"

    cur = src.cursor()

    try:
        cur.execute("select username, domain, grp from grp")
        groups = cur.fetchall()
    except MySQLdb.ProgrammingError:
        print "Error while querying grp table, skipping"
        return

    for group in groups:
        (username, domain, grp) = group
        domain = common.extract_domain(username, domain)
        try:
            uid = get_uid_by_uri(dst, username, domain)
            dst.cursor().execute(
                "insert into user_attrs (uid, name, type, value, flags) values" "(%s, %s, %s, %s, %s)",
                (uid, "acl", 2, grp, common.DB_LOAD_SER | common.DB_FOR_SERWEB),
            )
        except UidNotFoundException:
            print "ERROR: Cannot find UID for group entry for '%s@%s'" % (username, domain)

        except MySQLdb.IntegrityError:
            print "Conflicting row found in user_attrs table (%s, %s, %s, %s, %s)" % (
                uid,
                "acl",
                2,
                grp,
                common.DB_LOAD_SER | common.DB_FOR_SERWEB,
            )
            pass
Example #7
0
def convert_usr_preferences(dst, src):
    if common.verbose: print "Converting usr_preferences table into user attributes"
    
    cur = src.cursor();

    try:
        cur.execute("select username, domain, attribute, type, value from usr_preferences")
        ups = cur.fetchall()
    except MySQLdb.ProgrammingError:
        print "Error while querying usr_preferences table, skipping"
        return

    for up in ups:
        (username, domain, attribute, type, value) = up
        domain = extract_domain(username, domain)
        try:
            uid = get_uid_by_uri(dst, username, domain)
            dst.cursor().execute("insert into user_attrs (uid, name, type, value, flags) values "
                                 "(%s, %s, %s, %s, %)", (uid, attribute, type, value, common.DB_LOAD_SER | common.DB_FOR_SERWEB))
        except UidNotFoundException:
            print "ERROR: Cannot find UID user_preferences entry for '%s@%s'" % (username, domain)

        except MySQLdb.IntegrityError:
            print "Conflicting row found in usr_attrs table"
            sys.exit(1)
Example #8
0
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)
Example #9
0
def convert_admin_privileges(dst, src):
    cur = src.cursor();

    if common.verbose: print "Converting admin_privileges table"

    try:
        cur.execute("select username, domain, priv_name, priv_value from admin_privileges")
    except MySQLdb.ProgrammingError:
        print "Error while querying admin_privileges table, skipping"
        return

    privs = cur.fetchall()

    if common.verbose and cur.rowcount == 0:
        print "Source admin_privileges table is empty, skipping"
        return

    for priv in privs:
        (username, domain, priv_name, priv_value) = priv
        domain = common.extract_domain(username, domain)

        try:
            uid = get_uid_by_uri(dst, username, domain)
            if priv_name == "is_admin":
                dst.cursor().execute("insert into user_attrs (uid, name, type, value, flags) values "
                                     "(%s, %s, %s, %s, %s)", (uid, "sw_is_admin", 0, 1, common.DB_FOR_SERWEB))
            elif priv_name == "acl_control":
                dst.cursor().execute("insert into user_attrs (uid, name, value, type, flags) values "
                                     "(%s, %s, %s, %s, %s)", (uid, "sw_acl_control", priv_value, 2, common.DB_FOR_SERWEB))
            elif priv_name == "change_privileges":
                dst.cursor().execute("insert into user_attrs (uid, name, type, value, flags) values "
                                     "(%s, %s, %s, %s, %s)", (uid, "sw_is_hostmaster", 0, 1, common.DB_FOR_SERWEB))
            else:
                print "ERROR: Unsupported privilege '%s' in admin_privileges table, skipping" % priv_name
        except UidNotFoundException:
            print "ERROR: Cannot find UID for admin_privilege entry for '%s@%s'" % (username, domain)

        except MySQLdb.IntegrityError:
            print "Conflicting row found user_attrs table"
            sys.exit(1)
Example #10
0
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