#!/usr/bin/php
<?php

# Dieses Script sortiert /etc/shadow in der gleichen Reihenfolge wie /etc/passwd (ohne dessen Sortierung zu ändern)
# ACHTUNG! GANZ WICHTIG! Die Ausgabe dieses Scripts nicht per shell-redirect in /etc/shadow speichern! Aus irgendeinem Grund zerstört das die Datei!

$shadow_users = array();

$shadow = file('/etc/shadow');
foreach ($shadow as $line) {
	$line = trim($line);
	if (($line == '') || ($line[0] == '#')) continue;

	$bry = explode(':', $line);

	if (isset($shadow_users[$bry[0]])) {
		die('Error: Duplicate line for user '.$bry[0]."\n");
	}
	$shadow_users[$bry[0]] = $line;
}

$passwd = file('/etc/passwd');
foreach ($passwd as $n => $line) {
	$line = trim($line);
	if (($line == '') || ($line[0] == '#')) {
		if (($line == '') && ($n == count($passwd)-1)) continue;
		echo "$line\n";
	} else {
		$bry = explode(':', $line);
		echo $shadow_users[$bry[0]]."\n";
		unset($shadow_users[$bry[0]]);
	}
}

if (count($shadow_users) > 0) {
	echo "\n\n# ATTENTION!";
	echo "\n\n# Following users do not have an entry in /etc/passwd:\n";
	foreach ($shadow_users as $user) {
		echo "# - $user\n";
	}
	echo "# --- end ---\n";
}
