1 /* libmoonshot - Moonshot client library
2 * Copyright (c) 2011, JANET(UK)
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of JANET(UK) nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * Author: Sam Thursfield <samthursfield@codethink.co.uk>
37 #include <msrpc-mingw.h>
39 #include "libmoonshot.h"
40 #include "libmoonshot-common.h"
41 #include "moonshot-msrpc.h"
43 #define MOONSHOT_ENDPOINT_NAME "/org/janet/Moonshot"
44 #define MOONSHOT_INSTALL_PATH_KEY "Software\\Moonshot"
47 static void launch_server (MoonshotError **error) {
49 STARTUPINFO startup_info = { 0 };
50 PROCESS_INFORMATION process_info = { 0 };
56 status = RegOpenKeyEx (HKEY_LOCAL_MACHINE,
57 MOONSHOT_INSTALL_PATH_KEY,
63 *error = _moonshot_error_new (MOONSHOT_ERROR_OS_ERROR,
64 "Unable to read registry key HKLM\\%s",
65 MOONSHOT_INSTALL_PATH_KEY);
70 status = RegQueryValueEx (key, NULL, NULL, &value_type, exe_path, &length);
72 if (value_type != REG_SZ) {
73 *error = _moonshot_error_new (MOONSHOT_ERROR_OS_ERROR,
74 "Value of registry key HKLM\\%s is invalid. "
75 "Please set it to point to the location of "
77 MOONSHOT_INSTALL_PATH_KEY);
83 *error = _moonshot_error_new (MOONSHOT_ERROR_OS_ERROR,
84 "Unable to read value of registry key HKLM\\%s",
85 MOONSHOT_INSTALL_PATH_KEY);
89 startup_info.cb = sizeof (startup_info);
91 status = CreateProcess (exe_path, NULL,
93 FALSE, DETACHED_PROCESS,
95 &startup_info, &process_info);
98 *error = _moonshot_error_new (MOONSHOT_ERROR_UNABLE_TO_START_SERVICE,
99 "Unable to spawn the moonshot server at '%s'",
105 /*static void dbus_call_complete_cb (DBusGProxy *proxy,
106 DBusGProxyCall *call_id,
109 GError *error = NULL;
110 GSimpleAsyncResult *token;
111 MoonshotIdentityData *identity_data;
114 token = G_SIMPLE_ASYNC_RESULT (user_data);
115 identity_data = moonshot_identity_data_new ();
117 dbus_g_proxy_end_call (moonshot_dbus_proxy,
120 G_TYPE_STRING, &identity_data->nai,
121 G_TYPE_STRING, &identity_data->password,
122 G_TYPE_STRING, &identity_data->server_certificate_hash,
123 G_TYPE_STRING, &identity_data->ca_certificate,
124 G_TYPE_STRING, &identity_data->subject_name_constraint,
125 G_TYPE_STRING, &identity_data->subject_alt_name_constraint,
126 G_TYPE_BOOLEAN, &success,
130 g_simple_async_result_set_from_error (token, error);
133 if (success == FALSE) {
134 error = g_error_new (MOONSHOT_ERROR,
135 MOONSHOT_ERROR_NO_IDENTITY_SELECTED,
136 "No matching identity was available");
137 g_simple_async_result_set_from_error (token, error);
138 g_error_free (error);
141 g_simple_async_result_set_op_res_gpointer (token,
143 moonshot_identity_data_free);
146 g_simple_async_result_complete (token);
147 g_object_unref (token);
151 int moonshot_get_identity (const char *nai,
152 const char *password,
156 char **server_certificate_hash_out,
157 char **ca_certificate_out,
158 char **subject_name_constraint_out,
159 char **subject_alt_name_constraint_out,
160 MoonshotError **error)
164 status = rpc_client_bind (&moonshot_binding_handle,
165 MOONSHOT_ENDPOINT_NAME,
168 printf ("RPC status: %i\n", status);
170 /*DBusGProxyCall *call_id;
171 GSimpleAsyncResult *result;
172 GError *error = NULL;
174 if (moonshot_dbus_proxy == NULL)
175 moonshot_dbus_proxy = dbus_connect (&error);
177 if (moonshot_dbus_proxy == NULL) {
178 result = g_simple_async_result_new (NULL,
181 moonshot_get_identity);
182 g_simple_async_result_set_from_error (result, error);
183 g_simple_async_result_complete_in_idle (result);
184 g_error_free (error);
188 g_return_if_fail (DBUS_IS_G_PROXY (moonshot_dbus_proxy));
190 result = g_simple_async_result_new (NULL,
193 moonshot_get_identity);
195 call_id = dbus_g_proxy_begin_call (moonshot_dbus_proxy,
197 dbus_call_complete_cb,
200 G_TYPE_STRING, password,
201 G_TYPE_STRING, service);*/
204 /*gboolean moonshot_get_identity_finish (GAsyncResult *result,
207 char **server_certificate_hash,
208 char **ca_certificate,
209 char **subject_name_constraint,
210 char **subject_alt_name_constraint,
213 MoonshotIdentityData *identity;
215 g_return_val_if_fail (g_simple_async_result_is_valid (result,
217 moonshot_get_identity),
220 if (g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (result), error))
223 identity = g_simple_async_result_get_op_res_gpointer (G_SIMPLE_ASYNC_RESULT (result));
225 *nai = identity->nai;
226 *password = identity->password;
227 *server_certificate_hash = identity->server_certificate_hash;
228 *ca_certificate = identity->ca_certificate;
229 *subject_name_constraint = identity->subject_name_constraint;
230 *subject_alt_name_constraint = identity->subject_alt_name_constraint;
237 * Returns the default identity - most recently used.
239 * @param nai_out NAI stored in the ID card
240 * @param password_out Password stored in the ID card
242 * @return true on success, false if no identities are stored