HEX
Server: Apache/2.2.34 (Unix) mod_fastcgi/mod_fastcgi-SNAP-0910052141
System: Linux Kou-Etsu-Dou 4.4.59+ #25556 SMP PREEMPT Thu Mar 4 18:03:46 CST 2021 x86_64
User: hosam (1026)
PHP: 7.2.29
Disabled: NONE
Upload Files
File: //etc.defaults/init/syno-auth-check.conf
author "System Integration Team"
description "When DS join directory server, check if DS can connect to domain/ldap server, and emit syno.auth.ready when connect success or timeout"

start on runlevel 1
stop on runlevel [06]

console log
task

env SZD_BOOTUP_FLAG="/run/synoservice/bootup-fail-job/"

emits syno.auth.ready

pre-start script
	SUPPORT_HA=`/bin/get_key_value /etc.defaults/synoinfo.conf support_ha` || true
	RUN_HA=`/bin/get_key_value /etc/synoinfo.conf runha` || true

	if [ "yes" = "$SUPPORT_HA" ] && [ "yes" = "$RUN_HA" ]; then
		. /var/packages/HighAvailability/target/etc/rc.subr || true
		FLAG_HA_SXX_ROLE_PREACTIVE=$SXX_ROLE_PREACTIVE || true
		FLAG_HA_SXX_ROLE_PREPASSIVE=$SXX_ROLE_PREPASSIVE || true
		if [ -f "$SYNO_HA_UPG" ]; then
			if ! $SYNOHA_BIN --upg-is-active ; then
				stop ; exit 0
			fi
		fi

		time=0;
		while true; do
			if [ "$time" -ge "1800" ]; then
				/bin/echo "HA role unknown, skip syno-auth-check" || true
				stop ; exit 0
			fi
			if [ -f "$FLAG_HA_SXX_ROLE_PREPASSIVE" ]; then
				/bin/echo "skip syno-auth-check on passive" || true
				stop ; exit 0
			fi
			if [ -f "$FLAG_HA_SXX_ROLE_PREACTIVE" ]; then
				/bin/echo "HA role active - start syno-auth-check task" || true
				exit 0
			fi

			sleep 1 || true
			time=$(($time+1)) || true
		done
	fi
end script

script
	IS_JOIN_DONAMIN=`/bin/get_key_value /etc/synoinfo.conf domainjoined` || true
	# return 1: enable, 0: not enable
	if /usr/syno/sbin/synoservice --is-enable nslcd ; then
		IS_JOIN_LDAP="no"
	else
		IS_JOIN_LDAP="yes"
	fi

	## log time for debug
	/bin/date || true

	## emit auth ready immediately if DS do not join directory server
	if [ "xyes" != "x${IS_JOIN_DONAMIN}" -a "xyes" != "x${IS_JOIN_LDAP}" ]; then
		initctl emit --no-wait syno.auth.ready CONNECTED=true || true
		stop; exit 0;
	fi

	## check if directory related daemon is ready or not
	time=0;
	while true; do
		if [ "$time" -ge "180" ]; then
			break;
		fi

		if [ "xyes" = "x${IS_JOIN_DONAMIN}" ]; then
			if [ -f "${SZD_BOOTUP_FLAG}/winbindd" ]; then
				/bin/echo "winbindd failed to start on bootup, skip waiting" || true
				break;
			fi
			if /usr/syno/sbin/synoservice --status winbindd; then
				break;
			fi
		else
			if /usr/syno/sbin/synoservice --status nslcd; then
				break;
			fi
		fi

		sleep 1
		time=$(($time+1))
	done

	## emit event
	initctl emit --no-wait syno.auth.ready || true

	## check connection every 5 sec, timeout 300s
	time=0;
	while true; do
		if [ "$time" -ge "300" ]; then
			initctl emit --no-wait syno.auth.ready TIMEOUT=true || true
			break;
		fi

		if [ "xyes" = "x${IS_JOIN_DONAMIN}" ]; then
			if /usr/bin/wbinfo -P; then
				initctl emit --no-wait syno.auth.ready CONNECTED=true || true
				break;
			fi
		else
			if /usr/syno/bin/ldap-server-check ; then
				initctl emit --no-wait syno.auth.ready CONNECTED=true || true
				break;
			fi
		fi

		sleep 5
		time=$(($time+5))
	done
end script

# vim:ft=upstart