Advertisement
Guest User

BX Single User Creation v2

a guest
Dec 7th, 2017
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 7.78 KB | None | 0 0
  1. #!/usr/bin/perl -I/usr/sausalito/perl
  2.  
  3. # Debugging switch (0|1|2):
  4. # 0 = off
  5. # 1 = log to syslog
  6. # 2 = log to screen
  7. #
  8. $DEBUG = "2";
  9. if ($DEBUG) {
  10.     if ($DEBUG eq "1") {
  11.         use Sys::Syslog qw( :DEFAULT setlogsock);
  12.     }
  13. }
  14.  
  15. #
  16. ### CCE:
  17. #
  18.  
  19. use CCE;
  20. $cce = new CCE;
  21. $cce->connectuds();
  22.  
  23. #
  24. ### Load required Perl modules:
  25. #
  26.  
  27. use Getopt::Std;
  28. use Data::Dumper;
  29.  
  30. #
  31. ### Check if we are 'root':
  32. #
  33. &root_check;
  34.  
  35. #
  36. ### Command line option handling
  37. #
  38.  
  39. %options = ();
  40. getopts("hq:v:u:p:t:a:d:f:", \%options);
  41.  
  42. # Some Variables:
  43. $disk_quota = '1000'; # 1GB default
  44. $errors = '0';
  45. $vsite_oid = '';
  46. $vsite_name = '';
  47. $username = '';
  48. $password = '';
  49. $pw_type = 'crypted';
  50. @email_alias = ();
  51. $forward_email = '';
  52.  
  53. # Handle display of help text:
  54. if ($options{h}) {
  55.     &help;
  56. }
  57.  
  58. # Find Vsite:
  59. if ($options{v}) {
  60.     &header;
  61.     print "Searching for Vsite " . $options{v} . "\n";
  62.  
  63.     $vsite_found = '0';
  64.     @oids = $cce->find('Vsite', {'name' => $options{v}});
  65.     if ($#oids == 0) {
  66.         $vsite_found = '1';
  67.         $vsite_name = $options{v};
  68.     }
  69.     else {
  70.         @oids = $cce->find('Vsite', {'fqdn' => $options{v}});
  71.         if ($#oids == 0) {
  72.             $vsite_found = '1';
  73.             ($ok, $obj) = $cce->get($oids[0]);
  74.             $vsite_name = $obj->{'name'};
  75.         }
  76.     }
  77.     if ($vsite_found eq '0') {
  78.         print "Vsite '" . $options{v} . "' not found! Please check the name and try again!\n";
  79.         $cce->bye("FAIL");
  80.         exit(1);
  81.     }
  82.     else {
  83.         $vsite_oid = $oids[0];
  84.         print "Vsite '" . $options{v} . "' found! [OID:$vsite_oid|Group:$vsite_name]\n";
  85.     }
  86. }
  87. else {
  88.     print "\nERROR: You must specify a Vsite with the -v parameter!\n\n";
  89.     &help;
  90. }
  91.  
  92. # Username:
  93. if ($options{u}) {
  94.     # Check if Username already exists:
  95.     if (&cce_find_user($options{u})) {
  96.         print "User '" . $options{u} . "' already exists! Aborting transaction.\n";
  97.         $cce->bye("FAIL");
  98.         exit(1);
  99.     }
  100.     else {
  101.         print "Username '" . $options{u} . "' is available.\n";
  102.         $username = $options{u};
  103.     }
  104. }
  105. else {
  106.     print "\nERROR: You must specify a Username with the -u parameter!\n\n";
  107.     &help;
  108. }
  109.  
  110. # Password:
  111. if ($options{p}) {
  112.     if ($options{p} ne "") {
  113.         $password = $options{p};
  114.     }
  115. }
  116. else {
  117.     print "\nERROR: You must specify a Password with the -p parameter!\n\n";
  118.     &help;
  119. }
  120.  
  121. # Password Type:
  122. if ($options{t}) {
  123.     if (($options{t} eq "plain") || ($options{t} eq "plaintext")) {
  124.         $pw_type = 'plaintext'
  125.     }
  126. }
  127. print "Using password type '" . $pw_type . "'.\n";
  128.  
  129. # Email Alias:
  130. if ($options{a}) {
  131.     if ($options{a} ne "") {
  132.         @email_alias = split /,/, $options{a};
  133.         foreach my $alias (@email_alias) {
  134.             # Check if alias already exists:
  135.             @oids_s_alias = $cce->find("EmailAlias", {'alias' => $alias, 'site' => $vsite_name});
  136.             if (scalar(@oids_s_alias)) {
  137.                 print "ERROR: EmailAlias '" . $alias . "' already exists! Aborting transaction.\n";
  138.                 $cce->bye("FAIL");
  139.                 exit(1);
  140.             }
  141.             else {
  142.                 print "EmailAlias '" . $alias . "' is available.\n";
  143.             }
  144.         }
  145.     }
  146. }
  147.  
  148. # Disk Quota:
  149. if ($options{d}) {
  150.     if ($options{d} ne "") {
  151.         $disk_quota = $options{d};
  152.     }
  153. }
  154.  
  155. # Email-Forwarding:
  156. if ($options{f}) {
  157.     if ($options{f} ne "") {
  158.         $forward_email = $options{f};
  159.     }
  160. }
  161.  
  162. #
  163. ### Actual User creation:
  164. #
  165.  
  166. $do_user->{site} = $vsite_name;
  167. $do_user->{name} = $username;
  168. $do_user->{fullName} = $username;
  169. if ($pw_type eq "crypted") {
  170.     $do_user->{md5_password} = $password;
  171. }
  172. $do_user->{password} = '';
  173.  
  174. #
  175. ### CREATE the main User Object:
  176. #
  177.  
  178. ($ok) = $cce->create("User", $do_user, '');
  179.  
  180. # Check result:
  181. if ($ok ne "1") {
  182.     # Increment error counter:
  183.     $errors++;
  184.     print "\nERROR: User creation failed!\n\n";
  185.     print Dumper($cce);
  186. }
  187.  
  188. if ($pw_type eq "crypted") {
  189.     # Set Password-Hash:
  190.     print "Setting password via: /usr/sbin/usermod $username -p '" . $password . "'\n";
  191.     system("/usr/sbin/usermod $username -p '" . $password . "'");
  192. }
  193. else {
  194.     # Set Plaintext-Password:
  195.     print "Setting password via: echo \"$password\" | passwd \"$username\" --stdin\n";
  196.     system("echo \"$password\" | passwd \"$username\" --stdin");
  197. }
  198.  
  199. #
  200. ### EmailAlias:
  201. #
  202.  
  203. if (scalar(@email_alias)) {
  204.     $do_user_extra->{Email}->{aliases} = array_to_scalar(@email_alias);
  205. }
  206.  
  207. #
  208. ### Email Forwarding:
  209. #
  210. if ($forward_email ne '') {
  211.     $do_user_extra->{Email}->{forwardEnable} = '1';
  212.     $do_user_extra->{Email}->{forwardSave} = '0';
  213.     $do_user_extra->{Email}->{forwardEmail} = '&' . $forward_email . '&';
  214. }
  215.  
  216. #
  217. ### Disk Quota:
  218. #
  219.  
  220. $user_OID = &cce_find_user($username);
  221. $do_user_extra->{Disk}->{quota} = $disk_quota;
  222.  
  223. # Loop through all NameSpaces:
  224. foreach $uon ( keys %{ $do_user_extra } ) {
  225.     # Perform SET transaction:
  226.     ($ok) = $cce->set($user_OID, "$uon", $do_user_extra->{$uon});
  227.     delete $do_user_extra->{$uon};
  228.     # Check result:
  229.     if ($ok ne "1") {
  230.         # Increment error counter:
  231.         $errors++;        
  232.         print "\nERROR: User quota/alias update failed!\n\n";
  233.         print Dumper($cce);
  234.     }
  235. }
  236.  
  237. if ($errors ne '0') {
  238.     print "Error count: $errors\nRemoving partially created 'User' object from CODB.\n";
  239.     ($ok) = $cce->destroy($user_OID);
  240. }
  241.  
  242. $cce->bye("SUCCESS");
  243. exit(0);
  244.  
  245. #
  246. ### Subs:
  247. #
  248.  
  249. sub root_check {
  250.     my $id = `id -u`;
  251.     chomp($id);
  252.     if ($id ne "0") {
  253.         #print "$0 must be run by user 'root'!\n\n";
  254.         &help("$0 must be run by user 'root'!");
  255.     }
  256. }
  257.  
  258. sub debug_msg {
  259.     if ($DEBUG eq "1") {
  260.         $msg = shift;
  261.         $user = $ENV{'USER'};
  262.         setlogsock('unix');
  263.         openlog($0,'','user');
  264.         syslog('info', "$ARGV[0]: $msg");
  265.         closelog;
  266.     }
  267.     if ($DEBUG eq "2") {
  268.         my $msg = shift;
  269.         print $msg;
  270.     }
  271. }
  272.  
  273. sub cce_find_user {
  274.     my $u = shift || "";
  275.     @oids = $cce->find("User", {"name" => $u});
  276.     if (scalar(@oids) eq "1") {
  277.         return $oids[0];
  278.     }
  279. }
  280.  
  281. sub header {
  282.     print "########################################################### \n";
  283.     print "# bx-user-import.pl: BlueOnyx Generic User Import Utility #\n";
  284.     print "###########################################################\n\n";
  285. }
  286.  
  287. sub help {
  288.     $error = shift || "";
  289.     &header;
  290.     if ($error) {
  291.         print "ERROR: $error\n\n";
  292.     }
  293.     print "usage:   bx-user-import.pl [OPTION]\n";
  294.     print "         -v Specify Vsite (FQDN or 'siteX') of User you want to create.\n";
  295.     print "         -u Username of the User you want to create.\n";
  296.     print "         -p Password of the User you want to create.\n";
  297.     print "         -t Type of password: 'crypted' (default) or 'plaintext'.\n";
  298.     print "         -a Email alias for this User in the form of a single word (optional).\n";
  299.     print "         -d Disk Quota in MB (Optional. Defaults to 1000MB if not specified.)\n";
  300.     print "         -f Email Address to which emails of this user are forwarded to (Optional)\n";
  301.     print "         -h help, this help text\n\n";
  302.     $cce->bye("SUCCESS");
  303.     exit(0);
  304. }
  305.  
  306. # pack and unpack arrays
  307. sub array_to_scalar {
  308.     my $scalar = "&";
  309.     if ($_ eq "undef") {
  310.         next;
  311.     }
  312.     while (defined($_ = shift)) {
  313.         $scalar .= $_ . '&';
  314.     }
  315.     if ($scalar eq "&") {
  316.         $scalar = ""; # special case
  317.     }
  318.     return $scalar;
  319. }
  320.  
  321. sub scalar_to_array {
  322.     my $scalar = shift || "";
  323.     $scalar =~ s/^&//;
  324.     $scalar =~ s/&$//;
  325.     my @data = split(/&/, $scalar);
  326.     for ($i = 0; $i <= $#data; $i++) {
  327.         $data[$i] =~ s/\+/ /g;
  328.         $data[$i] =~ s/%([0-9a-fA-F]{2})/chr(hex($1))/ge;
  329.     }
  330.     return @data;
  331. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement