X-Git-Url: http://matita.cs.unibo.it/gitweb/?a=blobdiff_plain;f=matitaB%2Fmatita%2FmatitaAuthentication.ml;h=a3704ee4eb3056b16287f74f2d16847842afafc5;hb=111e641bb0772a542293b796c9d4a18fc9d58a00;hp=1b9a91b7160024d8debd958a1b70ee6dcd094cac;hpb=d8ae533d041cb600993ab2957111c105b6ded21d;p=helm.git diff --git a/matitaB/matita/matitaAuthentication.ml b/matitaB/matita/matitaAuthentication.ml index 1b9a91b71..a3704ee4e 100644 --- a/matitaB/matita/matitaAuthentication.ml +++ b/matitaB/matita/matitaAuthentication.ml @@ -25,54 +25,68 @@ type session_id = Uuidm.t -(* user table: user id, (password, optional session id) *) -type user = string * (string * session_id option) +type matita_file = MatitaFilesystem.matita_flag * string + +(* user table: user id, (salt, encrypted password, optional session id) *) +type user = string * (string * string * session_id option) let user_tbl = (ref [] : user list ref) -(* session table: (user id, session id), matita status *) -type session = session_id * (MatitaEngine.status * MatitaEngine.status list) +(* session table: session id, (user id, matita status, matita history *) +type session = session_id * (string * MatitaEngine.status * MatitaEngine.status list) let session_tbl = (ref [] : session list ref) exception UsernameCollision of string +exception InvalidPassword let lookup_user uid = List.assoc uid !user_tbl +let user_of_session sid = let res,_,_ = List.assoc sid !session_tbl in res + +let get_users () = List.map fst !user_tbl + let create_session uid = let status = new MatitaEngine.status (Some uid) "cic:/matita" in let history = [status] in - let pw,sid = List.assoc uid !user_tbl in + let salt,pw,sid = List.assoc uid !user_tbl in let clean_utbl = List.remove_assoc uid !user_tbl in let new_session = Uuidm.create `V4 in - user_tbl := (uid,(pw,Some new_session))::clean_utbl; + user_tbl := (uid,(salt,pw,Some new_session))::clean_utbl; let clean_stbl = match sid with | Some sid' -> List.remove_assoc sid' !session_tbl | _ -> !session_tbl in - session_tbl := (new_session,(status,history))::clean_stbl; + session_tbl := (new_session,(uid,status,history))::clean_stbl; new_session ;; -let get_status sid = fst (List.assoc sid !session_tbl) +let get_session_owner sid = + let uid,_,_ = List.assoc sid !session_tbl + in uid -let get_history sid = snd (List.assoc sid !session_tbl) +let get_status sid = + let _,st,_ = List.assoc sid !session_tbl + in st + +let get_history sid = + let _,_,hist = List.assoc sid !session_tbl + in hist let set_status sid st = - let oldst, hist = List.assoc sid !session_tbl in - session_tbl := (sid,(st,hist))::(List.remove_assoc sid !session_tbl) + let uid, oldst, hist = List.assoc sid !session_tbl in + session_tbl := (sid,(uid,st,hist))::(List.remove_assoc sid !session_tbl) let set_history sid hist = - let st, oldhist = List.assoc sid !session_tbl in - session_tbl := (sid,(st,hist))::(List.remove_assoc sid !session_tbl) - -let logout_user uid = - match List.assoc uid !user_tbl with - | _,None -> () - | pw, Some sid -> - user_tbl := (uid,(pw,None))::List.remove_assoc uid !user_tbl; - session_tbl := List.remove_assoc sid !session_tbl + let uid, st, oldhist = List.assoc sid !session_tbl in + session_tbl := (sid,(uid,st,hist))::(List.remove_assoc sid !session_tbl) + +let logout_user sid = + let uid,st,hist = List.assoc sid !session_tbl in + let salt,pw,_ = List.assoc uid !user_tbl in + user_tbl := (uid,(salt,pw,None))::List.remove_assoc uid !user_tbl; + session_tbl := List.remove_assoc sid !session_tbl ;; let remove_user uid = @@ -88,25 +102,102 @@ let config_path () = ;; let serialize () = - let clean_utbl = List.map (fun (uid,(pw,_)) -> uid,(pw,None)) !user_tbl in + let clean_utbl = List.map (fun (uid,(salt,pw,_)) -> uid,(salt,pw,None)) !user_tbl in let utbl_ch = open_out (config_path () ^ "/usertable.dump") in Marshal.to_channel utbl_ch clean_utbl []; close_out utbl_ch; ;; let deserialize () = - let utbl_ch = open_in (config_path () ^ "/usertable.dump") in - user_tbl := Marshal.from_channel utbl_ch; - close_in utbl_ch; + (try + let utbl_ch = open_in (config_path () ^ "/usertable.dump") in + user_tbl := Marshal.from_channel utbl_ch; + close_in utbl_ch; + with + | Sys_error _ -> + user_tbl := []; serialize()); (* old_sessions are now invalid *) session_tbl := []; ;; +let write_ft uid ft = + let ft_ch = open_out (config_path () ^ "/ft_" ^ uid ^ ".dump") in + Marshal.to_channel ft_ch ft []; + close_out ft_ch; +;; + +let read_ft uid = + try + let ft_ch = open_in (config_path () ^ "/ft_" ^ uid ^ ".dump") in + let ft = Marshal.from_channel ft_ch in + close_in ft_ch; + ft + with + | Sys_error _ -> + (* this is an error, we should rebuild the table by a diff of + the directory listing and svn stat *) + [] +;; + +let set_file_flag uid files_flags = + let ft = read_ft uid in + let files = List.map fst files_flags in + let ft = List.filter (fun (x,_) -> not (List.mem x files)) ft in + let ft' = List.fold_left (fun acc (filename,flag) -> + let filename = MatitaFilesystem.normalize_qfn filename in + try + (filename,HExtlib.unopt flag)::acc + with Failure _ -> acc) [] files_flags + in + write_ft uid (ft'@ft) +;; + let add_user uid pw = try let _ = lookup_user uid in raise (UsernameCollision uid) with Not_found -> - user_tbl := (uid,(pw,None))::!user_tbl; + let ft = MatitaFilesystem.checkout uid in + (* use a 8 byte salt *) + let salt = Cryptokit.Random.string Cryptokit.Random.secure_rng 8 in + let sha256 = Cryptokit.Hash.sha256 () in + sha256#add_string (salt ^ pw); + let crypto_pw = sha256#result in + user_tbl := (uid,(salt,crypto_pw,None))::!user_tbl; + write_ft uid ft; + serialize () +;; + +let add_user_no_checkout uid pw = + try + let _ = lookup_user uid in + raise (UsernameCollision uid) + with Not_found -> + (* use a 8 byte salt *) + let salt = Cryptokit.Random.string Cryptokit.Random.secure_rng 8 in + let sha256 = Cryptokit.Hash.sha256 () in + sha256#add_string (salt ^ pw); + let crypto_pw = sha256#result in + sha256#wipe; + user_tbl := (uid,(salt,crypto_pw,None))::!user_tbl; serialize () ;; + +let check_pw uid pw = + try + let salt,crypto_pw,_ = lookup_user uid in + let sha256 = Cryptokit.Hash.sha256 () in + sha256#add_string (salt ^ pw); + let computed_pw = sha256#result in + sha256#wipe; + if crypto_pw <> computed_pw + then (prerr_endline ("password " ^ pw ^ " incorrect"); raise InvalidPassword) + with Not_found _ -> raise InvalidPassword +;; + +let reset () = + user_tbl := []; + session_tbl := []; + MatitaFilesystem.reset_lib (); + serialize (); +;;