1*8460358cSPatrick Williams# 2*8460358cSPatrick Williams# Copyright OpenEmbedded Contributors 3*8460358cSPatrick Williams# 4*8460358cSPatrick Williams# SPDX-License-Identifier: GPL-2.0-only 5*8460358cSPatrick Williams# 6*8460358cSPatrick Williams 7*8460358cSPatrick Williamsimport os 8*8460358cSPatrick Williams 9*8460358cSPatrick Williamsdef sort_shadowutils_file(filename, mapping): 10*8460358cSPatrick Williams """ 11*8460358cSPatrick Williams Sorts a passwd or group file based on the numeric ID in the third column. 12*8460358cSPatrick Williams If a mapping is given, the name from the first column is mapped via that 13*8460358cSPatrick Williams dictionary instead (necessary for /etc/shadow and /etc/gshadow). If not, 14*8460358cSPatrick Williams a new mapping is created on the fly and returned. 15*8460358cSPatrick Williams """ 16*8460358cSPatrick Williams 17*8460358cSPatrick Williams new_mapping = {} 18*8460358cSPatrick Williams with open(filename, 'rb+') as f: 19*8460358cSPatrick Williams lines = f.readlines() 20*8460358cSPatrick Williams # No explicit error checking for the sake of simplicity. /etc 21*8460358cSPatrick Williams # files are assumed to be well-formed, causing exceptions if 22*8460358cSPatrick Williams # not. 23*8460358cSPatrick Williams for line in lines: 24*8460358cSPatrick Williams entries = line.split(b':') 25*8460358cSPatrick Williams name = entries[0] 26*8460358cSPatrick Williams if mapping is None: 27*8460358cSPatrick Williams id = int(entries[2]) 28*8460358cSPatrick Williams else: 29