updating top 0.8.2-1 of the smbldap tools
[kai/samba-autobuild/.git] / examples / LDAP / smbldap-tools / smbldap-migrate-accounts.pl
1 #!/usr/bin/perl -w
2
3 # $Id: smbldap-migrate-accounts.pl,v 1.1.6.4 2003/12/04 21:59:19 jerry Exp $
4 #
5 #  This code was developped by IDEALX (http://IDEALX.org/) and
6 #  contributors (their names can be found in the CONTRIBUTORS file).
7 #
8 #                 Copyright (C) 2002 IDEALX
9 #
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.
14 #
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.
19 #
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.
24
25 # Purpose of smbldap-migrate-accounts : add NT sam entries from pwdump 
26 #                                       to ldap
27
28 use strict;
29 use Getopt::Std;
30 use FindBin;
31 use FindBin qw($RealBin);
32 use lib "$RealBin/";
33 use smbldap_tools;
34 use smbldap_conf;
35
36 # smbldap-migrate.pl (-? or -h for help)
37 #
38 # Read pwdump entries on stdin, and add them to the ldap server.
39 # Output uncreated/unmodified entries (see parameters -C -U)
40 # in pwdump format to stdout.
41 # Errors, debug and stats are output to stderr.
42
43 sub modify_account
44   {
45         my ($login, $basedn, $lmpwd, $ntpwd, $gecos, $homedir) = @_;
46         # bind to a directory with dn and password
47         my $ldap_master=connect_ldap_master();
48         my $modify = $ldap_master->modify ("uid=$login,$basedn",
49                 changes => [
50                         replace => [lmpassword => "$lmpwd"],
51                         replace => [ntpassword => "$ntpwd"],
52                         replace => [gecos => "$gecos"],
53                         replace => [sambaHomePath => "$homedir"]
54                 ]
55         );
56         $modify->code && die "failed to modify entry: ", $modify->error ;
57         # take down the session
58         $ldap_master->unbind;
59   }
60
61 #####################
62
63
64 my %Options;
65
66 my $ok = getopts('awA:CUW:?h', \%Options);
67
68 if ( (!$ok) || ($Options{'?'}) || ($Options{'h'}) ) {
69   print "Usage: $0 [-awAWCU?]\n";
70   print "  -a         process only people, ignore computers\n";
71   print "  -w         process only computers, ignore persons\n";
72   print "  -A <opts>  option string passed verbatim to smbldap-useradd for persons\n";
73   print "  -W <opts>  option string passed verbatim to smbldap-useradd for computers\n";
74   print "  -C         if entry not found, don't create it and log it to stdout (default: create it)\n";
75   print "  -U         if entry found, don't update it and log it to stdout (default: update it)\n";
76   print "  -?|-h      show this help message\n";
77   exit (1);
78 }
79
80 my %processed = ( 'user' => 0, 'machine' => 0);
81 my %created = ( 'user' => 0, 'machine' => 0);
82 my %updated = ( 'user' => 0, 'machine' => 0);
83 my %logged = ( 'user' => 0, 'machine' => 0);
84 my %errors = ( 'user' => 0, 'machine' => 0);
85 my %existing = ( 'user' => 0, 'machine' => 0);
86 my $specialskipped = 0;
87
88 while (<>) {
89   my ($login, $rid, $lmpwd, $ntpwd, $gecos, $homedir, $b) = split(/:/, $_);
90   my $usertype;
91   my $userbasedn;
92
93   my $entry_type = 'user';
94
95   if ($login =~ m/.*\$$/ ) {    # computer
96     $processed{'machine'}++;
97     $entry_type = 'machine';
98     if (defined($Options{'a'})) {
99       print STDERR "ignoring $login\n";
100       next;
101     }
102  
103     $usertype = "-w $Options{'W'}";
104     $userbasedn = $computersdn;
105   } else {                                              # people
106     $processed{'user'}++;
107     if (defined($Options{'w'})) {
108       print STDERR "ignoring $login\n";
109       next;
110     }
111     if ($rid < 1000) {
112       $specialskipped++;
113       print STDERR "$login seems to be a special Win account (rid=$rid), skipping\n";
114       next;
115     }
116
117     $usertype = "-a $Options{'A'}";
118     $userbasedn = $usersdn;
119   }
120
121   # normalize homedir
122   # uncomment to replace configured share with share from pwdump
123   #  if ($homedir eq "") {
124   $homedir = $_userSmbHome;
125   #  }
126
127   # normalize gecos
128   if (!($gecos eq "")) {
129     $gecos =~ tr/ÁÀÂÄáàâäÇçÉÈÊËÆéèêëæÍÌÏÎíìîÏÑñÓÒÔÖóòôöÚÙÜÛúùüûÝýÿ/AAAAaaaaCcEEEEEeeeeeIIIIiiiiNnOOOOooooUUUUuuuuYyy/;
130   } else {
131     $gecos = $_userGecos;
132   }
133
134   my $user_exists = is_samba_user($login);
135  
136   if (!$user_exists) {
137     if (!defined($Options{'C'})) {
138       # uid doesn't exist and we want to create it
139       my $addcmd = "/usr/local/sbin/smbldap-useradd.pl $usertype $login > /dev/null";
140       print STDERR "$addcmd\n";
141       my $r = system "$addcmd";
142       if ($r != 0) {
143         print STDERR "error adding $login, skipping\n";
144         next;
145       }
146           # lem modif... a retirer si pb
147           if ($entry_type eq "user") {
148         modify_account($login, $userbasedn, $lmpwd, $ntpwd, $gecos, $homedir);
149           }
150
151           $created{$entry_type}++;
152     } else {                                    # uid doesn't exist and no create => log
153       print "$_";
154       $logged{$entry_type}++;
155     }
156   } else {                                              # account exists
157     $existing{$entry_type}++;
158     if (!defined($Options{'U'})) { # exists and modify 
159       modify_account($login, $userbasedn, $lmpwd, $ntpwd, $gecos, $homedir);
160       $updated{$entry_type}++;
161     } else {                                    # exists and log
162       print "$_";
163       $logged{$entry_type}++;
164     }
165   }
166 }
167
168 my $sum;
169
170 $sum = $processed{'user'} + $processed{'machine'};
171 print STDERR "processed: all=$sum user=$processed{'user'} machine=$processed{'machine'}\n";
172
173 $sum = $existing{'user'} + $existing{'machine'};
174 print STDERR "existing: all=$sum user=$existing{'user'} machine=$existing{'machine'}\n";
175
176 $sum = $created{'user'} + $created{'machine'};
177 print STDERR "created: all=$sum user=$created{'user'} machine=$created{'machine'}\n";
178
179 $sum = $updated{'user'} + $updated{'machine'};
180 print STDERR "updated: all=$sum user=$updated{'user'} machine=$updated{'machine'}\n";
181
182 $sum = $logged{'user'} + $logged{'machine'};
183 print STDERR "logged: all=$sum user=$logged{'user'} machine=$logged{'machine'}\n";
184
185 print STDERR "special users skipped: $specialskipped\n";
186
187
188 ########################################
189
190 =head1 NAME
191
192 smbldap-migrate.pl - Migrate NT accounts to LDAP
193
194 =head1 SYNOPSIS
195
196        smbldap-migrate.pl [-a] [-w] [-A opts] [-W opts] [-C] [-U] [-?]
197
198 =head1 DESCRIPTION
199
200        This command reads from stdin account entries as created by pwdump,
201        a tool to dump an user database on NT.
202        Depending of the options, some account entries may be output on
203        stdout. All errors and informations are sent to stderr.
204
205        -a     process only people, ignore computers
206
207        -w     process only computers, ignore persons
208
209        -A opts
210               a string containing arguments to pass verbatim to
211               smbldap-useradd when adding users, eg "-m -x".
212               You don't have to specify -a in this string.
213
214        -W opts
215               a string containing arguments to pass verbatim to
216               smbldap-useradd when adding computers, eg "-m -x".
217               You don't have to specify -w in this string.
218
219        -C     if NT account not found in LDAP, don't create it and log it to stdout
220               (default: create it)
221
222        -U     if NT account found in LDAP, don't update it and log it to stdout
223               (default: update it)
224
225        -?     show the help message
226
227 =cut
228
229 #'
230
231 # The End
232