doc: Fixes for the talloc dynamic type system tutorial.
[Samba/vl.git] / examples / scripts / users_and_groups / createdomobj.pl
blob919f75ad07699f1445ba1e5367690c2d35044296
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 3 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, see <http://www.gnu.org/licenses/>.
26 # WARNING: This script is still rather crude.
29 use strict;
30 use Getopt::Std;
33 my $target_type = "group"; # what type of object to create
34 my $rpc_cmd = "createdom".$target_type;
35 my $rpccli_cmd = "rpcclient";
37 # defaults:
39 my $server;
40 my $num_targets = 1;
41 my $startnum; # if empty, don't add numbers to prefix
42 my $prefix; # name-prefix
43 my $path; # path to rpcclient command
44 my $rpccli_path = $rpccli_cmd;
45 my $creds;
47 sub 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";
52 # parse commandline:
54 my %options = ();
55 getopts("U:t:S:s:n:p:P:h", \%options);
57 if (exists($options{h})) {
58 usage();
59 exit 0;
62 if (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;
69 $rpc_cmd = "createdom".$target_type;
72 if (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;
80 else {
81 print "ERROR: mandatory argument '-U' missing\n";
82 usage();
83 exit 1;
86 if (exists($options{S})) {
87 $server = $options{S};
89 else {
90 print "ERROR: madatory argument '-S' missing\n";
91 usage();
92 exit 1;
95 if (exists($options{s})) {
96 $startnum = $options{s};
99 if (exists($options{n})) {
100 $num_targets = $options{n};
103 if (exists($options{p})) {
104 $prefix = $options{p};
105 } else {
106 $prefix = $target_type;
109 if (exists($options{P})) {
110 $path = $options{p};
111 $rpccli_path = "$path/$rpccli_cmd";
114 if (@ARGV) {
115 print "ERROR: junk on the command line ('" . join(" ", @ARGV) . "')...\n";
116 usage();
117 exit 1;
120 # utility functions:
122 sub open_rpc_pipe {
123 print "opening rpc pipe\n";
124 open(IPC, "| $rpccli_cmd $server $creds -d0") or
125 die "error opening rpc pipe.";
128 sub close_rpc_pipe {
129 print "closing rpc pipe\n";
130 close(IPC);
133 sub do_create {
134 my $target_name = shift;
135 print "creating $target_type $target_name\n";
136 print IPC "$rpc_cmd $target_name\n";
139 # main:
141 open_rpc_pipe();
143 if ("x$startnum" eq "x") {
144 do_create($prefix);
146 else {
147 for (my $num = 1; $num <= $num_targets; ++$num) {
148 do_create(sprintf "%s%.05d", $prefix, $startnum + $num - 1);
149 if (($num) % 500 == 0) {
150 printf("500 ".$target_type."s created\n");
151 close_rpc_pipe();
152 sleep 2;
153 open_rpc_pipe();
158 close_rpc_pipe();