# Path: typtop/pw_pkcrypto.py

# 
# Path: typtop/config.py
# DB_NAME = "typtop"
# 
# GROUP = 'shadow' if DISTRO in ('debian') else \
#     'root' if DISTRO in ('fedora', 'arch') else \
#     'wheel' if DISTRO in ('darwin') \
#     else ''


import struct
import logging
import pwd, grp
import uuid
import os
from typtop.pw_pkcrypto import decrypt, hash256, urlsafe_b64encode
from typtop.config import DB_NAME, GROUP



def is_user(u):
    try:
        pwd.getpwnam(u)
        return True
    except KeyError:
        return False

logger = logging.getLogger(DB_NAME)


def change_db_ownership(fl):
    try:
        g_id = grp.getgrnam(GROUP).gr_gid
        f_stat = os.stat(fl)
        f_uid, f_gid, mode = f_stat.st_uid, f_stat.st_gid, f_stat.st_mode
        # 511 = 2**9 - 1, last 3x3 permission bits, Should be 0660
        # o-rw
        if f_uid != 0 or f_gid != g_id or (mode & 511 != 0o660):
            os.chown(fl, f_uid, g_id)

# Path: typtop/pw_pkcrypto.py

# 
# Path: typtop/config.py
# DB_NAME = "typtop"
# 
# GROUP = 'shadow' if DISTRO in ('debian') else \
#     'root' if DISTRO in ('fedora', 'arch') else \
#     'wheel' if DISTRO in ('darwin') \
#     else ''


# Path: typtop/dbutils.py
import struct
import logging
import pwd, grp
import uuid
import os
from typtop.pw_pkcrypto import decrypt, hash256, urlsafe_b64encode
from typtop.config import DB_NAME, GROUP




def is_user(u):
    try:
        pwd.getpwnam(u)
        return True
    except KeyError:
        return False

logger = logging.getLogger(DB_NAME)


def change_db_ownership(fl):
    try:
        g_id = grp.getgrnam(GROUP).gr_gid
        f_stat = os.stat(fl)
        f_uid, f_gid, mode = f_stat.st_uid, f_stat.st_gid, f_stat.st_mode
        # 511 = 2**9 - 1, last 3x3 permission bits, Should be 0660
        # o-rw
        if f_uid != 0 or f_gid != g_id or (mode & 511 != 0o660):
            os.chown(fl, f_uid, g_id)

            os.chmod(fl, 0o660)