smbutil.c 49.4 KB
Newer Older
1
2
/* Synchronet message base (SMB) utility */

3
/* $Id: smbutil.c,v 1.136 2020/05/25 00:40:34 rswindell Exp $ */
4
// vi: tabstop=4
5
6
7
8
9

/****************************************************************************
 * @format.tab-size 4		(Plain Text/Source Code File Header)			*
 * @format.use-tabs true	(see http://www.synchro.net/ptsc_hdr.html)		*
 *																			*
10
 * Copyright Rob Swindell - http://www.synchro.net/copyright.html			*
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
 *																			*
 * This program is free software; you can redistribute it and/or			*
 * modify it under the terms of the GNU General Public License				*
 * as published by the Free Software Foundation; either version 2			*
 * of the License, or (at your option) any later version.					*
 * See the GNU General Public License for more details: gpl.txt or			*
 * http://www.fsf.org/copyleft/gpl.html										*
 *																			*
 * Anonymous FTP access to the most recent released source is available at	*
 * ftp://vert.synchro.net, ftp://cvs.synchro.net and ftp://ftp.synchro.net	*
 *																			*
 * Anonymous CVS access to the development source and modification history	*
 * is available at cvs.synchro.net:/cvsroot/sbbs, example:					*
 * cvs -d :pserver:anonymous@cvs.synchro.net:/cvsroot/sbbs login			*
 *     (just hit return, no password is necessary)							*
 * cvs -d :pserver:anonymous@cvs.synchro.net:/cvsroot/sbbs checkout src		*
 *																			*
 * For Synchronet coding style and modification guidelines, see				*
 * http://www.synchro.net/source.html										*
 *																			*
 * You are encouraged to submit any modifications (preferably in Unix diff	*
 * format) via e-mail to mods@synchro.net									*
 *																			*
 * Note: If this box doesn't appear square, then you need to fix your tabs.	*
 ****************************************************************************/

rswindell's avatar
rswindell committed
37
#define SMBUTIL_VER "2.34"
38
39
char	revision[16];
char	compiler[32];
40

41
42
43
44
45
const char *wday[]={"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
const char *mon[]={"Jan","Feb","Mar","Apr","May","Jun"
            ,"Jul","Aug","Sep","Oct","Nov","Dec"};


46
#define NOANALYSIS		(1L<<0)
47
#define NOCRC			(1L<<1)
48
49
50
51
52
53
54

#ifdef __WATCOMC__
	#define ffblk find_t
    #define findfirst(x,y,z) _dos_findfirst(x,z,y)
	#define findnext(x) _dos_findnext(x)
#endif

55
56
57
58
59
#if defined(_WIN32)
	#include <conio.h>	/* getch() */
#endif

/* ANSI */
60
#include <stdio.h>
rswindell's avatar
rswindell committed
61
62
63
64
#include <time.h>		/* time */
#include <errno.h>		/* errno */
#include <string.h>		/* strrchr */
#include <ctype.h>		/* toupper */
65

66
#include "utf8.h"
67
68
#include "sbbs.h"
#include "conwrap.h"
69

70
/* gets is dangerous */
71
#define gets(str)  fgets((str), sizeof(str), stdin)
72

73
74
#define CHSIZE_FP(fp,size)	if(chsize(fileno(fp),size)) printf("chsize failed!\n");

75
76
77
78
/********************/
/* Global variables */
/********************/

79
80
81
82
83
84
85
86
87
smb_t		smb;
ulong		mode=0L;
ushort		tzone=0;
ushort		xlat=XLAT_NONE;
FILE*		nulfp;
FILE*		errfp;
FILE*		statfp;
BOOL		pause_on_exit=FALSE;
BOOL		pause_on_error=FALSE;
88
char*		beep="";
rswindell's avatar
rswindell committed
89
long		msgtxtmode = GETMSGTXT_ALL|GETMSGTXT_PLAIN;
90
91
92
93
94
95

/************************/
/* Program usage/syntax */
/************************/

char *usage=
96
"usage: smbutil [-opts] cmd <filespec.shd>\n"
97
98
99
100
"\n"
"cmd:\n"
"       l[n] = list msgs starting at number n\n"
"       r[n] = read msgs starting at number n\n"
101
"       x[n] = dump msg index at number n\n"
102
"       v[n] = view msg headers starting at number n\n"
103
104
105
"       i[f] = import msg from text file f (or use stdin)\n"
"       e[f] = import e-mail from text file f (or use stdin)\n"
"       n[f] = import netmail from text file f (or use stdin)\n"
rswindell's avatar
rswindell committed
106
"       h    = dump hash file\n"
107
108
"       s    = display msg base status\n"
"       c    = change msg base status\n"
rswindell's avatar
rswindell committed
109
"       R    = re-initialize/repair SMB/status headers\n"
110
"       d    = delete all msgs\n"
111
112
"       m    = maintain msg base - delete old msgs and msgs over max\n"
"       p[k] = pack msg base (k specifies minimum packable Kbytes)\n"
rswindell's avatar
rswindell committed
113
114
"       L    = lock a msg base for exclusive-access/backup\n"
"       U    = unlock a msg base\n"
115
116
117
118
119
"\n"
"            [n] may represent 1-based message index offset, or\n"
"            [#n] actual message number, or\n"
"            [-n] message age (in days)\n"
"\n"
120
"opts:\n"
rswindell's avatar
rswindell committed
121
122
123
124
125
126
127
128
129
"      -c[m] = create message base if it doesn't exist (m=max msgs)\n"
"      -a    = always pack msg base (disable compression analysis)\n"
"      -i    = ignore dupes (do not store CRCs or search for duplicate hashes)\n"
"      -d    = use default values (no prompt) for to, from, and subject\n"
"      -l    = LZH-compress message text\n"
"      -o    = print errors on stdout (instead of stderr)\n"
"      -p    = wait for keypress (pause) on exit\n"
"      -!    = wait for keypress (pause) on error\n"
"      -b    = beep on error\n"
rswindell's avatar
rswindell committed
130
"      -r    = display raw message body text (not MIME-decoded)\n"
rswindell's avatar
rswindell committed
131
132
133
134
135
136
137
138
"      -C    = continue after some (normally fatal) error conditions\n"
"      -t<s> = set 'to' user name for imported message\n"
"      -n<s> = set 'to' netmail address for imported message\n"
"      -u<s> = set 'to' user number for imported message\n"
"      -f<s> = set 'from' user name for imported message\n"
"      -e<s> = set 'from' user number for imported message\n"
"      -s<s> = set 'subject' for imported message\n"
"      -z[n] = set time zone (n=min +/- from UT or 'EST','EDT','CST',etc)\n"
139
#ifdef __unix__
140
"      -U[n] = set umask to specified value (use leading 0 for octal, e.g. 022)\n"
141
#endif
rswindell's avatar
rswindell committed
142
"      -#    = set number of messages to view/list (e.g. -1)\n"
143
144
;

145
146
147
148
149
150
151
152
153
154
155
156
157
void bail(int code)
{

	if(pause_on_exit || (code && pause_on_error)) {
		fprintf(statfp,"\nHit enter to continue...");
		getchar();
	}

	if(code)
		fprintf(statfp,"\nReturning error code: %d\n",code);
	exit(code);
}

158
/*****************************************************************************/
159
/* Expands Unix LF to CRLF													 */
160
/*****************************************************************************/
161
ulong lf_expand(uchar* inbuf, uchar* outbuf)
162
163
{
	ulong	i,j;
rswindell's avatar
rswindell committed
164

165
	for(i=j=0;inbuf[i];i++) {
166
167
168
169
		if(inbuf[i]=='\n' && (!i || inbuf[i-1]!='\r'))
			outbuf[j++]='\r';
		outbuf[j++]=inbuf[i];
	}
170
	outbuf[j]=0;
171
172
	return(j);
}
rswindell's avatar
rswindell committed
173

174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
char* gen_msgid(smb_t* smb, smbmsg_t* msg, char* msgid, size_t maxlen)
{
	char* host = getenv(
#if defined(_WIN32)
		"COMPUTERNAME"
#else
		"HOSTNAME"
#endif
	);
	if(host == NULL)
		host = getenv(
#if defined(_WIN32)
		"USERNAME"
#else
		"USER"
#endif
	);
	safe_snprintf(msgid, maxlen
		,"<%08lX.%lu.%s@%s>"
193
194
		,(ulong)msg->hdr.when_imported.time
		,(ulong)smb->status.last_msg + 1
195
196
197
198
199
		,getfname(smb->file)
		,host);
	return msgid;
}

200
201
202
/****************************************************************************/
/* Adds a new message to the message base									*/
/****************************************************************************/
203
204
void postmsg(char type, char* to, char* to_number, char* to_address, 
			 char* from, char* from_number, char* subject, FILE* fp)
205
{
206
207
	char		str[128];
	char		buf[1024];
rswindell's avatar
rswindell committed
208
209
	char*		msgtxt=NULL;
	char*		newtxt;
210
	const char* charset = NULL;
211
212
	long		msgtxtlen;
	int 		i;
213
	ushort		agent=AGENT_SMBUTIL;
214
	smbmsg_t	msg;
215
	long		dupechk_hashes=SMB_HASH_SOURCE_DUPE;
216

217
218
219
220
221
222
	/* Read message text from stream (file or stdin) */
	msgtxtlen=0;
	while(!feof(fp)) {
		i=fread(buf,1,sizeof(buf),fp);
		if(i<1)
			break;
rswindell's avatar
rswindell committed
223
		if((msgtxt = realloc(msgtxt,msgtxtlen+i+1))==NULL) {
224
			fprintf(errfp,"\n%s!realloc(%ld) failure\n",beep,msgtxtlen+i+1);
225
			bail(1);
226
		}
227
		memcpy(msgtxt+msgtxtlen,buf,i);
228
229
230
		msgtxtlen+=i;
	}

231
	if(msgtxt!=NULL) {
232
233
234

		msgtxt[msgtxtlen]=0;	/* Must be NULL-terminated */

rswindell's avatar
rswindell committed
235
		if((newtxt = malloc((msgtxtlen*2)+1))==NULL) {
236
			fprintf(errfp,"\n%s!malloc(%ld) failure\n",beep,(msgtxtlen*2)+1);
237
			bail(1);
238
		}
rswindell's avatar
rswindell committed
239

240
		/* Expand LFs to CRLFs */
rswindell's avatar
rswindell committed
241
		msgtxtlen=lf_expand((uchar*)msgtxt, (uchar*)newtxt);
242
243
		free(msgtxt);
		msgtxt=newtxt;
244
245
246
247
248
249
250

		if(!str_is_ascii(msgtxt) && utf8_str_is_valid(msgtxt))
			charset = FIDO_CHARSET_UTF8;
		else if(str_is_ascii(msgtxt))
			charset = FIDO_CHARSET_ASCII;
		else
			charset = FIDO_CHARSET_CP437;
251
	}
rswindell's avatar
rswindell committed
252
253

	memset(&msg,0,sizeof(smbmsg_t));
254
	msg.hdr.when_written.time=(uint32_t)time(NULL);
rswindell's avatar
rswindell committed
255
256
257
	msg.hdr.when_written.zone=tzone;
	msg.hdr.when_imported=msg.hdr.when_written;

258
259
	if((to==NULL || stricmp(to,"All")==0) && to_address!=NULL)
		to=to_address;
260
261
	if(to==NULL) {
		printf("To User Name: ");
262
		fgets(str,sizeof(str),stdin); 
263
	} else
264
		SAFECOPY(str,to);
rswindell's avatar
rswindell committed
265
	truncsp(str);
266
267

	if((i=smb_hfield_str(&msg,RECIPIENT,str))!=SMB_SUCCESS) {
268
269
		fprintf(errfp,"\n%s!smb_hfield_str(0x%02X) returned %d: %s\n"
			,beep,RECIPIENT,i,smb.last_error);
270
		bail(1); 
rswindell's avatar
rswindell committed
271
	}
rswindell's avatar
rswindell committed
272
273
274
	if(type=='E' || type=='N')
		smb.status.attr|=SMB_EMAIL;
	if(smb.status.attr&SMB_EMAIL) {
275
276
277
278
279
280
281
282
283
284
285
286
		if(to_address==NULL) {
			if(to_number==NULL) {
				printf("To User Number: ");
				gets(str);
			} else
				SAFECOPY(str,to_number);
			truncsp(str);
			if((i=smb_hfield_str(&msg,RECIPIENTEXT,str))!=SMB_SUCCESS) {
				fprintf(errfp,"\n%s!smb_hfield_str(0x%02X) returned %d: %s\n"
					,beep,RECIPIENTEXT,i,smb.last_error);
				bail(1); 
			}
287
288
		}
	}
rswindell's avatar
rswindell committed
289

290
	if(smb.status.attr&SMB_EMAIL && (type=='N' || to_address!=NULL)) {
291
		if(to_address==NULL) {
292
			printf("To Address (e.g. user@host or 1:2/3): ");
293
294
			gets(str);
		} else
295
			SAFECOPY(str,to_address);
rswindell's avatar
rswindell committed
296
297
		truncsp(str);
		if(*str) {
298
299
			if((i=smb_hfield_netaddr(&msg,RECIPIENTNETADDR,str,NULL))!=SMB_SUCCESS) {
				fprintf(errfp,"\n%s!smb_hfield_netaddr(0x%02X) returned %d: %s\n"
300
					,beep,RECIPIENTNETADDR,i,smb.last_error);
301
				bail(1); 
302
			}
303
		}
304
	}
rswindell's avatar
rswindell committed
305

306
307
308
309
	if(from==NULL) {
		printf("From User Name: ");
		gets(str);
	} else
310
		SAFECOPY(str,from);
rswindell's avatar
rswindell committed
311
	truncsp(str);
312
	if((i=smb_hfield_str(&msg,SENDER,str))!=SMB_SUCCESS) {
313
314
		fprintf(errfp,"\n%s!smb_hfield_str(0x%02X) returned %d: %s\n"
			,beep,SENDER,i,smb.last_error);
315
		bail(1); 
rswindell's avatar
rswindell committed
316
	}
317
	if((smb.status.attr&SMB_EMAIL) || from_number!=NULL) {
318
319
320
321
		if(from_number==NULL) {
			printf("From User Number: ");
			gets(str);
		} else
322
			SAFECOPY(str,from_number);
rswindell's avatar
rswindell committed
323
		truncsp(str);
324
		if((i=smb_hfield_str(&msg,SENDEREXT,str))!=SMB_SUCCESS) {
325
326
			fprintf(errfp,"\n%s!smb_hfield_str(0x%02X) returned %d: %s\n"
				,beep,SENDEREXT,i,smb.last_error);
327
			bail(1); 
328
329
		}
	}
330
	if((i=smb_hfield(&msg, SENDERAGENT, sizeof(agent), &agent))!=SMB_SUCCESS) {
331
332
		fprintf(errfp,"\n%s!smb_hfield(0x%02X) returned %d: %s\n"
			,beep,SENDERAGENT,i,smb.last_error);
333
		bail(1);
334
	}
rswindell's avatar
rswindell committed
335

336
337
338
339
	if(subject==NULL) {
		printf("Subject: ");
		gets(str);
	} else
340
		SAFECOPY(str,subject);
rswindell's avatar
rswindell committed
341
	truncsp(str);
342
	if((i=smb_hfield_str(&msg,SUBJECT,str))!=SMB_SUCCESS) {
343
344
		fprintf(errfp,"\n%s!smb_hfield_str(0x%02X) returned %d: %s\n"
			,beep,SUBJECT,i,smb.last_error);
345
		bail(1); 
346
	}
rswindell's avatar
rswindell committed
347

348
	safe_snprintf(str,sizeof(str),"SMBUTIL %s-%s r%s %s %s"
349
350
351
352
353
354
		,SMBUTIL_VER
		,PLATFORM_DESC
		,revision
		,__DATE__
		,compiler
		);
355
	if((i=smb_hfield_str(&msg,FIDOPID,str))!=SMB_SUCCESS) {
356
357
		fprintf(errfp,"\n%s!smb_hfield_str(0x%02X) returned %d: %s\n"
			,beep,FIDOPID,i,smb.last_error);
358
		bail(1); 
359
	}
rswindell's avatar
rswindell committed
360

361
	smb_hfield_str(&msg, RFC822MSGID, gen_msgid(&smb, &msg, str, sizeof(str)-1));
362
363
364
365
366
367
368
	if(charset != NULL)
		smb_hfield_str(&msg, FIDOCHARSET, charset);

	if((msg.to != NULL && !str_is_ascii(msg.to) && utf8_str_is_valid(msg.to))
		|| (msg.from != NULL && !str_is_ascii(msg.from) && utf8_str_is_valid(msg.from))
		|| (msg.subj != NULL && !str_is_ascii(msg.subj) && utf8_str_is_valid(msg.subj)))
		msg.hdr.auxattr |= MSG_HFIELDS_UTF8;
369

370
371
372
	if(mode&NOCRC || smb.status.max_crcs==0)	/* no CRC checking means no body text dupe checking */
		dupechk_hashes&=~(1<<SMB_HASH_SOURCE_BODY);

373
	if((i=smb_addmsg(&smb,&msg,smb.status.attr&SMB_HYPERALLOC
rswindell's avatar
rswindell committed
374
		,dupechk_hashes,xlat,(uchar*)msgtxt,NULL))!=SMB_SUCCESS) {
375
376
		fprintf(errfp,"\n%s!smb_addmsg returned %d: %s\n"
			,beep,i,smb.last_error);
377
		bail(1); 
rswindell's avatar
rswindell committed
378
	}
rswindell's avatar
rswindell committed
379
	smb_freemsgmem(&msg);
380

deuce's avatar
deuce committed
381
382
	// MSVC can't do %zu for size_t until MSVC 2017 it seems...
	fprintf(statfp, "Message (%" PRIu64 " bytes) added to %s successfully\n", (uint64_t)strlen((char *)msgtxt), smb.file);
383
	FREE_AND_NULL(msgtxt);
384
385
386
387
388
}

/****************************************************************************/
/* Shows the message base header											*/
/****************************************************************************/
389
void showstatus(void)
390
391
392
{
	int i;

rswindell's avatar
rswindell committed
393
394
	i=smb_locksmbhdr(&smb);
	if(i) {
395
396
		fprintf(errfp,"\n%s!smb_locksmbhdr returned %d: %s\n"
			,beep,i,smb.last_error);
rswindell's avatar
rswindell committed
397
398
		return; 
	}
rswindell's avatar
rswindell committed
399
400
401
	i=smb_getstatus(&smb);
	smb_unlocksmbhdr(&smb);
	if(i) {
402
403
		fprintf(errfp,"\n%s!smb_getstatus returned %d: %s\n"
			,beep,i,smb.last_error);
rswindell's avatar
rswindell committed
404
405
		return; 
	}
deuce's avatar
deuce committed
406
407
408
409
410
	printf("last_msg        =%"PRIu32"\n"
		   "total_msgs      =%"PRIu32"\n"
		   "header_offset   =%"PRIu32"\n"
		   "max_crcs        =%"PRIu32"\n"
		   "max_msgs        =%"PRIu32"\n"
rswindell's avatar
rswindell committed
411
412
413
414
415
416
417
418
419
420
		   "max_age         =%u\n"
		   "attr            =%04Xh\n"
		   ,smb.status.last_msg
		   ,smb.status.total_msgs
		   ,smb.status.header_offset
		   ,smb.status.max_crcs
		   ,smb.status.max_msgs
		   ,smb.status.max_age
		   ,smb.status.attr
		   );
421
422
423
424
425
}

/****************************************************************************/
/* Configure message base header											*/
/****************************************************************************/
426
void config(void)
427
{
428
	char str[128];
429
430
	char max_msgs[128],max_crcs[128],max_age[128],header_offset[128],attr[128];
	int i;
431
	uint32_t last_msg = 0;
432

rswindell's avatar
rswindell committed
433
434
	i=smb_locksmbhdr(&smb);
	if(i) {
435
436
		fprintf(errfp,"\n%s!smb_locksmbhdr returned %d: %s\n"
			,beep,i,smb.last_error);
rswindell's avatar
rswindell committed
437
438
		return; 
	}
rswindell's avatar
rswindell committed
439
440
441
	i=smb_getstatus(&smb);
	smb_unlocksmbhdr(&smb);
	if(i) {
442
443
		fprintf(errfp,"\n%s!smb_getstatus returned %d: %s\n"
			,beep,i,smb.last_error);
rswindell's avatar
rswindell committed
444
445
		return; 
	}
446
447
448
	printf("Last Message  =%-6"PRIu32" New Value (CR=No Change): "
		,smb.status.last_msg);
	gets(str);
449
	if(IS_DIGIT(str[0]))
450
		last_msg = atol(str);
deuce's avatar
deuce committed
451
	printf("Header offset =%-5"PRIu32"  New value (CR=No Change): "
rswindell's avatar
rswindell committed
452
453
		,smb.status.header_offset);
	gets(header_offset);
deuce's avatar
deuce committed
454
	printf("Max msgs      =%-5"PRIu32"  New value (CR=No Change): "
rswindell's avatar
rswindell committed
455
456
		,smb.status.max_msgs);
	gets(max_msgs);
deuce's avatar
deuce committed
457
	printf("Max crcs      =%-5"PRIu32"  New value (CR=No Change): "
rswindell's avatar
rswindell committed
458
459
460
461
462
463
464
465
466
467
		,smb.status.max_crcs);
	gets(max_crcs);
	printf("Max age       =%-5u  New value (CR=No Change): "
		,smb.status.max_age);
	gets(max_age);
	printf("Attributes    =%-5u  New value (CR=No Change): "
		,smb.status.attr);
	gets(attr);
	i=smb_locksmbhdr(&smb);
	if(i) {
468
469
		fprintf(errfp,"\n%s!smb_locksmbhdr returned %d: %s\n"
			,beep,i,smb.last_error);
rswindell's avatar
rswindell committed
470
471
		return; 
	}
rswindell's avatar
rswindell committed
472
473
	i=smb_getstatus(&smb);
	if(i) {
474
475
		fprintf(errfp,"\n%s!smb_getstatus returned %d: %s\n"
			,beep,i,smb.last_error);
rswindell's avatar
rswindell committed
476
		smb_unlocksmbhdr(&smb);
rswindell's avatar
rswindell committed
477
478
		return; 
	}
479
480
	if(last_msg != 0)
		smb.status.last_msg = last_msg;
481
	if(IS_DIGIT(max_msgs[0]))
rswindell's avatar
rswindell committed
482
		smb.status.max_msgs=atol(max_msgs);
483
	if(IS_DIGIT(max_crcs[0]))
rswindell's avatar
rswindell committed
484
		smb.status.max_crcs=atol(max_crcs);
485
	if(IS_DIGIT(max_age[0]))
rswindell's avatar
rswindell committed
486
		smb.status.max_age=atoi(max_age);
487
	if(IS_DIGIT(header_offset[0]))
rswindell's avatar
rswindell committed
488
		smb.status.header_offset=atol(header_offset);
489
	if(IS_DIGIT(attr[0]))
rswindell's avatar
rswindell committed
490
491
		smb.status.attr=atoi(attr);
	i=smb_putstatus(&smb);
492
	smb_unlocksmbhdr(&smb);
rswindell's avatar
rswindell committed
493
	if(i)
494
495
		fprintf(errfp,"\n%s!smb_putstatus returned %d: %s\n"
			,beep,i,smb.last_error);
496
497
498
499
500
501
502
503
504
505
506
}

/****************************************************************************/
/* Lists messages' to, from, and subject                                    */
/****************************************************************************/
void listmsgs(ulong start, ulong count)
{
	int i;
	ulong l=0;
	smbmsg_t msg;

rswindell's avatar
rswindell committed
507
508
	if(!start)
		start=1;
509
510
	if(!count)
		count=~0;
rswindell's avatar
rswindell committed
511
512
513
514
515
516
	fseek(smb.sid_fp,(start-1L)*sizeof(idxrec_t),SEEK_SET);
	while(l<count) {
		if(!fread(&msg.idx,1,sizeof(idxrec_t),smb.sid_fp))
			break;
		i=smb_lockmsghdr(&smb,&msg);
		if(i) {
517
518
			fprintf(errfp,"\n%s!smb_lockmsghdr returned %d: %s\n"
				,beep,i,smb.last_error);
rswindell's avatar
rswindell committed
519
520
			break; 
		}
rswindell's avatar
rswindell committed
521
522
523
		i=smb_getmsghdr(&smb,&msg);
		smb_unlockmsghdr(&smb,&msg);
		if(i) {
524
525
			fprintf(errfp,"\n%s!smb_getmsghdr returned %d: %s\n"
				,beep,i,smb.last_error);
rswindell's avatar
rswindell committed
526
527
			break; 
		}
528
529
		printf("%4lu/#%-4"PRIu32" %-25.25s %-25.25s %s\n"
			,start + l, msg.hdr.number,msg.from,msg.to,msg.subj);
rswindell's avatar
rswindell committed
530
		smb_freemsgmem(&msg);
rswindell's avatar
rswindell committed
531
532
		l++; 
	}
533
534
535
536
537
538
}

/****************************************************************************/
/* Generates a 24 character ASCII string that represents the time_t pointer */
/* Used as a replacement for ctime()                                        */
/****************************************************************************/
539
char *my_timestr(time_t intime)
540
541
542
543
544
{
    static char str[256];
    char mer[3],hour;
    struct tm *gm;

545
	gm=localtime(&intime);
rswindell's avatar
rswindell committed
546
547
	if(gm==NULL) {
		strcpy(str,"Invalid Time");
rswindell's avatar
rswindell committed
548
549
		return(str); 
	}
rswindell's avatar
rswindell committed
550
551
552
553
554
	if(gm->tm_hour>=12) {
		if(gm->tm_hour==12)
			hour=12;
		else
			hour=gm->tm_hour-12;
rswindell's avatar
rswindell committed
555
556
		strcpy(mer,"pm"); 
	}
rswindell's avatar
rswindell committed
557
558
559
560
561
	else {
		if(gm->tm_hour==0)
			hour=12;
		else
			hour=gm->tm_hour;
rswindell's avatar
rswindell committed
562
563
		strcpy(mer,"am"); 
	}
rswindell's avatar
rswindell committed
564
565
566
567
	sprintf(str,"%s %s %02d %4d %02d:%02d %s"
		,wday[gm->tm_wday],mon[gm->tm_mon],gm->tm_mday,1900+gm->tm_year
		,hour,gm->tm_min,mer);
	return(str);
568
569
}

570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
/****************************************************************************/
/****************************************************************************/
void dumpindex(ulong start, ulong count)
{
	ulong l=0;
	idxrec_t idx;

	if(!start)
		start=1;
	if(!count)
		count=~0;
	fseek(smb.sid_fp,(start-1L)*sizeof(idxrec_t),SEEK_SET);
	while(l<count) {
		if(!fread(&idx,1,sizeof(idx),smb.sid_fp))
			break;
585
		printf("%10"PRIu32"  ", idx.number);
586
		if(idx.attr&MSG_VOTE && !(idx.attr&MSG_POLL))
587
			printf("V  %04hX  %-10"PRIu32, idx.votes,idx.remsg);
588
		else
589
			printf("%c  %04hX  %04hX  %04X"
590
				,(idx.attr&MSG_POLL_VOTE_MASK) == MSG_POLL_CLOSURE ? 'C' : (idx.attr&MSG_POLL ? 'P':' ')
591
				,idx.from, idx.to, idx.subj);
592
		printf("  %04X  %06X  %s\n", idx.attr, idx.offset, my_timestr(idx.time));
593
594
595
596
		l++; 
	}
}

597
598
599
/****************************************************************************/
/* Displays message header information										*/
/****************************************************************************/
600
void viewmsgs(ulong start, ulong count, BOOL verbose)
601
{
602
	int i,j;
603
604
605
	ulong l=0;
	smbmsg_t msg;

rswindell's avatar
rswindell committed
606
607
	if(!start)
		start=1;
608
609
	if(!count)
		count=~0;
rswindell's avatar
rswindell committed
610
611
612
613
614
615
	fseek(smb.sid_fp,(start-1L)*sizeof(idxrec_t),SEEK_SET);
	while(l<count) {
		if(!fread(&msg.idx,1,sizeof(idxrec_t),smb.sid_fp))
			break;
		i=smb_lockmsghdr(&smb,&msg);
		if(i) {
616
617
			fprintf(errfp,"\n%s!smb_lockmsghdr returned %d: %s\n"
				,beep,i,smb.last_error);
rswindell's avatar
rswindell committed
618
619
			break; 
		}
rswindell's avatar
rswindell committed
620
621
622
		i=smb_getmsghdr(&smb,&msg);
		smb_unlockmsghdr(&smb,&msg);
		if(i) {
623
624
			fprintf(errfp,"\n%s!smb_getmsghdr returned %d: %s\n"
				,beep,i,smb.last_error);
rswindell's avatar
rswindell committed
625
626
			break; 
		}
627

628
		printf("--------------------\n");
629
		printf("%-16.16s %ld\n"		,"index record",ftell(smb.sid_fp)/sizeof(idxrec_t));
630
		smb_dump_msghdr(stdout,&msg);
631
632
633
634
635
636
637
638
639
		if(verbose) {
			for(i=0; i<msg.total_hfields; i++) {
				printf("hdr field[%02u]        type %02Xh, length %u, data:"
					,i, msg.hfield[i].type, msg.hfield[i].length);
				for(j=0; j<msg.hfield[i].length; j++)
					printf(" %02X", *((uint8_t*)msg.hfield_dat[i]+j));
				printf("\n");
			}
		}
rswindell's avatar
rswindell committed
640
641
		printf("\n");
		smb_freemsgmem(&msg);
rswindell's avatar
rswindell committed
642
643
		l++; 
	}
644
645
}

646
647
648
649
/****************************************************************************/
/****************************************************************************/
void dump_hashes(void)
{
rswindell's avatar
rswindell committed
650
	char	tmp[128];
651
652
653
654
	int		retval;
	hash_t	hash;

	if((retval=smb_open_hash(&smb))!=SMB_SUCCESS) {
655
656
		fprintf(errfp,"\n%s!smb_open_hash returned %d: %s\n"
			,beep, retval, smb.last_error);
657
658
659
660
661
662
		return;
	}

	while(!smb_feof(smb.hash_fp)) {
		if(smb_fread(&smb,&hash,sizeof(hash),smb.hash_fp)!=sizeof(hash))
			break;
rswindell's avatar
rswindell committed
663
		printf("\n");
deuce's avatar
deuce committed
664
		printf("%-10s: %"PRIu32"\n","Number",	hash.number);
665
		printf("%-10s: %s\n",		"Source",	smb_hashsourcetype(hash.source));
deuce's avatar
deuce committed
666
		printf("%-10s: %"PRIu32"\n","Length",	hash.length);
667
		printf("%-10s: %s\n",		"Time",		my_timestr(hash.time));
668
		printf("%-10s: %02x\n",		"Flags",	hash.flags);
rswindell's avatar
rswindell committed
669
670
671
		if(hash.flags&SMB_HASH_CRC16)
			printf("%-10s: %04x\n",	"CRC-16",	hash.crc16);
		if(hash.flags&SMB_HASH_CRC32)
deuce's avatar
deuce committed
672
			printf("%-10s: %08"PRIx32"\n","CRC-32",	hash.crc32);
rswindell's avatar
rswindell committed
673
		if(hash.flags&SMB_HASH_MD5)
674
			printf("%-10s: %s\n",	"MD5",		MD5_hex((BYTE*)tmp,hash.md5));
675
	}
676
677

	smb_close_hash(&smb);
678
679
}

680
681
682
683
684
685
686
687
688
689
/****************************************************************************/
/* Maintain message base - deletes messages older than max age (in days)	*/
/* or messages that exceed maximum											*/
/****************************************************************************/
void maint(void)
{
	int i;
	ulong l,m,n,f,flagged=0;
	time_t now;
	smbmsg_t msg;
deuce's avatar
deuce committed
690
	idxrec_t *idx;
691
692
693
694
695

	printf("Maintaining %s\r\n",smb.file);
	now=time(NULL);
	i=smb_locksmbhdr(&smb);
	if(i) {
696
697
		fprintf(errfp,"\n%s!smb_locksmbhdr returned %d: %s\n"
			,beep,i,smb.last_error);
rswindell's avatar
rswindell committed
698
699
		return; 
	}
700
701
702
	i=smb_getstatus(&smb);
	if(i) {
		smb_unlocksmbhdr(&smb);
703
704
		fprintf(errfp,"\n%s!smb_getstatus returned %d: %s\n"
			,beep,i,smb.last_error);
rswindell's avatar
rswindell committed
705
706
		return; 
	}
707
	if((smb.status.max_msgs || smb.status.max_crcs) && smb_open_hash(&smb) == SMB_SUCCESS)
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
	{
		ulong max_hashes=0;

		printf("Maintaining %s hash file\r\n", smb.file);

		if((smb.status.attr&(SMB_EMAIL|SMB_NOHASH)) == 0) {
			max_hashes = smb.status.max_msgs;
			if(smb.status.max_crcs > max_hashes)
				max_hashes = smb.status.max_crcs;
		}
		if(!max_hashes) {
			CHSIZE_FP(smb.hash_fp,0);
		} else if(filelength(fileno(smb.hash_fp)) > (long)(max_hashes * SMB_HASH_SOURCE_TYPES * sizeof(hash_t))) {
			if(fseek(smb.hash_fp, -((long)(max_hashes * SMB_HASH_SOURCE_TYPES * sizeof(hash_t))), SEEK_END) == 0) {
				hash_t*	hashes = malloc(max_hashes * SMB_HASH_SOURCE_TYPES * sizeof(hash_t));
				if(hashes != NULL) {
					if(fread(hashes, sizeof(hash_t), max_hashes * SMB_HASH_SOURCE_TYPES, smb.hash_fp) == max_hashes * SMB_HASH_SOURCE_TYPES) {
						rewind(smb.hash_fp);
						fwrite(hashes, sizeof(hash_t), max_hashes * SMB_HASH_SOURCE_TYPES, smb.hash_fp);
727
728
						fflush(smb.hash_fp);
						CHSIZE_FP(smb.hash_fp, sizeof(hash_t) * max_hashes * SMB_HASH_SOURCE_TYPES);
729
730
731
732
733
734
735
					}
					free(hashes);
				}
			}
		}
		smb_close_hash(&smb);
	}
736
737
738
	if(!smb.status.total_msgs) {
		smb_unlocksmbhdr(&smb);
		printf("Empty\n");
rswindell's avatar
rswindell committed
739
740
		return; 
	}
741
	printf("Loading index...\n");
deuce's avatar
deuce committed
742
	if((idx=(idxrec_t *)malloc(sizeof(idxrec_t)*smb.status.total_msgs))
743
744
		==NULL) {
		smb_unlocksmbhdr(&smb);
deuce's avatar
deuce committed
745
		fprintf(errfp,"\n%s!Error allocating %" XP_PRIsize_t "u bytes of memory\n"
746
			,beep,sizeof(idxrec_t)*smb.status.total_msgs);
rswindell's avatar
rswindell committed
747
748
		return; 
	}
749
	fseek(smb.sid_fp,0L,SEEK_SET);
750
	l = fread(idx, sizeof(idxrec_t), smb.status.total_msgs, smb.sid_fp);
751

752
	printf("\nDone.\n\n");
753
754
	printf("Scanning for pre-flagged messages...\n");
	for(m=0;m<l;m++) {
755
//		printf("\r%2lu%%",m ? (long)(100.0/((float)l/m)) : 0);
756
		if(idx[m].attr&MSG_DELETE)
rswindell's avatar
rswindell committed
757
758
			flagged++; 
	}
759
760
761
762
763
764
	printf("\r100%% (%lu pre-flagged for deletion)\n",flagged);

	if(smb.status.max_age) {
		printf("Scanning for messages more than %u days old...\n"
			,smb.status.max_age);
		for(m=f=0;m<l;m++) {
765
//			printf("\r%2lu%%",m ? (long)(100.0/((float)l/m)) : 0);
766
767
768
769
770
771
			if(idx[m].attr&(MSG_PERMANENT|MSG_DELETE))
				continue;
			if((ulong)now>idx[m].time && (now-idx[m].time)/(24L*60L*60L)
				>smb.status.max_age) {
				f++;
				flagged++;
rswindell's avatar
rswindell committed
772
773
774
				idx[m].attr|=MSG_DELETE; 
			} 
		}  /* mark for deletion */
775
		printf("\r100%% (%lu flagged for deletion due to age)\n",f); 
rswindell's avatar
rswindell committed
776
	}
777
778

	printf("Scanning for read messages to be killed...\n");
779
	uint32_t total_msgs = 0;
780
	for(m=f=0;m<l;m++) {
781
782
		enum smb_msg_type type = smb_msg_type(idx[m].attr);
		if(type == SMB_MSG_TYPE_NORMAL || type == SMB_MSG_TYPE_POLL)
783
			total_msgs++;
784
//		printf("\r%2lu%%",m ? (long)(100.0/((float)l/m)) : 0);
785
786
787
788
789
		if(idx[m].attr&(MSG_PERMANENT|MSG_DELETE))
			continue;
		if((idx[m].attr&(MSG_READ|MSG_KILLREAD))==(MSG_READ|MSG_KILLREAD)) {
			f++;
			flagged++;
rswindell's avatar
rswindell committed
790
			idx[m].attr|=MSG_DELETE; 
791
		}
rswindell's avatar
rswindell committed
792
	}
793
	printf("\r100%% (%lu flagged for deletion due to read status)\n",f);
794

795
	if(smb.status.max_msgs && total_msgs - flagged > smb.status.max_msgs) {
796
		printf("Flagging excess messages for deletion...\n");
797
		for(m=n=0,f=flagged; total_msgs - flagged > smb.status.max_msgs && m<l; m++) {
798
799
			if(idx[m].attr&(MSG_PERMANENT|MSG_DELETE))
				continue;
800
			printf("%lu of %lu\r",++n,(total_msgs - f)-smb.status.max_msgs);
801
			flagged++;
rswindell's avatar
rswindell committed
802
803
804
805
			idx[m].attr|=MSG_DELETE; 
		}			/* mark for deletion */
		printf("\nDone.\n\n"); 
	}
806
807

	if(!flagged) {				/* No messages to delete */
deuce's avatar
deuce committed
808
		free(idx);
809
		smb_unlocksmbhdr(&smb);
rswindell's avatar
rswindell committed
810
811
		return; 
	}
812
813
814
815
816
817
818
819

	if(!(mode&NOANALYSIS)) {

		printf("Freeing allocated header and data blocks for deleted messages...\n");
		if(!(smb.status.attr&SMB_HYPERALLOC)) {
			i=smb_open_da(&smb);
			if(i) {
				smb_unlocksmbhdr(&smb);
820
821
				fprintf(errfp,"\n%s!smb_open_da returned %d: %s\n"
					,beep,i,smb.last_error);
822
				bail(1); 
rswindell's avatar
rswindell committed
823
			}
824
825
826
			i=smb_open_ha(&smb);
			if(i) {
				smb_unlocksmbhdr(&smb);
827
828
				fprintf(errfp,"\n%s!smb_open_ha returned %d: %s\n"
					,beep,i,smb.last_error);
829
				bail(1); 
rswindell's avatar
rswindell committed
830
831
			} 
		}
832
833
834
835
836
837
838

		for(m=n=0;m<l;m++) {
			if(idx[m].attr&MSG_DELETE) {
				printf("%lu of %lu\r",++n,flagged);
				msg.idx=idx[m];
				msg.hdr.number=msg.idx.number;
				if((i=smb_getmsgidx(&smb,&msg))!=0) {
839
840
					fprintf(errfp,"\n%s!smb_getmsgidx returned %d: %s\n"
						,beep,i,smb.last_error);
rswindell's avatar
rswindell committed
841
842
					continue; 
				}
843
844
				i=smb_lockmsghdr(&smb,&msg);
				if(i) {
845
846
					fprintf(errfp,"\n%s!smb_lockmsghdr returned %d: %s\n"
						,beep,i,smb.last_error);
rswindell's avatar
rswindell committed
847
848
					break; 
				}
849
850
				if((i=smb_getmsghdr(&smb,&msg))!=0) {
					smb_unlockmsghdr(&smb,&msg);
851
852
					fprintf(errfp,"\n%s!smb_getmsghdr returned %d: %s\n"
						,beep,i,smb.last_error);
rswindell's avatar
rswindell committed
853
854
					break; 
				}
855
856
857
858
				msg.hdr.attr|=MSG_DELETE;			/* mark header as deleted */
				if((i=smb_putmsg(&smb,&msg))!=0) {
					smb_freemsgmem(&msg);
					smb_unlockmsghdr(&smb,&msg);
859
860
					fprintf(errfp,"\n%s!smb_putmsg returned %d: %s\n"
						,beep,i,smb.last_error);
rswindell's avatar
rswindell committed
861
862
					break; 
				}
863
864
865
				smb_unlockmsghdr(&smb,&msg);
				if((i=smb_freemsg(&smb,&msg))!=0) {
					smb_freemsgmem(&msg);
866
867
					fprintf(errfp,"\n%s!smb_freemsg returned %d: %s\n"
						,beep,i,smb.last_error);
rswindell's avatar
rswindell committed
868
869
870
871
872
					break; 
				}
				smb_freemsgmem(&msg); 
			} 
		}
873
874
		if(!(smb.status.attr&SMB_HYPERALLOC)) {
			smb_close_ha(&smb);
rswindell's avatar
rswindell committed
875
876
877
878
			smb_close_da(&smb); 
		}
		printf("\nDone.\n\n"); 
	}
879
880
881
882
883
884

	printf("Re-writing index...\n");
	rewind(smb.sid_fp);
	for(m=n=0;m<l;m++) {
		if(idx[m].attr&MSG_DELETE)
			continue;
885
886
		n++;
		printf("%lu of %lu\r", n, l-flagged);
rswindell's avatar
rswindell committed
887
888
		fwrite(&idx[m],sizeof(idxrec_t),1,smb.sid_fp); 
	}
889
	fflush(smb.sid_fp);
890
891
	CHSIZE_FP(smb.sid_fp, n * sizeof(idxrec_t));
	printf("\nDone.\n\n");
892

deuce's avatar
deuce committed
893
	free(idx);
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
	smb.status.total_msgs-=flagged;
	smb_putstatus(&smb);
	smb_unlocksmbhdr(&smb);
}


typedef struct {
	ulong old,new;
	} datoffset_t;

/****************************************************************************/
/* Removes all unused blocks from SDT and SHD files 						*/
/****************************************************************************/
void packmsgs(ulong packable)
{
909
	uchar	buf[SDT_BLOCK_LEN],ch;
910
	char	fname[MAX_PATH+1],tmpfname[MAX_PATH+1];
911
	int i,size;
912
	ulong l,m,n,datoffsets=0,length,total;
913
	FILE *tmp_sdt,*tmp_shd,*tmp_sid;
914
	BOOL		error=FALSE;
915
916
917
	smbhdr_t	hdr;
	smbmsg_t	msg;
	datoffset_t *datoffset=NULL;
918
	time_t		now;
919

rswindell's avatar
rswindell committed
920
921
922
	now=time(NULL);
	printf("Packing %s\n",smb.file);
	i=smb_locksmbhdr(&smb);
923
	if(i) {
924
925
		fprintf(errfp,"\n%s!smb_locksmbhdr returned %d: %s\n"
			,beep,i,smb.last_error);
rswindell's avatar
rswindell committed
926
927
		return; 
	}
rswindell's avatar
rswindell committed
928
	i=smb_getstatus(&smb);
929
930
	if(i) {
		smb_unlocksmbhdr(&smb);
931
932
		fprintf(errfp,"\n%s!smb_getstatus returned %d: %s\n"
			,beep,i,smb.last_error);
rswindell's avatar
rswindell committed
933
934
		return; 
	}
rswindell's avatar
rswindell committed
935

936
	if(!(smb.status.attr&SMB_HYPERALLOC)) {
rswindell's avatar
rswindell committed
937
938
939
		i=smb_open_ha(&smb);
		if(i) {
			smb_unlocksmbhdr(&smb);
940
941
			fprintf(errfp,"\n%s!smb_open_ha returned %d: %s\n"
				,beep,i,smb.last_error);
rswindell's avatar
rswindell committed
942
943
			return; 
		}
rswindell's avatar
rswindell committed
944
945
946
947
		i=smb_open_da(&smb);
		if(i) {
			smb_unlocksmbhdr(&smb);
			smb_close_ha(&smb);
948
949
			fprintf(errfp,"\n%s!smb_open_da returned %d: %s\n"
				,beep,i,smb.last_error);
rswindell's avatar
rswindell committed
950
951
952
			return; 
		} 
	}
953

rswindell's avatar
rswindell committed
954
955
956
	if(!smb.status.total_msgs) {
		printf("Empty\n");
		rewind(smb.shd_fp);
957
		CHSIZE_FP(smb.shd_fp,smb.status.header_offset);
rswindell's avatar
rswindell committed
958
		rewind(smb.sdt_fp);
959
		CHSIZE_FP(smb.sdt_fp,0);
rswindell's avatar
rswindell committed
960
		rewind(smb.sid_fp);
961
		CHSIZE_FP(smb.sid_fp,0);
rswindell's avatar
rswindell committed
962
963
		if(!(smb.status.attr&SMB_HYPERALLOC)) {
			rewind(smb.sha_fp);
964
			CHSIZE_FP(smb.sha_fp,0);
rswindell's avatar
rswindell committed
965
			rewind(smb.sda_fp);
966
			CHSIZE_FP(smb.sda_fp,0);
rswindell's avatar
rswindell committed
967
			smb_close_ha(&smb);
rswindell's avatar
rswindell committed
968
969
			smb_close_da(&smb); 
		}
rswindell's avatar
rswindell committed
970
		smb_unlocksmbhdr(&smb);
rswindell's avatar
rswindell committed
971
972
		return; 
	}
973
974


rswindell's avatar
rswindell committed
975
976
	if(!(smb.status.attr&SMB_HYPERALLOC) && !(mode&NOANALYSIS)) {
		printf("Analyzing data blocks...\n");
977

rswindell's avatar
rswindell committed
978
		length=filelength(fileno(smb.sda_fp));