]> matita.cs.unibo.it Git - helm.git/blob - matitaB/matita/matitaAuthentication.ml
update in basic_2
[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 (* low users can't commit or update *)
36 let luser_tbl = (ref [] : user list ref)
37
38 (* session table: session id, (user id, matita status, matita history, commit privileges *)
39 type session = session_id * (string * MatitaEngine.status * MatitaEngine.status list * bool)
40
41 let session_tbl = (ref [] : session list ref)
42
43 exception UsernameCollision of string
44 exception InvalidPassword
45
46 (* returns user entry and privileges *)
47 let lookup_user uid = 
48   try List.assoc uid !user_tbl, true
49   with Not_found -> List.assoc uid !luser_tbl, false
50
51 let user_of_session sid = let res,_,_,_ = List.assoc sid !session_tbl in res
52
53 (* disable for debugging *)
54 let prerr_endline _ = ()
55
56 (* used for commits, so lusers are excluded *)
57 let get_users () = List.map fst !user_tbl
58
59 let create_session uid =
60   let status = new MatitaEngine.status (Some uid) "cic:/matita" in
61   let history = [status] in
62   let (salt,pw,sid),cp = lookup_user uid in
63   let new_session = Uuidm.create `V4 in
64   (if cp then
65     let clean_utbl = List.remove_assoc uid !user_tbl in
66     user_tbl := (uid,(salt,pw,Some new_session))::clean_utbl
67   else
68     let clean_lutbl = List.remove_assoc uid !luser_tbl in
69     luser_tbl := (uid,(salt,pw,Some new_session))::clean_lutbl);
70   let clean_stbl = match sid with
71     | Some sid' -> 
72        List.remove_assoc sid' !session_tbl
73     | _ -> !session_tbl
74   in 
75   session_tbl := (new_session,(uid,status,history,cp))::clean_stbl;
76   new_session
77 ;;
78
79 let get_session_owner sid =
80   let uid,_,_,_ = List.assoc sid !session_tbl
81   in uid
82
83 let get_status sid =
84   let _,st,_,_ = List.assoc sid !session_tbl
85   in st
86
87 let get_history sid = 
88   let _,_,hist,_ = List.assoc sid !session_tbl
89   in hist
90
91 let get_commit_priv sid =
92   let _,_,_,cp = List.assoc sid !session_tbl
93   in cp
94
95 let probe_commit_priv sid =
96   let _,_,_,cp = List.assoc sid !session_tbl in
97   if not cp then failwith "no commit privileges" 
98
99 let set_status sid st =
100   let uid, oldst, hist, cp = List.assoc sid !session_tbl in
101   session_tbl := (sid,(uid,st,hist,cp))::(List.remove_assoc sid !session_tbl)
102    
103 let set_history sid hist =
104   let uid, st, oldhist, cp = List.assoc sid !session_tbl in
105   session_tbl := (sid,(uid,st,hist,cp))::(List.remove_assoc sid !session_tbl)
106
107 let logout_user sid =
108   let uid,st,hist,cp = List.assoc sid !session_tbl in
109   (if cp then 
110     let salt,pw,_ = List.assoc uid !user_tbl in
111     user_tbl := (uid,(salt,pw,None))::List.remove_assoc uid !user_tbl
112   else
113     let salt,pw,_ = List.assoc uid !luser_tbl in
114     luser_tbl := (uid,(salt,pw,None))::List.remove_assoc uid !luser_tbl);
115   session_tbl := List.remove_assoc sid !session_tbl
116 ;;
117
118 let remove_user uid =
119   user_tbl := List.remove_assoc uid !user_tbl;
120   luser_tbl := List.remove_assoc uid !luser_tbl
121 ;;
122
123 (* serialization and deserialization of the user table *)
124 let config_path () =
125   let path = Helm_registry.get "matita.basedir" in
126   let dirname = Filename.dirname path in
127   HExtlib.mkdir dirname;
128   path
129 ;;
130
131 let serialize () =
132   let clean_utbl = List.map (fun (uid,(salt,pw,_)) -> uid,(salt,pw,None)) !user_tbl in
133   let clean_lutbl = List.map (fun (uid,(salt,pw,_)) -> uid,(salt,pw,None)) !luser_tbl in
134   let utbl_ch = open_out (config_path () ^ "/usertable.dump") in
135   Marshal.to_channel utbl_ch clean_utbl [];
136   close_out utbl_ch;
137   let lutbl_ch = open_out (config_path () ^ "/lusertable.dump") in
138   Marshal.to_channel lutbl_ch clean_lutbl [];
139   close_out lutbl_ch
140 ;;
141
142 let deserialize () =
143   (try
144     let utbl_ch = open_in (config_path () ^ "/usertable.dump") in
145     user_tbl := Marshal.from_channel utbl_ch;
146     close_in utbl_ch;
147   with
148     | Sys_error _ -> 
149        user_tbl := []; serialize());
150   (try
151     let lutbl_ch = open_in (config_path () ^ "/lusertable.dump") in
152     luser_tbl := Marshal.from_channel lutbl_ch;
153     close_in lutbl_ch;
154   with
155     | Sys_error _ -> 
156        luser_tbl := []; serialize());
157   (* old_sessions are now invalid *)
158   session_tbl := [];
159 ;;
160
161 let write_ft uid ft =
162   let ft_ch = open_out (config_path () ^ "/ft_" ^ uid ^ ".dump") in
163   Marshal.to_channel ft_ch ft [];
164   close_out ft_ch;
165 ;;
166
167 let read_ft uid =
168   try
169     let ft_ch = open_in (config_path () ^ "/ft_" ^ uid ^ ".dump") in
170     let ft = Marshal.from_channel ft_ch in
171     close_in ft_ch;
172     ft
173   with
174     | Sys_error _ -> 
175       (* this is an error, we should rebuild the table by a diff of
176          the directory listing and svn stat *) 
177       [] 
178 ;;
179
180 let set_file_flag uid files_flags =
181   let ft = read_ft uid in
182   let files = List.map fst files_flags in
183   let ft = List.filter (fun (x,_) -> not (List.mem x files)) ft in
184   let ft' = List.fold_left (fun acc (filename,flag) ->  
185       let filename = MatitaFilesystem.normalize_qfn filename in 
186       try
187         (filename,HExtlib.unopt flag)::acc
188       with Failure _ -> acc) [] files_flags
189   in
190   write_ft uid (ft'@ft)
191 ;;
192
193 let add_user uid pw cp =
194   try
195     let _ = lookup_user uid in
196     raise (UsernameCollision uid)
197   with Not_found -> 
198     let ft = MatitaFilesystem.checkout uid in
199     (* use a 8 byte salt *)
200     let salt = Cryptokit.Random.string Cryptokit.Random.secure_rng 8 in
201     let sha256 = Cryptokit.Hash.sha256 () in
202     sha256#add_string (salt ^ pw);
203     let crypto_pw = sha256#result in
204     (if cp then
205       user_tbl := (uid,(salt,crypto_pw,None))::!user_tbl
206     else 
207       luser_tbl := (uid,(salt,crypto_pw,None))::!luser_tbl);
208     write_ft uid ft;
209     serialize ()
210 ;;
211
212 let add_user_no_checkout uid pw cp =
213   try
214     let _ = lookup_user uid in
215     raise (UsernameCollision uid)
216   with Not_found -> 
217     (* use a 8 byte salt *)
218     let salt = Cryptokit.Random.string Cryptokit.Random.secure_rng 8 in
219     let sha256 = Cryptokit.Hash.sha256 () in
220     sha256#add_string (salt ^ pw);
221     let crypto_pw = sha256#result in
222     sha256#wipe;
223     (if cp then
224       user_tbl := (uid,(salt,crypto_pw,None))::!user_tbl
225     else 
226       luser_tbl := (uid,(salt,crypto_pw,None))::!luser_tbl);
227     serialize ()
228 ;;
229
230 let check_pw uid pw =
231   try
232     let (salt,crypto_pw,_),_ = lookup_user uid in
233     let sha256 = Cryptokit.Hash.sha256 () in
234     sha256#add_string (salt ^ pw);
235     let computed_pw = sha256#result in
236     sha256#wipe;
237     if crypto_pw <> computed_pw 
238       then (prerr_endline ("password " ^ pw ^ " incorrect"); raise InvalidPassword)
239   with Not_found _ -> raise InvalidPassword
240 ;;
241
242 let reset () =
243   user_tbl := [];
244   luser_tbl := [];
245   session_tbl := [];
246   MatitaFilesystem.reset_lib ();
247   serialize ();
248 ;;