-
Notifications
You must be signed in to change notification settings - Fork 0
/
virtualmin-svn-lib.pl
executable file
·367 lines (346 loc) · 9.63 KB
/
virtualmin-svn-lib.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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
use strict;
use warnings;
our (%access, %config);
our $module_root_directory;
BEGIN { push(@INC, ".."); };
eval "use WebminCore;";
&init_config();
&foreign_require("virtual-server", "virtual-server-lib.pl");
$config{'auth'} ||= "Basic";
%access = &get_module_acl();
sub can_edit_domain
{
return &virtual_server::can_edit_domain($_[0]);
}
# list_reps(&domain)
# Returns a list of all repositories in some domain
sub list_reps
{
my (@rv, $f);
opendir(DIR, "$_[0]->{'home'}/svn");
while($f = readdir(DIR)) {
if ($f ne "." && $f ne "..") {
push(@rv, { 'dom' => $_[0],
'rep' => $f,
'dir' => "$_[0]->{'home'}/svn/$f" });
}
}
closedir(DIR);
return @rv;
}
sub svn_check
{
return &text('feat_echeck', "<tt>$config{'svnadmin'}</tt>")
if (!&has_command($config{'svnadmin'}));
return undef;
}
# list_rep_users(&domain, rep)
# Returns a list of all users with access to some repository
sub list_rep_users
{
my $lref = &virtual_server::read_file_lines_as_domain_user(
$_[0], &conf_file($_[0]));
my (@rv, $inrep, $l);
foreach my $l (@$lref) {
if ($l =~ /^\s*\[(.*):\/\S*\]/) {
$inrep = $1;
}
elsif ($l =~ /^\s*(\S+)\s*=\s*(\S+)/) {
push(@rv, { 'user' => $1,
'perms' => $2 }) if ($inrep eq $_[1]);
}
}
return @rv;
}
# save_rep_users(&domain, &rep, &users)
# Updates the list of users for some repository
sub save_rep_users
{
my ($dom, $rep, $users) = @_;
my $conf_file = &conf_file($_[0]);
&lock_file($conf_file);
my $lref = &virtual_server::read_file_lines_as_domain_user($dom, $conf_file);
my ($start, $end) = &rep_users_lines($dom, $rep, $lref);
my @lines = ( "[$rep->{'rep'}:/]",
map { "$_->{'user'} = $_->{'perms'}" } @$users );
if (defined($start)) {
splice(@$lref, $start, $end-$start+1, @lines);
}
else {
push(@$lref, @lines);
}
&virtual_server::flush_file_lines_as_domain_user($dom, $conf_file);
&unlock_file($conf_file);
&virtual_server::set_permissions_as_domain_user($dom, 0755, $conf_file);
}
# rep_users_lines(&domain, &rep, &lref)
sub rep_users_lines
{
my ($dom, $rep, $lref) = @_;
my ($start, $end, $l, $inrep);
my $lnum = 0;
foreach my $l (@$lref) {
if ($l =~ /^\s*\[(.*):\/\S*\]/) {
if ($1 eq $rep->{'rep'}) {
$start = $end = $lnum;
$inrep = 1;
}
else {
$inrep = 0;
}
}
elsif ($l =~ /^\s*(\S+)\s*=\s*(\S+)/ && $inrep) {
$end = $lnum;
}
$lnum++;
}
return ($start, $end);
}
# create_rep(&domain, &rep, type)
# Create a repository directory and perms file entry
sub create_rep
{
my ($dom, $rep, $type) = @_;
$rep->{'dir'} = "$dom->{'home'}/svn/$rep->{'rep'}";
my $qdir = quotemeta($rep->{'dir'});
my $cmd;
if (&supports_fs_type()) {
$cmd = "svnadmin create --fs-type $type $qdir 2>&1";
}
else {
$cmd = "svnadmin create $qdir 2>&1";
}
my ($out, $ex) = &virtual_server::run_as_domain_user($dom, $cmd);
if ($ex) {
return $out;
}
&set_rep_permissions($dom, $rep);
my $cfile = &conf_file($dom);
&lock_file($cfile);
my $lref = &virtual_server::read_file_lines_as_domain_user($dom, $cfile);
my ($start, $end) = &rep_users_lines($dom, $rep, $lref);
if (!defined($start)) {
push(@$lref, "[$rep->{'rep'}:/]");
&virtual_server::flush_file_lines_as_domain_user($dom, $cfile);
}
&unlock_file($cfile);
}
# set_rep_permissions(&domain, &rep)
# Sets the ownership and permissions on a repository
sub set_rep_permissions
{
my ($dom, $rep) = @_;
my $qdir = quotemeta($rep->{'dir'});
my $webuser = &virtual_server::get_apache_user($dom);
my @uinfo = getpwnam($webuser);
&virtual_server::run_as_domain_user($dom, "chmod -R 770 $qdir");
&virtual_server::run_as_domain_user($dom,
"find $qdir -type d | xargs chmod g+s");
&system_logged("chown -R $uinfo[2] $qdir");
}
# delete_rep(&domain, &rep)
# Delete a repository directory and perms file entry
sub delete_rep
{
my ($dom, $rep) = @_;
my $qdir = quotemeta($rep->{'dir'});
my $quser = quotemeta($dom->{'user'});
&system_logged("chown -R $quser:$quser $qdir");
&virtual_server::unlink_file_as_domain_user(
$dom, "$dom->{'home'}/svn/$rep->{'rep'}");
my $cfile = &conf_file($dom);
&lock_file($cfile);
my $lref = &virtual_server::read_file_lines_as_domain_user($dom, $cfile);
my ($start, $end) = &rep_users_lines($dom, $rep, $lref);
if (defined($start)) {
splice(@$lref, $start, $end-$start+1);
&virtual_server::flush_file_lines_as_domain_user($dom, $cfile);
}
&unlock_file($cfile);
}
sub supports_fs_type
{
my $out = &backquote_command("$config{'svnadmin'} help create 2>&1", 1);
return $config{'canfs'} && $out =~ /\-\-fs\-type/;
}
# passwd_file(&domain)
sub passwd_file
{
if ($config{'passfile'}) {
return "$_[0]->{'home'}/$config{'passfile'}";
}
else {
return "$_[0]->{'home'}/etc/svn.basic.passwd";
}
}
sub conf_file
{
return "$_[0]->{'home'}/etc/svn-access.conf";
}
# list_users(&domain)
sub list_users
{
my $users;
&foreign_require("htaccess-htpasswd", "htaccess-lib.pl");
if ($config{'auth'} eq 'Digest') {
$users = &htaccess_htpasswd::list_digest_users(&passwd_file($_[0]));
}
else {
$users = &htaccess_htpasswd::list_users(&passwd_file($_[0]));
}
return @$users;
}
# get_repository_email(&domain, &rep)
# Returns the email address to notify when changes to some repo are committed
sub get_repository_email
{
my ($dom, $rep) = @_;
my $pc = "$dom->{'home'}/svn/$rep->{'rep'}/hooks/post-commit";
my $lref = &read_file_lines($pc);
my ($prog, $email);
foreach my $l (@$lref) {
if ($l =~ /^\s*EMAIL="(.*)"/) {
$email = $1;
}
elsif ($l =~ /^\S+\/commit-email.pl.*\$EMAIL/ &&
$l !~ /^\#/) {
$prog = 1;
}
}
return $prog && $email ? $email : undef;
}
# save_repository_email(&domain, &rep, email)
# Updates the email address to notify when changes to some repo are committed
sub save_repository_email
{
my ($dom, $rep, $email) = @_;
my $pc = "$dom->{'home'}/svn/$rep->{'rep'}/hooks/post-commit";
&lock_file($pc);
my $lref = &virtual_server::read_file_lines_as_domain_user($dom, $pc);
my $svnlook = &has_command("svnlook");
if (!@$lref && $email) {
# Create initial file
$svnlook || &error("Could not find the svnlook command");
push(@$lref, "#!/bin/sh",
"EMAIL=\"$email\"",
"REPOS=\"\$1\"",
"REV=\"\$2\"",
"SVNLOOK=\"$svnlook\"",
"export SVNLOOK",
"$module_root_directory/commit-email.pl --from $dom->{'emailto'} -s \"SubVersion commit\" \"\$REPOS\" \"\$REV\" \"\$EMAIL\"");
&virtual_server::flush_file_lines_as_domain_user($dom, $pc);
&virtual_server::set_permissions_as_domain_user($dom, 0755, $pc);
}
elsif (@$lref && $email) {
# Just update email, comment and SVNLOOK in program
foreach my $l (@$lref) {
if ($l =~ /^\s*EMAIL="(.*)"/) {
$l = "EMAIL=\"$email\"";
}
elsif ($l =~ /^#\S+\/commit-email.pl/) {
$l =~ s/^#//;
}
elsif ($l =~ /^\s*SVNLOOK="(.*)"/ && $svnlook) {
$l = "SVNLOOK=\"$svnlook\"";
}
}
&virtual_server::flush_file_lines_as_domain_user($dom, $pc);
}
elsif (@$lref && !$email) {
# Comment out the program
foreach my $l (@$lref) {
if ($l =~ /^\S+\/commit-email.pl.*\$EMAIL/ && $l !~ /^\#/) {
$l = "#$l";
}
}
&virtual_server::flush_file_lines_as_domain_user($dom, $pc);
}
&unlock_file($pc);
}
# dump_rep(&domain, &rep, file)
# Dumps the contents of a repository to a file
sub dump_rep
{
my ($dom, $rep, $file) = @_;
my $cmd = "svnadmin dump -q ".quotemeta("$dom->{'home'}/svn/$rep->{'rep'}").
" 2>&1 >".quotemeta($file);
my $out = &virtual_server::run_as_domain_user($dom, $cmd);
return $out =~ /failed|error/i || !-r $file || $? ?
"<pre>".&html_escape($out)."</pre>" : undef;
}
# load_rep(&domain, &rep, file)
# Loads the contents of a repository from a file
sub load_rep
{
my ($dom, $rep, $file) = @_;
my $qdir = quotemeta($rep->{'dir'});
my $quser = quotemeta($dom->{'user'});
&system_logged("chown -R $quser:$quser $qdir");
my $cmd = "svnadmin load -q ".quotemeta("$dom->{'home'}/svn/$rep->{'rep'}").
" 2>&1 <".quotemeta($file);
my $out = &virtual_server::run_as_domain_user($dom, $cmd);
if ($out =~ /failed|error/i || $?) {
return "<pre>".&html_escape($out)."</pre>";
}
else {
&set_rep_permissions($dom, $rep);
return undef;
}
}
# set_user_password(&svn-user, &virtualmin-user, &domain)
# Sets password fields for an SVN user based on their virtualmin user hash
sub set_user_password
{
my ($newuser, $user, $dom) = @_;
if ($config{'auth'} eq 'Digest' && $user->{'pass_digest'}) {
# Digest mode .. use existing hashed password
$newuser->{'digest'} = 1;
$newuser->{'dom'} = $dom->{'dom'};
$newuser->{'pass'} = $user->{'pass_digest'};
}
elsif ($config{'auth'} eq 'Digest') {
# Digest mode .. need to re-hash from plain pass
$newuser->{'digest'} = 1;
$newuser->{'dom'} = $dom->{'dom'};
if ($user->{'user'} eq $dom->{'user'}) {
# User is domain owner, use stored digest hash or re-hash
# plaintext password
$newuser->{'pass'} = $dom->{'digest_enc_pass'} ||
&htaccess_htpasswd::digest_password(
$newuser->{'user'}, $dom->{'dom'}, $dom->{'pass'});
}
elsif ($user->{'passmode'} == 3 ||
defined($user->{'plainpass'})) {
# Regular mailbox, for which password is known
$newuser->{'pass'} = &htaccess_htpasswd::digest_password(
$newuser->{'user'}, $dom->{'dom'},$user->{'plainpass'});
}
else {
$newuser->{'pass'} = "UNKNOWN";
}
}
elsif ($user->{'pass_crypt'}) {
# Use stored crypt format hash
$newuser->{'pass'} = $user->{'pass_crypt'};
}
elsif ($user->{'pass'} =~ /^\$/ && $user->{'plainpass'}) {
# MD5-hashed, re-hash plain version
if ($user->{'user'} eq $dom->{'user'}) {
# User is domain owner, use stored DES hash or re-hash
# plaintext password
$newuser->{'pass'} = $dom->{'crypt_enc_pass'} ||
&unix_crypt($dom->{'pass'}, substr(time(), -2));
}
else {
# Regular mailbox, for which password is known
$newuser->{'pass'} = &unix_crypt($user->{'plainpass'},
substr(time(), -2));
}
}
else {
# Just copy hashed password
$newuser->{'digest'} = 0;
$newuser->{'pass'} = $user->{'pass'};
}
}
1;