forked from brunonymous/vpopmail
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvpopmail.c
4504 lines (3810 loc) · 124 KB
/
vpopmail.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
/*
* Copyright (C) 2022 TLK Games
* Copyright (C) 2000-2009 Inter7 Internet Technologies, Inc.
*
* 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.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
*/
#include <ctype.h>
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <pwd.h>
#include <signal.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <time.h>
#include <unistd.h>
#include "config.h"
#ifdef HAVE_ERR_H
#include <err.h>
#endif
#include "file_lock.h"
#include "maildirquota.h"
#include "md5.h"
#include "storage.h"
#include "vauth.h"
#include "vlimits.h"
#include "vpopmail.h"
#include "pwstr.h"
#ifdef VPOPMAIL_DEBUG
int show_trace = 0;
int show_query = 0;
int dump_data = 0;
#endif
/* Return value for password strength check */
int ret = 0;
#ifdef POP_AUTH_OPEN_RELAY
/* keep a output pipe to tcp.smtp file */
int tcprules_fdm;
static char relay_tempfile[MAX_BUFF];
#endif
int verrori = 0;
#ifdef USE_ONCHANGE
int allow_onchange = 1;
int call_onchange();
#endif
extern int cdb_seek();
/* Global Flags */
int NoMakeIndex = 0;
int OptimizeAddDomain = 0;
#define PS_TOKENS " \t"
#define CDB_TOKENS ":\n\r"
#ifdef IP_ALIAS_DOMAINS
int host_in_locals(char *domain);
#endif
static char gen_chars[] =
"abcdefghijklmnopqrstuvwxyz"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"0123456789.@!#%*";
static char ok_env_chars[] =
"abcdefghijklmnopqrstuvwxyz"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"1234567890_-.@";
typedef struct defsortrec {
char *key;
char *value;
} sortrec;
/************************************************************************/
void string_list_init(string_list *a, int initial) {
a->count = 0;
a->size = ((initial + 3) / 4) * 4;
if (a->size <= 0) a->size = 4;
a->values = calloc(a->size, sizeof(char *));
if (a->values == NULL) a->size = 0;
}
int string_list_add(string_list *a, const char *value) {
if (a->count >= (a->size - 2)) {
char **new;
a->size += 8;
new = realloc(a->values, a->size * sizeof(char *));
if (new != NULL) {
a->values = new;
} else {
return 0;
}
}
if ((a->values[a->count] = strdup(value)) == NULL) return 0;
a->count++;
return 1;
}
void string_list_free(string_list *a) {
int i;
if (a->values == NULL) return;
for (i = 0; i < a->count; i++) free(a->values[i]);
free(a->values);
}
/************************************************************************/
/*
* Add a domain to the email system
*
* input: domain name
* dir to put the files
* uid and gid to assign to the files
*/
int vadddomain(char *domain, char *dir, uid_t uid, gid_t gid) {
FILE *fs;
int i;
char *domain_hash;
char DomainSubDir[MAX_BUFF];
char dir_control_for_uid[MAX_BUFF];
char tmpbuf[MAX_BUFF];
char Dir[MAX_BUFF];
int call_dir;
string_list aliases;
char *ptld;
size_t sz;
int r;
#ifdef ONCHANGE_SCRIPT
/* Don't execute any implied onchange in called functions */
allow_onchange = 0;
#endif
/* we only do lower case */
lowerit(domain);
/* reject domain names that are too short to be valid */
if (strlen(domain) < 3) return (VA_INVALID_DOMAIN_NAME);
/* reject domain names that exceed our max permitted/storable size */
if (strlen(domain) > MAX_PW_DOMAIN) return (VA_DOMAIN_NAME_TOO_LONG);
/* reject domain names without TLD */
ptld = strrchr(domain, '.');
if (ptld == NULL) return (VA_INVALID_DOMAIN_NAME);
/* reject domain names with invalid TLD size */
sz = domain + strlen(domain) - 1 - ptld;
if ((sz < 2) || (sz > 63)) return (VA_INVALID_DOMAIN_NAME);
/* check invalid email domain characters */
for (i = 0; domain[i] != 0; ++i) {
if (i == 0 && domain[i] == '-') return (VA_INVALID_DOMAIN_NAME);
if (isalnum((int)domain[i]) == 0 && domain[i] != '-' && domain[i] != '.') {
return (VA_INVALID_DOMAIN_NAME);
}
if ((i > 0) && (domain[i] == '.') && (domain[i - 1] == '.')) {
return (VA_INVALID_DOMAIN_NAME);
}
}
if (domain[i - 1] == '-') return (VA_INVALID_DOMAIN_NAME);
/* after the name is okay, check if it already exists */
if (vget_assign(domain, NULL, 0, NULL, NULL) != NULL) {
return (VA_DOMAIN_ALREADY_EXISTS);
}
/* set our file creation mask for machines where the
* sysadmin has tightened default permissions
*/
umask(VPOPMAIL_UMASK);
/* store the calling directory */
call_dir = open(".", O_RDONLY);
/* go to the directory where our Domains dir is to be stored
* check for error and return error on error
*/
if (chdir(dir) != 0) return (VA_BAD_V_DIR);
/* go into the Domains subdir */
if (chdir(DOMAINS_DIR) != 0) {
/* if it's not there, no problem, just try to create it */
if (mkdir(DOMAINS_DIR, VPOPMAIL_DIR_MODE) != 0) {
fchdir(call_dir);
close(call_dir);
return (VA_CAN_NOT_MAKE_DOMAINS_DIR);
}
/* set the permisions on our new Domains dir */
chown(DOMAINS_DIR, uid, gid);
/* now try moving into the Domains subdir again */
if (chdir(DOMAINS_DIR) != 0) {
fchdir(call_dir);
close(call_dir);
return (VA_BAD_D_DIR);
}
}
/* since domains can be added under any /etc/passwd
* user, we have to create dir_control information
* for each user/domain combination
*/
snprintf(dir_control_for_uid, sizeof(dir_control_for_uid), "dom_%lu",
(long unsigned)uid);
/* work out a subdir name for the domain
* Depending on how many domains we have, it may need to be hashed
*/
open_big_dir(dir_control_for_uid, uid, gid);
domain_hash = next_big_dir(uid, gid);
close_big_dir(dir_control_for_uid, uid, gid);
if (strlen(domain_hash) > 0) {
snprintf(DomainSubDir, sizeof(DomainSubDir), "%s/%s", domain_hash, domain);
} else {
snprintf(DomainSubDir, sizeof(DomainSubDir), "%s", domain);
}
/* Check to make sure length of the dir isnt going to exceed
* the maximum storable size
* We dont want to start creating dirs and putting entries in
* the assign file etc if the path is going to be too long
*/
if (strlen(dir) + strlen(DOMAINS_DIR) + strlen(DomainSubDir) > MAX_PW_DIR) {
/* back out of changes made so far */
dec_dir_control(dir_control_for_uid, uid, gid);
fchdir(call_dir);
close(call_dir);
return (VA_DIR_TOO_LONG);
}
/* Make the subdir for the domain */
if (r_mkdir(DomainSubDir, uid, gid) != 0) {
/* back out of changes made so far */
dec_dir_control(dir_control_for_uid, uid, gid);
fchdir(call_dir);
close(call_dir);
return (VA_COULD_NOT_MAKE_DOMAIN_DIR);
}
if (chdir(DomainSubDir) != 0) {
/* back out of changes made so far */
vdelfiles(DomainSubDir);
dec_dir_control(dir_control_for_uid, uid, gid);
fchdir(call_dir);
close(call_dir);
return (VA_BAD_D_DIR);
}
/* create the .qmail-default file */
r = snprintf(tmpbuf, sizeof(tmpbuf), "%s/%s/%s/.qmail-default", dir,
DOMAINS_DIR, DomainSubDir);
if (r == -1) {
return (VA_DIR_TOO_LONG);
}
if ((fs = fopen(tmpbuf, "w+")) == NULL) {
/* back out of changes made so far */
chdir(dir);
chdir(DOMAINS_DIR);
if (vdelfiles(DomainSubDir) != 0) {
fprintf(stderr, "Failed to delete directory tree :%s\n", DomainSubDir);
}
dec_dir_control(dir_control_for_uid, uid, gid);
fchdir(call_dir);
close(call_dir);
return (VA_COULD_NOT_OPEN_QMAIL_DEFAULT);
} else {
fprintf(fs, "| %s/bin/vdelivermail '' delete\n", VPOPMAILDIR);
fclose(fs);
}
/* create an entry in the assign file for our new domain */
r = snprintf(tmpbuf, sizeof(tmpbuf), "%s/%s/%s", dir, DOMAINS_DIR,
DomainSubDir);
if (r == -1) {
return (VA_DIR_TOO_LONG);
}
if (add_domain_assign(domain, domain, tmpbuf, uid, gid) != 0) {
/* back out of changes made so far */
chdir(dir);
chdir(DOMAINS_DIR);
if (vdelfiles(DomainSubDir) != 0) {
fprintf(stderr, "Failed to delete directory tree: %s\n", DomainSubDir);
}
dec_dir_control(dir_control_for_uid, uid, gid);
fchdir(call_dir);
close(call_dir);
fprintf(stderr, "Error. Failed to add domain to assign file\n");
return (VA_COULD_NOT_UPDATE_FILE);
}
/* recursively change ownership to new file system entries */
r = snprintf(tmpbuf, sizeof(tmpbuf), "%s/%s/%s", dir, DOMAINS_DIR,
DomainSubDir);
if (r == -1) {
return (VA_DIR_TOO_LONG);
}
r_chown(tmpbuf, uid, gid);
#ifdef USE_ONCHANGE
on_change("add_domain", domain, "-", 0, 0);
#endif
/* ask the authentication module to add the domain entry */
/* until now we checked if domain already exists in cdb and
* setup all dirs, but vauth_adddomain may __fail__ so we need to check
*/
if (vauth_adddomain(domain) != VA_SUCCESS) {
/* ok we have run into problems here. adding domain to auth backend failed
* so now we need to reverse the steps we have already performed above
*/
fprintf(stderr,
"Error. Failed while attempting to add domain to auth backend\n");
chdir(dir);
chdir(DOMAINS_DIR);
if (vdelfiles(DomainSubDir) != 0) {
fprintf(stderr, "Failed to delete directory tree: %s\n", DomainSubDir);
}
dec_dir_control(dir_control_for_uid, uid, gid);
vget_assign(domain, Dir, sizeof(Dir), &uid, &gid);
string_list_init(&aliases, 1);
string_list_add(&aliases, domain);
if (del_domain_assign(aliases.values, 1, domain, Dir, uid, gid) != 0) {
fprintf(stderr,
"Failed while attempting to remove domain from assign file\n");
}
if (del_control(aliases.values, 1) != 0) {
fprintf(stderr,
"Failed while attempting to delete domain from the qmail control "
"files\n");
}
#ifdef USERS_BIG_DIR
if (vdel_dir_control(domain) != 0) {
fprintf(stderr, "Warning: Failed to delete dir_control for %s\n", domain);
}
#endif
/* send a HUP signal to qmail-send process to reread control files */
signal_process("qmail-send", SIGHUP);
fchdir(call_dir);
close(call_dir);
string_list_free(&aliases);
return (VA_NO_AUTH_CONNECTION);
}
/* ask qmail to re-read it's new control files */
if (OptimizeAddDomain == 0) {
signal_process("qmail-send", SIGHUP);
}
#ifdef USE_ONCHANGE
allow_onchange = 1;
on_change("add_domain", domain, "-", 1, 1);
allow_onchange = 0;
#endif
/* return back to the callers directory and return success */
fchdir(call_dir);
close(call_dir);
return (VA_SUCCESS);
}
/************************************************************************/
/* Delete a domain from the entire mail system
*
* If we have problems at any of the following steps, it has been
* decided that the best course of action is to continue rather than
* abort. The idea behind this is to allow the removal of a partially
* installed domain. We will emit warnings should any of the expected
* cleanup steps fail.
*/
int vdeldomain(char *domain) {
struct stat statbuf;
char Dir[MAX_BUFF];
char domain_to_del[MAX_BUFF];
char dircontrol[MAX_BUFF];
#if defined(ONCHANGE_SCRIPT) | defined(ONCHANGE_SCRIPT_BEFORE_AND_AFTER)
char domain_name[MAX_BUFF];
#endif
uid_t uid;
gid_t gid;
string_list aliases;
domain_entry *entry;
int i = 0;
int call_dir;
int r;
/* we always convert domains to lower case */
lowerit(domain);
/* Check the length of the domain to del
* If it exceeds the max storable size,
* then the user has made some sort of error in
* asking to del that domain, because such a domain
* wouldnt be able to exist in the 1st place
*/
if (strlen(domain) > MAX_PW_DOMAIN) return (VA_DOMAIN_NAME_TOO_LONG);
/* now we want to check a couple for things :
* a) if the domain to del exists in the system
* b) if the domain to del is an aliased domain or not
*/
/* Take a backup of the domain we want to del,
* because when we call vget_assign, if the domain
* is an alias, then the domain parameter will be
* rewritten on return as the name of the real domain
*/
snprintf(domain_to_del, sizeof(domain_to_del), "%s", domain);
/* check if the domain exists. If so extract the dir, uid, gid */
if (vget_assign(domain, Dir, sizeof(Dir), &uid, &gid) == NULL) {
return (VA_DOMAIN_DOES_NOT_EXIST);
}
if (strcmp(domain_to_del, domain) != 0) {
/* This is just an alias, so add it to the list of aliases
* that are about to be deleted. It will be the only one
* but I will use the same code as multi domains anyway.
*/
string_list_init(&aliases, 1);
string_list_add(&aliases, domain_to_del);
#ifdef USE_ONCHANGE
r = snprintf(domain_name, MAX_BUFF, "%s alias of %s", domain_to_del,
domain);
if (r == -1) {
return (VA_DIR_TOO_LONG);
}
on_change("del_domain", domain_name, "-", 1, 0);
#endif
} else {
/* this is an NOT aliased domain....
* (aliased domains dont have any filestructure of their own)
*/
/* check if the domain's dir exists */
if (stat(Dir, &statbuf) != 0) {
fprintf(stderr, "Warning: Could not access (%s)\n", Dir);
}
/*
* Michael Bowe 23rd August 2003
*
* at this point, we need to write some code to check if any alias domains
* point to this (real) domain. If we find such aliases, then I guess we
* have a couple of options :
* 1. Abort with an error, saying cant delete domain until all
* aliases are removed 1st (list them)
* 2. Zap all the aliases in additon to this domain
*
* Rick Widmer 28 April 3004
*
* OK. Option 2 won. If this domain has aliases they will all
* be deleted. If you want to warn people about this it should
* be done by the program calling vdeldomain() before you call it.
* You shuould be able to find example code in vdeldomain.c.
*
*/
entry = get_domain_entries(domain);
if (entry == NULL) { // something went wrong
if (verrori) { // could not open file
fprintf(stderr, "%s\n", verror(verrori));
} else { // domain does not exist
fprintf(stderr, "%s\n", verror(VA_DOMAIN_DOES_NOT_EXIST));
}
}
string_list_init(&aliases, 10);
while (entry) {
string_list_add(&aliases, entry->domain);
entry = get_domain_entries(NULL);
}
// Dump the alias list
// for(i=0;i<aliases.count;i++) {
// fprintf(stderr,"alias %s\n", aliases[i]);
// }
#ifdef USE_ONCHANGE
r = snprintf(domain_name, MAX_BUFF, "%s alias of %s", domain_to_del,
domain);
if (r == -1) {
return (VA_DIR_TOO_LONG);
}
on_change("del_domain", domain, "-", 1, 0);
#endif
/* call the auth module to delete the domain from the storage */
/* Note !! We must del domain from auth module __before__ we delete it from
* fs, because deletion from auth module may fail !!!!
*/
/* del a domain from the auth backend which includes :
* - drop the domain's table, or del all users from users table
* - delete domain's entries from lastauth table
* - delete domain's limit's entries
*/
if (vauth_deldomain(domain) != VA_SUCCESS) {
fprintf(stderr,
"Warning: Failed while attempting to delete domain from auth "
"backend\n");
}
/* vdel_limits does the following :
* If we have mysql_limits enabled,
* it will delete the domain's entries from the limits table
* Or if we arent using mysql_limits,
* it will delete the .qmail-admin file from the domain's dir
*
* Note there are inconsistencies in the auth backends. Some
* will run vdel_limits() in vauth_deldomain(), others don't.
* For now, we always run it to be safe. Ultimately, the auth
* backends should to be updated to do this.
*/
vdel_limits(domain);
#ifdef USERS_BIG_DIR
/* delete the dir control info for this domain */
if (vdel_dir_control(domain) != 0) {
fprintf(stderr, "Warning: Failed to delete dir_control for %s\n", domain);
}
#endif
/* Now remove domain from filesystem */
/* if it's a symbolic link just remove the link */
if (readlink(Dir, (char *)&call_dir, sizeof(call_dir)) != -1) {
if (unlink(Dir) != 0) {
fprintf(stderr, "Warning: Failed to remove symlink for %s\n", domain);
}
} else {
/* Not a symlink.. so we have to del some files structure now */
/* zap the domain's directory tree */
call_dir = open(".", O_RDONLY);
if (vdelfiles(Dir) != 0) {
fprintf(stderr, "Warning: Failed to delete directory tree: %s\n",
domain);
}
fchdir(call_dir);
close(call_dir);
}
/* decrement the master domain control info */
snprintf(dircontrol, sizeof(dircontrol), "dom_%lu", (long unsigned)uid);
dec_dir_control(dircontrol, uid, gid);
}
/* The following things need to happen for real and aliased domains */
/* delete the email domain from the qmail control files :
* rcpthosts, morercpthosts, virtualdomains
*/
if (del_control(aliases.values, aliases.count) != 0) {
fprintf(stderr,
"Warning: Failed to delete domain from qmail's control files\n");
}
/* delete the assign file line */
if (del_domain_assign(aliases.values, aliases.count, domain, Dir, uid, gid) !=
0) {
fprintf(stderr, "Warning: Failed to delete domain from the assign file\n");
}
#ifdef SQL_ALIASDOMAINS
/* aliasdomain table will eventually be created */
for (i = 0; i < aliases.count; i++) {
vdelete_sql_aliasdomain(aliases.values[i]);
}
#endif
/* send a HUP signal to qmail-send process to reread control files */
signal_process("qmail-send", SIGHUP);
/* clean up memory used by the alias list */
string_list_free(&aliases);
#ifdef USE_ONCHANGE
on_change("del_domain", domain_name, "-", 0, 1);
#endif
return (VA_SUCCESS);
}
/************************************************************************/
/* get_domain_entries
*
* Parses the qmail users/assign file and returns a domain_entry pointer.
* If first parameter is not NULL, re-open users/assign file and start scanning.
* If first parameter is "", return all entries. Otherwise, only return
* entries where "real" domain matches the first parameter.
* If first parameter is NULL, returns the next line in already open file.
*
* Example 1. Scan through all entries.
* domain_entry *e;
* e = get_domain_entries ("");
* while (e) {
* printf ("Alias: %s Real domain: %s uid: %d gid: %d path: %s\n",
* e->domain, e->realdomain, e->uid, e->gid, e->path);
* e = get_domain_entries (NULL);
* }
*
* Example 2. Find all entries (primary and aliases) for domain.com.
* domain_entry *e;
* e = get_domain_entries ("domain.com");
* while (e) {
* printf ("Alias: %s Real domain: %s uid: %d gid: %d path: %s\n",
* e->domain, e->realdomain, e->uid, e->gid, e->path);
* e = get_domain_entries (NULL);
* }
*
*
*/
domain_entry *get_domain_entries(const char *match_real) {
static FILE *fs = NULL;
static char match_buffer[MAX_PW_DOMAIN];
static domain_entry entry;
static char linebuf[MAX_BUFF];
char *p;
if (match_real != NULL) {
if (fs != NULL) fclose(fs);
snprintf(linebuf, sizeof(linebuf), "%s/users/assign", QMAILDIR);
fs = fopen(linebuf, "r");
snprintf(match_buffer, sizeof(match_buffer), "%s", match_real);
vget_assign(match_buffer, NULL, 0, NULL, NULL);
}
if (fs == NULL) {
verrori = VA_CANNOT_READ_ASSIGN;
return NULL;
}
while (fgets(linebuf, sizeof(linebuf), fs) != NULL) {
/* ignore non-domain entries */
if (*linebuf != '+') continue;
entry.domain = strtok(linebuf + 1, ":");
if (entry.domain == NULL) continue;
/* ignore entries without '.' in them */
if (strchr(entry.domain, '.') == NULL) continue;
entry.realdomain = strtok(NULL, ":");
if (entry.realdomain == NULL) continue;
/* remove trailing '-' from entry.domain */
if (entry.realdomain <= entry.domain + 2 || *(entry.realdomain - 2) != '-')
continue;
*(entry.realdomain - 2) = '\0';
if ((p = strtok(NULL, ":")) == NULL) continue;
entry.uid = atoi(p);
if ((p = strtok(NULL, ":")) == NULL) continue;
entry.gid = atoi(p);
entry.path = strtok(NULL, ":");
if (entry.path == NULL) continue;
if (!*match_buffer || (strcmp(match_buffer, entry.realdomain) == 0))
return &entry;
}
/* reached end of file, so we're done */
fclose(fs);
fs = NULL;
return NULL;
}
/************************************************************************/
/*
* Add a virtual domain user
*/
int vadduser(char *username, char *domain, char *password, char *gecos,
int apop) {
char Dir[MAX_BUFF];
char *user_hash;
int call_dir;
uid_t uid = VPOPMAILUID;
gid_t gid = VPOPMAILGID;
struct vlimits limits;
char quota[50];
/* defauldelivery patch */
FILE *fs;
char tmpbuf[MAX_BUFF], tmpbuf2[MAX_BUFF];
char ch, defaultdelivery_file[MAX_BUFF];
FILE *defaultdelivery;
int default_delivery_option;
#ifdef DEFAULT_DELIVERY
default_delivery_option = 1;
#else
default_delivery_option = 0;
#endif
/* end defauldelivery patch */
#ifdef USE_ONCHANGE
char user_domain[MAX_BUFF];
#endif
#ifdef USE_ONCHANGE
int temp_onchange;
temp_onchange = allow_onchange;
allow_onchange = 0;
#endif
/* check gecos for : characters - bad */
if (strchr(gecos, ':') != 0) return (VA_BAD_CHAR);
if (strlen(username) > MAX_PW_NAME) return (VA_USER_NAME_TOO_LONG);
#ifdef USERS_BIG_DIR
if (strlen(username) == 1) return (VA_ILLEGAL_USERNAME);
#endif
if (strlen(domain) > MAX_PW_DOMAIN) return (VA_DOMAIN_NAME_TOO_LONG);
if (strlen(domain) < 3) return (VA_INVALID_DOMAIN_NAME);
#ifdef CLEAR_PASS
if (strlen(password) > MAX_PW_CLEAR_PASSWD) return (VA_PASSWD_TOO_LONG);
#endif
if (strlen(gecos) > MAX_PW_GECOS) return (VA_GECOS_TOO_LONG);
umask(VPOPMAIL_UMASK);
lowerit(username);
lowerit(domain);
if (is_username_valid(username) != 0) return (VA_ILLEGAL_USERNAME);
if (is_domain_valid(domain) != 0) return (VA_INVALID_DOMAIN_NAME);
if (vauth_getpw(username, domain) != NULL) return (VA_USERNAME_EXISTS);
/* lookup the home dir, uid and gid for the domain */
if (vget_assign(domain, Dir, sizeof(Dir), &uid, &gid) == NULL) {
return (VA_DOMAIN_DOES_NOT_EXIST);
}
/* make sure we can load domain limits for default quota */
if (vget_limits(domain, &limits) != 0) {
return (VA_CANNOT_READ_LIMITS);
}
/* Check password strength */
ret = pw_strength(password);
if (ret != 1) return ret;
/* record the dir where the vadduser command was run from */
call_dir = open(".", O_RDONLY);
/* go to the domain's home dir (ie test it exists) */
/* would a stat be a better option here? */
if (chdir(Dir) != 0) {
close(call_dir);
return (VA_BAD_D_DIR);
}
/* create dir for the the user */
if ((user_hash = make_user_dir(username, domain, uid, gid)) == NULL) {
fchdir(call_dir);
close(call_dir);
if (verrori != 0)
return (verrori);
else
return (VA_BAD_U_DIR);
}
#ifdef USE_ONCHANGE
snprintf(user_domain, MAX_BUFF, "%s@%s", username, domain);
on_change("add_user", user_domain, "-", 0, 0);
#endif
/* add the user to the auth backend */
/* NOTE: We really need to update this method to include the quota. */
if (vauth_adduser(username, domain, password, gecos, user_hash, apop) != 0) {
fprintf(stderr, "Failed while attempting to add user to auth backend\n");
/* back out of changes made so far */
chdir(Dir);
if (strlen(user_hash) > 0) {
chdir(user_hash);
}
vdelfiles(username);
fchdir(call_dir);
close(call_dir);
return (VA_NO_AUTH_CONNECTION);
}
if (limits.defaultquota > 0) {
if (limits.defaultmaxmsgcount > 0)
snprintf(quota, sizeof(quota), "%" PRIu64 "S,%" PRIu64 "C",
limits.defaultquota, limits.defaultmaxmsgcount);
else
snprintf(quota, sizeof(quota), "%" PRIu64 "S", limits.defaultquota);
} else {
if (limits.defaultmaxmsgcount > 0)
snprintf(quota, sizeof(quota), "%" PRIu64 "C", limits.defaultmaxmsgcount);
else
strcpy(quota, "NOQUOTA");
}
if (vsetuserquota(username, domain, quota) == VA_USER_DOES_NOT_EXIST) {
/* server with replication, need to wait and try again */
sleep(5);
vsetuserquota(username, domain, quota);
}
#ifdef SQWEBMAIL_PASS
{
/* create the sqwebmail-pass file in the user's maildir
* This file contains a copy of the user's crypted password
*/
struct vqpasswd *mypw;
mypw = vauth_getpw(username, domain);
if (mypw != NULL) {
vsqwebmail_pass(mypw->pw_dir, mypw->pw_passwd, uid, gid);
}
}
#endif
#ifdef ENABLE_AUTH_LOGGING
if (vset_lastauth(username, domain, NULL_REMOTE_IP) != 0) {
/* should we back out of all the work we have done so far? */
fchdir(call_dir);
close(call_dir);
fprintf(stderr, "Failed to create create lastauth entry\n");
return (VA_NO_AUTH_CONNECTION);
}
#endif
/* jump back into the dir from which the vadduser was run */
fchdir(call_dir);
close(call_dir);
#ifdef USE_ONCHANGE
allow_onchange = temp_onchange;
snprintf(user_domain, MAX_BUFF, "%s@%s", username, domain);
on_change("add_user", user_domain, "-", 1, 1);
allow_onchange = 1;
#endif
/************************** defauldelivery patch ****************************************************************/
if (default_delivery_option == 1) {
/* create the .qmail file */
snprintf(tmpbuf, sizeof(tmpbuf), "%s/%s/%s/.qmail", Dir, user_hash, username);
if ( (fs = fopen(tmpbuf, "w+"))==NULL) vexit(VA_COULD_NOT_OPEN_DOT_QMAIL);
/* setup the permission of the .qmail file */
chown(tmpbuf, uid, gid);
chmod(tmpbuf, 0600);
/* Copy the content of control/defaultdelivery into ~userhomedir/.qmail */
snprintf(defaultdelivery_file, sizeof(defaultdelivery_file), "%s/control/defaultdelivery", QMAILDIR);
defaultdelivery = fopen(defaultdelivery_file, "r");
if( defaultdelivery == NULL )
{
printf("\nERROR: Missing %s/control/defaultdelivery file.\n", QMAILDIR);
printf("To create a %s/control/defaultdelivery type:\n", QMAILDIR);
printf("echo \"| %s/bin/vdelivermail '' delete\" > %s/control/defaultdelivery\n\n", VPOPMAILDIR, QMAILDIR);
vexit(EXIT_FAILURE);
}
// is_vdelivermail = 1 if defaultdelivery already contains vdelivermail
int is_vdelivermail = 0;
while((fgets(tmpbuf2, MAX_BUFF, defaultdelivery)!=NULL)) {
if(strstr(tmpbuf2, "vdelivermail")!=NULL) {
is_vdelivermail = 1;
break;
}
}
rewind(defaultdelivery);
while ( ( ch = fgetc(defaultdelivery) ) != EOF ) fputc(ch, fs);
fclose(defaultdelivery); // close control/defaultdelivery
fclose(fs); // close .qmail
// if defaultdelivery already contains vdelivermail remove the .qmail file
if (is_vdelivermail == 1) remove(tmpbuf);
}
/*********************** end defauldelivery patch *****************************************************************/
return (VA_SUCCESS);
}
/************************************************************************/
char randltr(void) {
static const char saltchar[] =
"./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
return saltchar[(rand() % 64)];
}
/************************************************************************/
/*
* encrypt a password
* Input
* clearpass = pointer to clear text password
* ssize = size of the crypted pointer buffer
*
* Output
* copies the encrypted password into the crypted
* character pointer
*
* Return code:
* VA_CRYPT_FAILED = encryption failed
* VA_SUCCESS = 0 = encryption success
*
*/
int mkpasswd3(char *clearpass, char *crypted, int ssize) {
char *tmpstr;
char salt[12];
static int seeded = 0;
if (!seeded) {
seeded = 1;
srand(time(NULL) ^ (getpid() << 15));
}
#if defined(SHA512_PASSWORDS)
salt[0] = '$';
salt[1] = '6';
salt[2] = '$';
salt[3] = randltr();
salt[4] = randltr();
salt[5] = randltr();
salt[6] = randltr();
salt[7] = randltr();
salt[8] = randltr();
salt[9] = randltr();
salt[10] = randltr();
salt[11] = 0;
#elif defined(MD5_PASSWORDS)
salt[0] = '$';
salt[1] = '1';
salt[2] = '$';
salt[3] = randltr();
salt[4] = randltr();
salt[5] = randltr();
salt[6] = randltr();
salt[7] = randltr();
salt[8] = randltr();
salt[9] = randltr();
salt[10] = randltr();
salt[11] = 0;
#else
salt[0] = randltr();
salt[1] = randltr();
salt[2] = 0;
#endif
tmpstr = crypt(clearpass, salt);