Rework indentation

git-svn-id: https://proxytunnel.svn.sourceforge.net/svnroot/proxytunnel/trunk/proxytunnel@199 bc163920-b10d-0410-b2c5-a5491ca2ceef
This commit is contained in:
Mark Janssen 2008-01-22 22:29:08 +00:00
parent 688f352eb4
commit fc32748906
5 changed files with 418 additions and 481 deletions

136
base64.c
View file

@ -1,5 +1,5 @@
/* Proxytunnel - (C) 2001-2006 Jos Visser / Mark Janssen */
/* Contact: josv@osp.nl / maniac@maniac.nl */
/* Proxytunnel - (C) 2001-2008 Jos Visser / Mark Janssen */
/* Contact: josv@osp.nl / maniac@maniac.nl */
/*
* This program is free software; you can redistribute it and/or modify
@ -25,18 +25,18 @@
/* Needed for base64 encoding... */
static const char base64digits[] =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
#define BAD -1
static const char base64val[] = {
BAD,BAD,BAD,BAD, BAD,BAD,BAD,BAD, BAD,BAD,BAD,BAD, BAD,BAD,BAD,BAD,
BAD,BAD,BAD,BAD, BAD,BAD,BAD,BAD, BAD,BAD,BAD,BAD, BAD,BAD,BAD,BAD,
BAD,BAD,BAD,BAD, BAD,BAD,BAD,BAD, BAD,BAD,BAD, 62, BAD,BAD,BAD, 63,
52, 53, 54, 55, 56, 57, 58, 59, 60, 61,BAD,BAD, BAD,BAD,BAD,BAD,
BAD, 0, 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,BAD, BAD,BAD,BAD,BAD,
BAD, 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,BAD, BAD,BAD,BAD,BAD
BAD,BAD,BAD,BAD, BAD,BAD,BAD,BAD, BAD,BAD,BAD,BAD, BAD,BAD,BAD,BAD,
BAD,BAD,BAD,BAD, BAD,BAD,BAD,BAD, BAD,BAD,BAD,BAD, BAD,BAD,BAD,BAD,
BAD,BAD,BAD,BAD, BAD,BAD,BAD,BAD, BAD,BAD,BAD, 62, BAD,BAD,BAD, 63,
52, 53, 54, 55, 56, 57, 58, 59, 60, 61,BAD,BAD, BAD,BAD,BAD,BAD,
BAD, 0, 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,BAD, BAD,BAD,BAD,BAD,
BAD, 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,BAD, BAD,BAD,BAD,BAD
};
#define DECODE64(c) (isascii(c) ? base64val[c] : BAD)
@ -64,78 +64,78 @@ static const char base64val[] = {
/* raw bytes to null-terminated base 64 string */
void base64(unsigned char *out, const unsigned char *in, int len)
{
while (len >= 3) {
*out++ = base64digits[in[0] >> 2];
*out++ = base64digits[((in[0] << 4) & 0x30) | (in[1] >> 4)];
*out++ = base64digits[((in[1] << 2) & 0x3c) | (in[2] >> 6)];
*out++ = base64digits[in[2] & 0x3f];
len -= 3;
in += 3;
}
while (len >= 3) {
*out++ = base64digits[in[0] >> 2];
*out++ = base64digits[((in[0] << 4) & 0x30) | (in[1] >> 4)];
*out++ = base64digits[((in[1] << 2) & 0x3c) | (in[2] >> 6)];
*out++ = base64digits[in[2] & 0x3f];
len -= 3;
in += 3;
}
/* clean up remainder */
if (len > 0) {
unsigned char fragment;
/* clean up remainder */
if (len > 0) {
unsigned char fragment;
*out++ = base64digits[in[0] >> 2];
fragment = (in[0] << 4) & 0x30;
if (len > 1)
fragment |= in[1] >> 4;
*out++ = base64digits[fragment];
*out++ = (len < 2) ? '=' : base64digits[(in[1] << 2) & 0x3c];
*out++ = '=';
}
*out = '\0';
*out++ = base64digits[in[0] >> 2];
fragment = (in[0] << 4) & 0x30;
if (len > 1)
fragment |= in[1] >> 4;
*out++ = base64digits[fragment];
*out++ = (len < 2) ? '=' : base64digits[(in[1] << 2) & 0x3c];
*out++ = '=';
}
*out = '\0';
}
int unbase64(unsigned char *out, const unsigned char *in, int maxlen)
/* base 64 to raw bytes in quasi-big-endian order, returning count of bytes */
/* maxlen limits output buffer size, set to zero to ignore */
{
int len = 0;
register unsigned char digit1, digit2, digit3, digit4;
int len = 0;
register unsigned char digit1, digit2, digit3, digit4;
if (in[0] == '+' && in[1] == ' ')
if (in[0] == '+' && in[1] == ' ')
in += 2;
if (*in == '\r')
if (*in == '\r')
return(0);
do {
digit1 = in[0];
if (DECODE64(digit1) == BAD)
return(-1);
digit2 = in[1];
if (DECODE64(digit2) == BAD)
return(-1);
digit3 = in[2];
if (digit3 != '=' && DECODE64(digit3) == BAD)
return(-1);
digit4 = in[3];
if (digit4 != '=' && DECODE64(digit4) == BAD)
return(-1);
in += 4;
++len;
if (maxlen && len > maxlen)
return(-1);
*out++ = (DECODE64(digit1) << 2) | (DECODE64(digit2) >> 4);
if (digit3 != '=')
{
++len;
if (maxlen && len > maxlen)
return(-1);
*out++ = ((DECODE64(digit2) << 4) & 0xf0) | (DECODE64(digit3) >> 2);
if (digit4 != '=')
{
++len;
do {
digit1 = in[0];
if (DECODE64(digit1) == BAD)
return(-1);
digit2 = in[1];
if (DECODE64(digit2) == BAD)
return(-1);
digit3 = in[2];
if (digit3 != '=' && DECODE64(digit3) == BAD)
return(-1);
digit4 = in[3];
if (digit4 != '=' && DECODE64(digit4) == BAD)
return(-1);
in += 4;
++len;
if (maxlen && len > maxlen)
return(-1);
*out++ = ((DECODE64(digit3) << 6) & 0xc0) | DECODE64(digit4);
}
}
} while
return(-1);
*out++ = (DECODE64(digit1) << 2) | (DECODE64(digit2) >> 4);
if (digit3 != '=')
{
++len;
if (maxlen && len > maxlen)
return(-1);
*out++ = ((DECODE64(digit2) << 4) & 0xf0) | (DECODE64(digit3) >> 2);
if (digit4 != '=')
{
++len;
if (maxlen && len > maxlen)
return(-1);
*out++ = ((DECODE64(digit3) << 6) & 0xc0) | DECODE64(digit4);
}
}
} while
(*in && *in != '\r' && digit4 != '=');
return (len);
return (len);
}
// vim:noet

View file

@ -1,4 +1,4 @@
/* Proxytunnel - (C) 2001-2006 Jos Visser / Mark Janssen */
/* Proxytunnel - (C) 2001-2008 Jos Visser / Mark Janssen */
/* Contact: josv@osp.nl / maniac@maniac.nl */
/*
@ -33,8 +33,7 @@
*/
void make_basicauth()
{
int len = strlen( args_info.user_arg ) + \
strlen( args_info.pass_arg ) + 2;
int len = strlen( args_info.user_arg ) + strlen( args_info.pass_arg ) + 2;
char *p = (char *) malloc( len );
/*

678
cmdline.c
View file

@ -1,4 +1,4 @@
/* Proxytunnel - (C) 2001-2006 Jos Visser / Mark Janssen */
/* Proxytunnel - (C) 2001-2008 Jos Visser / Mark Janssen */
/* Contact: josv@osp.nl / maniac@maniac.nl */
/*
@ -40,14 +40,14 @@ static char *getCredentialsFromFile( const char* filename, char **user, char **p
void
cmdline_parser_print_version (void)
{
printf ("%s %s (rev %d)\nCopyright 2001-2008 Proxytunnel Project\n%s\n", PACKAGE, VERSION, REV, AUTHORS);
printf ("%s %s (rev %d)\nCopyright 2001-2008 Proxytunnel Project\n%s\n", PACKAGE, VERSION, REV, AUTHORS);
}
void
cmdline_parser_print_help (void)
{
// cmdline_parser_print_version ();
printf(
// cmdline_parser_print_version ();
printf(
"Usage: %s [OPTIONS]...\n"
"Build generic tunnels trough HTTPS proxy's, supports HTTP authorization\n"
"\n"
@ -83,16 +83,8 @@ cmdline_parser_print_help (void)
" -h, --help Print help and exit\n"
" -V, --version Print version and exit\n", PACKAGE);
/* FIXME: Examples belong in the manpage, help is already too verbose
printf( "\nExamples:\n"
"%s [ -h | -V ]\n"
"%s -i [ -u user ] -p proxy:port -d host:port [ -v | -q ]\n"
"%s -i [ -U envvar ] -p proxy:port -d host:port [ -v | -q ]\n"
"%s -a port -p proxy:port -d host:port [ -v | -q ]\n", PACKAGE, PACKAGE, PACKAGE, PACKAGE );
*/
#ifndef HAVE_GETOPT_LONG
printf( "\n"
printf( "\n"
"Notice: This version is compiled without support for long options.\n"
"This means you can only use the short (1 letter) options on the commandline.\n" );
#endif
@ -102,43 +94,44 @@ cmdline_parser_print_help (void)
static char *
gengetopt_strdup (char * s)
{
char * n, * pn, * ps = s;
while (*ps) ps++;
n = (char *) malloc (1 + ps - s);
if (n != NULL)
{
for (ps=s,pn=n; *ps; ps++,pn++)
*pn = *ps;
*pn = 0;
}
return n;
char * n, * pn, * ps = s;
while (*ps) ps++;
n = (char *) malloc (1 + ps - s);
if (n != NULL)
{
for (ps=s,pn=n; *ps; ps++,pn++)
*pn = *ps;
*pn = 0;
}
return n;
}
int cmdline_parser( int argc, char * const *argv, struct gengetopt_args_info *args_info )
int
cmdline_parser( int argc, char * const *argv, struct gengetopt_args_info *args_info )
{
int c; /* Character of the parsed option. */
int r; /* Tmd retval */
int missing_required_options = 0;
char * tmp_env_var;
int c; /* Character of the parsed option. */
int r; /* Tmd retval */
int missing_required_options = 0;
char * tmp_env_var;
args_info->help_given = 0;
args_info->version_given = 0;
args_info->user_given = 0;
args_info->pass_given = 0;
args_info->proxy_given = 0;
args_info->proxyhost_given = 0;
args_info->proxyport_given = 0;
args_info->dest_given = 0;
args_info->remproxy_given = 0;
args_info->verbose_given = 0;
args_info->ntlm_given = 0;
args_info->inetd_given = 0;
args_info->quiet_given = 0;
args_info->header_given = 0;
args_info->domain_given = 0;
args_info->encrypt_given = 0;
args_info->encryptproxy_given = 0;
args_info->proctitle_given = 0;
args_info->help_given = 0;
args_info->version_given = 0;
args_info->user_given = 0;
args_info->pass_given = 0;
args_info->proxy_given = 0;
args_info->proxyhost_given = 0;
args_info->proxyport_given = 0;
args_info->dest_given = 0;
args_info->remproxy_given = 0;
args_info->verbose_given = 0;
args_info->ntlm_given = 0;
args_info->inetd_given = 0;
args_info->quiet_given = 0;
args_info->header_given = 0;
args_info->domain_given = 0;
args_info->encrypt_given = 0;
args_info->encryptproxy_given = 0;
args_info->proctitle_given = 0;
/* No... we can't make this a function... -- Maniac */
#define clear_args() \
@ -161,366 +154,343 @@ int cmdline_parser( int argc, char * const *argv, struct gengetopt_args_info *ar
args_info->proctitle_arg = NULL; \
}
clear_args();
clear_args();
optarg = 0;
optarg = 0;
#ifdef HAVE_GETOPT_LONG
optind = 1;
opterr = 1;
optopt = '?';
optind = 1;
opterr = 1;
optopt = '?';
#endif
while (1)
{
while (1) {
#ifdef HAVE_GETOPT_LONG
int option_index = 0;
int option_index = 0;
/* Struct option: Name, Has_arg, Flag, Value */
static struct option long_options[] = {
{ "help", 0, NULL, 'h' },
{ "version", 0, NULL, 'V' },
{ "user", 1, NULL, 'u' },
{ "pass", 1, NULL, 's' },
{ "domain", 1, NULL, 't' },
{ "uservar", 1, NULL, 'U' },
{ "passvar", 1, NULL, 'S' },
{ "passfile", 1, NULL, 'F' },
{ "proxy", 1, NULL, 'p' },
{ "proxyhost", 1, NULL, 'g' },
{ "proxyport", 1, NULL, 'G' },
{ "dest", 1, NULL, 'd' },
{ "remproxy", 1, NULL, 'r' },
{ "proctitle", 1, NULL, 'x' },
{ "header", 1, NULL, 'H' },
{ "verbose", 0, NULL, 'v' },
{ "ntlm", 0, NULL, 'N' },
{ "inetd", 0, NULL, 'i' },
{ "standalone", 1, NULL, 'a' },
{ "quiet", 0, NULL, 'q' },
{ "encrypt", 0, NULL, 'e' },
{ "encrypt-proxy", 0, NULL, 'E' },
{ NULL, 0, NULL, 0 }
};
/* Struct option: Name, Has_arg, Flag, Value */
static struct option long_options[] = {
{ "help", 0, NULL, 'h' },
{ "version", 0, NULL, 'V' },
{ "user", 1, NULL, 'u' },
{ "pass", 1, NULL, 's' },
{ "domain", 1, NULL, 't' },
{ "uservar", 1, NULL, 'U' },
{ "passvar", 1, NULL, 'S' },
{ "passfile", 1, NULL, 'F' },
{ "proxy", 1, NULL, 'p' },
{ "proxyhost", 1, NULL, 'g' },
{ "proxyport", 1, NULL, 'G' },
{ "dest", 1, NULL, 'd' },
{ "remproxy", 1, NULL, 'r' },
{ "proctitle", 1, NULL, 'x' },
{ "header", 1, NULL, 'H' },
{ "verbose", 0, NULL, 'v' },
{ "ntlm", 0, NULL, 'N' },
{ "inetd", 0, NULL, 'i' },
{ "standalone", 1, NULL, 'a' },
{ "quiet", 0, NULL, 'q' },
{ "encrypt", 0, NULL, 'e' },
{ "encrypt-proxy", 0, NULL, 'E' },
{ NULL, 0, NULL, 0 }
};
c = getopt_long (argc, argv, "hVia:u:s:t:U:S:F:p:r:d:H:x:nvNeEq", long_options, &option_index);
c = getopt_long (argc, argv, "hVia:u:s:t:U:S:F:p:r:d:H:x:nvNeEq", long_options, &option_index);
#else
c = getopt( argc, argv, "hVia:u:s:t:U:S:F:p:r:d:H:x:nvNeEq" );
c = getopt( argc, argv, "hVia:u:s:t:U:S:F:p:r:d:H:x:nvNeEq" );
#endif
if (c == -1) break; /* Exit from `while (1)' loop. */
if (c == -1)
break; /* Exit from `while (1)' loop. */
switch (c)
{
case 'h': /* Print help and exit. */
clear_args ();
cmdline_parser_print_help ();
exit (0);
switch (c) {
case 'h': /* Print help and exit. */
clear_args ();
cmdline_parser_print_help ();
exit(0);
#ifdef USE_SSL
case 'e': /* Turn on SSL encryption */
args_info->encrypt_flag = !(args_info->encrypt_flag);
if( args_info->verbose_flag )
message("SSL enabled\n");
break;
case 'e': /* Turn on SSL encryption */
args_info->encrypt_flag = !(args_info->encrypt_flag);
if( args_info->verbose_flag )
message("SSL enabled\n");
break;
case 'E': /* Turn on client to proxy SSL encryption */
args_info->encryptproxy_flag = !(args_info->encryptproxy_flag);
if( args_info->verbose_flag )
message("SSL client to proxy enabled\n");
break;
case 'E': /* Turn on client to proxy SSL encryption */
args_info->encryptproxy_flag = !(args_info->encryptproxy_flag);
if( args_info->verbose_flag )
message("SSL client to proxy enabled\n");
break;
#endif
case 'i': /* Run from inetd. */
if ( args_info->standalone_arg > 0 )
{
fprintf( stderr, "%s: '--inetd' ('-i') conflicts with '--standalone' ('-a')\n", PACKAGE );
exit( 1 );
}
args_info->inetd_flag = !(args_info->inetd_flag);
break;
case 'i': /* Run from inetd. */
if ( args_info->standalone_arg > 0 ) {
fprintf( stderr, "%s: '--inetd' ('-i') conflicts with '--standalone' ('-a')\n", PACKAGE );
clear_args();
exit( 1 );
}
args_info->inetd_flag = !(args_info->inetd_flag);
break;
case 'a': /* Run as standalone daemon */
if ( args_info->inetd_flag )
{
fprintf( stderr, "%s: `--standalone' (`-a') conflicts with `--inetd' (`-i')\n", PACKAGE );
exit (1);
}
if ( ( args_info->standalone_arg = atoi( optarg ) ) < 1 )
{
fprintf( stderr, "%s: Illegal port value for `--standalone' (`-a')\n", PACKAGE);
exit (1);
}
break;
case 'a': /* Run as standalone daemon */
if ( args_info->inetd_flag ) {
fprintf( stderr, "%s: `--standalone' (`-a') conflicts with `--inetd' (`-i')\n", PACKAGE );
clear_args();
exit(1);
}
if ( ( args_info->standalone_arg = atoi( optarg ) ) < 1 ) {
fprintf( stderr, "%s: Illegal port value for `--standalone' (`-a')\n", PACKAGE);
clear_args();
exit(1);
}
break;
case 'V': /* Print version and exit. */
clear_args ();
cmdline_parser_print_version ();
exit (0);
case 'V': /* Print version and exit. */
clear_args ();
cmdline_parser_print_version ();
exit(0);
case 'x':
args_info->proctitle_given = 1;
message( "Proctitle override enabled\n" );
args_info->proctitle_arg = gengetopt_strdup (optarg);
break;
case 'x':
args_info->proctitle_given = 1;
message( "Proctitle override enabled\n" );
args_info->proctitle_arg = gengetopt_strdup (optarg);
break;
case 'u': /* Username to send to HTTPS proxy for authentication. */
if (args_info->user_given)
{
fprintf (stderr, "%s: `--user' (`-u'), `--uservar' (`-U') or `--passfile' (`-F') option given more than once\n", PACKAGE);
clear_args ();
exit (1);
}
args_info->user_given = 1;
args_info->user_arg = gengetopt_strdup (optarg);
break;
case 'u': /* Username to send to HTTPS proxy for authentication. */
if (args_info->user_given) {
fprintf (stderr, "%s: `--user' (`-u'), `--uservar' (`-U') or `--passfile' (`-F') option given more than once\n", PACKAGE);
clear_args ();
exit (1);
}
args_info->user_given = 1;
args_info->user_arg = gengetopt_strdup (optarg);
break;
case 'U': /* Env Var with Username to send to HTTPS proxy for authentication. */
if (args_info->user_given)
{
fprintf (stderr, "%s: `--user' (`-u'), `--uservar' (`-U') or `--passfile' (`-F') option given more than once\n", PACKAGE);
clear_args ();
exit (1);
}
tmp_env_var = getenv(optarg) ;
if (!tmp_env_var) {
fprintf (stderr, "%s Invalid environment variable\n", optarg) ;
clear_args ();
exit (1);
}
args_info->user_given = 1;
args_info->user_arg = gengetopt_strdup (tmp_env_var);
break;
case 'U': /* Env Var with Username to send to HTTPS proxy for authentication. */
if (args_info->user_given) {
fprintf (stderr, "%s: `--user' (`-u'), `--uservar' (`-U') or `--passfile' (`-F') option given more than once\n", PACKAGE);
clear_args ();
exit (1);
}
tmp_env_var = getenv(optarg) ;
if (!tmp_env_var) {
fprintf (stderr, "%s Invalid environment variable\n", optarg) ;
clear_args ();
exit (1);
}
args_info->user_given = 1;
args_info->user_arg = gengetopt_strdup (tmp_env_var);
break;
case 's': /* Password to send to HTTPS proxy for authentication. */
if (args_info->pass_given)
{
fprintf (stderr, "%s: `--pass' (`-s'), `--passvar' (`-S') or `--passfile' (`-F') option given more than once\n", PACKAGE);
clear_args ();
exit (1);
}
args_info->pass_given = 1;
args_info->pass_arg = gengetopt_strdup (optarg);
break;
case 's': /* Password to send to HTTPS proxy for authentication. */
if (args_info->pass_given) {
fprintf (stderr, "%s: `--pass' (`-s'), `--passvar' (`-S') or `--passfile' (`-F') option given more than once\n", PACKAGE);
clear_args ();
exit (1);
}
args_info->pass_given = 1;
args_info->pass_arg = gengetopt_strdup (optarg);
break;
case 't': /* Env Var with NTLM DOMAIN (when overriding). */
if (args_info->domain_given)
{
fprintf (stderr, "%s: `--domain' (`-t') option given more than once\n", PACKAGE);
clear_args ();
exit (1);
}
args_info->domain_given = 1;
args_info->domain_arg = gengetopt_strdup (optarg);
break;
case 't': /* Env Var with NTLM DOMAIN (when overriding). */
if (args_info->domain_given) {
fprintf (stderr, "%s: `--domain' (`-t') option given more than once\n", PACKAGE);
clear_args ();
exit (1);
}
args_info->domain_given = 1;
args_info->domain_arg = gengetopt_strdup (optarg);
break;
case 'S': /* Env Var with Password to send to HTTPS proxy for authentication. */
if (args_info->pass_given)
{
fprintf (stderr, "%s: `--pass' (`-s') or `--passvar' (`-S') option given more than once\n", PACKAGE);
clear_args ();
exit (1);
}
tmp_env_var = getenv(optarg) ;
if (!tmp_env_var) {
fprintf (stderr, "%s Invalid environment variable\n", optarg) ;
clear_args ();
exit (1);
}
args_info->pass_given = 1;
args_info->pass_arg = gengetopt_strdup (tmp_env_var);
break;
case 'S': /* Env Var with Password to send to HTTPS proxy for authentication. */
if (args_info->pass_given) {
fprintf (stderr, "%s: `--pass' (`-s') or `--passvar' (`-S') option given more than once\n", PACKAGE);
clear_args ();
exit (1);
}
tmp_env_var = getenv(optarg) ;
if (!tmp_env_var) {
fprintf (stderr, "%s Invalid environment variable\n", optarg) ;
clear_args ();
exit (1);
}
args_info->pass_given = 1;
args_info->pass_arg = gengetopt_strdup (tmp_env_var);
break;
case 'F': /* File containing Username & Password to send to
HTTPS proxy for authentication. */
if (args_info->user_given)
{
fprintf (stderr, "%s: `--user' (`-u'), `--uservar' (`-U') or `--passfile' (`-F') option given more than once\n", PACKAGE);
clear_args ();
exit (1);
}
if (args_info->pass_given)
{
fprintf (stderr, "%s: `--pass' (`-s'), `--passvar' (`-S') or `--passfile' (`-F') option given more than once\n", PACKAGE);
clear_args ();
exit (1);
}
args_info->user_given = 1;
args_info->pass_given = 1;
char *result = getCredentialsFromFile(optarg, &(args_info->user_arg),
&(args_info->pass_arg) );
case 'F': /* File containing Username & Password to send to
HTTPS proxy for authentication. */
if (args_info->user_given) {
fprintf (stderr, "%s: `--user' (`-u'), `--uservar' (`-U') or `--passfile' (`-F') option given more than once\n", PACKAGE);
clear_args ();
exit (1);
}
if (args_info->pass_given) {
fprintf (stderr, "%s: `--pass' (`-s'), `--passvar' (`-S') or `--passfile' (`-F') option given more than once\n", PACKAGE);
clear_args ();
exit (1);
}
args_info->user_given = 1;
args_info->pass_given = 1;
char *result = getCredentialsFromFile(optarg, &(args_info->user_arg), &(args_info->pass_arg) );
if( result != NULL ) {
fprintf( stderr, "%s: Bad password file for `--passfile' (`-F')\n%s\n",
PACKAGE, result);
exit (1);
}
break;
if( result != NULL ) {
fprintf( stderr, "%s: Bad password file for `--passfile' (`-F')\n%s\n", PACKAGE, result);
clear_args();
exit (1);
}
break;
case 'g': /* HTTPS Proxy host to connect to. */
case 'G': /* HTTPS Proxy host portnumber to connect to. */
fprintf (stderr, "%s: `-g option is obsolete, use -p\n", PACKAGE);
clear_args ();
exit (1);
break;
case 'g': /* HTTPS Proxy host to connect to. */
fprintf (stderr, "%s: `-g option is obsolete, use -p\n", PACKAGE);
clear_args ();
exit (1);
break;
case 'p': /* HTTPS Proxy host:port to connect to. */
if (args_info->proxy_given) {
fprintf (stderr, "%s: `--proxy' (`-p') option given more than once\n", PACKAGE);
clear_args ();
exit (1);
}
args_info->proxy_given = 1;
args_info->proxy_arg = gengetopt_strdup (optarg);
break;
case 'G': /* HTTPS Proxy host portnumber to connect to. */
fprintf (stderr, "%s: `-g option is obsolete, use -p\n", PACKAGE);
clear_args ();
exit (1);
break;
case 'r': /* Use a remote proxy */
if (args_info->remproxy_given) {
fprintf (stderr, "%s: `--remproxy' (`-r') option given more than once\n", PACKAGE);
clear_args ();
exit (1);
}
args_info->remproxy_given = 1;
args_info->remproxy_arg = gengetopt_strdup (optarg);
break;
case 'p': /* HTTPS Proxy host:port to connect to. */
if (args_info->proxy_given)
{
fprintf (stderr, "%s: `--proxy' (`-p') option given more than once\n", PACKAGE);
case 'd': /* Destination host to built the tunnel to. */
if (args_info->dest_given) {
fprintf (stderr, "%s: `--dest' (`-d') option given more than once\n", PACKAGE);
clear_args ();
exit (1);
}
args_info->dest_given = 1;
args_info->dest_arg = gengetopt_strdup (optarg);
break;
case 'H': /* Extra headers to send to HTTPS proxy. */
args_info->header_given++; /* Amount of extra headers */
strlcat( args_info->header_arg, optarg, MAX_HEADER_SIZE );
strlcat( args_info->header_arg, "\r\n", MAX_HEADER_SIZE );
break;
case 'v': /* Turn on verbosity. */
if (args_info->quiet_flag) { /* -q also on cmd line */
fprintf (stderr, "-v and -q are mutually exclusive\n");
clear_args();
exit(1);
}
args_info->verbose_flag = !(args_info->verbose_flag);
break;
case 'N': /* Turn on NTLM. */
args_info->ntlm_flag = !(args_info->ntlm_flag);
break;
case 'q': /* Suppress messages -- Quiet mode */
args_info->quiet_flag = !(args_info->quiet_flag);
break;
case 0: /* Long option with no short option */
case '?': /* Invalid option. */
/* `getopt_long' already printed an error message. */
clear_args();
exit (1);
default: /* bug: option not considered. */
fprintf (stderr, "%s: option unknown: %c\n", PACKAGE, c);
clear_args();
abort();
} /* switch */
} /* while */
if (! args_info->proxy_given && ! args_info->dest_given ) {
clear_args ();
exit (1);
}
args_info->proxy_given = 1;
args_info->proxy_arg = gengetopt_strdup (optarg);
break;
case 'r': /* Use a remote proxy */
if (args_info->remproxy_given)
{
fprintf (stderr, "%s: `--remproxy' (`-r') option given more than once\n", PACKAGE);
clear_args ();
exit (1);
}
args_info->remproxy_given = 1;
args_info->remproxy_arg = gengetopt_strdup (optarg);
break;
case 'd': /* Destination host to built the tunnel to. */
if (args_info->dest_given)
{
fprintf (stderr, "%s: `--dest' (`-d') option given more than once\n", PACKAGE);
clear_args ();
exit (1);
}
args_info->dest_given = 1;
args_info->dest_arg = gengetopt_strdup (optarg);
break;
case 'H': /* Extra headers to send to HTTPS proxy. */
args_info->header_given++;
strlcat( args_info->header_arg, optarg, MAX_HEADER_SIZE );
strlcat( args_info->header_arg, "\r\n", MAX_HEADER_SIZE );
break;
case 'v': /* Turn on verbosity. */
if (args_info->quiet_flag) /* -q also on cmd line */
{
fprintf (stderr, "-v and -q are mutually exclusive\n");
exit(1);
}
args_info->verbose_flag = !(args_info->verbose_flag);
break;
case 'N': /* Turn on NTLM. */
args_info->ntlm_flag = !(args_info->ntlm_flag);
break;
case 'q': /* Suppress messages -- Quiet mode */
args_info->quiet_flag = !(args_info->quiet_flag);
break;
case 0: /* Long option with no short option */
case '?': /* Invalid option. */
/* `getopt_long' already printed an error message. */
exit (1);
default: /* bug: option not considered. */
fprintf (stderr, "%s: option unknown: %c\n", PACKAGE, c);
abort ();
} /* switch */
} /* while */
if (! args_info->proxy_given && ! args_info->dest_given )
{
clear_args ();
cmdline_parser_print_help ();
exit (0);
}
cmdline_parser_print_help ();
exit(0);
}
/* For Windows quiet is the default output. -- Dag */
#ifdef CYGWIN
if (! args_info->verbose_flag ) {
args_info->quiet_flag = 1;
}
if (! args_info->verbose_flag ) {
args_info->quiet_flag = 1;
}
#endif
if (args_info->proxy_given )
{
char * phost;
int pport;
if (args_info->proxy_given ) {
char * phost;
int pport;
phost = malloc( 50+1 );
phost = malloc( 50+1 );
//fprintf( stderr, "%s: proxyhost (pre parse) given, it is: '%s'\n", PACKAGE, args_info->proxy_arg );
// fprintf( stderr, "%s: proxyhost (pre parse) given, it is: '%s'\n", PACKAGE, args_info->proxy_arg );
r = sscanf( args_info->proxy_arg, "%50[^:]:%5u", phost, &pport );
if ( r == 2 )
{
args_info->proxyhost_arg = phost;
args_info->proxyport_arg = pport;
args_info->proxyhost_given = 1;
args_info->proxyport_given = 1;
}
else
{
message( "parse_cmdline: couln't find your proxy hostname/ip\n" );
missing_required_options++;
r = sscanf( args_info->proxy_arg, "%50[^:]:%5u", phost, &pport );
if ( r == 2 ) {
args_info->proxyhost_arg = phost;
args_info->proxyport_arg = pport;
args_info->proxyhost_given = 1;
args_info->proxyport_given = 1;
} else {
message( "parse_cmdline: couln't find your proxy hostname/ip\n" );
missing_required_options++;
}
// message( "%s: proxyhost (post parse) is '%s':'%d'\n", PACKAGE, args_info->proxyhost_arg, args_info->proxyport_arg );
}
//message( "%s: proxyhost (post parse) is '%s':'%d'\n", PACKAGE, args_info->proxyhost_arg, args_info->proxyport_arg );
}
if ( missing_required_options )
exit (1);
if ( missing_required_options )
exit (1);
return 0;
return 0;
}
static char *getCredentialsFromFile( const char* filename, char **user, char **pwd )
{
/* Check file permissions, must have '0' for group and other */
struct stat statbuf;
if ( stat( filename, &statbuf ) == 0 ) {
if ( statbuf.st_mode & (S_IRWXG|S_IRWXO) ) {
return strdup( "Stricter permissions required for password file" );
static char *
getCredentialsFromFile( const char* filename, char **user, char **pwd ) {
/* Check file permissions, must have '0' for group and other */
struct stat statbuf;
if ( stat( filename, &statbuf ) == 0 ) {
if ( statbuf.st_mode & (S_IRWXG|S_IRWXO) ) {
return strdup( "Stricter permissions required for password file" );
}
} else {
return strdup( strerror(errno) );
}
} else {
return strdup( strerror(errno) );
}
FILE* pwfile = fopen( filename, "r" );
char line[80], buf[80];
FILE* pwfile = fopen( filename, "r" );
char line[80], buf[80];
*user = NULL;
*pwd = NULL;
*user = NULL;
*pwd = NULL;
if( pwfile )
{
// Read a line
while (fgets( line, 80, pwfile ) != NULL ) {
if ( sscanf( line, "proxy_user = %s", buf ) == 1 ) {
*user = strdup( buf );
} else if ( sscanf( line, "proxy_passwd = %s", buf ) == 1 ) {
*pwd = strdup( buf );
}
if ( *user != NULL && *pwd != NULL ) {
if( pwfile ) {
/* Read a line */
while (fgets( line, 80, pwfile ) != NULL ) {
if ( sscanf( line, "proxy_user = %s", buf ) == 1 ) {
*user = strdup( buf );
} else if ( sscanf( line, "proxy_passwd = %s", buf ) == 1 ) {
*pwd = strdup( buf );
}
if ( *user != NULL && *pwd != NULL ) {
fclose( pwfile );
return NULL;
}
}
fclose( pwfile );
return NULL;
}
return strdup( "proxy_user & proxy_passwd not found in password file" );
}
fclose( pwfile );
return strdup( "proxy_user & proxy_passwd not found in password file" );
}
return strdup( "Error opening password file" );
return strdup( "Error opening password file" );
}
// vim:noet

2
http.c
View file

@ -1,4 +1,4 @@
/* Proxytunnel - (C) 2001-2006 Jos Visser / Mark Janssen */
/* Proxytunnel - (C) 2001-2008 Jos Visser / Mark Janssen */
/* Contact: josv@osp.nl / maniac@maniac.nl */
/*

78
io.c
View file

@ -1,4 +1,4 @@
/* Proxytunnel - (C) 2001-2006 Jos Visser / Mark Janssen */
/* Proxytunnel - (C) 2001-2008 Jos Visser / Mark Janssen */
/* Contact: josv@osp.nl / maniac@maniac.nl */
/*
@ -34,20 +34,14 @@
* Read one line of data from the tunnel. Line is terminated by a
* newline character. Result is stored in buf.
*/
int readline(PTSTREAM *pts)
{
int readline(PTSTREAM *pts) {
char *p = buf;
char c = 0;
int i = 0;
/*
* Read one character at a time into buf, until a newline is
* encountered.
*/
while ( c != 10 && ( i < SIZE - 1 ) )
{
if( stream_read( pts, &c ,1) < 0)
{
/* Read one character at a time into buf, until a newline is encountered. */
while ( c != 10 && ( i < SIZE - 1 ) ) {
if( stream_read( pts, &c ,1) < 0) {
my_perror( "Socket read error" );
exit( 1 );
}
@ -59,8 +53,7 @@ int readline(PTSTREAM *pts)
*p = 0;
if( args_info.verbose_flag )
{
if( args_info.verbose_flag ) {
/* Copy line of data into dstr without trailing newline */
char * dstr = malloc(sizeof(buf) + 1);
strlcpy( dstr, buf, strlen(buf) - 1);
@ -70,67 +63,46 @@ int readline(PTSTREAM *pts)
return strlen( buf );
}
/*
* Bond stream1 and stream2 together; any data received in stream1 is relayed
* to stream2, and vice-versa.
*/
void cpio(PTSTREAM *stream1, PTSTREAM *stream2)
{
fd_set readfds;
fd_set writefds;
fd_set exceptfds;
int in_max_fd, out_max_fd, max_fd;
void cpio(PTSTREAM *stream1, PTSTREAM *stream2) {
fd_set readfds;
fd_set writefds;
fd_set exceptfds;
int in_max_fd, out_max_fd, max_fd;
/*
* Find the biggest file descriptor for select()
*/
/* Find the biggest file descriptor for select() */
in_max_fd = MAX(stream_get_incoming_fd(stream1), stream_get_incoming_fd(stream2));
out_max_fd = MAX(stream_get_outgoing_fd(stream1), stream_get_outgoing_fd(stream2));
max_fd = MAX(in_max_fd, out_max_fd);
/*
* We're never interested in sockets being available for write.
*/
/* We're never interested in sockets being available for write. */
FD_ZERO( &writefds );
if( args_info.verbose_flag )
message( "\nTunnel established.\n" );
/*
* Only diamonds are forever :-)
*/
while( 1==1 )
{
/*
* Clear the interesting socket sets
*/
/* Only diamonds are forever :-) */
while( 1==1 ) {
/* Clear the interesting socket sets */
FD_ZERO( &readfds );
FD_ZERO( &exceptfds );
/*
* We want to know whether stream1 or stream2 is ready for reading
*/
/* We want to know whether stream1 or stream2 is ready for reading */
FD_SET( stream_get_incoming_fd(stream1), &readfds );
FD_SET( stream_get_incoming_fd(stream2), &readfds );
/*
* And we want to know about exceptional conditions on either stream
*/
/* And we want to know about exceptional conditions on either stream */
FD_SET( stream_get_incoming_fd(stream1), &exceptfds );
FD_SET( stream_get_outgoing_fd(stream1), &exceptfds );
FD_SET( stream_get_incoming_fd(stream2), &exceptfds );
FD_SET( stream_get_outgoing_fd(stream2), &exceptfds );
/*
* Wait until something happens on one of the registered
* sockets/files
*/
if ( select( max_fd + 1, &readfds, &writefds,
&exceptfds, 0 ) < 0 )
{
/* Wait until something happens on the registered sockets/files */
if ( select( max_fd + 1, &readfds, &writefds, &exceptfds, 0 ) < 0 ) {
perror("select error");
exit(1);
}
@ -142,18 +114,14 @@ void cpio(PTSTREAM *stream1, PTSTREAM *stream2)
* stream2 to stream1. Otherwise an exceptional condition
* is flagged and the program is terminated.
*/
if ( FD_ISSET( stream_get_incoming_fd(stream1), &readfds ) )
{
if ( FD_ISSET( stream_get_incoming_fd(stream1), &readfds ) ) {
if ( stream_copy(stream1, stream2 ) )
break;
}
else if( FD_ISSET( stream_get_incoming_fd(stream2), &readfds ) )
{
else if( FD_ISSET( stream_get_incoming_fd(stream2), &readfds ) ) {
if( stream_copy(stream2, stream1 ) )
break;
}
else
{
} else {
my_perror( "Exceptional condition" );
break;
}