forked from brunonymous/vpopmail
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vldap.c
2131 lines (1699 loc) · 53.6 KB
/
vldap.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
*/
#include <lber.h>
#include <ldap.h>
#include <pwd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/types.h>
#include <time.h>
#include <unistd.h>
#include <utime.h>
#include "config.h"
#include "vauth.h"
#include "vlimits.h"
#include "vpopmail.h"
#include "vldap.h"
LDAP *ld = NULL;
LDAPMessage *glm = NULL;
#ifdef CLEAR_PASS
#define NUM_LDAP_FIELDS 9
#else
#define NUM_LDAP_FIELDS 8
#endif
char *ldap_fields[NUM_LDAP_FIELDS] = {
"uid", /* 0 pw_name */
"userPassword", /* 1 pw_passwd */
"qmailUID", /* 2 pw_uid */
"qmailGID", /* 3 pw_gid */
"qmaildomain", /* 4 pw_gecos */
"mailMessageStore", /* 5 pw_dir */
"mailQuota", /* 6 pw_shell */
#ifndef CLEAR_PASS
"objectclass" /* 7 ldap */
#else
"clearPassword", /* 7 pw_clear_passwd */
"objectclass" /* 8 ldap */
#endif
};
/***************************************************************************/
/*
* get ldap connection info
*/
int load_connection_info() {
FILE *fp;
char conn_info[256];
char config[256];
int eof;
static int loaded = 0;
char *port;
char delimiters[] = "|\n";
char *conf_read;
if (loaded) return 0;
loaded = 1;
sprintf(config, "%s/etc/%s", VPOPMAILDIR, "vpopmail.ldap");
fp = fopen(config, "r");
if (fp == NULL) {
fprintf(stderr, "vldap: 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, "vldap: no valid settings in %s\n", config);
return (VA_NO_AUTH_CONNECTION);
}
conf_read = strdup(conn_info);
VLDAP_SERVER = strtok(conf_read, delimiters);
if (VLDAP_SERVER == NULL) return VA_PARSE_ERROR;
port = strtok(NULL, delimiters);
if (port == NULL) return VA_PARSE_ERROR;
VLDAP_PORT = atoi(port);
VLDAP_USER = strtok(NULL, delimiters);
if (VLDAP_USER == NULL) return VA_PARSE_ERROR;
VLDAP_PASSWORD = strtok(NULL, delimiters);
if (VLDAP_PASSWORD == NULL) return VA_PARSE_ERROR;
VLDAP_BASEDN = strtok(NULL, delimiters);
if (VLDAP_BASEDN == NULL) return VA_PARSE_ERROR;
return 0;
}
struct vqpasswd *vauth_getpw(char *user, char *domain) {
int ret = 0;
size_t len = 0;
struct vqpasswd *vpw = NULL;
LDAPMessage *res = NULL, *msg = NULL;
struct berval **rval = NULL;
char *filter = NULL, **vals = NULL, *h = NULL, *t = NULL, *passwd = NULL;
char *dn = NULL;
uid_t myuid;
uid_t uid;
gid_t gid;
verrori = 0;
lowerit(user);
lowerit(domain);
vget_assign(domain, NULL, 0, &uid, &gid);
myuid = geteuid();
if (myuid != 0 && myuid != uid) {
return (NULL);
}
/* connect to the ldap server (if we havent already got a connection open) */
if (ld == NULL) {
if (ldap_connect() != 0) {
safe_free((void **)&filter);
return NULL;
}
}
/* take a given domain, and set dn to be this format :
* ou=somedomain.com,o=vpopmail
*/
if (compose_dn(&dn, domain) != 0) return NULL;
/* take the username and create set filter ot be in this format :
* (&(objectclass=qmailUser)(uid=someusername))
*/
len = (strlen(user) + 32 + 1);
filter = (char *)safe_malloc(len);
memset((char *)filter, 0, len);
snprintf(filter, len, "(&(objectclass=qmailUser)(uid=%s))", user);
/* perform an ldap search
* int ldap_search_s(ld, base, scope, filter, attrs, attrsonly, res)
*
* Will search synchronously, and not return until the operation completes.
* base : DN of the entry at which to start the search
* scope : scope of the search
* LDAP_SCOPE_SUBTREE means to search the object and all of its descendents.
* filter : filter to apply to the search
* attrs : attribute types to return from entries that match filter
* attrsonly : set to 0 for attributes and attributetypes are wanted. 1 if
* only attributes are wanted.
*/
ret = ldap_search_ext_s(ld, dn, LDAP_SCOPE_SUBTREE, filter, vldap_attrs, 0,
NULL, NULL, NULL, LDAP_NO_LIMIT, &res);
safe_free((void **)&filter);
/* see if the search ran without generating an error */
if (ret != LDAP_SUCCESS) {
fprintf(stderr, "%s\n", ldap_err2string(ret));
return NULL;
}
/* grab a pointer to the 1st entry in the chain of search results */
msg = ldap_first_entry(ld, res);
if (msg == NULL) {
/* We had an error grabbing the pointer */
return NULL;
}
/* find out how many matches we found */
ret = ldap_count_entries(ld, msg);
if (ret == -1) {
/* an error occurred when counting the entries */
fprintf(stderr, "%s\n", ldap_err2string(ret));
return NULL;
}
/*
Fetch userPassword first so we can make sure
we're able to handle it's password encryption (if any)
*/
/* userPasswd / pw_password */
rval = ldap_get_values_len(ld, msg, "userPassword");
if (rval == NULL) {
fprintf(stderr, "Error\n");
return NULL;
}
t = h = NULL;
passwd = (char *)safe_malloc((*rval)->bv_len + 1);
memset((char *)passwd, 0, (*rval)->bv_len + 1);
memcpy((char *)passwd, (char *)((*rval)->bv_val), (*rval)->bv_len);
ldap_value_free_len(rval);
if (*passwd == '{') {
for (t = h = (passwd + 1); *t; t++) {
if (*t == '}') {
*t++ = '\0';
/* This is not the best, but we keep the pointer as (h - 1) */
passwd = t;
/*
Check against the encryption method, and if we see something
we dont recognize or support, invalidate user login.
*/
/* Steki <[email protected]> Thu Jan 24 17:27:18 CET 2002
* Added check for MD5 crypted passwords
*/
if (strcmp(h, "crypt") && strcmp(h, "MD5")) {
free(h - 1);
return NULL;
}
break;
}
}
/*
No terminating brace found, or empty password.
*/
if (!(*t)) {
return NULL;
}
}
/* create a vpw struct, which we will populate with the data we suck in from
* ldap */
vpw = (struct vqpasswd *)safe_malloc(sizeof(struct vqpasswd));
memset((struct vqpasswd *)vpw, 0, sizeof(struct vqpasswd));
vpw->pw_passwd = (char *)safe_malloc((strlen(passwd) + 1));
memset((char *)vpw->pw_passwd, 0, (strlen(passwd) + 1));
memcpy((char *)vpw->pw_passwd, (char *)(passwd), strlen(passwd));
if (vpw->pw_passwd == NULL) {
free(h - 1);
return NULL;
}
/*
Old passwd pointer.
..and don't forget to check if you even set the pointer *smack*
*/
if (h) free(h - 1);
/* uid / pw_name */
rval = ldap_get_values_len(ld, msg, "uid");
if (rval == NULL) {
safe_free((void **)&vpw->pw_passwd);
fprintf(stderr, "Error\n");
return NULL;
}
vpw->pw_name = (char *)safe_malloc((*rval)->bv_len + 1);
memset((char *)vpw->pw_name, 0, (*rval)->bv_len + 1);
memcpy((char *)vpw->pw_name, (char *)((*rval)->bv_val), (*rval)->bv_len);
ldap_value_free_len(rval);
/* mailQuota / pw_shell */
rval = ldap_get_values_len(ld, msg, "mailQuota");
if (rval)
vpw->pw_shell = (char *)safe_malloc((*rval)->bv_len + 1);
else
vpw->pw_shell = (char *)safe_malloc(1);
if (rval) {
memset((char *)vpw->pw_shell, 0, (strlen(*vals) + 1));
memcpy((char *)vpw->pw_shell, (char *)((*rval)->bv_val),
(*rval)->bv_len + 1);
ldap_value_free_len(rval);
} else {
*vpw->pw_shell = '\0';
fprintf(stderr, "Error\n");
}
/* qmaildomain / pw_gecos */
rval = ldap_get_values_len(ld, msg, "qmaildomain");
if (rval) {
vpw->pw_gecos = (char *)safe_malloc((*rval)->bv_len + 1);
memset((char *)vpw->pw_gecos, 0, (*rval)->bv_len + 1);
memcpy((char *)vpw->pw_gecos, (char *)((*rval)->bv_val),
(*rval)->bv_len + 1);
ldap_value_free_len(rval);
} else
fprintf(stderr, "Error\n");
/* mailMessageStore / pw_dir */
rval = ldap_get_values_len(ld, msg, "mailMessageStore");
if (rval) {
vpw->pw_dir = (char *)safe_malloc((*rval)->bv_len + 1);
memset((char *)vpw->pw_dir, 0, (*rval)->bv_len + 1);
memcpy((char *)vpw->pw_dir, (char *)((*rval)->bv_val), (*rval)->bv_len + 1);
ldap_value_free_len(rval);
} else
fprintf(stderr, "Error\n");
/* qmailUID / pw_uid */
rval = ldap_get_values_len(ld, msg, "qmailUID");
if (rval) {
vpw->pw_uid = atoi((*rval)->bv_val);
ldap_value_free_len(rval);
} else
fprintf(stderr, "Error\n");
/* qmailGID / pw_gid */
rval = ldap_get_values_len(ld, msg, "qmailGID");
if (rval) {
vpw->pw_gid = atoi((*rval)->bv_val);
ldap_value_free_len(rval);
} else
fprintf(stderr, "Error\n");
#ifdef CLEAR_PASS
/* clearPasswd / pw_clear_passwd */
rval = ldap_get_values_len(ld, msg, "clearPassword");
if (rval) {
vpw->pw_clear_passwd = (char *)safe_malloc((*rval)->bv_len + 1);
memset((char *)vpw->pw_clear_passwd, 0, (*rval)->bv_len + 1);
memcpy((char *)vpw->pw_clear_passwd, (char *)((*rval)->bv_val),
(*rval)->bv_len + 1);
ldap_value_free_len(rval);
}
#endif
vlimits_setflags(vpw, domain);
return vpw;
}
/***************************************************************************/
void vauth_end_getall() {}
/***************************************************************************/
struct vqpasswd *vauth_getall(char *domain, int first, int sortit) {
int ret = 0;
size_t len = 0;
struct vqpasswd *pw = NULL;
LDAPMessage *res = NULL;
char *filter = NULL, **vals = NULL;
char *basedn = NULL;
struct berval **rval = NULL;
/* if 1st time through, extract all users from this chosen domain */
if (first) {
lowerit(domain);
len = (32 + 1);
filter = (char *)safe_malloc(len);
memset((char *)filter, 0, len);
/* connect to the ldap server if we havent already done so */
if (ld == NULL) {
if (ldap_connect() != 0) {
safe_free((void **)&filter);
return NULL;
}
}
/* set basedn to be of the format :
* ou=somedomain,o=vpopmail
*/
if (compose_dn(&basedn, domain) != 0) {
safe_free((void **)&filter);
return NULL;
}
snprintf(filter, len, "(objectclass=qmailUser)");
/* perform the lookup for all users in a given domain */
ret = ldap_search_ext_s(ld, basedn, LDAP_SCOPE_SUBTREE, filter, vldap_attrs,
0, NULL, NULL, NULL, LDAP_NO_LIMIT, &res);
safe_free((void **)&basedn);
safe_free((void **)&filter);
if (ret != LDAP_SUCCESS) {
fprintf(stderr, "Error\n");
return NULL;
}
/* sort the entries alphabetically by username if required */
/* sorting is deprecated in recent version of openldap ! */
/*
if ( sortit ) {
if ( ldap_sort_entries( ld, &res, "uid", &strncmp ) != 0) {
fprintf(stderr, "Error\n");
return NULL;
}
if (ret != LDAP_SUCCESS)
return NULL;
}
*/
/* get a pointer to the first user in the list */
glm = ldap_first_entry(ld, res);
if (glm == NULL) return NULL;
/* grab the ldap properties of this user */
rval = ldap_get_values_len(ld, glm, "uid");
if (rval == NULL) {
fprintf(stderr, "Error\n");
return NULL;
}
/* grab the vpopmail properties of this user */
pw = vauth_getpw(*vals, domain);
return pw;
} else {
/* not 1st time through, so get next entry from the chain */
if (glm == NULL) /* Just to be safe. ([email protected]) */
return NULL;
res = glm;
glm = ldap_next_entry(ld, res);
if (glm == NULL) return NULL;
rval = ldap_get_values_len(ld, glm, "uid");
if (rval == NULL) {
fprintf(stderr, "Error\n");
return NULL;
}
pw = vauth_getpw(*vals, domain);
ldap_value_free_len(rval);
return pw;
}
}
/***************************************************************************/
/*
Higher-level functions no longer crypt.
Lame.
*/
int vauth_adduser(char *user, char *domain, char *password, char *gecos,
char *dir, int apop) {
char *dn = NULL;
char *dn_tmp = NULL;
LDAPMod **lm = NULL;
char dom_dir[156];
uid_t uid;
gid_t gid;
int ret = 0, vd = 0;
int i, len;
char *b = NULL;
char crypted[100] = {0};
if ((dir) && (*dir)) vd = 1;
if (gecos == 0 || gecos[0] == 0) gecos = user;
/* take a given domain, and lookup the dom_dir, uid, gid */
if (vget_assign(domain, dom_dir, 156, &uid, &gid) == NULL) {
fprintf(stderr, "failed to vget_assign the domain : %s", domain);
return (-1);
}
if (vd) {
ret = strlen(dom_dir) + 5 + strlen(dir) + strlen(user);
} else {
ret = strlen(dom_dir) + 5 + strlen(user);
}
b = (char *)safe_malloc(ret);
memset((char *)b, 0, ret);
if (vd) {
snprintf(b, ret, "%s/%s/%s", dom_dir, dir, user);
} else {
snprintf(b, ret, "%s/%s", dom_dir, user);
}
dir = b;
/* make an ldap connection (unless we already have one open) */
if (ld == NULL) {
if (ldap_connect() != 0) return -99;
}
lm = (LDAPMod **)safe_malloc(sizeof(LDAPMod *) * (NUM_LDAP_FIELDS + 1));
for (i = 0; i < NUM_LDAP_FIELDS; ++i) {
lm[i] = (LDAPMod *)safe_malloc(sizeof(LDAPMod));
memset((LDAPMod *)lm[i], 0, sizeof(LDAPMod));
lm[i]->mod_op = LDAP_MOD_ADD;
lm[i]->mod_type = safe_strdup(ldap_fields[i]);
lm[i]->mod_values = (char **)safe_malloc(sizeof(char *) * 2);
lm[i]->mod_values[1] = NULL;
}
lm[NUM_LDAP_FIELDS] = NULL;
/* lm[0] will store : uid / pw_name */
lm[0]->mod_values[0] = safe_strdup(user);
/* lm[1] will store : userPassword / pw_password */
memset((char *)crypted, 0, 100);
if (password[0] == 0) {
crypted[0] = 0;
} else {
mkpasswd3(password, crypted, 100);
}
lm[1]->mod_values[0] = (char *)safe_malloc(strlen(crypted) + 7 + 1);
#ifdef MD5_PASSWORDS
snprintf(lm[1]->mod_values[0], strlen(crypted) + 7 + 1, "{MD5}%s", crypted);
#else
snprintf(lm[1]->mod_values[0], strlen(crypted) + 7 + 1, "{crypt}%s", crypted);
#endif
/* lm[2] will store : qmailUID / pw_uid */
lm[2]->mod_values[0] = (char *)safe_malloc(10);
if (apop == USE_POP)
sprintf(lm[2]->mod_values[0], "%d", 1);
else
sprintf(lm[2]->mod_values[0], "%d", 2);
/* lm[3] will store : qmailGID / pw_gid */
lm[3]->mod_values[0] = (char *)safe_malloc(10);
sprintf(lm[3]->mod_values[0], "%d", 0);
/* lm[4] will store : qmaildomain / pw_gecos */
lm[4]->mod_values[0] = safe_strdup(gecos);
/* lm[5] will store : mailMessageStore / pw_dir */
lm[5]->mod_values[0] = safe_strdup(dir);
/* lm[6] will store : mailQuota / pw_shell */
lm[6]->mod_values[0] = safe_strdup("NOQUOTA");
/* When running with clearpasswords enabled,
* lm[7] will store : clearPassword / pw_clear_password
*/
#ifdef CLEAR_PASS
/* with clear passwords,
* lm[7] will store : clearPassword / pw_clear_password
* lm[8] will store : objectclass
*/
lm[7]->mod_values[0] = strdup(password);
lm[8]->mod_values[0] = safe_strdup("qmailUser");
#else
/* without clear passwords,
* lm[7] will store : objectclass
*/
lm[7]->mod_values[0] = safe_strdup("qmailUser");
#endif
/* set dn_tmp to be of the format :
* ou=somedomain.com,o=vpopmail
*/
if (compose_dn(&dn_tmp, domain) != 0) {
for (i = 0; i < 8; ++i) {
safe_free((void **)&lm[i]->mod_type);
safe_free((void **)&lm[i]->mod_values[0]);
}
safe_free((void **)&lm);
safe_free((void **)&dn);
return -98;
}
/* set dn to be of the format :
* uid=someuser, ou=somedomain,o=vpopmail
*/
len = 4 + strlen(user) + 2 + strlen(VLDAP_BASEDN) + 4 + strlen(domain) + 1;
dn = (char *)safe_malloc(len);
memset((char *)dn, 0, len);
snprintf(dn, len, "uid=%s, %s", user, dn_tmp);
safe_free((void **)&dn_tmp);
/* add object to ldap
* dn is the DN of the entry to add
* lm is the attributes of the entry to add
*/
ret = ldap_add_ext_s(ld, dn, lm, NULL, NULL);
safe_free((void **)&dn);
for (i = 0; i < NUM_LDAP_FIELDS; ++i) {
safe_free((void **)&lm[i]->mod_type);
safe_free((void **)&lm[i]->mod_values[0]);
}
safe_free((void **)&lm);
if (ret != LDAP_SUCCESS) {
fprintf(stderr, "%s\n", ldap_err2string(ret));
if (ret == LDAP_ALREADY_EXISTS) return VA_USERNAME_EXISTS;
return -99;
}
return VA_SUCCESS;
}
/***************************************************************************/
int vauth_adddomain(char *domain) {
int ret = 0;
char *dn = NULL;
LDAPMod **lm = NULL;
/* make a connection to the ldap server, if we are not already connected */
if (ld == NULL) {
ret = ldap_connect();
if (ret != 0) {
return -99;
/* Attention I am not quite shure, when we return NULL or -99, see above
*/
}
}
lm = (LDAPMod **)safe_malloc(sizeof(LDAPMod *) * 3);
lm[0] = (LDAPMod *)safe_malloc(sizeof(LDAPMod));
lm[1] = (LDAPMod *)safe_malloc(sizeof(LDAPMod));
lm[2] = NULL;
memset((LDAPMod *)lm[0], 0, sizeof(LDAPMod));
memset((LDAPMod *)lm[1], 0, sizeof(LDAPMod));
lm[0]->mod_op = LDAP_MOD_ADD;
lm[1]->mod_op = LDAP_MOD_ADD;
lm[0]->mod_type = safe_strdup("ou");
lm[1]->mod_type = safe_strdup("objectclass");
lm[0]->mod_values = (char **)safe_malloc(sizeof(char *) * 2);
lm[1]->mod_values = (char **)safe_malloc(sizeof(char *) * 2);
lm[0]->mod_values[1] = NULL;
lm[1]->mod_values[1] = NULL;
lm[0]->mod_values[0] = safe_strdup(domain);
lm[1]->mod_values[0] = safe_strdup("organizationalUnit");
/* set dn to be of the format :
* ou=somedomain.com,o=vpopmail
*/
if (compose_dn(&dn, domain) != 0) {
safe_free((void **)&lm[0]->mod_type);
safe_free((void **)&lm[1]->mod_type);
safe_free((void **)&lm[0]->mod_values[0]);
safe_free((void **)&lm[1]->mod_values[0]);
safe_free((void **)&lm[1]);
safe_free((void **)&lm[0]);
safe_free((void **)&lm);
return -98;
}
/* dn will be ou=somedomain.com,o=vpopmail
* lm will be the ldap propoerties of somedomain.com
*/
ret = ldap_add_ext_s(ld, dn, lm, NULL, NULL);
if (ret != LDAP_SUCCESS) {
fprintf(stderr, "%s\n", ldap_err2string(ret));
return -99;
}
safe_free((void **)&dn);
safe_free((void **)&lm[0]->mod_type);
safe_free((void **)&lm[1]->mod_type);
safe_free((void **)&lm[0]->mod_values[0]);
safe_free((void **)&lm[1]->mod_values[0]);
safe_free((void **)&lm[2]);
safe_free((void **)&lm[1]);
safe_free((void **)&lm[0]);
safe_free((void **)&lm);
if (ret != LDAP_SUCCESS) {
if (ret == LDAP_ALREADY_EXISTS) return VA_USERNAME_EXISTS;
return -99;
}
return VA_SUCCESS;
}
/***************************************************************************/
int vauth_deldomain(char *domain) {
int ret = 0;
size_t len = 0;
char *dn = NULL;
struct vqpasswd *pw = NULL;
/* make a connection to the ldap server, if we dont have one already */
if (ld == NULL) {
if (ldap_connect() != 0) return -99;
}
len = strlen(domain) + strlen(VLDAP_BASEDN) + 4 + 1;
/* dn will be of the format :
* ou=somedomain.com,o=vpopmail
*/
if (compose_dn(&dn, domain) != 0) return -98;
/* loop through all the users in the domain, deleting each one */
for (pw = vauth_getall(domain, 1, 0); pw; pw = vauth_getall(domain, 0, 0))
vauth_deluser(pw->pw_name, domain);
/* next, delete the actual domain */
ret = ldap_delete_ext_s(ld, dn, NULL, NULL);
safe_free((void **)&dn);
if (ret != LDAP_SUCCESS) {
fprintf(stderr, "%s\n", ldap_err2string(ret));
return -99;
}
#ifdef VALIAS
valias_delete_domain(domain);
#endif
return VA_SUCCESS;
}
/***************************************************************************/
int vauth_vpasswd(char *user, char *domain, char *crypted, int apop) {
int ret = 0;
struct vqpasswd *pw = NULL;
pw = vauth_getpw(user, domain);
if (pw == NULL) return VA_USER_DOES_NOT_EXIST;
pw->pw_passwd = safe_strdup(crypted);
ret = vauth_setpw(pw, domain);
return ret;
}
/***************************************************************************/
int vauth_deluser(char *user, char *domain) {
int ret = 0;
size_t len = 0;
char *dn = NULL;
char *dn_tmp = NULL;
/* make a connection to the ldap server if we dont have one already */
if (ld == NULL) {
if (ldap_connect() != 0) return -99;
}
len = 4 + strlen(user) + 2 + strlen(VLDAP_BASEDN) + 4 + strlen(domain) + 1;
/* make dn_tmp to be of the format
* ou=somedomain.com,o=vpopmail
*/
if (compose_dn(&dn_tmp, domain) != 0) return -98;
dn = (char *)safe_malloc(len);
memset((char *)dn, 0, len);
/* make dn to be of the format
* uid=someuser, ou=somedomain.com,o=vpopmail
*/
snprintf(dn, len, "uid=%s, %s", user, dn_tmp);
safe_free((void **)&dn_tmp);
/* delete the user */
ret = ldap_delete_ext_s(ld, dn, NULL, NULL);
safe_free((void **)&dn);
if (ret != LDAP_SUCCESS) {
fprintf(stderr, "%s\n", ldap_err2string(ret));
return -99;
}
return VA_SUCCESS;
}
/***************************************************************************/
int vauth_setquota(char *username, char *domain, char *quota) {
int ret = 0;
struct vqpasswd *pw = NULL;
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);
pw = vauth_getpw(username, domain);
if ((pw == NULL) && (verrori != 0))
return verrori;
else if (pw == NULL)
return VA_USER_DOES_NOT_EXIST;
pw->pw_shell = safe_strdup(quota);
ret = vauth_setpw(pw, domain);
return ret;
}
/***************************************************************************/
int vauth_setpw(struct vqpasswd *inpw, char *domain) {
int ret = 0;
size_t len = 0;
char *dn = NULL;
char *dn_tmp = NULL;
LDAPMod **lm = NULL;
int i;
#ifdef SQWEBMAIL_PASS
uid_t uid;
gid_t gid;
#endif
#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
ret = vcheck_vqpw(inpw, domain);
if (ret != 0) {
return (ret);
}
if (ld == NULL) {
if (ldap_connect() != 0) return -99;
}
lm = (LDAPMod **)malloc(sizeof(LDAPMod *) * NUM_LDAP_FIELDS + 1);
for (i = 0; i < NUM_LDAP_FIELDS; ++i) {
lm[i] = (LDAPMod *)safe_malloc(sizeof(LDAPMod));
memset((LDAPMod *)lm[i], 0, sizeof(LDAPMod));
lm[i]->mod_op = LDAP_MOD_REPLACE;
lm[i]->mod_values = (char **)safe_malloc(sizeof(char *) * 2);
lm[i]->mod_values[1] = NULL;
lm[i]->mod_type = safe_strdup(ldap_fields[i]);
}
lm[NUM_LDAP_FIELDS] = NULL;
lm[0]->mod_values[0] = safe_strdup(inpw->pw_name);
lm[1]->mod_values[0] = safe_malloc(strlen(inpw->pw_passwd) + 7 + 1);
#ifdef MD5_PASSWORDS
snprintf(lm[1]->mod_values[0], strlen(inpw->pw_passwd) + 7 + 1, "{MD5}%s",
inpw->pw_passwd);
#else
snprintf(lm[1]->mod_values[0], strlen(inpw->pw_passwd) + 7 + 1, "{crypt}%s",
inpw->pw_passwd);
#endif
lm[2]->mod_values[0] = (char *)safe_malloc(10);
sprintf(lm[2]->mod_values[0], "%d", inpw->pw_uid);
lm[3]->mod_values[0] = (char *)safe_malloc(10);
sprintf(lm[3]->mod_values[0], "%d", inpw->pw_gid);
if (inpw->pw_gecos == NULL) {
lm[4]->mod_values[0] = safe_strdup("");
} else {
lm[4]->mod_values[0] = safe_strdup(inpw->pw_gecos);
}
lm[5]->mod_values[0] = safe_strdup(inpw->pw_dir);
lm[6]->mod_values[0] = safe_strdup(inpw->pw_shell);
#ifdef CLEAR_PASS
lm[7]->mod_values[0] = safe_strdup(inpw->pw_clear_passwd);
#endif
lm[NUM_LDAP_FIELDS - 1]->mod_values[0] = strdup("qmailUser");
if (compose_dn(&dn_tmp, domain) != 0) {
safe_free((void **)&lm);
return -98;
}
len = 4 + strlen(inpw->pw_name) + 2 + strlen(VLDAP_BASEDN) + 4 +
strlen(domain) + 1;
dn = (char *)safe_malloc(len);
memset((char *)dn, 0, len);
snprintf(dn, len, "uid=%s, %s", inpw->pw_name, dn_tmp);
ret = ldap_modify_ext_s(ld, dn, lm, NULL, NULL);
safe_free((void **)&dn);
for (i = 0; i < NUM_LDAP_FIELDS; ++i) safe_free((void **)&lm);
if (ret != LDAP_SUCCESS) {
fprintf(stderr, "%s\n", ldap_err2string(ret));
return -99;
}
/* MARK */
#ifdef SQWEBMAIL_PASS
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 VA_SUCCESS;
}
/***************************************************************************/
/* 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(void) {
if (ld) {
ldap_unbind_ext_s(ld, NULL, NULL);
ld = NULL;
}
}
/***************************************************************************/
char *dc_filename(char *domain, uid_t uid, gid_t gid) {
static char dir_control_file[MAX_DIR_NAME];