1#!/usr/bin/perl 2 3# 4# createdomobj.pl 5# 6# create single or continuously numbered domain 7# users/groups/aliases via rpc 8# 9# Copyright (C) Michael Adam <obnox@samba.org> 2007 10# 11# This program is free software; you can redistribute it and/or modify it 12# under the terms of the GNU General Public License as published by the Free 13# Software Foundation; either version 3 of the License, or (at your option) 14# any later version. 15# 16# This program is distributed in the hope that it will be useful, but WITHOUT 17# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 18# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 19# more details. 20# 21# You should have received a copy of the GNU General Public License along with 22# this program; if not, see <http://www.gnu.org/licenses/>. 23# 24 25# 26# WARNING: This script is still rather crude. 27# 28 29use strict; 30use Getopt::Std; 31 32 33my $target_type = "group"; # what type of object to create 34my $rpc_cmd = "createdom".$target_type; 35my $rpccli_cmd = "rpcclient"; 36 37# defaults: 38 39my $server; 40my $num_targets = 1; 41my $startnum; # if empty, don't add numbers to prefix 42my $prefix = $target_type; # name-prefix 43my $path; # path to rpcclient command 44my $rpccli_path = $rpccli_cmd; 45my $creds; 46 47sub usage { 48 print "USAGE: $0 [-h] -S server -U user\%pass [-p prefix] \\\n" 49 . "\t[-t {alias|group|user}] [-s startnum] [-n numobjs] [-P path] \n"; 50} 51 52# parse commandline: 53 54my %options = (); 55getopts("U:t:S:s:n:p:P:h", \%options); 56 57if (exists($options{h})) { 58 usage(); 59 exit 0; 60} 61 62if (exists($options{t})) { 63 $target_type = $options{t}; 64 if ($target_type !~ /^(alias|user|group)$/) { 65 print "ERROR: invalid target type given\n"; 66 usage(); 67 exit 1; 68 } 69 $rpc_cmd = "createdom".$target_type; 70} 71 72if (exists($options{U})) { 73 $creds = "-U $options{U}"; 74 if ($creds !~ '%') { 75 print "ERROR: you need to specify credentials in the form -U user\%pass\n"; 76 usage(); 77 exit 1; 78 } 79} 80else { 81 print "ERROR: mandatory argument '-U' missing\n"; 82 usage(); 83 exit 1; 84} 85 86if (exists($options{S})) { 87 $server = $options{S}; 88} 89else { 90 print "ERROR: madatory argument '-S' missing\n"; 91 usage(); 92 exit 1; 93} 94 95if (exists($options{s})) { 96 $startnum = $options{s}; 97} 98 99if (exists($options{n})) { 100 $num_targets = $options{n}; 101} 102 103if (exists($options{p})) { 104 $prefix = $options{p}; 105} 106 107if (exists($options{P})) { 108 $path = $options{p}; 109 $rpccli_path = "$path/$rpccli_cmd"; 110} 111 112if (@ARGV) { 113 print "ERROR: junk on the command line ('" . join(" ", @ARGV) . "')...\n"; 114 usage(); 115 exit 1; 116} 117 118# utility functions: 119 120sub open_rpc_pipe { 121 print "opening rpc pipe\n"; 122 open(IPC, "| $rpccli_cmd $server $creds -d0") or 123 die "error opening rpc pipe."; 124} 125 126sub close_rpc_pipe { 127 print "closing rpc pipe\n"; 128 close(IPC); 129} 130 131sub do_create { 132 my $target_name = shift; 133 print "creating $target_type $target_name\n"; 134 print IPC "$rpc_cmd $target_name\n"; 135} 136 137# main: 138 139open_rpc_pipe(); 140 141if ("x$startnum" eq "x") { 142 do_create($prefix); 143} 144else { 145 for (my $num = 1; $num <= $num_targets; ++$num) { 146 do_create(sprintf "%s%.05d", $prefix, $startnum + $num - 1); 147 if (($num) % 500 == 0) { 148 printf("500 ".$target_type."s created\n"); 149 close_rpc_pipe(); 150 sleep 2; 151 open_rpc_pipe(); 152 } 153 } 154} 155 156close_rpc_pipe(); 157 158