Skip to content

Commit

Permalink
initial import
Browse files Browse the repository at this point in the history
  • Loading branch information
ajdiaz committed Oct 27, 2017
0 parents commit 4c50374
Show file tree
Hide file tree
Showing 8 changed files with 488 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
@@ -0,0 +1,4 @@
src/*.o
src/*.a
src/*.so
src/test
21 changes: 21 additions & 0 deletions COPYING
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2017 Andrés J. Díaz

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
131 changes: 131 additions & 0 deletions Makefile
@@ -0,0 +1,131 @@
# Copyright (c) 2008 Andrés J. Díaz <ajdiaz@connectical.com>
# This is a specific Makefile for C project. Distributed with
# NO WARRANTY as a part of larger project.

# There are the main configuration variables to make the project,
# any other variables in .mk files must be imported automatically.
# The USE and the ENABLED variables setup a lot of different values
# in the application source.
CC = gcc
INCLUDE = include
CFLAGS = -O2 -fPIC -ggdb -Wall -I$(INCLUDE)
SRCDIR = src
BINS = example.so
RM = rm -f
LD = ld
LDFLAGS =
ARFLAGS = rcsu
INSTALL = install
USE =
ENABLE = # ASSERT

-include config.mk

# Dependencies for each binary is setting using a variable called
# as output binary and contain a list of dependencies, for example
# for binary out the dependencies are one and two::
#
# out := one two

# example.so := example.o

# --- Do not touch below this line ---

ifndef _ARCH
_ARCH := $(shell uname -m)
export _ARCH
endif

ifndef LIBDIR
ifeq ($(_ARCH), x86_64)
LIBDIR := /lib64/
endif
ifeq ($(_ARCH), i386)
LIBDIR := /lib/
endif
endif

SRCS=$(shell find $(SRCDIR) -name '*.c')
OBJS=$(SRCS:.c=.o)
ALLS=$(OBJS) $(addprefix $(SRCDIR)/,$(BINS))

$(foreach name_,$(wildcard *.mk),$(eval include $(name_)))
$(foreach name_,$(USE),$(eval CFLAGS=$(CFLAGS) -DUSE_$(name_)))
$(foreach name_,$(ENABLE),$(eval CFLAGS=$(CFLAGS) -DENABLE_$(name_)))

all: $(addprefix $(SRCDIR)/,$(BINS))

define auto-dep
ifneq ($(suffix $1),.a)
ifeq ($(suffix $1),.so)
$$(SRCDIR)/$1: LDFLAGS += -shared
else
$$(SRCDIR)/$1: CFLAGS += $(LDFLAGS)
endif
else
$$(SRCDIR)/$1: CFLAGS := $(CFLAGS) $$(CFLAGS_$1)
endif
$$(SRCDIR)/$1: $(addprefix $(SRCDIR)/,$($1))
ifneq ($(suffix $1),.a)
ifeq ($(suffix $1),.so)
@echo "$$(CC) $$@"
@$(CC) $(CFLAGS) -fPIC -shared -Wl,-soname,$1 $(LDFLAGS) $$(LDFLAGS_$1) -o $$@ $$(addprefix $$(SRCDIR)/,$$($1))
else
@echo "$$(CC) $$@"
@$(CC) $(CFLAGS) $(LDFLAGS) $$(LDFLAGS_$1) -o $$@ $$(addprefix $$(SRCDIR)/,$$($1))
endif
else
@echo "$$(AR) $$@"
@$(AR) $(ARFLAGS) $$@ $$(addprefix $$(SRCDIR)/,$$($1))
endif
ifneq ($1, test)
install_$1: $$(SRCDIR)/$1
ifneq ($(suffix $1),.a)
ifeq ($(suffix $1),.so)
@echo "$$(INSTALL) $$<"
@$(INSTALL) -s -D $$< $(DESTDIR)$$(LIBDIR)$$(notdir $$<)
else
@echo "$$(INSTALL) $$<"
@$(INSTALL) -s -D $$< $(DESTDIR)$$(LIBDIR)$$(notdir $$<)
endif
else
@echo "$$(INSTALL) $$<"
@$(INSTALL) -s -D $$< $(DESTDIR)$$(BINDIR)$$(notdir $$<)
endif
uninstall_$1: $(DESTDIR)$$(LIBDIR)$$(notdir $1)
ifneq ($(suffix $1),.a)
ifeq ($(suffix $1),.so)
@echo "$$(RM) $$<"
@$(RM) $(DESTDIR)$$(LIBDIR)$$(notdir $$<)
else
@echo "$$(RM) $$<"
@$(RM) $(DESTDIR)$$(LIBDIR)$$(notdir $$<)
endif
else
@echo "$$(RM) $$<"
@$(RM) $(DESTDIR)$$(BINDIR)$$(notdir $$<)
endif
endif
endef

$(foreach name_,$(BINS),$(eval $(call auto-dep,$(name_))))

.c.o:
@echo "$(CC) $@"
@$(CC) $(CFLAGS) -c -o $@ $<

clean:
@echo "$(RM) $(ALLS)"
@$(RM) $(ALLS)

tags:
@which ctags 2>&1 >/dev/null && \
ctags --recurse=yes $(SRCS) || \
echo "Sorry :( Tags not available." >&2

install: $(addprefix install_, $(BINS))

uninstall: $(addprefix uninstall_, $(BINS))

test: $(SRCDIR)/test
@$(SRCDIR)/test && echo 'Test OK'
54 changes: 54 additions & 0 deletions README
@@ -0,0 +1,54 @@
libdothosts.so
==============

The dothosts library is a LD_PRELOAD based library which allows you to
have a user based hosts file.

The library will catch all calls to gethostbyname(3), gethostbyname2(3),
getaddrinfo(3),gethostbyname_r(3) and gethostbyname2_r(3) and perform
a search of the requested host in a local (i.e. a file into home user
directory) to resolve the hostname. If not found in this file, then proceed
with the regular resolv mechanism.

---- Install ---------------------------------------------------------------

make && make test && make install

If you want to use the 'dothosts' helper, just copy the script to your path.
The make install proccess will not copy helper script.

---- Usage -----------------------------------------------------------------

To use dothosts just type:

export LD_PRELOAD=path/to/libdothosts.so

And then new applications will use ~/.hosts file as /etc/hosts (the original
/etc/hosts will be also used if host does not match in ~/.hosts).

Aditionally, environment variable DOTHOSTS will use to change the path of
local .hosts file, for exaple:

export DOTHOSTS=~/.config/hosts

You can use the dothost utility to run an application with library loaded,
in the form:

dothosts wget http://test

---- Limitations ----------------------------------------------------------

- Only works for programs that use glibc calls to resolve hostnames.
- No working for SUID programs for security reasons.

---- Development ----------------------------------------------------------

For bug report please fill a issue in the github project [1], or send an
email to ajdiaz+dothosts at ajdiaz.me

[1] https://github.com/ajdiaz/dothosts/issues

Any pull-request is also welcome.


Happy Hacking!
8 changes: 8 additions & 0 deletions config.mk
@@ -0,0 +1,8 @@
BINS = libdothosts.so test
LDFLAGS = -ldl

libdothosts.so := dothosts.o

CFLAGS_test = -DUSE_TEST
test := test.o

4 changes: 4 additions & 0 deletions dothosts
@@ -0,0 +1,4 @@
#! /bin/sh
# libdothosts.so helper
export LD_PRELOAD=/usr/local/lib/libdothosts.so
exec "$@"
146 changes: 146 additions & 0 deletions src/dothosts.c
@@ -0,0 +1,146 @@
/*
* dothosts.c
* Copyright (C) 2017 Andres J. Diaz <ajdiaz@ajdiaz.me>
*
* Distributed under terms of the MIT license.
*/

#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <dlfcn.h>

#define _errexit(_s) {perror((_s)); exit(1);}


static char *
parse_host_line(char *hosts_line, const char *hname)
{
const char *delim = " \t\n\r";
char *r = NULL;
char *ret = NULL;
char *token = NULL;

/* Replace comment by 0x00 */
if ((r = index(hosts_line, '#')))
*r = '\0';

ret = strtok_r(hosts_line, delim, &r); /* ip addr */
while ((token = strtok_r(NULL, delim, &r))) {
if (!strcmp(hname, token))
return ret;
}
return NULL;
}


static char *
lookup(const char *hname)
{
FILE *fp = NULL;
char *env_file = NULL;
char *line = NULL;
char *ret = NULL;
size_t size;

if (hname == NULL)
return NULL;

if ((env_file = getenv("DOTHOSTS")) == NULL)
{
/* No DOTHOSTS variable, composing from HOME */
if ((env_file = getenv("HOME")) == NULL)
env_file = strdup("/.hosts");
else
{
if ((env_file = strdup(env_file)) == NULL)
_errexit("Not enough memory");

if ((env_file = (char *) realloc(env_file, strlen(env_file) + 8)) == NULL)
_errexit("Not enough memory");

(void) strcat(env_file, "/.hosts");
}
}
else
env_file = strdup(env_file);


if ((fp = fopen(env_file, "r")) == NULL)
{
free(env_file);
_errexit("File not found");
}

while (getline(&line, &size, fp) >= 0)
{
if (line == NULL || *line == '\0')
continue;

if ((ret = parse_host_line(line, hname)) != NULL)
goto finish;
}

finish:
fclose(fp);
free(env_file);

if (ret != NULL)
ret = strdup(ret);

free(line); /* free after strdup */
return ret;
}

/* Override library calls (note that some struct points are casting to
* void pointers to avoid includes */

#ifndef USE_TEST
#define _lcall_override(_r, _f, _n, ...) { \
_r ret = (_r)0; char *n = NULL; \
char *p = (char *)(_n); \
_r (*orig)() = dlsym(RTLD_NEXT, #_f); \
if ((n = lookup(_n)) != NULL) p = n; \
ret = (_r) orig(p, ## __VA_ARGS__); \
if (n != NULL) free(n); \
return ret; }


int
getaddrinfo(const char *node, const char *service,
const void *hints, void *res)
{
_lcall_override(int, getaddrinfo, node, service, hints, res);
}

void *
gethostbyname(const char *name)
{
_lcall_override(void *, gethostbyname, name);
}

void *
gethostbyname2(const char *name, int af)
{
_lcall_override(void *, gethostbyname2, name, af);
}

int
gethostbyname_r(const char *name, void *ret, char *buf,
size_t buflen, void *result, int *h_errnop)
{
_lcall_override(int, gethostbyname_r, name, ret, buf,
buflen, result, h_errnop);
}

int
gethostbyname2_r(const char *name, int af,
void *ret, char *buf, size_t buflen,
void *result, int *h_errnop)
{
_lcall_override(int, gethostbyname2_r, name, af, ret, buf, buflen,
result, h_errnop);
}
#endif

0 comments on commit 4c50374

Please sign in to comment.