File: //opt/Kaseya/TESTJS72184235756529/libexec/KaGdmUser
#! /bin/bash
# Run with a restricted path...
export PATH=/bin:/usr/bin:/sbin:/usr/sbin:/etc:/opt/gnome/bin:/opt/gnome/sbin
getAgentRoot() {
local p=${BASH_SOURCE[0]}
whereami=getAgentRoot
if [ "${p:0:1}" == "/" ]
then
(command cd $(dirname "$p")/..; echo $PWD)
else
(command cd $(dirname "$PWD/$p")/..; echo $PWD)
fi
}
AgentRoot=$(getAgentRoot)
AgentInstanceGuid=$(basename "$AgentRoot")
CommandOption=Install
Debug=
User=${User:-$USER}
Group=
Home=${Home:-$HOME}
Pid=$$
[ -r "$AgentRoot/tmp/KcsBashX" ] && set -x
Usage() {
whereami=Usage
if [ -n "$1" ]
then
echo "$1"
echo ""
fi
echo "Usage: KaGdmUser [ options ]"
echo ""
echo "Options:"
echo " --install - Add KaUsrTsk in this user's gnome-panel"
echo " --uninstall - Remove KaUsrTsk from this user's gnome-panel"
echo " -u - Set the user's username"
echo " -g - Set the user's group"
echo " -h - Set the user's home directory"
echo " -? - Show usage information"
}
errlog() {
whereami=errlog
logger -t "[KaGdmUser][$Pid]" $Verbose -p "user.err" "$1"
}
syslog() {
whereami=syslog
if [ -n "$Debug" ]
then
logger -t "[KaGdmUser][$Pid]" $Verbose -p "user.debug" "$1"
fi
}
while [ -n "$1" ]
do
case "$1" in
--install) CommandOption=Install ;;
--uninstall) CommandOption=Uninstall ;;
-u) User=$2; shift ;;
-g) Group=$2; shift ;;
-h) Home=$2; shift ;;
-V) Verbose="-s" ;;
-D) Debug=true ;;
-x) set -x ;;
-?) Usage; exit 1 ;;
-*) Usage "Unexpected option: \"$1\""; exit 1 ;;
*) Usage "Unexpected argument \"$1\""; exit 1 ;;
esac
shift
done
syslog "Starting KaGdmUser at $(date)"
if [ -z "$Group" ]
then
Group=${Group:-$(id -gn $User)}
fi
if [ -z "$User" -o -z "$Group" -o -z "$Home" ]
then
Usage "Error: Missing user, group, or home directory."
errlog "Error: Missing user, group, or home directory.\n"
exit 1;
fi
if [ -d "/etc/gconf" ]
then
ETC_GCONF=/etc/gconf
elif [ -d "/etc/opt/gnome/gconf" ]
then
ETC_GCONF=/etc/opt/gnome/gconf
else
errlog "Error: Could not find gconf directory.\n"
exit 1
fi
MyGCTool="gconftool-2 --direct --config-source=xml:readwrite:$Home/.gconf"
DefGCTool="gconftool-2 --direct --config-source=xml:readwrite:$ETC_GCONF/gconf.xml.defaults"
# OpenSUSE 11.0 adds a line starting with "Resolved address" which isn't useful, so we
# strip it off. We also divet stderr to /dev/null so no output goes to a terminal if
# run from a console.
gcToolGet() {
whereami=ccToolGet
gconftool-2 --direct --config-source=xml:readwrite:$HOME/.gconf --get $1 2> /dev/null | \
grep -v '^Resolved address'
}
defGcToolGet() {
whereami=defGcToolGet
gconftool-2 --direct --config-source=xml:readwrite:$ETC_GCONF/gconf.xml.defaults --get $1 2> /dev/null | \
grep -v '^Resolved address'
}
getPreferredPanel() {
local toplevel_id_list=$(gcToolGet /apps/panel/general/toplevel_id_list)
local plist=
local panel=
local oldIFS=$IFS
local firstPanel=
whereami=getPreferredPanel
if [ -z "$toplevel_id_list" ]
then
toplevel_id_list=$(defGcToolGet /apps/panel/default_setup/general/toplevel_id_list)
syslog "getPreferredPanel: Got toplevel_id_list from default_setup."
fi
if [ -n "$toplevel_id_list" ]
then
plist=${toplevel_id_list:1}
plist=${plist/\]/}
if [ -n "$plist" ]
then
IFS=","
set $plist
for p
do
if [ -z "$firstPanel" ]
then
firstPanel="$p"
fi
case "$p" in
*bottom*) panel=$p ;;
esac
done
IFS=$oldIFS
fi
else
syslog "getPrefferedPanel: Did not find toplevel_id_list."
fi
[ -n "$firstPanel" ] && panel=$firstPanel
syslog "getPreferredPanel: Returning \"$panel\"."
echo "$panel"
}
createGConfDir() {
local panel="$1"
local UsrGrp="$2"
local gdir="$3"
local kadir="$gdir/apps/panel/applets/KaUsrTskApplet_$AgentInstanceGuid"
whereami=createGConfDir
if [ ! -d "$kadir" ]
then
mkdir -p "$kadir"
if [ -d "$kadir" ]
then
syslog "createGConfDir: Created $kadir"
if [ ! -r "$kadir/%gconf.xml" ]
then
mtime=$(date +%s)
sed \
-e "s/%MTIME%/$mtime/g" \
-e "s/%AGENT_INSTANCE_GUID%/$AgentInstanceGuid/g" \
-e "s/%AGENT_APPLET_PANEL%/$panel/g" \
"$AgentRoot/share/GdmUserTemplate.xml" > "$kadir/%gconf.xml"
syslog "createGConfDir: Created $kadir/%gconf.xml"
fi
return 0
else
errlog "Error: Failed to create directory \"$kadir\"."
return 1
fi
else
syslog "createGConfDir: Nothing to do (KaUsrTskApplet_${AgentInstanceGuid} already exists)."
return 0
fi
}
if [ -d "$Home/.gconf" ]
then
if [ "$CommandOption" == "Install" ]
then
syslog "Adding KaUsrTsk_$AgentInstanceGuid to $User's gnome-panel."
panel=$(getPreferredPanel)
if [ -z "$panel" ]
then
syslog "Warning: Failed to find a panel to install into."
elif createGConfDir "$panel" "$User:$Group" $Home/.gconf
then
applet_id_list=$($MyGCTool --get /apps/panel/general/applet_id_list 2> /dev/null | tail -n 1)
ka_name="KaUsrTskApplet_$AgentInstanceGuid"
if [ -z "$applet_id_list" ]
then
applet_id_list=$($DefGCTool --get /apps/panel/default_setup/general/applet_id_list 2> /dev/null | tail -n 1 | perl \
-e 'chomp($_ = <>);' \
-e "\$applet = \"KaUsrTskApplet_$AgentInstanceGuid\";" \
-e '@list = split /[,\[\]]/;' \
-e 'foreach $elem (@list) { if ("$elem" && "$applet" ne "$elem") { $alist = "$alist$elem,"; }}' \
-e 'chop $alist;' \
-e 'print "[$applet,$alist]";')
else
applet_id_list=$(echo "$applet_id_list" | perl \
-e 'chomp($_ = <>);' \
-e "\$applet = \"KaUsrTskApplet_$AgentInstanceGuid\";" \
-e '@list = split /[,\[\]]/;' \
-e 'foreach $elem (@list) { if ("$elem" && "$applet" ne "$elem") { $alist = "$alist$elem,"; }}' \
-e 'chop $alist;' \
-e 'print "[$applet,$alist]";')
fi
$MyGCTool --type list --list-type=string --set /apps/panel/general/applet_id_list "$applet_id_list" 2> /dev/null > /dev/null
chown -R "$User:$Group" $Home/.gconf
syslog "Chowning \"$Home/.gconf\" to \"$User:$Group\""
else
syslog "Warning: creatGConfDir failed."
fi
else # "$CommandOption" == "Uninstall"
syslog "Uninstalling KaUsrTsk_$AgentInstanceGuid from $User's gnome-panel."
applet_id_list=$($MyGCTool --get /apps/panel/general/applet_id_list 2> /dev/null | tail -n 1)
ka_name="KaUsrTskApplet_$AgentInstanceGuid"
if [ -n "$applet_id_list" ]
then
applet_id_list=$(echo "$applet_id_list" | perl \
-e 'chomp($_ = <>);' \
-e "\$applet = \"KaUsrTskApplet_$AgentInstanceGuid\";" \
-e '@list = split /[,\[\]]/;' \
-e 'foreach $elem (@list) { if ("$elem" && "$applet" ne "$elem") { $alist = "$alist$elem,"; }}' \
-e 'chop $alist;' \
-e 'print "[$alist]";')
$MyGCTool --type list --list-type=string --set /apps/panel/general/applet_id_list "$applet_id_list" 2> /dev/null > /dev/null
rm -rf "$Home/.gconf/apps/panel/applets/KaUsrTskApplet_$AgentInstanceGuid"
fi
fi
else
syslog "Not adding KaUsrTsk to this users gnome-panel because this is a first login."
fi