2018-12-12 03:43:21 +03:00
#!/usr/bin/env python3
2008-05-22 07:13:31 +04:00
# -*- coding: utf-8 -*-
# Unix SMB/CIFS implementation.
# Copyright © Jelmer Vernooij <jelmer@samba.org> 2008
#
# Based on samr.js © Andrew Tridgell <tridge@samba.org>
2018-07-30 09:22:52 +03:00
#
2008-05-22 07:13:31 +04:00
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
2018-07-30 09:22:52 +03:00
#
2008-05-22 07:13:31 +04:00
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
2018-07-30 09:22:52 +03:00
#
2008-05-22 07:13:31 +04:00
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
import sys
sys . path . insert ( 0 , " bin/python " )
2009-07-30 22:04:42 +04:00
from samba . dcerpc import samr , security
2008-05-22 07:13:31 +04:00
2018-07-30 09:20:39 +03:00
2009-01-08 00:40:49 +03:00
def display_lsa_string ( str ) :
return str . string
2018-07-30 09:20:39 +03:00
2008-05-22 07:13:31 +04:00
def FillUserInfo ( samr , dom_handle , users , level ) :
""" fill a user array with user information from samrQueryUserInfo """
for i in range ( len ( users ) ) :
user_handle = samr . OpenUser ( handle , security . SEC_FLAG_MAXIMUM_ALLOWED , users [ i ] . idx )
info = samr . QueryUserInfo ( user_handle , level )
info . name = users [ i ] . name
info . idx = users [ i ] . idx
users [ i ] = info
samr . Close ( user_handle )
2018-07-30 09:20:39 +03:00
2022-05-05 12:07:36 +03:00
def toArray ( handle , array , num_entries ) :
2008-05-22 07:13:31 +04:00
ret = [ ]
for x in range ( num_entries ) :
ret . append ( ( array . entries [ x ] . idx , array . entries [ x ] . name ) )
return ret
2018-07-30 09:13:57 +03:00
2008-05-22 07:13:31 +04:00
def test_Connect ( samr ) :
""" test the samr_Connect interface """
2022-05-05 12:07:36 +03:00
print ( " Testing samr_Connect " )
2008-05-22 07:13:31 +04:00
return samr . Connect2 ( None , security . SEC_FLAG_MAXIMUM_ALLOWED )
2018-07-30 09:20:39 +03:00
2008-05-22 07:13:31 +04:00
def test_LookupDomain ( samr , handle , domain ) :
""" test the samr_LookupDomain interface """
2022-05-05 12:07:36 +03:00
print ( " Testing samr_LookupDomain " )
2008-05-22 07:13:31 +04:00
return samr . LookupDomain ( handle , domain )
2018-07-30 09:20:39 +03:00
2008-05-22 07:13:31 +04:00
def test_OpenDomain ( samr , handle , sid ) :
""" test the samr_OpenDomain interface """
2022-05-05 12:07:36 +03:00
print ( " Testing samr_OpenDomain " )
2008-05-22 07:13:31 +04:00
return samr . OpenDomain ( handle , security . SEC_FLAG_MAXIMUM_ALLOWED , sid )
2018-07-30 09:20:39 +03:00
2008-05-22 07:13:31 +04:00
def test_EnumDomainUsers ( samr , dom_handle ) :
""" test the samr_EnumDomainUsers interface """
2022-05-05 12:07:36 +03:00
print ( " Testing samr_EnumDomainUsers " )
users = toArray ( * samr . EnumDomainUsers ( dom_handle , 0 , 0 , 0xffffffff ) )
print ( " Found %d users " % len ( users ) )
2008-05-22 07:13:31 +04:00
for idx , user in users :
2022-05-05 12:07:36 +03:00
print ( " \t %s \t ( %d ) " % ( user . string , idx ) )
2008-05-22 07:13:31 +04:00
2018-07-30 09:20:39 +03:00
2008-05-22 07:13:31 +04:00
def test_EnumDomainGroups ( samr , dom_handle ) :
""" test the samr_EnumDomainGroups interface """
2022-05-05 12:07:36 +03:00
print ( " Testing samr_EnumDomainGroups " )
groups = toArray ( * samr . EnumDomainGroups ( dom_handle , 0 , 0 ) )
print ( " Found %d groups " % len ( groups ) )
2008-05-22 07:13:31 +04:00
for idx , group in groups :
2022-05-05 12:07:36 +03:00
print ( " \t %s \t ( %d ) " % ( group . string , idx ) )
2008-05-22 07:13:31 +04:00
2018-07-30 09:20:39 +03:00
2008-05-22 07:13:31 +04:00
def test_domain_ops ( samr , dom_handle ) :
""" test domain specific ops """
test_EnumDomainUsers ( samr , dom_handle )
test_EnumDomainGroups ( samr , dom_handle )
2018-07-30 09:20:39 +03:00
2008-05-22 07:13:31 +04:00
def test_EnumDomains ( samr , handle ) :
""" test the samr_EnumDomains interface """
2022-05-05 12:07:36 +03:00
print ( " Testing samr_EnumDomains " )
2008-05-22 07:13:31 +04:00
2022-05-05 12:07:36 +03:00
domains = toArray ( * samr . EnumDomains ( handle , 0 , 0xffffffff ) )
print ( " Found %d domains " % len ( domains ) )
2008-05-22 07:13:31 +04:00
for idx , domain in domains :
2022-05-05 12:07:36 +03:00
print ( " \t %s ( %d ) " % ( display_lsa_string ( domain ) , idx ) )
2008-05-22 07:13:31 +04:00
for idx , domain in domains :
2022-05-05 12:07:36 +03:00
print ( " Testing domain %s " % display_lsa_string ( domain ) )
2008-05-22 07:13:31 +04:00
sid = samr . LookupDomain ( handle , domain )
dom_handle = test_OpenDomain ( samr , handle , sid )
test_domain_ops ( samr , dom_handle )
samr . Close ( dom_handle )
2018-07-30 09:21:29 +03:00
2008-05-22 07:13:31 +04:00
if len ( sys . argv ) != 2 :
2022-05-05 12:07:36 +03:00
print ( " Usage: samr.js <BINDING> " )
2018-07-30 09:13:57 +03:00
sys . exit ( 1 )
2008-05-22 07:13:31 +04:00
binding = sys . argv [ 1 ]
2022-05-05 12:07:36 +03:00
print ( " Connecting to %s " % binding )
2008-05-22 07:13:31 +04:00
try :
samr = samr . samr ( binding )
2022-05-05 12:07:36 +03:00
except Exception as e :
print ( " Failed to connect to %s : %s " % ( binding , e . message ) )
2008-05-22 07:13:31 +04:00
sys . exit ( 1 )
handle = test_Connect ( samr )
test_EnumDomains ( samr , handle )
samr . Close ( handle )
2022-05-05 12:07:36 +03:00
print ( " All OK " )