03eed09c5b05831aeedd381143369b67f31922fb
[cocanwiki.git] / scripts / admin / edit_hostnames.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: edit_hostnames.ml,v 1.9 2006/03/27 18:09:47 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 split_re = Pcre.regexp "[\\s,;]+"
32
33 let run r (q : cgi) dbh _ host' _ =
34   let hostid = int_of_string (q#param "hostid") in
35
36   if q#param_true "cancel" then (
37     let { hostname = hostname } = host' in
38     q#redirect ("http://" ^ hostname ^ "/_bin/admin/host.cmo?hostid=" ^
39                 string_of_int hostid);
40     return ()
41   );
42
43   let canonical_hostname = q#param "canonical_hostname" in
44   let hostnames = try q#param "hostnames" with Not_found -> "" in
45
46   (* In theory we could verify characters in hostnames, and call
47    * error / return () if the format is incorrect.  However
48    * it's probably best to assume the sysadmin knows what they're up to
49    * here.  If this script is allowed to be accessed by untrusted users,
50    * then this has security implications.
51    *)
52   let check_hostname h =
53     let h = trim h in                   (* Trim whitespace. *)
54     let h = String.lowercase h in       (* Lowercase. *)
55     h
56   in
57
58   let canonical_hostname = check_hostname canonical_hostname in
59   let hostnames = Pcre.split ~rex:split_re hostnames in
60   let hostnames = List.map check_hostname hostnames in
61   let hostnames = List.filter ((<>) "") hostnames in
62
63   (* Update the database. *)
64   let sth = dbh#prepare_cached
65               "set constraints \"hosts_hostname_cn\" deferred" in
66   sth#execute [];
67   let sth = dbh#prepare_cached "update hosts set canonical_hostname = ?
68                                  where id = ?" in
69   sth#execute [`String canonical_hostname; `Int hostid];
70   let sth = dbh#prepare_cached "delete from hostnames where hostid = ?" in
71   sth#execute [`Int hostid];
72   let sth = dbh#prepare_cached "insert into hostnames (hostid, name)
73                                 values (?, ?)" in
74   sth#execute [`Int hostid; `String canonical_hostname];
75   List.iter (fun name ->
76                sth#execute [`Int hostid; `String name]) hostnames;
77
78   (* Commit to the database. *)
79   PGOCaml.commit dbh;
80
81   (* Print confirmation page. *)
82   let buttons = [
83     { Template.StdPages.label = "OK";
84       Template.StdPages.link = "/_bin/admin/host.cmo";
85       Template.StdPages.method_ = None;
86       Template.StdPages.params = [ "hostid", string_of_int hostid ] }
87   ] in
88
89   ok ~title:"Saved" ~buttons
90     dbh (-1) q "Hostnames updated."
91
92 let () =
93   register_script run