Advertisement
Guest User

BX Single User Creation

a guest
Dec 7th, 2017
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 6.63 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:", \%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.  
  52. # Handle display of help text:
  53. if ($options{h}) {
  54.     &help;
  55. }
  56.  
  57. # Find Vsite:
  58. if ($options{v}) {
  59.     &header;
  60.     print "Searching for Vsite " . $options{v} . "\n";
  61.  
  62.     $vsite_found = '0';
  63.     @oids = $cce->find('Vsite', {'name' => $options{v}});
  64.     if ($#oids == 0) {
  65.         $vsite_found = '1';
  66.         $vsite_name = $options{v};
  67.     }
  68.     else {
  69.         @oids = $cce->find('Vsite', {'fqdn' => $options{v}});
  70.         if ($#oids == 0) {
  71.             $vsite_found = '1';
  72.             ($ok, $obj) = $cce->get($oids[0]);
  73.             $vsite_name = $obj->{'name'};
  74.         }
  75.     }
  76.     if ($vsite_found eq '0') {
  77.         print "Vsite '" . $options{v} . "' not found! Please check the name and try again!\n";
  78.         $cce->bye("FAIL");
  79.         exit(1);
  80.     }
  81.     else {
  82.         $vsite_oid = $oids[0];
  83.         print "Vsite '" . $options{v} . "' found! [OID:$vsite_oid|Group:$vsite_name]\n";
  84.     }
  85. }
  86. else {
  87.     print "\nERROR: You must specify a Vsite with the -v parameter!\n\n";
  88.     &help;
  89. }
  90.  
  91. # Username:
  92. if ($options{u}) {
  93.     # Check if Username already exists:
  94.     if (&cce_find_user($options{u})) {
  95.         print "User '" . $options{u} . "' already exists! Aborting transaction.\n";
  96.         $cce->bye("FAIL");
  97.         exit(1);
  98.     }
  99.     else {
  100.         print "Username '" . $options{u} . "' is available.\n";
  101.         $username = $options{u};
  102.     }
  103. }
  104. else {
  105.     print "\nERROR: You must specify a Username with the -u parameter!\n\n";
  106.     &help;
  107. }
  108.  
  109. # Password:
  110. if ($options{p}) {
  111.     if ($options{p} ne "") {
  112.         $password = $options{p};
  113.     }
  114. }
  115. else {
  116.     print "\nERROR: You must specify a Password with the -p parameter!\n\n";
  117.     &help;
  118. }
  119.  
  120. # Password Type:
  121. if ($options{t}) {
  122.     if (($options{t} eq "plain") || ($options{t} eq "plaintext")) {
  123.         $pw_type = 'plaintext'
  124.     }
  125. }
  126. print "Using password type '" . $pw_type . "'.\n";
  127.  
  128. # Email Alias:
  129. if ($options{a}) {
  130.     if ($options{a} ne "") {
  131.         # Check if alias already exists:
  132.         @oids_s_alias = $cce->find("EmailAlias", {'alias' => $options{a}, 'site' => $vsite_name});
  133.         if (scalar(@oids_s_alias)) {
  134.             print "ERROR: EmailAlias '" . $options{a} . "' already exists! Aborting transaction.\n";
  135.             $cce->bye("FAIL");
  136.             exit(1);
  137.         }
  138.         else {
  139.             print "EmailAlias '" . $options{a} . "' is available.\n";
  140.             $email_alias = $options{a};
  141.         }
  142.     }
  143. }
  144.  
  145. # Disk Quota:
  146. if ($options{d}) {
  147.     if ($options{d} ne "") {
  148.         $disk_quota = $options{d};
  149.     }
  150. }
  151.  
  152. #
  153. ### Actual User creation:
  154. #
  155.  
  156. $do_user->{site} = $vsite_name;
  157. $do_user->{name} = $username;
  158. $do_user->{fullName} = $username;
  159. if ($pw_type eq "crypted") {
  160.     $do_user->{md5_password} = $password;
  161. }
  162. $do_user->{password} = '';
  163.  
  164. #
  165. ### CREATE the main User Object:
  166. #
  167.  
  168. ($ok) = $cce->create("User", $do_user, '');
  169.  
  170. # Check result:
  171. if ($ok ne "1") {
  172.     # Increment error counter:
  173.     $errors++;
  174.     print "\nERROR: User creation failed!\n\n";
  175.     print Dumper($cce);
  176. }
  177.  
  178. if ($pw_type eq "crypted") {
  179.     # Set Password-Hash:
  180.     print "Setting password via: /usr/sbin/usermod $username -p '" . $password . "'\n";
  181.     system("/usr/sbin/usermod $username -p '" . $password . "'");
  182. }
  183. else {
  184.     # Set Plaintext-Password:
  185.     print "Setting password via: echo \"$password\" | passwd \"$username\" --stdin\n";
  186.     system("echo \"$password\" | passwd \"$username\" --stdin");
  187. }
  188.  
  189. #
  190. ### EmailAlias:
  191. #
  192.  
  193. if ($email_alias ne '') {
  194.     $do_user_extra->{Email}->{aliases} = '&' . $email_alias . '&';
  195. }
  196.  
  197. #
  198. ### Disk Quota:
  199. #
  200.  
  201. $user_OID = &cce_find_user($username);
  202. $do_user_extra->{Disk}->{quota} = $disk_quota;
  203.  
  204. # Loop through all NameSpaces:
  205. foreach $uon ( keys %{ $do_user_extra } ) {
  206.     # Perform SET transaction:
  207.     ($ok) = $cce->set($user_OID, "$uon", $do_user_extra->{$uon});
  208.     delete $do_user_extra->{$uon};
  209.     # Check result:
  210.     if ($ok ne "1") {
  211.         # Increment error counter:
  212.         $errors++;        
  213.         print "\nERROR: User quota/alias update failed!\n\n";
  214.         print Dumper($cce);
  215.     }
  216. }
  217.  
  218. if ($errors ne '0') {
  219.     print "Error count: $errors\nRemoving partially created 'User' object from CODB.\n";
  220.     ($ok) = $cce->destroy($user_OID);
  221. }
  222.  
  223. $cce->bye("SUCCESS");
  224. exit(0);
  225.  
  226. #
  227. ### Subs:
  228. #
  229.  
  230. sub root_check {
  231.     my $id = `id -u`;
  232.     chomp($id);
  233.     if ($id ne "0") {
  234.         #print "$0 must be run by user 'root'!\n\n";
  235.         &help("$0 must be run by user 'root'!");
  236.     }
  237. }
  238.  
  239. sub debug_msg {
  240.     if ($DEBUG eq "1") {
  241.         $msg = shift;
  242.         $user = $ENV{'USER'};
  243.         setlogsock('unix');
  244.         openlog($0,'','user');
  245.         syslog('info', "$ARGV[0]: $msg");
  246.         closelog;
  247.     }
  248.     if ($DEBUG eq "2") {
  249.         my $msg = shift;
  250.         print $msg;
  251.     }
  252. }
  253.  
  254. sub cce_find_user {
  255.     my $u = shift || "";
  256.     @oids = $cce->find("User", {"name" => $u});
  257.     if (scalar(@oids) eq "1") {
  258.         return $oids[0];
  259.     }
  260. }
  261.  
  262. sub header {
  263.     print "########################################################### \n";
  264.     print "# bx-user-import.pl: BlueOnyx Generic User Import Utility #\n";
  265.     print "###########################################################\n\n";
  266. }
  267.  
  268. sub help {
  269.     $error = shift || "";
  270.     &header;
  271.     if ($error) {
  272.         print "ERROR: $error\n\n";
  273.     }
  274.     print "usage:   bx-user-import.pl [OPTION]\n";
  275.     print "         -v Specify Vsite (FQDN or 'siteX') of User you want to create.\n";
  276.     print "         -u Username of the User you want to create.\n";
  277.     print "         -p Password of the User you want to create.\n";
  278.     print "         -t Type of password: 'crypted' (default) or 'plaintext'.\n";
  279.     print "         -a Email alias for this User in the form of a single word (optional).\n";
  280.     print "         -d Disk Quota in MB (Optional. Defaults to 1000MB if not specified.)\n";
  281.     print "         -h help, this help text\n\n";
  282.     $cce->bye("SUCCESS");
  283.     exit(0);
  284. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement