Skip to content

Commit 87bb625

Browse files
Guillaume Destuyndergdestuynder
authored andcommitted
Handle error if gethostbyname fails
handling gethostname() isn't enough
1 parent 67e04ab commit 87bb625

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

audisp-cef.c

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@
4848
//Bump when the message is modified
4949
#define CEF_AUDIT_MESSAGE_VERSION 2
5050

51+
extern int h_errno;
52+
5153
static volatile int stop = 0;
5254
static volatile int hup = 0;
5355
static cef_conf_t config;
@@ -119,18 +121,25 @@ int main(int argc, char *argv[])
119121
sigaction(SIGTERM, &sa, NULL);
120122
sa.sa_handler = hup_handler;
121123

124+
openlog("audit-cef", LOG_CONS, config.facility);
125+
122126
if (gethostname(nodename, 63)) {
123-
snprintf(nodename, 9, "localhost");
127+
snprintf(nodename, 10, "localhost");
124128
}
125129
nodename[64] = '\0';
126130
ht = gethostbyname(nodename);
127-
hostname = strdup(ht->h_name);
131+
if (ht == NULL) {
132+
hostname = strpdup("localhost");
133+
syslog(LOG_ALERT,
134+
"gethostbyname could not find machine hostname, please fix this. Using %s as fallback. Error: %s",
135+
hostname, hstrerror(h_errno));
136+
} else {
137+
hostname = strdup(ht->h_name);
138+
}
128139

129140
if (load_config(&config, CONFIG_FILE))
130141
return 1;
131142

132-
openlog("audit-cef", LOG_CONS, config.facility);
133-
134143
au = auparse_init(AUSOURCE_FEED, 0);
135144
if (au == NULL) {
136145
syslog(LOG_ERR, "could not initialize auparse");

0 commit comments

Comments
 (0)