anchor.diff: -%% ===================================================================
  -%% @author Rafal Slota
  -%% @copyright (C): 2014 ACK CYFRONET AGH
  -%% This software is released under the MIT license
  -%% cited in 'LICENSE.txt'.
  -%% @end
  -%% ===================================================================
  -%% @doc: Port driver for oneproxy module.
  -%% @end
  -%% ===================================================================
  +%%%-------------------------------------------------------------------
  +%%% @author Rafal Slota
  +%%% @copyright (C) 2014 ACK CYFRONET AGH
  +%%% This software is released under the MIT license
  +%%% cited in 'LICENSE.txt'.
  +%%% @end
  +%%%-------------------------------------------------------------------
  +%%% @doc
  +%%% Port driver for oneproxy module.
  +%%% @end
  +%%%-------------------------------------------------------------------
   -module(oneproxy).
   -author("Rafal Slota").
   
  
positive.code:         {error, timeout}
      end.
  
  
  %% start/3
  %% ====================================================================
  %% @doc Starts oneproxy. This function either does not return or throws exception.
  %% @end
  -spec start(Mode :: string(), ListenerPort :: non_neg_integer(), Args :: list()) -> no_return().
  %% ====================================================================
  start(Mode, ListenerPort, Args) ->
      {ok, CWD} = file:get_cwd(),
      ExecPath = os:find_executable("oneproxy", filename:join(CWD, "c_lib")),
  
negative.code: get_local_port(Port) ->
      20000 + Port.
  
  %%--------------------------------------------------------------------
  %% @doc
  %% Starts oneproxy in proxy mode. This function either does not return or throws exception.
  %% @end
  %%--------------------------------------------------------------------
  -spec start_proxy(ListenerPort :: non_neg_integer(), CertFile :: string() | binary(),
      VerifyType :: verify_peer | verify_none) -> no_return().
  start_proxy(ListenerPort, CertFile, VerifyType) ->
      start("proxy", ListenerPort, [utils:ensure_list(CertFile),
          utils:ensure_list(VerifyType)]).
  
  %%--------------------------------------------------------------------
  %% @doc
  %% Starts oneproxy in reverse proxy mode with HTTP-based authentication
  %% delegation. This function either does not return or throws exception.
  %% @end
  %%--------------------------------------------------------------------
  -spec start_rproxy(ListenerPort :: non_neg_integer(), ForwardPort :: non_neg_integer(),
      CertFile :: string() | binary(), VerifyType :: verify_peer | verify_none) -> no_return().
  start_rproxy(ListenerPort, ForwardPort, CertFile, VerifyType) ->
      start_rproxy(ListenerPort, ForwardPort, CertFile, VerifyType, http).
  
  %%--------------------------------------------------------------------
  %% @doc
  %% Starts oneproxy in reverse proxy mode. This function either does not return or throws exception.
  %% @end
  %%--------------------------------------------------------------------
  -spec start_rproxy(ListenerPort :: non_neg_integer(), ForwardPort :: non_neg_integer(),
      CertFile :: string() | binary(), VerifyType :: verify_peer | verify_none,
      Http :: http | no_http) -> no_return().
  start_rproxy(ListenerPort, ForwardPort, CertFile, VerifyType, Http) ->
      start("reverse_proxy", ListenerPort, ["127.0.0.1",
          integer_to_list(ForwardPort), utils:ensure_list(CertFile),
          utils:ensure_list(VerifyType), utils:ensure_list(Http)]).
  
  %%--------------------------------------------------------------------
  %% @doc
  %% Returns directory (path) used by oneproxy to save DER CAs and CRLs.
  %% @end
  %%--------------------------------------------------------------------
  -spec get_der_certs_dir() -> string().
  get_der_certs_dir() ->
      {ok, CertDir} = application:get_env(?APP_NAME, ca_dir),
      filename:join(CertDir, ?DER_CERTS_DIR).
  
  %%--------------------------------------------------------------------
  %% @doc
  %% Save all CAs and CRLs from GSI state to given directory in DER format.
  %% @end
  %%--------------------------------------------------------------------
  -spec ca_crl_to_der(Dir :: string()) -> ok | {error, Reason :: any()}.
  ca_crl_to_der(Dir) ->
      case ets:info(gsi_state) of
          undefined ->
  