From: Chuck Lever <chuck.lever@oracle.com>
To: linux-nfs@vger.kernel.org
Cc: chris.mason@oracle.com
Subject: [PATCH 23/26] statd: Support IPv6 in sm_mon_1_svc()
Date: Tue, 13 Oct 2009 10:58:11 -0400 [thread overview]
Message-ID: <20091013145811.2424.8971.stgit@matisse.1015granger.net> (raw)
In-Reply-To: <20091013142257.2424.76946.stgit-RytpoXr2tKZ9HhUboXbp9zCvJB+x5qRC@public.gmane.org>
Replace deprecated gethostbyname(3) and gethostbyaddr(3) calls.
Handle IPv6 remotes and IDN labels properly. At least with fairly
recent glibc's we are guaranteed that the returned hostnames will
contain only ASCII characters, allowing DNS labels to continue to be
used as file names as IDNs become commonplace.
Also address a couple of memory leaks.
Signed-off-by: Chuck Lever <chuck.lever@oracle.com>
---
utils/statd/hostname.c | 30 ++++++++++++++++++++++++++++++
utils/statd/monitor.c | 31 ++++++++++++++++++-------------
utils/statd/statd.h | 2 ++
3 files changed, 50 insertions(+), 13 deletions(-)
diff --git a/utils/statd/hostname.c b/utils/statd/hostname.c
index 0148b9d..7d9ed45 100644
--- a/utils/statd/hostname.c
+++ b/utils/statd/hostname.c
@@ -149,6 +149,36 @@ get_addrinfo(const char *hostname, const struct addrinfo* gai_hint)
return NULL;
}
+/**
+ * nsm_forward_lookup - hostname or presentation address to sockaddr list
+ * @hostname: C string containing hostname or presentation address
+ *
+ * Returns addrinfo list, including the host's canonical DNS name,
+ * or NULL if an error occurs. Caller must free addrinfo list via
+ * freeaddrinfo(3).
+ *
+ * AI_ADDRCONFIG should prevent us from monitoring a host that we can't
+ * reach. If IPv6 is not enabled on this system, then we don't want to
+ * monitor remote hosts that have only IPv6 addresses.
+ */
+struct addrinfo *
+nsm_forward_lookup(const char *hostname)
+{
+ static const struct addrinfo gai_hint = {
+#ifdef IPV6_SUPPORTED
+ .ai_family = AF_UNSPEC,
+ .ai_flags = AI_CANONNAME | AI_ADDRCONFIG |
+ AI_IDN | AI_IDN_USE_STD3_ASCII_RULES,
+#else /* !IPV6_SUPPORTED */
+ .ai_family = AF_INET,
+ .ai_flags = AI_CANONNAME |
+ AI_IDN | AI_IDN_USE_STD3_ASCII_RULES,
+#endif /* !IPV6_SUPPORTED */
+ };
+
+ return get_addrinfo(hostname, &gai_hint);
+}
+
#ifdef IPV6_SUPPORTED
static int
compare_sockaddrs(const struct sockaddr *sa1, const struct sockaddr *sa2)
diff --git a/utils/statd/monitor.c b/utils/statd/monitor.c
index 3db7ce8..df3206a 100644
--- a/utils/statd/monitor.c
+++ b/utils/statd/monitor.c
@@ -72,8 +72,8 @@ sm_mon_1_svc(struct mon *argp, struct svc_req *rqstp)
.sin_family = AF_INET,
.sin_addr.s_addr = htonl(INADDR_LOOPBACK),
};
- char *dnsname;
- struct hostent *hostinfo = NULL;
+ struct addrinfo *gai_results;
+ char *dnsname = NULL;
xlog(D_CALL, "Received SM_MON for %s from %s", mon_name, my_name);
@@ -115,9 +115,6 @@ sm_mon_1_svc(struct mon *argp, struct svc_req *rqstp)
"or starting '.': %s", mon_name);
xlog(L_ERROR, "POSSIBLE SPOOF/ATTACK ATTEMPT!");
goto failure;
- } else if ((hostinfo = gethostbyname(mon_name)) == NULL) {
- xlog_warn("gethostbyname error for %s", mon_name);
- goto failure;
}
/* my_name must not have white space */
@@ -130,15 +127,21 @@ sm_mon_1_svc(struct mon *argp, struct svc_req *rqstp)
* Now choose a hostname to use for matching. We cannot
* really trust much in the incoming NOTIFY, so to make
* sure that multi-homed hosts work nicely, we get an
- * FQDN now, and use that for matching
+ * FQDN now, and use that for matching. If the kernel
+ * handed us an IP presentation address, getaddrinfo(3)
+ * copies that into ai_canonname.
*/
- hostinfo = gethostbyaddr(hostinfo->h_addr,
- hostinfo->h_length,
- hostinfo->h_addrtype);
- if (hostinfo)
- dnsname = xstrdup(hostinfo->h_name);
- else
- dnsname = xstrdup(my_name);
+ gai_results = nsm_forward_lookup(mon_name);
+ if (gai_results == NULL) {
+ xlog(L_WARNING, "No canonical hostname found for %s", mon_name);
+ goto failure;
+ }
+ dnsname = strdup(gai_results->ai_canonname);
+ freeaddrinfo(gai_results);
+ if (dnsname == NULL) {
+ xlog(L_ERROR, "Failed to allocate memory");
+ goto failure;
+ }
/* Now check to see if this is a duplicate, and warn if so.
* I will also return STAT_FAIL. (I *think* this is how I should
@@ -164,6 +167,7 @@ sm_mon_1_svc(struct mon *argp, struct svc_req *rqstp)
mon_name, my_name);
/* But we'll let you pass anyway. */
+ free(dnsname);
goto success;
}
clnt = NL_NEXT(clnt);
@@ -174,6 +178,7 @@ sm_mon_1_svc(struct mon *argp, struct svc_req *rqstp)
* doesn't fail. (I should probably fix this assumption.)
*/
if (!(clnt = nlist_new(my_name, mon_name, 0))) {
+ free(dnsname);
xlog_warn("out of memory");
goto failure;
}
diff --git a/utils/statd/statd.h b/utils/statd/statd.h
index 71a77e2..5ee3ea6 100644
--- a/utils/statd/statd.h
+++ b/utils/statd/statd.h
@@ -25,6 +25,8 @@
extern int nsm_matchhostname(const char *hostname1, const char *hostname2);
extern int nsm_present_address(const struct sockaddr *sap, socklen_t salen,
char *buf, const size_t buflen);
+extern struct addrinfo *
+ nsm_forward_lookup(const char *hostname);
extern void my_svc_run(void);
extern void notify_hosts(void);
extern void shuffle_dirs(void);
next prev parent reply other threads:[~2009-10-13 14:58 UTC|newest]
Thread overview: 34+ messages / expand[flat|nested] mbox.gz Atom feed top
2009-10-13 14:54 [PATCH 00/26] Basic IPv6 support in statd (take++) Chuck Lever
[not found] ` <20091013142257.2424.76946.stgit-RytpoXr2tKZ9HhUboXbp9zCvJB+x5qRC@public.gmane.org>
2009-10-13 14:54 ` [PATCH 01/26] statd: Replace note() with xlog() in rpc.statd Chuck Lever
[not found] ` <20091013145416.2424.12787.stgit-RytpoXr2tKZ9HhUboXbp9zCvJB+x5qRC@public.gmane.org>
2009-10-14 16:57 ` J. Bruce Fields
2009-10-15 14:58 ` Chuck Lever
2009-10-13 14:54 ` [PATCH 02/26] statd: Replace nsm_log() with xlog() in sm-notify command Chuck Lever
2009-10-13 14:54 ` [PATCH 03/26] statd: replace smn_{get, set}_port() with the shared equivalents Chuck Lever
2009-10-13 14:54 ` [PATCH 04/26] statd: fix address copy in sm-notify.c Chuck Lever
2009-10-13 14:54 ` [PATCH 05/26] statd: Move the sm_inter XDR pieces to libnsm.a Chuck Lever
2009-10-13 14:55 ` [PATCH 06/26] statd: Introduce common routines to handle persistent storage Chuck Lever
[not found] ` <20091013145506.2424.10505.stgit-RytpoXr2tKZ9HhUboXbp9zCvJB+x5qRC@public.gmane.org>
2009-10-16 14:05 ` Jeff Layton
[not found] ` <20091016100544.25f686c4-9yPaYZwiELC+kQycOl6kW4xkIHaj4LzF@public.gmane.org>
2009-10-16 22:46 ` Chuck Lever
2009-10-16 23:27 ` Jeff Layton
2009-10-13 14:55 ` [PATCH 07/26] statd: Use the new nsm_ file.c calls in sm_notify Chuck Lever
2009-10-13 14:55 ` [PATCH 08/26] statd: Use the new nsm_ file.c calls in rpc.statd Chuck Lever
2009-10-13 14:55 ` [PATCH 09/26] libnsm: Add RPC construction helper functions Chuck Lever
[not found] ` <20091013145546.2424.83816.stgit-RytpoXr2tKZ9HhUboXbp9zCvJB+x5qRC@public.gmane.org>
2009-10-14 17:21 ` J. Bruce Fields
2009-10-15 15:21 ` Chuck Lever
2009-10-13 14:55 ` [PATCH 10/26] statd: Support sending SM_NOTIFY requests to IPv6 remotes Chuck Lever
2009-10-13 14:56 ` [PATCH 11/26] statd: Update rmtcall.c Chuck Lever
2009-10-13 14:56 ` [PATCH 12/26] statd: factor socket creation out of notify() Chuck Lever
2009-10-13 14:56 ` [PATCH 13/26] statd: Support creating a PF_INET6 socket in smn_create_socket() Chuck Lever
2009-10-13 14:56 ` [PATCH 14/26] statd: IPv6 support in reserved port binding " Chuck Lever
2009-10-13 14:56 ` [PATCH 15/26] statd: Use getaddrinfo(3) to generate bind address " Chuck Lever
2009-10-13 14:56 ` [PATCH 16/26] statd: Support IPv6 DNS lookups in smn_lookup Chuck Lever
2009-10-13 14:57 ` [PATCH 17/26] statd: squelch compiler warning in sm-notify.c Chuck Lever
2009-10-13 14:57 ` [PATCH 18/26] statd: Introduce statd version of matchhostname() Chuck Lever
2009-10-13 14:57 ` [PATCH 19/26] libnsm.a: add nsm_present_address() API Chuck Lever
2009-10-13 14:57 ` [PATCH 20/26] statd: add IPv6 support in sm_notify_1_svc() Chuck Lever
2009-10-13 14:57 ` [PATCH 21/26] statd: Support IPv6 is caller_is_localhost() Chuck Lever
2009-10-13 14:58 ` [PATCH 22/26] statd: Support IPv6 in sm_simu_crash_1_svc Chuck Lever
2009-10-13 14:58 ` Chuck Lever [this message]
2009-10-13 14:58 ` [PATCH 24/26] statd: Support IPv6 in sm_stat_1_svc() Chuck Lever
2009-10-13 14:58 ` [PATCH 25/26] statd: retain CAP_NET_BIND when dropping privileges Chuck Lever
2009-10-13 14:58 ` [PATCH 26/26] statd: Support TI-RPC statd listener Chuck Lever
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20091013145811.2424.8971.stgit@matisse.1015granger.net \
--to=chuck.lever@oracle.com \
--cc=chris.mason@oracle.com \
--cc=linux-nfs@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox