This file is indexed.

/usr/share/perl5/IkiWiki/Customer.pm is in ikiwiki-hosting-common 0.20170622ubuntu1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
#!/usr/bin/perl
# Handles access to customer data.
package IkiWiki::Customer;
use warnings;
use strict;
use IkiWiki;
use IkiWiki::Hosting;
use Fcntl qw{:flock};
use File::Path qw{mkpath};
use Memoize;

# Currently, customer data is stored in a customersite.
# It could just as easily be a database.
#
# The customersite's repo is checked out into ~/customersite. Assuming
# that directory exists, or if a checkout is successfully done, returns the
# directory.
memoize("basedir");
sub basedir (;$) {
	my $nocheckout=shift;

	IkiWiki::Hosting::readconfig();
	my $customersite=$IkiWiki::Hosting::config{customersite};
	if (! defined $customersite || ! length $customersite) {
		return;
	}
	
	my $dir=(getpwuid($<))[7]."/customersite";
	return $dir if -d $dir;

	return if $nocheckout;
	# Try to checkout a copy of the customersite's git repo,
	# using ssh transport.
	my $username=IkiWiki::Hosting::getshell("ikisite", "username", 
		$customersite);
	return unless defined $username && length $username;
	my $gitsshurl="ssh://$username\@$customersite/";
	my $origumask=umask(077);
	eval {IkiWiki::Hosting::shell("git", "clone", $gitsshurl, $dir)};
	umask($origumask);
	if ($@ || ! -d $dir) {
		# This is bad, try to make the admins aware of the problem.
		IkiWiki::Hosting::syslog("failed to clone $gitsshurl to $dir",
			"IkiWiki::Customer");
		return;
	}
	else {
		return $dir;
	}
}

# Locks a given directory inside the customersite and executes the passed sub.
sub dir_lock ($$$) {
	my $locktype=shift;
	my $dir=shift;
	my $sub=shift;

	if ($dir eq ".") {
		# To operate on the top directory, must take a global lock.
		globallock($locktype, $sub);
	}
	else {
		my $base=basedir();
		return unless defined $base;

		# First take shared global lock, then individual
		# Directory lock.
		globallock(LOCK_SH, sub {
			_dolock($locktype, "$base/$dir", $sub);
		});
	}
}

# Locks a directory for reading and executes the passed sub.
sub dir_lock_read ($$) {
	my $dir=shift;
	my $sub=shift;

	dir_lock(LOCK_SH, $dir, $sub);
}

# Locks a directory for writing and executes the passed sub.
sub dir_lock_write ($$) {
	my $dir=shift;
	my $sub=shift;

	dir_lock(LOCK_EX, $dir, $sub);
}

# Takes a global lock of the whole customersite. This is used to
# block things like git pushes that cannot respect per-customer locks.
sub globallock ($$) {
	my $locktype=shift;
	my $sub=shift;

	my $base=basedir();
	return unless defined $base;

	_dolock($locktype, $base, $sub);
}

my %locks;
sub _dolock ($$$) {
	my $locktype=shift;
	my $dir=shift;
	my $sub=shift;

	# Taking an exclusive lock when a shared lock is already held would
	# deadlock.
	if ($locktype == LOCK_EX && exists $locks{$dir}[LOCK_SH]) {
		die "_dolock: deadlock detected while locking $dir for LOCK_EX";
	}

	# Avoid taking a lock if an equivilant one is already held.
	my $lock;
	if (! exists $locks{$dir}[$locktype] &&
	    ! exists $locks{$dir}[LOCK_EX]) {
		mkpath($dir);
		open ($lock, ">", "$dir/lock") || die $!;
		flock($lock, $locktype) || die $!;
		$locks{$dir}[$locktype]=$lock;
	}

	my $ret=eval { $sub->() };
	my $fail=$@;

	if ($lock) {
		close $lock;
		delete $locks{$dir}[$locktype];
	}

	if ($fail) {
		die $fail;
	}
	return $ret;
}

# Returns a field from a specified directory of the customersite.
sub dir_get ($$) {
	my $dir=shift;
	my $field=shift;

	my @ret;
	dir_lock(LOCK_SH, $dir, sub {
		my $base=basedir();
		return unless defined $base;

		if (open (my $fh, "<", "$base/$dir/$field")) {
			@ret=map { chomp $_; $_ } <$fh>;
			close $fh;
		}
	});
	
	return wantarray ? @ret : $ret[0];			
}

# Sets a field in a specified directory of the customersite.
sub dir_set ($$@) {
	my $dir=shift;
	my $field=shift;
	my @value=@_;
	
	dir_lock(LOCK_EX, $dir, sub {
		my $base=basedir();
		return unless defined $base;

		open (my $fh, ">", "$base/$dir/$field") || die $!;
		print $fh join("\n", @value)."\n";
		close $fh || die $!;
	});
}

# Commits all changes made to a directory of the customersite 
# into the git repository.
sub dir_commit ($;$) {
	my $dir=shift;
	my $message=shift;
	
	my $base=basedir();
	return unless defined $base;

	eval q{use Cwd};
	die $@ if $@;
	my $origdir=getcwd();
	chdir("$base/$dir") || die $!;

	dir_lock(LOCK_EX, $dir, sub {
		IkiWiki::Hosting::shell("git", "add", ".");
		# empty commits needed in case no changes were really made
		IkiWiki::Hosting::shell("git", "commit", "--allow-empty",
			"-m", "updated $dir".
				(defined $message ? "\n\n$message" : ""));
	});

	# Pushing is done in the background. Do not chdir daemon to /
	my $pid=IkiWiki::Hosting::daemonize(1);
	if ($pid) {
		chdir($origdir);
		return;
	}

	# To avoid deadlocks, this background process needs to drop all
	# possible locks.
	# Drop all existing locks on the customerdb.
	foreach my $key (keys %locks) {
		foreach my $lock (@{$locks{$key}}) {
			close $lock if defined $lock;
		}
	}
	# Drop all ikiwiki locks.
	IkiWiki::enable_commit_hook();
	IkiWiki::unlockwiki();

	eval {IkiWiki::Hosting::shell("git", "push", "origin", "master")};
	if ($@) {
		# A push is most likely to fail due to being diverged from
		# origin. Or it could be a network problem. Try to update.
		if (update()) {
			# Update worked; try again to push.
			eval {IkiWiki::Hosting::shell("git", "push", "origin", "master")};
			if ($@) {
				IkiWiki::Hosting::syslog("git push origin master failed",
					"IkiWiki::Customer");
			}
		}
	}

	exit;
}

# Updates the whole customersite by doing a git pull.
# Avoids leaving the site in a conflicted state.
sub update () {
	my $base=basedir(1);
	return unless defined $base;
	
	eval q{use Cwd};
	die $@ if $@;
	my $origdir=getcwd();
	chdir($base) || die $!;

	# See if the origin can be contacted, by trying to fetch from it.
	eval {IkiWiki::Hosting::shell("git", "fetch", "origin")};
	if ($@) {
		# This is sorta bad, though it may be a transient
		# problem. Try to make the admins aware of the problem.
		IkiWiki::Hosting::syslog("failed to fetch customersite ($@)",
			"IkiWiki::Customer");
		chdir($origdir);
		return 0;
	}

	# Global lock, because git merge can touch any files.
	globallock(LOCK_EX, sub {
		eval {IkiWiki::Hosting::shell("git", "merge", "origin", "master")};
		if ($@) {
			# Blast! A conflict. For now, resolving it is
			# beyond us, so whine for mommy to help, and
			# for now, reset our working copy so conflict
			# markers don't screw things up!
			IkiWiki::Hosting::syslog("customersite is in a conflicted state, please fix ($@)",
				"IkiWiki::Customer");
			eval {IkiWiki::Hosting::shell("git", "reset", "--hard")};
			if ($@) {
				# Should never happen.
				IkiWiki::Hosting::syslog("failed to use git reset --hard; conflict markers present in customersite! ($@)",
					"IkiWiki::Customer");
			}
			chdir($origdir);
			return 0;
		}
		chdir($origdir);
		return 1;
	});
}

# Converts a username into a sha1 hash.
sub customerid ($) {
	my $username=shift;

	eval q{use Digest::SHA};
	die $@ if $@;

	return Digest::SHA::sha1_hex($username);
}

# Returns the directory used to store information about a particular customer,
# given their username.
#
# A directory matching the customerid of their username is preferred, but
# failing that, it searches for a directory with an openid_list or
# email_list containing the username.
memoize("customerdir");
sub customerdir ($) {
	my $username=shift;

	my $base=basedir();
	return unless defined $base;

	my $dir="customer/".customerid($username);
	return $dir if -d "$base/$dir";

	foreach my $d (glob("$base/customer/*")) {
		my @openids=read_list_file($d, "openid_list");
		my @emails=read_list_file($d, "email_list");
		if (grep { $_ eq $username } (@openids, @emails)) {
			$d=~s/^\Q$base\/\E//;
			return $d;
		}
	}

	return $dir;
}

# Finds accounts that contain a given field with the specified value.
sub accounts_by_field ($$;$) {
	my $field=shift;
	my $value=shift;
	my $ignorecase=shift;

	if ($ignorecase) {
		$value=lc $value;
	}
	
	my $base=basedir();
	return () unless defined $base;
	
	my @ret;
	foreach my $d (glob("$base/customer/*")) {
		my @vals=read_list_file($d, $field);
		if (grep { ($ignorecase ? lc $_ : $_) eq $value } @vals) {
			my @openids=read_list_file($d, "openid_list");
			if (@openids) {
				push @ret, $openids[0];
			}
			else {
				my @emails=read_list_file($d, "email_list");
				push @ret, $emails[0] if @emails;
			}
		}
	}

	return @ret;
}

# Reads a file as a list of multiple lines.
#
# XXX Currently the file is not locked, so there is a small potential
# for a race here. There is some potential for deadlock if locking the
# file here.
sub read_list_file ($$) {
	my $dir=shift;
	my $file=shift;

	my @list;
	if (open (my $fh, "<", "$dir/$file")) {
		@list=grep { length $_ } map { chomp $_; $_ } <$fh>;
		close $fh;
	}
	return @list;
}

# Locks a customer for reading and executes the passed sub.
sub customer_lock_read ($$) {
	my $username=shift;
	my $sub=shift;

	dir_lock(LOCK_SH, customerdir($username), $sub);
}

# Locks a customer for writing and executes the passed sub.
sub customer_lock_write ($$) {
	my $username=shift;
	my $sub=shift;

	dir_lock(LOCK_EX, customerdir($username), $sub);
}

# Returns a given field of a customer's data, given their username.
sub customer_get ($$) {
	my $username=shift;
	my $field=shift;
	
	dir_get(customerdir($username), $field);
}

# Sets a field of a customer's data.
sub customer_set ($$@) {
	my $username=shift;
	my $field=shift;
	my @value=@_;
		
	dir_set(customerdir($username), $field, @value);
}

# A customer may have multiple openids and emails; given one this returns
# the whole list.
sub account_list ($) {
	my $username=shift;
	my @os=read_list_file(basedir()."/".customerdir($username), "openid_list");
	my @es=read_list_file(basedir()."/".customerdir($username), "email_list");
	return (@os, @es, $username);
}

# Commits all changes made to a customer into the git repository.
sub customer_commit ($;$) {
	my $username=shift;
	my $message=shift;
	
	dir_commit(customerdir($username), $message);
}

# Sends an email to a customer (or to stdout in dry run mode).
# The message body is parsed for Subject and From lines. (Also optionally,
# for To, Bcc, and References lines.)
sub customer_sendmail ($$;$) {
	my $username=shift;
	my $message=shift;
	my $dry_run=shift;

	$message=~s/^(?:To: (.*))//m;
	my $to=$1;
	$message=~s/^(?:From: (.*))//m;
	my $from=$1;
	$message=~s/^(?:Bcc: (.*))//m;
	my $bcc=$1;
	$message=~s/^(?:Subject: (.*))//m;
	my $subject=$1;
	$message=~s/^(?:References: (.*))//m;
	my $references=$1;

	# avoid extra blank lines from HTML::Template
	$message=~s/\n\n\n/\n\n/sg; 
	$message=~s/^\n+//s;
	$message=~s/\n+$/\n/s;
	
	$to=customer_get($username, "email") if ! defined $to || ! length $to;
	return unless defined $to && length $to;

	if ($dry_run) {
		print "(Not sending email; in dry run mode.)\n";
		print "\n";
		print "To: $to\n";
		print "From: $from\n";
		print "Bcc: $bcc\n" if defined $bcc && length $bcc;
		print "References: $references\n" if defined $references && length $references;
		print "Subject: $subject\n";
		print "\n";
		print $message."\n\n";
	}
	else {
		eval q{use Mail::Sendmail};
		error $@ if $@;

		my $ret=sendmail(
			To => $to,
			From => $from,
			((defined $bcc && length $bcc) ? (Bcc => $bcc) : ()),
			((defined $references && length $references) ? (References => $references) : ()),
			Subject => $subject,
			Message => $message,
		);
		if (! $ret) {
			die "failed to send email: $!";
		}
	}
}

# Used by programs that must run as a user which has a 
# ~/customersite checkout.
sub assert_has_customersite () {
	my $base=basedir(1);
	if (! defined $base) {
		die "$0 requires ~/customersite checkout exist\n";
	}
}

1