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