Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <unistd.h>
- #include <fcntl.h>
- #include <stdio.h>
- #include <unistd.h>
- #include <stdlib.h>
- #include <memory.h>
- #include <errno.h>
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <arpa/inet.h>
- #include <netdb.h>
- #include <time.h>
- #include <openssl/rsa.h>
- #include <openssl/crypto.h>
- #include <openssl/x509.h>
- #include <openssl/pem.h>
- #include <openssl/ssl.h>
- #include <openssl/err.h>
- #include <ctype.h>
- #include <vector>
- #include <signal.h>
- #include <wait.h>
- #include <sys/ipc.h>
- #include <sys/msg.h>
- #include <semaphore.h>
- #include <string>
- /* define HOME to be dir for key and cert files... */
- #define HOME "./keys/"
- /* Make these what you want for cert & key files */
- #define CERTF HOME "cert.crt"
- #define KEYF HOME "key.pem"
- #define CHK_NULL(x) if ((x)==NULL) exit (1)
- #define CHK_ERR(err,s) if ((err)==-1) { perror(s); exit(1); }
- #define CHK_SSL(err) if ((err)==-1) { ERR_print_errors_fp(stderr); exit(2); }
- int simpleReadSSL(SSL *ssl, int sd, char *data, int lenght)
- {
- char buf[512];
- fd_set readset;
- FD_ZERO(&readset);
- FD_SET(sd,&readset);
- int sel = select(sd + 1,&readset,NULL,NULL,NULL);
- if(FD_ISSET(sd,&readset))
- {
- int l = SSL_read(ssl,buf,512);
- if(l<=0) return 0;
- else
- {
- buf[l] = '\0';
- strcpy(data,buf);
- return l;
- }
- }
- }
- int main(int argc, char **argv) {
- int err;
- int master_socket;
- int sd;
- struct sockaddr_in sa_serv;
- struct sockaddr_in sa_cli;
- socklen_t client_len;
- SSL_CTX* ctx;
- X509* client_cert;
- const SSL_METHOD *meth;
- SSL_load_error_strings();
- SSLeay_add_ssl_algorithms();
- meth = SSLv23_server_method();
- ctx = SSL_CTX_new(meth);
- //Settings for server !!
- if (!ctx) {
- ERR_print_errors_fp(stderr);
- exit(2);
- }
- if (SSL_CTX_use_certificate_file(ctx, CERTF, SSL_FILETYPE_PEM) <= 0) {
- ERR_print_errors_fp(stderr);
- exit(3);
- };
- if (SSL_CTX_use_PrivateKey_file(ctx, KEYF, SSL_FILETYPE_PEM) <= 0) {
- ERR_print_errors_fp(stderr);
- exit(4);
- }
- if (!SSL_CTX_check_private_key(ctx)) {
- fprintf(stderr,
- "Private key does not match the certificate public key\n");
- exit(5);
- }
- master_socket = socket(AF_INET, SOCK_STREAM, 0);
- CHK_ERR(master_socket, " listen socket");
- int enable = 1;
- setsockopt(master_socket, SOL_SOCKET, SO_REUSEADDR, &enable,
- sizeof(enable));
- memset(&sa_serv, '\0', sizeof(sa_serv));
- sa_serv.sin_family = AF_INET;
- sa_serv.sin_addr.s_addr = INADDR_ANY;
- sa_serv.sin_port = htons(1111); /* Server Port number */
- err = bind(master_socket, (struct sockaddr*) &sa_serv, sizeof(sa_serv));
- CHK_ERR(err, "bind");
- /* Receive a TCP connection. */
- err = listen(master_socket, 5);
- CHK_ERR(err, "listen");
- while (1) {
- client_len = sizeof(sa_cli);
- sd = accept(master_socket, (struct sockaddr*) &sa_cli, &client_len);
- CHK_ERR(sd, "accept");
- printf("Connection from %s, port %d\n", inet_ntoa(sa_cli.sin_addr),
- sa_cli.sin_port);
- SSL* ssl = SSL_new(ctx);
- CHK_NULL(ssl);
- SSL_set_fd(ssl, sd);
- err = SSL_accept(ssl);
- CHK_SSL(err);
- printf("SSL connection using %s\n", SSL_get_cipher(ssl));
- if (fork() == 0) {
- char buf[99999];
- int p = 0;
- while (1) {
- int l = simpleReadSSL(ssl,sd,buf,sizeof(buf)-1);
- char *a = strstr(buf,"GET");
- char* posL = strchr(a,'/');
- char *posS = strchr(posL,' ');
- std::string file = posL;
- std::string f = file.substr(1, file.find(" "));
- printf("filename: %s \n",f.c_str());
- int n =0;
- int mypipe[ 2 ];
- pipe(mypipe);
- if(fork()==0)
- {
- close(mypipe[0]);
- dup2( mypipe[1], STDOUT_FILENO );
- close(mypipe[1]);
- char *argm[] = {"wc", "-l", "soubor"};
- execvp(argm[0], argm);
- }else
- {
- close(mypipe[1]);
- int i;
- dup2( mypipe[ 0 ], STDIN_FILENO );
- close(mypipe[0]);
- scanf( "%d", &i );
- printf("num: %d",i);
- }
- close(sd);
- exit(0);
- }
- }
- close(sd);
- }
- }
- /* EOF - ssl_srv.cpp */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement