]> matita.cs.unibo.it Git - helm.git/blob - matitaB/matita/matitaAuthentication.ml
Matitaweb: secure SHA-256 encryption for passwords.
[helm.git] / matitaB / matita / matitaAuthentication.ml
1 (* Copyright (C) 2004-2011, HELM Team.
2  * 
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.
6  * 
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.
11  * 
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.
16  *
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,
20  * MA  02111-1307, USA.
21  * 
22  * For details, see the HELM World-Wide-Web page,
23  * http://helm.cs.unibo.it/
24  *)
25
26 type session_id = Uuidm.t
27
28 type matita_file = MatitaFilesystem.matita_flag * string
29
30 (* user table: user id, (salt, encrypted password, optional session id) *)
31 type user = string * (string * string * session_id option)
32
33 let user_tbl = (ref [] : user list ref)
34
35 (* session table: session id, (user id, matita status, matita history *)
36 type session = session_id * (string * MatitaEngine.status * MatitaEngine.status list)
37
38 let session_tbl = (ref [] : session list ref)
39
40 exception UsernameCollision of string
41 exception InvalidPassword
42
43 let lookup_user uid = List.assoc uid !user_tbl
44
45 let user_of_session sid = let res,_,_ = List.assoc sid !session_tbl in res
46
47 let get_users () = List.map fst !user_tbl
48
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
57     | Some sid' -> 
58        List.remove_assoc sid' !session_tbl
59     | _ -> !session_tbl
60   in 
61   session_tbl := (new_session,(uid,status,history))::clean_stbl;
62   new_session
63 ;;
64
65 let get_session_owner sid =
66   let uid,_,_ = List.assoc sid !session_tbl
67   in uid
68
69 let get_status sid =
70   let _,st,_ = List.assoc sid !session_tbl
71   in st
72
73 let get_history sid = 
74   let _,_,hist = List.assoc sid !session_tbl
75   in hist
76
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)
80    
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)
84
85 let logout_user sid =
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
90 ;;
91
92 let remove_user uid =
93   user_tbl := List.remove_assoc uid !user_tbl
94 ;;
95
96 (* serialization and deserialization of the user table *)
97 let config_path () =
98   let path = Helm_registry.get "matita.basedir" in
99   let dirname = Filename.dirname path in
100   HExtlib.mkdir dirname;
101   path
102 ;;
103
104 let serialize () =
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 [];
108   close_out utbl_ch;
109 ;;
110
111 let deserialize () =
112   (try
113     let utbl_ch = open_in (config_path () ^ "/usertable.dump") in
114     user_tbl := Marshal.from_channel utbl_ch;
115     close_in utbl_ch;
116   with
117     | Sys_error _ -> 
118        user_tbl := []; serialize());
119   (* old_sessions are now invalid *)
120   session_tbl := [];
121 ;;
122
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 [];
126   close_out ft_ch;
127 ;;
128
129 let read_ft uid =
130   try
131     let ft_ch = open_in (config_path () ^ "/ft_" ^ uid ^ ".dump") in
132     let ft = Marshal.from_channel ft_ch in
133     close_in ft_ch;
134     ft
135   with
136     | Sys_error _ -> 
137       (* this is an error, we should rebuild the table by a diff of
138          the directory listing and svn stat *) 
139       [] 
140 ;;
141
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 
148       try
149         (filename,HExtlib.unopt flag)::acc
150       with Failure _ -> acc) [] files_flags
151   in
152   write_ft uid (ft'@ft)
153 ;;
154
155 let add_user uid pw =
156   try
157     let _ = lookup_user uid in
158     raise (UsernameCollision uid)
159   with Not_found -> 
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;
167     write_ft uid ft;
168     serialize ()
169 ;;
170
171 let add_user_no_checkout uid pw =
172   try
173     let _ = lookup_user uid in
174     raise (UsernameCollision uid)
175   with Not_found -> 
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
181     sha256#wipe;
182     user_tbl := (uid,(salt,crypto_pw,None))::!user_tbl;
183     serialize ()
184 ;;
185
186 let check_pw uid pw =
187   try
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
192     sha256#wipe;
193     if crypto_pw <> computed_pw 
194       then (prerr_endline ("password " ^ pw ^ " incorrect"); raise InvalidPassword)
195   with Not_found _ -> raise InvalidPassword
196 ;;
197
198 let reset () =
199   user_tbl := [];
200   session_tbl := [];
201   MatitaFilesystem.reset_lib ();
202   serialize ();
203 ;;