#!/usr/local/bin/perl -w use strict; ## Scott Wiersdorf ## Created: Tue Feb 3 16:45:48 GMT 2004 ## $Id: pwd_migrate,v 1.1 2005/08/15 18:25:30 scott Exp $ ## add accounts on vps v2 from vps1-style passwd file =pod scott:37xp9OdddQuVE:10658:100:Scott Wiersdorf:/:ftp;mail ftp:*:10658:100:Ftp Account:/ftp:ftp bill:iBXpa77yyykn2:10658:100:Bill:/usr/home/bill:mail,10 dan:17J9cGLVFbAe.:965:100:dan:/usr/home/dan:ftp;mail bosko:HgVZmgwTm2TI2:2930:100::Bosko:/ daffy:65StNJSvLCq6A:10656:100:Daffy Duck:/:ftp;mail dumbo:cEDRtHQv/W4n2:10651:100:Dumbo:/:ftp;mail fff:q6IUyVx1J3r2g:16213:100:Freddy Freeloader Foo Inc:/:ftp;mail b:2h3Ik9Yq4Gw6w:16213:100:b:/usr/home/b:ftp;mail jerry:Ppd3psKiOkl1Y:10646:100:Jerry:/:ftp;mail porky:eBvNBR8mjDdlk:2944:100:Porky Pig:/usr/home/porky:ftp;mail b:.uju2Baxy93YE:2944:100:J. Bee:/usr/local/etc/httpd/htdocs/b:noshell c:2UYGA1XGJodYm:2944:100:J. Cee:/ftp/pub/c:noshell aaa:nu9F0WD3L/7Ii:2944:100:J. Jonah Jameson:/usr/home/aaa:ftp,10;mail,1 jjj:07s2LUxUpfRAD:2944:100:J. Jonah Jameson:/usr/home/jjj:ftp,5;mail,10 joe:5xQ7k3cUxEgSE:2944:100:Joe:/usr/home/joe:ftp,3;mail =cut ## to do ## ## - offer an rsync option to suck down the old home directory ## contents to the new location use Getopt::Std; use vars qw($opt_d); getopts('d'); my $file = shift or usage(); open FILE, $file or die "Could not open '$file': $!\n"; while( ) { chomp; my($user,$pwd,undef,undef,$gecos,undef,$serv) = split(':', $_); print "####################\n" if $opt_d; if( defined getpwnam($user) ) { warn "Skipping existing user '$user'\n"; next; } $gecos = '&' unless $gecos; print "Entry: $user -- $pwd -- $gecos -- $serv\n" if $opt_d; ## get quota and service information my %services = (); for my $servquota ( grep { /^(?:mail|ftp)/ } split(';', $serv) ) { my($service, $quota) = split(',', $servquota); $services{$service} = ( defined $quota ? $quota : 0 ); } if( $opt_d ) { for my $service ( keys %services ) { print " $service quota for $user: $services{$service}\n"; } } my $services = join(',', keys %services); my $quota = 0; $quota += $services{$_} for keys %services; print "Quota for user $user: $quota\n" if $opt_d; print "Services for user $user: $services\n" if $opt_d; next if $opt_d; ## skip bogus users unless( $services ) { warn "Skipping user with no services '$user'\n"; next; } ## add the user system('vadduser', "--login=$user", "--home=/home/$user", "--fullname=$gecos", "--services=$services", "--quota=$quota", '--password=dummy') and do { warn "Could not add user '$user'\n"; next; }; system('chpass', '-p', $pwd, $user) and do { warn "Could not set password for '$user': password is 'dummy'\n"; next; }; print "New $services account added for '$user' (quota $quota)\n"; } close FILE; exit; sub usage { die <<_USAGE_; pwd_migrate passwd_file [quota [services]] examples: 1) pwd_migrate passwd.old will create accounts for all entries in passwd.old (except 'root' and 'ftp') with a 50MB quota and mail service only (no FTP access). 2) pwd_migrate passwd.old 10 ftp,mail will create accounts for all entries in passwd.old (except 'root' and 'ftp') with a 10MB quota and ftp and mail service. _USAGE_ } =pod $Log: pwd_migrate,v $ Revision 1.1 2005/08/15 18:25:30 scott - import sysadmin part of site - added 'psmon' Revision 1.8 2004/12/08 21:17:57 scott fix typo Revision 1.7 2004/02/04 21:52:44 scott - add default gecos string '&' Revision 1.6 2004/02/03 18:10:00 scott - next Revision 1.5 2004/02/03 18:09:23 scott - skip users with no services Revision 1.4 2004/02/03 18:06:15 scott - fix syntax boo boo Revision 1.3 2004/02/03 18:04:54 scott - create account with existing services and quotas Revision 1.2 2004/02/03 17:24:36 scott - add log directive =cut