chk_ar.cpp 16.4 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
/* chk_ar.cpp */

/* Synchronet ARS checking routine */

/* $Id$ */

/****************************************************************************
 * @format.tab-size 4		(Plain Text/Source Code File Header)			*
 * @format.use-tabs true	(see http://www.synchro.net/ptsc_hdr.html)		*
 *																			*
rswindell's avatar
rswindell committed
11
 * Copyright 2009 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
38
39
 *																			*
 * 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.	*
 ****************************************************************************/

#include "sbbs.h"

rswindell's avatar
rswindell committed
40
bool sbbs_t::ar_exp(const uchar **ptrptr, user_t* user, client_t* client)
41
{
42
	bool	result,_not,_or,equal;
43
44
	uint	i,n,artype,age;
	ulong	l;
45
	struct tm tm;
rswindell's avatar
rswindell committed
46
	const char*	p;
47
48
49
50
51
52
53
54

	result = true;

	for(;(**ptrptr);(*ptrptr)++) {

		if((**ptrptr)==AR_ENDNEST)
			break;

55
		_not=_or=equal = false;
56
57

		if((**ptrptr)==AR_OR) {
58
			_or=true;
59
60
61
			(*ptrptr)++; }
		
		if((**ptrptr)==AR_NOT) {
62
			_not=true;
63
64
65
66
67
68
			(*ptrptr)++; }

		if((**ptrptr)==AR_EQUAL) {
			equal=true;
			(*ptrptr)++; }

69
		if((result && _or) || (!result && !_or))
70
71
72
73
			break;

		if((**ptrptr)==AR_BEGNEST) {
			(*ptrptr)++;
rswindell's avatar
rswindell committed
74
			if(ar_exp(ptrptr,user,client))
75
				result=!_not;
76
			else
77
				result=_not;
78
79
80
81
82
83
84
85
86
87
88
89
90
91
			while((**ptrptr)!=AR_ENDNEST && (**ptrptr)) /* in case of early exit */
				(*ptrptr)++;
			if(!(**ptrptr))
				break;
			continue; }

		artype=(**ptrptr);
		switch(artype) {
			case AR_ANSI:				/* No arguments */
			case AR_RIP:
			case AR_WIP:
			case AR_LOCAL:
			case AR_EXPERT:
			case AR_SYSOP:
92
93
			case AR_GUEST:
			case AR_QNODE:
94
95
96
			case AR_QUIET:
			case AR_OS2:
			case AR_DOS:
97
98
99
			case AR_WIN32:
			case AR_UNIX:
			case AR_LINUX:
100
101
102
103
104
105
106
107
108
109
				break;
			default:
				(*ptrptr)++;
				break; }

		n=(**ptrptr);
		i=(*(short *)*ptrptr);
		switch(artype) {
			case AR_LEVEL:
				if((equal && user->level!=n) || (!equal && user->level<n))
110
					result=_not;
111
				else
112
					result=!_not;
113
114
115
116
117
118
119
				if(!result) {
					noaccess_str=text[NoAccessLevel];
					noaccess_val=n; }
				break;
			case AR_AGE:
				age=getage(&cfg,user->birth);
				if((equal && age!=n) || (!equal && age<n))
120
					result=_not;
121
				else
122
					result=!_not;
123
124
125
126
127
128
				if(!result) {
					noaccess_str=text[NoAccessAge];
					noaccess_val=n; }
				break;
			case AR_BPS:
				if((equal && cur_rate!=i) || (!equal && cur_rate<i))
129
					result=_not;
130
				else
131
					result=!_not;
132
133
134
135
136
137
138
				(*ptrptr)++;
				if(!result) {
					noaccess_str=text[NoAccessBPS];
					noaccess_val=i; }
				break;
			case AR_ANSI:
				if(!(user->misc&ANSI))
139
140
					result=_not;
				else result=!_not;
141
142
143
				break;
			case AR_RIP:
				if(!(user->misc&RIP))
144
145
					result=_not;
				else result=!_not;
146
147
148
				break;
			case AR_WIP:
				if(!(user->misc&WIP))
149
150
					result=_not;
				else result=!_not;
151
152
153
				break;
			case AR_OS2:
				#ifndef __OS2__
154
					result=_not;
155
				#else
156
					result=!_not;
157
158
159
				#endif
				break;
			case AR_DOS:
deuce's avatar
deuce committed
160
				result=_not;
161
162
163
				break;
			case AR_WIN32:
				#ifndef _WIN32
164
					result=_not;
165
				#else
166
					result=!_not;
167
168
169
170
				#endif
				break;
			case AR_UNIX:
				#ifndef __unix__
171
					result=_not;
172
				#else
173
					result=!_not;
174
175
176
177
				#endif
				break;
			case AR_LINUX:
				#ifndef __linux__
178
					result=_not;
179
				#else
180
					result=!_not;
181
182
				#endif
				break;
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
			case AR_ACTIVE:
				if(user->misc&(DELETED|INACTIVE))
					result=_not;
				else result=!_not;
				break;
			case AR_INACTIVE:
				if(!(user->misc&INACTIVE))
					result=_not;
				else result=!_not;
				break;
			case AR_DELETED:
				if(!(user->misc&DELETED))
					result=_not;
				else result=!_not;
				break;
198
199
			case AR_EXPERT:
				if(!(user->misc&EXPERT))
200
201
					result=_not;
				else result=!_not;
202
203
204
				break;
			case AR_SYSOP:
				if(!SYSOP)
205
206
					result=_not;
				else result=!_not;
207
				break;
208
209
210
211
212
213
214
215
216
217
			case AR_GUEST:
				if(!(user->rest&FLAG('G')))
					result=_not;
				else result=!_not;
				break;
			case AR_QNODE:
				if(!(user->rest&FLAG('Q')))
					result=_not;
				else result=!_not;
				break;
218
219
			case AR_QUIET:
				if(thisnode.status!=NODE_QUIET)
220
221
					result=_not;
				else result=!_not;
222
223
224
				break;
			case AR_LOCAL:
				if(online!=ON_LOCAL)
225
226
					result=_not;
				else result=!_not;
227
228
229
				break;
			case AR_DAY:
				now=time(NULL);
230
231
232
				localtime_r(&now,&tm);
				if((equal && tm.tm_wday!=(int)n) 
					|| (!equal && tm.tm_wday<(int)n))
233
					result=_not;
234
				else
235
					result=!_not;
236
237
238
239
240
241
242
243
				if(!result) {
					noaccess_str=text[NoAccessDay];
					noaccess_val=n; }
				break;
			case AR_CREDIT:
				l=(ulong)i*1024UL;
				if((equal && user->cdt+user->freecdt!=l)
					|| (!equal && user->cdt+user->freecdt<l))
244
					result=_not;
245
				else
246
					result=!_not;
247
248
249
250
251
252
253
				(*ptrptr)++;
				if(!result) {
					noaccess_str=text[NoAccessCredit];
					noaccess_val=l; }
				break;
			case AR_NODE:
				if((equal && cfg.node_num!=n) || (!equal && cfg.node_num<n))
254
					result=_not;
255
				else
256
					result=!_not;
257
258
259
260
261
262
				if(!result) {
					noaccess_str=text[NoAccessNode];
					noaccess_val=n; }
				break;
			case AR_USER:
				if((equal && user->number!=i) || (!equal && user->number<i))
263
					result=_not;
264
				else
265
					result=!_not;
266
267
268
269
270
271
272
273
274
275
276
				(*ptrptr)++;
				if(!result) {
					noaccess_str=text[NoAccessUser];
					noaccess_val=i; }
				break;
			case AR_GROUP:
				if((equal
					&& (cursubnum>=cfg.total_subs
						|| cfg.sub[cursubnum]->grp!=i))
					|| (!equal && cursubnum<cfg.total_subs
						&& cfg.sub[cursubnum]->grp<i))
277
					result=_not;
278
				else
279
					result=!_not;
280
281
282
283
284
285
286
				(*ptrptr)++;
				if(!result) {
					noaccess_str=text[NoAccessGroup];
					noaccess_val=i+1; }
				break;
			case AR_SUB:
				if((equal && cursubnum!=i) || (!equal && cursubnum<i))
287
					result=_not;
288
				else
289
					result=!_not;
290
291
292
293
294
295
296
				(*ptrptr)++;
				if(!result) {
					noaccess_str=text[NoAccessSub];
					noaccess_val=i+1; }
				break;
			case AR_SUBCODE:
				if(cursubnum>=cfg.total_subs
rswindell's avatar
rswindell committed
297
					|| !findstr_in_string(cfg.sub[cursubnum]->code,(char*)*ptrptr))
298
					result=_not;
299
				else
300
					result=!_not;
301
302
303
304
305
306
307
308
309
310
311
				while(*(*ptrptr))
					(*ptrptr)++;
				if(!result)
					noaccess_str=text[NoAccessSub];
				break;
			case AR_LIB:
				if((equal
					&& (curdirnum>=cfg.total_dirs
						|| cfg.dir[curdirnum]->lib!=i))
					|| (!equal && curdirnum<cfg.total_dirs
						&& cfg.dir[curdirnum]->lib<i))
312
					result=_not;
313
				else
314
					result=!_not;
315
316
317
318
319
320
321
				(*ptrptr)++;
				if(!result) {
					noaccess_str=text[NoAccessLib];
					noaccess_val=i+1; }
				break;
			case AR_DIR:
				if((equal && curdirnum!=i) || (!equal && curdirnum<i))
322
					result=_not;
323
				else
324
					result=!_not;
325
326
327
328
329
330
331
				(*ptrptr)++;
				if(!result) {
					noaccess_str=text[NoAccessDir];
					noaccess_val=i+1; }
				break;
			case AR_DIRCODE:
				if(curdirnum>=cfg.total_dirs
rswindell's avatar
rswindell committed
332
					|| !findstr_in_string(cfg.dir[curdirnum]->code,(char *)*ptrptr))
333
					result=_not;
334
				else
335
					result=!_not;
336
337
338
339
340
341
342
343
				while(*(*ptrptr))
					(*ptrptr)++;
				if(!result)
					noaccess_str=text[NoAccessSub];
				break;
			case AR_EXPIRE:
				now=time(NULL);
				if(!user->expire || now+((long)i*24L*60L*60L)>user->expire)
344
					result=_not;
345
				else
346
					result=!_not;
347
348
349
350
351
352
				(*ptrptr)++;
				if(!result) {
					noaccess_str=text[NoAccessExpire];
					noaccess_val=i; }
				break;
			case AR_RANDOM:
353
				n=sbbs_random(i+1);
354
				if((equal && n!=i) || (!equal && n<i))
355
					result=_not;
356
				else
357
					result=!_not;
358
359
360
361
362
				(*ptrptr)++;
				break;
			case AR_LASTON:
				now=time(NULL);
				if((now-user->laston)/(24L*60L*60L)<(long)i)
363
					result=_not;
364
				else
365
					result=!_not;
366
367
368
369
				(*ptrptr)++;
				break;
			case AR_LOGONS:
				if((equal && user->logons!=i) || (!equal && user->logons<i))
370
					result=_not;
371
				else
372
					result=!_not;
373
374
375
376
				(*ptrptr)++;
				break;
			case AR_MAIN_CMDS:
				if((equal && main_cmds!=i) || (!equal && main_cmds<i))
377
					result=_not;
378
				else
379
					result=!_not;
380
381
382
383
				(*ptrptr)++;
				break;
			case AR_FILE_CMDS:
				if((equal && xfer_cmds!=i) || (!equal && xfer_cmds<i))
384
					result=_not;
385
				else
386
					result=!_not;
387
388
389
390
				(*ptrptr)++;
				break;
			case AR_TLEFT:
				if(timeleft/60<(ulong)n)
391
					result=_not;
392
				else
393
					result=!_not;
394
395
396
397
398
399
				if(!result) {
					noaccess_str=text[NoAccessTimeLeft];
					noaccess_val=n; }
				break;
			case AR_TUSED:
				if((time(NULL)-logontime)/60<(long)n)
400
					result=_not;
401
				else
402
					result=!_not;
403
404
405
406
407
408
				if(!result) {
					noaccess_str=text[NoAccessTimeUsed];
					noaccess_val=n; }
				break;
			case AR_TIME:
				now=time(NULL);
409
410
				localtime_r(&now,&tm);
				if((tm.tm_hour*60)+tm.tm_min<(int)i)
411
					result=_not;
412
				else
413
					result=!_not;
414
415
416
417
418
				(*ptrptr)++;
				if(!result) {
					noaccess_str=text[NoAccessTime];
					noaccess_val=i; }
				break;
419
			case AR_PCR:	/* post/call ratio (by percentage) */
420
				if(user->logons>user->posts
421
					&& (!user->posts || (100/((float)user->logons/user->posts))<(long)n))
422
					result=_not;
423
				else
424
					result=!_not;
425
426
427
428
				if(!result) {
					noaccess_str=text[NoAccessPCR];
					noaccess_val=n; }
				break;
429
			case AR_UDR:	/* up/download byte ratio (by percentage) */
430
431
432
				l=user->dlb;
				if(!l) l=1;
				if(user->dlb>user->ulb
433
					&& (!user->ulb || (100/((float)l/user->ulb))<n))
434
					result=_not;
435
				else
436
					result=!_not;
437
438
439
440
				if(!result) {
					noaccess_str=text[NoAccessUDR];
					noaccess_val=n; }
				break;
441
			case AR_UDFR:	/* up/download file ratio (in percentage) */
442
443
444
				i=user->dls;
				if(!i) i=1;
				if(user->dls>user->uls
445
					&& (!user->uls || (100/((float)i/user->uls))<n))
446
					result=_not;
447
				else
448
					result=!_not;
449
450
451
452
				if(!result) {
					noaccess_str=text[NoAccessUDFR];
					noaccess_val=n; }
				break;
rswindell's avatar
rswindell committed
453
454
455
456
457
458
459
			case AR_ULS:
				if((equal && user->uls!=i) || (!equal && user->uls<i))
					result=_not;
				else
					result=!_not;
				(*ptrptr)++;
				break;
460
461
462
463
464
465
466
467
468
			case AR_ULK:
				if((equal && (user->ulb/1024)!=i) || (!equal && (user->ulb/1024)<i))
					result=_not;
				else
					result=!_not;
				(*ptrptr)++;
				break;
			case AR_ULM:
				if((equal && (user->ulb/(1024*1024))!=i) || (!equal && (user->ulb/(1024*1024))<i))
rswindell's avatar
rswindell committed
469
470
471
472
473
474
475
476
477
478
479
480
					result=_not;
				else
					result=!_not;
				(*ptrptr)++;
				break;
			case AR_DLS:
				if((equal && user->dls!=i) || (!equal && user->dls<i))
					result=_not;
				else
					result=!_not;
				(*ptrptr)++;
				break;
481
482
483
484
485
486
487
488
489
			case AR_DLK:
				if((equal && user->dlb/1024!=i) || (!equal && user->dlb/1024<i))
					result=_not;
				else
					result=!_not;
				(*ptrptr)++;
				break;
			case AR_DLM:
				if((equal && user->dlb/(1024*1024)!=i) || (!equal && user->dlb/(1024*1024)<i))
rswindell's avatar
rswindell committed
490
491
492
493
494
					result=_not;
				else
					result=!_not;
				(*ptrptr)++;
				break;
495
496
497
			case AR_FLAG1:
				if((!equal && !(user->flags1&FLAG(n)))
					|| (equal && user->flags1!=FLAG(n)))
498
					result=_not;
499
				else
500
					result=!_not;
501
502
503
504
505
506
507
				if(!result) {
					noaccess_str=text[NoAccessFlag1];
					noaccess_val=n; }
				break;
			case AR_FLAG2:
				if((!equal && !(user->flags2&FLAG(n)))
					|| (equal && user->flags2!=FLAG(n)))
508
					result=_not;
509
				else
510
					result=!_not;
511
512
513
514
515
516
517
				if(!result) {
					noaccess_str=text[NoAccessFlag2];
					noaccess_val=n; }
				break;
			case AR_FLAG3:
				if((!equal && !(user->flags3&FLAG(n)))
					|| (equal && user->flags3!=FLAG(n)))
518
					result=_not;
519
				else
520
					result=!_not;
521
522
523
524
525
526
527
				if(!result) {
					noaccess_str=text[NoAccessFlag3];
					noaccess_val=n; }
				break;
			case AR_FLAG4:
				if((!equal && !(user->flags4&FLAG(n)))
					|| (equal && user->flags4!=FLAG(n)))
528
					result=_not;
529
				else
530
					result=!_not;
531
532
533
534
535
536
537
				if(!result) {
					noaccess_str=text[NoAccessFlag4];
					noaccess_val=n; }
				break;
			case AR_REST:
				if((!equal && !(user->rest&FLAG(n)))
					|| (equal && user->rest!=FLAG(n)))
538
					result=_not;
539
				else
540
					result=!_not;
541
542
543
544
545
546
547
				if(!result) {
					noaccess_str=text[NoAccessRest];
					noaccess_val=n; }
				break;
			case AR_EXEMPT:
				if((!equal && !(user->exempt&FLAG(n)))
					|| (equal && user->exempt!=FLAG(n)))
548
					result=_not;
549
				else
550
					result=!_not;
551
552
553
554
555
556
				if(!result) {
					noaccess_str=text[NoAccessExempt];
					noaccess_val=n; }
				break;
			case AR_SEX:
				if(user->sex!=n)
557
					result=_not;
558
				else
559
					result=!_not;
560
561
562
				if(!result) {
					noaccess_str=text[NoAccessSex];
					noaccess_val=n; }
563
564
565
				break; 
			case AR_SHELL:
				if(user->shell>=cfg.total_shells
rswindell's avatar
rswindell committed
566
					|| !findstr_in_string(cfg.shell[user->shell]->code,(char*)*ptrptr))
567
568
569
570
571
572
					result=_not;
				else
					result=!_not;
				while(*(*ptrptr))
					(*ptrptr)++;
				break;
573
			case AR_PROT:
rswindell's avatar
rswindell committed
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
				if(client!=NULL)
					p=client->protocol;
				else
					p=user->modem;
				if(!findstr_in_string(p,(char*)*ptrptr))
					result=_not;
				else
					result=!_not;
				while(*(*ptrptr))
					(*ptrptr)++;
				break;
			case AR_HOST:
				if(client!=NULL)
					p=client->host;
				else
					p=user->comp;
				if(!findstr_in_string(p,(char*)*ptrptr))
					result=_not;
				else
					result=!_not;
				while(*(*ptrptr))
					(*ptrptr)++;
				break;
			case AR_IP:
				if(client!=NULL)
					p=client->addr;
				else
					p=user->note;
				if(!findstr_in_string(p,(char*)*ptrptr))
603
604
605
606
607
608
					result=_not;
				else
					result=!_not;
				while(*(*ptrptr))
					(*ptrptr)++;
				break;
609
610
		}
	}
611
612
613
	return(result);
}

rswindell's avatar
rswindell committed
614
bool sbbs_t::chk_ar(const uchar *ar, user_t* user, client_t* client)
615
{
616
	const uchar *p;
617
618
619
620

	if(ar==NULL)
		return(true);
	p=ar;
rswindell's avatar
rswindell committed
621
	return(ar_exp(&p,user,client));
622
623
624
625
626
627
628
629
630
631
632
633
}


/****************************************************************************/
/* This function fills the usrsub, usrsubs, usrgrps, curgrp, and cursub     */
/* variables based on the security clearance of the current user (useron)   */
/****************************************************************************/
void sbbs_t::getusrsubs()
{
    uint i,j,k,l;

	for(j=0,i=0;i<cfg.total_grps;i++) {
rswindell's avatar
rswindell committed
634
		if(!chk_ar(cfg.grp[i]->ar,&useron,&client))
635
636
637
			continue;
		for(k=0,l=0;l<cfg.total_subs;l++) {
			if(cfg.sub[l]->grp!=i) continue;
rswindell's avatar
rswindell committed
638
			if(!chk_ar(cfg.sub[l]->ar,&useron,&client))
639
				continue;
640
641
			usrsub[j][k++]=l; 
		}
642
643
644
		usrsubs[j]=k;
		if(!k)          /* No subs accessible in group */
			continue;
645
646
		usrgrp[j++]=i; 
	}
647
	usrgrps=j;
648
649
	if(usrgrps==0)
		return;
650
651
652
653
654
655
656
657
658
659
660
661
662
663
	while((curgrp>=usrgrps || !usrsubs[curgrp]) && curgrp) curgrp--;
	while(cursub[curgrp]>=usrsubs[curgrp] && cursub[curgrp]) cursub[curgrp]--;
}

/****************************************************************************/
/* This function fills the usrdir, usrdirs, usrlibs, curlib, and curdir     */
/* variables based on the security clearance of the current user (useron)   */
/****************************************************************************/
void sbbs_t::getusrdirs()
{
    uint i,j,k,l;

	if(useron.rest&FLAG('T')) {
		usrlibs=0;
664
665
		return; 
	}
666
	for(j=0,i=0;i<cfg.total_libs;i++) {
rswindell's avatar
rswindell committed
667
		if(!chk_ar(cfg.lib[i]->ar,&useron,&client))
668
669
670
			continue;
		for(k=0,l=0;l<cfg.total_dirs;l++) {
			if(cfg.dir[l]->lib!=i) continue;
rswindell's avatar
rswindell committed
671
			if(!chk_ar(cfg.dir[l]->ar,&useron,&client))
672
673
674
675
676
				continue;
			usrdir[j][k++]=l; }
		usrdirs[j]=k;
		if(!k)          /* No dirs accessible in lib */
			continue;
677
678
		usrlib[j++]=i; 
	}
679
	usrlibs=j;
680
681
	if(usrlibs==0)
		return;
682
683
684
685
	while((curlib>=usrlibs || !usrdirs[curlib]) && curlib) curlib--;
	while(curdir[curlib]>=usrdirs[curlib] && curdir[curlib]) curdir[curlib]--;
}

686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
uint sbbs_t::getusrgrp(uint subnum)
{
	uint	ugrp;

	if(subnum==INVALID_SUB)
		return(0);

	if(usrgrps<=0)
		return(0);

	for(ugrp=0;ugrp<usrgrps;ugrp++)
		if(usrgrp[ugrp]==cfg.sub[subnum]->grp)
			break;

	return(ugrp+1);
}

uint sbbs_t::getusrsub(uint subnum)
{
	uint	usub;
	uint	ugrp;

	ugrp = getusrgrp(subnum);
	if(ugrp<=0)
		return(0);
	
	for(usub=0;usub<usrsubs[ugrp];usub++)
		if(usrsub[ugrp][usub]==subnum)
			break;

	return(usub+1);
}

719
720
int sbbs_t::dir_op(uint dirnum)
{
rswindell's avatar
rswindell committed
721
	return(SYSOP || (cfg.dir[dirnum]->op_ar[0] && chk_ar(cfg.dir[dirnum]->op_ar,&useron,&client)));
722
723
724
}