1 #!/usr/bin/python2.4 --
3 chsh - change login shell
5 This utility imitates chsh(1) from the shadow password suite, but makes its
6 changes in the LDAP directory rather than in the passwd file.
8 When run from an unprivileged account, authentication will be performed
9 before the shell is changed, and the new shell must be listed in /etc/shells.
11 import os, sys, pwd, getopt, PAM
13 safe_environment = ['LOGNAME', 'USERNAME', 'USER', 'HOME', 'TERM', 'LANG'
14 'LC_ALL', 'LC_COLLATE', 'LC_CTYPE', 'LC_MESSAGES', 'LC_MONETARY',
15 'LC_NUMERIC', 'LC_TIME', 'UID', 'GID', 'SSH_CONNECTION', 'SSH_AUTH_SOCK',
18 for key in os.environ.keys():
19 if key not in safe_environment:
22 os.environ['PATH'] = '/usr/sbin:/usr/bin:/sbin:/bin'
24 for pathent in sys.path[:]:
25 if not pathent.find('/usr') == 0:
26 sys.path.remove(pathent)
28 from csc.common.excep import InvalidArgument
29 from csc.adm import accounts
31 progname = os.path.basename(sys.argv[0])
34 print "Usage: %s [-s shell] [username]" % progname
39 username = os.getlogin()
40 if pwd.getpwnam(username).pw_uid != uid:
41 username = pwd.getpwuid(uid).pw_name
42 return (uid, username)
44 def authenticate(username):
46 auth.start('chsh', username)
50 except PAM.error, resp:
51 print "%s: %s" % (progname, resp.args[0])
56 pwuid, pwnam = whoami()
59 options, arguments = getopt.gnu_getopt(sys.argv[1:], 's:')
61 for opt, val in options:
64 if len(arguments) > 1:
66 elif len(arguments) == 1:
67 username = arguments[0]
70 except getopt.GetoptError, e:
74 if pwuid and pwd.getpwnam(username).pw_uid != pwuid:
75 print "%s: You may not change the shell for %s." % (progname, username)
78 print "%s: unknown user %s" % (progname, username)
83 current_shell = accounts.get_shell(username)
86 authenticate(username)
89 print "Changing the login shell for %s" % username
90 print "Enter the new value, or press ENTER for the default"
91 print " Login Shell [%s]:" % current_shell,
92 new_shell = raw_input()
94 new_shell = current_shell
96 if new_shell != current_shell:
97 accounts.update_shell(username, new_shell, pwuid != 0)
99 except InvalidArgument, e:
100 if e.argname == 'shell':
101 print "%s: %s: invalid shell" % (progname, new_shell)
106 if __name__ == '__main__':
107 exceps = ( accounts.ConfigurationException, accounts.LDAPException,
108 accounts.KrbException, accounts.AccountException )
111 except KeyboardInterrupt:
114 print "%s: %s: %s" % (progname, e.filename, e.strerror)
117 print "%s: %s" % (progname, e)