-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathe-smith-ldap-5.2.0-simple-ldap-update.patch
453 lines (438 loc) · 16.6 KB
/
e-smith-ldap-5.2.0-simple-ldap-update.patch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
diff -up e-smith-ldap-5.2.0/createlinks.simple-ldap-update e-smith-ldap-5.2.0/createlinks
--- e-smith-ldap-5.2.0/createlinks.simple-ldap-update 2010-11-08 11:48:06.000000000 -0700
+++ e-smith-ldap-5.2.0/createlinks 2010-11-08 11:48:42.000000000 -0700
@@ -14,26 +14,27 @@ templates2events("/etc/openldap/$_",
));
}
-event_link("ldap-update", "group-create", "95");
+event_link("ldap-update-simple", "group-create", "95");
+event_link("ldap-update-simple", "group-modify", "95");
event_link("ldap-delete", "group-delete", "55");
-event_link("ldap-update", "user-create", "95");
+
+event_link("ldap-update-simple", "user-create", "95");
+event_link("ldap-update-simple", "user-modify", "95");
+event_link("ldap-update-simple", "user-modify-admin", "95");
event_link("ldap-delete", "user-delete", "55");
-event_link("ldap-update", "user-lock", "55");
-event_link("ldap-update", "user-modify", "95");
-event_link("ldap-update", "user-modify-admin", "95");
-event_link("ldap-update", "group-modify", "95");
-event_link("ldap-update", "password-modify", "95");
+event_link("ldap-update-simple", "password-modify", "95");
+event_link("ldap-update-simple", "user-lock", "55");
-event_link("ldap-update", "ibay-create", "95");
-event_link("ldap-update", "ibay-modify", "95");
+event_link("ldap-update-simple", "ibay-create", "95");
+event_link("ldap-update-simple", "ibay-modify", "95");
event_link("ldap-delete", "ibay-delete", "55");
+event_link("ldap-update-simple", "machine-account-create", "95");
+
event_link("ldap-update", "bootstrap-ldap-save", "25");
event_link("cleanup-unix-user-group", "bootstrap-ldap-save", "98");
-event_link("ldap-update", "machine-account-create", "95");
-
event_link("ldap-dump", "pre-backup", "30");
event_link("ldap-update", "ldap-update", "80");
diff -up e-smith-ldap-5.2.0/root/etc/e-smith/events/actions/ldap-update.simple-ldap-update e-smith-ldap-5.2.0/root/etc/e-smith/events/actions/ldap-update
--- e-smith-ldap-5.2.0/root/etc/e-smith/events/actions/ldap-update.simple-ldap-update 2010-11-08 11:48:06.000000000 -0700
+++ e-smith-ldap-5.2.0/root/etc/e-smith/events/actions/ldap-update 2010-11-08 11:48:42.000000000 -0700
@@ -120,7 +120,7 @@ my ($dc,undef) = split (/\./, $domain);
my $o = $l->prop('defaultCompany') || $domain;
# Try and find base record
-my %seen;
+my $seen;
my @objects = qw(top organization dcObject);
my $result = $ldap->search( base => $base, filter => '(objectClass=*)', scope => 'base' );
if ($result->code == 32)
@@ -134,8 +134,8 @@ elsif ($result->code)
else
{
# Don't overwrite objectClass (just update if necessary)
- %seen = ();
- @objects = grep { ! $seen{$_} ++ } (@objects, $result->entry(0)->get_value('objectClass') );
+ $seen = ();
+ @objects = grep { ! $seen->{$_} ++ } (@objects, $result->entry(0)->get_value('objectClass') );
$ldap->modify( $base, replace => { dc => $dc, o => $o, objectClass => \@objects } );
}
warn "failed to add/update entry $base: ", $result->error if $result->code;
@@ -156,8 +156,8 @@ foreach my $obj ( qw(Users Groups Comput
else
{
# Don't overwrite objectClass (just update if necessary)
- %seen = ();
- @objects = grep { ! $seen{$_} ++ } (@objects, $result->entry(0)->get_value('objectClass') );
+ $seen = ();
+ @objects = grep { ! $seen->{$_} ++ } (@objects, $result->entry(0)->get_value('objectClass') );
$result = $ldap->modify( "ou=$obj,$base", replace => { ou => $obj, objectClass => \@objects } );
}
warn "failed to add/update entry ou=$obj,$base: ", $result->error if $result->code;
@@ -175,6 +175,7 @@ foreach my $user (qw/www/){
# Read information from getent passwd
@{$updates->{$dn}}{'uid','userPassword','uidNumber','gidNumber','junk','junk','gecos','homeDirectory','loginShell'} = getpwnam($user);
+ $updates->{$dn}->{userPassword} = "!*" if $updates->{$dn}->{userPassword} eq '!!';
$updates->{$dn}->{userPassword} =~ s/^/{CRYPT}/ unless $updates->{$dn}->{userPassword} =~ m/^{/;
$updates->{$dn}->{cn} = $updates->{$dn}->{gecos};
@@ -197,6 +198,16 @@ foreach my $user (qw/www/){
}
push @{$updates->{$dn}->{objectClass}}, 'sambaSamAccount';
}
+ else
+ {
+ $updates->{$dn}->{sambaLMPassword} = [];
+ $updates->{$dn}->{sambaNTPassword} = [];
+ $updates->{$dn}->{sambaSID} = [];
+ $updates->{$dn}->{displayName} = [];
+ $updates->{$dn}->{sambaPrimaryGroupSID} = [];
+ $updates->{$dn}->{sambaAcctFlags} = [];
+ $updates->{$dn}->{sambaPwdLastSet} = [];
+ }
}
}
endpwent();
@@ -217,12 +228,21 @@ foreach my $group (qw/nobody shared www/
}
# Samba parameters if we find the samba.schema
- if ( -f "$schema" and exists $groupmap->{$group} )
- {
- push @{$updates->{$dn}->{objectClass}}, 'sambaGroupMapping';
- $updates->{$dn}->{displayName} = $groupmap->{$group}->{name};
- $updates->{$dn}->{sambaSID} = $groupmap->{$group}->{sid};
- $updates->{$dn}->{sambaGroupType} = '2';
+ if ( -f "$schema" )
+ {
+ if ( exists $groupmap->{$group} )
+ {
+ push @{$updates->{$dn}->{objectClass}}, 'sambaGroupMapping';
+ $updates->{$dn}->{displayName} = $groupmap->{$group}->{name};
+ $updates->{$dn}->{sambaSID} = $groupmap->{$group}->{sid};
+ $updates->{$dn}->{sambaGroupType} = '2';
+ }
+ else
+ {
+ $updates->{$dn}->{displayName} = [];
+ $updates->{$dn}->{sambaSID} = [];
+ $updates->{$dn}->{sambaGroupType} = [];
+ }
}
}
endgrent();
@@ -260,6 +280,7 @@ foreach my $acct (@accounts)
delete $updates->{$dn};
next;
}
+ $updates->{$dn}->{userPassword} = "!*" if $updates->{$dn}->{userPassword} eq '!!';
$updates->{$dn}->{userPassword} =~ s/^/{CRYPT}/ unless $updates->{$dn}->{userPassword} =~ m/^{/;
$desc = $updates->{$dn}->{cn} = $updates->{$dn}->{gecos};
@@ -300,6 +321,16 @@ foreach my $acct (@accounts)
}
push @{$updates->{$dn}->{objectClass}}, 'sambaSamAccount';
}
+ else
+ {
+ $updates->{$dn}->{sambaLMPassword} = [];
+ $updates->{$dn}->{sambaNTPassword} = [];
+ $updates->{$dn}->{sambaSID} = [];
+ $updates->{$dn}->{displayName} = [];
+ $updates->{$dn}->{sambaPrimaryGroupSID} = [];
+ $updates->{$dn}->{sambaAcctFlags} = [];
+ $updates->{$dn}->{sambaPwdLastSet} = [];
+ }
}
#------------------------------------------------------------
@@ -322,20 +353,27 @@ foreach my $acct (@accounts)
}
# Samba parameters if we find the samba.schema
- if ( -f "$schema" and exists $groupmap->{$key} )
+ if ( -f "$schema" )
{
- push @{$updates->{$dn}->{objectClass}}, 'sambaGroupMapping';
- $updates->{$dn}->{displayName} = $groupmap->{$key}->{name};
- $updates->{$dn}->{sambaSID} = $groupmap->{$key}->{sid};
- $updates->{$dn}->{sambaGroupType} = '2';
+ if ( exists $groupmap->{$key} )
+ {
+ push @{$updates->{$dn}->{objectClass}}, 'sambaGroupMapping';
+ $updates->{$dn}->{displayName} = $groupmap->{$key}->{name};
+ $updates->{$dn}->{sambaSID} = $groupmap->{$key}->{sid};
+ $updates->{$dn}->{sambaGroupType} = '2';
+ }
+ else
+ {
+ $updates->{$dn}->{displayName} = [];
+ $updates->{$dn}->{sambaSID} = [];
+ $updates->{$dn}->{sambaGroupType} = [];
+ }
}
-
}
}
endpwent();
endgrent();
-
#------------------------------------------------------------
# Update LDAP database entry.
#------------------------------------------------------------
@@ -423,8 +461,12 @@ foreach my $dn (keys %$updates)
else
{
# Don't overwrite objectClass (just remove person if existing)
- %seen = ( person => 1 );
- @{$updates->{$dn}->{objectClass}} = grep { ! $seen{$_}++ } (@{$updates->{$dn}->{objectClass}}, @objectClass );
+ $seen = { person => 1 };
+
+ # Remove samba objectClasses if removing samba attributes
+ @{$seen}{'sambaSamAccount','sambaGroupMapping'} = (1,1) if ref($updates->{$dn}->{sambaSID}) eq 'ARRAY';
+
+ @{$updates->{$dn}->{objectClass}} = grep { ! $seen->{$_}++ } (@{$updates->{$dn}->{objectClass}}, @objectClass );
$result = $ldap->modify( $dn, replace => $updates->{$dn});
$result->code && warn "failed to modify entry $dn: ", $result->error;
diff -up e-smith-ldap-5.2.0/root/etc/e-smith/events/actions/ldap-update-simple.simple-ldap-update e-smith-ldap-5.2.0/root/etc/e-smith/events/actions/ldap-update-simple
--- e-smith-ldap-5.2.0/root/etc/e-smith/events/actions/ldap-update-simple.simple-ldap-update 2010-11-08 11:48:53.000000000 -0700
+++ e-smith-ldap-5.2.0/root/etc/e-smith/events/actions/ldap-update-simple 2010-11-08 11:48:42.000000000 -0700
@@ -0,0 +1,245 @@
+#!/usr/bin/perl -w
+
+package esmith;
+
+use strict;
+use Errno;
+use esmith::ConfigDB;
+use esmith::AccountsDB;
+use esmith::util;
+use Net::LDAP;
+use Date::Parse;
+
+my $c = esmith::ConfigDB->open_ro;
+my $a = esmith::AccountsDB->open_ro;
+
+my $l = $c->get('ldap');
+my $status = $l->prop('status') || "disabled";
+unless ($status eq "enabled" )
+{
+ warn "Not running action script $0, LDAP service not enabled!\n";
+ exit(0);
+}
+
+exit(0) if ($c->get('ldap')->prop('Authentication') || 'disabled') eq 'enabled';
+
+my $domain = $c->get('DomainName')
+ || die("Couldn't determine domain name");
+$domain = $domain->value;
+
+my $schema = '/etc/openldap/schema/samba.schema';
+
+my $event = shift || die "Event name must be specified";
+
+my @name = @ARGV;
+die "Account name argument missing." unless scalar (@name) >= 1;
+
+#------------------------------------------------------------
+# Update LDAP database entry.
+#------------------------------------------------------------
+my $base = esmith::util::ldapBase ($domain);
+my $pw = esmith::util::LdapPassword();
+
+my $ldap = Net::LDAP->new('localhost')
+ or die "$@";
+
+$ldap->bind(
+ dn => "cn=root,$base",
+ password => $pw
+);
+
+my @accounts;
+my $account;
+foreach my $name (@name)
+{
+ $account = $a->get($name);
+ die "Account $name not found.\n" unless defined $account;
+ my $type = $account->prop('type') || "unknown";
+
+ die "Account $name is not a user, group, ibay, machine account; update LDAP entry failed.\n"
+ unless ($type =~ m{^(?:user|group|ibay|machine)$} or $name eq 'admin');
+
+ push @accounts, $account;
+}
+
+#------------------------------------------------------------
+# Read all samba groups (can't do individual lookups)
+#------------------------------------------------------------
+
+my $groupmap = ();
+
+# Only do if schema is found
+if ( -f "$schema" and -x '/usr/bin/net' )
+{
+ foreach (`/usr/bin/net groupmap list 2> /dev/null`){
+ chomp;
+ next if m{\(S-1-5-32-\d+\)};
+ $groupmap->{$3} = { name => "$1", sid => "$2" } if (/^(.*) \((S-.*-\d+)\) -> (.*)$/);
+ }
+}
+
+#------------------------------------------------------------
+# Create a list of updates that need to happen
+#------------------------------------------------------------
+
+my $updates;
+foreach my $acct (@accounts)
+{
+ my $key = $acct->key;
+ my $type = $acct->prop('type');
+ my $desc = undef;
+ my $dn;
+
+ if ($type =~ m{^(?:user|group|ibay|machine)$} or $key eq 'admin')
+ {
+ #------------------------------------------------------------
+ # Do the user portion
+ #------------------------------------------------------------
+ if ($type eq 'machine')
+ {
+ $dn = "uid=$key,ou=Computers,$base";
+ }
+ else
+ {
+ $dn = "uid=$key,ou=Users,$base";
+ }
+ utf8::upgrade($dn);
+
+ # Read information from getent passwd
+ @{$updates->{$dn}}{'uid','userPassword'} = getpwnam($key);
+ unless ($updates->{$dn}->{uid})
+ {
+ delete $updates->{$dn};
+ next;
+ }
+ $updates->{$dn}->{userPassword} = "!*" if $updates->{$dn}->{userPassword} eq '!!';
+ $updates->{$dn}->{userPassword} =~ s/^/{CRYPT}/ unless $updates->{$dn}->{userPassword} =~ m/^{/;
+
+ # Samba parameters if we find the samba.schema
+ if ( -f "$schema" and -x '/usr/bin/pdbedit' )
+ {
+ my $line = `/usr/bin/pdbedit -wu '$key' 2> /dev/null`;
+ chomp($line);
+ if ($line)
+ {
+ @{$updates->{$dn}}{'junk','junk','sambaLMPassword','sambaNTPassword'} = split(/:/,$line);
+ foreach $line (`/usr/bin/pdbedit -vu '$key' 2> /dev/null`)
+ {
+ chomp($line);
+ $updates->{$dn}->{sambaSID} = $1 if $line =~ m{User SID:\s+(S-.*)$};
+ $updates->{$dn}->{displayName} = $1 if $line =~ m{Full Name:\s+(.*)$};
+ $updates->{$dn}->{sambaPrimaryGroupSID} = $1 if $line =~ m{Primary Group SID:\s+(S-.*)$};
+ $updates->{$dn}->{sambaAcctFlags} = $1 if $line =~ m{Account Flags:\s+(.*)$};
+ $updates->{$dn}->{sambaPwdLastSet} = str2time($1) if $line =~ m{Password last set:\s+(.*)$};
+ }
+ push @{$updates->{$dn}->{objectClass}}, 'sambaSamAccount';
+ }
+ else
+ {
+ $updates->{$dn}->{sambaLMPassword} = [];
+ $updates->{$dn}->{sambaNTPassword} = [];
+ $updates->{$dn}->{sambaSID} = [];
+ $updates->{$dn}->{displayName} = [];
+ $updates->{$dn}->{sambaPrimaryGroupSID} = [];
+ $updates->{$dn}->{sambaAcctFlags} = [];
+ $updates->{$dn}->{sambaPwdLastSet} = [];
+ }
+ }
+ }
+}
+endpwent();
+
+#------------------------------------------------------------
+# Do the group portion (only if we have samba)
+#------------------------------------------------------------
+if ( -f "$schema" )
+{
+ foreach my $group ( (map { $_->key } $a->users), (map { $_->key } $a->groups), qw/admin nobody shared/ ){
+ my $dn = "cn=$group,ou=Groups,$base";
+ utf8::upgrade($dn);
+
+ if ( exists $groupmap->{$group} )
+ {
+ push @{$updates->{$dn}->{objectClass}}, 'sambaGroupMapping';
+ $updates->{$dn}->{displayName} = $groupmap->{$group}->{name};
+ $updates->{$dn}->{sambaSID} = $groupmap->{$group}->{sid};
+ $updates->{$dn}->{sambaGroupType} = '2';
+ }
+ else
+ {
+ $updates->{$dn}->{displayName} = [];
+ $updates->{$dn}->{sambaSID} = [];
+ $updates->{$dn}->{sambaGroupType} = [];
+ }
+ }
+}
+
+#------------------------------------------------------------
+# Update LDAP database entry.
+#------------------------------------------------------------
+foreach my $dn (keys %$updates)
+{
+ # Try and find record
+ my $result = $ldap->search( base => $dn, filter => '(objectClass=*)', scope => 'base' );
+ warn "failed looking up entry $dn: ", $result->error if $result->code && $result->code != 32;
+ my $code = $result->code;
+ my @objectClass = $code == 32 ? () : $result->entry(0)->get_value('objectClass');
+
+ # Clean up attributes and convert to utf8
+ delete $updates->{$dn}->{'junk'};
+ foreach my $attr ( keys %{$updates->{$dn}} )
+ {
+ if ( ref($updates->{$dn}->{$attr}) eq 'ARRAY' )
+ {
+ if ( $code == 32 and scalar(@{$updates->{$dn}->{$attr}}) == 0 )
+ {
+ delete $updates->{$dn}->{$attr};
+ }
+ else
+ {
+ for (my $c = 0; $c < scalar(@{$updates->{$dn}->{$attr}}); $c++)
+ {
+ utf8::upgrade($updates->{$dn}->{$attr}[$c]);
+ }
+ }
+ }
+ else
+ {
+ if ($updates->{$dn}->{$attr} !~ /^\s*$/)
+ {
+ utf8::upgrade($updates->{$dn}->{$attr});
+ }
+ elsif ( $code == 32 )
+ {
+ delete $updates->{$dn}->{$attr};
+ }
+ else
+ {
+ $updates->{$dn}->{$attr} = [];
+ }
+ }
+ }
+
+ # Perform insert or update
+ if ( $code == 32 )
+ {
+ $result = $ldap->add( $dn, attrs => [ %{$updates->{$dn}} ] );
+ $result->code && warn "failed to add entry $dn: ", $result->error;
+ }
+ else
+ {
+ # Don't overwrite objectClass (just update if necessary)
+ my $seen = ();
+
+ # Remove samba objectClasses if removing samba attributes
+ @{$seen}{'sambaSamAccount','sambaGroupMapping'} = (1,1) if ref($updates->{$dn}->{sambaSID}) eq 'ARRAY';
+
+ @{$updates->{$dn}->{objectClass}} = grep { ! $seen->{$_}++ } (@{$updates->{$dn}->{objectClass}}, @objectClass );
+
+ $result = $ldap->modify( $dn, replace => $updates->{$dn});
+ $result->code && warn "failed to modify entry $dn: ", $result->error;
+ }
+}
+$ldap->unbind;
+
+exit (0);