smbutil.c 48.8 KB
Newer Older
1
2
3
4
5
6
/* Synchronet message base (SMB) utility */

/****************************************************************************
 * @format.tab-size 4		(Plain Text/Source Code File Header)			*
 * @format.use-tabs true	(see http://www.synchro.net/ptsc_hdr.html)		*
 *																			*
7
 * Copyright Rob Swindell - http://www.synchro.net/copyright.html			*
8
9
10
11
12
13
14
15
16
17
18
19
20
21
 *																			*
 * 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										*
 *																			*
 * For Synchronet coding style and modification guidelines, see				*
 * http://www.synchro.net/source.html										*
 *																			*
 * Note: If this box doesn't appear square, then you need to fix your tabs.	*
 ****************************************************************************/

rswindell's avatar
rswindell committed
22
#define SMBUTIL_VER "2.34"
23
24
char	revision[16];
char	compiler[32];
25

26
27
28
29
30
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"};


31
#define NOANALYSIS		(1L<<0)
32
#define NOCRC			(1L<<1)
33
34
35
36
37
38
39

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

40
41
42
43
44
#if defined(_WIN32)
	#include <conio.h>	/* getch() */
#endif

/* ANSI */
45
#include <stdio.h>
rswindell's avatar
rswindell committed
46
47
48
49
#include <time.h>		/* time */
#include <errno.h>		/* errno */
#include <string.h>		/* strrchr */
#include <ctype.h>		/* toupper */
50

51
52
53
#include "fidodefs.h"
#include "smblib.h"
#include "str_util.h"
54
#include "utf8.h"
55
#include "conwrap.h"
56

57
/* gets is dangerous */
58
#define gets(str)  fgets((str), sizeof(str), stdin)
59

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

62
63
64
65
/********************/
/* Global variables */
/********************/

66
67
68
69
70
71
72
73
74
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;
75
char*		beep="";
rswindell's avatar
rswindell committed
76
long		msgtxtmode = GETMSGTXT_ALL|GETMSGTXT_PLAIN;
77
78
79
80
81
82

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

char *usage=
83
"usage: smbutil [-opts] cmd <filespec.shd>\n"
84
85
86
87
"\n"
"cmd:\n"
"       l[n] = list msgs starting at number n\n"
"       r[n] = read msgs starting at number n\n"
88
"       x[n] = dump msg index at number n\n"
89
"       v[n] = view msg headers starting at number n\n"
90
91
92
"       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
93
"       h    = dump hash file\n"
94
95
"       s    = display msg base status\n"
"       c    = change msg base status\n"
rswindell's avatar
rswindell committed
96
"       R    = re-initialize/repair SMB/status headers\n"
97
"       d    = delete all msgs\n"
98
99
"       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
100
101
"       L    = lock a msg base for exclusive-access/backup\n"
"       U    = unlock a msg base\n"
102
103
104
105
106
"\n"
"            [n] may represent 1-based message index offset, or\n"
"            [#n] actual message number, or\n"
"            [-n] message age (in days)\n"
"\n"
107
"opts:\n"
rswindell's avatar
rswindell committed
108
109
110
111
112
113
114
115
116
"      -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
117
"      -r    = display raw message body text (not MIME-decoded)\n"
rswindell's avatar
rswindell committed
118
119
120
121
122
123
124
125
"      -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"
126
#ifdef __unix__
127
"      -U[n] = set umask to specified value (use leading 0 for octal, e.g. 022)\n"
128
#endif
rswindell's avatar
rswindell committed
129
"      -#    = set number of messages to view/list (e.g. -1)\n"
130
131
;

132
133
134
135
136
137
138
139
140
141
142
143
144
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);
}

145
/*****************************************************************************/
146
/* Expands Unix LF to CRLF													 */
147
/*****************************************************************************/
148
ulong lf_expand(uchar* inbuf, uchar* outbuf)
149
150
{
	ulong	i,j;
rswindell's avatar
rswindell committed
151

152
	for(i=j=0;inbuf[i];i++) {
153
154
155
156
		if(inbuf[i]=='\n' && (!i || inbuf[i-1]!='\r'))
			outbuf[j++]='\r';
		outbuf[j++]=inbuf[i];
	}
157
	outbuf[j]=0;
158
159
	return(j);
}
rswindell's avatar
rswindell committed
160

161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
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>"
180
181
		,(ulong)msg->hdr.when_imported.time
		,(ulong)smb->status.last_msg + 1
182
183
184
185
186
		,getfname(smb->file)
		,host);
	return msgid;
}

187
188
189
/****************************************************************************/
/* Adds a new message to the message base									*/
/****************************************************************************/
190
191
void postmsg(char type, char* to, char* to_number, char* to_address, 
			 char* from, char* from_number, char* subject, FILE* fp)
192
{
193
194
	char		str[128];
	char		buf[1024];
rswindell's avatar
rswindell committed
195
196
	char*		msgtxt=NULL;
	char*		newtxt;
197
	const char* charset = NULL;
198
199
	long		msgtxtlen;
	int 		i;
200
	ushort		agent=AGENT_SMBUTIL;
201
	smbmsg_t	msg;
202
	long		dupechk_hashes=SMB_HASH_SOURCE_DUPE;
203

204
205
206
207
208
209
	/* 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
210
		if((msgtxt = realloc(msgtxt,msgtxtlen+i+1))==NULL) {
211
			fprintf(errfp,"\n%s!realloc(%ld) failure\n",beep,msgtxtlen+i+1);
212
			bail(1);
213
		}
214
		memcpy(msgtxt+msgtxtlen,buf,i);
215
216
217
		msgtxtlen+=i;
	}

218
	if(msgtxt!=NULL) {
219
220
221

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

rswindell's avatar
rswindell committed
222
		if((newtxt = malloc((msgtxtlen*2)+1))==NULL) {
223
			fprintf(errfp,"\n%s!malloc(%ld) failure\n",beep,(msgtxtlen*2)+1);
224
			bail(1);
225
		}
rswindell's avatar
rswindell committed
226

227
		/* Expand LFs to CRLFs */
rswindell's avatar
rswindell committed
228
		msgtxtlen=lf_expand((uchar*)msgtxt, (uchar*)newtxt);
229
230
		free(msgtxt);
		msgtxt=newtxt;
231
232
233
234
235
236
237

		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;
238
	}
rswindell's avatar
rswindell committed
239
240

	memset(&msg,0,sizeof(smbmsg_t));
241
	msg.hdr.when_written.time=(uint32_t)time(NULL);
rswindell's avatar
rswindell committed
242
243
244
	msg.hdr.when_written.zone=tzone;
	msg.hdr.when_imported=msg.hdr.when_written;

245
246
	if((to==NULL || stricmp(to,"All")==0) && to_address!=NULL)
		to=to_address;
247
248
	if(to==NULL) {
		printf("To User Name: ");
249
		fgets(str,sizeof(str),stdin); 
250
	} else
251
		SAFECOPY(str,to);
rswindell's avatar
rswindell committed
252
	truncsp(str);
253
254

	if((i=smb_hfield_str(&msg,RECIPIENT,str))!=SMB_SUCCESS) {
255
256
		fprintf(errfp,"\n%s!smb_hfield_str(0x%02X) returned %d: %s\n"
			,beep,RECIPIENT,i,smb.last_error);
257
		bail(1); 
rswindell's avatar
rswindell committed
258
	}
rswindell's avatar
rswindell committed
259
260
261
	if(type=='E' || type=='N')
		smb.status.attr|=SMB_EMAIL;
	if(smb.status.attr&SMB_EMAIL) {
262
263
264
265
266
267
268
269
270
271
272
273
		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); 
			}
274
275
		}
	}
rswindell's avatar
rswindell committed
276

277
	if(smb.status.attr&SMB_EMAIL && (type=='N' || to_address!=NULL)) {
278
		if(to_address==NULL) {
279
			printf("To Address (e.g. user@host or 1:2/3): ");
280
281
			gets(str);
		} else
282
			SAFECOPY(str,to_address);
rswindell's avatar
rswindell committed
283
284
		truncsp(str);
		if(*str) {
285
286
			if((i=smb_hfield_netaddr(&msg,RECIPIENTNETADDR,str,NULL))!=SMB_SUCCESS) {
				fprintf(errfp,"\n%s!smb_hfield_netaddr(0x%02X) returned %d: %s\n"
287
					,beep,RECIPIENTNETADDR,i,smb.last_error);
288
				bail(1); 
289
			}
290
		}
291
	}
rswindell's avatar
rswindell committed
292

293
294
295
296
	if(from==NULL) {
		printf("From User Name: ");
		gets(str);
	} else
297
		SAFECOPY(str,from);
rswindell's avatar
rswindell committed
298
	truncsp(str);
299
	if((i=smb_hfield_str(&msg,SENDER,str))!=SMB_SUCCESS) {
300
301
		fprintf(errfp,"\n%s!smb_hfield_str(0x%02X) returned %d: %s\n"
			,beep,SENDER,i,smb.last_error);
302
		bail(1); 
rswindell's avatar
rswindell committed
303
	}
304
	if((smb.status.attr&SMB_EMAIL) || from_number!=NULL) {
305
306
307
308
		if(from_number==NULL) {
			printf("From User Number: ");
			gets(str);
		} else
309
			SAFECOPY(str,from_number);
rswindell's avatar
rswindell committed
310
		truncsp(str);
311
		if((i=smb_hfield_str(&msg,SENDEREXT,str))!=SMB_SUCCESS) {
312
313
			fprintf(errfp,"\n%s!smb_hfield_str(0x%02X) returned %d: %s\n"
				,beep,SENDEREXT,i,smb.last_error);
314
			bail(1); 
315
316
		}
	}
317
	if((i=smb_hfield(&msg, SENDERAGENT, sizeof(agent), &agent))!=SMB_SUCCESS) {
318
319
		fprintf(errfp,"\n%s!smb_hfield(0x%02X) returned %d: %s\n"
			,beep,SENDERAGENT,i,smb.last_error);
320
		bail(1);
321
	}
rswindell's avatar
rswindell committed
322

323
324
325
326
	if(subject==NULL) {
		printf("Subject: ");
		gets(str);
	} else
327
		SAFECOPY(str,subject);
rswindell's avatar
rswindell committed
328
	truncsp(str);
329
	if((i=smb_hfield_str(&msg,SUBJECT,str))!=SMB_SUCCESS) {
330
331
		fprintf(errfp,"\n%s!smb_hfield_str(0x%02X) returned %d: %s\n"
			,beep,SUBJECT,i,smb.last_error);
332
		bail(1); 
333
	}
rswindell's avatar
rswindell committed
334

335
	safe_snprintf(str,sizeof(str),"SMBUTIL %s-%s r%s %s %s"
336
337
338
339
340
341
		,SMBUTIL_VER
		,PLATFORM_DESC
		,revision
		,__DATE__
		,compiler
		);
342
	if((i=smb_hfield_str(&msg,FIDOPID,str))!=SMB_SUCCESS) {
343
344
		fprintf(errfp,"\n%s!smb_hfield_str(0x%02X) returned %d: %s\n"
			,beep,FIDOPID,i,smb.last_error);
345
		bail(1); 
346
	}
rswindell's avatar
rswindell committed
347

348
	smb_hfield_str(&msg, RFC822MSGID, gen_msgid(&smb, &msg, str, sizeof(str)-1));
349
350
351
352
353
354
355
	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;
356

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

360
	if((i=smb_addmsg(&smb,&msg,smb.status.attr&SMB_HYPERALLOC
rswindell's avatar
rswindell committed
361
		,dupechk_hashes,xlat,(uchar*)msgtxt,NULL))!=SMB_SUCCESS) {
362
363
		fprintf(errfp,"\n%s!smb_addmsg returned %d: %s\n"
			,beep,i,smb.last_error);
364
		bail(1); 
rswindell's avatar
rswindell committed
365
	}
rswindell's avatar
rswindell committed
366
	smb_freemsgmem(&msg);
367

deuce's avatar
deuce committed
368
369
	// 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);
370
	FREE_AND_NULL(msgtxt);
371
372
373
374
375
}

/****************************************************************************/
/* Shows the message base header											*/
/****************************************************************************/
376
void showstatus(void)
377
378
379
{
	int i;

rswindell's avatar
rswindell committed
380
381
	i=smb_locksmbhdr(&smb);
	if(i) {
382
383
		fprintf(errfp,"\n%s!smb_locksmbhdr returned %d: %s\n"
			,beep,i,smb.last_error);
rswindell's avatar
rswindell committed
384
385
		return; 
	}
rswindell's avatar
rswindell committed
386
387
388
	i=smb_getstatus(&smb);
	smb_unlocksmbhdr(&smb);
	if(i) {
389
390
		fprintf(errfp,"\n%s!smb_getstatus returned %d: %s\n"
			,beep,i,smb.last_error);
rswindell's avatar
rswindell committed
391
392
		return; 
	}
deuce's avatar
deuce committed
393
394
395
396
397
	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
398
399
400
401
402
403
404
405
406
407
		   "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
		   );
408
409
410
411
412
}

/****************************************************************************/
/* Configure message base header											*/
/****************************************************************************/
413
void config(void)
414
{
415
	char str[128];
416
417
	char max_msgs[128],max_crcs[128],max_age[128],header_offset[128],attr[128];
	int i;
418
	uint32_t last_msg = 0;
419

rswindell's avatar
rswindell committed
420
421
	i=smb_locksmbhdr(&smb);
	if(i) {
422
423
		fprintf(errfp,"\n%s!smb_locksmbhdr returned %d: %s\n"
			,beep,i,smb.last_error);
rswindell's avatar
rswindell committed
424
425
		return; 
	}
rswindell's avatar
rswindell committed
426
427
428
	i=smb_getstatus(&smb);
	smb_unlocksmbhdr(&smb);
	if(i) {
429
430
		fprintf(errfp,"\n%s!smb_getstatus returned %d: %s\n"
			,beep,i,smb.last_error);
rswindell's avatar
rswindell committed
431
432
		return; 
	}
433
434
435
	printf("Last Message  =%-6"PRIu32" New Value (CR=No Change): "
		,smb.status.last_msg);
	gets(str);
436
	if(IS_DIGIT(str[0]))
437
		last_msg = atol(str);
deuce's avatar
deuce committed
438
	printf("Header offset =%-5"PRIu32"  New value (CR=No Change): "
rswindell's avatar
rswindell committed
439
440
		,smb.status.header_offset);
	gets(header_offset);
deuce's avatar
deuce committed
441
	printf("Max msgs      =%-5"PRIu32"  New value (CR=No Change): "
rswindell's avatar
rswindell committed
442
443
		,smb.status.max_msgs);
	gets(max_msgs);
deuce's avatar
deuce committed
444
	printf("Max crcs      =%-5"PRIu32"  New value (CR=No Change): "
rswindell's avatar
rswindell committed
445
446
447
448
449
450
451
452
453
454
		,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) {
455
456
		fprintf(errfp,"\n%s!smb_locksmbhdr returned %d: %s\n"
			,beep,i,smb.last_error);
rswindell's avatar
rswindell committed
457
458
		return; 
	}
rswindell's avatar
rswindell committed
459
460
	i=smb_getstatus(&smb);
	if(i) {
461
462
		fprintf(errfp,"\n%s!smb_getstatus returned %d: %s\n"
			,beep,i,smb.last_error);
rswindell's avatar
rswindell committed
463
		smb_unlocksmbhdr(&smb);
rswindell's avatar
rswindell committed
464
465
		return; 
	}
466
467
	if(last_msg != 0)
		smb.status.last_msg = last_msg;
468
	if(IS_DIGIT(max_msgs[0]))
rswindell's avatar
rswindell committed
469
		smb.status.max_msgs=atol(max_msgs);
470
	if(IS_DIGIT(max_crcs[0]))
rswindell's avatar
rswindell committed
471
		smb.status.max_crcs=atol(max_crcs);
472
	if(IS_DIGIT(max_age[0]))
rswindell's avatar
rswindell committed
473
		smb.status.max_age=atoi(max_age);
474
	if(IS_DIGIT(header_offset[0]))
rswindell's avatar
rswindell committed
475
		smb.status.header_offset=atol(header_offset);
476
	if(IS_DIGIT(attr[0]))
rswindell's avatar
rswindell committed
477
478
		smb.status.attr=atoi(attr);
	i=smb_putstatus(&smb);
479
	smb_unlocksmbhdr(&smb);
rswindell's avatar
rswindell committed
480
	if(i)
481
482
		fprintf(errfp,"\n%s!smb_putstatus returned %d: %s\n"
			,beep,i,smb.last_error);
483
484
485
486
487
488
489
490
491
492
493
}

/****************************************************************************/
/* 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
494
495
	if(!start)
		start=1;
496
497
	if(!count)
		count=~0;
rswindell's avatar
rswindell committed
498
499
500
501
502
503
	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) {
504
505
			fprintf(errfp,"\n%s!smb_lockmsghdr returned %d: %s\n"
				,beep,i,smb.last_error);
rswindell's avatar
rswindell committed
506
507
			break; 
		}
rswindell's avatar
rswindell committed
508
509
510
		i=smb_getmsghdr(&smb,&msg);
		smb_unlockmsghdr(&smb,&msg);
		if(i) {
511
512
			fprintf(errfp,"\n%s!smb_getmsghdr returned %d: %s\n"
				,beep,i,smb.last_error);
rswindell's avatar
rswindell committed
513
514
			break; 
		}
515
516
		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
517
		smb_freemsgmem(&msg);
rswindell's avatar
rswindell committed
518
519
		l++; 
	}
520
521
522
523
524
525
}

/****************************************************************************/
/* Generates a 24 character ASCII string that represents the time_t pointer */
/* Used as a replacement for ctime()                                        */
/****************************************************************************/
526
char *my_timestr(time_t intime)
527
528
529
530
531
{
    static char str[256];
    char mer[3],hour;
    struct tm *gm;

532
	gm=localtime(&intime);
rswindell's avatar
rswindell committed
533
534
	if(gm==NULL) {
		strcpy(str,"Invalid Time");
rswindell's avatar
rswindell committed
535
536
		return(str); 
	}
rswindell's avatar
rswindell committed
537
538
539
540
541
	if(gm->tm_hour>=12) {
		if(gm->tm_hour==12)
			hour=12;
		else
			hour=gm->tm_hour-12;
rswindell's avatar
rswindell committed
542
543
		strcpy(mer,"pm"); 
	}
rswindell's avatar
rswindell committed
544
545
546
547
548
	else {
		if(gm->tm_hour==0)
			hour=12;
		else
			hour=gm->tm_hour;
rswindell's avatar
rswindell committed
549
550
		strcpy(mer,"am"); 
	}
rswindell's avatar
rswindell committed
551
552
553
554
	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);
555
556
}

557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
/****************************************************************************/
/****************************************************************************/
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;
572
		printf("%10"PRIu32"  ", idx.number);
573
		if(idx.attr&MSG_VOTE && !(idx.attr&MSG_POLL))
574
			printf("V  %04hX  %-10"PRIu32, idx.votes,idx.remsg);
575
		else
576
			printf("%c  %04hX  %04hX  %04X"
577
				,(idx.attr&MSG_POLL_VOTE_MASK) == MSG_POLL_CLOSURE ? 'C' : (idx.attr&MSG_POLL ? 'P':' ')
578
				,idx.from, idx.to, idx.subj);
579
		printf("  %04X  %06X  %s\n", idx.attr, idx.offset, my_timestr(idx.time));
580
581
582
583
		l++; 
	}
}

584
585
586
/****************************************************************************/
/* Displays message header information										*/
/****************************************************************************/
587
void viewmsgs(ulong start, ulong count, BOOL verbose)
588
{
589
	int i,j;
590
591
592
	ulong l=0;
	smbmsg_t msg;

rswindell's avatar
rswindell committed
593
594
	if(!start)
		start=1;
595
596
	if(!count)
		count=~0;
rswindell's avatar
rswindell committed
597
598
599
600
601
602
	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) {
603
604
			fprintf(errfp,"\n%s!smb_lockmsghdr returned %d: %s\n"
				,beep,i,smb.last_error);
rswindell's avatar
rswindell committed
605
606
			break; 
		}
rswindell's avatar
rswindell committed
607
608
609
		i=smb_getmsghdr(&smb,&msg);
		smb_unlockmsghdr(&smb,&msg);
		if(i) {
610
611
			fprintf(errfp,"\n%s!smb_getmsghdr returned %d: %s\n"
				,beep,i,smb.last_error);
rswindell's avatar
rswindell committed
612
613
			break; 
		}
614

615
		printf("--------------------\n");
616
		printf("%-16.16s %ld\n"		,"index record",ftell(smb.sid_fp)/sizeof(idxrec_t));
617
		smb_dump_msghdr(stdout,&msg);
618
619
620
621
622
623
624
625
626
		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
627
628
		printf("\n");
		smb_freemsgmem(&msg);
rswindell's avatar
rswindell committed
629
630
		l++; 
	}
631
632
}

633
634
635
636
/****************************************************************************/
/****************************************************************************/
void dump_hashes(void)
{
rswindell's avatar
rswindell committed
637
	char	tmp[128];
638
639
640
641
	int		retval;
	hash_t	hash;

	if((retval=smb_open_hash(&smb))!=SMB_SUCCESS) {
642
643
		fprintf(errfp,"\n%s!smb_open_hash returned %d: %s\n"
			,beep, retval, smb.last_error);
644
645
646
647
648
649
		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
650
		printf("\n");
deuce's avatar
deuce committed
651
		printf("%-10s: %"PRIu32"\n","Number",	hash.number);
652
		printf("%-10s: %s\n",		"Source",	smb_hashsourcetype(hash.source));
deuce's avatar
deuce committed
653
		printf("%-10s: %"PRIu32"\n","Length",	hash.length);
654
		printf("%-10s: %s\n",		"Time",		my_timestr(hash.time));
655
		printf("%-10s: %02x\n",		"Flags",	hash.flags);
rswindell's avatar
rswindell committed
656
657
658
		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
659
			printf("%-10s: %08"PRIx32"\n","CRC-32",	hash.crc32);
rswindell's avatar
rswindell committed
660
		if(hash.flags&SMB_HASH_MD5)
661
			printf("%-10s: %s\n",	"MD5",		MD5_hex((BYTE*)tmp,hash.md5));
662
	}
663
664

	smb_close_hash(&smb);
665
666
}

667
668
669
670
671
672
673
674
675
676
/****************************************************************************/
/* 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
677
	idxrec_t *idx;
678
679
680
681
682

	printf("Maintaining %s\r\n",smb.file);
	now=time(NULL);
	i=smb_locksmbhdr(&smb);
	if(i) {
683
684
		fprintf(errfp,"\n%s!smb_locksmbhdr returned %d: %s\n"
			,beep,i,smb.last_error);
rswindell's avatar
rswindell committed
685
686
		return; 
	}
687
688
689
	i=smb_getstatus(&smb);
	if(i) {
		smb_unlocksmbhdr(&smb);
690
691
		fprintf(errfp,"\n%s!smb_getstatus returned %d: %s\n"
			,beep,i,smb.last_error);
rswindell's avatar
rswindell committed
692
693
		return; 
	}
694
	if((smb.status.max_msgs || smb.status.max_crcs) && smb_open_hash(&smb) == SMB_SUCCESS)
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
	{
		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);
714
715
						fflush(smb.hash_fp);
						CHSIZE_FP(smb.hash_fp, sizeof(hash_t) * max_hashes * SMB_HASH_SOURCE_TYPES);
716
717
718
719
720
721
722
					}
					free(hashes);
				}
			}
		}
		smb_close_hash(&smb);
	}
723
724
725
	if(!smb.status.total_msgs) {
		smb_unlocksmbhdr(&smb);
		printf("Empty\n");
rswindell's avatar
rswindell committed
726
727
		return; 
	}
728
	printf("Loading index...\n");
deuce's avatar
deuce committed
729
	if((idx=(idxrec_t *)malloc(sizeof(idxrec_t)*smb.status.total_msgs))
730
731
		==NULL) {
		smb_unlocksmbhdr(&smb);
deuce's avatar
deuce committed
732
		fprintf(errfp,"\n%s!Error allocating %" XP_PRIsize_t "u bytes of memory\n"
733
			,beep,sizeof(idxrec_t)*smb.status.total_msgs);
rswindell's avatar
rswindell committed
734
735
		return; 
	}
736
	fseek(smb.sid_fp,0L,SEEK_SET);
737
	l = fread(idx, sizeof(idxrec_t), smb.status.total_msgs, smb.sid_fp);
738

739
	printf("\nDone.\n\n");
740
741
	printf("Scanning for pre-flagged messages...\n");
	for(m=0;m<l;m++) {
742
//		printf("\r%2lu%%",m ? (long)(100.0/((float)l/m)) : 0);
743
		if(idx[m].attr&MSG_DELETE)
rswindell's avatar
rswindell committed
744
745
			flagged++; 
	}
746
747
748
749
750
751
	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++) {
752
//			printf("\r%2lu%%",m ? (long)(100.0/((float)l/m)) : 0);
753
754
755
756
757
758
			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
759
760
761
				idx[m].attr|=MSG_DELETE; 
			} 
		}  /* mark for deletion */
762
		printf("\r100%% (%lu flagged for deletion due to age)\n",f); 
rswindell's avatar
rswindell committed
763
	}
764
765

	printf("Scanning for read messages to be killed...\n");
766
	uint32_t total_msgs = 0;
767
	for(m=f=0;m<l;m++) {
768
769
		enum smb_msg_type type = smb_msg_type(idx[m].attr);
		if(type == SMB_MSG_TYPE_NORMAL || type == SMB_MSG_TYPE_POLL)
770
			total_msgs++;
771
//		printf("\r%2lu%%",m ? (long)(100.0/((float)l/m)) : 0);
772
773
774
775
776
		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
777
			idx[m].attr|=MSG_DELETE; 
778
		}
rswindell's avatar
rswindell committed
779
	}
780
	printf("\r100%% (%lu flagged for deletion due to read status)\n",f);
781

782
	if(smb.status.max_msgs && total_msgs - flagged > smb.status.max_msgs) {
783
		printf("Flagging excess messages for deletion...\n");
784
		for(m=n=0,f=flagged; total_msgs - flagged > smb.status.max_msgs && m<l; m++) {
785
786
			if(idx[m].attr&(MSG_PERMANENT|MSG_DELETE))
				continue;
787
			printf("%lu of %lu\r",++n,(total_msgs - f)-smb.status.max_msgs);
788
			flagged++;
rswindell's avatar
rswindell committed
789
790
791
792
			idx[m].attr|=MSG_DELETE; 
		}			/* mark for deletion */
		printf("\nDone.\n\n"); 
	}
793
794

	if(!flagged) {				/* No messages to delete */
deuce's avatar
deuce committed
795
		free(idx);
796
		smb_unlocksmbhdr(&smb);
rswindell's avatar
rswindell committed
797
798
		return; 
	}
799
800
801
802
803
804
805
806

	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);
807
808
				fprintf(errfp,"\n%s!smb_open_da returned %d: %s\n"
					,beep,i,smb.last_error);
809
				bail(1); 
rswindell's avatar
rswindell committed
810
			}
811
812
813
			i=smb_open_ha(&smb);
			if(i) {
				smb_unlocksmbhdr(&smb);
814
815
				fprintf(errfp,"\n%s!smb_open_ha returned %d: %s\n"
					,beep,i,smb.last_error);
816
				bail(1); 
rswindell's avatar
rswindell committed
817
818
			} 
		}
819
820
821
822
823
824
825

		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) {
826
827
					fprintf(errfp,"\n%s!smb_getmsgidx returned %d: %s\n"
						,beep,i,smb.last_error);
rswindell's avatar
rswindell committed
828
829
					continue; 
				}
830
831
				i=smb_lockmsghdr(&smb,&msg);
				if(i) {
832
833
					fprintf(errfp,"\n%s!smb_lockmsghdr returned %d: %s\n"
						,beep,i,smb.last_error);
rswindell's avatar
rswindell committed
834
835
					break; 
				}
836
837
				if((i=smb_getmsghdr(&smb,&msg))!=0) {
					smb_unlockmsghdr(&smb,&msg);
838
839
					fprintf(errfp,"\n%s!smb_getmsghdr returned %d: %s\n"
						,beep,i,smb.last_error);
rswindell's avatar
rswindell committed
840
841
					break; 
				}
842
843
844
845
				msg.hdr.attr|=MSG_DELETE;			/* mark header as deleted */
				if((i=smb_putmsg(&smb,&msg))!=0) {
					smb_freemsgmem(&msg);
					smb_unlockmsghdr(&smb,&msg);
846
847
					fprintf(errfp,"\n%s!smb_putmsg returned %d: %s\n"
						,beep,i,smb.last_error);
rswindell's avatar
rswindell committed
848
849
					break; 
				}
850
851
852
				smb_unlockmsghdr(&smb,&msg);
				if((i=smb_freemsg(&smb,&msg))!=0) {
					smb_freemsgmem(&msg);
853
854
					fprintf(errfp,"\n%s!smb_freemsg returned %d: %s\n"
						,beep,i,smb.last_error);
rswindell's avatar
rswindell committed
855
856
857
858
859
					break; 
				}
				smb_freemsgmem(&msg); 
			} 
		}
860
861
		if(!(smb.status.attr&SMB_HYPERALLOC)) {
			smb_close_ha(&smb);
rswindell's avatar
rswindell committed
862
863
864
865
			smb_close_da(&smb); 
		}
		printf("\nDone.\n\n"); 
	}
866
867
868
869
870
871

	printf("Re-writing index...\n");
	rewind(smb.sid_fp);
	for(m=n=0;m<l;m++) {
		if(idx[m].attr&MSG_DELETE)
			continue;
872
873
		n++;
		printf("%lu of %lu\r", n, l-flagged);
rswindell's avatar
rswindell committed
874
875
		fwrite(&idx[m],sizeof(idxrec_t),1,smb.sid_fp); 
	}
876
	fflush(smb.sid_fp);
877
878
	CHSIZE_FP(smb.sid_fp, n * sizeof(idxrec_t));
	printf("\nDone.\n\n");
879

deuce's avatar
deuce committed
880
	free(idx);
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
	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)
{
896
	uchar	buf[SDT_BLOCK_LEN],ch;
897
	char	fname[MAX_PATH+1],tmpfname[MAX_PATH+1];
898
	int i,size;
899
	ulong l,m,n,datoffsets=0,length,total;
900
	FILE *tmp_sdt,*tmp_shd,*tmp_sid;
901
	BOOL		error=FALSE;
902
903
904
	smbhdr_t	hdr;
	smbmsg_t	msg;
	datoffset_t *datoffset=NULL;
905
	time_t		now;
906

rswindell's avatar
rswindell committed
907
908
909
	now=time(NULL);
	printf("Packing %s\n",smb.file);
	i=smb_locksmbhdr(&smb);
910
	if(i) {
911
912
		fprintf(errfp,"\n%s!smb_locksmbhdr returned %d: %s\n"
			,beep,i,smb.last_error);
rswindell's avatar
rswindell committed
913
914
		return; 
	}
rswindell's avatar
rswindell committed
915
	i=smb_getstatus(&smb);
916
917
	if(i) {
		smb_unlocksmbhdr(&smb);
918
919
		fprintf(errfp,"\n%s!smb_getstatus returned %d: %s\n"
			,beep,i,smb.last_error);
rswindell's avatar
rswindell committed
920
921
		return; 
	}
rswindell's avatar
rswindell committed
922

923
	if(!(smb.status.attr&SMB_HYPERALLOC)) {
rswindell's avatar
rswindell committed
924
925
926
		i=smb_open_ha(&smb);
		if(i) {
			smb_unlocksmbhdr(&smb);
927
928
			fprintf(errfp,"\n%s!smb_open_ha returned %d: %s\n"
				,beep,i,smb.last_error);
rswindell's avatar
rswindell committed
929
930
			return; 
		}
rswindell's avatar
rswindell committed
931
932
933
934
		i=smb_open_da(&smb);
		if(i) {
			smb_unlocksmbhdr(&smb);
			smb_close_ha(&smb);
935
936
			fprintf(errfp,"\n%s!smb_open_da returned %d: %s\n"
				,beep,i,smb.last_error);
rswindell's avatar
rswindell committed
937
938
939
			return; 
		} 
	}
940

rswindell's avatar
rswindell committed
941
942
943
	if(!smb.status.total_msgs) {
		printf("Empty\n");
		rewind(smb.shd_fp);
944
		CHSIZE_FP(smb.shd_fp,smb.status.header_offset);
rswindell's avatar
rswindell committed
945
		rewind(smb.sdt_fp);
946
		CHSIZE_FP(smb.sdt_fp,0);
rswindell's avatar
rswindell committed
947
		rewind(smb.sid_fp);
948
		CHSIZE_FP(smb.sid_fp,0);
rswindell's avatar
rswindell committed
949
950
		if(!(smb.status.attr&SMB_HYPERALLOC)) {
			rewind(smb.sha_fp);
951
			CHSIZE_FP(smb.sha_fp,0);
rswindell's avatar
rswindell committed
952
			rewind(smb.sda_fp);
953
			CHSIZE_FP(smb.sda_fp,0);
rswindell's avatar
rswindell committed
954
			smb_close_ha(&smb);
rswindell's avatar
rswindell committed
955
956
			smb_close_da(&smb); 
		}
rswindell's avatar
rswindell committed
957
		smb_unlocksmbhdr(&smb);
rswindell's avatar
rswindell committed
958
959
		return; 
	}
960
961


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

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

rswindell's avatar
rswindell committed
967
968
		fseek(smb.sda_fp,0L,SEEK_SET);
		for(l=m=0;l<length;l+=2) {
rswindell's avatar
rswindell committed
969
			printf("\r%2lu%%  ",l ? (long)(100.0/((float)length/l)) : 0);
deuce's avatar
deuce committed
970