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

« back to all changes in this revision

Viewing changes to scripts/usrmgt-server

  • Committer: mattgiuca
  • Date: 2008-06-16 12:31:27 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:780
dispatch/login.py: When displaying the login page, returns a 200, not a 403
    Forbidden.
    This is because "Forbidden" has the wrong semantic - it means "you will
    never get in here even if you auth", whereas we want to present the
    opportunity to auth.
    Some user agents interpret a 403 as defeat, and don't even present the
    page body.

Show diffs side-by-side

added added

removed removed

Lines of Context:
7
7
import urllib
8
8
from functools import partial
9
9
import traceback
 
10
import logging
 
11
import errno
10
12
 
11
13
import conf
12
14
import common.db
33
35
#   - Rebuild svn auth file
34
36
#   - Rebuild passwd + push to nodes.
35
37
 
36
 
DEBUG_PRINT = True
37
 
 
38
38
def create_user(props):
39
39
    """Create the database record for the given user.
40
40
       Expected properties:
101
101
def get_login(login, passwd, _realm, _login, _may_save):
102
102
    """Callback function used by pysvn for authentication.
103
103
    """
104
 
    log("Getting password for %s (realm %s)" % (login, _realm))
 
104
    logging.debug("Getting password for %s (realm %s)" % (login, _realm))
105
105
    return (True, login, passwd, False)
106
106
 
107
107
def activate_user(props):
125
125
 
126
126
        # make svn config/auth
127
127
 
128
 
        log("Creating repo")
 
128
        logging.debug("Creating repo")
129
129
        common.makeuser.make_svn_repo(login, throw_on_error=False)
130
 
        log("Creating svn config")
 
130
        logging.debug("Creating svn config")
131
131
        common.makeuser.make_svn_config(login, throw_on_error=False)
132
 
        log("Creating svn auth")
 
132
        logging.debug("Creating svn auth")
133
133
        passwd = common.makeuser.make_svn_auth(login, throw_on_error=False)
134
 
        log("passwd: %s" % passwd)
 
134
        logging.debug("passwd: %s" % passwd)
135
135
 
136
136
        svn = pysvn.Client()
137
137
        svn.callback_get_login = partial(get_login, login, passwd)
145
145
        #        offerings.
146
146
        #        Instead, we're just going to use a single offering with
147
147
        #        a "short" name of "info1".
148
 
        log("Creating /info1")
 
148
        logging.debug("Creating /info1")
149
149
        try:
150
150
            svn.mkdir(conf.svn_addr + login + "/info1",
151
151
                      "Initial creation of work directory for Informatics 1")
152
152
        except Exception, exc:
153
 
            log("While mkdiring info1: %s" % str(exc))
154
 
            pass
155
 
        log("Creating /submissions")
156
 
        try:
157
 
            svn.mkdir(conf.svn_addr + login + "/submissions",
158
 
                      "Initial creation of submissions directory")
159
 
        except Exception, exc:
160
 
            log("While mkdiring submissions: %s" % str(exc))
161
 
            pass
162
 
        log("Creating /stuff")
 
153
            logging.warning("While mkdiring info1: %s" % str(exc))
 
154
            pass
 
155
        logging.debug("Creating /stuff")
163
156
        try:
164
157
            svn.mkdir(conf.svn_addr + login + "/stuff",
165
158
                      "Initial creation of directory for miscellania")
166
159
        except Exception, exc:
167
 
            log("While mkdiring stuff: %s" % str(exc))
168
 
            pass
169
 
 
170
 
        log("Creating jail")
171
 
        common.makeuser.make_jail(login, details.unixid)
172
 
 
173
 
        # FIXME: <hack>
174
 
 
175
 
        tcf_path = os.path.join(conf.jail_base, 'template/opt/ivle/lib/conf/conf.py')
176
 
        cf_path = os.path.join(conf.jail_base, login, 'opt/ivle/lib/conf/conf.py')
177
 
 
178
 
        os.remove(cf_path)
179
 
        cf = open(cf_path, "w")
180
 
        cf.write(open(tcf_path, "r").read())
181
 
        cf.write("# The login name for the owner of the jail\n")
182
 
        cf.write("login = %s\n" % repr(login))
183
 
        cf.write("\n")
184
 
        cf.write("# The subversion-only password for the owner of the jail\n")
185
 
        cf.write("svn_pass = %s\n" % repr(passwd))
186
 
        cf.close()
187
 
 
188
 
        # FIXME: </hack>
189
 
 
190
 
        log("Checking out directories in the jail")
191
 
        try:
192
 
            svn.checkout(conf.svn_addr + login + "/stuff",
193
 
                         common.studpath.url_to_local(login + "/stuff")[1])
194
 
        except Exception, exc:
195
 
            log("While mkdiring stuff: %s" % str(exc))
196
 
            pass
197
 
        try:
198
 
            svn.checkout(conf.svn_addr + login + "/submissions",
199
 
                         common.studpath.url_to_local(login + "/submissions")[1])
200
 
        except Exception, exc:
201
 
            log("While mkdiring submissions: %s" % str(exc))
202
 
            pass
203
 
        try:
204
 
            svn.checkout(conf.svn_addr + login + "/info1",
205
 
                         common.studpath.url_to_local(login + "/info1")[1])
206
 
        except Exception, exc:
207
 
            log("While mkdiring info1: %s" % str(exc))
208
 
            pass
 
160
            logging.warning("While mkdiring stuff: %s" % str(exc))
 
161
            pass
 
162
 
 
163
        logging.debug("Creating jail")
 
164
        common.makeuser.make_jail(login, details.unixid, svn_pass=passwd)
 
165
 
 
166
        do_checkout(props, svn_pass=passwd)
209
167
 
210
168
        # FIXME: should this be nicer?
211
169
        os.system("chown -R %d:%d %s" \
212
170
                % (details.unixid, details.unixid,
213
171
                   common.studpath.url_to_local(login)[1]))
214
172
 
215
 
 
216
 
        log("Enabling user")
 
173
        logging.info("Enabling user")
217
174
        db.update_user(login, state='enabled')
218
175
 
219
176
        return {"response": "okay"}
221
178
    finally:
222
179
        db.close()
223
180
 
 
181
def do_checkout(props, svn_pass=None):
 
182
    """Create the default contents of a user's jail by checking out from the
 
183
    repositories.
 
184
    If any directory already exists, just fail silently (so this is not a
 
185
    "wipe-and-checkout", merely a checkout if it failed or something).
 
186
       Expected properties:
 
187
        login       - the user name for the jail
 
188
                      STRING REQUIRED
 
189
       Return Value: None
 
190
    """
 
191
    login = props['login']
 
192
 
 
193
    # If svn_pass isn't supplied, grab it from the DB
 
194
    if svn_pass is None:
 
195
        db = common.db.DB()
 
196
        user = db.get_user(login)
 
197
        svn_pass = user.svn_pass
 
198
        db.close()
 
199
 
 
200
    svn = pysvn.Client()
 
201
    svn.callback_get_login = partial(get_login, login, svn_pass)
 
202
 
 
203
    if conf.svn_addr[-1] != os.sep:
 
204
        conf.svn_addr += os.sep
 
205
 
 
206
    # FIXME: <hack>
 
207
 
 
208
    logging.debug("Checking out directories in the jail")
 
209
    try:
 
210
        svn.checkout(conf.svn_addr + login + "/stuff",
 
211
                     common.studpath.url_to_local(login + "/stuff")[1])
 
212
        os.system("chown -R %d:%d %s" \
 
213
                % (user.unixid, user.unixid,
 
214
                   common.studpath.url_to_local(login + "/stuff")[1]))
 
215
    except Exception, exc:
 
216
        logging.warning("While mkdiring stuff: %s" % str(exc))
 
217
        pass
 
218
    try:
 
219
        svn.checkout(conf.svn_addr + login + "/info1",
 
220
                     common.studpath.url_to_local(login + "/info1")[1])
 
221
        os.system("chown -R %d:%d %s" \
 
222
                % (user.unixid, user.unixid,
 
223
                   common.studpath.url_to_local(login + "/info1")[1]))
 
224
    except Exception, exc:
 
225
        logging.warning("While mkdiring info1: %s" % str(exc))
 
226
        pass
 
227
 
 
228
    # FIXME: </hack>
 
229
 
 
230
    return {"response": "okay"}
 
231
 
224
232
actions = {
225
233
        'create_user':create_user,
226
234
        'update_user':update_user,
227
235
        'activate_user':activate_user,
 
236
        'do_checkout':do_checkout,
228
237
    }
229
238
 
230
 
def log(msg):
231
 
    """Writes a message to stderr, but only if DEBUG_PRINT is True.
232
 
    """
233
 
    global DEBUG_PRINT
234
 
    if DEBUG_PRINT:
235
 
        print >>sys.stderr, msg
 
239
def initializer():
 
240
    try:
 
241
        pidfile = open('/var/run/usrmgt-server.pid', 'w')
 
242
        pidfile.write('%d\n' % os.getpid())
 
243
        pidfile.close()
 
244
    except IOError, (errno, strerror):
 
245
        print "Couldn't write PID file. IO error(%s): %s" % (errno, strerror)
 
246
        sys.exit(1)
236
247
 
237
248
def dispatch(props):
238
 
    log(repr(props))
 
249
    logging.debug(repr(props))
239
250
    action = props.keys()[0]
240
251
    return actions[action](props[action])
241
252
 
242
253
if __name__ == "__main__":
 
254
    if len(sys.argv) <3:
 
255
        print >>sys.stderr, "Usage: usrmgt-server <port> <magic>"
 
256
        sys.exit(1)
243
257
    port = int(sys.argv[1])
244
258
    magic = sys.argv[2]
245
259
 
246
 
    common.chat.start_server(port, magic, False, dispatch)
 
260
    pid = os.getpid()
 
261
 
 
262
    logging.basicConfig(filename="/var/log/usrmgt.log", level=logging.INFO)
 
263
    logging.info("Starting usrmgt server on port %d (pid = %d)" % (port, pid))
 
264
 
 
265
    common.chat.start_server(port, magic, True, dispatch, initializer)