<s> import samba . getopt as options <EOL> import ldb <EOL> from ldb import LdbError <EOL> from samba . auth import system_session <EOL> from samba . netcmd import ( <EOL> Command , <EOL> CommandError , <EOL> SuperCommand , <EOL> Option , <EOL> ) <EOL> from samba . samdb import SamDB <EOL> def transfer_role ( outf , role , samdb ) : <EOL> m = ldb . Message ( ) <EOL> m . dn = ldb . Dn ( samdb , "<STR_LIT>" ) <EOL> if role == "<STR_LIT>" : <EOL> m [ "<STR_LIT>" ] = ldb . MessageElement ( <EOL> "<STR_LIT:1>" , ldb . FLAG_MOD_REPLACE , <EOL> "<STR_LIT>" ) <EOL> elif role == "<STR_LIT>" : <EOL> domain_dn = samdb . domain_dn ( ) <EOL> res = samdb . search ( domain_dn , <EOL> scope = ldb . SCOPE_BASE , attrs = [ "<STR_LIT>" ] ) <EOL> assert len ( res ) == <NUM_LIT:1> <EOL> sid = res [ <NUM_LIT:0> ] [ "<STR_LIT>" ] [ <NUM_LIT:0> ] <EOL> m [ "<STR_LIT>" ] = ldb . MessageElement ( <EOL> sid , ldb . FLAG_MOD_REPLACE , <EOL> "<STR_LIT>" ) <EOL> elif role == "<STR_LIT>" : <EOL> m [ "<STR_LIT>" ] = ldb . MessageElement ( <EOL> "<STR_LIT:1>" , ldb . FLAG_MOD_REPLACE , <EOL> "<STR_LIT>" ) <EOL> samdb . modify ( m ) <EOL> elif role == "<STR_LIT>" : <EOL> m [ "<STR_LIT>" ] = ldb . MessageElement ( <EOL> "<STR_LIT:1>" , ldb . FLAG_MOD_REPLACE , <EOL> "<STR_LIT>" ) <EOL> elif role == "<STR_LIT>" : <EOL> m [ "<STR_LIT>" ] = ldb . MessageElement ( <EOL> "<STR_LIT:1>" , ldb . FLAG_MOD_REPLACE , <EOL> "<STR_LIT>" ) <EOL> else : <EOL> raise CommandError ( "<STR_LIT>" ) <EOL> try : <EOL> samdb . modify ( m ) <EOL> except LdbError , ( num , msg ) : <EOL> raise CommandError ( "<STR_LIT>" % ( role , msg ) ) <EOL> outf . write ( "<STR_LIT>" % role ) <EOL> class cmd_fsmo_seize ( Command ) : <EOL> """<STR_LIT>""" <EOL> synopsis = "<STR_LIT>" <EOL> takes_optiongroups = { <EOL> "<STR_LIT>" : options . SambaOptions , <EOL> "<STR_LIT>" : options . CredentialsOptions , <EOL> "<STR_LIT>" : options . VersionOptions , <EOL> } <EOL> takes_options = [ <EOL> Option ( "<STR_LIT>" , "<STR_LIT>" , help = "<STR_LIT>" , type = str , <EOL> metavar = "<STR_LIT>" , dest = "<STR_LIT:H>" ) , <EOL> Option ( "<STR_LIT>" , help = "<STR_LIT>" , action = "<STR_LIT:store_true>" ) , <EOL> Option ( "<STR_LIT>" , type = "<STR_LIT>" , choices = [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT:all>" ] , <EOL> help = """<STR_LIT>""" ) , <EOL> ] <EOL> takes_args = [ ] <EOL> def seize_role ( self , role , samdb , force ) : <EOL> res = samdb . search ( "<STR_LIT>" , <EOL> scope = ldb . SCOPE_BASE , attrs = [ "<STR_LIT>" ] ) <EOL> assert len ( res ) == <NUM_LIT:1> <EOL> serviceName = res [ <NUM_LIT:0> ] [ "<STR_LIT>" ] [ <NUM_LIT:0> ] <EOL> domain_dn = samdb . domain_dn ( ) <EOL> self . infrastructure_dn = "<STR_LIT>" + domain_dn <EOL> self . naming_dn = "<STR_LIT>" % samdb . get_config_basedn ( ) <EOL> self . schema_dn = str ( samdb . get_schema_basedn ( ) ) <EOL> self . rid_dn = "<STR_LIT>" + domain_dn <EOL> m = ldb . Message ( ) <EOL> if role == "<STR_LIT>" : <EOL> m . dn = ldb . Dn ( samdb , self . rid_dn ) <EOL> elif role == "<STR_LIT>" : <EOL> m . dn = ldb . Dn ( samdb , domain_dn ) <EOL> elif role == "<STR_LIT>" : <EOL> m . dn = ldb . Dn ( samdb , self . naming_dn ) <EOL> elif role == "<STR_LIT>" : <EOL> m . dn = ldb . Dn ( samdb , self . infrastructure_dn ) <EOL> elif role == "<STR_LIT>" : <EOL> m . dn = ldb . Dn ( samdb , self . schema_dn ) <EOL> else : <EOL> raise CommandError ( "<STR_LIT>" ) <EOL> if force is None : <EOL> self . message ( "<STR_LIT>" ) <EOL> try : <EOL> transfer_role ( self . outf , role , samdb ) <EOL> except CommandError : <EOL> self . message ( "<STR_LIT>" ) <EOL> m [ "<STR_LIT>" ] = ldb . MessageElement ( <EOL> serviceName , ldb . FLAG_MOD_REPLACE , <EOL> "<STR_LIT>" ) <EOL> else : <EOL> self . message ( "<STR_LIT>" ) <EOL> m [ "<STR_LIT>" ] = ldb . MessageElement ( <EOL> serviceName , ldb . FLAG_MOD_REPLACE , <EOL> "<STR_LIT>" ) <EOL> try : <EOL> samdb . modify ( m ) <EOL> except LdbError , ( num , msg ) : <EOL> raise CommandError ( "<STR_LIT>" % ( role , msg ) ) <EOL> self . outf . write ( "<STR_LIT>" % role ) <EOL> def run ( self , force = None , H = None , role = None , <EOL> credopts = None , sambaopts = None , versionopts = None ) : <EOL> lp = sambaopts . get_loadparm ( ) <EOL> creds = credopts . get_credentials ( lp , fallback_machine = True ) <EOL> samdb = SamDB ( url = H , session_info = system_session ( ) , <EOL> credentials = creds , lp = lp ) <EOL> if role == "<STR_LIT:all>" : <EOL> self . seize_role ( "<STR_LIT>" , samdb , force ) <EOL> self . seize_role ( "<STR_LIT>" , samdb , force ) <EOL> self . seize_role ( "<STR_LIT>" , samdb , force ) <EOL> self . seize_role ( "<STR_LIT>" , samdb , force ) <EOL> self . seize_role ( "<STR_LIT>" , samdb , force ) <EOL> else : <EOL> self . seize_role ( role , samdb , force ) <EOL> class cmd_fsmo_show ( Command ) : <EOL> """<STR_LIT>""" <EOL> synopsis = "<STR_LIT>" <EOL> takes_optiongroups = { <EOL> "<STR_LIT>" : options . SambaOptions , <EOL> "<STR_LIT>" : options . CredentialsOptions , <EOL> "<STR_LIT>" : options . VersionOptions , <EOL> } <EOL> takes_options = [ <EOL> Option ( "<STR_LIT>" , "<STR_LIT>" , help = "<STR_LIT>" , type = str , <EOL> metavar = "<STR_LIT>" , dest = "<STR_LIT:H>" ) , <EOL> ] <EOL> takes_args = [ ] <EOL> def run ( self , H = None , credopts = None , sambaopts = None , versionopts = None ) : <EOL> lp = sambaopts . get_loadparm ( ) <EOL> creds = credopts . get_credentials ( lp , fallback_machine = True ) <EOL> samdb = SamDB ( url = H , session_info = system_session ( ) , <EOL> credentials = creds , lp = lp ) <EOL> domain_dn = samdb . domain_dn ( ) <EOL> self . infrastructure_dn = "<STR_LIT>" + domain_dn <EOL> self . naming_dn = "<STR_LIT>" % samdb . get_config_basedn ( ) <EOL> self . schema_dn = samdb . get_schema_basedn ( ) <EOL> self . rid_dn = "<STR_LIT>" + domain_dn <EOL> res = samdb . search ( self . infrastructure_dn , <EOL> scope = ldb . SCOPE_BASE , attrs = [ "<STR_LIT>" ] ) <EOL> assert len ( res ) == <NUM_LIT:1> <EOL> self . infrastructureMaster = res [ <NUM_LIT:0> ] [ "<STR_LIT>" ] [ <NUM_LIT:0> ] <EOL> res = samdb . search ( domain_dn , <EOL> scope = ldb . SCOPE_BASE , attrs = [ "<STR_LIT>" ] ) <EOL> assert len ( res ) == <NUM_LIT:1> <EOL> self . pdcEmulator = res [ <NUM_LIT:0> ] [ "<STR_LIT>" ] [ <NUM_LIT:0> ] <EOL> res = samdb . search ( self . naming_dn , <EOL> scope = ldb . SCOPE_BASE , attrs = [ "<STR_LIT>" ] ) <EOL> assert len ( res ) == <NUM_LIT:1> <EOL> self . namingMaster = res [ <NUM_LIT:0> ] [ "<STR_LIT>" ] [ <NUM_LIT:0> ] <EOL> res = samdb . search ( self . schema_dn , <EOL> scope = ldb . SCOPE_BASE , attrs = [ "<STR_LIT>" ] ) <EOL> assert len ( res ) == <NUM_LIT:1> <EOL> self . schemaMaster = res [ <NUM_LIT:0> ] [ "<STR_LIT>" ] [ <NUM_LIT:0> ] <EOL> res = samdb . search ( self . rid_dn , <EOL> scope = ldb . SCOPE_BASE , attrs = [ "<STR_LIT>" ] ) <EOL> assert len ( res ) == <NUM_LIT:1> <EOL> self . ridMaster = res [ <NUM_LIT:0> ] [ "<STR_LIT>" ] [ <NUM_LIT:0> ] <EOL> self . message ( "<STR_LIT>" + self . infrastructureMaster ) <EOL> self . message ( "<STR_LIT>" + self . ridMaster ) <EOL> self . message ( "<STR_LIT>" + self . pdcEmulator ) <EOL> self . message ( "<STR_LIT>" + self . namingMaster ) <EOL> self . message ( "<STR_LIT>" + self . schemaMaster ) <EOL> class cmd_fsmo_transfer ( Command ) : <EOL> """<STR_LIT>""" <EOL> synopsis = "<STR_LIT>" <EOL> takes_optiongroups = { <EOL> "<STR_LIT>" : options . SambaOptions , <EOL> "<STR_LIT>" : options . CredentialsOptions , <EOL> "<STR_LIT>" : options . VersionOptions , <EOL> } <EOL> takes_options = [ <EOL> Option ( "<STR_LIT>" , "<STR_LIT>" , help = "<STR_LIT>" , type = str , <EOL> metavar = "<STR_LIT>" , dest = "<STR_LIT:H>" ) , <EOL> Option ( "<STR_LIT>" , type = "<STR_LIT>" , choices = [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT:all>" ] , <EOL> help = """<STR_LIT>""" ) , <EOL> ] <EOL> takes_args = [ ] <EOL> def run ( self , force = None , H = None , role = None , <EOL> credopts = None , sambaopts = None , versionopts = None ) : <EOL> lp = sambaopts . get_loadparm ( ) <EOL> creds = credopts . get_credentials ( lp , fallback_machine = True ) <EOL> samdb = SamDB ( url = H , session_info = system_session ( ) , <EOL> credentials = creds , lp = lp ) <EOL> if role == "<STR_LIT:all>" : <EOL> transfer_role ( self . outf , "<STR_LIT>" , samdb ) <EOL> transfer_role ( self . outf , "<STR_LIT>" , samdb ) <EOL> transfer_role ( self . outf , "<STR_LIT>" , samdb ) <EOL> transfer_role ( self . outf , "<STR_LIT>" , samdb ) <EOL> transfer_role ( self . outf , "<STR_LIT>" , samdb ) <EOL> else : <EOL> transfer_role ( self . outf , role , samdb ) <EOL> class cmd_fsmo ( SuperCommand ) : <EOL> """<STR_LIT>""" <EOL> subcommands = { } <EOL> subcommands [ "<STR_LIT>" ] = cmd_fsmo_seize ( ) <EOL> subcommands [ "<STR_LIT>" ] = cmd_fsmo_show ( ) <EOL> subcommands [ "<STR_LIT>" ] = cmd_fsmo_transfer ( ) </s>
