Skip to main content
Version: Dev (main)

remote_app_onboarding.c File

An app on the primary hub's PC grants one app on another PC: the invitation joins that PC's hub as a member and admits the app there with no prompt, under a peer id the primary remembers. More...

Included Headers​

#include "common.h" #include <qar_streaming.h> #include <unistd.h>

Functions Index​

static intissue (QarRuntime *runtime)
static intjoin (QarRuntime *runtime)
intmain (int argc, char **argv)
static voidon_progress (QarActionSeverity severity, float percent, const char *message, void *state)
static QarResultonboard_local (QarRuntime *runtime, const char *tag, const QarMemberHubInvitation *grant, QarSession **out_session)
static boolread_invitation (uint8_t *buffer, size_t buffer_size, size_t *out_size)
static voidreport (const char *label, QarResult result)
static boolset_binary (FILE *stream)
static intstart (QarRuntime *runtime)

Macro Definitions Index​

#defineexample_fileno   fileno
#defineexample_isatty   isatty
#defineGRANT_TAG   "client.remote-app-onboarding.examples.quaternar"
#defineISSUER_TAG   "server.remote-app-onboarding.examples.quaternar"
#defineMAX_INVITATION_BYTES   (16 * 1024)

Description​

An app on the primary hub's PC grants one app on another PC: the invitation joins that PC's hub as a member and admits the app there with no prompt, under a peer id the primary remembers.

Functions​

issue()​

int issue (QarRuntime * runtime)
static

Definition at line 215 of file remote_app_onboarding.c.

join()​

int join (QarRuntime * runtime)
static

Definition at line 310 of file remote_app_onboarding.c.

main()​

int main (int argc, char ** argv)

Definition at line 392 of file remote_app_onboarding.c.

on_progress()​

void on_progress (QarActionSeverity severity, float percent, const char * message, void * state)
static

Definition at line 173 of file remote_app_onboarding.c.

onboard_local()​

QarResult onboard_local (QarRuntime * runtime, const char * tag, const QarMemberHubInvitation * grant, QarSession ** out_session)
static

Definition at line 186 of file remote_app_onboarding.c.

read_invitation()​

bool read_invitation (uint8_t * buffer, size_t buffer_size, size_t * out_size)
static

Definition at line 294 of file remote_app_onboarding.c.

report()​

void report (const char * label, QarResult result)
static

Definition at line 160 of file remote_app_onboarding.c.

set_binary()​

bool set_binary (FILE * stream)
static

Definition at line 144 of file remote_app_onboarding.c.

start()​

int start (QarRuntime * runtime)
static

Definition at line 373 of file remote_app_onboarding.c.

Macro Definitions​

example_fileno​

#define example_fileno   fileno

Definition at line 130 of file remote_app_onboarding.c.

example_isatty​

#define example_isatty   isatty

Definition at line 129 of file remote_app_onboarding.c.

GRANT_TAG​

#define GRANT_TAG   "client.remote-app-onboarding.examples.quaternar"

Definition at line 138 of file remote_app_onboarding.c.

ISSUER_TAG​

#define ISSUER_TAG   "server.remote-app-onboarding.examples.quaternar"

Definition at line 137 of file remote_app_onboarding.c.

MAX_INVITATION_BYTES​

#define MAX_INVITATION_BYTES   (16 * 1024)

Definition at line 140 of file remote_app_onboarding.c.

File Listing​

The file content with the documentation metadata removed is:

1/** \file remote_app_onboarding.c
2 * \brief An app on the primary hub's PC grants one app on another PC: the
3 * invitation joins that PC's hub as a member and admits the app there with
4 * no prompt, under a peer id the primary remembers.
5 * \example remote_app_onboarding.c
6 */
7
8/** \page qar_c_tutorial_remote_app_onboarding Remote App on Another PC
9 * \tableofcontents
10 *
11 * \section remote_app_intro What You Will Learn
12 * - Create a member-hub invitation that carries a remote-app grant
13 * (\ref QarMemberHubInvitationAppGrantExt) on the primary hub's PC
14 * - Carry it to the other PC over your application's own channel
15 * - Join that PC's hub to the primary as a member
16 * (\ref qar_runtime_join_hub_as_member)
17 * - Onboard the app there as a local app, redeeming the grant
18 * (\ref QarOnboardRemoteAppGrantExt), and later start it with nothing
19 *
20 * \section remote_app_when When to Use It
21 * Your application already spans two PCs (a simulation server on the QAROS
22 * PC and its clients on other PCs) and the part on the other PC should stream
23 * into the same QAROS session, without an operator walking over to pair it.
24 * The other PC needs QAROS installed; its hub becomes a member of the primary
25 * hub. Windows only.
26 *
27 * An app cannot obtain a pairing code or an unbound invite for another PC:
28 * the grant is the only app-issued way in, and it admits exactly one app.
29 *
30 * \section remote_app_run Build and Run
31 * \code{.bash}
32 * cmake --build --preset x64-windows-debug --target remote_app_onboarding
33 * # On the primary hub's PC (the app's approval needs "May bring in member hubs"):
34 * remote_app_onboarding.exe issue | <your encrypted, authenticated channel>
35 * # On the other PC, first start:
36 * <your channel> | remote_app_onboarding.exe join
37 * # On the other PC, every later start:
38 * remote_app_onboarding.exe start
39 * \endcode
40 * `issue` writes the invitation to stdout and refuses to print it on a
41 * console, so it never lands on screen or in a terminal log; its messages go
42 * to stderr. To try it out by hand, redirect it to a file you delete right
43 * after `join` read it.
44 *
45 * \section remote_app_issue Step 1: Issue the Invitation on the Primary's PC
46 * \snippet remote_app_onboarding.c remote_app_issue
47 *
48 * What the primary hub checks:
49 * - The calling process is an app it approved (the hub identifies it from
50 * Windows, as for a local app: \ref qar_c_tutorial_local_app_onboarding).
51 * This example joins as a local app first so the user can approve it.
52 * Unknown: `QAR_STATUS_ONBOARDING_APPROVAL_DENIED`.
53 * - The operator gave that approval the right to bring in member hubs, with
54 * quota left: otherwise `QAR_STATUS_MEMBER_SPONSOR_NOT_ALLOWED` or
55 * `QAR_STATUS_MEMBER_SPONSOR_QUOTA_EXCEEDED`.
56 * - The hub on this PC is a primary, not a member itself
57 * (`QAR_STATUS_SESSION_WIDE_ACTION_ON_MEMBER`).
58 * - The grant's tag follows the app tag rules
59 * (`QAR_STATUS_ARGUMENT_NOT_SUPPORTED` otherwise).
60 *
61 * The invitation is signed by the primary hub, valid 24 h and for one hub by
62 * default (\ref QarMemberHubInvitationInit), and listed in the primary's
63 * visualizer, where the operator can revoke it. It is a **bearer secret**:
64 * whoever holds it can join a hub until it expires or is used up. Send it only
65 * over an encrypted, authenticated channel, never log it, never put it on a
66 * command line.
67 *
68 * \section remote_app_join Step 2: Join the Other PC's Hub as a Member
69 * \snippet remote_app_onboarding.c remote_app_join
70 *
71 * - This joins the **hub** of that PC, not the app. The call starts the
72 * installed hub when none runs, checks the invitation's signature and
73 * expiry, checks that this PC trusts the primary (an administrator's
74 * allow-list, else the primary this hub joined first), joins and waits
75 * through the hub's restart into the primary's session.
76 * - Typical failures: `QAR_STATUS_MEMBER_INVITATION_EXPIRED`, `_USED_UP`,
77 * `_REVOKED` (ask for a new invitation), `_PRIMARY_NOT_ALLOWED` (this PC
78 * does not trust that primary), `QAR_STATUS_PRIMARY_UNREACHABLE`,
79 * `QAR_STATUS_TIMEOUT` (the join is committed; calling again succeeds once
80 * the hub is back).
81 * - Called again for the primary it already belongs to, it succeeds at once.
82 *
83 * \section remote_app_redeem Step 3: Onboard, Redeeming the Grant
84 * \snippet remote_app_onboarding.c remote_app_redeem
85 *
86 * - The first executable that redeems the grant is approved on the primary
87 * without a prompt and recorded by its publisher (signed) or file hash
88 * (unsigned). A different executable is refused afterwards
89 * (`QAR_STATUS_ONBOARDING_APPROVAL_DENIED`), also with a later invitation of
90 * the same app and tag.
91 * - The primary picks the peer id and remembers it under the grant's tag; no
92 * \ref QarOnboardLocalAppTagExt is needed.
93 * - With a grant, the issuing app's own approval is not extended to the
94 * member hub: the grant admits this one app only.
95 *
96 * \section remote_app_later Step 4: Every Later Start
97 * \snippet remote_app_onboarding.c remote_app_start
98 * A plain local onboard: the app is approved now, so no invitation and no
99 * prompt are needed, and the primary gives it the same peer id while no other
100 * live process holds it.
101 *
102 * \section remote_app_stored What Is Stored, and for How Long
103 * - Either app: nothing; onboarding ids stay zero.
104 * - The channel: the invitation, until used up or expired (default one hub,
105 * 24 h).
106 * - The remote app's memory: key and a 30-minute certificate, renewed while
107 * it runs, as for any local app.
108 * - The primary hub: the granted approval (until revoked) and the remembered
109 * peer id (until unused for 30 days).
110 *
111 * Revoking the issuing app's approval, or the granted one, on the primary
112 * revokes the remote app and forgets its peer id.
113 */
114
115#include "common.h"
116
117#ifndef QAR_ENABLE_DYNAMIC_LOADING
118#define QAR_ENABLE_DYNAMIC_LOADING
119#endif
120#include <qar_streaming.h>
121
122#ifdef _WIN32
123#include <fcntl.h>
124#include <io.h>
125#define example_isatty _isatty
126#define example_fileno _fileno
127#else
128#include <unistd.h>
129#define example_isatty isatty
130#define example_fileno fileno
131#endif
132
134
135/* The issuing app's own tag on the primary, and the tag the grant admits the
136 * remote app under. One grant admits one app: issue one tag per remote app. */
137#define ISSUER_TAG "server.remote-app-onboarding.examples.quaternar"
138#define GRANT_TAG "client.remote-app-onboarding.examples.quaternar"
139/* A serialized invitation is at most 16 KiB. */
140#define MAX_INVITATION_BYTES (16 * 1024)
141
142/* The invitation's bytes pass unchanged (no newline translation). */
143static bool
144set_binary(FILE* stream)
145{
146#ifdef _WIN32
147 if(_setmode(_fileno(stream), _O_BINARY) == -1)
148 {
149 fprintf(stderr, "Could not switch a standard stream to binary mode.\n");
150 return false;
151 }
152#else
153 (void)stream; /* POSIX streams do not translate */
154#endif
155 return true;
156}
157
158/* Messages go to stderr: in the issue role stdout carries the invitation. */
159static void
160report(const char* label, QarResult result)
161{
162 char message[512] = { 0 };
163 if(qar_result_is_success(result))
164 {
165 fprintf(stderr, "%s: ok\n", label);
166 return;
167 }
168 qar_result_message(result, message, sizeof(message));
169 fprintf(stderr, "%s: error %d: %s\n", label, (int)result.code, message);
170}
171
172static void
173on_progress(QarActionSeverity severity, float percent, const char* message, void* state)
174{
175 (void)severity;
176 (void)percent;
177 (void)state;
178 if(message != NULL && message[0] != '\0')
179 {
180 fprintf(stderr, "%s\n", message);
181 }
182}
183
184/* A local onboard; `grant` (optional) redeems a remote-app grant. */
185static QarResult
187 QarRuntime* runtime,
188 const char* tag,
189 const QarMemberHubInvitation* grant,
190 QarSession** out_session
191)
192{
194 init.presentation.display_name = "Remote App Onboarding Example";
198 init.header.next = &local.header;
199 if(grant != NULL)
200 {
201 redeem.invitation = grant; /* borrowed; the grant's tag applies */
202 local.header.next = &redeem.header;
203 }
204 else if(tag != NULL)
205 {
206 tag_ext.app_tag = tag;
207 local.header.next = &tag_ext.header;
208 }
209 QarOnboardingId unused = qar_onboarding_id_default(); /* stays zero */
210 return qar_runtime_onboard(runtime, &init, on_progress, NULL, NULL, &unused, out_session);
211}
212
213/* Role "issue", on the primary hub's PC. */
214static int
215issue(QarRuntime* runtime)
216{
218 {
219 fprintf(
220 stderr,
221 "Refusing to print the invitation on a console: pipe stdout into your channel.\n"
222 );
223 return 1;
224 }
225 /* The issuing app normally is in the session anyway; joining first also
226 * lets the user approve it. */
227 QarSession* session = NULL;
228 QarResult joined = onboard_local(runtime, ISSUER_TAG, NULL, &session);
229 report("qar_runtime_onboard (issuer)", joined);
230 if(qar_result_is_error(joined))
231 {
232 return 1;
233 }
234
235 int exit_code = 1;
236 //! [remote_app_issue]
238 init.label = "Remote app example"; /* shown to the primary's operator */
239 /* Defaults: valid 24 h, one hub. Keep both as small as your flow allows. */
241 grant.app_tag = GRANT_TAG;
242 init.header.next = &grant.header;
243
244 QarMemberHubInvitation* invitation = NULL;
245 QarResult created = qar_runtime_create_member_hub_invitation(runtime, &init, NULL, &invitation);
246 report("qar_runtime_create_member_hub_invitation", created);
248 {
249 fprintf(stderr, "Ask the operator to allow this app to bring in member hubs.\n");
250 }
251
252 size_t size = 0;
253 uint8_t* bytes = NULL;
254 if(qar_result_is_success(created))
255 {
257 if(qar_result_is_success(sized) && size > 0 && size <= MAX_INVITATION_BYTES)
258 {
259 bytes = (uint8_t*)calloc(1, size);
260 }
261 else
262 {
263 report("qar_member_hub_invitation_serialized_size", sized);
264 }
265 }
266 size_t written = 0;
267 if(bytes != NULL)
268 {
269 QarResult serialized =
270 qar_member_hub_invitation_serialize(invitation, bytes, size, &written);
271 report("qar_member_hub_invitation_serialize", serialized); /* never the bytes */
272 /* stdout stands in for your channel to the other PC. */
273 if(qar_result_is_success(serialized) && set_binary(stdout)
274 && fwrite(bytes, 1, written, stdout) == written && fflush(stdout) == 0)
275 {
276 exit_code = 0;
277 }
278 else
279 {
280 fprintf(stderr, "The invitation was not handed over.\n");
281 }
282 memset(bytes, 0, size); /* clear the secret before freeing it */
283 free(bytes);
284 }
285 qar_member_hub_invitation_handle_destroy(invitation); /* NULL is ignored */
286 //! [remote_app_issue]
287
289 return exit_code;
290}
291
292/* Reads the whole invitation from stdin into `buffer`. */
293static bool
294read_invitation(uint8_t* buffer, size_t buffer_size, size_t* out_size)
295{
296 if(!set_binary(stdin))
297 {
298 return false;
299 }
300 *out_size = fread(buffer, 1, buffer_size, stdin);
301 if(ferror(stdin) || *out_size == 0 || *out_size == buffer_size)
302 {
303 return false; /* nothing, or more than an invitation can be */
304 }
305 return true;
306}
307
308/* Role "join", on the other PC, first start. */
309static int
310join(QarRuntime* runtime)
311{
312 int exit_code = 1;
313 uint8_t* bytes = (uint8_t*)calloc(1, MAX_INVITATION_BYTES + 1);
314 size_t size = 0;
315 QarMemberHubInvitation* invitation = NULL;
317 const bool got_bytes = bytes != NULL && read_invitation(bytes, MAX_INVITATION_BYTES + 1, &size);
318 if(got_bytes)
319 {
320 /* Checks the format only; the signature is checked by the join. */
321 parsed = qar_member_hub_invitation_deserialize(bytes, size, &invitation);
322 report("qar_member_hub_invitation_deserialize", parsed);
323 }
324 else
325 {
326 fprintf(stderr, "No invitation on stdin.\n");
327 }
328 if(bytes != NULL)
329 {
330 memset(bytes, 0, MAX_INVITATION_BYTES + 1); /* the handle keeps its own copy */
331 free(bytes);
332 }
333 if(!got_bytes || qar_result_is_error(parsed))
334 {
335 return 1;
336 }
337
338 //! [remote_app_join]
340 join_init.invitation = invitation; /* borrowed for the call */
341 QarResult joined_hub =
342 qar_runtime_join_hub_as_member(runtime, &join_init, on_progress, NULL, NULL);
343 report("qar_runtime_join_hub_as_member", joined_hub);
345 {
346 fprintf(stderr, "The join is committed; run join again once the hub is back.\n");
347 }
348 //! [remote_app_join]
349
350 if(qar_result_is_success(joined_hub))
351 {
352 //! [remote_app_redeem]
353 QarSession* session = NULL;
354 QarResult joined = onboard_local(runtime, NULL, invitation, &session);
355 report("qar_runtime_onboard (remote app)", joined);
357 {
358 fprintf(stderr, "The grant already admitted another app, or it was revoked.\n");
359 }
360 //! [remote_app_redeem]
361 if(qar_result_is_success(joined))
362 {
363 exit_code = example_print_own_peer_id("Remote app", session) ? 0 : 1;
365 }
366 }
368 return exit_code;
369}
370
371/* Role "start", on the other PC, every later start. */
372static int
373start(QarRuntime* runtime)
374{
375 //! [remote_app_start]
376 QarSession* session = NULL;
377 /* No tag needed: the primary remembers a granted app under the grant's
378 * tag, whatever tag the app sends. */
379 QarResult joined = onboard_local(runtime, NULL, NULL, &session);
380 report("qar_runtime_onboard (remote app)", joined);
381 //! [remote_app_start]
382 if(qar_result_is_error(joined))
383 {
384 return 1;
385 }
386 const int exit_code = example_print_own_peer_id("Remote app", session) ? 0 : 1;
388 return exit_code;
389}
390
391int
392main(int argc, char** argv)
393{
394 const char* role = argc >= 2 ? argv[1] : "";
395 if(strcmp(role, "issue") != 0 && strcmp(role, "join") != 0 && strcmp(role, "start") != 0)
396 {
397 fprintf(
398 stderr, "Usage: %s issue|join|start\n", argc >= 1 ? argv[0] : "remote_app_onboarding"
399 );
400 return 1;
401 }
402
403 if(!qar_library_load(NULL))
404 {
405 fprintf(stderr, "QAROS is not installed on this PC.\n");
406 return 2;
407 }
409 library_init.enable_console_logging = false; /* keep stdout for the invitation */
410 QarResult initialized = qar_library_init(&library_init);
411 if(qar_result_is_error(initialized))
412 {
413 report("qar_library_init", initialized);
415 return 1;
416 }
417
418 int exit_code = 1;
420 QarRuntime* runtime = NULL;
421 QarResult created = qar_runtime_create(&runtime_init, &runtime);
422 report("qar_runtime_create", created);
423 if(qar_result_is_success(created))
424 {
425 if(strcmp(role, "issue") == 0)
426 {
427 exit_code = issue(runtime);
428 }
429 else if(strcmp(role, "join") == 0)
430 {
431 exit_code = join(runtime);
432 }
433 else
434 {
435 exit_code = start(runtime);
436 }
438 }
439
440 report("qar_library_destroy", qar_library_destroy());
442 return exit_code;
443}

Generated via doxygen2docusaurus 2.2.1 by Doxygen 1.9.8.