3 * Stefano Zacchiroli <zack@cs.unibo.it>
4 * for the HELM Team http://helm.cs.unibo.it/
6 * This file is part of HELM, an Hypertextual, Electronic
7 * Library of Mathematics, developed at the Computer Science
8 * Department, University of Bologna, Italy.
10 * HELM is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
15 * HELM is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with HELM; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
25 * For details, see the HELM World-Wide-Web page,
26 * http://helm.cs.unibo.it/
31 let (profile_db : (string, Profile.t) Hashtbl.t) =
33 let ch = open_in Profile_manager_env.profile_db_path in
36 _ -> Hashtbl.create 211
38 let get_param_opt (req : Http_types.request) p =
40 match req#param p with
44 Http_types.Param_not_found _ -> None
48 if n >= 0 && n < 10 then
50 else if n >= 10 && n < 16 then
51 String.make 1 (Char.chr (n - 10 + (Char.code 'A')))
58 | n -> aux ((hex_of_nibble (Random.int 16)) ^ s) (n - 1)
64 let key = new_key () in
65 if Hashtbl.mem profile_db key then
72 let list_profiles () =
75 (fun _ profile -> pl := (Profile.serialize profile false) :: !pl)
77 "<profiles>" ^ (List.fold_left (^) "" !pl) ^ "</profiles>"
79 let get_profile id pwd =
80 let profile = Hashtbl.find profile_db id in
81 Profile.get profile pwd
83 let new_profile id pwd =
85 Some id' when Hashtbl.mem profile_db id' -> get_profile id' pwd
87 let profile = Profile.create id' pwd in
88 Hashtbl.add profile_db profile.Profile.id profile ;
89 Profile.serialize profile true
91 let profile = Profile.create (new_unique_key ()) None in
92 Hashtbl.add profile_db profile.Profile.id profile ;
93 Profile.serialize profile true
95 let set_profile id field value pwd =
96 let profile = Hashtbl.find profile_db id in
97 Profile.set profile field value pwd
99 let del_profile id pwd =
100 let profile = Hashtbl.find profile_db id in
101 let res = Profile.del profile pwd in
102 Hashtbl.remove profile_db id ;
105 let set_password id new_pwd pwd =
106 let profile = Hashtbl.find profile_db id in
107 Profile.set_password profile new_pwd pwd
109 let set_access id read_access write_access profile_access pwd =
110 let profile = Hashtbl.find profile_db id in
111 Profile.set_access profile read_access write_access profile_access pwd
113 let callback (req : Http_types.request) outchan =
116 prerr_endline ("Connection from " ^ req#clientAddr) ;
117 prerr_endline ("Received request: " ^ req#path) ;
120 "/list" -> list_profiles ()
122 let id = req#param "id"
123 and pwd = get_param_opt req "password" in
126 let id = req#param "id"
127 and field = req#param "field"
128 and value = get_param_opt req "value"
129 and pwd = get_param_opt req "password" in
130 set_profile id field value pwd
132 let id = req#param "id"
133 and pwd = get_param_opt req "password" in
136 let id = get_param_opt req "id"
137 and pwd = get_param_opt req "password" in
140 let id = req#param "id"
141 and old_pwd = get_param_opt req "old_password"
142 and new_pwd = get_param_opt req "new_password" in
143 set_password id new_pwd old_pwd
145 let id = req#param "id"
146 and pwd = get_param_opt req "password"
147 and read_access = get_param_opt req "read"
148 and write_access = get_param_opt req "write"
149 and profile_access = get_param_opt req "profile" in
150 set_access id read_access write_access profile_access pwd
151 | s -> Response.error ("unrecognized command: " ^ s)
154 Not_found -> Response.error "unknown profile"
155 | Profile.Permission_denied -> Response.error "permission denied"
156 | e -> Response.error ("uncaught exception: " ^ (Printexc.to_string e))
159 ~body:res ~headers:["Content-Type", "text/xml"] outchan
161 (* daemon initialization *)
164 let ch = open_out Profile_manager_env.profile_db_path in
165 output_value ch profile_db ;
170 Sys.catch_break true ;
172 Http_daemon.start' ~timeout:(Some 600) ~port:58099 ~mode:`Thread callback