Marius van Witzenburg We fight for our survival, we fight!

11Nov/110

Apache Error Codes

Posted by mariusvw

With the function below you can add your own custom actions to error statuses of Apache.

Lets say you want a custom 404 error page. You could redirect a user upon a 404 error or show a complete different layout.

/**
 * HTTP Protocol defined status codes
 * @param int $num
 */
function HTTPStatus($num) {
   static $http = array (
       100 => "HTTP/1.1 100 Continue",
       101 => "HTTP/1.1 101 Switching Protocols",
       200 => "HTTP/1.1 200 OK",
       201 => "HTTP/1.1 201 Created",
       202 => "HTTP/1.1 202 Accepted",
       203 => "HTTP/1.1 203 Non-Authoritative Information",
       204 => "HTTP/1.1 204 No Content",
       205 => "HTTP/1.1 205 Reset Content",
       206 => "HTTP/1.1 206 Partial Content",
       300 => "HTTP/1.1 300 Multiple Choices",
       301 => "HTTP/1.1 301 Moved Permanently",
       302 => "HTTP/1.1 302 Found",
       303 => "HTTP/1.1 303 See Other",
       304 => "HTTP/1.1 304 Not Modified",
       305 => "HTTP/1.1 305 Use Proxy",
       307 => "HTTP/1.1 307 Temporary Redirect",
       400 => "HTTP/1.1 400 Bad Request",
       401 => "HTTP/1.1 401 Unauthorized",
       402 => "HTTP/1.1 402 Payment Required",
       403 => "HTTP/1.1 403 Forbidden",
       404 => "HTTP/1.1 404 Not Found",
       405 => "HTTP/1.1 405 Method Not Allowed",
       406 => "HTTP/1.1 406 Not Acceptable",
       407 => "HTTP/1.1 407 Proxy Authentication Required",
       408 => "HTTP/1.1 408 Request Time-out",
       409 => "HTTP/1.1 409 Conflict",
       410 => "HTTP/1.1 410 Gone",
       411 => "HTTP/1.1 411 Length Required",
       412 => "HTTP/1.1 412 Precondition Failed",
       413 => "HTTP/1.1 413 Request Entity Too Large",
       414 => "HTTP/1.1 414 Request-URI Too Large",
       415 => "HTTP/1.1 415 Unsupported Media Type",
       416 => "HTTP/1.1 416 Requested range not satisfiable",
       417 => "HTTP/1.1 417 Expectation Failed",
       500 => "HTTP/1.1 500 Internal Server Error",
       501 => "HTTP/1.1 501 Not Implemented",
       502 => "HTTP/1.1 502 Bad Gateway",
       503 => "HTTP/1.1 503 Service Unavailable",
       504 => "HTTP/1.1 504 Gateway Time-out"
   );
 
   header($http[$num]);
}
?>

You can download this 404 error page example to play with.

Geëtiketeerd als: , , , Geen reacties
26Sep/110

How to easy CVS update your SRC and Ports tree with a simple Bash script (v2)

Posted by mariusvw

This is an update to this post: How to easy CVS update your SRC and Ports tree with a simple Bash script

I adjusted the script to use the local csup version of the FreeBSD World system and moved the cvsup files to /etc.

First install a simple tool to get the fastest cvs server

pkg_add -r fastest_cvsup

Or from Ports (if you have the collection already):

cd /usr/ports/sysutils/fastest_cvsup/
make install clean

Setup config and binary to easy update the repository for SRC and PORTS.

mkdir /etc/cvsup
cd /usr/share/examples/cvsup
cp stable-supfile /etc/cvsup
cp ports-supfile /etc/cvsup
cp doc-supfile /etc/cvsup
cd /etc/cvsup

Edit stable-supfile and change:

*default release=cvs tag=RELENG_8_2

Set this to your release version.

Edit /usr/local/sbin/runcvsup and add:

#!/bin/sh
arg=$@
sType="tld,nl" # Can be: all / tld / tld,nl,de,fr,us
if SERVER=`/usr/local/bin/fastest_cvsup -q -c $sType`; then
    echo "Using server: ${SERVER}"
    case "$arg" in
    "stable")
        echo "Updating: stable"
        /usr/bin/csup -L 2 -g -z -h ${SERVER} /etc/cvsup/stable-supfile
    ;;
    "ports")
        echo "Updating: ports"
        /usr/bin/csup -L 2 -g -z -h ${SERVER} /etc/cvsup/ports-supfile
    ;;
    "doc")  
        echo "Updating: doc"
        /usr/bin/csup -L 2 -g -z -h ${SERVER} /etc/cvsup/doc-supfile
    ;;
    *)
        echo "Updating: all"
        /usr/bin/csup -L 2 -g -z -h ${SERVER} /etc/cvsup/stable-supfile
        /usr/bin/csup -L 2 -g -z -h ${SERVER} /etc/cvsup/ports-supfile
        /usr/bin/csup -L 2 -g -z -h ${SERVER} /etc/cvsup/doc-supfile
    ;;
    esac    
fi

Set the right permissions:

chmod 700 /usr/local/sbin/runcvsup

Update everything:

runcvsup
Geëtiketeerd als: , , , , , Geen reacties
17Jul/110

How to check if a key exists in an array with in_array for Bash

Posted by mariusvw

When working with Bash it might become handy to have a way to check if a record exists in an array. In PHP you have in_array for this... The below code adds a function similar to the PHP variant.

function in_array() {
  local x
  ENTRY=$1
  shift 1
  ARRAY=( "$@" )
  [ -z "${ARRAY}" ] && return 1
  [ -z "${ENTRY}" ] && return 1
  for x in ${ARRAY[@]}; do
    [ "${x}" == "${ENTRY}" ] && return 0
  done
  return 1
}
 
MASTER=()
CURRENT=()
FIRST=1
for SERVER in ${SERVERS}; do
  # collect all builds from server and populate CURRENT list
  COMMAND="${LS} -1fd ${WEBROOT}/${SITE}.*"
  BUILDS=`${SSH} ${SSHOPTS} root@${SERVER} "${COMMAND}"`
  for BUILD in ${BUILDS}; do
    CURRENT=( ${CURRENT[@]-} ${BUILD} )
  done
 
  # if this is our first time around, copy CURRENT to MASTER
  if [ ${FIRST} -eq 1 ]; then
    MASTER=( ${CURRENT[@]} )
    FIRST=0
  fi
 
  # now we do a compare between MASTER and CURRENT to see what builds
  # are common
  INTERSECT=()
  for ENTRY in ${CURRENT[@]}; do
    in_array "${ENTRY}" "${MASTER[@]}"
    RET=$?
    if [ "${RET}" -eq 0 ]; then
      INTERSECT=( ${INTERSECT[@]-} ${ENTRY} )
    fi
  done
  MASTER=( ${INTERSECT[@]} )
 
  # clear the CURRENT array
  CURRENT=()
done

Source: http://mykospark.net/tag/in_array/

Geëtiketeerd als: , , , Geen reacties
17Jul/110

How to add Base64 encoding and decoding in C

Posted by mariusvw

I found this page on the web while searching for something else but I thought this might be useful to others :-)

Overview
libb64 is a library of ANSI C routines for fast encoding/decoding data into and from a base64-encoded format. C++ wrappers are included, as well as the source code for standalone encoding and decoding executables.

Base64 uses a subset of displayable ASCII characters, and is therefore a useful encoding for storing binary data in a text file, such as XML, or sending binary data over text-only email.

Usage

#include <b64/encode.h>
#include <iostream>
int main() {
	base64::encoder E;
	E.encode(std::cin, std::cout);
	return 0;
}

You can read more info and download the package on the url below:

http://libb64.sourceforge.net/

Geëtiketeerd als: , , , , Geen reacties
10Jul/110

How to update your FreeBSD kernel and World system

Posted by mariusvw

Auto install using freebsd-update

See: How to use freebsd-update to update your FreeBSD kernel and world system

Manual Update World / Kernel

Its advised to run this in Screen, you can install it from /usr/ports/sysutils/screen.

First you have to update the SRC and Ports tree, follow the manual on this page to complete this step.

Next is to configure the make.conf so it won't install things we don't want.
Be sure you check out man make.conf for some make optimalisation settings.

Here is an example that I use:

WITHOUT_X11=yes
NO_GAMES=yes
NO_X=yes
 
# make optimalization (-j4, 4 = optimal for 1 cpu)
MAKEOPTS="-j4 -B"

In case you have edited (which is a 100% chance you did) files of the world installation you might need to edit the mergemaster.rc file and add some settings before running mergemaster.

Check this page to view an example: How to configure mergemaster to merge configurations on FreeBSD.

These are the steps to install a fresh kernel and world system. The TMPDIR that I use is because I disabled the ability to execute files on the standard /tmp mount.

mkdir /tmp_world
setenv TMPDIR /tmp_world
cd /usr/src
make clean
make buildworld
make buildkernel
make installkernel
make installworld

Merge the world system:

mergemaster

Be sure you check out man mergemaster. For example people use the -p argument. This can be handy in some situations.

Remove temp dir:

unsetenv TMPDIR
rm -r /tmp_world

Short versions (might be unsafe):

# Kernel & World
mkdir /tmp_world ; setenv TMPDIR /tmp_world; cd /usr/src; make clean
make buildworld && make buildkernel && make installkernel && make installworld
mergemaster
unsetenv TMPDIR; rm -r /tmp_world
 
# Kernel-only
mkdir /tmp_world ; setenv TMPDIR /tmp_world; cd /usr/src; make clean
make buildkernel; make installkernel
unsetenv TMPDIR; rm -r /tmp_world

Remove builded objects, only after a release upgrade:

cd /usr/obj
chflags -R noschg *
rm -rf *

Custom KERNEL

cd /usr/src/sys/i386/conf/
cp GENERIC YOURKERNEL
echo "# Kernel configuration" >> /etc/make.conf
echo "KERNCONF=YOURKERNEL" >> /etc/make.conf

Compile the same as above, only leave out the buildworld and installworld.

10Jul/110

How to configure mergemaster to merge configurations on FreeBSD

Posted by mariusvw

Ignore files for upgrade, for example you could add /etc/motd here.

Simply edit the file /etc/mergemaster.rc and add the following line:

IGNORE_FILES='/etc/motd'

Another example:

# Automatically install files that do not exist on the system already
AUTO_INSTALL=yes
 
# Automatically upgrade files that have not been user modified
AUTO_UPGRADE=yes
 
# Preserve files that you replace
PRESERVE_FILES=yes
PRESERVE_FILES_DIR=/var/tmp/mergemaster/preserved-files-`date +%y%m%d-%H%M%S`
 
# Files to always avoid comparing (space separated)
IGNORE_FILES='/etc/motd /etc/rc.conf'

For more information check out the manual page.

10Jul/110

How to easy CVS update your SRC and Ports tree with a simple Bash script

Posted by mariusvw

Check this updated post: How to easy CVS update your SRC and Ports tree with a simple Bash script (v2)

Its advised to run this in Screen, you can install it from /usr/ports/sysutils/screen.

First install a simple tool to get the fastest cvs server

pkg_add -r fastest_cvsup

Or from Ports (if you have the collection already):

cd /usr/ports/sysutils/fastest_cvsup/
make install clean

Install CVSUP

pkg_add -r cvsup-without-gui

Or from Ports (if you have the collection already):

cd /usr/ports/net/cvsup-without-gui
make install clean

Setup config and binary to easy update the repository for SRC and PORTS.

mkdir /usr/local/etc/cvsup
cd /usr/share/examples/cvsup
cp stable-supfile /usr/local/etc/cvsup
cp ports-supfile /usr/local/etc/cvsup
cp doc-supfile /usr/local/etc/cvsup
cd /usr/local/etc/cvsup

Edit stable-supfile and change:

*default release=cvs tag=RELENG_7_1

Set this to your release version.

Edit /usr/local/etc/cvsup/runcvsup and add:

#!/bin/sh
arg=$@
sType="tld,nl" # Can be: all / tld / tld,nl,de,fr,us
if SERVER=`/usr/local/bin/fastest_cvsup -q -c $sType`
then
        echo "Using server: ${SERVER}"
        case "$arg" in
        "stable")
                echo "Updating: stable"
                /usr/local/bin/cvsup -L 2 -g -z -h ${SERVER} /usr/local/etc/cvsup/stable-supfile
        ;;
        "ports")
                echo "Updating: ports"
                /usr/local/bin/cvsup -L 2 -g -z -h ${SERVER} /usr/local/etc/cvsup/ports-supfile
        ;;
        "doc")
                echo "Updating: doc"
                /usr/local/bin/cvsup -L 2 -g -z -h ${SERVER} /usr/local/etc/cvsup/doc-supfile
        ;;
        *)
                echo "Updating: all"
                /usr/local/bin/cvsup -L 2 -g -z -h ${SERVER} /usr/local/etc/cvsup/stable-supfile
                /usr/local/bin/cvsup -L 2 -g -z -h ${SERVER} /usr/local/etc/cvsup/ports-supfile
                /usr/local/bin/cvsup -L 2 -g -z -h ${SERVER} /usr/local/etc/cvsup/doc-supfile
        ;;
        esac
fi

Set the right permissions:

chmod 700 runcvsup

Link the script so it can be reached.

ln -s /usr/local/etc/cvsup/runcvsup /usr/local/bin/runcvsup
rehash

Update everything:

runcvsup
Geëtiketeerd als: , , , , , Geen reacties
10Jul/110

How to use freebsd-update to update your FreeBSD kernel and world system

Posted by mariusvw

Information

http://www.freebsd.org/doc/en/books/handbook/updating-upgrading-freebsdupdate.html

Keep in mind, if you have a custom kernel that you need to upgrade your kernel manually!!!

Usage

The freebsd-update(8) utility supports binary upgrades of i386 and amd64 systems running earlier FreeBSD releases. Systems running 7.[01234]-RELEASE, 8.[01]-RELEASE, 8.2-BETA1, or 8.2-RC[123] can upgrade as follows:

# freebsd-update upgrade -r 8.2-RELEASE

During this process, FreeBSD Update may ask the user to help by merging some configuration files or by confirming that the automatically performed merging was done correctly.

# freebsd-update install

The system must be rebooted with the newly installed kernel before continuing.

# shutdown -r now

After rebooting, freebsd-update needs to be run again to install the new userland components:

# freebsd-update install

At this point, users of systems being upgraded from FreeBSD 7.4-RELEASE or earlier will be prompted by freebsd-update to rebuild all third-party applications (e.g., ports installed from the ports tree) due to updates in system libraries.

After updating installed third-party applications (and again, only if freebsd-update printed a message indicating that this was necessary), run freebsd-update again so that it can delete the old (no longer used) system libraries:

# freebsd-update install

Finally, reboot into 8.2-RELEASE:

# shutdown -r now
Geëtiketeerd als: , , , Geen reacties
10Jul/110

How to install and use pkg_cutleaves on FreeBSD

Posted by mariusvw

Installation

Install from ports collection.

cd /usr/ports/ports-mgmt/pkg_cutleaves/
make install clean

Usage

To list all leaves use:

pkg_cutleaves -lc

You can remove the leaves on a several ways, the simplest way is:

pkg_cutleaves

The program will ask what you want to do with the current leave.

KEEP IN MIND! THIS CAN SERIOUSLY HARM YOUR INSTALLATION!

Geëtiketeerd als: , , , , Geen reacties
10Jul/110

How to install and use portaudit on FreeBSD

Posted by mariusvw

Installation

Install portaudit from the ports collection.

cd /usr/ports/ports-mgmt/portaudit
make install clean

Usage

When you install or upgrade a port, portaudit will automatically check for vurnability problems.

To disable the check while doing a //make install// add:

DISABLE_VULNERABILITIES=1

Manual check:

portaudit -Fda

Portupgrade
It might happen that you want to upgrade a port which has vulnerabilities. You need to pass on the make argument like this:

portupgrade -m DISABLE_VULNERABILITIES=1 'php52*'
Page 1 of 812345678