git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH 1/2] drop length limitations on gecos-derived names and emails

From
Jeff King <peff@peff.net>
Date
May 14, 2012, 17:05 UTC
Message-ID
<20120514170533.GA29909@sigill.intra.peff.net>
In-Reply-To
<20120514162824.GA24457@sigill.intra.peff.net>
On Mon, May 14, 2012 at 12:28:24PM -0400, Jeff King wrote:
Show 13 quoted lines
> I noticed in add_domainname that we look up the host via gethostname,
> and then if it is not fully qualified, call gethostbyname and steal the
> domain portion of the result, tacking it onto the hostname we got.
> 
> That seems oddly complex to me, and like it could result in a bogus
> hostname if the unqualified name does not match the first part of the
> returned qualified name. E.g., if the /etc/hosts file contains something
> like:
> 
>   192.168.1.1 foo.example.com bar.example.com bar
> 
> (and your hostname is "bar"). I doubt it matters much in practice, and
> it is outside the scope of this patch, so I left it for now.

It looks like a bug in adc3dbc (Use sensible domain name (the DNS one) when guessing ident information, 2005-10-21). Before that we used getdomainname, where that procedure made more sense.

The patch below fixes it. I doubt it matters much in practice, but I think the resulting code is way less confusing to read.

-- >8 --
Subject: [PATCH] ident: use full dns names to generate email addresses

When we construct an email address from the username and hostname, we generate the host part of the email with this procedure:

  1. add the result of gethostname
  2. if it has a dot, ok, it's fully qualified
  3. if not, then look up the unqualified hostname via
     gethostbyname; take the domain name of the result and
     append it to the hostname

Step 3 can actually produce a bogus result, as the name returned by gethostbyname may not be related to the hostname we fed it (e.g., consider a machine "foo" with names "foo.one.example.com" and "bar.two.example.com"; we may have the latter returned and generate the bogus name "foo.two.example.com").

This patch simply uses the full hostname returned by gethostbyname. In the common case that the first part is the same as the unqualified hostname, the behavior is identical. And in the case that it is not the same, we are much more likely to be generating a valid name.

Signed-off-by: Jeff King <peff@peff.net>
---
 ident.c | 13 ++++---------
 1 file changed, 4 insertions(+), 9 deletions(-)
diff --git a/ident.c b/ident.c
index 72944ba..e552e7f 100644
--- a/ident.c
+++ b/ident.c
@@ -62,23 +62,18 @@ static void add_domainname(struct strbuf *out)
 {
 	char buf[1024];
 	struct hostent *he;
-	const char *domainname;
 
 	if (gethostname(buf, sizeof(buf))) {
 		warning("cannot get host name: %s", strerror(errno));
 		strbuf_addstr(out, "(none)");
 		return;
 	}
-	strbuf_addstr(out, buf);
 	if (strchr(buf, '.'))
-		return;
-
-	he = gethostbyname(buf);
-	strbuf_addch(out, '.');
-	if (he && (domainname = strchr(he->h_name, '.')))
-		strbuf_addstr(out, domainname + 1);
+		strbuf_addstr(out, buf);
+	else if ((he = gethostbyname(buf)) && strchr(he->h_name, '.'))
+		strbuf_addstr(out, he->h_name);
 	else
-		strbuf_addstr(out, "(none)");
+		strbuf_addf(out, "%s.(none)", buf);
 }
 
 static void copy_email(const struct passwd *pw, struct strbuf *email)
-- 
1.7.10.2.8.g1101eed
Previous: Jeff KingNext: Jeff King
Message 9 of 23 in “Change error messages in ident.c Make error messages caused by failed reads of the /etc/passwd file easier to understand. Signed-off-by: Angus Hammond <angusgh@gmail.com>”
  1. 1/2 Change error messages in ident.c Make error messages caused by failed reads of the /etc/passwd file easier to understand. Signed-off-by: Angus Hammond <angusgh@gmail.com>Angus Hammond, May 10, 2012
  2. 2/2 Remove diagnostics section from commit-tree and var man pages New error messages shouldn't need explaining like the old ones did so just delete the diagnostics section of the man pages. Signed-off-by: Angus Hammond <angusgh@gmail.com>Angus Hammond, May 10, 2012
  3. Angus HammondMay 10, 2012
  4. Jeff KingMay 10, 2012
  5. Jeff KingMay 10, 2012
  6. Junio C HamanoMay 11, 2012
  7. Jeff KingMay 11, 2012
  8. 1/2 drop length limitations on gecos-derived names and emailsJeff King, May 14, 2012
  9. Jeff KingMay 14, 2012
  10. Jeff KingMay 14, 2012
  11. Jeff KingMay 14, 2012
  12. Jeff KingMay 15, 2012
  13. Jeff KingMay 15, 2012
  14. Junio C HamanoMay 15, 2012
  15. Jeff KingMay 15, 2012
  16. Junio C HamanoMay 15, 2012
  17. 2/2 ident: report passwd errors with a more friendly messageJeff King, May 14, 2012
  18. Junio C HamanoMay 10, 2012
  19. Jeff KingMay 10, 2012
  20. Junio C HamanoMay 10, 2012
  21. Junio C HamanoMay 10, 2012
  22. Angus HammondMay 10, 2012
  23. Nguyen Thai Ngoc DuyMay 11, 2012

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.