Skip to content

Commit 6ac37d7

Browse files
committed
CA-408500: Remove ListFile with Xapi_stdext_unix.Unixext
Signed-off-by: Lin Liu <[email protected]>
1 parent 938bdb4 commit 6ac37d7

File tree

3 files changed

+81
-99
lines changed

3 files changed

+81
-99
lines changed

ocaml/tests/test_extauth_plugin_ADwinbind.ml

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -505,7 +505,9 @@ let test_add_ipv4_localhost_to_hosts =
505505
let msg =
506506
Printf.sprintf "%s -> %s" (String.concat "\n" inp) (String.concat "\n" exp)
507507
in
508-
let actual = HostsConfIPv4.join "hostname" "domain" inp in
508+
let actual =
509+
HostsConfIPv4.join ~name:"hostname" ~domain:"domain" ~lines:inp
510+
in
509511
Alcotest.(check @@ list string) msg exp actual
510512
in
511513
let matrix =
@@ -537,7 +539,8 @@ let test_add_ipv4_and_ipv6_localhost_to_hosts =
537539
Printf.sprintf "%s -> %s" (String.concat "\n" inp) (String.concat "\n" exp)
538540
in
539541
let actual =
540-
HostsConfIPv6.join "hostname" "domain" inp |> fun lines ->
542+
HostsConfIPv6.join ~name:"hostname" ~domain:"domain" ~lines:inp
543+
|> fun lines ->
541544
HostsConfIPv4.join ~name:"hostname" ~domain:"domain" ~lines
542545
in
543546
Alcotest.(check @@ list string) msg exp actual
@@ -569,7 +572,9 @@ let test_remove_ipv4_localhost_from_hosts =
569572
let msg =
570573
Printf.sprintf "%s -> %s" (String.concat "\n" inp) (String.concat "\n" exp)
571574
in
572-
let actual = HostsConfIPv4.leave "hostname" "domain" inp in
575+
let actual =
576+
HostsConfIPv4.leave ~name:"hostname" ~domain:"domain" ~lines:inp
577+
in
573578
Alcotest.(check @@ list string) msg exp actual
574579
in
575580
let matrix =

ocaml/xapi/extauth_plugin_ADwinbind.ml

Lines changed: 73 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -815,7 +815,6 @@ let query_domain_workgroup ~domain =
815815
with _ -> raise (Auth_service_error (E_LOOKUP, err_msg))
816816

817817
let config_winbind_daemon ~workgroup ~netbios_name ~domain =
818-
let open Xapi_stdext_unix in
819818
let smb_config = "/etc/samba/smb.conf" in
820819
let allow_fallback =
821820
(*`allow kerberos auth fallback` depends on our internal samba patch,
@@ -825,42 +824,41 @@ let config_winbind_daemon ~workgroup ~netbios_name ~domain =
825824
* upgrade to samba packages with this capacity *)
826825
if !Xapi_globs.winbind_allow_kerberos_auth_fallback then "yes" else "no"
827826
in
828-
let conf_contents =
829-
match (workgroup, netbios_name, domain) with
830-
| Some wkgroup, Some netbios, Some dom ->
831-
[
832-
"# autogenerated by xapi"
833-
; "[global]"
834-
; "kerberos method = secrets and keytab"
835-
; Printf.sprintf "realm = %s" dom
836-
; "security = ADS"
837-
; "template shell = /bin/bash"
838-
; "winbind refresh tickets = yes"
839-
; "winbind enum groups = no"
840-
; "winbind enum users = no"
841-
; "winbind scan trusted domains = yes"
842-
; "winbind use krb5 enterprise principals = yes"
843-
; Printf.sprintf "winbind cache time = %d"
844-
!Xapi_globs.winbind_cache_time
845-
; Printf.sprintf "machine password timeout = 0"
846-
; Printf.sprintf "kerberos encryption types = %s"
847-
(Kerberos_encryption_types.Winbind.to_string
848-
!Xapi_globs.winbind_kerberos_encryption_type
849-
)
850-
; Printf.sprintf "workgroup = %s" wkgroup
851-
; Printf.sprintf "netbios name = %s" netbios
852-
; "idmap config * : range = 3000000-3999999"
853-
; Printf.sprintf "idmap config %s: backend = rid" dom
854-
; Printf.sprintf "idmap config %s: range = 2000000-2999999" dom
855-
; Printf.sprintf "log level = %s" (debug_level ())
856-
; Printf.sprintf "allow kerberos auth fallback = %s" allow_fallback
857-
; "idmap config * : backend = tdb"
858-
; "" (* Empty line at the end *)
859-
]
860-
| _ ->
861-
["# autogenerated by xapi"; "[global]"; "" (* Empty line at the end *)]
862-
in
863-
Helpers.ListFile.to_path smb_config conf_contents
827+
( match (workgroup, netbios_name, domain) with
828+
| Some wkgroup, Some netbios, Some dom ->
829+
[
830+
"# autogenerated by xapi"
831+
; "[global]"
832+
; "kerberos method = secrets and keytab"
833+
; Printf.sprintf "realm = %s" dom
834+
; "security = ADS"
835+
; "template shell = /bin/bash"
836+
; "winbind refresh tickets = yes"
837+
; "winbind enum groups = no"
838+
; "winbind enum users = no"
839+
; "winbind scan trusted domains = yes"
840+
; "winbind use krb5 enterprise principals = yes"
841+
; Printf.sprintf "winbind cache time = %d" !Xapi_globs.winbind_cache_time
842+
; Printf.sprintf "machine password timeout = 0"
843+
; Printf.sprintf "kerberos encryption types = %s"
844+
(Kerberos_encryption_types.Winbind.to_string
845+
!Xapi_globs.winbind_kerberos_encryption_type
846+
)
847+
; Printf.sprintf "workgroup = %s" wkgroup
848+
; Printf.sprintf "netbios name = %s" netbios
849+
; "idmap config * : range = 3000000-3999999"
850+
; Printf.sprintf "idmap config %s: backend = rid" dom
851+
; Printf.sprintf "idmap config %s: range = 2000000-2999999" dom
852+
; Printf.sprintf "log level = %s" (debug_level ())
853+
; Printf.sprintf "allow kerberos auth fallback = %s" allow_fallback
854+
; "idmap config * : backend = tdb"
855+
; "" (* Empty line at the end *)
856+
]
857+
| _ ->
858+
["# autogenerated by xapi"; "[global]"; "" (* Empty line at the end *)]
859+
)
860+
|> String.concat "\n"
861+
|> Xapi_stdext_unix.Unixext.write_string_to_file smb_config
864862

865863
let clear_winbind_config () =
866864
(* Keep the winbind configuration if xapi config file specified explictly,
@@ -1198,7 +1196,6 @@ module RotateMachinePassword = struct
11981196
let generate_krb5_tmp_config ~domain ~kdc_fqdn =
11991197
(* Configure which server to change the password
12001198
* https://web.mit.edu/kerberos/krb5-devel/doc/admin/conf_files/krb5_conf.html *)
1201-
let open Xapi_stdext_unix in
12021199
let realm = String.uppercase_ascii domain in
12031200
let domain_netbios =
12041201
Wbinfo.domain_name_of ~target_name_type:NetbiosName ~from_name:domain
@@ -1212,22 +1209,21 @@ module RotateMachinePassword = struct
12121209
[]
12131210
in
12141211

1215-
let conf_contents =
1216-
[
1217-
"# autogenerated by xapi"
1218-
; "[libdefaults]"
1219-
; Printf.sprintf "default_realm = %s" realm
1220-
; "[realms]"
1221-
; Printf.sprintf "%s={" realm
1222-
; Printf.sprintf "kpasswd_server=%s" kdc_fqdn
1223-
; Printf.sprintf "kdc=%s" kdc_fqdn
1224-
; "}" (* include winbind generated configure if exists *)
1225-
]
1226-
@ include_item
1227-
@ [""]
1228-
(* Empty line at the end *)
1229-
in
1230-
Helpers.ListFile.to_path tmp_krb5_conf conf_contents
1212+
[
1213+
"# autogenerated by xapi"
1214+
; "[libdefaults]"
1215+
; Printf.sprintf "default_realm = %s" realm
1216+
; "[realms]"
1217+
; Printf.sprintf "%s={" realm
1218+
; Printf.sprintf "kpasswd_server=%s" kdc_fqdn
1219+
; Printf.sprintf "kdc=%s" kdc_fqdn
1220+
; "}" (* include winbind generated configure if exists *)
1221+
]
1222+
@ include_item
1223+
@ [""]
1224+
(* Empty line at the end *)
1225+
|> String.concat "\n"
1226+
|> Xapi_stdext_unix.Unixext.write_string_to_file tmp_krb5_conf
12311227

12321228
let clear_tmp_krb5_conf () =
12331229
if !Xapi_globs.winbind_keep_configuration then
@@ -1354,19 +1350,23 @@ module HostsConfIPv4 = HostsConfFunc (HostsConfTagIPv4)
13541350
module HostsConfIPv6 = HostsConfFunc (HostsConfTagIPv6)
13551351

13561352
module ConfigHosts = struct
1353+
open Xapi_stdext_unix.Unixext
1354+
13571355
let path = "/etc/hosts"
13581356

13591357
let join ~name ~domain =
1360-
Helpers.ListFile.of_path path
1361-
|> HostsConfIPv4.join ~name ~domain
1362-
|> HostsConfIPv6.join ~name ~domain
1363-
|> Helpers.ListFile.to_path path
1358+
read_lines ~path |> fun lines ->
1359+
HostsConfIPv4.join ~name ~domain ~lines |> fun lines ->
1360+
HostsConfIPv6.join ~name ~domain ~lines
1361+
|> String.concat "\n"
1362+
|> write_string_to_file path
13641363

13651364
let leave ~name ~domain =
1366-
Helpers.ListFile.of_path path
1367-
|> HostsConfIPv4.leave ~name ~domain
1368-
|> HostsConfIPv6.leave ~name ~domain
1369-
|> Helpers.ListFile.to_path path
1365+
read_lines ~path |> fun lines ->
1366+
HostsConfIPv4.leave ~name ~domain ~lines |> fun lines ->
1367+
HostsConfIPv6.leave ~name ~domain ~lines
1368+
|> String.concat "\n"
1369+
|> write_string_to_file path
13701370
end
13711371

13721372
let build_netbios_name ~config_params =
@@ -1729,12 +1729,12 @@ module AuthADWinbind : Auth_signature.AUTH_MODULE = struct
17291729
let@ __context = Context.with_tracing ~__context __FUNCTION__ in
17301730
let user = List.assoc_opt "user" config_params in
17311731
let pass = List.assoc_opt "pass" config_params in
1732-
let {service_name; workgroup; netbios_name; _} =
1733-
get_domain_info_from_db ()
1734-
in
1735-
( if Option.is_some netbios_name then
1736-
Option.get netbios_name |> fun name ->
1732+
let {service_name; netbios_name; _} = get_domain_info_from_db () in
1733+
( match netbios_name with
1734+
| Some name ->
17371735
ConfigHosts.leave ~domain:service_name ~name
1736+
| _ ->
1737+
()
17381738
) ;
17391739

17401740
(* Clean extauth config *)
@@ -1763,12 +1763,12 @@ module AuthADWinbind : Auth_signature.AUTH_MODULE = struct
17631763
RotateMachinePassword.trigger_rotate ~start:5. ;
17641764
Winbind.check_ready_to_serve ~timeout:300. ;
17651765

1766-
let {service_name; workgroup; netbios_name; _} =
1767-
get_domain_info_from_db ()
1768-
in
1769-
if Option.is_some netbios_name then
1770-
Option.get netbios_name |> fun name ->
1771-
ConfigHosts.join ~domain:service_name ~name
1766+
let {service_name; netbios_name; _} = get_domain_info_from_db () in
1767+
match netbios_name with
1768+
| Some name ->
1769+
ConfigHosts.join ~domain:service_name ~name
1770+
| _ ->
1771+
()
17721772

17731773
(* unit on_xapi_exit()
17741774

ocaml/xapi/helpers.ml

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2360,26 +2360,3 @@ module AuthenticationCache = struct
23602360
None
23612361
end
23622362
end
2363-
2364-
module ListFile = struct
2365-
(* Read/Write List to/from file, line by line *)
2366-
let of_path path =
2367-
let ic = open_in path in
2368-
finally
2369-
(fun () ->
2370-
let rec read_lines acc =
2371-
try
2372-
let line = input_line ic in
2373-
read_lines (acc @ [line])
2374-
with End_of_file -> acc
2375-
in
2376-
read_lines []
2377-
)
2378-
(fun () -> close_in ic)
2379-
2380-
let to_path ?(perm = 0o0644) path contents =
2381-
String.concat "\n" contents |> fun x ->
2382-
Unixext.atomic_write_to_file path perm @@ fun fd ->
2383-
Unixext.really_write_string fd x |> ignore ;
2384-
Unix.fsync fd
2385-
end

0 commit comments

Comments
 (0)