View source with raw comments or as raw
    1/*  Part of SWI-Prolog
    2
    3    Author:        Jan Wielemaker
    4    E-mail:        J.Wielemaker@vu.nl
    5    WWW:           http://www.swi-prolog.org
    6    Copyright (c)  2008-2012, University of Amsterdam
    7    All rights reserved.
    8
    9    Redistribution and use in source and binary forms, with or without
   10    modification, are permitted provided that the following conditions
   11    are met:
   12
   13    1. Redistributions of source code must retain the above copyright
   14       notice, this list of conditions and the following disclaimer.
   15
   16    2. Redistributions in binary form must reproduce the above copyright
   17       notice, this list of conditions and the following disclaimer in
   18       the documentation and/or other materials provided with the
   19       distribution.
   20
   21    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
   22    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
   23    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
   24    FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
   25    COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
   26    INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
   27    BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
   28    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
   29    CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   30    LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
   31    ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
   32    POSSIBILITY OF SUCH DAMAGE.
   33*/
   34
   35:- module(doc_access,
   36          [ host_access_options/2       % +AllOptions, -NoAccessOptions
   37          ]).   38:- if(exists_source(library(http/http_hook))).   39:- use_module(library(http/http_hook)).   40:- endif.   41:- use_module(library(dcg/basics)).   42
   43:- dynamic
   44    can_edit/1,
   45    allow_from/1,
   46    deny_from/1.   47
   48:- meta_predicate
   49    match_peer(1, +, +).   50
   51%       http:authenticate(+Type, +Request, -Extra) is semidet.
   52%
   53%       PlDoc specific access control. The access   control  is based on
   54%       these options that are passed from starting PlDoc. If PlDoc runs
   55%       on  top  of   an   external    dispatch   loop,   the  predicate
   56%       host_access_options/2 can be used to specify access rights.
   57%
   58%           * allow(+IP)
   59%           * deny(+IP)
   60%           * edit(+Bool)
   61
   62http:authenticate(pldoc(read), Request, []) :-
   63    !,
   64    memberchk(peer(Peer), Request),
   65    allowed_peer(Peer).
   66http:authenticate(pldoc(edit), Request, []) :-
   67    !,
   68    (   can_edit(false)
   69    ->  fail
   70    ;   (   memberchk(x_forwarded_for(Forwarded), Request),
   71            primary_forwarded_host(Forwarded, IPAtom),
   72            parse_ip(IPAtom, Peer)
   73        ->  true
   74        ;   memberchk(peer(Peer), Request)
   75        ),
   76        match_peer(localhost, +, Peer)
   77    ).
 host_access_options(+AllOptions, -NoAuthOptions) is det
Filter the authorization options from AllOptions, leaving the remaining options in NoAuthOptions.
   85host_access_options([], []).
   86host_access_options([H|T0], T) :-
   87    host_access_option(H),
   88    !,
   89    host_access_options(T0, T).
   90host_access_options([H|T0], [H|T]) :-
   91    host_access_options(T0, T).
   92
   93host_access_option(allow(From)) :-
   94    assert(allow_from(From)).
   95host_access_option(deny(From)) :-
   96    assert(deny_from(From)).
   97host_access_option(edit(Bool)) :-
   98    assert(can_edit(Bool)).
 match_peer(:RuleSet, +PlusMin, +Peer) is semidet
True if Peer is covered by the ruleset RuleSet. Peer is a term ip(A,B,C,D). RuleSet is a predicate with one argument that is either a partial ip term, a hostname or a domainname. Domainnames start with a '.'.
Arguments:
PlusMin- Positive/negative test. If IP->Host fails, a positive test fails, while a negative succeeds. I.e. deny('.com') succeeds for unknown IP addresses.
  112match_peer(Spec, _, Peer) :-
  113    call(Spec, Peer),
  114    !.
  115match_peer(Spec, PM, Peer) :-
  116    (   call(Spec, HOrDom), atom(HOrDom)
  117    ->  (   catch(tcp_host_to_address(Host, Peer), E, true),
  118            var(E)
  119        ->  call(Spec, HostOrDomain),
  120            atom(HostOrDomain),
  121            (   sub_atom(HostOrDomain, 0, _, _, '.')
  122            ->  sub_atom(Host, _, _, 0, HostOrDomain)
  123            ;   HostOrDomain == Host
  124            )
  125        ;   PM == (+)
  126        ->  !, fail
  127        ;   true
  128        )
  129    ).
 allowed_peer(+Peer) is semidet
True if Peer is allowed according to the rules.
  135allowed_peer(Peer) :-
  136    match_peer(deny_from, -, Peer),
  137    !,
  138    match_peer(allow_from, +, Peer).
  139allowed_peer(Peer) :-
  140    allow_from(_),
  141    !,
  142    match_peer(allow_from, +, Peer).
  143allowed_peer(_).
  144
  145
  146:- dynamic
  147    can_edit/1.
 primary_forwarded_host(+Spec, -Host) is det
x_forwarded host contains multiple hosts separated by ', ' if there are multiple proxy servers in between. The first one is the one the user's browser knows about.
  155primary_forwarded_host(Spec, Host) :-
  156    sub_atom(Spec, B, _, _, ','),
  157    !,
  158    sub_atom(Spec, 0, B, _, Host).
  159primary_forwarded_host(Host, Host).
  160
  161
  162localhost(ip(127,0,0,1)).
  163localhost(localhost).
  164
  165parse_ip(Atom, IP) :-
  166    atom_codes(Atom, Codes),
  167    phrase(ip(IP), Codes).
 ip(?IP)// is semidet
Parses A.B.C.D into ip(A,B,C,D)
  173ip(ip(A,B,C,D)) -->
  174    integer(A), ".", integer(B), ".", integer(C), ".", integer(D)