diff options
author | 2013-07-19 13:11:18 +0000 | |
---|---|---|
committer | 2013-07-19 13:11:18 +0000 | |
commit | 9f1f4ec329687bf418359c364522cb8ebb58207c (patch) | |
tree | 75de0f53a29db7d0c92555a48bf39a8e5195c0c3 | |
parent | remove obsolete file (diff) | |
download | wireguard-openbsd-9f1f4ec329687bf418359c364522cb8ebb58207c.tar.xz wireguard-openbsd-9f1f4ec329687bf418359c364522cb8ebb58207c.zip |
Improve and document the way the default server name is found.
-rw-r--r-- | usr.sbin/smtpd/parse.y | 91 | ||||
-rw-r--r-- | usr.sbin/smtpd/smtpd.conf.5 | 40 |
2 files changed, 110 insertions, 21 deletions
diff --git a/usr.sbin/smtpd/parse.y b/usr.sbin/smtpd/parse.y index ede623459db..ec11ec99ae0 100644 --- a/usr.sbin/smtpd/parse.y +++ b/usr.sbin/smtpd/parse.y @@ -1,4 +1,4 @@ -/* $OpenBSD: parse.y,v 1.118 2013/06/03 08:48:40 zhuk Exp $ */ +/* $OpenBSD: parse.y,v 1.119 2013/07/19 13:11:18 eric Exp $ */ /* * Copyright (c) 2008 Gilles Chehade <gilles@poolp.org> @@ -103,6 +103,7 @@ int interface(const char *, const char *, const char *, void set_localaddrs(void); int delaytonum(char *); int is_if_in_group(const char *, const char *); +int getmailname(char *, size_t); typedef struct { union { @@ -1329,11 +1330,10 @@ parse_config(struct smtpd *x_conf, const char *filename, int opts) struct sym *sym, *next; struct table *t; char hostname[SMTPD_MAXHOSTNAMELEN]; + char hostname_copy[SMTPD_MAXHOSTNAMELEN]; - if (gethostname(hostname, sizeof hostname) == -1) { - fprintf(stderr, "invalid hostname: gethostname() failed\n"); + if (! getmailname(hostname, sizeof hostname)) return (-1); - } conf = x_conf; bzero(conf, sizeof(*conf)); @@ -1392,6 +1392,13 @@ parse_config(struct smtpd *x_conf, const char *filename, int opts) table_add(t, "localhost", NULL); table_add(t, hostname, NULL); + /* can't truncate here */ + (void)strlcpy(hostname_copy, hostname, sizeof hostname_copy); + + hostname_copy[strcspn(hostname_copy, ".")] = '\0'; + if (strcmp(hostname, hostname_copy) != 0) + table_add(t, hostname_copy, NULL); + table_create("getpwnam", "<getpwnam>", NULL, NULL); /* @@ -1423,12 +1430,7 @@ parse_config(struct smtpd *x_conf, const char *filename, int opts) } if (strlen(conf->sc_hostname) == 0) - if (gethostname(conf->sc_hostname, - sizeof(conf->sc_hostname)) == -1) { - log_warn("warn: could not determine host name"); - bzero(conf->sc_hostname, sizeof(conf->sc_hostname)); - errors++; - } + strlcpy(conf->sc_hostname, hostname, sizeof conf->sc_hostname); if (errors) { purge_config(PURGE_EVERYTHING); @@ -1856,3 +1858,72 @@ end: close(s); return ret; } + +int +getmailname(char *hostname, size_t len) +{ + struct addrinfo hints, *res = NULL; + FILE *fp; + char *buf, *lbuf = NULL; + size_t buflen; + int error; + int ret = 0; + + /* First, check if we have "/etc/mailname" */ + if ((fp = fopen("/etc/mailname", "r")) == NULL) + goto nomailname; + + if ((buf = fgetln(fp, &buflen)) == NULL) + goto end; + + if (buf[buflen-1] == '\n') + buf[buflen - 1] = '\0'; + else { + if ((lbuf = calloc(buflen + 1, 1)) == NULL) + err(1, "calloc"); + memcpy(lbuf, buf, buflen); + } + + if (strlcpy(hostname, buf, len) >= len) + fprintf(stderr, "/etc/mailname entry too long"); + else { + ret = 1; + goto end; + } + + +nomailname: + if (gethostname(hostname, len) == -1) { + fprintf(stderr, "invalid hostname: gethostname() failed\n"); + goto end; + } + + if (strchr(hostname, '.') == NULL) { + memset(&hints, 0, sizeof hints); + hints.ai_family = PF_UNSPEC; + hints.ai_socktype = SOCK_STREAM; + hints.ai_protocol = IPPROTO_TCP; + hints.ai_flags = AI_CANONNAME; + error = getaddrinfo(hostname, NULL, &hints, &res); + if (error) { + fprintf(stderr, "invalid hostname: getaddrinfo() failed: %s\n", + gai_strerror(error)); + goto end; + } + + if (strlcpy(hostname, res->ai_canonname, len) >= len) { + fprintf(stderr, "hostname too long"); + goto end; + } + } + + ret = 1; + +end: + free(lbuf); + if (res) + freeaddrinfo(res); + if (fp) + fclose(fp); + return ret; +} diff --git a/usr.sbin/smtpd/smtpd.conf.5 b/usr.sbin/smtpd/smtpd.conf.5 index aa8fbacc50e..294c470ba40 100644 --- a/usr.sbin/smtpd/smtpd.conf.5 +++ b/usr.sbin/smtpd/smtpd.conf.5 @@ -1,4 +1,4 @@ -.\" $OpenBSD: smtpd.conf.5,v 1.98 2013/07/19 08:12:19 eric Exp $ +.\" $OpenBSD: smtpd.conf.5,v 1.99 2013/07/19 13:11:18 eric Exp $ .\" .\" Copyright (c) 2008 Janne Johansson <jj@openbsd.org> .\" Copyright (c) 2009 Jacek Masiulaniec <jacekm@dobremiasto.net> @@ -223,15 +223,11 @@ table, see .It Ic for local Op Ic alias Aq Ar aliases This rule applies to mail destined to .Dq localhost -and to the server's fully qualified domain name, -as returned by -.Xr hostname 1 . +and to the default server name (see below). .It Ic for local virtual Aq Ar vmap This rule applies to mail destined to .Dq localhost -and to the server's fully qualified domain name, -as returned by -.Xr hostname 1 . +and to the default server name (see below). The .Ar vmap table will be used as the virtual domain mapping. @@ -296,7 +292,7 @@ value in the MX record for the domain than the one specified in .Ar mx . If .Ar mx -is not specified, local hostname will be assumed. +is not specified, default server name (see below) will be assumed. .Pp If the .Ic as @@ -321,9 +317,9 @@ when connecting to the relay. If the table contains more than one address, they are picked in turn each time a new connection is opened. .Pp -By default, +By default, when connecting to a remote server, .Xr smtpd 8 -advertises its local hostname when connecting to a remote server. +advertises its default server name (see below). A .Ic helo parameter may be specified to advertise an alternate hostname. @@ -414,6 +410,11 @@ will explicitly bind to an address found in the table referenced by when connecting to the relay. If the table contains more than one address, they are picked in turn each time a new connection is opened. +.Pp +By default, when connecting to a remote server, +.Xr smtpd 8 +advertises its default server name (see below). +A .Ic helo parameter may be specified to advertise an alternate hostname. Table @@ -533,7 +534,8 @@ tagged .Pp If the .Ic hostname -parameter is used, then it will be used in the greeting banner. +parameter is used, then it will be used in the greeting banner +instead of the default server name (see below). .It Ic max-message-size Ar n Specify a maximum message size of .Ar n @@ -604,12 +606,28 @@ many mappings as a list of comma separated .Ar key Ns = Ns Ar value descriptions. .El +.Sh DEFAULT SERVER NAME +The default server name is the hostname that +.Xr smtpd 8 +operates as, which is used at various places. +It is determined at startup as follows. +.Pp +If a +.Pa /etc/mailname +file exists, then the first line is read and used as the server name. +Otherwise, the server name is derived from the local hostname returned by +.Xr gethostname 3 , +either directly if it is a fully-qualified domain name, +or by retreiving the associated canonical name through +.Xr getaddrinfo 3 . .Sh FILES .Bl -tag -width "/etc/mail/smtpd.confXXX" -compact .It Pa /etc/mail/smtpd.conf Default .Xr smtpd 8 configuration file. +.It Pa /etc/mailname +Alternate server name to use. .It Pa /var/spool/smtpd/ Spool directories for mail during processing. .El |