-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathchkuser.c
1167 lines (937 loc) · 32.6 KB
/
chkuser.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
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
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
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
672
673
674
675
676
677
678
679
680
681
682
683
684
685
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
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
*
* 'chkuser.c' v.2.0.9
* for qmail/netqmail > 1.0.3 and vpopmail > 5.3.x
*
* Author: Antonio Nati tonix@interazioni.it
* All rights on this software and
* the identifying words chkusr and chkuser reserved by the author
*
* This software may be freely used, modified and distributed,
* but this lines must be kept in every original or derived version.
* Original author "Antonio Nati" and the web URL
* "http://www.interazioni.it/opensource"
* must be indicated in every related work or web page
*
*/
#include <pwd.h>
/* required by vpopmail */
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "case.h"
#include "dns.h"
#include "env.h"
#include "ipme.h"
#include "now.h"
#include "str.h"
#include "open.h"
#include "subfd.h"
#include "substdio.h"
#include "stralloc.h"
#include "vpopmail.h"
#include "vauth.h"
#include "vpopmail_config.h"
#include "chkuser.h"
#include "chkuser_settings.h"
#include "utf8.h"
#if defined _exit
#undef _exit
#endif
extern void flush();
extern void out (char *s);
extern int addrallowed();
extern unsigned int byte_rchr();
extern int vmaildir_readquota(const char *dir, const char *quota);
extern char *remotehost;
extern char *remoteip;
extern char *remoteinfo;
extern char *relayclient;
extern char *fakehelo;
extern void die_nomem();
#define DIE_NOMEM() die_nomem()
#if defined CHKUSER_DEBUG
#if defined CHKUSER_DEBUG_STDERR
#define CHKUSER_DBG(a) write (STDERR_FILENO, a, strlen (a))
#define CHKUSER_DBG_INT(a) { int x; char str[30]; sprintf (str, "%d", a); write (STDERR_FILENO, str, strlen (str));}
#else
#define CHKUSER_DBG(a) write (STDOUT_FILENO, a, strlen (a))
#define CHKUSER_DBG_INT(a) { int x; char str[30]; sprintf (str, "%d", a); write (STDOUT_FILENO, str, strlen (str));}
#endif
#else
#define CHKUSER_DBG(a) /* DBG dummy */
#define CHKUSER_DBG_INT(a) /* DBG dummy */
#endif
static int intrusion_threshold_reached = 0;
static int first_time_init_flag = 1;
static int recipients = 0;
static int wrong_recipients = 0;
static stralloc user = {0};
static stralloc domain = {0};
static stralloc domain_path = {0};
static stralloc tmp_path = {0};
static stralloc alias_path = {0};
#if defined CHKUSER_IDENTIFY_REMOTE_VARIABLE
static char *identify_remote;
#endif
#if defined CHKUSER_ENABLE_EXTENSIONS
#define CHKUSER_ENABLE_USERS_EXTENSIONS
#endif
#if defined CHKUSER_ENABLE_LISTS
#define CHKUSER_ENABLE_EZMLM_LISTS
#endif
#if defined CHKUSER_EXTENSION_DASH
#define CHKUSER_USERS_DASH CHKUSER_EXTENSION_DASH
#endif
#if defined CHKUSER_ENABLE_VALIAS
#error "chkuser setting error: CHKUSER_ENABLE_VALIAS has been substituted by VALIAS (within vpopmail includes); you don't need anymore this define"
#endif
#if defined CHKUSER_ENABLE_VAUTH_OPEN
#error "chkuser setting error: CHKUSER_ENABLE_VAUTH_OPEN has been substituted by CHKUSER_ENABLE_VAUTH_OPEN_CALL; edit chkuser_settings.h and change your settings"
#endif
#if defined CHKUSER_ENABLE_VAUTH_OPEN_CALL
static int db_already_open = 0;
#endif
#if defined CHKUSER_ALWAYS_ON && defined CHKUSER_STARTING_VARIABLE
#error "chkuser setting error: CHKUSER_ALWAYS_ON and CHKUSER_STARTING_VARIABLE are mutually esclusive. Edit your chkuser_settings.h and disable one of them"
#endif
static int starting_value = 0;
#if defined CHKUSER_STARTING_VARIABLE
static char *starting_string = 0;
#endif
#if defined CHKUSER_EXTRA_MUSTAUTH_VARIABLE
static int mustauth_value = 0;
#endif
#if defined CHKUSER_RCPT_LIMIT_VARIABLE
static char *maxrcpt_string = 0;
static int maxrcpt_limit = 0;
static int maxrcpt_limit_reached = 0;
#endif
#if defined CHKUSER_DISABLE_VARIABLE
static char *chkuser_disable_variable = 0;
#endif
#if defined CHKUSER_WRONGRCPT_LIMIT_VARIABLE
static char *maxwrongrcpt_string = 0;
static int maxwrongrcpt_limit = 0;
static int maxwrongrcpt_limit_reached = 0;
#endif
#if defined CHKUSER_MBXQUOTA_VARIABLE
static char *maxmbxquota_string = 0;
static int maxmbxquota_limit = 0;
#endif
static unsigned int sender_nocheck = 0;
static char *sender_nocheck_variable = 0;
#if defined CHKUSER_SENDER_FORMAT || defined CHKUSER_SENDER_MX
static stralloc sender_user = {0};
static stralloc sender_domain = {0};
#endif
#if defined CHKUSER_ENABLE_DOUBLEBOUNCE_VARIABLE
static unsigned int enable_doublebounce = 0;
#endif
#if defined CHKUSER_ERROR_DELAY
static int chkuser_delay_interval = CHKUSER_ERROR_DELAY * 1000;
#define CHKUSER_DELAY() chkuser_delay()
void chkuser_delay (void) {
usleep (chkuser_delay_interval);
#if defined CHKUSER_ERROR_DELAY_INCREASE
chkuser_delay_interval += CHKUSER_ERROR_DELAY_INCREASE * 1000;
#endif
}
#if defined CHKUSER_RCPT_DELAY_ANYERROR
#define CHKUSER_RCPT_DELAY_ANY() chkuser_delay()
#else
#define CHKUSER_RCPT_DELAY_ANY() /* no delay for any error */
#endif
#if defined CHKUSER_SENDER_DELAY_ANYERROR
#define CHKUSER_SENDER_DELAY_ANY() chkuser_delay()
#else
#define CHKUSER_SENDER_DELAY_ANY() /* no delay for any error */
#endif
#else
#define CHKUSER_DELAY() /* no delay */
#define CHKUSER_RCPT_DELAY_ANY() /* no delay */
#define CHKUSER_SENDER_DELAY_ANY() /* no delay */
#endif
#if defined CHKUSER_ENABLE_LOGGING
static stralloc logstr = { 0 };
static void chkuser_commonlog (char *sender, char *rcpt, char *title, char *description) {
substdio_puts (subfderr, "CHKUSER ");
substdio_puts (subfderr, title);
substdio_puts (subfderr, ": from <");
substdio_puts (subfderr, sender);
substdio_puts (subfderr, "|remoteinfo/auth:" );
if (remoteinfo) {
substdio_puts (subfderr, remoteinfo);
}
substdio_puts (subfderr, "|chkuser-identify:" );
#if defined CHKUSER_IDENTIFY_REMOTE_VARIABLE
if (identify_remote) substdio_puts (subfderr, identify_remote);
#endif
substdio_puts (subfderr, "> remote <helo:");
if (fakehelo) substdio_puts (subfderr, fakehelo);
substdio_puts (subfderr, "|remotehostname:" );
if (remotehost) substdio_puts (subfderr, remotehost);
substdio_puts (subfderr, "|remotehostip:" );
if (remoteip) substdio_puts (subfderr, remoteip);
substdio_puts (subfderr, "> rcpt <");
substdio_puts (subfderr, rcpt);
substdio_puts (subfderr, "> : ");
substdio_puts (subfderr, description);
substdio_puts (subfderr, "\n");
substdio_flush (subfderr);
}
#else
#define chkuser_commonlog(a,b,c,d) /* no log */
#endif
#if defined CHKUSER_SENDER_FORMAT || defined CHKUSER_RCPT_FORMAT
extern int smtputf8;
/**************************************************************************
*
* Performs mail sender/rcpt address verification
*
**************************************************************************/
static int make_mav(stralloc *user, stralloc *domain) {
int x;
/*
* If the remote server advertises SMTPUTF8 in MAIL FROM,
* then allow utf8 names not containing invalid characters.
*/
if ( smtputf8 && ( // if it advertises SMTPUTF8 in the MAIL FROM and
(!is_valid_utf8(user->s) || !is_valid_utf8(domain->s)) // if it has chars in user name or domain name that are not UTF8 compliant
#ifdef CHKUSER_INVALID_UTF8_CHARS
// or it has valid UTF8 chars that we don't accept
|| (strpbrk(user->s, CHKUSER_INVALID_UTF8_CHARS) || strpbrk(domain->s, CHKUSER_INVALID_UTF8_CHARS))
#endif
)
) return 0; // then flag the email address as not valid
/*
* else if the remote server does NOT advertise SMTPUTF8 in MAIL FROM,
* then allow only ASCII characters as usual, but accept the CHKUSER_ALLOWED_CHARS
* characters in addition to the ASCII set
*/
else if (!smtputf8) {
// user name check
for (x = 0; x < (user->len -1); ++x) {
#ifdef CHKUSER_ALLOWED_CHARS
char *chkuser_allowed_char = strchr(CHKUSER_ALLOWED_CHARS, user->s[x]);
#endif
if ( !isalnum (user->s[x])
#ifdef CHKUSER_ALLOWED_CHARS
&& (chkuser_allowed_char == NULL)
#endif
) return 0; // flag the email address as not valid
}
// domain name check
for (x = 0; x < (domain->len -1); ++x) {
if (!isalnum(domain->s[x]) && (domain->s[x] != '-') && (domain->s[x] != '.')) return 0;
}
}
/* Other checks on domain name validity */
/*
* Be careful, this is a base check
* Minimum is x.xx + ending \0
* Minimum characters needed are 5
*/
#if defined CHKUSER_MIN_DOMAIN_LEN
if (domain->len < (CHKUSER_MIN_DOMAIN_LEN +1)) return 0;
#endif
/*
* This is a safety check
*/
#if defined CHKUSER_MIN_DOMAIN_LEN
if (domain->len < 2) return 0;
#endif
if ( domain->s[0] == '-'
|| domain->s[domain->len -2] == '-'
|| domain->s[0] == '.'
|| domain->s[domain->len -2] == '.'
|| strstr(domain->s, "..") != NULL
|| strstr(domain->s, ".-") != NULL
|| strstr(domain->s, "-.") != NULL
|| strchr(domain->s, '.') == NULL
|| ( !smtputf8 && (strncmp(domain->s, "xn--", 4) == 0) && (strstr(&domain->s[4], "--") != NULL) ) // do no accept punycode if SMTPUTF8 not avaliable
// allowing domains with double hyphen like y--s.co.jp not in 1st char
// || strstr(domain->s, "--") != NULL
) return 0;
return 1;
}
#endif
#if defined CHKUSER_SENDER_MX || defined CHKUSER_RCPT_MX
static unsigned long mx_random;
static ipalloc mx_ip = {0};
static int chkuser_mx_lookup (stralloc *domain) {
int status;
mx_random = now() + getpid();
dns_init(0);
status = dns_mxip (&mx_ip, domain, mx_random);
if (status == DNS_MEM) DIE_NOMEM();
return status;
}
#endif
void chkuser_cleanup (int exit_value) {
#if defined CHKUSER_DB_CLEANUP
vclose ();
#endif
_exit (exit_value);
}
static void first_time_init (void) {
starting_value = 0;
#if defined CHKUSER_ALWAYS_ON
starting_value = 1;
#endif
#if defined CHKUSER_STARTING_VARIABLE
starting_string = env_get (CHKUSER_STARTING_VARIABLE);
if (starting_string) {
if (strcasecmp(starting_string, "ALWAYS") == 0) starting_value = 1;
else if (strcasecmp(starting_string, "DOMAIN") == 0) starting_value = 0;
/*
Edit by Roberto Puzzanghera
It seems like any other definition of starting_string ends up as "DOMAIN".
Instead, if starting_string is otherwise defined, we want to turn off chkuser,
just like if the starting_string is "NONE".
*/
else starting_value = -1;
} else {
starting_string = "";
starting_value = -1;
}
#endif
#if defined CHKUSER_DISABLE_VARIABLE
chkuser_disable_variable = env_get("CHKUSER_DISABLE_VARIABLE");
if (chkuser_disable_variable && env_get(chkuser_disable_variable)) starting_value = -1;
else if (env_get (CHKUSER_DISABLE_VARIABLE)) starting_value = -1;
#endif
#if defined CHKUSER_EXTRA_MUSTAUTH_VARIABLE
if (env_get (CHKUSER_EXTRA_MUSTAUTH_VARIABLE)) {
if (relayclient) mustauth_value = 0;
else mustauth_value = 1;
}
#endif
#if defined CHKUSER_RCPT_LIMIT_VARIABLE
maxrcpt_string = env_get (CHKUSER_RCPT_LIMIT_VARIABLE);
if (maxrcpt_string) {
maxrcpt_limit = atoi (maxrcpt_string);
if (maxrcpt_limit < 1) {
maxrcpt_limit = 0;
}
} else {
maxrcpt_string = "";;
}
#endif
#if defined CHKUSER_WRONGRCPT_LIMIT_VARIABLE
maxwrongrcpt_string = env_get (CHKUSER_WRONGRCPT_LIMIT_VARIABLE);
if (maxwrongrcpt_string) {
maxwrongrcpt_limit = atoi (maxwrongrcpt_string);
if (maxwrongrcpt_limit < 1) {
maxwrongrcpt_limit = 0;
}
} else {
maxwrongrcpt_string = "";
}
#endif
#if defined CHKUSER_MBXQUOTA_VARIABLE
maxmbxquota_string = env_get (CHKUSER_MBXQUOTA_VARIABLE);
if (maxmbxquota_string) {
maxmbxquota_limit = atoi (maxmbxquota_string);
if (maxmbxquota_limit < 1) {
maxmbxquota_limit = 0;
}
} else {
maxmbxquota_string = "";
}
#endif
#if defined CHKUSER_SENDER_NOCHECK_VARIABLE
sender_nocheck_variable = env_get("CHKUSER_SENDER_NOCHECK_VARIABLE");
if (sender_nocheck_variable) {
if (env_get (sender_nocheck_variable)) {
sender_nocheck = 1;
}
else {
sender_nocheck = 0;
}
} else {
if (env_get (CHKUSER_SENDER_NOCHECK_VARIABLE)) {
sender_nocheck = 1;
} else {
sender_nocheck = 0;
}
}
#endif
#if defined CHKUSER_IDENTIFY_REMOTE_VARIABLE
identify_remote = env_get (CHKUSER_IDENTIFY_REMOTE_VARIABLE);
#endif
#if defined CHKUSER_ENABLE_DOUBLEBOUNCE_VARIABLE
if (env_get (CHKUSER_ENABLE_DOUBLEBOUNCE_VARIABLE)) {
enable_doublebounce = 1;
} else {
enable_doublebounce = 0;
}
#endif
if (!stralloc_ready (&user, 300)) DIE_NOMEM();
if (!stralloc_ready (&domain, 500)) DIE_NOMEM();
if (!stralloc_ready (&domain_path, 1000)) DIE_NOMEM();
if (!stralloc_ready (&tmp_path, 1000)) DIE_NOMEM();
if (!stralloc_ready (&alias_path, 1000)) DIE_NOMEM();
first_time_init_flag = 0;
}
/*
* realrcpt ()
*
* Returns:
*
* CHKUSER_OK = 1 = Ok, recipients does exists
*
* 0 = Not in rcpthosts
*
* < 0 various errors
*
*
* Parameters:
* stralloc *sender = sender address
* stralloc *rcpt = rcpt address to check
*
*
*/
static int realrcpt (stralloc *sender, stralloc *rcpt)
{
int count;
int retstat = CHKUSER_KO;
struct vqpasswd *user_passwd = NULL;
int fd_file = -1;
int read_char;
int offset;
char read_buf[1024];
#if defined CHKUSER_ENABLE_UIDGID
uid_t eff_uid;
gid_t eff_gid;
#endif
#if defined CHKUSER_EXTRA_MUSTAUTH_VARIABLE
if (mustauth_value == 1) {
return CHKUSER_ERR_MUSTAUTH;
}
#endif
if (starting_value == -1) {
if (addrallowed()) {
return CHKUSER_OK_NOCHECKALL;
} else {
if (relayclient) {
return CHKUSER_RELAYING;
}
return CHKUSER_NORCPTHOSTS;
}
}
if (intrusion_threshold_reached == 1) {
return CHKUSER_ERR_INTRUSION_THRESHOLD;
}
#if defined CHKUSER_RCPT_LIMIT_VARIABLE
++recipients;
if ((maxrcpt_limit > 0) && (recipients >= maxrcpt_limit)) {
chkuser_commonlog (sender->s, rcpt->s, "intrusion threshold", "max number of allowed rcpt");
intrusion_threshold_reached = 1;
return CHKUSER_ERR_MAXRCPT;
}
#endif
/* Search the '@' character */
count = byte_rchr(rcpt->s,rcpt->len,'@');
if (count < rcpt->len) {
if (!stralloc_copyb (&user, rcpt->s, count)) DIE_NOMEM();
if (!stralloc_copys (&domain, rcpt->s + count + 1)) DIE_NOMEM();
}
else {
if (!stralloc_copys (&user, rcpt->s)) DIE_NOMEM();
domain.len = 0;
}
if (!stralloc_0 (&user)) DIE_NOMEM();
if (!stralloc_0 (&domain)) DIE_NOMEM();
#if defined CHKUSER_ENABLE_UIDGID
/* qmail-smtpd is running now as (effective) qmaild:nofiles */
/* Save the effective UID & GID (qmaild:nofiles) */
eff_uid = geteuid ();
eff_gid = getegid ();
/* Now set new effective UID & GID, getting it from real UID & GID (vpopmail:vchkpw) */
setegid (getgid());
seteuid (getuid());
/* qmail-smtpd is running now as effective vpopmail:vchkpw */
#endif
/*
*
* Now let's start the test/setting suite
*
**/
switch (0) {
case 0:
/* These are some preliminary settings */
case_lowers (user.s);
case_lowers (domain.s);
case 1:
if (domain.len == 1) {
#if defined CHKUSER_DOMAIN_WANTED
retstat = CHKUSER_ERR_DOMAIN_MISSING;
break;
#else
if (!stralloc_copys (&domain, DEFAULT_DOMAIN)) DIE_NOMEM();
if (!stralloc_0 (&domain)) DIE_NOMEM();
#endif
}
case 2:
#if defined CHKUSER_RCPT_FORMAT
if (!env_get ("CHKUSER_RCPT_FORMAT_NOCHECK")) {
if (make_mav(&user, &domain) == 0) {
retstat = CHKUSER_ERR_RCPT_FORMAT;
break;
}
}
#endif
case 3:
if (!addrallowed()) {
#if defined CHKUSER_RCPT_MX
if (!env_get ("CHKUSER_RCPT_MX_NOCHECK")) {
switch (chkuser_mx_lookup(&domain)) {
case DNS_HARD:
retstat = CHKUSER_ERR_RCPT_MX;
break;
case DNS_SOFT:
retstat = CHKUSER_ERR_RCPT_MX_TMP;
break;
}
if (retstat != CHKUSER_KO) {
break;
}
}
#endif
if (relayclient) {
retstat = CHKUSER_RELAYING;
break;
}
retstat = CHKUSER_NORCPTHOSTS;
break;
}
case 4:
#if defined CHKUSER_ENABLE_VGET_REAL_DOMAIN
/* Check if domain is a real domain */
vget_real_domain(domain.s, domain.a);
domain.len = strlen (domain.s) +1;
if (domain.len > (domain.a - 1)) DIE_NOMEM();
#endif
/* Let's get domain's real path */
if (vget_assign(domain.s, domain_path.s, domain_path.a -1, NULL, NULL) == NULL) {
retstat = CHKUSER_OK;
break;
}
domain_path.len = strlen (domain_path.s);
case 5:
/* Check if domain has bouncing enabled */
if (starting_value == 0) {
if (!stralloc_copy (&tmp_path, &domain_path)) DIE_NOMEM();
#if defined CHKUSER_SPECIFIC_BOUNCING
if (!stralloc_cats (&tmp_path, "/")) DIE_NOMEM();
if (!stralloc_cats (&tmp_path, CHKUSER_SPECIFIC_BOUNCING)) DIE_NOMEM();
if (!stralloc_0 (&tmp_path)) DIE_NOMEM();
fd_file = open_read (tmp_path.s);
if (fd_file != -1) {
close (fd_file);
} else {
retstat = CHKUSER_OK_NOCHECKDOMAIN;
break;
}
#else
if (!stralloc_cats (&tmp_path, "/.qmail-default")) DIE_NOMEM();
if (!stralloc_0 (&tmp_path)) DIE_NOMEM();
read_char = 0;
fd_file = open_read (tmp_path.s);
if (fd_file != -1) {
read_char = read (fd_file, read_buf, sizeof(read_buf) - 1);
close (fd_file);
if (read_char < 0) read_char = 0;
}
read_buf[read_char] = 0;
if ( strstr(read_buf, CHKUSER_BOUNCE_STRING) == NULL ) {
retstat = CHKUSER_OK_NOCHECKDOMAIN;
break;
}
#endif
}
case 6:
#if defined CHKUSER_ENABLE_VAUTH_OPEN_CALL
if (db_already_open != 1) {
if (CHKUSER_VAUTH_OPEN_CALL () == 0) {
db_already_open == 1;
} else {
retstat = CHKUSER_ERR_AUTH_RESOURCE;
break;
}
}
#endif
case 7:
#if defined VALIAS
/* Check for aliases/forwards - valias*/
if (valias_select (user.s, domain.s) != NULL) {
retstat = CHKUSER_OK;
break;
}
#endif
case 8:
#if defined CHKUSER_ENABLE_ALIAS
/* Check for aliases/forwards - .qmail.x files */
if (!stralloc_copy (&tmp_path, &user)) DIE_NOMEM();
/* Change all '.' in ':' before continuing on aliases */
for (count = 0; count < tmp_path.len; ++count)
if (*(tmp_path.s + count) == '.') *(tmp_path.s + count) = ':';
if (!stralloc_copy (&alias_path, &domain_path)) DIE_NOMEM();
if (!stralloc_cats (&alias_path, "/.qmail-")) DIE_NOMEM();
if (!stralloc_cats (&alias_path, tmp_path.s)) DIE_NOMEM();
if (!stralloc_0 (&alias_path)) DIE_NOMEM();
fd_file = open_read (alias_path.s);
if (fd_file != -1) {
close (fd_file);
retstat = CHKUSER_OK;
break;
}
#endif
case 9:
#if defined CHKUSER_ENABLE_ALIAS_DEFAULT
if (!stralloc_copy (&tmp_path, &user)) DIE_NOMEM();
/* Change all '.' in ':' before continuing on aliases */
for (count = 0; count < tmp_path.len; ++count)
if (*(tmp_path.s + count) == '.') *(tmp_path.s + count) = ':';
/* Search for the outer '-' character */
for (offset = user.len - 1; offset > 0; --offset) {
if (*(user.s + offset) == CHKUSER_USERS_DASH) {
if (!stralloc_copy (&alias_path, &domain_path)) die_nomem();
if (!stralloc_cats (&alias_path, "/.qmail-")) die_nomem();
if (!stralloc_catb (&alias_path, user.s, offset)) die_nomem();
if (!stralloc_cats (&alias_path, "-default")) die_nomem();
if (!stralloc_0 (&alias_path)) die_nomem();
fd_file = open_read (alias_path.s);
if (fd_file != -1) {
close (fd_file);
retstat = CHKUSER_OK;
break;
}
}
}
if (retstat != CHKUSER_KO) {
break;
}
#endif
case 10:
#if defined CHKUSER_ENABLE_USERS
/* User control: check the existance of a real user */
user_passwd = vauth_getpw (user.s, domain.s);
#if defined CHKUSER_ENABLE_USERS_EXTENSIONS
if (user_passwd == NULL) {
count = 0;
while ((count < (user.len -1)) && (user_passwd == NULL)) {
count += byte_chr(&user.s[count], user.len - count, CHKUSER_USERS_DASH);
if (count < user.len) {
if (!stralloc_copyb (&tmp_path, user.s, count)) DIE_NOMEM();
if (!stralloc_0 (&tmp_path)) DIE_NOMEM();
user_passwd = vauth_getpw (tmp_path.s, domain.s);
++count;
}
}
}
#endif
if (user_passwd != NULL) {
/* If user exists check if he has BOUNCE_MAIL flag set */
if (user_passwd->pw_gid & BOUNCE_MAIL)
retstat = CHKUSER_KO;
else {
retstat = CHKUSER_OK;
#if defined CHKUSER_MBXQUOTA_VARIABLE
if ((maxmbxquota_limit > 0) && (strcasecmp(user_passwd->pw_shell, "NOQUOTA") != 0)) {
if (!stralloc_copys (&tmp_path, user_passwd->pw_dir)) DIE_NOMEM();
if (!stralloc_cats (&tmp_path, "/Maildir")) DIE_NOMEM();
if (!stralloc_0 (&tmp_path)) DIE_NOMEM();
if (vmaildir_readquota(tmp_path.s,format_maildirquota(user_passwd->pw_shell))
>= maxmbxquota_limit) {
retstat = CHKUSER_ERR_MBXFULL;
}
}
#endif
}
break;
}
#endif
case 11:
#if defined CHKUSER_ENABLE_EZMLM_LISTS
/* Let's check for mailing lists */
/* Search for the outer CHKUSER_EZMLM_DASH character */
for (offset = user.len - 2; offset > 0; --offset) {
if (*(user.s + offset) == CHKUSER_EZMLM_DASH) {
if (!stralloc_copy (&tmp_path, &domain_path)) DIE_NOMEM();
if (!stralloc_cats (&tmp_path, "/")) DIE_NOMEM();
if (!stralloc_catb (&tmp_path, user.s, offset)) DIE_NOMEM();
if (!stralloc_cats (&tmp_path, "/editor")) DIE_NOMEM();
if (!stralloc_0 (&tmp_path)) DIE_NOMEM();
fd_file = open_read (tmp_path.s);
if (fd_file != -1) {
close (fd_file);
retstat = CHKUSER_OK;
break;
}
}
}
if (retstat != CHKUSER_KO) {
break;
}
#endif
case 12:
#if defined CHKUSER_ENABLE_MAILMAN_LISTS
/* Let's check for mailing lists */
/* Search for the outer CHKUSER_MAILMAN_DASH character */
for (offset = user.len - 2; offset > 0; --offset) {
if (*(user.s + offset) == CHKUSER_MAILMAN_DASH) {
if (!stralloc_copy (&tmp_path, &domain_path)) DIE_NOMEM();
if (!stralloc_cats (&tmp_path, "/")) DIE_NOMEM();
if (!stralloc_cats (&alias_path, "/.qmail-")) DIE_NOMEM();
if (!stralloc_catb (&tmp_path, user.s, offset)) DIE_NOMEM();
if (!stralloc_0 (&tmp_path)) DIE_NOMEM();
fd_file = open_read (tmp_path.s);
read_char = 0;
if (fd_file != -1) {
read_char = read (fd_file, read_buf, sizeof(read_buf) - 1);
close (fd_file);
if (read_char < 0) read_char = 0;
}
read_buf[read_char] = 0;
if ( strstr(read_buf, CHKUSER_MAILMAN_STRING) == NULL ) {
retstat = CHKUSER_OK;
break;
}
}
}
if (retstat != CHKUSER_KO) {
break;
}
#endif
/*
* Add this code if another case is following
case xx:
code ....
code ....
code ....
code ....
if (xxxxxxxx) {
retstat != CHKUSER_KO)
break;
}
*/
default:
retstat = CHKUSER_KO;
} /* end switch */
#if defined CHKUSER_ENABLE_UIDGID
/* Now switch back effective to saved UID & GID (qmaild:nofiles) */
setegid (eff_gid);
seteuid (eff_uid);
/* qmail-smtpd is running again as (effective) qmaild:nofiles */
#endif
return retstat;
}
/*
* chkuser_realrcpt ()
*
* Returns a simple status:
*
* CHKUSER_OK = 1 = Ok, recipients does exists
*
* CHKUSER_NORCPTHOSTS = Not in rcpthosts
*
* CHKUSER_KO = ERROR
*
*
* Parameters:
* stralloc *sender = sender address
* stralloc *rcpt = rcpt address to check
*
*
*/
int chkuser_realrcpt (stralloc *sender, stralloc *rcpt) {
int retstat;
if (first_time_init_flag) {
first_time_init ();
}
retstat = realrcpt (sender, rcpt);
switch (retstat) {
case CHKUSER_OK:
#if defined CHKUSER_LOG_VALID_RCPT
chkuser_commonlog (sender->s, rcpt->s, "accepted rcpt", "found existing recipient");
#endif
return CHKUSER_OK;
break;
case CHKUSER_OK_NOCHECKALL:
#if defined CHKUSER_LOG_VALID_RCPT
chkuser_commonlog (sender->s, rcpt->s, "accepted any rcpt", "accepted any recipient for any rcpt domain");
#endif
return CHKUSER_OK;
break;
case CHKUSER_OK_NOCHECKDOMAIN:
#if defined CHKUSER_LOG_VALID_RCPT
chkuser_commonlog (sender->s, rcpt->s, "accepted any rcpt", "accepted any recipient for this domain");
#endif
return CHKUSER_OK;
break;
case CHKUSER_RELAYING:
#if defined CHKUSER_LOG_VALID_RCPT
chkuser_commonlog (sender->s, rcpt->s, "relaying rcpt", "client allowed to relay");
#endif
return CHKUSER_RELAYING;
break;
case CHKUSER_NORCPTHOSTS:
chkuser_commonlog (sender->s, rcpt->s, "rejected relaying", "client not allowed to relay");
CHKUSER_RCPT_DELAY_ANY();
out(CHKUSER_NORELAY_STRING);
break;
case CHKUSER_KO:
chkuser_commonlog (sender->s, rcpt->s, "rejected rcpt", "not existing recipient");
CHKUSER_DELAY();
out(CHKUSER_NORCPT_STRING);
break;
case CHKUSER_ERR_AUTH_RESOURCE:
chkuser_commonlog (sender->s, rcpt->s, "no auth resource", "no auth resource available");
CHKUSER_RCPT_DELAY_ANY();
out(CHKUSER_RESOURCE_STRING);
break;
case CHKUSER_ERR_MUSTAUTH:
chkuser_commonlog (sender->s, rcpt->s, "must auth", "sender not authenticated/authorized");
CHKUSER_RCPT_DELAY_ANY();
out(CHKUSER_MUSTAUTH_STRING);
break;