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