check in Idealx tools after talking with Olivier Lemaire at idealx dot org.
[Samba.git] / examples / LDAP / smbldap-tools / smbldap-usermod.pl
blob6a89e4985c9359ae977bc8eae11f9bcec9c39cf1
1 #!/usr/bin/perl
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,
23 # USA.
25 # Purpose of smbldap-usermod : user (posix,shadow,samba) modification
27 use strict;
28 use smbldap_tools;
29 use smbldap_conf;
32 #####################
34 use Getopt::Std;
35 my %Options;
36 my $nscd_status;
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";
41 print " -c gecos\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";
46 print " -u uid\n";
47 print " -o uid can be non unique\n";
48 print " -g gid\n";
49 print " -G supplementary groups (comma separated)\n";
50 print " -l login name\n";
51 print " -s shell\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";
63 exit (1);
66 if ($< != 0) {
67 print "You must be root to modify an user\n";
68 exit (1);
71 # Read only first @ARGV
72 my $user = $ARGV[0];
74 # Read user datas
75 my $lines = read_user($user);
76 if (!defined($lines)) {
77 print "$0: user $user doesn't exist\n";
78 exit (1);
81 #print "$lines\n";
82 my $dn_line;
83 if ( $lines =~ /(^dn: .*)/ ) {
84 $dn_line = $1;
87 chomp($dn_line);
89 my $samba = 0;
90 if ($lines =~ m/objectClass: sambaAccount/) {
91 $samba = 1;
94 ############
96 my $tmp;
97 my $mods;
99 # Process options
100 my $changed_uid;
101 my $_userUidNumber;
102 my $_userRid;
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";
117 exit (6);
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";
131 if ($samba) {
132 $mods .= "rid: $_userRid\n";
134 $changed_uid = 1;
137 my $changed_gid;
138 my $_userGidNumber;
139 my $_userGroupRid;
140 if (defined($tmp = $Options{'g'})) {
141 $_userGidNumber = parse_group($tmp);
142 if ($_userGidNumber < 0) {
143 print "$0: group $tmp doesn't exist\n";
144 exit (6);
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";
152 if ($samba) {
153 $mods .= "primaryGroupID: $_userGroupRid\n";
155 $changed_gid = 1;
158 my $changed_shell;
159 my $_userLoginShell;
160 if (defined($tmp = $Options{'s'})) {
161 $_userLoginShell = $tmp;
162 $mods .= "loginShell: $_userLoginShell\n";
163 $changed_shell = 1;
166 my $changed_gecos;
167 my $_userGecos;
168 if (defined($tmp = $Options{'c'})) {
169 $_userGecos = $tmp;
170 $mods .= "gecos: $_userGecos\n";
171 $changed_gecos = 1;
174 my $changed_homedir;
175 my $newhomedir;
176 if (defined($tmp = $Options{'d'})) {
177 $newhomedir = $tmp;
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);
189 my $grp;
190 foreach $grp (@grplines) {
191 my $gname = "";
192 if ( $grp =~ /dn: cn=([^,]+),/) {
193 $gname = $1;
194 #print "xx $gname\n";
196 if ($gname ne "") {
197 group_remove_member($gname, $user);
201 # add user to new groups
202 add_grouplist_user($tmp, $user);
206 # A : pwdCanChange
207 # B : pwdMustChange
208 # C : smbHome
209 # D : homeDrive
210 # E : scriptPath
211 # F : profilePath
212 # H : acctFlags
214 my $attr;
215 my $winmagic = 2147483647;
217 if (defined($tmp = $Options{'A'})) {
218 $attr = "pwdCanChange";
219 if ($tmp != 0) {
220 $mods .= "$attr: 0\n";
221 } else {
222 $mods .= "$attr: $winmagic\n";
226 if (defined($tmp = $Options{'B'})) {
227 $attr = "pwdMustChange";
228 if ($tmp != 0) {
229 $mods .= "$attr: 0\n";
230 } else {
231 $mods .= "$attr: $winmagic\n";
235 if (defined($tmp = $Options{'C'})) {
236 $attr = "smbHome";
237 #$tmp =~ s/\\/\\\\/g;
238 $mods .= "$attr: $tmp\n";
241 if (defined($tmp = $Options{'D'})) {
242 $attr = "homeDrive";
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'})) {
260 $attr = "acctFlags";
261 #$tmp =~ s/\\/\\\\/g;
262 $mods .= "$attr: $tmp\n";
263 } elsif (defined($tmp = $Options{'I'})) {
264 my $flags;
266 if ( $lines =~ /^acctFlags: (.*)/m ) {
267 $flags = $1;
270 chomp($flags);
272 if ( !($flags =~ /D/) ) {
273 my $letters;
274 if ($flags =~ /(\w+)/) {
275 $letters = $1;
277 $mods .= "acctFlags: \[D$letters\]\n";
279 } elsif (defined($tmp = $Options{'J'})) {
280 my $flags;
282 if ( $lines =~ /^acctFlags: (.*)/m ) {
283 $flags = $1;
286 chomp($flags);
288 if ( $flags =~ /D/ ) {
289 my $letters;
290 if ($flags =~ /(\w+)/) {
291 $letters = $1;
293 $letters =~ s/D//;
294 $mods .= "acctFlags: \[$letters\]\n";
298 if ($mods ne '') {
299 #print "----\n$dn_line\n$mods\n----\n";
301 my $tmpldif =
302 "$dn_line
303 changetype: modify
304 $mods
307 die "$0: error while modifying user $user\n"
308 unless (do_ldapmodify($tmpldif) == 0);
310 undef $tmpldif;
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 ############################################################
322 =head1 NAME
324 smbldap-usermod.pl - Modify a user account
326 =head1 SYNOPSIS
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]
334 [-H acctflags] login
336 =head1 DESCRIPTION
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
342 -c comment
343 The new value of the user's comment field (gecos).
345 -d home_dir
346 The user's new login directory.
348 -g initial_group
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.
353 -G group,[...]
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
361 -l login_name
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
365 name.
367 -s shell
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
399 =head1 SEE ALSO
401 usermod(1)
403 =cut