Walking the Filesystem Using the File::Find Module ARIN American Registry for Internet Numbers: 8.3.3.2.. Walking the Filesystem Using the File::Find Module2.4.. Walking the Filesystem U
Trang 1designations have been printed in caps or initial caps
Disclaimer
While every precaution has been taken in the preparation of this product, thepublisher assumes no responsibility for errors or omissions, or for damages
resulting from the use of the information contained herein
Questions, comments, and suggestions to bookquestions@ora.com
Trang 2Copyright © 2001 O'Reilly & Associates All rights reserved.
Trang 4Intensely social, sea otters gather to float in groups called rafts They are
excellent swimmers, propelling themselves swiftly through the water with theirflipper-like, webbed back paws Their thick fur provides them with efficientinsulation in the water At times, their existence has been threatened as they havebeen mercilessly hunted to near extinction for their fur
Colleen Gorman was the production editor and copyeditor for Perl for System
Administration Jane Ellin was the proofreader Mary Sheehan and Emily Quill
provided quality control Molly Shangraw, Maeve O'Meara, Gabe Weiss, MarySheehan, and Darren Kelly provided production support Nancy Crumpton wrotethe index
Hanna Dyer designed the cover of this book, based on a series design by EdieFreedman The cover image is an original illustration created by Lorrie LeJeune.Emma Colby produced the cover layout with QuarkXPress 3.32 using Adobe'sITC Garamond font
Alicia Cech and David Futato designed the interior layout based on a seriesdesign by Nancy Priest Mike Sierra and David Futato implemented the design
in FrameMaker 5.5.6 The text and heading fonts are ITC Garamond Light andGaramond Book The illustrations that appear in the book were produced byRobert Romano and Rhon Porter using Macromedia FreeHand 8 and Adobe
Trang 5Copyright © 2001 O'Reilly & Associates All rights reserved.
Trang 6V | W | X | Y | Z
Trang 8V | W | X | Y | Z
Trang 98.3.3.2 Checking against Internet-Apple Events: 8.1.2 Using the OS-Specific IPC Framework
AppleScript: 8.1.2 Using the OS-Specific IPC Framework
Trang 10@ARGV, retrieving under Mac OS: 2.4 Walking the Filesystem Using the
File::Find Module
ARIN (American Registry for Internet Numbers): 8.3.3.2 Checking againstInternet-wide blacklists
Asia Pacific Address Allocations (APNIC): wide blacklists
8.3.3.2 Checking against Internet-Symbols | A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U |
V | W | X | Y | Z
Copyright © 2001 O'Reilly & Associates, Inc All Rights Reserved.
Trang 11V | W | X | Y | Z
Trang 13V | W | X | Y | Z
Trang 15Copyright © 2001 O'Reilly & Associates, Inc All Rights Reserved.
Trang 16V | W | X | Y | Z
Trang 17manipulating data in: D.3.3 Simple Manipulation of Data Returned byQueries
Trang 20V | W | X | Y | Z
Trang 21mistakes in: 8.2 Common Mistakes in Sending Email
Windows NT/2000: 8.1.1 Getting sendmail (or Similar Mail TransportAgent)
Trang 22Copyright © 2001 O'Reilly & Associates, Inc All Rights Reserved.
Trang 23V | W | X | Y | Z
Trang 26V | W | X | Y | Z
Trang 28V | W | X | Y | Z
Trang 30V | W | X | Y | Z
Trang 32V | W | X | Y | Z
Trang 34V | W | X | Y | Z
Trang 36V | W | X | Y | Z
Trang 39V | W | X | Y | Z
Trang 40installing: 1.5.3 Installing Modules on MacOS
process control: 4.1 MacOS Process Control
reading @ARGV from a dialog: 2.4 Walking the Filesystem Using theFile::Find Module
Trang 415.2.2 Windows Internet Name Server ( WINS)
message-digest algorithms: 10.1.1 Local Filesystem Changes
Messaging Application Programming Interface (MAPI): Specific IPC Framework
Trang 42recursive, deleting: 3.3.2.2 Windows NT/2000 account creation anddeletion routines
Finger operations, performing: 6.2 Finger: A Simple Directory Service6.2 Finger: A Simple Directory Service
Trang 43NIS data, getting: 5.2 NIS, NIS+, and WINS
open ports on Windows NT/2000, listing: 4.4.1 Tracking Operations onWindows NT/2000
owner of host, finding: 6.4.10 Putting It All Together
packets, dissecting: 10.4.1 Perl Saves the Day
passwords, reading: 10.5 Preventing Suspicious Activities
pathnames, parsing: 2.4 Walking the Filesystem Using the File::FindModule
checking: 3.1.1 The Classic Unix Password File
Windows NT/2000, managing: 3.2.1 NT/2000 User Identity Storageand Access
Trang 45V | W | X | Y | Z
Trang 46needspace script: 2.4 Walking the Filesystem Using the File::Find Module2.4 Walking the Filesystem Using the File::Find Module
net command (Windows NT/2000): 3.2.1 NT/2000 User Identity Storage andAccess
Trang 48V | W | X | Y | Z
Trang 49Object Identifier (OID): E The Twenty-Minute SNMP Tutorial
ODBC Driver Manager: 7.1 Interacting with an SQL Server from Perl
ODBC (Open Database Connectivity): 7.1 Interacting with an SQL Server fromPerl
Symbols | A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U |
V | W | X | Y | Z
Copyright © 2001 O'Reilly & Associates, Inc All Rights Reserved.
Trang 50V | W | X | Y | Z
Trang 51host files, reading: 5.1 Host Files
LDAP
modifying entries: 6.4.8 Modifying Entry Names
Trang 53Mac: 4.1 MacOS Process Control
Unix: 4.3 Unix Process Control
Windows NT/2000: 4.2.1 Using the Microsoft Resource Kit Binaries4.2.3 Using the Win32::Setupsup Module
Trang 54V | W | X | Y | Z
Trang 56V | W | X | Y | Z
Trang 58V | W | X | Y | Z
Trang 59SAM (Security Accounts Manager): 3.2.1 NT/2000 User Identity Storage andAccess
SASL (Simple Authentication and Security Layer): 6.4.2 The Initial LDAPConnection
security identifier (SID): 3.2.2 NT/2000 User ID Numbers
3.2.2 NT/2000 User ID Numbers
(see also users, identity of, Windows NT/2000)
Trang 60D.3.3 Simple Manipulation of Data Returned by Queries
Sendmail for NT program: 8.1.1 Getting sendmail (or Similar Mail TransportAgent)
SNMP Extension Module for the UCD SNMPv3 Library: 10.3.1 Using SNMPfrom Perl
SNMP (Simple Network Management Protocol): E The Twenty-Minute SNMPTutorial
Trang 63V | W | X | Y | Z
Trang 64TCP/IP (Transmission Control Protocol/Internet Protocol): 5 TCP/IP NameServices
Trang 65V | W | X | Y | Z
Trang 66processes, listing: 4.3 Unix Process Control
Trang 68V | W | X | Y | Z
Trang 70V | W | X | Y | Z
Trang 71services, managing via ADSI: 6.5.12 Working with NT/2000 Services viaADSI
Trang 72process control binaries: 4.2.1 Using the Microsoft Resource Kit Binaries
Windows NT Server Resource Kit, utilities for WINS: 5.2.2 Windows InternetName Server ( WINS)
Win32\:\:MAPI module: 8.1.2 Using the OS-Specific IPC Framework
Win32\:\:NetAdmin module: 3.2.1 NT/2000 User Identity Storage and Access3.2.4 NT Groups
DNS and: 5.3 Domain Name Service (DNS)
Win32\:\:Setupsup module: 4.2.3 Using the Win32::Setupsup Module
installing: 4.5.2 Installing Win32::Setupsup
Win32\:\:UserAdmin module: 3.2.1 NT/2000 User Identity Storage and Access3.3.2.2 Windows NT/2000 account creation and deletion routines
Trang 73V | W | X | Y | Z
Copyright © 2001 O'Reilly & Associates, Inc All Rights Reserved.
Trang 74V | W | X | Y | Z
Trang 76V | W | X | Y | Z
Trang 78V | W | X | Y | Z
Trang 82We'll start with a quick review of the native filesystems for each of our targetoperating systems Some of this may be old news to you, especially if you havesignificant experience with a particular operating system Still, it is worth yourwhile to pay careful attention to the differences between the filesystems
(especially the ones you don't know) if you intend to write Perl code that works
on multiple platforms
2.2.1 Unix
All modern Unix variants ship with a native filesystem with semantics thatresemble those of their common ancestor, the Berkeley Fast File System
Different vendors have extended their filesystem implementations in differentways (e.g., Solaris adds Access Control Lists for better security, Digital Unix
ships a spiffy transaction-based filesystem called advfs, etc.) We'll be writing
code aimed at the lowest common denominator to allow it to work across
different Unix platforms
The top, or root, of a Unix filesystem is indicated by a forward slash (/) Touniquely identify a file or directory in a Unix filesystem, we construct a pathstarting with a slash and then add directories, separating them with forwardslashes, as we descend deeper into the filesystem The final component of thispath is the desired directory or filename Directory and filenames in modernUnix variants are case sensitive Almost all ASCII characters can be used inthese names if you are crafty enough, but sticking to alphanumeric charactersand some limited punctuation will save you hassle later
2.2.2 Microsoft Windows NT/2000
Windows NT (Version 4.0 as of this writing) ships with two supported
filesystems: File Allocation Table (FAT) and NT FileSystem (NTFS) Windows
2000 adds FAT32, an improved version of FAT that allows for larger partitionsand smaller cluster sizes to the NT family
Windows NT uses an extended version of the basic FAT filesystems found inDOS Before we look at the extended version, it is important to understand the
Trang 83filenames conform to the 8.3 specification This means that file and directory
names can start with no more than eight characters, must have a period (or dot as
it is spoken), and are followed by a suffix of up to three characters in length.Unlike Unix, where a period in a filename has no special meaning, basic FATfilesystems can only use a single period as an enforced separator between thefilename and its extension or suffix
Real-mode FAT was later enhanced in a version called VFAT or protected-modeFAT This is roughly the version that Windows NT and Windows 2000 support.VFAT hides all of the name restrictions from the user Longer filenames withoutseparators are provided by a very creative hack VFAT uses a chain of standardfile/directory name slots to transparently shoehorn extended filename supportinto the basic FAT filesystem structure For compatibility, every file and
$path='\dir\dir\filename') are just fine However, situations in whichyou need to place multiple backslashes next to each other (i.e.,
\\server\dir\file) are potential trouble In those cases, you have to be vigilant
in doubling any multiple backslashes Some Perl functions and some Perlmodules will accept paths with forward slashes, but this convention shouldnot be counted upon when programming It is better to bite the bullet andwrite \\\\winnt\\temp\ than to learn that your code breaks because theconversion hasn't been done for you
3 FAT files and directories have special flags associated with them that are
Trang 84on the planet
NTFS also has some functional differences that distinguish it from the otherWindows NT/2000 and basic Unix filesystems NTFS supports the notion of anAccess Control List (ACL) ACLs provide a fine-grained permission mechanismfor file and directory access Later on in this chapter we will write some code totake advantage of some of these differences
Before we move on to another operating system, it is important to at least
mention the Universal Naming Convention UNC is a convention for locatingthings (files and directories in our case) in a networked environment Instead of
the drive letter and a colon preceding an absolute path, the drive letter: part is replaced with \\server\sharename This convention suffers from the same Perl
backslash syntax clash we saw a moment ago As a result, it is not uncommon tosee a set of leaning toothpicks like this:
$path = "\\\\server\\sharename\\directory\\file"
2.2.3 MacOS
Despite its GUI-centrist approach, the MacOS Hierarchical File System (HFS)also lets users specify textual pathnames, albeit with a few twists Absolute
Trang 85as of this writing
A more significant departure from the previous two operating systems (at leastfrom a Perl programming point of view) is MacOS's use of the "fork" idiom for
its file storage Each file is said to have a data fork and a resource fork The
former holds the data part of the file, while the latter contains a variety of
different resources These resources can include executable code (in the case of a
program), user interface specifications (dialog boxes, fonts, etc.), or any othercomponents a programmer wishes to define Though we won't be dealing withforks per se this chapter, MacPerl does have facilities for reading and writing toboth forks
Trang 86Sensitive?
Case-Filename Specification Length
Absolute Path Format
Relative Path Format
Unique Features
OS-/dir/file dir/file
dependentadditions
OS-variant-MacOS
31 chars (or
255 if usingHFS+)
volume:dir:file :dir:file
Data/resourceforks,
creator/typeattributes
WinNT/2000
(NTFS) \ N 255 chars Drive:\dir\file dir\file
ACLs,attributes,Unicodenames
DOS
Trang 872.2.5 Dealing with Filesystem Differences from Perl
Perl can help you write code that takes most of these filesystem quirks intoaccount It ships with a module called File::Spec to hide some of the
Copyright © 2001 O'Reilly & Associates All rights reserved.
Trang 89Now that we've had a good look at user identity, we can begin to address theadministration aspect of user accounts Rather than just show you the select Perlsubroutines or function calls you need for user addition and deletion, we're going
to take this topic to the next level by showing these operations in a larger
context In the remainder of this chapter, we're going to work towards writing abare-bones account system that starts to really manage both NT and Unix users
Our account system will be constructed in four parts: user interface, data storage,process scripts (Microsoft would call them the "business logic"), and low-levellibrary routines From a process perspective they work together (see Figure 3-2)
Figure 3.2 The structure of a basic account system
Requests come into the system through a user interface and get placed into an
"add account queue" file for processing We'll just call this an "add queue" fromhere on in A process script reads this queue, performs the required accountcreations, and stores information about the created accounts in a separate
database That takes care of adding the users to our system
For removing a user, the process is similar A user interface is used to create a
"remove queue." A second process script reads this queue and deletes the users
Trang 90We isolate these operations into separate conceptual parts because it gives us themaximum possible flexibility should we decide to change things later For
instance, if some day we decide to change our database backend, we only need
to modify the low-level library routines Similarly, if we want our user additionprocess to include additional steps (perhaps cross-checking against another
database in Human Resources), we will only need to change the process script inquestion.Let's start by looking at the first component: the user interface used tocreate the initial account queue For the bare-bones purposes of this book, we'lluse a simple text-based user interface to query for account parameters:
sub CollectInformation{
# list of fields init'd here for demo purposes, this should # really be kept in a central configuration file
Once the list has been created, the routine iterates through it and requests thevalue for each field Each value is then stored back into the record hash At theend of the question and answer session, a reference to this hash is returned forfurther processing Our next step will be to write the information to the addqueue Before we see this code, we should talk about data storage and data
formats for our account system
3.3.1 The Backend Database
The center of any account system is a database Some administrators use their
/etc/passwd file or SAM database as the only record of the users on their system,