Index: channels/chan_sip.c =================================================================== RCS file: /usr/cvsroot/asterisk/channels/chan_sip.c,v retrieving revision 1.489 diff -u -r1.489 chan_sip.c --- channels/chan_sip.c 2 Sep 2004 13:48:11 -0000 1.489 +++ channels/chan_sip.c 3 Sep 2004 08:42:23 -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 mydbfriends = 0; +static int mydbcanblock = 0; #endif /* SIP Debug */ @@ -1022,7 +1026,9 @@ 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); + snprintf(query, sizeof(query), "SELECT name, secret, context, username, ipaddr, port, regseconds, callerid, restrictcid FROM sipfriends WHERE name='%s'", name); + if (mydbcanblock) + strncat(query, sizeof(query), " AND active = 'y'"); ast_mutex_lock(&mysqllock); mysql_query(mysql, query); @@ -1090,12 +1096,12 @@ 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\"", + /* 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); } } @@ -1122,10 +1128,13 @@ 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 (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) + strncat(query, sizeof(query), " AND active = 'y'"); ast_mutex_lock(&mysqllock); mysql_query(mysql, query); if ((result = mysql_store_result(mysql))) { @@ -1187,7 +1196,7 @@ static void update_peer(struct sip_peer *p, int expiry) { #ifdef MYSQL_FRIENDS - if (p->temponly) + if (mydbfriends && p->temponly) mysql_update_peer(p->name, &p->addr, p->username, expiry); #endif return; @@ -1221,7 +1230,7 @@ } #ifdef MYSQL_FRIENDS - if (!p) { + if (mydbfriends && !p) { p = mysql_peer(peer, sin); } #endif @@ -6055,7 +6064,7 @@ break; ast_mutex_unlock(&peerl.lock); #ifdef MYSQL_FRIENDS - if (!peer) + if (mydbfriends && !peer) peer = mysql_peer(argv[3], NULL); #endif if (peer) { @@ -8532,6 +8541,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, "mysqlsipfriends")) { + if (!strcasecmp(v->value, "yes")) + mydbfriends = 1; + } else if (!strcasecmp(v->name, "dbcanblock")) { + if (!strcasecmp(v->value, "yes")) + mydbcanblock = 1; #endif } /* else if (strcasecmp(v->name,"type")) @@ -8625,17 +8640,20 @@ ast_destroy(cfg); #ifdef MYSQL_FRIENDS /* Connect to db if appropriate */ - if (!mysql && !ast_strlen_zero(mydbname)) { - mysql = mysql_init(NULL); - if (!mysql_real_connect(mysql, mydbhost[0] ? mydbhost : NULL, mydbuser, mydbpass, mydbname, 0, NULL, 0)) { - memset(mydbpass, '*', strlen(mydbpass)); - ast_log(LOG_WARNING, "Database connection failed (db=%s, host=%s, user=%s, pass=%s)!\n", - mydbname, mydbhost, mydbuser, mydbpass); - free(mysql); - mysql = NULL; - } else - ast_verbose(VERBOSE_PREFIX_1 "Connected to database '%s' on '%s' as '%s'\n", - mydbname, mydbhost, mydbuser); + if (mydbfriends) { + /* ...and it must be given in sip.conf */ + if (!mysql && !ast_strlen_zero(mydbname)) { + mysql = mysql_init(NULL); + if (!mysql_real_connect(mysql, mydbhost[0] ? mydbhost : NULL, mydbuser, mydbpass, mydbname, 0, NULL, 0)) { + memset(mydbpass, '*', strlen(mydbpass)); + ast_log(LOG_WARNING, "Database connection failed (db=%s, host=%s, user=%s, pass=%s)!\n", + mydbname, mydbhost, mydbuser, mydbpass); + free(mysql); + mysql = NULL; + } else + ast_verbose(VERBOSE_PREFIX_1 "Connected to database '%s' on '%s' as '%s'\n", + mydbname, mydbhost, mydbuser); + } } #endif return 0; Index: configs/sip.conf.sample =================================================================== RCS file: /usr/cvsroot/asterisk/configs/sip.conf.sample,v retrieving revision 1.39 diff -u -r1.39 sip.conf.sample --- configs/sip.conf.sample 27 Aug 2004 02:45:35 -0000 1.39 +++ configs/sip.conf.sample 3 Sep 2004 08:42:23 -0000 @@ -68,9 +68,28 @@ ; never = Never attempt NAT mode or RFC3581 support ; route = Assume NAT, don't send rport (work around more UNIDEN bugs) ;promiscredir = no ; If yes, allows 302 or REDIR to non-local SIP address +<<<<<<< sip.conf.sample + +; if using asteris sip_friends from mysql, the following makes sense +mysqlsipfriends=yes + +; enter database connection info here +dbname=database_name +dbhost=localhost +dbuser=asteriskuser +dbpass=somepassword + +; 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 + +======= ; ; Note that promiscredir when redirects are made to the ; ; local system will cause loops since SIP is incapable ; ; of performing a "hairpin" call. +>>>>>>> 1.38 ; 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]