From postmaster@nic.funet.fi Fri Sep  1 09:40:05 2000
Received: from smtp.inet.fi ([IPv6:::ffff:192.89.123.192]:40119 "EHLO
	smtp.inet.fi" ident: "NO-IDENT-SERVICE[2]" smtp-auth: <none> TLS-CIPHER:
	<none>) by mea.tmt.tele.fi with ESMTP id <S32450AbQIAGjv>;
	Fri, 1 Sep 2000 09:39:51 +0300
Received: from delenn.icpd.sonera.fi ([194.137.76.101]:35243 "EHLO
        delenn.icpd.sonera.fi") by smtp.inet.fi with ESMTP
	id <S45415AbQIAGkD>; Fri, 1 Sep 2000 09:40:03 +0300
Received: from smtp.tele.fi ([192.89.123.25]:39001 "EHLO smtp.tele.fi")
	by icpd.sonera.fi with ESMTP id <S304515AbQIAGjx>;
	Fri, 1 Sep 2000 09:39:53 +0300
Received: from nic.funet.fi ([193.166.0.145]:42634 "EHLO nic.funet.fi")
	by smtp.tele.fi with ESMTP id <S21017AbQIAGjC>;
	Fri, 1 Sep 2000 09:39:02 +0300
Received: (mea@nic) by nic.funet.fi id <S27856AbQIAGfn>;
	Fri, 1 Sep 2000 09:35:43 +0300
Received: from chronos.sovam.com ([194.67.3.135]:36112 "EHLO chronos.sovam.com"
	ident: "NO-IDENT-SERVICE[2]" smtp-auth: <none> TLS-CIPHER: <none>)
	by nic.funet.fi with ESMTP id <S28587AbQHaKB2>;
	Thu, 31 Aug 2000 13:01:28 +0300
Received: from ariel ([194.67.3.216]:1662 "EHLO ariel.sovam.com" whoson:
	"-unregistered-" smtp-auth: <none> TLS-CIPHER: <none> TLS-PEER: <none>)
	by chronos.sovam.com with ESMTP id <S70675AbQHaKBJ>;
	Thu, 31 Aug 2000 14:01:09 +0400
Date:	Thu, 31 Aug 2000 14:01:09 +0400 (MSD)
From:	Eugene Crosser <crosser@online.ru>
Sender:	crosser@ariel.sovam.com
Reply-To: Eugene Crosser <crosser@online.ru>
Subject: bad file operation catcher
To:	mea@nic.funet.fi
Message-ID: <ML-3.4.967716069.5386.crosser@ariel.sovam.com>
MIME-Version: 1.0
Content-Type: TEXT/PLAIN; CHARSET=US-ASCII
Resent-From: mea@nic.funet.fi
Resent-Date: Fri, 1 Sep 2000 09:35:41 +0300
Resent-To: Matti Aarnio <matti.aarnio@icpd.sonera.fi>
Return-Path: <postmaster@nic.funet.fi>
X-Orcpt: rfc822;matti.aarnio@icpd.sonera.fi
Status: RO
Content-Length: 3390
Lines: 135

Index: router/rfc822.c
===================================================================
RCS file: /cvsroot/zmailer/router/rfc822.c,v
retrieving revision 1.47
diff -u -r1.47 rfc822.c
--- router/rfc822.c	2000/08/30 11:57:54	1.47
+++ router/rfc822.c	2000/08/31 09:59:55
@@ -95,6 +95,22 @@
      const char *path;
 {
   int rc;
+
+#ifdef PARANOID_FILEOPS
+  struct stat st;
+
+  if (stat(path,&st)) {
+	zsyslog((LOG_ALERT, "zunlink: stat %s errno=%d", path, errno));
+	fprintf(stderr, "zunlink: stat %s errno=%d", path, errno);
+	return -1;
+  }
+  if (!S_ISREG(st.st_mode)) {
+	zsyslog((LOG_ALERT, "zunlink: not regular file %s", path));
+	fprintf(stderr, "zunlink: not regular file %s", path);
+	return -1;
+  }
+#endif /* PARANOID_FILEOPS */
+
   while ((rc = unlink(path)) < 0 && (errno == EBUSY || errno == EINTR))
     ;
   return rc;
Index: lib/esyslib.c
===================================================================
RCS file: /cvsroot/zmailer/lib/esyslib.c,v
retrieving revision 1.4
diff -u -r1.4 esyslib.c
--- lib/esyslib.c	2000/08/02 18:23:03	1.4
+++ lib/esyslib.c	2000/08/31 09:59:55
@@ -122,6 +122,39 @@
 {
 	int	r;
 
+#ifdef PARANOID_FILEOPS
+	struct stat st;
+
+	if (stat(file1,&st)) {
+	  int serrno = errno;
+	  fprintf(stderr, "%s: link(%s,%s): stat(%s) error %s\n", progname,
+		  file1, file2, file1, strerror(errno));
+	  errno = serrno;
+	  return -1;
+	}
+	if (!S_ISREG(st.st_mode)) {
+	  int serrno = errno;
+	  fprintf(stderr, "%s: link(%s,%s): %s is not file!\n", progname,
+		  file1, file2, file1);
+	  errno = serrno;
+	  return -1;
+	}
+	if (stat(file2,&st)) {
+	  int serrno = errno;
+	  fprintf(stderr, "%s: link(%s,%s): stat(%s) error %s\n", progname,
+		  file1, file2, file2, strerror(errno));
+	  errno = serrno;
+	  return -1;
+	}
+	if (!S_ISREG(st.st_mode)) {
+	  int serrno = errno;
+	  fprintf(stderr, "%s: link(%s,%s): %s is not file!\n", progname,
+		  file1, file2, file2);
+	  errno = serrno;
+	  return -1;
+	}
+#endif /* PARANOID_FILEOPS */
+
 	r = link(file1, file2);
 	if (r < 0) {
 	  int serrno = errno;
@@ -138,6 +171,25 @@
 {
 	int	r;
 
+#ifdef PARANOID_FILEOPS
+	struct stat st;
+
+	if (stat(file,&st)) {
+	  int serrno = errno;
+	  fprintf(stderr, "%s: unlink(%s)[%s]: stat() error %s\n", progname,
+		  file, tag, strerror(errno));
+	  errno = serrno;
+	  return -1;
+	}
+	if (!S_ISREG(st.st_mode)) {
+	  int serrno = errno;
+	  fprintf(stderr, "%s: unlink(%s)[%s]: is not file!\n", progname,
+		  file, tag);
+	  errno = serrno;
+	  return -1;
+	}
+#endif /* PARANOID_FILEOPS */
+
 	while ((r = unlink(file)) < 0 && (errno == EBUSY || errno == EINTR))
 	  ;
 	if (r < 0) {
@@ -240,6 +292,31 @@
 erename(from, to)
 	const char *from, *to;
 {
+#ifdef PARANOID_FILEOPS
+	struct stat st;
+
+	if (stat(from,&st)) {
+	  int serrno = errno;
+	  fprintf(stderr, "%s: rename(%s,%s): stat(%s) error %s\n", progname,
+		  from, to, from, strerror(errno));
+	  errno = serrno;
+	  return -1;
+	}
+	if (!S_ISREG(st.st_mode)) {
+	  int serrno = errno;
+	  fprintf(stderr, "%s: rename(%s,%s): %s is not file!\n", progname,
+		  from, to, from);
+	  errno = serrno;
+	  return -1;
+	}
+	if (stat(to,&st) == 0) {
+	  int serrno = errno;
+	  fprintf(stderr, "%s: rename(%s,%s): %s exists!\n", progname,
+		  from, to, to);
+	  errno = serrno;
+	  return -1;
+	}
+#endif /* PARANOID_FILEOPS */
 
 #ifdef	HAVE_RENAME
 	while (rename(from, to) < 0) {

