/usr/share/pyshared/pywebdav/lib/locks.py is in python-webdav 0.9.8-7.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 | import os
import sys
import time
import socket
import string
import posixpath
import base64
import urlparse
import urllib
import random
import logging
log = logging.getLogger(__name__)
import xml.dom
from xml.dom import minidom
from utils import rfc1123_date, IfParser, tokenFinder
from string import atoi,split
from errors import *
tokens_to_lock = {}
uris_to_token = {}
class LockManager:
""" Implements the locking backend and serves as MixIn for DAVRequestHandler """
def _init_locks(self):
return tokens_to_lock, uris_to_token
def _l_isLocked(self, uri):
tokens, uris = self._init_locks()
return uris.has_key(uri)
def _l_hasLock(self, token):
tokens, uris = self._init_locks()
return tokens.has_key(token)
def _l_getLockForUri(self, uri):
tokens, uris = self._init_locks()
return uris.get(uri, None)
def _l_getLock(self, token):
tokens, uris = self._init_locks()
return tokens.get(token, None)
def _l_delLock(self, token):
tokens, uris = self._init_locks()
if tokens.has_key(token):
del uris[tokens[token].uri]
del tokens[token]
def _l_setLock(self, lock):
tokens, uris = self._init_locks()
tokens[lock.token] = lock
uris[lock.uri] = lock
def _lock_unlock_parse(self, body):
doc = minidom.parseString(body)
data = {}
info = doc.getElementsByTagNameNS('DAV:', 'lockinfo')[0]
data['lockscope'] = info.getElementsByTagNameNS('DAV:', 'lockscope')[0]\
.firstChild.localName
data['locktype'] = info.getElementsByTagNameNS('DAV:', 'locktype')[0]\
.firstChild.localName
data['lockowner'] = info.getElementsByTagNameNS('DAV:', 'owner')
return data
def _lock_unlock_create(self, uri, creator, depth, data):
lock = LockItem(uri, creator, **data)
iscollection = uri[-1] == '/' # very dumb collection check
result = ''
if depth == 'infinity' and iscollection:
# locking of children/collections not yet supported
pass
if not self._l_isLocked(uri):
self._l_setLock(lock)
# because we do not handle children we leave result empty
return lock.token, result
def do_UNLOCK(self):
""" Unlocks given resource """
dc = self.IFACE_CLASS
if self._config.DAV.getboolean('verbose') is True:
log.info('UNLOCKing resource %s' % self.headers)
uri = urlparse.urljoin(self.get_baseuri(dc), self.path)
uri = urllib.unquote(uri)
# check lock token - must contain a dash
if not self.headers.get('Lock-Token', '').find('-')>0:
return self.send_status(400)
token = tokenFinder(self.headers.get('Lock-Token'))
if self._l_isLocked(uri):
self._l_delLock(token)
self.send_body(None, '204', 'Ok', 'Ok')
def do_LOCK(self):
""" Locking is implemented via in-memory caches. No data is written to disk. """
dc = self.IFACE_CLASS
log.info('LOCKing resource %s' % self.headers)
body = None
if self.headers.has_key('Content-Length'):
l = self.headers['Content-Length']
body = self.rfile.read(atoi(l))
depth = self.headers.get('Depth', 'infinity')
uri = urlparse.urljoin(self.get_baseuri(dc), self.path)
uri = urllib.unquote(uri)
log.info('do_LOCK: uri = %s' % uri)
ifheader = self.headers.get('If')
alreadylocked = self._l_isLocked(uri)
log.info('do_LOCK: alreadylocked = %s' % alreadylocked)
if body and alreadylocked:
# Full LOCK request but resource already locked
self.responses[423] = ('Locked', 'Already locked')
return self.send_status(423)
elif body and not ifheader:
# LOCK with XML information
data = self._lock_unlock_parse(body)
token, result = self._lock_unlock_create(uri, 'unknown', depth, data)
if result:
self.send_body(result, '207', 'Error', 'Error',
'text/xml; charset="utf-8"')
else:
lock = self._l_getLock(token)
self.send_body(lock.asXML(), '200', 'OK', 'OK',
'text/xml; charset="utf-8"',
{'Lock-Token' : '<opaquelocktoken:%s>' % token})
else:
# refresh request - refresh lock timeout
taglist = IfParser(ifheader)
found = 0
for tag in taglist:
for listitem in tag.list:
token = tokenFinder(listitem)
if token and self._l_hasLock(token):
lock = self._l_getLock(token)
timeout = self.headers.get('Timeout', 'Infinite')
lock.setTimeout(timeout) # automatically refreshes
found = 1
self.send_body(lock.asXML(),
'200', 'OK', 'OK', 'text/xml; encoding="utf-8"')
break
if found:
break
# we didn't find any of the tokens mentioned - means
# that table was cleared or another error
if not found:
self.send_status(412) # precondition failed
class LockItem:
""" Lock with support for exclusive write locks. Some code taken from
webdav.LockItem from the Zope project. """
def __init__(self, uri, creator, lockowner, depth=0, timeout='Infinite',
locktype='write', lockscope='exclusive', token=None, **kw):
self.uri = uri
self.creator = creator
self.owner = lockowner
self.depth = depth
self.timeout = timeout
self.locktype = locktype
self.lockscope = lockscope
self.token = token and token or self.generateToken()
self.modified = time.time()
def getModifiedTime(self):
return self.modified
def refresh(self):
self.modified = time.time()
def isValid(self):
now = time.time()
modified = self.modified
timeout = self.timeout
return (modified + timeout) > now
def generateToken(self):
_randGen = random.Random(time.time())
return '%s-%s-00105A989226:%.03f' % \
(_randGen.random(),_randGen.random(),time.time())
def getTimeoutString(self):
t = str(self.timeout)
if t[-1] == 'L': t = t[:-1]
return 'Second-%s' % t
def setTimeout(self, timeout):
self.timeout = timeout
self.modified = time.time()
def asXML(self, namespace='d', discover=False):
owner_str = ''
if isinstance(self.owner, str):
owner_str = self.owner
elif isinstance(self.owner, xml.dom.minicompat.NodeList):
owner_str = "".join([node.toxml() for node in self.owner[0].childNodes])
token = self.token
base = ('<%(ns)s:activelock>\n'
' <%(ns)s:locktype><%(ns)s:%(locktype)s/></%(ns)s:locktype>\n'
' <%(ns)s:lockscope><%(ns)s:%(lockscope)s/></%(ns)s:lockscope>\n'
' <%(ns)s:depth>%(depth)s</%(ns)s:depth>\n'
' <%(ns)s:owner>%(owner)s</%(ns)s:owner>\n'
' <%(ns)s:timeout>%(timeout)s</%(ns)s:timeout>\n'
' <%(ns)s:locktoken>\n'
' <%(ns)s:href>opaquelocktoken:%(locktoken)s</%(ns)s:href>\n'
' </%(ns)s:locktoken>\n'
' </%(ns)s:activelock>\n'
) % {
'ns': namespace,
'locktype': self.locktype,
'lockscope': self.lockscope,
'depth': self.depth,
'owner': owner_str,
'timeout': self.getTimeoutString(),
'locktoken': token,
}
if discover is True:
return base
s = """<?xml version="1.0" encoding="utf-8" ?>
<d:prop xmlns:d="DAV:">
<d:lockdiscovery>
%s
</d:lockdiscovery>
</d:prop>""" % base
return s
|