Une MAJ de sécurité est nécessaire sur notre version actuelle. Elle sera effectuée lundi 02/08 entre 12h30 et 13h. L'interruption de service devrait durer quelques minutes (probablement moins de 5 minutes).

web_site.ml 13.2 KB
Newer Older
Stephane Glondu's avatar
Stephane Glondu committed
1 2 3
(**************************************************************************)
(*                                BELENIOS                                *)
(*                                                                        *)
Stephane Glondu's avatar
Stephane Glondu committed
4
(*  Copyright © 2012-2014 Inria                                           *)
Stephane Glondu's avatar
Stephane Glondu committed
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
(*                                                                        *)
(*  This program is free software: you can redistribute it and/or modify  *)
(*  it under the terms of the GNU Affero General Public License as        *)
(*  published by the Free Software Foundation, either version 3 of the    *)
(*  License, or (at your option) any later version, with the additional   *)
(*  exemption that compiling, linking, and/or using OpenSSL is allowed.   *)
(*                                                                        *)
(*  This program is distributed in the hope that it will be useful, but   *)
(*  WITHOUT ANY WARRANTY; without even the implied warranty of            *)
(*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *)
(*  Affero General Public License for more details.                       *)
(*                                                                        *)
(*  You should have received a copy of the GNU Affero General Public      *)
(*  License along with this program.  If not, see                         *)
(*  <http://www.gnu.org/licenses/>.                                       *)
(**************************************************************************)

22
open Lwt
23
open Platform
24
open Serializable_j
25
open Signatures
26
open Common
Stephane Glondu's avatar
Stephane Glondu committed
27
open Web_serializable_j
28
open Web_common
29
open Web_signatures
Stephane Glondu's avatar
Stephane Glondu committed
30

31 32 33 34
module type CONFIG = sig
  val name : string
  val path : string list
  val source_file : string
35
  val spool_dir : string
36
  val auth_config : auth_config list
37
end
Stephane Glondu's avatar
Stephane Glondu committed
38

39 40 41 42
let rec list_remove x = function
  | [] -> []
  | y :: ys -> if x = y then ys else y :: (list_remove x ys)

43 44 45 46 47 48 49 50 51 52 53 54
let get_single_line x =
  match_lwt Lwt_stream.get x with
  | None -> return None
  | Some _ as line ->
    lwt b = Lwt_stream.is_empty x in
    if b then (
      return line
    ) else (
      Lwt_stream.junk_while (fun _ -> true) x >>
      return None
    )

55 56
let ( / ) = Filename.concat

57
module Make (C : CONFIG) : SITE = struct
58
  open Eliom_service
Stephane Glondu's avatar
Stephane Glondu committed
59 60
  open Eliom_registration

61 62 63 64 65
  module C = struct
    include C
    let kind = `Site
  end

66 67
  let make_path x = C.path @ x

68
  module Auth = Web_auth.Make (C)
69
  module LwtRandom = MakeLwtRandom (struct let rng = make_rng () end)
Stephane Glondu's avatar
Stephane Glondu committed
70

71 72 73 74 75 76 77 78 79 80 81 82 83
  let store = Ocsipersist.open_store C.name

  (* Persistent table, used to initialize the server. *)
  let election_ptable = Ocsipersist.open_table (C.name ^ "_elections")

  (* In-memory table, indexed by UUID, contains closures. *)
  let election_table = ref SMap.empty

  lwt main_election =
    Ocsipersist.make_persistent store "main_election" None

  lwt featured =
    Ocsipersist.make_persistent store "featured_elections" []
Stephane Glondu's avatar
Stephane Glondu committed
84 85 86 87

  (* The following reference is there to cut a dependency loop:
     S.register_election depends on S (via Templates). It will be set
     to a proper value once we have called Templates.Make. *)
88

89
  let import_election_ref = ref (fun _ -> assert false)
Stephane Glondu's avatar
Stephane Glondu committed
90 91 92 93 94

  (* We use an intermediate module S that will be passed to Templates
     and Web_election. S is not meant to leak and will be included
     in the returned module later. *)

95
  module S : SITE = struct
Stephane Glondu's avatar
Stephane Glondu committed
96
    include Auth.Services
97
    include Auth.Handlers
Stephane Glondu's avatar
Stephane Glondu committed
98 99
    open Eliom_parameter

Stephane Glondu's avatar
Stephane Glondu committed
100 101
    let scope = Eliom_common.default_session_scope

Stephane Glondu's avatar
Stephane Glondu committed
102
    let home = service
103
      ~path:(make_path [""])
Stephane Glondu's avatar
Stephane Glondu committed
104
      ~get_params:unit
105 106
      ()

107 108 109 110 111
    let admin = service
      ~path:(make_path ["admin"])
      ~get_params:unit
      ()

Stephane Glondu's avatar
Stephane Glondu committed
112
    let source_code = service
113
      ~path:(make_path ["belenios.tar.gz"])
Stephane Glondu's avatar
Stephane Glondu committed
114 115 116 117
      ~get_params:unit
      ()

    let get_randomness = service
118
      ~path:(make_path ["get-randomness"])
Stephane Glondu's avatar
Stephane Glondu committed
119 120 121
      ~get_params:unit
      ()

122 123 124 125 126 127 128 129 130 131 132 133
    let new_election = service
      ~path:(make_path ["new-election"])
      ~get_params:unit
      ()

    let new_election_post = post_service
      ~fallback:new_election
      ~post_params:(
        file "election" ** file "metadata"
        ** file "public_keys" ** file "public_creds"
      ) ()

134 135 136
    let tool =
      preapply (static_dir ()) ["tool"; "belenios-tool.html"]

Stephane Glondu's avatar
Stephane Glondu committed
137 138
    let cont = Eliom_reference.eref ~scope
      (fun () () -> Eliom_registration.Redirection.send home)
139

140
    let import_election f = !import_election_ref f
141

142 143 144 145 146 147 148 149 150 151 152 153 154 155
    let add_featured_election x =
      lwt the_featured = Ocsipersist.get featured in
      if List.mem x the_featured then (
        return ()
      ) else if SMap.mem x !election_table then (
        Ocsipersist.set featured (x :: the_featured)
      ) else (
        Lwt.fail Not_found
      )

    let remove_featured_election x =
      lwt the_featured = Ocsipersist.get featured in
      Ocsipersist.set featured (list_remove x the_featured)

156 157 158 159 160 161 162 163 164
    let set_main_election x =
      if SMap.mem x !election_table then (
        Ocsipersist.set main_election (Some x)
      ) else (
        Lwt.fail Not_found
      )

    let unset_main_election () =
      Ocsipersist.set main_election None
165

Stephane Glondu's avatar
Stephane Glondu committed
166 167
  end

Stephane Glondu's avatar
Stephane Glondu committed
168 169
  include S

170
  module T = Web_templates.Make (S)
Stephane Glondu's avatar
Stephane Glondu committed
171

172 173 174 175 176 177 178 179 180 181 182
  let register_election params web_params =
    let module P = (val params : ELECTION_PARAMS) in
    let uuid = Uuidm.to_string P.params.e_uuid in
    let module D = struct
      module G = P.G
      let election = {
        e_params = P.params;
        e_pks = None;
        e_fingerprint = P.fingerprint;
      }
    end in
183 184
    let module P = (val web_params : WEB_PARAMS) in
    let module R = Web_election.Make (D) (P) in
185
    (module R : Web_election.REGISTRABLE), fun () ->
186 187 188 189 190 191 192 193 194 195
      (* starting from here, we do side-effects on the running server *)
      let module R = R.Register (struct end) in
      let module W = R.W in
      let module X : EMPTY = R.Register (S) (T) in
      let election = (module W : WEB_ELECTION) in
      election_table := SMap.add uuid election !election_table;
      election

  (* Mutex to avoid simultaneous registrations of the same election *)
  let registration_mutex = Lwt_mutex.create ()
Stephane Glondu's avatar
Stephane Glondu committed
196

197
  let () = import_election_ref := fun f ->
198 199 200 201 202 203 204 205
    Lwt_mutex.lock registration_mutex >>
    try_lwt
      lwt raw_election =
        Lwt_io.lines_of_file f.f_election |>
        get_single_line >>=
        (function
        | Some e -> return e
        | None -> Printf.ksprintf
206
          failwith "election.json must contain a single line"
207
        )
208
      in
209 210 211 212 213 214 215 216
      let params = Group.election_params_of_string raw_election in
      let module P = (val params : ELECTION_PARAMS) in
      let uuid = Uuidm.to_string P.params.e_uuid in
      lwt exists =
        try_lwt
          lwt _ = Ocsipersist.find election_ptable uuid in
          return true
        with Not_found -> return false
217
      in
218
      if exists then (
219
        Lwt_mutex.unlock registration_mutex;
220 221 222 223 224 225 226
        return None
      ) else (
        let dir = C.spool_dir/uuid in
        lwt metadata =
          Lwt_io.chars_of_file f.f_metadata |>
          Lwt_stream.to_string >>=
          wrap1 metadata_of_string
227
        in
228 229 230 231 232
        let module X = struct
          let metadata = metadata
          let dir = dir
        end in
        let web_params = (module X : WEB_PARAMS) in
233 234 235
        let r, do_register = register_election params web_params in
        let module R = (val r : Web_election.REGISTRABLE) in
        let module G = R.W.G in
236
        let module KG = Election.MakeSimpleDistKeyGen (G) (LwtRandom) in
237
        let public_keys = Lwt_io.lines_of_file f.f_public_keys in
238 239 240 241 242 243 244 245 246
        lwt pks = Lwt_stream.(
          clone public_keys |>
          map (trustee_public_key_of_string G.read) |>
          to_list >>= wrap1 Array.of_list
        ) in
        if not (Array.forall KG.check pks) then
          failwith "Public keys are invalid.";
        if not G.(R.W.election.e_params.e_public_key =~ KG.combine pks) then
          failwith "Public keys mismatch with election public key.";
247 248 249 250 251 252 253 254 255
        let public_creds = Lwt_io.lines_of_file f.f_public_creds in
        lwt () = Lwt_stream.(
          clone public_creds |>
          iter_s (fun x ->
            if not G.(check @@ of_string x) then (
              Lwt.fail @@ Failure "Public credentials are invalid."
            ) else return ()
          )
        ) in
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
        let module R = struct
          let discard () = Lwt_mutex.unlock registration_mutex
          let register () =
            if not (Lwt_mutex.is_locked registration_mutex) then
              failwith "This election can no longer be registered.";
            try_lwt
              Lwt_unix.mkdir dir 0o700 >>
              Lwt_io.(with_file Output (dir/"election.json") (fun oc ->
                write_line oc raw_election
              )) >>
              Lwt_io.(with_file Output (dir/"public_keys.jsons") (fun oc ->
                write_lines oc public_keys
              )) >>
              let election = do_register () in
              let module W = (val election : WEB_ELECTION) in
              let () =
                Ocsigen_messages.debug (fun () ->
                  Printf.sprintf "Injecting credentials for %s" uuid
                )
              in
276
              public_creds |>
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
              Lwt_stream.iter_s W.B.inject_cred >>
              W.B.update_files () >>
              Ocsipersist.add election_ptable uuid (raw_election, web_params) >>
              let () = Lwt_mutex.unlock registration_mutex in
              return election
            with e ->
              Lwt_mutex.unlock registration_mutex;
              Lwt.fail e
        end in
        (* until here, no side-effects on the running server *)
        return @@ Some (module R : REGISTRABLE_ELECTION)
      )
    with e ->
      Lwt_mutex.unlock registration_mutex;
      Lwt.fail e
292 293 294 295

  lwt () =
    Ocsipersist.iter_step (fun uuid (raw_election, web_params) ->
      let params = Group.election_params_of_string raw_election in
296 297
      let _, do_register = register_election params web_params in
      let election = do_register () in
298 299 300 301 302 303 304 305
      let module W = (val election : WEB_ELECTION) in
      assert (uuid = Uuidm.to_string W.election.e_params.e_uuid);
      Ocsigen_messages.debug (fun () ->
        Printf.sprintf "Initialized election %s from persistent store" uuid
      );
      return ()
    ) election_ptable

306
  let () = let module X : EMPTY = Auth.Register (S) (T.Login (S)) in ()
Stephane Glondu's avatar
Stephane Glondu committed
307 308 309

  let () = Any.register ~service:home
    (fun () () ->
Stephane Glondu's avatar
Stephane Glondu committed
310
      Eliom_reference.unset cont >>
311
      match_lwt Ocsipersist.get main_election with
Stephane Glondu's avatar
Stephane Glondu committed
312
      | None ->
313 314 315 316 317 318 319 320 321 322
        lwt featured =
          Ocsipersist.get featured >>=
          Lwt_list.map_p (fun x ->
            let module W = (val SMap.find x !election_table : WEB_ELECTION) in
            return (module W : WEB_ELECTION_RO)
          )
        in
        T.home ~featured () >>= Html5.send
      | Some x ->
        let module W = (val SMap.find x !election_table : WEB_ELECTION) in
Stephane Glondu's avatar
Stephane Glondu committed
323 324 325
        Redirection.send W.S.home
    )

326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345
  let () = Html5.register ~service:admin
    (fun () () ->
      let cont () () = Redirection.send admin in
      Eliom_reference.set S.cont cont >>
      lwt elections =
        match_lwt get_user () with
        | None -> return []
        | Some u ->
          SMap.fold (fun _ w accu ->
            let module W = (val w : WEB_ELECTION) in
            if W.metadata.e_owner = Some u then (
              (module W : WEB_ELECTION_RO) :: accu
            ) else (
              accu
            )
          ) !election_table [] |> List.rev |> return
      in
      T.admin ~elections ()
    )

Stephane Glondu's avatar
Stephane Glondu committed
346 347 348
  let () = File.register
    ~service:source_code
    ~content_type:"application/x-gzip"
349
    (fun () () -> return C.source_file)
Stephane Glondu's avatar
Stephane Glondu committed
350

Stephane Glondu's avatar
Stephane Glondu committed
351 352
  let do_get_randomness =
    let prng = Lazy.lazy_from_fun (Lwt_preemptive.detach (fun () ->
353
      pseudo_rng (random_string secure_rng 16)
Stephane Glondu's avatar
Stephane Glondu committed
354 355 356 357 358
    )) in
    let mutex = Lwt_mutex.create () in
    fun () ->
      Lwt_mutex.with_lock mutex (fun () ->
        lwt prng = Lazy.force prng in
359
        return (random_string prng 32)
Stephane Glondu's avatar
Stephane Glondu committed
360 361
      )

Stephane Glondu's avatar
Stephane Glondu committed
362 363 364 365
  let () = String.register
    ~service:get_randomness
    (fun () () ->
      lwt r = do_get_randomness () in
366
      b64_encode_compact r |>
Stephane Glondu's avatar
Stephane Glondu committed
367 368 369
      (fun x -> string_of_randomness { randomness=x }) |>
      (fun x -> return (x, "application/json"))
    )
Stephane Glondu's avatar
Stephane Glondu committed
370

371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404
  let () = Html5.register ~service:new_election
    (fun () () ->
      match_lwt S.get_user () with
      | None -> forbidden ()
      | Some _ -> T.new_election ()
    )

  let () = Any.register ~service:new_election_post
    (fun () (election, (metadata, (public_keys, public_creds))) ->
      match_lwt S.get_user () with
      | Some u ->
        let open Ocsigen_extensions in
        let files = {
          f_election = election.tmp_filename;
          f_metadata = metadata.tmp_filename;
          f_public_keys = public_keys.tmp_filename;
          f_public_creds = public_creds.tmp_filename;
        } in
        begin try_lwt
          begin match_lwt S.import_election files with
          | None ->
            T.new_election_failure `Exists () >>= Html5.send
          | Some w ->
            let module W = (val w : REGISTRABLE_ELECTION) in
            lwt w = W.register () in
            let module W = (val w : WEB_ELECTION) in
            W.S.admin |> Redirection.send
          end
        with e ->
          T.new_election_failure (`Exception e) () >>= Html5.send
        end
      | None -> forbidden ()
    )

Stephane Glondu's avatar
Stephane Glondu committed
405
end