ServerMain.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821
  1. #define _POSIX_C_SOURCE 1
  2. #include <stdio.h>
  3. #include <unistd.h>
  4. #include <stdlib.h>
  5. #include <signal.h>
  6. #include <string.h>
  7. #include <dirent.h>
  8. #include <sys/stat.h>
  9. #include <sys/types.h>
  10. #include <sys/file.h>
  11. #include <fcntl.h>
  12. #include <dirent.h>
  13. #include <assert.h>
  14. #include "Server.h"
  15. int getMailCounter()
  16. {
  17. FILE* test = fopen("mailcounter", "r+");
  18. flock(fileno(test), LOCK_EX);
  19. if(test == NULL)
  20. {
  21. if(creat("mailcounter", S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH) == -1)
  22. {
  23. return 0;
  24. }
  25. test = fopen("mailcounter", "r+");
  26. if(test == NULL)
  27. {
  28. return 0;
  29. }
  30. }
  31. int counter;
  32. if(fread(&counter, sizeof(int), 1, test) == 0)
  33. {
  34. counter = -1;
  35. }
  36. counter++;
  37. rewind(test);
  38. if(fwrite(&counter, sizeof(int), 1, test) == 0)
  39. {
  40. return 0;
  41. }
  42. if(fflush(test) == EOF)
  43. {
  44. return 0;
  45. }
  46. flock(fileno(test), LOCK_UN);
  47. if(fclose(test) == EOF)
  48. {
  49. return 0;
  50. }
  51. return counter;
  52. }
  53. int isSpecialFolder(char* folder)
  54. {
  55. return strcmp(folder, ".") == 0 || strcmp(folder, "..") == 0;
  56. }
  57. Server server;
  58. void interruptHandler(int signal)
  59. {
  60. serverRemove(&server);
  61. exit(EXIT_SUCCESS);
  62. }
  63. void ensureBufferSize(char** data, int* size, int minSize)
  64. {
  65. if(minSize >= *size)
  66. {
  67. int newSize = *size;
  68. while(newSize <= minSize)
  69. {
  70. newSize *= 2;
  71. }
  72. //printf("Resize from %d to %d\n", *size, newSize);
  73. char* newData = malloc(sizeof(char) * newSize);
  74. memcpy(newData, *data, *size);
  75. *size = newSize;
  76. free(*data);
  77. *data = newData;
  78. }
  79. }
  80. void packageSendFailAnswer(int clientSocket)
  81. {
  82. Stream out;
  83. streamInit(&out, 16);
  84. streamWriteInt(&out, 0);
  85. streamWriteChars(&out, "ERR\n");
  86. serverSend(clientSocket, &out);
  87. streamRemove(&out);
  88. }
  89. void packageSendOkAnswer(int clientSocket)
  90. {
  91. Stream out;
  92. streamInit(&out, 16);
  93. streamWriteInt(&out, 0);
  94. streamWriteChars(&out, "OK\n");
  95. serverSend(clientSocket, &out);
  96. streamRemove(&out);
  97. }
  98. int createDirectory(char* path)
  99. {
  100. DIR* dir = opendir(path);
  101. if(dir == NULL)
  102. {
  103. if(mkdir(path, S_ISUID | S_ISGID | S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) == -1)
  104. {
  105. perror("create dir fail");
  106. return -1;
  107. }
  108. dir = opendir(path);
  109. if(dir == NULL)
  110. {
  111. perror("open dir fail");
  112. return -1;
  113. }
  114. }
  115. if(closedir(dir))
  116. {
  117. perror("close dir fail");
  118. return -1;
  119. }
  120. if(chdir(path) == -1)
  121. {
  122. perror("change dir fail");
  123. return -1;
  124. }
  125. return 0;
  126. }
  127. void storeMail(char* sender, char* receiver, char* subject, char* message)
  128. {
  129. /*printf("sender: %s\n", sender);
  130. printf("receiver: %s\n", receiver);
  131. printf("subject: %s\n", subject);
  132. printf("message: %s\n", message);*/
  133. if(createDirectory(receiver) == -1)
  134. {
  135. return;
  136. }
  137. int id = getMailCounter();
  138. if(createDirectory(sender) == -1)
  139. {
  140. return;
  141. }
  142. char buffer[12];
  143. snprintf(buffer, 12, "%d", id);
  144. if(creat(buffer, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH) == -1)
  145. {
  146. return;
  147. }
  148. FILE* file = fopen(buffer, "w");
  149. if(file == NULL)
  150. {
  151. perror("cant open file");
  152. return;
  153. }
  154. if(fputs(subject, file) == EOF)
  155. {
  156. perror("cant write to file");
  157. return;
  158. }
  159. if(fputc('\n', file) == EOF)
  160. {
  161. perror("cant write to file");
  162. return;
  163. }
  164. if(fputs(message, file) == EOF)
  165. {
  166. perror("cant write to file");
  167. return;
  168. }
  169. if(fflush(file) == EOF)
  170. {
  171. perror("cant flush the file");
  172. return;
  173. }
  174. if(fclose(file) == EOF)
  175. {
  176. perror("cant close");
  177. return;
  178. }
  179. }
  180. int packageSend(int client, int clientSocket, Stream* in)
  181. {
  182. char* user = serverGetUser(&server, client);
  183. if(user == NULL)
  184. {
  185. packageSendFailAnswer(clientSocket);
  186. return 0;
  187. }
  188. char c;
  189. int i;
  190. const char* error = "invalid SEND package\n";
  191. // receiver
  192. i = 0;
  193. char receiver[9];
  194. while(1)
  195. {
  196. if(streamGetChar(in, &c) != -1)
  197. {
  198. if(c == '\n')
  199. {
  200. receiver[i] = '\0';
  201. break;
  202. }
  203. else
  204. {
  205. if(i == 8)
  206. {
  207. printf("%s", error);
  208. packageSendFailAnswer(clientSocket);
  209. return 0;
  210. }
  211. receiver[i] = c;
  212. i++;
  213. }
  214. }
  215. else
  216. {
  217. printf("%s", error);
  218. packageSendFailAnswer(clientSocket);
  219. return 0;
  220. }
  221. }
  222. if(strlen(receiver) == 0 || isSpecialFolder(receiver))
  223. {
  224. printf("%s", error);
  225. packageSendFailAnswer(clientSocket);
  226. return 0;
  227. }
  228. // subject
  229. i = 0;
  230. char subject[81];
  231. while(1)
  232. {
  233. if(streamGetChar(in, &c) != -1)
  234. {
  235. if(c == '\n')
  236. {
  237. subject[i] = '\0';
  238. break;
  239. }
  240. else
  241. {
  242. if(i == 80)
  243. {
  244. printf("%s", error);
  245. packageSendFailAnswer(clientSocket);
  246. return 0;
  247. }
  248. subject[i] = c;
  249. i++;
  250. }
  251. }
  252. else
  253. {
  254. printf("%s", error);
  255. packageSendFailAnswer(clientSocket);
  256. return 0;
  257. }
  258. }
  259. if(strlen(subject) == 0)
  260. {
  261. printf("%s", error);
  262. packageSendFailAnswer(clientSocket);
  263. return 0;
  264. }
  265. // message
  266. i = 0;
  267. int buffer = 16;
  268. char* message = malloc(sizeof(char) * buffer);
  269. while(1)
  270. {
  271. if(streamGetChar(in, &c) != -1)
  272. {
  273. if(c == '\n')
  274. {
  275. char n1;
  276. if(streamGetChar(in, &n1) == -1)
  277. {
  278. printf("%s", error);
  279. packageSendFailAnswer(clientSocket);
  280. free(message);
  281. return 0;
  282. }
  283. char n2;
  284. if(streamGetChar(in, &n2) == -1)
  285. {
  286. printf("%s", error);
  287. packageSendFailAnswer(clientSocket);
  288. free(message);
  289. return 0;
  290. }
  291. if(n1 == '.' && n2 == '\n')
  292. {
  293. ensureBufferSize(&message, &buffer, i);
  294. message[i] = '\0';
  295. break;
  296. }
  297. else
  298. {
  299. ensureBufferSize(&message, &buffer, i + 2);
  300. message[i] = c;
  301. i++;
  302. message[i] = n1;
  303. i++;
  304. message[i] = n2;
  305. i++;
  306. continue;
  307. }
  308. }
  309. ensureBufferSize(&message, &buffer, i);
  310. message[i] = c;
  311. i++;
  312. }
  313. else
  314. {
  315. printf("%s", error);
  316. packageSendFailAnswer(clientSocket);
  317. free(message);
  318. return 0;
  319. }
  320. }
  321. char workingPath[256];
  322. if(getcwd(workingPath, 256) != NULL)
  323. {
  324. storeMail(user, receiver, subject, message);
  325. assert(chdir(workingPath) == 0);
  326. }
  327. free(message);
  328. packageSendOkAnswer(clientSocket);
  329. return 0;
  330. }
  331. void sendMailList(char* user, Stream* out)
  332. {
  333. DIR* receiverDir = opendir(user);
  334. if(receiverDir == NULL)
  335. {
  336. streamWriteInt(out, 0);
  337. return;
  338. }
  339. assert(chdir(user) == 0);
  340. int counterPos = streamGetPosition(out);
  341. streamWriteInt(out, 0);
  342. int counter = 0;
  343. struct dirent* receiverIterator = readdir(receiverDir);
  344. while(receiverIterator != NULL)
  345. {
  346. if(!isSpecialFolder(receiverIterator->d_name))
  347. {
  348. DIR* senderDir = opendir(receiverIterator->d_name);
  349. if(senderDir != NULL)
  350. {
  351. assert(chdir(receiverIterator->d_name) == 0);
  352. struct dirent* senderIterator = readdir(senderDir);
  353. while(senderIterator != NULL)
  354. {
  355. if(!isSpecialFolder(senderIterator->d_name))
  356. {
  357. // mails found
  358. counter++;
  359. // reading subject from first line
  360. FILE* file = fopen(senderIterator->d_name, "r");
  361. if(file != NULL)
  362. {
  363. int size = 16;
  364. int pos = 0;
  365. char* buffer = malloc(sizeof(char) * size);
  366. int data;
  367. while(1)
  368. {
  369. data = fgetc(file);
  370. if(data == '\n' || data == EOF)
  371. {
  372. ensureBufferSize(&buffer, &size, pos);
  373. buffer[pos] = '\0';
  374. break;
  375. }
  376. ensureBufferSize(&buffer, &size, pos);
  377. buffer[pos] = data;
  378. pos++;
  379. }
  380. streamWriteChars(out, senderIterator->d_name);
  381. streamWriteChars(out, " - ");
  382. streamWriteChars(out, buffer);
  383. free(buffer);
  384. streamWriteChar(out, '\n');
  385. if(fclose(file) == EOF)
  386. {
  387. perror("cannot close file");
  388. }
  389. }
  390. else
  391. {
  392. streamWriteChars(out, "ERR\n");
  393. }
  394. }
  395. senderIterator = readdir(senderDir);
  396. }
  397. assert(chdir("../") == 0);
  398. closedir(senderDir);
  399. }
  400. }
  401. receiverIterator = readdir(receiverDir);
  402. }
  403. int current = streamGetPosition(out);
  404. streamSetPosition(out, counterPos);
  405. streamWriteInt(out, counter);
  406. streamSetPosition(out, current);
  407. closedir(receiverDir);
  408. }
  409. int packageList(int client, int clientSocket, Stream* in)
  410. {
  411. char* user = serverGetUser(&server, client);
  412. if(user == NULL)
  413. {
  414. packageSendFailAnswer(clientSocket);
  415. return 0;
  416. }
  417. Stream out;
  418. streamInit(&out, 16);
  419. streamWriteInt(&out, 1);
  420. char workingPath[256];
  421. if(getcwd(workingPath, 256) != NULL)
  422. {
  423. sendMailList(user, &out);
  424. assert(chdir(workingPath) == 0);
  425. }
  426. serverSend(clientSocket, &out);
  427. streamRemove(&out);
  428. return 0;
  429. }
  430. void readMail(char* user, int number, Stream* out)
  431. {
  432. DIR* receiverDir = opendir(user);
  433. if(receiverDir == NULL)
  434. {
  435. streamWriteChars(out, "ERR\n");
  436. return;
  437. }
  438. assert(chdir(user) == 0);
  439. char buffer[12];
  440. snprintf(buffer, 12, "%d", number);
  441. struct dirent* receiverIterator;
  442. receiverIterator = readdir(receiverDir);
  443. while(receiverIterator != NULL)
  444. {
  445. if(!isSpecialFolder(receiverIterator->d_name))
  446. {
  447. if(chdir(receiverIterator->d_name) == 0)
  448. {
  449. FILE* file = fopen(buffer, "r");
  450. if(file != NULL)
  451. {
  452. // skip subject heading
  453. int data;
  454. while(1)
  455. {
  456. data = fgetc(file);
  457. if(data == '\n' || data == EOF)
  458. {
  459. break;
  460. }
  461. }
  462. // write data to stream
  463. while(1)
  464. {
  465. data = fgetc(file);
  466. if(data == EOF)
  467. {
  468. break;
  469. }
  470. streamWriteChar(out, data);
  471. }
  472. if(fclose(file) == EOF)
  473. {
  474. perror("cannot close file");
  475. }
  476. if(closedir(receiverDir) == -1)
  477. {
  478. perror("cannot close receiver dir");
  479. }
  480. return;
  481. }
  482. assert(chdir("../") == 0);
  483. }
  484. }
  485. receiverIterator = readdir(receiverDir);
  486. }
  487. if(closedir(receiverDir) == -1)
  488. {
  489. perror("cannot close dir");
  490. }
  491. streamWriteChars(out, "ERR\n");
  492. }
  493. int packageRead(int client, int clientSocket, Stream* in)
  494. {
  495. char* user = serverGetUser(&server, client);
  496. if(user == NULL)
  497. {
  498. packageSendFailAnswer(clientSocket);
  499. return 0;
  500. }
  501. char c;
  502. const char* error = "invalid READ package\n";
  503. int number;
  504. if(streamGetInt(in, &number) == -1)
  505. {
  506. printf("%s", error);
  507. packageSendFailAnswer(clientSocket);
  508. return 0;
  509. }
  510. if(streamGetChar(in, &c) == -1)
  511. {
  512. printf("%s", error);
  513. packageSendFailAnswer(clientSocket);
  514. return 0;
  515. }
  516. else if(c != '\n')
  517. {
  518. printf("%s", error);
  519. packageSendFailAnswer(clientSocket);
  520. return 0;
  521. }
  522. Stream out;
  523. streamInit(&out, 16);
  524. streamWriteInt(&out, 2);
  525. char workingPath[256];
  526. if(getcwd(workingPath, 256) != NULL)
  527. {
  528. readMail(user, number, &out);
  529. assert(chdir(workingPath) == 0);
  530. }
  531. else
  532. {
  533. streamWriteChars(&out, "ERR\n");
  534. }
  535. serverSend(clientSocket, &out);
  536. streamRemove(&out);
  537. return 0;
  538. }
  539. int deleteMail(char* user, int number)
  540. {
  541. DIR* receiverDir = opendir(user);
  542. if(receiverDir == NULL)
  543. {
  544. return 0;
  545. }
  546. assert(chdir(user) == 0);
  547. char buffer[12];
  548. snprintf(buffer, 12, "%d", number);
  549. int rValue = 0;
  550. struct dirent* receiverIterator;
  551. receiverIterator = readdir(receiverDir);
  552. while(receiverIterator != NULL)
  553. {
  554. if(!isSpecialFolder(receiverIterator->d_name))
  555. {
  556. if(chdir(receiverIterator->d_name) == 0)
  557. {
  558. if(remove(buffer) == 0)
  559. {
  560. rValue = 1;
  561. break;
  562. }
  563. assert(chdir("../") == 0);
  564. }
  565. }
  566. receiverIterator = readdir(receiverDir);
  567. }
  568. if(closedir(receiverDir) == -1)
  569. {
  570. perror("cannot close dir");
  571. }
  572. return rValue;
  573. }
  574. int packageDelete(int client, int clientSocket, Stream* in)
  575. {
  576. char* user = serverGetUser(&server, client);
  577. if(user == NULL)
  578. {
  579. packageSendFailAnswer(clientSocket);
  580. return 0;
  581. }
  582. char c;
  583. const char* error = "invalid DELETE package\n";
  584. int number;
  585. if(streamGetInt(in, &number) == -1)
  586. {
  587. printf("%s", error);
  588. packageSendFailAnswer(clientSocket);
  589. return 0;
  590. }
  591. if(streamGetChar(in, &c) == -1)
  592. {
  593. printf("%s", error);
  594. packageSendFailAnswer(clientSocket);
  595. return 0;
  596. }
  597. else if(c != '\n')
  598. {
  599. printf("%s", error);
  600. packageSendFailAnswer(clientSocket);
  601. return 0;
  602. }
  603. Stream out;
  604. streamInit(&out, 16);
  605. streamWriteInt(&out, 2);
  606. char workingPath[256];
  607. if(getcwd(workingPath, 256) != NULL)
  608. {
  609. if(deleteMail(user, number))
  610. {
  611. streamWriteChars(&out, "OK\n");
  612. }
  613. else
  614. {
  615. streamWriteChars(&out, "ERR\n");
  616. }
  617. assert(chdir(workingPath) == 0);
  618. }
  619. else
  620. {
  621. streamWriteChars(&out, "ERR\n");
  622. }
  623. serverSend(clientSocket, &out);
  624. streamRemove(&out);
  625. return 0;
  626. }
  627. int packageQuit(int client, int clientSocket, Stream* in)
  628. {
  629. printf("%d wants to disconnect ...\n", client);
  630. return 1;
  631. }
  632. int packageLogin(int client, int clientSocket, Stream* in)
  633. {
  634. printf("%d wants to login ...\n", client);
  635. char c;
  636. int i;
  637. const char* error = "invalid DELETE package\n";
  638. // user
  639. i = 0;
  640. char user[9];
  641. while(1)
  642. {
  643. if(streamGetChar(in, &c) != -1)
  644. {
  645. if(c == '\n')
  646. {
  647. user[i] = '\0';
  648. break;
  649. }
  650. else
  651. {
  652. if(i == 8)
  653. {
  654. printf("%s", error);
  655. packageSendFailAnswer(clientSocket);
  656. return 0;
  657. }
  658. user[i] = c;
  659. i++;
  660. }
  661. }
  662. else
  663. {
  664. printf("%s", error);
  665. packageSendFailAnswer(clientSocket);
  666. return 0;
  667. }
  668. }
  669. if(strlen(user) == 0)
  670. {
  671. printf("%s", error);
  672. packageSendFailAnswer(clientSocket);
  673. return 0;
  674. }
  675. // password
  676. i = 0;
  677. int buffer = 16;
  678. char* password = malloc(sizeof(char) * buffer);
  679. while(1)
  680. {
  681. if(streamGetChar(in, &c) != -1)
  682. {
  683. if(c == '\n')
  684. {
  685. ensureBufferSize(&password, &buffer, i);
  686. password[i] = '\0';
  687. break;
  688. }
  689. ensureBufferSize(&password, &buffer, i);
  690. password[i] = c;
  691. i++;
  692. }
  693. else
  694. {
  695. printf("%s", error);
  696. packageSendFailAnswer(clientSocket);
  697. free(password);
  698. return 0;
  699. }
  700. }
  701. printf("%s %s\n", user, password);
  702. // insert LDAP check here
  703. if(serverSetUser(&server, client, user))
  704. {
  705. packageSendFailAnswer(clientSocket);
  706. free(password);
  707. return 0;
  708. }
  709. free(password);
  710. packageSendOkAnswer(clientSocket);
  711. return 0;
  712. }
  713. int main(int argc, char** argv)
  714. {
  715. if(argc < 3)
  716. {
  717. printf("Usage: %s directory port\n", argv[0]);
  718. return EXIT_FAILURE;
  719. }
  720. int port = atoi(argv[2]);
  721. if(port <= 0 || port >= 65536)
  722. {
  723. printf("invalid port");
  724. return EXIT_FAILURE;
  725. }
  726. serverInitDefaults(&server);
  727. signal(SIGINT, interruptHandler);
  728. signal(SIGKILL, interruptHandler);
  729. if(serverInit(&server, 3, port, argv[1]) == -1)
  730. {
  731. return EXIT_FAILURE;
  732. }
  733. serverRegisterHandler(&server, packageSend);
  734. serverRegisterHandler(&server, packageList);
  735. serverRegisterHandler(&server, packageRead);
  736. serverRegisterHandler(&server, packageDelete);
  737. serverRegisterHandler(&server, packageQuit);
  738. serverRegisterHandler(&server, packageLogin);
  739. serverWaitForConnection(&server);
  740. serverRemove(&server);
  741. return EXIT_SUCCESS;
  742. }