-
Notifications
You must be signed in to change notification settings - Fork 8
/
hda-alias
executable file
·124 lines (97 loc) · 2.56 KB
/
hda-alias
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#!/usr/bin/perl -w
#
# Amahi Home Server
# Copyright (C) 2007-2009 Amahi Team
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License v3
# (29 June 2007), as published in the COPYING file.
#
# 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
# file COPYING for more details.
#
# You should have received a copy of the GNU General Public
# License along with this program; if not, write to the Amahi
# team at http://www.amahi.org/ under "Contact Us."
use strict;
use DBI();
use Getopt::Std;
use LWP::Simple;
my $version = "1.48";
my $DATABASE_NAME = "hda_production";
my $DATABASE_USER = "amahihda";
my $DATABASE_PASSWORD = "AmahiHDARulez";
our($opt_h, $opt_D);
getopts('hD');
# global database handle for both functions below
my $dbh;
my %settings;
sub db_connect {
my $password = $DATABASE_PASSWORD ; # &get_password();
# connect to the database.
$dbh = DBI->connect("DBI:mysql:database=$DATABASE_NAME;host=localhost",
$DATABASE_USER, $password, {RaiseError => 1})
or die $DBI::errstr;
}
sub get_db_settings {
my $sth = $dbh->prepare("SELECT Name, Value FROM settings");
$sth->execute();
my @row = ();
while (@row = $sth->fetchrow_array) {
my $name = $row[0];
my $value = $row[1];
$settings {$name} = $value;
if ($opt_D) { printf "name = %s, value = %s\n", $name, $value; }
}
}
sub do_hda_ctl_hup {
system("hda-ctl-hup");
}
sub do_add {
my $alias = shift;
&db_connect ();
my $sth = $dbh->prepare("SELECT name FROM dns_aliases where name = '$alias'");
$sth->execute();
my @row = ();
if (@row = $sth->fetchrow_array) {
# alias exists already
# FIXME - warning?
return 0;
}
# else - need to put it in
$sth = $dbh->prepare("INSERT INTO dns_aliases (name, address) values ('$alias', '')");
$sth->execute();
# FIXME - check for errors.
&do_hda_ctl_hup();
}
sub do_remove {
my $alias = shift;
&db_connect ();
my $sth = $dbh->prepare("DELETE FROM dns_aliases where name = '$alias'");
$sth->execute();
&do_hda_ctl_hup();
}
sub usage {
printf "usage: hda-alias (add|rm) <alias>\n";
exit -1;
}
sub version {
printf "hda-alias version $version\n";
exit (0);
}
sub main {
&version() if (defined($opt_h));
&usage() unless ($#ARGV > 0 );
my $op = $ARGV[0];
my $alias = $ARGV[1];
if ($op eq "add") {
return &do_add($alias);
} elsif ($op eq "rm") {
return &do_remove($alias);
} else {
&usage();
}
}
&main ();