line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Win32::AD::User;
|
2
|
1
|
|
|
1
|
|
23950
|
use strict;
|
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
36
|
|
3
|
1
|
|
|
1
|
|
350
|
use Win32::OLE 'in';
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
4
|
|
|
|
|
|
|
$Win32::OLE::Warn = 3;
|
5
|
|
|
|
|
|
|
our $VERSION = '0.04';
|
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
######################################################################
|
8
|
|
|
|
|
|
|
sub new{
|
9
|
|
|
|
|
|
|
my $class = shift;
|
10
|
|
|
|
|
|
|
my ($connect_string,$username) = @_;
|
11
|
|
|
|
|
|
|
warn "Win32::AD::User -- Warning ADS_CONNECT_STRING not defined.\n" if(not defined $connect_string);
|
12
|
|
|
|
|
|
|
warn "Win32::AD::User -- Warning USER_REFERENCE_STRING not defined.\n" if(not defined $username);
|
13
|
|
|
|
|
|
|
bless { _connect_string => $connect_string,
|
14
|
|
|
|
|
|
|
_username => $username,
|
15
|
|
|
|
|
|
|
_LDAPAdsPath => ($connect_string =~ /LDAP/) ? "CN=".$username.",".(split /\//,$connect_string)[3] : undef,
|
16
|
|
|
|
|
|
|
_LDAPAdsSvr => ($connect_string =~ /LDAP/) ? (split /\//,$connect_string)[2] : undef,
|
17
|
|
|
|
|
|
|
_WinNTAdsPath => ($connect_string =~ /WinNT/) ? "WinNT://" . (split /\//,$connect_string)[2] . "/$username,user" : undef,
|
18
|
|
|
|
|
|
|
_WinNTDomain => ($connect_string =~ /WinNT/) ? (split /\//,$connect_string)[2] : (split /\./, (split /\//,$connect_string)[2])[0],
|
19
|
|
|
|
|
|
|
_user_ref => undef}, $class;
|
20
|
|
|
|
|
|
|
}
|
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
######################################################################
|
23
|
|
|
|
|
|
|
sub print_me{
|
24
|
|
|
|
|
|
|
my $self = shift; print "$_: $self->{$_}\n" for (keys %$self)}
|
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
######################################################################
|
27
|
|
|
|
|
|
|
sub create_new{
|
28
|
|
|
|
|
|
|
my $self = shift;
|
29
|
|
|
|
|
|
|
my $server = Win32::OLE->GetObject($self->{_connect_string});
|
30
|
|
|
|
|
|
|
my $user;
|
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
if ($self->_connect_type eq "LDAP"){
|
33
|
|
|
|
|
|
|
$user = $server->Create("user","cn=".$self->{_username});
|
34
|
|
|
|
|
|
|
$user->{samAccountName}=$self->{_username};
|
35
|
|
|
|
|
|
|
$user->SetInfo();
|
36
|
|
|
|
|
|
|
}
|
37
|
|
|
|
|
|
|
else{
|
38
|
|
|
|
|
|
|
$user = $server->Create("user",$self->{_username});
|
39
|
|
|
|
|
|
|
$user->SetInfo();
|
40
|
|
|
|
|
|
|
}#fi
|
41
|
|
|
|
|
|
|
$self->{_user_ref} = $user;
|
42
|
|
|
|
|
|
|
}
|
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
######################################################################
|
45
|
|
|
|
|
|
|
sub get_info{
|
46
|
|
|
|
|
|
|
my $self = shift;
|
47
|
|
|
|
|
|
|
my $user;
|
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
if ($self->_connect_type eq "LDAP"){
|
50
|
|
|
|
|
|
|
$user = Win32::OLE->GetObject( join "/", ("LDAP:/",$self->{_LDAPAdsSvr},$self->{_LDAPAdsPath}) );
|
51
|
|
|
|
|
|
|
}
|
52
|
|
|
|
|
|
|
else{
|
53
|
|
|
|
|
|
|
$user = Win32::OLE->GetObject($self->{_WinNTAdsPath});
|
54
|
|
|
|
|
|
|
}#fi
|
55
|
|
|
|
|
|
|
$self->{_user_ref} = $user;
|
56
|
|
|
|
|
|
|
}
|
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
######################################################################
|
59
|
|
|
|
|
|
|
sub delete{
|
60
|
|
|
|
|
|
|
my $self = shift;
|
61
|
|
|
|
|
|
|
my $server = Win32::OLE->GetObject($self->{_connect_string});
|
62
|
|
|
|
|
|
|
if ($self->_connect_type eq "LDAP"){
|
63
|
|
|
|
|
|
|
$server->Delete("user","cn=".$self->{_username});
|
64
|
|
|
|
|
|
|
}
|
65
|
|
|
|
|
|
|
else{
|
66
|
|
|
|
|
|
|
$server->Delete("user",$self->{_username});
|
67
|
|
|
|
|
|
|
}#fi
|
68
|
|
|
|
|
|
|
$self->{_user_ref} = undef;
|
69
|
|
|
|
|
|
|
}
|
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
######################################################################
|
72
|
|
|
|
|
|
|
sub set_password{
|
73
|
|
|
|
|
|
|
my $self = shift;
|
74
|
|
|
|
|
|
|
my ($new_password) = @_;
|
75
|
|
|
|
|
|
|
$self->{_user_ref}->SetPassword($new_password);
|
76
|
|
|
|
|
|
|
}
|
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
######################################################################
|
79
|
|
|
|
|
|
|
sub lock{
|
80
|
|
|
|
|
|
|
my $self = shift;
|
81
|
|
|
|
|
|
|
$self->{_user_ref}->{'AccountDisabled'} = '1';
|
82
|
|
|
|
|
|
|
$self->{_user_ref}->SetInfo();
|
83
|
|
|
|
|
|
|
}
|
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
######################################################################
|
86
|
|
|
|
|
|
|
sub un_lock{
|
87
|
|
|
|
|
|
|
my $self = shift;
|
88
|
|
|
|
|
|
|
$self->{_user_ref}->{'AccountDisabled'} = '0';
|
89
|
|
|
|
|
|
|
$self->{_user_ref}->SetInfo();
|
90
|
|
|
|
|
|
|
}
|
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
######################################################################
|
93
|
|
|
|
|
|
|
sub is_locked{
|
94
|
|
|
|
|
|
|
my $self = shift;
|
95
|
|
|
|
|
|
|
$self->{_user_ref}->{'AccountDisabled'};
|
96
|
|
|
|
|
|
|
}
|
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
######################################################################
|
99
|
|
|
|
|
|
|
sub set_properties{
|
100
|
|
|
|
|
|
|
my $self = shift;
|
101
|
|
|
|
|
|
|
my ($properties) = @_;
|
102
|
|
|
|
|
|
|
for my $i (keys %$properties){
|
103
|
|
|
|
|
|
|
$self->{_user_ref}->{$i} = $properties->{$i};
|
104
|
|
|
|
|
|
|
}#rof
|
105
|
|
|
|
|
|
|
$self->{_user_ref}->SetInfo();
|
106
|
|
|
|
|
|
|
}
|
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
######################################################################
|
109
|
|
|
|
|
|
|
sub set_property{
|
110
|
|
|
|
|
|
|
my $self = shift;
|
111
|
|
|
|
|
|
|
my ($property,$value) = @_;
|
112
|
|
|
|
|
|
|
$self->{_user_ref}->{$property} = $value;
|
113
|
|
|
|
|
|
|
$self->{_user_ref}->SetInfo();
|
114
|
|
|
|
|
|
|
}
|
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
######################################################################
|
117
|
|
|
|
|
|
|
sub get_properties{
|
118
|
|
|
|
|
|
|
my $self = shift;
|
119
|
|
|
|
|
|
|
my @props = @_;
|
120
|
|
|
|
|
|
|
my %props;
|
121
|
|
|
|
|
|
|
$props{$_}=$self->{_user_ref}->{$_} for (@props);
|
122
|
|
|
|
|
|
|
%props;
|
123
|
|
|
|
|
|
|
}
|
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
######################################################################
|
126
|
|
|
|
|
|
|
sub get_property{
|
127
|
|
|
|
|
|
|
my $self = shift;
|
128
|
|
|
|
|
|
|
my ($property) = @_;
|
129
|
|
|
|
|
|
|
$self->{_user_ref}->{$property};
|
130
|
|
|
|
|
|
|
}
|
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
######################################################################
|
133
|
|
|
|
|
|
|
sub _connect_type{
|
134
|
|
|
|
|
|
|
my $self = shift;
|
135
|
|
|
|
|
|
|
($self->{_connect_string} =~ /^LDAP/) ? return "LDAP" : return "WinNT";
|
136
|
|
|
|
|
|
|
}
|
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
######################################################################
|
139
|
|
|
|
|
|
|
sub rename{
|
140
|
|
|
|
|
|
|
## Inspiration:
|
141
|
|
|
|
|
|
|
# http://www.rallenhome.com/books/adcookbook/src/06.06-rename_user.pls.txt
|
142
|
|
|
|
|
|
|
my $self = shift;
|
143
|
|
|
|
|
|
|
my ($new_name) = @_;
|
144
|
|
|
|
|
|
|
my $old_name = $self->{_username};
|
145
|
|
|
|
|
|
|
my $server = Win32::OLE->GetObject($self->{_connect_string});
|
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
if ($self->_connect_type eq "LDAP"){
|
148
|
|
|
|
|
|
|
$self->set_property("samAccountName",$new_name);
|
149
|
|
|
|
|
|
|
$server->MoveHere( join ("/", ("LDAP:/",$self->{_LDAPAdsSvr},$self->{_LDAPAdsPath})),"cn=".$new_name);
|
150
|
|
|
|
|
|
|
for my $key (keys %$self){
|
151
|
|
|
|
|
|
|
if ($self->{$key} =~ /$old_name/){
|
152
|
|
|
|
|
|
|
$self->{$key} =~ s/$old_name/$new_name/g
|
153
|
|
|
|
|
|
|
}#fi
|
154
|
|
|
|
|
|
|
}#rof
|
155
|
|
|
|
|
|
|
$self->get_info();
|
156
|
|
|
|
|
|
|
}
|
157
|
|
|
|
|
|
|
else{
|
158
|
|
|
|
|
|
|
warn "Win32::AD::User -- The 'rename' function is not available when using \n",
|
159
|
|
|
|
|
|
|
"a WinNT:// ADsPath... use a LDAP:// ADsPath.\n";
|
160
|
|
|
|
|
|
|
}#fi
|
161
|
|
|
|
|
|
|
}
|
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
######################################################################
|
164
|
|
|
|
|
|
|
sub move{
|
165
|
|
|
|
|
|
|
#Inspiration:
|
166
|
|
|
|
|
|
|
# http://www.rallenhome.com/books/adcookbook/src/08.04-move_computer.pls.txt
|
167
|
|
|
|
|
|
|
my $self = shift;
|
168
|
|
|
|
|
|
|
my ($new_connect) = @_;
|
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
if ($self->_connect_type eq "LDAP"){
|
171
|
|
|
|
|
|
|
my $server = Win32::OLE->GetObject($new_connect);
|
172
|
|
|
|
|
|
|
$server->MoveHere($self->{_user_ref}->{'ADsPath'},$self->{_user_ref}->{'Name'});
|
173
|
|
|
|
|
|
|
$self->{_connect_string} = $new_connect;
|
174
|
|
|
|
|
|
|
$self->{_LDAPAdsPath} = "CN=".$self->{_username}.",".(split /\//,$self->{_connect_string})[3];
|
175
|
|
|
|
|
|
|
$self->{_LDAPAdsSvr} = (split /\//,$self->{_connect_string})[2];
|
176
|
|
|
|
|
|
|
$self->get_info();
|
177
|
|
|
|
|
|
|
}
|
178
|
|
|
|
|
|
|
else{
|
179
|
|
|
|
|
|
|
warn "Win32::AD::User -- The 'move' function is not available when using \n",
|
180
|
|
|
|
|
|
|
"a WinNT:// ADsPath... use a LDAP:// ADsPath.\n";
|
181
|
|
|
|
|
|
|
}#fi
|
182
|
|
|
|
|
|
|
|
183
|
|
|
|
|
|
|
}
|
184
|
|
|
|
|
|
|
|
185
|
|
|
|
|
|
|
######################################################################
|
186
|
|
|
|
|
|
|
sub get_groups{
|
187
|
|
|
|
|
|
|
my $self = shift;
|
188
|
|
|
|
|
|
|
my @grp;
|
189
|
|
|
|
|
|
|
push(@grp, $_->{Name}) for (in $self->{_user_ref}->{Groups});
|
190
|
|
|
|
|
|
|
@grp;
|
191
|
|
|
|
|
|
|
}
|
192
|
|
|
|
|
|
|
|
193
|
|
|
|
|
|
|
######################################################################
|
194
|
|
|
|
|
|
|
sub add_to_group{
|
195
|
|
|
|
|
|
|
#Inspiration & Reference:
|
196
|
|
|
|
|
|
|
# http://www.rallenhome.com/books/adcookbook/src/06.15-set_primary_group.pls.txt
|
197
|
|
|
|
|
|
|
# http://www.rallenhome.com/books/adcookbook/src/07.04-add_group_member.pls.txt
|
198
|
|
|
|
|
|
|
my $self = shift;
|
199
|
|
|
|
|
|
|
my ($group_ads_path) = @_;
|
200
|
|
|
|
|
|
|
$group_ads_path = "WinNT://".$self->{_WinNTDomain}."/$group_ads_path,group" if ($group_ads_path !~ /\:\/\//);
|
201
|
|
|
|
|
|
|
my $group = Win32::OLE->GetObject($group_ads_path);
|
202
|
|
|
|
|
|
|
|
203
|
|
|
|
|
|
|
if ($self->_connect_type eq "LDAP"){
|
204
|
|
|
|
|
|
|
if($group_ads_path =~ /WinNT:/){
|
205
|
|
|
|
|
|
|
my $wintmp = "WinNT://".$self->{_WinNTDomain}."/".$self->{_username}.",user";
|
206
|
|
|
|
|
|
|
$group->Add($wintmp);
|
207
|
|
|
|
|
|
|
}
|
208
|
|
|
|
|
|
|
else{
|
209
|
|
|
|
|
|
|
$group->Add("LDAP://".$self->{_LDAPAdsPath});
|
210
|
|
|
|
|
|
|
}#fi
|
211
|
|
|
|
|
|
|
$group->SetInfo();
|
212
|
|
|
|
|
|
|
$self->get_info();
|
213
|
|
|
|
|
|
|
}
|
214
|
|
|
|
|
|
|
else{
|
215
|
|
|
|
|
|
|
$group->Add($self->{_WinNTAdsPath});
|
216
|
|
|
|
|
|
|
$group->SetInfo();
|
217
|
|
|
|
|
|
|
}#fi
|
218
|
|
|
|
|
|
|
}
|
219
|
|
|
|
|
|
|
|
220
|
|
|
|
|
|
|
######################################################################
|
221
|
|
|
|
|
|
|
sub remove_from_group{
|
222
|
|
|
|
|
|
|
#Inspiration & Reference:
|
223
|
|
|
|
|
|
|
# http://www.rallenhome.com/books/adcookbook/src/06.15-set_primary_group.pls.txt
|
224
|
|
|
|
|
|
|
# http://www.rallenhome.com/books/adcookbook/src/07.04-add_group_member.pls.txt
|
225
|
|
|
|
|
|
|
my $self = shift;
|
226
|
|
|
|
|
|
|
my ($group_ads_path) = @_;
|
227
|
|
|
|
|
|
|
$group_ads_path = "WinNT://".$self->{_WinNTDomain}."/$group_ads_path,group" if ($group_ads_path !~ /\:\/\//);
|
228
|
|
|
|
|
|
|
my $group = Win32::OLE->GetObject($group_ads_path);
|
229
|
|
|
|
|
|
|
|
230
|
|
|
|
|
|
|
if ($self->_connect_type eq "LDAP"){
|
231
|
|
|
|
|
|
|
if($group_ads_path =~ /WinNT:/){
|
232
|
|
|
|
|
|
|
my $wintmp = "WinNT://".$self->{_WinNTDomain}."/".$self->{_username}.",user";
|
233
|
|
|
|
|
|
|
$group->Remove($wintmp);
|
234
|
|
|
|
|
|
|
}
|
235
|
|
|
|
|
|
|
else{
|
236
|
|
|
|
|
|
|
$group->Remove("LDAP://".$self->{_LDAPAdsPath});
|
237
|
|
|
|
|
|
|
}#fi
|
238
|
|
|
|
|
|
|
$group->SetInfo();
|
239
|
|
|
|
|
|
|
$self->get_info();
|
240
|
|
|
|
|
|
|
}
|
241
|
|
|
|
|
|
|
else{
|
242
|
|
|
|
|
|
|
$group->Remove($self->{_WinNTAdsPath});
|
243
|
|
|
|
|
|
|
$group->SetInfo();
|
244
|
|
|
|
|
|
|
}#fi
|
245
|
|
|
|
|
|
|
}
|
246
|
|
|
|
|
|
|
|
247
|
|
|
|
|
|
|
######################################################################
|
248
|
|
|
|
|
|
|
sub get_ou_member_list{
|
249
|
|
|
|
|
|
|
#Inspiration & Reference:
|
250
|
|
|
|
|
|
|
# http://www.rallenhome.com/books/adcookbook/src/05.03-enumerate_children.pls.txt
|
251
|
|
|
|
|
|
|
my $self = shift;
|
252
|
|
|
|
|
|
|
my ($search_mask) = @_;
|
253
|
|
|
|
|
|
|
my @members;
|
254
|
|
|
|
|
|
|
my $parent_ou = Win32::OLE->GetObject($self->{_connect_string});
|
255
|
|
|
|
|
|
|
if ($self->_connect_type eq "LDAP"){
|
256
|
|
|
|
|
|
|
for my $child (in $parent_ou) {
|
257
|
|
|
|
|
|
|
push(@members, $child->Name) if (defined $search_mask && $child->Name =~ /$search_mask/);
|
258
|
|
|
|
|
|
|
push(@members, $child->Name) if (not defined $search_mask);
|
259
|
|
|
|
|
|
|
}#rof
|
260
|
|
|
|
|
|
|
}
|
261
|
|
|
|
|
|
|
else{
|
262
|
|
|
|
|
|
|
warn "Win32::AD::User - The 'get_ou_member_list' function is not available when using \n",
|
263
|
|
|
|
|
|
|
"a WinNT:// ADsPath... use a LDAP:// ADsPath.\n";
|
264
|
|
|
|
|
|
|
}#fi
|
265
|
|
|
|
|
|
|
@members;
|
266
|
|
|
|
|
|
|
}
|
267
|
|
|
|
|
|
|
|
268
|
|
|
|
|
|
|
1;
|
269
|
|
|
|
|
|
|
|
270
|
|
|
|
|
|
|
__END__
|