Advertisement
Guest User

Untitled

a guest
Jan 17th, 2019
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.98 KB | None | 0 0
  1. #include <unistd.h>
  2. #include <fcntl.h>
  3. #include <stdio.h>
  4. #include <unistd.h>
  5. #include <stdlib.h>
  6. #include <memory.h>
  7. #include <errno.h>
  8. #include <sys/types.h>
  9. #include <sys/socket.h>
  10. #include <netinet/in.h>
  11. #include <arpa/inet.h>
  12. #include <netdb.h>
  13. #include <time.h>
  14.  
  15. #include <openssl/rsa.h>
  16. #include <openssl/crypto.h>
  17. #include <openssl/x509.h>
  18. #include <openssl/pem.h>
  19. #include <openssl/ssl.h>
  20. #include <openssl/err.h>
  21.  
  22. #include <ctype.h>
  23. #include <vector>
  24. #include <signal.h>
  25. #include <wait.h>
  26.  
  27. #include <sys/ipc.h>
  28. #include <sys/msg.h>
  29. #include <semaphore.h>
  30.  
  31. #include <string>
  32.  
  33. /* define HOME to be dir for key and cert files... */
  34. #define HOME "./keys/"
  35. /* Make these what you want for cert & key files */
  36. #define CERTF HOME "cert.crt"
  37. #define KEYF HOME "key.pem"
  38.  
  39. #define CHK_NULL(x) if ((x)==NULL) exit (1)
  40. #define CHK_ERR(err,s) if ((err)==-1) { perror(s); exit(1); }
  41. #define CHK_SSL(err) if ((err)==-1) { ERR_print_errors_fp(stderr); exit(2); }
  42.  
  43. int simpleReadSSL(SSL *ssl, int sd, char *data, int lenght)
  44. {
  45. char buf[512];
  46. fd_set readset;
  47. FD_ZERO(&readset);
  48. FD_SET(sd,&readset);
  49. int sel = select(sd + 1,&readset,NULL,NULL,NULL);
  50. if(FD_ISSET(sd,&readset))
  51. {
  52. int l = SSL_read(ssl,buf,512);
  53. if(l<=0) return 0;
  54. else
  55. {
  56. buf[l] = '\0';
  57. strcpy(data,buf);
  58. return l;
  59. }
  60. }
  61.  
  62.  
  63. }
  64. int main(int argc, char **argv) {
  65. int err;
  66. int master_socket;
  67. int sd;
  68. struct sockaddr_in sa_serv;
  69. struct sockaddr_in sa_cli;
  70. socklen_t client_len;
  71. SSL_CTX* ctx;
  72.  
  73. X509* client_cert;
  74. const SSL_METHOD *meth;
  75. SSL_load_error_strings();
  76. SSLeay_add_ssl_algorithms();
  77. meth = SSLv23_server_method();
  78. ctx = SSL_CTX_new(meth);
  79.  
  80. //Settings for server !!
  81.  
  82. if (!ctx) {
  83. ERR_print_errors_fp(stderr);
  84. exit(2);
  85. }
  86. if (SSL_CTX_use_certificate_file(ctx, CERTF, SSL_FILETYPE_PEM) <= 0) {
  87. ERR_print_errors_fp(stderr);
  88. exit(3);
  89. };
  90. if (SSL_CTX_use_PrivateKey_file(ctx, KEYF, SSL_FILETYPE_PEM) <= 0) {
  91. ERR_print_errors_fp(stderr);
  92. exit(4);
  93. }
  94. if (!SSL_CTX_check_private_key(ctx)) {
  95. fprintf(stderr,
  96. "Private key does not match the certificate public key\n");
  97. exit(5);
  98. }
  99.  
  100. master_socket = socket(AF_INET, SOCK_STREAM, 0);
  101. CHK_ERR(master_socket, " listen socket");
  102. int enable = 1;
  103. setsockopt(master_socket, SOL_SOCKET, SO_REUSEADDR, &enable,
  104. sizeof(enable));
  105.  
  106. memset(&sa_serv, '\0', sizeof(sa_serv));
  107. sa_serv.sin_family = AF_INET;
  108. sa_serv.sin_addr.s_addr = INADDR_ANY;
  109. sa_serv.sin_port = htons(1111); /* Server Port number */
  110.  
  111. err = bind(master_socket, (struct sockaddr*) &sa_serv, sizeof(sa_serv));
  112. CHK_ERR(err, "bind");
  113.  
  114. /* Receive a TCP connection. */
  115. err = listen(master_socket, 5);
  116. CHK_ERR(err, "listen");
  117.  
  118. while (1) {
  119. client_len = sizeof(sa_cli);
  120. sd = accept(master_socket, (struct sockaddr*) &sa_cli, &client_len);
  121. CHK_ERR(sd, "accept");
  122. printf("Connection from %s, port %d\n", inet_ntoa(sa_cli.sin_addr),
  123. sa_cli.sin_port);
  124. SSL* ssl = SSL_new(ctx);
  125. CHK_NULL(ssl);
  126. SSL_set_fd(ssl, sd);
  127. err = SSL_accept(ssl);
  128. CHK_SSL(err);
  129. printf("SSL connection using %s\n", SSL_get_cipher(ssl));
  130. if (fork() == 0) {
  131. char buf[99999];
  132. int p = 0;
  133. while (1) {
  134.  
  135. int l = simpleReadSSL(ssl,sd,buf,sizeof(buf)-1);
  136. char *a = strstr(buf,"GET");
  137. char* posL = strchr(a,'/');
  138. char *posS = strchr(posL,' ');
  139. std::string file = posL;
  140.  
  141. std::string f = file.substr(1, file.find(" "));
  142. printf("filename: %s \n",f.c_str());
  143.  
  144. int n =0;
  145.  
  146. int mypipe[ 2 ];
  147. pipe(mypipe);
  148.  
  149. if(fork()==0)
  150. {
  151. close(mypipe[0]);
  152. dup2( mypipe[1], STDOUT_FILENO );
  153. close(mypipe[1]);
  154. char *argm[] = {"wc", "-l", "soubor"};
  155. execvp(argm[0], argm);
  156.  
  157. }else
  158. {
  159.  
  160. close(mypipe[1]);
  161. int i;
  162. dup2( mypipe[ 0 ], STDIN_FILENO );
  163. close(mypipe[0]);
  164. scanf( "%d", &i );
  165. printf("num: %d",i);
  166. }
  167.  
  168.  
  169.  
  170. close(sd);
  171. exit(0);
  172.  
  173.  
  174.  
  175. }
  176. }
  177.  
  178. close(sd);
  179.  
  180. }
  181.  
  182. }
  183. /* EOF - ssl_srv.cpp */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement