Skip to content

Commit 0bc3eb5

Browse files
committed
lib: add canonical_hostname function
1 parent 7a2963a commit 0bc3eb5

File tree

11 files changed

+72
-52
lines changed

11 files changed

+72
-52
lines changed

exch/http/main.cpp

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,6 @@ static bool http_reload_config(std::shared_ptr<CONFIG_FILE> xcfg = nullptr,
156156

157157
int main(int argc, char **argv)
158158
{
159-
char temp_buff[256];
160159
int retcode = EXIT_FAILURE;
161160
char host_name[UDOM_SIZE], *ptoken;
162161
const char *dns_name, *dns_domain, *netbios_name;
@@ -193,13 +192,12 @@ int main(int argc, char **argv)
193192

194193
auto str_val = g_config_file->get_value("host_id");
195194
if (str_val == NULL) {
196-
memset(temp_buff, 0, std::size(temp_buff));
197-
gethostname(temp_buff, std::size(temp_buff));
198-
temp_buff[std::size(temp_buff)-1] = '\0';
199-
g_config_file->set_value("host_id", temp_buff);
200-
str_val = temp_buff;
201-
if (strchr(str_val, '.') == nullptr)
202-
mlog(LV_NOTICE, "System hostname \"%s\" has no dot, which may point to a misconfiguration", str_val);
195+
std::string hn;
196+
auto ret = canonical_hostname(hn);
197+
if (ret != 0)
198+
return EXIT_FAILURE;
199+
g_config_file->set_value("host_id", hn.c_str());
200+
str_val = g_config_file->get_value("host_id");
203201
}
204202
mlog(LV_INFO, "system: host ID is \"%s\"", str_val);
205203
gx_strlcpy(host_name, str_val, std::size(host_name));
@@ -238,6 +236,7 @@ int main(int argc, char **argv)
238236
thread_init_num);
239237

240238
unsigned int context_aver_mem = g_config_file->get_ll("context_average_mem") / (64 * 1024);
239+
char temp_buff[256];
241240
HX_unit_size(temp_buff, std::size(temp_buff), context_aver_mem * 64 * 1024, 1024, 0);
242241
mlog(LV_INFO, "http: context average memory is %s", temp_buff);
243242

include/gromox/dsn.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ struct GX_EXPORT DSN {
2525
}
2626
std::vector<dsn_field> *get_message_fields() { return &message_fields; }
2727
std::vector<dsn_field> *new_rcpt_fields();
28-
static bool append_field(std::vector<dsn_field> *, const char *tag, const char *value);
28+
static bool append_field(std::vector<dsn_field> *, std::string_view tag, std::string_view value);
2929
bool enum_rcpts_fields(RCPTS_FIELDS_ENUM, void *) const;
3030
static bool enum_fields(const std::vector<dsn_field> &, DSN_FIELDS_ENUM, void *);
3131
bool serialize(char *out, size_t maxlen) const;

include/gromox/util.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ extern GX_EXPORT uint64_t apptime_to_nttime_approx(double);
109109
extern GX_EXPORT std::string gx_utf8_to_punycode(const char *);
110110
extern GX_EXPORT bool str_isascii(const char *);
111111
extern GX_EXPORT bool str_isasciipr(const char *);
112+
extern GX_EXPORT gromox::errno_t canonical_hostname(std::string &);
112113

113114
/* _xlen - exact length (chars); _len - allocation size, i.e. \0-terminated */
114115
/* All the classic 8-bit charsets map to within the Unicode Basic Multilingual Plane */

lib/bounce_gen.cpp

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -92,22 +92,13 @@ errno_t bounce_gen_init(const char *cfgdir, const char *datadir,
9292
}
9393
g_bounce_postmaster = str;
9494
if (str[strlen(str)-1] == '@') {
95-
char buf[UDOM_SIZE];
96-
if (gethostname(buf, std::size(buf)) != 0) {
97-
mlog(LV_ERR, "gethostname: %s", strerror(errno));
95+
std::string hn;
96+
auto ret = canonical_hostname(hn);
97+
if (ret != 0) {
98+
mlog(LV_ERR, "canonical_hostname: %s", strerror(ret));
9899
return EINVAL;
99100
}
100-
static constexpr struct addrinfo hints = {AI_CANONNAME};
101-
struct addrinfo *aires = nullptr;
102-
mlog(LV_DEBUG, "bounce_gen: group %s: DNS lookup for \"%s\"...",
103-
bounce_grp, buf);
104-
auto err = getaddrinfo(buf, nullptr, &hints, &aires);
105-
if (err != 0) {
106-
mlog(LV_ERR, "getaddrinfo %s: %s", buf, gai_strerror(err));
107-
return EINVAL;
108-
}
109-
auto cl_0 = make_scope_exit([&]() { freeaddrinfo(aires); });
110-
g_bounce_postmaster += aires->ai_canonname;
101+
g_bounce_postmaster += std::move(hn);
111102
mlog(LV_INFO, "bounce_gen: postmaster set to <%s>", g_bounce_postmaster.c_str());
112103
}
113104

lib/email/dsn.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,10 @@ std::vector<dsn_field> *DSN::new_rcpt_fields() try
5353
return nullptr;
5454
}
5555

56-
bool DSN::append_field(std::vector<dsn_field> *pfields, const char *tag,
57-
const char *value) try
56+
bool DSN::append_field(std::vector<dsn_field> *pfields, std::string_view tag,
57+
std::string_view value) try
5858
{
59-
pfields->push_back(dsn_field{tag, value});
59+
pfields->emplace_back(std::string(tag), std::string(value));
6060
return true;
6161
} catch (const std::bad_alloc &) {
6262
mlog(LV_ERR, "E-1212: ENOMEM");

lib/mapi/oxcmail.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3663,12 +3663,13 @@ static BOOL oxcmail_export_dsn(const MESSAGE_CONTENT *pmsg, const char *charset,
36633663
auto pdsn_fields = dsn.get_message_fields();
36643664
auto str = pmsg->proplist.get<const char>(PidTagReportingMessageTransferAgent);
36653665
if (str == nullptr) {
3666-
char tmp_buff[5+UDOM_SIZE+1];
3667-
strcpy(tmp_buff, "dns; ");
3668-
gethostname(tmp_buff + 5, sizeof(tmp_buff) - 5);
3669-
tmp_buff[std::size(tmp_buff)-1] = '\0';
3670-
if (!dsn.append_field(pdsn_fields, "Reporting-MTA", tmp_buff))
3671-
return FALSE;
3666+
std::string hn;
3667+
auto ret = canonical_hostname(hn);
3668+
if (ret == 0) {
3669+
hn.insert(0, "dsn; ");
3670+
if (!dsn.append_field(pdsn_fields, "Reporting-MTA", hn))
3671+
return FALSE;
3672+
}
36723673
} else {
36733674
if (!dsn.append_field(pdsn_fields, "Reporting-MTA", str))
36743675
return FALSE;

lib/rfbl.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1659,6 +1659,30 @@ int haproxy_intervene(int fd, unsigned int level, struct sockaddr_storage *ss)
16591659
return -1;
16601660
}
16611661

1662+
errno_t canonical_hostname(std::string &out) try
1663+
{
1664+
char buf[UDOM_SIZE];
1665+
if (gethostname(buf, std::size(buf)) != 0)
1666+
return errno;
1667+
if (strchr(buf, '.') != nullptr) {
1668+
out = buf;
1669+
return 0;
1670+
}
1671+
static constexpr struct addrinfo hints = {AI_CANONNAME};
1672+
struct addrinfo *aires = nullptr;
1673+
mlog(LV_DEBUG, "my_hostname: canonicalization of hostname \"%s\"...", buf);
1674+
auto err = getaddrinfo(buf, nullptr, &hints, &aires);
1675+
if (err != 0) {
1676+
mlog(LV_ERR, "getaddrinfo %s: %s", buf, gai_strerror(err));
1677+
return EINVAL;
1678+
}
1679+
auto cl_0 = make_scope_exit([&]() { freeaddrinfo(aires); });
1680+
out = aires->ai_canonname;
1681+
return 0;
1682+
} catch (const std::bad_alloc &) {
1683+
return ENOMEM;
1684+
}
1685+
16621686
}
16631687

16641688
int XARRAY::append(MITEM &&ptr, unsigned int tag) try

mda/delivery_app/main.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -122,11 +122,12 @@ int main(int argc, char **argv)
122122

123123
auto str_val = g_config_file->get_value("host_id");
124124
if (str_val == NULL) {
125-
memset(temp_buff, '\0', std::size(temp_buff));
126-
gethostname(temp_buff, std::size(temp_buff));
127-
temp_buff[std::size(temp_buff)-1] = '\0';
128-
g_config_file->set_value("host_id", temp_buff);
129-
str_val = temp_buff;
125+
std::string hn;
126+
auto ret = canonical_hostname(hn);
127+
if (ret != 0)
128+
return EXIT_FAILURE;
129+
g_config_file->set_value("host_id", hn.c_str());
130+
str_val = g_config_file->get_value("host_id");
130131
}
131132
mlog(LV_NOTICE, "system: host ID is \"%s\"", str_val);
132133

mda/smtp/main.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -306,11 +306,12 @@ int main(int argc, char **argv)
306306

307307
auto str_val = g_config_file->get_value("host_id");
308308
if (str_val == NULL) {
309-
memset(temp_buff, '\0', std::size(temp_buff));
310-
gethostname(temp_buff, std::size(temp_buff));
311-
temp_buff[std::size(temp_buff)-1] = '\0';
312-
g_config_file->set_value("host_id", temp_buff);
313-
str_val = temp_buff;
309+
std::string hn;
310+
auto ret = canonical_hostname(hn);
311+
if (ret != 0)
312+
return EXIT_FAILURE;
313+
g_config_file->set_value("host_id", hn.c_str());
314+
str_val = g_config_file->get_value("host_id");
314315
}
315316
mlog(LV_NOTICE, "system: host ID is \"%s\"", str_val);
316317

mra/imap/main.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -410,11 +410,12 @@ int main(int argc, char **argv)
410410
uint16_t listen_tls_port = g_config_file->get_ll("imap_listen_tls_port");
411411
auto str_val = g_config_file->get_value("host_id");
412412
if (str_val == NULL) {
413-
memset(temp_buff, 0, std::size(temp_buff));
414-
gethostname(temp_buff, std::size(temp_buff));
415-
temp_buff[std::size(temp_buff)-1] = '\0';
416-
g_config_file->set_value("host_id", temp_buff);
417-
str_val = temp_buff;
413+
std::string hn;
414+
auto ret = canonical_hostname(hn);
415+
if (ret != 0)
416+
return EXIT_FAILURE;
417+
g_config_file->set_value("host_id", hn.c_str());
418+
str_val = g_config_file->get_value("host_id");
418419
}
419420
printf("[system]: host ID is %s\n", str_val);
420421

0 commit comments

Comments
 (0)