#define __FORCE_GLIBC
|
#define __FORCE_GLIBC
|
#include <features.h>
|
#include <features.h>
|
#include <stdio.h>
|
#include <stdio.h>
|
#include <string.h>
|
#include <string.h>
|
#include <errno.h>
|
#include <errno.h>
|
#include <sys/param.h>
|
#include <sys/param.h>
|
#include <netinet/in.h>
|
#include <netinet/in.h>
|
#include <netdb.h>
|
#include <netdb.h>
|
#include <fcntl.h>
|
#include <fcntl.h>
|
#include <unistd.h>
|
#include <unistd.h>
|
|
|
|
|
#define HOSTID "/etc/hostid"
|
#define HOSTID "/etc/hostid"
|
|
|
int sethostid(long int new_id)
|
int sethostid(long int new_id)
|
{
|
{
|
int fd;
|
int fd;
|
int ret;
|
int ret;
|
|
|
if (geteuid() || getuid()) return __set_errno(EPERM);
|
if (geteuid() || getuid()) return __set_errno(EPERM);
|
if ((fd=open(HOSTID,O_CREAT|O_WRONLY,0644))<0) return -1;
|
if ((fd=open(HOSTID,O_CREAT|O_WRONLY,0644))<0) return -1;
|
ret = write(fd,(void *)&new_id,sizeof(new_id)) == sizeof(new_id)
|
ret = write(fd,(void *)&new_id,sizeof(new_id)) == sizeof(new_id)
|
? 0 : -1;
|
? 0 : -1;
|
close (fd);
|
close (fd);
|
return ret;
|
return ret;
|
}
|
}
|
|
|
long int gethostid(void)
|
long int gethostid(void)
|
{
|
{
|
char host[MAXHOSTNAMELEN + 1];
|
char host[MAXHOSTNAMELEN + 1];
|
int fd, id;
|
int fd, id;
|
|
|
/* If hostid was already set the we can return that value.
|
/* If hostid was already set the we can return that value.
|
* It is not an error if we cannot read this file. It is not even an
|
* It is not an error if we cannot read this file. It is not even an
|
* error if we cannot read all the bytes, we just carry on trying...
|
* error if we cannot read all the bytes, we just carry on trying...
|
*/
|
*/
|
if ((fd=open(HOSTID,O_RDONLY))>=0 && read(fd,(void *)&id,sizeof(id)))
|
if ((fd=open(HOSTID,O_RDONLY))>=0 && read(fd,(void *)&id,sizeof(id)))
|
{
|
{
|
close (fd);
|
close (fd);
|
return id;
|
return id;
|
}
|
}
|
if (fd >= 0) close (fd);
|
if (fd >= 0) close (fd);
|
|
|
/* Try some methods of returning a unique 32 bit id. Clearly IP
|
/* Try some methods of returning a unique 32 bit id. Clearly IP
|
* numbers, if on the internet, will have a unique address. If they
|
* numbers, if on the internet, will have a unique address. If they
|
* are not on the internet then we can return 0 which means they should
|
* are not on the internet then we can return 0 which means they should
|
* really set this number via a sethostid() call. If their hostname
|
* really set this number via a sethostid() call. If their hostname
|
* returns the loopback number (i.e. if they have put their hostname
|
* returns the loopback number (i.e. if they have put their hostname
|
* in the /etc/hosts file with 127.0.0.1) then all such hosts will
|
* in the /etc/hosts file with 127.0.0.1) then all such hosts will
|
* have a non-unique hostid, but it doesn't matter anyway and
|
* have a non-unique hostid, but it doesn't matter anyway and
|
* gethostid() will return a non zero number without the need for
|
* gethostid() will return a non zero number without the need for
|
* setting one anyway.
|
* setting one anyway.
|
* Mitch
|
* Mitch
|
*/
|
*/
|
if (gethostname(host,MAXHOSTNAMELEN)>=0 && *host) {
|
if (gethostname(host,MAXHOSTNAMELEN)>=0 && *host) {
|
struct hostent *hp;
|
struct hostent *hp;
|
struct in_addr in;
|
struct in_addr in;
|
|
|
if ((hp = gethostbyname(host)) == (struct hostent *)NULL)
|
if ((hp = gethostbyname(host)) == (struct hostent *)NULL)
|
|
|
/* This is not a error if we get here, as all it means is that
|
/* This is not a error if we get here, as all it means is that
|
* this host is not on a network and/or they have not
|
* this host is not on a network and/or they have not
|
* configured their network properly. So we return the unset
|
* configured their network properly. So we return the unset
|
* hostid which should be 0, meaning that they should set it !!
|
* hostid which should be 0, meaning that they should set it !!
|
*/
|
*/
|
return 0;
|
return 0;
|
else {
|
else {
|
memcpy((char *) &in, (char *) hp->h_addr, hp->h_length);
|
memcpy((char *) &in, (char *) hp->h_addr, hp->h_length);
|
|
|
/* Just so it doesn't look exactly like the IP addr */
|
/* Just so it doesn't look exactly like the IP addr */
|
return(in.s_addr<<16|in.s_addr>>16);
|
return(in.s_addr<<16|in.s_addr>>16);
|
}
|
}
|
}
|
}
|
else return 0;
|
else return 0;
|
|
|
}
|
}
|
|
|