Skip to content

Commit

Permalink
CA-356901: Escape subject before perform ldap query
Browse files Browse the repository at this point in the history
Signed-off-by: Lin Liu <[email protected]>
  • Loading branch information
liulinC committed Jul 26, 2021
1 parent 1809cd2 commit 6ff5967
Showing 1 changed file with 36 additions and 41 deletions.
77 changes: 36 additions & 41 deletions ocaml/xapi/extauth_plugin_ADwinbind.ml
Original file line number Diff line number Diff line change
Expand Up @@ -90,19 +90,18 @@ let ntlm_auth uname passwd : (unit, exn) result =
with _ -> Error (auth_ex uname)

let get_domain_info_from_db () =
(fun __context ->
let host = Helpers.get_localhost ~__context in
let service_name =
Db.Host.get_external_auth_service_name ~__context ~self:host
in
let workgroup, netbios_name =
Db.Host.get_external_auth_configuration ~__context ~self:host |> fun l ->
(List.assoc_opt "workgroup" l, List.assoc_opt "netbios_name" l)
in
{service_name; workgroup; netbios_name}
)
|> Server_helpers.exec_with_new_task
"retrieving external auth domain workgroup"
Server_helpers.exec_with_new_task "retrieving external auth domain workgroup"
@@ fun __context ->
let host = Helpers.get_localhost ~__context in
let service_name =
Db.Host.get_external_auth_service_name ~__context ~self:host
in
let workgroup, netbios_name =
Db.Host.get_external_auth_configuration ~__context ~self:host
|> fun config ->
(List.assoc_opt "workgroup" config, List.assoc_opt "netbios_name" config)
in
{service_name; workgroup; netbios_name}

module Ldap = struct
type user = {
Expand Down Expand Up @@ -223,15 +222,15 @@ module Ldap = struct
; password_expired= logand user_account_control passw_expire_bit <> 0l
}

let env_of_lookup domain_netbios =
let env_of_krb5 domain_netbios =
let domain_krb5_cfg =
Filename.concat domain_krb5_dir
(Printf.sprintf "krb5.conf.%s" domain_netbios)
in
[|Printf.sprintf "KRB5_CONFIG=%s" domain_krb5_cfg|]

let query_user sid domain_netbios kdc =
let env = env_of_lookup domain_netbios in
let env = env_of_krb5 domain_netbios in
let* stdout =
try
(* Query KDC instead of use domain here
Expand Down Expand Up @@ -259,7 +258,8 @@ module Ldap = struct

let query_sid ~name ~kdc ~domain_netbios =
let key = "objectSid" in
let env = env_of_lookup domain_netbios in
let env = env_of_krb5 domain_netbios in
let name = String.escaped name in
let query = Printf.sprintf "(|(sAMAccountName=%s)(name=%s))" name name in
let args =
[
Expand Down Expand Up @@ -676,11 +676,9 @@ let from_config ~name ~err_msg ~config_params =
let all_number_re = Re.Perl.re {|^\d+$|} |> Re.Perl.compile

let get_localhost_name () =
(fun __context ->
Helpers.get_localhost ~__context |> fun host ->
Db.Host.get_hostname ~__context ~self:host
)
|> Server_helpers.exec_with_new_task "retrieving hostname"
Server_helpers.exec_with_new_task "retrieving hostname" @@ fun __context ->
Helpers.get_localhost ~__context |> fun host ->
Db.Host.get_hostname ~__context ~self:host

let assert_hostname_valid ~hostname =
let all_numbers = Re.matches all_number_re hostname <> [] in
Expand Down Expand Up @@ -716,13 +714,12 @@ let persist_extauth_config ~domain ~user ~ou_conf ~workgroup ~netbios_name =
]
@ ou_conf
in
(fun __context ->
Helpers.get_localhost ~__context |> fun self ->
Db.Host.set_external_auth_configuration ~__context ~self ~value ;
Db.Host.get_name_label ~__context ~self
|> debug "update external_auth_configuration for host %s"
)
|> Server_helpers.exec_with_new_task "update external_auth_configuration"
Server_helpers.exec_with_new_task "update external_auth_configuration"
@@ fun __context ->
Helpers.get_localhost ~__context |> fun self ->
Db.Host.set_external_auth_configuration ~__context ~self ~value ;
Db.Host.get_name_label ~__context ~self
|> debug "update external_auth_configuration for host %s"

let disable_machine_account ~service_name = function
| Some u, Some p -> (
Expand Down Expand Up @@ -884,21 +881,19 @@ module ClosestKdc = struct
Error e

let update_db ~domain ~kdc =
(fun __context ->
let self = Helpers.get_localhost ~__context in
Db.Host.get_external_auth_configuration ~__context ~self |> fun value ->
(domain, kdc) :: List.remove_assoc domain value |> fun value ->
Db.Host.set_external_auth_configuration ~__context ~self ~value
)
|> Server_helpers.exec_with_new_task "update domain closest kdc"
Server_helpers.exec_with_new_task "update domain closest kdc"
@@ fun __context ->
let self = Helpers.get_localhost ~__context in
Db.Host.get_external_auth_configuration ~__context ~self |> fun value ->
(domain, kdc) :: List.remove_assoc domain value |> fun value ->
Db.Host.set_external_auth_configuration ~__context ~self ~value

let from_db domain =
(fun __context ->
let self = Helpers.get_localhost ~__context in
Db.Host.get_external_auth_configuration ~__context ~self
|> List.assoc_opt domain
)
|> Server_helpers.exec_with_new_task "query domain closest kdc"
Server_helpers.exec_with_new_task "query domain closest kdc"
@@ fun __context ->
let self = Helpers.get_localhost ~__context in
Db.Host.get_external_auth_configuration ~__context ~self
|> List.assoc_opt domain

let lookup domain =
try
Expand Down

0 comments on commit 6ff5967

Please sign in to comment.