~ubuntu-branches/ubuntu/raring/uml-utilities/raring

« back to all changes in this revision

Viewing changes to honeypot/hppfs.pm

  • Committer: Bazaar Package Importer
  • Author(s): Matt Zimmerman
  • Date: 2004-04-14 17:59:45 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20040414175945-olq69xkt2da1oxca
Tags: 20040406-1
* New upstream release
* Patch from Carlos Perelló Marín to fix uml_proxy_arp with multiple
  addresses (Closes: #243834)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
2
# Copyright (C) 2002 Jeff Dike (jdike@karaya.com)
 
3
# Licensed under the GPL
 
4
#
 
5
 
 
6
package hppfs;
 
7
 
 
8
use Socket;
 
9
use IO::Select;
 
10
 
 
11
use strict;
 
12
 
 
13
sub new {
 
14
    my $class = shift;
 
15
    my $base = shift;
 
16
 
 
17
    !defined($base) and $base = ".";
 
18
    my $me = { files => { }, handles => { }, base => $base };
 
19
 
 
20
    bless($me, $class);
 
21
    return($me);
 
22
}
 
23
 
 
24
sub add {
 
25
    my $me = shift;
 
26
 
 
27
    while(@_){
 
28
        my $file = shift;
 
29
        my $handler = shift;
 
30
 
 
31
        $me->{files}->{$file} = $handler;
 
32
    }
 
33
}
 
34
 
 
35
sub prepare {
 
36
    my $me = shift;
 
37
    my $dir = shift;
 
38
    my $file = shift;
 
39
 
 
40
    my $full = $me->{base} . "/proc/$dir";
 
41
    if(! -d $full){
 
42
        unlink $full;
 
43
        my $out = `mkdir -p $full 2>&1`;
 
44
        $? and die "mkdir '$full' failed : $out";
 
45
    }
 
46
 
 
47
    my $out = `chmod 755 $full 2>&1`;
 
48
    $? and die "chmod 755 $full failed : $out";
 
49
 
 
50
    defined($file) and unlink "$full/$file";
 
51
    return("$full/$file");
 
52
}
 
53
 
 
54
sub setup_sock {
 
55
    my ($me, $file, undef, $mode) = @_;
 
56
 
 
57
    my $full = $me->prepare($file, $mode);
 
58
 
 
59
    my $sock = sockaddr_un($full);
 
60
    !defined($sock) and die "sockaddr_un of '$sock' failed : $!";
 
61
 
 
62
    !defined(socket(my $fh, AF_UNIX, SOCK_STREAM, 0)) and 
 
63
        die "socket failed : $!";
 
64
 
 
65
    !defined(bind($fh, $sock)) and die "bind failed : $!";
 
66
    my $out = `chmod 777 $full 2>&1`;
 
67
    $? ne 0 and die "'chmod 777 $full' failed : $!";
 
68
 
 
69
    !defined(listen($fh, 5)) and die "listen failed : $!";
 
70
 
 
71
    $me->{select}->add(\*$fh);
 
72
    $me->{handles}->{fileno(\*$fh)} = $file;
 
73
}
 
74
 
 
75
sub setup_remove {
 
76
    my ($me, $file) = @_;
 
77
 
 
78
    my $full = $me->prepare($file);
 
79
 
 
80
    my $out = `touch $full/remove 2>&1`;
 
81
    $? != 0 and die "touch $full/remove failed : $out";
 
82
}
 
83
 
 
84
sub handler {
 
85
    my $me = shift;
 
86
    $me->{select} = IO::Select->new();
 
87
 
 
88
    foreach my $file (keys(%{$me->{files}})){
 
89
        my $handler = $me->{files}->{$file};
 
90
        if(ref($handler) eq "ARRAY"){
 
91
            $me->setup_sock($file, @$handler);
 
92
        }
 
93
        elsif($handler eq "remove"){
 
94
            $me->setup_remove($file);
 
95
        }
 
96
        else {
 
97
            die "Bad handler for '$file'";
 
98
        }
 
99
    }
 
100
 
 
101
    while(1){
 
102
        my @ready = $me->{select}->can_read();
 
103
        
 
104
        foreach my $sock (@ready){
 
105
            my $file = $me->{handles}->{fileno($sock)};
 
106
            !defined($file) and die "Couldn't map from socket to file";
 
107
 
 
108
            !accept(CONN, $sock) and die "accept failed : $!";
 
109
 
 
110
            my ($handler, $mode) = @{$me->{files}->{$file}};
 
111
 
 
112
            (!defined($handler) || !defined($mode)) and 
 
113
                die "Couldn't map from file to handler";
 
114
 
 
115
            my $output;
 
116
 
 
117
            if($mode eq "rw"){
 
118
                my $input = join("", <CONN>);
 
119
                $output = $handler->($input);
 
120
            }
 
121
            else {
 
122
                $output = $handler->();
 
123
            }
 
124
 
 
125
            print CONN $output;
 
126
            close CONN;
 
127
        }
 
128
    }
 
129
}
 
130
 
 
131
1;