r25598: Add missing become_root/unbecome_root around calls of add_aliases.
[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, see <http://www.gnu.org/licenses/>.
23 #
24
25 #
26 # WARNING: This script is still rather crude.
27 #
28
29 use strict;
30 use Getopt::Std;
31
32
33 my $target_type = "group";              # what type of object to create
34 my $rpc_cmd     = "createdom".$target_type;
35 my $rpccli_cmd  = "rpcclient";
36
37 # defaults:
38
39 my $server;
40 my $num_targets = 1;
41 my $startnum;                           # if empty, don't add numbers to prefix
42 my $prefix      = $target_type;         # name-prefix
43 my $path;                               # path to rpcclient command
44 my $rpccli_path = $rpccli_cmd;
45 my $creds;
46
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";
50 }
51
52 # parse commandline:
53
54 my %options = ();
55 getopts("U:t:S:s:n:p:P:h", \%options);
56
57 if (exists($options{h})) {
58         usage();
59         exit 0;
60 }
61
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;
68         }
69         $rpc_cmd = "createdom".$target_type;
70 }
71
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;
78         }
79 }
80 else {
81         print "ERROR: mandatory argument '-U' missing\n";
82         usage();
83         exit 1;
84 }
85
86 if (exists($options{S})) {
87         $server = $options{S};
88 }
89 else {
90         print "ERROR: madatory argument '-S' missing\n";
91         usage();
92         exit 1;
93 }
94
95 if (exists($options{s})) {
96         $startnum = $options{s};
97 }
98
99 if (exists($options{n})) {
100         $num_targets = $options{n};
101 }
102
103 if (exists($options{p})) {
104         $prefix = $options{p};
105 }
106
107 if (exists($options{P})) {
108         $path = $options{p};
109         $rpccli_path = "$path/$rpccli_cmd";
110 }
111
112 if (@ARGV) {
113         print "ERROR: junk on the command line ('" . join(" ", @ARGV) . "')...\n";
114         usage();
115         exit 1;
116 }
117
118 # utility functions:
119
120 sub 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
126 sub close_rpc_pipe {
127         print "closing rpc pipe\n";
128         close(IPC);
129 }
130
131 sub 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
139 open_rpc_pipe();
140
141 if ("x$startnum" eq "x") {
142         do_create($prefix);
143 }
144 else {
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
156 close_rpc_pipe();
157