| 1 | import copy |
| 2 | import ctypes |
| 3 | import errno |
| 4 | import fcntl |
| 5 | import importlib |
| 6 | import json |
| 7 | import os |
| 8 | from pathlib import Path |
| 9 | import re |
| 10 | import secrets |
| 11 | import sqlite3 |
| 12 | import subprocess |
| 13 | import time |
| 14 | import urllib.error |
| 15 | import urllib.request |
| 16 | import uuid |
| 17 | |
| 18 | runs = importlib.import_module('dashboard-run') |
| 19 | Error = runs.Error |
| 20 | PODMAN = ['podman', '--remote', '--url', 'unix:///run/podman/podman.sock'] |
| 21 | |
| 22 | ACTIONS = { |
| 23 | 'state': set(), 'folders.list': {'path'}, |
| 24 | 'account.create': {'name'}, 'account.update': {'id', 'name'}, |
| 25 | 'account.rotate': {'id'}, 'account.delete': {'id'}, |
| 26 | 'grant.save': {'id', 'subject', 'path', 'permissions'}, 'grant.delete': {'id'}, |
| 27 | 'link.create': {'name', 'path', 'permissions', 'expires', 'password', 'createFolder'}, |
| 28 | 'link.update': {'key', 'name', 'path', 'permissions', 'expires', 'password', 'mode'}, |
| 29 | 'link.delete': {'key'}, |
| 30 | } |
| 31 | |
| 32 | |
| 33 | def identifier(value): |
| 34 | if not isinstance(value, str): |
| 35 | raise Error(400, 'Choose an account or folder permission from the list.') |
| 36 | try: |
| 37 | if str(uuid.UUID(value)) == value: |
| 38 | return value |
| 39 | except ValueError: |
| 40 | pass |
| 41 | raise Error(400, 'Choose an account or folder permission from the list.') |
| 42 | |
| 43 | |
| 44 | def label(value): |
| 45 | if (not isinstance(value, str) or not value.strip() or len(value) > 200 |
| 46 | or any(ord(c) < 32 or c in '/\\' for c in value) |
| 47 | or value.strip() in {'.', '..'}): |
| 48 | raise Error(400, 'Enter a name under 200 characters without slashes.') |
| 49 | return value.strip() |
| 50 | |
| 51 | |
| 52 | def permissions(value, choices): |
| 53 | if not isinstance(value, str) or not value or set(value) - set(choices): |
| 54 | raise Error(400, 'Choose at least one supported permission.') |
| 55 | return ''.join(c for c in choices if c in value) |
| 56 | |
| 57 | |
| 58 | def atomic(path, data, owner=None): |
| 59 | pending = path.with_name('.' + path.name + '.pending') |
| 60 | fd = os.open(pending, os.O_WRONLY | os.O_CREAT | os.O_TRUNC | os.O_NOFOLLOW, 0o600) |
| 61 | try: |
| 62 | os.fchmod(fd, 0o600) |
| 63 | with os.fdopen(fd, 'wb') as target: |
| 64 | target.write(data) |
| 65 | target.flush() |
| 66 | os.fsync(target.fileno()) |
| 67 | if owner is not None: |
| 68 | os.chown(pending, *owner) |
| 69 | os.replace(pending, path) |
| 70 | finally: |
| 71 | pending.unlink(missing_ok=True) |
| 72 | |
| 73 | |
| 74 | def volumes(base): |
| 75 | sections = re.split(r'(?m)^(?=\[/)', base) |
| 76 | result = [] |
| 77 | for section in sections[1:]: |
| 78 | lines = [line.strip() for line in section.splitlines() if line.strip()] |
| 79 | match = re.fullmatch(r'\[(/[^\r\n]*)\]', lines[0]) |
| 80 | if not match or len(lines) < 2: |
| 81 | raise Error(503, 'Copyparty configuration needs repair before editing shares.') |
| 82 | entry = {'url': match[1].rstrip('/') or '/', 'path': lines[1], 'accs': [], 'flags': []} |
| 83 | target = None |
| 84 | for line in lines[2:]: |
| 85 | if line == 'accs:': |
| 86 | target = entry['accs'] |
| 87 | elif line == 'flags:': |
| 88 | target = entry['flags'] |
| 89 | elif target is not None: |
| 90 | target.append(line) |
| 91 | else: |
| 92 | raise Error(503, 'Copyparty configuration needs repair before editing shares.') |
| 93 | result.append(entry) |
| 94 | return sections[0], result |
| 95 | |
| 96 | |
| 97 | def render(base, state, hashes): |
| 98 | global_config, entries = volumes(base) |
| 99 | accounts = state['accounts'] |
| 100 | if accounts: |
| 101 | global_config += '\n[accounts]\n' + ''.join( |
| 102 | ' api-' + account['id'] + ': ' + hashes['file_api_' + account['id'].replace('-', '')] + '\n' |
| 103 | for account in accounts |
| 104 | ) |
| 105 | api_names = ['api-' + account['id'] for account in accounts] |
| 106 | base_entries = copy.deepcopy(entries) |
| 107 | paths = {grant['path'] for grant in state['grants']} |
| 108 | for entry in base_entries: |
| 109 | if entry['url'] == '/clover' or entry['url'].startswith('/clover/'): |
| 110 | host = str(Path(state['root']) / entry['url'][len('/clover'):].lstrip('/')) |
| 111 | if any(host == grant['path'] or host.startswith(grant['path'].rstrip('/') + '/') for grant in state['grants']): |
| 112 | paths.add(host) |
| 113 | for path in sorted(paths): |
| 114 | relative = Path(path).relative_to(state['root']) |
| 115 | url = '/clover' + ('/' + relative.as_posix() if relative.parts else '') |
| 116 | parent = max((entry for entry in base_entries if url == entry['url'] or url.startswith(entry['url'].rstrip('/') + '/')), |
| 117 | key=lambda entry: len(entry['url'])) |
| 118 | exact = next((entry for entry in entries if entry['url'] == url), None) |
| 119 | entry = exact if exact is not None else { |
| 120 | 'url': url, 'path': '/w/clover' + ('/' + relative.as_posix() if relative.parts else ''), |
| 121 | 'accs': list(parent['accs']), 'flags': list(parent['flags']), |
| 122 | } |
| 123 | for grant in state['grants']: |
| 124 | if path == grant['path'] or path.startswith(grant['path'].rstrip('/') + '/'): |
| 125 | kind, subject = grant['subject'].split(':', 1) |
| 126 | principal = '@snow-user-' + subject if kind == 'user' else 'api-' + subject |
| 127 | entry['accs'].append(grant['permissions'] + ': ' + principal) |
| 128 | if 'xvol' not in entry['flags']: |
| 129 | entry['flags'].append('xvol') |
| 130 | if exact is None: |
| 131 | entries.append(entry) |
| 132 | rendered = global_config |
| 133 | for entry in entries: |
| 134 | allowed = {} |
| 135 | if entry['url'] == '/clover' or entry['url'].startswith('/clover/'): |
| 136 | host = str(Path(state['root']) / entry['url'][len('/clover'):].lstrip('/')) |
| 137 | for grant in state['grants']: |
| 138 | if grant['subject'].startswith('api:') and (host == grant['path'] or host.startswith(grant['path'].rstrip('/') + '/')): |
| 139 | for perm in grant['permissions']: |
| 140 | allowed.setdefault(perm, set()).add('api-' + grant['subject'][4:]) |
| 141 | access = [] |
| 142 | for line in entry['accs']: |
| 143 | perms, names = line.split(':', 1) |
| 144 | names = [name.strip() for name in names.split(',')] |
| 145 | if api_names and ('*' in names or '@acct' in names): |
| 146 | perms = perms.replace('A', 'rwmda.') |
| 147 | for perm in dict.fromkeys(perms): |
| 148 | denied = [name for name in api_names if name not in allowed.get(perm, set())] |
| 149 | access.append(perm + ': ' + ','.join(names + ['-' + name for name in denied])) |
| 150 | else: |
| 151 | access.append(line) |
| 152 | entry['accs'] = access |
| 153 | rendered += '\n[' + entry['url'] + ']\n ' + entry['path'] + '\n accs:\n' |
| 154 | rendered += ''.join(' ' + line + '\n' for line in entry['accs']) |
| 155 | if entry['flags']: |
| 156 | rendered += ' flags:\n' + ''.join(' ' + line + '\n' for line in entry['flags']) |
| 157 | return rendered |
| 158 | |
| 159 | |
| 160 | def configuration(cfg, state, items, source=None): |
| 161 | source = source or Path(__file__).with_name('copyparty.conf') |
| 162 | base = source.read_text() |
| 163 | proof = items.get('idp_header', '') |
| 164 | if not re.fullmatch(r'[0-9a-f]{32}', proof): |
| 165 | raise Error(503, 'Copyparty sign-in configuration is unavailable. Redeploy the service.') |
| 166 | capacity = os.statvfs(cfg) |
| 167 | reserve = max(1, min(16, capacity.f_blocks * capacity.f_frsize // (4 * 10**9))) |
| 168 | base = base.replace('SNOWGLOBE_IDP_HEADER', 'X-Snowglobe-Idp-' + proof).replace('SNOWGLOBE_MIN_FREE_GB', str(reserve)) |
| 169 | return render(base, state, items).encode() |
| 170 | |
| 171 | |
| 172 | def remaining(deadline, maximum=10): |
| 173 | timeout = min(maximum, deadline - time.monotonic()) |
| 174 | if timeout <= 0: |
| 175 | raise Error(504, 'Copyparty took too long to respond. Check the service before retrying.') |
| 176 | return timeout |
| 177 | |
| 178 | |
| 179 | def rename(source, destination): |
| 180 | # RENAME_NOREPLACE keeps a concurrently created destination from being overwritten. |
| 181 | libc = ctypes.CDLL(None, use_errno=True) |
| 182 | if hasattr(libc, 'renameat2'): |
| 183 | result = libc.renameat2(-100, os.fsencode(source), -100, os.fsencode(destination), 1) |
| 184 | elif hasattr(libc, 'renamex_np'): |
| 185 | result = libc.renamex_np(os.fsencode(source), os.fsencode(destination), 4) |
| 186 | else: |
| 187 | raise Error(503, 'This host cannot safely move folders. Retarget the share instead.') |
| 188 | if result: |
| 189 | code = ctypes.get_errno() |
| 190 | if code == errno.EEXIST: |
| 191 | raise Error(409, 'The destination already exists. Choose a new folder name.') |
| 192 | if code == errno.EXDEV: |
| 193 | raise Error(409, 'Choose a destination on the same filesystem to move this folder.') |
| 194 | raise OSError(code, os.strerror(code)) |
| 195 | |
| 196 | |
| 197 | class Manager: |
| 198 | def __init__(self, users): |
| 199 | root = Path(os.environ.get('SNOWGLOBE_ROOT', '/srv')) |
| 200 | self.root = Path(os.environ.get('SNOWGLOBE_CLOVER_ROOT', str(root / 'clover'))).resolve() |
| 201 | self.cfg = Path(os.environ.get('SNOWGLOBE_COPYPARTY_CFG', str(root / 'prod/copyparty/cfg'))) |
| 202 | self.native = self.cfg / 'copyparty' |
| 203 | self.service = os.environ.get('SNOWGLOBE_COPYPARTY_SERVICE', 'copyparty') |
| 204 | self.users = users |
| 205 | self.path = self.cfg / 'manager.json' |
| 206 | self.changed = set() |
| 207 | self.undo = [] |
| 208 | self.secret_before = {} |
| 209 | self.container_id = None |
| 210 | self.db = None |
| 211 | self.job = None |
| 212 | self.stopped = False |
| 213 | self.offline_at = None |
| 214 | self.deadline = None |
| 215 | |
| 216 | def path_for(self, value, missing=False): |
| 217 | if not isinstance(value, str) or len(value) > 4096 or any(ord(c) < 32 or c in '#[]\\' for c in value) or ' ' in value: |
| 218 | raise Error(400, 'Choose a folder without control characters, brackets, or repeated spaces.') |
| 219 | if value == '/Volumes/clover' or value.startswith('/Volumes/clover/'): |
| 220 | value = str(self.root) + value[len('/Volumes/clover'):] |
| 221 | path = Path(value) |
| 222 | if not path.is_absolute() or '..' in path.parts or not path.is_relative_to(self.root): |
| 223 | raise Error(400, 'Choose a folder inside the Clover file store.') |
| 224 | current = self.root |
| 225 | try: |
| 226 | for part in path.relative_to(self.root).parts: |
| 227 | current /= part |
| 228 | if current.is_symlink(): |
| 229 | raise Error(400, 'Choose a folder directly instead of a symbolic link.') |
| 230 | if not missing and not path.is_dir(): |
| 231 | raise Error(404, 'That folder no longer exists. Choose another folder.') |
| 232 | if missing and not path.parent.is_dir(): |
| 233 | raise Error(404, 'The parent folder does not exist. Choose an existing parent.') |
| 234 | if path.exists() and not path.is_dir(): |
| 235 | raise Error(400, 'Choose a folder instead of a file.') |
| 236 | except OSError: |
| 237 | raise Error(400, 'That folder cannot be opened. Choose another folder.') from None |
| 238 | return path |
| 239 | |
| 240 | def container(self): |
| 241 | if self.container_id: |
| 242 | return self.container_id |
| 243 | result = subprocess.run([*PODMAN, 'ps', '--format', '{{.ID}} {{.Image}}'], capture_output=True, text=True, timeout=10, check=True) |
| 244 | for row in result.stdout.splitlines(): |
| 245 | fields = row.split() |
| 246 | if len(fields) != 2 or '/copyparty/' not in fields[1]: |
| 247 | continue |
| 248 | result = subprocess.run([*PODMAN, 'inspect', '--format', '{{json .Mounts}}', fields[0]], |
| 249 | capture_output=True, text=True, timeout=10, check=True) |
| 250 | mounts = {mount['Destination']: mount['Source'] for mount in json.loads(result.stdout)} |
| 251 | if mounts.get('/cfg') == str(self.cfg): |
| 252 | if mounts.get('/w/clover') != str(self.root): |
| 253 | raise Error(503, 'The share manager points at a different file store. Check its host configuration.') |
| 254 | self.container_id = fields[0] |
| 255 | return self.container_id |
| 256 | raise Error(503, 'Copyparty is offline. Start it before editing shares.') |
| 257 | |
| 258 | def hash(self, password): |
| 259 | if not isinstance(password, str) or len(password) > 255 or any(c in password for c in '\r\n\0'): |
| 260 | raise Error(400, 'Use a single-line password under 256 characters.') |
| 261 | if not password: |
| 262 | return '' |
| 263 | script = ('import argparse,sys; from pathlib import Path; from copyparty.pwhash import PWHash; ' |
| 264 | 'args=argparse.Namespace(ah_alg="argon2",ah_salt=Path("/cfg/copyparty/ah-salt.txt").read_text().strip()); ' |
| 265 | 'print(PWHash(args).hash(sys.stdin.read()))') |
| 266 | result = subprocess.run([*PODMAN, 'exec', '-i', self.container(), 'python3', '-c', script], |
| 267 | input=password, capture_output=True, text=True, timeout=25) |
| 268 | value = result.stdout.strip() |
| 269 | if result.returncode or not re.fullmatch(r'\+[A-Za-z0-9_-]{32}', value): |
| 270 | raise Error(502, 'The password could not be secured. Check Copyparty and try again.') |
| 271 | return value |
| 272 | |
| 273 | def reload(self, items, timeout=30): |
| 274 | deadline = time.monotonic() + timeout |
| 275 | instances = runs.nomad('service/' + self.service, timeout=remaining(deadline)) or [] |
| 276 | if len(instances) != 1: |
| 277 | raise Error(503, 'Copyparty is offline or changing allocations. Try again shortly.') |
| 278 | address, port = instances[0]['Address'], instances[0]['Port'] |
| 279 | if not re.fullmatch(r'[0-9a-fA-F:.]+', address) or type(port) is not int or not 1 <= port <= 65535: |
| 280 | raise Error(503, 'Copyparty address is unavailable. Redeploy its service.') |
| 281 | address = '[' + address + ']' if ':' in address else address |
| 282 | request = urllib.request.Request('http://' + address + ':' + str(port) + '/?reload=cfg', headers={ |
| 283 | 'User-Name': 'snow', 'User-Groups': '', 'X-Snowglobe-Idp-' + items['idp_header']: '1', |
| 284 | }) |
| 285 | try: |
| 286 | with urllib.request.urlopen(request, timeout=remaining(deadline, 30)) as response: |
| 287 | body = response.read(262144) |
| 288 | if response.status != 200 or b'config has been reloaded' not in body: |
| 289 | raise Error(502, 'Copyparty did not confirm the update. Try again after checking the service.') |
| 290 | except (urllib.error.URLError, TimeoutError): |
| 291 | raise Error(502, 'Copyparty could not apply the update. Check the service and try again.') from None |
| 292 | |
| 293 | def stop(self): |
| 294 | if self.offline_at is None: |
| 295 | self.offline_at = time.monotonic() |
| 296 | if self.deadline is None: |
| 297 | self.deadline = self.offline_at + 160 |
| 298 | deadline = min(self.deadline, time.monotonic() + 40) |
| 299 | if self.job is None: |
| 300 | self.job = runs.nomad('job/' + self.service, timeout=remaining(deadline)) |
| 301 | if self.job is None: |
| 302 | raise Error(503, 'Copyparty is offline. Start it before moving folders.') |
| 303 | self.job['Stop'] = False |
| 304 | runs.nomad('job/' + self.service + '?purge=false', 'DELETE', timeout=remaining(deadline)) |
| 305 | self.stopped = True |
| 306 | while time.monotonic() < deadline: |
| 307 | allocations = runs.nomad('job/' + self.service + '/allocations', timeout=remaining(deadline)) or [] |
| 308 | if all(entry.get('ClientStatus') in {'complete', 'failed', 'lost'} for entry in allocations): |
| 309 | self.container_id = None |
| 310 | return |
| 311 | time.sleep(0.5) |
| 312 | raise Error(502, 'Copyparty did not stop in time. Check the service before retrying the move.') |
| 313 | |
| 314 | def start(self): |
| 315 | if self.job is None: |
| 316 | raise Error(503, 'Copyparty restart settings are unavailable. Redeploy its service.') |
| 317 | deadline = min(self.deadline, time.monotonic() + 70) |
| 318 | runs.nomad('jobs', 'POST', {'Job': self.job}, timeout=remaining(deadline)) |
| 319 | items = getattr(self, 'items', None) |
| 320 | if items is None: |
| 321 | variable = runs.nomad('var/nomad/jobs/' + self.service, timeout=remaining(deadline)) |
| 322 | if variable is None: |
| 323 | raise Error(503, 'Copyparty secrets are unavailable. Redeploy its service.') |
| 324 | items = variable['Items'] |
| 325 | while time.monotonic() < deadline: |
| 326 | try: |
| 327 | self.reload(items, timeout=remaining(deadline, 30)) |
| 328 | self.stopped = False |
| 329 | return |
| 330 | except Error as error: |
| 331 | if error.status not in {502, 503}: |
| 332 | raise |
| 333 | time.sleep(0.5) |
| 334 | raise Error(502, 'Copyparty could not restart in time. Check its service logs before retrying.') |
| 335 | |
| 336 | def virtual(self, path): |
| 337 | return 'clover' + ('/' + path.relative_to(self.root).as_posix() if path != self.root else '') |
| 338 | |
| 339 | def real(self, virtual): |
| 340 | if virtual == 'clover' or virtual.startswith('clover/'): |
| 341 | return self.root / virtual[len('clover'):].lstrip('/') |
| 342 | if virtual == 'media' or virtual.startswith('media/'): |
| 343 | media = Path(os.environ.get('SNOWGLOBE_MEDIA_ROOT', str(self.root / 'Media'))) |
| 344 | return media / virtual[len('media'):].lstrip('/') |
| 345 | return None |
| 346 | |
| 347 | def state(self): |
| 348 | links = [] |
| 349 | for key, password, path, perms, count, owner, created, expires in self.db.execute('SELECT * FROM sh ORDER BY t0 DESC,k'): |
| 350 | real = self.real(path) |
| 351 | links.append({'key': key, 'name': self.data['names'].get(key, real.name if real is not None else Path(path).name or key), |
| 352 | 'path': str(real) if real is not None else '/' + path, |
| 353 | 'permissions': perms, 'owner': owner, 'created': created, 'expires': expires, |
| 354 | 'hasPassword': bool(password), 'files': [row[0] for row in self.db.execute('SELECT vp FROM sf WHERE k=? ORDER BY vp', (key,))] if count else []}) |
| 355 | return {'root': str(self.root), 'defaultRoot': str(self.root / 'Share'), |
| 356 | 'accounts': [{**account, 'subject': 'api:' + account['id'], 'username': 'api-' + account['id']} for account in self.data['accounts']], |
| 357 | 'grants': self.data['grants'], 'links': links} |
| 358 | |
| 359 | def secret(self, key, value): |
| 360 | before = self.items.get(key) |
| 361 | if value is None: |
| 362 | self.items.pop(key, None) |
| 363 | else: |
| 364 | self.items[key] = value |
| 365 | self.secret_before[key] = before |
| 366 | |
| 367 | def account(self, value): |
| 368 | value = identifier(value) |
| 369 | account = next((entry for entry in self.data['accounts'] if entry['id'] == value), None) |
| 370 | if account is None: |
| 371 | raise Error(404, 'That API account no longer exists. Reload accounts.') |
| 372 | return account |
| 373 | |
| 374 | def grant(self, payload): |
| 375 | subject = payload['subject'] |
| 376 | if not isinstance(subject, str) or ':' not in subject: |
| 377 | raise Error(400, 'Choose a Snow Sign On user or API account.') |
| 378 | kind, value = subject.split(':', 1) |
| 379 | identifier(value) |
| 380 | if kind == 'api': |
| 381 | self.account(value) |
| 382 | elif kind == 'user': |
| 383 | if not any(user['id'] == value and user.get('enabled') is True and user.get('kind') in {None, 'user'} for user in self.users): |
| 384 | raise Error(400, 'Choose an enabled Snow Sign On user.') |
| 385 | else: |
| 386 | raise Error(400, 'Choose a Snow Sign On user or API account.') |
| 387 | path = str(self.path_for(payload['path'])) |
| 388 | perms = permissions(payload['permissions'], 'rwmda.g') |
| 389 | entry = {'id': identifier(payload['id']) if payload['id'] is not None else str(uuid.uuid4()), |
| 390 | 'subject': subject, 'path': path, 'permissions': perms} |
| 391 | found = next((grant for grant in self.data['grants'] if grant['id'] == entry['id']), None) |
| 392 | if payload['id'] is not None and found is None: |
| 393 | raise Error(404, 'That folder permission no longer exists. Reload permissions.') |
| 394 | if any(grant['id'] != entry['id'] and grant['subject'] == subject and grant['path'] == path for grant in self.data['grants']): |
| 395 | raise Error(409, 'This account already has permissions for that folder. Edit its existing permission.') |
| 396 | self.data['grants'] = [grant for grant in self.data['grants'] if grant['id'] != entry['id']] + [entry] |
| 397 | |
| 398 | def owner_permissions(self, owner, path, required): |
| 399 | _, entries = volumes(configuration(self.cfg, self.data, self.items).decode()) |
| 400 | url = '/' + self.virtual(path) |
| 401 | entry = max((entry for entry in entries if url == entry['url'] or url.startswith(entry['url'].rstrip('/') + '/')), |
| 402 | key=lambda entry: len(entry['url'])) |
| 403 | groups = {'acct'} |
| 404 | user = next((user for user in self.users if user.get('username') == owner), None) |
| 405 | if user is not None: |
| 406 | if user.get('enabled') is not True or user.get('kind') not in {None, 'user'} or user.get('requiredActions'): |
| 407 | raise Error(409, 'The share owner cannot sign in. Enable their account before editing this share.') |
| 408 | groups.update(user.get('groups', [])) |
| 409 | groups.add('snow-user-' + identifier(user['id'])) |
| 410 | effective = set() |
| 411 | excluded_permissions = set() |
| 412 | for rule in entry['accs']: |
| 413 | perms, principals = rule.split(':', 1) |
| 414 | names = {name.strip() for name in principals.split(',')} |
| 415 | selected = owner in names or '*' in names or any('@' + group in names for group in groups) |
| 416 | excluded = '-' + owner in names or any('-@' + group in names for group in groups) |
| 417 | expanded = perms.replace('A', 'rwmda.') |
| 418 | if selected: |
| 419 | effective.update(expanded) |
| 420 | if excluded: |
| 421 | excluded_permissions.update(expanded) |
| 422 | if set(required) - (effective - excluded_permissions): |
| 423 | raise Error(409, 'The share owner needs these permissions at the destination. Grant them access before changing this share.') |
| 424 | |
| 425 | def link(self, action, payload): |
| 426 | key = payload['key'] if action == 'link.update' else secrets.token_urlsafe(12) |
| 427 | if not isinstance(key, str) or not re.fullmatch(r'[A-Za-z0-9_-]{1,200}', key): |
| 428 | raise Error(400, 'Choose a share from the list.') |
| 429 | old = self.db.execute('SELECT * FROM sh WHERE k=?', (key,)).fetchone() |
| 430 | if action == 'link.update' and old is None: |
| 431 | raise Error(404, 'That share no longer exists. Reload shares.') |
| 432 | name = label(payload['name']) |
| 433 | perms = permissions(payload['permissions'], 'rwmd') |
| 434 | expires = payload['expires'] |
| 435 | if type(expires) is not int or expires < 0 or expires > 2**53: |
| 436 | raise Error(400, 'Choose an expiration date or keep the share indefinitely.') |
| 437 | password = payload['password'] |
| 438 | if password is not None and not isinstance(password, str): |
| 439 | raise Error(400, 'Enter a password or leave the existing password unchanged.') |
| 440 | if password is None: |
| 441 | password = old[1] if old is not None else '' |
| 442 | else: |
| 443 | password = self.hash(password) |
| 444 | create = action == 'link.create' and payload['createFolder'] is True |
| 445 | if action == 'link.create' and type(payload['createFolder']) is not bool: |
| 446 | raise Error(400, 'Choose an existing folder or create a new one.') |
| 447 | destination = payload['path'] |
| 448 | share_root = self.root / 'Share' |
| 449 | if create and (not destination or Path(destination).parent == share_root or str(destination).startswith('/Volumes/clover/Share/')): |
| 450 | self.path_for(str(share_root), missing=True) |
| 451 | if not share_root.exists(): |
| 452 | share_root.mkdir(mode=0o775) |
| 453 | share_root.chmod(0o775) |
| 454 | owner = self.root.stat() |
| 455 | os.chown(share_root, owner.st_uid, owner.st_gid) |
| 456 | self.undo.append(lambda: share_root.rmdir()) |
| 457 | if not destination: |
| 458 | destination = str(share_root / name) |
| 459 | destination = self.path_for(destination, missing=create or action == 'link.update' and payload['mode'] == 'move') |
| 460 | files = [row[0] for row in self.db.execute('SELECT vp FROM sf WHERE k=?', (key,))] if old is not None and old[4] else [] |
| 461 | mode = payload.get('mode', 'retarget') |
| 462 | if mode not in {'retarget', 'move'}: |
| 463 | raise Error(400, 'Choose whether to move the folder or use another folder.') |
| 464 | if create: |
| 465 | try: |
| 466 | destination.mkdir(mode=0o775) |
| 467 | destination.chmod(0o775) |
| 468 | except FileExistsError: |
| 469 | raise Error(409, 'That folder already exists. Choose it as an existing folder or change its name.') from None |
| 470 | owner = destination.parent.stat() |
| 471 | os.chown(destination, owner.st_uid, owner.st_gid) |
| 472 | self.undo.append(lambda: destination.rmdir()) |
| 473 | if old is not None and mode == 'move': |
| 474 | source = self.real(old[2]) |
| 475 | if source is None: |
| 476 | raise Error(400, 'Only folders inside the Clover file store can be moved here.') |
| 477 | source = self.path_for(str(source)) |
| 478 | if source != destination: |
| 479 | for grant in self.data['grants']: |
| 480 | if Path(grant['path']).is_relative_to(source): |
| 481 | grant['path'] = str(destination / Path(grant['path']).relative_to(source)) |
| 482 | self.owner_permissions(old[5], destination, perms) |
| 483 | self.move(source, destination) |
| 484 | for filename in files: |
| 485 | if (not isinstance(filename, str) or filename in {'.', '..'} or '/' in filename or '\\' in filename |
| 486 | or not (destination / filename).is_file() or (destination / filename).is_symlink()): |
| 487 | raise Error(409, 'The destination must contain every selected file. Choose a matching folder.') |
| 488 | if old is not None: |
| 489 | self.owner_permissions(old[5], destination, perms) |
| 490 | self.changed.add(key) |
| 491 | self.db.execute('UPDATE sh SET pw=?,vp=?,pr=?,t1=? WHERE k=?', (password, self.virtual(destination), perms, expires, key)) |
| 492 | else: |
| 493 | self.changed.add(key) |
| 494 | self.db.execute('INSERT INTO sh VALUES(?,?,?,?,?,?,?,?)', (key, password, self.virtual(destination), perms, 0, 'snow', int(time.time()), expires)) |
| 495 | self.data['names'][key] = name |
| 496 | |
| 497 | def move(self, source, destination): |
| 498 | if source == self.root or source == self.root / 'Share' or destination.is_relative_to(source) or source.is_relative_to(destination): |
| 499 | raise Error(400, 'Choose separate folders below the Clover file store.') |
| 500 | if destination.exists(): |
| 501 | raise Error(409, 'The destination already exists. Choose a new folder name.') |
| 502 | if source.stat().st_dev != destination.parent.stat().st_dev: |
| 503 | raise Error(409, 'Choose a destination on the same filesystem to move this folder.') |
| 504 | mounts = Path('/proc/self/mountinfo') |
| 505 | if mounts.exists(): |
| 506 | for row in mounts.read_text().splitlines(): |
| 507 | mount = Path(re.sub(r'\\([0-7]{3})', lambda match: chr(int(match[1], 8)), row.split()[4])) |
| 508 | if mount.is_relative_to(source): |
| 509 | raise Error(409, 'A mounted filesystem cannot be moved. Choose a folder without mounts inside it.') |
| 510 | elif os.path.ismount(source): |
| 511 | raise Error(409, 'A mounted filesystem cannot be moved. Choose a folder inside it.') |
| 512 | rows = self.db.execute('SELECT k,vp,st FROM sh').fetchall() |
| 513 | for key, virtual, count in rows: |
| 514 | real = self.real(virtual) |
| 515 | if real is not None and source != real and source.is_relative_to(real): |
| 516 | selected = {row[0] for row in self.db.execute('SELECT vp FROM sf WHERE k=?', (key,))} if count else None |
| 517 | if selected is None or source.relative_to(real).parts[0] in selected: |
| 518 | raise Error(409, 'Another share includes this folder through its parent. Retarget that share before moving the folder.') |
| 519 | self.stop() |
| 520 | for key, virtual, _ in rows: |
| 521 | real = self.real(virtual) |
| 522 | if real is not None and real.is_relative_to(source): |
| 523 | self.changed.add(key) |
| 524 | self.db.execute('UPDATE sh SET vp=? WHERE k=?', (self.virtual(destination / real.relative_to(source)), key)) |
| 525 | for grant in self.data['grants']: |
| 526 | if Path(grant['path']).is_relative_to(source): |
| 527 | grant['path'] = str(destination / Path(grant['path']).relative_to(source)) |
| 528 | rename(source, destination) |
| 529 | self.undo.append(lambda: rename(destination, source)) |
| 530 | |
| 531 | def dispatch(self, action, payload): |
| 532 | extra = {} |
| 533 | if action.startswith('account.'): |
| 534 | account = {'id': str(uuid.uuid4()), 'name': label(payload['name'])} if action == 'account.create' else self.account(payload['id']) |
| 535 | value = account['id'] |
| 536 | if action == 'account.create': |
| 537 | if any(user.get('username') == 'api-' + value for user in self.users): |
| 538 | raise Error(409, 'That API username already belongs to a Snow Sign On user. Try creating the account again.') |
| 539 | self.data['accounts'].append(account) |
| 540 | if action in {'account.update', 'account.create'}: |
| 541 | account['name'] = label(payload['name']) |
| 542 | if action in {'account.create', 'account.rotate'}: |
| 543 | password = secrets.token_urlsafe(32) |
| 544 | self.secret('file_api_' + value.replace('-', ''), self.hash(password)) |
| 545 | extra = {'account': {**account, 'subject': 'api:' + value, 'username': 'api-' + value}, 'password': password} |
| 546 | if action in {'account.delete', 'account.rotate'}: |
| 547 | session_path = self.native / 'sessions.db' |
| 548 | if session_path.exists(): |
| 549 | with sqlite3.connect(session_path) as sessions: |
| 550 | sessions.execute('DELETE FROM us WHERE un=?', ('api-' + value,)) |
| 551 | if action == 'account.delete': |
| 552 | self.data['accounts'].remove(account) |
| 553 | self.data['grants'] = [grant for grant in self.data['grants'] if grant['subject'] != 'api:' + value] |
| 554 | self.secret('file_api_' + value.replace('-', ''), None) |
| 555 | elif action == 'grant.save': |
| 556 | self.grant(payload) |
| 557 | elif action == 'grant.delete': |
| 558 | value = identifier(payload['id']) |
| 559 | if not any(grant['id'] == value for grant in self.data['grants']): |
| 560 | raise Error(404, 'That folder permission no longer exists. Reload permissions.') |
| 561 | self.data['grants'] = [grant for grant in self.data['grants'] if grant['id'] != value] |
| 562 | elif action in {'link.create', 'link.update'}: |
| 563 | self.link(action, payload) |
| 564 | elif action == 'link.delete': |
| 565 | key = payload['key'] |
| 566 | if not isinstance(key, str) or not self.db.execute('SELECT 1 FROM sh WHERE k=?', (key,)).fetchone(): |
| 567 | raise Error(404, 'That share no longer exists. Reload shares.') |
| 568 | self.changed.add(key) |
| 569 | self.db.execute('DELETE FROM sh WHERE k=?', (key,)) |
| 570 | self.db.execute('DELETE FROM sf WHERE k=?', (key,)) |
| 571 | self.data['names'].pop(key, None) |
| 572 | return extra |
| 573 | |
| 574 | def apply(self, action, payload): |
| 575 | self.data = json.loads(runs.read(self.path, missing='{"accounts":[],"grants":[],"names":{}}')) |
| 576 | origin = Path(self.data.get('root', os.environ.get('SNOWGLOBE_SOURCE_CLOVER_ROOT', '/srv/clover'))) |
| 577 | if 'root' not in self.data and self.data.get('grants'): |
| 578 | origin = Path(os.environ.get('SNOWGLOBE_SOURCE_CLOVER_ROOT', str(self.root))) |
| 579 | if origin != self.root: |
| 580 | for grant in self.data['grants']: |
| 581 | grant['path'] = str(self.root / Path(grant['path']).relative_to(origin)) |
| 582 | self.data['root'] = str(self.root) |
| 583 | if not isinstance(self.data.get('accounts'), list) or not isinstance(self.data.get('grants'), list) or not isinstance(self.data.get('names'), dict): |
| 584 | raise Error(503, 'Share settings need repair before editing. Check the host service.') |
| 585 | database = self.native / 'shares.db' |
| 586 | if not database.is_file(): |
| 587 | raise Error(503, 'Copyparty shares are not ready. Start Copyparty and try again.') |
| 588 | self.db = sqlite3.connect(database.as_uri() + '?mode=rw', uri=True, timeout=5) |
| 589 | self.db.row_factory = None |
| 590 | try: |
| 591 | version = self.db.execute("SELECT v FROM kv WHERE k='sver'").fetchone() |
| 592 | if version != (2,) or [row[1] for row in self.db.execute('PRAGMA table_info(sh)')] != ['k', 'pw', 'vp', 'pr', 'st', 'un', 't0', 't1']: |
| 593 | raise Error(503, 'This Copyparty share format is unsupported. Update Snowglobe before editing shares.') |
| 594 | if action == 'state': |
| 595 | return self.state() |
| 596 | if action == 'folders.list': |
| 597 | path = self.path_for(payload['path']) |
| 598 | with os.scandir(path) as entries: |
| 599 | directories = sorted(({'name': entry.name, 'path': str(path / entry.name)} for entry in entries |
| 600 | if not entry.name.startswith('.') and entry.is_dir(follow_symlinks=False)), key=lambda entry: entry['name'].casefold()) |
| 601 | return {'path': str(path), 'directories': directories} |
| 602 | self.container() |
| 603 | variable_path = 'nomad/jobs/' + self.service |
| 604 | variable = runs.nomad('var/' + variable_path) |
| 605 | if variable is None: |
| 606 | raise Error(503, 'Copyparty secrets are unavailable. Redeploy its service.') |
| 607 | self.items = dict(variable['Items']) |
| 608 | original_items = dict(self.items) |
| 609 | config_path = self.cfg / 'copyparty.conf' |
| 610 | old_config = config_path.read_bytes() |
| 611 | metadata = config_path.stat() |
| 612 | owner = (metadata.st_uid, metadata.st_gid) |
| 613 | self.db.execute('BEGIN IMMEDIATE') |
| 614 | original_rows = {row[0]: row for row in self.db.execute('SELECT * FROM sh')} |
| 615 | original_files = {key: self.db.execute('SELECT * FROM sf WHERE k=?', (key,)).fetchall() for key in original_rows} |
| 616 | committed = False |
| 617 | wrote_files = False |
| 618 | old_manager = self.path.read_bytes() if self.path.exists() else None |
| 619 | changed_secrets = False |
| 620 | after_rows = {} |
| 621 | try: |
| 622 | extra = self.dispatch(action, payload) |
| 623 | rendered = configuration(self.cfg, self.data, self.items) |
| 624 | if self.secret_before: |
| 625 | runs.nomad('var/' + variable_path + '?cas=' + str(variable['ModifyIndex']), 'PUT', |
| 626 | {'Namespace': 'default', 'Path': variable_path, 'Items': self.items}) |
| 627 | changed_secrets = True |
| 628 | atomic(config_path, rendered, owner) |
| 629 | wrote_files = True |
| 630 | atomic(self.path, json.dumps(self.data, ensure_ascii=False).encode()) |
| 631 | after_rows = {key: self.db.execute('SELECT * FROM sh WHERE k=?', (key,)).fetchone() for key in self.changed} |
| 632 | self.db.commit() |
| 633 | committed = True |
| 634 | if self.job is not None: |
| 635 | self.start() |
| 636 | else: |
| 637 | self.reload(self.items) |
| 638 | return {**self.state(), **extra} |
| 639 | except Exception: |
| 640 | if self.job is not None: |
| 641 | self.deadline = self.offline_at + 280 |
| 642 | self.stop() |
| 643 | self.db.rollback() |
| 644 | if committed and self.changed: |
| 645 | self.db.execute('BEGIN IMMEDIATE') |
| 646 | for key in self.changed: |
| 647 | current = self.db.execute('SELECT * FROM sh WHERE k=?', (key,)).fetchone() |
| 648 | if current != after_rows[key]: |
| 649 | raise Error(502, 'The update could not finish and a share changed during recovery. Check Copyparty before retrying.') from None |
| 650 | self.db.execute('DELETE FROM sh WHERE k=?', (key,)) |
| 651 | self.db.execute('DELETE FROM sf WHERE k=?', (key,)) |
| 652 | if key in original_rows: |
| 653 | self.db.execute('INSERT INTO sh VALUES(?,?,?,?,?,?,?,?)', original_rows[key]) |
| 654 | self.db.executemany('INSERT INTO sf VALUES(?,?)', original_files[key]) |
| 655 | self.db.commit() |
| 656 | for undo in reversed(self.undo): |
| 657 | undo() |
| 658 | if wrote_files: |
| 659 | atomic(config_path, old_config, owner) |
| 660 | if old_manager is None: |
| 661 | self.path.unlink(missing_ok=True) |
| 662 | else: |
| 663 | atomic(self.path, old_manager) |
| 664 | if changed_secrets: |
| 665 | latest = runs.nomad('var/' + variable_path) |
| 666 | items = dict(latest['Items']) |
| 667 | for key, value in self.secret_before.items(): |
| 668 | if items.get(key) != self.items.get(key): |
| 669 | raise Error(502, 'The API credential changed during recovery. Check Copyparty before retrying.') from None |
| 670 | if value is None: |
| 671 | items.pop(key, None) |
| 672 | else: |
| 673 | items[key] = value |
| 674 | runs.nomad('var/' + variable_path + '?cas=' + str(latest['ModifyIndex']), 'PUT', |
| 675 | {'Namespace': 'default', 'Path': variable_path, 'Items': items}) |
| 676 | if self.job is not None: |
| 677 | self.items = original_items |
| 678 | self.start() |
| 679 | elif committed: |
| 680 | self.reload(original_items) |
| 681 | raise |
| 682 | finally: |
| 683 | self.db.close() |
| 684 | |
| 685 | |
| 686 | def handle(request): |
| 687 | action, payload, users = request.get('action'), request.get('payload'), request.get('users') |
| 688 | if (not isinstance(action, str) or action not in ACTIONS or not isinstance(payload, dict) |
| 689 | or set(payload) != ACTIONS[action] or not isinstance(users, list)): |
| 690 | raise Error(400, 'Use the fields required by this share action.') |
| 691 | manager = Manager(users) |
| 692 | if not manager.cfg.is_dir(): |
| 693 | raise Error(503, 'Copyparty settings are unavailable. Deploy its service before editing shares.') |
| 694 | lock = os.open(manager.cfg / '.manager.lock', os.O_WRONLY | os.O_CREAT | os.O_NOFOLLOW, 0o600) |
| 695 | with os.fdopen(lock, 'w') as locked: |
| 696 | fcntl.flock(locked, fcntl.LOCK_EX) |
| 697 | return manager.apply(action, payload) |