]> arthur.barton.de Git - ngircd-alex.git/blobdiff - src/ngircd/conf.c
Auto-generate server name when not set in any config file
[ngircd-alex.git] / src / ngircd / conf.c
index 1c5ba624d5dd93e732b511d204dca4e8f6196244..29270875444993f38c418a8bd06cb0884820e1a9 100644 (file)
@@ -33,6 +33,7 @@
 #include <grp.h>
 #include <sys/types.h>
 #include <dirent.h>
+#include <netdb.h>
 
 #include "ngircd.h"
 #include "conn.h"
@@ -2052,6 +2053,7 @@ Validate_Config(bool Configtest, bool Rehash)
 #ifdef DEBUG
        int i, servers, servers_once;
 #endif
+       struct hostent *h;
        bool config_valid = true;
        char *ptr;
 
@@ -2076,9 +2078,30 @@ Validate_Config(bool Configtest, bool Rehash)
                break;
        } while (*(++ptr));
 
-       if (!Conf_ServerName[0] || !strchr(Conf_ServerName, '.'))
-       {
-               /* No server name configured! */
+       if (!Conf_ServerName[0]) {
+               /* No server name configured, try to get a sane name from the
+                * host name. Note: the IRC server name MUST contain
+                * at least one dot, so the "node name" is not sufficient! */
+               gethostname(Conf_ServerName, sizeof(Conf_ServerName));
+               h = gethostbyname(Conf_ServerName);
+               if (h && strchr(h->h_name, '.')) {
+                       /* Now we have a valid name! */
+                       strlcpy(Conf_ServerName, h->h_name,
+                               sizeof(Conf_ServerName));
+                       Config_Error(LOG_WARNING,
+                                    "No server name configured, using host name \"%s\".",
+                                    Conf_ServerName);
+               } else {
+                       /* No or no valid name, no dot! Show error message and
+                        * reset the server name, so the regular error handler
+                        * below will be triggered ... */
+                       Config_Error(LOG_ALERT,
+                                    "Failed to get a valid server name from node name \"%s\"!",
+                                    Conf_ServerName);
+                       Conf_ServerName[0] = '\0';
+               }
+       }
+       if (!Conf_ServerName[0] || !strchr(Conf_ServerName, '.')) {
                config_valid = false;
                Config_Error(LOG_ALERT,
                             "No (valid) server name configured in \"%s\" (section 'Global': 'Name')!",