Commit Graph

732 Commits

Author SHA1 Message Date
Nicolas Fella
bf09e1f048 neard: Fix reading State message
dbus_message_iter_recurse only makese sense for container types, this is a string.

Fixes: https://github.com/bluez/bluez/issues/300
2022-02-24 13:57:59 -08:00
Fabrice Fontaine
fb57ad9b9d build: Fix errors with glibc < 2.25
getrandom and sys/random.h are only available since glibc 2.25:
https://www.gnu.org/software/gnulib/manual/html_node/sys_002frandom_002eh.html
resulting in the following build failures since version 5.63 and
https://git.kernel.org/pub/scm/bluetooth/bluez.git/log/?qt=grep&q=getrandom:

plugins/autopair.c:20:24: fatal error: sys/random.h: No such file or directory
 #include <sys/random.h>
                        ^

To fix this build failure, add util_getrandom and a fallback (borrowed
from pipewire and licensed under MIT):
https://gitlab.freedesktop.org/pipewire/pipewire/-/blob/master/src/pipewire/utils.c

Fixes:
 - http://autobuild.buildroot.org/results/6b8870d12e0804d6154230a7322c49416c1dc0e2
2022-02-15 13:36:35 -08:00
Luiz Augusto von Dentz
cfab569484 build: Replace use of g_memdup with util_memdup
This replaces the uses of g_memdup with util_memdup since the former has
been deprecated:

  warning: ‘g_memdup’ is deprecated: Use 'g_memdup2' instead
  [-Wdeprecated-declarations]

g_memdup2 requires bumping glib version which would likely have its
own problems thus why util_memdup was introduced.
2022-01-06 12:58:39 -08:00
Tedd Ho-Jeong An
6efadbcd07 plugins: Replace random number generation function
This patch replaces the rand() function to the getrandom() syscall.

It was reported by the Coverity scan
  rand() should not be used for security-related applications, because
  linear congruential algorithms are too easy to break
2021-12-08 16:56:18 -08:00
Yun-Hao Chung
927059ba60 admin: fix devices not reset
When |admin_policy_remove| is called, we set |devices| to NULL but never
set it back until |admin_init|. This makes admin lost track of current
registered device interface, so the next |admin_policy_removed| will not
be able to unregister those interfaces.

Reviewed-by: Archie Pusaka <apusaka@chromium.org>
2021-11-15 13:06:57 -08:00
Tedd Ho-Jeong An
0b23a290d0 plugins/admin: Fix unchecked return value
This patch fixes the unchecked return value(CWE-252) issues reported by
the Coverity.
2021-10-18 15:31:54 -07:00
Archie Pusaka
e5af955ef8 plugins/sixaxis: Inclusive language changes
BT core spec 5.3 promotes the usage of inclusive languages.
This CL uses "central" as it is deemed to be more appropriate.

Signed-off-by: Marcel Holtmann <marcel@holtmann.org>
2021-09-21 10:51:54 +02:00
Luiz Augusto von Dentz
dbb3af61ed admin: Fix double free
Fixes the following double free which happen due to exit calling
btd_unregister_adapter_driver:

Invalid read of size 8
   at 0x1CDA97: queue_foreach (queue.c:198)
   by 0x1318B8: admin_policy_remove (admin.c:591)
   by 0x18982A: plugin_cleanup (plugin.c:217)
   by 0x12E3FD: main (main.c:1214)
 Address 0x547ffb8 is 8 bytes inside a block of size 32 free'd
   at 0x483A9F5: free (vg_replace_malloc.c:538)
   by 0x1318CB: admin_policy_remove (admin.c:592)
   by 0x18F416: unload_driver (adapter.c:7215)
   by 0x496F50F: g_slist_foreach (in /usr/lib64/libglib-2.0.so.0.6600.8)
   by 0x131988: admin_exit (admin.c:623)
   by 0x18982A: plugin_cleanup (plugin.c:217)
   by 0x12E3FD: main (main.c:1214)
 Block was alloc'd at
   at 0x4839809: malloc (vg_replace_malloc.c:307)
   by 0x1CDE1E: btd_malloc (util.c:33)
   by 0x1CD83D: queue_new (queue.c:47)
   by 0x13150D: admin_init (admin.c:614)
   by 0x18966B: plugin_init (plugin.c:187)
   by 0x12E358: main (main.c:1198)
2021-09-17 12:49:51 -07:00
Luiz Augusto von Dentz
36e4ce9076 admin: Fix leaking uuids loads from storage
This fixes the following trace:

8 bytes in 1 blocks are definitely lost in loss record 27 of 274
   at 0x4839809: malloc (vg_replace_malloc.c:307)
   by 0x495BBB8: g_malloc (in /usr/lib64/libglib-2.0.so.0.6600.8)
   by 0x494C024: g_key_file_get_string_list (in /usr/lib64/libglib-2.0.so.0.6600.8)
   by 0x131ECD: key_file_load_service_allowlist (admin.c:294)
   by 0x131ECD: load_policy_settings (admin.c:346)
   by 0x131ECD: admin_policy_adapter_probe (admin.c:497)
   by 0x18F554: probe_driver (adapter.c:4858)
   by 0x19DF5A: load_drivers (adapter.c:4873)
   by 0x19DF5A: adapter_register (adapter.c:8975)
   by 0x19DF5A: read_info_complete (adapter.c:9791)
   by 0x1CE831: request_complete (mgmt.c:264)
   by 0x1CF7D4: can_read_data (mgmt.c:356)
   by 0x1DE634: watch_callback (io-glib.c:157)
   by 0x4953A9E: g_main_context_dispatch (in /usr/lib64/libglib-2.0.so.0.6600.8)
   by 0x49A5A97: ??? (in /usr/lib64/libglib-2.0.so.0.6600.8)
   by 0x4953162: g_main_loop_run (in /usr/lib64/libglib-2.0.so.0.6600.8)
2021-09-17 12:49:50 -07:00
Luiz Augusto von Dentz
f7eb887f4e policy: Use btd_service_is_initiator
Instead of using BTD_SERVICE_STATE_CONNECTING use
btd_service_is_initiator to determine if the service initiated the
connection and then proceed to connect other service immediately.

Fixes: https://github.com/bluez/bluez/issues/205
2021-09-17 12:49:50 -07:00
Yun-Hao Chung
b22dc8accf plugins/admin: create admin_policy_settings if not exists
If admin_policy_settings is not found when loading, we should create one
instead of printing error.

Reviewed-by: Shyh-In Hwang <josephsih@chromium.org>
Reviewed-by: Miao-chen Chou <mcchou@chromium.org>
2021-09-15 16:38:59 -07:00
Yun-Hao Chung
bf88ab8383 plugins/admin: add adapter_remove handler
Currently admin doesn't handle adapter removed callbacks, which causes
interfaces AdminPolicySet1 and AdminPolicyStatus1 not being
unregistered, which in turns causes these interfaces can not be
re-registered once adapter is back.

This adds handler for adapter_remove.

Reviewed-by: Shyh-In Hwang <josephsih@chromium.org>
Reviewed-by: Miao-chen Chou <mcchou@chromium.org>
2021-09-15 16:38:59 -07:00
Yun-Hao Chung
355d06b3f3 plugin/admin: fix set empty allowlist no persistence issue
This patch fixes a bug when setting empty service allowlist, the
allowlist sets successfully but it fails to be stored in the file.

Reviewed-by: Miao-chen Chou <mcchou@chromium.org>
2021-08-31 17:56:10 -07:00
Yun-Hao Chung
561c500795 plugins/admin: add uuid duplicate check
SetServiceAllowlist should ignore those duplicated UUIDs.

Reviewed-by: Miao-chen Chou <mcchou@chromium.org>
2021-08-11 11:22:00 -07:00
Yun-Hao Chung
b97174f2ea plugins/admin: persist policy settings
This adds code to store the ServiceAllowlist to file
/var/lib/bluetooth/{MAC_ADDR}/admin_policy
The stored settings will be loaded upon admin_policy initialized.

Reviewed-by: Miao-chen Chou <mcchou@chromium.org>
2021-08-04 15:01:33 -07:00
Yun-Hao Chung
3c258b15a5 plugins/admin: add AffectedByPolicy property
This adds property to indicate if a device has any service that is being
blocked by admin policy.

Reviewed-by: Miao-chen Chou <mcchou@chromium.org>
2021-08-04 15:01:33 -07:00
Yun-Hao Chung
efe2eb474c plugins/admin: add device callbacks
This adds callbacks for device resolved and device removed. It is
necessary for implementation of "AffectedByPolicy" property since it
needs to register an interface for each device object and unregister it
once the device gets removed.
2021-08-04 15:01:33 -07:00
Yun-Hao Chung
2106a20d5c plugins/admin: add ServiceAllowList property
This adds code to register interface org.bluez.AdminPolicyStatus.
The interface will provide read-only properties to indicate the current
settings of admin policies. We separate this from AdminPolicySet so that
normal clients can check current policy settings while only a few
clients can change policies.

This patch also adds readonly property ServiceAllowlist to
AdminPolicyStatus1, which indicates the current setting of service
allowlist.

Reviewed-by: Miao-chen Chou <mcchou@chromium.org>
2021-08-04 15:01:33 -07:00
Yun-Hao Chung
0d31b1945a plugins/admin: add ServiceAllowList method
This adds code to register interface org.bluez.AdminPolicySet1.
The interface will provide methods to limit users to operate certain
functions of bluez, such as allow/disallow user to taggle adapter power,
or only allow users to connect services in the specified list, etc.

This patch also implements ServiceAllowlist in
org.bluez.AdminPolicySet1.

Reviewed-by: Miao-chen Chou <mcchou@chromium.org>
2021-08-04 15:01:33 -07:00
Yun-Hao Chung
36f34110b3 plugins/admin: add admin_policy adapter driver
This adds code to register admin_policy driver to adapter when
admin plugin is enabled.

The following test steps were performed:
1. restart bluetoothd
2. check if "Admin Policy is enabled" in system log

Reviewed-by: Miao-chen Chou <mcchou@chromium.org>
2021-08-04 15:01:33 -07:00
Yun-Hao Chung
e7c349950e plugins: new plugin
This adds an initial code for a new plugin admin.

Reviewed-by: Miao-chen Chou <mcchou@chromium.org>
2021-08-04 15:01:33 -07:00
Steve Grubb
28ce42b050 plugin: Fix memory leaks
g_file_get_contents allocates memory. It needs to be freed on any
function exit.
2021-05-14 17:04:35 -07:00
Frédéric Danis
7b18419775 plugins: Use timeout_add_seconds
Replace calls to g_timeout_add_seconds() by the timeout_add_seconds()
wrapper which takes care of 0 delay.
2021-03-16 10:55:59 -07:00
Szymon Janc
61745d2bb8 sixaxis: Fix Bluetooth PS3 clone joypad being named like the original
When cable pairing a PS3 clone device, we should try and keep the USB device
name to create a new btd_device so that the joypad is named after its USB name
when connecting through Bluetooth.

If that isn't done, "Shanwan" clone joypads are named like the genuine joypads, and
kernel Bluetooth quirks aren't applied.

gh-issue: https://github.com/bluez/bluez/issues/46
2021-02-22 09:22:59 +01:00
Luiz Augusto von Dentz
c4ef8f8d04 core: Rename hcid.h to btd.h
Since we use btd term for daemon APIs it makes sense to use it also
for the header name instead of hcid.
2020-10-29 10:27:38 -07:00
Luiz Augusto von Dentz
01d035d338 core: Rename main_opts to btd_opts
Use btd_ prefix since this is a daemon API.
2020-10-29 10:27:37 -07:00
Tedd Ho-Jeong An
95bcae1e13 plugins: Add SPDX License Identifier
This patch adds SPDX License Identifier and removes the license text.

-------------------------------------
       License            COUNT
-------------------------------------
 GPL-2.0-or-later     :      7

License: GPL-2.0-or-later
   plugins/autopair.c
   plugins/hostname.c
   plugins/neard.c
   plugins/policy.c
   plugins/sixaxis.c
   plugins/wiimote.c
   plugins/external-dummy.c
2020-09-21 16:19:36 -07:00
Miao-chen Chou
3f2406997f sixaxis: Fix crash caused by return of udev_device_get_property_value
This adds a NULL check before calling sscanf().
2020-09-16 23:17:20 -07:00
Luiz Augusto von Dentz
7f4bdf3820 adapter: Add btd_ prefix to has_kernel_feature
APIs exposed to plugins shall have btd_ prefix.
2020-09-15 11:04:18 -07:00
Abhishek Pandit-Subedi
6611b72600 policy: Reconnect audio on controller resume
During system suspend, all peer devices are disconnected. On resume, HID
devices will reconnect but audio devices stay disconnected. As a quality
of life improvement, mark audio devices that were disconnected due to
suspend and attempt to reconnect them when the controller resumes (after
a delay for better co-existence with Wi-Fi).
2020-09-14 13:25:37 -07:00
Abhishek Pandit-Subedi
20275c5a9b policy: Enable reconnect for a2dp-sink in defaults
Add a2dp-sink to default reconnects list.
2020-09-14 13:20:41 -07:00
Sonny Sasaka
823821dca0 autopair: Fix compiler warning
With clang, comparing an array with NULL generates a warning because the
value is always non-NULL. With maintainer mode enabled, this becomes a
compilation error.
2020-03-26 10:31:35 -07:00
Bastien Nocera
ee70e5e070 sixaxis: Throw an error when cable setup fails
If btd_request_authorization_cable_configured() fails, throw an error
and free resources.
2019-06-13 11:24:55 +02:00
Bastien Nocera
59b934ab8d sixaxis: Fix another problem with already setup devices
If the device went through any kind of pairing once, it might have been
set as trusted. Make sure to set the device as untrusted before starting
the cable pairing authorization so that we don't exit early from
process_auth_queue() (which considers trusted devices to be paired).
2019-06-13 11:24:55 +02:00
Bastien Nocera
a5958b5ba4 autopair: Add pin codes to try for gaming input devices
As well as remote controls.
2019-03-26 11:48:56 +01:00
Bastien Nocera
c8ef6d8abe autopair: Add more common PIN codes for audio devices
PIN codes "1111", and "1234" are fairly common PIN codes used for audio
devices such as speakers and headsets. This replaces similar quirks
already present in gnome-bluetooth's PIN database.
2019-03-26 11:48:56 +01:00
Marcel Holtmann
261948090e build: Move declaration of _GNU_SOURCE back into individual source files 2018-12-06 21:28:18 +01:00
Luiz Augusto von Dentz
477ecca127 policy: Add logic to connect a Sink
If HFP/HSP HS connects and the device also supports a Sink connect it
as well since some devices (e.g. Sony MW600) may not connect it
automatically.
2018-06-26 14:02:21 +03:00
Bastien Nocera
7bb73f092a plugins/sixaxis: Don't ignore previously setup devices
1. Setup PS3 controller through cable pairing
2. Remove device from BlueZ's database
3. Plug original PS3 controller back

As we don't change the master bdaddr on the device itself, the joypad
will likely be trying to connect to BlueZ, and fail to connect after a
small period of time. But the device will appear connected just long
enough for the cable pairing to say "hey, I already have this setup".

Ideally, we would test for whether the device is temporary, or already
trusted, but testing for whether we've setup its internal services *and*
it's connected, rather than *or*, is sufficient.
2017-11-30 09:53:45 +01:00
Bastien Nocera
1629c39ede plugins/sixaxis: Provide DualShock 3 SDP record while adding new device
This allows to skip SDP search for DualShock 3 devices, since some
DS3 clones do not provide any SDP record. This allows them to operate
nonetheless.

The HID SDP record is lifted straight off an original DualShock 3
controller. The PNPID SDP record is not set as not required to provide
a working device.

Tested with a "SHANWAN" clone controller.
2017-11-09 09:52:01 +01:00
Luiz Augusto von Dentz
6c467e923b sixaxis: Fix compilation and various coding style issues
This fixes the following problems:

plugins/sixaxis.c:443:7: error: ‘version’ may be used uninitialized in this function [-Werror=maybe-uninitialized]
  if (!setup_device(fd, sysfs_path, name, source, vid, pid, version, type, adapter))
       ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
plugins/sixaxis.c:409:34: note: ‘version’ was declared here
  uint16_t bus, vid, pid, source, version;
                                  ^~~~~~~
plugins/sixaxis.c:443:7: error: ‘source’ may be used uninitialized in this function [-Werror=maybe-uninitialized]
  if (!setup_device(fd, sysfs_path, name, source, vid, pid, version, type, adapter))
       ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
plugins/sixaxis.c:409:26: note: ‘source’ was declared here
  uint16_t bus, vid, pid, source, version;
                          ^~~~~~
cc1: all warnings being treated as errors

And many instances of code going over 80 columns.
2017-10-27 13:42:33 +03:00
Bastien Nocera
8a016ba915 plugins/sixaxis: Cancel cable pairing if unplugged
Cancel pending authorization requests when the device is unplugged
while a request is pending.
2017-10-27 09:58:06 +02:00
Juha Kuikka
f0c27bb9c2 plugins/sixaxis: Add support for DualShock 4/PS4 cable pairing
This patch adds support for "pairing" a Dualshock4 controller over USB
into the sixaxis plugin, similarly to what the Sixaxis/PS3 controller
supported.

Actual bonding happens on first connection, but the device will be
marked as trusted when the agent replies.

This patch is based on DS4 supprt for sixpair tool by t0xicCode:
975c38cb6c
2017-10-27 09:58:06 +02:00
Bastien Nocera
4bf85b2750 plugins/sixaxis: Rename sixaxis specific functions
And provide wrappers to prepare for the addition of other device types.
2017-10-27 09:58:06 +02:00
Bastien Nocera
bbc99b7d7f plugins/sixaxis: Move device discovery to shared header
Move the struct containing the Sixaxis-compatible devices to a
header shared with the input profiles code, so as to reduce device
declaration.

Adding support for new devices should be as easy as adding the device's
declaration in profiles/input/sixaxis.h
2017-10-27 09:58:06 +02:00
Bastien Nocera
b6ae731364 sixaxis: Ask user whether cable configuration should be allowed
Previously, users doing cable configuration of Sixaxis PS3 controllers
would only get asked whether a device was allowed to connect to the
computer when switching it to Bluetooth mode: unplugging it, and
pressing the PS button.

Instead, we should ask the user straight away, through the agent,
whether the pad should be allowed to connect.

This makes it easier to setup those devices, while keeping security.
2017-10-27 09:58:06 +02:00
Bastien Nocera
db4ec11036 autopair: Don't handle the iCade
We can't easily enter digits other than 1 through 4 (inclusive)
so leave it up to the agent to figure out a good passcode
for the iCade.

Note that we can not use the VID/PID of the device, as it is not
yet known at that point.
2017-10-17 11:33:59 +02:00
Bastien Nocera
efe53dc46f plugins/sixaxis: Remove LEDs handling
It's done in the kernel since 2014 in linux kernel commit
8025087acf9d2b941bae93b3e0967560e7e03e87
2017-09-27 11:04:20 +02:00
Nicolas Werner
7cdfddada0 plugins/sixaxis: Use the same device name as the kernel
Some games check the device name to recognize a playstation controller.
This changes the device name, when using a PS3 controller over
bluetooth, to match the device name, that is advertised when using the
controller via USB.
2017-08-08 13:30:17 +02:00
ERAMOTO Masaya
d4477e8fc3 plugins: Fix reconnect_interval for cases of improper main.conf
There are two problems:

 - When main.conf is not found, bluetoothd copies short the default set
   of reconnecting intervals to reconnect_interval. It does not match
   the reconnct_interval_len used as the array length.
   So if a link of device is disconnected, bluetoothd is run over
   reconnect_interval as time proceeds and will not time out as expected.

   bluetooothd with --debug option outputed the following log in my box:

     plugins/policy.c:reconnect_set_timer() attempt 1/7 1 seconds
     plugins/policy.c:reconnect_timeout() Reconnecting profiles
     plugins/policy.c:conn_fail_cb() status 4
     plugins/policy.c:reconnect_set_timer() attempt 2/7 2 seconds
     plugins/policy.c:reconnect_timeout() Reconnecting profiles
     plugins/policy.c:conn_fail_cb() status 4
     plugins/policy.c:reconnect_set_timer() attempt 3/7 0 seconds
     plugins/policy.c:reconnect_timeout() Reconnecting profiles
     plugins/policy.c:conn_fail_cb() status 4
     plugins/policy.c:reconnect_set_timer() attempt 4/7 0 seconds

 - When ReconnectIntervals in main.conf includes invalid characters,
   reconnct_interval_len value is bigger than the default array length.
   So if ReconnectAttempts value in main.conf is bigger than
   reconnct_interval_len value and a link of device is disconnected,
   bluetoothd is run over reconnect_interval as time proceeds and will
   not time out as expected.

   bluetooothd with --debug option outputed the following log in my box,
   if ReconnectAttempts value was 28 and ReconnectIntervals was inproper:

     ...
     plugins/policy.c:reconnect_set_timer() attempt 6/28 32 seconds
     plugins/policy.c:reconnect_timeout() Reconnecting profiles
     plugins/policy.c:conn_fail_cb() status 4
     plugins/policy.c:reconnect_set_timer() attempt 7/28 64 seconds
     plugins/policy.c:reconnect_timeout() Reconnecting profiles
     plugins/policy.c:conn_fail_cb() status 4
     plugins/policy.c:reconnect_set_timer() attempt 8/28 0 seconds
     ...
     plugins/policy.c:reconnect_set_timer() attempt 25/28 1 seconds
     plugins/policy.c:reconnect_timeout() Reconnecting profiles
     plugins/policy.c:conn_fail_cb() status 4
     plugins/policy.c:reconnect_set_timer() attempt 26/28 3 seconds
     plugins/policy.c:reconnect_timeout() Reconnecting profiles
     plugins/policy.c:conn_fail_cb() status 4
     plugins/policy.c:reconnect_set_timer() attempt 27/28 110683472 seconds

This fix properly uses the default set of reconnecting intervals.
2017-07-21 14:34:39 +03:00