smbutil.c 43.8 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
/* smbutil.c */

/* Synchronet message base (SMB) utility */

/* $Id$ */

/****************************************************************************
 * @format.tab-size 4		(Plain Text/Source Code File Header)			*
 * @format.use-tabs true	(see http://www.synchro.net/ptsc_hdr.html)		*
 *																			*
11
 * Copyright 2014 Rob Swindell - http://www.synchro.net/copyright.html		*
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
37
 *																			*
 * 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.	*
 ****************************************************************************/

38
#define SMBUTIL_VER "2.33"
39
40
char	revision[16];
char	compiler[32];
41

42
43
44
45
46
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"};


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

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

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

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

68
#include "genwrap.h"	/* stricmp */
rswindell's avatar
rswindell committed
69
#include "dirwrap.h"	/* fexist */
rswindell's avatar
rswindell committed
70
#include "conwrap.h"	/* getch */
71
#include "filewrap.h"
72
#include "smblib.h"
73
#include "crc16.h"
74
#include "crc32.h"
75
#include "gen_defs.h"	/* MAX_PATH */
76
77
78
79
80

#ifdef __WATCOMC__
	#include <dos.h>
#endif

81
/* gets is dangerous */
82
#define gets(str)  fgets((str), sizeof(str), stdin)
83

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

86
87
88
89
/********************/
/* Global variables */
/********************/

90
91
92
93
94
95
96
97
98
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;
99
char*		beep="";
100
101
102
103
104
105

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

char *usage=
106
"usage: smbutil [-opts] cmd <filespec.shd>\n"
107
108
109
110
"\n"
"cmd:\n"
"       l[n] = list msgs starting at number n\n"
"       r[n] = read msgs starting at number n\n"
111
"       x[n] = dump msg index at number n\n"
112
"       v[n] = view msg headers starting at number n\n"
113
114
115
"       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
116
"       h    = dump hash file\n"
117
118
"       s    = display msg base status\n"
"       c    = change msg base status\n"
119
"       d    = delete all msgs\n"
120
121
122
"       m    = maintain msg base - delete old msgs and msgs over max\n"
"       p[k] = pack msg base (k specifies minimum packable Kbytes)\n"
"opts:\n"
123
"       c[m] = create message base if it doesn't exist (m=max msgs)\n"
124
"       a    = always pack msg base (disable compression analysis)\n"
125
"       i    = ignore dupes (do not store CRCs or search for duplicate hashes)\n"
126
"       d    = use default values (no prompt) for to, from, and subject\n"
127
"       l    = LZH-compress message text\n"
128
129
130
"       o    = print errors on stdout (instead of stderr)\n"
"       p    = wait for keypress (pause) on exit\n"
"       !    = wait for keypress (pause) on error\n"
131
"       b    = beep on error\n"
132
133
134
135
136
137
"       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"
138
"       z[n] = set time zone (n=min +/- from UT or 'EST','EDT','CST',etc)\n"
139
"       #    = set number of messages to view/list (e.g. -1)\n"
140
141
;

142
143
144
145
146
147
148
149
150
151
152
153
154
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);
}

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

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

171
172
173
/****************************************************************************/
/* Adds a new message to the message base									*/
/****************************************************************************/
174
175
void postmsg(char type, char* to, char* to_number, char* to_address, 
			 char* from, char* from_number, char* subject, FILE* fp)
176
{
177
178
	char		str[128];
	char		buf[1024];
179
180
	uchar*		msgtxt=NULL;
	uchar*		newtxt;
181
182
183
	long		msgtxtlen;
	ushort		net;
	int 		i;
184
	ushort		agent=AGENT_SMBUTIL;
185
	smbmsg_t	msg;
186
	long		dupechk_hashes=SMB_HASH_SOURCE_DUPE;
187

188
189
190
191
192
193
	/* Read message text from stream (file or stdin) */
	msgtxtlen=0;
	while(!feof(fp)) {
		i=fread(buf,1,sizeof(buf),fp);
		if(i<1)
			break;
194
		if((msgtxt=(uchar*)realloc(msgtxt,msgtxtlen+i+1))==NULL) {
195
			fprintf(errfp,"\n%s!realloc(%ld) failure\n",beep,msgtxtlen+i+1);
196
			bail(1);
197
		}
198
		memcpy(msgtxt+msgtxtlen,buf,i);
199
200
201
		msgtxtlen+=i;
	}

202
	if(msgtxt!=NULL) {
203
204
205

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

206
		if((newtxt=(uchar*)malloc((msgtxtlen*2)+1))==NULL) {
207
			fprintf(errfp,"\n%s!malloc(%ld) failure\n",beep,(msgtxtlen*2)+1);
208
			bail(1);
209
		}
rswindell's avatar
rswindell committed
210

211
212
213
214
215
		/* Expand LFs to CRLFs */
		msgtxtlen=lf_expand(msgtxt, newtxt);
		free(msgtxt);
		msgtxt=newtxt;
	}
rswindell's avatar
rswindell committed
216
217

	memset(&msg,0,sizeof(smbmsg_t));
218
	msg.hdr.when_written.time=(uint32_t)time(NULL);
rswindell's avatar
rswindell committed
219
220
221
	msg.hdr.when_written.zone=tzone;
	msg.hdr.when_imported=msg.hdr.when_written;

222
223
	if((to==NULL || stricmp(to,"All")==0) && to_address!=NULL)
		to=to_address;
224
225
	if(to==NULL) {
		printf("To User Name: ");
226
		fgets(str,sizeof(str),stdin); 
227
	} else
228
		SAFECOPY(str,to);
rswindell's avatar
rswindell committed
229
	truncsp(str);
230
231

	if((i=smb_hfield_str(&msg,RECIPIENT,str))!=SMB_SUCCESS) {
232
233
		fprintf(errfp,"\n%s!smb_hfield_str(0x%02X) returned %d: %s\n"
			,beep,RECIPIENT,i,smb.last_error);
234
		bail(1); 
rswindell's avatar
rswindell committed
235
	}
rswindell's avatar
rswindell committed
236
237
238
	if(type=='E' || type=='N')
		smb.status.attr|=SMB_EMAIL;
	if(smb.status.attr&SMB_EMAIL) {
239
240
241
242
243
244
245
246
247
248
249
250
		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); 
			}
251
252
		}
	}
rswindell's avatar
rswindell committed
253

254
	if(smb.status.attr&SMB_EMAIL && (type=='N' || to_address!=NULL)) {
255
		if(to_address==NULL) {
256
			printf("To Address (e.g. user@host): ");
257
258
			gets(str);
		} else
259
			SAFECOPY(str,to_address);
rswindell's avatar
rswindell committed
260
261
		truncsp(str);
		if(*str) {
262
			net=smb_netaddr_type(str);
263
			if((i=smb_hfield(&msg,RECIPIENTNETTYPE,sizeof(net),&net))!=SMB_SUCCESS) {
264
265
				fprintf(errfp,"\n%s!smb_hfield(0x%02X) returned %d: %s\n"
					,beep,RECIPIENTNETTYPE,i,smb.last_error);
266
				bail(1); 
rswindell's avatar
rswindell committed
267
			}
268
			if((i=smb_hfield_str(&msg,RECIPIENTNETADDR,str))!=SMB_SUCCESS) {
269
270
				fprintf(errfp,"\n%s!smb_hfield_str(0x%02X) returned %d: %s\n"
					,beep,RECIPIENTNETADDR,i,smb.last_error);
271
				bail(1); 
272
273
274
			} 
		} 
	}
rswindell's avatar
rswindell committed
275

276
277
278
279
	if(from==NULL) {
		printf("From User Name: ");
		gets(str);
	} else
280
		SAFECOPY(str,from);
rswindell's avatar
rswindell committed
281
	truncsp(str);
282
	if((i=smb_hfield_str(&msg,SENDER,str))!=SMB_SUCCESS) {
283
284
		fprintf(errfp,"\n%s!smb_hfield_str(0x%02X) returned %d: %s\n"
			,beep,SENDER,i,smb.last_error);
285
		bail(1); 
rswindell's avatar
rswindell committed
286
	}
287
	if((smb.status.attr&SMB_EMAIL) || from_number!=NULL) {
288
289
290
291
		if(from_number==NULL) {
			printf("From User Number: ");
			gets(str);
		} else
292
			SAFECOPY(str,from_number);
rswindell's avatar
rswindell committed
293
		truncsp(str);
294
		if((i=smb_hfield_str(&msg,SENDEREXT,str))!=SMB_SUCCESS) {
295
296
			fprintf(errfp,"\n%s!smb_hfield_str(0x%02X) returned %d: %s\n"
				,beep,SENDEREXT,i,smb.last_error);
297
			bail(1); 
298
299
		}
	}
300
	if((i=smb_hfield(&msg, SENDERAGENT, sizeof(agent), &agent))!=SMB_SUCCESS) {
301
302
		fprintf(errfp,"\n%s!smb_hfield(0x%02X) returned %d: %s\n"
			,beep,SENDERAGENT,i,smb.last_error);
303
		bail(1);
304
	}
rswindell's avatar
rswindell committed
305

306
307
308
309
	if(subject==NULL) {
		printf("Subject: ");
		gets(str);
	} else
310
		SAFECOPY(str,subject);
rswindell's avatar
rswindell committed
311
	truncsp(str);
312
	if((i=smb_hfield_str(&msg,SUBJECT,str))!=SMB_SUCCESS) {
313
314
		fprintf(errfp,"\n%s!smb_hfield_str(0x%02X) returned %d: %s\n"
			,beep,SUBJECT,i,smb.last_error);
315
		bail(1); 
316
	}
rswindell's avatar
rswindell committed
317

318
	safe_snprintf(str,sizeof(str),"SMBUTIL %s-%s r%s %s %s"
319
320
321
322
323
324
		,SMBUTIL_VER
		,PLATFORM_DESC
		,revision
		,__DATE__
		,compiler
		);
325
	if((i=smb_hfield_str(&msg,FIDOPID,str))!=SMB_SUCCESS) {
326
327
		fprintf(errfp,"\n%s!smb_hfield_str(0x%02X) returned %d: %s\n"
			,beep,FIDOPID,i,smb.last_error);
328
		bail(1); 
329
	}
rswindell's avatar
rswindell committed
330

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

334
	if((i=smb_addmsg(&smb,&msg,smb.status.attr&SMB_HYPERALLOC
335
		,dupechk_hashes,xlat,msgtxt,NULL))!=SMB_SUCCESS) {
336
337
		fprintf(errfp,"\n%s!smb_addmsg returned %d: %s\n"
			,beep,i,smb.last_error);
338
		bail(1); 
rswindell's avatar
rswindell committed
339
	}
rswindell's avatar
rswindell committed
340
	smb_freemsgmem(&msg);
341

342
	FREE_AND_NULL(msgtxt);
343
344
345
346
347
}

/****************************************************************************/
/* Shows the message base header											*/
/****************************************************************************/
348
void showstatus(void)
349
350
351
{
	int i;

rswindell's avatar
rswindell committed
352
353
	i=smb_locksmbhdr(&smb);
	if(i) {
354
355
		fprintf(errfp,"\n%s!smb_locksmbhdr returned %d: %s\n"
			,beep,i,smb.last_error);
rswindell's avatar
rswindell committed
356
357
		return; 
	}
rswindell's avatar
rswindell committed
358
359
360
	i=smb_getstatus(&smb);
	smb_unlocksmbhdr(&smb);
	if(i) {
361
362
		fprintf(errfp,"\n%s!smb_getstatus returned %d: %s\n"
			,beep,i,smb.last_error);
rswindell's avatar
rswindell committed
363
364
		return; 
	}
deuce's avatar
deuce committed
365
366
367
368
369
	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
370
371
372
373
374
375
376
377
378
379
		   "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
		   );
380
381
382
383
384
}

/****************************************************************************/
/* Configure message base header											*/
/****************************************************************************/
385
void config(void)
386
387
388
389
{
	char max_msgs[128],max_crcs[128],max_age[128],header_offset[128],attr[128];
	int i;

rswindell's avatar
rswindell committed
390
391
	i=smb_locksmbhdr(&smb);
	if(i) {
392
393
		fprintf(errfp,"\n%s!smb_locksmbhdr returned %d: %s\n"
			,beep,i,smb.last_error);
rswindell's avatar
rswindell committed
394
395
		return; 
	}
rswindell's avatar
rswindell committed
396
397
398
	i=smb_getstatus(&smb);
	smb_unlocksmbhdr(&smb);
	if(i) {
399
400
		fprintf(errfp,"\n%s!smb_getstatus returned %d: %s\n"
			,beep,i,smb.last_error);
rswindell's avatar
rswindell committed
401
402
		return; 
	}
deuce's avatar
deuce committed
403
	printf("Header offset =%-5"PRIu32"  New value (CR=No Change): "
rswindell's avatar
rswindell committed
404
405
		,smb.status.header_offset);
	gets(header_offset);
deuce's avatar
deuce committed
406
	printf("Max msgs      =%-5"PRIu32"  New value (CR=No Change): "
rswindell's avatar
rswindell committed
407
408
		,smb.status.max_msgs);
	gets(max_msgs);
deuce's avatar
deuce committed
409
	printf("Max crcs      =%-5"PRIu32"  New value (CR=No Change): "
rswindell's avatar
rswindell committed
410
411
412
413
414
415
416
417
418
419
		,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) {
420
421
		fprintf(errfp,"\n%s!smb_locksmbhdr returned %d: %s\n"
			,beep,i,smb.last_error);
rswindell's avatar
rswindell committed
422
423
		return; 
	}
rswindell's avatar
rswindell committed
424
425
	i=smb_getstatus(&smb);
	if(i) {
426
427
		fprintf(errfp,"\n%s!smb_getstatus returned %d: %s\n"
			,beep,i,smb.last_error);
rswindell's avatar
rswindell committed
428
		smb_unlocksmbhdr(&smb);
rswindell's avatar
rswindell committed
429
430
		return; 
	}
rswindell's avatar
rswindell committed
431
432
433
434
435
436
437
438
439
440
441
	if(isdigit(max_msgs[0]))
		smb.status.max_msgs=atol(max_msgs);
	if(isdigit(max_crcs[0]))
		smb.status.max_crcs=atol(max_crcs);
	if(isdigit(max_age[0]))
		smb.status.max_age=atoi(max_age);
	if(isdigit(header_offset[0]))
		smb.status.header_offset=atol(header_offset);
	if(isdigit(attr[0]))
		smb.status.attr=atoi(attr);
	i=smb_putstatus(&smb);
442
	smb_unlocksmbhdr(&smb);
rswindell's avatar
rswindell committed
443
	if(i)
444
445
		fprintf(errfp,"\n%s!smb_putstatus returned %d: %s\n"
			,beep,i,smb.last_error);
446
447
448
449
450
451
452
453
454
455
456
}

/****************************************************************************/
/* 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
457
458
	if(!start)
		start=1;
459
460
	if(!count)
		count=~0;
rswindell's avatar
rswindell committed
461
462
463
464
465
466
	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) {
467
468
			fprintf(errfp,"\n%s!smb_lockmsghdr returned %d: %s\n"
				,beep,i,smb.last_error);
rswindell's avatar
rswindell committed
469
470
			break; 
		}
rswindell's avatar
rswindell committed
471
472
473
		i=smb_getmsghdr(&smb,&msg);
		smb_unlockmsghdr(&smb,&msg);
		if(i) {
474
475
			fprintf(errfp,"\n%s!smb_getmsghdr returned %d: %s\n"
				,beep,i,smb.last_error);
rswindell's avatar
rswindell committed
476
477
			break; 
		}
deuce's avatar
deuce committed
478
		printf("%4"PRIu32" %-25.25s %-25.25s %s\n"
rswindell's avatar
rswindell committed
479
480
			,msg.hdr.number,msg.from,msg.to,msg.subj);
		smb_freemsgmem(&msg);
rswindell's avatar
rswindell committed
481
482
		l++; 
	}
483
484
485
486
487
488
}

/****************************************************************************/
/* Generates a 24 character ASCII string that represents the time_t pointer */
/* Used as a replacement for ctime()                                        */
/****************************************************************************/
489
char *my_timestr(time_t intime)
490
491
492
493
494
{
    static char str[256];
    char mer[3],hour;
    struct tm *gm;

495
	gm=localtime(&intime);
rswindell's avatar
rswindell committed
496
497
	if(gm==NULL) {
		strcpy(str,"Invalid Time");
rswindell's avatar
rswindell committed
498
499
		return(str); 
	}
rswindell's avatar
rswindell committed
500
501
502
503
504
	if(gm->tm_hour>=12) {
		if(gm->tm_hour==12)
			hour=12;
		else
			hour=gm->tm_hour-12;
rswindell's avatar
rswindell committed
505
506
		strcpy(mer,"pm"); 
	}
rswindell's avatar
rswindell committed
507
508
509
510
511
	else {
		if(gm->tm_hour==0)
			hour=12;
		else
			hour=gm->tm_hour;
rswindell's avatar
rswindell committed
512
513
		strcpy(mer,"am"); 
	}
rswindell's avatar
rswindell committed
514
515
516
517
	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);
518
519
}

520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
/****************************************************************************/
/****************************************************************************/
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;

deuce's avatar
deuce committed
536
		printf("%4"PRIu32" %04hX %04hX %04Xh %04Xh %06X %s\n"
537
			,idx.number,idx.from,idx.to,idx.subj,idx.attr
538
			,idx.offset,my_timestr(idx.time));
539
540
541
542
		l++; 
	}
}

543
544
545
546
547
548
549
550
551
/****************************************************************************/
/* Displays message header information										*/
/****************************************************************************/
void viewmsgs(ulong start, ulong count)
{
	int i;
	ulong l=0;
	smbmsg_t msg;

rswindell's avatar
rswindell committed
552
553
	if(!start)
		start=1;
554
555
	if(!count)
		count=~0;
rswindell's avatar
rswindell committed
556
557
558
559
560
561
	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) {
562
563
			fprintf(errfp,"\n%s!smb_lockmsghdr returned %d: %s\n"
				,beep,i,smb.last_error);
rswindell's avatar
rswindell committed
564
565
			break; 
		}
rswindell's avatar
rswindell committed
566
567
568
		i=smb_getmsghdr(&smb,&msg);
		smb_unlockmsghdr(&smb,&msg);
		if(i) {
569
570
			fprintf(errfp,"\n%s!smb_getmsghdr returned %d: %s\n"
				,beep,i,smb.last_error);
rswindell's avatar
rswindell committed
571
572
			break; 
		}
573

574
575
576
		printf("--------------------\n");
		printf("%-20.20s %ld\n"		,"index record",ftell(smb.sid_fp)/sizeof(idxrec_t));
		smb_dump_msghdr(stdout,&msg);
rswindell's avatar
rswindell committed
577
578
		printf("\n");
		smb_freemsgmem(&msg);
rswindell's avatar
rswindell committed
579
580
		l++; 
	}
581
582
}

583
584
585
586
/****************************************************************************/
/****************************************************************************/
void dump_hashes(void)
{
rswindell's avatar
rswindell committed
587
	char	tmp[128];
588
589
590
591
	int		retval;
	hash_t	hash;

	if((retval=smb_open_hash(&smb))!=SMB_SUCCESS) {
592
593
		fprintf(errfp,"\n%s!smb_open_hash returned %d: %s\n"
			,beep, retval, smb.last_error);
594
595
596
597
598
599
		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
600
		printf("\n");
deuce's avatar
deuce committed
601
		printf("%-10s: %"PRIu32"\n","Number",	hash.number);
602
		printf("%-10s: %s\n",		"Source",	smb_hashsourcetype(hash.source));
deuce's avatar
deuce committed
603
		printf("%-10s: %"PRIu32"\n","Length",	hash.length);
604
		printf("%-10s: %s\n",		"Time",		my_timestr(hash.time));
605
		printf("%-10s: %02x\n",		"Flags",	hash.flags);
rswindell's avatar
rswindell committed
606
607
608
		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
609
			printf("%-10s: %08"PRIx32"\n","CRC-32",	hash.crc32);
rswindell's avatar
rswindell committed
610
		if(hash.flags&SMB_HASH_MD5)
611
			printf("%-10s: %s\n",	"MD5",		MD5_hex((BYTE*)tmp,hash.md5));
612
	}
613
614

	smb_close_hash(&smb);
615
616
}

617
618
619
620
621
622
623
624
625
626
/****************************************************************************/
/* 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
627
	idxrec_t *idx;
628
629
630
631
632

	printf("Maintaining %s\r\n",smb.file);
	now=time(NULL);
	i=smb_locksmbhdr(&smb);
	if(i) {
633
634
		fprintf(errfp,"\n%s!smb_locksmbhdr returned %d: %s\n"
			,beep,i,smb.last_error);
rswindell's avatar
rswindell committed
635
636
		return; 
	}
637
638
639
	i=smb_getstatus(&smb);
	if(i) {
		smb_unlocksmbhdr(&smb);
640
641
		fprintf(errfp,"\n%s!smb_getstatus returned %d: %s\n"
			,beep,i,smb.last_error);
rswindell's avatar
rswindell committed
642
643
		return; 
	}
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
	if(smb_open_hash(&smb) == SMB_SUCCESS)
	{
		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) {
						CHSIZE_FP(smb.hash_fp,0);
						rewind(smb.hash_fp);
						fwrite(hashes, sizeof(hash_t), max_hashes * SMB_HASH_SOURCE_TYPES, smb.hash_fp);
					}
					free(hashes);
				}
			}
		}
		smb_close_hash(&smb);
	}
672
673
674
	if(!smb.status.total_msgs) {
		smb_unlocksmbhdr(&smb);
		printf("Empty\n");
rswindell's avatar
rswindell committed
675
676
		return; 
	}
677
	printf("Loading index...\n");
deuce's avatar
deuce committed
678
	if((idx=(idxrec_t *)malloc(sizeof(idxrec_t)*smb.status.total_msgs))
679
680
		==NULL) {
		smb_unlocksmbhdr(&smb);
deuce's avatar
deuce committed
681
		fprintf(errfp,"\n%s!Error allocating %u bytes of memory\n"
682
			,beep,sizeof(idxrec_t)*smb.status.total_msgs);
rswindell's avatar
rswindell committed
683
684
		return; 
	}
685
686
	fseek(smb.sid_fp,0L,SEEK_SET);
	for(l=0;l<smb.status.total_msgs;l++) {
deuce's avatar
deuce committed
687
		printf("%lu of %"PRIu32"\r"
688
689
			,l+1,smb.status.total_msgs);
		if(!fread(&idx[l],1,sizeof(idxrec_t),smb.sid_fp))
rswindell's avatar
rswindell committed
690
691
			break; 
	}
692
693
694
695
	printf("\nDone.\n\n");

	printf("Scanning for pre-flagged messages...\n");
	for(m=0;m<l;m++) {
rswindell's avatar
rswindell committed
696
		printf("\r%2lu%%",m ? (long)(100.0/((float)l/m)) : 0);
697
		if(idx[m].attr&MSG_DELETE)
rswindell's avatar
rswindell committed
698
699
			flagged++; 
	}
700
701
702
703
704
705
	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++) {
rswindell's avatar
rswindell committed
706
			printf("\r%2lu%%",m ? (long)(100.0/((float)l/m)) : 0);
707
708
709
710
711
712
			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
713
714
715
				idx[m].attr|=MSG_DELETE; 
			} 
		}  /* mark for deletion */
716
		printf("\r100%% (%lu flagged for deletion due to age)\n",f); 
rswindell's avatar
rswindell committed
717
	}
718
719
720

	printf("Scanning for read messages to be killed...\n");
	for(m=f=0;m<l;m++) {
rswindell's avatar
rswindell committed
721
		printf("\r%2lu%%",m ? (long)(100.0/((float)l/m)) : 0);
722
723
724
725
726
		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
727
728
729
			idx[m].attr|=MSG_DELETE; 
		} 
	}
730
	printf("\r100%% (%lu flagged for deletion due to read status)\n",f);
731

732
	if(smb.status.max_msgs && l-flagged>smb.status.max_msgs) {
733
734
735
736
737
738
		printf("Flagging excess messages for deletion...\n");
		for(m=n=0,f=flagged;l-flagged>smb.status.max_msgs && m<l;m++) {
			if(idx[m].attr&(MSG_PERMANENT|MSG_DELETE))
				continue;
			printf("%lu of %lu\r",++n,(l-f)-smb.status.max_msgs);
			flagged++;
rswindell's avatar
rswindell committed
739
740
741
742
			idx[m].attr|=MSG_DELETE; 
		}			/* mark for deletion */
		printf("\nDone.\n\n"); 
	}
743
744

	if(!flagged) {				/* No messages to delete */
deuce's avatar
deuce committed
745
		free(idx);
746
		smb_unlocksmbhdr(&smb);
rswindell's avatar
rswindell committed
747
748
		return; 
	}
749
750
751
752
753
754
755
756

	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);
757
758
				fprintf(errfp,"\n%s!smb_open_da returned %d: %s\n"
					,beep,i,smb.last_error);
759
				bail(1); 
rswindell's avatar
rswindell committed
760
			}
761
762
763
			i=smb_open_ha(&smb);
			if(i) {
				smb_unlocksmbhdr(&smb);
764
765
				fprintf(errfp,"\n%s!smb_open_ha returned %d: %s\n"
					,beep,i,smb.last_error);
766
				bail(1); 
rswindell's avatar
rswindell committed
767
768
			} 
		}
769
770
771
772
773
774
775

		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) {
776
777
					fprintf(errfp,"\n%s!smb_getmsgidx returned %d: %s\n"
						,beep,i,smb.last_error);
rswindell's avatar
rswindell committed
778
779
					continue; 
				}
780
781
				i=smb_lockmsghdr(&smb,&msg);
				if(i) {
782
783
					fprintf(errfp,"\n%s!smb_lockmsghdr returned %d: %s\n"
						,beep,i,smb.last_error);
rswindell's avatar
rswindell committed
784
785
					break; 
				}
786
787
				if((i=smb_getmsghdr(&smb,&msg))!=0) {
					smb_unlockmsghdr(&smb,&msg);
788
789
					fprintf(errfp,"\n%s!smb_getmsghdr returned %d: %s\n"
						,beep,i,smb.last_error);
rswindell's avatar
rswindell committed
790
791
					break; 
				}
792
793
794
795
				msg.hdr.attr|=MSG_DELETE;			/* mark header as deleted */
				if((i=smb_putmsg(&smb,&msg))!=0) {
					smb_freemsgmem(&msg);
					smb_unlockmsghdr(&smb,&msg);
796
797
					fprintf(errfp,"\n%s!smb_putmsg returned %d: %s\n"
						,beep,i,smb.last_error);
rswindell's avatar
rswindell committed
798
799
					break; 
				}
800
801
802
				smb_unlockmsghdr(&smb,&msg);
				if((i=smb_freemsg(&smb,&msg))!=0) {
					smb_freemsgmem(&msg);
803
804
					fprintf(errfp,"\n%s!smb_freemsg returned %d: %s\n"
						,beep,i,smb.last_error);
rswindell's avatar
rswindell committed
805
806
807
808
809
					break; 
				}
				smb_freemsgmem(&msg); 
			} 
		}
810
811
		if(!(smb.status.attr&SMB_HYPERALLOC)) {
			smb_close_ha(&smb);
rswindell's avatar
rswindell committed
812
813
814
815
			smb_close_da(&smb); 
		}
		printf("\nDone.\n\n"); 
	}
816
817
818

	printf("Re-writing index...\n");
	rewind(smb.sid_fp);
819
	CHSIZE_FP(smb.sid_fp,0);
820
821
822
823
	for(m=n=0;m<l;m++) {
		if(idx[m].attr&MSG_DELETE)
			continue;
		printf("%lu of %lu\r",++n,l-flagged);
rswindell's avatar
rswindell committed
824
825
		fwrite(&idx[m],sizeof(idxrec_t),1,smb.sid_fp); 
	}
826
827
828
	printf("\nDone.\n\n");
	fflush(smb.sid_fp);

deuce's avatar
deuce committed
829
	free(idx);
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
	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)
{
845
846
	uchar	buf[SDT_BLOCK_LEN],ch;
	char	str[128],fname[128],tmpfname[128];
847
	int i,size;
848
	ulong l,m,n,datoffsets=0,length,total;
849
	FILE *tmp_sdt,*tmp_shd,*tmp_sid;
850
	BOOL		error=FALSE;
851
852
853
	smbhdr_t	hdr;
	smbmsg_t	msg;
	datoffset_t *datoffset=NULL;
854
	time_t		now;
855

rswindell's avatar
rswindell committed
856
857
858
	now=time(NULL);
	printf("Packing %s\n",smb.file);
	i=smb_locksmbhdr(&smb);
859
	if(i) {
860
861
		fprintf(errfp,"\n%s!smb_locksmbhdr returned %d: %s\n"
			,beep,i,smb.last_error);
rswindell's avatar
rswindell committed
862
863
		return; 
	}
rswindell's avatar
rswindell committed
864
	i=smb_getstatus(&smb);
865
866
	if(i) {
		smb_unlocksmbhdr(&smb);
867
868
		fprintf(errfp,"\n%s!smb_getstatus returned %d: %s\n"
			,beep,i,smb.last_error);
rswindell's avatar
rswindell committed
869
870
		return; 
	}
rswindell's avatar
rswindell committed
871

872
	if(!(smb.status.attr&SMB_HYPERALLOC)) {
rswindell's avatar
rswindell committed
873
874
875
		i=smb_open_ha(&smb);
		if(i) {
			smb_unlocksmbhdr(&smb);
876
877
			fprintf(errfp,"\n%s!smb_open_ha returned %d: %s\n"
				,beep,i,smb.last_error);
rswindell's avatar
rswindell committed
878
879
			return; 
		}
rswindell's avatar
rswindell committed
880
881
882
883
		i=smb_open_da(&smb);
		if(i) {
			smb_unlocksmbhdr(&smb);
			smb_close_ha(&smb);
884
885
			fprintf(errfp,"\n%s!smb_open_da returned %d: %s\n"
				,beep,i,smb.last_error);
rswindell's avatar
rswindell committed
886
887
888
			return; 
		} 
	}
889

rswindell's avatar
rswindell committed
890
891
892
	if(!smb.status.total_msgs) {
		printf("Empty\n");
		rewind(smb.shd_fp);
893
		CHSIZE_FP(smb.shd_fp,smb.status.header_offset);
rswindell's avatar
rswindell committed
894
		rewind(smb.sdt_fp);
895
		CHSIZE_FP(smb.sdt_fp,0);
rswindell's avatar
rswindell committed
896
		rewind(smb.sid_fp);
897
		CHSIZE_FP(smb.sid_fp,0);
rswindell's avatar
rswindell committed
898
899
		if(!(smb.status.attr&SMB_HYPERALLOC)) {
			rewind(smb.sha_fp);
900
			CHSIZE_FP(smb.sha_fp,0);
rswindell's avatar
rswindell committed
901
			rewind(smb.sda_fp);
902
			CHSIZE_FP(smb.sda_fp,0);
rswindell's avatar
rswindell committed
903
			smb_close_ha(&smb);
rswindell's avatar
rswindell committed
904
905
			smb_close_da(&smb); 
		}
rswindell's avatar
rswindell committed
906
		smb_unlocksmbhdr(&smb);
rswindell's avatar
rswindell committed
907
908
		return; 
	}
909
910


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

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

rswindell's avatar
rswindell committed
916
917
		fseek(smb.sda_fp,0L,SEEK_SET);
		for(l=m=0;l<length;l+=2) {
rswindell's avatar
rswindell committed
918
			printf("\r%2lu%%  ",l ? (long)(100.0/((float)length/l)) : 0);
deuce's avatar
deuce committed
919
			/* TODO: Only works on LE (size mismatch) */
rswindell's avatar
rswindell committed
920
921
922
923
			i=0;
			if(!fread(&i,2,1,smb.sda_fp))
				break;
			if(!i)
rswindell's avatar
rswindell committed
924
925
				m++; 
		}
926

rswindell's avatar
rswindell committed
927
		printf("\rAnalyzing header blocks...\n");
928

rswindell's avatar
rswindell committed
929
		length=filelength(fileno(smb.sha_fp));
930

rswindell's avatar
rswindell committed
931
932
		fseek(smb.sha_fp,0L,SEEK_SET);
		for(l=n=0;l<length;l++) {
rswindell's avatar
rswindell committed
933
			printf("\r%2lu%%  ",l ? (long)(100.0/((float)length/l)) : 0);
rswindell's avatar
rswindell committed
934
935
936
937
			ch=0;
			if(!fread(&ch,1,1,smb.sha_fp))
				break;
			if(!ch)
rswindell's avatar
rswindell committed
938
939
				n++; 
		}
rswindell's avatar
rswindell committed
940
941
942
943
944
945

		if(!m && !n) {
			printf("\rAlready compressed.\n\n");
			smb_close_ha(&smb);
			smb_close_da(&smb);
			smb_unlocksmbhdr(&smb);
rswindell's avatar
rswindell committed
946
947
			return; 
		}
948

rswindell's avatar
rswindell committed
949
		if(packable && (m*SDT_BLOCK_LEN)+(n*SHD_BLOCK_LEN)<packable*1024L) {
950
			printf("\r%lu less than %lu compressible bytes.\n\n",(m*SDT_BLOCK_LEN)+(n*SHD_BLOCK_LEN), packable*1024L);
rswindell's avatar
rswindell committed
951
952
953
			smb_close_ha(&smb);
			smb_close_da(&smb);
			smb_unlocksmbhdr(&smb);
rswindell's avatar
rswindell committed
954
955
			return; 
		}
rswindell's avatar
rswindell committed
956
957
958

		printf("\rCompressing %lu data blocks (%lu bytes)\n"
				 "        and %lu header blocks (%lu bytes)\n"
rswindell's avatar
rswindell committed
959
960
				  ,m,m*SDT_BLOCK_LEN,n,n*SHD_BLOCK_LEN); 
	}
rswindell's avatar
rswindell committed
961
962
963

	if(!(smb.status.attr&SMB_HYPERALLOC)) {
		rewind(smb.sha_fp);
964
		CHSIZE_FP(smb.sha_fp,0);		/* Reset both allocation tables */
rswindell's avatar
rswindell committed
965
		rewind(smb.sda_fp);
966
		CHSIZE_FP(smb.sda_fp,0); 
rswindell's avatar
rswindell committed
967
	}
rswindell's avatar
rswindell committed
968
969
970
971
972
973
974

	if(smb.status.attr&SMB_HYPERALLOC && !(mode&NOANALYSIS)) {
		printf("Analyzing %s\n",smb.file);

		length=filelength(fileno(smb.shd_fp));
		m=n=0;
		for(l=smb.status.header_offset;l<length;l+=size) {
rswindell's avatar
rswindell committed
975
			printf("\r%2lu%%  ",(long)(100.0/((float)length/l)));
rswindell's avatar
rswindell committed
976
977
978
979
			msg.idx.offset=l;
			if((i=smb_lockmsghdr(&smb,&msg))!=0) {
				printf("\n(%06lX) smb_lockmsghdr returned %d\n",l,i);
				size=SHD_BLOCK_LEN;
rswindell's avatar
rswindell committed
980
981
				continue; 
			}
rswindell's avatar
rswindell committed
982
983
984
985
			if((i=smb_getmsghdr(&smb,&msg))!=0) {
				smb_unlockmsghdr(&smb,&msg);
				m++;
				size=SHD_BLOCK_LEN;
rswindell's avatar
rswindell committed
986
987
				continue; 
			}
rswindell's avatar
rswindell committed
988
989
990
			smb_unlockmsghdr(&smb,&msg);
			if(msg.hdr.attr&MSG_DELETE) {
				m+=smb_hdrblocks(msg.hdr.length);
rswindell's avatar
rswindell committed
991
992
993
994
995
				n+=smb_datblocks(smb_getmsgdatlen(&msg)); 
			}
			size=smb_hdrblocks(smb_getmsghdrlen(&msg))*SHD_BLOCK_LEN;
			smb_freemsgmem(&msg); 
		}
rswindell's avatar
rswindell committed
996
997
998
999
1000


		if(!m && !n) {
			printf("\rAlready compressed.\n\n");
			smb_unlocksmbhdr(&smb);
rswindell's avatar
rswindell committed
1001
1002
			return; 
		}
rswindell's avatar
rswindell committed
1003
1004

		if(packable && (n*SDT_BLOCK_LEN)+(m*SHD_BLOCK_LEN)<packable*1024L) {
1005
			printf("\r%lu less than %lu compressible bytes.\n\n",(n*SDT_BLOCK_LEN)+(m*SHD_BLOCK_LEN), packable*1024);
rswindell's avatar
rswindell committed
1006
			smb_unlocksmbhdr(&smb);
rswindell's avatar
rswindell committed
1007
1008
			return; 
		}
rswindell's avatar
rswindell committed
1009
1010
1011

		printf("\rCompressing %lu data blocks (%lu bytes)\n"
				 "        and %lu header blocks (%lu bytes)\n"
rswindell's avatar
rswindell committed
1012
1013
				  ,n,n*SDT_BLOCK_LEN,m,m*SHD_BLOCK_LEN); 
	}
rswindell's avatar
rswindell committed
1014
1015