Advertisement
Guest User

Untitled

a guest
Dec 3rd, 2016
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 23.85 KB | None | 0 0
  1. #undef UNICODE
  2.  
  3. #define WIN32_LEAN_AND_MEAN
  4.  
  5. #include <sys/types.h>
  6. #include <sys/stat.h>
  7. #include <windows.h>
  8. #include <winsock2.h>
  9. #include <ws2tcpip.h>
  10. #include <sstream>
  11. #include <stdlib.h>
  12. #include <stdio.h>
  13. #include <iostream>
  14. #include <sstream>
  15. #include <time.h>
  16. #include <fstream>
  17. #include <cstdio>
  18. #include <memory>
  19. #include <stdexcept>
  20. #include "sqlite3.h"
  21. #include <vector>
  22.  
  23. // Need to link with Ws2_32.lib
  24. #pragma comment (lib, "Ws2_32.lib")
  25. // #pragma comment (lib, "Mswsock.lib")
  26. #define DEFAULT_BUFLEN 1024
  27. #define DEFAULT_PORT "8888"
  28.  
  29.  
  30. using namespace std;
  31.  
  32.  
  33.  
  34.  
  35. std::string exec(const char* cmd) {
  36. char buffer[128];
  37. std::string result = "";
  38. std::shared_ptr<FILE> pipe(_popen(cmd, "r"), _pclose);
  39. if (!pipe) throw std::runtime_error("popen() failed!");
  40. while (!feof(pipe.get())) {
  41. if (fgets(buffer, 128, pipe.get()) != NULL)
  42. result += buffer;
  43. }
  44. return result;
  45. }
  46.  
  47.  
  48. string getIPv4(string message)
  49. {
  50. int size = message.length();
  51. if ( size != 0)
  52. {
  53. char c = 'a';
  54. int spot = 0;
  55. int arraySpot = 0;
  56. string word = "";
  57. bool found = false;
  58.  
  59. do
  60. {
  61. if (message[spot] != ' ')
  62. {
  63. word += message[spot];
  64. }
  65. else
  66. {
  67.  
  68. if (word == "IPv4")
  69. {
  70. word = "";
  71. spot += 32;
  72.  
  73. while (message[spot] != '\n')
  74. {
  75. word += message[spot];
  76. spot++;
  77.  
  78. }
  79.  
  80. found = true;
  81. }
  82. else
  83. {
  84. word = "";
  85. }
  86. }
  87. spot++;
  88.  
  89.  
  90. } while ((size != spot) && (!found));
  91.  
  92. return word;
  93. }
  94.  
  95. return 0;
  96. }
  97.  
  98. vector<string> fromRequestToWords(string message)
  99. {
  100. int size = message.length();
  101. vector < string > words;
  102. if (size != 0)
  103. {
  104. char c = 'a';
  105. int spot = 0;
  106. int arraySpot = 0;
  107. string word = "";
  108.  
  109.  
  110. do
  111. {
  112. if (message[spot] != ' ')
  113. {
  114. word += message[spot];
  115. }
  116. else
  117. {
  118. words.push_back(word);
  119. word = "";
  120. arraySpot++;
  121.  
  122. }
  123. spot++;
  124.  
  125.  
  126. } while (size != spot);
  127.  
  128.  
  129. }
  130.  
  131. return words;
  132. }
  133.  
  134.  
  135. string getResquest(string message)
  136. {
  137. int size = message.length();
  138. if ( size != 0)
  139. {
  140. char c = 'a';
  141. int spot = 0;
  142.  
  143. string word = "";
  144.  
  145. do
  146. {
  147. if (message[spot] != ' ')
  148. {
  149. word += message[spot];
  150. }
  151.  
  152. spot++;
  153.  
  154.  
  155. } while (message[spot] != ' ');
  156. return word;
  157.  
  158. }
  159.  
  160. return 0;
  161. }
  162.  
  163. int callbackExistingUsernameOrAddedUsername(void* notUsed, int argc, char** argv, char** azCol) //returns if the command was successful (of sql)
  164. {
  165. if (argv[0] == NULL)
  166. {
  167. return -1;
  168. }
  169.  
  170. return 1;
  171. }
  172.  
  173. bool isUserExists(sqlite3 *db,string username) //checked
  174. {
  175.  
  176. char** err = NULL;
  177. string a = "SELECT username FROM users WHERE username = '" + username + "';";
  178. const char* line = a.c_str();
  179. int rc2 = sqlite3_exec(db, line, callbackExistingUsernameOrAddedUsername, NULL, err);
  180.  
  181. if ((rc2 == 0) || (rc2 == -1))
  182. {
  183. return false;
  184. }
  185.  
  186. return true;
  187.  
  188. }
  189.  
  190. bool usernameAndPasswordMatch(sqlite3* db,string username, string password)
  191. {
  192.  
  193. if (isUserExists(db,username))
  194. {
  195.  
  196. char** err = NULL;
  197. string a = "SELECT username FROM users WHERE userName = '" + username + "' and password = '" + password + "' ;";
  198. const char* line = a.c_str();
  199. int rc2 = sqlite3_exec(db, line, callbackExistingUsernameOrAddedUsername, NULL, err);
  200.  
  201. if ((rc2 == 0) || (rc2 == -1))
  202. {
  203. return false;
  204. }
  205. sqlite3_free(err);
  206. return true;
  207.  
  208. }
  209.  
  210.  
  211. return false;
  212. }
  213.  
  214.  
  215. string decryption(string message)
  216. {
  217.  
  218. string output = message;
  219. int size = message.size();
  220.  
  221. for (int i = 0; i < size; i++)
  222. output[i] = message[i] ^ 'K';
  223.  
  224. for (int i = 0; i < size; i++)
  225. output[i] = message[i] ^ 'w';
  226.  
  227. for (int i = 0; i < size; i++)
  228. output[i] = message[i] ^ 'z';
  229.  
  230. return output;
  231.  
  232.  
  233.  
  234. }
  235.  
  236. int main(void)
  237. {
  238. time_t start = time(0);
  239. sqlite3 *db;
  240. int rc;
  241. try
  242. {
  243. rc = sqlite3_open("Users.db", &db);
  244. if (rc)
  245. {
  246. throw sqlite3_errmsg(db);
  247. }
  248. }
  249. catch (std::exception &ex)
  250. {
  251. std::cout << ex.what() << std::endl;
  252. }
  253.  
  254. WSADATA wsaData;
  255. int iResult;
  256. int closeConnection = 0;
  257.  
  258. SOCKET ListenSocket = INVALID_SOCKET;
  259. SOCKET ClientSocket = INVALID_SOCKET;
  260.  
  261. struct addrinfo *result = NULL;
  262. struct addrinfo hints;
  263.  
  264. int iSendResult;
  265.  
  266. int recvbuflen = DEFAULT_BUFLEN;
  267.  
  268. // Initialize Winsock
  269. iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
  270. if (iResult != 0) {
  271. printf("WSAStartup failed with error: %d\n", iResult);
  272. return 1;
  273. }
  274.  
  275. ZeroMemory(&hints, sizeof(hints));
  276. hints.ai_family = AF_INET;
  277. hints.ai_socktype = SOCK_STREAM;
  278. hints.ai_protocol = IPPROTO_TCP;
  279. hints.ai_flags = AI_PASSIVE;
  280.  
  281. // Resolve the server address and port
  282. iResult = getaddrinfo(NULL, DEFAULT_PORT, &hints, &result);
  283. if (iResult != 0) {
  284. printf("getaddrinfo failed with error: %d\n", iResult);
  285. WSACleanup();
  286. return 1;
  287. }
  288.  
  289. // Create a SOCKET for connecting to server
  290. ListenSocket = socket(result->ai_family, result->ai_socktype, result->ai_protocol);
  291. if (ListenSocket == INVALID_SOCKET) {
  292. printf("socket failed with error: %ld\n", WSAGetLastError());
  293. freeaddrinfo(result);
  294. WSACleanup();
  295. return 1;
  296. }
  297.  
  298. // Setup the TCP listening socket
  299. iResult = bind(ListenSocket, result->ai_addr, (int)result->ai_addrlen);
  300. if (iResult == SOCKET_ERROR) {
  301. printf("bind failed with error: %d\n", WSAGetLastError());
  302. freeaddrinfo(result);
  303. closesocket(ListenSocket);
  304. WSACleanup();
  305. return 1;
  306. }
  307. cout << ListenSocket << endl;
  308.  
  309. printf("Im ready\n");
  310. freeaddrinfo(result);
  311.  
  312. iResult = listen(ListenSocket, SOMAXCONN);
  313. if (iResult == SOCKET_ERROR) {
  314. printf("listen failed with error: %d\n", WSAGetLastError());
  315. closesocket(ListenSocket);
  316. WSACleanup();
  317. return 1;
  318. }
  319.  
  320. // Accept a client socket
  321. ClientSocket = accept(ListenSocket, NULL, NULL);
  322. if (ClientSocket == INVALID_SOCKET) {
  323. printf("accept failed with error: %d\n", WSAGetLastError());
  324. closesocket(ListenSocket);
  325. WSACleanup();
  326. return 1;
  327. }
  328. printf("connected\n");
  329.  
  330. // No longer need server socket
  331. closesocket(ListenSocket);
  332.  
  333. // Receive until the peer shuts down the connection
  334.  
  335.  
  336. //********************* login start
  337. char recvbuf[DEFAULT_BUFLEN] = { 0 };
  338. char* sendbuf= "Enter Username\n" ;
  339.  
  340. iSendResult = send(ClientSocket, sendbuf, (int)strlen(sendbuf), 0);
  341. if (iSendResult == SOCKET_ERROR) { printf("send failed with error: %d\n", WSAGetLastError()); closesocket(ClientSocket); WSACleanup(); exit(1); }
  342. iResult = recv(ClientSocket, recvbuf, recvbuflen, 0);
  343. cout << recvbuf << endl;
  344. string username(recvbuf);
  345.  
  346. sendbuf = "Enter password\n";
  347. for (int i = 0; i < 1024; i++){ recvbuf[i] = 0; }
  348.  
  349. iSendResult = send(ClientSocket, sendbuf, (int)strlen(sendbuf), 0);
  350. if (iSendResult == SOCKET_ERROR) { printf("send failed with error: %d\n", WSAGetLastError()); closesocket(ClientSocket); WSACleanup(); exit(1); }
  351. iResult = recv(ClientSocket, recvbuf, recvbuflen, 0);
  352. cout << recvbuf << endl;
  353. string password(recvbuf);
  354.  
  355. password = decryption(password);
  356. username = decryption(username);
  357.  
  358. //****************** sent the messages to get username and passwords
  359.  
  360. if (usernameAndPasswordMatch(db,username, password))
  361. {
  362.  
  363. sendbuf = "d Access granted.\n";
  364. iSendResult = send(ClientSocket, sendbuf, (int)strlen(sendbuf), 0);
  365. if (iSendResult == SOCKET_ERROR) { printf("send failed with error: %d\n", WSAGetLastError()); closesocket(ClientSocket); WSACleanup(); exit(1); }
  366.  
  367. do
  368. {
  369. sendbuf = "Enter your desired command.\n";
  370. iSendResult = send(ClientSocket, sendbuf, (int)strlen(sendbuf), 0);
  371.  
  372. //taking commands
  373. if (iSendResult == SOCKET_ERROR) { printf("send failed with error: %d\n", WSAGetLastError()); closesocket(ClientSocket); WSACleanup(); exit(1); }
  374. iResult = recv(ClientSocket, recvbuf, recvbuflen, 0);
  375. cout << recvbuf << endl;
  376. string commandLine(recvbuf);
  377. commandLine.erase(0, 2);
  378.  
  379.  
  380.  
  381. if (getResquest(commandLine) == "ECHO")
  382. {
  383. commandLine.erase(0, 5);
  384. string temp = "i " + commandLine;
  385. const char* string1 = temp.c_str();
  386. iSendResult = send(ClientSocket, sendbuf, (int)strlen(sendbuf), 0);
  387. if (iSendResult == SOCKET_ERROR) { printf("send failed with error: %d\n", WSAGetLastError()); closesocket(ClientSocket); WSACleanup(); exit(1); }
  388.  
  389.  
  390. }
  391. else if (getResquest(commandLine) == "GIVEIP")
  392. {
  393. commandLine.erase(0, 7);
  394. string result = exec("ipconfig");
  395. result = getIPv4(result);
  396. string out = "i ";
  397. out = decryption(out); //PAY ATTENTION THAT ITS ENCRYPTED
  398. out += result;
  399. const char *tempOut = out.c_str();
  400. iSendResult = send(ClientSocket, tempOut, (int)strlen(tempOut), 0);
  401. if (iSendResult == SOCKET_ERROR) { printf("send failed with error: %d\n", WSAGetLastError()); closesocket(ClientSocket); WSACleanup(); exit(1); }
  402.  
  403. }
  404. else if (getResquest(commandLine) == "UPTIME")
  405. {
  406. double a = difftime(time(0), start);
  407. ostringstream sstream;
  408. sstream << a;
  409. string output = "i "+sstream.str()+" seconds\n";
  410. const char *tempOut = output.c_str();
  411. iSendResult = send(ClientSocket, tempOut, (int)strlen(tempOut), 0);
  412. if (iSendResult == SOCKET_ERROR) { printf("send failed with error: %d\n", WSAGetLastError()); closesocket(ClientSocket); WSACleanup(); exit(1); }
  413.  
  414.  
  415. }
  416. else if (getResquest(commandLine) == "WHOAMI")
  417. {
  418. string output = "You are the user"+'"' + username + '"'+" that runs the operating system of: ";//needs to be continued
  419.  
  420. }
  421. else if (getResquest(commandLine) == "MOVETO")
  422. {
  423. commandLine.erase(0, 7);
  424. string temp = "cd " + commandLine;
  425. string result = exec(temp.c_str());
  426. result = decryption(result); //ENCRYPTION ALERT
  427. temp = "i "+result;
  428. const char *tempOut = temp.c_str();
  429. iSendResult = send(ClientSocket, tempOut, (int)strlen(tempOut), 0);
  430. if (iSendResult == SOCKET_ERROR) { printf("send failed with error: %d\n", WSAGetLastError()); closesocket(ClientSocket); WSACleanup(); exit(1); }
  431.  
  432.  
  433. }
  434. else if (getResquest(commandLine) == "RUN")
  435. {
  436. commandLine.erase(0, 4);
  437. string result = exec(commandLine.c_str());
  438. result = decryption(result);
  439. string output = "i " + result;
  440. output = decryption(output); //ENCRYPTION ALERT
  441. const char *tempOut = output.c_str();
  442. iSendResult = send(ClientSocket, tempOut, (int)strlen(tempOut), 0);
  443. if (iSendResult == SOCKET_ERROR) { printf("send failed with error: %d\n", WSAGetLastError()); closesocket(ClientSocket); WSACleanup(); exit(1); }
  444.  
  445.  
  446. }
  447. else if (getResquest(commandLine) == "PRINTF")
  448. {
  449.  
  450. string line = decryption(commandLine); //DECRYPTION ALERT
  451. line = commandLine.erase(0, 7);
  452.  
  453.  
  454. if ((commandLine[-1] == 't') && (commandLine[-2] == 'x') && (commandLine[-3] == 't') && (commandLine[-4] == '.'))
  455. {
  456.  
  457. string command = "print " + commandLine;
  458. string result = exec(command.c_str());
  459.  
  460. int size = result.length();
  461.  
  462. if (size / 1024 > 0)
  463. {
  464. string output = result.substr(0, 1025);
  465. result.erase(0, 1025);
  466.  
  467. string temp(sendbuf);
  468. temp = decryption(temp); //Encryption alert!
  469.  
  470. const char *tempOut = temp.c_str();
  471. iSendResult = send(ClientSocket, tempOut, (int)strlen(tempOut), 0);
  472. if (iSendResult == SOCKET_ERROR) { printf("send failed with error: %d\n", WSAGetLastError()); closesocket(ClientSocket); WSACleanup(); exit(1); }
  473. size -= 1024;
  474. }
  475. else
  476. {
  477.  
  478.  
  479. string temp(sendbuf);
  480. temp = decryption(temp); //Encryption alert!
  481.  
  482. const char *tempOut = temp.c_str();
  483. iSendResult = send(ClientSocket, tempOut, (int)strlen(tempOut), 0);
  484. if (iSendResult == SOCKET_ERROR) { printf("send failed with error: %d\n", WSAGetLastError()); closesocket(ClientSocket); WSACleanup(); exit(1); }
  485.  
  486. tempOut = "m";
  487. iSendResult = send(ClientSocket, tempOut, (int)strlen(tempOut), 0);
  488. if (iSendResult == SOCKET_ERROR) { printf("send failed with error: %d\n", WSAGetLastError()); closesocket(ClientSocket); WSACleanup(); exit(1); }
  489.  
  490. }
  491. }
  492.  
  493. }
  494. else if (getResquest(commandLine) == "GOAWAYF")
  495. {
  496. string line = decryption(commandLine); //DECRYPTION ALERT
  497. line = commandLine.erase(0, 7);
  498. string temp = "k Are you sure you want to delete " + line + " ?";
  499. const char *tempOut = temp.c_str();
  500. iSendResult = send(ClientSocket, tempOut, (int)strlen(tempOut), 0);
  501. if (iSendResult == SOCKET_ERROR) { printf("send failed with error: %d\n", WSAGetLastError()); closesocket(ClientSocket); WSACleanup(); exit(1); }
  502. iResult = recv(ClientSocket, recvbuf, recvbuflen, 0);
  503. if ((recvbuf[2] == 'y') || (recvbuf[2] == 'Y'))
  504. {
  505. string command = "DEL /Q" + line;
  506. string result = exec(command.c_str());
  507. const char *tempOut = result.c_str();
  508. iSendResult = send(ClientSocket, tempOut, (int)strlen(tempOut), 0);
  509. if (iSendResult == SOCKET_ERROR) { printf("send failed with error: %d\n", WSAGetLastError()); closesocket(ClientSocket); WSACleanup(); exit(1); }
  510. }
  511.  
  512.  
  513. }
  514. else if (getResquest(commandLine) == "GOAWAYD")
  515. {
  516. string line = decryption(commandLine); //DECRYPTION ALERT
  517. line = commandLine.erase(0, 7);
  518. string temp = "k Are you sure you want to delete " + line + " ?";
  519. const char *tempOut = temp.c_str();
  520. iSendResult = send(ClientSocket, tempOut, (int)strlen(tempOut), 0);
  521. if (iSendResult == SOCKET_ERROR) { printf("send failed with error: %d\n", WSAGetLastError()); closesocket(ClientSocket); WSACleanup(); exit(1); }
  522. iResult = recv(ClientSocket, recvbuf, recvbuflen, 0);
  523. if ((recvbuf[2] == 'y') || (recvbuf[2] == 'Y'))
  524. {
  525. string command = "rmdir /Q /S " + line;
  526. string result = exec(command.c_str());
  527. command = "rmdir /Q " + line;
  528. result = exec(command.c_str());
  529. const char *tempOut = result.c_str();
  530. iSendResult = send(ClientSocket, tempOut, (int)strlen(tempOut), 0);
  531. if (iSendResult == SOCKET_ERROR) { printf("send failed with error: %d\n", WSAGetLastError()); closesocket(ClientSocket); WSACleanup(); exit(1); }
  532. }
  533.  
  534. }
  535. else if (getResquest(commandLine) == "MAKEF")
  536. {
  537. commandLine.erase(0, 6);
  538.  
  539. struct stat info;
  540.  
  541. if (stat(commandLine.c_str(), &info) != 0) // cant access
  542. {
  543. const char *tempOut = "z can't access directory\n";
  544. iSendResult = send(ClientSocket, tempOut, (int)strlen(tempOut), 0);
  545. if (iSendResult == SOCKET_ERROR) { printf("send failed with error: %d\n", WSAGetLastError()); closesocket(ClientSocket); WSACleanup(); exit(1); }
  546.  
  547.  
  548. }
  549. else if (info.st_mode & S_IFDIR) // there is a directory
  550. {
  551. const char *tempOut = "k The folder alright exists would you like to override it?\n";
  552. iSendResult = send(ClientSocket, tempOut, (int)strlen(tempOut), 0);
  553. if (iSendResult == SOCKET_ERROR) { printf("send failed with error: %d\n", WSAGetLastError()); closesocket(ClientSocket); WSACleanup(); exit(1); }
  554.  
  555. iResult = recv(ClientSocket, recvbuf, recvbuflen, 0);
  556. if ((recvbuf[2] == 'y') || (recvbuf[2] == 'Y'))
  557. {
  558. string command1 = "DEL /Q" + commandLine;
  559. exec(command1.c_str());
  560. string command = "copy nul " + commandLine + " /Q";
  561. string result = exec(command.c_str());
  562. if (result == "")
  563. {
  564. const char *tempOut = "i Folder was successfuly overwritten\n";
  565. iSendResult = send(ClientSocket, tempOut, (int)strlen(tempOut), 0);
  566. if (iSendResult == SOCKET_ERROR) { printf("send failed with error: %d\n", WSAGetLastError()); closesocket(ClientSocket); WSACleanup(); exit(1); }
  567. }
  568. else
  569. {
  570. const char *tempOut = "z Error while overwriting the Folder\n";
  571. iSendResult = send(ClientSocket, tempOut, (int)strlen(tempOut), 0);
  572. if (iSendResult == SOCKET_ERROR) { printf("send failed with error: %d\n", WSAGetLastError()); closesocket(ClientSocket); WSACleanup(); exit(1); }
  573. }
  574. }
  575.  
  576. }
  577. else
  578. {
  579. string command = "copy nul " + commandLine + " /Q"; // there is no directory
  580. string result = exec(command.c_str());
  581. string out = "i " + result;
  582. const char *tempOut = out.c_str();
  583. iSendResult = send(ClientSocket, tempOut, (int)strlen(tempOut), 0);
  584. if (iSendResult == SOCKET_ERROR) { printf("send failed with error: %d\n", WSAGetLastError()); closesocket(ClientSocket); WSACleanup(); exit(1); }
  585.  
  586. }
  587.  
  588. }
  589. else if (getResquest(commandLine) == "MAKED")
  590. {
  591. commandLine.erase(0, 6);
  592.  
  593. struct stat info;
  594.  
  595. if (stat(commandLine.c_str(), &info) != 0)
  596. {
  597. const char *tempOut = "z error while trying to execute\n";
  598. iSendResult = send(ClientSocket, tempOut, (int)strlen(tempOut), 0);
  599. if (iSendResult == SOCKET_ERROR) { printf("send failed with error: %d\n", WSAGetLastError()); closesocket(ClientSocket); WSACleanup(); exit(1); }
  600.  
  601.  
  602. }
  603. else if (info.st_mode & S_IFDIR) // S_ISDIR() doesn't exist on my windows
  604. {
  605. const char *tempOut = "k The folder alright exists would you like to override it?\n";
  606. iSendResult = send(ClientSocket, tempOut, (int)strlen(tempOut), 0);
  607. if (iSendResult == SOCKET_ERROR) { printf("send failed with error: %d\n", WSAGetLastError()); closesocket(ClientSocket); WSACleanup(); exit(1); }
  608.  
  609.  
  610.  
  611. iResult = recv(ClientSocket, recvbuf, recvbuflen, 0);
  612. if ((recvbuf[2] == 'y') || (recvbuf[2] == 'Y'))
  613. {
  614. string command = "DEL /Q" + commandLine;
  615. string result = exec(command.c_str());
  616. command = " MKDIR " + commandLine;
  617. result = exec(command.c_str());
  618. if (result == "")
  619. {
  620. const char *tempOut = "i Folder was successfuly overwritten\n";
  621. iSendResult = send(ClientSocket, tempOut, (int)strlen(tempOut), 0);
  622. if (iSendResult == SOCKET_ERROR) { printf("send failed with error: %d\n", WSAGetLastError()); closesocket(ClientSocket); WSACleanup(); exit(1); }
  623. }
  624. else
  625. {
  626. const char *tempOut = "z Errow while overwriting the Folder\n";
  627. iSendResult = send(ClientSocket, tempOut, (int)strlen(tempOut), 0);
  628. if (iSendResult == SOCKET_ERROR) { printf("send failed with error: %d\n", WSAGetLastError()); closesocket(ClientSocket); WSACleanup(); exit(1); }
  629. }
  630. }
  631.  
  632.  
  633.  
  634. }
  635. else
  636. {
  637. string command = " MKDIR " + commandLine; // there is no directory
  638. string result = exec(command.c_str());
  639. string out = "i " + result;
  640. const char *tempOut = out.c_str();
  641. iSendResult = send(ClientSocket, tempOut, (int)strlen(tempOut), 0);
  642. if (iSendResult == SOCKET_ERROR) { printf("send failed with error: %d\n", WSAGetLastError()); closesocket(ClientSocket); WSACleanup(); exit(1); }
  643.  
  644. }
  645. }
  646. else if (getResquest(commandLine) == "COPYF")
  647. {
  648. commandLine.erase(0, 6);
  649.  
  650. string src = "";
  651. string dst = "";
  652. int spot = 0;
  653. while (commandLine[spot] != ' ')
  654. {
  655. src += commandLine[spot];
  656. }
  657. spot++;
  658.  
  659. while (commandLine[spot] != ' ')
  660. {
  661. dst += commandLine[spot];
  662. }
  663.  
  664. streampos beg, end;
  665. ofstream copyTo(dst, ios::binary);
  666. if (copyTo.is_open())
  667. {
  668. ifstream copyFrom(src, ios::binary);
  669.  
  670. copyFrom.seekg(0, ios::end);
  671. end = copyFrom.tellg();
  672. copyFrom.seekg(0, ios::beg);
  673. int size = end;
  674. char* file = NULL;
  675. copyFrom.read(file, size);
  676. copyFrom.close();
  677.  
  678. copyTo.seekp(0, ios::end);
  679. end = copyTo.tellp();
  680. int size1 = end;
  681. copyTo.seekp(0, ios::beg);
  682.  
  683. copyTo << file;
  684. copyTo.close();
  685.  
  686. const char *tempOut("i Successfully copied file\n");
  687. iSendResult = send(ClientSocket, tempOut, (int)strlen(tempOut), 0);
  688. if (iSendResult == SOCKET_ERROR) { printf("send failed with error: %d\n", WSAGetLastError()); closesocket(ClientSocket); WSACleanup(); exit(1); }
  689. }
  690. else
  691. {
  692. const char *tempOut("i Error while copying'n");
  693. iSendResult = send(ClientSocket, tempOut, (int)strlen(tempOut), 0);
  694. if (iSendResult == SOCKET_ERROR) { printf("send failed with error: %d\n", WSAGetLastError()); closesocket(ClientSocket); WSACleanup(); exit(1); }
  695. }
  696.  
  697. }
  698. else if (getResquest(commandLine) == "EXEC")
  699. {
  700. commandLine.erase(0, 5);
  701. string result = exec(commandLine.c_str());
  702. result = decryption(result);
  703. string output = "i " + result;
  704. output = decryption(output); //ENCRYPTION AND DECRYPTION ALERT
  705. const char *tempOut = output.c_str();
  706. iSendResult = send(ClientSocket, tempOut, (int)strlen(tempOut), 0);
  707. if (iSendResult == SOCKET_ERROR) { printf("send failed with error: %d\n", WSAGetLastError()); closesocket(ClientSocket); WSACleanup(); exit(1); }
  708.  
  709. }
  710. else if (getResquest(commandLine) == "DL")
  711. {
  712. commandLine.erase(0, 3);
  713. streampos begin, end;
  714. ifstream myfile(commandLine, ios::binary);
  715. begin = myfile.tellg();
  716. myfile.seekg(0, ios::end);
  717. end = myfile.tellg();
  718. myfile.seekg(0, ios::beg);
  719.  
  720. int size = end;
  721. while (size > 0)
  722. {
  723.  
  724.  
  725. if (size / 1024 > 0)
  726. {
  727. myfile.read(sendbuf, 1024);
  728.  
  729. string temp(sendbuf);
  730. temp = decryption(temp);
  731.  
  732. const char *tempOut = temp.c_str();
  733. iSendResult = send(ClientSocket, tempOut, (int)strlen(tempOut), 0);
  734. if (iSendResult == SOCKET_ERROR) { printf("send failed with error: %d\n", WSAGetLastError()); closesocket(ClientSocket); WSACleanup(); exit(1); }
  735. size -= 1024;
  736. }
  737. else
  738. {
  739. myfile.read(sendbuf, size);
  740.  
  741. string temp(sendbuf);
  742. temp = decryption(temp);
  743.  
  744. const char *tempOut = temp.c_str();
  745. iSendResult = send(ClientSocket, tempOut, (int)strlen(tempOut), 0);
  746. if (iSendResult == SOCKET_ERROR) { printf("send failed with error: %d\n", WSAGetLastError()); closesocket(ClientSocket); WSACleanup(); exit(1); }
  747.  
  748. }
  749. }
  750. myfile.close();
  751. const char *tempOut = "m /n";
  752. iSendResult = send(ClientSocket, tempOut, (int)strlen(tempOut), 0);
  753. if (iSendResult == SOCKET_ERROR) { printf("send failed with error: %d\n", WSAGetLastError()); closesocket(ClientSocket); WSACleanup(); exit(1); }
  754.  
  755.  
  756.  
  757. }
  758. else
  759. {
  760. sendbuf = "g Not a valid request";
  761. iSendResult = send(ClientSocket, sendbuf, (int)strlen(sendbuf), 0);
  762. if (iSendResult == SOCKET_ERROR) { printf("send failed with error: %d\n", WSAGetLastError()); closesocket(ClientSocket); WSACleanup(); exit(1); }
  763.  
  764. }
  765.  
  766.  
  767.  
  768. //closeConnection++;
  769. } while ((iResult > 0) && (iSendResult >0) && (!closeConnection));
  770.  
  771.  
  772. }
  773. else
  774. {
  775. sendbuf = "f Access Denied, terminating connection.\n";
  776. iSendResult = send(ClientSocket, sendbuf, (int)strlen(sendbuf), 0);
  777. if (iSendResult == SOCKET_ERROR) { printf("send failed with error: %d\n", WSAGetLastError()); closesocket(ClientSocket); WSACleanup(); exit(1); }
  778.  
  779. }
  780.  
  781.  
  782.  
  783.  
  784.  
  785. // shutdown the connection since we're done
  786. iResult = shutdown(ClientSocket, SD_SEND);
  787. if (iResult == SOCKET_ERROR) {
  788. printf("shutdown failed with error: %d\n", WSAGetLastError());
  789. closesocket(ClientSocket);
  790. WSACleanup();
  791. return 1;
  792. }
  793.  
  794. // cleanup
  795. closesocket(ClientSocket);
  796. WSACleanup();
  797.  
  798. return 0;
  799. }
  800.  
  801. //100 opening connection -e
  802. //101 closing connection -f
  803. //102 rawText -g
  804. //103 request -h
  805. //104 response-Request -i
  806. //105 varification question - j
  807. //106 varification answer - k
  808. //107 end sending -l
  809. //107 excecution failed - z
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement