Debian Codegraph @

src/mbimcli/mbimcli-intel-mutual-authentication.c

raw
1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* SPDX-License-Identifier: GPL-2.0-or-later */
3 /*
4 * mbimcli -- Command line interface to control MBIM devices
5 *
6 * Copyright (C) 2022 Intel Corporation
7 */
8
9 #include "config.h"
10
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <locale.h>
14 #include <string.h>
15 #include <errno.h>
16
17 #include <glib.h>
18 #include <gio/gio.h>
19
20 #include <libmbim-glib.h>
21
22 #include "mbim-common.h"
23 #include "mbimcli.h"
24 #include "mbimcli-helpers.h"
25
26 /* Context */
27 typedef struct {
28 MbimDevice *device;
29 GCancellable *cancellable;
30 } Context;
31 static Context *ctx;
32
33 /* Options */
34 static gboolean query_fcc_lock_flag;
35 static gchar *set_fcc_lock_str;
36
37 static GOptionEntry entries[] = {
38 { "intel-query-fcc-lock", 0, 0, G_OPTION_ARG_NONE, &query_fcc_lock_flag,
39 "Query FCC lock information",
40 NULL
41 },
42 { "intel-set-fcc-lock", 0, 0, G_OPTION_ARG_STRING, &set_fcc_lock_str,
43 "Set FCC lock information",
44 "[(ResponsePresent),(Response)]"
45 },
46 { NULL, 0, 0, 0, NULL, NULL, NULL }
47 };
48
49 GOptionGroup *
50 mbimcli_intel_mutual_authentication_get_option_group (void)
51 {
52 GOptionGroup *group;
53
54 group = g_option_group_new ("intel-mutual-authentication",
55 "Intel mutual authentication Service options:",
56 "Show Intel mutual authentication Service options",
57 NULL,
58 NULL);
59 g_option_group_add_entries (group, entries);
60
61 return group;
62 }
63
64 gboolean
65 mbimcli_intel_mutual_authentication_options_enabled (void)
66 {
67 static guint n_actions = 0;
68 static gboolean checked = FALSE;
69
70 if (checked)
71 return !!n_actions;
72
73 n_actions = (query_fcc_lock_flag +
74 !!set_fcc_lock_str);
75
76 if (n_actions > 1) {
77 g_printerr ("error: too many Intel mutual Authentication actions requested\n");
78 exit (EXIT_FAILURE);
79 }
80
81 checked = TRUE;
82 return !!n_actions;
83 }
84
85 static void
86 context_free (Context *context)
87 {
88 if (!context)
89 return;
90
91 if (context->cancellable)
92 g_object_unref (context->cancellable);
93 if (context->device)
94 g_object_unref (context->device);
95 g_slice_free (Context, context);
96 }
97
98 static void
99 shutdown (gboolean operation_status)
100 {
101 /* Cleanup context and finish async operation */
102 context_free (ctx);
103 mbimcli_async_operation_done (operation_status);
104 }
105
106 static void
107 query_fcc_lock_ready (MbimDevice *device,
108 GAsyncResult *res,
109 gpointer user_data)
110 {
111 MbimMessage *response = NULL;
112 GError *error = NULL;
113 gboolean challenge_present = FALSE;
114 guint32 challenge = 0;
115
116 response = mbim_device_command_finish (device, res, &error);
117 if (!response || !mbim_message_response_get_result (response, MBIM_MESSAGE_TYPE_COMMAND_DONE, &error)) {
118 g_printerr ("error: operation failed: %s\n", error->message);
119 shutdown (FALSE);
120 return;
121 }
122
123 if (!mbim_message_intel_mutual_authentication_fcc_lock_response_parse (response,
124 &challenge_present,
125 &challenge,
126 &error)) {
127 g_printerr ("error: couldn't parse response message: %s\n", error->message);
128 shutdown (FALSE);
129 return;
130 }
131
132 g_print ("FCC lock status: %s\n", challenge_present ? "locked" : "unlocked");
133 if (challenge_present)
134 g_print ("\tChallenge: %u\n", challenge);
135
136 shutdown (TRUE);
137 }
138
139 void
140 mbimcli_intel_mutual_authentication_run (MbimDevice *device,
141 GCancellable *cancellable)
142 {
143 g_autoptr(MbimMessage) request = NULL;
144
145 /* Initialize context */
146 ctx = g_slice_new (Context);
147 ctx->device = g_object_ref (device);
148 ctx->cancellable = cancellable ? g_object_ref (cancellable) : NULL;
149
150 /* Query FCC lock information */
151 if (query_fcc_lock_flag) {
152 g_debug ("Asynchronously querying FCC lock information...");
153
154 request = mbim_message_intel_mutual_authentication_fcc_lock_query_new (NULL);
155 mbim_device_command (ctx->device,
156 request,
157 10,
158 ctx->cancellable,
159 (GAsyncReadyCallback)query_fcc_lock_ready,
160 NULL);
161 return;
162 }
163
164 /* Set FCC lock information */
165 if (set_fcc_lock_str) {
166 gboolean response_present = FALSE;
167 guint32 response = 0;
168 g_auto(GStrv) split = NULL;
169
170 split = g_strsplit (set_fcc_lock_str, ",", -1);
171
172 if (g_strv_length (split) < 2) {
173 g_printerr ("error: couldn't parse input arguments, missing arguments\n");
174 shutdown (FALSE);
175 return;
176 }
177
178 if (g_strv_length (split) > 2) {
179 g_printerr ("error: couldn't parse input arguments, too many arguments\n");
180 shutdown (FALSE);
181 return;
182 }
183
184 if (!mbimcli_read_boolean_from_string (split[0], &response_present)) {
185 g_printerr ("error: couldn't parse input, wrong value given\n");
186 shutdown (FALSE);
187 return;
188 }
189
190 if (!mbimcli_read_uint_from_string (split[1], &response)) {
191 g_printerr ("error: couldn't parse input, wrong value given\n");
192 shutdown (FALSE);
193 return;
194 }
195
196 request = mbim_message_intel_mutual_authentication_fcc_lock_set_new (response_present, response, NULL);
197 mbim_device_command (ctx->device,
198 request,
199 10,
200 ctx->cancellable,
201 (GAsyncReadyCallback)query_fcc_lock_ready,
202 NULL);
203 return;
204 }
205 g_warn_if_reached ();
206 }