# vim: fileencoding=utf8 # CCS CS Lab Main Utilities # # Douglas Thrift # # $Id$ from BeautifulSoup import BeautifulStoneSoup import cgi import common from django.conf import settings from django.core.mail import mail_admins import ldap import os.path import pytz import re import socket common.SSL_ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..', 'ssl')) ldap.set_option(ldap.OPT_PROTOCOL_VERSION, 3) ldap.set_option(ldap.OPT_X_TLS_CACERTFILE, os.path.join(common.SSL_ROOT, 'ccscert.pem')) ldap.set_option(ldap.OPT_X_TLS_REQUIRE_CERT, ldap.OPT_X_TLS_DEMAND) def full_name(name): return name + '.ccs.ucsb.edu' servers = map(full_name, ('zweihander', 'wireless')) def finger_connection(server = servers[0]): for info in socket.getaddrinfo(server, 'finger', socket.AF_INET, socket.SOCK_STREAM): family, type, protocol, name, address = info try: finger = socket.socket(family, type, protocol) except socket.error, exception: finger = None continue finger.settimeout(0.5) try: finger.connect(address) except socket.error, exception: finger.close() finger = None continue break if finger is None: raise exception finger.settimeout(10.0) return finger.makefile('r+b') scheme = '[A-Za-z][-+.0-9A-Za-z]*' unreserved = '[-._~0-9A-Za-z]' # XXX: extension to allow Latin characters in URIs latin = u'[ÀÁÂÃÄÅÆÇÈÉÊËËÌÍÎÏÐÑÒÓÔÕÖØUÚÛÜÝÞßàáâãäåæçèéêëìíïðñòóô°öùúûüý]' pct_encoded = '(?:%[0-9A-Fa-f]{2}|' + latin + ')' sub_delims = '[!$&\'()*+,;=]' userinfo = '(?:%s|%s|%s|:)*' % (unreserved, pct_encoded, sub_delims) h16 = '[0-9A-Fa-f]{1,4}' dec_octet = '(?:[0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])' ipv4address = r'\.'.join((dec_octet,) * 4) ls32 = '(?:%s:%s|%s)' % (h16, h16, ipv4address) ipv6address = '(?:(?:%s){6}%s)' % (h16, ls32) ipvfuture = 'v[0-9A-Fa-f]+.(?:%s|%s|:)+' % (unreserved, sub_delims) ip_literal = r'\[(?:%s|%s)\]' % (ipv6address, ipvfuture) reg_name = '(?:%s|%s|%s)*' % (unreserved, pct_encoded, sub_delims) host = '(?:%s|%s|%s)' % (ip_literal, ipv4address, reg_name) port = '[0-9]*' authority = '(?:%s@)?%s(?::%s)?' % (userinfo, host, port) pchar = '(?:%s|%s|%s)' % (unreserved, pct_encoded, sub_delims) segment = pchar + '*' path_abempty = '(?:/%s)*' % segment segment_nz = pchar + '+' path_absolute = '/(?:%s(?:/%s)*)?' % (segment_nz, segment) path_rootless = '%s(?:/%s)*' % (segment_nz, segment) hier_part = '(?://%s%s|%s|%s)' % (authority, path_abempty, path_absolute, path_rootless) query = r'(?:%s|/|\?)*' % pchar fragment = r'(?:%s|/|\?)*' % pchar uri = re.compile('%s:%s(?:%s)?(?:#%s)?' % (scheme, hier_part, query, fragment)) del scheme, unreserved, latin, pct_encoded, sub_delims, userinfo, h16, dec_octet, ipv4address, ls32, ipv6address, ipvfuture, ip_literal, reg_name, host, port, authority, pchar, segment, path_abempty, segment_nz, path_absolute, path_rootless, hier_part, query, fragment def htmlify(text): items = '' for item in uri.split(text): if uri.match(item) is not None: items += '' + cgi.escape(item) + '' else: items += cgi.escape(item) return items ldap_uri = ' '.join(map(lambda server: 'ldaps://' + server, servers)) base = 'dc=ccs,dc=ucsb,dc=edu' def ldap_connection(user = None, password = None): connection = ldap.initialize(ldap_uri) connection.simple_bind_s('uid=%s,ou=People,%s' % (user, base) if user is not None else '', password if password is not None else '') return connection is_wireless = socket.gethostname() == 'wireless.ccs.ucsb.edu' def request_mail(request): if not is_wireless: mail_admins('CCS CS Account Request from ' + request.full_name, 'The supposed CCS CS student going by the name %s wants the account %s.\n\n%s/account/requests' % (request.full_name, request.name, settings.CCSCSLAB_CALLBACK_SECURE)) def timezone(when, timezone = pytz.timezone(settings.CCSCSLAB_TIMEZONE)): return (pytz.UTC.localize(when) if when.tzinfo is None else when).astimezone(timezone) def unescape(html): return unicode(BeautifulStoneSoup(html, convertEntities = BeautifulStoneSoup.ALL_ENTITIES))