forked from brunonymous/vpopmail
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvcdb.c
1327 lines (1112 loc) · 33 KB
/
vcdb.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) 1999-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
*/
/******************************************************************************
**
** Change a domain's password file to a CDB database
**
** Chris Johnson, July 1998
**
******************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <pwd.h>
#include <sys/time.h>
#include <time.h>
#include <utime.h>
#include <sys/types.h>
#include "config.h"
#ifdef TINYCDB
#include <cdb.h>
#else
#include <cdbmake.h>
#endif
#include "vpopmail.h"
#include "vauth.h"
#include "vcdb.h"
#include "file_lock.h"
#include "vlimits.h"
#define TOKENS " \n"
#ifdef TINYCDB
typedef uint32_t uint32;
#endif
char *dc_filename(char *domain, uid_t uid, gid_t gid);
void vcdb_strip_char( char *instr );
char sqlerr[MAX_BUFF] = "";
char *last_query = NULL;
extern int cdb_seek();
static char vpasswd_file[MAX_BUFF];
static char vpasswd_bak_file[MAX_BUFF];
static char vpasswd_cdb_file[MAX_BUFF];
static char vpasswd_cdb_tmp_file[MAX_BUFF];
static char vpasswd_lock_file[MAX_BUFF];
static char vpasswd_dir[MAX_BUFF];
static char TmpBuf1[MAX_BUFF];
#ifdef TINYCDB
int make_vpasswd_cdb(char *domain)
{
struct cdb_make cdbm;
char pwline[MAX_BUFF_CDB];
char Dir[156];
char *key;
char *data;
char *ptr;
long unsigned keylen,datalen;
FILE *pwfile;
int tmpfile;
uid_t uid;
gid_t gid;
char *tmpstr;
mode_t oldmask;
/* If we don't optimize the index this time, just return */
if ( NoMakeIndex == 1 ) return(0);
if ((pwfile = fopen(vpasswd_file,"r")) == NULL) {
return(-1);
}
/* temporarily set umask (no group/other access) and open temp file */
oldmask = umask(VPOPMAIL_UMASK);
tmpfile = open(vpasswd_cdb_tmp_file, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
umask(oldmask);
if (tmpfile == -1) {
fprintf(stderr,"Error: could not create/open temporary file\n");
return(-1);
}
if (cdb_make_start(&cdbm, tmpfile) != 0) {
fprintf(stderr,"Error: could not initialise cdb\n");
return(-1);
}
/* creation */
fgets(pwline,MAX_BUFF_CDB,pwfile);
while (!feof(pwfile)) {
key = pwline; ptr = pwline;
while (*ptr != ':') { ptr++; }
*ptr = 0; data = ptr; data++;
while (*ptr != '\n') { ptr++; }
*ptr = 0;
keylen = strlen(key); datalen = strlen(data);
#ifdef VPOPMAIL_DEBUG
fprintf (stderr,"Got entry: keylen = %lu, key = %s\n datalen = %lu, data = %s\n",keylen,key,datalen,data);
#endif
if (cdb_make_add(&cdbm, key, keylen, data, datalen) != 0) {
fprintf(stderr,"Error: could not add cdb entry\n");
return(-1);
}
fgets(pwline,MAX_BUFF_CDB,pwfile);
}
fclose(pwfile);
if (cdb_make_finish(&cdbm) != 0) {
fprintf(stderr,"Error: could not write cdb file\n");
return(-1);
}
if (close(tmpfile) == -1) {
fprintf(stderr,"Error 24: error with close()\n");
return(-1);
}
if (rename(vpasswd_cdb_tmp_file,vpasswd_cdb_file)) {
fprintf(stderr,
"Error 25: could not rename cdb.tmp to vpasswd.cdb\n");
return(-1);
}
tmpstr = vget_assign(domain, Dir, 156, &uid, &gid );
if (chown(vpasswd_cdb_file, uid, gid) == -1) fprintf(stderr, "error!\n");
if (chown(vpasswd_lock_file, uid, gid) == -1) fprintf(stderr, "error!\n");
if (chown(vpasswd_file, uid, gid) == -1) fprintf(stderr, "error!\n");
return 0;
}
#else
int make_vpasswd_cdb(char *domain)
{
char pwline[MAX_BUFF_CDB];
char packbuf[8];
char *key;
char *data;
char *ptr;
int i,j,h;
int len;
long unsigned keylen,datalen;
uint32 pos,op;
struct cdbmake cdbm;
FILE *pwfile, *tmfile;
char Dir[156];
uid_t uid;
gid_t gid;
char *tmpstr;
mode_t oldmask;
/* If we don't optimize the index this time, just return */
if ( NoMakeIndex == 1 ) return(0);
if ((pwfile = fopen(vpasswd_file,"r")) == NULL) {
return(-1);
}
cdbmake_init(&cdbm);
/* temporarily set umask (no group/other access) and open temp file */
oldmask = umask(VPOPMAIL_UMASK);
tmfile = fopen(vpasswd_cdb_tmp_file,"w");
umask(oldmask);
if (tmfile == NULL) {
fprintf(stderr,"Error: could not create/open temporary file\n");
return(-1);
}
for (i=0; i < (int)sizeof(cdbm.final); i++) {
if (putc(' ',tmfile) == EOF) {
fprintf(stderr,"Error:error writing temp file\n");
return(-1);
}
}
pos = sizeof(cdbm.final);
/* creation **/
fgets(pwline,MAX_BUFF_CDB,pwfile);
while (!feof(pwfile)) {
key = pwline; ptr = pwline;
while (*ptr != ':') { ptr++; }
*ptr = 0; data = ptr; data++;
while (*ptr != '\n') { ptr++; }
*ptr = 0;
keylen = strlen(key); datalen = strlen(data);
#ifdef VPOPMAIL_DEBUG
fprintf (stderr,"Got entry: keylen = %lu, key = %s\n datalen = %lu, data = %s\n",keylen,key,datalen,data);
#endif
cdbmake_pack(packbuf, (uint32)keylen);
cdbmake_pack(packbuf + 4, (uint32)datalen);
if (fwrite(packbuf,1,8,tmfile) < 8) {
fprintf(stderr,"Error: error writing temp file\n");
return(-1);
}
h = CDBMAKE_HASHSTART;
for (i=0; i < (int)keylen; i++) {
h = cdbmake_hashadd(h,key[i]);
if (putc(key[i],tmfile) == EOF) {
fprintf (stderr,"Error: error temp file\n");
return(-1);
}
}
for (i=0; i < (int)datalen; i++) {
if (putc(data[i],tmfile) == EOF) {
fprintf (stderr,"Error: write error temp file");
return(-1);
}
}
if (!cdbmake_add(&cdbm,h,pos,malloc)) {
fprintf(stderr, "Error: out of memory\n");
return(-1);
}
op = pos;
pos += (uint32)8;
pos += (uint32)keylen;
pos += (uint32)datalen;
if (pos < op) {
fprintf(stderr,"Error: too much data\n");
return(-1);
}
if (!cdbmake_split(&cdbm,malloc)) {
fprintf(stderr,"Error: out of memory\n");
return(-1);
}
fgets(pwline,MAX_BUFF_CDB,pwfile);
free(cdbm.split);
}
fclose(pwfile);
if (!cdbmake_split(&cdbm,malloc)) {
fprintf(stderr, "Error: out of memory\n");
return(-1);
}
for (i=0; i < 256; i++) {
len = cdbmake_throw(&cdbm,pos,i);
for (j=0; j < len; j++) {
cdbmake_pack(packbuf,cdbm.hash[j].h);
cdbmake_pack(packbuf + 4, cdbm.hash[j].p);
if (fwrite(packbuf,1,8,tmfile) < 8) {
fprintf(stderr,"Error 1: error temp file\n");
return(-1);
}
op = pos;
pos += (uint32)8;
if (pos < op) {
fprintf (stderr, "Error 12: too much data\n");
return(-1);
}
}
}
if (fflush(tmfile) == EOF) {
fprintf (stderr,"Error 20: write error temp file\n");
return(-1);
}
rewind(tmfile);
if (fwrite(cdbm.final,1,sizeof(cdbm.final),tmfile)<sizeof(cdbm.final)){
fprintf(stderr,"Error 21: write error temp file\n");
return(-1);
}
if (fflush(tmfile) == EOF) {
fprintf(stderr,"Error 22: write error temp file\n");
return(-1);
}
if (close(fileno(tmfile)) == -1) {
fprintf(stderr,"Error 24: error with close()\n");
return(-1);
}
if (rename(vpasswd_cdb_tmp_file,vpasswd_cdb_file)) {
fprintf(stderr,
"Error 25: could not rename cdb.tmp to vpasswd.cdb\n");
return(-1);
}
free(cdbm.head);
free(cdbm.split);
tmpstr = vget_assign(domain, Dir, 156, &uid, &gid );
chown(vpasswd_cdb_file, uid, gid);
chown(vpasswd_lock_file, uid, gid);
chown(vpasswd_file, uid, gid);
return 0;
}
#endif
struct vqpasswd *vauth_getpw(char *user, char *domain)
{
char in_domain[156];
static struct vqpasswd pwent;
static char line[2048];
char *ptr = NULL, *uid = NULL, *gid = NULL;
uid_t myuid;
uid_t tuid;
gid_t tgid;
uint32 dlen;
int pwf;
#ifdef FILE_LOCKING
int lock_fd;
#endif
verrori = 0;
lowerit(user);
lowerit(domain);
if (vget_assign(domain,NULL,0,&tuid,&tgid) == NULL) {
/* domain does not exist */
return(NULL);
}
myuid = geteuid();
if ( myuid != 0 && myuid != tuid ) {
return(NULL);
}
strncpy( in_domain, domain, sizeof(in_domain));
in_domain[sizeof(in_domain)-1] = '\0'; /* ensure NULL termination */
if (set_vpasswd_files( in_domain ) == -1) {
return (NULL);
}
if ((pwf = open(vpasswd_cdb_file,O_RDONLY)) < 0 ) {
#ifdef FILE_LOCKING
if ( (lock_fd=open(vpasswd_lock_file, O_WRONLY|O_CREAT, S_IRUSR|S_IWUSR)) < 0) {
return(NULL);
}
get_write_lock( lock_fd );
#endif
make_vpasswd_cdb(domain);
#ifdef FILE_LOCKING
unlock_lock(lock_fd, 0, SEEK_SET, 0);
close(lock_fd);
#endif
if ((pwf = open(vpasswd_cdb_file,O_RDONLY)) < 0 ) {
return(NULL);
}
}
strncpy(line,user,sizeof(line));
strncat(line,":",sizeof(line)-strlen(line)-1);
ptr = line;
while (*ptr != ':') { ptr++; }
ptr++;
switch (cdb_seek(pwf,user,strlen(user),&dlen)) {
case -1:
case 0:
close(pwf);
return NULL;
}
#ifdef TINYCDB
if (cdb_bread(pwf, ptr, dlen) != 0) {
close(pwf);
return NULL;
}
#else
if (read(pwf, ptr,dlen) != (int)dlen) {
close(pwf);
return NULL;
}
#endif
close(pwf);
line[(dlen+strlen(user)+1)] = 0;
pwent.pw_name = "";
pwent.pw_passwd = "";
pwent.pw_gecos = "";
pwent.pw_dir = "";
pwent.pw_shell = "";
pwent.pw_clear_passwd = "";
ptr = line;
pwent.pw_name = line;
while (*ptr!=0&&*ptr != ':') { ptr++; }
if ( *ptr!=0 ){ *ptr = 0; ptr++; pwent.pw_passwd = ptr; }
while (*ptr!=0&&*ptr != ':') { ptr++; }
if ( *ptr!=0 ){ *ptr = 0; ptr++; uid = ptr; }
while (*ptr!=0&&*ptr != ':') { ptr++; }
if ( *ptr!=0 ){ *ptr = 0; ptr++; gid = ptr; }
while (*ptr!=0&&*ptr != ':') { ptr++; }
if ( *ptr!=0 ){ *ptr = 0; ptr++; pwent.pw_gecos = ptr; }
while (*ptr!=0&&*ptr != ':') { ptr++; }
if ( *ptr!=0 ){ *ptr = 0; ptr++; pwent.pw_dir = ptr; }
while (*ptr!=0&&*ptr != ':') { ptr++; }
if ( *ptr!=0 ){ *ptr = 0; ptr++; pwent.pw_shell = ptr; }
while (*ptr!=0&&*ptr != ':') { ptr++; }
if ( *ptr!=0 ){ *ptr = 0; ptr++; pwent.pw_clear_passwd = ptr; }
if (!*uid) { pwent.pw_uid = 0; } else { pwent.pw_uid = atoi(uid); }
if (!*gid) { pwent.pw_gid = 0; } else { pwent.pw_gid = atoi(gid); }
vlimits_setflags (&pwent, in_domain);
#ifdef VPOPMAIL_DEBUG
if( dump_data ) {
fprintf (stderr,"vgetpw: db: results: pw_name = %s\n",pwent.pw_name);
fprintf (stderr," pw_passwd = %s\n",pwent.pw_passwd);
fprintf (stderr," pw_uid = %d\n",pwent.pw_uid);
fprintf (stderr," pw_gid = %d\n",pwent.pw_gid);
fprintf (stderr," pw_flags = %d\n",pwent.pw_flags);
fprintf (stderr," pw_gecos = %s\n",pwent.pw_gecos);
fprintf (stderr," pw_dir = %s\n",pwent.pw_dir);
fprintf (stderr," pw_shell = %s\n",pwent.pw_shell);
}
#endif
return(&pwent);
}
struct vqpasswd *vauth_getall(char *domain, int first, int sortit)
{
static FILE *fsv = NULL;
struct vqpasswd *tmpwd;
if (set_vpasswd_files( domain ) == -1) {
return (NULL);
}
if ( first == 1 ) {
if ( fsv != NULL ) fclose(fsv);
if (set_vpasswd_files( domain ) == -1) {
return (NULL);
}
if ((fsv = fopen(vpasswd_file, "r")) == NULL) return(NULL);
} else if ( fsv == NULL ) {
return(NULL);
}
tmpwd = vgetent(fsv);
if ( tmpwd == NULL ) {
fclose(fsv);
fsv = NULL;
}
if(tmpwd) vlimits_setflags(tmpwd,domain) ;
return(tmpwd);
}
void vauth_end_getall()
{
}
int set_vpasswd_files( char *domain )
{
char *tmpstr;
uid_t uid;
gid_t gid;
int r;
char Dir[156];
vset_default_domain( domain );
tmpstr = vget_assign(domain, Dir, 156, &uid, &gid );
memset(vpasswd_dir, 0, MAX_BUFF);
memset(vpasswd_file, 0, MAX_BUFF);
memset(vpasswd_cdb_file, 0, MAX_BUFF);
memset(vpasswd_cdb_tmp_file, 0, MAX_BUFF);
memset(vpasswd_lock_file, 0, MAX_BUFF);
if ( domain == NULL || domain[0] == 0 ) {
snprintf(vpasswd_dir, MAX_BUFF, "%s/users", VPOPMAILDIR);
} else {
snprintf(vpasswd_dir, MAX_BUFF, "%s", Dir);
}
r = snprintf(vpasswd_file, MAX_BUFF, "%s/%s", vpasswd_dir,VPASSWD_FILE);
if (r == -1) {
return -1;
}
r = snprintf(vpasswd_bak_file, MAX_BUFF, "%s/%s.%d",
vpasswd_dir,VPASSWD_BAK_FILE, getpid());
if (r == -1) {
return -1;
}
r = snprintf(vpasswd_cdb_file, MAX_BUFF,
"%s/%s", vpasswd_dir,VPASSWD_CDB_FILE);
if (r == -1) {
return -1;
}
r = snprintf(vpasswd_cdb_tmp_file, MAX_BUFF,
"%s/%s",vpasswd_dir,VPASSWD_CDB_TMP_FILE);
if (r == -1) {
return -1;
}
r = snprintf(vpasswd_lock_file, MAX_BUFF,
"%s/%s", vpasswd_dir,VPASSWD_LOCK_FILE);
if (r == -1) {
return -1;
}
return 0;
}
int vauth_adduser(char *user, char *domain, char *pass, char *gecos, char *dir, int apop )
{
static char tmpbuf1[MAX_BUFF_CDB];
static char tmpbuf2[MAX_BUFF_CDB];
char *tmpstr;
int added = 0;
FILE *fs1;
FILE *fs2;
#ifdef FILE_LOCKING
int fd3;
#endif
/* do not trod on the vpasswd file */
if ( strcmp( "vpasswd", user ) == 0 ) {
return( VA_ILLEGAL_USERNAME );
}
if (set_vpasswd_files( domain ) == -1) {
return (-1);
}
/* if the gecos field is null, set it to user name */
if ( gecos==0 || gecos[0]==0) gecos=user;
vcdb_strip_char( gecos );
#ifdef FILE_LOCKING
fd3 = open(vpasswd_lock_file, O_WRONLY | O_CREAT, S_IRUSR|S_IWUSR);
if ( get_write_lock(fd3) < 0 ) return(-2);
#endif
fs1 = fopen(vpasswd_bak_file, "w+");
if ( (fs2 = fopen(vpasswd_file, "r+")) == NULL ) {
fs2 = fopen(vpasswd_file, "w+");
}
if ( fs1 == NULL || fs2 == NULL ) {
if ( fs1 != NULL ) fclose(fs1);
if ( fs2 != NULL ) fclose(fs2);
#ifdef FILE_LOCKING
unlock_lock(fd3, 0, SEEK_SET, 0);
close(fd3);
#endif
return(-1);
}
while (fgets(tmpbuf1,MAX_BUFF_CDB,fs2)!=NULL){
strncpy(tmpbuf2, tmpbuf1, MAX_BUFF_CDB);
tmpstr = strtok(tmpbuf2,":");
if ( added == 0 && strcmp(user, tmpstr) < 0 ) {
added = 1;
vauth_adduser_line( fs1, user, pass, domain,gecos,dir, apop);
}
fputs(tmpbuf1, fs1);
}
if ( added == 0 ) {
vauth_adduser_line( fs1, user, pass, domain,gecos,dir,apop);
}
fclose(fs1);
fclose(fs2);
rename(vpasswd_bak_file, vpasswd_file);
make_vpasswd_cdb(domain);
#ifdef FILE_LOCKING
unlock_lock(fd3, 0, SEEK_SET, 0);
close(fd3);
#endif
return(0);
}
int vauth_adddomain( char *domain )
{
return(0);
}
int vauth_deldomain( char *domain )
{
return(0);
}
int vauth_deluser( char *user, char *domain )
{
static char tmpbuf1[MAX_BUFF_CDB];
static char tmpbuf2[MAX_BUFF_CDB];
char *tmpstr;
FILE *fs1;
FILE *fs2;
#ifdef FILE_LOCKING
int fd3;
#endif
if (set_vpasswd_files( domain ) == -1) {
return (-1);
}
#ifdef FILE_LOCKING
fd3 = open(vpasswd_lock_file, O_WRONLY | O_CREAT, S_IRUSR|S_IWUSR);
if ( get_write_lock(fd3) < 0 ) return(-2);
#endif
fs1 = fopen(vpasswd_bak_file, "w+");
if ( (fs2 = fopen(vpasswd_file, "r+")) == NULL ) {
fs2 = fopen(vpasswd_file, "w+");
}
if ( fs1 == NULL || fs2 == NULL ) {
if ( fs1 != NULL ) fclose(fs1);
if ( fs2 != NULL ) fclose(fs2);
#ifdef FILE_LOCKING
unlock_lock(fd3, 0, SEEK_SET, 0);
close(fd3);
#endif
return(-1);
}
while (fgets(tmpbuf1,MAX_BUFF_CDB,fs2)!=NULL){
strncpy(tmpbuf2, tmpbuf1, MAX_BUFF_CDB);
tmpstr = strtok(tmpbuf2,":");
if ( strcmp(user, tmpstr) != 0) {
fputs(tmpbuf1, fs1);
}
}
fclose(fs1);
fclose(fs2);
rename(vpasswd_bak_file, vpasswd_file);
make_vpasswd_cdb(domain);
#ifdef FILE_LOCKING
unlock_lock(fd3, 0, SEEK_SET, 0);
close(fd3);
#endif
return(0);
}
/* Utility function to set the users quota
*
* Calls underlying vauth_getpw and vauth_setpw
* to actually change the users information
*/
int vauth_setquota( char *username, char *domain, char *quota)
{
struct vqpasswd *vpw;
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(quota) > MAX_PW_QUOTA ) return(VA_QUOTA_TOO_LONG);
vpw = vauth_getpw( username, domain );
if ( vpw==NULL ) return(VA_USER_DOES_NOT_EXIST);
vpw->pw_shell = quota;
return(vauth_setpw(vpw,domain));
}
int vauth_setpw( struct vqpasswd *inpw, char *domain )
{
static char tmpbuf1[MAX_BUFF_CDB];
static char tmpbuf2[MAX_BUFF_CDB];
#ifdef USE_ONCHANGE
char user_domain[MAX_BUFF];
#endif
char *tmpstr;
FILE *fs1;
FILE *fs2;
#ifdef FILE_LOCKING
int fd3;
#endif
uid_t myuid;
uid_t uid;
gid_t gid;
int ret;
#ifdef USE_ONCHANGE
snprintf( user_domain, MAX_BUFF, "%s@%s", inpw->pw_name, domain);
on_change("mod_user", user_domain, "-", 0, 0);
#endif
ret = vcheck_vqpw(inpw, domain);
if ( ret != 0 ) return(ret);
/* get the owner of the domain */
vget_assign(domain,NULL,0,&uid,&gid);
/* get the current effective user */
myuid = geteuid();
/*
* if it is not the owner, vpopmail or root
* then reject this operation
*/
if ( myuid != 0 && myuid != uid ) {
return(VA_BAD_UID);
}
if (set_vpasswd_files( domain ) == -1) {
return (-1);
}
#ifdef FILE_LOCKING
fd3 = open(vpasswd_lock_file, O_WRONLY | O_CREAT, S_IRUSR|S_IWUSR);
if ( get_write_lock(fd3) < 0 ) return(-2);
#endif
fs1 = fopen(vpasswd_bak_file, "w+");
if ( (fs2 = fopen(vpasswd_file, "r+")) == NULL ) {
fs2 = fopen(vpasswd_file, "w+");
}
if ( fs1 == NULL || fs2 == NULL ) {
if ( fs1 != NULL ) fclose(fs1);
if ( fs2 != NULL ) fclose(fs2);
#ifdef FILE_LOCKING
unlock_lock(fd3, 0, SEEK_SET, 0);
close(fd3);
#endif
return(-1);
}
vcdb_strip_char( inpw->pw_gecos );
#ifndef CLEAR_PASS
vcdb_strip_char( inpw->pw_clear_passwd );
#endif
while (fgets(tmpbuf1,MAX_BUFF_CDB,fs2)!=NULL){
strncpy(tmpbuf2, tmpbuf1, MAX_BUFF_CDB);
tmpstr = strtok(tmpbuf2,":\n");
if ( strcmp(inpw->pw_name, tmpstr) != 0) {
fputs(tmpbuf1, fs1);
} else {
#ifndef CLEAR_PASS
fprintf(fs1, "%s:%s:%d:%d:%s:%s:%s\n",
inpw->pw_name,
inpw->pw_passwd,
inpw->pw_uid,
inpw->pw_gid,
inpw->pw_gecos,
inpw->pw_dir,
inpw->pw_shell);
#else
fprintf(fs1, "%s:%s:%d:%d:%s:%s:%s:%s\n",
inpw->pw_name,
inpw->pw_passwd,
inpw->pw_uid,
inpw->pw_gid,
inpw->pw_gecos,
inpw->pw_dir,
inpw->pw_shell, inpw->pw_clear_passwd);
#endif
}
}
fclose(fs1);
fclose(fs2);
rename(vpasswd_bak_file, vpasswd_file);
make_vpasswd_cdb(domain);
#ifdef FILE_LOCKING
unlock_lock(fd3, 0, SEEK_SET, 0);
close(fd3);
#endif
#ifdef SQWEBMAIL_PASS
tmpstr = vget_assign(domain, NULL, 0, &uid, &gid );
vsqwebmail_pass( inpw->pw_dir, inpw->pw_passwd, uid, gid);
#endif
#ifdef USE_ONCHANGE
snprintf( user_domain, MAX_BUFF, "%s@%s", inpw->pw_name, domain);
on_change("mod_user", user_domain, "-", 1, 1);
#endif
return(0);
}
int vauth_adduser_line( FILE *fs1,
char *user,
char *pass,
char *domain,
char *gecos,
char *dir, int apop )
{
char Dir[156];
uid_t uid;
gid_t gid;
char crypted[100];
if ( vget_assign(domain, Dir, 156, &uid, &gid ) == NULL ) {
strcpy(Dir, VPOPMAILDIR);
}
if ( pass[0] != 0 ) {
mkpasswd3(pass,crypted, 100);
} else {
crypted[0] = 0;
}
fprintf(fs1,"%s:", user );
if ( apop == USE_POP ) fprintf(fs1, "%s:1:", crypted);
else fprintf(fs1, "%s:2:", crypted);
fprintf(fs1, "0:%s:%s", gecos, Dir);
if ( strlen(domain) <= 0 ) {
if ( strlen(dir) > 0 ) {
fprintf(fs1, "/users/%s/%s:", dir, user);
} else {
fprintf(fs1, "/users/%s:", user);
}
} else {
if ( strlen(dir) > 0 ) {
fprintf(fs1,"/%s/%s:", dir,user);
} else {
fprintf(fs1, "/%s:", user);
}
}
fprintf(fs1, "NOQUOTA");
#ifndef CLEAR_PASS
fprintf(fs1, "\n");
#else
fprintf(fs1, ":%s\n", pass);
#endif
return(0);
}
int vmkpasswd( char *domain )
{
#ifdef FILE_LOCKING
int fd3;
#endif
char Dir[156];
uid_t uid;
gid_t gid;
char *tmpstr;
getcwd(TmpBuf1, MAX_BUFF);
tmpstr = vget_assign(domain, Dir, 156, &uid, &gid );
if ( chdir(Dir) != 0 ) return(VA_BAD_DIR);
lowerit(domain);
if (set_vpasswd_files( domain ) == -1) {
return (-1);
}
#ifdef FILE_LOCKING
fd3 = open(vpasswd_lock_file, O_WRONLY | O_CREAT, S_IRUSR|S_IWUSR);
if ( get_write_lock(fd3) < 0 ) return(-2);
#endif
make_vpasswd_cdb(domain);
#ifdef FILE_LOCKING
unlock_lock(fd3, 0, SEEK_SET, 0);
close(fd3);
#endif
return(0);
}
/* Verify the connection to the authentication database */
int vauth_open( int will_update ) {
#ifdef VPOPMAIL_DEBUG
show_trace = ( getenv("VPSHOW_TRACE") != NULL);
show_query = ( getenv("VPSHOW_QUERY") != NULL);
dump_data = ( getenv("VPDUMP_DATA") != NULL);
#endif
#ifdef VPOPMAIL_DEBUG
if( show_trace ) {
fprintf( stderr, "vauth_open()\n");
}
#endif
/*
* If the connection to this authentication database can fail
* you should test access here. If it works, return 0, else
* return VA_NO_AUTH_CONNECTION. You can also set the string
* sqlerr to some short descriptive text about the problem,
* and allocate a much longer string, pointed to by last_query
* that can be displayed in an error message returned because
* of this problem.
*
*/
return( 0 );
}
void vclose()
{
}
#ifdef IP_ALIAS_DOMAINS
int vget_ip_map( char *ip, char *domain, int domain_size)
{
FILE *fs;
char tmpbuf[156];
char *tmpstr;
if ( ip == NULL || strlen(ip) <= 0 ) return(-1);
/* open the ip_alias_map file */
snprintf(tmpbuf, 156, "%s/%s", VPOPMAILDIR, IP_ALIAS_MAP_FILE);
if ( (fs = fopen(tmpbuf,"r")) == NULL ) return(-1);
while( fgets(tmpbuf, 156, fs) != NULL ) {
tmpstr = strtok(tmpbuf, IP_ALIAS_TOKENS);
if ( tmpstr == NULL ) continue;
if ( strcmp(ip, tmpstr) != 0 ) continue;
tmpstr = strtok(NULL, IP_ALIAS_TOKENS);
if ( tmpstr == NULL ) continue;
strncpy(domain, tmpstr, domain_size);
fclose(fs);
return(0);
}
fclose(fs);
return(-1);
}
/*
* Add an ip to domain mapping
* It will remove any duplicate entry before adding it
*
*/
int vadd_ip_map( char *ip, char *domain)
{
FILE *fs;
char tmpbuf[156];
if ( ip == NULL || strlen(ip) <= 0 ) return(-1);
if ( domain == NULL || strlen(domain) <= 0 ) return(-10);
vdel_ip_map( ip, domain );
snprintf(tmpbuf, 156, "%s/%s", VPOPMAILDIR, IP_ALIAS_MAP_FILE);
if ( (fs = fopen(tmpbuf,"a+")) == NULL ) return(-1);
fprintf( fs, "%s %s\n", ip, domain);
fclose(fs);
return(0);
}
int vdel_ip_map( char *ip, char *domain)
{
FILE *fs;
FILE *fs1;
char file1[156];
char file2[156];
char tmpbuf[156];
char tmpbuf1[156];
char *ip_f;
char *domain_f;
if ( ip == NULL || strlen(ip) <= 0 ) return(-1);
if ( domain == NULL || strlen(domain) <= 0 ) return(-1);
snprintf(file1, 156, "%s/%s", VPOPMAILDIR, IP_ALIAS_MAP_FILE);
if ( (fs = fopen(file1,"r")) == NULL ) return(-1);