| 1 | ##---------------------------------------------------------------------------## | ||
| 2 | ## File: | ||
| 3 | ## $Id: mhfile.pl,v 2.13 2010/12/31 21:20:41 ehood Exp $ | ||
| 4 | ## Author: | ||
| 5 | ## Earl Hood mhonarc@mhonarc.org | ||
| 6 | ## Description: | ||
| 7 | ## File routines for MHonArc | ||
| 8 | ##---------------------------------------------------------------------------## | ||
| 9 | ## MHonArc -- Internet mail-to-HTML converter | ||
| 10 | ## Copyright (C) 1997-1999 Earl Hood, mhonarc@mhonarc.org | ||
| 11 | ## | ||
| 12 | ## This program is free software; you can redistribute it and/or modify | ||
| 13 | ## it under the terms of the GNU General Public License as published by | ||
| 14 | ## the Free Software Foundation; either version 2 of the License, or | ||
| 15 | ## (at your option) any later version. | ||
| 16 | ## | ||
| 17 | ## This program is distributed in the hope that it will be useful, | ||
| 18 | ## but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 19 | ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 20 | ## GNU General Public License for more details. | ||
| 21 | ## | ||
| 22 | ## You should have received a copy of the GNU General Public License | ||
| 23 | ## along with this program; if not, write to the Free Software | ||
| 24 | ## Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA | ||
| 25 | ## 02111-1307, USA | ||
| 26 | ##---------------------------------------------------------------------------## | ||
| 27 | |||
| 28 | package mhonarc; | ||
| 29 | |||
| 30 | use Symbol; | ||
| 31 | use Fcntl; | ||
| 32 | use File::Basename; | ||
| 33 | |||
| 34 | my $_have_File_Temp; | ||
| 35 | |||
| 36 | BEGIN { | ||
| 37 | # If File::Temp is installed, we will use it for temporary file | ||
| 38 | # generation. | ||
| 39 | eval { require File::Temp; }; | ||
| 40 | $_have_File_Temp = scalar($@) ? 0 : 1; | ||
| 41 | |||
| 42 | # Increase File::Temp safety level if setuid | ||
| 43 | if ($_have_File_Temp && $UNIX && $TaintMode) { | ||
| 44 | File::Temp->safe_level(File::Temp::MEDIUM); | ||
| 45 | } | ||
| 46 | |||
| 47 | # Perl <5.004 did not auto-call srand(). | ||
| 48 | eval { require 5.004; }; | ||
| 49 | srand(time ^ ($$ + ($$ << 15))) if scalar($@); | ||
| 50 | } | ||
| 51 | |||
| 52 | # Characters to use for home-grown temporay file generation. We stick to | ||
| 53 | # basic alphanumerics to avoid OS-specific filename limitations. | ||
| 54 | my @TEMP_CHARS = qw( | ||
| 55 | A B C D E F G H I J K L M N O P Q R S T U V W X Y Z | ||
| 56 | a b c d e f g h i j k l m n o p q r s t u v w x y z | ||
| 57 | 0 1 2 3 4 5 6 7 8 9 _ | ||
| 58 | ); | ||
| 59 | |||
| 60 | # Maximum tries to create a temporary file in home-grown implementation | ||
| 61 | sub TEMP_MAX_TRIES() { 10; } | ||
| 62 | |||
| 63 | ##---------------------------------------------------------------------------## | ||
| 64 | |||
| 65 | sub file_open { | ||
| 66 | my ($file) = shift; | ||
| 67 | my ($handle) = gensym; | ||
| 68 | my ($gz) = $file =~ /\.gz$/i; | ||
| 69 | |||
| 70 | if ($gz) { | ||
| 71 | cmd_pipe_open($handle, $GzipExe, '-cd', $file); | ||
| 72 | return $handle; | ||
| 73 | } | ||
| 74 | return $handle if open($handle, $file); | ||
| 75 | if (-e "$file.gz") { | ||
| 76 | cmd_pipe_open($handle, $GzipExe, '-cd', "$file.gz"); | ||
| 77 | return $handle; | ||
| 78 | } | ||
| 79 | die qq/ERROR: Failed to open "$file": $!\n/; | ||
| 80 | } | ||
| 81 | |||
| 82 | sub cmd_pipe_open { | ||
| 83 | my $handle = shift; | ||
| 84 | my @cmd = @_; | ||
| 85 | |||
| 86 | if (!$UNIX) { | ||
| 87 | return $handle if open($handle, join(' ', @cmd, '|')); | ||
| 88 | die qq/ERROR: Failed to exec @cmd: $!\n/; | ||
| 89 | } | ||
| 90 | my $child_pid = open($handle, '-|'); | ||
| 91 | if ($child_pid) { # parent | ||
| 92 | return $handle; | ||
| 93 | } else { # child | ||
| 94 | #open(STDERR, '>&STDOUT'); | ||
| 95 | exec(@cmd) || die qq/ERROR: Cannot exec "@cmd": $!\n/; | ||
| 96 | } | ||
| 97 | } | ||
| 98 | |||
| 99 | sub file_gzip { | ||
| 100 | my $file = shift; | ||
| 101 | return if ($file =~ /\.gz$/i); | ||
| 102 | if (system($GzipExe, $file) != 0) { | ||
| 103 | die qq/ERROR: Failed to exec "$GzipExe $file": $! $?\n/; | ||
| 104 | } | ||
| 105 | } | ||
| 106 | |||
| 107 | ## This function is currently not used anymore | ||
| 108 | sub file_create { | ||
| 109 | my ($file) = shift; | ||
| 110 | my ($gz) = shift; | ||
| 111 | my ($handle) = gensym; | ||
| 112 | |||
| 113 | if ($gz) { | ||
| 114 | $file .= ".gz" unless $file =~ /\.gz$/; | ||
| 115 | return $handle if open($handle, "| $GzipExe > $file"); | ||
| 116 | die qq{ERROR: Failed to exec "| $GzipExe > $file": $!\n}; | ||
| 117 | } | ||
| 118 | return $handle if open($handle, "> $file"); | ||
| 119 | die qq{ERROR: Failed to create "$file": $!\n}; | ||
| 120 | } | ||
| 121 | |||
| 122 | sub file_exists { | ||
| 123 | (-e $_[0]) || (-e "$_[0].gz"); | ||
| 124 | } | ||
| 125 | |||
| 126 | sub file_copy { | ||
| 127 | my ($src, $dst) = ($_[0], $_[1]); | ||
| 128 | my ($gz) = $src =~ /\.gz$/i; | ||
| 129 | |||
| 130 | if ($gz || (-e "$src.gz")) { | ||
| 131 | $src .= ".gz" unless $gz; | ||
| 132 | $dst .= ".gz" unless $dst =~ /\.gz$/i; | ||
| 133 | } | ||
| 134 | &cp($src, $dst); | ||
| 135 | } | ||
| 136 | |||
| 137 | sub file_rename { | ||
| 138 | my ($src, $dst) = ($_[0], $_[1]); | ||
| 139 | my ($gz) = $src =~ /\.gz$/i; | ||
| 140 | |||
| 141 | if ($gz || (-e "$src.gz")) { | ||
| 142 | $src .= ".gz" unless $gz; | ||
| 143 | $dst .= ".gz" unless $dst =~ /\.gz$/i; | ||
| 144 | } | ||
| 145 | if (!rename($src, $dst)) { | ||
| 146 | die qq/ERROR: Unable to rename "$src" to "$dst": $!\n/; | ||
| 147 | } | ||
| 148 | $dst; | ||
| 149 | } | ||
| 150 | |||
| 151 | sub file_remove { | ||
| 152 | my ($file) = shift; | ||
| 153 | |||
| 154 | unlink($file); | ||
| 155 | unlink("$file.gz"); | ||
| 156 | } | ||
| 157 | |||
| 158 | sub file_utime { | ||
| 159 | my ($atime) = shift; | ||
| 160 | my ($mtime) = shift; | ||
| 161 | foreach (@_) { | ||
| 162 | utime($atime, $mtime, $_, "$_.gz"); | ||
| 163 | } | ||
| 164 | } | ||
| 165 | |||
| 166 | sub file_temp { | ||
| 167 | my $template = shift; # Filename template | ||
| 168 | my $dir = shift || $CURDIR; # Where to write the file | ||
| 169 | my $suffix = shift; # Required suffix (optional) | ||
| 170 | my ($handle, $tmpfile); | ||
| 171 | |||
| 172 | MKTEMP: { | ||
| 173 | # Do not honor FASTTEMPFILES if a suffix is required, mainly | ||
| 174 | # because things like attachment writing will not work with | ||
| 175 | # FASTTEMPFILES. | ||
| 176 | if ($FastTempFiles && !defined($suffix)) { | ||
| 177 | $handle = gensym; | ||
| 178 | $tmpfile = join($DIRSEP, $dir, $template . $$); | ||
| 179 | if (!sysopen( | ||
| 180 | $handle, $tmpfile, (O_WRONLY | O_EXCL | O_CREAT), 0600 | ||
| 181 | ) | ||
| 182 | ) { | ||
| 183 | die qq/ERROR: Unable to create temp file "$tmpfile": $!\n/; | ||
| 184 | } | ||
| 185 | last MKTEMP; | ||
| 186 | } | ||
| 187 | |||
| 188 | # Use File::Temp when at all possible | ||
| 189 | if ($_have_File_Temp) { | ||
| 190 | my @tf_opts = ('DIR' => $dir, 'UNLINK' => 0); | ||
| 191 | push(@tf_opts, 'SUFFIX' => $suffix) if $suffix; | ||
| 192 | ($handle, $tmpfile) = File::Temp::tempfile($template, @tf_opts); | ||
| 193 | last MKTEMP; | ||
| 194 | } | ||
| 195 | |||
| 196 | $handle = gensym; | ||
| 197 | my ($i); | ||
| 198 | for ($i = 0; $i < TEMP_MAX_TRIES; ++$i) { | ||
| 199 | ($tmpfile = $template) =~ | ||
| 200 | s/X/$TEMP_CHARS[int(rand($#TEMP_CHARS))]/ge; | ||
| 201 | $tmpfile = join($DIRSEP, $dir, $tmpfile); | ||
| 202 | $tmpfile .= $suffix if defined($suffix); | ||
| 203 | last | ||
| 204 | if sysopen($handle, $tmpfile, | ||
| 205 | (O_WRONLY | O_EXCL | O_CREAT), 0600); | ||
| 206 | } | ||
| 207 | if ($i >= TEMP_MAX_TRIES) { | ||
| 208 | die qq/ERROR: Unable to create temp file "$tmpfile": $!\n/; | ||
| 209 | } | ||
| 210 | } | ||
| 211 | ($handle, $tmpfile); | ||
| 212 | } | ||
| 213 | |||
| 214 | sub file_chmod { | ||
| 215 | my $file = shift; | ||
| 216 | my $perm = shift || $FilePermsOct; | ||
| 217 | ## Capture any die's in case chmod not supported. | ||
| 218 | eval { | ||
| 219 | if (chmod(($perm & ~umask), $file) < 1) { | ||
| 220 | warn qq/Warning: Unable to change "$file" permissions to "/, | ||
| 221 | sprintf('%o' . $perm), | ||
| 222 | qq/": $!\n/; | ||
| 223 | } | ||
| 224 | }; | ||
| 225 | } | ||
| 226 | |||
| 227 | ##---------------------------------------------------------------------------## | ||
| 228 | |||
| 229 | sub dir_create { | ||
| 230 | my $path = shift; | ||
| 231 | my $perms = shift || 0777; | ||
| 232 | |||
| 233 | if (!$UNIX) { | ||
| 234 | ## Non-Unix OS's do not have symlinks | ||
| 235 | return if (-e $path); | ||
| 236 | if (!mkdir($path, $perms)) { | ||
| 237 | die qq/ERROR: Unable to create "$path": $!\n/; | ||
| 238 | } | ||
| 239 | return; | ||
| 240 | } | ||
| 241 | |||
| 242 | ## Check if $path is a symlink | ||
| 243 | if (-l $path) { | ||
| 244 | if ($FollowSymlinks) { | ||
| 245 | # Symlinks allowed, so we check if symlink is to a directory | ||
| 246 | die qq/ERROR: "$path" is not a directory: $!\n/ if !(-d $path); | ||
| 247 | return; | ||
| 248 | } | ||
| 249 | # symlink, try to delete | ||
| 250 | warn qq/Warning: "$path" is a symlink, will try to replace...\n/; | ||
| 251 | if (!unlink($path)) { | ||
| 252 | die qq/ERROR: "$path" is a symlink, unable to remove: $!\n/; | ||
| 253 | } | ||
| 254 | } elsif (-e $path) { | ||
| 255 | die qq/ERROR: "$path" is not a directory: $!\n/ if !(-d _); | ||
| 256 | # already exists, nothing to do | ||
| 257 | return; | ||
| 258 | } | ||
| 259 | |||
| 260 | my $dirname = dirname($path); | ||
| 261 | my @info = stat($dirname); | ||
| 262 | if ($info[2] & Fcntl::S_IWGRP || $info[2] & Fcntl::S_IWOTH) { | ||
| 263 | my ($i, $errstr, $tmpdir); | ||
| 264 | for ($i = 0; $i < TEMP_MAX_TRIES; ++$i) { | ||
| 265 | $tmpdir = dir_temp('dirXXXXXXXXXX', $dirname); | ||
| 266 | if (!rename($tmpdir, $path)) { | ||
| 267 | $errstr = "$!"; | ||
| 268 | rmdir($tmpdir); | ||
| 269 | if (-l $path) { | ||
| 270 | # hmmmm, somone trying to so something malicious? | ||
| 271 | warn qq/Warning: Possible symlink attack attempted with /, | ||
| 272 | qq/"$path"\n/; | ||
| 273 | die | ||
| 274 | qq/ERROR: "$path" is a symlink, unable to remove: $!\n/ | ||
| 275 | unless unlink $path; | ||
| 276 | } elsif (-d $path) { | ||
| 277 | # somebody snuck in and created it | ||
| 278 | return; | ||
| 279 | } elsif (-e _) { | ||
| 280 | die qq/ERROR: "$path" exists, but it did not before, /, | ||
| 281 | qq/and it is not a directory!\n/; | ||
| 282 | } | ||
| 283 | } else { | ||
| 284 | last; | ||
| 285 | } | ||
| 286 | } | ||
| 287 | if ($i >= TEMP_MAX_TRIES) { | ||
| 288 | die qq/ERROR: Unable to rename "$tmpdir" to "$path": $errstr\n/; | ||
| 289 | } | ||
| 290 | |||
| 291 | } else { | ||
| 292 | if (!mkdir($path, $perms)) { | ||
| 293 | die qq/ERROR: Unable to create "$path": $!\n/; | ||
| 294 | } | ||
| 295 | } | ||
| 296 | chmod(($perms & ~umask), $path); | ||
| 297 | } | ||
| 298 | |||
| 299 | sub dir_temp { | ||
| 300 | my $template = shift; | ||
| 301 | my $dir = shift || $CURDIR; | ||
| 302 | my ($tmpdir); | ||
| 303 | |||
| 304 | MKTEMP: { | ||
| 305 | if ($_have_File_Temp) { | ||
| 306 | $tmpdir = | ||
| 307 | File::Temp::tempdir($template, 'DIR' => $dir, 'CLEANUP' => 0); | ||
| 308 | last MKTEMP; | ||
| 309 | } | ||
| 310 | |||
| 311 | my ($i); | ||
| 312 | for ($i = 0; $i < TEMP_MAX_TRIES; ++$i) { | ||
| 313 | ($tmpdir = $template) =~ | ||
| 314 | s/X/$TEMP_CHARS[int(rand($#TEMP_CHARS))]/ge; | ||
| 315 | $tmpdir = join($DIRSEP, $dir, $tmpdir); | ||
| 316 | last if mkdir $tmpdir, 0700; | ||
| 317 | } | ||
| 318 | if ($i >= TEMP_MAX_TRIES) { | ||
| 319 | die qq/ERROR: Unable to create temp dir "$tmpdir": $!\n/; | ||
| 320 | } | ||
| 321 | } | ||
| 322 | $tmpdir; | ||
| 323 | } | ||
| 324 | |||
| 325 | sub dir_remove { | ||
| 326 | my ($file) = shift; | ||
| 327 | |||
| 328 | if (-d $file) { | ||
| 329 | local (*DIR); | ||
| 330 | local ($_); | ||
| 331 | if (!opendir(DIR, $file)) { | ||
| 332 | warn qq{Warning: Unable to open "$file"\n}; | ||
| 333 | return 0; | ||
| 334 | } | ||
| 335 | my @files = grep(!/^(\.|\..)$/i, readdir(DIR)); | ||
| 336 | closedir(DIR); | ||
| 337 | foreach (@files) { | ||
| 338 | &dir_remove($file . $mhonarc::DIRSEP . $_); | ||
| 339 | } | ||
| 340 | if (!rmdir($file)) { | ||
| 341 | warn qq{Warning: Unable to remove "$file": $!\n}; | ||
| 342 | return 0; | ||
| 343 | } | ||
| 344 | |||
| 345 | } else { | ||
| 346 | if (!unlink($file)) { | ||
| 347 | warn qq{Warning: Unable to delete "$file": $!\n}; | ||
| 348 | return 0; | ||
| 349 | } | ||
| 350 | } | ||
| 351 | 1; | ||
| 352 | } | ||
| 353 | |||
| 354 | ##---------------------------------------------------------------------------## | ||
| 355 | |||
| 356 | sub rand_string { | ||
| 357 | my $template = shift; | ||
| 358 | $template =~ s/X/$TEMP_CHARS[int(rand($#TEMP_CHARS))]/ge; | ||
| 359 | $template; | ||
| 360 | } | ||
| 361 | |||
| 362 | ##---------------------------------------------------------------------------## | ||
| 363 | 1; |