C   61

prox2.c

Guest on 25th April 2022 12:08:34 PM

  1. /*
  2.  
  3.   Ok.. so to explain.. this is basically a C program that can run with a modified version of
  4.   the proxy2ssh script. It's useful if you don't want to run the cgi from apache.. this is a
  5.   standalone program that listens on it's own 'http' port and then invokes sshd and hands it
  6.   the connected sockets as stdin/stdout.
  7.  
  8.   I wrote it as an experiment and actually
  9.   it does have a bit less lag than the cgi version. It really does need quite a bit of work
  10.   before a release, but what the hell.. those of you who find it here may well be able to
  11.   figure out how to use it or at least use the concept to write a better one yourself.
  12.  
  13.   It needs to run as root - this isn't really a requirement, but if it doesn't run
  14.   as root, you need to supply more parameters to sshd including it's own private key
  15.   file that it can read. I couldn't be bothered to setup a seperate key and config for
  16.   this sshd so I'm just using the (default) system one which requires root. Also if you
  17.   dont' run it as root then you can only log in as the user that's running this (obviously).
  18.  
  19.   To compile it.. should be fairly simple;
  20.  
  21.   gcc -o prox2 prox2.c
  22.  
  23.  
  24.   If you want to try it and can't get it to work please feel free to email me.
  25.  
  26.   Robert McKay
  27.  
  28.  
  29. */
  30.  
  31.  
  32. #include <stdio.h>
  33. #include <sys/types.h>
  34. #include <sys/socket.h>
  35. #include <stdlib.h>
  36. #include <unistd.h>
  37. #include <arpa/inet.h>
  38.  
  39.  
  40. extern char **environ;
  41.  
  42. int
  43. main (int argc, char *argv[])
  44. {
  45.    int rfds[2];
  46.    int wfds[2];
  47.    int m_getfd,c_getfd;
  48.    int m_postfd,c_postfd;
  49.    int ret;
  50.    struct sockaddr_in gets,c_gets;
  51.    struct sockaddr_in posts,c_posts;
  52.  
  53.    int rfd = rfds[1];
  54.    int wfd = wfds[1];
  55.    char *params[] = { "/usr/sbin/sshd", "-i", 0 };
  56.  
  57.  
  58.  
  59.    socklen_t getslen,postslen,c_getslen,c_postslen;
  60.  
  61.    ret=socketpair(PF_UNIX, SOCK_STREAM, 0, rfds);
  62.    ret=socketpair(PF_UNIX, SOCK_STREAM, 0, wfds);
  63.  
  64.  
  65.    m_getfd = socket(PF_INET, SOCK_STREAM, 0);
  66.    m_postfd = socket(PF_INET, SOCK_STREAM, 0);
  67.  
  68.    gets.sin_family = AF_INET;
  69.    gets.sin_port = htons(9080);
  70.    ret=inet_pton(AF_INET, "0.0.0.0", &gets.sin_addr);
  71.  
  72.    posts.sin_family = AF_INET;
  73.    posts.sin_port = htons(9081);
  74.    ret=inet_pton(AF_INET, "0.0.0.0", &posts.sin_addr);
  75.  
  76.    ret=bind(m_getfd, (struct sockaddr *)&gets, sizeof(gets));
  77.    //ret=bind(m_postfd, (struct sockaddr *)&posts, sizeof(posts));
  78.  
  79.    listen(m_getfd, 2);
  80.    //listen(m_postfd, 1);
  81.  
  82.    c_getslen = sizeof(c_gets);
  83.    c_getfd = accept(m_getfd, (struct sockaddr *)&c_gets, &c_getslen);
  84.    usleep(500); // wait for req
  85.    dprintf(c_getfd, "\nKey: wtfkey\n" );
  86.  
  87.    c_postfd = accept(m_getfd, (struct sockaddr *)&c_gets, &c_getslen);
  88.    perror("wtf?");
  89.    int i=0;
  90.    for (i=0;i<9;i++)
  91.    {
  92.      char c=0;
  93.      while(c!='\n')
  94.      {
  95.        ret=read(c_postfd, &c, 1);
  96.      }
  97.    }
  98.  
  99.    // set stdin/stdout/stderr to our side of the socketpair
  100.    dup2(c_postfd, 0);
  101.    dup2(c_getfd, 1);
  102.    dup2(c_getfd, 2);
  103.  
  104.    execve("/usr/sbin/sshd", params, environ );
  105.  
  106. }

Raw Paste


Login or Register to edit or fork this paste. It's free.