"Fossies" - the Fresh Open Source Software Archive

Member "passwd_exp-1.2.11/mod/passwd.bsd" (21 Nov 2005, 6458 Bytes) of package /linux/privat/old/passwd_exp-1.2.11.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Perl source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 #! /usr/bin/perl
    2 # PROGRAM   : passwd.fbsd (passwd_exp 1.x helper)
    3 # PURPOSE   : create input list from 4.3BSD passwd format file
    4 # AUTHOR    : Samuel Behan <samkob@gmail.com> (c) 2000-2006
    5 # HOMEPAGE  : http://devel.dob.sk/passwd_exp
    6 # LICENSE   : GNU GPL v2, NO WARRANTY VOID (see file LICENSE)
    7 ################################################################################
    8 #requirements
    9 use POSIX    qw(uname);
   10 #pragmas
   11 #use strict;
   12 use integer;
   13 use vars qw($VERSION $AUTHOR $AUTHOR_EMAIL $SCRIPT);
   14 
   15 #script info
   16 $AUTHOR     = 'Samuel Behan';
   17 $AUTHOR_EMAIL   = 'samkob@gmail.com';
   18 $VERSION    = '0.1.0';
   19 $SCRIPT     = 'passwd.fbsd';
   20 
   21 #config defaults
   22 my %CONFIG;
   23 $CONFIG{-shells_file}   = '/etc/shells';
   24 $CONFIG{"passwd"}   = '/etc/passwd';
   25 $CONFIG{"host"}     = ( uname() )[1];
   26 $CONFIG{"login_conf"}   = '/etc/login.conf';
   27 $CONFIG{-path}      = (@_ = split('/', $0), pop @_, join('/', @_)); 
   28 
   29 ##constants
   30 #shadow
   31 sub PW_NAME     {   0;  };
   32 sub PW_PASSWD       {   1;  };
   33 sub PW_UID      {   2;  };
   34 sub PW_GID      {   3;  };
   35 sub PW_CLASS        {   4;  };
   36 sub PW_LAST_CHANGE  {   5;  };
   37 sub PW_EXPIRE_DATE  {   6;  };
   38 sub PW_GECOS        {   7;  };
   39 sub PW_DIR      {   8;  };
   40 sub PW_SHELL        {   9;  };
   41 sub PW__MAX     {   10; };
   42 
   43 #print info
   44 sub cmd_print_info(;$)
   45 {   
   46     print STDERR <<__EOT__
   47 usage: $SCRIPT [command] [options]
   48       -- $SCRIPT (passwd_exp helper) $VERSION by $AUTHOR 
   49 [command]
   50     info            print module info
   51 [options]
   52     uid_min=INT     set minimum UID
   53     uid_max=INT     set maximum UID
   54     uid_regexp=EXPR     check UID against regexp
   55     gid_min=INT     set minimum GID
   56     gid_max=INT     set minimum GID
   57     gid_regexp=EXPR     check GID against regexp
   58     group_regexp=EXPR   check group name against regexp
   59     class_regexp=EXPR   check class name against regexp
   60     host=HOST       set host name
   61     shell=0/1       require valid shell (from '$CONFIG{-shells_file}')
   62     passwd=FILE     set passwd file to use (default '$CONFIG{passwd}')
   63     login_conf=FILE     login.conf file to use (default '$CONFIG{login_conf}')
   64 __EOT__
   65 ;
   66     exit(defined($_[0]));
   67 }
   68 
   69 
   70 ################
   71 # MAIN PROGRAM #
   72 ################
   73 my ($cmd, $line, $linenum, @pwdata, @pcdata);
   74 my (@SHELLS, %LOGIN);
   75 
   76 #parse command line
   77 foreach $cmd (@ARGV)
   78 {
   79     cmd_print_info() if($cmd eq '-info' || $cmd eq '--info' || $cmd eq '-h' ||
   80                 $cmd eq '-help' || $cmd eq '--help' || $file ne '');
   81     cmd_print_info(1) if(substr($cmd, 0, 1) eq '-' || substr($cmd, 0, 2) eq '--');
   82     if($cmd =~ /^(\w+)=(.*)$/o)
   83     {   $CONFIG{$1} = $2 if(defined($2) && $2 ne '');
   84         next;   }
   85     warn("$SCRIPT: unknown argument '$cmd'\n")
   86 }
   87 #check params
   88 die("$SCRIPT: missing host name (try passing host=HOST), exiting...\n")
   89     if(!defined($CONFIG{"host"}) || $CONFIG{"host"} eq '');
   90 
   91 #open passwd file
   92 die("$SCRIPT: failed to open passwd file '$CONFIG{passwd}' ($!)\n")
   93     if(!open(PASSWD, $CONFIG{passwd}));
   94 
   95 #test if login.conf exists and is readable
   96 die("$SCRIPT: failed to read $CONFIG{login_conf} file ($!)\n")
   97     if(!(-e $CONFIG{"login_conf"} && -r $CONFIG{"login_conf"}));
   98 
   99 #read whole login conf file
  100 warn("$SCRIPT: can not get login.conf entries ($!)\n")
  101     if(!open(LOGIN_CONF, "_termcap.reader -all $CONFIG{login_conf}|") &&
  102         !open(LOGIN_CONF, "$CONFIG{-path}/_termcap.reader -all $CONFIG{login_conf}|"));
  103 while(defined(($line = <LOGIN_CONF>)))
  104 {
  105     chomp($line);
  106     $line =~ s/^(.+?):://o;
  107     $LOGIN{$1} = $line;
  108 }
  109 close(LOGIN_CONF);
  110 
  111 #read shell file
  112 if(defined($CONFIG{"shell"}) && $CONFIG{"shell"} == 1)
  113 {   
  114     die("$SCRIPT: can not get $CONFIG{-shells_file} entries ($!)\n")
  115         if(!open(SHELL, "_shell.reader $CONFIG{-shells_file}|") &&
  116             !open(SHELL, "$CONFIG{-path}/_shell.reader $CONFIG{-shells_file}|"));
  117     while(defined(($cmd = <SHELL>)))
  118     {   chomp($cmd);
  119         push(@SHELLS, $cmd);    }
  120     close(SHELL);
  121 }
  122 
  123 ##
  124 # get_login_config($class, $option)
  125 sub get_login_conf($$)
  126 {
  127     my $class = $_[0];
  128     my $opt = $_[1];
  129     my $retval;
  130 
  131     #test for class definition
  132     $class = "default" if(!defined($class) || $class eq '' || !exists($LOGIN{$class}));
  133     #match value
  134     $retval = $1 if($LOGIN{$class}  =~ /:$opt=(.*?):/);
  135     #check for redir to another class
  136     if($retval eq '' && $LOGIN{$class} =~ /:tc=(.*?):/)
  137     {   return get_login_conf($1, $opt);    }
  138     else    #or return loaded value
  139     {   return $retval;         }
  140 }
  141 
  142 ##
  143 # eval_to_days($value)
  144 sub eval_to_days($)
  145 {
  146     my $value = $_[0];
  147     my %mod2sec = (
  148         "y" => 31536000,
  149         "w" => 604800,
  150         "d" => 86400,
  151         "h" => 3600,
  152         "m" => 60,
  153         "s" => 1
  154         );
  155     my $retval = 0;
  156 
  157     #transform time
  158     while(defined($value) && $value ne '')
  159     {
  160         #match any string, very relax format checking
  161         $value =~ s/^(\d*?)(\w)//;
  162         $retval += $1 * (exists($mod2sec{$2}) ? $mod2sec{$2} : 0);
  163     }
  164     #return in days
  165     $retval = $retval / 86400;
  166     return $retval;
  167 }
  168 
  169 #read passwd
  170 $linenum = 0;
  171 while(defined(($line = <PASSWD>)))
  172 {
  173     $linenum++;
  174     chomp($line);
  175 
  176     #check empty lines
  177     next    if($line =~ /^#/o || $line eq '' || $line =~ /^\s+$/o);
  178 
  179     #split data & check them
  180     @pwdata = split(/:/o, $line, PW__MAX);
  181     
  182     #notice days for account expire
  183     my $warn_exp    = get_login_conf($pwdata[PW_CLASS], "warnexpire");  
  184     if(!defined($warn_exp) || $warn_exp eq '')
  185     {   $warn_exp = 0;              }
  186     else
  187     {   $warn_exp = eval_to_days($warn_exp);    }
  188 
  189     #notice days for passwd expire
  190     my $warn_pass   = get_login_conf($pwdata[PW_CLASS], "warnpassword") ;   
  191     if(!defined($warn_pass) || $warn_pass eq '')
  192     {   $warn_pass = 0;             }
  193     else
  194     {   $warn_pass = eval_to_days($warn_pass);  }
  195     # HACK FOR report engine (we got currently only one global expire field
  196     $warn_exp = $warn_pass if($warn_pass > $warn_exp);
  197 
  198     #notice days for account inactivation
  199     my $warn_inact = get_login_conf($pwdata[PW_CLASS], "graceexpire") ; 
  200     if(!defined($warn_inact) || $warn_inact eq '')
  201     {   $warn_inact = 0;            }
  202     else
  203     {   $warn_inact = eval_to_days($warn_inact);}
  204 
  205     #test if account active/enabled and need be reported
  206     next    if(($pwdata[PW_PASSWD] eq '*' || $pwdata[PW_PASSWD] eq '!!')        #disabled
  207             || ((!$pwdata[PW_LAST_CHANGE] || $pwdata[PW_LAST_CHANGE] <= 0)  #never used
  208             && (!$pwdata[PW_EXPIRE_DATA] && $pwdata[PW_EXPIRE_DATA] <= 0)));#never expires
  209 
  210     #transform times
  211     $pwdata[PW_LAST_CHANGE] = $pwdata[PW_LAST_CHANGE] / 86400;
  212     $pwdata[PW_EXPIRE_DATE] = $pwdata[PW_EXPIRE_DATE] / 86400;
  213 
  214     #assemble data
  215     @pcdata = ($pwdata[PW_NAME],                #name
  216         $pwdata[PW_GECOS],              #info
  217         $pwdata[SH_NAME].'@'.$CONFIG{"host"},       #email
  218         $pwdata[SH_LAST_CHANGE],            #edate
  219         $pwdata[SH_EXPIRE_DATE],            #adate
  220         $warn_pass,                 #wdays
  221         $warn_inact,                    #idays
  222         0                       #nosend
  223         );
  224 
  225     #send data
  226     syswrite(STDOUT, join(":", @pcdata)."\n");
  227 
  228     #undef data
  229     undef @pcdata;
  230     undef @pwdata;
  231 }
  232 
  233 #finish
  234 close(PASSWD);
  235 
  236 #EOF (c) by UN*X 1970-$EOD (End of Days) [ EOD (c) by God ]