Allow exporting processed data as passwd and group files
This commit is contained in:
@@ -49,6 +49,12 @@ def main():
|
||||
default="out",
|
||||
)
|
||||
|
||||
parser.add_argument(
|
||||
"--output-passwd",
|
||||
action="store_true",
|
||||
help="Output passwd and group files instead of JSON records. This still applies any filtering and transformations.",
|
||||
)
|
||||
|
||||
parser.add_argument(
|
||||
"-e",
|
||||
"--email-domain",
|
||||
@@ -80,25 +86,25 @@ def main():
|
||||
"--set-default-umask",
|
||||
type=str,
|
||||
help="Set the umask for all users.",
|
||||
metavar='OCTAL',
|
||||
metavar="OCTAL",
|
||||
)
|
||||
parser.add_argument(
|
||||
"--set-default-mount-no-devices",
|
||||
type=bool,
|
||||
help="Set mountNoDevices for all users.",
|
||||
metavar='BOOL',
|
||||
metavar="BOOL",
|
||||
)
|
||||
parser.add_argument(
|
||||
"--set-default-mount-no-suid",
|
||||
type=bool,
|
||||
help="Set mountNoSuid for all users.",
|
||||
metavar='BOOL',
|
||||
metavar="BOOL",
|
||||
)
|
||||
parser.add_argument(
|
||||
"--set-default-mount-no-execute",
|
||||
type=bool,
|
||||
help="Set mountNoExecute for all users.",
|
||||
metavar='BOOL',
|
||||
metavar="BOOL",
|
||||
)
|
||||
|
||||
args = parser.parse_args()
|
||||
@@ -126,7 +132,10 @@ def main():
|
||||
if user.name in shadow:
|
||||
user.set_shadow_entry(shadow[user.name])
|
||||
|
||||
output_to_directory(users, groups, args.output_dir)
|
||||
if args.output_passwd:
|
||||
generate_passwd_and_group_files(users, groups, args.output_dir)
|
||||
else:
|
||||
output_to_directory(users, groups, args.output_dir)
|
||||
|
||||
|
||||
def ensure_no_overlapping_uids(users: dict[str, User]):
|
||||
@@ -496,5 +505,46 @@ def output_to_directory(
|
||||
json.dump({}, f)
|
||||
|
||||
|
||||
def generate_passwd_and_group_files(
|
||||
users: dict[str, User],
|
||||
groups: dict[str, Group],
|
||||
output_dir: Path,
|
||||
):
|
||||
if not output_dir.exists():
|
||||
output_dir.mkdir(parents=True)
|
||||
|
||||
passwd_path = output_dir / "passwd"
|
||||
group_path = output_dir / "group"
|
||||
|
||||
users_ = list(users.values())
|
||||
users_.sort(key=lambda u: u.uid)
|
||||
|
||||
groups_ = list(groups.values())
|
||||
groups_.sort(key=lambda g: g.gid)
|
||||
|
||||
with passwd_path.open("w") as passwd_file:
|
||||
for user in users_:
|
||||
gecos = ",".join(
|
||||
filter(
|
||||
None,
|
||||
[
|
||||
user.full_name,
|
||||
user.location,
|
||||
user.work_phone,
|
||||
user.home_phone,
|
||||
user.other,
|
||||
],
|
||||
)
|
||||
)
|
||||
passwd_file.write(
|
||||
f"{user.name}:{user.password if user.password else 'x'}:{user.uid}:{user.gid}:{gecos}:{user.home}:{user.shell}\n"
|
||||
)
|
||||
|
||||
with group_path.open("w") as group_file:
|
||||
for group in groups_:
|
||||
members_str = ",".join(group.members)
|
||||
group_file.write(f"{group.name}:x:{group.gid}:{members_str}\n")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
||||
Reference in New Issue
Block a user