2011-02-25 09:40:11 +00:00
|
|
|
/***************************************************************************
|
|
|
|
main.c - description
|
2011-09-03 23:45:16 +00:00
|
|
|
|
2011-02-25 09:40:11 +00:00
|
|
|
begin : Tue May 14 2002
|
2011-09-03 23:45:16 +00:00
|
|
|
copyright : netcreature (C) 2002
|
|
|
|
email : netcreature@users.sourceforge.net
|
2011-02-25 09:40:11 +00:00
|
|
|
***************************************************************************/
|
|
|
|
/* GPL */
|
|
|
|
/***************************************************************************
|
|
|
|
* *
|
|
|
|
* This program is free software; you can redistribute it and/or modify *
|
|
|
|
* it under the terms of the GNU General Public License as published by *
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or *
|
|
|
|
* (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
***************************************************************************/
|
2012-01-24 06:14:45 +00:00
|
|
|
#undef _POSIX_C_SOURCE
|
|
|
|
#define _POSIX_C_SOURCE 200809L
|
|
|
|
#undef _XOPEN_SOURCE
|
|
|
|
#define _XOPEN_SOURCE 700
|
2011-02-25 09:40:11 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdlib.h>
|
2011-09-03 23:45:16 +00:00
|
|
|
#include <string.h>
|
2011-02-25 09:40:11 +00:00
|
|
|
#include <errno.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
|
2011-09-10 20:32:27 +00:00
|
|
|
#include "common.h"
|
2011-02-25 12:21:34 +00:00
|
|
|
|
2012-01-27 17:59:44 +00:00
|
|
|
static int usage(char **argv) {
|
|
|
|
printf("\nUsage:\t%s -q -f config_file program_name [arguments]\n"
|
|
|
|
"\t-q makes proxychains quiet - this overrides the config setting\n"
|
|
|
|
"\t-t allows to manually specify a configfile to use\n"
|
|
|
|
"\tfor example : proxychains telnet somehost.com\n" "More help in README file\n\n", argv[0]);
|
2012-01-24 07:26:37 +00:00
|
|
|
return EXIT_FAILURE;
|
2011-02-25 12:21:34 +00:00
|
|
|
}
|
|
|
|
|
2012-01-27 17:59:44 +00:00
|
|
|
int check_path(char *path) {
|
|
|
|
if(!path)
|
|
|
|
return 0;
|
2011-09-10 20:32:27 +00:00
|
|
|
return access(path, R_OK) != -1;
|
|
|
|
}
|
|
|
|
|
2012-01-27 18:31:01 +00:00
|
|
|
static const char *dll_name = DLL_NAME;
|
2011-11-06 13:11:36 +00:00
|
|
|
|
2011-11-06 16:47:44 +00:00
|
|
|
static char own_dir[256];
|
2012-01-27 17:59:44 +00:00
|
|
|
static const char *dll_dirs[] = {
|
2011-11-06 13:11:36 +00:00
|
|
|
".",
|
2011-11-06 16:47:44 +00:00
|
|
|
own_dir,
|
2011-11-06 13:11:36 +00:00
|
|
|
LIB_DIR,
|
|
|
|
"/lib",
|
|
|
|
"/usr/lib",
|
|
|
|
"/usr/local/lib",
|
|
|
|
"/lib64",
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
2012-01-27 17:59:44 +00:00
|
|
|
static void set_own_dir(const char *argv0) {
|
2011-11-06 16:47:44 +00:00
|
|
|
size_t l = strlen(argv0);
|
2012-01-27 17:59:44 +00:00
|
|
|
while(l && argv0[l - 1] != '/')
|
|
|
|
l--;
|
2011-11-06 16:47:44 +00:00
|
|
|
if(l == 0)
|
|
|
|
memcpy(own_dir, ".", 2);
|
|
|
|
else {
|
|
|
|
memcpy(own_dir, argv0, l - 1);
|
|
|
|
own_dir[l] = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-27 16:50:04 +00:00
|
|
|
#define MAX_COMMANDLINE_FLAGS 2
|
|
|
|
|
2011-09-03 23:45:16 +00:00
|
|
|
int main(int argc, char *argv[]) {
|
|
|
|
char *path = NULL;
|
|
|
|
char buf[256];
|
|
|
|
char pbuf[256];
|
2011-09-10 21:05:07 +00:00
|
|
|
int start_argv = 1;
|
2012-01-24 07:26:37 +00:00
|
|
|
int quiet = 0;
|
2012-01-26 04:11:52 +00:00
|
|
|
size_t i;
|
2012-01-27 17:59:44 +00:00
|
|
|
const char *prefix = NULL;
|
2012-01-27 16:55:37 +00:00
|
|
|
|
2012-01-27 16:50:04 +00:00
|
|
|
for(i = 0; i < MAX_COMMANDLINE_FLAGS; i++) {
|
2012-01-26 04:11:52 +00:00
|
|
|
if(start_argv < argc && argv[start_argv][0] == '-') {
|
|
|
|
if(argv[start_argv][1] == 'q') {
|
2012-01-24 07:26:37 +00:00
|
|
|
quiet = 1;
|
|
|
|
start_argv++;
|
2012-01-26 04:11:52 +00:00
|
|
|
} else if(argv[start_argv][1] == 'f') {
|
2012-01-27 16:55:37 +00:00
|
|
|
|
|
|
|
if(start_argv + 1 < argc)
|
2012-01-26 04:11:52 +00:00
|
|
|
path = argv[start_argv + 1];
|
2012-01-27 16:55:37 +00:00
|
|
|
else
|
2012-01-26 04:11:52 +00:00
|
|
|
return usage(argv);
|
2012-01-27 16:55:37 +00:00
|
|
|
|
2012-01-24 07:26:37 +00:00
|
|
|
start_argv += 2;
|
2011-09-03 23:45:16 +00:00
|
|
|
}
|
2012-01-27 16:55:37 +00:00
|
|
|
} else
|
2012-01-26 04:11:52 +00:00
|
|
|
break;
|
2011-09-03 23:45:16 +00:00
|
|
|
}
|
2012-01-26 04:11:52 +00:00
|
|
|
|
2012-01-27 17:59:44 +00:00
|
|
|
if(start_argv >= argc)
|
|
|
|
return usage(argv);
|
2011-02-25 12:21:34 +00:00
|
|
|
|
2011-09-10 20:32:27 +00:00
|
|
|
/* check if path of config file has not been passed via command line */
|
2011-09-03 23:45:16 +00:00
|
|
|
if(!path) {
|
2011-09-10 20:32:27 +00:00
|
|
|
// priority 1: env var PROXYCHAINS_CONF_FILE
|
|
|
|
path = getenv(PROXYCHAINS_CONF_FILE_ENV_VAR);
|
2012-01-27 17:59:44 +00:00
|
|
|
if(check_path(path))
|
|
|
|
goto have;
|
2012-01-27 16:55:37 +00:00
|
|
|
|
2011-09-10 20:32:27 +00:00
|
|
|
// priority 2; proxychains conf in actual dir
|
|
|
|
path = getcwd(buf, sizeof(buf));
|
|
|
|
snprintf(pbuf, sizeof(pbuf), "%s/%s", path, PROXYCHAINS_CONF_FILE);
|
|
|
|
path = pbuf;
|
2012-01-27 17:59:44 +00:00
|
|
|
if(check_path(path))
|
|
|
|
goto have;
|
2012-01-27 16:55:37 +00:00
|
|
|
|
2011-09-10 20:32:27 +00:00
|
|
|
// priority 3; $HOME/.proxychains/proxychains.conf
|
|
|
|
path = getenv("HOME");
|
|
|
|
snprintf(pbuf, sizeof(pbuf), "%s/.proxychains/%s", path, PROXYCHAINS_CONF_FILE);
|
|
|
|
path = pbuf;
|
2012-01-27 17:59:44 +00:00
|
|
|
if(check_path(path))
|
|
|
|
goto have;
|
2012-01-27 16:55:37 +00:00
|
|
|
|
2012-01-27 18:28:00 +00:00
|
|
|
// priority 4: $INSTALL_PREFIX/etc/proxychains.conf
|
2012-01-29 01:34:04 +00:00
|
|
|
path = INSTALL_PREFIX "/etc/" PROXYCHAINS_CONF_FILE;
|
2012-01-27 18:28:00 +00:00
|
|
|
if(check_path(path))
|
|
|
|
goto have;
|
|
|
|
|
|
|
|
// priority 5: /etc/proxychains.conf
|
2012-01-28 17:34:40 +00:00
|
|
|
path = "/etc/" PROXYCHAINS_CONF_FILE;
|
2012-01-27 17:59:44 +00:00
|
|
|
if(check_path(path))
|
|
|
|
goto have;
|
2011-09-03 23:45:16 +00:00
|
|
|
perror("couldnt find configuration file");
|
|
|
|
return 1;
|
|
|
|
}
|
2012-01-27 16:55:37 +00:00
|
|
|
|
2011-09-03 23:45:16 +00:00
|
|
|
have:
|
2011-02-25 12:21:34 +00:00
|
|
|
|
2012-01-27 17:59:44 +00:00
|
|
|
if(!quiet)
|
|
|
|
fprintf(stderr, LOG_PREFIX "config file found: %s\n", path);
|
2011-02-25 12:21:34 +00:00
|
|
|
|
2011-09-03 23:45:16 +00:00
|
|
|
/* Set PROXYCHAINS_CONF_FILE to get proxychains lib to use new config file. */
|
2011-09-10 20:32:27 +00:00
|
|
|
setenv(PROXYCHAINS_CONF_FILE_ENV_VAR, path, 1);
|
2012-01-27 16:55:37 +00:00
|
|
|
|
2012-01-27 17:59:44 +00:00
|
|
|
if(quiet)
|
|
|
|
setenv(PROXYCHAINS_QUIET_MODE_ENV_VAR, "1", 1);
|
2011-11-06 13:11:36 +00:00
|
|
|
|
|
|
|
|
|
|
|
// search DLL
|
|
|
|
|
2011-11-06 16:47:44 +00:00
|
|
|
set_own_dir(argv[0]);
|
|
|
|
|
2011-11-06 13:11:36 +00:00
|
|
|
while(dll_dirs[i]) {
|
|
|
|
snprintf(buf, sizeof(buf), "%s/%s", dll_dirs[i], dll_name);
|
|
|
|
if(access(buf, R_OK) != -1) {
|
|
|
|
prefix = dll_dirs[i];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!prefix) {
|
2012-01-24 07:26:37 +00:00
|
|
|
fprintf(stderr, "couldnt locate %s\n", dll_name);
|
2011-11-06 13:11:36 +00:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
2012-01-27 17:59:44 +00:00
|
|
|
if(!quiet)
|
|
|
|
fprintf(stderr, LOG_PREFIX "preloading %s/%s\n", prefix, dll_name);
|
2012-01-27 16:55:37 +00:00
|
|
|
|
2012-01-27 19:48:24 +00:00
|
|
|
#ifndef IS_MAC
|
2011-11-06 13:11:36 +00:00
|
|
|
snprintf(buf, sizeof(buf), "LD_PRELOAD=%s/%s", prefix, dll_name);
|
2011-09-03 23:45:16 +00:00
|
|
|
putenv(buf);
|
2012-01-27 19:48:24 +00:00
|
|
|
#else
|
|
|
|
snprintf(buf, sizeof(buf), "DYLD_INSERT_LIBRARIES=%s/%s", prefix, dll_name);
|
|
|
|
putenv(buf);
|
|
|
|
putenv("DYLD_FORCE_FLAT_NAMESPACE=1");
|
|
|
|
#endif
|
2011-09-10 21:05:07 +00:00
|
|
|
execvp(argv[start_argv], &argv[start_argv]);
|
2011-09-03 23:45:16 +00:00
|
|
|
perror("proxychains can't load process....");
|
2011-02-25 12:21:34 +00:00
|
|
|
|
2011-09-10 20:32:27 +00:00
|
|
|
return EXIT_FAILURE;
|
2011-02-25 09:40:11 +00:00
|
|
|
}
|