Skip to content

Commit

Permalink
ident: use full dns names to generate email addresses
Browse files Browse the repository at this point in the history
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>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
  • Loading branch information
Jeff King authored and Junio C Hamano committed May 22, 2012
1 parent 2f70587 commit f8254d3
Showing 1 changed file with 4 additions and 9 deletions.
13 changes: 4 additions & 9 deletions ident.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,23 +65,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)
Expand Down

0 comments on commit f8254d3

Please sign in to comment.