--- asterisk-1.6.2.11-rc1/channels/chan_sip.c.orig 2010-07-24 22:03:22.000000000 -0400 +++ asterisk-1.6.2.11-rc1/channels/chan_sip.c 2010-07-24 22:05:06.000000000 -0400 @@ -14015,7 +14015,7 @@ } /*! \brief Validate device authentication */ -static enum check_auth_result check_peer_ok(struct sip_pvt *p, char *of, +static enum check_auth_result check_peer_ok(struct sip_pvt *p, char *of, char *domain, struct sip_request *req, int sipmethod, struct sockaddr_in *sin, struct sip_peer **authpeer, enum xmittype reliable, @@ -14031,8 +14031,16 @@ */ peer = find_peer(of, NULL, TRUE, FINDALLDEVICES, FALSE, 0); } else { - /* First find devices based on username (avoid all type=peer's) */ - peer = find_peer(of, NULL, TRUE, FINDUSERS, FALSE, 0); + /* Make sure the device is from a local domain */ + if (AST_LIST_EMPTY(&domain_list) || check_sip_domain(domain, NULL, 0)) { + /* First find devices based on username (avoid all type=peer's) */ + peer = find_peer(of, NULL, TRUE, FINDUSERS, FALSE, 0); + } + else { + if(debug) + ast_verbose("Unknown peer from domain %s\n", domain); + peer = NULL; + } /* Then find devices based on IP */ if (!peer) { @@ -14271,7 +14279,7 @@ } } - res = check_peer_ok(p, of, req, sipmethod, sin, + res = check_peer_ok(p, of, domain, req, sipmethod, sin, authpeer, reliable, rpid_num, calleridname, uri2); if (res != AUTH_DONT_KNOW) return res;