TeileausUIDs

From NJH-Wiki

Jump to: navigation, search

Vorraussetzungen

Dieses Script muss man als root ausführen, da sonst die Benutzerrechte nicht geändert werden können

Script

#!/usr/bin/php
 
<?php
 
/**
  * PHP Script to change the UIDs of users and set the appropriate right on all their files
  *
  * @author Frank Proessdorf <frank.proessdorf@ibb.de>
  *
  */
 
/**
  * general parameters
  *
  */
 
// where to start looking for user's files
$start = '/home/';
 
// directories that are not to be searched for user files
$dirs_not_search = array(
        "/proc",
        "/lost+found",
        "/boot",
        "/dev",
        "/root",
        "/sys");
 
// array of users and their new UIDs
$new_users = array(
        'proessdo' => 1007,
);
 
/**
 * search the tree for user's files and save them in array
 *
 * @param       string          directory to search in
 */
function search_tree($dir, $user_id) {
        global $user_files;
        global $dirs_not_search;
 
        $dh = opendir($dir);
        while (false !== ($file = readdir($dh))) {
                $path = $dir.$file;
                if (!in_array($path, $dirs_not_search)) {
                        if (is_dir($path) && $file != '.' && $file != '..') {
                                search_tree($path.'/', $user_id);
                        }
                        $fo = @fileowner($path);
                        if ($fo == $user_id) {
                                if (is_dir($path) && $file != '.' && $file != '..') {
                                        $user_files[] = $path.'/';
                                } else {
                                        $user_files[] = $path;
                                }
                        }
                }
        }
        closedir($dh);
 
        return;
}
 
 
/**
 * change the passwd file using the new uid array
 *
 * @param       array           the new users: name => newUID
 */
function change_passwd($new_users) {
        $passwd = '/etc/passwd';
        $newpasswd = '';
 
        $pwdlines = file($passwd);
        foreach ($pwdlines as $line) {
                $user = explode(':', $line);
                if (array_key_exists($user[0], $new_users)) {
                        $users_on_here[$user[0]] = $user[2];
                        $user[2] = $new_users[$user[0]];
                        $newuser = implode(':', $user);
                        $newpasswd .= $newuser;
                } else {
                        $newpasswd .= $line;
                }
        }
 
        // oeffne passwd zum schreiben
        if(!$fh = fopen($passwd, 'w')) { echo $passwd.' kann nicht geoeffnet werden'; exit; }
 
        // schreibe die neue passwd
        if (!fwrite($fh, $newpasswd)) { echo 'kann nicht in '.$passwd.' schreiben'; exit; }
 
        // passwd schliessen
        fclose($fh);
 
        // return an array of the users whose UIDs were changed
        return $users_on_here;
}
 
 
// change the passwd file using the new uid array
$users_on_here = change_passwd($new_users);
 
// search the tree starting at $start for each user on this machine
foreach ($users_on_here as $current_user => $current_user_olduid) {
        $user_data = posix_getpwnam($current_user);
        $current_user_newuid = $user_data['uid'];
        $user_files = array();
        search_tree($start, $current_user_olduid);
        $all_users_files[$current_user_newuid] = $user_files;
}
 
// change owner rights of all users files
foreach ($all_users_files as $newuid => $files) {
        foreach($files as $file) {
                chown($file, $newuid);
        }
}
 
?>
Personal tools