-
-
Notifications
You must be signed in to change notification settings - Fork 98
/
create-login-link.pl
executable file
·130 lines (118 loc) · 3.35 KB
/
create-login-link.pl
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
#!/usr/local/bin/perl
=head1 create-login-link.pl
Generates a link that can be used to login to Virtualmin.
This command can be used to login to Virtualmin as a domain owner without
needing to enter a password. When a server is selected with either the
C<--domain> or C<--user> flag, a URL will be displayed that when opened in
a browser will immediately login as the owner of that server.
Alternately, you can use the C<--usermin-user> flag to login to Usermin
as a mailbox user. This must be followed by the full Unix username of the
mailbox.
If you want to login as root, use C<--root> flag only.
=cut
package virtual_server;
if (!$module_name) {
$main::no_acl_check++;
$ENV{'WEBMIN_CONFIG'} ||= "/etc/webmin";
$ENV{'WEBMIN_VAR'} ||= "/var/webmin";
if ($0 =~ /^(.*)\/[^\/]+$/) {
chdir($pwd = $1);
}
else {
chop($pwd = `pwd`);
}
$0 = "$pwd/create-login-link.pl";
require './virtual-server-lib.pl';
$< == 0 || die "create-login-link.pl must be run as root";
}
@OLDARGV = @ARGV;
&set_all_text_print();
# Parse command-line args
while(@ARGV > 0) {
local $a = shift(@ARGV);
if ($a eq "--domain") {
$dname = shift(@ARGV);
}
elsif ($a eq "--user") {
$uname = shift(@ARGV);
}
elsif ($a eq "--usermin-user") {
$uname = shift(@ARGV);
$usermin = 1;
}
elsif ($a eq "--root") {
$root = 1;
}
elsif ($a eq "--help") {
&usage();
}
else {
&usage("Unknown parameter $a");
}
}
# Validate inputs
if ($root) {
$uname = "root";
$usermin = 0;
}
elsif ($dname) {
$d = &get_domain_by("dom", $dname);
$d || &usage("No virtual server named $dname found");
}
elsif ($uname) {
if ($usermin) {
getpwnam($uname) || &usage("Unix user $uname does not exist");
$d = &get_user_domain($uname);
}
else {
$d = &get_domain_by("user", $uname, "parent", "");
$d || &usage("No virtual server owned by $uname found");
}
}
else {
&usage("One of --domain or --user must be provided");
}
if ($d) {
$d->{'webmin'} ||
&usage("Virtual server $d->{'dom'} has no Webmin login");
$uname = $d->{'user'};
}
# Create the session in the appropriate program
&foreign_require("acl");
defined(&acl::create_session_user) ||
&usage("Your Webmin version does not support switching sessions");
if ($usermin) {
# Add a Usermin session
&foreign_require("usermin");
my %miniserv;
&usermin::get_usermin_miniserv_config(\%miniserv);
my $sid = &acl::create_session_user(\%miniserv, $uname);
$sid || &usage("Failed to create login session");
&usermin::restart_usermin_miniserv();
my $dom = $d ? $d->{'dom'} : undef;
$url = &usermin::get_usermin_email_url(undef, undef, undef, $dom).
"/session_login.cgi?session=".&urlize($sid);
print $url."\n";
}
else {
# Add a Webmin session
&foreign_require("webmin");
my %miniserv;
&get_miniserv_config(\%miniserv);
($uinfo) = grep { $_->{'name'} eq $uname } &acl::list_users();
$uinfo || &usage("Webmin user $uname does not exist");
my $sid = &acl::create_session_user(\%miniserv, $uname);
$sid || &usage("Failed to create login session");
&reload_miniserv();
$url = &get_virtualmin_url($d)."/session_login.cgi?session=".&urlize($sid);
print $url."\n";
}
sub usage
{
print "$_[0]\n\n" if ($_[0]);
print "Generates a link that can be used to login to Virtualmin.\n";
print "\n";
print "virtualmin create-login-link [--domain name | --user name |\n";
print " --usermin-user name | --root]\n";
exit(1);
}