52a7d01703d71591f1874e8e5b72b180b32d341b
[kai/samba-autobuild/.git] / examples / scripts / users_and_groups / adduserstogroups.pl
1 #!/usr/bin/perl
2
3 #
4 # adduserstogroups.pl
5 #
6 #    add single or continuously numbered domain users
7 #    to a given single group or list of groups
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 my $net_cmd     = "net";
34
35 # defaults:
36
37 my $server;
38 my $num_members = 1;
39 my $startmem;                   # if empty, don't add numbers to member prefix
40 my $member_prefix;              # name prefix for member
41 my $num_groups = 1;
42 my $startgroup;                 # if empty, don't add numbers to group prefix
43 my $group_prefix;               # name prefix for group
44 my $path;                       # path to rpcclient command
45 my $net_path    = $net_cmd;
46 my $creds;
47
48 sub usage {
49         print "USAGE: $0 [-h] -S server -U user\%pass \\\n"
50                 . "\t-m member [-s startmem] [-n nummem] \\\n"
51                 . "\t-g group [-G stargroup] [-N numgroups] \\\n"
52                 . "\t[-P path]\n";
53 }
54
55 # parse commandline:
56
57 my %options = ();
58 getopts("U:S:m:s:n:g:G:N:P:h", \%options);
59
60 if (exists($options{h})) {
61         usage();
62         exit 0;
63 }
64
65 if (exists($options{g})) {
66         $group_prefix = $options{g};
67 }
68 else {
69         print "ERROR: mandatory argument '-g' missing\n";
70         usage();
71         exit 1;
72 }
73
74 if (exists($options{U})) {
75         $creds = "-U $options{U}";
76         if ($creds !~ '%') {
77                 print "ERROR: you need to specify credentials in the form -U user\%pass\n";
78                 usage();
79                 exit 1;
80         }
81 }
82 else {
83         print "ERROR: mandatory argument '-U' missing\n";
84         usage();
85         exit 1;
86 }
87
88 if (exists($options{S})) {
89         $server = $options{S};
90 }
91 else {
92         print "ERROR: madatory argument '-S' missing\n";
93         usage();
94         exit 1;
95 }
96
97 if (exists($options{s})) {
98         $startmem = $options{s};
99 }
100
101 if (exists($options{n})) {
102         $num_members = $options{n};
103 }
104
105 if (exists($options{m})) {
106         $member_prefix = $options{m};
107 }
108 else {
109         print "ERROR: mandatory argument '-m' missing\n";
110         usage();
111         exit 1;
112 }
113
114 if (exists($options{G})) {
115         $startgroup = $options{G};
116 }
117
118 if (exists($options{N})) {
119         $num_groups = $options{N};
120 }
121
122 if (exists($options{P})) {
123         $path = $options{p};
124         $net_path = "$path/$net_cmd";
125 }
126
127 if (@ARGV) {
128         print "ERROR: junk on the command line ('" . join(" ", @ARGV) . "')...\n";
129         usage();
130         exit 1;
131 }
132
133 # utility functions:
134
135 sub do_add {
136         my $member_name = shift;
137         my $group_name = shift;
138         print "adding member $member_name to group $group_name\n";
139         system("$net_path rpc -I $server ".$creds." group addmem $group_name $member_name");
140 }
141
142 sub add_group_loop {
143         my $member_name = shift;
144
145         if ("x$startgroup" eq "x") {
146                 do_add($member_name, $group_prefix);
147         }
148         else {
149                 for (my $groupnum = 1; $groupnum <= $num_groups; ++$groupnum) {
150                         do_add($member_name, 
151                                sprintf("%s%.05d", $group_prefix, $startgroup + $groupnum - 1));
152                 }
153         }
154 }
155
156
157 # main:
158
159 if ("x$startmem" eq "x") {
160         add_group_loop($member_prefix);
161 }
162 else {
163         for (my $memnum = 1; $memnum <= $num_members; ++$memnum) {
164                 add_group_loop(sprintf("%s%.05d", $member_prefix, $startmem + $memnum - 1));
165         }
166 }
167