8 #ifdef ENABLE_MEMCACHED_SASL
9 #ifdef HAVE_SASL_CB_GETCONF
13 const char *
const locations[] = {
14 "/etc/sasl/memcached.conf",
15 "/etc/sasl2/memcached.conf",
20 #ifdef ENABLE_SASL_PWDB
21 #define MAX_ENTRY_LEN 256
23 static const char *memcached_sasl_pwdb;
30 struct propctx *propctx)
32 size_t unmlen = strlen(user);
33 if ((passlen + unmlen) > (MAX_ENTRY_LEN - 4)) {
34 settings.extensions.logger->
log(EXTENSION_LOG_WARNING, NULL,
35 "WARNING: Failed to authenticate <%s> due to too long password (%d)",
40 FILE *pwfile = fopen(memcached_sasl_pwdb,
"r");
51 char buffer[MAX_ENTRY_LEN];
54 while ((fgets(buffer,
sizeof(buffer), pwfile)) != NULL) {
55 if (memcmp(user, buffer, unmlen) == 0 && buffer[unmlen] ==
':') {
58 if (memcmp(pass, buffer + unmlen, passlen) == 0 &&
59 (buffer[unmlen + passlen] ==
':' ||
60 buffer[unmlen + passlen] ==
'\n' ||
61 buffer[unmlen + passlen] ==
'\r'||
62 buffer[unmlen + passlen] ==
'\0')) {
75 settings.extensions.logger->
log(EXTENSION_LOG_INFO, NULL,
76 "INFO: User <%s> failed to authenticate",
84 #ifdef HAVE_SASL_CB_GETCONF
85 static int sasl_getconf(
void *context,
const char **path)
87 *path = getenv(
"SASL_CONF_PATH");
90 for (
int i = 0; locations[
i] != NULL; ++
i) {
91 if (access(locations[
i], F_OK) == 0) {
100 settings.extensions.logger->
log(EXTENSION_LOG_INFO, NULL,
101 "Reading configuration from: <%s>", *path);
103 settings.extensions.logger->
log(EXTENSION_LOG_INFO, NULL,
104 "Failed to locate a config path");
109 return (*path != NULL) ? SASL_OK : SASL_FAIL;
114 static int sasl_log(
void *context,
int level,
const char *
message)
116 EXTENSION_LOG_LEVEL lvl = EXTENSION_LOG_DETAIL;
125 lvl = EXTENSION_LOG_DEBUG;
129 lvl = EXTENSION_LOG_INFO;
137 "SASL (severity %d): %s", level, message);
144 #ifdef ENABLE_SASL_PWDB
145 { SASL_CB_SERVER_USERDB_CHECKPASS, sasl_server_userdb_checkpass, NULL },
149 { SASL_CB_LOG, sasl_log, NULL },
152 #ifdef HAVE_SASL_CB_GETCONF
153 { SASL_CB_GETCONF, sasl_getconf, NULL },
156 { SASL_CB_LIST_END, NULL, NULL }
159 void init_sasl(
void) {
160 #ifdef ENABLE_SASL_PWDB
161 memcached_sasl_pwdb = getenv(
"MEMCACHED_SASL_PWDB");
162 if (memcached_sasl_pwdb == NULL) {
164 settings.extensions.logger->
log(EXTENSION_LOG_INFO, NULL,
165 "INFO: MEMCACHED_SASL_PWDB not specified. "
166 "Internal passwd database disabled.");
168 sasl_callbacks[0].id = SASL_CB_LIST_END;
169 sasl_callbacks[0].proc = NULL;
173 if (sasl_server_init(sasl_callbacks,
"memcached") != SASL_OK) {
174 settings.extensions.logger->
log(EXTENSION_LOG_WARNING, NULL,
175 "Error initializing sasl.");
180 int result=sasl_server_new(
"memcached",
181 NULL, NULL, NULL, NULL,
182 NULL, 0, &c.sasl_conn);
184 sasl_dispose(&c.sasl_conn);
189 settings.extensions.logger->
log(EXTENSION_LOG_INFO, NULL,
190 "Initialized SASL.");