aboutsummaryrefslogtreecommitdiff
path: root/libssh/misc.c
diff options
context:
space:
mode:
Diffstat (limited to 'libssh/misc.c')
-rw-r--r--libssh/misc.c46
1 files changed, 46 insertions, 0 deletions
diff --git a/libssh/misc.c b/libssh/misc.c
index 95b20306..22fb1a65 100644
--- a/libssh/misc.c
+++ b/libssh/misc.c
@@ -158,6 +158,52 @@ uint64_t ntohll(uint64_t a) {
#endif
}
+#ifdef _WIN32
+char *ssh_get_local_username(ssh_session session) {
+ DWORD size = 0;
+ char *user;
+
+ /* get the size */
+ GetUserName(NULL, &size);
+
+ user = malloc(size);
+ if (user == NULL) {
+ ssh_set_error_oom(session);
+ return NULL;
+ }
+
+ if (GetUserName(user, &size)) {
+ return user;
+ }
+
+ return NULL;
+}
+#else
+char *ssh_get_local_username(ssh_session session) {
+ struct passwd pwd;
+ struct passwd *pwdbuf;
+ char buf[NSS_BUFLEN_PASSWD];
+ char *name;
+ int rc;
+
+ rc = getpwuid_r(getuid(), &pwd, buf, NSS_BUFLEN_PASSWD, &pwdbuf);
+ if (rc != 0) {
+ ssh_set_error(session, SSH_FATAL,
+ "Couldn't retrieve information for current user!");
+ return NULL;
+ }
+
+ name = strdup(pwd.pw_name);
+
+ if (name == NULL) {
+ ssh_set_error_oom(session);
+ return NULL;
+ }
+
+ return name;
+}
+#endif
+
/**
* @brief Check if libssh is the required version or get the version
* string.