Skip to content

Commit ed01321

Browse files
committed
reuse ipmappings where possible. Documentation improvements
1 parent ef27232 commit ed01321

9 files changed

Lines changed: 94 additions & 32 deletions

File tree

.travis.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ language: c
22

33
os:
44
- linux
5-
- osx
65

76
compiler:
87
- clang

README.md

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -143,10 +143,11 @@ You can specify 'match modifiers' for a function. The config line will only be u
143143
```
144144
path perform match against first arg of the function. This is usually a file path, but for 'connect' and 'bind' it can be a URL
145145
basename peform a match against the basename (leading directory removed) of the first arg of the function
146-
family for 'connect', 'bind' and 'accept' this is the url type. It can be 'tcp6', 'tcp', 'udp', or 'unix'
147-
peer for 'connect' and 'accept' this is the remote host ip
148-
user match against username
149-
group match against groupname
146+
family for 'connect', 'bind' and 'accept' this is the url type. It can be 'ip4', 'ip6', 'net', or 'unix'. 'net' matches both 'ip4' and 'ip6'
147+
peer for 'connect' and 'accept' this is the remote host ip, extracted from 'path' which will be url
148+
port for 'connect' and 'bind' this will be the port to bind or connect to
149+
user match against username current process is running as
150+
group match against primary groupname current process is running as
150151
arg match if any arg in the programs arguments matches
151152
```
152153

@@ -281,9 +282,18 @@ Will map the hostname lookup to a false ipaddress in the form '0.0.0.x' When the
281282

282283
```
283284
gethostip ipmap
284-
connect family=tcp redirect socks:127.0.0.1:9090
285+
connect path=tcp:* redirect socks:127.0.0.1:9090
285286
```
286287

288+
It's a good idea to at least specify `path=tcp:` to prevent trying to redirect, say, a connection for syslog logging to socks. You can be more specific if you only want to map certain hosts. e.g. if local hosts are in the domain '.local' then we might use:
289+
290+
```
291+
gethostip path!=*.local ipmap
292+
connect path=tcp:0.* redirect socks:127.0.0.1:9090
293+
```
294+
295+
The use of `path=tcp:0.*` in this case ensures that only IP addresses that have been mapped with ipmap are redirected to socks. The use of `path!=*.local` in the 'gethostip' rule means that local addresses are not ipmapped.
296+
287297

288298
UNSHARE
289299
=======

common.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ const char **enhancer_argv;
2424
void enhancer_fail_die(const char *FuncName)
2525
{
2626
fprintf(stderr, "FATAL: %s failed!\n", FuncName);
27+
syslog(LOG_CRIT, "FATAL: %s failed!\n", FuncName);
2728
exit(1);
2829
}
2930

config.c

Lines changed: 52 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,8 @@ extern char *enhancer_prog_name;
99

1010

1111

12-
#define MATCH_ALL 0
13-
#define MATCH_PATH 1
14-
#define MATCH_BASENAME 2
15-
#define MATCH_FAMILY 3
16-
#define MATCH_PORT 4
17-
#define MATCH_USER 5
18-
#define MATCH_GROUP 6
19-
#define MATCH_FD 7
20-
#define MATCH_ARG 8
21-
#define MATCH_PEER 9
22-
#define MATCH_CHROOTED 10
12+
typedef enum {MATCH_ALL, MATCH_PATH, MATCH_BASENAME, MATCH_FAMILY, MATCH_PROTO, MATCH_PEER, MATCH_PORT, MATCH_USER, MATCH_GROUP, MATCH_FD, MATCH_ARG, MATCH_CHROOTED} EMatchTypes;
13+
2314

2415
#define OP_EQ 0
2516
#define OP_NOT 1
@@ -30,9 +21,9 @@ extern char *enhancer_prog_name;
3021
char *EnhancerFuncNames[]={"all","main", "onexit", "arg", "open", "close", "read", "write", "uname", "socket", "connect", "bind", "listen", "accept", "gethostip", "sprintf", "fork", "exec", "system", "sysexec", "unlink", "setuid", "setgid", "chown", "chmod", "chdir", "chroot", "time","settime","mprotect", "fsync", "fdatasync", "select", "XMapWindow","XRaiseWindow", "XLowerWindow", "XSendEvent", "XLoadFont", "XChangeProperty", NULL};
3122

3223

33-
char *EnhancerTokNames[]={"deny","allow","die","abort","setvar","setbasename","log","syslog","syslogcrit","echo", "debug", "send", "exec", "die-on-fail", "collect", "sleep", "usleep", "deny-links","deny-symlinks","redirect","fallback","chrooted","if-chrooted","path","basename","peer","user","group","family","fd", "arg", "keepalive", "localnet", "reuseport", "tcp-qack", "tcp-nodelay", "ttl", "freebind", "cmod", "lock", "fdcache","create", "shred", "searchpath", "xstayabove", "xstaybelow", "xiconized", "xunmanaged", "xfullscreen", "xtransparent", "xnormal","pidfile","lockfile", "xtermtitle","backup", "nosync", "fsync", "fdatasync", "writejail", "unshare", "setenv", "getip", "cd", "chroot", "copyclone", "linkclone", "ipmap", "fadv_seq", "fadv_rand", "fadv_nocache", "qlen", "sanitise", "die-on-taint", "deny-on-taint", NULL};
24+
char *EnhancerTokNames[]={"deny","allow","die","abort","setvar","setbasename","log","syslog","syslogcrit","echo", "debug", "send", "exec", "die-on-fail", "collect", "sleep", "usleep", "deny-links","deny-symlinks","redirect","fallback","chrooted","if-chrooted","path","basename","peer","port","user","group","family","fd", "arg", "keepalive", "localnet", "reuseport", "tcp-qack", "tcp-nodelay", "ttl", "freebind", "cmod", "lock", "fdcache","create", "shred", "searchpath", "xstayabove", "xstaybelow", "xiconized", "xunmanaged", "xfullscreen", "xtransparent", "xnormal","pidfile","lockfile", "xtermtitle","backup", "nosync", "fsync", "fdatasync", "writejail", "unshare", "setenv", "getip", "cd", "chroot", "copyclone", "linkclone", "ipmap", "fadv_seq", "fadv_rand", "fadv_nocache", "qlen", "sanitise", "die-on-taint", "deny-on-taint", NULL};
3425

35-
typedef enum {TOK_DENY, TOK_ALLOW, TOK_DIE, TOK_ABORT, TOK_SETVAR, TOK_SETBASENAME, TOK_LOG, TOK_SYSLOG, TOK_SYSLOGCRIT, TOK_ECHO, TOK_DEBUG, TOK_SEND, TOK_EXEC, TOK_FAILDIE, TOK_COLLECT, TOK_SLEEP, TOK_USLEEP, TOK_DENYLINKS, TOK_DENYSYMLINKS, TOK_REDIRECT, TOK_FALLBACK, TOK_CHROOTED, TOK_CHROOTED2, TOK_PATH, TOK_BASENAME, TOK_PEER, TOK_USER, TOK_GROUP, TOK_FAMILY, TOK_FD, TOK_ARG, TOK_KEEPALIVE, TOK_LOCALNET, TOK_REUSEPORT, TOK_TCP_QACK, TOK_TCP_NODELAY, TOK_TTL, TOK_FREEBIND, TOK_CMOD, TOK_LOCK, TOK_FDCACHE, TOK_CREATE, TOK_SHRED, TOK_SEARCHPATH, TOK_X11_STAYABOVE, TOK_X11_STAYBELOW, TOK_X11_ICONIZED, TOK_X11_UNMANAGED, TOK_X11_FULLSCREEN, TOK_X11_TRANSPARENT, TOK_X11_NORMAL, TOK_PIDFILE, TOK_LOCKFILE, TOK_XTERM_TITLE, TOK_BACKUP, TOK_NOSYNC, TOK_FSYNC, TOK_FDATASYNC, TOK_WRITEJAIL, TOK_UNSHARE, TOK_SETENV, TOK_GETIP, TOK_CHDIR, TOK_CHROOT, TOK_COPY_CLONE, TOK_LINK_CLONE, TOK_IPMAP, TOK_FADV_SEQU, TOK_FADV_RAND, TOK_FADV_NOCACHE, TOK_QLEN, TOK_SANITISE, TOK_DIE_ON_TAINT, TOK_DENY_ON_TAINT} TActions;
26+
typedef enum {TOK_DENY, TOK_ALLOW, TOK_DIE, TOK_ABORT, TOK_SETVAR, TOK_SETBASENAME, TOK_LOG, TOK_SYSLOG, TOK_SYSLOGCRIT, TOK_ECHO, TOK_DEBUG, TOK_SEND, TOK_EXEC, TOK_FAILDIE, TOK_COLLECT, TOK_SLEEP, TOK_USLEEP, TOK_DENYLINKS, TOK_DENYSYMLINKS, TOK_REDIRECT, TOK_FALLBACK, TOK_CHROOTED, TOK_CHROOTED2, TOK_PATH, TOK_BASENAME, TOK_PEER, TOK_PORT, TOK_USER, TOK_GROUP, TOK_FAMILY, TOK_FD, TOK_ARG, TOK_KEEPALIVE, TOK_LOCALNET, TOK_REUSEPORT, TOK_TCP_QACK, TOK_TCP_NODELAY, TOK_TTL, TOK_FREEBIND, TOK_CMOD, TOK_LOCK, TOK_FDCACHE, TOK_CREATE, TOK_SHRED, TOK_SEARCHPATH, TOK_X11_STAYABOVE, TOK_X11_STAYBELOW, TOK_X11_ICONIZED, TOK_X11_UNMANAGED, TOK_X11_FULLSCREEN, TOK_X11_TRANSPARENT, TOK_X11_NORMAL, TOK_PIDFILE, TOK_LOCKFILE, TOK_XTERM_TITLE, TOK_BACKUP, TOK_NOSYNC, TOK_FSYNC, TOK_FDATASYNC, TOK_WRITEJAIL, TOK_UNSHARE, TOK_SETENV, TOK_GETIP, TOK_CHDIR, TOK_CHROOT, TOK_COPY_CLONE, TOK_LINK_CLONE, TOK_IPMAP, TOK_FADV_SEQU, TOK_FADV_RAND, TOK_FADV_NOCACHE, TOK_QLEN, TOK_SANITISE, TOK_DIE_ON_TAINT, TOK_DENY_ON_TAINT} TActions;
3627

3728
char *EnhancerFamilyNames[]={"unix","raw","netlink","net","ip4","ip6",NULL};
3829
typedef enum {FAMILY_UNIX, FAMILY_RAW, FAMILY_NETLINK, FAMILY_NET, FAMILY_IP4, FAMILY_IP6} E_NETFAM;
@@ -334,9 +325,24 @@ case FUNC_XLoadFont:
334325
}
335326
break;
336327

328+
case FUNC_GETHOSTIP:
329+
switch (action)
330+
{
331+
case ACT_TTL:
332+
case ACT_CMOD:
333+
case ACT_WRITEJAIL:
334+
case ACT_FALLBACK:
335+
case ACT_SEARCHPATH:
336+
return(FALSE);
337+
break;
338+
339+
default:
340+
return(TRUE);
341+
break;
342+
}
343+
break;
337344

338345
case FUNC_UNAME:
339-
case FUNC_GETHOSTIP:
340346
case FUNC_TIME:
341347
case FUNC_SETTIME:
342348
case FUNC_SETUID:
@@ -474,20 +480,43 @@ enhancer_add_action(Combined, Act->Type, "", Act->IntArg, Act->StrArg);
474480
static int ConfigStrMatch(TConfigItem *Config, const char *MatchStr)
475481
{
476482
int result;
477-
char *Peer=NULL;
483+
char *Item=NULL;
478484
const char *ptr, *p_MatchStr;
479485

480-
if (Config->Type==MATCH_PEER)
486+
switch(Config->Type)
481487
{
482-
ptr=enhancer_strtok(MatchStr, ":", &Peer);
483-
ptr=enhancer_strtok(ptr, ":", &Peer);
484-
p_MatchStr=Peer;
488+
case MATCH_BASENAME:
489+
p_MatchStr=basename(MatchStr);
490+
break;
491+
492+
493+
case MATCH_PROTO:
494+
ptr=enhancer_strtok(MatchStr, ":", &Item);
495+
ptr=enhancer_strtok(ptr, ":", &Item);
496+
p_MatchStr=Item;
497+
break;
498+
499+
500+
case MATCH_PEER:
501+
ptr=enhancer_strtok(MatchStr, ":", &Item);
502+
ptr=enhancer_strtok(ptr, ":", &Item);
503+
p_MatchStr=Item;
504+
break;
505+
506+
case MATCH_PORT:
507+
ptr=enhancer_strtok(MatchStr, ":", &Item);
508+
ptr=enhancer_strtok(ptr, ":", &Item);
509+
ptr=enhancer_strtok(ptr, ":", &Item);
510+
p_MatchStr=Item;
511+
break;
512+
513+
default:
514+
p_MatchStr=MatchStr;
515+
break;
485516
}
486-
else if (Config->Type==MATCH_BASENAME) p_MatchStr=basename(MatchStr);
487-
else p_MatchStr=MatchStr;
488517

489518
result=StrListMatch(p_MatchStr, Config->StrArg);
490-
destroy(Peer);
519+
destroy(Item);
491520

492521
if (Config->Op==OP_NOT) result = !result;
493522

@@ -514,6 +543,7 @@ for (i=0; i < Config->NoOfMatches; i++)
514543
case MATCH_ARG:
515544
case MATCH_PATH:
516545
case MATCH_PEER:
546+
case MATCH_PORT:
517547
case MATCH_BASENAME:
518548
if (ConfigStrMatch(Match, MatchStr)) return(TRUE);
519549
break;
@@ -541,7 +571,6 @@ for (i=0; i < Config->NoOfMatches; i++)
541571
if (Match->IntArg == getgid()) return(TRUE);
542572
break;
543573

544-
case MATCH_PORT:
545574
case MATCH_FD:
546575
if (Match->IntArg == MatchInt) return(TRUE);
547576
break;

enhancer.so

5 KB
Binary file not shown.

iplist.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,17 @@ if (! IPList) return("");
1717
return(enhancer_getvarlist(IPList, ip_addr));
1818
}
1919

20-
2120
char *enhancer_map_ip(const char *RetStr, const char *name)
2221
{
2322
uint32_t ip_nbo;
23+
const char *ptr;
24+
25+
if (IPList)
26+
{
27+
ptr=enhancer_varlist_find_value(IPList, name);
28+
printf("IPMAP: %s\n", ptr);
29+
if (strvalid(ptr)) return(enhancer_strcpy(RetStr, ptr));
30+
}
2431

2532
ip_maps++;
2633

socks.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ char *Auth=NULL;
8282
const char *ptr;
8383
int fd;
8484

85+
if (! DestHost) return(-1);
8586
Auth=enhancer_strcpy(Auth, "");
8687
if (strchr(ProxyURL, '@'))
8788
{
@@ -95,8 +96,6 @@ if (fd > -1)
9596
ptr=enhancer_iplist_get(DestHost);
9697
if (! strvalid(ptr)) ptr=DestHost;
9798

98-
fprintf(stderr, "SOCKS: %s %s\n", DestHost, ptr);
99-
10099
if (
101100
(! socks_request(fd, Auth, ptr, DestPort)) ||
102101
(! socks_reply(fd))

vars.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,22 @@ for (i=0; i < list->max; i++)
8282
return("");
8383
}
8484

85+
const char *enhancer_varlist_find_value(TVarList *list, const char *value)
86+
{
87+
TVar *Var;
88+
int i;
89+
90+
for (i=0; i < list->max; i++)
91+
{
92+
Var=&(list->vars[i]);
93+
if (Var && (strcmp(Var->value,value)==0) ) return(Var->name);
94+
}
95+
96+
return("");
97+
}
98+
99+
100+
85101
const char *enhancer_getvar(const char *name)
86102
{
87103
if (! enhancer_varlist) return("");

vars.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ const char *enhancer_getvarlist(TVarList *list, const char *name);
2323
void enhancer_setvar(const char *name, const char *value);
2424
const char *enhancer_getvar(const char *name);
2525
void enhancer_func_setvar(const char *Arg, const char *FuncName, const char *Str1, const char *Str2);
26+
const char *enhancer_varlist_find_value(TVarList *list, const char *value);
2627

2728

2829
#endif

0 commit comments

Comments
 (0)