tld.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  1. /* tld.c --- Declarations for TLD restriction checking.
  2. Copyright (C) 2004-2024 Simon Josefsson.
  3. Copyright (C) 2003-2024 Free Software Foundation, Inc.
  4. Author: Thomas Jacob, Internet24.de
  5. This file is part of GNU Libidn.
  6. GNU Libidn is free software: you can redistribute it and/or
  7. modify it under the terms of either:
  8. * the GNU Lesser General Public License as published by the Free
  9. Software Foundation; either version 3 of the License, or (at
  10. your option) any later version.
  11. or
  12. * the GNU General Public License as published by the Free
  13. Software Foundation; either version 2 of the License, or (at
  14. your option) any later version.
  15. or both in parallel, as here.
  16. GNU Libidn is distributed in the hope that it will be useful,
  17. but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  19. General Public License for more details.
  20. You should have received copies of the GNU General Public License and
  21. the GNU Lesser General Public License along with this program. If
  22. not, see <https://www.gnu.org/licenses/>. */
  23. #include <config.h>
  24. /* Get stringprep_utf8_to_ucs4, stringprep_locale_to_utf8. */
  25. #include <stringprep.h>
  26. /* Get strcmp(). */
  27. #include <string.h>
  28. /* Get specifications. */
  29. #include <tld.h>
  30. /* Array of built-in domain restriction structures. See tlds.c. */
  31. extern const Tld_table *_tld_tables[];
  32. /**
  33. * tld_get_table:
  34. * @tld: TLD name (e.g. "com") as zero terminated ASCII byte string.
  35. * @tables: Zero terminated array of #Tld_table info-structures for
  36. * TLDs.
  37. *
  38. * Get the TLD table for a named TLD by searching through the given
  39. * TLD table array.
  40. *
  41. * Return value: Return structure corresponding to TLD @tld by going
  42. * thru @tables, or return %NULL if no such structure is found.
  43. */
  44. const Tld_table *
  45. tld_get_table (const char *tld, const Tld_table **tables)
  46. {
  47. const Tld_table **tldtable = NULL;
  48. if (!tld || !tables)
  49. return NULL;
  50. for (tldtable = tables; *tldtable; tldtable++)
  51. if (!strcmp ((*tldtable)->name, tld))
  52. return *tldtable;
  53. return NULL;
  54. }
  55. /**
  56. * tld_default_table:
  57. * @tld: TLD name (e.g. "com") as zero terminated ASCII byte string.
  58. * @overrides: Additional zero terminated array of #Tld_table
  59. * info-structures for TLDs, or %NULL to only use library default
  60. * tables.
  61. *
  62. * Get the TLD table for a named TLD, using the internal defaults,
  63. * possibly overridden by the (optional) supplied tables.
  64. *
  65. * Return value: Return structure corresponding to TLD @tld_str, first
  66. * looking through @overrides then thru built-in list, or %NULL if
  67. * no such structure found.
  68. */
  69. const Tld_table *
  70. tld_default_table (const char *tld, const Tld_table **overrides)
  71. {
  72. const Tld_table *tldtable = NULL;
  73. if (!tld)
  74. return NULL;
  75. if (overrides)
  76. tldtable = tld_get_table (tld, overrides);
  77. if (!tldtable)
  78. tldtable = tld_get_table (tld, _tld_tables);
  79. return tldtable;
  80. }
  81. #define DOTP(c) ((c) == 0x002E || (c) == 0x3002 || \
  82. (c) == 0xFF0E || (c) == 0xFF61)
  83. /**
  84. * tld_get_4:
  85. * @in: Array of unicode code points to process. Does not need to be
  86. * zero terminated.
  87. * @inlen: Number of unicode code points.
  88. * @out: Zero terminated ascii result string pointer.
  89. *
  90. * Isolate the top-level domain of @in and return it as an ASCII
  91. * string in @out.
  92. *
  93. * Return value: Return %TLD_SUCCESS on success, or the corresponding
  94. * #Tld_rc error code otherwise.
  95. */
  96. int
  97. tld_get_4 (const uint32_t *in, size_t inlen, char **out)
  98. {
  99. const uint32_t *ipos;
  100. size_t olen;
  101. *out = NULL;
  102. if (!in || inlen == 0)
  103. return TLD_NODATA;
  104. ipos = &in[inlen - 1];
  105. olen = 0;
  106. /* Scan backwards for non(latin)letters. */
  107. while (ipos >= in && ((*ipos >= 0x41 && *ipos <= 0x5A) ||
  108. (*ipos >= 0x61 && *ipos <= 0x7A)))
  109. ipos--, olen++;
  110. if (olen > 0 && ipos >= in && DOTP (*ipos))
  111. {
  112. /* Found something that appears a TLD. */
  113. char *out_s = malloc (sizeof (char) * (olen + 1));
  114. char *opos = out_s;
  115. if (!opos)
  116. return TLD_MALLOC_ERROR;
  117. ipos++;
  118. /* Transcribe to lowercase ascii string. */
  119. for (; ipos < &in[inlen]; ipos++, opos++)
  120. *opos = *ipos > 0x5A ? *ipos : *ipos + 0x20;
  121. *opos = 0;
  122. *out = out_s;
  123. return TLD_SUCCESS;
  124. }
  125. return TLD_NO_TLD;
  126. }
  127. /**
  128. * tld_get_4z:
  129. * @in: Zero terminated array of unicode code points to process.
  130. * @out: Zero terminated ascii result string pointer.
  131. *
  132. * Isolate the top-level domain of @in and return it as an ASCII
  133. * string in @out.
  134. *
  135. * Return value: Return %TLD_SUCCESS on success, or the corresponding
  136. * #Tld_rc error code otherwise.
  137. */
  138. int
  139. tld_get_4z (const uint32_t *in, char **out)
  140. {
  141. const uint32_t *ipos = in;
  142. if (!in)
  143. return TLD_NODATA;
  144. while (*ipos)
  145. ipos++;
  146. return tld_get_4 (in, ipos - in, out);
  147. }
  148. /**
  149. * tld_get_z:
  150. * @in: Zero terminated character array to process.
  151. * @out: Zero terminated ascii result string pointer.
  152. *
  153. * Isolate the top-level domain of @in and return it as an ASCII
  154. * string in @out. The input string @in may be UTF-8, ISO-8859-1 or
  155. * any ASCII compatible character encoding.
  156. *
  157. * Return value: Return %TLD_SUCCESS on success, or the corresponding
  158. * #Tld_rc error code otherwise.
  159. */
  160. int
  161. tld_get_z (const char *in, char **out)
  162. {
  163. uint32_t *iucs;
  164. size_t i, ilen;
  165. int rc;
  166. ilen = strlen (in);
  167. iucs = calloc (ilen, sizeof (*iucs));
  168. if (!iucs)
  169. return TLD_MALLOC_ERROR;
  170. for (i = 0; i < ilen; i++)
  171. iucs[i] = in[i];
  172. rc = tld_get_4 (iucs, ilen, out);
  173. free (iucs);
  174. return rc;
  175. }
  176. /*
  177. * tld_checkchar - verify that character is permitted
  178. * @ch: 32 bit unicode character to check.
  179. * @tld: A #Tld_table data structure to check @ch against.
  180. *
  181. * Verify if @ch is either in [a-z0-9-.] or mentioned as a valid
  182. * character in @tld.
  183. *
  184. * Return value: Return the #Tld_rc value %TLD_SUCCESS if @ch is a
  185. * valid character for the TLD @tld or if @tld is %NULL,
  186. * %TLD_INVALID if @ch is invalid as defined by @tld.
  187. */
  188. static int
  189. _tld_checkchar (uint32_t ch, const Tld_table *tld)
  190. {
  191. const Tld_table_element *s, *e, *m;
  192. if (!tld)
  193. return TLD_SUCCESS;
  194. /* Check for [-a-z0-9.]. */
  195. if ((ch >= 0x61 && ch <= 0x7A) ||
  196. (ch >= 0x30 && ch <= 0x39) || ch == 0x2D || DOTP (ch))
  197. return TLD_SUCCESS;
  198. s = tld->valid;
  199. e = s + tld->nvalid;
  200. while (s < e)
  201. {
  202. m = s + ((e - s) >> 1);
  203. if (ch < m->start)
  204. e = m;
  205. else if (ch > m->end)
  206. s = m + 1;
  207. else
  208. return TLD_SUCCESS;
  209. }
  210. return TLD_INVALID;
  211. }
  212. /**
  213. * tld_check_4t:
  214. * @in: Array of unicode code points to process. Does not need to be
  215. * zero terminated.
  216. * @inlen: Number of unicode code points.
  217. * @errpos: Position of offending character is returned here.
  218. * @tld: A #Tld_table data structure representing the restrictions for
  219. * which the input should be tested.
  220. *
  221. * Test each of the code points in @in for whether or not
  222. * they are allowed by the data structure in @tld, return
  223. * the position of the first character for which this is not
  224. * the case in @errpos.
  225. *
  226. * Return value: Returns the #Tld_rc value %TLD_SUCCESS if all code
  227. * points are valid or when @tld is null, %TLD_INVALID if a
  228. * character is not allowed, or additional error codes on general
  229. * failure conditions.
  230. */
  231. int
  232. tld_check_4t (const uint32_t *in, size_t inlen, size_t *errpos,
  233. const Tld_table *tld)
  234. {
  235. const uint32_t *ipos;
  236. int rc;
  237. if (!tld) /* No data for TLD so everything is valid. */
  238. return TLD_SUCCESS;
  239. ipos = in;
  240. while (ipos < &in[inlen])
  241. {
  242. rc = _tld_checkchar (*ipos, tld);
  243. if (rc != TLD_SUCCESS)
  244. {
  245. if (errpos)
  246. *errpos = ipos - in;
  247. return rc;
  248. }
  249. ipos++;
  250. }
  251. return TLD_SUCCESS;
  252. }
  253. /**
  254. * tld_check_4tz:
  255. * @in: Zero terminated array of unicode code points to process.
  256. * @errpos: Position of offending character is returned here.
  257. * @tld: A #Tld_table data structure representing the restrictions for
  258. * which the input should be tested.
  259. *
  260. * Test each of the code points in @in for whether or not
  261. * they are allowed by the data structure in @tld, return
  262. * the position of the first character for which this is not
  263. * the case in @errpos.
  264. *
  265. * Return value: Returns the #Tld_rc value %TLD_SUCCESS if all code
  266. * points are valid or when @tld is null, %TLD_INVALID if a
  267. * character is not allowed, or additional error codes on general
  268. * failure conditions.
  269. */
  270. int
  271. tld_check_4tz (const uint32_t *in, size_t *errpos, const Tld_table *tld)
  272. {
  273. const uint32_t *ipos = in;
  274. if (!ipos)
  275. return TLD_NODATA;
  276. while (*ipos)
  277. ipos++;
  278. return tld_check_4t (in, ipos - in, errpos, tld);
  279. }
  280. /**
  281. * tld_check_4:
  282. * @in: Array of unicode code points to process. Does not need to be
  283. * zero terminated.
  284. * @inlen: Number of unicode code points.
  285. * @errpos: Position of offending character is returned here.
  286. * @overrides: A #Tld_table array of additional domain restriction
  287. * structures that complement and supersede the built-in information.
  288. *
  289. * Test each of the code points in @in for whether or not they are
  290. * allowed by the information in @overrides or by the built-in TLD
  291. * restriction data. When data for the same TLD is available both
  292. * internally and in @overrides, the information in @overrides takes
  293. * precedence. If several entries for a specific TLD are found, the
  294. * first one is used. If @overrides is %NULL, only the built-in
  295. * information is used. The position of the first offending character
  296. * is returned in @errpos.
  297. *
  298. * Return value: Returns the #Tld_rc value %TLD_SUCCESS if all code
  299. * points are valid or when @tld is null, %TLD_INVALID if a
  300. * character is not allowed, or additional error codes on general
  301. * failure conditions.
  302. */
  303. int
  304. tld_check_4 (const uint32_t *in, size_t inlen, size_t *errpos,
  305. const Tld_table **overrides)
  306. {
  307. const Tld_table *tld;
  308. char *domain;
  309. int rc;
  310. if (errpos)
  311. *errpos = 0;
  312. /* Get TLD name. */
  313. rc = tld_get_4 (in, inlen, &domain);
  314. if (rc != TLD_SUCCESS)
  315. {
  316. if (rc == TLD_NO_TLD) /* No TLD, say OK */
  317. return TLD_SUCCESS;
  318. else
  319. return rc;
  320. }
  321. /* Retrieve appropriate data structure. */
  322. tld = tld_default_table (domain, overrides);
  323. free (domain);
  324. return tld_check_4t (in, inlen, errpos, tld);
  325. }
  326. /**
  327. * tld_check_4z:
  328. * @in: Zero-terminated array of unicode code points to process.
  329. * @errpos: Position of offending character is returned here.
  330. * @overrides: A #Tld_table array of additional domain restriction
  331. * structures that complement and supersede the built-in information.
  332. *
  333. * Test each of the code points in @in for whether or not they are
  334. * allowed by the information in @overrides or by the built-in TLD
  335. * restriction data. When data for the same TLD is available both
  336. * internally and in @overrides, the information in @overrides takes
  337. * precedence. If several entries for a specific TLD are found, the
  338. * first one is used. If @overrides is %NULL, only the built-in
  339. * information is used. The position of the first offending character
  340. * is returned in @errpos.
  341. *
  342. * Return value: Returns the #Tld_rc value %TLD_SUCCESS if all code
  343. * points are valid or when @tld is null, %TLD_INVALID if a
  344. * character is not allowed, or additional error codes on general
  345. * failure conditions.
  346. */
  347. int
  348. tld_check_4z (const uint32_t *in, size_t *errpos, const Tld_table **overrides)
  349. {
  350. const uint32_t *ipos = in;
  351. if (!ipos)
  352. return TLD_NODATA;
  353. while (*ipos)
  354. ipos++;
  355. return tld_check_4 (in, ipos - in, errpos, overrides);
  356. }
  357. /**
  358. * tld_check_8z:
  359. * @in: Zero-terminated UTF8 string to process.
  360. * @errpos: Position of offending character is returned here.
  361. * @overrides: A #Tld_table array of additional domain restriction
  362. * structures that complement and supersede the built-in information.
  363. *
  364. * Test each of the characters in @in for whether or not they are
  365. * allowed by the information in @overrides or by the built-in TLD
  366. * restriction data. When data for the same TLD is available both
  367. * internally and in @overrides, the information in @overrides takes
  368. * precedence. If several entries for a specific TLD are found, the
  369. * first one is used. If @overrides is %NULL, only the built-in
  370. * information is used. The position of the first offending character
  371. * is returned in @errpos. Note that the error position refers to the
  372. * decoded character offset rather than the byte position in the
  373. * string.
  374. *
  375. * Return value: Returns the #Tld_rc value %TLD_SUCCESS if all
  376. * characters are valid or when @tld is null, %TLD_INVALID if a
  377. * character is not allowed, or additional error codes on general
  378. * failure conditions.
  379. */
  380. int
  381. tld_check_8z (const char *in, size_t *errpos, const Tld_table **overrides)
  382. {
  383. uint32_t *iucs;
  384. size_t ilen;
  385. int rc;
  386. if (!in)
  387. return TLD_NODATA;
  388. iucs = stringprep_utf8_to_ucs4 (in, -1, &ilen);
  389. if (!iucs)
  390. return TLD_MALLOC_ERROR;
  391. rc = tld_check_4 (iucs, ilen, errpos, overrides);
  392. free (iucs);
  393. return rc;
  394. }
  395. /**
  396. * tld_check_lz:
  397. * @in: Zero-terminated string in the current locales encoding to process.
  398. * @errpos: Position of offending character is returned here.
  399. * @overrides: A #Tld_table array of additional domain restriction
  400. * structures that complement and supersede the built-in information.
  401. *
  402. * Test each of the characters in @in for whether or not they are
  403. * allowed by the information in @overrides or by the built-in TLD
  404. * restriction data. When data for the same TLD is available both
  405. * internally and in @overrides, the information in @overrides takes
  406. * precedence. If several entries for a specific TLD are found, the
  407. * first one is used. If @overrides is %NULL, only the built-in
  408. * information is used. The position of the first offending character
  409. * is returned in @errpos. Note that the error position refers to the
  410. * decoded character offset rather than the byte position in the
  411. * string.
  412. *
  413. * Return value: Returns the #Tld_rc value %TLD_SUCCESS if all
  414. * characters are valid or when @tld is null, %TLD_INVALID if a
  415. * character is not allowed, or additional error codes on general
  416. * failure conditions.
  417. */
  418. int
  419. tld_check_lz (const char *in, size_t *errpos, const Tld_table **overrides)
  420. {
  421. char *utf8;
  422. int rc;
  423. if (!in)
  424. return TLD_NODATA;
  425. utf8 = stringprep_locale_to_utf8 (in);
  426. if (!utf8)
  427. return TLD_ICONV_ERROR;
  428. rc = tld_check_8z (utf8, errpos, overrides);
  429. free (utf8);
  430. return rc;
  431. }
  432. /**
  433. * Tld_rc:
  434. * @TLD_SUCCESS: Successful operation. This value is guaranteed to
  435. * always be zero, the remaining ones are only guaranteed to hold
  436. * non-zero values, for logical comparison purposes.
  437. * @TLD_INVALID: Invalid character found.
  438. * @TLD_NODATA: No input data was provided.
  439. * @TLD_MALLOC_ERROR: Error during memory allocation.
  440. * @TLD_ICONV_ERROR: Character encoding conversion error.
  441. * @TLD_NO_TLD: No top-level domain found in domain string.
  442. * @TLD_NOTLD: Same as @TLD_NO_TLD, for compatibility
  443. * with typo in earlier versions.
  444. *
  445. * Enumerated return codes of the TLD checking functions.
  446. * The value 0 is guaranteed to always correspond to success.
  447. */