1 #include <stdlib.h>
2 #include <string.h>
3 #include <bglibs/ucspi.h>
4 #include "sasl.h"
5 #include "sasl_internal.h"
6
7 static struct sasl_mechanism mechanisms[4] = {
8 { "LOGIN", "CVM_SASL_PLAIN", 0, sasl_login_start, 0 },
9 { "PLAIN", "CVM_SASL_PLAIN", 0, sasl_plain_start, 0 },
10 { "CRAM-MD5", "CVM_SASL_CRAM_MD5", 0, sasl_cram_md5_start, 0 },
11 { 0, 0, 0, 0, 0 },
12 };
13
14 const struct sasl_mechanism* sasl_mechanisms;
15
set_mech(struct sasl_mechanism * mech,const char * cvm,struct sasl_mechanism ** prev,struct sasl_mechanism ** first)16 static void set_mech(struct sasl_mechanism* mech, const char* cvm,
17 struct sasl_mechanism** prev,
18 struct sasl_mechanism** first)
19 {
20 mech->cvm = cvm;
21 if (*prev != 0)
22 (*prev)->next = mech;
23 else if (*first == 0)
24 *prev = *first = mech;
25 }
26
sasl_init(struct sasl_state * ss)27 int sasl_init(struct sasl_state* ss)
28 {
29 struct sasl_mechanism* prev;
30 struct sasl_mechanism* first;
31 struct sasl_mechanism* mech;
32 const char* tmp;
33 for (mech = mechanisms, first = 0, prev = 0; mech->name != 0; ++mech) {
34 if ((tmp = getenv(mech->var)) != 0)
35 set_mech(mech, tmp, &prev, &first);
36 }
37 /* backwards compatibility for $CVM_SASL_LOGIN */
38 if (!mechanisms[0].cvm && (tmp = getenv("CVM_SASL_LOGIN")) != 0) {
39 prev = 0;
40 first = 0;
41 set_mech(&mechanisms[0], tmp, &prev, &first);
42 }
43 sasl_mechanisms = first;
44 memset(ss, 0, sizeof *ss);
45 ss->domain = ucspi_localhost();
46 return 1;
47 }
48