1#!/usr/local/bin/perl
2# create_group.cgi
3# Creates a new group on all servers
4
5require './cluster-useradmin-lib.pl';
6use Time::Local;
7&error_setup($text{'gsave_err'});
8&ReadParse();
9@hosts = &list_useradmin_hosts();
10@servers = &list_servers();
11
12# Strip out \n characters in inputs
13$in{'group'} =~ s/\r|\n//g;
14$in{'pass'} =~ s/\r|\n//g;
15$in{'encpass'} =~ s/\r|\n//g;
16$in{'gid'} =~ s/\r|\n//g;
17
18# check group name
19$in{'group'} =~ /^[^:\t]+$/ ||
20	&error(&text('gsave_ebadname', $in{'group'}));
21$uconfig{'max_length'} && length($in{'group'}) > $uconfig{'max_length'} &&
22	&error(&text('gsave_elength', $uconfig{'max_length'}));
23
24$| = 1;
25&ui_print_header(undef, $text{'gedit_title2'}, "");
26
27# Work out which hosts to create on
28@already = grep { local ($alr) = grep { $_->{'group'} eq $in{'group'} }
29				    @{$_->{'groups'}};
30		  $alr } @hosts;
31@hosts = &create_on_parse("gsave_header", \@already, $in{'group'});
32
33# Check for group name clash
34foreach $h (@hosts) {
35	local ($og) = grep { $_->{'group'} eq $in{'group'} } @{$h->{'groups'}};
36	&error(&text('gsave_einuse', $in{'group'})) if ($og);
37	}
38$group{'group'} = $in{'group'};
39
40# Validate and save inputs
41$in{'gid'} =~ /^[0-9]+$/ || &error(&text('gsave_egid', $in{'gid'}));
42@mems = split(/\s+/, $in{members});
43$group{'members'} = join(',', @mems);
44$group{'gid'} = $in{'gid'};
45
46$salt = chr(int(rand(26))+65) . chr(int(rand(26))+65);
47if ($in{'passmode'} == 0) { $group{'pass'} = ""; }
48elsif ($in{'passmode'} == 1) { $group{'pass'} = $in{'encpass'}; }
49elsif ($in{'passmode'} == 2) { $group{'pass'} = &unix_crypt($in{'pass'}, $salt); }
50
51# Setup error handler for down hosts
52sub add_error
53{
54$add_error_msg = join("", @_);
55}
56&remote_error_setup(\&add_error);
57
58foreach $host (@hosts) {
59	$add_error_msg = undef;
60	local ($serv) = grep { $_->{'id'} == $host->{'id'} } @servers;
61	print "<b>",&text('gsave_con', $serv->{'desc'} ? $serv->{'desc'} :
62				       $serv->{'host'}),"</b><p>\n";
63	print "<ul>\n";
64	&remote_foreign_require($serv->{'host'}, "useradmin", "user-lib.pl");
65	if ($add_error_msg) {
66		# Host is down ..
67		print &text('gsave_failed', $add_error_msg),"<p>\n";
68		print "</ul>\n";
69		next;
70		}
71
72	# Run the pre-change command
73	&remote_eval($serv->{'host'}, "useradmin", <<EOF
74\$ENV{'USERADMIN_GROUP'} = '$group{'group'}';
75\$ENV{'USERADMIN_ACTION'} = 'CREATE_GROUP';
76EOF
77	);
78	$merr = &remote_foreign_call($serv->{'host'}, "useradmin",
79				     "making_changes");
80	if (defined($merr)) {
81		print &text('usave_emaking', "<tt>$merr</tt>"),"<p>\n";
82		print "</ul>\n";
83		next;
84		}
85
86	# Save group details
87	print "$text{'gsave_create'}<br>\n";
88	&remote_foreign_call($serv->{'host'}, "useradmin",
89			     "create_group", \%group);
90	print "$text{'udel_done'}<p>\n";
91
92	# Run post-change command
93	&remote_foreign_call($serv->{'host'}, "useradmin", "made_changes");
94
95	if ($in{'others'}) {
96		if (&supports_gothers($serv)) {
97			# Create in other modules on the server
98			print "$text{'usave_others'}<br>\n";
99			&remote_foreign_call($serv->{'host'}, "useradmin",
100				     "other_modules", "useradmin_create_group",
101				     \%group);
102			print "$text{'udel_done'}<p>\n";
103			}
104		else {
105			# Group syncing not supported
106			print "$text{'gsave_nosync'}<p>\n";
107			}
108		}
109
110	# Update host
111	push(@{$host->{'groups'}}, \%group);
112	&save_useradmin_host($host);
113	print "</ul>\n";
114	}
115&webmin_log("create", "group", $group{'group'}, \%group);
116
117&ui_print_footer("", $text{'index_return'});
118
119