1 (* Copyright (C) 2004-2011, HELM Team.
3 * This file is part of HELM, an Hypertextual, Electronic
4 * Library of Mathematics, developed at the Computer Science
5 * Department, University of Bologna, Italy.
7 * HELM is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
12 * HELM is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with HELM; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
22 * For details, see the HELM World-Wide-Web page,
23 * http://helm.cs.unibo.it/
26 type session_id = Uuidm.t
28 type matita_file = MatitaFilesystem.matita_flag * string
30 (* user table: user id, (salt, encrypted password, optional session id) *)
31 type user = string * (string * string * session_id option)
33 let user_tbl = (ref [] : user list ref)
35 (* session table: session id, (user id, matita status, matita history *)
36 type session = session_id * (string * MatitaEngine.status * MatitaEngine.status list)
38 let session_tbl = (ref [] : session list ref)
40 exception UsernameCollision of string
41 exception InvalidPassword
43 let lookup_user uid = List.assoc uid !user_tbl
45 let user_of_session sid = let res,_,_ = List.assoc sid !session_tbl in res
47 let get_users () = List.map fst !user_tbl
49 let create_session uid =
50 let status = new MatitaEngine.status (Some uid) "cic:/matita" in
51 let history = [status] in
52 let salt,pw,sid = List.assoc uid !user_tbl in
53 let clean_utbl = List.remove_assoc uid !user_tbl in
54 let new_session = Uuidm.create `V4 in
55 user_tbl := (uid,(salt,pw,Some new_session))::clean_utbl;
56 let clean_stbl = match sid with
58 List.remove_assoc sid' !session_tbl
61 session_tbl := (new_session,(uid,status,history))::clean_stbl;
65 let get_session_owner sid =
66 let uid,_,_ = List.assoc sid !session_tbl
70 let _,st,_ = List.assoc sid !session_tbl
74 let _,_,hist = List.assoc sid !session_tbl
77 let set_status sid st =
78 let uid, oldst, hist = List.assoc sid !session_tbl in
79 session_tbl := (sid,(uid,st,hist))::(List.remove_assoc sid !session_tbl)
81 let set_history sid hist =
82 let uid, st, oldhist = List.assoc sid !session_tbl in
83 session_tbl := (sid,(uid,st,hist))::(List.remove_assoc sid !session_tbl)
86 let uid,st,hist = List.assoc sid !session_tbl in
87 let salt,pw,_ = List.assoc uid !user_tbl in
88 user_tbl := (uid,(salt,pw,None))::List.remove_assoc uid !user_tbl;
89 session_tbl := List.remove_assoc sid !session_tbl
93 user_tbl := List.remove_assoc uid !user_tbl
96 (* serialization and deserialization of the user table *)
98 let path = Helm_registry.get "matita.basedir" in
99 let dirname = Filename.dirname path in
100 HExtlib.mkdir dirname;
105 let clean_utbl = List.map (fun (uid,(salt,pw,_)) -> uid,(salt,pw,None)) !user_tbl in
106 let utbl_ch = open_out (config_path () ^ "/usertable.dump") in
107 Marshal.to_channel utbl_ch clean_utbl [];
113 let utbl_ch = open_in (config_path () ^ "/usertable.dump") in
114 user_tbl := Marshal.from_channel utbl_ch;
118 user_tbl := []; serialize());
119 (* old_sessions are now invalid *)
123 let write_ft uid ft =
124 let ft_ch = open_out (config_path () ^ "/ft_" ^ uid ^ ".dump") in
125 Marshal.to_channel ft_ch ft [];
131 let ft_ch = open_in (config_path () ^ "/ft_" ^ uid ^ ".dump") in
132 let ft = Marshal.from_channel ft_ch in
137 (* this is an error, we should rebuild the table by a diff of
138 the directory listing and svn stat *)
142 let set_file_flag uid files_flags =
143 let ft = read_ft uid in
144 let files = List.map fst files_flags in
145 let ft = List.filter (fun (x,_) -> not (List.mem x files)) ft in
146 let ft' = List.fold_left (fun acc (filename,flag) ->
147 let filename = MatitaFilesystem.normalize_qfn filename in
149 (filename,HExtlib.unopt flag)::acc
150 with Failure _ -> acc) [] files_flags
152 write_ft uid (ft'@ft)
155 let add_user uid pw =
157 let _ = lookup_user uid in
158 raise (UsernameCollision uid)
160 let ft = MatitaFilesystem.checkout uid in
161 (* use a 8 byte salt *)
162 let salt = Cryptokit.Random.string Cryptokit.Random.secure_rng 8 in
163 let sha256 = Cryptokit.Hash.sha256 () in
164 sha256#add_string (salt ^ pw);
165 let crypto_pw = sha256#result in
166 user_tbl := (uid,(salt,crypto_pw,None))::!user_tbl;
171 let add_user_no_checkout uid pw =
173 let _ = lookup_user uid in
174 raise (UsernameCollision uid)
176 (* use a 8 byte salt *)
177 let salt = Cryptokit.Random.string Cryptokit.Random.secure_rng 8 in
178 let sha256 = Cryptokit.Hash.sha256 () in
179 sha256#add_string (salt ^ pw);
180 let crypto_pw = sha256#result in
182 user_tbl := (uid,(salt,crypto_pw,None))::!user_tbl;
186 let check_pw uid pw =
188 let salt,crypto_pw,_ = lookup_user uid in
189 let sha256 = Cryptokit.Hash.sha256 () in
190 sha256#add_string (salt ^ pw);
191 let computed_pw = sha256#result in
193 if crypto_pw <> computed_pw
194 then (prerr_endline ("password " ^ pw ^ " incorrect"); raise InvalidPassword)
195 with Not_found _ -> raise InvalidPassword
201 MatitaFilesystem.reset_lib ();