View source with formatted comments or as raw
    1/*  Part of SWISH
    2
    3    Author:        Jan Wielemaker
    4    E-mail:        J.Wielemaker@cs.vu.nl
    5    WWW:           http://www.swi-prolog.org
    6    Copyright (C): 2017, VU University Amsterdam
    7			 CWI Amsterdam
    8    All rights reserved.
    9
   10    Redistribution and use in source and binary forms, with or without
   11    modification, are permitted provided that the following conditions
   12    are met:
   13
   14    1. Redistributions of source code must retain the above copyright
   15       notice, this list of conditions and the following disclaimer.
   16
   17    2. Redistributions in binary form must reproduce the above copyright
   18       notice, this list of conditions and the following disclaimer in
   19       the documentation and/or other materials provided with the
   20       distribution.
   21
   22    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
   23    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
   24    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
   25    FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
   26    COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
   27    INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
   28    BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
   29    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
   30    CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   31    LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
   32    ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
   33    POSSIBILITY OF SUCH DAMAGE.
   34*/
   35
   36:- module(swish_pep,
   37          [ authorized/2,               % +Action, +Options
   38            ws_authorized/2             % +Action, +WSID
   39          ]).   40:- use_module(library(debug)).   41:- use_module(library(option)).   42:- use_module(library(error)).   43:- use_module(library(http/http_wrapper)).   44
   45:- use_module(authenticate).   46:- use_module(storage).   47:- use_module(config).   48
   49/** <module> SWISH PEP (Policy Enforcement Point)
   50
   51This module implements the _Policy Enforcement   Point_. It is called by
   52modules that perform operations that may   not be publically accessible.
   53Examples are:
   54
   55  - Access to files (download, create, update, delete, list, search)
   56  - Control of the sandboxing
   57  - Access to users (profile management)
   58*/
   59
   60:- multifile
   61    swish_config:approve/3.   62
   63%!  authorized(+Action, +Options) is det.
   64%
   65%   Verify that Action is authorized.  Options:
   66%
   67%     - indentity(+Identity)
   68%       Indentity is the identity dict as collected by autenticate.pl.
   69%
   70%   Actions defined:
   71%
   72%     * Gitty store actions
   73%       - gitty(download(Obj, Format))
   74%         Attempt to download Obj, one of file(File) or hash(Hash) in
   75%         Format, see storage_get/4 from storage.pl
   76%       - gitty(create(File,Named,Meta))
   77%         Create file name File with the given meta-data.  Named is one
   78%         of `named` or `random` and indicates whether the file is named
   79%         by the user or the name is generated by the system.
   80%       - gitty(update(File,PrevMeta,Meta))
   81%         Update File and change meta-data from PrevMeta to Meta.
   82%       - gitty(delete(File,Meta))
   83%         Delete File that has the given meta data.
   84%     * File actions
   85%       - file(update(File,Meta))
   86%         Update (save) a physical file outside the versioned gitty
   87%         store.
   88%     * Social options
   89%       - chat(open)
   90%         Open websocket chat channel
   91%       - chat(post(Message, About))
   92%         Post a chat message about a specific topic
   93%
   94%   @throws http_reply(forbidden(URL)) if the action is not allowed. Can
   95%   we generate a JSON error object?
   96
   97authorized(Action, _Options) :-
   98    var(Action),
   99    !,
  100    instantiation_error(Action).
  101authorized(Action, Options) :-
  102    option(identity(Id), Options),
  103    (   authorize(Action, Id)
  104    ->  debug(pep, 'Approved gitty action ~p to ~p', [Action, Id])
  105    ;   debug(pep, 'Denied action ~p to ~p', [Action, Id]),
  106        http_current_request(Request),
  107        option(path(Path), Request),
  108        throw(http_reply(forbidden(Path)))
  109    ).
  110
  111%!  ws_authorized(+Action, +WSUser) is semidet.
  112%
  113%   True when WSUser is allowed to  perform   action.  WSUser  is a dict
  114%   containing the user info as  provided by chat:chat_add_user_id/3. It
  115%   notably has a key `profile_id` if the user is logged on.
  116%
  117%   @tbd Generalise. Notably, how do we get the identity as
  118%   authenticate/2 returns?
  119
  120ws_authorized(Action, _WSUser) :-
  121    var(Action),
  122    !,
  123    instantiation_error(Action).
  124ws_authorized(chat(post(_,_)), WSUser) :-
  125    _Profile = WSUser.get(profile_id).
  126
  127
  128:- multifile
  129    approve/2,
  130    deny/2.  131
  132authorize(Action, Id) :-
  133    swish_config:approve(Action, Id, Approve),
  134    !,
  135    Approve == true.
  136authorize(Action, Id) :-
  137    approve(Action, Id), !,
  138    \+ deny(Action, Id).
  139
  140%!  approve(+Action, +Id)
  141
  142approve(gitty(update(_File,PrevMeta,_Meta)), Auth) :- !,
  143    storage_meta_property(PrevMeta, modify(Modify)),
  144    (   memberchk(any, Modify)
  145    ->  true
  146    ;   memberchk(login, Modify)
  147    ->  user_property(Auth, login(_))
  148    ;   storage_meta_property(PrevMeta, identity(Id)),
  149        user_property(Auth, identity(Id))
  150    ).
  151approve(gitty(_), _).
  152approve(file(update(_File, _Meta)), Auth) :-
  153    user_property(Auth, login(local)).
  154approve(run(any, _), Auth) :-
  155    user_property(Auth, login(local)).
  156approve(chat(open), _).
  157
  158%!  deny(+Action, +Id)
  159
  160%!  swish_config:approve(+Action, +Identity, -Approve) is semidet.
  161%
  162%   This hook is called by approve/2 and deny/2 before the default
  163%   rules.  If this hook succeeds it must unify Approve with `true`
  164%   or `false`.  Action is approved if Approve is `true`.
  165
  166
  167
  168		 /*******************************
  169		 *           PENGINES		*
  170		 *******************************/
  171
  172%!  pengines:not_sandboxed(+User, +Application) is semidet.
  173%
  174%   Called by Pengines to  see  whether   User  may  call  non-sandboxed
  175%   operations in Application.
  176
  177:- multifile pengines:not_sandboxed/2.  178
  179pengines:not_sandboxed(User, Application) :-
  180    authorized(run(any, Application), [identity(User)])