<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[Freeswitch-trunk][14409] </title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<div id="header">FreeSWITCH Subversion</div>
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://fisheye.freeswitch.org/changelog/FreeSWITCH?cs=14409">14409</a></dd>
<dt>Author</dt> <dd>michaelgg</dd>
<dt>Date</dt> <dd>2009-07-29 03:32:20 -0500 (Wed, 29 Jul 2009)</dd>
</dl>

<h3>Log Message</h3>
<pre>Be safe; use parameters. Detect bad ODBC drivers and refuse to load.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#freeswitchtrunksrcmodlanguagesmod_managedmanagedexampleseasyrouteAppconfig">freeswitch/trunk/src/mod/languages/mod_managed/managed/examples/easyroute/App.config</a></li>
<li><a href="#freeswitchtrunksrcmodlanguagesmod_managedmanagedexampleseasyrouteeasyroutefs">freeswitch/trunk/src/mod/languages/mod_managed/managed/examples/easyroute/easyroute.fs</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="freeswitchtrunksrcmodlanguagesmod_managedmanagedexampleseasyrouteAppconfig"></a>
<div class="modfile"><h4>Modified: freeswitch/trunk/src/mod/languages/mod_managed/managed/examples/easyroute/App.config (14408 => 14409)</h4>
<pre class="diff"><span>
<span class="info">--- freeswitch/trunk/src/mod/languages/mod_managed/managed/examples/easyroute/App.config        2009-07-29 06:00:15 UTC (rev 14408)
+++ freeswitch/trunk/src/mod/languages/mod_managed/managed/examples/easyroute/App.config        2009-07-29 08:32:20 UTC (rev 14409)
</span><span class="lines">@@ -4,13 +4,9 @@
</span><span class="cx">     &lt;add key=&quot;connectionString&quot; value=&quot;DSN=easyroute;User=root;Pwd=;&quot; /&gt;
</span><span class="cx">     &lt;add key=&quot;defaultProfile&quot; value=&quot;sofia/default&quot;/&gt;
</span><span class="cx">     &lt;add key=&quot;defaultGateway&quot; value=&quot;192.168.1.1&quot;/&gt;
</span><del>-    &lt;!-- query can be changed, but fields must be in the same order, and the number parameter must be %number% --&gt;
-    &lt;add key=&quot;query&quot; value=&quot;SELECT gateways.gateway_ip, gateways.group, gateways.limit, gateways.techprofile, numbers.acctcode, numbers.translated from gateways, numbers where numbers.number = %number% and numbers.gateway_id = gateways.gateway_id;&quot; /&gt;
</del><ins>+    &lt;!-- query can be changed, but fields must be in the same order, and the number parameter must be ? --&gt;
+    &lt;add key=&quot;query&quot; value=&quot;SELECT gateways.gateway_ip, gateways.group, gateways.limit, gateways.techprofile, numbers.acctcode, numbers.translated from gateways, numbers where numbers.number = ? and numbers.gateway_id = gateways.gateway_id;&quot; /&gt;
</ins><span class="cx">     
</span><del>-    &lt;!-- MySQL and other DBs improperly consider \ to be an escape character. easyroute will remove all backslashes from queries to be safe. 
-         if you can handle backlashes properly, set keepBackslashes to true. --&gt;
-    &lt;!-- &lt;add key=&quot;keepBackslashes&quot; value=&quot;false&quot; --&gt;
-    
</del><span class="cx">     &lt;!-- To avoid other injections, the incoming number will remove characters matching this regex. Default [^0-9#\*] allows only digits, # and *.--&gt;
</span><span class="cx">     &lt;add key=&quot;numberRegexFilter&quot; value=&quot;[^0-9#\*]&quot; /&gt;
</span><span class="cx">   &lt;/appSettings&gt;
</span></span></pre></div>
<a id="freeswitchtrunksrcmodlanguagesmod_managedmanagedexampleseasyrouteeasyroutefs"></a>
<div class="modfile"><h4>Modified: freeswitch/trunk/src/mod/languages/mod_managed/managed/examples/easyroute/easyroute.fs (14408 => 14409)</h4>
<pre class="diff"><span>
<span class="info">--- freeswitch/trunk/src/mod/languages/mod_managed/managed/examples/easyroute/easyroute.fs        2009-07-29 06:00:15 UTC (rev 14408)
+++ freeswitch/trunk/src/mod/languages/mod_managed/managed/examples/easyroute/easyroute.fs        2009-07-29 08:32:20 UTC (rev 14409)
</span><span class="lines">@@ -6,15 +6,25 @@
</span><span class="cx"> type QueryResult = { dialstring: string; group: string; acctcode: string; limit: int; translated: string }
</span><span class="cx"> 
</span><span class="cx"> module easyroute =
</span><ins>+    // Basic config
</ins><span class="cx">     let defaultStr def = function null | &quot;&quot; -&gt; def | s -&gt; s
</span><span class="cx">     let getAppSetting (name:string) = match Configuration.ConfigurationManager.AppSettings.Get name with null -&gt; &quot;&quot; | x -&gt; x
</span><span class="cx">     let connString      = getAppSetting &quot;connectionString&quot;
</span><span class="cx">     let defaultProfile  = getAppSetting &quot;defaultProfile&quot;
</span><span class="cx">     let defaultGateway  = getAppSetting &quot;defaultGateway&quot;
</span><del>-    let query = getAppSetting &quot;query&quot;
</del><ins>+    let query           = getAppSetting &quot;query&quot;
</ins><span class="cx">     let configOk = [ connString; defaultProfile; defaultGateway; query; ] |&gt; List.forall (String.IsNullOrEmpty &gt;&gt; not)
</span><del>-    let keepBackslashes = defaultStr &quot;false&quot; (getAppSetting &quot;keepBackslashes&quot;) = &quot;true&quot;
</del><span class="cx">     let numberRegexFilter = defaultStr &quot;[^0-9#]&quot; (getAppSetting &quot;numberRegexFilter&quot;)
</span><ins>+    
+    // Determine if ODBC driver quotes parameters properly - MySQL &lt; 3.51.16 apparently does not
+    // We'll select the string &quot;'&quot; -- if quoting works, we'll get ' back. Otherwise, it'll fail, and we'll refuse to load
+    // Error 1064 seems to be the syntax error code MySQL returns. Otherwise, the exception will still stop it from loading, just less gracefully.
+    let odbcOk = use conn = new Odbc.OdbcConnection(connString)
+                 use comm = new Odbc.OdbcCommand(&quot;SELECT ?&quot;, conn)
+                 comm.Parameters.AddWithValue(&quot;@test&quot;, &quot;'&quot;) |&gt; ignore
+                 conn.Open()
+                 try string (comm.ExecuteScalar()) = &quot;'&quot; 
+                 with :? Odbc.OdbcException as ex when ex.Errors.Count &gt; 0 &amp;&amp; ex.Errors.[0].NativeError = 1064 -&gt; false
</ins><span class="cx">                 
</span><span class="cx">     let formatDialstring number gateway profile separator = 
</span><span class="cx">         match separator with 
</span><span class="lines">@@ -39,11 +49,9 @@
</span><span class="cx">     let lookup (number: string) sep =
</span><span class="cx">         try
</span><span class="cx">             let number = if numberRegexFilter = &quot;&quot; then number else Text.RegularExpressions.Regex.Replace(number, numberRegexFilter, &quot;&quot;, regexOpts)
</span><del>-            let number = if keepBackslashes        then number else number.Replace(&quot;\\&quot;, &quot;&quot;) 
-            let query = query.Replace(&quot;%number%&quot;, sprintf &quot;'%s'&quot; (number.Replace(&quot;'&quot;, &quot;''&quot;))) // Don't use params cause some odbc drivers are awesome
-            Log.WriteLine(LogLevel.Debug, &quot;EasyRoute query prepared: {0}&quot;, query)
</del><span class="cx">             use conn = new Odbc.OdbcConnection(connString)
</span><span class="cx">             use comm = new Odbc.OdbcCommand(query, conn)
</span><ins>+            comm.Parameters.AddWithValue(&quot;@number&quot;, number) |&gt; ignore
</ins><span class="cx">             conn.Open()
</span><span class="cx">             use reader = comm.ExecuteReader CommandBehavior.SingleRow
</span><span class="cx">             match reader.Read() with
</span><span class="lines">@@ -67,7 +75,8 @@
</span><span class="cx">     interface ILoadNotificationPlugin with
</span><span class="cx">         member x.Load() = 
</span><span class="cx">             if not configOk then Log.WriteLine(LogLevel.Alert, &quot;EasyRoute configuration is missing values.&quot;)
</span><del>-            configOk
</del><ins>+            if not odbcOk   then Log.WriteLine(LogLevel.Critical, &quot;ODBC driver doesn't handle quoting properly; upgrade driver.&quot;)
+            configOk &amp;&amp; odbcOk
</ins><span class="cx">         
</span><span class="cx">     interface IApiPlugin with
</span><span class="cx">         member x.ExecuteBackground ctx = 
</span></span></pre>
</div>
</div>
<div id="footer">See you at ClueCon</div>

</body>
</html>