@@ -90,21 +90,61 @@ let ntlm_auth uname passwd : (unit, exn) result =
9090 with _ -> Error (auth_ex uname)
9191
9292let get_domain_info_from_db () =
93- (fun __context ->
94- let host = Helpers. get_localhost ~__context in
95- let service_name =
96- Db.Host. get_external_auth_service_name ~__context ~self: host
97- in
98- let workgroup, netbios_name =
99- Db.Host. get_external_auth_configuration ~__context ~self: host |> fun l ->
100- (List. assoc_opt " workgroup" l, List. assoc_opt " netbios_name" l)
101- in
102- {service_name; workgroup; netbios_name}
103- )
104- |> Server_helpers. exec_with_new_task
105- " retrieving external auth domain workgroup"
93+ Server_helpers. exec_with_new_task " retrieving external auth domain workgroup"
94+ @@ fun __context ->
95+ let host = Helpers. get_localhost ~__context in
96+ let service_name =
97+ Db.Host. get_external_auth_service_name ~__context ~self: host
98+ in
99+ let workgroup, netbios_name =
100+ Db.Host. get_external_auth_configuration ~__context ~self: host
101+ |> fun config ->
102+ (List. assoc_opt " workgroup" config, List. assoc_opt " netbios_name" config)
103+ in
104+ {service_name; workgroup; netbios_name}
106105
107106module Ldap = struct
107+ module Escape = struct
108+ (*
109+ * Escape characters according to
110+ * https://docs.microsoft.com/en-gb/windows/win32/adsi/search-filter-syntax?redirectedfrom=MSDN#special-characters
111+ * *)
112+
113+ let reg_star = {|*| } |> Re. str |> Re. compile
114+
115+ let reg_left_bracket = {| (| } |> Re. str |> Re. compile
116+
117+ let reg_right_bracket = {| )| } |> Re. str |> Re. compile
118+
119+ let reg_backward_slash = {| \| } |> Re. str |> Re. compile
120+
121+ let reg_null = " \000 " |> Re. str |> Re. compile
122+
123+ let reg_slash = {|/| } |> Re. str |> Re. compile
124+
125+ let escape_map =
126+ [
127+ (* backward slash goes first as others will include backward slash*)
128+ (reg_backward_slash, {| \5 d| })
129+ ; (reg_star, {| \2 a| })
130+ ; (reg_left_bracket, {| \28 | })
131+ ; (reg_right_bracket, {| \29 | })
132+ ; (reg_null, {| \00 | })
133+ ; (reg_slash, {| \2 f| })
134+ ]
135+
136+ let escape str =
137+ List. fold_left
138+ (fun acc element ->
139+ let reg = fst element in
140+ let value = snd element in
141+ Re. replace_string reg ~by: value acc
142+ )
143+ str escape_map
144+ end
145+
146+ let escape str = Escape. escape str
147+
108148 type user = {
109149 name : string
110150 ; display_name : string
@@ -223,15 +263,15 @@ module Ldap = struct
223263 ; password_expired= logand user_account_control passw_expire_bit <> 0l
224264 }
225265
226- let env_of_lookup domain_netbios =
266+ let env_of_krb5 domain_netbios =
227267 let domain_krb5_cfg =
228268 Filename. concat domain_krb5_dir
229269 (Printf. sprintf " krb5.conf.%s" domain_netbios)
230270 in
231271 [|Printf. sprintf " KRB5_CONFIG=%s" domain_krb5_cfg|]
232272
233273 let query_user sid domain_netbios kdc =
234- let env = env_of_lookup domain_netbios in
274+ let env = env_of_krb5 domain_netbios in
235275 let * stdout =
236276 try
237277 (* Query KDC instead of use domain here
@@ -259,7 +299,9 @@ module Ldap = struct
259299
260300 let query_sid ~name ~kdc ~domain_netbios =
261301 let key = " objectSid" in
262- let env = env_of_lookup domain_netbios in
302+ let env = env_of_krb5 domain_netbios in
303+ let name = escape name in
304+ (* Escape name to avoid injection detection *)
263305 let query = Printf. sprintf " (|(sAMAccountName=%s)(name=%s))" name name in
264306 let args =
265307 [
@@ -676,11 +718,9 @@ let from_config ~name ~err_msg ~config_params =
676718let all_number_re = Re.Perl. re {|^ \d+$| } |> Re.Perl. compile
677719
678720let get_localhost_name () =
679- (fun __context ->
680- Helpers. get_localhost ~__context |> fun host ->
681- Db.Host. get_hostname ~__context ~self: host
682- )
683- |> Server_helpers. exec_with_new_task " retrieving hostname"
721+ Server_helpers. exec_with_new_task " retrieving hostname" @@ fun __context ->
722+ Helpers. get_localhost ~__context |> fun host ->
723+ Db.Host. get_hostname ~__context ~self: host
684724
685725let assert_hostname_valid ~hostname =
686726 let all_numbers = Re. matches all_number_re hostname <> [] in
@@ -716,13 +756,12 @@ let persist_extauth_config ~domain ~user ~ou_conf ~workgroup ~netbios_name =
716756 ]
717757 @ ou_conf
718758 in
719- (fun __context ->
720- Helpers. get_localhost ~__context |> fun self ->
721- Db.Host. set_external_auth_configuration ~__context ~self ~value ;
722- Db.Host. get_name_label ~__context ~self
723- |> debug " update external_auth_configuration for host %s"
724- )
725- |> Server_helpers. exec_with_new_task " update external_auth_configuration"
759+ Server_helpers. exec_with_new_task " update external_auth_configuration"
760+ @@ fun __context ->
761+ Helpers. get_localhost ~__context |> fun self ->
762+ Db.Host. set_external_auth_configuration ~__context ~self ~value ;
763+ Db.Host. get_name_label ~__context ~self
764+ |> debug " update external_auth_configuration for host %s"
726765
727766let disable_machine_account ~service_name = function
728767 | Some u , Some p -> (
@@ -884,21 +923,19 @@ module ClosestKdc = struct
884923 Error e
885924
886925 let update_db ~domain ~kdc =
887- (fun __context ->
888- let self = Helpers. get_localhost ~__context in
889- Db.Host. get_external_auth_configuration ~__context ~self |> fun value ->
890- (domain, kdc) :: List. remove_assoc domain value |> fun value ->
891- Db.Host. set_external_auth_configuration ~__context ~self ~value
892- )
893- |> Server_helpers. exec_with_new_task " update domain closest kdc"
926+ Server_helpers. exec_with_new_task " update domain closest kdc"
927+ @@ fun __context ->
928+ let self = Helpers. get_localhost ~__context in
929+ Db.Host. get_external_auth_configuration ~__context ~self |> fun value ->
930+ (domain, kdc) :: List. remove_assoc domain value |> fun value ->
931+ Db.Host. set_external_auth_configuration ~__context ~self ~value
894932
895933 let from_db domain =
896- (fun __context ->
897- let self = Helpers. get_localhost ~__context in
898- Db.Host. get_external_auth_configuration ~__context ~self
899- |> List. assoc_opt domain
900- )
901- |> Server_helpers. exec_with_new_task " query domain closest kdc"
934+ Server_helpers. exec_with_new_task " query domain closest kdc"
935+ @@ fun __context ->
936+ let self = Helpers. get_localhost ~__context in
937+ Db.Host. get_external_auth_configuration ~__context ~self
938+ |> List. assoc_opt domain
902939
903940 let lookup domain =
904941 try
@@ -982,6 +1019,7 @@ let closest_kdc_of_domain domain =
9821019
9831020module AuthADWinbind : Auth_signature .AUTH_MODULE = struct
9841021 let get_subject_identifier' subject_name =
1022+ (* Called in the login path with a yet unauthenticated user *)
9851023 let * domain =
9861024 try Ok (get_domain_info_from_db () ).service_name with e -> Error e
9871025 in
0 commit comments