summaryrefslogtreecommitdiff
path: root/scripts/vyatta-dhcpv6-client.pl
blob: 3f01a67757e31dcba5c0bfbdea9b184ef5100df5 (plain)
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#!/usr/bin/perl
#
# Module: vyatta-dhcpv6-client.pl
#
# **** License ****
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.
#
# 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
# General Public License for more details.
#
# This code was originally developed by Vyatta, Inc.
# Portions created by Vyatta are Copyright (C) 2005-2009 Vyatta, Inc.
# All Rights Reserved.
#
# Author: Bob Gilligan <gilligan@vyatta.com>
# Date: April 2010
# Description: Start and stop DHCPv6 client daemon for an interface.
#
# **** End License ****
#
#

use strict;
use lib "/opt/vyatta/share/perl5/";
use FileHandle;
use Vyatta::Config;
use Getopt::Long;

my $start_flag;		# Start the daemon
my $stop_flag;		# Stop the daemon and delete all config files
my $release_flag;	# Stop the daemon, but leave config file
my $renew_flag;		# Re-start the daemon.  Functionally same as start_flag
my $temp_flag;
my $params_only_flag;
my $ifname;


sub gen_conf_file {
    my ($conffile, $ifname) = @_;

    my $FD_WR = new FileHandle;

    if (!open($FD_WR, ">$conffile")) {
	printf("Can't write config file: $conffile\n");
	exit 1;
    }
    my $date = `date`;
    my $user = `id -un`;
    my $hostname = `hostname`;
    chomp($date);
    chomp($user);
    chomp($hostname);

    print $FD_WR "# This file was auto-generated by the Vyatta\n";
    print $FD_WR "# configuration sub-system.  Do not edit it.\n";
    print $FD_WR "\n";
    print $FD_WR "#   Generated on $date by $user\n";
    print $FD_WR "#\n";
    print $FD_WR "interface \"$ifname\" {\n";
#    print $FD_WR "        send host-name \"$hostname\";\n";
#    print $FD_WR "        send dhcp6.oro 1, 2, 7, 12, 13, 23, 24, 39;\n";
    print $FD_WR "}\n";
}
    

#
# Main Section
#

GetOptions("start" => \$start_flag,
	   "stop" => \$stop_flag,
	   "release" => \$release_flag,
	   "renew" => \$renew_flag,
	   "temporary" => \$temp_flag,
	   "parameters-only" => \$params_only_flag,
	   "ifname=s" => \$ifname,
    );

if ((defined $temp_flag) && (defined $params_only_flag)) {
    printf("Error: --temporary and --parameters-only flags are mutually exclusive.\n");
    exit 1;
}

if (!defined $ifname) {
    printf("Error: Interface name must be specified with --ifname parameter.\n");
    exit 1;
}

my $pidfile = "/var/lib/dhcp3/dhclient_v6_$ifname.pid";
my $leasefile = "/var/lib/dhcp3/dhclient_v6_$ifname.leases";
my $conffile = "/var/lib/dhcp3/dhclient_v6_$ifname.conf";
my $cmdname = "/sbin/dhclient";

if (defined $release_flag) {
    if (! -e $conffile) {
	printf("DHCPv6 client is not configured on interface $ifname.\n");
	exit 1;
    }

    if (! -e $pidfile) {
	printf("DHCPv6 client is already released on interface $ifname.\n");
	exit 1;
    }
}

if (defined $renew_flag) {
    if (! -e $conffile) {
	printf("DHCPv6 client is not configured on interface $ifname.\n");
	exit 1;
    }
}

if (defined $stop_flag || defined $release_flag) {
    # Stop dhclient -6 on $ifname

    printf("Stopping daemon...\n");
    my $output=`$cmdname -6 -nw -cf $conffile -pf $pidfile -lf $leasefile -x $ifname`;
    printf($output);
    
    # Delete files it leaves behind...
    printf("Deleting related files...\n");
    unlink($pidfile);
    if (defined $stop_flag) {
	# If just releasing, leave the config file around as a flag that
	# DHCPv6 remains configured on this interface.
	unlink($conffile);
    }
}

if (defined $start_flag || defined $renew_flag) {
    # Generate the DHCP client config file...
    gen_conf_file($conffile, $ifname);

    # First, kill any previous instance of dhclient running on this interface
    #
    printf("Stopping old daemon...\n");
    my $output = `$cmdname -6 -pf $pidfile -x $ifname`;
    printf($output);

    # start "dhclient -6" on $ifname

    my $args = "";
    if (defined $temp_flag) {
	$args .= " -T";
    }
    if (defined $params_only_flag) {
	$args .= " -S";
    }

    printf("Starting new daemon...\n");
    my $output=`$cmdname -6 -nw -cf $conffile -pf $pidfile -lf $leasefile $args $ifname`;
    printf($output);
}