summaryrefslogtreecommitdiffstats
path: root/examples/scripts/users_and_groups/adduserstogroups.pl
blob: 17ca5bd84209d601b81ddde28fe13516fc69944b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#!/usr/bin/perl

#
# adduserstogroups.pl
#
#    add single or continuously numbered domain users
#    to a given single group or list of groups
#
# Copyright (C) Michael Adam <obnox@samba.org> 2007
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the Free
# Software Foundation; either version 3 of the License, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
# more details.
#
# You should have received a copy of the GNU General Public License along with
# this program; if not, see <http://www.gnu.org/licenses/>.
#

#
# WARNING: This script is still rather crude.
#

use strict;
use Getopt::Std;

my $net_cmd	= "net";

# defaults:

my $server;
my $num_members	= 1;
my $startmem;			# if empty, don't add numbers to member prefix
my $member_prefix;		# name prefix for member
my $num_groups = 1;
my $startgroup;			# if empty, don't add numbers to group prefix
my $group_prefix;		# name prefix for group
my $path;			# path to rpcclient command
my $net_path	= $net_cmd;
my $creds;

sub usage {
	print "USAGE: $0 [-h] -S server -U user\%pass \\\n"
		. "\t-m member [-s startmem] [-n nummem] \\\n"
		. "\t-g group [-G stargroup] [-N numgroups] \\\n"
		. "\t[-P path]\n";
}

# parse commandline:

my %options = ();
getopts("U:S:m:s:n:g:G:N:P:h", \%options);

if (exists($options{h})) {
	usage();
	exit 0;
}

if (exists($options{g})) {
	$group_prefix = $options{g};
}
else {
	print "ERROR: mandatory argument '-g' missing\n";
	usage();
	exit 1;
}

if (exists($options{U})) {
	$creds = "-U $options{U}";
	if ($creds !~ '%') {
		print "ERROR: you need to specify credentials in the form -U user\%pass\n";
		usage();
		exit 1;
	}
}
else {
	print "ERROR: mandatory argument '-U' missing\n";
	usage();
	exit 1;
}

if (exists($options{S})) {
	$server = $options{S};
}
else {
	print "ERROR: madatory argument '-S' missing\n";
	usage();
	exit 1;
}

if (exists($options{s})) {
	$startmem = $options{s};
}

if (exists($options{n})) {
	$num_members = $options{n};
}

if (exists($options{m})) {
	$member_prefix = $options{m};
}
else {
	print "ERROR: mandatory argument '-m' missing\n";
	usage();
	exit 1;
}

if (exists($options{G})) {
	$startgroup = $options{G};
}

if (exists($options{N})) {
	$num_groups = $options{N};
}

if (exists($options{P})) {
	$path = $options{p};
	$net_path = "$path/$net_cmd";
}

if (@ARGV) {
	print "ERROR: junk on the command line ('" . join(" ", @ARGV) . "')...\n";
	usage();
	exit 1;
}

# utility functions:

sub do_add {
	my $member_name = shift;
	my $group_name = shift;
	print "adding member $member_name to group $group_name\n";
	system("$net_path rpc -I $server ".$creds." group addmem $group_name $member_name");
}

sub add_group_loop {
	my $member_name = shift;

	if ("x$startgroup" eq "x") {
		do_add($member_name, $group_prefix);
	}
	else {
		for (my $groupnum = 1; $groupnum <= $num_groups; ++$groupnum) {
			do_add($member_name, 
			       sprintf("%s%.05d", $group_prefix, $startgroup + $groupnum - 1));
		}
	}
}


# main:

if ("x$startmem" eq "x") {
	add_group_loop($member_prefix);
}
else {
	for (my $memnum = 1; $memnum <= $num_members; ++$memnum) {
		add_group_loop(sprintf("%s%.05d", $member_prefix, $startmem + $memnum - 1));
	}
}