Handle hostnames with upper-case letters
[webmin.git] / pap / secrets-lib.pl
1 # secrets-lib.pl
2 # Common functions for editing a PPP users file
3
4 # list_secrets()
5 sub list_secrets
6 {
7 local(@rv, $line, $_);
8 open(SEC, $config{'pap_file'});
9 $line = 0;
10 while(<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         }
27 close(SEC);
28 return @rv;
29 }
30
31 # create_secret(&secret)
32 sub 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)
41 sub 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)
49 sub delete_secret
50 {
51 &replace_file_line($config{'pap_file'}, $_[0]->{'line'});
52 }
53
54 # split_words(string)
55 sub split_words
56 {
57 local($s, @w);
58 $s = $_[0];
59 while($s =~ /^\s*([^"\s]+|"([^"]*)")(.*)$/) {
60         push(@w, defined($2) ? $2 : $1);
61         $s = $3;
62         }
63 return @w;
64 }
65
66 sub join_words
67 {
68 local(@w, $w);
69 foreach $w (@_) {
70         if ($w =~ /^[a-zA-Z0-9\.\-]+$/) { push(@w, $w); }
71         else { push(@w, "\"$w\""); }
72         }
73 return join("  ", @w);
74 }
75
76 # opt_crypt(password)
77 # Returns the given password, crypted if the user has configured it
78 sub opt_crypt
79 {
80 if ($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         }
86 return $_[0];
87 }
88
89