changeset 507:65e80d258b34

Irccd: use system username, closes #562 Use the system user login name as both nickname and username to avoid having collision with possible existing irccd. This is currently only possible if the system has `getlogin` function which targets primarily unix platforms. Windows support will be added later.
author David Demelier <markand@malikania.fr>
date Mon, 16 Oct 2017 12:54:01 +0200
parents 24eeaa4cc221
children 8328d56e1e35
files cmake/IrccdSystem.cmake cmake/internal/sysconfig.hpp.in doc/html/irccd/configuring.md doc/man/irccd.conf.5.in libcommon/irccd/system.cpp libcommon/irccd/system.hpp libirccd/irccd/server.cpp libirccd/irccd/server.hpp
diffstat 8 files changed, 49 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/cmake/IrccdSystem.cmake	Mon Oct 16 12:43:48 2017 +0200
+++ b/cmake/IrccdSystem.cmake	Mon Oct 16 12:54:01 2017 +0200
@@ -118,6 +118,7 @@
 #
 # HAVE_ACCESS           True if has access(2) function (and sys/types.h and sys/stat.h),
 # HAVE_DAEMON           True if daemon(3),
+# HAVE_GETLOGIN         True if getlogin(3) function (and unistd.h)
 # HAVE_GETPID           True if has getpid(2) function (and sys/types.h and unistd.h and grp.h),
 # HAVE_POPEN            True if has popen(3) function (in stdio.h)
 # HAVE_SETGID           True if has setgid(2) function and getgrnam(3) (and sys/types.h and unistd.h and pwd.h),
@@ -165,6 +166,17 @@
     set(HAVE_ACCESS FALSE)
 endif ()
 
+# getlogin() function
+#
+# If HAVE_GETLOGIN is defined, include:
+#
+# #include <unistd.h>
+check_function_exists(getlogin HAVE_GETLOGIN)
+
+if (NOT HAVE_UNISTD_H)
+    set(HAVE_GETLOGIN FALSE)
+endif ()
+
 # getpid() function
 #
 # If HAVE_GETPID is defined, include:
--- a/cmake/internal/sysconfig.hpp.in	Mon Oct 16 12:43:48 2017 +0200
+++ b/cmake/internal/sysconfig.hpp.in	Mon Oct 16 12:54:01 2017 +0200
@@ -81,6 +81,7 @@
 
 #cmakedefine HAVE_ACCESS
 #cmakedefine HAVE_DAEMON
+#cmakedefine HAVE_GETLOGIN
 #cmakedefine HAVE_GETPID
 #cmakedefine HAVE_POPEN
 #cmakedefine HAVE_SETGID
--- a/doc/html/irccd/configuring.md	Mon Oct 16 12:43:48 2017 +0200
+++ b/doc/html/irccd/configuring.md	Mon Oct 16 12:54:01 2017 +0200
@@ -111,9 +111,9 @@
 The available options:
 
   - **name**: (id) the identity unique id,
-  - **nickname**: (string) the nickname (Optional, default: irccd),
+  - **nickname**: (string) the nickname (Optional, default: system username if available or irccd),
   - **realname**: (string) the realname (Optional, default: IRC Client Daemon),
-  - **username**: (string) the username name (Optional, default: irccd),
+  - **username**: (string) the username name (Optional, default: system username if available or irccd),
   - **ctcp-version**: (string) what version to respond to CTCP VERSION (Optional, default: IRC Client Daemon).
 
 **Example**
--- a/doc/man/irccd.conf.5.in	Mon Oct 16 12:43:48 2017 +0200
+++ b/doc/man/irccd.conf.5.in	Mon Oct 16 12:54:01 2017 +0200
@@ -106,11 +106,11 @@
 .It name
 (id) the identity unique id.
 .It nickname
-(string) the nickname (Optional, default: irccd).
+(string) the nickname (Optional, default: system username if available or irccd).
 .It realname
 (string) the realname (Optional, default: IRC Client daemon).
 .It username
-(string) the username name (Optional, default: irccd).
+(string) the username name (Optional, default: system username if available or irccd).
 .It ctcp-version
 (string) what version to respond to CTCP VERSION (Optional, default: IRC Client Daemon).
 .El
--- a/libcommon/irccd/system.cpp	Mon Oct 16 12:43:48 2017 +0200
+++ b/libcommon/irccd/system.cpp	Mon Oct 16 12:54:01 2017 +0200
@@ -95,6 +95,11 @@
 #   include <pwd.h>
 #endif
 
+// For sys::username
+#if defined(HAVE_GETLOGIN)
+#   include <unistd.h>
+#endif
+
 #include "fs.hpp"
 #include "logger.hpp"
 #include "system.hpp"
@@ -563,6 +568,18 @@
     return system_directory(WITH_SYSCONFDIR);
 }
 
+std::string username()
+{
+#if defined(HAVE_GETLOGIN)
+    auto v = getlogin();
+
+    if (v)
+        return v;
+#endif
+
+    return "";
+}
+
 std::vector<std::string> config_filenames(std::string file)
 {
     std::vector<std::string> result;
--- a/libcommon/irccd/system.hpp	Mon Oct 16 12:43:48 2017 +0200
+++ b/libcommon/irccd/system.hpp	Mon Oct 16 12:54:01 2017 +0200
@@ -150,6 +150,12 @@
  */
 std::string sysconfigdir();
 
+/**
+ * Get user account login or empty if not available.
+ *
+ * \return the user account name
+ */
+std::string username();
 
 /**
  * Construct a list of paths to read configuration files from.
--- a/libirccd/irccd/server.cpp	Mon Oct 16 12:43:48 2017 +0200
+++ b/libirccd/irccd/server.cpp	Mon Oct 16 12:54:01 2017 +0200
@@ -36,6 +36,7 @@
 #include "logger.hpp"
 #include "util.hpp"
 #include "server.hpp"
+#include "system.hpp"
 
 namespace irccd {
 
@@ -507,6 +508,12 @@
     , session_(std::make_unique<session>())
     , state_(std::make_unique<connecting_state>())
 {
+    // Initialize nickname and username.
+    auto user = sys::username();
+
+    nickname_ = user.empty() ? "irccd" : user;
+    username_ = user.empty() ? "irccd" : user;
+
     irc_callbacks_t callbacks;
 
     /*
--- a/libirccd/irccd/server.hpp	Mon Oct 16 12:43:48 2017 +0200
+++ b/libirccd/irccd/server.hpp	Mon Oct 16 12:54:01 2017 +0200
@@ -439,8 +439,8 @@
     std::uint8_t flags_{0};
 
     // Identity.
-    std::string nickname_{"irccd"};
-    std::string username_{"irccd"};
+    std::string nickname_;
+    std::string username_;
     std::string realname_{"IRC Client Daemon"};
     std::string ctcpversion_{"IRC Client Daemon"};