1
0
mirror of https://github.com/samba-team/samba.git synced 2025-02-25 17:57:42 +03:00

samba-tools: more reasonable defaults for samba-tool commands

- fallback to machine account where possible

- default to local hostname where this is reasonable
This commit is contained in:
Andrew Tridgell 2010-12-08 08:20:54 +11:00
parent 249afc8906
commit 545932fda5
13 changed files with 79 additions and 53 deletions

View File

@ -151,7 +151,7 @@ class CredentialsOptions(optparse.OptionGroup):
def _set_simple_bind_dn(self, option, opt_str, arg, parser):
self.creds.set_bind_dn(arg)
def get_credentials(self, lp):
def get_credentials(self, lp, fallback_machine=False):
"""Obtain the credentials set on the command-line.
:param lp: Loadparm object to use.
@ -160,6 +160,15 @@ class CredentialsOptions(optparse.OptionGroup):
self.creds.guess(lp)
if self.no_pass:
self.creds.set_cmdline_callbacks()
# possibly fallback to using the machine account, if we have
# access to the secrets db
if fallback_machine and not self.creds.authentication_requested():
try:
self.creds.set_machine_account(lp)
except Exception:
pass
return self.creds
class CredentialsOptionsDouble(CredentialsOptions):

View File

@ -0,0 +1,24 @@
#!/usr/bin/env python
#
# common functions for samba-tool python commands
#
# Copyright Andrew Tridgell 2010
#
# 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.
#
# 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.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
def netcmd_dnsname(lp):
'''return the full DNS name of our own host. Used as a default
for hostname when running status queries'''
return lp.get('netbios name').lower() + "." + lp.get('realm').lower()

View File

@ -67,7 +67,7 @@ class cmd_domainlevel(Command):
def run(self, subcommand, H=None, forest=None, domain=None, quiet=False,
credopts=None, sambaopts=None, versionopts=None):
lp = sambaopts.get_loadparm()
creds = credopts.get_credentials(lp)
creds = credopts.get_credentials(lp, fallback_machine=True)
samdb = SamDB(url=H, session_info=system_session(),
credentials=creds, lp=lp)

View File

@ -33,7 +33,7 @@ from samba.netcmd import (
from samba.samdb import SamDB
from samba import drs_utils, nttime2string, dsdb
from samba.dcerpc import drsuapi, misc
import common
def drsuapi_connect(ctx):
'''make a DRSUAPI connection to the server'''
@ -94,7 +94,7 @@ class cmd_drs_showrepl(Command):
"credopts": options.CredentialsOptions,
}
takes_args = ["DC"]
takes_args = ["DC?"]
def print_neighbour(self, n):
'''print one set of neighbour information'''
@ -127,15 +127,14 @@ class cmd_drs_showrepl(Command):
return (info_type, info)
def run(self, DC, sambaopts=None,
def run(self, DC=None, sambaopts=None,
credopts=None, versionopts=None, server=None):
self.server = DC
self.lp = sambaopts.get_loadparm()
self.creds = credopts.get_credentials(self.lp)
if not self.creds.authentication_requested():
self.creds.set_machine_account(self.lp)
if DC is None:
DC = common.netcmd_dnsname(self.lp)
self.server = DC
self.creds = credopts.get_credentials(self.lp, fallback_machine=True)
drsuapi_connect(self)
samdb_connect(self)
@ -145,7 +144,10 @@ class cmd_drs_showrepl(Command):
server_dns = self.samdb.search(base="", scope=ldb.SCOPE_BASE, attrs=["dnsHostName"])[0]['dnsHostName'][0]
(site, server) = drs_parse_ntds_dn(ntds_dn)
ntds = self.samdb.search(base=ntds_dn, scope=ldb.SCOPE_BASE, attrs=['options', 'objectGUID', 'invocationId'])
try:
ntds = self.samdb.search(base=ntds_dn, scope=ldb.SCOPE_BASE, attrs=['options', 'objectGUID', 'invocationId'])
except Exception, e:
raise CommandError("Failed to search NTDS DN %s" % ntds_dn)
conn = self.samdb.search(base=ntds_dn, expression="(objectClass=nTDSConnection)")
print("%s\\%s" % (site, server))
@ -207,17 +209,17 @@ class cmd_drs_kcc(Command):
"credopts": options.CredentialsOptions,
}
takes_args = ["DC"]
takes_args = ["DC?"]
def run(self, DC, sambaopts=None,
def run(self, DC=None, sambaopts=None,
credopts=None, versionopts=None, server=None):
self.server = DC
self.lp = sambaopts.get_loadparm()
if DC is None:
DC = common.netcmd_dnsname(self.lp)
self.server = DC
self.creds = credopts.get_credentials(self.lp)
if not self.creds.authentication_requested():
self.creds.set_machine_account(self.lp)
self.creds = credopts.get_credentials(self.lp, fallback_machine=True)
drsuapi_connect(self)
@ -254,9 +256,7 @@ class cmd_drs_replicate(Command):
self.server = DEST_DC
self.lp = sambaopts.get_loadparm()
self.creds = credopts.get_credentials(self.lp)
if not self.creds.authentication_requested():
self.creds.set_machine_account(self.lp)
self.creds = credopts.get_credentials(self.lp, fallback_machine=True)
drsuapi_connect(self)
samdb_connect(self)
@ -292,7 +292,7 @@ class cmd_drs_replicate(Command):
try:
self.drsuapi.DsReplicaSync(self.drsuapi_handle, 1, req1)
except Exception, e:
except Exception, estr:
raise CommandError("DsReplicaSync failed", estr)
print("Replicate from %s to %s was successful." % (SOURCE_DC, DEST_DC))
@ -309,17 +309,16 @@ class cmd_drs_bind(Command):
"credopts": options.CredentialsOptions,
}
takes_args = ["DC"]
takes_args = ["DC?"]
def run(self, DC, sambaopts=None,
def run(self, DC=None, sambaopts=None,
credopts=None, versionopts=None, server=None):
self.server = DC
self.lp = sambaopts.get_loadparm()
self.creds = credopts.get_credentials(self.lp)
if not self.creds.authentication_requested():
self.creds.set_machine_account(self.lp)
if DC is None:
DC = common.netcmd_dnsname(self.lp)
self.server = DC
self.creds = credopts.get_credentials(self.lp, fallback_machine=True)
drsuapi_connect(self)
samdb_connect(self)

View File

@ -53,7 +53,7 @@ class cmd_enableaccount(Command):
filter = "(&(objectClass=user)(sAMAccountName=%s))" % (username)
lp = sambaopts.get_loadparm()
creds = credopts.get_credentials(lp)
creds = credopts.get_credentials(lp, fallback_machine=True)
samdb = SamDB(url=H, session_info=system_session(),
credentials=creds, lp=lp)

View File

@ -133,7 +133,7 @@ all=all of the above"""),
def run(self, subcommand, force=None, host=None, role=None,
credopts=None, sambaopts=None, versionopts=None):
lp = sambaopts.get_loadparm()
creds = credopts.get_credentials(lp)
creds = credopts.get_credentials(lp, fallback_machine=True)
samdb = SamDB(url=host, session_info=system_session(),
credentials=creds, lp=lp)

View File

@ -101,9 +101,7 @@ class cmd_listall(Command):
self.url = H
self.lp = sambaopts.get_loadparm()
self.creds = credopts.get_credentials(self.lp)
if not self.creds.authentication_requested():
self.creds.set_machine_account(self.lp)
self.creds = credopts.get_credentials(self.lp, fallback_machine=True)
samdb_connect(self)
@ -153,9 +151,7 @@ class cmd_list(Command):
self.url = H
self.lp = sambaopts.get_loadparm()
self.creds = credopts.get_credentials(self.lp)
if not self.creds.authentication_requested():
self.creds.set_machine_account(self.lp)
self.creds = credopts.get_credentials(self.lp, fallback_machine=True)
samdb_connect(self)

View File

@ -76,7 +76,7 @@ class cmd_group_add(Command):
gtype = distribution_group.get(group_scope, GTYPE_DISTRIBUTION_GLOBAL_GROUP)
lp = sambaopts.get_loadparm()
creds = credopts.get_credentials(lp)
creds = credopts.get_credentials(lp, fallback_machine=True)
try:
samdb = SamDB(url=H, session_info=system_session(),
@ -107,7 +107,7 @@ class cmd_group_delete(Command):
def run(self, groupname, credopts=None, sambaopts=None, versionopts=None, H=None):
lp = sambaopts.get_loadparm()
creds = credopts.get_credentials(lp)
creds = credopts.get_credentials(lp, fallback_machine=True)
try:
samdb = SamDB(url=H, session_info=system_session(),
@ -138,7 +138,7 @@ class cmd_group_add_members(Command):
versionopts=None, H=None):
lp = sambaopts.get_loadparm()
creds = credopts.get_credentials(lp)
creds = credopts.get_credentials(lp, fallback_machine=True)
try:
samdb = SamDB(url=H, session_info=system_session(),
@ -169,7 +169,7 @@ class cmd_group_remove_members(Command):
versionopts=None, H=None):
lp = sambaopts.get_loadparm()
creds = credopts.get_credentials(lp)
creds = credopts.get_credentials(lp, fallback_machine=True)
try:
samdb = SamDB(url=H, session_info=system_session(),

View File

@ -770,7 +770,7 @@ class cmd_ldapcmp(Command):
two=False, quiet=False, verbose=False, descriptor=False, view="section",
credopts=None, sambaopts=None, versionopts=None):
lp = sambaopts.get_loadparm()
creds = credopts.get_credentials(lp)
creds = credopts.get_credentials(lp, fallback_machine=True)
creds2 = credopts.get_credentials2(lp, False)
if creds2.is_anonymous():
creds2 = creds

View File

@ -39,7 +39,7 @@ class cmd_machinepw(Command):
def run(self, secret, sambaopts=None, credopts=None, versionopts=None):
lp = sambaopts.get_loadparm()
creds = credopts.get_credentials(lp)
creds = credopts.get_credentials(lp, fallback_machine=True)
url = lp.get("secrets database")
secretsdb = Ldb(url=url, session_info=system_session(),
credentials=creds, lp=lp)

View File

@ -77,11 +77,7 @@ class cmd_rodc_preload(Command):
lp = sambaopts.get_loadparm()
creds = credopts.get_credentials(lp)
if not creds.authentication_requested():
print "Using machine account"
creds.set_machine_account(lp)
creds = credopts.get_credentials(lp, fallback_machine=True)
# connect to the remote and local SAMs
samdb = SamDB(url="ldap://%s" % server,

View File

@ -19,7 +19,7 @@
#
import samba.getopt as options
import common
from samba.net import Net
from samba.netcmd import (
@ -36,10 +36,12 @@ class cmd_time(Command):
"versionopts": options.VersionOptions,
}
takes_args = ["server_name"]
takes_args = ["server_name?"]
def run(self, server_name, credopts=None, sambaopts=None, versionopts=None):
def run(self, server_name=None, credopts=None, sambaopts=None, versionopts=None):
lp = sambaopts.get_loadparm()
creds = credopts.get_credentials(lp)
creds = credopts.get_credentials(lp, fallback_machine=True)
net = Net(creds, lp, server=credopts.ipaddress)
if server_name is None:
server_name = common.netcmd_dnsname(lp)
print net.time(server_name)

View File

@ -41,7 +41,7 @@ class cmd_user_add(Command):
def run(self, name, password=None, credopts=None, sambaopts=None, versionopts=None):
lp = sambaopts.get_loadparm()
creds = credopts.get_credentials(lp)
creds = credopts.get_credentials(lp )
net = Net(creds, lp, server=credopts.ipaddress)
net.create_user(name)
if password is not None:
@ -62,7 +62,7 @@ class cmd_user_delete(Command):
def run(self, name, credopts=None, sambaopts=None, versionopts=None):
lp = sambaopts.get_loadparm()
creds = credopts.get_credentials(lp)
creds = credopts.get_credentials(lp, fallback_machine=True)
net = Net(creds, lp, server=credopts.ipaddress)
net.delete_user(name)