Networker

Re: [Networker] 7.1.3 client DNS checks and multihomed clients

2005-08-21 22:27:29
Subject: Re: [Networker] 7.1.3 client DNS checks and multihomed clients
From: Jason Koelker <jkoelker AT RACKSPACE DOT COM>
To: NETWORKER AT LISTSERV.TEMPLE DOT EDU
Date: Sun, 21 Aug 2005 21:23:38 -0500
> I do want the code, bugs and all!  It will probably save me a whole bunch
> of time down the road.
> 
> Thanks again for your response, it was extremely helpful!
> 
> Tim

Here is the code (the read_config() function was based off one found on
the net, I didn't think this would ever be going out so I didnt'
document its source, so appologies to the dude who wrote it for not
giving appropriate credit):

#include <unistd.h>
#include <string.h>
#include <stdio.h>
#define RSHOSTFILE "/etc/rs.hostname"

int read_config( char * , char [] );
int gethostname( char *, size_t );

#ifndef GOOD
        #define GOOD 0
#endif
#ifndef BAD
        #define BAD -1
#endif
#ifndef UGLY
        #define UGLY -2
#endif

/* BAD == file error, UGLY == bad user parm                    */

int read_config( char *keyword , char value[] )
{
        static char str[80];
        int len;
        FILE * _file;


        if( keyword == NULL ) return (BAD);

        len = strlen(keyword);

        if( len > 77) return(UGLY);


        if( RSHOSTFILE )
        {
                _file = fopen(RSHOSTFILE, "r");
                if (_file == NULL) return(BAD);
        }
        else
                return(UGLY);

        if( fseek(_file, 0, SEEK_SET) )
        {
                fclose(_file);
                return(BAD);
        }

        for(;;)
        {
                fgets(str, 80, _file);
                if( ferror(_file) || feof(_file) ) return(UGLY);
                len = strlen(str);

                if( strncmp(keyword, str, strlen(keyword)) == 0 )
                {
                                if (str[len - 1] == '\n') str[--len] =
0;
                                sprintf(value, "%s",
&str[strlen(keyword)+1] ); break;
                }
        }

        fclose(_file);

        return 0;
}

int gethostname( char *name, size_t len )
{
        char value[80];
        if( read_config("RSHOSTNAME", value) == 0 )
        {
                snprintf(name, len, value);
        }
        else
                snprintf(name, len, "error");

        return 0;
}

Compile this with:
$ gcc -rdynamic -c RShostname.c
$ gcc -rdynamic -shared -o RShostname.so RShostname.o

Then create /etc/rs.hostname or whatever you set the '#define
RSHOSTFILE' to and put in 'RSHOSTNAME=mynewname' to have the library
return mynewname as the hostname.

You can then start networker with:
# LD_PRELOAD=/path/to/RShostname.so nsrexecd
 
or just put 'LD_PRELOAD=/path/to/RShostname.so' at the top of the
networker startup script.  Then you can verify that it sees the new
name:

# nsradmin -s localhost -p nsrexec
nsradmin> show hostname
nsradmin> print type: nsrla
                    hostname: mynewname;
nsradmin>

If something goes wrong, or it can't find/read the file it just returns
'error' as the hostname, probly would be better to actually return the
real hostname, but, ehh, it was a quick hack ;).

Have a good one,

Jason

To sign off this list, send email to listserv AT listserv.temple DOT edu and 
type "signoff networker" in the
body of the email. Please write to networker-request AT listserv.temple DOT edu 
if you have any problems
wit this list. You can access the archives at 
http://listserv.temple.edu/archives/networker.html or
via RSS at http://listserv.temple.edu/cgi-bin/wa?RSS&L=NETWORKER