+csv dep for PG'OCaml.
[cocanwiki.git] / scripts / forgot_password.ml
1 (* COCANWIKI - a wiki written in Objective CAML.
2  * Written by Richard W.M. Jones <rich@merjis.com>.
3  * Copyright (C) 2004 Merjis Ltd.
4  * $Id: forgot_password.ml,v 1.13 2006/12/06 09:46:57 rich Exp $
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; see the file COPYING.  If not, write to
18  * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  *)
21
22 open Apache
23 open Registry
24 open Cgi
25 open Printf
26
27 open Cocanwiki
28 open Cocanwiki_ok
29 open Cocanwiki_strings
30
31 let run r (q : cgi) dbh hostid { hostname = hostname } _ =
32   let name = trim (q#param "name") in
33
34   if name = "" then (
35     error ~back_button:true ~title:"No username or email address"
36       r dbh hostid q "You didn't give a username or email address";
37     return ()
38   );
39
40   (* Look it up in the database. *)
41   let rows = PGSQL(dbh)
42     "select email, name, password from users
43       where hostid = $hostid
44         and email is not null
45         and (lower (name) = lower ($name) or lower (email) = lower ($name))" in
46
47   let email, name, password =
48     match rows with
49     | [ Some email, name, password ] ->
50         email, name, password
51     | _ ->
52         (* Artificially limit the rate at which people can search the
53          * database for usernames.
54          *)
55         Unix.sleep 10;
56
57         error ~back_button:true ~title:"Nothing known"
58           r dbh hostid q
59           "Sorry, don't know anyone with that name or email address.";
60         return () in
61
62   (* Get the IP address of the user, if available. *)
63   let ip =
64     try Connection.remote_ip (Request.connection r) with Not_found -> "" in
65
66   let subject = "Password for " ^ hostname in
67
68   let body =
69     "Someone, possibly you, requested your password for " ^ hostname ^
70       ".\n\n" ^
71       "Username: " ^ name ^ "\n" ^
72       "Password: " ^ password ^ "\n" ^
73       "\n" ^
74       "IP address of request: " ^ ip ^ "\n" in
75
76   let content_type =
77     "text/plain", ["charset", Mimestring.mk_param "UTF-8"] in
78   let to_addrs = [ "", email ] in
79
80   let msg = Netsendmail.compose ~subject ~to_addrs ~content_type body in
81   Netsendmail.sendmail msg;
82
83   let buttons = [ ok_button "/_login" ] in
84   ok ~buttons ~title:"Password sent by email"
85     r dbh hostid q
86     ("Your password was sent by email.  If you don't receive the password " ^
87      "within an hour, please notify the site's administrator.")
88
89 let () =
90   register_script run