ЭЛЕКТРОННАЯ БИБЛИОТЕКА КОАПП |
Сборники Художественной, Технической, Справочной, Английской, Нормативной, Исторической, и др. литературы. |
16. System Database AccessContents: 16.1 Getting Password and Group InformationThe information that the UNIX system keeps about your username and user ID is fairly public. In fact, nearly everything but your unencrypted password is available for perusal by any program that cares to scan the /etc/passwd file. This file has a particular format, defined in passwd (5), which looks something like this: name:passwd:uid:gid:gcos:dir:shell The fields are defined as follows:
A typical portion of the password file looks like this: fred:*:123:15:Fred Flintstone,,,:/home/fred:/bin/csh barney:*:125:15:Barney Rubble,,,:/home/barney:/bin/csh Now, Perl has enough tools to parse this kind of line easily (using For example, the getpwnam routine becomes the Perl ($name, $passwd, $uid, $gid, $quota, $comment, $gcos, $dir, $shell) Note that there are few more values here than in the password file. For every UNIX system we've seen, the ("fred", "*", 123, 15, "", "Fred Flintstone,,,", "Fred Flintstone,,,", "/home/fred"," /bin/csh") by invoking either of the following two calls: getpwuid(123) getpwnam("fred") Note that The $idnum = getpwuid("daemon"); $login = getpwnam(25); You'll probably want to pick this apart, using some of the list operations that we've seen before. One way is to grab a part of the list using a list slice, such as getting just the home directory for Fred using: ($fred_home) = (getpwnam ("fred"))[7]; # put Fred's home How would you scan through the entire password file? Well, you could do something like this: for($id = 0; $id <= 10_000; $id++) { @stuff = getpwuid $id; } ### not recommended! But this is probably the wrong way to go. Just because there's more than one way to do it doesn't mean that all ways are equally cool. You can think of the The sequential access routines for the password file are the This description begs for an example, so here's one now: setpwent(); # initialize the scan while (@list = getpwent()) { # fetch the next entry ($login,$home) = @list[0,7]; # grab login name and home print "Home directory for $login is $home\n"; # say so } endpwent(); # all done This example shows the home directory of everyone in the password file. Suppose you wanted them alphabetically by home directory? We learned about setpwent(); # initialize the scan while (@list = getpwent()) { # fetch the next entry ($login,$home) = @list[0,7]; # grab login name and home $home{$login} = $home; # save it away } endpwent(); # all done @keys = sort { $home{$a} cmp $home{$b} } keys %home; foreach $login (@keys) { # step through the sorted names print "home of $login is $home{$login}\n"; } This fragment, while a little longer, illustrates an important thing about scanning sequentially through the password file; you can save away the pertinent portions of the data in data structures of your choice. The first part of the example scans through the entire password file, creating a hash where the key is the login name and the value is the corresponding home directory for that login name. The Generally, you should use the random access routines (
The /etc/group file is accessed in a similar way. Sequential access is provided with the ($name, $passwd, $gid, $members) These four values correspond roughly to the four fields of the /etc/group file, so see the descriptions in the manpages about this file format for details. The corresponding random access functions are |