]> git.ipfire.org Git - thirdparty/squid.git/blame - src/acl/external/kerberos_sid_group/ext_kerberos_sid_group_acl.pl.in
Source Format Enforcement (#532)
[thirdparty/squid.git] / src / acl / external / kerberos_sid_group / ext_kerberos_sid_group_acl.pl.in
CommitLineData
4a544c9d 1#!@PERL@ -w
2
3use strict;
4use Pod::Usage;
5use Getopt::Long;
6use File::Basename;
7use Date::Format;
8
9=pod
10
11=head1 NAME
12
13 ext_kerberos_sid_group_acl - external ACL helper for Squid to verify AD Domain group membership using sid.
14
15=head1 SYNOPSIS
16
17 ext_kerberos_sid_group_acl [-d] [-h] -p Principal Name -D Domain Controller -b Base DN -G Group1:Group2
18
19=head1 DESCRIPTION
20
21B<ext_kerberos_sid_group_acl> is an installed executable script.
22It uses B<ldapsearch> from Openldap to lookup the name of a AD group sid.
23
24This helper must be used in with the negotiate_kerberos_auth helper in a
25Microsft AD or Samba environement.
26
27It reads from the standard input the domain username and a list of group sids
28and tries to match the group SIDs to the AD group sids.
29
30=head1 OPTIONS
31
32=over 12
33
34=item B<-d>
35
36Write debug info to stderr.
37
38=item B<-h>
39
40Print the help.
41
42=item B<-p principal name>
43
44Principal name in squid keytab to use for ldap authentication to AD
45
46=item B<-D domain controller>
47
48Domain controller to contact to lookup group SID
49
50=item B<-b base DN>
51
52Base DN for ldap search
53
54=item B<-G AD group name>
55
56AD group name to be used for SID lookup. List separated by a colon (:)
57
58=back
59
60=head1 CONFIGURATION
61
62 auth_param negotiate program /path/to/negotiate_wrapper_auth -d \
63 --ntlm /path/to/ntlm_auth --helper-protocol=squid-2.5-ntlmssp --domain example.com \
64 --kerberos /path/to/negotiate_kerberos_auth -d -s GSS_C_NO_NAME -k /path/to/squid.keytab -t none
65 external_acl_type sid_check %LOGIN %note{group} /path/to/kerberos_sid_group_acl -p principal -D dc1.example.com -b "DC=example,DC=com" -G Group1:Group2
66 acl squid_allow external sid_check
67 acl allowed_group external sid_check
68 http_access allow allowed_group
69
70If the local perl interpreter is in a unusual location it may need to be added:
71
72 external_acl_type sid_check %LOGIN %note{group} /path/to/perl /path/to/kerberos_sid_group_acl -p principal -D dc1.example.com -b "DC=example,DC=com" -G Group1:Group2
73
74=head1 AUTHOR
75
76This program was written by Markus Moeller <markus_moeller@compuserve.com>
77
78This manual was written by Markus Moeller <markus_moeller@compuserve.com>
79
80=head1 COPYRIGHT
81
77b1029d 82 * Copyright (C) 1996-2020 The Squid Software Foundation and contributors
4a544c9d 83 *
84 * Squid software is distributed under GPLv2+ license and includes
85 * contributions from numerous individuals and organizations.
86 * Please see the COPYING and CONTRIBUTORS files for details.
87
88 This program is put in the public domain by Markus Moeller
89 <markus_moeller@compuserve.com>. It is distributed in the hope that it will
90 be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
91 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
92
93=head1 QUESTIONS
94
95Questions on the usage of this program can be sent to the I<Squid Users mailing list <squid-users@lists.squid-cache.org>>
96
97=head1 REPORTING BUGS
98
99Bug reports need to be made in English.
100See http://wiki.squid-cache.org/SquidFaq/BugReporting for details of what you need to include with your bug report.
101
102Report bugs or bug fixes using http://bugs.squid-cache.org/
103
104Report serious security bugs to I<Squid Bugs <squid-bugs@lists.squid-cache.org>>
105
106Report ideas for new improvements to the I<Squid Developers mailing list <squid-dev@lists.squid-cache.org>>
107
108=head1 SEE ALSO
109
110negotiate_kerberos_auth(8)
111
112The Squid FAQ wiki http://wiki.squid-cache.org/SquidFaq
113
114The Squid Configuration Manual http://www.squid-cache.org/Doc/config/
115
116=cut
117
118#
119# Version history:
120# 2018-06-10 Markus Moeller <markus_moeller@compuserve.com>
121# Initial release
122#
123# Globals
124#
125use vars qw/ %opt /;
126
127my $name = basename($0);
128my $principal;
129my $dc;
130my $basedn;
131my $ccname="/tmp/squid_krb5cc";
132my $groupSIDs;
133my @ADgroupSIDs;
134my $user;
135my @groups;
136my $ans;
137
138# Disable output buffering
139$|=1;
140
141sub debug()
142{
143 my @lt = localtime;
144 print STDERR strftime("%Y/%m/%d %H:%M:%S", @lt)." | $name: @_\n" if $opt{d};
145}
146
147sub info()
148{
149 my @lt = localtime;
150 print STDERR strftime("%Y/%m/%d %H:%M:%S", @lt)." | $name: @_\n";
151}
152
153sub check()
154{
155 if ( grep( /^@_$/, @ADgroupSIDs) ) {
156 &debug("DEBUG: Found @_ in AD group SID");
157 return "OK";
158 } else {
159 &debug("DEBUG: Did not find @_ in AD group SID");
160 return "ERR";
161 }
162}
163
164#
165# Command line options processing
166#
167sub init()
168{
169 use Getopt::Std;
170 my $errmsg;
171 my $opt_string = 'hdD:p:b:G:';
172 getopts( "$opt_string", \%opt ) or usage();
173 Pod::Usage::pod2usage(1) if $opt{h};
174 Pod::Usage::pod2usage(1) if not defined $opt{D};
175 Pod::Usage::pod2usage(1) if not defined $opt{b};
176 Pod::Usage::pod2usage(1) if not defined $opt{p};
177 Pod::Usage::pod2usage(1) if not defined $opt{G};
178
179 $ENV{'KRB5CCNAME'} = $ccname;
180
181 @groups = split(/:/,$opt{G});
182 $errmsg=`kinit -k $opt{p} 2>&1`;
183 &info("ERROR: $errmsg") if $errmsg;
184 exit 99 if $errmsg;
185
186 $errmsg="";
187 foreach my $group (@groups) {
188 open(LDAP, "ldapsearch -LLL -Ygssapi -H ldap://$opt{D}:389 -s sub -b \"$opt{b}\" \"(CN=$group)\" objectsid 2>&1 |");
189 my $sid;
190 while (<LDAP>) {
191 chomp($_);
192 if ( $_ =~ /^object/ && defined $sid ) {
193 &info("ERROR: multiple SIDs returned for group $group");
194 } elsif ( $_ =~ /^object/ ) {
195 $sid=$_;
196 $sid=~s/^[^\s]+\s+//;
197 } else {
198 $errmsg=$errmsg.";".$_;
199 }
200 }
201 close(LDAP);
202 if ( ! defined $sid ) {
203 $errmsg=~s/^;//;
204 &info("ERROR: $errmsg");
205 &info("ERROR: no SID returned for group $group");
206 } else {
207 &info("INFO:ldapsearch result Group=$group, SID=$sid");
208 push @ADgroupSIDs, $sid;
209 }
210 }
211 &info("ERROR: Exit as no sid was found for any group") if ! @ADgroupSIDs;
212 exit 99 if ! @ADgroupSIDs;
213}
214
215init();
216&debug("INFO: Debugging mode ON.");
217
218#
219# Main loop
220#
221while (<STDIN>) {
222 chop;
223 &debug("DEBUG: Got $_ from squid");
224 ($user, $groupSIDs) = split(/\s+/);
225 if ( defined $user && defined $groupSIDs ) {
226 &debug("DEBUG: user=$user");
227 &debug("DEBUG: groups=$groupSIDs");
228 # test for each group squid send in it's request
229 foreach my $group (split(/,/,$groupSIDs)) {
230 $ans = &check($group);
231 last if $ans eq "OK";
232 }
233 &debug("DEBUG: Sending $ans to squid");
234 print "$ans\n";
235 } else {
236 &debug("DEBUG: Sending ERR to squid");
237 print "ERR\n";
238 }
239}
240