How to add user to Samba server using SAMR protocol?

67 views Asked by At

I have a samba server which is up locally this way using docker compose:

...
services:
  samba:
    image: instantlinux/samba-dc:latest
    container_name: my_samba
    cap_add:
      - CAP_SYS_ADMIN
    hostname: com.example
    environment:
      DOMAIN_ACTION: provision
      REALM: com.example
    volumes:
      - etc:/etc/samba
      - lib:/var/lib/samba
    ports:
      - "53:53"
      - "53:53/udp"
      - "88:88"
      - "88:88/udp"
      - "389:389"

I want to create user in samba using SAMR protocol. After long googling I've found library Jcifs and it contains such a package: https://github.com/codelibs/jcifs/tree/master/src/main/java/jcifs/dcerpc/msrpc

Looks like it could solve my issue but I can't find any example how to use it. What to start from ?

Could you please provide any high level algorhytm what to do ?

What url/port should I use to connect to Samba ?

P.S.

I tried to use chatGpt to find answer for that question and it provided the code example for me which looks appropriate.

import jcifs.smb.*;

public class SambaUserCreationExample {
    public static void main(String[] args) {
        String username = "admin";
        String password = "admin_password";
        String domainController = "smb://domain_controller_ip_address";
        String userToCreate = "new_user";
        String userPassword = "new_user_password";
        String groupName = "users";

        try {
            NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication("", username, password);
            String remotePath = domainController + "/samr";

            SmbFile smbFile = new SmbFile(remotePath, auth);
            SamrDomain domain = new SamrDomain(smbFile, auth);

            // Create the new user
            SamrUser newUser = domain.createUser(userToCreate, SamrUser.USER_NORMAL_ACCOUNT);
            newUser.setPassword(userPassword);
            newUser.commit();

            // Add the new user to a group
            SamrGroup group = domain.getGroup(groupName);
            group.addUser(newUser);
            group.commit();
            
            System.out.println("New user created successfully!");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

But I can't find any dependency which contains classes SamrUser and SamrDomain

0

There are 0 answers