3 # Created by P.Wieleba@iem.pw.edu.pl in 2004
8 use FindBin qw($RealBin);
12 # function declaration
17 my $ok = getopts('f:r:w:h:o:s:?v', \%Options);
18 if ( (!$ok) || ($Options{'?'}) ) {
19 print "Usage: $0 [-frwhosh?] username\n";
20 print " -?|-h show this help message\n";
21 print " -f full_name\n";
22 print " -r room_no\n";
23 print " -w work_ph\n";
24 print " -h home_ph\n";
27 print " -v show modified user record\n";
35 my $current_user = getpwuid($<);
36 if ($current_user and $ARGV[0] and $current_user ne $ARGV[0] ) {
37 die "Only root can change other users inormation\n";
46 if (!defined($user)) {
50 my ($dn,$ldap_master);
51 # First, connecting to the directory
54 if (!defined($pass)) {
56 print "UNIX password: ";
57 system "stty -echo" if (-t STDIN);
59 system "stty echo" if (-t STDIN);
62 $config{masterDN}="uid=$user,$config{usersdn}";
63 $config{masterPw}="$pass";
64 $ldap_master=connect_ldap_master();
65 $dn=$config{masterDN};
66 if (!is_user_valid($user, $dn, $pass)) {
67 print "Authentication failure\n";
73 $ldap_master=connect_ldap_master();
74 # test existence of user in LDAP
76 if (!defined($dn_line = get_user_dn($user))) {
77 print "$0: user $user doesn't exist\n";
80 $dn = get_dn_from_line($dn_line);
84 'shell' => 'User Shell',
85 'name' => 'Full Name',
86 'office' => 'Room Number',
87 'wphone' => 'Work Phone',
88 'hphone' => 'Home Phone',
93 my $entry = read_user_entry($user);
94 my $gecos = $entry->get_value('gecos');
101 ) = split(/,/,$gecos);
102 $old{'shell'} = $entry->get_value('LoginShell');
104 $ldap_master->unbind();
106 foreach my $key (keys %old) {
107 !defined($old{$key}) and $old{$key}="";
113 $new{'name'} = $Options{'f'};
116 $new{'office'} = $Options{'r'};
119 $new{'wphone'} = $Options{'w'};
122 $new{'hphone'} = $Options{'h'};
125 $new{'other'} = $Options{'o'};
128 $new{'shell'} = $Options{'s'};
130 if ( keys(%Options) < 1 or keys(%Options) == 1 and $Options{'v'} ) {
131 print "Changing the user information for $user\n";
132 print "Enter the new value, or press ENTER for the default\n";
134 print " $eng{'shell'} [$old{'shell'}]:";
135 $new{'shell'} = readline(*STDIN);
136 print " $eng{'name'} [$old{'name'}]:";
137 $new{'name'} = readline(*STDIN);
138 print " $eng{'office'} [$old{'office'}]:";
139 $new{'office'} = readline(*STDIN);
140 print " $eng{'wphone'} [$old{'wphone'}]:";
141 $new{'wphone'} = readline(*STDIN);
142 print " $eng{'hphone'} [$old{'hphone'}]:";
143 $new{'hphone'} = readline(*STDIN);
144 print " $eng{'other'} [$old{'other'}]:";
145 $new{'other'} = readline(*STDIN);
149 foreach my $key (keys %old) {
151 $new{$key} = $old{$key};
155 # simple check of new values
156 foreach my $key (keys %new) {
157 chop($new{$key}) if ( $new{$key}=~/\n$/ );
158 if ($new{$key} =~ /^\s+$/ and $key ne 'shell') {
160 } elsif ($new{$key} =~ /^$/) {
161 $new{$key} = $old{$key};
162 } elsif ($key ne 'other' and $new{$key} =~ /.*,.*/) {
163 print "Comma cannot be used with $key.\n";
169 # [TODO] check if shell really exists
170 if ( $new{'shell'} and !($new{'shell'}=~/^\/.+\/.+/)
171 and ($old{'shell'}=~/^\/.+\/.+/)
173 $new{'shell'} = $old{'shell'};
174 } elsif ( $new{'shell'} and !($new{'shell'}=~/^\/.+\/.+/)
175 or !$new{'shell'} and !$old{'shell'}
177 $new{'shell'} = '/bin/sh';
180 if ( !$new{'name'} ) {
181 $new{'name'} = $user;
184 # prepare gecos field
194 my @tmp = split(/\s+/,$new{'name'});
195 my $sn = $tmp[$#tmp];
197 my $givenName = join(' ',@tmp);
199 $entry->replace( 'gecos' => $gecos );
200 $entry->replace( 'cn' => $new{'name'} );
202 if ( exist_in_tab( [$entry->get_value('objectClass')],'inetOrgPerson') ) {
204 $entry->replace('sn' => $sn);
206 $entry->replace('sn' => $user);
209 $entry->replace('givenName' => $givenName);
211 $entry->get_value('givenName') and $entry->delete('givenName');
213 if ( $new{'office'} ) {
214 $entry->replace('roomNumber' => $new{'office'});
216 $entry->get_value('roomNumber') and $entry->delete('roomNumber');
218 if ( $new{'wphone'} ) {
219 $entry->replace('telephoneNumber' => $new{'wphone'});
221 $entry->get_value('telephoneNumber') and $entry->delete('telephoneNumber');
223 if ( $new{'hphone'} ) {
224 $entry->replace('homePhone' => $new{'hphone'});
226 $entry->get_value('homePhone') and $entry->delete('homePhone');
228 } #end of inetOrgPerson
229 if ( $new{'shell'} ) {
230 $entry->replace('loginShell' => $new{'shell'});
232 $entry->get_value('loginShell') and $entry->delete('loginShell');
238 # bind to LDAP and update entry
239 $ldap_master = connect_ldap_master();
240 my $mesg = $entry->update($ldap_master);
241 if ($mesg->is_error()) {
242 print "Error: " . $mesg->error() . "\n";
244 print "LDAP updated\n";
246 $ldap_master and $ldap_master->unbind;
248 # Check if a $text element exists in @table
249 # eg. exist_in_tab(\@table,$text);
252 my($ref_tab,$text) = @_;
255 foreach my $elem (@tab) {
256 if ( lc($elem) eq lc($text) ) {
263 ########################################
267 smbldap-chfn - change user real name, information and shell
271 smbldap-chfn [-f full_name] [-r room_no] [-w work_ph] [-h home_ph]
272 [-o other] [-s login_shell] [-?] [-v]
276 This command changes user gecos fields and login shell.
277 The normal user can change only the fields for his own account,
278 the super user may change the fiels for any account.
280 If none of the options are selected, the command is run
281 in an interactive mode for the current user account. User is
282 asked for all fields. To accept a default value you should
283 just press <ENTER>, otherwise write text and press <ENTER>.
285 posixAccount objectClasses has to be present in the modified
286 entry. If inetOrgPerson objectClass is also present additional
287 attributes will be changed (givenName,sn,roomNumber,telephoneNumber,
291 affected attributes: 'gecos', 'cn' (and 'givenName', 'sn'
292 if inetOrgPerson is present)
295 affected attributes: 'gecos' (and 'roomNumber'
296 if inetOrgPerson is present)
299 affected attributes: 'gecos' (and 'telephoneNumber'
300 if inetOrgPerson is present)
303 affected attributes: 'gecos' (and 'homePhone'
304 if inetOrgPerson is present)
307 affected attributes: 'gecos'
310 affected attributes: 'loginShell'
312 -? show the help message
314 -v verbose - show modified user entry