1 /* 2 * 3 * BlueZ - Bluetooth protocol stack for Linux 4 * 5 * Copyright (C) 2004-2009 Marcel Holtmann <marcel (at) holtmann.org> 6 * 7 * 8 * This program is free software; you can redistribute it and/or modify 9 * it under the terms of the GNU General Public License as published by 10 * the Free Software Foundation; either version 2 of the License, or 11 * (at your option) any later version. 12 * 13 * This program is distributed in the hope that it will be useful, 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 * GNU General Public License for more details. 17 * 18 * You should have received a copy of the GNU General Public License 19 * along with this program; if not, write to the Free Software 20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 21 * 22 */ 23 24 #ifdef HAVE_CONFIG_H 25 #include <config.h> 26 #endif 27 28 #include <ctype.h> 29 #include <dirent.h> 30 #include <errno.h> 31 #include <fcntl.h> 32 #include <stdio.h> 33 #include <stdlib.h> 34 #include <string.h> 35 #include <termios.h> 36 #include <unistd.h> 37 #include <arpa/inet.h> 38 #include <sys/param.h> 39 #include <sys/ioctl.h> 40 #include <sys/stat.h> 41 #include <sys/types.h> 42 #include <sys/un.h> 43 44 #include <bluetooth/bluetooth.h> 45 #include <bluetooth/hci.h> 46 #include <bluetooth/hci_lib.h> 47 #include <bluetooth/sdp.h> 48 #include <bluetooth/sdp_lib.h> 49 #include <bluetooth/rfcomm.h> 50 51 #include <glib.h> 52 #include <gdbus.h> 53 54 #include "../src/dbus-common.h" 55 #include "../src/adapter.h" 56 57 #include "logging.h" 58 #include "textfile.h" 59 60 #include "error.h" 61 #include "sdpd.h" 62 #include "glib-helper.h" 63 #include "btio.h" 64 #include "proxy.h" 65 66 #define SERIAL_PORT_NAME "spp" 67 #define SERIAL_PORT_UUID "00001101-0000-1000-8000-00805F9B34FB" 68 69 #define DIALUP_NET_NAME "dun" 70 #define DIALUP_NET_UUID "00001103-0000-1000-8000-00805F9B34FB" 71 72 #define SERIAL_PROXY_INTERFACE "org.bluez.SerialProxy" 73 #define SERIAL_MANAGER_INTERFACE "org.bluez.SerialProxyManager" 74 #define BUF_SIZE 1024 75 76 typedef enum { 77 TTY_PROXY, 78 UNIX_SOCKET_PROXY, 79 TCP_SOCKET_PROXY, 80 UNKNOWN_PROXY_TYPE = 0xFF 81 } proxy_type_t; 82 83 struct serial_adapter { 84 struct btd_adapter *btd_adapter; /* Adapter pointer */ 85 DBusConnection *conn; /* Adapter connection */ 86 GSList *proxies; /* Proxies list */ 87 }; 88 89 struct serial_proxy { 90 bdaddr_t src; /* Local address */ 91 bdaddr_t dst; /* Remote address */ 92 char *path; /* Proxy path */ 93 char *uuid128; /* UUID 128 */ 94 char *address; /* TTY or Unix socket name */ 95 char *owner; /* Application bus name */ 96 guint watch; /* Application watch */ 97 short int port; /* TCP port */ 98 proxy_type_t type; /* TTY or Unix socket */ 99 struct termios sys_ti; /* Default TTY setting */ 100 struct termios proxy_ti; /* Proxy TTY settings */ 101 uint8_t channel; /* RFCOMM channel */ 102 uint32_t record_id; /* Service record id */ 103 GIOChannel *io; /* Server listen */ 104 GIOChannel *rfcomm; /* Remote RFCOMM channel*/ 105 GIOChannel *local; /* Local channel: TTY or Unix socket */ 106 struct serial_adapter *adapter; /* Adapter pointer */ 107 }; 108 109 static GSList *adapters = NULL; 110 static int sk_counter = 0; 111 112 static void disable_proxy(struct serial_proxy *prx) 113 { 114 if (prx->rfcomm) { 115 g_io_channel_shutdown(prx->rfcomm, TRUE, NULL); 116 g_io_channel_unref(prx->rfcomm); 117 prx->rfcomm = NULL; 118 } 119 120 if (prx->local) { 121 g_io_channel_shutdown(prx->local, TRUE, NULL); 122 g_io_channel_unref(prx->local); 123 prx->local = NULL; 124 } 125 126 remove_record_from_server(prx->record_id); 127 prx->record_id = 0; 128 129 g_io_channel_unref(prx->io); 130 prx->io = NULL; 131 } 132 133 static void proxy_free(struct serial_proxy *prx) 134 { 135 g_free(prx->owner); 136 g_free(prx->path); 137 g_free(prx->address); 138 g_free(prx->uuid128); 139 g_free(prx); 140 } 141 142 static inline DBusMessage *does_not_exist(DBusMessage *msg, 143 const char *description) 144 { 145 return g_dbus_create_error(msg, ERROR_INTERFACE ".DoesNotExist", 146 description); 147 } 148 149 static inline DBusMessage *invalid_arguments(DBusMessage *msg, 150 const char *description) 151 { 152 return g_dbus_create_error(msg, ERROR_INTERFACE ".InvalidArguments", 153 description); 154 } 155 156 static inline DBusMessage *failed(DBusMessage *msg, const char *description) 157 { 158 return g_dbus_create_error(msg, ERROR_INTERFACE ".Failed", 159 description); 160 } 161 162 static void add_lang_attr(sdp_record_t *r) 163 { 164 sdp_lang_attr_t base_lang; 165 sdp_list_t *langs = 0; 166 167 /* UTF-8 MIBenum (http://www.iana.org/assignments/character-sets) */ 168 base_lang.code_ISO639 = (0x65 << 8) | 0x6e; 169 base_lang.encoding = 106; 170 base_lang.base_offset = SDP_PRIMARY_LANG_BASE; 171 langs = sdp_list_append(0, &base_lang); 172 sdp_set_lang_attr(r, langs); 173 sdp_list_free(langs, 0); 174 } 175 176 static sdp_record_t *proxy_record_new(const char *uuid128, uint8_t channel) 177 { 178 sdp_list_t *apseq, *aproto, *profiles, *proto[2], *root, *svclass_id; 179 uuid_t uuid, root_uuid, l2cap, rfcomm; 180 sdp_profile_desc_t profile; 181 sdp_record_t *record; 182 sdp_data_t *ch; 183 184 record = sdp_record_alloc(); 185 if (!record) 186 return NULL; 187 188 sdp_uuid16_create(&root_uuid, PUBLIC_BROWSE_GROUP); 189 root = sdp_list_append(NULL, &root_uuid); 190 sdp_set_browse_groups(record, root); 191 sdp_list_free(root, NULL); 192 193 bt_string2uuid(&uuid, uuid128); 194 sdp_uuid128_to_uuid(&uuid); 195 svclass_id = sdp_list_append(NULL, &uuid); 196 sdp_set_service_classes(record, svclass_id); 197 sdp_list_free(svclass_id, NULL); 198 199 sdp_uuid16_create(&profile.uuid, SERIAL_PORT_PROFILE_ID); 200 profile.version = 0x0100; 201 profiles = sdp_list_append(NULL, &profile); 202 sdp_set_profile_descs(record, profiles); 203 sdp_list_free(profiles, NULL); 204 205 sdp_uuid16_create(&l2cap, L2CAP_UUID); 206 proto[0] = sdp_list_append(NULL, &l2cap); 207 apseq = sdp_list_append(NULL, proto[0]); 208 209 sdp_uuid16_create(&rfcomm, RFCOMM_UUID); 210 proto[1] = sdp_list_append(NULL, &rfcomm); 211 ch = sdp_data_alloc(SDP_UINT8, &channel); 212 proto[1] = sdp_list_append(proto[1], ch); 213 apseq = sdp_list_append(apseq, proto[1]); 214 215 aproto = sdp_list_append(NULL, apseq); 216 sdp_set_access_protos(record, aproto); 217 218 add_lang_attr(record); 219 220 sdp_set_info_attr(record, "Serial Proxy", NULL, "Serial Proxy"); 221 222 sdp_data_free(ch); 223 sdp_list_free(proto[0], NULL); 224 sdp_list_free(proto[1], NULL); 225 sdp_list_free(apseq, NULL); 226 sdp_list_free(aproto, NULL); 227 228 return record; 229 } 230 231 static GIOError channel_write(GIOChannel *chan, char *buf, size_t size) 232 { 233 GIOError err = G_IO_ERROR_NONE; 234 gsize wbytes, written; 235 236 wbytes = written = 0; 237 while (wbytes < size) { 238 err = g_io_channel_write(chan, 239 buf + wbytes, 240 size - wbytes, 241 &written); 242 243 if (err != G_IO_ERROR_NONE) 244 return err; 245 246 wbytes += written; 247 } 248 249 return err; 250 } 251 252 static gboolean forward_data(GIOChannel *chan, GIOCondition cond, gpointer data) 253 { 254 char buf[BUF_SIZE]; 255 struct serial_proxy *prx = data; 256 GIOChannel *dest; 257 GIOError err; 258 size_t rbytes; 259 260 if (cond & G_IO_NVAL) 261 return FALSE; 262 263 dest = (chan == prx->rfcomm) ? prx->local : prx->rfcomm; 264 265 if (cond & (G_IO_HUP | G_IO_ERR)) { 266 /* Try forward remaining data */ 267 do { 268 rbytes = 0; 269 err = g_io_channel_read(chan, buf, sizeof(buf), &rbytes); 270 if (err != G_IO_ERROR_NONE || rbytes == 0) 271 break; 272 273 err = channel_write(dest, buf, rbytes); 274 } while (err == G_IO_ERROR_NONE); 275 276 g_io_channel_shutdown(prx->local, TRUE, NULL); 277 g_io_channel_unref(prx->local); 278 prx->local = NULL; 279 280 g_io_channel_shutdown(prx->rfcomm, TRUE, NULL); 281 g_io_channel_unref(prx->rfcomm); 282 prx->rfcomm = NULL; 283 284 return FALSE; 285 } 286 287 rbytes = 0; 288 err = g_io_channel_read(chan, buf, sizeof(buf), &rbytes); 289 if (err != G_IO_ERROR_NONE) 290 return FALSE; 291 292 err = channel_write(dest, buf, rbytes); 293 if (err != G_IO_ERROR_NONE) 294 return FALSE; 295 296 return TRUE; 297 } 298 299 static inline int unix_socket_connect(const char *address) 300 { 301 struct sockaddr_un addr; 302 int err, sk; 303 304 memset(&addr, 0, sizeof(addr)); 305 addr.sun_family = PF_UNIX; 306 307 if (strncmp("x00", address, 3) == 0) { 308 /* 309 * Abstract namespace: first byte NULL, x00 310 * must be removed from the original address. 311 */ 312 strncpy(addr.sun_path + 1, address + 3, 313 sizeof(addr.sun_path) - 2); 314 } else { 315 /* Filesystem address */ 316 strncpy(addr.sun_path, address, sizeof(addr.sun_path) - 1); 317 } 318 319 /* Unix socket */ 320 sk = socket(AF_UNIX, SOCK_STREAM, 0); 321 if (sk < 0) { 322 err = errno; 323 error("Unix socket(%s) create failed: %s(%d)", 324 address, strerror(err), err); 325 return -err; 326 } 327 328 if (connect(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) { 329 err = errno; 330 error("Unix socket(%s) connect failed: %s(%d)", 331 address, strerror(err), err); 332 close(sk); 333 errno = err; 334 return -err; 335 } 336 337 return sk; 338 } 339 340 static int tcp_socket_connect(const char *address) 341 { 342 struct sockaddr_in addr; 343 int err, sk; 344 unsigned short int port; 345 346 memset(&addr, 0, sizeof(addr)); 347 348 if (strncmp(address, "localhost", 9) != 0) { 349 error("Address should have the form localhost:port."); 350 return -1; 351 } 352 port = atoi(strchr(address, ':') + 1); 353 if (port <= 0) { 354 error("Invalid port '%d'.", port); 355 return -1; 356 } 357 addr.sin_family = AF_INET; 358 addr.sin_addr.s_addr = inet_addr("127.0.0.1"); 359 addr.sin_port = htons(port); 360 361 sk = socket(PF_INET, SOCK_STREAM, 0); 362 if (sk < 0) { 363 err = errno; 364 error("TCP socket(%s) create failed %s(%d)", address, 365 strerror(err), err); 366 return -err; 367 } 368 if (connect(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) { 369 err = errno; 370 error("TCP socket(%s) connect failed: %s(%d)", 371 address, strerror(err), err); 372 close(sk); 373 errno = err; 374 return -err; 375 } 376 return sk; 377 } 378 379 static inline int tty_open(const char *tty, struct termios *ti) 380 { 381 int err, sk; 382 383 sk = open(tty, O_RDWR | O_NOCTTY); 384 if (sk < 0) { 385 err = errno; 386 error("Can't open TTY %s: %s(%d)", tty, strerror(err), err); 387 return -err; 388 } 389 390 if (ti && tcsetattr(sk, TCSANOW, ti) < 0) { 391 err = errno; 392 error("Can't change serial settings: %s(%d)", 393 strerror(err), err); 394 close(sk); 395 errno = err; 396 return -err; 397 } 398 399 return sk; 400 } 401 402 static void connect_event_cb(GIOChannel *chan, GError *conn_err, gpointer data) 403 { 404 struct serial_proxy *prx = data; 405 int sk; 406 407 if (conn_err) { 408 error("%s", conn_err->message); 409 goto drop; 410 } 411 412 /* Connect local */ 413 switch (prx->type) { 414 case UNIX_SOCKET_PROXY: 415 sk = unix_socket_connect(prx->address); 416 break; 417 case TTY_PROXY: 418 sk = tty_open(prx->address, &prx->proxy_ti); 419 break; 420 case TCP_SOCKET_PROXY: 421 sk = tcp_socket_connect(prx->address); 422 break; 423 default: 424 sk = -1; 425 } 426 427 if (sk < 0) 428 goto drop; 429 430 prx->local = g_io_channel_unix_new(sk); 431 432 g_io_add_watch(prx->rfcomm, 433 G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL, 434 forward_data, prx); 435 436 g_io_add_watch(prx->local, 437 G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL, 438 forward_data, prx); 439 440 return; 441 442 drop: 443 g_io_channel_shutdown(prx->rfcomm, TRUE, NULL); 444 g_io_channel_unref(prx->rfcomm); 445 prx->rfcomm = NULL; 446 } 447 448 static void auth_cb(DBusError *derr, void *user_data) 449 { 450 struct serial_proxy *prx = user_data; 451 GError *err = NULL; 452 453 if (derr) { 454 error("Access denied: %s", derr->message); 455 goto reject; 456 } 457 458 if (!bt_io_accept(prx->rfcomm, connect_event_cb, prx, NULL, 459 &err)) { 460 error("bt_io_accept: %s", err->message); 461 g_error_free(err); 462 goto reject; 463 } 464 465 return; 466 467 reject: 468 g_io_channel_shutdown(prx->rfcomm, TRUE, NULL); 469 g_io_channel_unref(prx->rfcomm); 470 prx->rfcomm = NULL; 471 } 472 473 static void confirm_event_cb(GIOChannel *chan, gpointer user_data) 474 { 475 struct serial_proxy *prx = user_data; 476 int perr; 477 char address[18]; 478 GError *err = NULL; 479 480 bt_io_get(chan, BT_IO_RFCOMM, &err, 481 BT_IO_OPT_DEST_BDADDR, &prx->dst, 482 BT_IO_OPT_DEST, address, 483 BT_IO_OPT_INVALID); 484 if (err) { 485 error("%s", err->message); 486 g_error_free(err); 487 goto drop; 488 } 489 490 if (prx->rfcomm) { 491 error("Refusing connect from %s: Proxy already in use", 492 address); 493 goto drop; 494 } 495 496 debug("Serial Proxy: incoming connect from %s", address); 497 498 prx->rfcomm = g_io_channel_ref(chan); 499 500 perr = btd_request_authorization(&prx->src, &prx->dst, 501 prx->uuid128, auth_cb, prx); 502 if (perr < 0) { 503 error("Refusing connect from %s: %s (%d)", address, 504 strerror(-perr), -perr); 505 g_io_channel_unref(prx->rfcomm); 506 prx->rfcomm = NULL; 507 goto drop; 508 } 509 510 return; 511 512 drop: 513 g_io_channel_shutdown(chan, TRUE, NULL); 514 } 515 516 static int enable_proxy(struct serial_proxy *prx) 517 { 518 sdp_record_t *record; 519 GError *gerr = NULL; 520 int err; 521 522 if (prx->io) 523 return -EALREADY; 524 525 /* Listen */ 526 prx->io = bt_io_listen(BT_IO_RFCOMM, NULL, confirm_event_cb, prx, 527 NULL, &gerr, 528 BT_IO_OPT_SOURCE_BDADDR, &prx->src, 529 BT_IO_OPT_INVALID); 530 if (!prx->io) 531 goto failed; 532 533 bt_io_get(prx->io, BT_IO_RFCOMM, &gerr, 534 BT_IO_OPT_CHANNEL, &prx->channel, 535 BT_IO_OPT_INVALID); 536 if (gerr) { 537 g_io_channel_unref(prx->io); 538 prx->io = NULL; 539 goto failed; 540 } 541 542 debug("Allocated channel %d", prx->channel); 543 544 g_io_channel_set_close_on_unref(prx->io, TRUE); 545 546 record = proxy_record_new(prx->uuid128, prx->channel); 547 if (!record) { 548 g_io_channel_unref(prx->io); 549 return -ENOMEM; 550 } 551 552 err = add_record_to_server(&prx->src, record); 553 if (err < 0) { 554 sdp_record_free(record); 555 g_io_channel_unref(prx->io); 556 return err; 557 } 558 559 prx->record_id = record->handle; 560 561 return 0; 562 563 failed: 564 error("%s", gerr->message); 565 g_error_free(gerr); 566 return -EIO; 567 568 } 569 static DBusMessage *proxy_enable(DBusConnection *conn, 570 DBusMessage *msg, void *data) 571 { 572 struct serial_proxy *prx = data; 573 int err; 574 575 err = enable_proxy(prx); 576 if (err == -EALREADY) 577 return failed(msg, "Already enabled"); 578 else if (err == -ENOMEM) 579 return failed(msg, "Unable to allocate new service record"); 580 else if (err < 0) 581 return g_dbus_create_error(msg, ERROR_INTERFACE "Failed", 582 "Proxy enable failed (%s)", strerror(-err)); 583 584 return dbus_message_new_method_return(msg); 585 } 586 587 static DBusMessage *proxy_disable(DBusConnection *conn, 588 DBusMessage *msg, void *data) 589 { 590 struct serial_proxy *prx = data; 591 592 if (!prx->io) 593 return failed(msg, "Not enabled"); 594 595 /* Remove the watches and unregister the record */ 596 disable_proxy(prx); 597 598 return dbus_message_new_method_return(msg); 599 } 600 601 static DBusMessage *proxy_get_info(DBusConnection *conn, 602 DBusMessage *msg, void *data) 603 { 604 struct serial_proxy *prx = data; 605 DBusMessage *reply; 606 DBusMessageIter iter, dict; 607 dbus_bool_t boolean; 608 609 reply = dbus_message_new_method_return(msg); 610 if (!reply) 611 return NULL; 612 613 dbus_message_iter_init_append(reply, &iter); 614 615 dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, 616 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING 617 DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING 618 DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict); 619 620 dict_append_entry(&dict, "uuid", DBUS_TYPE_STRING, &prx->uuid128); 621 622 dict_append_entry(&dict, "address", DBUS_TYPE_STRING, &prx->address); 623 624 if (prx->channel) 625 dict_append_entry(&dict, "channel", 626 DBUS_TYPE_BYTE, &prx->channel); 627 628 boolean = (prx->io ? TRUE : FALSE); 629 dict_append_entry(&dict, "enabled", DBUS_TYPE_BOOLEAN, &boolean); 630 631 boolean = (prx->rfcomm ? TRUE : FALSE); 632 dict_append_entry(&dict, "connected", DBUS_TYPE_BOOLEAN, &boolean); 633 634 /* If connected: append the remote address */ 635 if (boolean) { 636 char bda[18]; 637 const char *pstr = bda; 638 639 ba2str(&prx->dst, bda); 640 dict_append_entry(&dict, "address", DBUS_TYPE_STRING, &pstr); 641 } 642 643 dbus_message_iter_close_container(&iter, &dict); 644 645 return reply; 646 } 647 648 static struct { 649 const char *str; 650 speed_t speed; 651 } supported_speed[] = { 652 {"50", B50 }, 653 {"300", B300 }, 654 {"600", B600 }, 655 {"1200", B1200 }, 656 {"1800", B1800 }, 657 {"2400", B2400 }, 658 {"4800", B4800 }, 659 {"9600", B9600 }, 660 {"19200", B19200 }, 661 {"38400", B38400 }, 662 {"57600", B57600 }, 663 {"115200", B115200 }, 664 { NULL, B0 } 665 }; 666 667 static speed_t str2speed(const char *str, speed_t *speed) 668 { 669 int i; 670 671 for (i = 0; supported_speed[i].str; i++) { 672 if (strcmp(supported_speed[i].str, str) != 0) 673 continue; 674 675 if (speed) 676 *speed = supported_speed[i].speed; 677 678 return supported_speed[i].speed; 679 } 680 681 return B0; 682 } 683 684 static int set_parity(const char *str, tcflag_t *ctrl) 685 { 686 if (strcasecmp("even", str) == 0) { 687 *ctrl |= PARENB; 688 *ctrl &= ~PARODD; 689 } else if (strcasecmp("odd", str) == 0) { 690 *ctrl |= PARENB; 691 *ctrl |= PARODD; 692 } else if (strcasecmp("mark", str) == 0) 693 *ctrl |= PARENB; 694 else if ((strcasecmp("none", str) == 0) || 695 (strcasecmp("space", str) == 0)) 696 *ctrl &= ~PARENB; 697 else 698 return -1; 699 700 return 0; 701 } 702 703 static int set_databits(uint8_t databits, tcflag_t *ctrl) 704 { 705 if (databits < 5 || databits > 8) 706 return -EINVAL; 707 708 *ctrl &= ~CSIZE; 709 switch (databits) { 710 case 5: 711 *ctrl |= CS5; 712 break; 713 case 6: 714 *ctrl |= CS6; 715 break; 716 case 7: 717 *ctrl |= CS7; 718 break; 719 case 8: 720 *ctrl |= CS8; 721 break; 722 } 723 724 return 0; 725 } 726 727 static int set_stopbits(uint8_t stopbits, tcflag_t *ctrl) 728 { 729 /* 1.5 will not be allowed */ 730 switch (stopbits) { 731 case 1: 732 *ctrl &= ~CSTOPB; 733 return 0; 734 case 2: 735 *ctrl |= CSTOPB; 736 return 0; 737 } 738 739 return -EINVAL; 740 } 741 742 static DBusMessage *proxy_set_serial_params(DBusConnection *conn, 743 DBusMessage *msg, void *data) 744 { 745 struct serial_proxy *prx = data; 746 const char *ratestr, *paritystr; 747 uint8_t databits, stopbits; 748 tcflag_t ctrl; /* Control mode flags */ 749 speed_t speed = B0; /* In/Out speed */ 750 751 /* Don't allow change TTY settings if it is open */ 752 if (prx->local) 753 return failed(msg, "Not allowed"); 754 755 if (!dbus_message_get_args(msg, NULL, 756 DBUS_TYPE_STRING, &ratestr, 757 DBUS_TYPE_BYTE, &databits, 758 DBUS_TYPE_BYTE, &stopbits, 759 DBUS_TYPE_STRING, &paritystr, 760 DBUS_TYPE_INVALID)) 761 return NULL; 762 763 if (str2speed(ratestr, &speed) == B0) 764 return invalid_arguments(msg, "Invalid baud rate"); 765 766 ctrl = prx->proxy_ti.c_cflag; 767 if (set_databits(databits, &ctrl) < 0) 768 return invalid_arguments(msg, "Invalid data bits"); 769 770 if (set_stopbits(stopbits, &ctrl) < 0) 771 return invalid_arguments(msg, "Invalid stop bits"); 772 773 if (set_parity(paritystr, &ctrl) < 0) 774 return invalid_arguments(msg, "Invalid parity"); 775 776 prx->proxy_ti.c_cflag = ctrl; 777 prx->proxy_ti.c_cflag |= (CLOCAL | CREAD); 778 cfsetispeed(&prx->proxy_ti, speed); 779 cfsetospeed(&prx->proxy_ti, speed); 780 781 return dbus_message_new_method_return(msg); 782 } 783 784 static GDBusMethodTable proxy_methods[] = { 785 { "Enable", "", "", proxy_enable }, 786 { "Disable", "", "", proxy_disable }, 787 { "GetInfo", "", "a{sv}",proxy_get_info }, 788 { "SetSerialParameters", "syys", "", proxy_set_serial_params }, 789 { }, 790 }; 791 792 static void proxy_path_unregister(gpointer data) 793 { 794 struct serial_proxy *prx = data; 795 int sk; 796 797 debug("Unregistered proxy: %s", prx->address); 798 799 if (prx->type != TTY_PROXY) 800 goto done; 801 802 /* Restore the initial TTY configuration */ 803 sk = open(prx->address, O_RDWR | O_NOCTTY); 804 if (sk >= 0) { 805 tcsetattr(sk, TCSAFLUSH, &prx->sys_ti); 806 close(sk); 807 } 808 done: 809 810 proxy_free(prx); 811 } 812 813 static int register_proxy_object(struct serial_proxy *prx) 814 { 815 struct serial_adapter *adapter = prx->adapter; 816 char path[MAX_PATH_LENGTH + 1]; 817 818 snprintf(path, MAX_PATH_LENGTH, "%s/proxy%d", 819 adapter_get_path(adapter->btd_adapter), sk_counter++); 820 821 if (!g_dbus_register_interface(adapter->conn, path, 822 SERIAL_PROXY_INTERFACE, 823 proxy_methods, NULL, NULL, 824 prx, proxy_path_unregister)) { 825 error("D-Bus failed to register %s path", path); 826 return -1; 827 } 828 829 prx->path = g_strdup(path); 830 adapter->proxies = g_slist_append(adapter->proxies, prx); 831 832 debug("Registered proxy: %s", path); 833 834 return 0; 835 } 836 837 static int proxy_tty_register(struct serial_adapter *adapter, 838 const char *uuid128, const char *address, 839 struct termios *ti, 840 struct serial_proxy **proxy) 841 { 842 struct termios sys_ti; 843 struct serial_proxy *prx; 844 int sk, ret; 845 846 sk = open(address, O_RDONLY | O_NOCTTY); 847 if (sk < 0) { 848 error("Cant open TTY: %s(%d)", strerror(errno), errno); 849 return -EINVAL; 850 } 851 852 prx = g_new0(struct serial_proxy, 1); 853 prx->address = g_strdup(address); 854 prx->uuid128 = g_strdup(uuid128); 855 prx->type = TTY_PROXY; 856 adapter_get_address(adapter->btd_adapter, &prx->src); 857 prx->adapter = adapter; 858 859 /* Current TTY settings */ 860 memset(&sys_ti, 0, sizeof(sys_ti)); 861 tcgetattr(sk, &sys_ti); 862 memcpy(&prx->sys_ti, &sys_ti, sizeof(sys_ti)); 863 close(sk); 864 865 if (!ti) { 866 /* Use current settings */ 867 memcpy(&prx->proxy_ti, &sys_ti, sizeof(sys_ti)); 868 } else { 869 /* New TTY settings: user provided */ 870 memcpy(&prx->proxy_ti, ti, sizeof(*ti)); 871 } 872 873 ret = register_proxy_object(prx); 874 if (ret < 0) { 875 proxy_free(prx); 876 return ret; 877 } 878 879 *proxy = prx; 880 881 return ret; 882 } 883 884 static int proxy_socket_register(struct serial_adapter *adapter, 885 const char *uuid128, const char *address, 886 struct serial_proxy **proxy) 887 { 888 struct serial_proxy *prx; 889 int ret; 890 891 prx = g_new0(struct serial_proxy, 1); 892 prx->address = g_strdup(address); 893 prx->uuid128 = g_strdup(uuid128); 894 prx->type = UNIX_SOCKET_PROXY; 895 adapter_get_address(adapter->btd_adapter, &prx->src); 896 prx->adapter = adapter; 897 898 ret = register_proxy_object(prx); 899 if (ret < 0) { 900 proxy_free(prx); 901 return ret; 902 } 903 904 *proxy = prx; 905 906 return ret; 907 } 908 909 static int proxy_tcp_register(struct serial_adapter *adapter, 910 const char *uuid128, const char *address, 911 struct serial_proxy **proxy) 912 { 913 struct serial_proxy *prx; 914 int ret; 915 916 prx = g_new0(struct serial_proxy, 1); 917 prx->address = g_strdup(address); 918 prx->uuid128 = g_strdup(uuid128); 919 prx->type = TCP_SOCKET_PROXY; 920 adapter_get_address(adapter->btd_adapter, &prx->src); 921 prx->adapter = adapter; 922 923 ret = register_proxy_object(prx); 924 if (ret < 0) { 925 proxy_free(prx); 926 return ret; 927 } 928 929 *proxy = prx; 930 931 return ret; 932 } 933 934 static proxy_type_t addr2type(const char *address) 935 { 936 struct stat st; 937 938 if (stat(address, &st) < 0) { 939 /* 940 * Unix socket: if the sun_path starts with null byte 941 * it refers to abstract namespace. 'x00' will be used 942 * to represent the null byte. 943 */ 944 if (strncmp("localhost:", address, 10) == 0) 945 return TCP_SOCKET_PROXY; 946 if (strncmp("x00", address, 3) != 0) 947 return UNKNOWN_PROXY_TYPE; 948 else 949 return UNIX_SOCKET_PROXY; 950 } else { 951 /* Filesystem: char device or unix socket */ 952 if (S_ISCHR(st.st_mode) && strncmp("/dev/", address, 4) == 0) 953 return TTY_PROXY; 954 else if (S_ISSOCK(st.st_mode)) 955 return UNIX_SOCKET_PROXY; 956 else 957 return UNKNOWN_PROXY_TYPE; 958 } 959 } 960 961 static int proxy_addrcmp(gconstpointer proxy, gconstpointer addr) 962 { 963 const struct serial_proxy *prx = proxy; 964 const char *address = addr; 965 966 return strcmp(prx->address, address); 967 } 968 969 static int proxy_pathcmp(gconstpointer proxy, gconstpointer p) 970 { 971 const struct serial_proxy *prx = proxy; 972 const char *path = p; 973 974 return strcmp(prx->path, path); 975 } 976 977 static int register_proxy(struct serial_adapter *adapter, 978 const char *uuid_str, const char *address, 979 struct serial_proxy **proxy) 980 { 981 proxy_type_t type; 982 int err; 983 984 type = addr2type(address); 985 if (type == UNKNOWN_PROXY_TYPE) 986 return -EINVAL; 987 988 /* Only one proxy per address(TTY or unix socket) is allowed */ 989 if (g_slist_find_custom(adapter->proxies, address, proxy_addrcmp)) 990 return -EALREADY; 991 992 switch (type) { 993 case UNIX_SOCKET_PROXY: 994 err = proxy_socket_register(adapter, uuid_str, address, proxy); 995 break; 996 case TTY_PROXY: 997 err = proxy_tty_register(adapter, uuid_str, address, NULL, 998 proxy); 999 break; 1000 case TCP_SOCKET_PROXY: 1001 err = proxy_tcp_register(adapter, uuid_str, address, proxy); 1002 break; 1003 default: 1004 err = -EINVAL; 1005 } 1006 1007 if (err < 0) 1008 return err; 1009 1010 g_dbus_emit_signal(adapter->conn, 1011 adapter_get_path(adapter->btd_adapter), 1012 SERIAL_MANAGER_INTERFACE, "ProxyCreated", 1013 DBUS_TYPE_STRING, &(*proxy)->path, 1014 DBUS_TYPE_INVALID); 1015 1016 return 0; 1017 } 1018 1019 static void unregister_proxy(struct serial_proxy *proxy) 1020 { 1021 struct serial_adapter *adapter = proxy->adapter; 1022 char *path = g_strdup(proxy->path); 1023 1024 if (proxy->watch > 0) 1025 g_dbus_remove_watch(adapter->conn, proxy->watch); 1026 1027 g_dbus_emit_signal(adapter->conn, 1028 adapter_get_path(adapter->btd_adapter), 1029 SERIAL_MANAGER_INTERFACE, "ProxyRemoved", 1030 DBUS_TYPE_STRING, &path, 1031 DBUS_TYPE_INVALID); 1032 1033 adapter->proxies = g_slist_remove(adapter->proxies, proxy); 1034 1035 g_dbus_unregister_interface(adapter->conn, path, 1036 SERIAL_PROXY_INTERFACE); 1037 1038 g_free(path); 1039 } 1040 1041 static void watch_proxy(DBusConnection *connection, void *user_data) 1042 { 1043 struct serial_proxy *proxy = user_data; 1044 1045 proxy->watch = 0; 1046 unregister_proxy(proxy); 1047 } 1048 1049 static DBusMessage *create_proxy(DBusConnection *conn, 1050 DBusMessage *msg, void *data) 1051 { 1052 struct serial_adapter *adapter = data; 1053 struct serial_proxy *proxy; 1054 const char *pattern, *address; 1055 char *uuid_str; 1056 int err; 1057 1058 if (!dbus_message_get_args(msg, NULL, 1059 DBUS_TYPE_STRING, &pattern, 1060 DBUS_TYPE_STRING, &address, 1061 DBUS_TYPE_INVALID)) 1062 return NULL; 1063 1064 uuid_str = bt_name2string(pattern); 1065 if (!uuid_str) 1066 return invalid_arguments(msg, "Invalid UUID"); 1067 1068 err = register_proxy(adapter, uuid_str, address, &proxy); 1069 g_free(uuid_str); 1070 1071 if (err == -EINVAL) 1072 return invalid_arguments(msg, "Invalid address"); 1073 else if (err == -EALREADY) 1074 return g_dbus_create_error(msg, ERROR_INTERFACE ".AlreadyExist", 1075 "Proxy already exists"); 1076 else if (err < 0) 1077 return g_dbus_create_error(msg, ERROR_INTERFACE "Failed", 1078 "Proxy creation failed (%s)", strerror(-err)); 1079 1080 proxy->owner = g_strdup(dbus_message_get_sender(msg)); 1081 proxy->watch = g_dbus_add_disconnect_watch(conn, proxy->owner, 1082 watch_proxy, 1083 proxy, NULL); 1084 1085 return g_dbus_create_reply(msg, DBUS_TYPE_STRING, &proxy->path, 1086 DBUS_TYPE_INVALID); 1087 } 1088 1089 static DBusMessage *list_proxies(DBusConnection *conn, 1090 DBusMessage *msg, void *data) 1091 { 1092 struct serial_adapter *adapter = data; 1093 const GSList *l; 1094 DBusMessage *reply; 1095 DBusMessageIter iter, iter_array; 1096 1097 reply = dbus_message_new_method_return(msg); 1098 if (!reply) 1099 return NULL; 1100 1101 dbus_message_iter_init_append(reply, &iter); 1102 dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, 1103 DBUS_TYPE_STRING_AS_STRING, &iter_array); 1104 1105 for (l = adapter->proxies; l; l = l->next) { 1106 struct serial_proxy *prx = l->data; 1107 1108 dbus_message_iter_append_basic(&iter_array, 1109 DBUS_TYPE_STRING, &prx->path); 1110 } 1111 1112 dbus_message_iter_close_container(&iter, &iter_array); 1113 1114 return reply; 1115 } 1116 1117 static DBusMessage *remove_proxy(DBusConnection *conn, 1118 DBusMessage *msg, void *data) 1119 { 1120 struct serial_adapter *adapter = data; 1121 struct serial_proxy *prx; 1122 const char *path, *sender; 1123 GSList *l; 1124 1125 if (!dbus_message_get_args(msg, NULL, 1126 DBUS_TYPE_STRING, &path, 1127 DBUS_TYPE_INVALID)) 1128 return NULL; 1129 1130 l = g_slist_find_custom(adapter->proxies, path, proxy_pathcmp); 1131 if (!l) 1132 return does_not_exist(msg, "Invalid proxy path"); 1133 1134 prx = l->data; 1135 1136 sender = dbus_message_get_sender(msg); 1137 if (g_strcmp0(prx->owner, sender) != 0) 1138 return failed(msg, "Permission denied"); 1139 1140 unregister_proxy(prx); 1141 1142 return dbus_message_new_method_return(msg); 1143 } 1144 1145 static void manager_path_unregister(void *data) 1146 { 1147 struct serial_adapter *adapter = data; 1148 GSList *l; 1149 1150 /* Remove proxy objects */ 1151 for (l = adapter->proxies; l; l = l->next) { 1152 struct serial_proxy *prx = l->data; 1153 char *path = g_strdup(prx->path); 1154 1155 g_dbus_unregister_interface(adapter->conn, path, 1156 SERIAL_PROXY_INTERFACE); 1157 g_free(path); 1158 } 1159 1160 if (adapter->conn) 1161 dbus_connection_unref(adapter->conn); 1162 1163 adapters = g_slist_remove(adapters, adapter); 1164 g_slist_free(adapter->proxies); 1165 btd_adapter_unref(adapter->btd_adapter); 1166 g_free(adapter); 1167 } 1168 1169 static GDBusMethodTable manager_methods[] = { 1170 { "CreateProxy", "ss", "s", create_proxy }, 1171 { "ListProxies", "", "as", list_proxies }, 1172 { "RemoveProxy", "s", "", remove_proxy }, 1173 { }, 1174 }; 1175 1176 static GDBusSignalTable manager_signals[] = { 1177 { "ProxyCreated", "s" }, 1178 { "ProxyRemoved", "s" }, 1179 { } 1180 }; 1181 1182 static struct serial_adapter *find_adapter(GSList *list, 1183 struct btd_adapter *btd_adapter) 1184 { 1185 GSList *l; 1186 1187 for (l = list; l; l = l->next) { 1188 struct serial_adapter *adapter = l->data; 1189 1190 if (adapter->btd_adapter == btd_adapter) 1191 return adapter; 1192 } 1193 1194 return NULL; 1195 } 1196 1197 static void serial_proxy_init(struct serial_adapter *adapter) 1198 { 1199 GKeyFile *config; 1200 GError *gerr = NULL; 1201 const char *file = CONFIGDIR "/serial.conf"; 1202 char **group_list; 1203 int i; 1204 1205 config = g_key_file_new(); 1206 1207 if (!g_key_file_load_from_file(config, file, 0, &gerr)) { 1208 error("Parsing %s failed: %s", file, gerr->message); 1209 g_error_free(gerr); 1210 g_key_file_free(config); 1211 return; 1212 } 1213 1214 group_list = g_key_file_get_groups(config, NULL); 1215 1216 for (i = 0; group_list[i] != NULL; i++) { 1217 char *group_str = group_list[i], *uuid_str, *address; 1218 int err; 1219 struct serial_proxy *prx; 1220 1221 /* string length of "Proxy" is 5 */ 1222 if (strlen(group_str) < 5 || strncmp(group_str, "Proxy", 5)) 1223 continue; 1224 1225 uuid_str = g_key_file_get_string(config, group_str, "UUID", 1226 &gerr); 1227 if (gerr) { 1228 debug("%s: %s", file, gerr->message); 1229 g_error_free(gerr); 1230 g_key_file_free(config); 1231 return; 1232 } 1233 1234 address = g_key_file_get_string(config, group_str, "Address", 1235 &gerr); 1236 if (gerr) { 1237 debug("%s: %s", file, gerr->message); 1238 g_error_free(gerr); 1239 g_key_file_free(config); 1240 g_free(uuid_str); 1241 return; 1242 } 1243 1244 err = register_proxy(adapter, uuid_str, address, &prx); 1245 if (err == -EINVAL) 1246 error("Invalid address."); 1247 else if (err == -EALREADY) 1248 debug("Proxy already exists."); 1249 else if (err < 0) 1250 error("Proxy creation failed (%s)", strerror(-err)); 1251 else { 1252 err = enable_proxy(prx); 1253 if (err < 0) 1254 error("Proxy enable failed (%s)", 1255 strerror(-err)); 1256 } 1257 1258 g_free(uuid_str); 1259 g_free(address); 1260 } 1261 1262 g_strfreev(group_list); 1263 g_key_file_free(config); 1264 } 1265 1266 int proxy_register(DBusConnection *conn, struct btd_adapter *btd_adapter) 1267 { 1268 struct serial_adapter *adapter; 1269 const char *path; 1270 1271 adapter = find_adapter(adapters, btd_adapter); 1272 if (adapter) 1273 return -EINVAL; 1274 1275 adapter = g_new0(struct serial_adapter, 1); 1276 adapter->conn = dbus_connection_ref(conn); 1277 adapter->btd_adapter = btd_adapter_ref(btd_adapter); 1278 1279 path = adapter_get_path(btd_adapter); 1280 1281 if (!g_dbus_register_interface(conn, path, 1282 SERIAL_MANAGER_INTERFACE, 1283 manager_methods, manager_signals, NULL, 1284 adapter, manager_path_unregister)) { 1285 error("Failed to register %s interface to %s", 1286 SERIAL_MANAGER_INTERFACE, path); 1287 return -1; 1288 } 1289 1290 adapters = g_slist_append(adapters, adapter); 1291 1292 debug("Registered interface %s on path %s", 1293 SERIAL_MANAGER_INTERFACE, path); 1294 1295 serial_proxy_init(adapter); 1296 1297 return 0; 1298 } 1299 1300 void proxy_unregister(struct btd_adapter *btd_adapter) 1301 { 1302 struct serial_adapter *adapter; 1303 1304 adapter = find_adapter(adapters, btd_adapter); 1305 if (!adapter) 1306 return; 1307 1308 g_dbus_unregister_interface(adapter->conn, 1309 adapter_get_path(btd_adapter), 1310 SERIAL_MANAGER_INTERFACE); 1311 } 1312