~azzar1/unity/add-show-desktop-key

« back to all changes in this revision

Viewing changes to ivle/console.py

  • Committer: me at id
  • Date: 2009-01-15 00:37:10 UTC
  • mto: This revision was merged to the branch mainline in revision 1090.
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:branches%2Fstorm:1141
ivle.database.User: Add an authenticate() method, and a hash_password()
     staticmethod to replace the auth functions in ivle.db.

Show diffs side-by-side

added added

removed removed

Lines of Context:
23
23
 
24
24
import errno
25
25
import cPickle
26
 
import hashlib
 
26
import md5
27
27
import os
28
28
import random
29
29
import socket
32
32
 
33
33
import cjson
34
34
 
35
 
from ivle import chat
 
35
import ivle.conf
 
36
from ivle import (chat, util)
 
37
 
 
38
# Outside Jail
 
39
trampoline_path = os.path.join(ivle.conf.lib_path, "trampoline")
 
40
# Inside Jail
 
41
python_path = "/usr/bin/python"
 
42
console_dir = os.path.join(ivle.conf.share_path, 'services')
 
43
console_path = os.path.join(console_dir, 'python-console')
36
44
 
37
45
class ConsoleError(Exception):
38
46
    """ The console failed in some way. This is bad. """
39
 
    pass
 
47
    def __init__(self, value):
 
48
        self.value = value
 
49
    def __str__(self):
 
50
        return repr(self.value)
40
51
 
41
52
class ConsoleException(Exception):
42
53
    """ The code being exectuted on the console returned an exception. 
43
54
    """
44
 
    pass
 
55
    def __init__(self, value):
 
56
        self.value = value
 
57
    def __str__(self):
 
58
        return repr(self.value)
45
59
 
46
60
class TruncateStringIO(StringIO.StringIO):
47
61
    """ A class that wraps around StringIO and truncates the buffer when the 
108
122
class Console(object):
109
123
    """ Provides a nice python interface to the console
110
124
    """
111
 
    def __init__(self, config, uid, jail_path, working_dir):
 
125
    def __init__(self, uid, jail_path, working_dir):
112
126
        """Starts up a console service for user uid, inside chroot jail 
113
127
        jail_path with work directory of working_dir
114
128
        """
115
129
        super(Console, self).__init__()
116
130
 
117
 
        self.config = config
118
131
        self.uid = uid
119
132
        self.jail_path = jail_path
120
133
        self.working_dir = working_dir
141
154
 
142
155
        # Create magic
143
156
        # TODO
144
 
        self.magic = hashlib.md5(uuid.uuid4().bytes).hexdigest()
 
157
        self.magic = md5.new(uuid.uuid4().bytes).digest().encode('hex')
145
158
 
146
159
        # Try to find a free port on the server.
147
160
        # Just try some random ports in the range [3000,8000)
155
168
        while tries < 5:
156
169
            self.port = int(random.uniform(3000, 8000))
157
170
 
158
 
            trampoline_path = os.path.join(self.config['paths']['lib'],
159
 
                                           "trampoline")
160
 
 
161
171
            # Start the console server (port, magic)
162
172
            # trampoline usage: tramp uid jail_dir working_dir script_path args
163
173
            # console usage:    python-console port magic
164
174
            res = os.spawnv(os.P_WAIT, trampoline_path, [
165
175
                trampoline_path,
166
176
                str(self.uid),
167
 
                self.config['paths']['jails']['mounts'],
168
 
                self.config['paths']['jails']['src'],
169
 
                self.config['paths']['jails']['template'],
170
177
                self.jail_path,
171
 
                os.path.join(self.config['paths']['share'], 'services'),
172
 
                "/usr/bin/python",
173
 
                os.path.join(self.config['paths']['share'],
174
 
                             'services/python-console'),
 
178
                console_dir,
 
179
                python_path,
 
180
                console_path,
175
181
                str(self.port),
176
182
                str(self.magic),
177
183
                self.working_dir
207
213
            # Couldn't decode the JSON
208
214
            raise ConsoleError(
209
215
                "Could not understand the python console response")
210
 
        except chat.ProtocolError, e:
211
 
            raise ConsoleError(*e.args)
212
216
 
213
217
        return response
214
218
 
293
297
              
294
298
        # Unpickle any exceptions
295
299
        if 'exception' in execute:
296
 
            execute['exception'] = cPickle.loads(execute['exception'])
297
 
        return execute
 
300
            return cPickle.loads(execute['exception'])
 
301
        else:
 
302
            return execute
298
303
 
299
304
 
300
305
    def set_vars(self, variables):