Online Book Reader

Home Category

Classic Shell Scripting - Arnold Robbins [167]

By Root 913 0
a series of scripts that would:

Merge the /etc/passwd files of the two systems. This entailed ensuring that all users from both systems had unique UID numbers.

Change the ownership of all files to the correct users in the case where an existing UID was to be used for a different user.

It is this task that we recreate in this chapter, from scratch. (The original scripts are long gone, and it's occasionally interesting and instructive to reinvent a useful wheel.) This problem isn't just academic, either: consider two departments in a company that have been separate but that now must merge. It's possible for there to be users with accounts on systems in multiple departments. If you're a system administrator, you may one day face this very task. In any case, we think it is an interesting problem to solve.

* * *

[1] BSD systems maintain an additional file, /etc/master.passwd, which has three additional fields: the user's login class, password change time, and account expiration time. These fields are placed between the GID field and the field for the full name.

The Password Files

Let's call our two hypothetical Unix systems u1 and u2. Example 11-1 presents the /etc/passwd file from u1.[2]

Example 11-1. u1 /etc/passwd file

root:x:0:0:root:/root:/bin/bash

bin:x:1:1:bin:/bin:/sbin/nologin

daemon:x:2:2:daemon:/sbin:/sbin/nologin

adm:x:3:4:adm:/var/adm:/sbin/nologin

tolstoy:x:2076:10:Leo Tolstoy:/home/tolstoy:/bin/bash

camus:x:112:10:Albert Camus:/home/camus:/bin/bash

jhancock:x:200:10:John Hancock:/home/jhancock:/bin/bash

ben:x:201:10:Ben Franklin:/home/ben:/bin/bash

abe:x:105:10:Honest Abe Lincoln:/home/abe:/bin/bash

dorothy:x:110:10:Dorothy Gale:/home/dorothy:/bin/bash

And Example 11-2 presents /etc/passwd from u2.

Example 11-2. u2 /etc/passwd file

root:x:0:0:root:/root:/bin/bash

bin:x:1:1:bin:/bin:/sbin/nologin

daemon:x:2:2:daemon:/sbin:/sbin/nologin

adm:x:3:4:adm:/var/adm:/sbin/nologin

george:x:1100:10:George Washington:/home/george:/bin/bash

betsy:x:1110:10:Betsy Ross:/home/betsy:/bin/bash

jhancock:x:300:10:John Hancock:/home/jhancock:/bin/bash

ben:x:301:10:Ben Franklin:/home/ben:/bin/bash

tj:x:105:10:Thomas Jefferson:/home/tj:/bin/bash

toto:x:110:10:Toto Gale:/home/toto:/bin/bash

If you examine these files carefully, you'll see they represent the various possibilities that our program has to handle:

Users for whom the username and UID are the same on both systems. This happens most typically with administrative accounts such as root and bin.

Users for whom the username and UID exist only on one system but not the other. In this case, when the files are merged, there is no problem.

Users for whom the username is the same on both systems, but the UIDs are different.

Users for whom the username is different on both systems, but the UIDs are the same.

* * *

[2] Any resemblance to actual users, living or dead, is purely coincidental.

Merging Password Files

The first step is to create a merged /etc/passwd file. This involves several substeps:

Physically merge the files, bringing duplicate usernames together. This becomes the input for the following steps.

Split the merged file into three separate parts for use in later processing:

Users for whom the username and UID are the same go into one file, named unique1. Users with nonrepeated usernames also go into this file.

Users with the same username and different UIDs go into a second file, named dupusers.

Users with the same UID and different usernames go into a third file, named dupids.

Create a list of all unique UID numbers that already are in use. This will be needed so that we can find new, unused UID numbers when a conflict occurs and we need to do a UID change (e.g., users jhancock and ben).

Given the list of in-use UID numbers, write a separate program to find a new, unused UID number.

Create a list of (username, old UID, new UID) triples to be used in creating final /etc/passwd entries, and more importantly, in generating commands to change the ownership of files in the filesystem.

Return Main Page Previous Page Next Page

®Online Book Reader