Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/perl -I/usr/sausalito/perl
- # Debugging switch (0|1|2):
- # 0 = off
- # 1 = log to syslog
- # 2 = log to screen
- #
- $DEBUG = "2";
- if ($DEBUG) {
- if ($DEBUG eq "1") {
- use Sys::Syslog qw( :DEFAULT setlogsock);
- }
- }
- #
- ### CCE:
- #
- use CCE;
- $cce = new CCE;
- $cce->connectuds();
- #
- ### Load required Perl modules:
- #
- use Getopt::Std;
- use Data::Dumper;
- #
- ### Check if we are 'root':
- #
- &root_check;
- #
- ### Command line option handling
- #
- %options = ();
- getopts("hq:v:u:p:t:a:d:f:", \%options);
- # Some Variables:
- $disk_quota = '1000'; # 1GB default
- $errors = '0';
- $vsite_oid = '';
- $vsite_name = '';
- $username = '';
- $password = '';
- $pw_type = 'crypted';
- @email_alias = ();
- $forward_email = '';
- # Handle display of help text:
- if ($options{h}) {
- &help;
- }
- # Find Vsite:
- if ($options{v}) {
- &header;
- print "Searching for Vsite " . $options{v} . "\n";
- $vsite_found = '0';
- @oids = $cce->find('Vsite', {'name' => $options{v}});
- if ($#oids == 0) {
- $vsite_found = '1';
- $vsite_name = $options{v};
- }
- else {
- @oids = $cce->find('Vsite', {'fqdn' => $options{v}});
- if ($#oids == 0) {
- $vsite_found = '1';
- ($ok, $obj) = $cce->get($oids[0]);
- $vsite_name = $obj->{'name'};
- }
- }
- if ($vsite_found eq '0') {
- print "Vsite '" . $options{v} . "' not found! Please check the name and try again!\n";
- $cce->bye("FAIL");
- exit(1);
- }
- else {
- $vsite_oid = $oids[0];
- print "Vsite '" . $options{v} . "' found! [OID:$vsite_oid|Group:$vsite_name]\n";
- }
- }
- else {
- print "\nERROR: You must specify a Vsite with the -v parameter!\n\n";
- &help;
- }
- # Username:
- if ($options{u}) {
- # Check if Username already exists:
- if (&cce_find_user($options{u})) {
- print "User '" . $options{u} . "' already exists! Aborting transaction.\n";
- $cce->bye("FAIL");
- exit(1);
- }
- else {
- print "Username '" . $options{u} . "' is available.\n";
- $username = $options{u};
- }
- }
- else {
- print "\nERROR: You must specify a Username with the -u parameter!\n\n";
- &help;
- }
- # Password:
- if ($options{p}) {
- if ($options{p} ne "") {
- $password = $options{p};
- }
- }
- else {
- print "\nERROR: You must specify a Password with the -p parameter!\n\n";
- &help;
- }
- # Password Type:
- if ($options{t}) {
- if (($options{t} eq "plain") || ($options{t} eq "plaintext")) {
- $pw_type = 'plaintext'
- }
- }
- print "Using password type '" . $pw_type . "'.\n";
- # Email Alias:
- if ($options{a}) {
- if ($options{a} ne "") {
- @email_alias = split /,/, $options{a};
- foreach my $alias (@email_alias) {
- # Check if alias already exists:
- @oids_s_alias = $cce->find("EmailAlias", {'alias' => $alias, 'site' => $vsite_name});
- if (scalar(@oids_s_alias)) {
- print "ERROR: EmailAlias '" . $alias . "' already exists! Aborting transaction.\n";
- $cce->bye("FAIL");
- exit(1);
- }
- else {
- print "EmailAlias '" . $alias . "' is available.\n";
- }
- }
- }
- }
- # Disk Quota:
- if ($options{d}) {
- if ($options{d} ne "") {
- $disk_quota = $options{d};
- }
- }
- # Email-Forwarding:
- if ($options{f}) {
- if ($options{f} ne "") {
- $forward_email = $options{f};
- }
- }
- #
- ### Actual User creation:
- #
- $do_user->{site} = $vsite_name;
- $do_user->{name} = $username;
- $do_user->{fullName} = $username;
- if ($pw_type eq "crypted") {
- $do_user->{md5_password} = $password;
- }
- $do_user->{password} = '';
- #
- ### CREATE the main User Object:
- #
- ($ok) = $cce->create("User", $do_user, '');
- # Check result:
- if ($ok ne "1") {
- # Increment error counter:
- $errors++;
- print "\nERROR: User creation failed!\n\n";
- print Dumper($cce);
- }
- if ($pw_type eq "crypted") {
- # Set Password-Hash:
- print "Setting password via: /usr/sbin/usermod $username -p '" . $password . "'\n";
- system("/usr/sbin/usermod $username -p '" . $password . "'");
- }
- else {
- # Set Plaintext-Password:
- print "Setting password via: echo \"$password\" | passwd \"$username\" --stdin\n";
- system("echo \"$password\" | passwd \"$username\" --stdin");
- }
- #
- ### EmailAlias:
- #
- if (scalar(@email_alias)) {
- $do_user_extra->{Email}->{aliases} = array_to_scalar(@email_alias);
- }
- #
- ### Email Forwarding:
- #
- if ($forward_email ne '') {
- $do_user_extra->{Email}->{forwardEnable} = '1';
- $do_user_extra->{Email}->{forwardSave} = '0';
- $do_user_extra->{Email}->{forwardEmail} = '&' . $forward_email . '&';
- }
- #
- ### Disk Quota:
- #
- $user_OID = &cce_find_user($username);
- $do_user_extra->{Disk}->{quota} = $disk_quota;
- # Loop through all NameSpaces:
- foreach $uon ( keys %{ $do_user_extra } ) {
- # Perform SET transaction:
- ($ok) = $cce->set($user_OID, "$uon", $do_user_extra->{$uon});
- delete $do_user_extra->{$uon};
- # Check result:
- if ($ok ne "1") {
- # Increment error counter:
- $errors++;
- print "\nERROR: User quota/alias update failed!\n\n";
- print Dumper($cce);
- }
- }
- if ($errors ne '0') {
- print "Error count: $errors\nRemoving partially created 'User' object from CODB.\n";
- ($ok) = $cce->destroy($user_OID);
- }
- $cce->bye("SUCCESS");
- exit(0);
- #
- ### Subs:
- #
- sub root_check {
- my $id = `id -u`;
- chomp($id);
- if ($id ne "0") {
- #print "$0 must be run by user 'root'!\n\n";
- &help("$0 must be run by user 'root'!");
- }
- }
- sub debug_msg {
- if ($DEBUG eq "1") {
- $msg = shift;
- $user = $ENV{'USER'};
- setlogsock('unix');
- openlog($0,'','user');
- syslog('info', "$ARGV[0]: $msg");
- closelog;
- }
- if ($DEBUG eq "2") {
- my $msg = shift;
- print $msg;
- }
- }
- sub cce_find_user {
- my $u = shift || "";
- @oids = $cce->find("User", {"name" => $u});
- if (scalar(@oids) eq "1") {
- return $oids[0];
- }
- }
- sub header {
- print "########################################################### \n";
- print "# bx-user-import.pl: BlueOnyx Generic User Import Utility #\n";
- print "###########################################################\n\n";
- }
- sub help {
- $error = shift || "";
- &header;
- if ($error) {
- print "ERROR: $error\n\n";
- }
- print "usage: bx-user-import.pl [OPTION]\n";
- print " -v Specify Vsite (FQDN or 'siteX') of User you want to create.\n";
- print " -u Username of the User you want to create.\n";
- print " -p Password of the User you want to create.\n";
- print " -t Type of password: 'crypted' (default) or 'plaintext'.\n";
- print " -a Email alias for this User in the form of a single word (optional).\n";
- print " -d Disk Quota in MB (Optional. Defaults to 1000MB if not specified.)\n";
- print " -f Email Address to which emails of this user are forwarded to (Optional)\n";
- print " -h help, this help text\n\n";
- $cce->bye("SUCCESS");
- exit(0);
- }
- # pack and unpack arrays
- sub array_to_scalar {
- my $scalar = "&";
- if ($_ eq "undef") {
- next;
- }
- while (defined($_ = shift)) {
- $scalar .= $_ . '&';
- }
- if ($scalar eq "&") {
- $scalar = ""; # special case
- }
- return $scalar;
- }
- sub scalar_to_array {
- my $scalar = shift || "";
- $scalar =~ s/^&//;
- $scalar =~ s/&$//;
- my @data = split(/&/, $scalar);
- for ($i = 0; $i <= $#data; $i++) {
- $data[$i] =~ s/\+/ /g;
- $data[$i] =~ s/%([0-9a-fA-F]{2})/chr(hex($1))/ge;
- }
- return @data;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement