| 1 |
/* |
| 2 |
* Copyright(c) 2014-2024 Tim Ruehsen |
| 3 |
* |
| 4 |
* Permission is hereby granted, free of charge, to any person obtaining a |
| 5 |
* copy of this software and associated documentation files (the "Software"), |
| 6 |
* to deal in the Software without restriction, including without limitation |
| 7 |
* the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 8 |
* and/or sell copies of the Software, and to permit persons to whom the |
| 9 |
* Software is furnished to do so, subject to the following conditions: |
| 10 |
* |
| 11 |
* The above copyright notice and this permission notice shall be included in |
| 12 |
* all copies or substantial portions of the Software. |
| 13 |
* |
| 14 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 19 |
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 20 |
* DEALINGS IN THE SOFTWARE. |
| 21 |
* |
| 22 |
* This file is part of libpsl. |
| 23 |
* |
| 24 |
* Using the libpsl functions via command line |
| 25 |
* |
| 26 |
* Changelog |
| 27 |
* 11.04.2014 Tim Ruehsen created |
| 28 |
* |
| 29 |
*/ |
| 30 |
|
| 31 |
#if HAVE_CONFIG_H |
| 32 |
# include <config.h> |
| 33 |
#endif |
| 34 |
|
| 35 |
#ifdef HAVE_UNISTD_H |
| 36 |
# include <unistd.h> |
| 37 |
#endif |
| 38 |
|
| 39 |
#ifdef _WIN32 |
| 40 |
# include <winsock2.h> // WSAStartup, WSACleanup |
| 41 |
|
| 42 |
// Windows does not have localtime_r but has localtime_s, which is more or less |
| 43 |
// the same except that the arguments are reversed |
| 44 |
# define LOCALTIME_R_SUCCESSFUL(t_sec,t_now) \ |
| 45 |
(localtime_s(t_now, t_sec) == 0) |
| 46 |
#else |
| 47 |
# define LOCALTIME_R_SUCCESSFUL(t_sec,t_now) \ |
| 48 |
(localtime_r(t_sec, t_now) != NULL) |
| 49 |
#endif |
| 50 |
|
| 51 |
#include <stdlib.h> |
| 52 |
#include <string.h> |
| 53 |
#include <ctype.h> |
| 54 |
#include <locale.h> |
| 55 |
|
| 56 |
#include <libpsl.h> |
| 57 |
|
| 58 |
static void usage(int err, FILE* f) |
| 59 |
{ |
| 60 |
fprintf(f, "Usage: psl [options] <domains...>\n"); |
| 61 |
fprintf(f, "\n"); |
| 62 |
fprintf(f, "Options:\n"); |
| 63 |
fprintf(f, " --version show library version information\n"); |
| 64 |
fprintf(f, " --use-latest-data use the latest PSL data available [default]\n"); |
| 65 |
fprintf(f, " --use-builtin-data use the builtin PSL data\n"); |
| 66 |
fprintf(f, " --no-star-rule do not apply the prevailing star rule\n"); |
| 67 |
fprintf(f, " (only applies to --is-public-suffix)\n"); |
| 68 |
fprintf(f, " --load-psl-file <filename> load PSL data from file\n"); |
| 69 |
fprintf(f, " --is-public-suffix check if domains are public suffixes [default]\n"); |
| 70 |
fprintf(f, " --is-cookie-domain-acceptable <cookie-domain>\n"); |
| 71 |
fprintf(f, " check if cookie-domain is acceptable for domains\n"); |
| 72 |
fprintf(f, " --print-unreg-domain print the longest public suffix part\n"); |
| 73 |
fprintf(f, " --print-reg-domain print the shortest private suffix part\n"); |
| 74 |
fprintf(f, " --print-info print info about library builtin data\n"); |
| 75 |
fprintf(f, " -b, --batch don't print leading domain\n"); |
| 76 |
fprintf(f, "\n"); |
| 77 |
|
| 78 |
exit(err); |
| 79 |
} |
| 80 |
|
| 81 |
static void init_windows(void) { |
| 82 |
#ifdef _WIN32 |
| 83 |
WSADATA wsa_data; |
| 84 |
int err; |
| 85 |
|
| 86 |
if ((err = WSAStartup(MAKEWORD(2,2), &wsa_data))) { |
| 87 |
printf("WSAStartup failed with error: %d\n", err); |
| 88 |
exit(EXIT_FAILURE); |
| 89 |
} |
| 90 |
|
| 91 |
atexit((void (__cdecl*)(void)) WSACleanup); |
| 92 |
#endif |
| 93 |
} |
| 94 |
|
| 95 |
/* RFC 2822-compliant date format */ |
| 96 |
static const char *time2str(time_t t) |
| 97 |
{ |
| 98 |
static char buf[64]; |
| 99 |
struct tm tm; |
| 100 |
|
| 101 |
if (LOCALTIME_R_SUCCESSFUL(&t, &tm)) |
| 102 |
strftime(buf, sizeof(buf), "%a, %d %b %Y %H:%M:%S %Z", &tm); |
| 103 |
else |
| 104 |
strcpy(buf, "--notime--"); |
| 105 |
return buf; |
| 106 |
} |
| 107 |
|
| 108 |
int main(int argc, const char *const *argv) |
| 109 |
{ |
| 110 |
int mode = 1, no_star_rule = 0, batch_mode = 0; |
| 111 |
const char *const *arg, *psl_file = NULL, *cookie_domain = NULL; |
| 112 |
psl_ctx_t *psl = (psl_ctx_t *) psl_latest(NULL); |
| 113 |
|
| 114 |
/* set current locale according to the environment variables */ |
| 115 |
setlocale(LC_ALL, ""); |
| 116 |
|
| 117 |
for (arg = argv + 1; arg < argv + argc; arg++) { |
| 118 |
if (**arg == '-') { |
| 119 |
if (!strcmp(*arg, "--is-public-suffix")) |
| 120 |
mode = 1; |
| 121 |
else if (!strcmp(*arg, "--print-unreg-domain")) |
| 122 |
mode = 2; |
| 123 |
else if (!strcmp(*arg, "--print-reg-domain")) |
| 124 |
mode = 3; |
| 125 |
else if (!strcmp(*arg, "--print-info")) |
| 126 |
mode = 99; |
| 127 |
else if (!strcmp(*arg, "--is-cookie-domain-acceptable") && arg < argv + argc - 1) { |
| 128 |
mode = 4; |
| 129 |
cookie_domain = *(++arg); |
| 130 |
} |
| 131 |
else if (!strcmp(*arg, "--use-latest-data")) { |
| 132 |
psl_free(psl); |
| 133 |
if (psl_file) { |
| 134 |
fprintf(stderr, "Dropped data from %s\n", psl_file); |
| 135 |
psl_file = NULL; |
| 136 |
} |
| 137 |
if (!(psl = (psl_ctx_t *) psl_latest(NULL))) |
| 138 |
printf("No PSL data available\n"); |
| 139 |
} |
| 140 |
else if (!strcmp(*arg, "--use-builtin-data")) { |
| 141 |
psl_free(psl); |
| 142 |
if (psl_file) { |
| 143 |
fprintf(stderr, "Dropped data from %s\n", psl_file); |
| 144 |
psl_file = NULL; |
| 145 |
} |
| 146 |
if (!(psl = (psl_ctx_t *) psl_builtin())) |
| 147 |
printf("No builtin PSL data available\n"); |
| 148 |
} |
| 149 |
else if (!strcmp(*arg, "--no-star-rule")) { |
| 150 |
no_star_rule = 1; |
| 151 |
} |
| 152 |
else if (!strcmp(*arg, "--load-psl-file") && arg < argv + argc - 1) { |
| 153 |
psl_free(psl); |
| 154 |
if (psl_file) { |
| 155 |
fprintf(stderr, "Dropped data from %s\n", psl_file); |
| 156 |
psl_file = NULL; |
| 157 |
} |
| 158 |
if (!(psl = psl_load_file(psl_file = *(++arg)))) { |
| 159 |
fprintf(stderr, "Failed to load PSL data from %s\n\n", psl_file); |
| 160 |
psl_file = NULL; |
| 161 |
} |
| 162 |
} |
| 163 |
else if (!strcmp(*arg, "--batch") || !strcmp(*arg, "-b")) { |
| 164 |
batch_mode = 1; |
| 165 |
} |
| 166 |
else if (!strcmp(*arg, "--help")) { |
| 167 |
fprintf(stdout, "`psl' explores the Public Suffix List\n\n"); |
| 168 |
usage(0, stdout); |
| 169 |
} |
| 170 |
else if (!strcmp(*arg, "--version")) { |
| 171 |
printf("psl %s (0x%06x)\n", PACKAGE_VERSION, psl_check_version_number(0)); |
| 172 |
printf("libpsl %s\n", psl_get_version()); |
| 173 |
printf("\n"); |
| 174 |
printf("Copyright (C) 2014-2024 Tim Ruehsen\n"); |
| 175 |
printf("License: MIT\n"); |
| 176 |
exit(0); |
| 177 |
} |
| 178 |
else if (!strcmp(*arg, "--")) { |
| 179 |
arg++; |
| 180 |
break; |
| 181 |
} |
| 182 |
else { |
| 183 |
fprintf(stderr, "Unknown option '%s'\n", *arg); |
| 184 |
usage(1, stderr); |
| 185 |
} |
| 186 |
} else |
| 187 |
break; |
| 188 |
} |
| 189 |
|
| 190 |
if (mode != 99) { |
| 191 |
if (mode != 1 && no_star_rule) { |
| 192 |
fprintf(stderr, "--no-star-rule only combines with --is-public-suffix\n"); |
| 193 |
usage(1, stderr); |
| 194 |
} |
| 195 |
if (!psl) { |
| 196 |
fprintf(stderr, "No PSL data available - aborting\n"); |
| 197 |
exit(2); |
| 198 |
} |
| 199 |
if (arg >= argv + argc) { |
| 200 |
char buf[256], *domain, *lower; |
| 201 |
size_t len; |
| 202 |
psl_error_t rc; |
| 203 |
|
| 204 |
/* read URLs from STDIN */ |
| 205 |
while (fgets(buf, sizeof(buf), stdin)) { |
| 206 |
for (domain = buf; isspace(*domain); domain++); /* skip leading spaces */ |
| 207 |
if (*domain == '#' || !*domain) continue; /* skip empty lines and comments */ |
| 208 |
for (len = strlen(domain); len && isspace(domain[len - 1]); len--); /* skip trailing spaces */ |
| 209 |
domain[len] = 0; |
| 210 |
|
| 211 |
if ((rc = psl_str_to_utf8lower(domain, NULL, NULL, &lower)) != PSL_SUCCESS) { |
| 212 |
fprintf(stderr, "%s: Failed to convert to lowercase UTF-8 (%d)\n", domain, rc); |
| 213 |
continue; |
| 214 |
} |
| 215 |
|
| 216 |
if (!batch_mode && mode != 4) |
| 217 |
printf("%s: ", domain); |
| 218 |
|
| 219 |
if (mode == 1) { |
| 220 |
if (no_star_rule) |
| 221 |
printf("%d", psl_is_public_suffix2(psl, lower, PSL_TYPE_ANY|PSL_TYPE_NO_STAR_RULE)); |
| 222 |
else |
| 223 |
printf("%d", psl_is_public_suffix(psl, lower)); |
| 224 |
|
| 225 |
if (!batch_mode) |
| 226 |
printf(" (%s)\n", lower); |
| 227 |
else |
| 228 |
putchar('\n'); |
| 229 |
} |
| 230 |
else if (mode == 2) { |
| 231 |
const char *dom = psl_unregistrable_domain(psl, lower); |
| 232 |
printf("%s\n", dom ? dom : "(null)"); |
| 233 |
} |
| 234 |
else if (mode == 3) { |
| 235 |
const char *dom = psl_registrable_domain(psl, lower); |
| 236 |
printf("%s\n", dom ? dom : "(null)"); |
| 237 |
} |
| 238 |
else if (mode == 4) { |
| 239 |
char *cookie_domain_lower; |
| 240 |
|
| 241 |
init_windows(); |
| 242 |
|
| 243 |
if ((rc = psl_str_to_utf8lower(domain, NULL, NULL, &cookie_domain_lower)) == PSL_SUCCESS) { |
| 244 |
if (!batch_mode) |
| 245 |
printf("%s: ", domain); |
| 246 |
printf("%d\n", psl_is_cookie_domain_acceptable(psl, lower, cookie_domain)); |
| 247 |
free(cookie_domain_lower); |
| 248 |
} else |
| 249 |
fprintf(stderr, "%s: Failed to convert cookie domain '%s' to lowercase UTF-8 (%d)\n", domain, cookie_domain, rc); |
| 250 |
} |
| 251 |
|
| 252 |
psl_free_string(lower); |
| 253 |
} |
| 254 |
|
| 255 |
psl_free(psl); |
| 256 |
exit(0); |
| 257 |
} |
| 258 |
} |
| 259 |
|
| 260 |
if (mode == 1) { |
| 261 |
for (; arg < argv + argc; arg++) { |
| 262 |
if (!batch_mode) |
| 263 |
printf("%s: ", *arg); |
| 264 |
if (no_star_rule) |
| 265 |
printf("%d\n", psl_is_public_suffix2(psl, *arg, PSL_TYPE_ANY|PSL_TYPE_NO_STAR_RULE)); |
| 266 |
else |
| 267 |
printf("%d\n", psl_is_public_suffix(psl, *arg)); |
| 268 |
} |
| 269 |
} |
| 270 |
else if (mode == 2) { |
| 271 |
for (; arg < argv + argc; arg++) { |
| 272 |
const char *dom = psl_unregistrable_domain(psl, *arg); |
| 273 |
if (!batch_mode) |
| 274 |
printf("%s: ", *arg); |
| 275 |
printf("%s\n", dom ? dom : "(null)"); |
| 276 |
} |
| 277 |
} |
| 278 |
else if (mode == 3) { |
| 279 |
for (; arg < argv + argc; arg++) { |
| 280 |
const char *dom = psl_registrable_domain(psl, *arg); |
| 281 |
if (!batch_mode) |
| 282 |
printf("%s: ", *arg); |
| 283 |
printf("%s\n", dom ? dom : "(null)"); |
| 284 |
} |
| 285 |
} |
| 286 |
else if (mode == 4) { |
| 287 |
init_windows(); |
| 288 |
|
| 289 |
for (; arg < argv + argc; arg++) { |
| 290 |
if (!batch_mode) |
| 291 |
printf("%s: ", *arg); |
| 292 |
printf("%d\n", psl_is_cookie_domain_acceptable(psl, *arg, cookie_domain)); |
| 293 |
} |
| 294 |
} |
| 295 |
else if (mode == 99) { |
| 296 |
printf("dist filename: %s\n", psl_dist_filename()); |
| 297 |
|
| 298 |
if (psl && psl != psl_builtin()) { |
| 299 |
static char not_avail[] = "- information not available -"; |
| 300 |
int n; |
| 301 |
|
| 302 |
if ((n = psl_suffix_count(psl)) >= 0) |
| 303 |
printf("suffixes: %d\n", n); |
| 304 |
else |
| 305 |
printf("suffixes: %s\n", not_avail); |
| 306 |
|
| 307 |
if ((n = psl_suffix_exception_count(psl)) >= 0) |
| 308 |
printf("exceptions: %d\n", n); |
| 309 |
else |
| 310 |
printf("exceptions: %s\n", not_avail); |
| 311 |
|
| 312 |
if ((n = psl_suffix_wildcard_count(psl)) >= 0) |
| 313 |
printf("wildcards: %d\n", n); |
| 314 |
else |
| 315 |
printf("wildcards: %s\n", not_avail); |
| 316 |
} |
| 317 |
|
| 318 |
psl_free(psl); |
| 319 |
psl = (psl_ctx_t *) psl_builtin(); |
| 320 |
|
| 321 |
if (psl) { |
| 322 |
printf("builtin suffixes: %d\n", psl_suffix_count(psl)); |
| 323 |
printf("builtin exceptions: %d\n", psl_suffix_exception_count(psl)); |
| 324 |
printf("builtin wildcards: %d\n", psl_suffix_wildcard_count(psl)); |
| 325 |
printf("builtin filename: %s\n", psl_builtin_filename()); |
| 326 |
printf("builtin file time: %ld (%s)\n", (long) psl_builtin_file_time(), time2str(psl_builtin_file_time())); |
| 327 |
printf("builtin SHA1 file hash: %s\n", psl_builtin_sha1sum()); |
| 328 |
printf("builtin outdated: %d\n", psl_builtin_outdated()); |
| 329 |
} else |
| 330 |
printf("No builtin PSL data available\n"); |
| 331 |
} |
| 332 |
|
| 333 |
psl_free(psl); |
| 334 |
|
| 335 |
return 0; |
| 336 |
} |