netdb.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/Assertions.h>
  27. #include <AK/ByteBuffer.h>
  28. #include <AK/ScopeGuard.h>
  29. #include <AK/String.h>
  30. #include <Kernel/Net/IPv4.h>
  31. #include <arpa/inet.h>
  32. #include <netdb.h>
  33. #include <netinet/in.h>
  34. #include <stdio.h>
  35. #include <string.h>
  36. #include <unistd.h>
  37. extern "C" {
  38. int h_errno;
  39. static hostent __gethostbyname_buffer;
  40. static char __gethostbyname_name_buffer[512];
  41. static in_addr_t __gethostbyname_address;
  42. static in_addr_t* __gethostbyname_address_list_buffer[2];
  43. static hostent __gethostbyaddr_buffer;
  44. static char __gethostbyaddr_name_buffer[512];
  45. static in_addr_t* __gethostbyaddr_address_list_buffer[2];
  46. //Get service entry buffers and file information for the getservent() family of functions
  47. static FILE* services_file = nullptr;
  48. static const char* services_path = "/etc/services";
  49. static bool fill_getserv_buffers(char* line, ssize_t read);
  50. static servent __getserv_buffer;
  51. static char __getserv_name_buffer[512];
  52. static char __getserv_protocol_buffer[10];
  53. static int __getserv_port_buffer;
  54. static Vector<ByteBuffer> __getserv_alias_list_buffer;
  55. static Vector<char*> __getserv_alias_list;
  56. static bool keep_service_file_open = false;
  57. static ssize_t service_file_offset = 0;
  58. //Get protocol entry buffers and file information for the getprotent() family of functions
  59. static FILE* protocols_file = nullptr;
  60. static const char* protocols_path = "/etc/protocols";
  61. static bool fill_getproto_buffers(char* line, ssize_t read);
  62. static protoent __getproto_buffer;
  63. static char __getproto_name_buffer[512];
  64. static Vector<ByteBuffer> __getproto_alias_list_buffer;
  65. static Vector<char*> __getproto_alias_list;
  66. static int __getproto_protocol_buffer;
  67. static bool keep_protocols_file_open = false;
  68. static ssize_t protocol_file_offset = 0;
  69. static int connect_to_lookup_server()
  70. {
  71. int fd = socket(AF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0);
  72. if (fd < 0) {
  73. perror("socket");
  74. return -1;
  75. }
  76. sockaddr_un address;
  77. address.sun_family = AF_LOCAL;
  78. strcpy(address.sun_path, "/tmp/portal/lookup");
  79. if (connect(fd, (const sockaddr*)&address, sizeof(address)) < 0) {
  80. perror("connect_to_lookup_server");
  81. close(fd);
  82. return -1;
  83. }
  84. return fd;
  85. }
  86. hostent* gethostbyname(const char* name)
  87. {
  88. auto ipv4_address = IPv4Address::from_string(name);
  89. if (ipv4_address.has_value()) {
  90. sprintf(__gethostbyname_name_buffer, "%s", ipv4_address.value().to_string().characters());
  91. __gethostbyname_buffer.h_name = __gethostbyname_name_buffer;
  92. __gethostbyname_buffer.h_aliases = nullptr;
  93. __gethostbyname_buffer.h_addrtype = AF_INET;
  94. new (&__gethostbyname_address) IPv4Address(ipv4_address.value());
  95. __gethostbyname_address_list_buffer[0] = &__gethostbyname_address;
  96. __gethostbyname_address_list_buffer[1] = nullptr;
  97. __gethostbyname_buffer.h_addr_list = (char**)__gethostbyname_address_list_buffer;
  98. __gethostbyname_buffer.h_length = 4;
  99. return &__gethostbyname_buffer;
  100. }
  101. int fd = connect_to_lookup_server();
  102. if (fd < 0)
  103. return nullptr;
  104. auto close_fd_on_exit = ScopeGuard([fd] {
  105. close(fd);
  106. });
  107. auto line = String::format("L%s\n", name);
  108. int nsent = write(fd, line.characters(), line.length());
  109. if (nsent < 0) {
  110. perror("write");
  111. return nullptr;
  112. }
  113. ASSERT((size_t)nsent == line.length());
  114. char buffer[1024];
  115. int nrecv = read(fd, buffer, sizeof(buffer) - 1);
  116. if (nrecv < 0) {
  117. perror("recv");
  118. return nullptr;
  119. }
  120. if (!memcmp(buffer, "Not found.", sizeof("Not found.") - 1))
  121. return nullptr;
  122. auto responses = String(buffer, nrecv).split('\n');
  123. if (responses.is_empty())
  124. return nullptr;
  125. auto& response = responses[0];
  126. int rc = inet_pton(AF_INET, response.characters(), &__gethostbyname_address);
  127. if (rc <= 0)
  128. return nullptr;
  129. strncpy(__gethostbyname_name_buffer, name, sizeof(__gethostbyaddr_name_buffer) - 1);
  130. __gethostbyname_buffer.h_name = __gethostbyname_name_buffer;
  131. __gethostbyname_buffer.h_aliases = nullptr;
  132. __gethostbyname_buffer.h_addrtype = AF_INET;
  133. __gethostbyname_address_list_buffer[0] = &__gethostbyname_address;
  134. __gethostbyname_address_list_buffer[1] = nullptr;
  135. __gethostbyname_buffer.h_addr_list = (char**)__gethostbyname_address_list_buffer;
  136. __gethostbyname_buffer.h_length = 4;
  137. return &__gethostbyname_buffer;
  138. }
  139. hostent* gethostbyaddr(const void* addr, socklen_t addr_size, int type)
  140. {
  141. if (type != AF_INET) {
  142. errno = EAFNOSUPPORT;
  143. return nullptr;
  144. }
  145. if (addr_size < sizeof(in_addr)) {
  146. errno = EINVAL;
  147. return nullptr;
  148. }
  149. int fd = connect_to_lookup_server();
  150. if (fd < 0)
  151. return nullptr;
  152. auto close_fd_on_exit = ScopeGuard([fd] {
  153. close(fd);
  154. });
  155. IPv4Address ipv4_address((const u8*)&((const in_addr*)addr)->s_addr);
  156. auto line = String::format("R%d.%d.%d.%d.in-addr.arpa\n",
  157. ipv4_address[3],
  158. ipv4_address[2],
  159. ipv4_address[1],
  160. ipv4_address[0]);
  161. int nsent = write(fd, line.characters(), line.length());
  162. if (nsent < 0) {
  163. perror("write");
  164. return nullptr;
  165. }
  166. ASSERT((size_t)nsent == line.length());
  167. char buffer[1024];
  168. int nrecv = read(fd, buffer, sizeof(buffer) - 1);
  169. if (nrecv < 0) {
  170. perror("recv");
  171. return nullptr;
  172. }
  173. if (!memcmp(buffer, "Not found.", sizeof("Not found.") - 1))
  174. return nullptr;
  175. auto responses = String(buffer, nrecv).split('\n');
  176. if (responses.is_empty())
  177. return nullptr;
  178. auto& response = responses[0];
  179. strncpy(__gethostbyaddr_name_buffer, response.characters(), max(sizeof(__gethostbyaddr_name_buffer), response.length()));
  180. __gethostbyaddr_buffer.h_name = __gethostbyaddr_name_buffer;
  181. __gethostbyaddr_buffer.h_aliases = nullptr;
  182. __gethostbyaddr_buffer.h_addrtype = AF_INET;
  183. // FIXME: Should we populate the hostent's address list here with a sockaddr_in for the provided host?
  184. __gethostbyaddr_address_list_buffer[0] = nullptr;
  185. __gethostbyaddr_buffer.h_addr_list = (char**)__gethostbyaddr_address_list_buffer;
  186. __gethostbyaddr_buffer.h_length = 4;
  187. return &__gethostbyaddr_buffer;
  188. }
  189. struct servent* getservent()
  190. {
  191. //If the services file is not open, attempt to open it and return null if it fails.
  192. if (!services_file) {
  193. services_file = fopen(services_path, "r");
  194. if (!services_file) {
  195. perror("error opening services file");
  196. return nullptr;
  197. }
  198. }
  199. if (fseek(services_file, service_file_offset, SEEK_SET) != 0) {
  200. perror("error seeking file");
  201. fclose(services_file);
  202. return nullptr;
  203. }
  204. char* line = nullptr;
  205. size_t len = 0;
  206. ssize_t read;
  207. auto free_line_on_exit = ScopeGuard([line] {
  208. if (line) {
  209. free(line);
  210. }
  211. });
  212. //Read lines from services file until an actual service name is found.
  213. do {
  214. read = getline(&line, &len, services_file);
  215. service_file_offset += read;
  216. if (read > 0 && (line[0] >= 65 && line[0] <= 122)) {
  217. break;
  218. }
  219. } while (read != -1);
  220. if (read == -1) {
  221. fclose(services_file);
  222. services_file = nullptr;
  223. service_file_offset = 0;
  224. return nullptr;
  225. }
  226. servent* service_entry = nullptr;
  227. if (!fill_getserv_buffers(line, read))
  228. return nullptr;
  229. __getserv_buffer.s_name = __getserv_name_buffer;
  230. __getserv_buffer.s_port = __getserv_port_buffer;
  231. __getserv_buffer.s_proto = __getserv_protocol_buffer;
  232. __getserv_alias_list.clear();
  233. for (auto& alias : __getserv_alias_list_buffer) {
  234. __getserv_alias_list.append((char*)alias.data());
  235. }
  236. __getserv_buffer.s_aliases = __getserv_alias_list.data();
  237. service_entry = &__getserv_buffer;
  238. if (!keep_service_file_open) {
  239. endservent();
  240. }
  241. return service_entry;
  242. }
  243. struct servent* getservbyname(const char* name, const char* protocol)
  244. {
  245. bool previous_file_open_setting = keep_service_file_open;
  246. setservent(1);
  247. struct servent* current_service = nullptr;
  248. auto service_file_handler = ScopeGuard([previous_file_open_setting] {
  249. if (!previous_file_open_setting) {
  250. endservent();
  251. }
  252. });
  253. while (true) {
  254. current_service = getservent();
  255. if (current_service == nullptr)
  256. break;
  257. else if (!protocol && strcmp(current_service->s_name, name) == 0)
  258. break;
  259. else if (strcmp(current_service->s_name, name) == 0 && strcmp(current_service->s_proto, protocol) == 0)
  260. break;
  261. }
  262. return current_service;
  263. }
  264. struct servent* getservbyport(int port, const char* protocol)
  265. {
  266. bool previous_file_open_setting = keep_service_file_open;
  267. setservent(1);
  268. struct servent* current_service = nullptr;
  269. auto service_file_handler = ScopeGuard([previous_file_open_setting] {
  270. if (!previous_file_open_setting) {
  271. endservent();
  272. }
  273. });
  274. while (true) {
  275. current_service = getservent();
  276. if (current_service == nullptr)
  277. break;
  278. else if (!protocol && current_service->s_port == port)
  279. break;
  280. else if (current_service->s_port == port && (strcmp(current_service->s_proto, protocol) == 0))
  281. break;
  282. }
  283. return current_service;
  284. }
  285. void setservent(int stay_open)
  286. {
  287. if (!services_file) {
  288. services_file = fopen(services_path, "r");
  289. if (!services_file) {
  290. perror("error opening services file");
  291. return;
  292. }
  293. }
  294. rewind(services_file);
  295. keep_service_file_open = stay_open;
  296. service_file_offset = 0;
  297. }
  298. void endservent()
  299. {
  300. if (!services_file) {
  301. return;
  302. }
  303. fclose(services_file);
  304. services_file = nullptr;
  305. }
  306. //Fill the service entry buffer with the information contained in the currently read line, returns true if successfull, false if failure occurs.
  307. static bool fill_getserv_buffers(char* line, ssize_t read)
  308. {
  309. //Splitting the line by tab delimiter and filling the servent buffers name, port, and protocol members.
  310. String string_line = String(line, read);
  311. string_line.replace(" ", "\t", true);
  312. auto split_line = string_line.split('\t');
  313. //This indicates an incorrect file format. Services file entries should always at least contain name and port/protocol, seperated by tabs.
  314. if (split_line.size() < 2) {
  315. perror("malformed services file: entry");
  316. return false;
  317. }
  318. if (sizeof(__getserv_name_buffer) >= split_line[0].length() + 1) {
  319. strncpy(__getserv_name_buffer, split_line[0].characters(), split_line[0].length() + 1);
  320. } else {
  321. perror("invalid buffer length: service name");
  322. return false;
  323. }
  324. auto port_protocol_split = String(split_line[1]).split('/');
  325. if (port_protocol_split.size() < 2) {
  326. perror("malformed services file: port/protocol");
  327. return false;
  328. }
  329. bool conversion_checker;
  330. __getserv_port_buffer = port_protocol_split[0].to_int(conversion_checker);
  331. if (!conversion_checker) {
  332. return false;
  333. }
  334. //Removing any annoying whitespace at the end of the protocol.
  335. port_protocol_split[1].replace(" ", "", true);
  336. port_protocol_split[1].replace("\t", "", true);
  337. port_protocol_split[1].replace("\n", "", true);
  338. if (sizeof(__getserv_protocol_buffer) >= port_protocol_split[1].length()) {
  339. strncpy(__getserv_protocol_buffer, port_protocol_split[1].characters(), port_protocol_split[1].length() + 1);
  340. } else {
  341. perror("malformed services file: protocol");
  342. return false;
  343. }
  344. __getserv_alias_list_buffer.clear();
  345. //If there are aliases for the service, we will fill the alias list buffer.
  346. if (split_line.size() > 2 && !split_line[2].starts_with('#')) {
  347. for (size_t i = 2; i < split_line.size(); i++) {
  348. if (split_line[i].starts_with('#')) {
  349. break;
  350. }
  351. auto alias = split_line[i].to_byte_buffer();
  352. alias.append("\0", sizeof(char));
  353. __getserv_alias_list_buffer.append(alias);
  354. }
  355. }
  356. return true;
  357. }
  358. struct protoent* getprotoent()
  359. {
  360. //If protocols file isn't open, attempt to open and return null on failure.
  361. if (!protocols_file) {
  362. protocols_file = fopen(protocols_path, "r");
  363. if (!protocols_file) {
  364. perror("error opening protocols file");
  365. return nullptr;
  366. }
  367. }
  368. if (fseek(protocols_file, protocol_file_offset, SEEK_SET) != 0) {
  369. perror("error seeking protocols file");
  370. fclose(protocols_file);
  371. return nullptr;
  372. }
  373. char* line = nullptr;
  374. size_t len = 0;
  375. ssize_t read;
  376. auto free_line_on_exit = ScopeGuard([line] {
  377. if (line) {
  378. free(line);
  379. }
  380. });
  381. do {
  382. read = getline(&line, &len, protocols_file);
  383. protocol_file_offset += read;
  384. if (read > 0 && (line[0] >= 65 && line[0] <= 122)) {
  385. break;
  386. }
  387. } while (read != -1);
  388. if (read == -1) {
  389. fclose(protocols_file);
  390. protocols_file = nullptr;
  391. protocol_file_offset = 0;
  392. return nullptr;
  393. }
  394. struct protoent* protocol_entry = nullptr;
  395. if (!fill_getproto_buffers(line, read))
  396. return nullptr;
  397. __getproto_buffer.p_name = __getproto_name_buffer;
  398. __getproto_buffer.p_proto = __getproto_protocol_buffer;
  399. __getproto_alias_list.clear();
  400. for (auto& alias : __getproto_alias_list_buffer) {
  401. __getproto_alias_list.append((char*)alias.data());
  402. }
  403. __getproto_buffer.p_aliases = __getproto_alias_list.data();
  404. protocol_entry = &__getproto_buffer;
  405. if (!keep_protocols_file_open)
  406. endprotoent();
  407. return protocol_entry;
  408. }
  409. struct protoent* getprotobyname(const char* name)
  410. {
  411. bool previous_file_open_setting = keep_protocols_file_open;
  412. setprotoent(1);
  413. struct protoent* current_protocol = nullptr;
  414. auto protocol_file_handler = ScopeGuard([previous_file_open_setting] {
  415. if (!previous_file_open_setting) {
  416. endprotoent();
  417. }
  418. });
  419. while (true) {
  420. current_protocol = getprotoent();
  421. if (current_protocol == nullptr)
  422. break;
  423. else if (strcmp(current_protocol->p_name, name) == 0)
  424. break;
  425. }
  426. return current_protocol;
  427. }
  428. struct protoent* getprotobynumber(int proto)
  429. {
  430. bool previous_file_open_setting = keep_protocols_file_open;
  431. setprotoent(1);
  432. struct protoent* current_protocol = nullptr;
  433. auto protocol_file_handler = ScopeGuard([previous_file_open_setting] {
  434. if (!previous_file_open_setting) {
  435. endprotoent();
  436. }
  437. });
  438. while (true) {
  439. current_protocol = getprotoent();
  440. if (current_protocol == nullptr)
  441. break;
  442. else if (current_protocol->p_proto == proto)
  443. break;
  444. }
  445. return current_protocol;
  446. }
  447. void setprotoent(int stay_open)
  448. {
  449. if (!protocols_file) {
  450. protocols_file = fopen(protocols_path, "r");
  451. if (!protocols_file) {
  452. perror("error opening protocols file");
  453. return;
  454. }
  455. }
  456. rewind(protocols_file);
  457. keep_protocols_file_open = stay_open;
  458. protocol_file_offset = 0;
  459. }
  460. void endprotoent()
  461. {
  462. if (!protocols_file) {
  463. return;
  464. }
  465. fclose(protocols_file);
  466. protocols_file = nullptr;
  467. }
  468. static bool fill_getproto_buffers(char* line, ssize_t read)
  469. {
  470. String string_line = String(line, read);
  471. string_line.replace(" ", "\t", true);
  472. auto split_line = string_line.split('\t');
  473. //This indicates an incorrect file format. Protocols file entries should always have at least a name and a protocol.
  474. if (split_line.size() < 2) {
  475. perror("malformed protocols file: entry");
  476. return false;
  477. }
  478. if (sizeof(__getproto_name_buffer) >= split_line[0].length() + 1) {
  479. strncpy(__getproto_name_buffer, split_line[0].characters(), split_line[0].length() + 1);
  480. } else {
  481. perror("invalid buffer length: protocol name");
  482. return false;
  483. }
  484. bool conversion_checker;
  485. __getproto_protocol_buffer = split_line[1].to_int(conversion_checker);
  486. if (!conversion_checker) {
  487. return false;
  488. }
  489. __getproto_alias_list_buffer.clear();
  490. //If there are aliases for the protocol, we will fill the alias list buffer.
  491. if (split_line.size() > 2 && !split_line[2].starts_with('#')) {
  492. for (size_t i = 2; i < split_line.size(); i++) {
  493. if (split_line[i].starts_with('#')) {
  494. break;
  495. }
  496. auto alias = split_line[i].to_byte_buffer();
  497. alias.append("\0", sizeof(char));
  498. __getproto_alias_list_buffer.append(alias);
  499. }
  500. }
  501. return true;
  502. }
  503. }