<commit_before>#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>

#include <sys/param.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>

#ifdef WITHSSL
#include <openssl/ssl.h>
#include <openssl/err.h>
#endif /* WITHSSL */

#include "capdecls.h"

int debug = 0;
int allowRemote = 0;

unsigned short port = 0;
int sock = 0;

unsigned short tunnelPort = 0;
int tunnelSock = 0;

char * programToLaunch = NULL;
char * hostname = NULL;

secretkey_t key;

typedef int WriteFunc( void * data, int size );
typedef int ReadFunc( void * data, int size );

WriteFunc * writeFunc;
ReadFunc  * readFunc;

void usage();
void loadAcl( const char * filename );
void doConnect();
void doAuthenticate();
void doCreateTunnel();
void doTunnelConnection();

int writeNormal( void * data, int size );
int readNormal( void * data, int size );

#ifdef WITHSSL

void initSSL();
void sslConnect();

int writeSSL( void * data, int size );
int readSSL( void * data, int size );

SSL * ssl;
SSL_CTX * ctx;

int usingSSL = 0;
char * certString = NULL;

#endif /* WITHSSL */

int main( int argc, char ** argv )
{
  int op;

  while ((op = getopt( argc, argv, "sp:rd" )) != -1) {
    switch (op) {
      case 'p':
        tunnelPort = atoi( optarg );
	break;
      case 'd':
	debug++;
	break;
      case 'r':
	allowRemote++;
	break;
    }
  }

  argc -= optind;
  argv += optind;

  if (argc < 1) {
    usage();
  }

  if (argc > 1) {
    programToLaunch = strdup( argv[1] );    
  }

  loadAcl( argv[0] );

#ifdef WITHSSL
  if (usingSSL) { 
    writeFunc = writeSSL;
    readFunc  = readSSL;
    initSSL();
    doConnect();
    sslConnect();
  } else
#endif /* WITHSSL */
  {
    /* either there is no SSL, or they're not using it. */
    writeFunc = writeNormal;
    readFunc  = readNormal;
    doConnect();
  }

  doAuthenticate();
  doCreateTunnel();

  if (programToLaunch) {
    char portString[12];
    sprintf( portString, "%i", tunnelPort );
    if (debug) printf("Launching %s with args localhost, %s.\n",
		      programToLaunch, portString );
    if (!fork()) {
      execlp( programToLaunch, 
	      programToLaunch, "localhost", portString, NULL );
    }
  }
  doTunnelConnection();
  if (debug) { printf("tiptunnel closing.\n"); }
}

void usage()
{
  printf("Usage:\n"
	 "tiptunnel [-p <portnum>] [-d] [-r] aclfile [program]\n"
	 "-p <portnum>     specifies tunnel port number\n"
	 "-d               turns on more verbose messages\n"
	 "-r               allows connections to tunnel from non-localhost\n"
	 "\n"
	 "[program]        path of program to launch with 'localhost'\n"
	 "                 and tunnel port as arguments.\n"
	 //"-k keeps accepting connections until killed"
	 );
  exit(-1);
}

void loadAcl( const char * filename )
{
  FILE * aclFile = fopen( filename, "r" );<commit_msg>Added more powerful command specification, and useful default.<commit_after>#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>

#include <sys/param.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>

#ifdef WITHSSL
#include <openssl/ssl.h>
#include <openssl/err.h>
#endif /* WITHSSL */

#include "capdecls.h"


#define DEFAULT_PROGRAM "xterm -T TIP -e telnet %s %s"

int debug = 0;
int allowRemote = 0;

unsigned short port = 0;
int sock = 0;

unsigned short tunnelPort = 0;
int tunnelSock = 0;

char * programToLaunch = NULL;
char * hostname = NULL;

secretkey_t key;

typedef int WriteFunc( void * data, int size );
typedef int ReadFunc( void * data, int size );

WriteFunc * writeFunc;
ReadFunc  * readFunc;

void usage();
void loadAcl( const char * filename );
void doConnect();
void doAuthenticate();
void doCreateTunnel();
void doTunnelConnection();

int writeNormal( void * data, int size );
int readNormal( void * data, int size );

#ifdef WITHSSL

void initSSL();
void sslConnect();

int writeSSL( void * data, int size );
int readSSL( void * data, int size );

SSL * ssl;
SSL_CTX * ctx;

int usingSSL = 0;
char * certString = NULL;

#endif /* WITHSSL */

int main( int argc, char ** argv )
{
  int op;

  while ((op = getopt( argc, argv, "sp:rd" )) != -1) {
    switch (op) {
      case 'p':
        tunnelPort = atoi( optarg );
	break;
      case 'd':
	debug++;
	break;
      case 'r':
	allowRemote++;
	break;
    }
  }

  argc -= optind;
  argv += optind;

  if (argc < 1) {
    usage();
  }

  if (argc > 1) {
    programToLaunch = strdup( argv[1] );    
  } else {
    programToLaunch = DEFAULT_PROGRAM;
  }

  loadAcl( argv[0] );

#ifdef WITHSSL
  if (usingSSL) { 
    writeFunc = writeSSL;
    readFunc  = readSSL;
    initSSL();
    doConnect();
    sslConnect();
  } else
#endif /* WITHSSL */
  {
    /* either there is no SSL, or they're not using it. */
    writeFunc = writeNormal;
    readFunc  = readNormal;
    doConnect();
  }

  doAuthenticate();
  doCreateTunnel();

  if (programToLaunch) {
    char portString[12];
    char runString[1024];
    char * foo;

    sprintf( portString, "%i", tunnelPort );

    /*
      if (debug) printf("Launching %s with args localhost, %s.\n",
		      programToLaunch, portString );
    */

    sprintf(runString, programToLaunch, "localhost", portString);
    for (foo = runString; *foo; foo++);
    foo[0] = ' ';
    foo[1] = '&';
    foo[2] = '\0';
    if (debug) printf("Running '%s'\n", runString);
    system( runString );
    /*
    if (!fork()) {
      execlp( programToLaunch, 
	      programToLaunch, "localhost", portString, NULL );
    }
    */
  }
  doTunnelConnection();
  if (debug) { printf("tiptunnel closing.\n"); }
}

void usage()
{
  printf("Usage:\n"
	 "tiptunnel [-p <portnum>] [-d] [-r] aclfile [program]\n"
	 "-p <portnum>     specifies tunnel port number\n"
	 "-d               turns on more verbose messages\n"
	 "-r               allows connections to tunnel from non-localhost\n"
	 "\n"
	 "[program]        path of program to launch; default is\n"
	 "                 \"%s\"\n",
         DEFAULT_PROGRAM
	 //"-k keeps accepting connections until killed"
	 );
  exit(-1);
}

void loadAcl( const char * filename )
{
  FILE * aclFile = fopen( filename, "r" );
{'fraction_non_alphanumeric': 0.14834177421989872, 'fraction_numerical': 0.006208135925502369, 'mean_word_length': 2.8771374287523748, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5278925', 'n_tokens_mistral': 2203, 'n_tokens_neox': 2108, 'n_words': 636}