xref: /openbsd/usr.bin/vi/build/recover (revision 133306f0)
1#!/usr/bin/perl -w
2#
3# $OpenBSD: recover,v 1.7 2001/01/11 04:56:52 millert Exp $
4#
5# Script to (safely) recover nvi edit sessions.
6#
7
8use Fcntl;
9require 'sys/syscall.ph';
10
11$recoverdir = $ARGV[0] || "/var/tmp/vi.recover";
12$sendmail = "/usr/sbin/sendmail";
13
14die "Sorry, $0 must be run as root\n" if $>;
15
16# Make the recovery dir if it does not already exist.
17if (!sysopen(DIR, $recoverdir, O_RDONLY|O_NOFOLLOW) || !stat(DIR)) {
18	die "Warning! $recoverdir is a symbolic link! (ignoring)\n"
19	    if -l $recoverdir;
20	mkdir($recoverdir, 01777) || die "Unable to create $recoverdir: $!\n";
21	chmod(01777, $recoverdir);
22	exit(0);
23}
24
25#
26# Sanity check the vi recovery dir
27# Perl doesn't support fchdir, fchmod, or fchown so we call
28# fchdir(2) via the syscall interface and then just modify ".".
29#
30die "Warning! $recoverdir is not a directory! (ignoring)\n"
31    unless -d _;
32die "$0: can't chdir to $recoverdir: $!\n"
33    unless syscall(&SYS_fchdir, fileno(DIR)) == 0;
34if (! -O _) {
35	warn "Warning! $recoverdir is not owned by root! (fixing)\n";
36	chown(0, 0, ".");
37}
38if (((stat(_))[2] & 07777) != 01777) {
39	warn "Warning! $recoverdir is not mode 01777! (fixing)\n";
40	chmod(01777, ".");
41}
42
43# Check editor backup files.
44opendir(RECDIR, ".") || die "$0: can't open $recoverdir: $!\n";
45foreach $file (readdir(RECDIR)) {
46	next unless $file =~ /^vi\./;
47
48	#
49	# Unmodified vi editor backup files either have the
50	# execute bit set or are zero length.  Delete them.
51	# Anything that is not a normal file gets deleted too.
52	#
53	lstat($file) || die "$0: can't stat $file: $!\n";
54	if (-x _ || ! -s _ || ! -f _) {
55		unlink($file) unless -d _;
56	}
57}
58
59#
60# It is possible to get incomplete recovery files if the editor crashes
61# at the right time.
62#
63rewinddir(RECDIR);
64foreach $file (readdir(RECDIR)) {
65	next unless $file =~ /^recover\./;
66
67	if (!sysopen(RECFILE, $file, O_RDONLY|O_NOFOLLOW)) {
68	    warn "$0: can't open $file: $!\n";
69	    next;
70	}
71
72	#
73	# Delete anything that is not a regular file as that is either
74	# filesystem corruption from fsck or an exploit attempt.
75	#
76	if (!stat(RECFILE)) {
77		warn "$0: can't stat $file: $!\n";
78		close(RECFILE);
79		next;
80	}
81	if (! -f _ || ! -s _) {
82		unlink($file) unless -d _;
83		close(RECFILE);
84		next;
85	}
86
87	#
88	# Slurp in the recover.* file and search for X-vi-recover-path
89	# (which should point to an existing vi.* file).
90	#
91	@recfile = <RECFILE>;
92	close(RECFILE);
93
94	#
95	# Delete any recovery files that have no (or more than one)
96	# corresponding backup file.
97	#
98	@backups = grep(/^X-vi-recover-path:/, @recfile);
99	unlink($file) unless $#backups == 0;
100
101	#
102	# If recovery file is zero length, remove it.
103	# Else send mail to the user.
104	#
105	$backups[0] =~ /^X-vi-recover-path:\s*(.*)[\r\n]*$/;
106	$backup = $1;
107	if (! -s $backup) {
108		unlink($file, $backup);
109	} else {
110		open(SENDMAIL, "|$sendmail -t") ||
111		    die "$0: can't run $sendmail -t: $!\n";
112		print SENDMAIL @recfile;
113		close(SENDMAIL);
114	}
115}
116closedir(RECDIR);
117close(DIR);
118
119exit(0);
120