forked from brunonymous/vpopmail
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vmysql.c
2029 lines (1665 loc) · 57.2 KB
/
vmysql.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
*/
/* TODO
Add error result for "unable to read vpopmail.mysql" and return it
*/
#include <pwd.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <string.h>
#include <time.h>
#include <mysql.h>
#include <mysqld_error.h>
#include "config.h"
#include "vpopmail.h"
#include "vauth.h"
#include "vlimits.h"
#include "vmysql.h"
static MYSQL mysql_update;
static MYSQL mysql_read_getall;
#ifdef MYSQL_REPLICATION
static MYSQL mysql_read;
#else
#define mysql_read mysql_update
#endif
static int update_open = 0;
static int read_getall_open = 0;
#ifdef MYSQL_REPLICATION
static int read_open = 0;
#else
#define read_open update_open
#endif
static MYSQL_RES *res_update = NULL;
static MYSQL_RES *res_read_getall = NULL;
#ifdef MYSQL_REPLICATION
static MYSQL_RES *res_read = NULL;
#else
#define res_read res_update
#endif
static MYSQL_ROW row;
static MYSQL_ROW row_getall;
#define SQL_BUF_SIZE 2048
static char SqlBufRead[SQL_BUF_SIZE];
static char SqlBufUpdate[SQL_BUF_SIZE];
#define SMALL_BUFF 200
char IUser[SMALL_BUFF];
char IPass[SMALL_BUFF];
char IGecos[SMALL_BUFF];
char IDir[SMALL_BUFF];
char IShell[SMALL_BUFF];
char IClearPass[SMALL_BUFF];
void vcreate_dir_control(char *domain);
void vcreate_vlog_table();
#ifdef POP_AUTH_OPEN_RELAY
void vcreate_relay_table();
#endif
#ifdef VALIAS
void vcreate_valias_table();
#endif
#ifdef ENABLE_AUTH_LOGGING
void vcreate_lastauth_table();
#endif
char *MYSQL_READ_SERVER;
int MYSQL_READ_PORT;
char *MYSQL_READ_SOCKET;
char *MYSQL_READ_USER;
char *MYSQL_READ_PASSWD;
char *MYSQL_READ_DATABASE;
char *MYSQL_UPDATE_SERVER;
int MYSQL_UPDATE_PORT;
char *MYSQL_UPDATE_SOCKET;
char *MYSQL_UPDATE_USER;
char *MYSQL_UPDATE_PASSWD;
int MYSQL_UPDATE_VPORT;
char *MYSQL_UPDATE_DATABASE;
/************************************************************************/
/*
* get mysql connection info
*/
int load_connection_info() {
FILE *fp;
char conn_info[256];
char config[256];
int eof;
static int loaded = 0;
char delimiters[] = "|\n";
char *conf_read, *conf_update;
if (loaded) return 0;
loaded = 1;
sprintf(config, "%s/etc/%s", VPOPMAILDIR, "vpopmail.mysql");
fp = fopen(config, "r");
if (fp == NULL) {
fprintf(stderr, "vmysql: can't read settings from %s\n", config);
return(VA_NO_AUTH_CONNECTION);
}
/* skip comments and blank lines */
do {
eof = (fgets (conn_info, sizeof(conn_info), fp) == NULL);
} while (!eof && ((*conn_info == '#') || (*conn_info == '\n')));
if (eof) {
/* no valid data read, return error */
fprintf(stderr, "vmysql: no valid settings in %s\n", config);
return(VA_NO_AUTH_CONNECTION);
}
conf_read = strdup(conn_info);
MYSQL_READ_SERVER = strtok(conf_read, delimiters);
if (MYSQL_READ_SERVER == NULL) return VA_PARSE_ERROR01;
MYSQL_READ_SOCKET = strtok(NULL, delimiters);
if (MYSQL_READ_SOCKET == NULL) return VA_PARSE_ERROR;
if (MYSQL_READ_SOCKET[0] != '/') {
MYSQL_READ_PORT = atoi(MYSQL_READ_SOCKET);
MYSQL_READ_SOCKET = NULL;
}
MYSQL_READ_USER = strtok(NULL, delimiters);
if (MYSQL_READ_USER == NULL) return VA_PARSE_ERROR03;
MYSQL_READ_PASSWD = strtok(NULL, delimiters);
if (MYSQL_READ_PASSWD == NULL) return VA_PARSE_ERROR04;
MYSQL_READ_DATABASE = strtok(NULL, delimiters);
if (MYSQL_READ_DATABASE == NULL) return VA_PARSE_ERROR05;
/* skip comments and blank lines */
do {
eof = (fgets (conn_info, sizeof(conn_info), fp) == NULL);
} while (!eof && ((*conn_info == '#') || (*conn_info == '\n')));
if (eof) {
/* re-use read-only settings for update */
MYSQL_UPDATE_SERVER = MYSQL_READ_SERVER;
MYSQL_UPDATE_PORT = MYSQL_READ_PORT;
MYSQL_UPDATE_USER = MYSQL_READ_USER;
MYSQL_UPDATE_PASSWD = MYSQL_READ_PASSWD;
MYSQL_UPDATE_DATABASE = MYSQL_READ_DATABASE;
MYSQL_UPDATE_SOCKET = MYSQL_READ_SOCKET;
} else {
conf_update = strdup(conn_info);
MYSQL_UPDATE_SERVER = strtok(conf_update, delimiters);
if (MYSQL_UPDATE_SERVER == NULL) return VA_PARSE_ERROR06;
MYSQL_UPDATE_SOCKET = strtok(NULL, delimiters);
if (MYSQL_UPDATE_SOCKET == NULL) return VA_PARSE_ERROR;
if (MYSQL_UPDATE_SOCKET[0] != '/') {
MYSQL_UPDATE_PORT = atoi(MYSQL_UPDATE_SOCKET);
MYSQL_UPDATE_SOCKET = NULL;
}
MYSQL_UPDATE_USER = strtok(NULL, delimiters);
if (MYSQL_UPDATE_USER == NULL) return VA_PARSE_ERROR08;
MYSQL_UPDATE_PASSWD = strtok(NULL, delimiters);
if (MYSQL_UPDATE_PASSWD == NULL) return VA_PARSE_ERROR09;
MYSQL_UPDATE_DATABASE = strtok(NULL, delimiters);
if (MYSQL_UPDATE_DATABASE == NULL) return VA_PARSE_ERROR10;
}
/* useful debugging info
fprintf(stderr, "read settings: server:%s port:%d user:%s pw:%s db:%s\n",
MYSQL_READ_SERVER, MYSQL_READ_PORT, MYSQL_READ_USER,
MYSQL_READ_PASSWD, MYSQL_READ_DATABASE);
fprintf(stderr, "update settings: server:%s port:%d user:%s pw:%s db:%s\n",
MYSQL_UPDATE_SERVER, MYSQL_UPDATE_PORT, MYSQL_UPDATE_USER,
MYSQL_UPDATE_PASSWD, MYSQL_UPDATE_DATABASE);
*/
return 0;
}
/************************************************************************/
/*
* Open a connection to mysql for updates
*/
int vauth_open_update()
{
unsigned int timeout = 2;
if ( update_open && (mysql_ping(&mysql_update)==0) ) return(0);
update_open = 1;
verrori = load_connection_info();
if (verrori) return -1;
mysql_init(&mysql_update);
mysql_options(&mysql_update, MYSQL_OPT_CONNECT_TIMEOUT, (char *)&timeout);
/* Try to connect to the mysql update server */
if (!(mysql_real_connect(&mysql_update, MYSQL_UPDATE_SERVER,
MYSQL_UPDATE_USER, MYSQL_UPDATE_PASSWD, NULL, MYSQL_UPDATE_PORT,
MYSQL_UPDATE_SOCKET, 0))) {
/* if we can not connect, report a error and return */
verrori = VA_NO_AUTH_CONNECTION;
return(VA_NO_AUTH_CONNECTION);
}
/* set the database we use */
if (mysql_select_db(&mysql_update, MYSQL_UPDATE_DATABASE)) {
/* we were able to connect, so create the database */
snprintf( SqlBufUpdate, SQL_BUF_SIZE,
"create database %s", MYSQL_UPDATE_DATABASE );
if (mysql_query(&mysql_update,SqlBufUpdate)) {
/* we could not create the database
* so report the error and return
*/
fprintf(stderr, "vmysql: couldn't create database '%s': %s\n", MYSQL_UPDATE_DATABASE,
mysql_error(&mysql_update));
return(-1);
}
/* set the database (we just created)*/
if (mysql_select_db(&mysql_update, MYSQL_UPDATE_DATABASE)) {
fprintf(stderr, "could not enter (just created) %s database\n", MYSQL_UPDATE_DATABASE);
return(-1);
}
}
return(0);
}
/************************************************************************/
/*
* Open a connection to the database for read-only queries
*/
#ifdef MYSQL_REPLICATION
int vauth_open_read()
{
/* if we are already connected, just return */
if ( read_open && (mysql_ping(&mysql_read)==0) ) return(0);
read_open = 1;
/* connect to mysql and set the database */
verrori = load_connection_info();
if (verrori) return -1;
mysql_init(&mysql_read);
if (!(mysql_real_connect(&mysql_read, MYSQL_READ_SERVER,
MYSQL_READ_USER, MYSQL_READ_PASSWD, MYSQL_READ_DATABASE,
MYSQL_READ_PORT, MYSQL_READ_SOCKET, 0))) {
/* we could not connect, at least try the update server */
if (!(mysql_real_connect(&mysql_read, MYSQL_UPDATE_SERVER,
MYSQL_UPDATE_USER, MYSQL_UPDATE_PASSWD, MYSQL_UPDATE_DATABASE,
MYSQL_READ_PORT, MYSQL_UPDATE_SOCKET, 0))) {
verrori = VA_NO_AUTH_CONNECTION;
return( VA_NO_AUTH_CONNECTION );
}
}
/* return success */
return(0);
}
#else
#define vauth_open_read vauth_open_update
#endif
/************************************************************************/
// Make vauth_open_read answer for vauth_open, so that function
// can be called to test the database.
int vauth_open( int will_update ) {
if( will_update ) {
return( vauth_open_update());
}
else {
return( vauth_open_read());
}
}
/************************************************************************
*
* Open a connection to the database for read-only queries
*/
int vauth_open_read_getall()
{
/* if we are already connected, just return */
if ( read_getall_open != 0 ) return(0);
read_getall_open = 1;
/* connect to mysql and set the database */
verrori = load_connection_info();
if (verrori) return -1;
mysql_init(&mysql_read_getall);
if (!(mysql_real_connect(&mysql_read_getall, MYSQL_READ_SERVER,
MYSQL_READ_USER, MYSQL_READ_PASSWD, MYSQL_READ_DATABASE,
MYSQL_READ_PORT, MYSQL_READ_SOCKET, 0))) {
/* we could not connect, at least try the update server */
if (!(mysql_real_connect(&mysql_read_getall, MYSQL_UPDATE_SERVER,
MYSQL_UPDATE_USER, MYSQL_UPDATE_PASSWD, MYSQL_UPDATE_DATABASE,
MYSQL_UPDATE_PORT, MYSQL_UPDATE_SOCKET, 0))) {
verrori = VA_NO_AUTH_CONNECTION;
return(-1);
}
return(-1);
}
/* return success */
return(0);
}
/************************************************************************/
int vauth_create_table (char *table, char *layout, int showerror)
{
int err;
char SqlBufCreate[SQL_BUF_SIZE];
if ((err = vauth_open_update()) != 0) return (err);
snprintf (SqlBufCreate, SQL_BUF_SIZE, "CREATE TABLE %s ( %s )", table, layout);
if (mysql_query (&mysql_update, SqlBufCreate)) {
if (showerror)
fprintf (stderr, "vmysql: error creating table '%s': %s\n", table,
mysql_error(&mysql_update));
return -1;
} else {
return 0;
}
}
/************************************************************************/
int vauth_adddomain( char *domain )
{
#ifndef MANY_DOMAINS
#ifdef SQL_ALIASDOMAINS
vcreate_sql_procedures();
#endif
vset_default_domain( domain );
return (vauth_create_table (vauth_munch_domain( domain ), TABLE_LAYOUT, 1));
#else
/* if creation fails, don't show an error */
vauth_create_table (MYSQL_DEFAULT_TABLE, TABLE_LAYOUT, 0);
#endif
#ifdef SQL_ALIASDOMAINS
vcreate_aliasdomains_table();
#endif
return (0);
}
/************************************************************************/
int vauth_adduser(char *user, char *domain, char *pass, char *gecos,
char *dir, int apop )
{
char *domstr;
char dom_dir[156];
uid_t uid;
gid_t gid;
char dirbuf[200];
char quota[30];
char Crypted[100];
int err;
if ( (err=vauth_open_update()) != 0 ) return(err);
vset_default_domain( domain );
strncpy( quota, "NOQUOTA", 30 );
#ifndef MANY_DOMAINS
domstr = vauth_munch_domain( domain );
#else
domstr = MYSQL_DEFAULT_TABLE;
#endif
if ( domain == NULL || domain[0] == 0 ) {
domstr = MYSQL_LARGE_USERS_TABLE;
}
if ( strlen(domain) <= 0 ) {
if ( strlen(dir) > 0 ) {
snprintf(dirbuf, sizeof(dirbuf),
"%s/users/%s/%s", VPOPMAILDIR, dir, user);
} else {
snprintf(dirbuf, sizeof(dirbuf), "%s/users/%s", VPOPMAILDIR, user);
}
} else {
vget_assign(domain, dom_dir, sizeof(dom_dir), &uid, &gid );
if ( strlen(dir) > 0 ) {
snprintf(dirbuf, sizeof(dirbuf), "%s/%s/%s", dom_dir, dir, user);
} else {
snprintf(dirbuf, sizeof(dirbuf), "%s/%s", dom_dir, user);
}
}
if ( pass[0] != 0 ) {
mkpasswd3(pass,Crypted, 100);
} else {
Crypted[0] = 0;
}
qnprintf( SqlBufUpdate, SQL_BUF_SIZE, INSERT,
domstr, user,
#ifdef MANY_DOMAINS
domain,
#endif
Crypted, apop, gecos, dirbuf, quota
#ifdef CLEAR_PASS
, pass
#endif
);
if (mysql_query(&mysql_update,SqlBufUpdate)) {
fprintf(stderr, "vmysql: sql error[2]: %s\n", mysql_error(&mysql_update));
return(-1);
}
return(0);
}
/************************************************************************/
struct vqpasswd *vauth_getpw(char *user, char *domain)
{
char *domstr;
static struct vqpasswd vpw;
static char in_domain[156];
int err;
uid_t myuid;
uid_t uid;
gid_t gid;
vget_assign(domain,NULL,0,&uid,&gid);
myuid = geteuid();
if ( myuid != 0 && myuid != uid ) return(NULL);
verrori = 0;
if ( (err=vauth_open_read()) != 0 ) {
verrori = err;
return(NULL);
}
lowerit(user);
lowerit(domain);
snprintf (in_domain, sizeof(in_domain), "%s", domain);
vset_default_domain( in_domain );
#ifndef MANY_DOMAINS
domstr = vauth_munch_domain( in_domain );
#else
domstr = MYSQL_DEFAULT_TABLE;
#endif
if ( domstr == NULL || domstr[0] == 0 ) domstr = MYSQL_LARGE_USERS_TABLE;
qnprintf(SqlBufRead, SQL_BUF_SIZE, USER_SELECT, domstr, user
#ifdef MANY_DOMAINS
, in_domain
#endif
);
if (mysql_query(&mysql_read,SqlBufRead)) {
fprintf(stderr, "vmysql: sql error[3]: %s\n", mysql_error(&mysql_read));
/* Ron Gage - May 29, 2006 - With newer versions of MySQL, there is such a thing
as a connection timeout regardless of activity. By default under MySQL 5, this
timeout is 28800 seconds (8 hours). If your vpopmail system runs fine for the
first 8 hours, then stops authenticating, this timeout is your problem (especially
under authdaemond).
What this code does is when an error is encountered, it first tries to drop and
rebuild a connection to the SQL server and tries again. If this second attempt
fails, then something other than the connection timeout is the problem. This fix
need to be implemented in other places but in my setup (Slackware 10.2, netqmail,
vpopmail, courier-authdaemond, courier-imapd and a few others), this is always where
the auth attempt died with a "SQL server has gone away" error.
*/
fprintf(stderr, "Attempting to rebuild connection to SQL server\n");
vclose();
verrori = 0;
if ( (err=vauth_open_read()) != 0 ) {
verrori = err;
return(NULL);
}
if (mysql_query(&mysql_read, SqlBufRead)) {
fprintf (stderr, "vmysql: connection rebuild failed: %s\n", mysql_error(&mysql_read));
return(NULL);
}
}
if (!(res_read = mysql_store_result(&mysql_read))) {
fprintf(stderr, "vmysql: store result failed 1\n");
return(NULL);
}
if ( mysql_num_rows(res_read) == 0 ) {
mysql_free_result(res_read);
return(NULL);
}
memset(IUser, 0, sizeof(IUser));
memset(IPass, 0, sizeof(IPass));
memset(IGecos, 0, sizeof(IGecos));
memset(IDir, 0, sizeof(IDir));
memset(IShell, 0, sizeof(IShell));
memset(IClearPass, 0, sizeof(IClearPass));
vpw.pw_name = IUser;
vpw.pw_passwd = IPass;
vpw.pw_gecos = IGecos;
vpw.pw_dir = IDir;
vpw.pw_shell = IShell;
vpw.pw_clear_passwd = IClearPass;
if((row = mysql_fetch_row(res_read))) {
strncpy(vpw.pw_name,row[0],SMALL_BUFF);
if ( row[1] != 0 ) strncpy(vpw.pw_passwd,row[1],SMALL_BUFF);
if ( row[2] != 0 ) vpw.pw_uid = atoi(row[2]);
if ( row[3] != 0 ) vpw.pw_gid = atoi(row[3]);
if ( row[4] != 0 ) strncpy(vpw.pw_gecos,row[4],SMALL_BUFF);
if ( row[5] != 0 ) strncpy(vpw.pw_dir,row[5],SMALL_BUFF);
if ( row[6] != 0 ) strncpy(vpw.pw_shell, row[6],SMALL_BUFF);
#ifdef CLEAR_PASS
if ( row[7] != 0 ) strncpy(vpw.pw_clear_passwd, row[7],SMALL_BUFF);
#endif
} else {
mysql_free_result(res_read);
return(NULL);
}
mysql_free_result(res_read);
vlimits_setflags (&vpw, in_domain);
return(&vpw);
}
/* del a domain from the auth backend
* - 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
* - delete domain's vlog entries ( If defined with ENABLE_SQL_LOGGING )
*/
/************************************************************************/
int vauth_deldomain( char *domain )
{
char *tmpstr;
int err;
if ( (err=vauth_open_update()) != 0 ) return(err);
vset_default_domain( domain );
#ifndef MANY_DOMAINS
/* convert the domain name to the table name (eg convert . to _ ) */
tmpstr = vauth_munch_domain( domain );
snprintf( SqlBufUpdate, SQL_BUF_SIZE, "drop table %s", tmpstr);
#else
tmpstr = MYSQL_DEFAULT_TABLE;
qnprintf( SqlBufUpdate, SQL_BUF_SIZE, "delete from %s where pw_domain = '%s'",
tmpstr, domain );
#endif
if (mysql_query(&mysql_update,SqlBufUpdate)) {
return(-1);
}
#ifdef VALIAS
valias_delete_domain( domain);
#endif
#ifdef ENABLE_AUTH_LOGGING
qnprintf( SqlBufUpdate, SQL_BUF_SIZE,
"delete from lastauth where domain = '%s'", domain );
if (mysql_query(&mysql_update,SqlBufUpdate)) {
err = mysql_errno(&mysql_update);
if (err != ER_NO_SUCH_TABLE)
fprintf(stderr, "vauth_deldomain: warning: mysql_query(%s) failed: %s\n", SqlBufUpdate, mysql_error(&mysql_update));
}
#endif
#ifdef ENABLE_SQL_LOGGING
#ifdef ENABLE_SQL_REMOVE_DELETED
qnprintf( SqlBufUpdate, SQL_BUF_SIZE,
"delete from vlog where domain = '%s'", domain );
if (mysql_query(&mysql_update,SqlBufUpdate)) {
err = mysql_errno(&mysql_update);
if (err != ER_NO_SUCH_TABLE)
fprintf(stderr, "vauth_deldomain: warning: mysql_query(%s) failed: %s\n", SqlBufUpdate, mysql_error(&mysql_update));
}
#endif
#endif
vdel_limits(domain);
return(0);
}
/************************************************************************/
int vauth_deluser( char *user, char *domain )
{
char *tmpstr;
int err = 0;
if ( (err=vauth_open_update()) != 0 ) return(err);
vset_default_domain( domain );
#ifndef MANY_DOMAINS
if ( domain == NULL || domain[0] == 0 ) {
tmpstr = MYSQL_LARGE_USERS_TABLE;
} else {
tmpstr = vauth_munch_domain( domain );
}
#else
tmpstr = MYSQL_DEFAULT_TABLE;
#endif
qnprintf( SqlBufUpdate, SQL_BUF_SIZE, DELETE_USER, tmpstr, user
#ifdef MANY_DOMAINS
, domain
#endif
);
if (mysql_query(&mysql_update,SqlBufUpdate)) {
err = -1;
}
#ifdef ENABLE_AUTH_LOGGING
qnprintf( SqlBufUpdate, SQL_BUF_SIZE,
"delete from lastauth where user = '%s' and domain = '%s'",
user, domain );
if (mysql_query(&mysql_update,SqlBufUpdate)) {
err = mysql_errno(&mysql_update);
if (err != ER_NO_SUCH_TABLE)
err = -1;
else
err = 0;
}
#endif
#ifdef ENABLE_SQL_LOGGING
#ifdef ENABLE_SQL_REMOVE_DELETED
qnprintf( SqlBufUpdate, SQL_BUF_SIZE,
"delete from vlog where domain = '%s' and user = '%s'",
domain, user );
if (mysql_query(&mysql_update,SqlBufUpdate)) {
err = mysql_errno(&mysql_update);
if (err != ER_NO_SUCH_TABLE)
err = -1;
else
err = 0;
}
#endif
#endif
return(err);
}
int vauth_setquota( char *username, char *domain, char *quota)
{
char *tmpstr;
int err;
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);
if ( (err=vauth_open_update()) != 0 ) return(err);
vset_default_domain( domain );
#ifndef MANY_DOMAINS
tmpstr = vauth_munch_domain( domain );
#else
tmpstr = MYSQL_DEFAULT_TABLE;
#endif
qnprintf( SqlBufUpdate, SQL_BUF_SIZE, SETQUOTA, tmpstr, quota, username
#ifdef MANY_DOMAINS
, domain
#endif
);
if (mysql_query(&mysql_update,SqlBufUpdate)) {
fprintf(stderr, "vmysql: sql error[4]: %s\n", mysql_error(&mysql_update));
return(-1);
}
return(0);
}
/************************************************************************/
struct vqpasswd *vauth_getall(char *domain, int first, int sortit)
{
char *domstr = NULL;
static struct vqpasswd vpw;
static int more = 0;
int err;
vset_default_domain( domain );
#ifdef MANY_DOMAINS
domstr = MYSQL_DEFAULT_TABLE;
#else
domstr = vauth_munch_domain( domain );
#endif
if ( first == 1 ) {
if ( (err=vauth_open_read_getall()) != 0 ) return(NULL);
qnprintf(SqlBufRead, SQL_BUF_SIZE, GETALL, domstr
#ifdef MANY_DOMAINS
,domain
#endif
);
if ( sortit == 1 ) {
strncat( SqlBufRead, " order by pw_name", SQL_BUF_SIZE-strlen(SqlBufRead)-1);
}
if (res_read!=NULL) mysql_free_result(res_read_getall);
res_read = NULL;
if (mysql_query(&mysql_read_getall,SqlBufRead)) {
fprintf(stderr, "vmysql: sql error[5]: %s\n", mysql_error(&mysql_read_getall));
return(NULL);
}
if (!(res_read_getall=mysql_store_result(&mysql_read_getall))) {
fprintf(stderr, "vmysql: store result failed 2\n");
return(NULL);
}
} else if ( more == 0 ) {
return(NULL);
}
memset(IUser, 0, sizeof(IUser));
memset(IPass, 0, sizeof(IPass));
memset(IGecos, 0, sizeof(IGecos));
memset(IDir, 0, sizeof(IDir));
memset(IShell, 0, sizeof(IShell));
memset(IClearPass, 0, sizeof(IClearPass));
vpw.pw_name = IUser;
vpw.pw_passwd = IPass;
vpw.pw_gecos = IGecos;
vpw.pw_dir = IDir;
vpw.pw_shell = IShell;
vpw.pw_clear_passwd = IClearPass;
if ((row_getall = mysql_fetch_row(res_read_getall)) != NULL) {
strncpy(vpw.pw_name,row_getall[0],SMALL_BUFF);
if (row_getall[1]!=0) strncpy(vpw.pw_passwd,row_getall[1],SMALL_BUFF);
if (row_getall[2]!=0) vpw.pw_uid = atoi(row_getall[2]);
if (row_getall[3]!=0) vpw.pw_gid = atoi(row_getall[3]);
if (row_getall[4]!=0) strncpy(vpw.pw_gecos,row_getall[4],SMALL_BUFF);
if (row_getall[5]!=0) strncpy(vpw.pw_dir,row_getall[5],SMALL_BUFF);
if (row_getall[6]!=0) {
strncpy(vpw.pw_shell, row_getall[6],SMALL_BUFF);
}
#ifdef CLEAR_PASS
if (row_getall[7]!=0) {
strncpy(vpw.pw_clear_passwd, row_getall[7],SMALL_BUFF);
}
#endif
more = 1;
vlimits_setflags(&vpw,domain);
return(&vpw);
}
more = 0;
mysql_free_result(res_read_getall);
res_read_getall = NULL;
return(NULL);
}
/************************************************************************/
void vauth_end_getall()
{
if ( res_read_getall != NULL ) {
mysql_free_result(res_read_getall);
}
res_read_getall = NULL;
}
/************************************************************************/
char *vauth_munch_domain( char *domain )
{
int i;
static char tmpbuf[512];
if ( domain == NULL || domain[0] == 0 ) return(domain);
for(i=0; ((domain[i]!=0) && (i < (sizeof(tmpbuf) - 1)));++i){
tmpbuf[i] = domain[i];
if ( domain[i] == '.' || domain[i] == '-' ) {
tmpbuf[i] = MYSQL_DOT_CHAR;
}
}
tmpbuf[i] = 0;
return(tmpbuf);
}
/************************************************************************/
int vauth_setpw( struct vqpasswd *inpw, char *domain )
{
char *tmpstr;
uid_t myuid;
uid_t uid;
gid_t gid;
int err;
#ifdef USE_ONCHANGE
char user_domain[MAX_BUFF];
#endif
#ifdef USE_ONCHANGE
snprintf( user_domain, MAX_BUFF, "%s@%s", inpw->pw_name, domain);
on_change("mod_user", user_domain, "-", 0, 0);
#endif
err = vcheck_vqpw(inpw, domain);
if ( err != 0 ) return(err);
vget_assign(domain,NULL,0,&uid,&gid);
myuid = geteuid();
if ( myuid != 0 && myuid != uid ) {
return(VA_BAD_UID);
}
if ( (err=vauth_open_update()) != 0 ) return(err);
vset_default_domain( domain );
#ifndef MANY_DOMAINS
tmpstr = vauth_munch_domain( domain );
#else
tmpstr = MYSQL_DEFAULT_TABLE;
#endif
qnprintf( SqlBufUpdate,SQL_BUF_SIZE,SETPW,
tmpstr,
inpw->pw_passwd,
inpw->pw_uid,
inpw->pw_gid,
inpw->pw_gecos,
inpw->pw_dir,
inpw->pw_shell,
#ifdef CLEAR_PASS
inpw->pw_clear_passwd,
#endif
inpw->pw_name
#ifdef MANY_DOMAINS
,domain
#endif
);
if (mysql_query(&mysql_update,SqlBufUpdate)) {
fprintf(stderr, "vmysql: sql error[6]: %s\n", mysql_error(&mysql_update));
return(-1);
}
#ifdef SQWEBMAIL_PASS
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);
}
/************************************************************************/
#ifdef POP_AUTH_OPEN_RELAY
int vopen_smtp_relay()
{
char *ipaddr;
time_t mytime;
int err;
int rows;
mytime = time(NULL);
ipaddr = get_remote_ip();
if ( ipaddr == NULL ) {
return 0;
}
// open_smtp_relay() expects "-1" on database errors. "0" means duplicate record. (See vpopmail.c)
if ( (err=vauth_open_update()) != 0 ) return (-1);
qnprintf( SqlBufUpdate, SQL_BUF_SIZE,
"replace into relay ( ip_addr, timestamp ) values ( '%s', %d )",
ipaddr, (int)mytime);
if (mysql_query(&mysql_update,SqlBufUpdate)) {
vcreate_relay_table();
if (mysql_query(&mysql_update,SqlBufUpdate)) {
fprintf(stderr, "vmysql: sql error[7]: %s\n", mysql_error(&mysql_update));
}
}
rows = mysql_affected_rows(&mysql_update);
/* return true if only INSERT (didn't exist) */
/* would return 2 if replaced, or -1 if error */
return rows == 1;
}
/************************************************************************/
void vupdate_rules(int fdm)
{
if (vauth_open_read() != 0) return;
snprintf(SqlBufRead, SQL_BUF_SIZE, "select ip_addr from relay");
if (mysql_query(&mysql_read,SqlBufRead)) {
vcreate_relay_table();
if (mysql_query(&mysql_read,SqlBufRead)) {
fprintf(stderr, "vmysql: sql error[8]: %s\n", mysql_error(&mysql_read));
return;
}
}
if (!(res_read = mysql_store_result(&mysql_read))) {
fprintf(stderr, "vmysql: store result failed 3\n");
return;
}
while((row = mysql_fetch_row(res_read))) {
snprintf(SqlBufRead, SQL_BUF_SIZE, "%s:allow,RELAYCLIENT=\"\",RBLSMTPD=\"\"\n", row[0]);
write(fdm,SqlBufRead, strlen(SqlBufRead));
}
mysql_free_result(res_read);
}
/************************************************************************/
void vclear_open_smtp(time_t clear_minutes, time_t mytime)
{
time_t delete_time;
int err;
if ( (err=vauth_open_update()) != 0 ) return;
delete_time = mytime - clear_minutes;
snprintf( SqlBufUpdate, SQL_BUF_SIZE, "delete from relay where timestamp <= %d",
(int)delete_time);
if (mysql_query(&mysql_update,SqlBufUpdate)) {
vcreate_relay_table();
return;
}
}
/************************************************************************/
void vcreate_relay_table()
{
vauth_create_table ("relay", RELAY_TABLE_LAYOUT, 1);
return;
}