Skip to content

Commit

Permalink
Begin to add groupmap logic
Browse files Browse the repository at this point in the history
  • Loading branch information
anodos325 committed Jul 6, 2024
1 parent ed52a7a commit 1d5240c
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions src/middlewared/middlewared/utils/groupmap.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 1d5240c

Please sign in to comment.