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

« back to all changes in this revision

Viewing changes to scripts/usrmgt-server

  • Committer: mattgiuca
  • Date: 2008-02-29 01:18:22 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:621
Added 2 new apps: home and subjects. Both fairly incomplete, just a basic
    skeleton.
    "home" is the new default app, replacing "files".
    (It has a link to files).

Show diffs side-by-side

added added

removed removed

Lines of Context:
8
8
from functools import partial
9
9
import traceback
10
10
import logging
11
 
import errno
12
11
 
13
12
import conf
14
13
import common.db
65
64
       Return Value: the uid associated with the user. INT
66
65
    """
67
66
 
 
67
    if 'unixid' not in props or props['unixid'] is None:
 
68
        # For the time being, just choose a random unixid.
 
69
        # This may result in duplicates, but the unixid is essentially
 
70
        # a monitoring/logging convenience only. Oh, and dups could kill
 
71
        # each other. <sigh>
 
72
        props['unixid'] = random.randrange(5000,10000)
 
73
        # raise NotImplementedError, "No algorithm for creating uids yet!"
 
74
        # unixid = invent-uid
 
75
        # props['unixid'] = unixid
 
76
 
68
77
    common.makeuser.make_user_db(**props)
69
 
    user = common.db.get_user(props["login"])
70
 
    return user["unixid"]
 
78
 
 
79
    return int(props['unixid'])
71
80
 
72
81
def update_user(props):
73
82
    """Create the database record for the given user.
88
97
    db.update_user(**update)
89
98
    db.close()
90
99
 
91
 
def get_login(login, passwd, realm, existing_login, _may_save):
 
100
def get_login(login, passwd, _realm, _login, _may_save):
92
101
    """Callback function used by pysvn for authentication.
93
 
    login, passwd: Credentials of the user attempting to log in.
94
 
        passwd in clear (this should be the user's svn_pass, a
95
 
        randomly-generated permanent password for this user, not their main
96
 
        IVLE password).
97
 
    realm, existing_login, _may_save: The 3 arguments passed by pysvn to
98
 
        callback_get_login.
99
 
        The following has been determined empirically, not from docs:
100
 
        existing_login will be the name of the user who owns the process on
101
 
        the first attempt, "" on subsequent attempts. We use this fact.
102
102
    """
103
 
    logging.debug("Getting password for %s (realm %s)" % (login, realm))
104
 
    # Only provide credentials on the _first_ attempt.
105
 
    # If we're being asked again, then it means the credentials failed for
106
 
    # some reason and we should just fail. (This is not desirable, but it's
107
 
    # better than being asked an infinite number of times).
108
 
    if existing_login == "":
109
 
        logging.warning("Could not authenticate SVN for %s" % login)
110
 
        return (False, login, passwd, False)
111
 
    else:
112
 
        return (True, login, passwd, False)
 
103
    logging.debug("Getting password for %s (realm %s)" % (login, _realm))
 
104
    return (True, login, passwd, False)
113
105
 
114
106
def activate_user(props):
115
107
    """Create the on-disk stuff for the given user.
120
112
       Return Value: None
121
113
    """
122
114
 
123
 
    os.umask(0022) # Bad, but start_server sets it worse.
124
 
 
125
115
    login = props['login']
126
116
 
127
117
    db = common.db.DB()
161
151
        except Exception, exc:
162
152
            logging.warning("While mkdiring info1: %s" % str(exc))
163
153
            pass
 
154
        logging.debug("Creating /submissions")
 
155
        try:
 
156
            svn.mkdir(conf.svn_addr + login + "/submissions",
 
157
                      "Initial creation of submissions directory")
 
158
        except Exception, exc:
 
159
            logging.warning("While mkdiring submissions: %s" % str(exc))
 
160
            pass
164
161
        logging.debug("Creating /stuff")
165
162
        try:
166
163
            svn.mkdir(conf.svn_addr + login + "/stuff",
170
167
            pass
171
168
 
172
169
        logging.debug("Creating jail")
173
 
        common.makeuser.make_jail(login, details.unixid, svn_pass=passwd)
174
 
 
175
 
        common.makeuser.mount_jail(login)
176
 
 
177
 
        do_checkout(props, svn_pass=passwd)
 
170
        common.makeuser.make_jail(login, details.unixid)
 
171
 
 
172
        # FIXME: <hack>
 
173
 
 
174
        tcf_path = os.path.join(conf.jail_base, 'template/opt/ivle/lib/conf/conf.py')
 
175
        cf_path = os.path.join(conf.jail_base, login, 'opt/ivle/lib/conf/conf.py')
 
176
 
 
177
        os.remove(cf_path)
 
178
        cf = open(cf_path, "w")
 
179
        cf.write(open(tcf_path, "r").read())
 
180
        cf.write("# The login name for the owner of the jail\n")
 
181
        cf.write("login = %s\n" % repr(login))
 
182
        cf.write("\n")
 
183
        cf.write("# The subversion-only password for the owner of the jail\n")
 
184
        cf.write("svn_pass = %s\n" % repr(passwd))
 
185
        cf.close()
 
186
 
 
187
        # FIXME: </hack>
 
188
 
 
189
        logging.debug("Checking out directories in the jail")
 
190
        try:
 
191
            svn.checkout(conf.svn_addr + login + "/stuff",
 
192
                         common.studpath.url_to_local(login + "/stuff")[1])
 
193
        except Exception, exc:
 
194
            logging.warning("While mkdiring stuff: %s" % str(exc))
 
195
            pass
 
196
        try:
 
197
            svn.checkout(conf.svn_addr + login + "/submissions",
 
198
                         common.studpath.url_to_local(login + "/submissions")[1])
 
199
        except Exception, exc:
 
200
            logging.warning("While mkdiring submissions: %s" % str(exc))
 
201
            pass
 
202
        try:
 
203
            svn.checkout(conf.svn_addr + login + "/info1",
 
204
                         common.studpath.url_to_local(login + "/info1")[1])
 
205
        except Exception, exc:
 
206
            logging.warning("While mkdiring info1: %s" % str(exc))
 
207
            pass
178
208
 
179
209
        # FIXME: should this be nicer?
180
210
        os.system("chown -R %d:%d %s" \
181
211
                % (details.unixid, details.unixid,
182
212
                   common.studpath.url_to_local(login)[1]))
183
213
 
 
214
 
184
215
        logging.info("Enabling user")
185
216
        db.update_user(login, state='enabled')
186
217
 
189
220
    finally:
190
221
        db.close()
191
222
 
192
 
def do_checkout(props, svn_pass=None):
193
 
    """Create the default contents of a user's jail by checking out from the
194
 
    repositories.
195
 
    If any directory already exists, just fail silently (so this is not a
196
 
    "wipe-and-checkout", merely a checkout if it failed or something).
197
 
       Expected properties:
198
 
        login       - the user name for the jail
199
 
                      STRING REQUIRED
200
 
       Return Value: None
201
 
    """
202
 
    login = props['login']
203
 
 
204
 
    db = common.db.DB()
205
 
    user = db.get_user(login)
206
 
    # If svn_pass isn't supplied, grab it from the DB
207
 
    if svn_pass is None:
208
 
        svn_pass = user.svn_pass
209
 
    db.close()
210
 
 
211
 
    svn = pysvn.Client()
212
 
    svn.callback_get_login = partial(get_login, login, svn_pass)
213
 
 
214
 
    if conf.svn_addr[-1] != os.sep:
215
 
        conf.svn_addr += os.sep
216
 
 
217
 
    # FIXME: <hack>
218
 
 
219
 
    logging.debug("Checking out directories in the jail")
220
 
    try:
221
 
        svn.checkout(conf.svn_addr + login + "/stuff",
222
 
                     common.studpath.url_to_local(login + "/stuff")[1])
223
 
        os.system("chown -R %d:%d %s" \
224
 
                % (user.unixid, user.unixid,
225
 
                   common.studpath.url_to_local(login + "/stuff")[1]))
226
 
    except Exception, exc:
227
 
        logging.warning("While mkdiring stuff: %s" % str(exc))
228
 
        pass
229
 
    try:
230
 
        svn.checkout(conf.svn_addr + login + "/info1",
231
 
                     common.studpath.url_to_local(login + "/info1")[1])
232
 
        os.system("chown -R %d:%d %s" \
233
 
                % (user.unixid, user.unixid,
234
 
                   common.studpath.url_to_local(login + "/info1")[1]))
235
 
    except Exception, exc:
236
 
        logging.warning("While mkdiring info1: %s" % str(exc))
237
 
        pass
238
 
 
239
 
    # FIXME: </hack>
240
 
 
241
 
    return {"response": "okay"}
242
 
 
243
223
actions = {
244
224
        'create_user':create_user,
245
225
        'update_user':update_user,
246
226
        'activate_user':activate_user,
247
 
        'do_checkout':do_checkout,
248
227
    }
249
228
 
250
 
def initializer():
251
 
    try:
252
 
        pidfile = open('/var/run/usrmgt-server.pid', 'w')
253
 
        pidfile.write('%d\n' % os.getpid())
254
 
        pidfile.close()
255
 
    except IOError, (errno, strerror):
256
 
        print "Couldn't write PID file. IO error(%s): %s" % (errno, strerror)
257
 
        sys.exit(1)
258
 
 
259
229
def dispatch(props):
260
230
    logging.debug(repr(props))
261
231
    action = props.keys()[0]
273
243
    logging.basicConfig(filename="/var/log/usrmgt.log", level=logging.INFO)
274
244
    logging.info("Starting usrmgt server on port %d (pid = %d)" % (port, pid))
275
245
 
276
 
    common.chat.start_server(port, magic, True, dispatch, initializer)
 
246
    common.chat.start_server(port, magic, True, dispatch)