session monitor
[freeside.git] / fs_sesmon / FS-SessionClient / SessionClient.pm
1 package FS::SessionClient;
2
3 use strict;
4 use vars qw($VERSION @ISA @EXPORT_OK $fs_sessiond_socket);
5 use Exporter;
6 use Socket;
7 use FileHandle;
8 use IO::Handle;
9
10 $VERSION = '0.01';
11
12 @ISA = qw( Exporter );
13 @EXPORT_OK = qw( login logoff );
14
15 $fs_sessiond_socket = "/usr/local/freeside/fs_sessiond_socket";
16
17 $ENV{'PATH'} ='/usr/bin:/bin';
18 $ENV{'SHELL'} = '/bin/sh';
19 $ENV{'IFS'} = " \t\n";
20 $ENV{'CDPATH'} = '';
21 $ENV{'ENV'} = '';
22 $ENV{'BASH_ENV'} = '';
23
24 my $freeside_uid = scalar(getpwnam('freeside'));
25 die "not running as the freeside user\n" if $> != $freeside_uid;
26
27 =head1 NAME
28
29 FS::SessionClient - Freeside session client API
30
31 =head1 SYNOPSIS
32
33   use FS::SessionClient qw( login portnum logoff );
34
35   $error = login ( {
36     'username' => $username,
37     'password' => $password,
38     'login'    => $timestamp,
39     'portnum'  => $portnum,
40   } );
41
42   $portnum = portnum( { 'ip' => $ip } ) or die "unknown ip!"
43   $portnum = portnum( { 'nasnum' => $nasnum, 'nasport' => $nasport } )
44     or die "unknown nasnum/nasport";
45
46   $error = logoff ( {
47     'username' => $username,
48     'password' => $password,
49     'logoff'   => $timestamp,
50     'portnum'  => $portnum,
51   } );
52
53 =head1 DESCRIPTION
54
55 This modules provides an API for a remote session application.
56
57 It needs to be run as the freeside user.  Because of this, the program which
58 calls these subroutines should be written very carefully.
59
60 =head1 SUBROUTINES
61
62 =over 4
63
64 =item login HASHREF
65
66 HASHREF should have the following keys: username, password, login and portnum.
67 login is a UNIX timestamp; if not specified, will default to the current time.
68 Starts a new session for the specified user and portnum.  The password is
69 optional, but must be correct if specified.
70
71 Returns a scalar error message, or the empty string for success.
72
73 =item portnum
74
75 HASHREF should contain a single key: ip, or the two keys: nasnum and nasport.
76 Returns a portnum suitable for the login and logoff subroutines, or false
77 on error.
78
79 =item logoff HASHREF
80
81 HASHREF should have the following keys: usrename, password, logoff and portnum.
82 logoff is a UNIX timestamp; if not specified, will default to the current time.
83 Starts a new session for the specified user and portnum.  The password is
84 optional, but must be correct if specified.
85
86 Returns a scalar error message, or the empty string for success.
87
88 =cut
89
90 sub AUTOLOAD {
91   my $hashref = shift;
92   socket(SOCK, PF_UNIX, SOCK_STREAM, 0) or die "socket: $!";
93   connect(SOCK, sockaddr_un($fs_sessiond_socket)) or die "connect: $!";
94   print SOCK "$AUTOLOAD\n";
95
96   print SOCK join("\n", %{$hashref}, 'END' ), "\n";
97   SOCK->flush;
98
99   chomp( my $r = <SOCK> );
100   $r;
101 }
102
103 =back
104
105 =head1 VERSION
106
107 $Id: SessionClient.pm,v 1.1 2000-10-27 20:15:50 ivan Exp $
108
109 =head1 BUGS
110
111 =head1 SEE ALSO
112
113 L<fs_sessiond>
114
115 =cut
116
117 1;
118
119
120