Esempio n. 1
0
def connect():
    print time.ctime()

    baseConfig = ConfigRegistry()
    baseConfig.load()

    if '%s/s4/ldap/host' % CONFIGBASENAME not in baseConfig:
        print '%s/s4/ldap/host not set' % CONFIGBASENAME
        sys.exit(1)
    if '%s/s4/ldap/port' % CONFIGBASENAME not in baseConfig:
        print '%s/s4/ldap/port not set' % CONFIGBASENAME
        sys.exit(1)
    if '%s/s4/ldap/base' % CONFIGBASENAME not in baseConfig:
        print '%s/s4/ldap/base not set' % CONFIGBASENAME
        sys.exit(1)

    if '%s/s4/ldap/certificate' % CONFIGBASENAME not in baseConfig and not (
            '%s/s4/ldap/ssl' % CONFIGBASENAME in baseConfig
            and baseConfig['%s/s4/ldap/ssl' % CONFIGBASENAME] == 'no'):
        print '%s/s4/ldap/certificate not set' % CONFIGBASENAME
        sys.exit(1)

    if '%s/s4/listener/dir' % CONFIGBASENAME not in baseConfig:
        print '%s/s4/listener/dir not set' % CONFIGBASENAME
        sys.exit(1)

    if '%s/s4/retryrejected' % CONFIGBASENAME not in baseConfig:
        baseconfig_retry_rejected = 10
    else:
        baseconfig_retry_rejected = baseConfig['%s/s4/retryrejected' %
                                               CONFIGBASENAME]

    if baseConfig.get('%s/s4/ldap/bindpw' % CONFIGBASENAME) and os.path.exists(
            baseConfig['%s/s4/ldap/bindpw' % CONFIGBASENAME]):
        s4_ldap_bindpw = open(baseConfig['%s/s4/ldap/bindpw' %
                                         CONFIGBASENAME]).read()
        if s4_ldap_bindpw[-1] == '\n':
            s4_ldap_bindpw = s4_ldap_bindpw[0:-1]
    else:
        s4_ldap_bindpw = None

    poll_sleep = int(baseConfig['%s/s4/poll/sleep' % CONFIGBASENAME])
    s4_init = None
    while not s4_init:
        try:
            s4 = univention.s4connector.s4.s4(
                CONFIGBASENAME, mapping.s4_mapping, baseConfig,
                baseConfig['%s/s4/ldap/host' % CONFIGBASENAME],
                baseConfig['%s/s4/ldap/port' % CONFIGBASENAME],
                baseConfig['%s/s4/ldap/base' % CONFIGBASENAME],
                baseConfig.get('%s/s4/ldap/binddn' % CONFIGBASENAME,
                               None), s4_ldap_bindpw,
                baseConfig['%s/s4/ldap/certificate' % CONFIGBASENAME],
                baseConfig['%s/s4/listener/dir' % CONFIGBASENAME])
            s4_init = True
        except ldap.SERVER_DOWN:
            print "Warning: Can't initialize LDAP-Connections, wait..."
            sys.stdout.flush()
            time.sleep(poll_sleep)

    # Initialisierung auf UCS und S4 Seite durchfuehren
    s4_init = None
    ucs_init = None

    while not ucs_init:
        try:
            s4.initialize_ucs()
            ucs_init = True
        except ldap.SERVER_DOWN:
            print "Can't contact LDAP server during ucs-poll, sync not possible."
            sys.stdout.flush()
            time.sleep(poll_sleep)
            s4.open_s4()
            s4.open_ucs()

    while not s4_init:
        try:
            s4.initialize()
            s4_init = True
        except ldap.SERVER_DOWN:
            print "Can't contact LDAP server during ucs-poll, sync not possible."
            sys.stdout.flush()
            time.sleep(poll_sleep)
            s4.open_s4()
            s4.open_ucs()

    retry_rejected = 0
    connected = True
    while connected:
        print time.ctime()
        # Aenderungen pollen
        sys.stdout.flush()
        while True:
            # Read changes from OpenLDAP
            try:
                change_counter = s4.poll_ucs()
                if change_counter > 0:
                    # UCS changes, read again from UCS
                    retry_rejected = 0
                    time.sleep(1)
                    continue
                else:
                    break
            except ldap.SERVER_DOWN:
                print "Can't contact LDAP server during ucs-poll, sync not possible."
                connected = False
                sys.stdout.flush()
                break

        while True:
            try:
                change_counter = s4.poll()
                if change_counter > 0:
                    # S4 changes, read again from S4
                    retry_rejected = 0
                    time.sleep(1)
                    continue
                else:
                    break
            except ldap.SERVER_DOWN:
                print "Can't contact LDAP server during s4-poll, sync not possible."
                connected = False
                sys.stdout.flush()
                break

        try:
            if str(retry_rejected) == baseconfig_retry_rejected:
                s4.resync_rejected_ucs()
                s4.resync_rejected()
                retry_rejected = 0
            else:
                retry_rejected += 1
        except ldap.SERVER_DOWN:
            print "Can't contact LDAP server during resync rejected, sync not possible."
            connected = False
            sys.stdout.flush()
            change_counter = 0
            retry_rejected += 1

        print '- sleep %s seconds (%s/%s until resync) -' % (
            poll_sleep, retry_rejected, baseconfig_retry_rejected)
        sys.stdout.flush()
        time.sleep(poll_sleep)
    s4.close_debug()
Esempio n. 2
0
def connect():

    daemon()

    f = open(STATUSLOGFILE, 'w+')
    sys.stdout = f
    print time.ctime()

    baseConfig = univention_baseconfig.baseConfig()
    baseConfig.load()

    if not baseConfig.has_key('%s/s4/ldap/host' % CONFIGBASENAME):
        print '%s/s4/ldap/host not set' % CONFIGBASENAME
        f.close()
        sys.exit(1)
    if not baseConfig.has_key('%s/s4/ldap/port' % CONFIGBASENAME):
        print '%s/s4/ldap/port not set' % CONFIGBASENAME
        f.close()
        sys.exit(1)
    if not baseConfig.has_key('%s/s4/ldap/base' % CONFIGBASENAME):
        print '%s/s4/ldap/base not set' % CONFIGBASENAME
        f.close()
        sys.exit(1)

    if not baseConfig.has_key(
            '%s/s4/ldap/certificate' % CONFIGBASENAME) and not (
                baseConfig.has_key('%s/s4/ldap/ssl' % CONFIGBASENAME)
                and baseConfig['%s/s4/ldap/ssl' % CONFIGBASENAME] == 'no'):
        print '%s/s4/ldap/certificate not set' % CONFIGBASENAME
        f.close()
        sys.exit(1)

    if not baseConfig.has_key('%s/s4/listener/dir' % CONFIGBASENAME):
        print '%s/s4/listener/dir not set' % CONFIGBASENAME
        f.close()
        sys.exit(1)

    if not baseConfig.has_key('%s/s4/retryrejected' % CONFIGBASENAME):
        baseconfig_retry_rejected = 10
    else:
        baseconfig_retry_rejected = baseConfig['%s/s4/retryrejected' %
                                               CONFIGBASENAME]

    if baseConfig.get('%s/s4/ldap/bindpw' % CONFIGBASENAME) and os.path.exists(
            baseConfig['%s/s4/ldap/bindpw' % CONFIGBASENAME]):
        s4_ldap_bindpw = open(baseConfig['%s/s4/ldap/bindpw' %
                                         CONFIGBASENAME]).read()
        if s4_ldap_bindpw[-1] == '\n':
            s4_ldap_bindpw = s4_ldap_bindpw[0:-1]
    else:
        s4_ldap_bindpw = None

    poll_sleep = int(baseConfig['%s/s4/poll/sleep' % CONFIGBASENAME])
    s4_init = None
    while not s4_init:
        try:
            s4 = univention.s4connector.s4.s4(
                CONFIGBASENAME, mapping.s4_mapping, baseConfig,
                baseConfig['%s/s4/ldap/host' % CONFIGBASENAME],
                baseConfig['%s/s4/ldap/port' % CONFIGBASENAME],
                baseConfig['%s/s4/ldap/base' % CONFIGBASENAME],
                baseConfig.get('%s/s4/ldap/binddn' % CONFIGBASENAME,
                               None), s4_ldap_bindpw,
                baseConfig['%s/s4/ldap/certificate' % CONFIGBASENAME],
                baseConfig['%s/s4/listener/dir' % CONFIGBASENAME])
            s4_init = True
        except ldap.SERVER_DOWN:
            print "Warning: Can't initialize LDAP-Connections, wait..."
            sys.stdout.flush()
            time.sleep(poll_sleep)
            pass

    # Initialisierung auf UCS und S4 Seite durchfuehren
    s4_init = None
    ucs_init = None

    while not ucs_init:
        try:
            s4.initialize_ucs()
            ucs_init = True
        except ldap.SERVER_DOWN:
            print "Can't contact LDAP server during ucs-poll, sync not possible."
            sys.stdout.flush()
            time.sleep(poll_sleep)
            s4.open_s4()
            s4.open_ucs()
            pass

    while not s4_init:
        try:
            s4.initialize()
            s4_init = True
        except ldap.SERVER_DOWN:
            print "Can't contact LDAP server during ucs-poll, sync not possible."
            sys.stdout.flush()
            time.sleep(poll_sleep)
            s4.open_s4()
            s4.open_ucs()
            pass

    f.close()
    retry_rejected = 0
    connected = True
    while connected:
        f = open(STATUSLOGFILE, 'w+')
        sys.stdout = f
        print time.ctime()
        # Aenderungen pollen
        change_counter = 1
        while change_counter != 0:
            sys.stdout.flush()
            try:
                change_counter = s4.poll_ucs()
            except ldap.SERVER_DOWN:
                print "Can't contact LDAP server during ucs-poll, sync not possible."
                connected = False
                sys.stdout.flush()
                change_counter = 0

            try:
                change_counter += s4.poll()
            except ldap.SERVER_DOWN:
                print "Can't contact LDAP server during s4-poll, sync not possible."
                connected = False
                sys.stdout.flush()
                change_counter = 0

            if change_counter > 0:
                retry_rejected = 0

        try:
            if str(retry_rejected) == baseconfig_retry_rejected:
                s4.resync_rejected_ucs()
                s4.resync_rejected()
                retry_rejected = 0
            else:
                retry_rejected += 1
        except ldap.SERVER_DOWN:
            print "Can't contact LDAP server during resync rejected, sync not possible."
            connected = False
            sys.stdout.flush()
            change_counter = 0
            retry_rejected += 1

        print '- sleep %s seconds (%s/%s until resync) -' % (
            poll_sleep, retry_rejected, baseconfig_retry_rejected)
        sys.stdout.flush()
        time.sleep(poll_sleep)
        f.close()
    s4.close_debug()
Esempio n. 3
0
File: main.py Progetto: B-Rich/smart
def connect():

	daemon()

	f=open(STATUSLOGFILE, 'w+')
	sys.stdout=f
	print time.ctime()

	baseConfig=univention_baseconfig.baseConfig()
	baseConfig.load()

	if not baseConfig.has_key('%s/s4/ldap/host' % CONFIGBASENAME):
		print '%s/s4/ldap/host not set' % CONFIGBASENAME
		f.close()
		sys.exit(1)
	if not baseConfig.has_key('%s/s4/ldap/port' % CONFIGBASENAME):
		print '%s/s4/ldap/port not set' % CONFIGBASENAME
		f.close()
		sys.exit(1)
	if not baseConfig.has_key('%s/s4/ldap/base' % CONFIGBASENAME):
		print '%s/s4/ldap/base not set' % CONFIGBASENAME
		f.close()
		sys.exit(1)

	if not baseConfig.has_key('%s/s4/ldap/certificate' % CONFIGBASENAME) and not (baseConfig.has_key('%s/s4/ldap/ssl' % CONFIGBASENAME) and baseConfig['%s/s4/ldap/ssl' % CONFIGBASENAME] == 'no') :
		print '%s/s4/ldap/certificate not set' % CONFIGBASENAME
		f.close()
		sys.exit(1)

	if not baseConfig.has_key('%s/s4/listener/dir' % CONFIGBASENAME):
		print '%s/s4/listener/dir not set' % CONFIGBASENAME
		f.close()
		sys.exit(1)

	if not baseConfig.has_key('%s/s4/retryrejected' % CONFIGBASENAME):
		baseconfig_retry_rejected=10
	else:
		baseconfig_retry_rejected=baseConfig['%s/s4/retryrejected' % CONFIGBASENAME]

	if baseConfig.get('%s/s4/ldap/bindpw' % CONFIGBASENAME) and os.path.exists(baseConfig['%s/s4/ldap/bindpw' % CONFIGBASENAME]):
		s4_ldap_bindpw=open(baseConfig['%s/s4/ldap/bindpw' % CONFIGBASENAME]).read()
		if s4_ldap_bindpw[-1] == '\n':
			s4_ldap_bindpw=s4_ldap_bindpw[0:-1]
	else:
		s4_ldap_bindpw=None
	
	poll_sleep=int(baseConfig['%s/s4/poll/sleep' % CONFIGBASENAME])
	s4_init=None
	while not s4_init:
		try:
			s4=univention.s4connector.s4.s4(	CONFIGBASENAME,
							mapping.s4_mapping,
							baseConfig,
							baseConfig['%s/s4/ldap/host' % CONFIGBASENAME],
							baseConfig['%s/s4/ldap/port' % CONFIGBASENAME],
							baseConfig['%s/s4/ldap/base' % CONFIGBASENAME],
							baseConfig.get('%s/s4/ldap/binddn' % CONFIGBASENAME, None),
							s4_ldap_bindpw,
							baseConfig['%s/s4/ldap/certificate' % CONFIGBASENAME],
							baseConfig['%s/s4/listener/dir' % CONFIGBASENAME])
			s4_init=True
		except ldap.SERVER_DOWN:
			print "Warning: Can't initialize LDAP-Connections, wait..."
			sys.stdout.flush()
			time.sleep(poll_sleep)
			pass


	# Initialisierung auf UCS und S4 Seite durchfuehren
	s4_init=None
	ucs_init=None

	while not ucs_init:
		try:
			s4.initialize_ucs()
			ucs_init=True
		except ldap.SERVER_DOWN:
			print "Can't contact LDAP server during ucs-poll, sync not possible."
 			sys.stdout.flush()
			time.sleep(poll_sleep)
			s4.open_s4()
			s4.open_ucs()
			pass
	

	while not s4_init:
		try:
			s4.initialize()
			s4_init=True
		except ldap.SERVER_DOWN:
			print "Can't contact LDAP server during ucs-poll, sync not possible."
 			sys.stdout.flush()
			time.sleep(poll_sleep)
			s4.open_s4()
			s4.open_ucs()
			pass

	f.close()
	retry_rejected=0
	connected = True
	while connected:
		f=open(STATUSLOGFILE, 'w+')
		sys.stdout=f
		print time.ctime()
		# Aenderungen pollen
		change_counter=1
		while change_counter != 0:
			sys.stdout.flush()
			try:
				change_counter=s4.poll_ucs()			
			except ldap.SERVER_DOWN:
				print "Can't contact LDAP server during ucs-poll, sync not possible."
				connected = False
 				sys.stdout.flush()
				change_counter=0

			try:
				change_counter+=s4.poll()
			except ldap.SERVER_DOWN:
				print "Can't contact LDAP server during s4-poll, sync not possible."
				connected = False
 				sys.stdout.flush()
				change_counter=0

			if change_counter > 0:
				retry_rejected=0

		try:
			if str(retry_rejected) == baseconfig_retry_rejected:
				s4.resync_rejected_ucs()
				s4.resync_rejected()
				retry_rejected=0
			else:
				retry_rejected+=1
		except ldap.SERVER_DOWN:
			print "Can't contact LDAP server during resync rejected, sync not possible."
			connected = False
 			sys.stdout.flush()
			change_counter=0
			retry_rejected+=1

		print '- sleep %s seconds (%s/%s until resync) -'%(poll_sleep, retry_rejected, baseconfig_retry_rejected)
		sys.stdout.flush()
		time.sleep(poll_sleep)
		f.close()
	s4.close_debug()
def _connect(s4, poll_sleep, baseconfig_retry_rejected):
    # Initialisierung auf UCS und S4 Seite durchfuehren
    s4_init = None
    ucs_init = None

    while not ucs_init:
        try:
            s4.initialize_ucs()
            ucs_init = True
        except ldap.SERVER_DOWN:
            print(
                "Can't contact LDAP server during ucs-poll, sync not possible."
            )
            sys.stdout.flush()
            time.sleep(poll_sleep)
            s4.open_s4()
            s4.open_ucs()

    while not s4_init:
        try:
            s4.initialize()
            s4_init = True
        except ldap.SERVER_DOWN:
            print(
                "Can't contact LDAP server during ucs-poll, sync not possible."
            )
            sys.stdout.flush()
            time.sleep(poll_sleep)
            s4.open_s4()
            s4.open_ucs()

    retry_rejected = 0
    connected = True
    while connected:
        print(time.ctime())
        # Aenderungen pollen
        sys.stdout.flush()
        while True:
            # Read changes from OpenLDAP
            try:
                change_counter = s4.poll_ucs()
                if change_counter > 0:
                    # UCS changes, read again from UCS
                    retry_rejected = 0
                    time.sleep(1)
                    continue
                else:
                    break
            except ldap.SERVER_DOWN:
                print(
                    "Can't contact LDAP server during ucs-poll, sync not possible."
                )
                connected = False
                sys.stdout.flush()
                break

        while True:
            try:
                change_counter = s4.poll()
                if change_counter > 0:
                    # S4 changes, read again from S4
                    retry_rejected = 0
                    time.sleep(1)
                    continue
                else:
                    break
            except ldap.SERVER_DOWN:
                print(
                    "Can't contact LDAP server during s4-poll, sync not possible."
                )
                connected = False
                sys.stdout.flush()
                break

        try:
            if str(
                    retry_rejected
            ) == baseconfig_retry_rejected:  # FIXME: if the UCR variable is not set this compares string with integer (default value)
                s4.resync_rejected_ucs()
                s4.resync_rejected()
                retry_rejected = 0
            else:
                retry_rejected += 1
        except ldap.SERVER_DOWN:
            print(
                "Can't contact LDAP server during resync rejected, sync not possible."
            )
            connected = False
            sys.stdout.flush()
            change_counter = 0
            retry_rejected += 1

        print('- sleep %s seconds (%s/%s until resync) -' %
              (poll_sleep, retry_rejected, baseconfig_retry_rejected))
        sys.stdout.flush()
        time.sleep(poll_sleep)