Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

List users without homes and homes without users

by codeout (Initiate)
on May 16, 2018 at 18:06 UTC ( #1214675=perlquestion: print w/replies, xml ) Need Help??
codeout has asked for the wisdom of the Perl Monks concerning the following question:

I'm trying to develop a Perl script to look for orphaned homes (home directories without users, or the inverse). This script should generate a formatted listing of any user without a home directory... ...or any home directory (a directory in /home) that is not attached to a user. The identifying element is the UID, not the username. I need to understand the difference between running the program as root as well as running it without root privileges

I'm using file handles to grab information from the OS about homes and users -- and put that information into arrays. The script would essentially be running external shell processes and reading from those processes' output just like reading from a text file. This code, will be executed on a standard Linux OS.

Array for the users
open (USERS, '-|' , 'getent passwd' ) or die $!; @passwd_entries = <USERS>;
Array for the homes
open (HOMES, '-|' , 'ls /home' ) or die $!; @home_dirs = <HOMES>; close HOMES;

Replies are listed 'Best First'.
Re: List users without homes and homes without users
by poj (Monsignor) on May 16, 2018 at 18:38 UTC

    This same question was asked just 3 weeks ago by wap, so is this course work ?

Re: List users without homes and homes without users
by trippledubs (Chaplain) on May 16, 2018 at 18:27 UTC

    Perl has the getpwent function from C. You can use that directly as a perl function if you want. You might need the stat function. Also linux has the pwck tool that identifies some of the problems you describe. It's just as fine to do it the way you're going. /etc/passwd is very parseable as text, another way to do it.

    If you run the program as root, your program has all the permissions as root. So if you make a mistake in your programming and accidentally tell perl to wipe out your password file, the OS is not going to stop it from doing that, it is allowed. If your program runs with more limited permissions, the OS will prevent it from doing things it is not allowed to do.

    Perl has more to offer in this venture also. You could make your program aggregate the results into an excel spreadsheet, e-mail you the results, fix them right there, etc. To actually fix the results you would probably need more permissions.

Re: List users without homes and homes without users
by Anonymous Monk on May 16, 2018 at 18:31 UTC

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1214675]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (5)
As of 2018-11-13 02:18 GMT
Find Nodes?
    Voting Booth?
    My code is most likely broken because:

    Results (149 votes). Check out past polls.