File: session.py

package info (click to toggle)
python-ipmi 0.5.7-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 1,132 kB
  • sloc: python: 12,645; makefile: 2
file content (140 lines) | stat: -rw-r--r-- 4,175 bytes parent folder | download
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
# Copyright (c) 2016  Kontron Europe GmbH
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA


class Session(object):
    AUTH_TYPE_NONE = 0x00
    AUTH_TYPE_MD2 = 0x01
    AUTH_TYPE_MD5 = 0x02
    AUTH_TYPE_PASSWORD = 0x04
    AUTH_TYPE_OEM = 0x05

    PRIV_LEVEL_USER = 2
    PRIV_LEVEL_OPERATOR = 3
    PRIV_LEVEL_ADMINISTRATOR = 4
    PRIV_LEVEL_OEM = 5

    session_id = None
    _interface = None
    _priv_level = PRIV_LEVEL_ADMINISTRATOR
    _auth_type = AUTH_TYPE_NONE
    _auth_username = None
    _auth_password = None
    _rmcp_host = None
    _rmcp_port = None
    _serial_port = None
    _serial_baudrate = None

    def __init__(self):
        self.established = False
        self.sid = 0
        self.sequence_number = 0
        self.activated = False

    def _get_interface(self):
        try:
            return self._interface
        except AttributeError:
            raise RuntimeError('No interface has been set')

    def _set_interface(self, interface):
        self._interface = interface

    def increment_sequence_number(self):
        self.sequence_number += 1
        if self.sequence_number > 0xffffffff:
            self.sequence_number = 1

    def set_session_type_rmcp(self, host, port=623):
        self._rmcp_host = host
        self._rmcp_port = port

    @property
    def rmcp_host(self):
        return self._rmcp_host

    @property
    def rmcp_port(self):
        return self._rmcp_port

    def set_session_type_serial(self, port, baudrate):
        self._serial_port = port
        self._serial_baudrate = baudrate

    @property
    def serial_port(self):
        return self._serial_port

    @property
    def serial_baudrate(self):
        return self._serial_baudrate

    @property
    def priv_level(self):
        return self._priv_level

    def set_priv_level(self, level):
        LEVELS = {
                   'user': self.PRIV_LEVEL_USER,
                   'operator': self.PRIV_LEVEL_OPERATOR,
                   'administrator': self.PRIV_LEVEL_ADMINISTRATOR,
                 }
        self._priv_level = LEVELS[level.lower()]

    def _set_auth_type(self, auth_type):
        self._auth_type = auth_type

    def _get_auth_type(self):
        return self._auth_type

    def set_auth_type_user(self, username, password):
        self._auth_type = self.AUTH_TYPE_PASSWORD
        self._auth_username = username
        self._auth_password = password

    @property
    def auth_username(self):
        return self._auth_username

    @property
    def auth_password(self):
        return self._auth_password

    def establish(self):
        if hasattr(self.interface, 'establish_session'):
            self.interface.establish_session(self)

    def close(self):
        if hasattr(self.interface, 'close_session'):
            self.interface.close_session()

    def rmcp_ping(self):
        if hasattr(self.interface, 'rmcp_ping'):
            self.interface.rmcp_ping()

    def __str__(self):
        string = 'Session:\n'
        string += '  ID: 0x%08x\n' % self.sid
        string += '  Seq: 0x%08x\n' % self.sequence_number
        string += '  Host: %s:%s\n' % (self._rmcp_host, self._rmcp_port)
        string += '  Auth.: %s\n' % self.auth_type
        string += '  User: %s\n' % self._auth_username
        string += '  Password: %s\n' % self._auth_password
        string += '\n'
        return string

    interface = property(_get_interface, _set_interface)
    auth_type = property(_get_auth_type, _set_auth_type)