Here's a little weekend trick for you :)
I ran into this while compiling the latest version of cfengine. If you've ever built it, you know that two of its prerequisites are having access to includes and library files from openssl and Berkely db during configure and compile time. We visited the one program with a heavy take on building the db package in a previous post.
So, with the assumption that you've already installed Berkeley db and openssl and are now ready to configure cfengine, the first hurdle is easy enough to get around. If your libraries and includes are in standard places (like /usr/lib and /usr/include) it isn't even a hurdle at all. However, if you've built your Berkeley db and openssl software into special directories, you'll need to pass at least one of these two options to configure:
host # ./configure --with-berkeleydb=/some/dir/db --with-openssl=/some/dir/openssl
That part's simple to figure out, because
host # ./configure --help
will tell you what flags you need to pass after you get the error during configuration. Of course, reading the INSTALL and/or README file first would help with that, too ;)
The second hurdle is a little less obvious, and may not happen to everyone. It has to do with how you compiled your Berkeley db and openssl source. If you compiled them to have shared libraries, you won't run into this issue. However, if you compiled them with static libraries, the cfengine "make" process will balk when it gets to the point that it call those libraries. You would see an error similar to:
Undefined error dlopen -- undefined symbol...blah, blah, blah
This is because it won't, or can't, deal with the static libraries you've compiled for db and openssl.
The good news is that you can get around this fairly easily by setting the following two variables in your shell and just restarting the "make."
host # BERKELEY_DB_LIB="/some/dir/db/lib/libdb-4.5.a -lrt"
host # OPENSSL_LIB_LIB="/some/dir/openssl/lib/libcrypto.a -ldl"
and then running make with the -e flag, which will give your environment variables (set above) precedence over variables set in the Makefile.
It's important to note that some systems (generally older ones, like Solaris 2.6) don't have the -lrt (librt) library. In these cases, just use -ldl for both the db and openssl evironment variables.
After that, it's all icing :)
Cheers,
, Mike
linux unix internet technology
Saturday, March 8, 2008
Problems Compiling CFEngine With Static Libraries
Friday, March 7, 2008
CC FLAG UPDATE - Building Solaris Packages Quickly - An Old Post Revisited
Hey There,
Updated Some More - Thank you for your emails :) I've updated this once more to hard-code the CC=gcc flag on the configure line. This db package's configure script appears not to pickup gcc more often than it does, so, unless you're using cc, it's best just to force gcc.
Now that we've just gotten through a marathon "how to" on creating Linux RPMS, I'd thought I'd look back at the old post I referenced at the beginning of the week regarding making your own Solaris packages.
That post was, and still is, theoretically sound. Although, looking back at it, I feel that it really lacked the kind of example work-up that would have made the instructions come back and hit home. So with reference to my original (admittedly dry) instruction on how to make your own Solaris pkg files, here's a quick example of the actual process of creating a package from compiling your software to the finished product. Hopefully this will not only flesh-out the original post, but also show how much simpler this is to do in reality than it is in theory :)
And, in an effort to make up for lost time and dispel any possibility of confusion, we're going to use a real freely-available source gzip that anyone can grab for free and walk through this on their own Solaris machine step-by-step, cut-and-paste. Here are the basic requirements in order to reproduce this on your own without having to do any extra work outside of the instructions posted:
1. Have gcc and make installed on your Solaris System.
2. Run Solaris on Sparc (not x_86 or Intel - although this should still work).
3. If possible, use the 64 bit version (As long as the "isainfo" or "isalist" commands return "sparcv9" somewhere in their output, you should be in the same boat I am as I do this compile and package creation. Again, this probably won't be an issue if you use 32 bit.
And that should be it. Now we just need to download the Berkeley DB 4.6.21 source gzipped tarball (available by clicking the name of the product on this line or here ;)
And off we go! I'm going to basically walk through this by example (what I'll actually type during the process) and leave out the tons of output from "configure" and "make," etc, so this doesn't get too hard to read. Hopefully, it will be painfully easy to understand :)
Step 1. Unpack and build your software (grab your Berkeley DB source now if you haven't already!
SPECIAL NOTE: This post has been updated to reflect the need to run configure from the build_unix subdirectory under the db-4.6.21 directory. You may also need to set CC=gcc as an option on your configure command line, but db should pick it up as long as it comes first in your PATH. Apologies for the typo.
host # mkdir /tmp/build
host # cp db-4.6.21.tar.gz /tmp/build/.
host # cd /tmp/build
host # gzip -d -c db-4.6.21.tar.gz|tar xvpf -
... <--- This will be shorthand for obligatory command output that doesn't make a difference to us here.
host # cd db-4.6.21
host # cd build_unix
host # ../dist/configure CC=gcc --prefix=/usr/local/db-4.6.21
...
host # make
...
host # make docdir=/usr/local/db-4.6.21/doc/4.6.21 install
...
host # cd /tmp/build
host # rm -r db-4.6.21
And we're done with the compile :)
Step 2. Create the Solaris pkg file from our installation!
host # cd /usr/local/db-4.6.21
host # find . -print|pkgproto >prototype
host # echo "i pkginfo" >>TEMPFILE
host # echo "i checkinstall" >>TEMPFILE
host # cat prototype >>TEMPFILE
host # mv TEMPFILE prototype
host # vi checkinstall <--- For the two files we need to edit, I'll just indent and put the contents of each below the lines where I use "vi" (my favorite editor). You can edit the file with any editor you want :)
#!/bin/sh
platform_should_be="sparc"
platform=`uname -p`
if [ ${platform} != ${platform_should_be} ]
then
echo "PROGRAM can only be installed on ${platform_should_be}"
exit 1
fi
exit
host # vi pkginfo
SUNW_PRODNAME="SunOS"
SUNW_PRODVERS="5.9"
SUNW_PKGTYPE="usr"
PKG="MTdb"
NAME="Berkely DB-4.6.21"
VERSION="4.6.21"
VENDOR="Open Source"
ARCH="sparc"
EMAIL="eggi@comcast.net"
CATEGORY="application"
BASEDIR=/usr/local/db-4.6.21
DESC="http://linuxshellaccount.blogspot.com DB-4.6.21"
PSTAMP="The Linux And Unix Menagerie"
CLASSES="none"
host # pkgmk -b `pwd` -d /tmp
...
host # pkgtrans -s /tmp /tmp/build/MTdb.pkg MTdb
...
host # rm -r /tmp/MTdb
And now you can move the /tmp/build/MTdb.pkg file anywhere on the file system you want. You should be able to utilize all the applicable "pkginfo" command options against it and it should install as easily (assuming you left it in /tmp/build) as this (of course, it won't be any harder if you install it from anywhere else ;)
host # pkgadd -d /tmp/build/MTdb.pkg
also
host # pkgrm MTdb <--- If you ever want to delete it from the package repository
Hope you enjoyed that whirlwind tour of a Solaris package (pkg) build in almost-real-time. Hopefully, also, it helps complement our previous post on creating Solaris packages by giving you a solid working example to compare against the theory!
Cheers,
, Mike
linux unix internet technology
Wednesday, December 19, 2007
Non-Maliciously Scanning For Open Network Ports
Generally, port scanning is looked at as a "Black Hat" activity. This isn't so much stereotyping, as a heavily weighed and considered generalization. Entire industries are built around the fact that most, if not all, networked servers (public and private) are under almost constant attack by some individual or group. It's for that reason that most folks, like myself, take pause before ever engaging in such activity, even if our motives are pure. Depending on your situation, the very fact that you do this might get you flagged.
On the flipside, however, is the obvious "need" to be able to check whether ports are open. Reasons range from making sure that services are up and available, to "White Hat" activity, like making sure that all of the network ports that you think are closed actually are; thus enabling you to detect security risks and eliminate them before someone else can take advantage.
The script I'm putting up today is a port scanner in its purest sense. It doesn't attempt to "investigate" the services running on any of the network ports it finds open, which is usually enough to keep you from getting threatening emails from your security department ;) The little reporting it does do on open network ports is variable, as it gets the information from the /etc/services file on the host from which it's being run (using the standard getservbyport call).
You call the script by whatever name you give it (I'll call it "scanner" because it makes sense and, maybe, I'm not feeling very original right now ;) and supply it with the IP Address of the target server and the highest port on it that you want to scan. The script assumes you want to start at port 1. You can change that very easily by modifying the $port variable. You also need to be root to run this script, as it creates sockets (using standard Berkeley Sockets code) and regular users don't have system permission to do that on Unix and Linux systems. If they do, something is horribly wrong :P
A sample run would look like this:
host # ./scanner 127.0.0.1 1000
Open : 80/tcp :
Open : 111/tcp : sunrpc
Open : 443/tcp :
host #
In the above example, it appears that we've got a regular and secure webserver running, as well as the sunrpc service. The reason that the description field on the left is empty for ports 80 and 443 is that there are no entries in the host's /etc/services file for those ports. Simply adding those entries in that file would make the program produce this on an exactly equal run:
host # ./scanner 127.0.0.1 1000
Open : 80/tcp : http
Open : 111/tcp : sunrpc
Open : 443/tcp : https
host #
Hopefully, you'll find this useful with regards to your completely-above-board administration activities :)
Cheers,
This work is licensed under a
Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License#!/bin/perl
#
# scanner - simple port scanner
# 2007 - Mike Golvach - eggi@comcast.net
#
# Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License
#
if ( $#ARGV != 1 ) {
print "Usage: $0 hostnameOrIP highPort\n";
exit(1);
}
use Socket;
$host = $ARGV[0];
$highport = $ARGV[1];
$port = 1;
OUTERLOOP: while ( $port <= $highport ) {
$| = 1;
$line = " ";
$tcp = 1;
INNERLOOP: while ($tcp >= 0) {
if ( $tcp ) {
$protocol = "tcp";
} else {
$protocol = "udp";
}
#print "${port}/$protocol... ";
if ( $port =~ /\D/) {
$port = getservbyname($port, $protocol);
}
unless ( $port ) {
print "No port : ${port}/$protocol\n";
if ( $tcp ) {
$tcp--;
next INNERLOOP;
} else {
$port++;
next OUTERLOOP;
}
}
$inet_address = inet_aton($host) || die "No host: ${host}!";
$port_address = sockaddr_in($port, $inet_address);
$protocol_num = getprotobyname('$protocol');
socket(SOCKET, PF_INET, SOCK_STREAM, $protocol_num) || die "Socket: $!";
unless ( connect(SOCKET, $port_address) ) {
if ( $tcp ) {
$tcp--;
next INNERLOOP;
} else {
$port++;
$tcp--;
next OUTERLOOP;
}
}
if ( $tcp ) {
send(SOCKET, $line, 'SOCK_STREAM') == length($line) || die "Cannot Send Message!: $!\n";
} else {
send(SOCKET, $line, 'SOCK_DGRAM', $protocol) == length($line) || die "Cannot Send Message!: $!\n";
}
close(SOCKET) || die "Close $!";
$service = getservbyport($port, $protocol);
print "Open : ${port}/$protocol : $service\n";
$port++;
$tcp--;
}
}
, Mike
linux unix internet technology
Sunday, November 11, 2007
Checking If A Mail Server Is An Open Relay Using Perl and Berkeley Sockets!
SPAM is common enough nowadays that you can probably find some version of this basic check somewhere on the web (probably a lot of places), but our intent here is to take a look at what's behind the pretty looking web pages and/or software, used in determining whether or not the mail server you're runnning (or any mail server, for that matter) is an open relay.
Below is a little script I put together using Perl (and its Socket module - based on Berkeley Sockets) that will do this check for you. In a future post, we'll explore Berkeley Sockets programming at a more granular level (as it's far outside the scope of this simple blog post), but for now check out the script below:
This work is licensed under a
Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License
#!/bin/perl
#
# 2007 - Mike Golvach - eggi@comcast.net
#
# Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License
#
use Socket;
$host = "whateverMailServerYouWantToCheck.xyz.com";
$relaydomain = "xyz.com";
$sender = "eggi@noOneWillEverPickThisDomain.com";
$adressee = "info@hopefullyThisMailServerIsNotInTheDomainWereTesting.zyx.com";
$protocol = "tcp";
$port = 25;
if ( $port =~ /\D/) {
$port = getservbyname($port, $protocol);
}
unless ( $port ) {
print "No port : ${port}/$protocol\n";
}
$inet_address = inet_aton($host) || die "No host: ${host}!";
$port_address = sockaddr_in($port, $inet_address);
$protocol_num = getprotobyname('$protocol');
socket(SOCKET, PF_INET, SOCK_STREAM, $protocol_num) || die "Socket: $!";
connect(SOCKET, $port_address) || die "Socket $!";
send(SOCKET, $line, 'SOCK_STREAM') == length($line) || die "Cannot Send Message!: $!\n";
print SOCKET "HELO $relaydomain\n";
print SOCKET "MAIL From: $sender\n";
print SOCKET "RCPT To: $adressee\n";
print SOCKET "DATA\n";
print SOCKET "Subject: If this didn't get rejected - you may be running an open relay!\n";
print SOCKET ".\n";
print SOCKET "quit\n";
close(SOCKET) || die "Close $!";
exit(0);
At a very basic level, there are just a few things (covered in the script above) that you need to do to quickly determine if a mail server is an open relay. First, make sure you're sending from a different domain than the mail server's domain. Second, connect to the mail server and attempt to send mail to another (different) domain. If this works, the mail server is acting as an open relay.
Of course, like I said, it might be easier to just go to any number of sites on the web and have that free service do the check for you. But, in any event, this way can be more fun and you can customize it to do whatever (hopefully, good and decent) things you want it to :)
, Mike
linux unix internet technology





