3 # $Id: smbldap-usermod.pl,v 1.1.2.1 2002/06/04 22:25:39 jerry Exp $
5 # This code was developped by IDEALX (http://IDEALX.org/) and
6 # contributors (their names can be found in the CONTRIBUTORS file).
8 # Copyright (C) 2001-2002 IDEALX
10 # This program is free software; you can redistribute it and/or
11 # modify it under the terms of the GNU General Public License
12 # as published by the Free Software Foundation; either version 2
13 # of the License, or (at your option) any later version.
15 # This program is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 # GNU General Public License for more details.
20 # You should have received a copy of the GNU General Public License
21 # along with this program; if not, write to the Free Software
22 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
25 # Purpose of smbldap-usermod : user (posix,shadow,samba) modification
38 my $ok = getopts
('A:B:C:D:E:F:H:IJxme:f:u:g:G:d:l:s:c:ok:?', \
%Options);
39 if ( (!$ok) || (@ARGV < 1) || ($Options{'?'}) ) {
40 print "Usage: $0 [-awmugdsckxABCDEFGHI?] username\n";
42 print " -d home directory\n";
43 #print " -m move home directory\n";
44 #print " -e expire date (YYYY-MM-DD)\n";
45 #print " -f inactive days\n";
47 print " -o uid can be non unique\n";
49 print " -G supplementary groups (comma separated)\n";
50 print " -l login name\n";
52 print " -x creates rid and primaryGroupID in hex instead of decimal (for Samba 2.2.2 unpatched only)\n";
53 print " -A can change password ? 0 if no, 1 if yes\n";
54 print " -B must change password ? 0 if no, 1 if yes\n";
55 print " -C smbHome (SMB home share, like '\\\\PDC-SRV\\homes')\n";
56 print " -D homeDrive (letter associated with home share, like 'H:')\n";
57 print " -E scriptPath (DOS script to execute on login)\n";
58 print " -F profilePath (profile directory, like '\\\\PDC-SRV\\profiles\\foo')\n";
59 print " -H acctFlags (samba account control bits like '[NDHTUMWSLKI]')\n";
60 print " -I disable an user. Can't be used with -H or -J\n";
61 print " -J enable an user. Can't be used with -H or -I\n";
62 print " -? show this help message\n";
67 print "You must be root to modify an user\n";
71 # Read only first @ARGV
75 my $lines = read_user
($user);
76 if (!defined($lines)) {
77 print "$0: user $user doesn't exist\n";
83 if ( $lines =~ /(^dn: .*)/ ) {
90 if ($lines =~ m/objectClass: sambaAccount/) {
103 if (defined($tmp = $Options{'u'})) {
104 if (defined($Options{'o'})) {
105 $nscd_status = system "/etc/init.d/nscd status >/dev/null 2>&1";
107 if ($nscd_status == 0) {
108 system "/etc/init.d/nscd stop > /dev/null 2>&1";
111 if (getpwuid($tmp)) {
112 if ($nscd_status == 0) {
113 system "/etc/init.d/nscd start > /dev/null 2>&1";
116 print "$0: uid number $tmp exists\n";
119 if ($nscd_status == 0) {
120 system "/etc/init.d/nscd start > /dev/null 2>&1";
124 $_userUidNumber = $tmp;
125 # as rid we use 2 * uid + 1000
126 my $_userRid = 2 * $_userUidNumber + 1000;
127 if (defined($Options{'x'})) {
128 $_userRid= sprint
("%x", $_userRid);
130 $mods .= "uidNumber: $_userUidNumber\n";
132 $mods .= "rid: $_userRid\n";
140 if (defined($tmp = $Options{'g'})) {
141 $_userGidNumber = parse_group
($tmp);
142 if ($_userGidNumber < 0) {
143 print "$0: group $tmp doesn't exist\n";
146 # as grouprid we use 2 * gid + 1001
147 my $_userGroupRid = 2 * $_userGidNumber + 1001;
148 if (defined($Options{'x'})) {
149 $_userGroupRid = sprint
("%x", $_userGroupRid);
151 $mods .= "gidNumber: $_userGidNumber\n";
153 $mods .= "primaryGroupID: $_userGroupRid\n";
160 if (defined($tmp = $Options{'s'})) {
161 $_userLoginShell = $tmp;
162 $mods .= "loginShell: $_userLoginShell\n";
168 if (defined($tmp = $Options{'c'})) {
170 $mods .= "gecos: $_userGecos\n";
176 if (defined($tmp = $Options{'d'})) {
178 $mods .= "homeDirectory: $newhomedir\n";
179 $changed_homedir = 1;
183 if (defined($tmp = $Options{'G'})) {
185 # remove user from old groups
186 my $groups = find_groups_of
$user;
187 my @grplines = split(/\n/, $groups);
190 foreach $grp (@grplines) {
192 if ( $grp =~ /dn: cn=([^,]+),/) {
194 #print "xx $gname\n";
197 group_remove_member
($gname, $user);
201 # add user to new groups
202 add_grouplist_user
($tmp, $user);
215 my $winmagic = 2147483647;
217 if (defined($tmp = $Options{'A'})) {
218 $attr = "pwdCanChange";
220 $mods .= "$attr: 0\n";
222 $mods .= "$attr: $winmagic\n";
226 if (defined($tmp = $Options{'B'})) {
227 $attr = "pwdMustChange";
229 $mods .= "$attr: 0\n";
231 $mods .= "$attr: $winmagic\n";
235 if (defined($tmp = $Options{'C'})) {
237 #$tmp =~ s/\\/\\\\/g;
238 $mods .= "$attr: $tmp\n";
241 if (defined($tmp = $Options{'D'})) {
243 $tmp = $tmp.":" unless ($tmp =~ /:/);
244 $mods .= "$attr: $tmp\n";
247 if (defined($tmp = $Options{'E'})) {
248 $attr = "scriptPath";
249 #$tmp =~ s/\\/\\\\/g;
250 $mods .= "$attr: $tmp\n";
253 if (defined($tmp = $Options{'F'})) {
254 $attr = "profilePath";
255 #$tmp =~ s/\\/\\\\/g;
256 $mods .= "$attr: $tmp\n";
259 if (defined($tmp = $Options{'H'})) {
261 #$tmp =~ s/\\/\\\\/g;
262 $mods .= "$attr: $tmp\n";
263 } elsif (defined($tmp = $Options{'I'})) {
266 if ( $lines =~ /^acctFlags: (.*)/m ) {
272 if ( !($flags =~ /D/) ) {
274 if ($flags =~ /(\w+)/) {
277 $mods .= "acctFlags: \[D$letters\]\n";
279 } elsif (defined($tmp = $Options{'J'})) {
282 if ( $lines =~ /^acctFlags: (.*)/m ) {
288 if ( $flags =~ /D/ ) {
290 if ($flags =~ /(\w+)/) {
294 $mods .= "acctFlags: \[$letters\]\n";
299 #print "----\n$dn_line\n$mods\n----\n";
307 die "$0: error while modifying user $user\n"
308 unless (do_ldapmodify
($tmpldif) == 0);
313 $nscd_status = system "/etc/init.d/nscd status >/dev/null 2>&1";
315 if ($nscd_status == 0) {
316 system "/etc/init.d/nscd restart > /dev/null 2>&1";
320 ############################################################
324 smbldap-usermod.pl - Modify a user account
328 smbldap-usermod.pl [-c comment] [-d home_dir]
329 [-g initial_group] [-G group[,...]]
330 [-l login_name] [-p passwd]
331 [-s shell] [-u uid [ -o]] [-x]
332 [-A canchange] [-B mustchange] [-C smbhome]
333 [-D homedrive] [-E scriptpath] [-F profilepath]
338 The smbldap-usermod.pl command modifies the system account files
339 to reflect the changes that are specified on the command line.
340 The options which apply to the usermod command are
343 The new value of the user's comment field (gecos).
346 The user's new login directory.
349 The group name or number of the user's new initial login group.
350 The group name must exist. A group number must refer to an
351 already existing group. The default group number is 1.
354 A list of supplementary groups which the user is also a member
355 of. Each group is separated from the next by a comma, with no
356 intervening whitespace. The groups are subject to the same
357 restrictions as the group given with the -g option. If the user
358 is currently a member of a group which is not listed, the user
359 will be removed from the group
362 The name of the user will be changed from login to login_name.
363 Nothing else is changed. In particular, the user's home direcĀ
364 tory name should probably be changed to reflect the new login
368 The name of the user's new login shell. Setting this field to
369 blank causes the system to select the default login shell.
371 -u uid The numerical value of the user's ID. This value must be
372 unique, unless the -o option is used. The value must be non-
373 negative. Any files which the user owns and which are
374 located in the directory tree rooted at the user's home direcĀ
375 tory will have the file user ID changed automatically. Files
376 outside of the user's home directory must be altered manually.
378 -x Creates rid and primaryGroupID in hex instead of decimal (for
379 Samba 2.2.2 unpatched only - higher versions always use decimal)
381 -A can change password ? 0 if no, 1 if yes
383 -B must change password ? 0 if no, 1 if yes
385 -C smbHome (SMB home share, like '\\\\PDC-SRV\\homes')
387 -D homeDrive (letter associated with home share, like 'H:')
389 -E scriptPath, relative to the [netlogon] share (DOS script to execute on login, like 'foo.bat')
391 -F profilePath (profile directory, like '\\\\PDC-SRV\\profiles\\foo')
393 -H acctFlags, spaces and trailing bracket are ignored (samba account control bits like '[NDHTUMWSLKI]')
395 -I disable user. Can't be used with -H or -J
397 -J enable user. Can't be used with -H or -I