1# secrets-lib.pl
2# Common functions for editing a PPP users file
3
4# list_secrets()
5sub list_secrets
6{
7local(@rv, $line, $_);
8open(SEC, "<".$config{'pap_file'});
9$line = 0;
10while(<SEC>) {
11	chop;
12	s/^#.*$//g;
13	@w = &split_words($_);
14	if (@w >= 3) {
15		local(%sec, @ips);
16		$sec{'client'} = $w[0];
17		$sec{'server'} = $w[1];
18		$sec{'secret'} = $w[2];
19		@ips = @w[3..$#w];
20		$sec{'ips'} = \@ips;
21		$sec{'line'} = $line;
22		$sec{'index'} = scalar(@rv);
23		push(@rv, \%sec);
24		}
25	$line++;
26	}
27close(SEC);
28return @rv;
29}
30
31# create_secret(&secret)
32sub create_secret
33{
34&open_tempfile(SEC, ">>$config{'pap_file'}");
35&print_tempfile(SEC, &join_words($_[0]->{'client'}, $_[0]->{'server'},
36		      $_[0]->{'secret'}, @{$_[0]->{'ips'}}),"\n");
37&close_tempfile(SEC);
38}
39
40# change_secret(&secret)
41sub change_secret
42{
43&replace_file_line($config{'pap_file'}, $_[0]->{'line'},
44		   &join_words($_[0]->{'client'}, $_[0]->{'server'},
45			       $_[0]->{'secret'}, @{$_[0]->{'ips'}})."\n");
46}
47
48# delete_secret(&secret)
49sub delete_secret
50{
51&replace_file_line($config{'pap_file'}, $_[0]->{'line'});
52}
53
54# split_words(string)
55sub split_words
56{
57local($s, @w);
58$s = $_[0];
59while($s =~ /^\s*([^"\s]+|"([^"]*)")(.*)$/) {
60	push(@w, defined($2) ? $2 : $1);
61	$s = $3;
62	}
63return @w;
64}
65
66sub join_words
67{
68local(@w, $w);
69foreach $w (@_) {
70	if ($w =~ /^[a-zA-Z0-9\.\-]+$/) { push(@w, $w); }
71	else { push(@w, "\"$w\""); }
72	}
73return join("  ", @w);
74}
75
76# opt_crypt(password)
77# Returns the given password, crypted if the user has configured it
78sub opt_crypt
79{
80if ($config{'encrypt_pass'}) {
81	local($salt);
82	srand(time());
83	$salt = chr(int(rand(26))+65).chr(int(rand(26))+65);
84	return &unix_crypt($_[0], $salt);
85	}
86return $_[0];
87}
88
89
90