1#!/usr/local/bin/perl
2##
3## @PACKAGE@ @VERSION@
4## Copyright (c) @COPYYEARS@ by Henry Kilmer and John Heasley
5## All rights reserved.
6##
7## This code is derived from software contributed to and maintained by
8## Henry Kilmer, John Heasley, Andrew Partan,
9## Pete Whiting, Austin Schutz, and Andrew Fort.
10##
11## Redistribution and use in source and binary forms, with or without
12## modification, are permitted provided that the following conditions
13## are met:
14## 1. Redistributions of source code must retain the above copyright
15##    notice, this list of conditions and the following disclaimer.
16## 2. Redistributions in binary form must reproduce the above copyright
17##    notice, this list of conditions and the following disclaimer in the
18##    documentation and/or other materials provided with the distribution.
19## 3. Neither the name of RANCID nor the names of its
20##    contributors may be used to endorse or promote products derived from
21##    this software without specific prior written permission.
22##
23## THIS SOFTWARE IS PROVIDED BY Henry Kilmer, John Heasley AND CONTRIBUTORS
24## ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
25## TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26## PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COMPANY OR CONTRIBUTORS
27## BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28## CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29## SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30## INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31## CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32## ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33## POSSIBILITY OF SUCH DAMAGE.
34##
35## It is the request of the authors, but not a condition of license, that
36## parties packaging or redistributing RANCID NOT distribute altered versions
37## of the etc/rancid.types.base file nor alter how this file is processed nor
38## when in relation to etc/rancid.types.conf.  The goal of this is to help
39## suppress our support costs.  If it becomes a problem, this could become a
40## condition of license.
41#
42#  The expect login scripts were based on Erik Sherk's gwtn, by permission.
43#
44#  The original looking glass software was written by Ed Kern, provided by
45#  permission and modified beyond recognition.
46#
47# copy configs from tftpboot within rancid; assumes CWD=<group>/configs and
48# that /bin/domainname is the same domainname as the routers, which is
49# replaced with "-confg" to form the filename expected in /tftpboot.
50#
51# usage: rtftpcopy [-dltCV] [-f filename | hostname]
52#
53use Getopt::Std;
54getopts('dflt:CV');
55if ($opt_V) {
56    print "@PACKAGE@ @VERSION@\n";
57    exit(0);
58}
59$log = $opt_l;
60$debug = $opt_d;
61$file = $opt_f;
62$host = $ARGV[0];
63$domain = system("/bin/domainname");
64$domain =~ s/[.]/\\./g;
65if ($file) {
66    $srcfile = $host;
67    $host =~ s/^.*\///;
68} else {
69    $srcfile = $host;
70    $srcfile =~ s/$domain$/-confg/;
71}
72
73if (length($host) == 0) {
74    if ($file) {
75	print(STDERR "Too few arguments: file name required\n");
76	exit(1);
77    } else {
78	print(STDERR "Too few arguments: host name required\n");
79	exit(1);
80    }
81}
82if ($opt_C) {
83    if ($file) {
84	print "cp $srcfile $host.new\n";
85    } else {
86	print "cp /tftpboot/$srcfile $host.new\n";
87    }
88    exit(0);
89}
90
91if ($file) {
92    print STDERR "copying file $host\n" if ($debug);
93    print STDOUT "copying file $host\n" if ($log);
94    system("/bin/cp $srcfile $host.new");
95} else {
96    print STDERR "copying file $host\n" if ($debug);
97    print STDOUT "copying file $host\n" if ($log);
98    system("/bin/cp /tftpboot/$srcfile $host.new") == 0;
99}
100if (!$?) {
101    print STDERR "copy failed: $!\n";
102}
103print STDOUT "Done cp: $_\n" if ($log);
104
105# check for completeness
106if (scalar(%commands) || !$clean_run || !$found_end) {
107    if (scalar(keys %commands) eq $commandcnt) {
108	printf(STDERR "$host: missed cmd(s): all commands\n");
109    } elsif (scalar(%commands)) {
110	printf(STDOUT "$host: missed cmd(s): %s\n", join(',', keys(%commands)));
111	printf(STDERR "$host: missed cmd(s): %s\n", join(',', keys(%commands))) if ($debug);
112    }
113    if (!$clean_run || !$found_end) {
114	print STDOUT "$host: End of run not found\n";
115	print STDERR "$host: End of run not found\n" if ($debug);
116	system("/usr/bin/tail -1 $host.new");
117    }
118    unlink "$host.new" if (! $debug);
119}
120