agent.c (12973B)
1 /* 2 * 3 * Wireless daemon for Linux 4 * 5 * Copyright (C) 2017-2019 Intel Corporation. All rights reserved. 6 * 7 * This library is free software; you can redistribute it and/or 8 * modify it under the terms of the GNU Lesser General Public 9 * License as published by the Free Software Foundation; either 10 * version 2.1 of the License, or (at your option) any later version. 11 * 12 * This library is distributed in the hope that it will be useful, 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 15 * Lesser General Public License for more details. 16 * 17 * You should have received a copy of the GNU Lesser General Public 18 * License along with this library; if not, write to the Free Software 19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 20 * 21 */ 22 23 #ifdef HAVE_CONFIG_H 24 #include <config.h> 25 #endif 26 27 #include <ell/ell.h> 28 29 #include "client/agent.h" 30 #include "client/dbus-proxy.h" 31 #include "client/display.h" 32 #include "client/command.h" 33 34 #define IWD_AGENT_INTERFACE "net.connman.iwd.Agent" 35 36 #define PROMPT_USERNAME "Username:" 37 #define PROMPT_PASSWORD "Password:" 38 #define PROMPT_PASSPHRASE "Passphrase:" 39 40 enum AGENT_OP_TYPE { 41 AGENT_OP_TYPE_PASSPHRASE = 1, 42 AGENT_OP_TYPE_PASSWORD, 43 AGENT_OP_TYPE_UNAME_PASSWORD, 44 }; 45 46 static struct l_dbus_message *pending_message; 47 static struct pending_op { 48 enum AGENT_OP_TYPE type; 49 char *last_label; 50 struct l_queue *saved_input; 51 } pending_op; 52 53 static struct l_dbus_message *agent_reply_canceled( 54 struct l_dbus_message *message, 55 const char *text) 56 { 57 return l_dbus_message_new_error(message, 58 IWD_AGENT_INTERFACE ".Error.Canceled", 59 "Error: %s", text); 60 } 61 62 static struct l_dbus_message *agent_reply_failed(struct l_dbus_message *message, 63 const char *text) 64 { 65 return l_dbus_message_new_error(message, 66 IWD_AGENT_INTERFACE ".Error.Failed", 67 "Error: %s", text); 68 } 69 70 static struct l_dbus_message *agent_error(struct l_dbus_message *message, 71 const char *text) 72 { 73 display_error(text); 74 75 if (!command_is_interactive_mode()) { 76 command_set_exit_status(EXIT_FAILURE); 77 78 l_main_quit(); 79 } 80 81 return agent_reply_canceled(message, text); 82 } 83 84 static struct l_dbus_message *release_method_call(struct l_dbus *dbus, 85 struct l_dbus_message *message, 86 void *user_data) 87 { 88 display_agent_prompt_release(pending_op.last_label); 89 90 l_dbus_message_unref(pending_message); 91 pending_message = NULL; 92 93 return l_dbus_message_new_method_return(message); 94 } 95 96 static struct l_dbus_message *request_passphrase_command_option( 97 struct l_dbus_message *message) 98 { 99 struct l_dbus_message *reply; 100 const char *passphrase = NULL; 101 102 command_option_get(COMMAND_OPTION_PASSPHRASE, &passphrase); 103 if (!passphrase) 104 return NULL; 105 106 reply = l_dbus_message_new_method_return(message); 107 l_dbus_message_set_arguments(reply, "s", passphrase); 108 109 return reply; 110 } 111 112 static struct l_dbus_message *request_passphrase_method_call( 113 struct l_dbus *dbus, 114 struct l_dbus_message *message, 115 void *user_data) 116 { 117 const struct proxy_interface *proxy; 118 struct l_dbus_message *reply; 119 const char *path; 120 121 reply = request_passphrase_command_option(message); 122 if (reply) 123 return reply; 124 125 if (command_option_get(COMMAND_OPTION_DONTASK, NULL)) 126 return agent_error(message, "No passphrase is provided as " 127 "'--"COMMAND_OPTION_PASSPHRASE"' " 128 "command-line option.\n"); 129 130 if (!l_dbus_message_get_arguments(message, "o", &path)) 131 return agent_reply_failed(message, "Invalid argument"); 132 133 if (!path) 134 return agent_reply_failed(message, "Invalid argument"); 135 136 proxy = proxy_interface_find(IWD_NETWORK_INTERFACE, path); 137 if (!proxy) 138 return agent_reply_failed(message, "Invalid network object"); 139 140 display("Type the network passphrase for %s.\n", 141 proxy_interface_get_identity_str(proxy)); 142 display_agent_prompt(PROMPT_PASSPHRASE, true); 143 144 pending_op.type = AGENT_OP_TYPE_PASSPHRASE; 145 pending_op.last_label = PROMPT_PASSPHRASE; 146 147 pending_message = l_dbus_message_ref(message); 148 149 return NULL; 150 } 151 152 static struct l_dbus_message *request_private_key_passphrase_method_call( 153 struct l_dbus *dbus, 154 struct l_dbus_message *message, 155 void *user_data) 156 { 157 const struct proxy_interface *proxy; 158 struct l_dbus_message *reply; 159 const char *path; 160 161 reply = request_passphrase_command_option(message); 162 if (reply) 163 return reply; 164 165 if (command_option_get(COMMAND_OPTION_DONTASK, NULL)) 166 return agent_error(message, "No passphrase is provided as " 167 "'--"COMMAND_OPTION_PASSPHRASE"' " 168 "command-line option.\n"); 169 170 if (!l_dbus_message_get_arguments(message, "o", &path)) 171 return agent_reply_failed(message, "Invalid argument"); 172 173 if (!path) 174 return agent_reply_failed(message, "Invalid argument"); 175 176 proxy = proxy_interface_find(IWD_NETWORK_INTERFACE, path); 177 if (!proxy) 178 return agent_reply_failed(message, "Invalid network object"); 179 180 display("Type the passphrase for the network encrypted private key for " 181 "%s.\n", proxy_interface_get_identity_str(proxy)); 182 display_agent_prompt(PROMPT_PASSPHRASE, true); 183 184 pending_op.type = AGENT_OP_TYPE_PASSPHRASE; 185 pending_op.last_label = PROMPT_PASSPHRASE; 186 187 pending_message = l_dbus_message_ref(message); 188 189 return NULL; 190 } 191 192 static struct l_dbus_message *request_username_and_password_command_option( 193 struct l_dbus_message *message) 194 { 195 struct l_dbus_message *reply; 196 const char *username = NULL; 197 const char *password = NULL; 198 199 command_option_get(COMMAND_OPTION_USERNAME, &username); 200 if (!username) 201 return NULL; 202 203 command_option_get(COMMAND_OPTION_PASSWORD, &password); 204 if (!password) 205 return NULL; 206 207 reply = l_dbus_message_new_method_return(message); 208 l_dbus_message_set_arguments(reply, "ss", username, password); 209 210 return reply; 211 } 212 213 static struct l_dbus_message *request_username_and_password_method_call( 214 struct l_dbus *dbus, 215 struct l_dbus_message *message, 216 void *user_data) 217 { 218 const struct proxy_interface *proxy; 219 struct l_dbus_message *reply; 220 const char *path; 221 222 reply = request_username_and_password_command_option(message); 223 if (reply) 224 return reply; 225 226 if (command_option_get(COMMAND_OPTION_DONTASK, NULL)) 227 return agent_error(message, "No username or password is " 228 "provided as " 229 "'--"COMMAND_OPTION_USERNAME"' or " 230 "'--"COMMAND_OPTION_PASSWORD"' " 231 "command-line option.\n"); 232 233 if (!l_dbus_message_get_arguments(message, "o", &path)) 234 return agent_reply_failed(message, "Invalid argument"); 235 236 if (!path) 237 return agent_reply_failed(message, "Invalid argument"); 238 239 proxy = proxy_interface_find(IWD_NETWORK_INTERFACE, path); 240 if (!proxy) 241 return agent_reply_failed(message, "Invalid network object"); 242 243 display("Type the network credentials for %s.\n", 244 proxy_interface_get_identity_str(proxy)); 245 display_agent_prompt(PROMPT_USERNAME, false); 246 247 pending_op.type = AGENT_OP_TYPE_UNAME_PASSWORD; 248 pending_op.last_label = PROMPT_USERNAME; 249 250 pending_message = l_dbus_message_ref(message); 251 252 return NULL; 253 } 254 255 static struct l_dbus_message *request_user_password_command_option( 256 struct l_dbus_message *message) 257 { 258 struct l_dbus_message *reply; 259 const char *password = NULL; 260 261 command_option_get(COMMAND_OPTION_PASSWORD, &password); 262 if (!password) 263 return NULL; 264 265 reply = l_dbus_message_new_method_return(message); 266 l_dbus_message_set_arguments(reply, "s", password); 267 268 return reply; 269 } 270 271 static struct l_dbus_message *request_user_password_method_call( 272 struct l_dbus *dbus, 273 struct l_dbus_message *message, 274 void *user_data) 275 { 276 const struct proxy_interface *proxy; 277 struct l_dbus_message *reply; 278 const char *path; 279 const char *username; 280 char *username_prompt; 281 282 reply = request_user_password_command_option(message); 283 if (reply) 284 return reply; 285 286 if (command_option_get(COMMAND_OPTION_DONTASK, NULL)) 287 return agent_error(message, "No password is provided as " 288 "'--"COMMAND_OPTION_PASSWORD"' " 289 "command-line option.\n"); 290 291 if (!l_dbus_message_get_arguments(message, "os", &path, &username)) 292 return agent_reply_failed(message, "Invalid arguments"); 293 294 if (!path || !username) 295 return agent_reply_failed(message, "Invalid argument"); 296 297 proxy = proxy_interface_find(IWD_NETWORK_INTERFACE, path); 298 if (!proxy) 299 return agent_reply_failed(message, "Invalid network object"); 300 301 display("Type the network password for %s.\n", 302 proxy_interface_get_identity_str(proxy)); 303 304 username_prompt = l_strdup_printf(COLOR_BLUE(PROMPT_USERNAME " ") 305 "%s\n", username); 306 display("%s", username_prompt); 307 l_free(username_prompt); 308 309 display_agent_prompt(PROMPT_PASSWORD, true); 310 311 pending_op.type = AGENT_OP_TYPE_PASSWORD; 312 pending_op.last_label = PROMPT_PASSWORD; 313 314 pending_message = l_dbus_message_ref(message); 315 316 return NULL; 317 } 318 319 static struct l_dbus_message *cancel_method_call(struct l_dbus *dbus, 320 struct l_dbus_message *message, 321 void *user_data) 322 { 323 display_agent_prompt_release(pending_op.last_label); 324 325 l_dbus_message_unref(pending_message); 326 pending_message = NULL; 327 328 return l_dbus_message_new_method_return(message); 329 } 330 331 static void setup_agent_interface(struct l_dbus_interface *interface) 332 { 333 l_dbus_interface_method(interface, "Release", 0, release_method_call, 334 "", ""); 335 336 l_dbus_interface_method(interface, "RequestPassphrase", 0, 337 request_passphrase_method_call, "s", "o", 338 "passphrase", "network"); 339 340 l_dbus_interface_method(interface, "RequestPrivateKeyPassphrase", 0, 341 request_private_key_passphrase_method_call, 342 "s", "o", "private_key_path", "network"); 343 344 l_dbus_interface_method(interface, "RequestUserNameAndPassword", 0, 345 request_username_and_password_method_call, 346 "ss", "o", "user", "password", "network"); 347 348 l_dbus_interface_method(interface, "RequestUserPassword", 0, 349 request_user_password_method_call, "s", "os", 350 "password", "network", "user"); 351 352 l_dbus_interface_method(interface, "Cancel", 0, cancel_method_call, 353 "", "s", "reason"); 354 } 355 356 static void agent_send_reply(struct l_dbus_message *reply) 357 { 358 l_dbus_send(dbus_get_bus(), reply); 359 360 l_dbus_message_unref(pending_message); 361 pending_message = NULL; 362 } 363 364 static void process_input_username_password(const char *prompt) 365 { 366 struct l_dbus_message *reply; 367 char *username; 368 369 if (!prompt || !strlen(prompt)) { 370 reply = agent_reply_canceled(pending_message, 371 "Canceled by user"); 372 373 l_queue_clear(pending_op.saved_input, l_free); 374 375 goto send_reply; 376 } 377 378 if (l_queue_isempty(pending_op.saved_input)) { 379 /* received username */ 380 l_queue_push_tail(pending_op.saved_input, l_strdup(prompt)); 381 382 display_agent_prompt(PROMPT_PASSWORD, true); 383 pending_op.last_label = PROMPT_PASSWORD; 384 385 return; 386 } 387 388 username = l_queue_pop_head(pending_op.saved_input); 389 390 reply = l_dbus_message_new_method_return(pending_message); 391 l_dbus_message_set_arguments(reply, "ss", username, prompt); 392 393 l_free(username); 394 395 send_reply: 396 agent_send_reply(reply); 397 } 398 399 static void process_input_passphrase(const char *prompt) 400 { 401 struct l_dbus_message *reply; 402 403 if (!prompt || !strlen(prompt)) { 404 reply = agent_reply_canceled(pending_message, 405 "Canceled by user"); 406 goto send_reply; 407 } 408 409 reply = l_dbus_message_new_method_return(pending_message); 410 l_dbus_message_set_arguments(reply, "s", prompt); 411 412 send_reply: 413 agent_send_reply(reply); 414 } 415 416 static void process_input_password(const char *prompt) 417 { 418 struct l_dbus_message *reply; 419 420 if (!prompt || !strlen(prompt)) { 421 reply = agent_reply_canceled(pending_message, 422 "Canceled by user"); 423 goto send_reply; 424 } 425 426 reply = l_dbus_message_new_method_return(pending_message); 427 l_dbus_message_set_arguments(reply, "s", prompt); 428 429 send_reply: 430 agent_send_reply(reply); 431 } 432 433 bool agent_prompt(const char *prompt) 434 { 435 if (!pending_message) 436 return false; 437 438 display_agent_prompt_release(pending_op.last_label); 439 440 switch (pending_op.type) { 441 case AGENT_OP_TYPE_UNAME_PASSWORD: 442 process_input_username_password(prompt); 443 break; 444 case AGENT_OP_TYPE_PASSPHRASE: 445 process_input_passphrase(prompt); 446 break; 447 case AGENT_OP_TYPE_PASSWORD: 448 process_input_password(prompt); 449 break; 450 } 451 452 return true; 453 } 454 455 bool agent_init(const char *path) 456 { 457 struct l_dbus *dbus = dbus_get_bus(); 458 459 if (!l_dbus_register_interface(dbus, IWD_AGENT_INTERFACE, 460 setup_agent_interface, NULL, false)) { 461 l_info("Unable to register %s interface", IWD_AGENT_INTERFACE); 462 463 return false; 464 } 465 466 if (!l_dbus_object_add_interface(dbus, path, IWD_AGENT_INTERFACE, 467 NULL)) { 468 l_info("Unable to register the agent manager object on '%s'", 469 path); 470 l_dbus_unregister_interface(dbus, IWD_AGENT_INTERFACE); 471 472 return false; 473 } 474 475 pending_op.saved_input = l_queue_new(); 476 477 return true; 478 } 479 480 bool agent_exit(const char *path) 481 { 482 struct l_dbus *dbus = dbus_get_bus(); 483 484 if (pending_message) { 485 l_dbus_message_unref(pending_message); 486 display_agent_prompt_release(pending_op.last_label); 487 } 488 489 l_queue_destroy(pending_op.saved_input, l_free); 490 491 l_dbus_unregister_object(dbus, path); 492 l_dbus_unregister_interface(dbus, IWD_AGENT_INTERFACE); 493 494 return true; 495 }