Friday, June 29, 2012

forked-daapd in CentOS 6.2 64 Bit

Hey, folks; I was setting up forked-daapd in CentOS 6.2 (64 Bit), and ran into a few problems. One of the most recent was that the libantlr3c was not being used (headers were found). I found that if I add a --enable-64bit to the configure line (I used an RPM spec file), it will create it for 64 bit, which is really what I needed. I realized this when it kept failing to configure the forked-daapd with an error about libantlr3 and a missing function, and when I looked in the config.log for the error, the linker was ignoring the 32 bit libraries. (If you haven't enabled 64 bit, it is 32 by default, and you would have to use a 32 bit compiler by default if you wanted to). So, there is another quick hack.

Friday, June 8, 2012

Auto-Documentation - Revisited

Auto-Documentation has been around for quite a while. However, most applications don't cover a wide range of file types (e.g. some are more specific to c/c++, etc). I couldn't get some of the "famous" projects (like "Doxygen") to do exactly what I wanted to do with such a wide range of code types in a lot of my personal SVN repos. So, I did what any red-blooded geek would do. I wrote one. Sure, it still needs some work, but the reality is that it will use Doxygen formats for the C/C++ files to generate documentation, perl (scripts and modules), some of the graphics (and POV-Ray stuff) I have in the repo, and feeds the results through the template-toolkit for the result, and it will do surgical updates just on specific files when something is checked into the repo. Sure, some of you will say "github". But, like the arguments between "RedHat" vs. SuSE, or "Vi" vs. "Emacs", it's a matter of what you cut your teeth on, and SVN (and CVS, used for a decade) still meet my needs perfectly.

Friday, May 18, 2012

For the First Time, A Full Set of Seat Belts

Wow! Last night was a huge milestone in my project! I have both sets of seat belts in the project for the first time since the previous owner had the car!

If you want a little background, I found and procured my 1977 fifteen years ago (about a year or two after I joined the VetteNet). I was ecstatic at the time it showed up. The car barely ran (had to let it warm up for 5 minutes before you could shift into drive or it would die). There was no interior in the car (lots of the original , broken panels), and sometimes, it wouldn't even crank when trying to start it. The body had stress fractures, and the previous owner (who had it for two years) had painted primer over the top of the original lacquer paint. He said he got the car out of a field in Ohio. From my experiences with this car, I believe it sat in that field for 10 years (a literal rat nest in the air cleaner, weeds growing IN the frame, and the frame itself collapsing under its own weight from when I tried to jack it up because of corrosion). It had the original block and TH350 (I still have those) , and the infamous "lazy eye" headlights because of a leak in the reservoir.

It has been a long, hard, educational experience. In the spring of 1995, I saw a gorgeous, victory red '74-'77, and knew I wanted one. I was not a car guy - at the time, I knew they had engines, and wheels, and when you pushed the throttle, a car went. Being in college, the only one I could afford (and I could find) was a "project" that someone was getting rid of. I bought it over the Internet, sight unseen (it supposedly "ran"). I received the car in Dec 1997, and started trying to get it road worthy. For two years, it was the only car I had.

I know many don't appreciate the "Haynes" manuals - I found the manual invaluable with my lack of knowledge. I later picked up an AIM (which I still use). I dismantled the engine twice trying to figure out problems, and realized I had to get this car back into a full, beautiful state if I wanted to drive it. That was in '00 - and the start of a complete rebuild (including removing the frame, rebuilding chunks of the frame and shipping it off to a local company who could spray finishes inside the frame, and putting in a stroker with a Tremec TKO II). Basically, after getting the frame back, I spent the time building a brand new car right beside the pile of parts formerly known as the old one. If I couldn't find a part that I wanted, I rebuilt what I had. Sent it off to the painter in '06 and got it back in '09 (expensive learning experience there - we can leave that out of this conversation though) in non-perfect state, and began to finish putting the thing together. Married in '09, my wife has never seen it run. Prior to me having the car, it's never had both seats in it at the same time.

It is definitely not going to be a top-flighter - I've installed rack and pinion, and a series of other aftermarket parts that make this just the way I like it. It's taken me such a long time, but I am getting closer and closer. There is still a lot of work to do (I want to put in electric, pop-up headlights, and some other goodies for the engine [18*, super, etc]), but I also want to get it on the road. It seems that (once a month) I get an hour to turn a wrench on it, and I make just a little bit of progress. Carpets have been installed for the first time I've had the car, and with the seat belts, I can install quarter panels (still figuring out how they fit into the car - I don't know since I didn't remove them).

But, I'll get it figured out. I'm not even sure I have all of the interior panels - but I'll find out. Has it been a good experience? You bet - I wouldn't trade what I learned for anything. The real problem is that my wife now has things in the queue - the original 1973 Dodge Power Wagon her parents bought new (and she drove to high school), and later she wants me to help her build her own Co*ra kit car. The bug even transferred to my father who purchased a 1959 Pontiac Star Chief to rebuild. Now that I am getting close, I can taste victory, and it is tasting sweet!

Thursday, April 26, 2012

Interrupt-Driven Alarm on Hold - Polling Instead

BUG REPORTS :

Bug # 1 - Polling instead of Interruptions

After finding a bug in the interrupt-driven alarm mechanism, that has been put on hold while the project progresses. The bug is this - with the basic circuit design, if ANY input is open, opening another one will NOT trigger the interrupt.  In lieu of the interrupt-based concepts, I implemented a simpler design that people would have used defacto - polling. In this case, the loop() function sleeps for 50ms, then manually calls the interrupt function, and continues this way indefinitely. Power savings were merely 10 milliamp (very paltry considering the overall design), and simpler is always better. As a result, it's now a polling-based alarm mechanism, and functions well. Code is available at http://svn.silverhawk.net/files/polling_alarm.ino.

Bug # 2 - DD-WRT Router, Static Lease, and Arduino Static Config

This one isn't a bug with the Arduino, but a bug with the system.  In configuring the network, I used my current Asus device with DD-WRT installed, and configured a static lease.  On the Arduino side, I set the IP address statically (no DHCP).  The problem came about after realizing the Arduino could not talk to the outside world and running some iptables routing commands on the DD-WRT :

iptables -t mangle -A POSTROUTING -d 10.1.1.20 -j ROUTE --tee --gw 10.1.1.100
iptables -t mangle -A PREROUTING -s 10.1.1.20 -j ROUTE --tee --gw 10.1.1.100
The above two lines copied all network traffic to and from 10.1.1.20 to my workstation, 10.1.1.100, and then I slapped Wireshark on there.  I saw internal communication from the Arduino to the alarm server, but as soon as it tried to talk externally, nothing got out.  In fact, the Arduino threw "Who as 10.1.1.1?" ARP messages out to the gateway in order to communicate with it, but the gateway (the DD-WRT) simply ignored the Arduino.

Hmph!  I adjusted the static lease in the DD-WRT, and the thing was instantly communicating to the outside world.  It was live (open a door and my VPS server knew what happened).  Awesome!  So, let's run it with the PoE instead of USB power.....

Bug # 3 - Insufficient Power From PoE Device

After having successful communication to the outside world with everything in place (and the sensors installed in the doors), I tried running the Arduino over the PoE with long cables - and some sensors stopped working (they just reported back as "open").  When using the USB to power the device, it worked flawlessly.  Back to the PoE, and they stopped working again.

Conclusion : put the Arduino where it can be powered using a transformer, preferrably a 12v power source.

Next, I tried installing Linux motion after ZoneMinder failed to give me decent enough performance on the frame rate,  but that had poor performance, too, so I opted to hack some security flood lights that had LED indicators - might as well get dual purpose devices.  I start picking the theories for this apart in Motion Detection via Flood Light, and interfacing that to trigger camera recording, not just events.

Friday, April 13, 2012

tcpdump - when no other tools are available to filter a packet

I am fairly used to using tcpdump to capture network data.  However, I really gained the ability to review and understand the captures via Ethereal, later renamed to Wireshark (as it was spun off and the original trademark owner wanted to keep the trademark).  As a systems admin, I have to be familiar with reading the capture file from a network, but not always are the tools available.  As an example, I recently had to filter a packet capture for a partner as we did some troubleshooting, because policies require not exposing internal network structures, layouts, or organization.

So, I found myself one day sitting at my Linux box (without Wireshark installed) having the results of four tcpdumps from separate hosts in separate files.  I needed to combine the captures together and then filter out the partner-specific traffic.  Took me a while to remember the filtering options, so I thought I'd post the process here in the form of a journal - I can't even remember how old I am sometimes.

First, you have to combine the packet capture files together.  You cannot just cat the files together as you might have an incomplete packet from when you started.  The later versions of tcpdump actually have a tool to facilitate this :
mergecap -w silverhawk.cap host1.cap host2.cap host3.cap

After merging the captures together, you will want to remove the stuff not related to your current problem (to prevent the prying eyes from seeing inside of your network).  To do this, take the combined packet capture, and write another one, feeding it through a filter.  Your filters can be complex or simple.  I prefer the simple filters :

tcpdump -r silverhawk.cap -w silverhawk-filtered.cap -n "port 443"
The above would grab all of the TCP port 443 traffic, or the SSL-encrypted traffic (for HTTPS connections).  For most concerns, that alone would be enough (decrypting the SSL traffic would require the SSL certificate keys for the traffic streams you need to see).  However, if you have multiple connections to different port 443 traffic and you would prefer the partner doesn't see whom else you are connecting to, you can do it via IP address :

tcpdump -r silverhawk.cap -w silverhawk-filtered.cap -n "host 200.20.200.20"
Those are fairly simple filters, and should definitely assist in restricting who sees what.  I'd strongly suggest looking at those, and then adjusting as necessary (e.g. adding other, fancier filters by merging them together).

Tuesday, April 10, 2012

Apache, mod_perl, and an example output filter

Perhaps I should explain what an output filter really is.  Before apache serves any content to the web browser, it filters it, or allows different modules to alter it.  Here is an example situation :
You have been given a task where you have HTML pages that contain information for both the authenticated user and the user that hasn't authenticated.  You need to prevent the information intended for the user isn't served when a visitor hasn't authenticated.
How do you implement the above?  It's called an output filter.  For the above example, you will have to implement a simple module that checks a response page for a special tag, check for authentication, and if we are authenticated, allow the contents of the tag to be returned to the browser.  If not authenticated, remove the tag.  As an example, I'll use an HTML type tag :
<AuthenticatedOnly>
You are logged in!
</AuthenticatedOnly>
Please note, we aren't actually implementing the above.  It is merely a hypothetical situation.

There are two ways around creating an output filter.  You can create code that has to be compiled (my mod_template.so is a good example - it's written in C, and it wraps any HTML content in a template so that the whole site appears the same), or you can accomplish the same thing using mod_perl.  We'll take the latter one, as we won't need to install the compiler, just mod_perl.

To start out, we need to modify the Apache configuration to add it in.  Once you have it open, there are three things we need to do :

  1. Enable mod_perl, and insert an "internal" configuration to add our path.
  2. Create the filter module.
  3. Enable the configuration directive for the filter
To enable mod_perl :
LoadModule perl_module modules/mod_perl.so
PerlSwitches -Mlib=/opt/hacks/perl
With those two options in the Apache config, we also need to create our filter.  We'll create it in /opt/hacks/perl, obviously, since we told mod_perl to use that in the above example.


package MyOutputFilter;

use strict;
use warnings;

use base qw(Apache2::Filter);
use Apache2::RequestRec;
use APR::Brigade;
use Apache2::Const -compile => qw(OK);

sub handler : FilterRequestHandler {
  my $f = shift;
  my $bucketBrigade = shift;
  my $r = $f->r;
  my $uri = $r->uri();
  my $contentType = $r->headers_out->get("Content-Type");
  if ($contentType eq 'text/xml') {
    my $content = '';
    my $contentLength = $bucketBrigade->flatten($content);

    # modify $content here

    $f->r->headers_out->set('Content-Length', length $content);
    $f->print($content);
  }
  return Apache2::Const::OK;
}
This is a very simple filter - it does absolutely nothing, but should give you an idea of how a filter works. I'll break things down a little bit at a time. The first line is a good key - it's the name we use later when we enable our specific module. After the name, we have a few libraries we use, and then we must declare our handler function, since that is what mod_perl will call. Ours is the line :
sub handler : FilterRequestHandler {
The contents of that function define what we are doing.

Note that I have grabbed a "bucket brigade". A bucket brigade in apache is a pretty novel idea. Remember the stories about people putting out fires by passing buckets of water between each other? This is the same thing, but the "people" are apache modules, and the "buckets" aren't water, but response data.

In the above example, I call "flatten" on the bucket brigade - for another project, I needed to have ALL of the data at the same time, so it needed to be flattened into one "bucket".  If you work with large data, do NOT flatten them - you can cause Apache to use up free memory!  That assigned the contents of the response into the $content variable, which I can then modify.

After I am done with the data, I can pass it on to the next filter by calling $f->print($content);.  Note that I am also re-setting the Content-Length header - if your browser respects that, and you change the data but don't change the header, you might end up with blank pages (Google Chrome is a good example browser that does that).  Congrats!  You now have a simple filter that you can start to enhance to do some pretty amazing things!

Apache - proxiing to simple proxy to allow viewing of authenticated data

In Apache, we can enable the proxy module to allow our web server to send a request to another location, and we serve the result.  To do this quickly, simply enable the proxy modules you need :
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_connect_module modules/mod_proxy_connect.so
LoadModule proxy_ftp_module modules/mod_proxy_ftp.so
LoadModule proxy_http_module modules/mod_proxy_http.so
Once enabled, you can then create the proxy section. Please note that the following configuration is for an example - do NOT use it without hardening who has access to your proxy!  The config :
<Proxy *>
Order deny,allow
Allow from all
</Proxy>
ProxyPass / http://www.google.com/
Again, note that this will create an open proxy, and that is bad if you don't control who can access what, because you will be the one left holding the bag if they request illegal material. You have been warned!

This works great for most things, but let's presume for a minue that you wanted to serve a page from an HTTPS connection, no matter how it's requested from our web server. We need to enable SSLProxyEngine. If we are using self-signed certificates from that location, you might have to also add "SSLProxyCheckPeerCN off", e.g. :
SSLProxyEngine On
SSLProxyCheckPeerCN off
Let's toss in two more criteria. First, let's presume that you want one subsection of your site to be served from the local filesystem.  We can disable the proxy for a location by adding another proxy pass :
ProxyPass /local/ !
Now, for the second part, let's presume we have a chunk of the proxy that requires authentication.  In this case, we don't want to present the real user with the password pop-up, so, how do we do this?  Web browsers utilize a basic authentication using a series of headers, WWW-Authenticate, and Authorization.  The WWW-Authenticate is a response header that triggers the browser to authenticate the request.  The browser pops up the username/password dialog box, and then uses that information to generate an Authorization header.

Since we really don't need to worry about the WWW-Authenticate header, we can simple inject a permanent Authorization header into the system.  It's done with the RequestHeader directive, similar to :
<Location /svn/trunk/>
RequestHeader set Authorization "Basic dXNlcm5hbWU6cGFzc3dvcmQ="
</Location>
Now, here is a problem. How do we know what to put with the Authorization header?  Looking at the value that follows Basic, it looks like a MIME Base64 encoding.  Feeding it through a simple perl command, we can decode it and find out that it is indeed a Base64 encoding in the format of "username:pass".  We can figure out what we need by installing the MIME::Base64 perl module and running :
[username@hostname ~]$ perl -MMIME::Base64 -e "print encode_base64('username:password');"
dXNlcm5hbWU6cGFzc3dvcmQ=
[username@hostname ~]$
This will inject an authorization header into the proxied request.  Hopefully, this little cheat sheet will be of assistance to someone.