]> git.ipfire.org Git - thirdparty/freeswitch.git/commitdiff
FS-3794 fix scope on sql stmts
authorAnthony Minessale <anthm@freeswitch.org>
Mon, 30 Jan 2012 17:43:43 +0000 (11:43 -0600)
committerAnthony Minessale <anthm@freeswitch.org>
Mon, 30 Jan 2012 17:43:43 +0000 (11:43 -0600)
src/mod/endpoints/mod_sofia/sofia_presence.c

index c8a16672397781f6f7f4b767cae6be3cc9750c2e..30d5ba7888fe534d2d081e68de82efb26c849a21 100644 (file)
@@ -461,7 +461,8 @@ static void actual_sofia_presence_mwi_event_handler(switch_event_t *event)
                        char buf[512] = "";
                        switch_console_callback_match_t *matches;
 
-                       sql = switch_mprintf("select profile_name from sip_registrations where sip_host='%s' or mwi_host='%s'", host, host);
+                       sql = switch_mprintf("select profile_name from sip_registrations where hostname='%q' and profile_name='%q' and (sip_host='%s' or mwi_host='%s')", 
+                                                                mod_sofia_globals.hostname, profile->name, host, host);
 
                        if (list_profiles_full(NULL, NULL, &matches, SWITCH_FALSE) == SWITCH_STATUS_SUCCESS) {
                                switch_console_callback_match_node_t *m;
@@ -519,14 +520,17 @@ static void actual_sofia_presence_mwi_event_handler(switch_event_t *event)
        if (for_everyone) {
                sql = switch_mprintf("select proto,sip_user,sip_host,sub_to_user,sub_to_host,event,contact,call_id,full_from,"
                                                         "full_via,expires,user_agent,accept,profile_name,network_ip"
-                                                        ",'%q',full_to,network_ip,network_port from sip_subscriptions where event='message-summary' "
-                                                        "and sub_to_user='%q' and (sub_to_host='%q' or presence_hosts like '%%%q%%')", stream.data, user, host, host);
+                                                        ",'%q',full_to,network_ip,network_port from sip_subscriptions "
+                                                        "where hostname='%q' and profile_name='%q' and event='message-summary' "
+                                                        "and sub_to_user='%q' and (sub_to_host='%q' or presence_hosts like '%%%q%%')", 
+                                                        stream.data, mod_sofia_globals.hostname, profile->name, user, host, host);
        } else if (sub_call_id) {
                sql = switch_mprintf("select proto,sip_user,sip_host,sub_to_user,sub_to_host,event,contact,call_id,full_from,"
                                                         "full_via,expires,user_agent,accept,profile_name,network_ip"
-                                                        ",'%q',full_to,network_ip,network_port from sip_subscriptions where event='message-summary' "
+                                                        ",'%q',full_to,network_ip,network_port from sip_subscriptions where "
+                                                        "hostname='%q' and profile_name='%q' and event='message-summary' "
                                                         "and sub_to_user='%q' and (sub_to_host='%q' or presence_hosts like '%%%q%%') and call_id='%q'",
-                                                        stream.data, user, host, host, sub_call_id);
+                                                        stream.data, mod_sofia_globals.hostname, profile->name, user, host, host, sub_call_id);
        }
 
 
@@ -539,10 +543,12 @@ static void actual_sofia_presence_mwi_event_handler(switch_event_t *event)
 
        if (for_everyone) {
                sql = switch_mprintf("select sip_user,sip_host,contact,profile_name,network_ip,'%q' "
-                                                        "from sip_registrations where mwi_user='%q' and mwi_host='%q'", stream.data, user, host);
+                                                        "from sip_registrations where hostname='%q' and profile_name='%q' and mwi_user='%q' and mwi_host='%q'", 
+                                                        stream.data, mod_sofia_globals.hostname, profile->name, user, host);
        } else if (call_id) {
                sql = switch_mprintf("select sip_user,sip_host,contact,profile_name,network_ip,'%q' "
-                                                        "from sip_registrations where mwi_user='%q' and mwi_host='%q' and call_id='%q'", stream.data, user, host, call_id);
+                                                        "from sip_registrations where hostname='%q' and profile_name='%q' and mwi_user='%q' and mwi_host='%q' and call_id='%q'", 
+                                                        mod_sofia_globals.hostname, profile->name, stream.data, user, host, call_id);
        }
 
        if (sql) {
@@ -606,8 +612,9 @@ static void do_normal_probe(switch_event_t *event)
 
        if (probe_euser && probe_host && (profile = sofia_glue_find_profile(probe_host))) {
                sql = switch_mprintf("select status,rpid,presence_id from sip_dialogs "
-                                                        "where ((sip_from_user='%q' and sip_from_host='%q') or presence_id='%q@%q') order by rcd desc", 
-                                                        probe_euser, probe_host, probe_euser, probe_host);
+                                                        "where hostname='%q' and profile_name='%q' and "
+                                                        "((sip_from_user='%q' and sip_from_host='%q') or presence_id='%q@%q') order by rcd desc", 
+                                                        mod_sofia_globals.hostname, profile->name, probe_euser, probe_host, probe_euser, probe_host);
                
                
                sofia_glue_execute_sql_callback(profile, profile->ireg_mutex, sql, sofia_presence_dialog_callback, &dh);
@@ -635,21 +642,22 @@ static void do_normal_probe(switch_event_t *event)
                                                         "from sip_registrations "
 
                                                         "left join sip_dialogs on "
+                                                        "sip_dialogs.hostname = sip_registration.hostname and sip_dialogs.profile_name = sip_registration.profile_name and ("
                                                         "sip_dialogs.presence_id = sip_registrations.sip_user %q '@' %q sip_registrations.sub_host "
                                                         "or (sip_dialogs.sip_from_user = sip_registrations.sip_user "
-                                                        "and sip_dialogs.sip_from_host = sip_registrations.sip_host) "
+                                                        "and sip_dialogs.sip_from_host = sip_registrations.sip_host)) "
  
                                                         "left join sip_presence on "
+                                                        "sip_presence.hostname=sip_registrations.hostname and "
                                                         "(sip_registrations.sip_user=sip_presence.sip_user and sip_registrations.orig_server_host=sip_presence.sip_host and "
                                                         "sip_registrations.profile_name=sip_presence.profile_name) "
-                                                        "where sip_dialogs.call_info_state != 'seized' and "
-                                                        "(sip_registrations.profile_name='%q' or sip_registrations.presence_hosts like '%%%q%%') "
+                                                        "where sip_registrations.hostname='%q' and sip_registrations.profile_name='%q' and sip_dialogs.call_info_state != 'seized' "
                                                         "and sip_dialogs.presence_id='%q@%q' or (sip_registrations.sip_user='%q' and "
                                                         "(sip_registrations.orig_server_host='%q' or sip_registrations.sub_host='%q' "
                                                         "))",
                                                         dh.status, dh.rpid, switch_str_nil(sub_call_id),
                                                         switch_sql_concat(), switch_sql_concat(),
-                                                        profile->name, probe_host, probe_euser, probe_host,  probe_euser, probe_host, probe_host);
+                                                        mod_sofia_globals.hostname, profile->name, probe_euser, probe_host,  probe_euser, probe_host, probe_host);
                
                
 
@@ -678,9 +686,9 @@ static void do_normal_probe(switch_event_t *event)
                                                                                 
                                                                 "from sip_dialogs "
                                                                                 
-                                                                "where (presence_id='%q@%q' or "
+                                                                "where hostname='%q' and profile_name='%q' and (presence_id='%q@%q' or "
                                                                 "(sip_from_user='%q' and (sip_from_host='%q' or sip_to_host='%q')))",
-                                                                                
+                                                                mod_sofia_globals.hostname, profile->name,
                                                                 dh.status, dh.rpid, probe_euser, probe_host,  probe_euser, probe_host, probe_host);
 
                        if (mod_sofia_globals.debug_presence > 0) {
@@ -755,16 +763,20 @@ static void do_dialog_probe(switch_event_t *event)
                                                         "sip_dialogs.contact, sip_dialogs.contact_user, sip_dialogs.contact_host, "
                                                         "sip_dialogs.sip_to_tag, sip_dialogs.sip_from_tag, sip_subscriptions.orig_proto "
                                                         "from sip_dialogs "
-                                                        "left join sip_subscriptions on sip_subscriptions.call_id='%q' "
-                                                        "left join sip_registrations on "
+                                                        "left join sip_subscriptions on sip_subscriptions.hostname=sip_dialogs.hostname and "
+                                                        "sip_subscriptions.profile_name=sip_dialogs.profile_name and "
+                                                        "sip_subscriptions.call_id='%q' "
+                                                        "left join sip_registrations on sip_registrations.hostname=sip_dialogs.hostname and "
+                                                        "sip_registrations.profile_name=sip_dialogs.profile_name and "
                                                         "(sip_dialogs.sip_from_user = sip_registrations.sip_user "
                                                         "and (sip_dialogs.sip_from_host = sip_registrations.orig_server_host or "
                                                         "sip_dialogs.sip_from_host = sip_registrations.sip_host) ) "
-                                                        "where sip_dialogs.call_info_state != 'seized' and sip_dialogs.presence_id='%q@%q' or (sip_registrations.sip_user='%q' and "
+                                                        "where sip_dialogs.hostname='%q' and sip_dialogs.profile_name='%q' and "
+                                                        "sip_dialogs.call_info_state != 'seized' and sip_dialogs.presence_id='%q@%q' or (sip_registrations.sip_user='%q' and "
                                                         "(sip_registrations.orig_server_host='%q' or sip_registrations.sub_host='%q' "
                                                         "or sip_registrations.presence_hosts like '%%%q%%'))",
                                                         probe_euser, probe_host,
-                                                        sub_call_id,
+                                                        sub_call_id, mod_sofia_globals.hostname, profile->name,
                                                         probe_euser, probe_host,
                                                         probe_euser, probe_host, probe_host, probe_host);
                switch_assert(sql);
@@ -790,8 +802,8 @@ static void do_dialog_probe(switch_event_t *event)
 
 
                sql = switch_mprintf("update sip_subscriptions set version=version+1 "
-                                                        "where hostname='%q' and sub_to_user='%q' and sub_to_host='%q' and call_id='%q'",
-                                                        mod_sofia_globals.hostname, probe_euser, probe_host, sub_call_id);
+                                                        "where hostname='%q' and profile_name='%q' and sub_to_user='%q' and sub_to_host='%q' and call_id='%q'",
+                                                        mod_sofia_globals.hostname, profile->name, probe_euser, probe_host, sub_call_id);
 
                if (mod_sofia_globals.debug_presence > 1) {
                        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "%s DUMP DIALOG_PROBE set version sql:\n%s\n", profile->name, sql);
@@ -805,8 +817,8 @@ static void do_dialog_probe(switch_event_t *event)
                sql = switch_mprintf("select call_id,expires,sub_to_user,sub_to_host,event,version, "
                                                         "'full',full_to,full_from,contact,network_ip,network_port "
                                                         "from sip_subscriptions "
-                                                        "where hostname='%q' and sub_to_user='%q' and sub_to_host='%q' and call_id='%q'",
-                                                        mod_sofia_globals.hostname, probe_euser, probe_host, sub_call_id);
+                                                        "where hostname='%q' and profile_name='%q' and sub_to_user='%q' and sub_to_host='%q' and call_id='%q'",
+                                                        mod_sofia_globals.hostname, profile->name, probe_euser, probe_host, sub_call_id);
 
                if (mod_sofia_globals.debug_presence > 1) {
                        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "%s DUMP DIALOG_PROBE subscription sql:\n%s\n", profile->name, sql);
@@ -880,8 +892,9 @@ static void actual_sofia_presence_event_handler(switch_event_t *event)
 
                if (from) {
 
-                       sql = switch_mprintf("update sip_subscriptions set version=version+1 where "
-                                                                "sip_subscriptions.event='presence' and sip_subscriptions.full_from like '%%%q%%'", from);
+                       sql = switch_mprintf("update sip_subscriptions set version=version+1 where hostname='%q' and profile_name='%q' and "
+                                                                "sip_subscriptions.event='presence' and sip_subscriptions.full_from like '%%%q%%'",
+                                                                mod_sofia_globals.hostname, profile->name, from);
                        
                        if (mod_sofia_globals.debug_presence > 1) {
                                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "PRES SQL %s\n", sql);
@@ -899,14 +912,14 @@ static void actual_sofia_presence_event_handler(switch_event_t *event)
                                                                 " sip_subscriptions.full_to,sip_subscriptions.network_ip,sip_subscriptions.network_port "
                                                                 "from sip_subscriptions left join sip_presence on "
                                                                 "(sip_subscriptions.sub_to_user=sip_presence.sip_user and sip_subscriptions.sub_to_host=sip_presence.sip_host and "
-                                                                "sip_subscriptions.profile_name=sip_presence.profile_name) "
-                                                                "where "
+                                                                "sip_subscriptions.profile_name=sip_presence.profile_name and sip_presence.profile_name=sip_subscriptions.profile_name) "
+                                                                "where sip_subscriptions.hostname='%q' and sip_subscriptions.profile_name='%q' and "
                                                                 "sip_subscriptions.event='presence' and sip_subscriptions.full_from like '%%%q%%'",
-                                                                switch_str_nil(status), switch_str_nil(rpid), from);
+                                                                switch_str_nil(status), switch_str_nil(rpid), mod_sofia_globals.hostname, profile->name, from);
                } else {
 
-                       sql = switch_mprintf("update sip_subscriptions set version=version+1 where "
-                                                                "sip_subscriptions.event='presence'");
+                       sql = switch_mprintf("update sip_subscriptions set version=version+1 where hostname='%q' and profile_name='%q' and "
+                                                                "sip_subscriptions.event='presence'", mod_sofia_globals.hostname, profile->name);
                        
                        if (mod_sofia_globals.debug_presence > 1) {
                                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "PRES SQL %s\n", sql);
@@ -923,10 +936,10 @@ static void actual_sofia_presence_event_handler(switch_event_t *event)
                                                                 "sip_subscriptions.full_to,sip_subscriptions.network_ip,sip_subscriptions.network_port "
                                                                 "from sip_subscriptions left join sip_presence on "
                                                                 "(sip_subscriptions.sub_to_user=sip_presence.sip_user and sip_subscriptions.sub_to_host=sip_presence.sip_host and "
-                                                                "sip_subscriptions.profile_name=sip_presence.profile_name) "
-                                                                "where "
+                                                                "sip_subscriptions.profile_name=sip_presence.profile_name and sip_subscriptions.hostname = sip_presence.hostname) "
+                                                                "where sip_subscriptions.hostname='%q' and sip_subscriptions.profile_name='%q' and "
                                                                 "sip_subscriptions.event='presence'", switch_str_nil(status),
-                                                                switch_str_nil(rpid));
+                                                                switch_str_nil(rpid), mod_sofia_globals.hostname, profile->name);
                }
 
                switch_assert(sql != NULL);
@@ -1039,13 +1052,14 @@ static void actual_sofia_presence_event_handler(switch_event_t *event)
 #endif
 
                                        if (uuid) {
-                                               sql = switch_mprintf("update sip_dialogs set call_info='%q',call_info_state='%q' where hostname='%q' and uuid='%q'",
-                                                                                        call_info, call_info_state, mod_sofia_globals.hostname, uuid);
+                                               sql = switch_mprintf("update sip_dialogs set call_info='%q',call_info_state='%q' where "
+                                                                                        "hostname='%q' and profile_name='%q' and uuid='%q'",
+                                                                                        call_info, call_info_state, mod_sofia_globals.hostname, profile->name, uuid);
                                        } else {
-                                               sql = switch_mprintf("update sip_dialogs set call_info='%q', call_info_state='%q' where hostname='%q' and "
+                                               sql = switch_mprintf("update sip_dialogs set call_info='%q', call_info_state='%q' where hostname='%q' and profile_name='%q' and "
                                                                                         "((sip_dialogs.sip_from_user='%q' and sip_dialogs.sip_from_host='%q') or presence_id='%q@%q') and call_info='%q'",
                                                                                         
-                                                                                        call_info, call_info_state, mod_sofia_globals.hostname, euser, host, euser, host, call_info);
+                                                                                        call_info, call_info_state, mod_sofia_globals.hostname, profile->name, euser, host, euser, host, call_info);
                                                
                                        }
                                        
@@ -1064,14 +1078,16 @@ static void actual_sofia_presence_event_handler(switch_event_t *event)
                                }
                                
                                if (!strcmp(proto, "dp")) {
-                                       sql = switch_mprintf("update sip_presence set rpid='%q',status='%q' where sip_user='%q' and sip_host='%q'",
-                                                                                rpid, status, euser, host);
+                                       sql = switch_mprintf("update sip_presence set rpid='%q',status='%q' where hostname='%q' nad profile_name='%q' and "
+                                                                                "sip_user='%q' and sip_host='%q'",
+                                                                                rpid, status, mod_sofia_globals.hostname, profile->name, euser, host);
                                        sofia_glue_execute_sql_now(profile, &sql, SWITCH_TRUE);
                                }
                                
                                sql = switch_mprintf("select status,rpid,presence_id from sip_dialogs "
-                                                                        "where ((sip_from_user='%q' and sip_from_host='%q') or presence_id='%q@%q') order by rcd desc", 
-                                                                        euser, host, euser, host);
+                                                                        "where hostname='%q' and profile_name='%q' and "
+                                                                        "((sip_from_user='%q' and sip_from_host='%q') or presence_id='%q@%q') order by rcd desc", 
+                                                                        mod_sofia_globals.hostname, profile->name, euser, host, euser, host);
                                sofia_glue_execute_sql_callback(profile, profile->ireg_mutex, sql, sofia_presence_dialog_callback, &dh);
                                switch_safe_free(sql);
 
@@ -1082,12 +1098,13 @@ static void actual_sofia_presence_event_handler(switch_event_t *event)
 
                                if (zstr(call_id)) {
                                        
-                                       sql = switch_mprintf("update sip_subscriptions set version=version+1 where "
+                                       sql = switch_mprintf("update sip_subscriptions set version=version+1 where hostname='%q' and profile_name='%q' and "
                                                                                 "sip_subscriptions.event != 'line-seize' "
                                                                                 "and sip_subscriptions.proto='%q' and (event='%q' or event='%q') and sub_to_user='%q' and "
                                                                                 "(sub_to_host='%q' or sub_to_host='%q' or sub_to_host='%q' or "
                                                                                 "presence_hosts like '%%%q%%') and "
                                                                                 "(sip_subscriptions.profile_name = '%q' or presence_hosts like '%%%q%%')", 
+                                                                                mod_sofia_globals.hostname, profile->name,
                                                                                 proto, event_type, alt_event_type, euser, host, profile->sipip,
                                                                                 profile->extsipip ? profile->extsipip : "N/A", host, profile->name, host);
                                        
@@ -1111,22 +1128,23 @@ static void actual_sofia_presence_event_handler(switch_event_t *event)
                                                                                 "from sip_subscriptions "
                                                                                 "left join sip_presence on "
                                                                                 "(sip_subscriptions.sub_to_user=sip_presence.sip_user and sip_subscriptions.sub_to_host=sip_presence.sip_host and "
-                                                                                "sip_subscriptions.profile_name=sip_presence.profile_name) "
+                                                                                "sip_subscriptions.profile_name=sip_presence.profile_name and sip_subscriptions.hostname=sip_presence.hostname) "
                                                                                  
-                                                                                "where sip_subscriptions.event != 'line-seize' and "
+                                                                                "where sip_subscriptions.hostname='%q' and sip_subscriptions.profile_name='%q' and "
+                                                                                "sip_subscriptions.event != 'line-seize' and "
                                                                                 "sip_subscriptions.proto='%q' and "
                                                                                 "(event='%q' or event='%q') and sub_to_user='%q' "
-                                                                                "and (sub_to_host='%q' or sub_to_host='%q' or sub_to_host='%q' or presence_hosts like '%%%q%%') "
-                                                                                "and (sip_subscriptions.profile_name = '%q' or presence_hosts like '%%%q%%') ",
+                                                                                "and (sub_to_host='%q' or sub_to_host='%q' or sub_to_host='%q' or presence_hosts like '%%%q%%') ",
+                                                                                
                                                                                  
                                                                                 switch_str_nil(status), switch_str_nil(rpid), host,
-                                                                                dh.status,dh.rpid,dh.presence_id, proto,
+                                                                                dh.status,dh.rpid,dh.presence_id, mod_sofia_globals.hostname, profile->name, proto,
                                                                                 event_type, alt_event_type, euser, host, profile->sipip, 
-                                                                                profile->extsipip ? profile->extsipip : "N/A", host, profile->name, host);
+                                                                                profile->extsipip ? profile->extsipip : "N/A", host);
                                } else {
                                        sql = switch_mprintf("update sip_subscriptions set version=version+1 where sip_subscriptions.event != 'line-seize' and "
-                                                                                "sip_subscriptions.profile_name = '%q' and sip_subscriptions.call_id='%q'",
-                                                                                profile->name, call_id);
+                                                                                "hostname='%q' and profile_name = '%q' and sip_subscriptions.call_id='%q'",
+                                                                                mod_sofia_globals.hostname, profile->name, call_id);
 
 
                                        if (mod_sofia_globals.debug_presence > 1) {
@@ -1147,13 +1165,14 @@ static void actual_sofia_presence_event_handler(switch_event_t *event)
                                                                                 "from sip_subscriptions "
                                                                                 "left join sip_presence on "
                                                                                 "(sip_subscriptions.sub_to_user=sip_presence.sip_user and sip_subscriptions.sub_to_host=sip_presence.sip_host and "
-                                                                                "sip_subscriptions.profile_name=sip_presence.profile_name) "
+                                                                                "sip_subscriptions.profile_name=sip_presence.profile_name and sip_subscriptions.hostname=sip_presence.hostname) "
                                                                                  
-                                                                                "where sip_subscriptions.event != 'line-seize' and "
-                                                                                "sip_subscriptions.profile_name = '%q' and sip_subscriptions.call_id='%q'",
+                                                                                "where sip_subscriptions.hostname='%q' and sip_subscriptions.profile_name='%q' and "
+                                                                                "sip_subscriptions.event != 'line-seize' and "
+                                                                                "sip_subscriptions.call_id='%q'",
                                                                                  
                                                                                 switch_str_nil(status), switch_str_nil(rpid), host,
-                                                                                dh.status,dh.rpid,dh.presence_id, profile->name, call_id);
+                                                                                dh.status,dh.rpid,dh.presence_id, mod_sofia_globals.hostname, profile->name, call_id);
 
                        }
                        
@@ -2507,7 +2526,8 @@ static int sofia_presence_sub_callback(void *pArg, int argc, char **argv, char *
 
                if (!zstr(astate) && !zstr(uuid) && 
                        helper && helper->stream.data && strcmp(helper->last_uuid, uuid) && strcasecmp(astate, "terminated") && strchr(uuid, '-')) {
-                       helper->stream.write_function(&helper->stream, "update sip_dialogs set state='%s' where uuid='%s';", astate, uuid);
+                       helper->stream.write_function(&helper->stream, "update sip_dialogs set state='%s' where hostname='%q' and profile_name='%q' and uuid='%s';", 
+                                                                                 mod_sofia_globals.hostname, profile->name, astate, uuid);
                        switch_copy_string(helper->last_uuid, uuid, sizeof(helper->last_uuid));
                }
 
@@ -2637,7 +2657,9 @@ static int sofia_presence_sub_callback(void *pArg, int argc, char **argv, char *
                const char *register_source = switch_event_get_header_nil(helper->event, "register-source");
                
                if (!zstr(uuid) && strchr(uuid, '-') && !zstr(status_line) && !zstr(rpid) && (zstr(register_source) || strcasecmp(register_source, "register"))) {
-                   char *sql = switch_mprintf("update sip_dialogs set rpid='%q',status='%q' where uuid='%q'", rpid, status_line, uuid);
+                   char *sql = switch_mprintf("update sip_dialogs set rpid='%q',status='%q' where hostname='%q' and profile_name='%q' and uuid='%q'", 
+                                                                          mod_sofia_globals.hostname, profile->name,
+                                                                          rpid, status_line, uuid);
                        sofia_glue_execute_sql(profile, &sql, SWITCH_TRUE);
                }
        }
@@ -2902,9 +2924,10 @@ static int sync_sla(sofia_profile_t *profile, const char *to_user, const char *t
        switch_core_hash_init(&sh->hash, sh->pool);
 
        sql = switch_mprintf("select sip_from_user,sip_from_host,call_info,call_info_state,uuid from sip_dialogs "
-                                                "where call_info_state is not null and hostname='%q' and ((sip_from_user='%q' and sip_from_host='%q') or presence_id='%q@%q') "
+                                                "where call_info_state is not null and hostname='%q' and profile_name='%q' "
+                                                "and ((sip_from_user='%q' and sip_from_host='%q') or presence_id='%q@%q') "
                                                 "and profile_name='%q'", 
-                                                mod_sofia_globals.hostname, to_user, to_host, to_user, to_host, profile->name);
+                                                mod_sofia_globals.hostname, profile->name, to_user, to_host, to_user, to_host, profile->name);
 
 
        if (mod_sofia_globals.debug_sla > 1) {
@@ -2918,11 +2941,13 @@ static int sync_sla(sofia_profile_t *profile, const char *to_user, const char *t
 
                if (unseize) {
                        sql = switch_mprintf("select call_id,expires,sub_to_user,sub_to_host,event,full_to,full_from,contact,expires,network_ip,network_port "
-                                                                "from sip_subscriptions where call_id='%q' and profile_name='%q')", call_id, profile->name);
+                                                                "from sip_subscriptions where call_id='%q' and hostname='%q' and profile_name='%q')", 
+                                                                mod_sofia_globals.hostname, call_id, profile->name);
 
                } else {
                        sql = switch_mprintf("select call_id,expires,sub_to_user,sub_to_host,event,full_to,full_from,contact,expires,network_ip,network_port "
-                                                                "from sip_subscriptions where call_id='%q' and profile_name='%q'", call_id, profile->name);
+                                                                "from sip_subscriptions where call_id='%q' and hostname='%q' and profile_name='%q'",
+                                                                mod_sofia_globals.hostname, call_id, profile->name);
                }
 
        } else {
@@ -2930,17 +2955,17 @@ static int sync_sla(sofia_profile_t *profile, const char *to_user, const char *t
                if (unseize) {
                        sql = switch_mprintf("select call_id,expires,sub_to_user,sub_to_host,event,full_to,full_from,contact,expires,network_ip,network_port "
                                                                 "from sip_subscriptions "
-                                                                "where hostname='%q' "
+                                                                "where hostname='%q' and profile_name='%q' "
                                                                 "and sub_to_user='%q' and sub_to_host='%q' "
                                                                 "and (event='call-info' or event='line-seize') and (profile_name='%q' or presence_hosts like '%%%q%%')", 
-                                                                mod_sofia_globals.hostname, to_user, to_host, profile->name, to_host);
+                                                                mod_sofia_globals.hostname, profile->name, to_user, to_host, profile->name, to_host);
                } else {
                        sql = switch_mprintf("select call_id,expires,sub_to_user,sub_to_host,event,full_to,full_from,contact,expires,network_ip,network_port "
                                                                 "from sip_subscriptions "
-                                                                "where hostname='%q' "
+                                                                "where hostname='%q' and profile_name='%q' "
                                                                 "and sub_to_user='%q' and sub_to_host='%q' " "and (event='call-info') and "
                                                                 "(profile_name='%q' or presence_hosts like '%%%q%%')", 
-                                                                mod_sofia_globals.hostname, to_user, to_host, profile->name, to_host);
+                                                                mod_sofia_globals.hostname, profile->name, to_user, to_host, profile->name, to_host);
                }
        }
 
@@ -2958,8 +2983,9 @@ static int sync_sla(sofia_profile_t *profile, const char *to_user, const char *t
 
 
        if (clear) {
-               sql = switch_mprintf("delete from sip_dialogs where ((sip_from_user='%q' and sip_from_host='%q') or presence_id='%q@%q') "
-                                                        "and call_info_state='seized'", to_user, to_host, to_user, to_host);
+               sql = switch_mprintf("delete from sip_dialogs where hostname='%q' and profile_name='%q' and "
+                                                        "((sip_from_user='%q' and sip_from_host='%q') or presence_id='%q@%q') "
+                                                        "and call_info_state='seized'", mod_sofia_globals.hostname, profile->name, to_user, to_host, to_user, to_host);
 
 
                if (mod_sofia_globals.debug_sla > 1) {
@@ -3116,7 +3142,8 @@ void sofia_presence_handle_sip_i_subscribe(int status,
        }
        
        if ((sub_state != nua_substate_terminated)) {
-               sql = switch_mprintf("select count(*) from sip_subscriptions where call_id='%q'", call_id);
+               sql = switch_mprintf("select count(*) from sip_subscriptions where call_id='%q' and hostname='%q' and profile_name='%q'", 
+                                                        call_id, mod_sofia_globals.hostname, profile->name);
                sofia_glue_execute_sql2str(profile, profile->ireg_mutex, sql, buf, sizeof(buf));
 
 
@@ -3138,9 +3165,9 @@ void sofia_presence_handle_sip_i_subscribe(int status,
                
                sql = switch_mprintf("update sip_subscriptions "
                                                         "set expires=%ld,contact='%q' "
-                                                        "where call_id='%q'",
-                                                        (long) switch_epoch_time_now(NULL) + exp_delta, contact_str,
-                                                        call_id);
+                                                        "where hostname='%q' and profile_name='%q' and call_id='%q' and profile_name='%q'",
+                                                        (long) switch_epoch_time_now(NULL) + exp_delta, contact_str, mod_sofia_globals.hostname, profile->name,
+                                                        call_id, profile->name);
 
                if (mod_sofia_globals.debug_presence > 0 || mod_sofia_globals.debug_sla > 0) {
                        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR,
@@ -3151,7 +3178,8 @@ void sofia_presence_handle_sip_i_subscribe(int status,
        } else {
 
                if (sub_state == nua_substate_terminated) {
-                       sql = switch_mprintf("delete from sip_subscriptions where call_id='%q'", call_id, mod_sofia_globals.hostname);
+                       sql = switch_mprintf("delete from sip_subscriptions where call_id='%q' and profile_name='%q' and hostname='%q'", 
+                                                                call_id, profile->name, mod_sofia_globals.hostname);
                
                        if (mod_sofia_globals.debug_presence > 0 || mod_sofia_globals.debug_sla > 0) {
                                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR,
@@ -3289,9 +3317,10 @@ void sofia_presence_handle_sip_i_subscribe(int status,
                                        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "CANCEL LINE SEIZE\n");
                                }
 
-                               sql = switch_mprintf("delete from sip_dialogs where ((sip_from_user='%q' and sip_from_host='%q') or presence_id='%q@%q') "
+                               sql = switch_mprintf("delete from sip_dialogs where hostname='%q' and profile_name='%q' and "
+                                                                        "((sip_from_user='%q' and sip_from_host='%q') or presence_id='%q@%q') "
                                                                         "and call_info_state='seized'",
-                                                                        to_user, to_host, to_user, to_host);
+                                                                        mod_sofia_globals.hostname, profile->name, to_user, to_host, to_user, to_host);
 
 
                                if (mod_sofia_globals.debug_sla > 1) {
@@ -3325,9 +3354,10 @@ void sofia_presence_handle_sip_i_subscribe(int status,
 
 
 
-                               sql = switch_mprintf("delete from sip_dialogs where ((sip_from_user='%q' and sip_from_host='%q') or presence_id='%q@%q') "
+                               sql = switch_mprintf("delete from sip_dialogs where hostname='%q' and profile_name='%q' and "
+                                                                        "((sip_from_user='%q' and sip_from_host='%q') or presence_id='%q@%q') "
                                                                         "and call_info_state='seized' and profile_name='%q'",
-                                                                        to_user, to_host, to_user, to_host, profile->name);
+                                                                        mod_sofia_globals.hostname, profile->name, to_user, to_host, to_user, to_host, profile->name);
 
 
                                if (mod_sofia_globals.debug_sla > 1) {
@@ -3361,9 +3391,11 @@ void sofia_presence_handle_sip_i_subscribe(int status,
        if (!strcasecmp(event, "message-summary")) {
                if ((sql = switch_mprintf("select proto,sip_user,'%q',sub_to_user,sub_to_host,event,contact,call_id,full_from,"
                                                                  "full_via,expires,user_agent,accept,profile_name,network_ip"
-                                                                 " from sip_subscriptions where "
+                                                                 " from sip_subscriptions where hostname='%q' and profile_name='%q' and "
                                                                  "event='message-summary' and sip_user='%q' "
-                                                                 "and (sip_host='%q' or presence_hosts like '%%%q%%')", to_host, to_user, to_host, to_host))) {
+                                                                 "and (sip_host='%q' or presence_hosts like '%%%q%%')", 
+                                                                 mod_sofia_globals.hostname, profile->name,
+                                                                 to_host, to_user, to_host, to_host))) {
                        sofia_glue_execute_sql_callback(profile, profile->ireg_mutex, sql, sofia_presence_sub_reg_callback, profile);
 
                        switch_safe_free(sql);
@@ -3581,7 +3613,8 @@ uint32_t sofia_presence_contact_count(sofia_profile_t *profile, const char *cont
        char buf[32] = "";
        char *sql;
        
-       sql = switch_mprintf("select count(*) from sip_subscriptions where profile_name='%q' and contact='%q'", profile->name, contact_str);
+       sql = switch_mprintf("select count(*) from sip_subscriptions where hostname='%q' and profile_name='%q' and contact='%q'", 
+                                                mod_sofia_globals.hostname, profile->name, contact_str);
        
        sofia_glue_execute_sql2str(profile, profile->ireg_mutex, sql, buf, sizeof(buf));
        switch_safe_free(sql);
@@ -3695,8 +3728,13 @@ void sofia_presence_handle_sip_i_publish(nua_t *nua, sofia_profile_t *profile, n
 
                                sql = switch_mprintf("select full_to, full_from, contact, expires, call_id, event, network_ip, network_port, "
                                                                         "'application/pidf+xml' as ct,'%q' as pt "
-                                                                        " from sip_subscriptions where sub_to_user='%q' and sub_to_host='%q' and event='%q'"
-                                                                        "and contact = '%q' ", switch_str_nil(payload->pl_data), from_user, from_host, event_type);
+                                                                        " from sip_subscriptions where "
+                                                                        "hostname='%q' and profile_name='%q' and sub_to_user='%q' and sub_to_host='%q' and event='%q'"
+                                                                        "and contact = '%q' ", 
+                                                                        
+                                                                        switch_str_nil(payload->pl_data), 
+                                                                        mod_sofia_globals.hostname, profile->name,
+                                                                        from_user, from_host, event_type);
                                                                         
                                sofia_glue_execute_sql_callback(profile, profile->ireg_mutex, sql, sofia_presence_send_sql, &cb);
                                switch_safe_free(sql);