summaryrefslogtreecommitdiffstats
path: root/perl-install/network/smb.pm
blob: a9f8907cefa53cc3f4585dd661a56e7606acb950 (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
package network::smb;

use common;
use network::network;

sub check {
    my ($in) = @_;
    my $f = '/usr/bin/nmblookup';
    -e $f or $in->do_pkgs->install('samba-client');
    -e $f or $in->ask_warn('', "Mandatory package samba-client is missing"), return;
    1;
}


sub find_servers() {
    my (undef, @l) = `nmblookup "*"`;
    s/\s.*\n// foreach @l;
    my @servers = grep { network::network::is_ip($_) } @l;
    my %servers;
    $servers{$_}{ip} = $_ foreach @servers;
    my ($ip);
    foreach (`nmblookup -A @servers`) {
	if (my $nb = /^Looking up status of (\S+)/ .. /^$/) {
	    if ($nb == 1) {
		$ip = $1;
	    } else {
		/<00>/ or next;
		$servers{$ip}{/<GROUP>/ ? 'group' : 'name'} ||= lc first(/(\S+)/);
	    }
	}
    }
    values %servers;
}

sub find_exports {
    my ($server) = @_;
    my @l;
    my $ip    = $server->{ip}    ? " -I $server->{ip}"  : '';
    my $group = $server->{group} ? " -W $server->{group}" : '';

    # WARNING: using smbclient -L is ugly. It can't handle more than 15
    # characters shared names

    foreach (`smbclient -U% -L $server->{name}$ip$group`) {
	chomp;
	s/^\t//;
	my ($name, $type, $comment) = unpack "A15 A10 A*", $_;
	if ($name eq '---------' && $type eq '----' && $comment eq '-------' .. /^$/) {
	    push @l, { name => $name, type => $type, comment => $comment }
	      if $type eq 'Disk' && $name ne 'ADMIN$';
	}
    }
    @l;
}

1;