source: home-linker/trunk/fuentes/home-linker.install/usr/sbin/home-linker @ 2713

Last change on this file since 2713 was 2713, checked in by daduve, 3 years ago

Change name for diversion

  • Property svn:executable set to *
File size: 9.0 KB
Line 
1#!/bin/sh
2set -x
3
4
5echo
6
7LOG="/var/log/home_linker.log"
8echo "" > "$LOG"
9echo "Username -> $1" >> "$LOG"
10
11trim() {
12    local var="$*"
13    var="${var#"${var%%[![:space:]]*}"}"   # remove leading whitespace characters
14    var="${var%"${var##*[![:space:]]}"}"   # remove trailing whitespace characters
15    echo -n "$var"
16}
17
18#SET VARIABLES FOR THE ENVIRONMENT
19
20#GET VARIABLES
21USER=$1
22HL_USER=$(trim "$USER")
23
24if [ ! -z "$2" ]; then
25                HL_HOME="$2"   
26        else
27                NHOME=$(python -c "
28tmp='$HOME'.split('/')
29last=tmp[-1]
30print '/'.join(tmp[:-1])+'/'+last.strip()
31")
32
33                HL_HOME="$NHOME"
34fi
35
36
37if [ ! -z "$LDM_LANGUAGE" ]; then
38                AUX_LANG="$LDM_LANGUAGE"       
39        else
40                AUX_LANG="$LANG"
41fi
42
43
44
45# From this file get $LANG
46{ [ -r "$HL_HOME/.pam_environment" ] && . "$HL_HOME/.pam_environment" ;} || { [ -r "/etc/default/locale" ] && . "/etc/default/locale" ;} || LANG="en"
47
48
49# Get Usernames
50GRP_USER="$(id -ng $HL_USER)"
51
52# Source for user-dirs if it exists
53USER_DIRS="$HL_HOME/.config/user-dirs.dirs"
54[ ! -r $USER_DIRS ] || . $USER_DIRS
55
56# Source the token file if it exists
57LAST_LOGIN_DIRECTORIES="$HL_HOME/.config/user-dirs.created"
58[ ! -r "$LAST_LOGIN_DIRECTORIES" ] || . "$LAST_LOGIN_DIRECTORIES"
59
60#Temp file with new user-dirs
61[ -d "$HL_HOME" ] || { mkdir -p "$HL_HOME" ; chown $HL_USER:$GRP_USER "$HL_HOME" ;}
62[ -d "$HL_HOME/.config" ] || { mkdir -p "$HL_HOME/.config" ; chown $HL_USER:$GRP_USER "$HL_HOME/.config" ;}
63TEMP_NEW_USER_DIRS="$HL_HOME/.config/aux-user-dirs.create"
64:> $TEMP_NEW_USER_DIRS
65
66#set Acl on Home to be more restricted
67get_acl_from_server "$HL_HOME" "$GRP_USER" $HL_USER
68
69# Download directory actions
70DOWNLOADS_LOCALE="$(/usr/bin/xdg_translator "$AUX_LANG" "Downloads")"
71DESKTOP_LOCALE="$(/usr/bin/xdg_translator "$AUX_LANG"  "Desktop")"
72DOCUMENTS_LOCALE="$(/usr/bin/xdg_translator "$AUX_LANG"  "Documents")"
73SHARE_LOCALE="$(/usr/bin/xdg_translator "$AUX_LANG"  "Share")"
74SHARETEACHERS_LOCALE="$(/usr/bin/xdg_translator "$AUX_LANG"  "Teachers_Share")"
75GROUPSSHARE_LOCALE="$(/usr/bin/xdg_translator "$AUX_LANG"  "Groups_Share")"
76
77
78[ ! -z $DESKTOP_LOCALE ] || DESKTOP_LOCALE="Desktop"
79[ ! -z $DOWNLOADS_LOCALE ] || DOWNLOADS_LOCALE="Downloads"
80[ ! -z $DOCUMENTS_LOCALE ] || DOCUMENTS_LOCALE="Documents"
81[ ! -z $SHARE_LOCALE ] || SHARE_LOCALE="Share"
82[ ! -z $SHARETEACHERS_LOCALE ] || SHARETEACHERS_LOCALE="Teachers_Share"
83[ ! -z $GROUPSSHARE_LOCALE ] || GROUPSSHARE_LOCALE="Groups_Share"
84
85
86
87
88# Umount directories mounted previously
89LIST_BIND_MOUNTED="$(mount | grep -E '[,(]bind[,)]' | cut -d ' ' -f3 | tr '\n' ' ')"
90for MOUNT_POINT in $LIST_BIND_MOUNTED; do
91    rc=0
92    $(echo $MOUNT_POINT | grep -q $HL_HOME) || rc=1
93    if [ $rc -eq 0 ]; then
94        umount $MOUNT_POINT || true
95    fi
96done
97
98
99if [ "$TOKEN_DESKTOP" != "$DESKTOP_LOCALE" ]; then
100    if [ ! -z "$TOKEN_DESKTOP" -a -e $HL_HOME/${TOKEN_DESKTOP} ]; then
101        mv $HL_HOME/${TOKEN_DESKTOP} $HL_HOME/${DESKTOP_LOCALE}
102    else
103        mkdir $HL_HOME/${DESKTOP_LOCALE} || true
104    fi
105fi
106
107if [ -L "$HL_HOME/${DESKTOP_LOCALE}" ]; then
108    rm -rf "$HL_HOME/${DESKTOP_LOCALE}"
109    mkdir $HL_HOME/${DESKTOP_LOCALE}
110fi
111
112chown $HL_USER:$GRP_USER $HL_HOME/${DESKTOP_LOCALE}
113echo "TOKEN_DESKTOP=\"$DESKTOP_LOCALE\"" >> $TEMP_NEW_USER_DIRS
114
115
116if [ "$TOKEN_DOCUMENTS" != "$DOCUMENTS_LOCALE" ]; then
117    if [ ! -z "$TOKEN_DOCUMENTS" -a -e $HL_HOME/${TOKEN_DOCUMENTS} ]; then
118        mv $HL_HOME/${TOKEN_DOCUMENTS} $HL_HOME/${DOCUMENTS_LOCALE}
119    else
120        mkdir $HL_HOME/${DOCUMENTS_LOCALE} || true
121    fi
122fi
123
124if [ -L "$HL_HOME/${DOCUMENTS_LOCALE}" ]; then
125    rm -rf "$HL_HOME/${DOCUMENTS_LOCALE}"
126    mkdir $HL_HOME/${DOCUMENTS_LOCALE}
127fi
128
129
130chown $HL_USER:$GRP_USER $HL_HOME/${DOCUMENTS_LOCALE}
131echo "TOKEN_DOCUMENTS=\"$DOCUMENTS_LOCALE\"" >> $TEMP_NEW_USER_DIRS
132
133if [ "$TOKEN_DOWNLOADS" != "$DOWNLOADS_LOCALE" ]; then
134    if [ ! -z "$TOKEN_DOWNLOADS" -a -e $HL_HOME/${TOKEN_DOWNLOADS} ]; then
135        mv $HL_HOME/${TOKEN_DOWNLOADS} $HL_HOME/${DOWNLOADS_LOCALE}
136    else
137        mkdir $HL_HOME/${DOWNLOADS_LOCALE} || true
138    fi
139fi
140
141if [ -L "$HL_HOME/${DOWNLOADS_LOCALE}" ]; then
142    rm -rf "$HL_HOME/${DOWNLOADS_LOCALE}"
143    mkdir $HL_HOME/${DOWNLOADS_LOCALE}
144fi
145
146chown $HL_USER:$GRP_USER $HL_HOME/${DOWNLOADS_LOCALE}
147echo "TOKEN_DOWNLOADS=\"$DOWNLOADS_LOCALE\"" >> $TEMP_NEW_USER_DIRS
148
149if [ -d "/run/$HL_USER/home" -o -d "/net/server-sync/home/" ]; then
150    if [ "$TOKEN_SHARE" != "$SHARE_LOCALE" ]; then
151            if [ ! -z "$TOKEN_SHARE" -a -e $HL_HOME/${TOKEN_SHARE} ]; then
152                mv $HL_HOME/${TOKEN_SHARE} $HL_HOME/${SHARE_LOCALE}
153            else
154                mkdir $HL_HOME/${SHARE_LOCALE} || true
155            fi
156    fi
157
158    if [ -L "$HL_HOME/${SHARE_LOCALE}" ]; then
159            rm -rf "$HL_HOME/${SHARE_LOCALE}"
160            mkdir "$HL_HOME/${SHARE_LOCALE}"
161    fi
162    chown $HL_USER:$GRP_USER $HL_HOME/${SHARE_LOCALE}
163    echo "TOKEN_SHARE=\"$SHARE_LOCALE\"" >> $TEMP_NEW_USER_DIRS
164    if [ "$GRP_USER" = "teachers" ]; then
165        if [ "$TOKEN_SHARETEACHERS" != "$SHARETEACHERS_LOCALE" ]; then
166                if [ ! -z "$SHARETEACHERS_TOKEN" -a -e $HL_HOME/${SHARETEACHERS_TOKEN} ]; then
167                        mv $HL_HOME/${SHARETEACHERS_TOKEN} $HL_HOME/${SHARETEACHERS_LOCALE}
168                else
169                        mkdir $HL_HOME/${SHARETEACHERS_LOCALE} || true
170                fi
171        fi
172
173        if [ -L "$HL_HOME/${SHARETEACHERS_LOCALE}" ]; then
174            rm -rf "$HL_HOME/${SHARETEACHERS_LOCALE}"
175            mkdir "$HL_HOME/${SHARETEACHERS_LOCALE}"
176        fi
177
178        chown $HL_USER:$GRP_USER $HL_HOME/${SHARETEACHERS_LOCALE}
179        echo "TOKEN_SHARETEACHERS=\"$SHARETEACHERS_LOCALE\"" >> $TEMP_NEW_USER_DIRS
180    fi
181fi
182
183if [ "$TOKEN_GROUPSSHARE" != "$GROUPSSHARE_LOCALE" ]; then
184    if [ ! -z "$TOKEN_GROUPSSHARE" -a -e $HL_HOME/${TOKEN_GROUPSSHARE} ]; then
185        mv $HL_HOME/${TOKEN_GROUPSSHARE} $HL_HOME/${GROUPSSHARE_LOCALE}
186    else
187        mkdir $HL_HOME/${GROUPSSHARE_LOCALE} || true
188    fi
189fi
190
191if [ -L "$HL_HOME/${GROUPSSHARE_LOCALE}" ]; then
192    rm -rf "$HL_HOME/${GROUPSSHARE_LOCALE}"
193    mkdir "$HL_HOME/${GROUPSSHARE_LOCALE}"
194fi
195
196chown $HL_USER:$GRP_USER $HL_HOME/${GROUPSSHARE_LOCALE}
197echo "TOKEN_GROUPSSHARE=\"$GROUPSSHARE_LOCALE\"" >> $TEMP_NEW_USER_DIRS
198
199fix_owner_home "$HL_HOME" "$GRP_USER" "$HL_USER"
200
201if [ -d "/net/server-sync/home/$GRP_USER/$HL_USER/" ]; then
202    # other machines in aula
203    echo "OTHER MACHINES IN AULA" >> "$LOG"
204    [ "${DESKTOP_LOCALE}" = "" ] || mount --bind /net/server-sync/home/$GRP_USER/$HL_USER/Desktop $HL_HOME/${DESKTOP_LOCALE} || true
205    [ "${DOCUMENTS_LOCALE}" = "" ] || mount --bind /net/server-sync/home/$GRP_USER/$HL_USER/Documents $HL_HOME/${DOCUMENTS_LOCALE} || true
206    [ "${SHARE_LOCALE}" = "" ] || mount --bind /net/server-sync/share $HL_HOME/${SHARE_LOCALE} || true
207    if [ "$GRP_USER" = "teachers" ]; then
208        [ "${DESKTOP_LOCALE}" = "" ] || mount --bind /net/server-sync/teachers_share $HL_HOME/${SHARETEACHERS_LOCALE} || true
209        [ -d $HL_HOME/alum ] || mkdir $HL_HOME/alum
210        mount --bind /net/server-sync/home/students/ $HL_HOME/alum || true
211    fi
212    [ "${GROUPSSHARE_LOCALE}" = "" ] || mount --bind /net/server-sync/groups_share $HL_HOME/${GROUPSSHARE_LOCALE} || true
213   
214elif [ -d "/var/run/$HL_USER/home" ]; then
215    #Fat clients
216    echo "FATCLIENTS IN AULA" >> "$LOG"
217    [ "${DESKTOP_LOCALE}" = "" ] || mount --bind /run/$HL_USER/home/$GRP_USER/$HL_USER/Desktop $HL_HOME/${DESKTOP_LOCALE} || true
218    [ "${DOCUMENTS_LOCALE}" = "" ] || mount --bind /run/$HL_USER/home/$GRP_USER/$HL_USER/Documents $HLHOME/${DOCUMENTS_LOCALE} || true
219    [ "${SHARE_LOCALE}" = "" ] || mount --bind /run/$HL_USER/share $HL_HOME/${SHARE_LOCALE} || true
220    if [ "$GRP_USER" = "teachers" ]; then
221        [ "${SHARETEACHERS_LOCALE}" = "" ] || mount --bind /run/$HL_USER/teachers_share $HL_HOME/${SHARETEACHERS_LOCALE} || true
222        [ -d $HL_HOME/alum ] || mkdir $HL_HOME/alum
223        mount --bind /run/$HL_USER/home/students $HL_HOME/alum || true
224    fi
225    [ "${GROUPSSHARE_LOCALE}" = "" ] || mount --bind /run/$HL_USER/groups_share $HL_HOME/${GROUPSSHARE_LOCALE} || true
226fi
227mv ${TEMP_NEW_USER_DIRS} ${LAST_LOGIN_DIRECTORIES}
228
229
230cat > $USER_DIRS << EOF
231# This file is written by xdg-user-dirs-update
232# If you want to change or add directories, just edit the line you're
233# interested in. All local changes will be retained on the next run
234# Format is XDG_xxx_DIR="$HL_HOME/yyy", where yyy is a shell-escaped
235# homedir-relative path, or XDG_xxx_DIR="/yyy", where /yyy is an
236# absolute path. No other format is supported.
237#
238XDG_DESKTOP_DIR="\$HOME/$DESKTOP_LOCALE"
239XDG_DOWNLOAD_DIR="\$HOME/$DOWNLOADS_LOCALE"
240XDG_DOCUMENTS_DIR="\$HOME/$DOCUMENTS_LOCALE"
241
242EOF
243chmod 600 "$USER_DIRS"
244chown $HL_USER:$GRP_USER "$USER_DIRS"
245
246date >> "$LOG"
247echo "" >> "$LOG"
248echo "Las variables a tener en cuenta son:" >> "$LOG"
249echo "         \$HL_USER -> $HL_USER" >> "$LOG"
250echo "         \$HL_HOME -> $HL_HOME" >> "$LOG"
251echo "         \$HOME -> $HOME" >> "$LOG"
252echo "         \$DESKTOP_LOCALE -> $DESKTOP_LOCALE" >> "$LOG"
253echo "" >> "$LOG"
254echo "El idioma es $AUX_LANG" >> "$LOG"
255echo "" >> "$LOG"
256echo "AFTER HOME LINKER" >> "$LOG"
257tree $HL_HOME >> "$LOG"
258echo "" >> "$LOG"
259cat $HL_HOME/.config/user-dirs.created >> "$LOG"
260echo "" >> "$LOG"
261cat $HL_HOME/.config/user-dirs.dirs >> "$LOG"
262echo "" >> "$LOG"
263echo "END /usr/sbin/home-linker" >> "$LOG"
264
265
266
267exit 0
Note: See TracBrowser for help on using the repository browser.