Index: channels/chan_sip.c =================================================================== RCS file: /usr/cvsroot/asterisk/channels/chan_sip.c,v retrieving revision 1.466 diff -u -r1.466 chan_sip.c --- channels/chan_sip.c 31 Jul 2004 04:04:05 -0000 1.466 +++ channels/chan_sip.c 6 Aug 2004 11:43:41 -0000 @@ -1,3 +1,4 @@ +// vim:ts=4:sw=4:ruler:cindent /* * Asterisk -- A telephony toolkit for Linux. * @@ -9,6 +10,7 @@ * * This program is free software, distributed under the terms of * the GNU General Public License + * */ @@ -116,6 +118,8 @@ static char mydbpass[80]; static char mydbhost[80]; static char mydbname[80]; +static int mydbcanblock = 0; +static int mydbdropduplicates = 0; #endif /* SIP Debug */ @@ -1006,7 +1010,11 @@ mysql_real_escape_string(mysql, name, user, strlen(user)); } - snprintf(query, sizeof(query), "SELECT name, secret, context, username, ipaddr, port, regseconds, callerid, restrictcid FROM sipfriends WHERE name=\"%s\"", name); + if (mydbcanblock) { + snprintf(query, sizeof(query), "SELECT name, secret, context, username, ipaddr, port, regseconds, callerid, restrictcid FROM sipfriends WHERE name='%s' AND active = 'y'", name); + } else { + snprintf(query, sizeof(query), "SELECT name, secret, context, username, ipaddr, port, regseconds, callerid, restrictcid FROM sipfriends WHERE name='%s'", name); + } ast_mutex_lock(&mysqllock); mysql_query(mysql, query); @@ -1074,12 +1082,21 @@ time(&nowtime); mysql_real_escape_string(mysql, name, peer, strlen(peer)); mysql_real_escape_string(mysql, uname, username, strlen(username)); - snprintf(query, sizeof(query), "UPDATE sipfriends SET ipaddr=\"%s\", port=\"%d\", regseconds=\"%ld\", username=\"%s\" WHERE name=\"%s\"", + /* In case of IP address or account change, reset address,port,regseconds and username if address is found */ + if (mydbdropduplicates) { + snprintf(query, sizeof(query), "UPDATE sipfriends SET ipaddr=NULL, regseconds=NULL, username=NULL WHERE ipaddr='%s' AND username='%s'", + ast_inet_ntoa(iabuf, sizeof(iabuf), sin->sin_addr), uname); + ast_mutex_lock(&mysqllock); + if (mysql_real_query(mysql, query, strlen(query))) + ast_log(LOG_WARNING, "Unable to update database (%s)\n",query); + ast_mutex_unlock(&mysqllock); + } + /* Update with the correct address */ + snprintf(query, sizeof(query), "UPDATE sipfriends SET ipaddr='%s', port='%d', regseconds='%ld', username='%s' WHERE name='%s'", ast_inet_ntoa(iabuf, sizeof(iabuf), sin->sin_addr), ntohs(sin->sin_port), nowtime + expiry, uname, name); ast_mutex_lock(&mysqllock); if (mysql_real_query(mysql, query, strlen(query))) - ast_log(LOG_WARNING, "Unable to update database\n"); - + ast_log(LOG_WARNING, "Unable to update database (%s)\n",query); ast_mutex_unlock(&mysqllock); } } @@ -1106,10 +1123,19 @@ name = alloca(strlen(peer) * 2 + 1); mysql_real_escape_string(mysql, name, peer, strlen(peer)); } - if (sin) - snprintf(query, sizeof(query), "SELECT name, secret, context, username, ipaddr, port, regseconds FROM sipfriends WHERE ipaddr=\"%s\" AND port=\"%d\"", ast_inet_ntoa(iabuf, sizeof(iabuf), sin->sin_addr), ntohs(sin->sin_port)); - else - snprintf(query, sizeof(query), "SELECT name, secret, context, username, ipaddr, port, regseconds FROM sipfriends WHERE name=\"%s\"", name); + if (mydbcanblock) { + if (sin) { + snprintf(query, sizeof(query), "SELECT name, secret, context, username, ipaddr, port, regseconds FROM sipfriends WHERE ipaddr='%s' AND port='%d' AND active = 'y'", ast_inet_ntoa(iabuf, sizeof(iabuf), sin->sin_addr), ntohs(sin->sin_port)); + } else { + snprintf(query, sizeof(query), "SELECT name, secret, context, username, ipaddr, port, regseconds FROM sipfriends WHERE name='%s' AND active = 'y'", name); + } + } else { + if (sin) { + snprintf(query, sizeof(query), "SELECT name, secret, context, username, ipaddr, port, regseconds FROM sipfriends WHERE ipaddr='%s' AND port='%d'", ast_inet_ntoa(iabuf, sizeof(iabuf), sin->sin_addr), ntohs(sin->sin_port)); + } else { + snprintf(query, sizeof(query), "SELECT name, secret, context, username, ipaddr, port, regseconds FROM sipfriends WHERE name='%s'", name); + } + } ast_mutex_lock(&mysqllock); mysql_query(mysql, query); if ((result = mysql_store_result(mysql))) { @@ -8429,6 +8455,12 @@ strncpy(mydbhost, v->value, sizeof(mydbhost) - 1); } else if (!strcasecmp(v->name, "dbname")) { strncpy(mydbname, v->value, sizeof(mydbname) - 1); + } else if (!strcasecmp(v->name, "dbcanblock")) { + if (!strcasecmp(v->value, "yes")) + mydbcanblock = 1; + } else if (!strcasecmp(v->name, "dbdropduplicates")) { + if (!strcasecmp(v->value, "yes")) + mydbdropduplicates = 1; #endif } /* else if (strcasecmp(v->name,"type")) Index: configs/sip.conf.sample =================================================================== RCS file: /usr/cvsroot/asterisk/configs/sip.conf.sample,v retrieving revision 1.37 diff -u -r1.37 sip.conf.sample --- configs/sip.conf.sample 28 Jul 2004 21:07:38 -0000 1.37 +++ configs/sip.conf.sample 6 Aug 2004 11:43:41 -0000 @@ -67,6 +67,21 @@ ; no = Use NAT mode only according to RFC3581 ; never = Never attempt NAT mode or RFC3581 support ;promiscredir = no ; If yes, allows 302 or REDIR to non-local SIP address + +; if using asteris sip_friends from mysql, the following makes sense +dbname=database_name +dbhost=localhost +dbuser=asteriskuser +dbpass=somepassword + +; drop duplicate ip/user pairs if exists upon registration +dbdropduplicates=yes + +; allow for blocking users. This uses a column in the sipfriends table called +; 'active', being a char(1) and having a value of 'y' if active, that is, not +; blocked. This is a dirty hack (see the source), but it works, and I need it. +dbcanblock=yes + ; Asterisk can register as a SIP user agent to a SIP proxy (provider) ; Format for the register statement is: ; register => user[:secret[:authuser]]@host[:port][/extension]