Handle hostnames with upper-case letters
[webmin.git] / htaccess-htpasswd / htaccess-lib.pl
1 # htaccess-lib.pl
2 # Common functions for the htaccess and htpasswd file management module
3
4 BEGIN { push(@INC, ".."); };
5 use WebminCore;
6 &init_config();
7 do 'htpasswd-file-lib.pl';
8
9 @accessdirs = ( );
10 if ($module_info{'usermin'}) {
11         # Allowed directories are in module configuration
12         &switch_to_remote_user();
13         &create_user_config_dirs();
14         $default_dir = &resolve_links($remote_user_info[7]);
15         push(@accessdirs, $default_dir) if ($config{'home'});
16         local $d;
17         foreach $d (split(/\t+/, $config{'dirs'})) {
18                 push(@accessdirs, $d =~ /^\// ? $d : "$default_dir/$d");
19                 }
20         $directories_file = "$user_module_config_directory/directories";
21         $apachemod = "htaccess";
22         $can_htpasswd = $config{'can_htpasswd'};
23         $can_htgroups = $config{'can_htgroups'};
24         $can_create = !$config{'nocreate'};
25         }
26 else {
27         # Allowed directories come from ACL
28         %access = &get_module_acl();
29         local @uinfo;
30         if (&supports_users()) {
31                 # Include user home
32                 @uinfo = getpwnam($remote_user);
33                 if ($access{'home'} && scalar(@uinfo)) {
34                         push(@accessdirs, &resolve_links($uinfo[7]));
35                         }
36                 }
37         local $d;
38         foreach $d (split(/\t+/, $access{'dirs'})) {
39                 push(@accessdirs, $d =~ /^\// || !@uinfo ?
40                         $d : &resolve_links("$uinfo[7]/$d"));
41                 }
42         $directories_file = "$module_config_directory/directories";
43         $directories_file .= ".".$remote_user if ($access{'userdirs'});
44         $apachemod = "apache";
45         $can_htpasswd = 1;
46         $can_htgroups = 1;
47         $default_dir = $accessdirs[0];
48         $can_sync = $access{'sync'};
49         $can_create = !$access{'uonly'};
50         }
51
52 # list_directories([even-if-missing])
53 # Returns a list of protected directories known to this module, and the
54 # users file, encryption mode, sync mode and groups file for each
55 sub list_directories
56 {
57 local @rv;
58 open(DIRS, $directories_file);
59 while(<DIRS>) {
60         s/\r|\n//g;
61         local @dir = split(/\t+/, $_);
62         next if (!@dir);
63         if ($_[0] || -d $dir[0] && -r "$dir[0]/$config{'htaccess'}") {
64                 push(@rv, \@dir);
65                 }
66         }
67 closedir(DIRS);
68 return @rv;
69 }
70
71 # save_directories(&dirs)
72 # Save the list of known directories, which must be in the same format as
73 # returned by list_directories
74 sub save_directories
75 {
76 local $d;
77 &open_tempfile(DIRS, ">$directories_file");
78 foreach $d (@{$_[0]}) {
79         &print_tempfile(DIRS, join("\t", @$d),"\n");
80         }
81 &close_tempfile(DIRS);
82 }
83
84 # can_access_dir(dir)
85 # Returns 1 if files can be created under some directory, 0 if not
86 sub can_access_dir
87 {
88 return 1 if (!$ENV{'GATEWAY_INTERFACE'});
89 local $d;
90 foreach $d (@accessdirs) {
91         return 1 if (&is_under_directory(&resolve_links($d),
92                                          &resolve_links($_[0])));
93         }
94 return 0;
95 }
96
97 # switch_user()
98 # Switch to the Unix user that files are accessed as.
99 # No need to do anything for Usermin, because the switch was done above.
100 sub switch_user
101 {
102 if (!$module_info{'usermin'} &&
103     $access{'user'} ne 'root' && !defined($old_uid) && &supports_users()) {
104         local @uinfo = getpwnam($access{'user'} eq "*" ? $remote_user
105                                                        : $access{'user'});
106         $old_uid = $>;
107         $old_gid = $);
108         $) = "$uinfo[3] $uinfo[3]";
109         $> = $uinfo[2];
110         }
111 }
112
113 sub switch_back
114 {
115 if (defined($old_uid)) {
116         $> = $old_uid;
117         $) = $old_gid;
118         $old_uid = $old_gid = undef;
119         }
120 }
121
122 1;
123