/
Shares.pm
565 lines (480 loc) · 15.5 KB
/
Shares.pm
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
package Shares;
use strict;
use POSIX qw(:fcntl_h);
use PerlShareCommon::Dirs;
use PerlShareCommon::Log;
use PerlShareCommon::Str;
use PerlShareCommon::Constants;
use PerlShareCommon::WatchDirectoryTree;
use SshCmd;
use IO::Socket::SSL qw( SSL_VERIFY_NONE );
use LWP::Simple;
use Unison;
sub new() {
my $class = shift;
my $obj = {};
$obj->{message} = "";
mkdir(global_conf_dir());
tie my %cfg, 'PerlShareCommon::Cfg', READ => global_conf();
my $sleep_time = $cfg{main}{sync_sleep};
if ($sleep_time <= 0) { $sleep_time = 30; }
elsif ($sleep_time < 10) { $sleep_time = 10; }
$obj->{sleep_time} = $sleep_time;
untie %cfg;
bless $obj, $class;
return $obj;
}
sub get_shares() {
my $self = shift;
tie my %cfg, 'PerlShareCommon::Cfg', READ => global_conf();
my $count = $cfg{shares}{count} or 0;
my $i = 0;
my @shares;
while ($i < $count) {
push @shares, $cfg{shares}{share}[$i];
$i += 1;
}
return @shares;
}
sub associate() {
my $self = shift;
my $share = shift;
my $type = shift;
my $obj = shift;
my $assoc = {};
if (defined($self->{"assoc_$share"})) {
$assoc = $self->{"assoc_$share"};
}
$assoc->{$type} = $obj;
$self->{"assoc_$share"} = $assoc;
}
sub get_assoc() {
my $self = shift;
my $share = shift;
my $type = shift;
my $assoc = {};
if (defined($self->{"assoc_$share"})) {
$assoc = $self->{"assoc_$share"};
}
return $assoc->{$type};
}
sub check_share_host() {
my $self = shift;
my $share = shift;
my $host = shift;
my $email = shift;
my $pass = shift;
my $checker = sub {
my $url = shift;
log_info("Checking $url for user '$email' and share '$share'");
my $browser = LWP::UserAgent->new();
$browser->ssl_opts(
SSL_verify_hostname => 0,
SSL_verify_mode => SSL_VERIFY_NONE
);
my $response = $browser->post(
$url,
[ 'share' => $share,
'email' => $email,
'pass' => $pass
]
);
if (not($response->is_success)) {
log_error("Error at $url - '".$response->status_line."' Aborting");
return "Can't reach host '$host'";
}
my $content = $response->content();
log_info($content);
if ($content =~ /OKOKOK/) {
return undef;
} else {
return "User '$email' is not registered or wrong credentials have been given";
}
};
$ENV{PERL_LWP_SSL_VERIFY_HOSTNAME}=0;
my $result = $checker->("https://$host/check_user.php", $host);
if ($result=~/^Can[']t[ ]reach[ ]host/) {
my $r1 = $checker->("https://$host/perlshare/check_user.php", $host);
if ($r1=~/^Can[']t[ ]reach[ ]host/) {
return $result;
} else {
return $r1;
}
} else {
return $result;
}
}
sub push_public_key() {
my $self = shift;
my $share = shift;
my $host = shift;
my $email = shift;
my $pass = shift;
my $keyfile = shift;
open my $fh, "<$keyfile.pub";
my $key = "";
while(my $line = <$fh>) {
$key .= $line;
}
close($fh);
my $checker = sub {
my $url = shift;
log_info("Checking $url for user '$email' and share '$share'");
my $browser = LWP::UserAgent->new();
$browser->ssl_opts(
SSL_verify_hostname => 0,
SSL_verify_mode => SSL_VERIFY_NONE
);
my $response = $browser->post(
$url,
[ 'share' => $share,
'email' => $email,
'pass' => $pass,
'key' => $key
]
);
if (not($response->is_success)) {
log_error("Error at $url - '".$response->status_line."' Aborting");
return "Can't reach host";
}
my $content = $response->content();
log_info($content);
$ENV{PERL_LWP_SSL_VERIFY_HOSTNAME}=0;
if ($content =~ /OKOKOK/) {
return undef;
} else {
return "User not registered or wrong credentials given";
}
};
my $result = $checker->("https://$host/push_key.php");
if ($result eq "Can't reach host") {
return $checker->("https://$host/perlshare/push_key.php");
} else {
return $result;
}
}
sub create_share() {
my $self = shift;
my $sharename = shift;
my $host = shift;
my $email = shift;
my $pass = shift;
my $locshare = shift;
if (not($locshare)) { $locshare = $sharename; }
log_info("####");
log_info("Creating new share for host '$host', with email '$email'");
log_info("####");
# First check if we can reach the host
log_info("Checking access to $host for user $email (share=$sharename, local=$locshare)");
my $message = $self->check_share_host($sharename, $host, $email, $pass);
if (defined($message)) {
log_error($message);
$self->{message} = $message;
log_info("####");
return 0;
}
my $sshkey_file;
my $ssh = new SshCmd();
my $fh;
# Go on and create
mkdir(perlshare_dir($locshare));
chmod(0755, perlshare_dir($locshare));
mkdir(conf_dir($locshare));
mkdir(unison_dir($locshare));
my $sshkey_file = sshkey($locshare);
my $sshkey_pub_file = pub_sshkey($locshare);
# Create a new sshkey if necessary
log_info("Creating RSA key at $sshkey_file");
if (! -r $sshkey_file) {
$ssh->create_keyfile($sshkey_file);
}
# Check if we already can reach the host with this key
log_info("Pushing public key to server $host for user $email");
my $result = 1;
$message = $self->push_public_key($sharename, $host, $email, $pass, $sshkey_file);
if (defined($message)) {
log_error($message);
$self->{message} = $message;
log_info("####");
$result = 0;
}
if ($result) {
my $prf_file = unison_dir($locshare)."/default.prf";
my $sshconfig = unison_dir($locshare)."/sshconfig";
log_info("Creating profile '$prf_file'");
my $os = $^O;
$ssh->create_ssh_config($sshconfig, $host, $email, $sshkey_file);
my $perlsharemerge = my_dir()."/PerlShareMerge.pl";
my $sharedir = perlshare_dir($locshare);
open $fh, ">$prf_file";
print $fh "root = $sharedir\n";
print $fh "root = ssh://$host//home/perlshare/$email/$sharename\n";
if ($os=~/MSWin/) {
print $fh "sshargs = -F '$sshconfig' -l $email\n";
} else {
print $fh "sshargs = -F $sshconfig -l $email\n";
}
print $fh "ignore = Path .*\n";
print $fh "follow = Regex .*\n";
print $fh "fastcheck = true\n";
print $fh "fat = true\n";
print $fh "dontchmod = false\n";
print $fh "perms = 0\n";
print $fh "merge = Name * -> perl $perlsharemerge \"$sharedir\" \"PATH\" \"CURRENT1\" \"CURRENT2\" \"NEW\"\n";
print $fh "servercmd = /usr/share/perlshare/unison_umask\n";
print $fh "copythreshold = 1000\n";
print $fh "copyprog = rsync -e \"ssh -F '$sshconfig' -l $email\" --inplace --compress\n";
# Maybe later, don't know the consequences yet.
#print $fh "copyprogrest = rsync -e \"ssh -F '$sshconfig' -l $email\" --partial --inplace --compress\n";
close($fh);
$self->{message} = "Success";
my $result = 1;
}
if ($result) {
# Check if unison is there and unison versions
log_info("Checking consistency of unison versions");
my $unison_ctrl = new Unison();
if ($unison_ctrl->has_unison()) {
my $local_version = $unison_ctrl->version();
my $remote_version = $unison_ctrl->version($host, $sshkey_file, $email);
log_info("Unison: local version: $local_version");
log_info("Unison: remote version: $remote_version");
if ($local_version ne $remote_version) {
log_error("Unison: local and remote versions differ!");
$self->{message} = "The local and remote versions of PerlShare differ. Make sure they are the same.";
$result = 0;
}
} else {
log_error("No unison installed!");
$self->{message} = "You need unison installed for PerlShare to work";
$result = 0;
}
}
# put share in config
if ($result) {
log_info("Adding share to configuration");
tie my %cfg, 'PerlShareCommon::Cfg', READ => global_conf(), WRITE => global_conf();
my $num_of_shares = $cfg{shares}{count};
if (not(defined($num_of_shares))) { $num_of_shares = 0; }
$cfg{shares}{share}[$num_of_shares] = $locshare;
$cfg{shares}{count} = $num_of_shares + 1;
$cfg{data}{$locshare}{host} = $host;
$cfg{data}{$locshare}{keyfile} = $sshkey_file;
$cfg{data}{$locshare}{email} = $email;
$cfg{data}{$locshare}{local} = $locshare;
$cfg{data}{$locshare}{remote} = $sharename;
untie %cfg;
}
log_info($self->{message});
log_info("####");
return $result;
}
sub drop_local() {
my $self = shift;
my $share = shift;
tie my %cfg, 'PerlShareCommon::Cfg', READ => global_conf(), WRITE => global_conf();
my $num_of_shares = $cfg{shares}{count};
if (not(defined($num_of_shares))) { $num_of_shares = 0; }
log_info("analyzing shares (num of shares = $num_of_shares)");
my @sharenames;
my $i = 0;
while ($i < $num_of_shares) {
my $shr = $cfg{shares}{share}[$i];
log_info("share = $share, shr = $shr, i=$i");
if ($cfg{shares}{share}[$i] ne $share) {
push @sharenames, $cfg{shares}{share}[$i];
}
$i += 1;
}
log_info("deleting shares");
delete $cfg{shares};
log_info("recreating shares");
$i = 0;
foreach my $shr (@sharenames) {
$cfg{shares}{share}[$i] = $shr;
$i += 1;
}
$cfg{shares}{count} = $i;
log_info("done, num of shares = $i");
untie %cfg;
}
sub get_message() {
my $self = shift;
return $self->{message};
}
sub set_last_sync() {
my $self = shift;
my $share = shift;
}
# if a client has local changes, it will push these changes to the server
# along with it, it will update a .count file in the share.
# if the local .count file < the remote .count file,
# we need to synchronize and update the .count file to the latest.
# while synchronizing we need to ignore changes to the filesystem
#
sub check_last_sync() {
my $self = shift;
my $share = shift;
tie my %cfg, 'PerlShareCommon::Cfg', READ => global_conf();
my $host = $cfg{data}{$share}{host};
my $keyfile = $cfg{data}{$share}{keyfile};
my $email = $cfg{data}{$share}{email};
my $local_share = $cfg{data}{$share}{local};
my $remote_share = $cfg{data}{$share}{remote};
untie %cfg;
# Only work this through when the directory is present
# So mobile devices that are not connected will be skipped
{
my $localshr = perlshare_dir($local_share);
if (! -d $localshr) {
log_warn("Local directory for $local_share not present");
return (-2, undef, undef);
}
}
log_info("checking share '$share', host=$host, email=$email");
# We need to check for each top directory if the count has changed.
my $ssh = new SshCmd();
my $cmd = $ssh->ssh_cmd($host, $email, "cat /home/perlshare/$email/$remote_share/.count", $keyfile);
my $log2 = temp_dir()."/perlsharecheck.err";
open my $fh, "$cmd 2>\"$log2\" |";
my $remote_count = <$fh>;
$remote_count = trim($remote_count);
if (not($remote_count)) { $remote_count = -1; }
close($fh);
my $exit_code = $? / 256;
log_info("exitcode = $exit_code");
if ($exit_code != 0) {
log_warn("Cannot connect host for sync checking");
return (-1, undef, undef);
}
my $localshr = perlshare_dir($local_share);
open $fh, "<$localshr/.count";
my $local_count = <$fh>;
$local_count = trim($local_count);
close($fh);
if (not($local_count)) { $local_count = 0; }
my $conflict = 0;
if (-e "$localshr/.conflict") {
unlink("$localshr/.conflict");
$conflict = 1;
}
if ($remote_count != $local_count || $conflict) {
return (1, $remote_count, $local_count);
} else {
return (0, $remote_count, $local_count);
}
}
sub get_share_info() {
my $shares = shift;
my $share = shift;
tie my %cfg, 'PerlShareCommon::Cfg', READ => global_conf();
my $host = $cfg{data}{$share}{host};
my $keyfile = $cfg{data}{$share}{keyfile};
my $email = $cfg{data}{$share}{email};
my $local_share = $cfg{data}{$share}{local};
my $remote_share = $cfg{data}{$share}{remote};
untie %cfg;
return ($host, $email, $local_share, $remote_share, $keyfile);
}
sub sync_now() {
my $self = shift;
my $share = shift;
my $cb_func = shift;
my $remote_count = shift;
my $local_count = shift;
tie my %cfg, 'PerlShareCommon::Cfg', READ => global_conf();
my $host = $cfg{data}{$share}{host};
my $keyfile = $cfg{data}{$share}{keyfile};
my $email = $cfg{data}{$share}{email};
my $local_share = $cfg{data}{$share}{local};
my $remote_share = $cfg{data}{$share}{remote};
untie %cfg;
my $unison = new Unison();
$unison->run($share, $cb_func);
my $count = ($remote_count > $local_count) ? $remote_count-1 : $local_count;
$count += 1;
my $localshr = perlshare_dir($local_share);
open my $fh, ">$localshr/.count";
print $fh "$count\n";
close($fh);
my $ssh = new SshCmd();
my $cmd = $ssh->ssh_cmd($host, $email,
"echo $count >/home/perlshare/$email/$remote_share/.count;".
"chmod 664 /home/perlshare/$email/$remote_share/.count",
$keyfile
);
my $log2 = temp_dir()."/perlsharecheck.err";
open $fh, "$cmd 2>$log2 |";
while (my $line = <$fh>) {
log_info($line);
}
close($fh);
}
# This is the synchronized gardian.
sub synchronizer() {
my $self = shift;
my $cb = shift;
my $thr = threads->create(
sub {
my $shares = new Shares();
log_info("Starting synchronizer for all shares");
my $first_time = 1;
while ( 1 ) {
log_info("looping");
my @S = $shares->get_shares();
foreach my $share (@S) {
tie my %cfg, 'PerlShareCommon::Cfg', READ => global_conf();
my $local_share = $cfg{data}{$share}{local};
untie %cfg;
my $dir = perlshare_dir($local_share);
# Now check if we have this directory. It could be disconnected
# if it is a directory on a mobile device.
if (-d $dir) {
# We have this directory, so we can do things with it.
my $watcher = $shares->get_assoc($share, "watcher");
if (defined($watcher)) {
if ($watcher->get_directory_changes()) {
open my $fh, "<$dir/.count";
my $cnt = <$fh>;
log_info("count = $cnt");
trim($cnt);
close($fh);
$cnt += 1;
open $fh, ">$dir/.count";
print $fh "$cnt\n";
close($fh);
}
} else {
$watcher = new PerlShareCommon::WatchDirectoryTree($dir);
$shares->associate($share, "watcher", $watcher);
}
my ($sync, $remote_count, $local_count) = $shares->check_last_sync($share);
if ($sync == -1) {
# We cannot
$cb->($share, "disconnected", -1);
} elsif ($sync || $first_time) {
# we can connect, and need to sync
$cb->($share, "connected", 0);
$shares->sync_now($share, $cb, $remote_count, $local_count);
} else {
# we can connect, but don't need to sync
$cb->($share, "connected", 0);
}
log_info("done syncing");
# flush watcher after sync
$watcher->get_directory_changes();
log_info("got directory changes");
} else {
log_info("This directory is not present");
$cb->($share, "not present", -1);
}
}
$first_time = 0;
log_info("sleeping ".$self->{sleep_time}." seconds");
sleep($self->{sleep_time});
}
}
);
$thr->detach();
}
1;