File: secret.c

package info (click to toggle)
roger-router 1.8.14-4
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 7,256 kB
  • sloc: ansic: 24,958; sh: 4,322; makefile: 702; xml: 511; cpp: 130
file content (162 lines) | stat: -rw-r--r-- 4,560 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/**
 * Roger Router
 * Copyright (c) 2012-2014 Jan-Michael Brummer
 *
 * This file is part of Roger Router.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 only.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <libsecret/secret.h>

#include <libroutermanager/call.h>
#include <libroutermanager/plugins.h>
#include <libroutermanager/password.h>

#include <secret.h>

#define ROUTERMANAGER_TYPE_SECRET_PLUGIN        (routermanager_secret_plugin_get_type ())
#define ROUTERMANAGER_SECRET_PLUGIN(o)          (G_TYPE_CHECK_INSTANCE_CAST((o), ROUTERMANAGER_TYPE_SECRET_PLUGIN, RouterManagerSecretPlugin))

typedef struct {
	guint signal_id;
} RouterManagerSecretPluginPrivate;

ROUTERMANAGER_PLUGIN_REGISTER(ROUTERMANAGER_TYPE_SECRET_PLUGIN, RouterManagerSecretPlugin, routermanager_secret_plugin)

/**
 * \brief Get secret schema for this plugin
 * \return secret schema layout
 */
const SecretSchema *roger_get_schema(void)
{
	static const SecretSchema the_schema = {
		"org.tabos.roger.Password", SECRET_SCHEMA_NONE,
		{
			{"profile", SECRET_SCHEMA_ATTRIBUTE_STRING},
			{"name", SECRET_SCHEMA_ATTRIBUTE_STRING},
			{"NULL", 0},
		}
	};

	return &the_schema;
}

/**
 * \brief Store password
 * \param profile profile pointer
 * \param name password name
 * \param password password
 */
static void secret_store_password(struct profile *profile, const gchar *name, const gchar *password)
{
	GError *error = NULL;

	secret_password_store_sync(SECRET_SCHEMA, SECRET_COLLECTION_DEFAULT,
	                           "Roger Router password", password, NULL, &error,
	                           "profile", profile ? profile->name : "fallback",
	                           "name", name,
	                           NULL);

	if (error != NULL) {
		/* ... handle the failure here */
		g_debug("could not store password: %s", error->message);
		g_error_free(error);
	} else {
		/* ... do something now that the password has been stored */
	}
}

/**
 * \brief Get password
 * \param profile profile pointer
 * \param name password name
 * \return password
 */
static gchar *secret_get_password(struct profile *profile, const gchar *name)
{
	GError *error = NULL;
	gchar *password = secret_password_lookup_sync(SECRET_SCHEMA, NULL, &error,
	                  "profile", profile ? profile->name : "fallback",
	                  "name", name,
	                  NULL);

	if (error != NULL) {
		/* ... handle the failure here */
		g_debug("could not get password: %s", error->message);
		g_error_free(error);
	} else {
		/* ... do something now that the password has been stored */
	}

	return password;
}

/**
 * \brief Remove password
 * \param profile profile pointer
 * \param name password name
 * \return TRUE on success, otherwise FALSE
 */
static gboolean secret_remove_password(struct profile *profile, const gchar *name)
{
	GError *error = NULL;
	gboolean removed = secret_password_clear_sync(SECRET_SCHEMA, NULL, &error,
	                   "profile", profile ? profile->name : "fallback",
	                   "name", name,
	                   NULL);

	if (error != NULL) {
		/* ... handle the failure here */
		g_debug("could not store password: %s", error->message);
		g_error_free(error);
	} else {
		/* ... do something now that the password has been stored */
	}

	return removed;
}

struct password_manager secret = {
	"libsecret",
	secret_store_password,
	secret_get_password,
	secret_remove_password,
};

/**
 * \brief Activate peas plugin - register libsecret password manager if present
 * \param plugin peas plugin
 */
void impl_activate(PeasActivatable *plugin)
{
	g_debug("Register libsecret password manager plugin");
	password_manager_register(&secret);

#ifdef SECRET_TEST
	g_debug("Starting secret\n");

	secret_store_password(profile_get_active(), "Test", "testpassword");

	g_debug("Got test password: '%s'", secret_get_password(profile_get_active(), "Test"));
	secret_remove_password(profile_get_active(), "Test");
#endif
}

/**
 * \brief Deactivate peas plugin
 * \param plugin peas plugin
 */
void impl_deactivate(PeasActivatable *plugin)
{
}