Typo
[linux_from_scratch_hints.git] / OLD / execute-session-scripts-using-kdm-and-pam.txt
blob2dba74fc2d40381f85ee01528d89d0fd5b382584
1 AUTHOR: Stef Bon <stef at bononline dot nl>
3 DATE: 2006-01-30
5 LICENSE: GNU Free Documentation License Version 1.2
7 SYNOPSIS: Execute scripts at begin and end of a KDE-session using KDM and PAM.
9 DESCRIPTION:
11 This hint is about the ability to execute scripts when a KDE session starts 
12 and when it stops. 
14 Earlier I wrote a hint about this using PAM, with the help of the module pam_script. 
15 I discovered that PAM is not the best place to do so. PAM is not the place to start 
16 scripts, KDM is. KDM provides a very easy way via the Xstartup and Xreset files to 
17 execute scripts. PAM has the abilty to do something with the credentials provided 
18 at login, with the help of a module called pam_script. 
20 I'm trying to combine those two.
23 warning:
25 I use PAM and a module called pam_script to store the credentials provided at
26 login (the username and the password!!) for authentication against SMB servers, when 
27 mounting shares or browsing the network with fusesmb.
28 This looks a little bit like Single Sign On, but it isn't!! The credentials are stored in 
29 a subdirectory of the homedir (~/.cifs/mount.cifs.conf and ~/.smb/fusesmb.conf), with enough security at runtime.
30 But somebody can still find them being root, or with a LiveCD. The credentials are stored
31 plaintext, no encryption!!
33 So, this should never be used in an environment where you can't trust your users!
36 ATTACHMENTS:
39 PREREQUISITES:
40 This hint requires sufficient knowledge of LINUX in general, and scripts in particular.
42 HINT:
44 Content:
45 1. KDM: the files
46 2. PAM: the files
47 2.1 Installation of pam_script
48 2.2 Adjusting pam configuration
49 2.3 Creating the onauth script
50 3. TODO and suggestions
53 1. KDM: the files
54 -----------------
56 KDM uses some files to start and stop:
58 . Xstartup 
59 run as root, after a user succesfully logs in. 
61 . Xsession
62 runs with permissions of the authorized user, to start the desired session (KDE).
64 . Xreset
65 run as root, after the user session has ended.
67 Where Xstartup is the place to start things up, Xreset is the place to undo these commands.
69 For more information about these files look at the handbook of KDM.
71 By adding the following code to the Xstartup file:
74 -- snip --
76 for script in /etc/session.d/kdm/startup/*.sh; do
78         if [ -x $script ]; then
79         
80                 eval $script $USER
82         fi;
84 done;
87 and the code to the Xreset file:
90 -- snip --
92 for script in /etc/session.d/kdm/reset/*.sh; do
94         if [ -x $script ]; then
95         
96                 eval $script $USER
98         fi;
100 done;
103 Create the directories where the scripts go:
105 install -m755 -d /etc/session.d/kdm/startup
106 install -m755 -d /etc/session.d/kdm/reset
108 The files in these directories must be accessible for every ordinary user: 
109 therefore the permissions are 755. 
110 All scripts in these directories should have the same permissions: 755.
112 Every user should be able to execute the script, but only root is able to modify 
113 them.
116 2. PAM: the files
117 -----------------
119 My version of PAM is 0.80. 
120 I using pam-script to make credentials provided at login available for password
121 sensitive programs like mount.cifs and fusesmb. If this is not what you want, skip 
122 this section. 
123 Also be aware of the "danger" of this construction, as already stated in the 
124 DESCRIPTION.
127 2.1 installation of pam_script
128 ------------------------------
130 Get the module pam_script from http://freshmeat.net/projects/pam_script. 
131 I'm using version 0.1.6.
133 unpack:
135 tar -xzf pam-script-*.tar.gz
137 compile and move to the proper place:
139 cd pam-script-*
141 make
142 mv pam_script.so /lib/security
143 chown root:root /lib/security/pam_script.so
144 chmod 755 /lib/security/pam_script.so
147 2.2 Adjusting pam configuration
148 -------------------------------
151 Adjusting the /etc/pam.d/login file:
154 Pam_script has the ability (from version 0.1.5) to get the password provided at login, 
155 and make this available via an environmentvariable PAM_AUTHTOK to scripts. 
156 Insert it in the authpart:
158 -- snip --
160 auth            required        pam_shells.so
161 auth            required        pam_script.so expose=1
162 auth            sufficient      pam_unix.so use_first_apss
163 auth            required        pam_ldap.so use_first_pass
166 When using other ways for users to login than the standard, like a X-based login as kdm,
167 adjust them the same way. On my machine I login frequently in with kdm, and that uses the 
168 kde-service, which is a symlink to the login-service:
170 cd /etc/pam.d
172 lrwxrwxrwx   1 root root    5 2005-07-11 13:59 kde -> login
173 lrwxrwxrwx   1 root root    5 2005-07-11 13:59 kde-np -> login
174 -rw-r--r--   1 root root  931 2005-07-19 13:20 login
177 Notes:
179 - the pam_script.so uses some parameters. All of them are described in the README in the
180 source directory.
181 I use expose=1 in the autpart because I want the password to be used by fusesmb and mount.cifs.
184 2.3 Creating the onauth script
185 ------------------------------
188 The pam_script works with two standard scripts, onsessionopen and onsessionclose in the
189 /etc/security directory. 
192 cat >> /etc/security/onauth << "EOF"
193 #!/bin/bash
195 userid=$1
196 service=$2
197 userproperties=$(getent passwd | grep -E "^$userid")
199 if [ -z "$userproperties" ]; then
201     #
202     # userproperties not found: something wrong
203     #
205     echo "User not found."
206     exit
210 homedir=$(echo $userproperties | cut -d ":" -f 6);
211 gidnr=$(echo $userproperties | cut -d ":" -f 4);
212 uidnr=$(echo $userproperties | cut -d ":" -f 3);
214 nrusers=$(w -h $userid | wc -l);
216 if [ $nrusers -eq 0 ]; then
218     if [ -d /etc/session.d/pam ]; then
220         for script in /etc/session.d/pam/onauth/*.sh; do
221         
222             if [ -x $script ]; then
224                 eval $script $userid $service $PAM_AUTHTOK
226             fi;
227         done;
228         
229     fi;
233 exit 0
237 chown root:root /etc/security/onauth
238 chmod 755 /etc/security/onauth
241 Create the following directories:
243 mkdir -p /etc/session.d/pam/onauth
245 Here is where the scripts will go.
248 Notes:
250 -  as you can see I use the command "w" to determine the users logged in. 
251 Other utilities as who, users and last gave not reliable information. It 
252 looks as if the utmp file is not always presenting the right values. 
253 Utilities as who,users and last show information from utmp without any check, so 
254 they inherit the faults. 'w' does some extra checking, which makes it more 
255 usable. 
257 Other pammodules, like pam_mount, have other ways to keep track of the amount of logins 
258 per user. With pam_mount a seperate file (/var/run/pam_mount/$userid) is created for
259 this purpose.
261 Anyone knowing a better way to determine how many times a user is logged 
262 in, please let me know.
264 - I choose to execute the script only when it's the first time a user logs in.
265 It's also possible to leave that to the scripts (in /etc/session.d/pam/).
267 - pam_script is able to execute scripts when a sessions starts, and when is ends 
268 (pam_script calls it onsessionopen and onsessionclose). 
269 I've used this, but not anymore. These scripts I now put in /etc/session.d/kdm/startup and 
270 /etc/session.d/kdm/reset.
274 3. TODO and suggestions
275 -----------------------
277 The construction is working, but is not complete:
279 - it does no logging and proper userfeedback (on the screen) 
280 with the hint xconsole_setup.txt (unmaintained: in hints/downloads/files)
281 you'll find a way to launch xconsole at startup by root. Using logger and 
282 adjusting /etc/syslog.conf it's possible to write messages to it, which
283 appear just before the splash screen of KDE comes up.
286 - scripts are executed in the order the command
288 for script in /etc/session.d/kdm/startup/*.sh; do
290 works.
292 Maybe there should be an order. Some scripts first and others later. Just like
293 the rc script works to start and stop scripts in the /etc/rc.d structure.
296 - I'm testing FreeNX now. It turns your desktop into a terminalserver for
297 X11 sessions. You should test it!
298 There is a hint for LFS already!! 
299 It does not work with KDM, so does nothing with the construction I'm using.
300 I'll check this is a sollution.
303 ACKNOWLEDGEMENTS:
304   * Thanks to the author of pam_script, Izak Burger, for his module and 
305     some usefull hints.
307 CHANGELOG:
308 [2006-01-15]
309   * Initial hint.
310 [2006-01-30]
311   * added chapter 3. TODO and suggestions
313 TODO:
314   * add logging via xconsole