1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
|
#include <errno.h>
#include <dlfcn.h>
#include <stdio.h>
#include <stdlib.h>
#include "wrap.h"
#ifndef RTLD_LAZY
#define RTLD_LAZY 1
#endif
#ifndef LIBC_NAME
#define LIBC_NAME "libc.so.6"
#endif
int InSocksFunctions = 0;
#define GETSYM(handle, libname, flags) \
if (!(handle) && ((handle) = dlopen((libname), (flags))) == NULL) { \
return; \
} \
if ((*fptr = dlsym((handle), name)) != NULL) { \
return; \
}
#define GETSYMHANDLE(mask, handle, libname, envname, flags) \
if (libmask & (mask)) { \
static void *(handle) = NULL; \
char *lname = (envname)?getenv((envname)):NULL; \
if (lname == NULL) lname = (libname); \
if (lname) { GETSYM((handle), lname, (flags)); } \
}
/* Look up name in libc.so, libnsl.so, and libsocket.so, if its there return */
/* the symbol we found, otherwise return NULL... */
void GetOriginalFunc(void **fptr, char *name, int libmask) {
/* Synchronize access to func and lib opening functions if we can... */
if (*fptr) return;
// printf("GetOriginalFunc(%s)\n", name);
/* Still have to figure out what OSs USE_RTLD_NEXT is valid for. For */
/* most that I've tried, it hasn't worked right... */
#if defined(RTLD_NEXT) && defined(USE_RTLD_NEXT)
if (name && ~libmask & NO_RTLD_NEXT && (*fptr = dlsym(RTLD_NEXT, name)) != NULL) {
return;
}
#endif
GETSYMHANDLE(TRY_LIBC, libc_handle, LIBC_NAME, "LIBC_NAME", RTLD_LAZY);
}
void doinit()
{
static int init = 0;
if (++init == 1)
{
pid_t pid;
FILE *fd;
char procfile[255];
static char cmd[255];
pid = getpid();
sprintf(procfile, "/proc/%d/cmdline", pid);
if ((fd = fopen(procfile, "r")) == NULL)
{
fprintf(stderr, "Can not open %s : %s\n",
procfile, strerror(errno));
}
else
{
fgets(cmd, 254, fd);
// printf("%d:%s:%s\n", pid,procfile,cmd);
SOCKSinit(cmd);
}
}
}
|