summaryrefslogtreecommitdiffstats
path: root/sway/ipc-server.c
diff options
context:
space:
mode:
Diffstat (limited to 'sway/ipc-server.c')
-rw-r--r--sway/ipc-server.c60
1 files changed, 36 insertions, 24 deletions
diff --git a/sway/ipc-server.c b/sway/ipc-server.c
index 241fe742..3e510c2e 100644
--- a/sway/ipc-server.c
+++ b/sway/ipc-server.c
@@ -263,7 +263,10 @@ static void ipc_send_event(const char *json_string, enum ipc_command_type event)
263 client->current_command = event; 263 client->current_command = event;
264 if (!ipc_send_reply(client, json_string, (uint32_t) strlen(json_string))) { 264 if (!ipc_send_reply(client, json_string, (uint32_t) strlen(json_string))) {
265 wlr_log_errno(L_INFO, "Unable to send reply to IPC client"); 265 wlr_log_errno(L_INFO, "Unable to send reply to IPC client");
266 ipc_client_disconnect(client); 266 /* ipc_send_reply destroys client on error, which also
267 * removes it from the list, so we need to process
268 * current index again */
269 i--;
267 } 270 }
268 } 271 }
269} 272}
@@ -383,9 +386,7 @@ void ipc_client_disconnect(struct ipc_client *client) {
383 return; 386 return;
384 } 387 }
385 388
386 if (client->fd != -1) { 389 shutdown(client->fd, SHUT_RDWR);
387 shutdown(client->fd, SHUT_RDWR);
388 }
389 390
390 wlr_log(L_INFO, "IPC Client %d disconnected", client->fd); 391 wlr_log(L_INFO, "IPC Client %d disconnected", client->fd);
391 wl_event_source_remove(client->event_source); 392 wl_event_source_remove(client->event_source);
@@ -465,8 +466,7 @@ void ipc_client_handle_command(struct ipc_client *client) {
465 } 466 }
466 buf[client->payload_length] = '\0'; 467 buf[client->payload_length] = '\0';
467 468
468 const char *error_denied = "{ \"success\": false, \"error\": \"Permission denied\" }"; 469 bool client_valid = true;
469
470 switch (client->current_command) { 470 switch (client->current_command) {
471 case IPC_COMMAND: 471 case IPC_COMMAND:
472 { 472 {
@@ -474,7 +474,7 @@ void ipc_client_handle_command(struct ipc_client *client) {
474 const char *json = cmd_results_to_json(results); 474 const char *json = cmd_results_to_json(results);
475 char reply[256]; 475 char reply[256];
476 int length = snprintf(reply, sizeof(reply), "%s", json); 476 int length = snprintf(reply, sizeof(reply), "%s", json);
477 ipc_send_reply(client, reply, (uint32_t) length); 477 client_valid = ipc_send_reply(client, reply, (uint32_t)length);
478 free_cmd_results(results); 478 free_cmd_results(results);
479 goto exit_cleanup; 479 goto exit_cleanup;
480 } 480 }
@@ -497,7 +497,8 @@ void ipc_client_handle_command(struct ipc_client *client) {
497 } 497 }
498 } 498 }
499 const char *json_string = json_object_to_json_string(outputs); 499 const char *json_string = json_object_to_json_string(outputs);
500 ipc_send_reply(client, json_string, (uint32_t) strlen(json_string)); 500 client_valid =
501 ipc_send_reply(client, json_string, (uint32_t)strlen(json_string));
501 json_object_put(outputs); // free 502 json_object_put(outputs); // free
502 goto exit_cleanup; 503 goto exit_cleanup;
503 } 504 }
@@ -508,7 +509,8 @@ void ipc_client_handle_command(struct ipc_client *client) {
508 container_for_each_descendant_dfs(&root_container, 509 container_for_each_descendant_dfs(&root_container,
509 ipc_get_workspaces_callback, workspaces); 510 ipc_get_workspaces_callback, workspaces);
510 const char *json_string = json_object_to_json_string(workspaces); 511 const char *json_string = json_object_to_json_string(workspaces);
511 ipc_send_reply(client, json_string, (uint32_t) strlen(json_string)); 512 client_valid =
513 ipc_send_reply(client, json_string, (uint32_t)strlen(json_string));
512 json_object_put(workspaces); // free 514 json_object_put(workspaces); // free
513 goto exit_cleanup; 515 goto exit_cleanup;
514 } 516 }
@@ -518,7 +520,7 @@ void ipc_client_handle_command(struct ipc_client *client) {
518 // TODO: Check if they're permitted to use these events 520 // TODO: Check if they're permitted to use these events
519 struct json_object *request = json_tokener_parse(buf); 521 struct json_object *request = json_tokener_parse(buf);
520 if (request == NULL) { 522 if (request == NULL) {
521 ipc_send_reply(client, "{\"success\": false}", 18); 523 client_valid = ipc_send_reply(client, "{\"success\": false}", 18);
522 wlr_log_errno(L_INFO, "Failed to read request"); 524 wlr_log_errno(L_INFO, "Failed to read request");
523 goto exit_cleanup; 525 goto exit_cleanup;
524 } 526 }
@@ -539,7 +541,8 @@ void ipc_client_handle_command(struct ipc_client *client) {
539 } else if (strcmp(event_type, "binding") == 0) { 541 } else if (strcmp(event_type, "binding") == 0) {
540 client->subscribed_events |= event_mask(IPC_EVENT_BINDING); 542 client->subscribed_events |= event_mask(IPC_EVENT_BINDING);
541 } else { 543 } else {
542 ipc_send_reply(client, "{\"success\": false}", 18); 544 client_valid =
545 ipc_send_reply(client, "{\"success\": false}", 18);
543 json_object_put(request); 546 json_object_put(request);
544 wlr_log_errno(L_INFO, "Failed to parse request"); 547 wlr_log_errno(L_INFO, "Failed to parse request");
545 goto exit_cleanup; 548 goto exit_cleanup;
@@ -547,7 +550,7 @@ void ipc_client_handle_command(struct ipc_client *client) {
547 } 550 }
548 551
549 json_object_put(request); 552 json_object_put(request);
550 ipc_send_reply(client, "{\"success\": true}", 17); 553 client_valid = ipc_send_reply(client, "{\"success\": true}", 17);
551 goto exit_cleanup; 554 goto exit_cleanup;
552 } 555 }
553 556
@@ -559,7 +562,8 @@ void ipc_client_handle_command(struct ipc_client *client) {
559 json_object_array_add(inputs, ipc_json_describe_input(device)); 562 json_object_array_add(inputs, ipc_json_describe_input(device));
560 } 563 }
561 const char *json_string = json_object_to_json_string(inputs); 564 const char *json_string = json_object_to_json_string(inputs);
562 ipc_send_reply(client, json_string, (uint32_t)strlen(json_string)); 565 client_valid =
566 ipc_send_reply(client, json_string, (uint32_t)strlen(json_string));
563 json_object_put(inputs); // free 567 json_object_put(inputs); // free
564 goto exit_cleanup; 568 goto exit_cleanup;
565 } 569 }
@@ -572,7 +576,8 @@ void ipc_client_handle_command(struct ipc_client *client) {
572 json_object_array_add(seats, ipc_json_describe_seat(seat)); 576 json_object_array_add(seats, ipc_json_describe_seat(seat));
573 } 577 }
574 const char *json_string = json_object_to_json_string(seats); 578 const char *json_string = json_object_to_json_string(seats);
575 ipc_send_reply(client, json_string, (uint32_t)strlen(json_string)); 579 client_valid =
580 ipc_send_reply(client, json_string, (uint32_t)strlen(json_string));
576 json_object_put(seats); // free 581 json_object_put(seats); // free
577 goto exit_cleanup; 582 goto exit_cleanup;
578 } 583 }
@@ -582,7 +587,8 @@ void ipc_client_handle_command(struct ipc_client *client) {
582 json_object *tree = 587 json_object *tree =
583 ipc_json_describe_container_recursive(&root_container); 588 ipc_json_describe_container_recursive(&root_container);
584 const char *json_string = json_object_to_json_string(tree); 589 const char *json_string = json_object_to_json_string(tree);
585 ipc_send_reply(client, json_string, (uint32_t) strlen(json_string)); 590 client_valid =
591 ipc_send_reply(client, json_string, (uint32_t) strlen(json_string));
586 json_object_put(tree); 592 json_object_put(tree);
587 goto exit_cleanup; 593 goto exit_cleanup;
588 } 594 }
@@ -593,7 +599,8 @@ void ipc_client_handle_command(struct ipc_client *client) {
593 container_descendants(&root_container, C_VIEW, ipc_get_marks_callback, 599 container_descendants(&root_container, C_VIEW, ipc_get_marks_callback,
594 marks); 600 marks);
595 const char *json_string = json_object_to_json_string(marks); 601 const char *json_string = json_object_to_json_string(marks);
596 ipc_send_reply(client, json_string, (uint32_t)strlen(json_string)); 602 client_valid =
603 ipc_send_reply(client, json_string, (uint32_t)strlen(json_string));
597 json_object_put(marks); 604 json_object_put(marks);
598 goto exit_cleanup; 605 goto exit_cleanup;
599 } 606 }
@@ -602,7 +609,8 @@ void ipc_client_handle_command(struct ipc_client *client) {
602 { 609 {
603 json_object *version = ipc_json_get_version(); 610 json_object *version = ipc_json_get_version();
604 const char *json_string = json_object_to_json_string(version); 611 const char *json_string = json_object_to_json_string(version);
605 ipc_send_reply(client, json_string, (uint32_t)strlen(json_string)); 612 client_valid =
613 ipc_send_reply(client, json_string, (uint32_t)strlen(json_string));
606 json_object_put(version); // free 614 json_object_put(version); // free
607 goto exit_cleanup; 615 goto exit_cleanup;
608 } 616 }
@@ -617,7 +625,9 @@ void ipc_client_handle_command(struct ipc_client *client) {
617 json_object_array_add(bars, json_object_new_string(bar->id)); 625 json_object_array_add(bars, json_object_new_string(bar->id));
618 } 626 }
619 const char *json_string = json_object_to_json_string(bars); 627 const char *json_string = json_object_to_json_string(bars);
620 ipc_send_reply(client, json_string, (uint32_t)strlen(json_string)); 628 client_valid =
629 ipc_send_reply(client, json_string,
630 (uint32_t)strlen(json_string));
621 json_object_put(bars); // free 631 json_object_put(bars); // free
622 } else { 632 } else {
623 // Send particular bar's details 633 // Send particular bar's details
@@ -631,12 +641,15 @@ void ipc_client_handle_command(struct ipc_client *client) {
631 } 641 }
632 if (!bar) { 642 if (!bar) {
633 const char *error = "{ \"success\": false, \"error\": \"No bar with that ID\" }"; 643 const char *error = "{ \"success\": false, \"error\": \"No bar with that ID\" }";
634 ipc_send_reply(client, error, (uint32_t)strlen(error)); 644 client_valid =
645 ipc_send_reply(client, error, (uint32_t)strlen(error));
635 goto exit_cleanup; 646 goto exit_cleanup;
636 } 647 }
637 json_object *json = ipc_json_describe_bar_config(bar); 648 json_object *json = ipc_json_describe_bar_config(bar);
638 const char *json_string = json_object_to_json_string(json); 649 const char *json_string = json_object_to_json_string(json);
639 ipc_send_reply(client, json_string, (uint32_t)strlen(json_string)); 650 client_valid =
651 ipc_send_reply(client, json_string,
652 (uint32_t)strlen(json_string));
640 json_object_put(json); // free 653 json_object_put(json); // free
641 } 654 }
642 goto exit_cleanup; 655 goto exit_cleanup;
@@ -647,11 +660,10 @@ void ipc_client_handle_command(struct ipc_client *client) {
647 goto exit_cleanup; 660 goto exit_cleanup;
648 } 661 }
649 662
650 ipc_send_reply(client, error_denied, (uint32_t)strlen(error_denied));
651 wlr_log(L_DEBUG, "Denied IPC client access to %i", client->current_command);
652
653exit_cleanup: 663exit_cleanup:
654 client->payload_length = 0; 664 if (client_valid) {
665 client->payload_length = 0;
666 }
655 free(buf); 667 free(buf);
656 return; 668 return;
657} 669}