[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [cobalt-users] Importing MANY users (!!?!!)



Hi Bob,

See the attached file.  Watch your formatting carefully, enjoy.

Thanks for the p.s. below, a very good point and noticed too.


	-- Will

Bob Terry wrote:
> 
> Hi all,
> 
> I am planning to move hundreds of mail accounts away from an old mail
> server to some of our Cobalt Raq2's.
> 
> Is there any easy solution to 'import' these users into the Cobalt,
> rather than entering them one by one?  It would be rather easy on a
> RH Linux box, but the Cobalt GUI (and related background scripts) may
> require other ways of doing this.
> 
> The answer _could_ affect our future use of Cobalt products...  Any
> help would be appreciated
> 
> Thanks in advance,
> 
> -Bob
> 
> p.s. Ya'd think Cobalt already had a utility for this, enticing
> people to *switch* to their product much easier...
> 
> --
> Bob Terry
> Senior Internet Systems Administrator
> Forward Network Communications, Inc
> ph:  773-395-0023 ext. 105
> fax: 773-395-0025
> http://www.forward.net
> 
> _______________________________________________
> cobalt-users mailing list
> cobalt-users@xxxxxxxxxxxxxxx
> To Subscribe or Unsubscribe, please go to:
> http://list.cobalt.com/mailman/listinfo/cobalt-users
#!/usr/bin/perl 

# raq2_useradd
# author: Jonathan Mayer <jmayer@xxxxxxxxxxxxx>
#
# Cobalt Networks, Inc 7/1/99
# This script is not officially supported 

use strict; 
use Getopt::Long;
use FileHandle;
use IPC::Open2;

# print help and exit:
my $help = 0;
my %subst = ();

my ($quota) = 99; 
my ($site) = `/bin/hostname`; chomp($site); 
my ($shell) = ""; 

my $ret = GetOptions (
	'help', \$help,
	'quota=i', \$quota,
	'site=s', \$site,
	'shell', \$shell,
);

if ($help || $#ARGV <0) {
	print STDERR <<eot ;
$0

Adds all users described in a text file.

Usage: $0 [--help] [--site <sitename>] [-quota <size>] <filename(s)>

The input files should contain a list of the users to be added, one
per line, according the the following syntax:

	<full name of user> , <username> , <password>

for example:
	John Doe, jdoe, secret

eot
	exit (1);
}

# Other variables read in from the file 
my ($fullname); 
my ($passwd); 
my ($uid); 
my ($f_name); 
my ($l_name); 
my ($line); 
my ($arg); 

my $group = site2group($site);

my $errs = 0;
foreach my $file (@ARGV)
{
	$errs += import_users ($file);
}
exit $errs;

sub site2group
{
	my $site = shift;
	$_ = `ls -ld /home/sites/$site`; chomp($_);
	if (m!$site -> /home/sites/(\S+)!) {
		return $1;
	} else {
		print STDERR $_,"\n";
		print STDERR "ERROR: Site \"$site\" does not exist on this system.\n";
		exit(1);
		return 0;
	}
}

sub import_users
{
	my $fname = shift;
	
	my $fh = new FileHandle("< $fname");
	if (!$fh) {
		print "Couldn't read: $fname\n";
		return 1;
	}

	my @data = <$fh>;
	$fh->close();
	$_ = join("",@data);
	@data = split(/\n[\s\n]*/, $_);
	
	my $line;
	foreach $line (@data)
	{
		$line =~ s/^\s*//;
		$line =~ s/\s*$//;
		# print STDERR "line: $line\n";
		($fullname, $uid, $passwd, $f_name) = split(/\s*,\s*/, $line);
		# print STDERR "attempting: fn=$fullname user=$uid pw=$passwd\n";

		# add user here:
		my $href = {
			'fullname' => $fullname,
			'username' => $uid,
			'password1' => $passwd,
			'password2' => $passwd,
			'diskquota' => $quota,
			'shell' => $shell,
			'siteadmin' => '',
			'aliases' => $uid,
			'group' => $group,
			'page' => 'siteuseradd',
		};
		
		my $data = encode($href);
		
		my $resp = invoke(
			'script' => '/usr/admserv/cgi-bin/.cobalt/siteUserAdd/siteUserAdd.cgi',
			'data' => $data,
			'query' => '',
			'method' => 'POST',
		);
		
		if ($resp !~/^location/) {
			print STDERR "data: $data\n";
			print STDERR "An error occurred when invoking the CGI.\n";
			print STDERR "This information might be useful:\n";
			my ($msg) = ($resp =~ /top.showInfo\s*\(\s*'(.*?)'/);
			print STDERR $msg,"\n";
			exit(1);
		}
		
		print STDERR "Successfully added user $uid to $site\n";
		# print STDERR "resp = $resp\n";
		
	}
	
}

##############################################

# stolen from URI::Escape.pm:
sub uri_escape
{
    my($text, $patn) = @_;

	# Build a char->hex map
	my %escapes = ();
	for (0..255) {
    	$escapes{chr($_)} = sprintf("%%%02X", $_);
	}


    return undef unless defined $text;
    if (defined $patn){
        unless (exists  $subst{$patn}) {
            # Because we can't compile regex we fake it with a cached sub
            $subst{$patn} =
              eval "sub {\$_[0] =~ s/([$patn])/\$escapes{\$1}/g; }";
            Carp::croak("uri_escape: $@") if $@;
        }
        &{$subst{$patn}}($text);
    } else {
        # Default unsafe characters. (RFC 2396 ^uric)
        $text =~ s/([^;\/?:@&=+\$,A-Za-z0-9\-_.!~*'()])/$escapes{$1}/g;
    }
    $text;
}

sub encode
{
	my $data = shift;

	if (ref($data) eq 'SCALAR') 
	{
		return $data;
	}

	if (ref($data) eq 'HASH')
	{
		my %h = %{$data};
		my @d = ();
		my ($key, $value);
		while( ($key, $value) = each %h ) {
			push (@d, uri_escape($key) . "=" . uri_escape($value));
		}
		$data = join("&", @d);
		return $data;
	}
	
	if (ref($data) eq 'ARRAY')
	{
		$data = join("&", @{$data});
		return $data;
	}
	
	return $data; # huh?
}

sub invoke
{
	my %p = @_;
	my $script = $p{script};
	my $data = $p{data};
	my $query = $p{query};
	my $method = $p{method};

	# if data is a hash, assemble it:
	$data = encode($data);
	$query = encode ($query);

	# print STDERR "Running: $script\n\tparams: $data\n";

	# set up environment:
	$ENV{SERVER_SOFTWARE} = "InvokeCGI 0.0";
	$ENV{SERVER_NAME} = "localhost";
	$ENV{GATEWAY_INTERFACE} = 'CGI/1.1';
	$ENV{QUERY_STRING} = $query;
	$ENV{CONTENT_TYPE} = 'application/x-www-form-urlencoded';
	$ENV{CONTENT_LENGTH} = length($data);
	if (defined($method)) { $ENV{REQUEST_METHOD} = $method; };
	# hopefully that's enough.

	# run script:
	my ($reader, $writer) = (IO::Handle->new, IO::Handle->new);
	my $kid = undef;
	eval {
		$kid = open2($reader, $writer, $script);
	};
	if ($@) {
		print STDERR "ack, couldn't run: $script\n";
		exit(1);
		return undef;
	}

	print $writer $data;
	$writer->close();

	$reader->input_record_separator(undef);
	my $resp = undef;
	my $tmp = $SIG{ALRM};
	$SIG{ALRM} = sub { die "timeout"; };
	eval {
		alarm(120);
		$resp = <$reader>;
		alarm(0);
	};
	
	if ($@) {
		die ("timed out");
		$resp = undef;
	}

	$reader->close();
	
	return $resp;	
}

sub POST
{
	invoke(
		'method' => 'POST',
		@_,
	);
}

1;