session BUGFIX memory leaks
diff --git a/src/session_client.c b/src/session_client.c
index 7640856..f6ce856 100644
--- a/src/session_client.c
+++ b/src/session_client.c
@@ -1249,12 +1249,6 @@
         return NULL;
     }
 
-    pw = nc_getpwuid(geteuid(), &pw_buf, &buf, &buf_size);
-    if (pw == NULL) {
-        ERR(NULL, "Failed to find username for euid=%u.\n", geteuid());
-        goto fail;
-    }
-
     sock = socket(AF_UNIX, SOCK_STREAM, 0);
     if (sock < 0) {
         ERR(NULL, "Failed to create socket (%s).", strerror(errno));
@@ -1295,8 +1289,14 @@
 
     lydict_insert(ctx, address, 0, &session->path);
 
+    pw = nc_getpwuid(geteuid(), &pw_buf, &buf, &buf_size);
+    if (!pw) {
+        ERR(NULL, "Failed to find username for UID %u.", (unsigned int)geteuid());
+        goto fail;
+    }
     username = strdup(pw->pw_name);
-    if (username == NULL) {
+    free(buf);
+    if (!username) {
         ERRMEM;
         goto fail;
     }
diff --git a/src/session_client_ssh.c b/src/session_client_ssh.c
index 7533285..0b0bdb9 100644
--- a/src/session_client_ssh.c
+++ b/src/session_client_ssh.c
@@ -1547,8 +1547,8 @@
     int sock;
     struct passwd *pw, pw_buf;
     struct nc_session *session = NULL;
-    char *buf;
-    size_t buf_len;
+    char *buf = NULL;
+    size_t buf_len = 0;
 
     if (!ssh_session) {
         ERRARG("ssh_session");
@@ -1612,6 +1612,7 @@
                     goto fail;
                 }
                 username = strdup(pw->pw_name);
+                free(buf);
             } else {
                 username = strdup(opts->username);
             }
@@ -1771,9 +1772,11 @@
     session->port = port;
     lydict_insert(ctx, username, 0, &session->username);
 
+    free(buf);
     return session;
 
 fail:
+    free(buf);
     free(ip_host);
     nc_session_free(session, NULL);
     return NULL;
@@ -1886,6 +1889,7 @@
             return NULL;
         }
         ssh_options_set(sess, SSH_OPTIONS_USER, pw->pw_name);
+        free(buf);
     } else {
         ssh_options_set(sess, SSH_OPTIONS_USER, ssh_ch_opts.username);
     }
diff --git a/src/session_server.c b/src/session_server.c
index 4e90d25..8509af9 100644
--- a/src/session_server.c
+++ b/src/session_server.c
@@ -1897,6 +1897,7 @@
     }
 
     username = strdup(pw->pw_name);
+    free(buf);
     if (username == NULL) {
         ERRMEM;
         close(sock);