1 #!/usr/bin/python2.4 --
3 chfn - change real user name and information
5 This utility imitates chfn(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 account information is changed.
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])
41 ('fullname', 'Full Name'),
42 ('roomnumber', 'Room Number'),
43 ('workphone', 'Work Phone'),
44 ('homephone', 'Home Phone'),
47 READONLY_FIELDS = [ 'fullname', 'other' ]
50 umesg = "Usage: %s [-f full name] [-r room no] [-w work ph] " + \
51 "[-h home ph] [-o other] [user]"
52 print umesg % progname
58 username = os.getlogin()
59 if pwd.getpwnam(username).pw_uid != uid:
60 username = pwd.getpwuid(uid).pw_name
61 return (uid, username)
63 def authenticate(username):
65 auth.start('chsh', username)
69 except PAM.error, resp:
70 print "%s: %s" % (progname, resp.args[0])
75 pwuid, pwnam = whoami()
78 os.setreuid(euid, euid)
83 options, arguments = getopt.gnu_getopt(sys.argv[1:], 'f:r:w:h:o:')
84 for opt, val in options:
85 gecos_params[OPTION_MAP[opt]] = val
86 if len(arguments) > 1:
88 elif len(arguments) == 1:
89 username = arguments[0]
92 except getopt.GetoptError, e:
95 for field in READONLY_FIELDS:
96 if field in gecos_params and pwuid:
97 print "%s: Permission denied." % progname
101 if pwuid and pwd.getpwnam(username).pw_uid != pwuid:
102 print "%s: Permission denied." % progname
105 print "%s: unknown user %s" % (progname, username)
110 gecos_raw = accounts.get_gecos(username)
111 gecos = accounts.parse_gecos(gecos_raw)
114 authenticate(username)
117 print "Changing the user information for %s" % username
118 print "Enter the new value, or press ENTER for the default"
119 for field, longname in LONG_NAMES:
120 if pwuid and field == 'other' and 'other' in READONLY_FIELDS:
122 if gecos[field] is None:
124 if field in READONLY_FIELDS and pwuid:
125 print " %s: %s" % (longname, gecos[field])
127 print " %s: [%s]:" % (longname, gecos[field]),
128 new_value = raw_input()
130 gecos[field] = new_value.strip()
132 gecos.update(gecos_params)
134 gecos_raw_new = accounts.build_gecos(**gecos)
135 if gecos_raw != gecos_raw_new:
136 accounts.update_gecos(username, gecos_raw_new)
138 except InvalidArgument, e:
139 longnames = dict(LONG_NAMES)
140 longname = longnames.get(e.argname, e.argname).lower()
141 print "%s: invalid %s: %s" % (progname, longname, e.argval)
144 if __name__ == '__main__':
145 exceps = ( accounts.ConfigurationException, accounts.LDAPException,
146 accounts.KrbException, accounts.AccountException )
149 except KeyboardInterrupt:
152 print "%s: %s: %s" % (progname, e.filename, e.strerror)
155 print "%s: %s" % (progname, e)