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. auto ip4_string = ipv4_address.value().to_string();
  91. ASSERT(ip4_string.length() < sizeof(__gethostbyname_name_buffer));
  92. strncpy(__gethostbyname_name_buffer, ip4_string.characters(), ip4_string.length());
  93. __gethostbyname_buffer.h_name = __gethostbyname_name_buffer;
  94. __gethostbyname_buffer.h_aliases = nullptr;
  95. __gethostbyname_buffer.h_addrtype = AF_INET;
  96. new (&__gethostbyname_address) IPv4Address(ipv4_address.value());
  97. __gethostbyname_address_list_buffer[0] = &__gethostbyname_address;
  98. __gethostbyname_address_list_buffer[1] = nullptr;
  99. __gethostbyname_buffer.h_addr_list = (char**)__gethostbyname_address_list_buffer;
  100. __gethostbyname_buffer.h_length = 4;
  101. return &__gethostbyname_buffer;
  102. }
  103. int fd = connect_to_lookup_server();
  104. if (fd < 0)
  105. return nullptr;
  106. auto close_fd_on_exit = ScopeGuard([fd] {
  107. close(fd);
  108. });
  109. auto line = String::format("L%s\n", name);
  110. int nsent = write(fd, line.characters(), line.length());
  111. if (nsent < 0) {
  112. perror("write");
  113. return nullptr;
  114. }
  115. ASSERT((size_t)nsent == line.length());
  116. char buffer[1024];
  117. int nrecv = read(fd, buffer, sizeof(buffer) - 1);
  118. if (nrecv < 0) {
  119. perror("recv");
  120. return nullptr;
  121. }
  122. if (!memcmp(buffer, "Not found.", sizeof("Not found.") - 1))
  123. return nullptr;
  124. auto responses = String(buffer, nrecv).split('\n');
  125. if (responses.is_empty())
  126. return nullptr;
  127. auto& response = responses[0];
  128. int rc = inet_pton(AF_INET, response.characters(), &__gethostbyname_address);
  129. if (rc <= 0)
  130. return nullptr;
  131. strncpy(__gethostbyname_name_buffer, name, sizeof(__gethostbyaddr_name_buffer) - 1);
  132. __gethostbyname_buffer.h_name = __gethostbyname_name_buffer;
  133. __gethostbyname_buffer.h_aliases = nullptr;
  134. __gethostbyname_buffer.h_addrtype = AF_INET;
  135. __gethostbyname_address_list_buffer[0] = &__gethostbyname_address;
  136. __gethostbyname_address_list_buffer[1] = nullptr;
  137. __gethostbyname_buffer.h_addr_list = (char**)__gethostbyname_address_list_buffer;
  138. __gethostbyname_buffer.h_length = 4;
  139. return &__gethostbyname_buffer;
  140. }
  141. hostent* gethostbyaddr(const void* addr, socklen_t addr_size, int type)
  142. {
  143. if (type != AF_INET) {
  144. errno = EAFNOSUPPORT;
  145. return nullptr;
  146. }
  147. if (addr_size < sizeof(in_addr)) {
  148. errno = EINVAL;
  149. return nullptr;
  150. }
  151. int fd = connect_to_lookup_server();
  152. if (fd < 0)
  153. return nullptr;
  154. auto close_fd_on_exit = ScopeGuard([fd] {
  155. close(fd);
  156. });
  157. IPv4Address ipv4_address((const u8*)&((const in_addr*)addr)->s_addr);
  158. auto line = String::format("R%d.%d.%d.%d.in-addr.arpa\n",
  159. ipv4_address[3],
  160. ipv4_address[2],
  161. ipv4_address[1],
  162. ipv4_address[0]);
  163. int nsent = write(fd, line.characters(), line.length());
  164. if (nsent < 0) {
  165. perror("write");
  166. return nullptr;
  167. }
  168. ASSERT((size_t)nsent == line.length());
  169. char buffer[1024];
  170. int nrecv = read(fd, buffer, sizeof(buffer) - 1);
  171. if (nrecv < 0) {
  172. perror("recv");
  173. return nullptr;
  174. }
  175. if (!memcmp(buffer, "Not found.", sizeof("Not found.") - 1))
  176. return nullptr;
  177. auto responses = String(buffer, nrecv).split('\n');
  178. if (responses.is_empty())
  179. return nullptr;
  180. auto& response = responses[0];
  181. strncpy(__gethostbyaddr_name_buffer, response.characters(), max(sizeof(__gethostbyaddr_name_buffer), response.length()));
  182. __gethostbyaddr_buffer.h_name = __gethostbyaddr_name_buffer;
  183. __gethostbyaddr_buffer.h_aliases = nullptr;
  184. __gethostbyaddr_buffer.h_addrtype = AF_INET;
  185. // FIXME: Should we populate the hostent's address list here with a sockaddr_in for the provided host?
  186. __gethostbyaddr_address_list_buffer[0] = nullptr;
  187. __gethostbyaddr_buffer.h_addr_list = (char**)__gethostbyaddr_address_list_buffer;
  188. __gethostbyaddr_buffer.h_length = 4;
  189. return &__gethostbyaddr_buffer;
  190. }
  191. struct servent* getservent()
  192. {
  193. //If the services file is not open, attempt to open it and return null if it fails.
  194. if (!services_file) {
  195. services_file = fopen(services_path, "r");
  196. if (!services_file) {
  197. perror("error opening services file");
  198. return nullptr;
  199. }
  200. }
  201. if (fseek(services_file, service_file_offset, SEEK_SET) != 0) {
  202. perror("error seeking file");
  203. fclose(services_file);
  204. return nullptr;
  205. }
  206. char* line = nullptr;
  207. size_t len = 0;
  208. ssize_t read;
  209. auto free_line_on_exit = ScopeGuard([line] {
  210. if (line) {
  211. free(line);
  212. }
  213. });
  214. //Read lines from services file until an actual service name is found.
  215. do {
  216. read = getline(&line, &len, services_file);
  217. service_file_offset += read;
  218. if (read > 0 && (line[0] >= 65 && line[0] <= 122)) {
  219. break;
  220. }
  221. } while (read != -1);
  222. if (read == -1) {
  223. fclose(services_file);
  224. services_file = nullptr;
  225. service_file_offset = 0;
  226. return nullptr;
  227. }
  228. servent* service_entry = nullptr;
  229. if (!fill_getserv_buffers(line, read))
  230. return nullptr;
  231. __getserv_buffer.s_name = __getserv_name_buffer;
  232. __getserv_buffer.s_port = __getserv_port_buffer;
  233. __getserv_buffer.s_proto = __getserv_protocol_buffer;
  234. __getserv_alias_list.clear();
  235. for (auto& alias : __getserv_alias_list_buffer) {
  236. __getserv_alias_list.append((char*)alias.data());
  237. }
  238. __getserv_buffer.s_aliases = __getserv_alias_list.data();
  239. service_entry = &__getserv_buffer;
  240. if (!keep_service_file_open) {
  241. endservent();
  242. }
  243. return service_entry;
  244. }
  245. struct servent* getservbyname(const char* name, const char* protocol)
  246. {
  247. bool previous_file_open_setting = keep_service_file_open;
  248. setservent(1);
  249. struct servent* current_service = nullptr;
  250. auto service_file_handler = ScopeGuard([previous_file_open_setting] {
  251. if (!previous_file_open_setting) {
  252. endservent();
  253. }
  254. });
  255. while (true) {
  256. current_service = getservent();
  257. if (current_service == nullptr)
  258. break;
  259. else if (!protocol && strcmp(current_service->s_name, name) == 0)
  260. break;
  261. else if (strcmp(current_service->s_name, name) == 0 && strcmp(current_service->s_proto, protocol) == 0)
  262. break;
  263. }
  264. return current_service;
  265. }
  266. struct servent* getservbyport(int port, const char* protocol)
  267. {
  268. bool previous_file_open_setting = keep_service_file_open;
  269. setservent(1);
  270. struct servent* current_service = nullptr;
  271. auto service_file_handler = ScopeGuard([previous_file_open_setting] {
  272. if (!previous_file_open_setting) {
  273. endservent();
  274. }
  275. });
  276. while (true) {
  277. current_service = getservent();
  278. if (current_service == nullptr)
  279. break;
  280. else if (!protocol && current_service->s_port == port)
  281. break;
  282. else if (current_service->s_port == port && (strcmp(current_service->s_proto, protocol) == 0))
  283. break;
  284. }
  285. return current_service;
  286. }
  287. void setservent(int stay_open)
  288. {
  289. if (!services_file) {
  290. services_file = fopen(services_path, "r");
  291. if (!services_file) {
  292. perror("error opening services file");
  293. return;
  294. }
  295. }
  296. rewind(services_file);
  297. keep_service_file_open = stay_open;
  298. service_file_offset = 0;
  299. }
  300. void endservent()
  301. {
  302. if (!services_file) {
  303. return;
  304. }
  305. fclose(services_file);
  306. services_file = nullptr;
  307. }
  308. //Fill the service entry buffer with the information contained in the currently read line, returns true if successfull, false if failure occurs.
  309. static bool fill_getserv_buffers(char* line, ssize_t read)
  310. {
  311. //Splitting the line by tab delimiter and filling the servent buffers name, port, and protocol members.
  312. String string_line = String(line, read);
  313. string_line.replace(" ", "\t", true);
  314. auto split_line = string_line.split('\t');
  315. //This indicates an incorrect file format. Services file entries should always at least contain name and port/protocol, seperated by tabs.
  316. if (split_line.size() < 2) {
  317. perror("malformed services file: entry");
  318. return false;
  319. }
  320. if (sizeof(__getserv_name_buffer) >= split_line[0].length() + 1) {
  321. strncpy(__getserv_name_buffer, split_line[0].characters(), split_line[0].length() + 1);
  322. } else {
  323. perror("invalid buffer length: service name");
  324. return false;
  325. }
  326. auto port_protocol_split = String(split_line[1]).split('/');
  327. if (port_protocol_split.size() < 2) {
  328. perror("malformed services file: port/protocol");
  329. return false;
  330. }
  331. auto number = port_protocol_split[0].to_int();
  332. if (!number.has_value())
  333. return false;
  334. __getserv_port_buffer = number.value();
  335. //Removing any annoying whitespace at the end of the protocol.
  336. port_protocol_split[1].replace(" ", "", true);
  337. port_protocol_split[1].replace("\t", "", true);
  338. port_protocol_split[1].replace("\n", "", true);
  339. if (sizeof(__getserv_protocol_buffer) >= port_protocol_split[1].length()) {
  340. strncpy(__getserv_protocol_buffer, port_protocol_split[1].characters(), port_protocol_split[1].length() + 1);
  341. } else {
  342. perror("malformed services file: protocol");
  343. return false;
  344. }
  345. __getserv_alias_list_buffer.clear();
  346. //If there are aliases for the service, we will fill the alias list buffer.
  347. if (split_line.size() > 2 && !split_line[2].starts_with('#')) {
  348. for (size_t i = 2; i < split_line.size(); i++) {
  349. if (split_line[i].starts_with('#')) {
  350. break;
  351. }
  352. auto alias = split_line[i].to_byte_buffer();
  353. alias.append("\0", sizeof(char));
  354. __getserv_alias_list_buffer.append(alias);
  355. }
  356. }
  357. return true;
  358. }
  359. struct protoent* getprotoent()
  360. {
  361. //If protocols file isn't open, attempt to open and return null on failure.
  362. if (!protocols_file) {
  363. protocols_file = fopen(protocols_path, "r");
  364. if (!protocols_file) {
  365. perror("error opening protocols file");
  366. return nullptr;
  367. }
  368. }
  369. if (fseek(protocols_file, protocol_file_offset, SEEK_SET) != 0) {
  370. perror("error seeking protocols file");
  371. fclose(protocols_file);
  372. return nullptr;
  373. }
  374. char* line = nullptr;
  375. size_t len = 0;
  376. ssize_t read;
  377. auto free_line_on_exit = ScopeGuard([line] {
  378. if (line) {
  379. free(line);
  380. }
  381. });
  382. do {
  383. read = getline(&line, &len, protocols_file);
  384. protocol_file_offset += read;
  385. if (read > 0 && (line[0] >= 65 && line[0] <= 122)) {
  386. break;
  387. }
  388. } while (read != -1);
  389. if (read == -1) {
  390. fclose(protocols_file);
  391. protocols_file = nullptr;
  392. protocol_file_offset = 0;
  393. return nullptr;
  394. }
  395. struct protoent* protocol_entry = nullptr;
  396. if (!fill_getproto_buffers(line, read))
  397. return nullptr;
  398. __getproto_buffer.p_name = __getproto_name_buffer;
  399. __getproto_buffer.p_proto = __getproto_protocol_buffer;
  400. __getproto_alias_list.clear();
  401. for (auto& alias : __getproto_alias_list_buffer) {
  402. __getproto_alias_list.append((char*)alias.data());
  403. }
  404. __getproto_buffer.p_aliases = __getproto_alias_list.data();
  405. protocol_entry = &__getproto_buffer;
  406. if (!keep_protocols_file_open)
  407. endprotoent();
  408. return protocol_entry;
  409. }
  410. struct protoent* getprotobyname(const char* name)
  411. {
  412. bool previous_file_open_setting = keep_protocols_file_open;
  413. setprotoent(1);
  414. struct protoent* current_protocol = nullptr;
  415. auto protocol_file_handler = ScopeGuard([previous_file_open_setting] {
  416. if (!previous_file_open_setting) {
  417. endprotoent();
  418. }
  419. });
  420. while (true) {
  421. current_protocol = getprotoent();
  422. if (current_protocol == nullptr)
  423. break;
  424. else if (strcmp(current_protocol->p_name, name) == 0)
  425. break;
  426. }
  427. return current_protocol;
  428. }
  429. struct protoent* getprotobynumber(int proto)
  430. {
  431. bool previous_file_open_setting = keep_protocols_file_open;
  432. setprotoent(1);
  433. struct protoent* current_protocol = nullptr;
  434. auto protocol_file_handler = ScopeGuard([previous_file_open_setting] {
  435. if (!previous_file_open_setting) {
  436. endprotoent();
  437. }
  438. });
  439. while (true) {
  440. current_protocol = getprotoent();
  441. if (current_protocol == nullptr)
  442. break;
  443. else if (current_protocol->p_proto == proto)
  444. break;
  445. }
  446. return current_protocol;
  447. }
  448. void setprotoent(int stay_open)
  449. {
  450. if (!protocols_file) {
  451. protocols_file = fopen(protocols_path, "r");
  452. if (!protocols_file) {
  453. perror("error opening protocols file");
  454. return;
  455. }
  456. }
  457. rewind(protocols_file);
  458. keep_protocols_file_open = stay_open;
  459. protocol_file_offset = 0;
  460. }
  461. void endprotoent()
  462. {
  463. if (!protocols_file) {
  464. return;
  465. }
  466. fclose(protocols_file);
  467. protocols_file = nullptr;
  468. }
  469. static bool fill_getproto_buffers(char* line, ssize_t read)
  470. {
  471. String string_line = String(line, read);
  472. string_line.replace(" ", "\t", true);
  473. auto split_line = string_line.split('\t');
  474. //This indicates an incorrect file format. Protocols file entries should always have at least a name and a protocol.
  475. if (split_line.size() < 2) {
  476. perror("malformed protocols file: entry");
  477. return false;
  478. }
  479. if (sizeof(__getproto_name_buffer) >= split_line[0].length() + 1) {
  480. strncpy(__getproto_name_buffer, split_line[0].characters(), split_line[0].length() + 1);
  481. } else {
  482. perror("invalid buffer length: protocol name");
  483. return false;
  484. }
  485. auto number = split_line[1].to_int();
  486. if (!number.has_value())
  487. return false;
  488. __getproto_protocol_buffer = number.value();
  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. }