[gnome-remote-desktop] rdp: Perform basic error checking in SAM file creation
- From: Jonas Ådahl <jadahl src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-remote-desktop] rdp: Perform basic error checking in SAM file creation
- Date: Mon, 25 Apr 2022 19:36:36 +0000 (UTC)
commit 4f98e08a54b705434e51e4c262a1269c703fd25a
Author: Pascal Nowack <Pascal Nowack gmx de>
Date: Sun Apr 24 11:00:24 2022 +0200
rdp: Perform basic error checking in SAM file creation
src/grd-rdp-sam.c | 82 ++++++++++++++++++++++++++++++++-------------------
src/grd-rdp-sam.h | 2 +-
src/grd-session-rdp.c | 9 ++----
3 files changed, 55 insertions(+), 38 deletions(-)
---
diff --git a/src/grd-rdp-sam.c b/src/grd-rdp-sam.c
index 17cd3c06..e71c6c5b 100644
--- a/src/grd-rdp-sam.c
+++ b/src/grd-rdp-sam.c
@@ -28,6 +28,19 @@
#include <sys/stat.h>
#include <winpr/ntlm.h>
+void
+grd_rdp_sam_free_sam_file (GrdRdpSAMFile *rdp_sam_file)
+{
+ if (rdp_sam_file->fd >= 0)
+ {
+ unlink (rdp_sam_file->filename);
+ close (rdp_sam_file->fd);
+ }
+
+ g_free (rdp_sam_file->filename);
+ g_free (rdp_sam_file);
+}
+
static char *
create_sam_string (const char *username,
const char *password)
@@ -59,51 +72,58 @@ grd_rdp_sam_create_sam_file (const char *username,
{
const char *grd_path = "/gnome-remote-desktop";
const char *template = "/rdp-sam-XXXXXX";
- const char *path;
- char *filename;
- char *sam_string;
- int fd_flags;
GrdRdpSAMFile *rdp_sam_file;
+ g_autofree char *file_dir = NULL;
+ g_autofree char *filename = NULL;
+ g_autofree char *sam_string = NULL;
+ int fd;
+ int fd_flags;
FILE *sam_file;
- rdp_sam_file = g_malloc0 (sizeof (GrdRdpSAMFile));
+ file_dir = g_strdup_printf ("%s%s", g_get_user_runtime_dir (), grd_path);
+ filename = g_strdup_printf ("%s%s%s", g_get_user_runtime_dir (), grd_path,
+ template);
- path = getenv ("XDG_RUNTIME_DIR");
- filename = g_malloc0 (strlen (path) + strlen (grd_path) +
- strlen (template) + 1);
- strcpy (filename, path);
- strcat (filename, grd_path);
- if (g_access (filename, F_OK))
- mkdir (filename, 0700);
+ if (g_access (file_dir, F_OK) &&
+ mkdir (file_dir, 0700))
+ {
+ g_warning ("Failed to create base runtime directory for "
+ "gnome-remote-desktop: %s", g_strerror (errno));
+ return NULL;
+ }
- strcat (filename, template);
+ fd = mkstemp (filename);
+ if (fd < 0)
+ {
+ g_warning ("[RDP] mkstemp() failed: %s", g_strerror (errno));
+ return NULL;
+ }
- rdp_sam_file->fd = mkstemp (filename);
- rdp_sam_file->filename = filename;
+ rdp_sam_file = g_new0 (GrdRdpSAMFile, 1);
+ rdp_sam_file->fd = fd;
+ rdp_sam_file->filename = g_steal_pointer (&filename);
fd_flags = fcntl (rdp_sam_file->fd, F_GETFD);
- fcntl (rdp_sam_file->fd, F_SETFD, fd_flags | FD_CLOEXEC);
+ if (fd_flags == -1 ||
+ fcntl (rdp_sam_file->fd, F_SETFD, fd_flags | FD_CLOEXEC) == -1)
+ {
+ g_warning ("[RDP] fcntl() failed: %s", g_strerror (errno));
+ grd_rdp_sam_free_sam_file (rdp_sam_file);
+ return NULL;
+ }
sam_string = create_sam_string (username, password);
sam_file = fdopen (rdp_sam_file->fd, "w+");
+ if (!sam_file)
+ {
+ g_warning ("[RDP] Failed to open SAM database: %s", g_strerror (errno));
+ grd_rdp_sam_free_sam_file (rdp_sam_file);
+ return NULL;
+ }
+
fputs (sam_string, sam_file);
fclose (sam_file);
- g_free (sam_string);
-
return rdp_sam_file;
}
-
-void
-grd_rdp_sam_maybe_close_and_free_sam_file (GrdRdpSAMFile *rdp_sam_file)
-{
- if (rdp_sam_file->fd >= 0)
- {
- unlink (rdp_sam_file->filename);
- close (rdp_sam_file->fd);
- }
-
- g_free (rdp_sam_file->filename);
- g_free (rdp_sam_file);
-}
diff --git a/src/grd-rdp-sam.h b/src/grd-rdp-sam.h
index ef759574..db296d59 100644
--- a/src/grd-rdp-sam.h
+++ b/src/grd-rdp-sam.h
@@ -31,6 +31,6 @@ struct _GrdRdpSAMFile
GrdRdpSAMFile *grd_rdp_sam_create_sam_file (const char *username,
const char *password);
-void grd_rdp_sam_maybe_close_and_free_sam_file (GrdRdpSAMFile *rdp_sam_file);
+void grd_rdp_sam_free_sam_file (GrdRdpSAMFile *rdp_sam_file);
#endif /* GRD_RDP_SAM_H */
diff --git a/src/grd-session-rdp.c b/src/grd-session-rdp.c
index 7c24ca9e..7464523a 100644
--- a/src/grd-session-rdp.c
+++ b/src/grd-session-rdp.c
@@ -1698,7 +1698,6 @@ rdp_peer_post_connect (freerdp_peer *peer)
RdpPeerContext *rdp_peer_context = (RdpPeerContext *) peer->context;
GrdSessionRdp *session_rdp = rdp_peer_context->session_rdp;
rdpSettings *rdp_settings = peer->settings;
- GrdRdpSAMFile *sam_file;
g_debug ("New RDP client");
@@ -1764,8 +1763,7 @@ rdp_peer_post_connect (freerdp_peer *peer)
grd_session_start (GRD_SESSION (session_rdp));
- sam_file = g_steal_pointer (&session_rdp->sam_file);
- grd_rdp_sam_maybe_close_and_free_sam_file (sam_file);
+ g_clear_pointer (&session_rdp->sam_file, grd_rdp_sam_free_sam_file);
if (rdp_settings->SupportGraphicsPipeline &&
rdp_peer_context->network_autodetection)
@@ -1978,7 +1976,7 @@ init_rdp_session (GrdSessionRdp *session_rdp,
{
g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
"Failed to initialize peer");
- grd_rdp_sam_maybe_close_and_free_sam_file (session_rdp->sam_file);
+ g_clear_pointer (&session_rdp->sam_file, grd_rdp_sam_free_sam_file);
freerdp_peer_context_free (peer);
freerdp_peer_free (peer);
return FALSE;
@@ -2228,8 +2226,7 @@ grd_session_rdp_stop (GrdSession *session)
peer->Close (peer);
g_clear_object (&session_rdp->connection);
- if (session_rdp->sam_file)
- grd_rdp_sam_maybe_close_and_free_sam_file (session_rdp->sam_file);
+ g_clear_pointer (&session_rdp->sam_file, grd_rdp_sam_free_sam_file);
g_clear_object (&rdp_peer_context->network_autodetection);
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]