From 59ea885da07c0916fb5917efb5538df2ecec1553 Mon Sep 17 00:00:00 2001 From: Tatsuya Kinoshita Date: Fri, 23 Apr 2021 20:16:50 +0900 Subject: Treat 127.0.0.1, [::1], and hostname as localhost --- etc.c | 10 ++++++++++ fm.h | 6 ++++++ local.c | 5 +---- main.c | 12 ++++++++++++ proto.h | 1 + url.c | 42 ++++++++---------------------------------- 6 files changed, 38 insertions(+), 38 deletions(-) diff --git a/etc.c b/etc.c index 7cdd220..aeb65ec 100644 --- a/etc.c +++ b/etc.c @@ -1573,6 +1573,16 @@ expandName(char *name) } #endif +int +is_localhost(const char *host) +{ + if (!host || + !strcasecmp(host, "localhost") || !strcmp(host, "127.0.0.1") || + (HostName && !strcasecmp(host, HostName)) || !strcmp(host, "[::1]")) + return TRUE; + return FALSE; +} + char * file_to_url(char *file) { diff --git a/fm.h b/fm.h index 594b457..b6d91fb 100644 --- a/fm.h +++ b/fm.h @@ -19,6 +19,7 @@ #include #include #include +#include #include #include #include @@ -135,6 +136,10 @@ void bzero(void *, int); #define DICTBUFFERNAME "*dictionary*" #endif /* USE_DICT */ +#ifndef HOST_NAME_MAX +#define HOST_NAME_MAX 255 +#endif + /* * Line Property */ @@ -837,6 +842,7 @@ global char PreserveTimestamp init(TRUE); global char ArgvIsURL init(TRUE); global char MetaRefresh init(FALSE); global char LocalhostOnly init(FALSE); +global char* HostName init(NULL); global char fmInitialized init(FALSE); global char QuietMessage init(FALSE); diff --git a/local.c b/local.c index 56d589d..8cae710 100644 --- a/local.c +++ b/local.c @@ -51,13 +51,10 @@ writeLocalCookie() Str localCookie() { - char hostname[256]; - if (Local_cookie) return Local_cookie; - gethostname(hostname, 256); srand48((long)New(char) + (long)time(NULL)); - Local_cookie = Sprintf("%ld@%s", lrand48(), hostname); + Local_cookie = Sprintf("%ld@%s", lrand48(), HostName ? HostName : "localhost"); return Local_cookie; } diff --git a/main.c b/main.c index fa41cce..6f15e36 100644 --- a/main.c +++ b/main.c @@ -455,6 +455,18 @@ main(int argc, char **argv, char **envp) BookmarkFile = NULL; config_file = NULL; + { + char hostname[HOST_NAME_MAX + 2]; + if (gethostname(hostname, HOST_NAME_MAX + 2) == 0) { + size_t hostname_len; + /* Don't use hostname if it is truncated. */ + hostname[HOST_NAME_MAX + 1] = '\0'; + hostname_len = strlen(hostname); + if (hostname_len <= HOST_NAME_MAX && hostname_len <= INT_MAX) + HostName = allocStr(hostname, (int)hostname_len); + } + } + /* argument search 1 */ for (i = 1; i < argc; i++) { if (*argv[i] == '-') { diff --git a/proto.h b/proto.h index 42aa4f8..66d497e 100644 --- a/proto.h +++ b/proto.h @@ -672,6 +672,7 @@ extern void myExec(char *command); extern void mySystem(char *command, int background); extern Str myExtCommand(char *cmd, char *arg, int redirect); extern Str myEditor(char *cmd, char *file, int line); +extern int is_localhost(const char *host); extern char *file_to_url(char *file); #ifdef USE_M17N extern char *url_unquote_conv(char *url, wc_ces charset); diff --git a/url.c b/url.c index 0c0b709..1fbda17 100644 --- a/url.c +++ b/url.c @@ -41,10 +41,6 @@ #define close(fd) closesocket(fd) #endif -#ifndef HOST_NAME_MAX -#define HOST_NAME_MAX 64 -#endif - #ifdef INET6 /* see rc.c, "dns_order" and dnsorders[] */ int ai_family_order_table[7][3] = { @@ -781,34 +777,13 @@ parseURL(char *url, ParsedURL *p_url, ParsedURL *current) copyParsedURL(p_url, current); goto do_label; } - if (!strncasecmp(url, "file://", 7)) { #if defined( __EMX__ ) || defined( __CYGWIN__ ) - if (!strncasecmp(url + 7, "localhost/", 10)) { - p_url->scheme = SCM_LOCAL; - p += 7 + 10 - 1; - url += 7 + 10 - 1; - } else -#endif - { - /* Recognize the machine's host name. This is necessary for URLs - * produced by 'ls --hyperlink' or similar. */ - char hostname[HOST_NAME_MAX + 2]; - if (gethostname (hostname, HOST_NAME_MAX + 2) == 0) { - size_t hostname_len; - /* Don't use hostname if it is truncated. */ - hostname[HOST_NAME_MAX + 1] = '\0'; - hostname_len = strlen (hostname); - if (hostname_len <= HOST_NAME_MAX) { - if (!strncasecmp(url + 7, hostname, hostname_len) - && *(url + 7 + hostname_len) == '/') { - p_url->scheme = SCM_LOCAL; - p += 7 + hostname_len; - url += 7 + hostname_len; - } - } - } - } + if (!strncasecmp(url, "file://localhost/", 17)) { + p_url->scheme = SCM_LOCAL; + p += 17 - 1; + url += 17 - 1; } +#endif #ifdef SUPPORT_DOS_DRIVE_PREFIX if (IS_ALPHA(*p) && (p[1] == ':' || p[1] == '|')) { p_url->scheme = SCM_LOCAL; @@ -951,7 +926,7 @@ parseURL(char *url, ParsedURL *p_url, ParsedURL *current) #ifndef SUPPORT_NETBIOS_SHARE if (p_url->scheme == SCM_LOCAL && p_url->user == NULL && p_url->host != NULL && *p_url->host != '\0' && - strcmp(p_url->host, "localhost")) { + !is_localhost(p_url->host)) { /* * In the environments other than CYGWIN, a URL like * file://host/file is regarded as ftp://host/file. @@ -1262,7 +1237,7 @@ parseURL2(char *url, ParsedURL *pu, ParsedURL *current) } if (pu->scheme == SCM_LOCAL) { #ifdef SUPPORT_NETBIOS_SHARE - if (pu->host && strcmp(pu->host, "localhost") != 0) { + if (pu->host && !is_localhost(pu->host)) { Str tmp = Strnew_charp("//"); Strcat_m_charp(tmp, pu->host, cleanupName(file_unquote(pu->file)), NULL); @@ -1696,8 +1671,7 @@ openURL(char *url, ParsedURL *pu, ParsedURL *current, } } - if (LocalhostOnly && pu->host && - strcasecmp(pu->host, "localhost") && strcasecmp(pu->host, "127.0.0.1")) + if (LocalhostOnly && pu->host && !is_localhost(pu->host)) pu->host = NULL; uf.scheme = pu->scheme; -- cgit v1.2.3