check method

Future<void> check()

Checks owner and mode (see prepare).

Implementation

Future<void> check() async {
  if (Platform.isWindows) {
    final out = await _run('icacls', [directory]);
    final user = Platform.environment['USERNAME'] ?? '';
    final lines = out
        .split(RegExp(r'\r?\n'))
        .map((l) => l.replaceFirst(directory, '').trim())
        .where((l) => l.contains(':('))
        .toList();
    for (final line in lines) {
      final who = line.split(':(').first.split(r'\').last.toLowerCase();
      if (who != user.toLowerCase() && who != 'system' && who != 'system-') {
        throw TomSecretsRuntimeException(
          'the runtime directory grants $who access',
        );
      }
    }
    return;
  }
  final stat = await FileStat.stat(directory);
  if (stat.type != FileSystemEntityType.directory) {
    throw TomSecretsRuntimeException('$directory is not a directory');
  }
  if (stat.mode & 0x3F != 0) {
    throw TomSecretsRuntimeException(
      'the runtime directory is open to others '
      '(mode ${(stat.mode & 0x1FF).toRadixString(8)})',
    );
  }
  final owner = int.parse(
    (await _run('stat', [
      Platform.isMacOS ? '-f' : '-c',
      '%u',
      directory,
    ])).trim(),
  );
  final me = currentUid();
  if (me != null && owner != me) {
    throw TomSecretsRuntimeException(
      'the runtime directory belongs to user $owner',
    );
  }
}