Skip to content

Commit

Permalink
SecretAgent initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
cmuhammedrafi committed Dec 30, 2024
1 parent b8d5719 commit 3382423
Show file tree
Hide file tree
Showing 5 changed files with 1,855 additions and 0 deletions.
108 changes: 108 additions & 0 deletions gdbus/SecretAgent/NetworkManagerSecretAgent.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
#include <gio/gio.h>
#include "secret_agent.h" // Generated by gdbus-codegen


// gcc -o secret_agent NetworkManagerSecretAgent.c secret_agent.c $(pkg-config --cflags --libs gio-2.0)
GMainLoop *loop;
// Handler for the GetSecrets method
static gboolean handle_get_secrets(
OrgFreedesktopNetworkManagerSecretAgent *object,
GDBusMethodInvocation *invocation,
GVariant *connection,
const gchar *connection_path,
const gchar *setting_name,
const gchar *const *hints,
guint flags,
gpointer user_data)
{
// Log the method call
// Log the method call
g_print("cancel_get_secrets called: connection_path='%s', setting_name='%s'\n",
connection_path, setting_name);
flags & 0x01/* NM_SECRET_AGENT_GET_SECRETS_FLAG_ALLOW_INTERACTION */
? g_print("Allow interaction\n")
: g_print("Disallow interaction\n");
flags & 0x08 /* NM_SECRET_AGENT_GET_SECRETS_FLAG_WPS_PBC_ACTIVE */
? g_print("wps pbc active\n")
: g_print("not a wps request\n");


// Return dummy secrets as a{sa{sv}}
GVariantBuilder secrets_builder;
g_variant_builder_init(&secrets_builder, G_VARIANT_TYPE("a{sa{sv}}"));

g_variant_builder_open(&secrets_builder, G_VARIANT_TYPE("sa{sv}"));
g_variant_builder_add(&secrets_builder, "s", "dummy-setting");
g_variant_builder_open(&secrets_builder, G_VARIANT_TYPE("a{sv}"));
g_variant_builder_add(&secrets_builder, "{sv}", "key", g_variant_new_string("secret-value"));
g_variant_builder_close(&secrets_builder);
g_variant_builder_close(&secrets_builder);

GVariant *secrets = g_variant_builder_end(&secrets_builder);
org_freedesktop_network_manager_secret_agent_complete_get_secrets(object, invocation, secrets);

return TRUE;
}

// Handler for the CancelGetSecrets() method
static gboolean handle_cancel_get_secrets(
OrgFreedesktopNetworkManagerSecretAgent *object,
GDBusMethodInvocation *invocation,
const gchar *connection_path,
const gchar *setting_name,
gpointer user_data)
{
// Log the method call
g_print("cancel_get_secrets called: connection_path='%s', setting_name='%s'\n",
connection_path, setting_name);

org_freedesktop_network_manager_secret_agent_complete_cancel_get_secrets(object, invocation);

return TRUE;
}

// Start the secret agent
void main()
{
GError *error = NULL;

// Create a connection to the session bus
GDBusConnection *connection = g_bus_get_sync(G_BUS_TYPE_SESSION, NULL, &error);
if (error != NULL) {
g_printerr("Error connecting to D-Bus: %s\n", error->message);
g_error_free(error);
return;
}

// Create an instance of the interface
OrgFreedesktopNetworkManagerSecretAgent *agent = org_freedesktop_network_manager_secret_agent_skeleton_new();

// Connect the method handler
g_signal_connect(agent, "handle-get-secrets", G_CALLBACK(handle_get_secrets), NULL);
// Connect the method handler
g_signal_connect(agent, "handle-cancel-get-secrets", G_CALLBACK(handle_cancel_get_secrets), NULL);

// Export the object on the D-Bus
if (!g_dbus_interface_skeleton_export(
G_DBUS_INTERFACE_SKELETON(agent),
connection,
"/org/freedesktop/NetworkManager/SecretAgent",
&error)) {
g_printerr("Error exporting object: %s\n", error->message);
g_error_free(error);
g_object_unref(agent);
return;
}

g_print("SecretAgent running at /org/freedesktop/NetworkManager/SecretAgent\n");

// Run the main loop
loop = g_main_loop_new(NULL, FALSE);
g_main_loop_run(loop);
g_main_loop_quit(loop);
// Cleanup
g_object_unref(agent);
g_main_loop_unref(loop);
g_object_unref(connection);

}
18 changes: 18 additions & 0 deletions gdbus/SecretAgent/secretAgent.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<!DOCTYPE node PUBLIC "-//freedesktop//DTD D-Bus Object Introspection 1.0//EN"
"http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd">
<node>
<interface name="org.freedesktop.NetworkManager.SecretAgent">
<method name="GetSecrets">
<arg type="a{sa{sv}}" name="connection" direction="in"/>
<arg type="o" name="connection_path" direction="in"/>
<arg type="s" name="setting_name" direction="in"/>
<arg type="as" name="hints" direction="in"/>
<arg type="u" name="flags" direction="in"/>
<arg type="a{sa{sv}}" name="secrets" direction="out"/>
</method>
<method name="CancelGetSecrets">
<arg type="o" name="connection_path" direction="in"/>
<arg type="s" name="setting_name" direction="in"/>
</method>
</interface>
</node>
Binary file added gdbus/SecretAgent/secret_agent
Binary file not shown.
Loading

0 comments on commit 3382423

Please sign in to comment.