check in Idealx tools after talking with Olivier Lemaire at idealx dot org.
[Samba.git] / examples / LDAP / smbldap-tools / smbldap-migrate-accounts.pl
blobd188ef5666af3b7e4f1a7ce8cd76ee2cacdef8c4
1 #!/usr/bin/perl
3 # $Id: smbldap-migrate-accounts.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) 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-migrate-accounts : add NT sam entries from pwdump
26 # to ldap
28 use strict;
29 use Getopt::Std;
30 use smbldap_tools;
31 use smbldap_conf;
33 # smbldap-migrate.pl (-? for help)
35 # Read pwdump entries on stdin, and add them to the ldap server.
36 # Output uncreated/unmodified entries (see parameters -C -U)
37 # in pwdump format to stdout.
38 # Errors, debug and stats are output to stderr.
40 sub modify_account
42 my ($login, $basedn, $lmpwd, $ntpwd, $gecos, $homedir) = @_;
44 my $tmpldif =
45 "dn: uid=$login,$basedn
46 changetype: modify
47 lmpassword: $lmpwd
48 ntpassword: $ntpwd
49 gecos: $gecos
50 smbHome: $homedir
54 die "$0: error while modifying user $login\n"
55 unless (do_ldapmodify($tmpldif) == 0);
56 undef $tmpldif;
59 #####################
62 my %Options;
64 my $ok = getopts('awA:CUW:?', \%Options);
66 if ( (!$ok) || ($Options{'?'}) ) {
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 " -? 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 (<>)
88 my ($login, $rid, $lmpwd, $ntpwd, $gecos, $homedir, $b) = split(/:/, $_);
89 my $usertype;
90 my $userbasedn;
92 my $entry_type = 'user';
94 if ($login =~ m/.*\$$/ ) { # computer
95 $processed{'machine'}++;
96 $entry_type = 'machine';
97 if (defined($Options{'a'})) {
98 print STDERR "ignoring $login\n";
99 next;
102 $usertype = "-w $Options{'W'}";
103 $userbasedn = $computersdn;
105 else { # people
106 $processed{'user'}++;
107 if (defined($Options{'w'})) {
108 print STDERR "ignoring $login\n";
109 next;
111 if ($rid < 1000) {
112 $specialskipped++;
113 print STDERR "$login seems to be a special Win account (rid=$rid), skipping\n";
114 next;
117 $usertype = "-a $Options{'A'}";
118 $userbasedn = $usersdn;
121 # normalize homedir
122 # uncomment to replace configured share with share from pwdump
123 # if ($homedir eq "") {
124 $homedir = $_userSmbHome;
127 # normalize gecos
128 if (!($gecos eq "")) {
129 $gecos =~ tr/ÁÀÂÄáàâäÇçÉÈÊËÆéèêëæÍÌÏÎíìîÏÑñÓÒÔÖóòôöÚÙÜÛúùüûÝýÿ/AAAAaaaaCcEEEEEeeeeeIIIIiiiiNnOOOOooooUUUUuuuuYyy/;
130 } else {
131 $gecos = $_userGecos;
134 my $user_exists = is_samba_user($login);
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;
146 # lem modif... a retirer si pb
147 if ($entry_type eq "user")
149 modify_account($login, $userbasedn, $lmpwd, $ntpwd, $gecos, $homedir);
152 $created{$entry_type}++;
154 else { # uid doesn't exist and no create => log
155 print "$_";
156 $logged{$entry_type}++;
159 else { # account exists
160 $existing{$entry_type}++;
161 if (!defined($Options{'U'})) { # exists and modify
162 modify_account($login, $userbasedn, $lmpwd, $ntpwd, $gecos, $homedir);
163 $updated{$entry_type}++;
165 else { # exists and log
166 print "$_";
167 $logged{$entry_type}++;
172 my $sum;
174 $sum = $processed{'user'} + $processed{'machine'};
175 print STDERR "processed: all=$sum user=$processed{'user'} machine=$processed{'machine'}\n";
177 $sum = $existing{'user'} + $existing{'machine'};
178 print STDERR "existing: all=$sum user=$existing{'user'} machine=$existing{'machine'}\n";
180 $sum = $created{'user'} + $created{'machine'};
181 print STDERR "created: all=$sum user=$created{'user'} machine=$created{'machine'}\n";
183 $sum = $updated{'user'} + $updated{'machine'};
184 print STDERR "updated: all=$sum user=$updated{'user'} machine=$updated{'machine'}\n";
186 $sum = $logged{'user'} + $logged{'machine'};
187 print STDERR "logged: all=$sum user=$logged{'user'} machine=$logged{'machine'}\n";
189 print STDERR "special users skipped: $specialskipped\n";
192 ########################################
194 =head1 NAME
196 smbldap-migrate.pl - Migrate NT accounts to LDAP
198 =head1 SYNOPSIS
200 smbldap-migrate.pl [-a] [-w] [-A opts] [-W opts] [-C] [-U] [-?]
202 =head1 DESCRIPTION
204 This command reads from stdin account entries as created by pwdump,
205 a tool to dump an user database on NT.
206 Depending of the options, some account entries may be output on
207 stdout. All errors and informations are sent to stderr.
209 -a process only people, ignore computers
211 -w process only computers, ignore persons
213 -A opts
214 a string containing arguments to pass verbatim to
215 smbldap-useradd when adding users, eg "-m -x".
216 You don't have to specify -a in this string.
218 -W opts
219 a string containing arguments to pass verbatim to
220 smbldap-useradd when adding computers, eg "-m -x".
221 You don't have to specify -w in this string.
223 -C if NT account not found in LDAP, don't create it and log it to stdout
224 (default: create it)
226 -U if NT account found in LDAP, don't update it and log it to stdout
227 (default: update it)
229 -? show the help message
231 =cut
235 # The End