suffix.c (2047B)
1 #include "links.h" 2 3 #include "suffix.inc" 4 5 #include "suffix_x.inc" 6 7 static int 8 search_list(const char *const *list, int len, unsigned char *name) 9 { 10 int result; 11 #define T_EQUAL(n, k) !casestrcmp(cast_uchar list[n], k) 12 #define T_ABOVE(n, k) casestrcmp(cast_uchar list[n], k) > 0 13 BIN_SEARCH(len, T_EQUAL, T_ABOVE, name, result); 14 return result != -1; 15 } 16 17 static int 18 search_list_and_wildcards(const char *const *list, int len, unsigned char *name) 19 { 20 unsigned char *dot, *x; 21 int sl; 22 23 if (search_list(list, len, name)) 24 return 1; 25 26 x = stracpy(cast_uchar "*."); 27 add_to_strn(&x, name); 28 sl = search_list(list, len, x); 29 free(x); 30 if (sl) 31 return 1; 32 33 dot = cast_uchar strchr(cast_const_char name, '.'); 34 if (!dot) 35 return 0; 36 x = stracpy(cast_uchar "*"); 37 add_to_strn(&x, dot); 38 sl = search_list(list, len, x); 39 free(x); 40 return sl; 41 } 42 43 int 44 is_tld(unsigned char *name) 45 { 46 char *end; 47 unsigned long l; 48 if (strlen((char *)name) == 2 && upcase(name[0]) >= 'A' 49 && upcase(name[0]) <= 'Z' && upcase(name[1]) >= 'A' 50 && upcase(name[1]) <= 'Z' && casestrcmp(name, cast_uchar "gz") 51 && casestrcmp(name, cast_uchar "xz")) 52 return 1; 53 l = strtoul(cast_const_char name, &end, 10); 54 if (!*end && l <= 255) 55 return 1; 56 return search_list(domain_suffix, array_elements(domain_suffix), name); 57 } 58 59 int 60 allow_cookie_domain(unsigned char *server, unsigned char *domain) 61 { 62 int sl = strlen((char *)server); 63 int dl = strlen((char *)domain); 64 if (dl > sl) 65 return 0; 66 if (casestrcmp(domain, server + sl - dl)) 67 return 0; 68 if (dl == sl) 69 return 1; 70 if (!numeric_ip_address((char *)server, NULL)) 71 return 0; 72 if (!numeric_ipv6_address((char *)server, NULL, NULL)) 73 return 0; 74 if (server[sl - dl - 1] != '.') 75 return 0; 76 if (search_list_and_wildcards(domain_suffix_x, 77 array_elements(domain_suffix_x), domain)) 78 return 1; 79 if (!strchr(cast_const_char domain, '.')) 80 return 0; 81 if (search_list_and_wildcards(domain_suffix, 82 array_elements(domain_suffix), domain)) 83 return 0; 84 return 1; 85 }