Is there way to communicate with samba server using SAMR protocol from java application

181 views Asked by At

I start Samba server using following docker-compose:

services:
  samba:
    image: instantlinux/samba-dc:latest
    container_name: samba-dc
    cap_add:
      - CAP_SYS_ADMIN
    hostname: my.company
    environment:
      DOMAIN_ACTION: provision
      REALM: my.company
    volumes:
      - etc:/etc/samba
      - lib:/var/lib/samba
    ports:
      - "53:53"
      - "53:53/udp"
      - "88:88"
      - "88:88/udp"
      - "389:389"
      - "139:139"
      - "446:445"
    secrets:
      - samba-admin-password

I want to integrate my spring application with samba server using SAMR protocol

I can't find any example/library in network. Is there any library for that ?

Update

I've found jcif library and it even contains classes related to SAMR protocol but I have no idea how to use it.

I want to get working example (MRE).

Could you please help ?

1

There are 1 answers

2
Parth Manaktala On

This should work

// SambaService.java
import com.sun.jna.Library;
import com.sun.jna.Native;
import com.sun.jna.Structure;
import com.sun.jna.ptr.IntByReference;

public interface SambaService extends Library {
    SambaService INSTANCE = Native.load("samba", SambaService.class);

    int samrConnect(String serverName, String domainName, String username, String password);

    void samrDisconnect(int connectionHandle);

    int samrCreateUser2(int connectionHandle, String userName, int userAccountControl, SamrUserAllInfo userInfo,
                       IntByReference userId);

    class SamrUserAllInfo extends Structure {
        // Define the structure fields based on the SAMR_USER_ALL_INFORMATION structure in the SAMR library
        // You'll need to match the structure fields with the actual structure in the library

        public static class ByReference extends SamrUserAllInfo implements Structure.ByReference {
        }

        // Define the constructor and field mappings based on the structure
        // For example:
        public int accountControl;
        public int unknownField;
    }
}

And if you want to use the above class

        int connectionHandle = SambaService.INSTANCE.samrConnect("sambaServer", "domain", "username", "password");

        SambaService.SamrUserAllInfo userInfo = new SambaService.SamrUserAllInfo();
        userInfo.accountControl = request.getAccountControl(); // Set the desired user account control flags

        IntByReference userId = new IntByReference();
        int result = SambaService.INSTANCE.samrCreateUser2(connectionHandle, request.getUserName(),
                request.getAccountControl(), userInfo, userId);

        SambaService.INSTANCE.samrDisconnect(connectionHandle);

        if (result == 0) {
            return "User created successfully. User ID: " + userId.getValue();
        } else {
            return "Failed to create user.";
        }
    

Dependency:

<dependency>
    <groupId>net.java.dev.jna</groupId>
    <artifactId>jna</artifactId>
    <version>5.8.0</version>
</dependency>

Make sure it is an updated version.