From 5e8d38ea8388469c216822206cf46a4ccd054ea7 Mon Sep 17 00:00:00 2001
From: rswindell <>
Date: Mon, 8 Sep 2003 23:06:55 +0000
Subject: [PATCH] Using MAX_PATH+1 for path name array lengths (instead of
 128). Created new functions: smb_lock, smb_unlock, and smb_islocked - used to
 lock a message base during maintenance/packing. New smb error constant:
 SMB_ERR_DELETE. Created anonymous union members in smbmsg_t for future
 filebase support. Increased SMBLIB_VERSION to 2.30.

---
 src/smblib/smbdefs.h | 16 ++++++++---
 src/smblib/smblib.c  | 66 ++++++++++++++++++++++++++++++++++++++++----
 src/smblib/smblib.h  |  3 ++
 3 files changed, 75 insertions(+), 10 deletions(-)

diff --git a/src/smblib/smbdefs.h b/src/smblib/smbdefs.h
index bc6cdb74b3..717d880398 100644
--- a/src/smblib/smbdefs.h
+++ b/src/smblib/smbdefs.h
@@ -39,6 +39,7 @@
 #define _SMBDEFS_H
 
 #include <stdio.h>
+#include "dirwrap.h"	/* MAX_PATH */
 
 /**********/
 /* Macros */
@@ -136,6 +137,7 @@
 #define SMB_ERR_WRITE		-204		/* File write error */
 #define SMB_ERR_TIMEOUT		-205		/* File operation timed-out */
 #define SMB_ERR_FILE_LEN	-206		/* File length invalid */
+#define SMB_ERR_DELETE		-207		/* File deletion error */
 #define SMB_ERR_MEM			-300		/* Memory allocation error */
 
 #define SMB_DUPE_MSG		1			/* Duplicate message detected by smb_addcrc() */
@@ -548,9 +550,15 @@ typedef struct {				// Message
 				*ftn_area,		// FTN AREA
 				*ftn_flags,		// FTN FLAGS
 				*ftn_msgid,		// FTN MSGID
-				*ftn_reply,		// FTN REPLY
-				*subj,			// Subject (or filename)
-				*summary;		// Summary (or file description)
+				*ftn_reply;		// FTN REPLY
+	union {
+		char*	summary;		// Summary 
+		char*	description;	// (or file description)
+	};
+	union {
+		char*	subj;			// Subject 
+		char*	name;			// (or filename)
+	};
 	ushort		to_agent,		// Type of agent message is to
 				from_agent, 	// Type of agent message is from
 				replyto_agent;	// Type of agent replies should be sent to
@@ -582,7 +590,7 @@ typedef struct {			// Message base
 	smbstatus_t status; 	// Status header record
 	int		locked;			// SMB header is locked
 	char	shd_buf[SHD_BLOCK_LEN]; 	// File I/O buffer for header file
-	char	last_error[128];			// Last error message
+	char	last_error[MAX_PATH*2];		// Last error message
 
 	/* Private member variables (not initialized by or used by smblib) */
 	uint	subnum;			// Sub-board number
diff --git a/src/smblib/smblib.c b/src/smblib/smblib.c
index 94bdff84a7..4876bfe24b 100644
--- a/src/smblib/smblib.c
+++ b/src/smblib/smblib.c
@@ -68,7 +68,7 @@
 #include "filewrap.h"
 
 /* Use smb_ver() and smb_lib_ver() to obtain these values */
-#define SMBLIB_VERSION		"2.23"      /* SMB library version */
+#define SMBLIB_VERSION		"2.30"      /* SMB library version */
 #define SMB_VERSION 		0x0121		/* SMB format version */
 										/* High byte major, low byte minor */
 
@@ -107,9 +107,13 @@ static char* DLLCALL truncsp(char *str)
 int SMBCALL smb_open(smb_t* smb)
 {
     int file;
-    char str[128];
+    char str[MAX_PATH+1];
 	smbhdr_t hdr;
 
+	/* Check for message-base lock semaphore file (under maintenance?) */
+	if(smb_islocked(smb))
+		return(SMB_ERR_OPEN);
+
 	/* Set default values, if uninitialized */
 	if(!smb->retry_time)
 		smb->retry_time=10;		/* seconds */
@@ -234,7 +238,7 @@ void SMBCALL smb_close(smb_t* smb)
 int SMBCALL smb_open_da(smb_t* smb)
 {
 	int 	file;
-	char	str[128];
+	char	str[MAX_PATH+1];
 	time_t	start=0;
 
 	sprintf(str,"%s.sda",smb->file);
@@ -281,7 +285,7 @@ void SMBCALL smb_close_da(smb_t* smb)
 int SMBCALL smb_open_ha(smb_t* smb)
 {
 	int 	file;
-	char	str[128];
+	char	str[MAX_PATH+1];
 	time_t	start=0;
 
 	sprintf(str,"%s.sha",smb->file);
@@ -320,6 +324,56 @@ void SMBCALL smb_close_ha(smb_t* smb)
 	smb->sha_fp=NULL;
 }
 
+/****************************************************************************/
+/* This set of functions is used to exclusively-lock an entire message base	*/
+/* against any other process opening any of the message base files.			*/
+/* Currently, this is only used while smbutil packs a message base.			*/
+/* This is achieved with a semaphore lock file (e.g. mail.lock).			*/
+/****************************************************************************/
+static char* smb_lockfname(smb_t* smb, char* fname)
+{
+	sprintf(fname,"%s.lock",smb->file);
+	return(fname);
+}
+
+int SMBCALL smb_lock(smb_t* smb)
+{
+	char	str[MAX_PATH+1];
+	int		file;
+
+	smb_lockfname(smb,str);
+	if((file=open(str,O_CREAT|O_EXCL|O_RDWR,S_IREAD|S_IWRITE))==-1) {
+		sprintf(smb->last_error,"%d (%s) creating %s"
+			,errno,STRERROR(errno),str);
+		return(SMB_ERR_LOCK);
+	}
+	close(file);
+	return(0);
+}
+
+int SMBCALL smb_unlock(smb_t* smb)
+{
+	char	str[MAX_PATH+1];
+
+	smb_lockfname(smb,str);
+	if(remove(str)!=0) {
+		sprintf(smb->last_error,"%d (%s) removing %s"
+			,errno,STRERROR(errno),str);
+		return(SMB_ERR_DELETE);
+	}
+	return(0);
+}
+
+int SMBCALL smb_islocked(smb_t* smb)
+{
+	char	str[MAX_PATH+1];
+
+	if(access(smb_lockfname(smb,str),0)!=0)
+		return(0);
+	sprintf(smb->last_error,"%s exists",str);
+	return(1);
+}
+
 /****************************************************************************/
 /* If the parameter 'push' is non-zero, this function stores the currently  */
 /* open message base to the "virtual" smb stack. Up to SMB_STACK_LEN        */
@@ -1231,7 +1285,7 @@ int SMBCALL smb_dfield(smbmsg_t* msg, ushort type, ulong length)
 /****************************************************************************/
 int SMBCALL smb_addcrc(smb_t* smb, ulong crc)
 {
-	char	str[128];
+	char	str[MAX_PATH+1];
 	int 	file;
 	int		wr;
 	long	length;
@@ -1532,7 +1586,7 @@ int SMBCALL smb_putmsghdr(smb_t* smb, smbmsg_t* msg)
 /****************************************************************************/
 int SMBCALL smb_create(smb_t* smb)
 {
-    char        str[128];
+    char        str[MAX_PATH+1];
 	smbhdr_t	hdr;
 
 	if(smb->shd_fp==NULL || smb->sdt_fp==NULL || smb->sid_fp==NULL) {
diff --git a/src/smblib/smblib.h b/src/smblib/smblib.h
index 57d706fcde..54a7d72fe4 100644
--- a/src/smblib/smblib.h
+++ b/src/smblib/smblib.h
@@ -104,6 +104,9 @@ SMBEXPORT void	SMBCALL smb_close_ha(smb_t* smb);
 SMBEXPORT int 	SMBCALL smb_create(smb_t* smb);
 SMBEXPORT int 	SMBCALL smb_stack(smb_t* smb, int op);
 SMBEXPORT int 	SMBCALL smb_trunchdr(smb_t* smb);
+SMBEXPORT int	SMBCALL smb_lock(smb_t* smb);
+SMBEXPORT int	SMBCALL smb_unlock(smb_t* smb);
+SMBEXPORT int	SMBCALL smb_islocked(smb_t* smb);
 SMBEXPORT int 	SMBCALL smb_locksmbhdr(smb_t* smb);
 SMBEXPORT int 	SMBCALL smb_getstatus(smb_t* smb);
 SMBEXPORT int 	SMBCALL smb_putstatus(smb_t* smb);
-- 
GitLab