~zulcss/samba/server-dailies-3.4

« back to all changes in this revision

Viewing changes to examples/scripts/users_and_groups/adduserstogroups.pl

  • Committer: Chuck Short
  • Date: 2010-09-28 20:38:39 UTC
  • Revision ID: zulcss@ubuntu.com-20100928203839-pgjulytsi9ue63x1
Initial version

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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, 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
my $net_cmd     = "net";
 
33
 
 
34
# defaults:
 
35
 
 
36
my $server;
 
37
my $num_members = 1;
 
38
my $startmem;                   # if empty, don't add numbers to member prefix
 
39
my $member_prefix;              # name prefix for member
 
40
my $num_groups = 1;
 
41
my $startgroup;                 # if empty, don't add numbers to group prefix
 
42
my $group_prefix;               # name prefix for group
 
43
my $path;                       # path to rpcclient command
 
44
my $net_path    = $net_cmd;
 
45
my $creds;
 
46
 
 
47
sub usage {
 
48
        print "USAGE: $0 [-h] -S server -U user\%pass \\\n"
 
49
                . "\t-m member [-s startmem] [-n nummem] \\\n"
 
50
                . "\t-g group [-G startgroup] [-N numgroups] \\\n"
 
51
                . "\t[-P path]\n";
 
52
}
 
53
 
 
54
# parse commandline:
 
55
 
 
56
my %options = ();
 
57
getopts("U:S:m:s:n:g:G:N:P:h", \%options);
 
58
 
 
59
if (exists($options{h})) {
 
60
        usage();
 
61
        exit 0;
 
62
}
 
63
 
 
64
if (exists($options{g})) {
 
65
        $group_prefix = $options{g};
 
66
}
 
67
else {
 
68
        print "ERROR: mandatory argument '-g' missing\n";
 
69
        usage();
 
70
        exit 1;
 
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
        $startmem = $options{s};
 
98
}
 
99
 
 
100
if (exists($options{n})) {
 
101
        $num_members = $options{n};
 
102
}
 
103
 
 
104
if (exists($options{m})) {
 
105
        $member_prefix = $options{m};
 
106
}
 
107
else {
 
108
        print "ERROR: mandatory argument '-m' missing\n";
 
109
        usage();
 
110
        exit 1;
 
111
}
 
112
 
 
113
if (exists($options{G})) {
 
114
        $startgroup = $options{G};
 
115
}
 
116
 
 
117
if (exists($options{N})) {
 
118
        $num_groups = $options{N};
 
119
}
 
120
 
 
121
if (exists($options{P})) {
 
122
        $path = $options{p};
 
123
        $net_path = "$path/$net_cmd";
 
124
}
 
125
 
 
126
if (@ARGV) {
 
127
        print "ERROR: junk on the command line ('" . join(" ", @ARGV) . "')...\n";
 
128
        usage();
 
129
        exit 1;
 
130
}
 
131
 
 
132
# utility functions:
 
133
 
 
134
sub do_add {
 
135
        my $member_name = shift;
 
136
        my $group_name = shift;
 
137
        print "adding member $member_name to group $group_name\n";
 
138
        system("$net_path rpc -I $server ".$creds." group addmem $group_name $member_name");
 
139
}
 
140
 
 
141
sub add_group_loop {
 
142
        my $member_name = shift;
 
143
 
 
144
        if ("x$startgroup" eq "x") {
 
145
                do_add($member_name, $group_prefix);
 
146
        }
 
147
        else {
 
148
                for (my $groupnum = 1; $groupnum <= $num_groups; ++$groupnum) {
 
149
                        do_add($member_name, 
 
150
                               sprintf("%s%.05d", $group_prefix, $startgroup + $groupnum - 1));
 
151
                }
 
152
        }
 
153
}
 
154
 
 
155
 
 
156
# main:
 
157
 
 
158
if ("x$startmem" eq "x") {
 
159
        add_group_loop($member_prefix);
 
160
}
 
161
else {
 
162
        for (my $memnum = 1; $memnum <= $num_members; ++$memnum) {
 
163
                add_group_loop(sprintf("%s%.05d", $member_prefix, $startmem + $memnum - 1));
 
164
        }
 
165
}
 
166