ServerMain.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740
  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. #include "String.h"
  16. #include "LDAP.h"
  17. #include <string.h>
  18. int getMailCounter()
  19. {
  20. FILE* test = fopen("mailcounter", "r+");
  21. if(test == NULL)
  22. {
  23. if(creat("mailcounter", S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH) == -1)
  24. {
  25. return 0;
  26. }
  27. test = fopen("mailcounter", "r+");
  28. if(test == NULL)
  29. {
  30. return 0;
  31. }
  32. }
  33. flock(fileno(test), LOCK_EX);
  34. int counter;
  35. if(fread(&counter, sizeof(int), 1, test) == 0)
  36. {
  37. counter = -1;
  38. }
  39. counter++;
  40. rewind(test);
  41. if(fwrite(&counter, sizeof(int), 1, test) == 0)
  42. {
  43. return 0;
  44. }
  45. if(fflush(test) == EOF)
  46. {
  47. return 0;
  48. }
  49. flock(fileno(test), LOCK_UN);
  50. if(fclose(test) == EOF)
  51. {
  52. return 0;
  53. }
  54. return counter;
  55. }
  56. int isSpecialFolder(char* folder)
  57. {
  58. return strcmp(folder, ".") == 0 || strcmp(folder, "..") == 0;
  59. }
  60. Server server;
  61. void interruptHandler(int signal)
  62. {
  63. serverRemove(&server);
  64. exit(EXIT_SUCCESS);
  65. }
  66. void ensureBufferIndex(char** data, int* size, int index)
  67. {
  68. if(index >= *size)
  69. {
  70. int newSize = *size;
  71. while(newSize <= index)
  72. {
  73. newSize *= 2;
  74. }
  75. char* newData = malloc(sizeof(char) * newSize);
  76. memcpy(newData, *data, *size);
  77. *size = newSize;
  78. free(*data);
  79. *data = newData;
  80. }
  81. }
  82. void packageSendFailAnswer(int clientSocket)
  83. {
  84. Stream out;
  85. streamInit(&out, 16);
  86. streamWriteChars(&out, "ERR\n");
  87. serverSend(clientSocket, &out);
  88. streamRemove(&out);
  89. }
  90. void packageSendOkAnswer(int clientSocket)
  91. {
  92. Stream out;
  93. streamInit(&out, 16);
  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. if(createDirectory(receiver) == -1)
  130. {
  131. return;
  132. }
  133. int id = getMailCounter();
  134. if(createDirectory(sender) == -1)
  135. {
  136. return;
  137. }
  138. char buffer[12];
  139. snprintf(buffer, 12, "%d", id);
  140. if(creat(buffer, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH) == -1)
  141. {
  142. return;
  143. }
  144. FILE* file = fopen(buffer, "w");
  145. if(file == NULL)
  146. {
  147. perror("cant open file");
  148. return;
  149. }
  150. if(fputs(sender, file) == EOF)
  151. {
  152. perror("cant write to file");
  153. return;
  154. }
  155. if(fputc('\n', file) == EOF)
  156. {
  157. perror("cant write to file");
  158. return;
  159. }
  160. if(fputs(receiver, file) == EOF)
  161. {
  162. perror("cant write to file");
  163. return;
  164. }
  165. if(fputc('\n', file) == EOF)
  166. {
  167. perror("cant write to file");
  168. return;
  169. }
  170. if(fputs(subject, file) == EOF)
  171. {
  172. perror("cant write to file");
  173. return;
  174. }
  175. if(fputc('\n', file) == EOF)
  176. {
  177. perror("cant write to file");
  178. return;
  179. }
  180. if(fputs(message, file) == EOF)
  181. {
  182. perror("cant write to file");
  183. return;
  184. }
  185. if(fflush(file) == EOF)
  186. {
  187. perror("cant flush the file");
  188. return;
  189. }
  190. if(fclose(file) == EOF)
  191. {
  192. perror("cant close");
  193. return;
  194. }
  195. }
  196. int packageSend(int client, int clientSocket, Stream* in)
  197. {
  198. char* user = serverGetUser(&server, client);
  199. if(user == NULL)
  200. {
  201. packageSendFailAnswer(clientSocket);
  202. return 0;
  203. }
  204. int length;
  205. // receiver
  206. String receiver;
  207. stringInit(&receiver);
  208. stringReadStreamLine(&receiver, in);
  209. length = stringGetLength(&receiver);
  210. if(length == 0 || length > 8)
  211. {
  212. packageSendFailAnswer(clientSocket);
  213. stringRemove(&receiver);
  214. return 0;
  215. }
  216. // subject
  217. String subject;
  218. stringInit(&subject);
  219. stringReadStreamLine(&subject, in);
  220. length = stringGetLength(&subject);
  221. if(length == 0 || length > 80)
  222. {
  223. packageSendFailAnswer(clientSocket);
  224. stringRemove(&subject);
  225. return 0;
  226. }
  227. // message
  228. int index = 0;
  229. int buffer = 16;
  230. char* message = malloc(sizeof(char) * buffer);
  231. String sMessage;
  232. stringInit(&sMessage);
  233. while(streamHasData(in))
  234. {
  235. stringReadStreamLine(&sMessage, in);
  236. if(stringCompare(&sMessage, ".") || buffer > 60)
  237. {
  238. break;
  239. }
  240. int length = stringGetLength(&sMessage);
  241. // "i + length" is one too much but right for "length + \n"
  242. ensureBufferIndex(&message, &buffer, index + length);
  243. memcpy(message + index, sMessage.data, length);
  244. index += length;
  245. message[index] = '\n';
  246. index++;
  247. }
  248. stringRemove(&sMessage);
  249. char workingPath[256];
  250. if(getcwd(workingPath, 256) != NULL)
  251. {
  252. storeMail(user, receiver.data, subject.data, message);
  253. assert(chdir(workingPath) == 0);
  254. }
  255. stringRemove(&receiver);
  256. stringRemove(&subject);
  257. free(message);
  258. packageSendOkAnswer(clientSocket);
  259. return 0;
  260. }
  261. void sendMailList(char* user, Stream* out)
  262. {
  263. DIR* receiverDir = opendir(user);
  264. if(receiverDir == NULL)
  265. {
  266. streamWriteChars(out, "0\n");
  267. return;
  268. }
  269. assert(chdir(user) == 0);
  270. // count mails first, this is easier than buffering everything else
  271. int counter = 0;
  272. struct dirent* receiverIterator = readdir(receiverDir);
  273. while(receiverIterator != NULL)
  274. {
  275. if(!isSpecialFolder(receiverIterator->d_name))
  276. {
  277. DIR* senderDir = opendir(receiverIterator->d_name);
  278. if(senderDir != NULL)
  279. {
  280. assert(chdir(receiverIterator->d_name) == 0);
  281. struct dirent* senderIterator = readdir(senderDir);
  282. while(senderIterator != NULL)
  283. {
  284. if(!isSpecialFolder(senderIterator->d_name))
  285. {
  286. // mails found
  287. counter++;
  288. }
  289. senderIterator = readdir(senderDir);
  290. }
  291. assert(chdir("../") == 0);
  292. closedir(senderDir);
  293. }
  294. }
  295. receiverIterator = readdir(receiverDir);
  296. }
  297. // write the number of mails
  298. char buffer[12];
  299. snprintf(buffer, 12, "%d", counter);
  300. streamWriteChars(out, buffer);
  301. streamWriteChar(out, '\n');
  302. // add the mail subjects
  303. assert(chdir("../") == 0);
  304. closedir(receiverDir);
  305. receiverDir = opendir(user);
  306. if(receiverDir == NULL)
  307. {
  308. return;
  309. }
  310. assert(chdir(user) == 0);
  311. receiverIterator = readdir(receiverDir);
  312. while(receiverIterator != NULL)
  313. {
  314. if(!isSpecialFolder(receiverIterator->d_name))
  315. {
  316. DIR* senderDir = opendir(receiverIterator->d_name);
  317. if(senderDir != NULL)
  318. {
  319. assert(chdir(receiverIterator->d_name) == 0);
  320. struct dirent* senderIterator = readdir(senderDir);
  321. while(senderIterator != NULL)
  322. {
  323. if(!isSpecialFolder(senderIterator->d_name))
  324. {
  325. // reading subject from first line
  326. FILE* file = fopen(senderIterator->d_name, "r");
  327. if(file != NULL)
  328. {
  329. streamWriteChars(out, senderIterator->d_name);
  330. streamWriteChars(out, " - ");
  331. //skip first 2 lines (receiver, sender)
  332. int skip = 0;
  333. int data;
  334. while(1)
  335. {
  336. data = fgetc(file);
  337. if(data == '\n' || data == EOF)
  338. {
  339. skip++;
  340. if(skip == 2)
  341. {
  342. break;
  343. }
  344. }
  345. }
  346. while(1)
  347. {
  348. data = fgetc(file);
  349. if(data == '\n' || data == EOF)
  350. {
  351. break;
  352. }
  353. streamWriteChar(out, data);
  354. }
  355. streamWriteChar(out, '\n');
  356. if(fclose(file) == EOF)
  357. {
  358. perror("cannot close file");
  359. }
  360. }
  361. else
  362. {
  363. streamWriteChars(out, "ERR\n");
  364. }
  365. }
  366. senderIterator = readdir(senderDir);
  367. }
  368. assert(chdir("../") == 0);
  369. closedir(senderDir);
  370. }
  371. }
  372. receiverIterator = readdir(receiverDir);
  373. }
  374. closedir(receiverDir);
  375. }
  376. int packageList(int client, int clientSocket, Stream* in)
  377. {
  378. char* user = serverGetUser(&server, client);
  379. if(user == NULL)
  380. {
  381. packageSendFailAnswer(clientSocket);
  382. return 0;
  383. }
  384. Stream out;
  385. streamInit(&out, 16);
  386. char workingPath[256];
  387. if(getcwd(workingPath, 256) != NULL)
  388. {
  389. sendMailList(user, &out);
  390. assert(chdir(workingPath) == 0);
  391. }
  392. serverSend(clientSocket, &out);
  393. streamRemove(&out);
  394. return 0;
  395. }
  396. void readMail(char* user, char* number, Stream* out)
  397. {
  398. DIR* receiverDir = opendir(user);
  399. if(receiverDir == NULL)
  400. {
  401. streamWriteChars(out, "ERR\n");
  402. return;
  403. }
  404. assert(chdir(user) == 0);
  405. struct dirent* receiverIterator;
  406. receiverIterator = readdir(receiverDir);
  407. while(receiverIterator != NULL)
  408. {
  409. if(!isSpecialFolder(receiverIterator->d_name))
  410. {
  411. if(chdir(receiverIterator->d_name) == 0)
  412. {
  413. FILE* file = fopen(number, "r");
  414. if(file != NULL)
  415. {
  416. streamWriteChars(out, "OK\n");
  417. // write data to stream
  418. int data;
  419. while(1)
  420. {
  421. data = fgetc(file);
  422. if(data == EOF)
  423. {
  424. break;
  425. }
  426. streamWriteChar(out, data);
  427. }
  428. streamWriteChars(out, ".\n");
  429. if(fclose(file) == EOF)
  430. {
  431. perror("cannot close file");
  432. }
  433. if(closedir(receiverDir) == -1)
  434. {
  435. perror("cannot close receiver dir");
  436. }
  437. return;
  438. }
  439. assert(chdir("../") == 0);
  440. }
  441. }
  442. receiverIterator = readdir(receiverDir);
  443. }
  444. if(closedir(receiverDir) == -1)
  445. {
  446. perror("cannot close dir");
  447. }
  448. streamWriteChars(out, "ERR\n");
  449. }
  450. int packageRead(int client, int clientSocket, Stream* in)
  451. {
  452. char* user = serverGetUser(&server, client);
  453. if(user == NULL)
  454. {
  455. packageSendFailAnswer(clientSocket);
  456. return 0;
  457. }
  458. // number
  459. String number;
  460. stringInit(&number);
  461. stringReadStreamLine(&number, in);
  462. if(stringGetLength(&number) == 0)
  463. {
  464. packageSendFailAnswer(clientSocket);
  465. stringRemove(&number);
  466. return 0;
  467. }
  468. Stream out;
  469. streamInit(&out, 16);
  470. char workingPath[256];
  471. if(getcwd(workingPath, 256) != NULL)
  472. {
  473. readMail(user, number.data, &out);
  474. assert(chdir(workingPath) == 0);
  475. }
  476. else
  477. {
  478. streamWriteChars(&out, "ERR\n");
  479. }
  480. stringRemove(&number);
  481. serverSend(clientSocket, &out);
  482. streamRemove(&out);
  483. return 0;
  484. }
  485. int deleteMail(char* user, char* number)
  486. {
  487. DIR* receiverDir = opendir(user);
  488. if(receiverDir == NULL)
  489. {
  490. return 0;
  491. }
  492. assert(chdir(user) == 0);
  493. int rValue = 0;
  494. struct dirent* receiverIterator;
  495. receiverIterator = readdir(receiverDir);
  496. while(receiverIterator != NULL)
  497. {
  498. if(!isSpecialFolder(receiverIterator->d_name))
  499. {
  500. if(chdir(receiverIterator->d_name) == 0)
  501. {
  502. if(remove(number) == 0)
  503. {
  504. rValue = 1;
  505. break;
  506. }
  507. assert(chdir("../") == 0);
  508. }
  509. }
  510. receiverIterator = readdir(receiverDir);
  511. }
  512. if(closedir(receiverDir) == -1)
  513. {
  514. perror("cannot close dir");
  515. }
  516. return rValue;
  517. }
  518. int packageDelete(int client, int clientSocket, Stream* in)
  519. {
  520. char* user = serverGetUser(&server, client);
  521. if(user == NULL)
  522. {
  523. packageSendFailAnswer(clientSocket);
  524. return 0;
  525. }
  526. // number
  527. String number;
  528. stringInit(&number);
  529. stringReadStreamLine(&number, in);
  530. if(stringGetLength(&number) == 0)
  531. {
  532. packageSendFailAnswer(clientSocket);
  533. stringRemove(&number);
  534. return 0;
  535. }
  536. Stream out;
  537. streamInit(&out, 16);
  538. char workingPath[256];
  539. if(getcwd(workingPath, 256) != NULL)
  540. {
  541. if(deleteMail(user, number.data))
  542. {
  543. streamWriteChars(&out, "OK\n");
  544. }
  545. else
  546. {
  547. streamWriteChars(&out, "ERR\n");
  548. }
  549. assert(chdir(workingPath) == 0);
  550. }
  551. else
  552. {
  553. streamWriteChars(&out, "ERR\n");
  554. }
  555. stringRemove(&number);
  556. serverSend(clientSocket, &out);
  557. streamRemove(&out);
  558. return 0;
  559. }
  560. int packageLogin(int client, int clientSocket, Stream* in)
  561. {
  562. int length;
  563. // receiver
  564. String user;
  565. stringInit(&user);
  566. stringReadStreamLine(&user, in);
  567. length = stringGetLength(&user);
  568. if(length == 0 || length > 8)
  569. {
  570. packageSendFailAnswer(clientSocket);
  571. stringRemove(&user);
  572. return 0;
  573. }
  574. // password
  575. String password;
  576. stringInit(&password);
  577. stringReadStreamLine(&password, in);
  578. if(!ldapTryLogin(user.data, password.data))
  579. {
  580. stringRemove(&user);
  581. stringRemove(&password);
  582. if(serverRaiseLoginFails(&server, client))
  583. {
  584. serverBann(&server, client);
  585. return 1;
  586. }
  587. packageSendFailAnswer(clientSocket);
  588. return 0;
  589. }
  590. stringRemove(&password);
  591. serverResetLoginFails(&server, client);
  592. if(serverSetUser(&server, client, user.data))
  593. {
  594. packageSendFailAnswer(clientSocket);
  595. stringRemove(&user);
  596. return 0;
  597. }
  598. packageSendOkAnswer(clientSocket);
  599. stringRemove(&user);
  600. return 0;
  601. }
  602. int package(int client, int clientSocket, Stream* in)
  603. {
  604. if(streamHasData(in))
  605. {
  606. String s;
  607. stringInit(&s);
  608. stringReadStreamLine(&s, in);
  609. int r = 0;
  610. if(stringCompare(&s, "SEND"))
  611. {
  612. r = packageSend(client, clientSocket, in);
  613. }
  614. else if(stringCompare(&s, "LIST"))
  615. {
  616. r = packageList(client, clientSocket, in);
  617. }
  618. else if(stringCompare(&s, "READ"))
  619. {
  620. r = packageRead(client, clientSocket, in);
  621. }
  622. else if(stringCompare(&s, "DEL"))
  623. {
  624. r = packageDelete(client, clientSocket, in);
  625. }
  626. else if(stringCompare(&s, "LOGIN"))
  627. {
  628. r = packageLogin(client, clientSocket, in);
  629. }
  630. else
  631. {
  632. printf("Invalid package from %d\n", client);
  633. r = 1;
  634. }
  635. stringRemove(&s);
  636. return r;
  637. }
  638. return 0;
  639. }
  640. int main(int argc, char** argv)
  641. {
  642. if(argc < 3)
  643. {
  644. printf("Usage: %s directory port\n", argv[0]);
  645. return EXIT_FAILURE;
  646. }
  647. int port = atoi(argv[2]);
  648. if(port <= 0 || port >= 65536)
  649. {
  650. printf("invalid port");
  651. return EXIT_FAILURE;
  652. }
  653. serverInitDefaults(&server);
  654. signal(SIGINT, interruptHandler);
  655. signal(SIGKILL, interruptHandler);
  656. if(serverInit(&server, 3, port, argv[1]) == -1)
  657. {
  658. return EXIT_FAILURE;
  659. }
  660. serverRegisterHandler(&server, package);
  661. serverWaitForConnection(&server);
  662. serverRemove(&server);
  663. return EXIT_SUCCESS;
  664. }