summaryrefslogtreecommitdiff
path: root/usr.sbin/smtpd
diff options
context:
space:
mode:
authorSunil Nimmagadda <sunil@cvs.openbsd.org>2016-01-12 17:29:44 +0000
committerSunil Nimmagadda <sunil@cvs.openbsd.org>2016-01-12 17:29:44 +0000
commit7876d3f3c89d56dee275ec4b107457d6e6569412 (patch)
treef6ebe2597e43f279edd7589155b1a75bf20ad7be /usr.sbin/smtpd
parentd978eb639dcfb7c940c8631cd4a2287fdc0fb1a5 (diff)
Let smtpd start on machines without a FQDN as hostname.
Ok millert@ gilles@ jung@
Diffstat (limited to 'usr.sbin/smtpd')
-rw-r--r--usr.sbin/smtpd/enqueue.c4
-rw-r--r--usr.sbin/smtpd/parse.y4
-rw-r--r--usr.sbin/smtpd/util.c87
3 files changed, 49 insertions, 46 deletions
diff --git a/usr.sbin/smtpd/enqueue.c b/usr.sbin/smtpd/enqueue.c
index cf595c1f71f..9bb0d423eed 100644
--- a/usr.sbin/smtpd/enqueue.c
+++ b/usr.sbin/smtpd/enqueue.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: enqueue.c,v 1.110 2015/12/29 17:57:03 millert Exp $ */
+/* $OpenBSD: enqueue.c,v 1.111 2016/01/12 17:29:43 sunil Exp $ */
/*
* Copyright (c) 2005 Henning Brauer <henning@bulabula.org>
@@ -238,7 +238,7 @@ enqueue(int argc, char *argv[], FILE *ofp)
argv += optind;
if (getmailname(host, sizeof(host)) == -1)
- err(EX_NOHOST, "getmailname");
+ errx(EX_NOHOST, "getmailname");
if (no_getlogin) {
if ((pw = getpwuid(getuid())) == NULL)
user = "anonymous";
diff --git a/usr.sbin/smtpd/parse.y b/usr.sbin/smtpd/parse.y
index 777673e4853..216ffff585f 100644
--- a/usr.sbin/smtpd/parse.y
+++ b/usr.sbin/smtpd/parse.y
@@ -1,4 +1,4 @@
-/* $OpenBSD: parse.y,v 1.179 2016/01/04 13:30:20 jung Exp $ */
+/* $OpenBSD: parse.y,v 1.180 2016/01/12 17:29:43 sunil Exp $ */
/*
* Copyright (c) 2008 Gilles Chehade <gilles@poolp.org>
@@ -1836,7 +1836,7 @@ parse_config(struct smtpd *x_conf, const char *filename, int opts)
char hostname[HOST_NAME_MAX+1];
char hostname_copy[HOST_NAME_MAX+1];
- if (!getmailname(hostname, sizeof hostname))
+ if (getmailname(hostname, sizeof hostname) == -1)
return (-1);
conf = x_conf;
diff --git a/usr.sbin/smtpd/util.c b/usr.sbin/smtpd/util.c
index b6b7cd5abdb..8e62e941660 100644
--- a/usr.sbin/smtpd/util.c
+++ b/usr.sbin/smtpd/util.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: util.c,v 1.124 2015/12/28 22:08:30 jung Exp $ */
+/* $OpenBSD: util.c,v 1.125 2016/01/12 17:29:43 sunil Exp $ */
/*
* Copyright (c) 2000,2001 Markus Friedl. All rights reserved.
@@ -53,6 +53,7 @@
const char *log_in6addr(const struct in6_addr *);
const char *log_sockaddr(struct sockaddr *);
+static int parse_mailname_file(char *, size_t);
void *
xmalloc(size_t size, const char *where)
@@ -737,67 +738,69 @@ parse_smtp_response(char *line, size_t len, char **msg, int *cont)
return NULL;
}
-int
-getmailname(char *hostname, size_t len)
+static int
+parse_mailname_file(char *hostname, size_t len)
{
- struct addrinfo hints, *res = NULL;
FILE *fp;
char *buf = NULL;
size_t bufsz = 0;
ssize_t buflen;
- int error, ret = 0;
- /* First, check if we have MAILNAME_FILE */
if ((fp = fopen(MAILNAME_FILE, "r")) == NULL)
- goto nomailname;
+ return 1;
if ((buflen = getline(&buf, &bufsz, fp)) == -1)
- goto end;
+ goto error;
if (buf[buflen - 1] == '\n')
buf[buflen - 1] = '\0';
- if (strlcpy(hostname, buf, len) >= len)
+ if (strlcpy(hostname, buf, len) >= len) {
fprintf(stderr, MAILNAME_FILE " entry too long");
- else {
- ret = 1;
- goto end;
+ goto error;
}
-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;
- }
+ return 0;
+error:
+ fclose(fp);
+ free(buf);
+ return 1;
+}
- if (strlcpy(hostname, res->ai_canonname, len) >= len) {
- fprintf(stderr, "hostname too long");
- goto end;
- }
+int
+getmailname(char *hostname, size_t len)
+{
+ struct addrinfo hints, *res = NULL;
+ int error;
+
+ /* Try MAILNAME_FILE first */
+ if (parse_mailname_file(hostname, len) == 0)
+ return 0;
+
+ /* Next, gethostname(3) */
+ if (gethostname(hostname, len) == -1) {
+ fprintf(stderr, "getmailname: gethostname() failed\n");
+ return -1;
}
- ret = 1;
+ if (strchr(hostname, '.') != NULL)
+ return 0;
-end:
- free(buf);
- if (res)
- freeaddrinfo(res);
- if (fp)
- fclose(fp);
- return ret;
+ /* Canonicalize if domain part is missing */
+ memset(&hints, 0, sizeof hints);
+ hints.ai_family = PF_UNSPEC;
+ hints.ai_flags = AI_CANONNAME;
+ error = getaddrinfo(hostname, NULL, &hints, &res);
+ if (error)
+ return 0; /* Continue with non-canon hostname */
+
+ if (strlcpy(hostname, res->ai_canonname, len) >= len) {
+ fprintf(stderr, "hostname too long");
+ return -1;
+ }
+
+ freeaddrinfo(res);
+ return 0;
}
int