mirror of
https://github.com/libguestfs/libguestfs.git
synced 2026-03-21 22:53:37 +00:00
mllib: password: Replace password string with password_selector type.
This is just code motion, there is no functional change.
This commit is contained in:
@@ -466,7 +466,7 @@ let main () =
|
||||
let pw = make_random_password () in
|
||||
msg (f_"Random root password: %s [did you mean to use --root-password?]")
|
||||
pw;
|
||||
pw in
|
||||
Password.Set_password pw in
|
||||
|
||||
match g#inspect_get_type root with
|
||||
| "linux" ->
|
||||
|
||||
@@ -137,7 +137,7 @@ let parse_cmdline () =
|
||||
|
||||
let root_password = ref None in
|
||||
let set_root_password arg =
|
||||
let pw = get_password ~prog arg in
|
||||
let pw = parse_selector ~prog arg in
|
||||
root_password := Some pw
|
||||
in
|
||||
|
||||
|
||||
@@ -21,7 +21,9 @@ open Printf
|
||||
|
||||
type password_crypto = [`MD5 | `SHA256 | `SHA512 ]
|
||||
|
||||
type password_map = (string, string) Hashtbl.t
|
||||
type password_selector = Set_password of string
|
||||
|
||||
type password_map = (string, password_selector) Hashtbl.t
|
||||
|
||||
let password_crypto_of_string ~prog = function
|
||||
| "md5" -> `MD5
|
||||
@@ -32,7 +34,7 @@ let password_crypto_of_string ~prog = function
|
||||
prog arg;
|
||||
exit 1
|
||||
|
||||
let rec get_password ~prog arg =
|
||||
let rec parse_selector ~prog arg =
|
||||
let i =
|
||||
try String.index arg ':'
|
||||
with Not_found ->
|
||||
@@ -43,8 +45,8 @@ let rec get_password ~prog arg =
|
||||
String.sub arg 0 i, String.sub arg (i+1) (len-(i+1)) in
|
||||
|
||||
match key with
|
||||
| "file" -> read_password_from_file value
|
||||
| "password" -> value
|
||||
| "file" -> Set_password (read_password_from_file value)
|
||||
| "password" -> Set_password value
|
||||
| _ ->
|
||||
eprintf (f_"%s: password format, \"%s:...\" is not recognized; see the man page.\n") prog key;
|
||||
exit 1
|
||||
@@ -78,10 +80,12 @@ let rec set_linux_passwords ~prog ?password_crypto g root passwords =
|
||||
*)
|
||||
let i = String.index line ':' in
|
||||
let user = String.sub line 0 i in
|
||||
let password = Hashtbl.find passwords user in
|
||||
let selector = Hashtbl.find passwords user in
|
||||
let j = String.index_from line (i+1) ':' in
|
||||
let rest = String.sub line j (String.length line - j) in
|
||||
user ^ ":" ^ encrypt password crypto ^ rest
|
||||
match selector with
|
||||
| Set_password password ->
|
||||
user ^ ":" ^ encrypt password crypto ^ rest
|
||||
with Not_found -> line
|
||||
) shadow in
|
||||
|
||||
|
||||
@@ -21,11 +21,15 @@ type password_crypto = [ `MD5 | `SHA256 | `SHA512 ]
|
||||
val password_crypto_of_string : prog:string -> string -> password_crypto
|
||||
(** Parse --password-crypto parameter on command line. *)
|
||||
|
||||
val get_password : prog:string -> string -> string
|
||||
(** Parse various --password/--root-password/--user passwords on command line.*)
|
||||
type password_selector = Set_password of string
|
||||
|
||||
type password_map = (string, string) Hashtbl.t
|
||||
val parse_selector : prog:string -> string -> password_selector
|
||||
(** Parse the selector field in --password/--root-password. Note this
|
||||
doesn't parse the username part. Exits if the format is not valid. *)
|
||||
|
||||
type password_map = (string, password_selector) Hashtbl.t
|
||||
(** A map of username -> selector. *)
|
||||
|
||||
val set_linux_passwords : prog:string -> ?password_crypto:password_crypto -> Guestfs.guestfs -> string -> password_map -> unit
|
||||
(** Adjust the passwords of a Linux guest according to the (username,
|
||||
password) map. *)
|
||||
(** Adjust the passwords of a Linux guest according to the
|
||||
password map. *)
|
||||
|
||||
@@ -39,7 +39,7 @@ and set_user_password arg =
|
||||
set_password (String.sub arg 0 i) (String.sub arg (i+1) (len-(i+1)))
|
||||
|
||||
and set_password user arg =
|
||||
let pw = get_password ~prog arg in
|
||||
let pw = parse_selector ~prog arg in
|
||||
|
||||
if Hashtbl.mem passwords user then (
|
||||
eprintf (f_"virt-sysprep: password: multiple --root-password/--password options set the password for user '%s' twice.\n") user;
|
||||
|
||||
Reference in New Issue
Block a user