b2f7f85acdef21c5c55c30f2936927abeb6ec043
[moonshot-ui.git] / libmoonshot / libmoonshot-msrpc.c
1 /* libmoonshot - Moonshot client library
2  * Copyright (c) 2011, JANET(UK)
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  *
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.
15  *
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.
19  *
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
30  * SUCH DAMAGE.
31  *
32  * Author: Sam Thursfield <samthursfield@codethink.co.uk>
33  */
34
35 #include <windows.h>
36 //#include <rpc.h>
37 #include <msrpc-mingw.h>
38
39 #include "libmoonshot.h"
40 #include "libmoonshot-common.h"
41 #include "moonshot-msrpc.h"
42
43 #define MOONSHOT_ENDPOINT_NAME "/org/janet/Moonshot"
44 #define MOONSHOT_INSTALL_PATH_KEY "Software\\Moonshot"
45
46
47 static void launch_server (MoonshotError **error) {
48     HKEY key = NULL;
49     STARTUPINFO startup_info = { 0 };
50     PROCESS_INFORMATION process_info = { 0 };
51     LONG status;
52     DWORD value_type;
53     DWORD length;
54     char exe_path[1024];
55
56     status = RegOpenKeyEx (HKEY_LOCAL_MACHINE,
57                            MOONSHOT_INSTALL_PATH_KEY,
58                            0,
59                            KEY_READ,
60                            &key);
61
62     if (status != 0) {
63         *error = _moonshot_error_new (MOONSHOT_ERROR_OS_ERROR,
64                                       "Unable to read registry key HKLM\\%s",
65                                       MOONSHOT_INSTALL_PATH_KEY);
66         return;
67     }
68
69     length = 1023;
70     status = RegQueryValueEx (key, NULL, NULL, &value_type, exe_path, &length);
71
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 "
76                                       "moonshot.exe",
77                                       MOONSHOT_INSTALL_PATH_KEY);
78         return;
79     }
80
81
82     if (status != 0) {
83         *error = _moonshot_error_new (MOONSHOT_ERROR_OS_ERROR,
84                                       "Unable to read value of registry key HKLM\\%s",
85                                       MOONSHOT_INSTALL_PATH_KEY);
86         return;
87     }
88
89     startup_info.cb = sizeof (startup_info);
90
91     status = CreateProcess (exe_path, NULL,
92                             NULL, NULL,
93                             FALSE, DETACHED_PROCESS,
94                             NULL, NULL,
95                             &startup_info, &process_info);
96
97     if (status != 0) {
98         *error = _moonshot_error_new (MOONSHOT_ERROR_UNABLE_TO_START_SERVICE,
99                                       "Unable to spawn the moonshot server at '%s'",
100                                       exe_path);
101         return;
102     }
103 }
104
105 /*static void dbus_call_complete_cb (DBusGProxy     *proxy,
106                                    DBusGProxyCall *call_id,
107                                    void           *user_data)
108 {
109     GError *error = NULL;
110     GSimpleAsyncResult   *token;
111     MoonshotIdentityData *identity_data;
112     gboolean              success;
113
114     token = G_SIMPLE_ASYNC_RESULT (user_data);
115     identity_data = moonshot_identity_data_new ();
116
117     dbus_g_proxy_end_call (moonshot_dbus_proxy,
118                            call_id,
119                            &error,
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,
127                            G_TYPE_INVALID);
128
129     if (error != NULL) {
130         g_simple_async_result_set_from_error (token, error);
131     }
132     else
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);
139     }
140     else {        
141         g_simple_async_result_set_op_res_gpointer (token,
142                                                    identity_data,
143                                                    moonshot_identity_data_free);
144     }
145
146     g_simple_async_result_complete (token);
147     g_object_unref (token);
148 }
149 */
150
151 int moonshot_get_identity (const char     *nai,
152                            const char     *password,
153                            const char     *service,
154                            char          **nai_out,
155                            char          **password_out,
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)
161 {
162     int status;
163
164     status = rpc_client_bind (&moonshot_binding_handle,
165                               MOONSHOT_ENDPOINT_NAME,
166                               RPC_PER_USER);
167
168     printf ("RPC status: %i\n", status);
169
170     /*DBusGProxyCall     *call_id;
171     GSimpleAsyncResult *result; 
172     GError *error = NULL;
173
174     if (moonshot_dbus_proxy == NULL)
175         moonshot_dbus_proxy = dbus_connect (&error);
176
177     if (moonshot_dbus_proxy == NULL) {
178         result = g_simple_async_result_new (NULL,
179                                             callback,
180                                             user_data,
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);
185         return;
186     }
187
188     g_return_if_fail (DBUS_IS_G_PROXY (moonshot_dbus_proxy));
189
190     result = g_simple_async_result_new (NULL,
191                                         callback,
192                                         user_data,
193                                         moonshot_get_identity);
194
195     call_id = dbus_g_proxy_begin_call (moonshot_dbus_proxy,
196                                        "GetIdentity",
197                                        dbus_call_complete_cb,
198                                        result, NULL,
199                                        G_TYPE_STRING, nai,
200                                        G_TYPE_STRING, password,
201                                        G_TYPE_STRING, service);*/
202 }
203
204 /*gboolean moonshot_get_identity_finish (GAsyncResult  *result,
205                                        char         **nai,
206                                        char         **password,
207                                        char         **server_certificate_hash,
208                                        char         **ca_certificate,
209                                        char         **subject_name_constraint,
210                                        char         **subject_alt_name_constraint,
211                                        GError       **error)
212 {
213     MoonshotIdentityData *identity;
214
215     g_return_val_if_fail (g_simple_async_result_is_valid (result,
216                                                           NULL,
217                                                           moonshot_get_identity),
218                           FALSE);
219
220     if (g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (result), error))
221         return FALSE;
222
223     identity = g_simple_async_result_get_op_res_gpointer (G_SIMPLE_ASYNC_RESULT (result));
224
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;
231
232     return TRUE;
233 }*/
234
235
236     /**
237      * Returns the default identity - most recently used.
238      *
239      * @param nai_out NAI stored in the ID card
240      * @param password_out Password stored in the ID card
241      *
242      * @return true on success, false if no identities are stored
243      */