<commit_before>(*
 * Copyright (C) 2006-2009 Citrix Systems Inc.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published
 * by the Free Software Foundation; version 2.1 only. with the special
 * exception on linking described in file LICENSE.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *)
(* Functions to help create HTTP handlers which check the user is properly authenticated. *)

module D = Debug.Debugger(struct let name="xapi" end)
open D

let validate_session __context session_id realm = 
  try
    let (_: string) = Db.Session.get_uuid ~__context ~self:session_id in ()
  with _ ->
    debug "Failed to find session_id %s in database. Perhaps it timed-out?" (Context.trackid_of_session (Some session_id));
    raise (Http.Unauthorised realm)

(* Talk to the master over the network. NB we deliberately use the network rather than
   the unix domain socket because we don't want to accidentally bypass the authentication *)
let inet_rpc xml = Xmlrpcclient.do_secure_xml_rpc
  ~use_stunnel_cache:true
  ~version:"1.1" ~host:(if Pool_role.is_master() then "127.0.0.1" else Pool_role.get_master_address ())
  ~port:!Xapi_globs.https_port ~path:"/" xml 

open Client

open Http

exception NoAuth

let get_session_id (req: request) =
  let all = req.cookie @ req.query in
  if List.mem_assoc "session_id" all
  then
		let session_id = (Ref.of_string (List.assoc "session_id" all)) in
		session_id
	else
		Ref.null


let append_to_master_audit_log __context action line =
  (* http actions are not automatically written to the master's audit log *)
  (* it is necessary to do that manually from the slaves *)
	if Stringext.String.startswith
		Datamodel.rbac_http_permission_prefix
		action
	then
	if Pool_role.is_slave ()
	then begin
		Helpers.call_api_functions ~__context (fun rpc session_id ->
			Client.Pool.audit_log_append ~rpc ~session_id  ~line
		)<commit_msg>CA-56324: Fix HTTP handlers when the management interface is disabled<commit_after>(*
 * Copyright (C) 2006-2009 Citrix Systems Inc.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published
 * by the Free Software Foundation; version 2.1 only. with the special
 * exception on linking described in file LICENSE.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *)
(* Functions to help create HTTP handlers which check the user is properly authenticated. *)

module D = Debug.Debugger(struct let name="xapi" end)
open D

let validate_session __context session_id realm = 
  try
    let (_: string) = Db.Session.get_uuid ~__context ~self:session_id in ()
  with _ ->
    debug "Failed to find session_id %s in database. Perhaps it timed-out?" (Context.trackid_of_session (Some session_id));
    raise (Http.Unauthorised realm)

(* Talk to the master over the network. NB we deliberately use the network rather than
   the unix domain socket because we don't want to accidentally bypass the authentication *)
let inet_rpc xml = 
	let version = "1.1" and host = "127.0.0.1" and path = "/" in
	let http = 80 and https = !Xapi_globs.https_port in
	(* Bypass SSL for localhost, this works even if the management interface
	   is disabled. *)
	if Pool_role.is_master ()
	then Xmlrpcclient.do_xml_rpc ~version ~host ~port:http ~path xml
	else Xmlrpcclient.do_secure_xml_rpc ~version ~host ~port:https ~path ~use_stunnel_cache:true xml


open Client

open Http

exception NoAuth

let get_session_id (req: request) =
  let all = req.cookie @ req.query in
  if List.mem_assoc "session_id" all
  then
		let session_id = (Ref.of_string (List.assoc "session_id" all)) in
		session_id
	else
		Ref.null


let append_to_master_audit_log __context action line =
  (* http actions are not automatically written to the master's audit log *)
  (* it is necessary to do that manually from the slaves *)
	if Stringext.String.startswith
		Datamodel.rbac_http_permission_prefix
		action
	then
	if Pool_role.is_slave ()
	then begin
		Helpers.call_api_functions ~__context (fun rpc session_id ->
			Client.Pool.audit_log_append ~rpc ~session_id  ~line
		)
{'fraction_non_alphanumeric': 0.0718471890601259, 'fraction_numerical': 0.009333622747992185, 'mean_word_length': 4.183352080989876, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 8}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3792562', 'n_tokens_mistral': 1421, 'n_tokens_neox': 1343, 'n_words': 555}