diff --git a/README.md b/README.md index 1bfbb3fe1..9819456be 100644 --- a/README.md +++ b/README.md @@ -74,7 +74,7 @@ The library will remain open source and MIT licensed and can still be used, fork - **Minimum Java version: 21** - **Removed** all methods, members and classes marked as deprecated - Update JUnit to Version 6 - - Remove `throws IOException` from `AbstractConnectionBase.close()` (Issue #287) + - Remove `throws IOException` from `AbstractConnectionBase.close()` ([#287](https://github.com/hypfvieh/dbus-java/issues/287)) - Support usage of `Struct`s as return value (as alternative to `Tuple` with generics) (based on discussion in #285) - Updated dependencies and plugins - Added support to use `Struct` datatypes as return values instead of `Tuple`# @@ -87,6 +87,8 @@ The library will remain open source and MIT licensed and can still be used, fork - This can be enabled using the `DBusConnectionBuilder`, example: `DBusConnection sessionConnection = DBusConnectionBuilder.forSystemBus().receivingThreadConfig().withAllVirtualThreads(true).connectionConfig().build()` - Virtual-Threads can be enabled/disabled for each of the different executor services used in `ReceivingService`: `SIGNAL`, `ERROR`, `METHODCALL`, `METHODRETURN` - default remains native threads on all executors + - Fixed possible NullPointerException in SASL auth ([#294](https://github.com/hypfvieh/dbus-java/issues/294)) + - Fixed SASL authentication issue when running in server mode in combination with unix sockets ([#298](https://github.com/hypfvieh/dbus-java/issues/298)) ##### Changes in 5.2.0 (2025-12-21): - removed properties from dbus-java.version which causes issues with reproducable builds ([PR#279](https://github.com/hypfvieh/dbus-java/issues/279)) diff --git a/dbus-java-core/src/main/java/org/freedesktop/dbus/connections/SASL.java b/dbus-java-core/src/main/java/org/freedesktop/dbus/connections/SASL.java index a12e7c2c5..f564b62bd 100644 --- a/dbus-java-core/src/main/java/org/freedesktop/dbus/connections/SASL.java +++ b/dbus-java-core/src/main/java/org/freedesktop/dbus/connections/SASL.java @@ -598,24 +598,18 @@ public boolean auth(SocketChannel _sock, AbstractTransport _transport) throws IO case SERVER: switch (state) { case INITIAL_STATE: - ByteBuffer buf = ByteBuffer.allocate(1); - if (_sock != null) { - _sock.read(buf); // 0 + try { + int kuid = -1; + if (_transport instanceof AbstractUnixTransport aut) { + kuid = aut.getUid(_sock); + } + if (kuid >= 0) { + kernelUid = stupidlyEncode("" + kuid); + } state = SaslAuthState.WAIT_AUTH; - } else { - try { - int kuid = -1; - if (_transport instanceof AbstractUnixTransport aut) { - kuid = aut.getUid(null); - } - if (kuid >= 0) { - kernelUid = stupidlyEncode("" + kuid); - } - state = SaslAuthState.WAIT_AUTH; - } catch (SocketException _ex) { - state = SaslAuthState.FAILED; - } + } catch (SocketException _ex) { + state = SaslAuthState.FAILED; } break; case WAIT_AUTH: diff --git a/dbus-java-core/src/main/java/org/freedesktop/dbus/connections/transports/AbstractTransport.java b/dbus-java-core/src/main/java/org/freedesktop/dbus/connections/transports/AbstractTransport.java index 8d73b810d..c43a78896 100644 --- a/dbus-java-core/src/main/java/org/freedesktop/dbus/connections/transports/AbstractTransport.java +++ b/dbus-java-core/src/main/java/org/freedesktop/dbus/connections/transports/AbstractTransport.java @@ -249,6 +249,9 @@ public void setPreConnectCallback(Consumer _run) { * @throws IOException on any error */ private void authenticate(SocketChannel _sock) throws IOException { + if (_sock == null) { + throw new IOException("SocketChannel instance required"); + } SASL sasl = new SASL(config.getSaslConfig()); try { if (!sasl.auth(_sock, this)) { diff --git a/dbus-java-transport-native-unixsocket/src/main/java/org/freedesktop/dbus/transport/jre/NativeUnixSocketHelper.java b/dbus-java-transport-native-unixsocket/src/main/java/org/freedesktop/dbus/transport/jre/NativeUnixSocketHelper.java index e91ecda0b..ec92cf364 100644 --- a/dbus-java-transport-native-unixsocket/src/main/java/org/freedesktop/dbus/transport/jre/NativeUnixSocketHelper.java +++ b/dbus-java-transport-native-unixsocket/src/main/java/org/freedesktop/dbus/transport/jre/NativeUnixSocketHelper.java @@ -13,9 +13,17 @@ private NativeUnixSocketHelper() {} /** * Get the UID of peer credentials. + *

+ * Gathering the UID of SO_PEERCRED directly is not obvious when it comes to JDK native unix sockets.
+ * based on the implementation in {@code sun.nio.fs.UnixUserPrincipals.User},
+ * calling {@code hashCode()} on the {@link UserPrincipal} will give you either the UID or the hashCode of the name. + *

+ * This method ensures that a proper UID is returned and not the hashCode of the name. + * If there is no UID, -1 is returned. + *

* * @param _sock socket to read from - * @return UID, -1 if given {@link SocketChannel} was null + * @return UID, -1 if given {@link SocketChannel} was {@code null} or UID could not be determined * * @throws IOException when socket channel fails to read SO_PEERCRED option */ @@ -27,7 +35,12 @@ public static int getUid(SocketChannel _sock) throws IOException { UnixDomainPrincipal creds = _sock.getOption(ExtendedSocketOptions.SO_PEERCRED); UserPrincipal user = creds.user(); - return Integer.parseInt(user.getName()); + int uid = -1; + if (user != null && user.hashCode() != user.getName().hashCode()) { + uid = user.hashCode(); + } + + return uid; } }