#!/usr/bin/perl -w # # $Id$ # # The author disclaims all copyrights and releases this script into the # public domain. # # Converts /etc/passwd data into a .ini format. use strict; use Getopt::Std; my %opts; getopts 'a:c:', \%opts; # build any 'system_account=true;foo=bar' pairs into defaults hash my %defaults; if ( exists $opts{a} ) { %defaults = map { split /(?) { next if m/^#/; # OS X has leading comments chomp; my %user = %defaults; my @fields = split /:/, $_; # KLUGE assume 7 == standard, 10 == +class/change/expire fields. Will # likely nned "on $OS, use the following fields names" lookup table... if ( @fields == 7 ) { @user{qw(name passwd id gid gecos home shell)} = @fields; } elsif ( @fields == 10 ) { @user{qw(name passwd id gid class change expire gecos home shell)} = @fields; } elsif ( @fields == 6 ) { # KLUGE seen this on Linux, assume shell not set... @user{qw(name passwd id gid gecos home shell)} = ( @fields, $false_shell ); } else { die "error: unknown number of fields: count=", scalar @fields, ", file=$ARGV, line=$.\n"; } unless ( exists $user{name} and defined $user{name} and exists $user{id} and defined $user{id} ) { warn "warning: skipping invalid user entry: line=$.\n"; next; } push @users, \%user; } continue { close ARGV if eof; } for my $user ( sort { $a->{id} <=> $b->{id} } @users ) { print "[$user->{name}]\n"; print " passwd = $user->{passwd}\n" unless $user->{passwd} =~ m/^\*/ or $user->{passwd} eq 'x'; $user->{gecos} ||= q{}; $user->{home} ||= q{}; $user->{shell} ||= $false_shell; print " id = $user->{id}\n"; print " gid = $user->{gid}\n" unless $user->{id} == $user->{gid}; print " realname = $user->{gecos}\n"; print " homedir = $user->{home}\n"; print " shell = $user->{shell}\n"; # KLUGE print additional fields. probably should template this... for my $entry ( qw(class change expire), keys %defaults ) { next if not exists $user->{$entry}; next if not defined $user->{$entry} or $user->{$entry} eq q{}; next if $user->{$entry} eq '0'; print " $entry = $user->{$entry}\n" if exists $user->{$entry}; } print "\n"; }