]> git.ipfire.org Git - thirdparty/shadow.git/commitdiff
libmisc: implement `get_session_host()`
authorIker Pedrosa <ipedrosa@redhat.com>
Tue, 18 Jul 2023 13:56:46 +0000 (15:56 +0200)
committerSerge Hallyn <serge@hallyn.com>
Wed, 2 Aug 2023 15:13:28 +0000 (10:13 -0500)
Implement `get_session_host()` in `utmp.c` and `logind.c`.

Signed-off-by: Iker Pedrosa <ipedrosa@redhat.com>
lib/prototypes.h
libmisc/Makefile.am
libmisc/logind.c [new file with mode: 0644]
libmisc/utmp.c

index 350893e0eeef6e0526f0d2bbd22c2099448d6b2c..066c3d16915ef27195bf2cf1c14376f3d1384c90 100644 (file)
@@ -472,7 +472,19 @@ extern int set_filesize_limit (int blocks);
 /* user_busy.c */
 extern int user_busy (const char *name, uid_t uid);
 
-/* utmp.c */
+/*
+ * Session management: utmp.c or logind.c
+ */
+
+/**
+ * @brief Get host for the current session
+ *
+ * @param[out] out Host name
+ *
+ * @return 0 or a positive integer if the host was obtained properly,
+ *         another value on error.
+ */
+extern int get_session_host (char **out);
 extern /*@null@*/struct utmp *get_current_utmp (void);
 extern struct utmp *prepare_utmp (const char *name,
                                   const char *line,
index cc24901e441feaa53d6e950fc76bbff9cae3deee..6723a7c7c6938a4379a4b81937499546bc39a5ad 100644 (file)
@@ -94,4 +94,4 @@ endif
 
 if ENABLE_LASTLOG
 libmisc_la_SOURCES += log.c
-endif
+endif
\ No newline at end of file
diff --git a/libmisc/logind.c b/libmisc/logind.c
new file mode 100644 (file)
index 0000000..ac1b3a1
--- /dev/null
@@ -0,0 +1,36 @@
+/*
+ * SPDX-FileCopyrightText:  2023, Iker Pedrosa <ipedrosa@redhat.com>
+ *
+ * SPDX-License-Identifier:  BSD-3-Clause
+ */
+
+#include <config.h>
+
+#ident "$Id$"
+
+#include "defines.h"
+#include "prototypes.h"
+
+#include <systemd/sd-login.h>
+
+int get_session_host (char **out)
+{
+    char *host = NULL;
+    char *session = NULL;
+    int ret;
+
+    ret = sd_pid_get_session (getpid(), &session);
+    if (ret < 0) {
+        return ret;
+    }
+    ret = sd_session_get_remote_host (session, &host);
+    if (ret < 0) {
+        goto done;
+    }
+
+    *out = host;
+
+done:
+    free (session);
+    return ret;
+}
index 20b6c9bfc674a60d549571f597e55c671ac7a5c0..c1bc1972563a44a9d0037aaf0347fe4ba9312b71 100644 (file)
@@ -101,6 +101,32 @@ static bool is_my_tty (const char tty[UT_LINESIZE])
        return ret;
 }
 
+int get_session_host (char **out)
+{
+       char *hostname = NULL;
+       struct utmp *ut = NULL;
+       int ret = 0;
+
+       ut = get_current_utmp();
+
+#ifdef HAVE_STRUCT_UTMP_UT_HOST
+       if ((ut != NULL) && (ut->ut_host[0] != '\0')) {
+               hostname = XMALLOC(sizeof(ut->ut_host) + 1, char);
+               strncpy (hostname, ut->ut_host, sizeof (ut->ut_host));
+               hostname[sizeof (ut->ut_host)] = '\0';
+               *out = hostname;
+               free (ut);
+       } else {
+               *out = NULL;
+               ret = -2;
+       }
+#else
+       *out = NULL;
+       ret = -2;
+#endif /* HAVE_STRUCT_UTMP_UT_HOST */
+
+       return ret;
+}
 
 #ifndef USE_PAM
 /*