1import copy
2import ctypes
3import errno
4import fcntl
5import importlib
6import json
7import os
8from pathlib import Path
9import re
10import secrets
11import sqlite3
12import subprocess
13import time
14import urllib.error
15import urllib.request
16import uuid
17
18runs = importlib.import_module('dashboard-run')
19Error = runs.Error
20PODMAN = ['podman', '--remote', '--url', 'unix:///run/podman/podman.sock']
21
22ACTIONS = {
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'},
28 'link.update': {'key', 'name', 'path', 'permissions', 'expires', 'password', 'mode'},
29 'link.delete': {'key'},
30}
31
32
33def 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
44def 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
52def 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
58def 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
74def 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
97def 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
160def 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
172def 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
179def 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
197class 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'have 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'
445 destination = payload['path']
446 share_root = self.root / 'Share'
447 if create and (not destination or Path(destination).parent == share_root or str(destination).startswith('/Volumes/clover/Share/')):
448 self.path_for(str(share_root), missing=True)
449 if not share_root.exists():
450 share_root.mkdir(mode=0o775)
451 share_root.chmod(0o775)
452 owner = self.root.stat()
453 os.chown(share_root, owner.st_uid, owner.st_gid)
454 self.undo.append(lambda: share_root.rmdir())
455 if not destination:
456 destination = str(share_root / name)
457 destination = self.path_for(destination, missing=create or action == 'link.update' and payload['mode'] == 'move')
458 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 []
459 mode = payload.get('mode', 'retarget')
460 if mode not in {'retarget', 'move'}:
461 raise Error(400, 'Choose whether to move the folder or use another folder.')
462 if create and not destination.exists():
463 destination.mkdir(mode=0o775)
464 destination.chmod(0o775)
465 owner = destination.parent.stat()
466 os.chown(destination, owner.st_uid, owner.st_gid)
467 self.undo.append(lambda: destination.rmdir())
468 if old is not None and mode == 'move':
469 source = self.real(old[2])
470 if source is None:
471 raise Error(400, 'Only folders inside the Clover file store can be moved here.')
472 source = self.path_for(str(source))
473 if source != destination:
474 for grant in self.data['grants']:
475 if Path(grant['path']).is_relative_to(source):
476 grant['path'] = str(destination / Path(grant['path']).relative_to(source))
477 self.owner_permissions(old[5], destination, perms)
478 self.move(source, destination)
479 for filename in files:
480 if (not isinstance(filename, str) or filename in {'.', '..'} or '/' in filename or '\\' in filename
481 or not (destination / filename).is_file() or (destination / filename).is_symlink()):
482 raise Error(409, 'The destination must contain every selected file. Choose a matching folder.')
483 if old is not None:
484 self.owner_permissions(old[5], destination, perms)
485 self.changed.add(key)
486 self.db.execute('UPDATE sh SET pw=?,vp=?,pr=?,t1=? WHERE k=?', (password, self.virtual(destination), perms, expires, key))
487 else:
488 self.changed.add(key)
489 self.db.execute('INSERT INTO sh VALUES(?,?,?,?,?,?,?,?)', (key, password, self.virtual(destination), perms, 0, 'snow', int(time.time()), expires))
490 self.data['names'][key] = name
491
492 def move(self, source, destination):
493 if source == self.root or source == self.root / 'Share' or destination.is_relative_to(source) or source.is_relative_to(destination):
494 raise Error(400, 'Choose separate folders below the Clover file store.')
495 if destination.exists():
496 raise Error(409, 'The destination already exists. Choose a new folder name.')
497 if source.stat().st_dev != destination.parent.stat().st_dev:
498 raise Error(409, 'Choose a destination on the same filesystem to move this folder.')
499 mounts = Path('/proc/self/mountinfo')
500 if mounts.exists():
501 for row in mounts.read_text().splitlines():
502 mount = Path(re.sub(r'\\([0-7]{3})', lambda match: chr(int(match[1], 8)), row.split()[4]))
503 if mount.is_relative_to(source):
504 raise Error(409, 'A mounted filesystem cannot be moved. Choose a folder without mounts inside it.')
505 elif os.path.ismount(source):
506 raise Error(409, 'A mounted filesystem cannot be moved. Choose a folder inside it.')
507 rows = self.db.execute('SELECT k,vp,st FROM sh').fetchall()
508 for key, virtual, count in rows:
509 real = self.real(virtual)
510 if real is not None and source != real and source.is_relative_to(real):
511 selected = {row[0] for row in self.db.execute('SELECT vp FROM sf WHERE k=?', (key,))} if count else None
512 if selected is None or source.relative_to(real).parts[0] in selected:
513 raise Error(409, 'Another share includes this folder through its parent. Retarget that share before moving the folder.')
514 self.stop()
515 for key, virtual, _ in rows:
516 real = self.real(virtual)
517 if real is not None and real.is_relative_to(source):
518 self.changed.add(key)
519 self.db.execute('UPDATE sh SET vp=? WHERE k=?', (self.virtual(destination / real.relative_to(source)), key))
520 for grant in self.data['grants']:
521 if Path(grant['path']).is_relative_to(source):
522 grant['path'] = str(destination / Path(grant['path']).relative_to(source))
523 rename(source, destination)
524 self.undo.append(lambda: rename(destination, source))
525
526 def dispatch(self, action, payload):
527 extra = {}
528 if action.startswith('account.'):
529 account = {'id': str(uuid.uuid4()), 'name': label(payload['name'])} if action == 'account.create' else self.account(payload['id'])
530 value = account['id']
531 if action == 'account.create':
532 if any(user.get('username') == 'api-' + value for user in self.users):
533 raise Error(409, 'That API username already belongs to a Snow Sign On user. Try creating the account again.')
534 self.data['accounts'].append(account)
535 if action in {'account.update', 'account.create'}:
536 account['name'] = label(payload['name'])
537 if action in {'account.create', 'account.rotate'}:
538 password = secrets.token_urlsafe(32)
539 self.secret('file_api_' + value.replace('-', ''), self.hash(password))
540 extra = {'account': {**account, 'subject': 'api:' + value, 'username': 'api-' + value}, 'password': password}
541 if action in {'account.delete', 'account.rotate'}:
542 session_path = self.native / 'sessions.db'
543 if session_path.exists():
544 with sqlite3.connect(session_path) as sessions:
545 sessions.execute('DELETE FROM us WHERE un=?', ('api-' + value,))
546 if action == 'account.delete':
547 self.data['accounts'].remove(account)
548 self.data['grants'] = [grant for grant in self.data['grants'] if grant['subject'] != 'api:' + value]
549 self.secret('file_api_' + value.replace('-', ''), None)
550 elif action == 'grant.save':
551 self.grant(payload)
552 elif action == 'grant.delete':
553 value = identifier(payload['id'])
554 if not any(grant['id'] == value for grant in self.data['grants']):
555 raise Error(404, 'That folder permission no longer exists. Reload permissions.')
556 self.data['grants'] = [grant for grant in self.data['grants'] if grant['id'] != value]
557 elif action in {'link.create', 'link.update'}:
558 self.link(action, payload)
559 elif action == 'link.delete':
560 key = payload['key']
561 if not isinstance(key, str) or not self.db.execute('SELECT 1 FROM sh WHERE k=?', (key,)).fetchone():
562 raise Error(404, 'That share no longer exists. Reload shares.')
563 self.changed.add(key)
564 self.db.execute('DELETE FROM sh WHERE k=?', (key,))
565 self.db.execute('DELETE FROM sf WHERE k=?', (key,))
566 self.data['names'].pop(key, None)
567 return extra
568
569 def apply(self, action, payload):
570 self.data = json.loads(runs.read(self.path, missing='{"accounts":[],"grants":[],"names":{}}'))
571 origin = Path(self.data.get('root', os.environ.get('SNOWGLOBE_SOURCE_CLOVER_ROOT', '/srv/clover')))
572 if 'root' not in self.data and self.data.get('grants'):
573 origin = Path(os.environ.get('SNOWGLOBE_SOURCE_CLOVER_ROOT', str(self.root)))
574 if origin != self.root:
575 for grant in self.data['grants']:
576 grant['path'] = str(self.root / Path(grant['path']).relative_to(origin))
577 self.data['root'] = str(self.root)
578 if not isinstance(self.data.get('accounts'), list) or not isinstance(self.data.get('grants'), list) or not isinstance(self.data.get('names'), dict):
579 raise Error(503, 'Share settings need repair before editing. Check the host service.')
580 database = self.native / 'shares.db'
581 if not database.is_file():
582 raise Error(503, 'Copyparty shares are not ready. Start Copyparty and try again.')
583 self.db = sqlite3.connect(database.as_uri() + '?mode=rw', uri=True, timeout=5)
584 self.db.row_factory = None
585 try:
586 version = self.db.execute("SELECT v FROM kv WHERE k='sver'").fetchone()
587 if version != (2,) or [row[1] for row in self.db.execute('PRAGMA table_info(sh)')] != ['k', 'pw', 'vp', 'pr', 'st', 'un', 't0', 't1']:
588 raise Error(503, 'This Copyparty share format is unsupported. Update Snowglobe before editing shares.')
589 if action == 'state':
590 return self.state()
591 if action == 'folders.list':
592 path = self.path_for(payload['path'])
593 with os.scandir(path) as entries:
594 directories = sorted(({'name': entry.name, 'path': str(path / entry.name)} for entry in entries
595 if not entry.name.startswith('.') and entry.is_dir(follow_symlinks=False)), key=lambda entry: entry['name'].casefold())
596 return {'path': str(path), 'directories': directories}
597 self.container()
598 variable_path = 'nomad/jobs/' + self.service
599 variable = runs.nomad('var/' + variable_path)
600 if variable is None:
601 raise Error(503, 'Copyparty secrets are unavailable. Redeploy its service.')
602 self.items = dict(variable['Items'])
603 original_items = dict(self.items)
604 config_path = self.cfg / 'copyparty.conf'
605 old_config = config_path.read_bytes()
606 metadata = config_path.stat()
607 owner = (metadata.st_uid, metadata.st_gid)
608 self.db.execute('BEGIN IMMEDIATE')
609 original_rows = {row[0]: row for row in self.db.execute('SELECT * FROM sh')}
610 original_files = {key: self.db.execute('SELECT * FROM sf WHERE k=?', (key,)).fetchall() for key in original_rows}
611 committed = False
612 wrote_files = False
613 old_manager = self.path.read_bytes() if self.path.exists() else None
614 changed_secrets = False
615 after_rows = {}
616 try:
617 extra = self.dispatch(action, payload)
618 rendered = configuration(self.cfg, self.data, self.items)
619 if self.secret_before:
620 runs.nomad('var/' + variable_path + '?cas=' + str(variable['ModifyIndex']), 'PUT',
621 {'Namespace': 'default', 'Path': variable_path, 'Items': self.items})
622 changed_secrets = True
623 atomic(config_path, rendered, owner)
624 wrote_files = True
625 atomic(self.path, json.dumps(self.data, ensure_ascii=False).encode())
626 after_rows = {key: self.db.execute('SELECT * FROM sh WHERE k=?', (key,)).fetchone() for key in self.changed}
627 self.db.commit()
628 committed = True
629 if self.job is not None:
630 self.start()
631 else:
632 self.reload(self.items)
633 return {**self.state(), **extra}
634 except Exception:
635 if self.job is not None:
636 self.deadline = self.offline_at + 280
637 self.stop()
638 self.db.rollback()
639 if committed and self.changed:
640 self.db.execute('BEGIN IMMEDIATE')
641 for key in self.changed:
642 current = self.db.execute('SELECT * FROM sh WHERE k=?', (key,)).fetchone()
643 if current != after_rows[key]:
644 raise Error(502, 'The update could not finish and a share changed during recovery. Check Copyparty before retrying.') from None
645 self.db.execute('DELETE FROM sh WHERE k=?', (key,))
646 self.db.execute('DELETE FROM sf WHERE k=?', (key,))
647 if key in original_rows:
648 self.db.execute('INSERT INTO sh VALUES(?,?,?,?,?,?,?,?)', original_rows[key])
649 self.db.executemany('INSERT INTO sf VALUES(?,?)', original_files[key])
650 self.db.commit()
651 for undo in reversed(self.undo):
652 undo()
653 if wrote_files:
654 atomic(config_path, old_config, owner)
655 if old_manager is None:
656 self.path.unlink(missing_ok=True)
657 else:
658 atomic(self.path, old_manager)
659 if changed_secrets:
660 latest = runs.nomad('var/' + variable_path)
661 items = dict(latest['Items'])
662 for key, value in self.secret_before.items():
663 if items.get(key) != self.items.get(key):
664 raise Error(502, 'The API credential changed during recovery. Check Copyparty before retrying.') from None
665 if value is None:
666 items.pop(key, None)
667 else:
668 items[key] = value
669 runs.nomad('var/' + variable_path + '?cas=' + str(latest['ModifyIndex']), 'PUT',
670 {'Namespace': 'default', 'Path': variable_path, 'Items': items})
671 if self.job is not None:
672 self.items = original_items
673 self.start()
674 elif committed:
675 self.reload(original_items)
676 raise
677 finally:
678 self.db.close()
679
680
681def handle(request):
682 action, payload, users = request.get('action'), request.get('payload'), request.get('users')
683 if (not isinstance(action, str) or action not in ACTIONS or not isinstance(payload, dict)
684 or set(payload) != ACTIONS[action] or not isinstance(users, list)):
685 raise Error(400, 'Use the fields required by this share action.')
686 manager = Manager(users)
687 if not manager.cfg.is_dir():
688 raise Error(503, 'Copyparty settings are unavailable. Deploy its service before editing shares.')
689 lock = os.open(manager.cfg / '.manager.lock', os.O_WRONLY | os.O_CREAT | os.O_NOFOLLOW, 0o600)
690 with os.fdopen(lock, 'w') as locked:
691 fcntl.flock(locked, fcntl.LOCK_EX)
692 return manager.apply(action, payload)