36e0c0f9fd080865f2cf9927c0f33dcc0d6d815e
[kai/samba.git] / examples / scripts / users_and_groups / createdomobj.pl
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, write to the Free Software Foundation, Inc., 675
23 # Mass Ave, Cambridge, MA 02139, USA.
24 #
25
26 #
27 # WARNING: This script is still rather crude.
28 #
29
30 use strict;
31 use Getopt::Std;
32
33
34 my $target_type = "group";              # what type of object to create
35 my $rpc_cmd     = "createdom".$target_type;
36 my $rpccli_cmd  = "rpcclient";
37
38 # defaults:
39
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;
47
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";
51 }
52
53 # parse commandline:
54
55 my %options = ();
56 getopts("U:t:S:s:n:p:P:h", \%options);
57
58 if (exists($options{h})) {
59         usage();
60         exit 0;
61 }
62
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;
69         }
70         $rpc_cmd = "createdom".$target_type;
71 }
72
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;
79         }
80 }
81 else {
82         print "ERROR: mandatory argument '-U' missing\n";
83         usage();
84         exit 1;
85 }
86
87 if (exists($options{S})) {
88         $server = $options{S};
89 }
90 else {
91         print "ERROR: madatory argument '-S' missing\n";
92         usage();
93         exit 1;
94 }
95
96 if (exists($options{s})) {
97         $startnum = $options{s};
98 }
99
100 if (exists($options{n})) {
101         $num_targets = $options{n};
102 }
103
104 if (exists($options{p})) {
105         $prefix = $options{p};
106 }
107
108 if (exists($options{P})) {
109         $path = $options{p};
110         $rpccli_path = "$path/$rpccli_cmd";
111 }
112
113 if (@ARGV) {
114         print "ERROR: junk on the command line ('" . join(" ", @ARGV) . "')...\n";
115         usage();
116         exit 1;
117 }
118
119 # utility functions:
120
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.";
125 }
126
127 sub close_rpc_pipe {
128         print "closing rpc pipe\n";
129         close(IPC);
130 }
131
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";
136 }
137
138 # main:
139
140 open_rpc_pipe();
141
142 if ("x$startnum" eq "x") {
143         do_create($prefix);
144 }
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();
153                 }
154         }
155 }
156
157 close_rpc_pipe();
158