3 # Written 2008 by J Kunkel [jkunkel@aplusg.de] for the OpenXPKI project
4 # (C) Copyright 2008 by The OpenXPKI Project
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
10 # http://www.apache.org/licenses/LICENSE-2.0
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
20 # export LOGIN=jkunkel && export PASSWD=topsecret \
21 # && ./auth_MS-ADS_over_LDAP-SASL.pl
24 # use this handler in auth.xml:
25 # =============================
26 # <handler name="MS-ADS LDAP-SASL" type="External">
28 # Login with your Microsoft ADS User-Account.
30 # <command>auth_MS-ADS_over_LDAP-SASL.pl</command>
32 # <pattern>x</pattern>
33 # <replacement>x</replacement>
36 # <value>__USER__</value>
40 # <value>__PASSWD__</value>
52 #########################
54 #########################
58 my $HOST = 'ag-dom-003';
59 my $BASEDN = "dc=a-g,dc=de";
61 my $USER = $ENV{'LOGIN'};
62 my $PASSWD = $ENV{'PASSWD'};
64 print "==>" . $USER . "\n" if $DEBUG;
65 print "==>" . $PASSWD . "\n" if $DEBUG;
67 # AD groups how represent the OpenXPKI roles
68 my $GROUP_CA = "OpenXPKI_CA";
69 my $GROUP_RA = "OpenXPKI_RA";
70 my $GROUP_USER = "OpenXPKI_USER";
77 #########################
78 #LDAP connect over SASL
79 #########################
80 until($ldap = Net
::LDAP
->new($HOST)) {
81 die "Can not connect to ldap://$HOST/" if ++$count > 10;
85 my $sasl = Authen
::SASL
->new(
86 mechanism
=> 'DIGEST-MD5',
93 my $mesg = $ldap->bind($BASEDN, sasl
=> $sasl, version
=> 3);
94 exit $mesg->code if $mesg->code;
96 #########################
98 #########################
99 my $group_ca_dn = &getGroupDN
($GROUP_CA);
100 my $group_ra_dn = &getGroupDN
($GROUP_RA);
101 my $group_user_dn = &getGroupDN
($GROUP_USER);
103 #########################
105 #########################
106 my $userDN = &getUserDN
($USER);
108 print "==>" . $group_user_dn . "\n" if $DEBUG;
109 print "==>" . $userDN . "\n" if $DEBUG;
111 #########################
114 #########################
116 &setRoleAndExit
("CA Operator") if &getIsMember
($group_ca_dn, $userDN);
117 &setRoleAndExit
("RA Operator") if &getIsMember
($group_ra_dn, $userDN);
118 &setRoleAndExit
("User") if &getIsMember
($group_user_dn, $userDN);
120 exit 1; # if not member of a group
122 #########################
124 #########################
136 # get DN of given group
140 #get original group DN
141 $mesg = $ldap->search(
143 filter
=> "(&(cn=$group)(objectclass=group))",
146 my $entry = $mesg->pop_entry();
147 print "==> group is ",$entry->dn(),"\n" if $DEBUG;
151 # get DN of given user
155 $mesg = $ldap->search(
157 filter
=> "samaccountname=$user",
160 my $entry = $mesg->pop_entry();
161 print "==> user is ",$entry->dn(),"\n" if $DEBUG;
165 #function userdn is member of groudn
168 my ($groupDN,$userDN) = @_;
171 print "==> in getIsMember:$groupDN\n" if $DEBUG;
172 #if user is a member then return true
173 my $mesg = $ldap->compare(
180 #0x06 == LDAP_COMPARE_TRUE
181 if ($mesg->code() == 0x06) {
185 #is also a group and perhaps a member of that group
186 my @groupattrs = ["member","objectclass","memberurl"];
188 $mesg = $ldap->search(
190 filter
=> "(|(objectclass=group)(objectclass=groupOfUrls))",
193 my $entry = $mesg->pop_entry();
194 #check is a member then return true
195 my $urlvalues = $entry->get_value("memberurl", asref
=> 1);
196 foreach my $urlval (@
{$urlvalues})
198 my $uri = new URI
($urlval);
199 my $filter = $uri->filter();
200 my @attrs = $uri->attributes();
202 $mesg = $ldap->search(
208 #if we find an entry it returns true
210 $entry = $mesg->pop_entry();
211 print "ldapurl",$entry->dn,"\n" if $DEBUG;
220 my $membervalues = $entry->get_value("member", asref
=> 1);
221 foreach my $val (@
{$membervalues})
223 #stop as soon as we have a match
224 if (&getIsMember
($val,$userDN))
231 die $mesg->error if $mesg->code;
232 #if make it this far then you must be a member
234 # retrun 0 if a fault