implementing my own strdup

While chatting on IRC last night I entered conversation about strdup(), a C function I had never used or encountered. I was then challenged to write my own. Checking the man page had me thinking about a for loop to iterate the data from one place to another, however, the genious Maxlor then pointed me to memcpy() (another function I had never used.)

Two minutes later I had my own version of strdup.

char * mystrdup( const char * string )
{
    char * p;
    if ( (p = malloc(strlen(string)) ) == NULL )
        return NULL;

    return memcpy(p, string, strlen(string));
}

Quite interesting to think through how some functions you use (or don’t use) are implemented.

udev interface naming

Following a hardware failure on a server, it seems that simply swapping the disk to a similar hardware system is not as easy as I had thought. It would appear that udev attempts to keep a permanant record of the device name (eg. eth0) and the MAC address associated with it, resulting in device names such as:

eth1_rename

The file containing these associations is:

/etc/udev/rules.d/70-persistent-net.rules

By simply commenting out the old MAC address entries in that file and ensuring the new ones have the names correctly associated, all worked fine again. Example entries in the file include:

# PCI device 0x11ab:0x4320 (skge)
SUBSYSTEM=="net", DRIVERS=="?*", ATTR{address}=="00:18:f3:68:42:78", ATTR{type}=="1", NAME="eth0"

# PCI device 0x11ab:0x4364 (sky2)
SUBSYSTEM=="net", DRIVERS=="?*", ATTR{address}=="00:18:f3:68:80:98", ATTR{type}=="1", NAME="eth1"

recursion power of

A book I was reading suggested I write a recursive function that calculated a power of a number. Here’s my attempt – comments and suggestions welcome:

double powerup( double num, double powr)
{
    if ( powr < 0 || num < 0 )
        return 0;

    if ( powr == 1 )
        return num;

    powr--;
    return num * powerup(num, powr);
}

gpg quick guide

GENERATE KEY PAIR

Generate personal key pair (private and public)

gpg --gen-key

You will be prompted for a password for private key

LISTING / VIEWING KEYS IN KEYRING

List public keys in your gpg keyring

gpg --list-keys [key_reference]

example output:

/home/jondoe/.gnupg/pubring.gpg
-----------------------------
pub   1024D/1A2B3C4D 2013-09-10
uid                  Jon Doe (a user) <jon.doe@example.com>
sub   2048g/F5E4D3C2 2013-09-10

The “1A2B3C4D” is the key reference for the key listed above. The appropriate key reference should be used where any example on this page writes “key_reference”

List secret keys in your gig keyring

gpg --list-secret-keys

PUBLIC KEYS

Export a public key to a file ‘public.key’

gpg --export -a key_reference > public.key

Import a public key in the file ‘public.key’

gpg --import public.key

Delete a public key

gpg --delete-key -a key_reference

NOTE – if the public key has a private key in the keychain it cannot be deleted until the private key is deleted

PRIVATE KEYS

Export a private key

gpg --export-secret-key -a key_reference > private.key

Import a private key in file ‘private.key’

gpg --allow-secret-key-import --import private.key

Delete a private key

gpg --delete-secret-key -a key_reference

ENCRYPTING FILES

Encrypt a file with a public key

gpg -e -r key_reference file.ext

NOTE – a new file file.ext.gpg is created, but the original file.ext still exists and needs to be deleted manually if so wished

DECRYPT FILES

You will be prompted for the password for the private key related to the public key used for encryption. If you do not have the private key you cannot decrypt the file

gpg -d file.ext.gpg > file.ext

open_basedir restriction in effect

PHP threw up this error today

PHP Warning:  Unknown: open_basedir restriction in effect. File(/foo/index.php) is not within the allowed path(s): (/bar/:/tmp/:/usr/share/pear/)

Seems open_basedir is set in php.ini, however it can also be set on a directory by directory basis within apache as follows:

Alias /foo /somepath/filesystem
<Directory /somepath/filesystem>
    php_admin_value open_basedir /somepath/filesystem
</Directory>

gpg-agent pinentry

Sometimes, the default setting of the gpg-agent is to use the gtk version of the pinentry program, however this may not be desirable on headless servers.

It is possible to alter this behaviour by adding an entry to the file ~/.gnupg/gpg-agent.conf (create it if required) for example:

# PIN entry program
pinentry-program /usr/bin/pinentry-curses