This is the mail archive of the cygwin mailing list for the Cygwin project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: ssh + patch + $TMP


Kurt Franke a écrit :
-----Ursprüngliche Nachricht-----
Von: Cyrille Lefevre <cyrille.lefevre-lists@laposte.net>
Gesendet: 06.03.2010 23:30:57
An: cygwin@cygwin.com,Kurt-Franke@web.de
Betreff: Re: ssh + patch + $TMP

the attached script only fork 5 processes (ps, grep, mount, find and awk), and does not use any temporary files, so, it should not degrade ssh logins performance.
existing environment variables are not overwritten.

<snip things I known for a long time, now, about 20 years of unix behind me..., but you couldn't known that, so, don't care>


thus you must write the environment settings build in awk script from registry to a file and source it at shell level after awk script has finished.

you are wrong here, no need for an output file to be sourced, you miss the eval statement which interpret the output of the awk script within the current shell. however, I've fixed a small bug here, it should be eval "$(...)" and not eval $(...).


and, of course, not all all existing environment variables should be preserved.

as said before, only non existing variables are exported. however, I've added an array (override) of environment variables to override.


in the special those with bad values must get a correct value.

all windows variables are converted to cygwin ones using cygpath -u like translation within awk. however, I've added an array (nocygpath) of variables to not translate.


for example TMP and TEMP have invalid values after a logon via sshd - they are set
to the value used by the user which is running the sshd and to the value of
the actual user.

original TEMP variable :


TEMP="/cygdrive/c/Users/CYG_SE~1/AppData/Local/Temp"

yours (wrong) :

TEMP="/cygdrive/c/AppData/Local/Temp"

mine w/ override=TEMP :

TEMP="/cygdrive/c/Users/Cyrille/AppData/Local/Temp"

also, you don't handle ComSpec, nor Path and windir upper case conversion as cygwin does and the Path content isn't added to the current PATH one as windows does (improved w/ no duplicates).

if running with a windows version less 6.0 in a sshd session USERNAME and USERDOMAIN
have the values belonging to the SYSTEM user and not the values of the actual user.

did you setup cyglsa ? what is the value of whoami from the "Windows XP Support Tools" ? => /cygdrive/c/program\ files/support\ tools/whoami under Vista, it's /cygdrive/c/windows/system32/whoami

may be some others have also invalid values.
I think it is better to preserve a known list of variables and overwrite all others.

whatever you wanted, the idea was here and I just wanted to help you to optimize your work.
anyway, I do it for fun, so, please, be more gentle next time and don't fire someone else for whatever reason.


your script takes 15s while mine takes 1s only, so, learn about it.

Regards,

Cyrille Lefevre
--
mailto:Cyrille.Lefevre-lists@laposte.net
#!/bin/sh
#
# ssh-session-env.sh - script for installation in /etc/profile.d
#
# because in sessions started from sshd the windows system environment
# variables in general are not set except for some which are possible
# special handled (like PATH, etc.) and the windows user environment
# variables are not set from the actual user but from the user of the
# sshd server this script is used to build this environment settings in
# shells with bournish syntax which uses /etc/profile for initialization.
#
# authors: Kurt Franke, Cyrille Lefevre
#
# date:   06 march 2010

if ps -fp ${PPID} | grep -q "/usr/sbin/sshd$"; then
	_SECONDS_=${SECONDS:-$(date +%s)}
	_IFS_=${IFS}
	IFS='
'
	eval "$(awk -v q="'" -v mp="$(mount --show-cygdrive-prefixes)" '
function s2a(str, a, sep,	i, t) {
	if (sep == "") sep = " +"
	split(str, t, sep)
	for (i in t) a[t[i]] = ""
}
function uniqp(p,	i, j, k, o, n, a, s) {
	k = split(p, o, ":")
	for (i = j = 1; i <= k; i++)
		if (!(tolower(o[i]) in a))
			a[tolower(n[j++] = o[i])] = ""
	p = s = ""
	for (i = 1; i < j; i++) {
		p = p s n[i]
		s = ":"
	}
	return p
}
BEGIN {
	s2a("APPDATA CLASSPATH QTJAVA LOCALAPPDATA USERPROFILE VS90COMNTOOLS", \
	    noconvert)
	s2a("HOMEPATH PATH TEMP TMP", override)
	sub(/.*\n/, "", mp)
	sub(/[ \t].*/, "", mp)
}
{
	var = FILENAME
	sub(".*/", "", var)
	var = toupper(var)
	sub("\0$", "")
	environ[var] = var == "PATH" && var in environ ? \
		environ[var] ";" $0 : $0
}
END {
	flag = 1
	while (flag) {
		flag = 0
		for (var in environ) {
			val = environ[var]
			if (match(val,/%[^%]+%/)) {
				flag = 1
				subvar = substr(val, RSTART+1, RLENGTH-2)
				subvar = toupper(subvar)
				subval = subvar in environ ? \
					environ[subvar] : ENVIRON[subvar]
				if (subval !~ /%[^%]%/) {
					head = substr(val, 1, RSTART-1)
					tail = substr(val, RSTART+RLENGTH)
					environ[var] = head subval tail
				}
			}
		}
	}
	for (var in environ) {
		if (!(var in override) && var in ENVIRON)
			continue
		val = environ[var]
		if (!(var in noconvert) && (val ~ /;/ || val ~ /^.:/)) {
			gsub(/([a-zA-Z]):/, mp "/&", val)
			gsub(/:/,"",val)
			gsub(/\\/,"/",val)
			gsub(/;/,":",val)
		}
		if (var == "PATH") val = uniqp(ENVIRON[var] ":" val)
		gsub(q, "\\"q, val)
		print "export", var "="q val q
	}
}
' /proc/registry/HKEY_LOCAL_MACHINE/SYSTEM/CurrentControlSet/Control/Session\ Manager/Environment/* \
  /proc/registry/HKEY_CURRENT_USER/Environment/* \
  $(find /proc/registry/HKEY_CURRENT_USER/Volatile\ Environment -type f 2>/dev/null))"
	echo "elapsed: $(( ${SECONDS:-$(date +%s)} - $_SECONDS_ ))s"
	IFS=${_IFS_}
	unset _IFS_ _SECONDS_
fi

# eof

--
Problem reports:       http://cygwin.com/problems.html
FAQ:                   http://cygwin.com/faq/
Documentation:         http://cygwin.com/docs.html
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]