Can create multiple user at once with ldap2smb

This commit is contained in:
Théo Barnouin 2024-01-04 16:28:36 +01:00
parent e3e9e79965
commit 4e9c4fa853

View file

@ -1,3 +1,4 @@
import sys
import json import json
import getpass import getpass
@ -10,6 +11,10 @@ from samba.ndr import ndr_pack, ndr_unpack
from samba.param import LoadParm from samba.param import LoadParm
from samba.samdb import SamDB from samba.samdb import SamDB
global lp
global creds
global samdb
lp = LoadParm() lp = LoadParm()
creds = Credentials() creds = Credentials()
creds.guess(lp) creds.guess(lp)
@ -17,39 +22,42 @@ creds.set_username('admin')
creds.set_password('Jps55Sk8An9y2nVL') creds.set_password('Jps55Sk8An9y2nVL')
samdb = SamDB(url='/var/lib/samba/private/sam.ldb', session_info=system_session(),credentials=creds, lp=lp) samdb = SamDB(url='/var/lib/samba/private/sam.ldb', session_info=system_session(),credentials=creds, lp=lp)
data_file="user.json" def adduser():
with open(DATA_FILE, "r", encoding="utf8") as file:
ldap_users = json.load(file)
with open(data_file, "r") as file: smb_full_username = ldap_users["params"]["attributes"]["uid"]
ldap_users = json.load(file) smb_username = smb_full_username.split('@', 1)[0]
smb_full_username = ldap_users["params"]["attributes"]["uid"] smb_passwd = ldap_users["params"]["attributes"]["userPassword"]
smb_username = smb_full_username.split('@', 1)[0]
smb_full_passwd = ldap_users["params"]["attributes"]["userPassword"] smb_givenname = ldap_users["params"]["attributes"]["givenName"]
smb_passwd = smb_full_passwd.split('}', 1)[1] smb_surname = ldap_users["params"]["attributes"]["sn"]
smb_homedir = ldap_users["params"]["attributes"]["homeDirectory"]
smb_mail = ldap_users["params"]["attributes"]["mail"]
smb_uid_number = ldap_users["params"]["attributes"]["uidNumber"]
smb_gid_number = ldap_users["params"]["attributes"]["gidNumber"]
smb_givenname = ldap_users["params"]["attributes"]["givenName"] samdb.newuser(
smb_surname = ldap_users["params"]["attributes"]["sn"] username=smb_username,
smb_homedir = ldap_users["params"]["attributes"]["homeDirectory"] password=smb_passwd,
smb_mail = ldap_users["params"]["attributes"]["mail"] givenname=smb_givenname,
smb_uid_number = ldap_users["params"]["attributes"]["uidNumber"] surname=smb_surname,
smb_gid_number = ldap_users["params"]["attributes"]["gidNumber"] homedirectory=smb_homedir,
mailaddress=smb_mail,
uidnumber=smb_uid_number,
gidnumber=smb_gid_number,
useusernameascn=True
)
samdb.newuser( query = "(objectclass=user)"
username=smb_username, result = samdb.search('DC=nantes,DC=omero-fbi,DC=fr', expression=query, scope=ldb.SCOPE_SUBTREE)
password=smb_passwd, for item in result:
givenname=smb_givenname, if 'sAMAccountName' in item:
surname=smb_surname, print(item['distinguishedName'])
homedirectory=smb_homedir, print(item['sAMAccountName'])
mailaddress=smb_mail,
uidnumber=smb_uid_number,
gidnumber=smb_gid_number,
useusernameascn=True
)
query = "(objectclass=user)" for arg in sys.argv[1:]:
result = samdb.search('DC=nantes,DC=omero-fbi,DC=fr', expression=query, scope=ldb.SCOPE_SUBTREE) print(arg)
for item in result: DATA_FILE = arg
if 'sAMAccountName' in item: adduser()
print(item['distinguishedName'])
print(item['sAMAccountName'])