summaryrefslogtreecommitdiff
path: root/scripts/vyatta-image-tools.pl
blob: 0adc1f064219366a6b23d67fd11c9fd37438971a (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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
#!/usr/bin/perl -w
use Getopt::Long;
use lib "/opt/vyatta/share/perl5/";

use strict;

my ($show, $delete, $updateone);
my @copy;
my @update;

GetOptions("show=s"        => \$show,
           "delete=s"      => \$delete,
           "update=s{2}"   => \@update,
           "updateone=s"   => \$updateone,
           "copy=s{2}"     => \@copy);

if (defined $show){
  show($show);
}
if (defined $delete){
  delete_file($delete);
}
if (@update){
  update(@update);
}
if (defined($updateone)){
  update($updateone, "running://");
}
if (@copy){
  copy(@copy);
}

sub conv_file {
  my $file = " ";
  my $filein = pop(@_);
  $file = $filein;
  my $topdir;
  if ($file =~ /(.+?):\/\/(.*)/){
    $topdir = $1;
    $file = $2;
  } elsif ($file =~ /^\//) {
    $topdir = "running";
  } else {
    print "File: $filein not found \n";
    exit 1;
  }
  if ( $topdir eq "running" ) {
    $file = "/$file";
  } elsif ( lc($topdir) eq 'tftp') {
    $file = $filein;
    $topdir = 'url';
  } elsif ( lc($topdir) eq 'ftp') {
    $file = $filein;
    $topdir = 'url';
  } elsif ( lc($topdir) eq 'scp') {
    $file = $filein;
    $topdir = 'url';
  } else {
    $file = "/live/image/boot/$topdir/live-rw/$file";
  }
  return ($topdir, $file);
}

sub conv_file_to_rel {
  my ($topdir, $filename) = @_;
  if ($topdir eq "running"){
    $filename =~ s?/?$topdir://?;
  } else {
    $filename =~ s?/live/image/boot/$topdir/live-rw/?$topdir://?;
  }
  return $filename;
}

sub delete_file {
  my ($file) = @_;
  (my $topdir, $file) = conv_file($file);
  if ($topdir eq 'url'){
    print "Cannot delete files from a url\n";
    exit 1;
  }
  if (-d $file){
    my $print_dir = conv_file_to_rel($topdir,$file);
    if (y_or_n("Do you want to erase the entire $print_dir directory?")){
      system("rm -rf $file");
      print("Directory erased\n");
    }
  } elsif (-f $file) {
    my $print_file = conv_file_to_rel($topdir,$file);
    if (y_or_n("Do you want to erase the $print_file file?")){
      system("rm -rf $file");
      print("File erased\n");
    }
  }
}

sub url_copy {
  my ($from, $to) = @_;
  my ($f_topdir, $t_topdir);
  ($f_topdir, $from) = conv_file($from);
  ($t_topdir, $to) = conv_file($to);
  if ($t_topdir eq 'url' && $f_topdir eq 'url'){
    print "Cannot copy a url to a url\n";
    exit 1;
  } elsif($t_topdir eq 'url') {
    if (-d $from){
      print "Cannot upload an entire directory to url\n";
      exit 1;
    }
    curl_to($from, $to);
  } elsif ($f_topdir eq 'url') {
    if (-d $to){
      $from =~ /.*\/(.*)/;
      my $from_file = $1;
      $to = "$to/$from_file";
    }
    curl_from($from, $to);
  }
  exit 0;
}

sub copy {
  my ($from, $to) = @_;
  my ($f_topdir, $t_topdir);
  ($f_topdir, $from) = conv_file($from);
  if ($f_topdir eq 'url'){
    url_copy($from, $to);
  }
  ($t_topdir, $to) = conv_file($to);
  if ($t_topdir eq 'url'){
    url_copy($from, $to);
  }
  $from =~ /.*\/(.*)/;
  my $from_file = $1;
  if ( -d $from && -e $to && !( -d $to ) ){
    print "Cannot copy a directory to a file.\n";
    return 1;
  } elsif ( -f $to  || (-d $to && -f "$to/$from_file") ) {
    if (y_or_n("This file exists; overwrite if needed?")){
      rsync($from, $to);
    }
  } elsif ( -d $to && -d $from ){
    my $print_from = conv_file_to_rel($f_topdir, $from);
    my $print_to = conv_file_to_rel($t_topdir, $to);
    if (y_or_n("Merge directory $print_from with $print_to?")){
      rsync($from, $to);
    }
  } else {
    rsync($from, $to);
  }
}

sub update {
  my ($to, $from) = @_;
  my ($t_topdir, $f_topdir);
  ($f_topdir, $from) = conv_file($from);
  if ($f_topdir eq 'url'){
    print "Cannot update from a url\n";
    exit 1;
  }
  ($t_topdir, $to) = conv_file($to);
  if ($f_topdir eq 'url'){
    print "Cannot update to a url\n";
    exit 1;
  }
  my $print_from = conv_file_to_rel($f_topdir, $from);
  my $print_to = conv_file_to_rel($t_topdir, $to);
  my $msg = "WARNING: This is a destructive copy of the /config directories\n"
          . "This will erase all data in the ".$print_to."config directory\n"
          . "This data will be replaced with the data from $print_from\n"
          . "Do you wish to continue?";
  if (y_or_n("$msg")){
    system("rm -rf $to/config");
    rsync("$from/config", $to);
  }
}

sub rsync {
  my ($from,$to) = @_;
  system("rsync -av --progress $from $to");
}

sub curl_to {
  my ($from, $to) = @_;
  my $rc = system("curl -# -k -T $from $to");
  print "\n";
}

sub curl_from {
  my ($from, $to) = @_;
  my $rc = system("curl -# -k $from > $to");
  print "\n";
}

sub y_or_n {
  my ($msg) = @_;
  print "$msg (Y/N): ";
  my $input = <>;
  return 1 if ($input =~ /Y|y/);
  return 0;
}

sub show {
  my ($topdir, $file) = conv_file(pop(@_));
  my $output = "";
  if ($topdir eq 'url'){
    print "Cannot show files from a url\n";
    exit 1;
  }
  if ( -d $file ) {
    print "########### DIRECTORY LISTING ###########\n";
    system("ls -lGph  --group-directories-first $file");
  } elsif ( -T $file ) {
    print "########### FILE INFO ###########\n";
    my $filename = conv_file_to_rel($topdir, $file);
    print "File Name: $filename\n";
    print "Text File: \n";
    my $lsstr = `ls -lGh $file`;
    parsels($lsstr);
    print "  Description:\t";
    system("file -sb $file");
    print "\n########### FILE DATA ###########\n";
    system("cat $file");
  } elsif ( $file =~ /.*\.pcap/ ){
    print "########### FILE INFO ###########\n";
    my $filename = conv_file_to_rel($topdir, $file);
    print "File Name: $filename\n";
    print "Binary File: \n";
    my $lsstr = `ls -lGh $file`;
    parsels($lsstr);
    print "  Description:\t";
    system("file -sb $file");
    print "\n########### FILE DATA ###########\n";
    system("sudo tshark -r $file | less");
  } elsif ( -B $file ) {
    print "########### FILE INFO ###########\n";
    my $filename = conv_file_to_rel($topdir, $file);
    print "File Name: $filename\n";
    print "Binary File: \n";
    my $lsstr = `ls -lGh $file`;
    parsels($lsstr);
    print "  Description:\t";
    system("file -sb $file");
    print "\n########### FILE DATA ###########\n";
    system("hexdump -C $file| less");
  } else {
    my $filename = conv_file_to_rel($topdir, $file);
    print "File: $filename not found\n";
  }
}

sub parsels {
  my $lsout = pop(@_);
  my @ls = split(' ', $lsout);
  print "  Permissions: $ls[0]\n";
  print "  Owner:\t$ls[2]\n";
  print "  Size:\t\t$ls[3]\n";
  print "  Modified:\t$ls[4] $ls[5] $ls[6]\n";
}