r25068: Older samba3 DCs will return DCERPC_FAULT_OP_RNG_ERROR for every opcode on the
[Samba.git] / examples / scripts / users_and_groups / createdomobj.pl
blob6568b216642f304f0b179f40f304f1075eeae62d
1 #!/usr/bin/perl
4 # createdomobj.pl
6 # create single or continuously numbered domain
7 # users/groups/aliases via rpc
9 # Copyright (C) Michael Adam <obnox@samba.org> 2007
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 2 of the License, or (at your option)
14 # any later version.
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.
21 # You should have received a copy of the GNU General Public License along with
22 # this program; if not, write to the Free Software Foundation, Inc., 675
23 # Mass Ave, Cambridge, MA 02139, USA.
27 # WARNING: This script is still rather crude.
30 use strict;
31 use Getopt::Std;
34 my $target_type = "group"; # what type of object to create
35 my $rpc_cmd = "createdom".$target_type;
36 my $rpccli_cmd = "rpcclient";
38 # defaults:
40 my $server;
41 my $num_targets = 1;
42 my $startnum; # if empty, don't add numbers to prefix
43 my $prefix = $target_type; # name-prefix
44 my $path; # path to rpcclient command
45 my $rpccli_path = $rpccli_cmd;
46 my $creds;
48 sub usage {
49 print "USAGE: $0 [-h] -S server -U user\%pass [-p prefix] \\\n"
50 . "\t[-t {alias|group|user}] [-s startnum] [-n numobjs] [-P path] \n";
53 # parse commandline:
55 my %options = ();
56 getopts("U:t:S:s:n:p:P:h", \%options);
58 if (exists($options{h})) {
59 usage();
60 exit 0;
63 if (exists($options{t})) {
64 $target_type = $options{t};
65 if ($target_type !~ /^(alias|user|group)$/) {
66 print "ERROR: invalid target type given\n";
67 usage();
68 exit 1;
70 $rpc_cmd = "createdom".$target_type;
73 if (exists($options{U})) {
74 $creds = "-U $options{U}";
75 if ($creds !~ '%') {
76 print "ERROR: you need to specify credentials in the form -U user\%pass\n";
77 usage();
78 exit 1;
81 else {
82 print "ERROR: mandatory argument '-U' missing\n";
83 usage();
84 exit 1;
87 if (exists($options{S})) {
88 $server = $options{S};
90 else {
91 print "ERROR: madatory argument '-S' missing\n";
92 usage();
93 exit 1;
96 if (exists($options{s})) {
97 $startnum = $options{s};
100 if (exists($options{n})) {
101 $num_targets = $options{n};
104 if (exists($options{p})) {
105 $prefix = $options{p};
108 if (exists($options{P})) {
109 $path = $options{p};
110 $rpccli_path = "$path/$rpccli_cmd";
113 if (@ARGV) {
114 print "ERROR: junk on the command line ('" . join(" ", @ARGV) . "')...\n";
115 usage();
116 exit 1;
119 # utility functions:
121 sub open_rpc_pipe {
122 print "opening rpc pipe\n";
123 open(IPC, "| $rpccli_cmd $server $creds -d0") or
124 die "error opening rpc pipe.";
127 sub close_rpc_pipe {
128 print "closing rpc pipe\n";
129 close(IPC);
132 sub do_create {
133 my $target_name = shift;
134 print "creating $target_type $target_name\n";
135 print IPC "$rpc_cmd $target_name\n";
138 # main:
140 open_rpc_pipe();
142 if ("x$startnum" eq "x") {
143 do_create($prefix);
145 else {
146 for (my $num = 1; $num <= $num_targets; ++$num) {
147 do_create(sprintf "%s%.05d", $prefix, $startnum + $num - 1);
148 if (($num) % 500 == 0) {
149 printf("500 ".$target_type."s created\n");
150 close_rpc_pipe();
151 sleep 2;
152 open_rpc_pipe();
157 close_rpc_pipe();