[syslog-ng][PATCH] use_fqdn with gethostname() not returning fqdn

Sven Wegener syslog-ng@lists.balabit.hu
Sun May 8 06:32:10 CEST 2005


--Hf61M2y+wYpnELGG
Content-Type: multipart/mixed; boundary="ncSAzJYg3Aa9+CRW"
Content-Disposition: inline


--ncSAzJYg3Aa9+CRW
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable

Hi all!

I came across this issue while I was setting up a central syslog-ng
server that collects logs from several hosts in different domains. I
wanted to use the use_fqdn option to distinguish every host in my logs
better, but all I saw was just the plain hostname. Please find attached
two patches, one against 1.6.7 and one against 1.9.4 that add support
for fqdns. If gethostname() returns a name without dots it tries to
lookup the fqdn of the host.

Cheers,
Sven

--=20
Sven Wegener
Gentoo Linux Developer
http://www.gentoo.org/

--ncSAzJYg3Aa9+CRW
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment; filename="syslog-ng-1.6.7-fqdn.patch"

diff -Nur syslog-ng-1.6.7/src/sources.c syslog-ng-1.6.7-fqdn/src/sources.c
--- syslog-ng-1.6.7/src/sources.c	2005-02-03 12:08:11.000000000 +0100
+++ syslog-ng-1.6.7-fqdn/src/sources.c	2005-05-08 04:01:19.167140941 +0200
@@ -229,6 +229,16 @@
 			if (usefqdn) {
 				gethostname(buf, sizeof(buf) - 1);
 				buf[127] = 0;
+				// Check if hostname includes a . else do a fqdn lookup
+				if (NULL == strchr(buf, '.')) {
+					struct addrinfo *result, hints;
+					memset(&hints, 0, sizeof(hints));
+					hints.ai_flags = AI_CANONNAME;
+					if (0 == getaddrinfo(buf, NULL, &hints, &result)) {
+						strncpy(buf, result->ai_canonname, sizeof(buf) - 1);
+						freeaddrinfo(result);
+					}
+				}
 			}
 			else {
 				getshorthostname(buf, sizeof(buf));

--ncSAzJYg3Aa9+CRW
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment; filename="syslog-ng-1.9.4-fqdn.patch"
Content-Transfer-Encoding: quoted-printable

diff -Nur syslog-ng-1.9.4/src/misc.c syslog-ng-1.9.4-fqdn/src/misc.c
--- syslog-ng-1.9.4/src/misc.c	2004-12-27 23:45:41.000000000 +0100
+++ syslog-ng-1.9.4-fqdn/src/misc.c	2005-05-08 07:04:04.392031522 +0200
@@ -45,6 +45,24 @@
 }
=20
 char *
+getlonghostname(char *buf, size_t bufsize)
+{
+  gethostname(buf, bufsize - 1);
+  if (NULL =3D=3D strchr(buf, '.'))
+    {
+      struct addrinfo *result, hints;
+      memset(&hints, 0, sizeof(hints));
+      hints.ai_flags =3D AI_CANONNAME;
+      if (0 =3D=3D getaddrinfo(buf, NULL, &hints, &result))
+        {
+          strncpy(buf, result->ai_canonname, bufsize - 1);
+          freeaddrinfo(result);
+        }
+    }
+  return buf;
+}
+
+char *
 getshorthostname(char *buf, size_t bufsize)
 {
   char *s;
@@ -96,9 +114,16 @@
     }
   else=20
     {
-      if (!local_hostname[0])=20
+      if (!local_hostname[0])
        {
-         getshorthostname(local_hostname, sizeof(local_hostname));
+         if (usefqdn)
+           {
+             getlonghostname(local_hostname, sizeof(local_hostname));
+           }
+         else
+            {
+              getshorthostname(local_hostname, sizeof(local_hostname));
+            }
 	}
=20
       hname =3D local_hostname;
diff -Nur syslog-ng-1.9.4/src/misc.h syslog-ng-1.9.4-fqdn/src/misc.h
--- syslog-ng-1.9.4/src/misc.h	2004-12-27 23:48:02.000000000 +0100
+++ syslog-ng-1.9.4-fqdn/src/misc.h	2005-05-08 07:00:54.731289603 +0200
@@ -33,6 +33,7 @@
 GString *g_string_assign_len(GString *s, gchar *val, gint len);
=20
 char *getshorthostname(char *buf, size_t buflen);
+char *getlonghostname(char *buf, size_t buflen);
 GString *resolve_hostname(GSockAddr *saddr, int usedns, int usefqdn);
 gboolean g_fd_set_nonblock(int fd, gboolean enable);
=20

--ncSAzJYg3Aa9+CRW--

--Hf61M2y+wYpnELGG
Content-Type: application/pgp-signature
Content-Disposition: inline

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (GNU/Linux)

iD8DBQFCfaRaAXomK8S72HoRAj1VAJ9WTRYks8QaZk3gnjB8tBSzCXdRiwCdFTEN
yKHE0bLRCaYH+93j3l4fwtY=
=vFx6
-----END PGP SIGNATURE-----

--Hf61M2y+wYpnELGG--



More information about the syslog-ng mailing list