-
Notifications
You must be signed in to change notification settings - Fork 0
/
cpn-client.c
308 lines (257 loc) · 8.69 KB
/
cpn-client.c
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
/*
* Copyright (C) 2016 Patrick Steinhardt
*
* 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, either version 3 of the License, or
* (at your option) any later version.
*
* 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 <errno.h>
#include <string.h>
#include <stdio.h>
#include <sodium.h>
#include <inttypes.h>
#include "capone/client.h"
#include "capone/common.h"
#include "capone/channel.h"
#include "capone/global.h"
#include "capone/log.h"
#include "capone/opts.h"
#include "capone/service.h"
static struct cpn_opt request_opts[] = {
CPN_OPTS_OPT_STRINGLIST(0, "--parameters", NULL, "PARAMETER", true),
CPN_OPTS_OPT_STRING('c', "--service-type",
"Type of service which is to be invoked", "TYPE", false),
CPN_OPTS_OPT_END
};
static struct cpn_opt connect_opts[] = {
CPN_OPTS_OPT_STRING(0, "--service-type",
"Type of service which is to be invoked", "TYPE", false),
CPN_OPTS_OPT_UINT32(0, "--session-id", NULL, "ID", false),
CPN_OPTS_OPT_STRING('c', "--session-cap", NULL, "CAP", false),
CPN_OPTS_OPT_END
};
static struct cpn_opt terminate_opts[] = {
CPN_OPTS_OPT_UINT32(0, "--session-id", NULL, "ID", false),
CPN_OPTS_OPT_STRING('c', "--session-cap", NULL, "CAP", false),
CPN_OPTS_OPT_END
};
static struct cpn_opt opts[] = {
CPN_OPTS_OPT_STRING('c', "--config",
"Path to configuration file", "CFGFILE", false),
CPN_OPTS_OPT_SIGKEY(0, "--remote-key",
"Public signature key of the host to query", "KEY", false),
CPN_OPTS_OPT_STRING(0, "--remote-host",
"Network address of the host to query", "ADDRESS", false),
CPN_OPTS_OPT_UINT32(0, "--remote-port",
"Port of the host to query", "PORT", false),
CPN_OPTS_OPT_COUNTER('v', "--verbose", "Control logging verbosity"),
CPN_OPTS_OPT_ACTION("query", NULL, NULL),
CPN_OPTS_OPT_ACTION("request", NULL, request_opts),
CPN_OPTS_OPT_ACTION("connect", NULL, connect_opts),
CPN_OPTS_OPT_ACTION("terminate", NULL, terminate_opts),
CPN_OPTS_OPT_END
};
static struct cpn_cfg cfg;
static struct cpn_sign_keys local_keys;
static struct cpn_sign_pk remote_key;
static const char *remote_host;
static uint32_t remote_port;
static int cmd_query(void)
{
struct cpn_sign_pk_hex hex;
struct cpn_query_results results;
struct cpn_channel channel;
if (cpn_client_connect(&channel, remote_host, remote_port,
&local_keys, &remote_key) < 0)
{
puts("Could not establish connection");
return -1;
}
if (cpn_client_query_service(&results, &channel) < 0) {
puts("Could not query service");
return -1;
}
cpn_sign_pk_hex_from_key(&hex, &remote_key);
printf("%s\n"
"\tname: %s\n"
"\tcategory: %s\n"
"\ttype: %s\n"
"\tversion: %"PRIu32"\n"
"\tlocation: %s\n"
"\tport: %"PRIu32"\n",
hex.data,
results.name,
results.category,
results.type,
results.version,
results.location,
results.port);
cpn_channel_close(&channel);
return 0;
}
static int cmd_request(const char *service_type, const struct cpn_opts_stringlist *parameters)
{
ProtobufCMessage *params = NULL;
const struct cpn_service_plugin *plugin;
struct cpn_cap *cap = NULL;
struct cpn_channel channel;
char *cap_hex = NULL;
uint32_t sessionid;
int err = -1;
memset(&channel, 0, sizeof(channel));
if (cpn_service_plugin_for_type(&plugin, service_type) < 0) {
printf("Could not find service plugin for type %s\n", service_type);
goto out_err;
}
if (plugin->parse_fn && plugin->parse_fn(¶ms, parameters->argc, parameters->argv) < 0) {
printf("Could not parse parameters\n");
goto out_err;
}
if (cpn_client_connect(&channel, remote_host, remote_port,
&local_keys, &remote_key) < 0)
{
puts("Could not establish connection");
goto out_err;
}
if (cpn_client_request_session(&sessionid, &cap, &channel, params) < 0) {
puts("Unable to request session");
goto out_err;
}
if (cpn_cap_to_string(&cap_hex, cap) < 0)
{
puts("Invalid capability");
goto out_err;
}
printf("sessionid: %"PRIu32"\n"
"capability: %s\n",
sessionid, cap_hex);
err = 0;
out_err:
cpn_channel_close(&channel);
cpn_cap_free(cap);
free(cap_hex);
if (params)
protobuf_c_message_free_unpacked(params, NULL);
return err;
}
static int cmd_connect(const char *service_type, uint32_t sessionid,
const char *capability)
{
const struct cpn_service_plugin *plugin;
struct cpn_channel channel;
struct cpn_session *session = NULL;
struct cpn_cap *cap = NULL;
int err = -1;
channel.fd = -1;
if (cpn_service_plugin_for_type(&plugin, service_type) < 0) {
printf("Invalid service plugin %s\n", service_type);
goto out;
}
if (cpn_cap_from_string(&cap, capability) < 0) {
puts("Invalid capability");
goto out;
}
if (cpn_client_connect(&channel, remote_host, remote_port,
&local_keys, &remote_key) < 0)
{
puts("Could not start connection");
goto out;
}
if (cpn_client_start_session(&session, &channel, sessionid, cap, plugin) < 0) {
puts("Could not connect to session");
goto out;
}
if (plugin->client_fn(&channel, session, &cfg) < 0) {
puts("Could not invoke service");
goto out;
}
err = 0;
out:
cpn_channel_close(&channel);
cpn_cap_free(cap);
cpn_session_free(session);
return err;
}
static int cmd_terminate(uint32_t sessionid, const char *capability)
{
struct cpn_channel channel;
struct cpn_cap *cap = NULL;
int err = -1;
if (cpn_cap_from_string(&cap, capability) < 0) {
puts("Invalid capability\n");
goto out;
}
if (cpn_client_connect(&channel, remote_host, remote_port,
&local_keys, &remote_key) < 0)
{
puts("Could not start connection");
goto out;
}
if (cpn_client_terminate_session(&channel, sessionid, cap) < 0) {
puts("Could not initiate termination");
goto out;
}
err = 0;
out:
cpn_cap_free(cap);
return err;
}
int main(int argc, const char *argv[])
{
if (cpn_global_init() < 0)
return -1;
if (cpn_opts_parse_cmd(opts, argc, argv) < 0) {
return -1;
}
switch (cpn_opts_get(opts, 'v', NULL)->counter) {
case 0:
cpn_log_set_level(LOG_LEVEL_ERROR);
break;
case 1:
cpn_log_set_level(LOG_LEVEL_WARNING);
break;
case 2:
cpn_log_set_level(LOG_LEVEL_VERBOSE);
break;
case 3:
cpn_log_set_level(LOG_LEVEL_TRACE);
break;
default:
break;
}
if (cpn_cfg_parse(&cfg, cpn_opts_get(opts, 'c', NULL)->string) < 0) {
printf("Could not parse config '%s", cpn_opts_get(opts, 'c', NULL)->string);
return -1;
}
if (cpn_sign_keys_from_config(&local_keys, &cfg) < 0) {
puts("Could not keys from config");
return -1;
}
memcpy(&remote_key, &cpn_opts_get(opts, 0, "--remote-key")->sigkey, sizeof(struct cpn_sign_pk));
remote_host = cpn_opts_get(opts, 0, "--remote-host")->string;
remote_port = cpn_opts_get(opts, 0, "--remote-port")->uint32;
if (cpn_opts_get(opts, 0, "query"))
return cmd_query();
else if (cpn_opts_get(opts, 0, "request"))
return cmd_request(cpn_opts_get(request_opts, 0, "--service-type")->string,
&cpn_opts_get(request_opts, 0, "--parameters")->stringlist);
else if (cpn_opts_get(opts, 0, "connect"))
return cmd_connect(cpn_opts_get(connect_opts, 0, "--service-type")->string,
cpn_opts_get(connect_opts, 0, "--session-id")->uint32,
cpn_opts_get(connect_opts, 0, "--session-cap")->string);
else if (cpn_opts_get(opts, 0, "terminate"))
return cmd_terminate(cpn_opts_get(terminate_opts, 0, "--session-id")->uint32,
cpn_opts_get(terminate_opts, 0, "--session-cap")->string);
else
puts("No action specified");
return 0;
}