diff --git a/src/middlewared/middlewared/utils/groupmap.py b/src/middlewared/middlewared/utils/groupmap.py new file mode 100644 index 0000000000000..3c3c2c93a1821 --- /dev/null +++ b/src/middlewared/middlewared/utils/groupmap.py @@ -0,0 +1,33 @@ +from base64 import b64decode, b64encode +from collections import namedtuple + +from middlewared.utils.sid import ( + lsa_sidtype +) +from socket import htonl, ntohl + +UNIX_GROUP_KEY_PREFIX = 'UNIXGROUP/' +MEMBEROF_PREFIX = 'MEMBEROF/' + +GROUP_MAP = namedtuple('SMBGroupMap', ['gid', 'sid_type', 'name', 'comment']) +GROUPMEM = namedtuple('SMBGroupMembership', ['sid', 'members']) + +def _parse_unixgroup(tdb_key: str, tdb_val: str) -> GROUP_MAP: + sid = tdb_key[len(UNIX_GROUP_KEY_PREFIX):] + data = b64decode(tdb_val) + + # unix groups are written into tdb file via tdb_pack + gid = htonl(data[0:4]) + sid_type = lsa_sidtype(htonl(data[4:8])) + + # remaining bytes are two null-terminated strings + bname, bcomment = data[8:-1].split(b'\x00') + return GROUP_MAP(gid, sid_type, bname.decode(), bcomment.decode()) + + +def _parse_memberof(tdb_key: str, tdb_val: str) -> GROUP_MEMBER: + sid = tdb_key[len(MEMBEROF_PREFIX):] + data = b64decode(tdb_val) + + members = tuple(data[:-1].decode().split()) + return GROUPMEM(sid, members)