missed some of Derrel's changes
[Samba/gebeck_regimport.git] / examples / LDAP / smbldap-tools / smbldap-migrate-accounts.pl
blob54e4d7f7e3f14768e2aec0ceedef42af52915233
1 #!/usr/bin/perl -w
3 # This code was developped by IDEALX (http://IDEALX.org/) and
4 # contributors (their names can be found in the CONTRIBUTORS file).
6 # Copyright (C) 2002 IDEALX
8 # This program is free software; you can redistribute it and/or
9 # modify it under the terms of the GNU General Public License
10 # as published by the Free Software Foundation; either version 2
11 # of the License, or (at your option) any later version.
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with this program; if not, write to the Free Software
20 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
21 # USA.
23 # Purpose of smbldap-migrate-accounts : add NT sam entries from pwdump
24 # to ldap
26 use strict;
27 use Getopt::Std;
28 use FindBin;
29 use FindBin qw($RealBin);
30 use lib "$RealBin/";
31 use smbldap_tools;
32 use smbldap_conf;
34 # smbldap-migrate.pl (-? or -h for help)
36 # Read pwdump entries on stdin, and add them to the ldap server.
37 # Output uncreated/unmodified entries (see parameters -C -U)
38 # in pwdump format to stdout.
39 # Errors, debug and stats are output to stderr.
41 sub modify_account
43 my ($login, $basedn, $lmpwd, $ntpwd, $gecos, $homedir) = @_;
44 # bind to a directory with dn and password
45 my $ldap_master=connect_ldap_master();
46 my $modify = $ldap_master->modify ("uid=$login,$basedn",
47 changes => [
48 replace => [sambaLMPassword => "$lmpwd"],
49 replace => [sambaNTpassword => "$ntpwd"],
50 replace => [gecos => "$gecos"],
51 replace => [sambaHomePath => "$homedir"]
54 $modify->code && die "failed to modify entry: ", $modify->error ;
55 # take down the session
56 $ldap_master->unbind;
59 #####################
62 my %Options;
64 my $ok = getopts('awA:CUW:?h', \%Options);
66 if ( (!$ok) || ($Options{'?'}) || ($Options{'h'}) ) {
67 print "Usage: $0 [-awAWCU?]\n";
68 print " -a process only people, ignore computers\n";
69 print " -w process only computers, ignore persons\n";
70 print " -A <opts> option string passed verbatim to smbldap-useradd for persons\n";
71 print " -W <opts> option string passed verbatim to smbldap-useradd for computers\n";
72 print " -C if entry not found, don't create it and log it to stdout (default: create it)\n";
73 print " -U if entry found, don't update it and log it to stdout (default: update it)\n";
74 print " -?|-h show this help message\n";
75 exit (1);
78 my %processed = ( 'user' => 0, 'machine' => 0);
79 my %created = ( 'user' => 0, 'machine' => 0);
80 my %updated = ( 'user' => 0, 'machine' => 0);
81 my %logged = ( 'user' => 0, 'machine' => 0);
82 my %errors = ( 'user' => 0, 'machine' => 0);
83 my %existing = ( 'user' => 0, 'machine' => 0);
84 my $specialskipped = 0;
86 while (<>) {
87 my ($login, $rid, $lmpwd, $ntpwd, $gecos, $homedir, $b) = split(/:/, $_);
88 my $usertype;
89 my $userbasedn;
91 my $entry_type = 'user';
93 if ($login =~ m/.*\$$/ ) { # computer
94 $processed{'machine'}++;
95 $entry_type = 'machine';
96 if (defined($Options{'a'})) {
97 print STDERR "ignoring $login\n";
98 next;
101 $usertype = "-w $Options{'W'}";
102 $userbasedn = $computersdn;
103 } else { # people
104 $processed{'user'}++;
105 if (defined($Options{'w'})) {
106 print STDERR "ignoring $login\n";
107 next;
109 if ($rid < 1000) {
110 $specialskipped++;
111 print STDERR "$login seems to be a special Win account (rid=$rid), skipping\n";
112 next;
115 $usertype = "-a $Options{'A'}";
116 $userbasedn = $usersdn;
119 # normalize homedir
120 # uncomment to replace configured share with share from pwdump
121 # if ($homedir eq "") {
122 $homedir = $_userSmbHome;
125 # normalize gecos
126 if (!($gecos eq "")) {
127 $gecos =~ tr/ÁÀÂÄáàâäÇçÉÈÊËÆéèêëæÍÌÏÎíìîÏÑñÓÒÔÖóòôöÚÙÜÛúùüûÝýÿ/AAAAaaaaCcEEEEEeeeeeIIIIiiiiNnOOOOooooUUUUuuuuYyy/;
128 } else {
129 $gecos = $_userGecos;
132 my $user_exists = is_samba_user($login);
134 if (!$user_exists) {
135 if (!defined($Options{'C'})) {
136 # uid doesn't exist and we want to create it
137 my $addcmd = "/usr/local/sbin/smbldap-useradd.pl $usertype $login > /dev/null";
138 print STDERR "$addcmd\n";
139 my $r = system "$addcmd";
140 if ($r != 0) {
141 print STDERR "error adding $login, skipping\n";
142 next;
144 # lem modif... a retirer si pb
145 if ($entry_type eq "user") {
146 modify_account($login, $userbasedn, $lmpwd, $ntpwd, $gecos, $homedir);
149 $created{$entry_type}++;
150 } else { # uid doesn't exist and no create => log
151 print "$_";
152 $logged{$entry_type}++;
154 } else { # account exists
155 $existing{$entry_type}++;
156 if (!defined($Options{'U'})) { # exists and modify
157 modify_account($login, $userbasedn, $lmpwd, $ntpwd, $gecos, $homedir);
158 $updated{$entry_type}++;
159 } else { # exists and log
160 print "$_";
161 $logged{$entry_type}++;
166 my $sum;
168 $sum = $processed{'user'} + $processed{'machine'};
169 print STDERR "processed: all=$sum user=$processed{'user'} machine=$processed{'machine'}\n";
171 $sum = $existing{'user'} + $existing{'machine'};
172 print STDERR "existing: all=$sum user=$existing{'user'} machine=$existing{'machine'}\n";
174 $sum = $created{'user'} + $created{'machine'};
175 print STDERR "created: all=$sum user=$created{'user'} machine=$created{'machine'}\n";
177 $sum = $updated{'user'} + $updated{'machine'};
178 print STDERR "updated: all=$sum user=$updated{'user'} machine=$updated{'machine'}\n";
180 $sum = $logged{'user'} + $logged{'machine'};
181 print STDERR "logged: all=$sum user=$logged{'user'} machine=$logged{'machine'}\n";
183 print STDERR "special users skipped: $specialskipped\n";
186 ########################################
188 =head1 NAME
190 smbldap-migrate.pl - Migrate NT accounts to LDAP
192 =head1 SYNOPSIS
194 smbldap-migrate.pl [-a] [-w] [-A opts] [-W opts] [-C] [-U] [-?]
196 =head1 DESCRIPTION
198 This command reads from stdin account entries as created by pwdump,
199 a tool to dump an user database on NT.
200 Depending of the options, some account entries may be output on
201 stdout. All errors and informations are sent to stderr.
203 -a process only people, ignore computers
205 -w process only computers, ignore persons
207 -A opts
208 a string containing arguments to pass verbatim to
209 smbldap-useradd when adding users, eg "-m -x".
210 You don't have to specify -a in this string.
212 -W opts
213 a string containing arguments to pass verbatim to
214 smbldap-useradd when adding computers, eg "-m -x".
215 You don't have to specify -w in this string.
217 -C if NT account not found in LDAP, don't create it and log it to stdout
218 (default: create it)
220 -U if NT account found in LDAP, don't update it and log it to stdout
221 (default: update it)
223 -? show the help message
225 =cut
229 # The End