<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Too Far Afield &#187; base64</title>
	<atom:link href="http://blog.nachtarbeiter.net/tag/base64/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.nachtarbeiter.net</link>
	<description></description>
	<lastBuildDate>Mon, 09 Jan 2012 05:41:26 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Monitoring PowerDNS via the internal web server</title>
		<link>http://blog.nachtarbeiter.net/2010/02/16/monitoring-powerdns-via-the-internal-web-server/</link>
		<comments>http://blog.nachtarbeiter.net/2010/02/16/monitoring-powerdns-via-the-internal-web-server/#comments</comments>
		<pubDate>Tue, 16 Feb 2010 07:02:12 +0000</pubDate>
		<dc:creator>Patrick</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[apache]]></category>
		<category><![CDATA[base64]]></category>
		<category><![CDATA[dns]]></category>
		<category><![CDATA[howto]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[nginx]]></category>
		<category><![CDATA[pdns]]></category>
		<category><![CDATA[powerdns]]></category>

		<guid isPermaLink="false">http://stage.nachtarbeiter.net/?p=585</guid>
		<description><![CDATA[If you want to stay informed about the current status of your PowerDNS server, there is no need to scan the logs or use third party tools like logcheckd. Actually, you might want to do this anyway, but PowerDNS provides an internal web server, which summarizes all the status information (including log messages) in a [...]]]></description>
			<content:encoded><![CDATA[<p>If you want to stay informed about the current status of your <cite>PowerDNS</cite> server, there is no need to scan the logs or use third party tools like <code>logcheckd</code>. Actually, you might want to do this anyway, but <cite>PowerDNS</cite> provides an internal web server, which summarizes all the status information (including log messages) in a very nice and compact way for you to view.</p>
<p>By default, the web server will listen on port <code>8081</code> on <code>localhost</code>. That means, that you can&#8217;t view the status information from the outside network. To view the page, you could use a command line browser like <code>lynx</code>. You could change the interface of the web server to a public IP address, but this is <strong>very insecure</strong> and not recommended. The <cite>PowerDNS</cite> web server provides some sensitive information about your DNS server and you should not expose this information to the public. Also, the <cite>PowerDNS</cite> web server is probably not as thoroughly tested and hardened as <abbr title="for example">e.g.</abbr> <cite>Apache</cite> or <cite>nginx</cite>.  Another problem is, that you can only specify a single password and that you are limited to basic authentication, which is not very secure.</p>
<p>There is a secure way to retrieve the information provided by <cite>PowerDNS</cite> from outside your DNS server host. You could use <cite>Apache</cite>, <cite>nginx</cite> or any other web server you like as a proxy server. That way you can use more advanced authentication methods built into that web server to secure your status page. I will now show you how to do this using Apache 2 on Debian. We&#8217;ll need <code>mod_proxy</code>, <code>mod_proxy_http</code> and <code>mod_headers</code> enabled on the Apache 2 server. If you do not want to run an instance of Apache 2 on your DNS server, you could use an SSL tunnel or a secure back channel link to a remote Apache server to retrieve the status page. But this is beyond the scope of this post.</p>
<p>First, enable the internal <cite>PowerDNS</cite> web server by editing the configuration file.</p>
<ol class="code">
<li><code>webserver=yes</code></li>
<li><code>webserver-address=127.0.0.1</code></li>
<li><code>webserver-port=8081</code></li>
<li><code>webserver-password=PowerDNS</code></li>
</ol>
<p>This tells <cite>PowerDNS</cite> to run the internal web server on port <code>8081</code> of the <code>localhost</code> interface. The user name will be <code>admin</code> and the password will be <code>PowerDNS</code>. Of course you should change the password to something more secure, it&#8217;s just an example. After a restart of <cite>PowerDNS</cite> you can connect to your server from your DNS server host. The password is optional, but it&#8217;s safer that way, especially in an environment where you&#8217;ve got other users on your DNS server box.</p>
<p>Now install and enable the required modules of Apache 2 by executing the following commands.</p>
<ol class="code">
<li><code>apt-get install libapache2-mod-proxy-html</code></li>
<li><code>a2enmod proxy</code></li>
<li><code>a2enmod proxy_http</code></li>
<li><code>a2enmod headers</code></li>
</ol>
<p>The last module is only needed, if you set a password for your internal web server as recommended above. I assume that you&#8217;ve got some kind of virtual host configuration for your Apache server. You&#8217;ll want to add a new virtual host for the DNS status information. If you use a subdirectory, navigation might be a bit odd. Let&#8217;s add a new site to the available sites.</p>
<ol class="code">
<li><code>vi /etc/apache2/sites-available/status.dns.example.net</code></li>
<li><code>&lt;VirtualHost 192.168.0.1:80&gt;</code></li>
<li><code>        ServerName status.dns.example.net</code></li>
<li><code>        DocumentRoot /var/www/</code></li>
<li><code></code></li>
<li><code>        ProxyRequests Off</code></li>
<li><code>        &lt;proxy *&gt;</code></li>
<li><code>               Order deny,allow</code></li>
<li><code>               Allow from all</code></li>
<li><code>                ForceType 'text/html; charset=UTF-8'</code></li>
<li><code>        &lt;/proxy&gt;</code></li>
<li><code>        ProxyPass / http://localhost:8081</code></li>
<li><code>        ProxyPassReverse / http://localhost:8081</code></li>
<li><code></code></li>
<li><code>&lt;/VirtualHost&gt;</code></li>
</ol>
<p>Alright, so what are we doing here? Basically, we&#8217;re adding a new virtual host <code>status.dns.example.net</code> on our main interface of <code>dns.example.net</code>. We&#8217;re using a reverse proxy to to send all requests coming from the outside to our internal <cite>PowerDNS</cite> web server on port <code>8081</code>. Also, we&#8217;re forcing a <code>text/html</code> content type in the proxy request filter, because otherwise we would just get <code>text/plain</code> and we would simply see the source code, which is probably not what you want. Let&#8217;s enable the site for a test:</p>
<ol class="code">
<li><code>a2ensite status.dns.example.net</code></li>
<li><code>/etc/init.d/apache2 reload</code></li>
</ol>
<p>If you point your web browser to <code>status.dns.example.net</code> you should now see the internal status page of <cite>PowerDNS</cite>. If you set a password above, you will see a password dialogue. This is the password dialogue sent by the <cite>PowerDNS</cite> web server. The user name is <code>admin</code> and the password is your password. Try it out now, because we&#8217;ll get rid of this in a minute.</p>
<p>For security reasons, you probably want to use Apache 2 for authentication. E.g. you might want use a SSL connection and authenticate your co-workers using the internal LDAP server of your company intranet. You might even stay with the insecure basic authentication method, but use other user names. This is entirely up to you and beyond the scope of this post. Consult the Apache 2 documentation on how to do this. What you probably don&#8217;t want to do, however, is to authenticate twice (first your secure authentication method and then <cite>PowerDNS</cite> basic authentication. Luckily, we can configure the Apache 2 proxy to do the authentication for us. This is a bit tricky, though.<br />
To authenticate at the <cite>PowerDNS</cite> server, Apache 2 needs to send an additional <code>Authorization</code> header line to the <cite>PowerDNS</cite> server with every request it handles. We use the <code>RequestHeader</code> directive to override any existing <code>Authorization</code> header with our own authentication data. Add the following lines just before the end of the virtual host container.</p>
<ol class="code">
<li><code>        &lt;Location /&gt;</code></li>
<li><code>                RequestHeader Set Authorization "Basic YWRtaW46UG93ZXJETlM="</code></li>
<li><code>        &lt;/Location&gt;</code></li>
</ol>
<p>The above example works only for our example password, which is <code>PowerDNS</code>. Try it for a test. This is, because part of the header value is encrypted using the <code>base64</code> algorithm. You need to change the encrypted part <code>YWRtaW46UG93ZXJETlM=</code>. In plain text the encrypted string would read <code>admin:PowerDNS</code>, where <code>admin</code> is the user name and <code>PowerDNS</code> is the password. To use your own password, you need to encrypt the string <code>admin:yourownpassword</code> using the <code>base64</code> algorithm and replace our example string. Be sure to keep the <code>Basic</code> and the space. It is crucial for success, that you&#8217;ve got the right encrypted string. There are a number of <a href="http://www.motobit.com/util/base64-decoder-encoder.asp">online tools</a>, to encode and decode these strings. To ensure, that you&#8217;ve got the correct encryption method, encode the example string and compare it to the string above for a reference. If you&#8217;ve got the wrong string, it will not work.</p>
<p>Restart the Apache 2 server. To clear your password cache, restart your browser. Now surf to the site again. You will see, that the password dialogue is gone. Now, don&#8217;t forget to secure the page again using Apache 2. Under any circumstances, do <em>not</em> use <code>Directory</code> containers in the configuration. These will not apply to the proxy, because the proxy is not a physical directory on your server. Use <code>Location</code> containers like we did above for setting the <code>RequestHeader</code> directive. Also, you could still use insecure basic authentication to secure the page, if you wanted. It would work regardless of the <code>RequestHeader</code> magic.</p>
<h3>A light-weight alternative</h3>
<p>For those of you, who think that <cite>Apache</cite> is too heavy, here is an example for the <cite>nginx</cite> web server:</p>
<ol class="code">
<li><code>vi /etc/nginx/sites-available/status.dns.example.net</code></li>
<li><code>server {</code></li>
<li><code>   listen 192.168.0.1:80;</code></li>
<li><code>   server_name status.dns.example.net;</code></li>
<li><code>   root /var/www/nginx-default;</code></li>
<li><code>   location / {</code></li>
<li><code>      index index.html</code></li>
<li><code>      proxy_pass http://localhost:8081;</code></li>
<li><code>      proxy_redirect off;</code></li>
<li><code>      proxy_set_header Authorization "Basic YWRtaW46UG93ZXJETlM=";</code></li>
<li><code>   }</code></li>
<li><code>}</code></li>
</ol>
]]></content:encoded>
			<wfw:commentRss>http://blog.nachtarbeiter.net/2010/02/16/monitoring-powerdns-via-the-internal-web-server/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Storing binary data inside a MySQL database</title>
		<link>http://blog.nachtarbeiter.net/2009/04/28/storing-binary-data-inside-a-mysql-database/</link>
		<comments>http://blog.nachtarbeiter.net/2009/04/28/storing-binary-data-inside-a-mysql-database/#comments</comments>
		<pubDate>Tue, 28 Apr 2009 05:52:33 +0000</pubDate>
		<dc:creator>Patrick</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[base64]]></category>
		<category><![CDATA[binary]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://blog.nachtarbeiter.net/?p=743</guid>
		<description><![CDATA[Sometimes you need to store binary data inside a MySQL database. While there are various field types for binary data in MySQL, its handling of binary data has a share of different problems. As far as I know you might hit some bugs, if you&#8217;re using non-standard connection encodings or character sets. Also, binary data [...]]]></description>
			<content:encoded><![CDATA[<p>Sometimes you need to store binary data inside a <cite>MySQL</cite> database. While there are various field types for binary data in <cite>MySQL</cite>, its handling of binary data has a share of different problems. As far as I know you might hit some bugs, if you&#8217;re using non-standard connection encodings or character sets. Also, binary data is a hell to escape properly. For example, if you store <code>S????'x7?ma?X?UHMh?</code> inside the <cite>MySQL</cite> database you might end up with <code>?????r??v ???????v</code> later under certain conditions. This is not really ideal, to say the least.</p>
<p>The best thing to do is to convert the binary data to a string-based representation, e.g. a hexadecimal form or use a two-way encryption method like <a href="http://en.wikipedia.org/wiki/Base64"><cite>Base64</cite></a>. In fact, I recommend to use <cite>Base64</cite>. The drawback is, that a <cite>Base64</cite> representation uses around 35% more space than the original binary representation. For example, in <cite><abbr title="PHP Hypertext Preprocessor">PHP</abbr></cite> you would do something like this:</p>
<ol class="code">
<li><code>// Encode binary data</code></li>
<li><code>$binary = base64_encode($binary);</code></li>
<li><code>&nbsp;</code></li>
<li><code>// Decode binary data</code></li>
<li><code>$binary = base64_decode($binary);</code></li>
</ol>
<p><em>Update:</em> Changed the example data slightly to accommodate some screen readers, that require a valid feed. Thank you <cite lang="de">Jan</cite> for pointing this out.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.nachtarbeiter.net/2009/04/28/storing-binary-data-inside-a-mysql-database/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

