-
Notifications
You must be signed in to change notification settings - Fork 0
/
virtual_feature.pl
executable file
·246 lines (225 loc) · 6.12 KB
/
virtual_feature.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
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
# Defines functions for this feature
use strict;
use warnings;
our (%text);
our $module_name;
do 'virtualmin-init-lib.pl';
my $input_name = $module_name;
$input_name =~ s/[^A-Za-z0-9]/_/g;
# feature_name()
# Returns a short name for this feature
sub feature_name
{
return $text{'feat_name'};
}
# feature_losing(&domain)
# Returns a description of what will be deleted when this feature is removed
sub feature_losing
{
return $text{'feat_losing'};
}
# feature_label(in-edit-form)
# Returns the name of this feature, as displayed on the domain creation and
# editing form
sub feature_label
{
my ($edit) = @_;
return $edit ? $text{'feat_label2'} : $text{'feat_label'};
}
# feature_hlink(in-edit-form)
# Returns a help page linked to by the label returned by feature_label
sub feature_hlink
{
return 'label';
}
# feature_check()
# Returns undef if all the needed programs for this feature are installed,
# or an error message if not
sub feature_check
{
return &virtualmin_init_check();
}
# feature_depends(&domain)
# Returns undef if all pre-requisite features for this domain are enabled,
# or an error message if not
sub feature_depends
{
return !$_[0]->{'unix'} && !$_[0]->{'parent'} ? $text{'feat_eunix'} :
undef;
}
# feature_clash(&domain)
# Returns undef if there is no clash for this domain for this feature, or
# an error message if so
sub feature_clash
{
return undef; # Can never clash
}
# feature_suitable([&parentdom], [&aliasdom], [&subdom])
# Returns 1 if some feature can be used with the specified alias,
# parent and sub domains
sub feature_suitable
{
return !$_[1] && !$_[2];
}
# feature_setup(&domain)
# Called when this feature is added, with the domain object as a parameter
sub feature_setup
{
# Does nothing, as no setup is needed
}
# feature_modify(&domain, &olddomain)
# Called when a domain with this feature is modified, to rename scripts if
# user or domain is changed
sub feature_modify
{
my ($d, $oldd) = @_;
if ($d->{'dom'} ne $oldd->{'dom'} ||
$d->{'user'} ne $oldd->{'user'}) {
# Need to re-save all actions under the new user or domain name
&$virtual_server::first_print($text{'feat_rename'});
my $c = 0;
foreach my $init (&list_domain_actions($oldd)) {
my $oldinit = { %$init };
$init->{'user'} = $d->{'user'};
&modify_domain_action($d, $oldd, $init, $oldinit);
$c++;
}
if ($c) {
&$virtual_server::second_print(
$virtual_server::text{'setup_done'});
}
else {
&$virtual_server::second_print($text{'feat_norename'});
}
}
}
# feature_delete(&domain)
# Called when this feature is disabled, or when the domain is being deleted.
# Removes all bootup scripts for the domain.
sub feature_delete
{
my ($d) = @_;
&$virtual_server::first_print($text{'feat_delete'});
my $c = 0;
foreach my $init (&list_domain_actions($d)) {
&delete_domain_action($d, $init);
$c++;
}
if ($c) {
&$virtual_server::second_print(
$virtual_server::text{'setup_done'});
}
else {
&$virtual_server::second_print($text{'feat_norename'});
}
}
# feature_webmin(&main-domain, &all-domains)
# Returns a list of webmin module names and ACL hash references to be set for
# the Webmin user when this feature is enabled
# (optional)
sub feature_webmin
{
my ($d, $doms) = @_;
my @doms = &unique(map { $_->{'id'} } grep { $_->{$module_name} } @$doms);
if (@doms) {
return ( [ $module_name,
{ 'doms' => join(' ', @doms),
'max' => $d->{$module_name.'limit'},
'templates' => 0 } ] );
}
else {
return ( );
}
}
# feature_limits_input(&domain)
# Returns HTML for editing limits related to this plugin
sub feature_limits_input
{
my ($d) = @_;
return undef if (!$d->{$module_name});
return &ui_table_row(&hlink($text{'limits_max'}, "limits_max"),
&ui_opt_textbox($input_name."limit", $d->{$module_name."limit"},
4, $virtual_server::text{'form_unlimit'},
$virtual_server::text{'form_atmost'}));
}
# feature_limits_parse(&domain, &in)
# Updates the domain with limit inputs generated by feature_limits_input
sub feature_limits_parse
{
my ($d, $in) = @_;
return undef if (!$d->{$module_name});
if ($in->{$input_name."limit_def"}) {
delete($d->{$module_name."limit"});
}
else {
$in->{$input_name."limit"} =~ /^\d+$/ || return $text{'limit_emax'};
$d->{$module_name."limit"} = $in->{$input_name."limit"};
}
return undef;
}
# feature_links(&domain)
# Returns an array of link objects for webmin modules for this feature
sub feature_links
{
my ($d) = @_;
return ( { 'mod' => $module_name,
'desc' => $text{'links_link'},
'page' => 'index.cgi?dom='.$d->{'id'},
'cat' => 'services',
} );
}
sub feature_modules
{
return ( [ $module_name, $text{'feat_module'} ] );
}
# feature_backup(&domain, file, &opts, &all-opts)
# Called to backup this feature for the domain to the given file. Must return 1
# on success or 0 on failure.
# Gets all action objects for the domain, and serializes them to the file.
sub feature_backup
{
my ($d, $file) = @_;
&$virtual_server::first_print($text{'feat_backup'});
my $actions = [ &list_domain_actions($d) ];
no strict "subs";
&virtual_server::open_tempfile_as_domain_user($d, INIT, ">$file") || return 0;
&print_tempfile(INIT, &serialise_variable($actions));
&virtual_server::close_tempfile_as_domain_user($d, INIT);
use strict "subs";
if (@$actions) {
&$virtual_server::second_print($virtual_server::text{'setup_done'});
}
else {
&$virtual_server::second_print($text{'feat_norename'});
}
return 1;
}
# feature_restore(&domain, file, &opts, &all-opts)
# Called to restore this feature for the domain from the given file. Must
# return 1 on success or 0 on failure.
# Reads the serialized actions from the file, deletes existing actions, then
# re-creates them.
sub feature_restore
{
my ($d, $file) = @_;
my $data = &read_file_contents($file);
if ($data) {
&$virtual_server::first_print($text{'feat_restore'});
my $actions = &unserialise_variable($data);
foreach my $init (&list_domain_actions($d)) {
&delete_domain_action($d, $init);
}
foreach my $init (@$actions) {
&create_domain_action($d, $init);
}
&$virtual_server::second_print($virtual_server::text{'setup_done'});
}
return 1;
}
# feature_backup_name()
# Returns a description for what is backed up for this feature
sub feature_backup_name
{
return $text{'feat_bname'};
}
1;