-
Notifications
You must be signed in to change notification settings - Fork 55
/
isasl.c
388 lines (342 loc) · 10.1 KB
/
isasl.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
#include "config.h"
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <assert.h>
#include <ctype.h>
#include <stdint.h>
#include <pthread.h>
#include <stdbool.h>
#include <sys/stat.h>
#include "hash.h"
#include "isasl.h"
#include "memcached.h"
static struct stat prev_stat = { 0 };
static pthread_mutex_t uhash_lock = PTHREAD_MUTEX_INITIALIZER;
static pthread_mutex_t sasl_db_thread_lock = PTHREAD_MUTEX_INITIALIZER;
static bool run_sasl_db_thread;
static pthread_t sasl_db_thread_tid;
static user_db_entry_t **user_ht;
static const int n_uht_buckets = 12289;
static void kill_whitey(char *s) {
for(int i = strlen(s) - 1; i > 0 && isspace(s[i]); i--) {
s[i] = '\0';
}
}
static int u_hash_key(const char *u)
{
uint32_t h = mc_hash(u, strlen(u), 0) % n_uht_buckets;
assert(h < n_uht_buckets);
return h;
}
static char *find_pw(const char *u, char **cfg)
{
assert(u);
assert(user_ht);
int h = u_hash_key(u);
user_db_entry_t *e = user_ht[h];
while (e && strcmp(e->username, u) != 0) {
e = e->next;
}
if (e != NULL) {
*cfg = e->config;
return e->password;
} else {
return NULL;
}
}
static void store_pw(user_db_entry_t **ht, const char *u, const char *p, const char *cfg)
{
assert(ht);
assert(u);
assert(p);
user_db_entry_t *e = calloc(1, sizeof(user_db_entry_t));
assert(e);
e->username = strdup(u);
assert(e->username);
e->password = strdup(p);
assert(e->password);
e->config = cfg ? strdup(cfg) : NULL;
assert(!cfg || e->config);
int h = u_hash_key(u);
e->next = ht[h];
ht[h] = e;
}
static void free_user_ht(void)
{
if (user_ht) {
for (int i = 0; i < n_uht_buckets; i++) {
while (user_ht[i]) {
user_db_entry_t *e = user_ht[i];
user_db_entry_t *n = e->next;
free(e->username);
free(e->password);
free(e->config);
free(e);
user_ht[i] = n;
}
}
free(user_ht);
user_ht = NULL;
}
}
static const char *get_isasl_filename(void)
{
return getenv("ISASL_PWFILE");
}
static int load_user_db(void)
{
user_db_entry_t **new_ut = calloc(n_uht_buckets,
sizeof(user_db_entry_t*));
if (!new_ut) {
return SASL_NOMEM;
}
pthread_mutex_lock(&uhash_lock);
free_user_ht();
user_ht = new_ut;
pthread_mutex_unlock(&uhash_lock);
const char *filename = get_isasl_filename();
if (!filename) {
return SASL_OK;
}
FILE *sfile = fopen(filename, "r");
if (!sfile) {
return SASL_OK;
}
// File has lines that are newline terminated.
// File may have comment lines that must being with '#'.
// Lines should look like...
// <NAME><whitespace><PASSWORD><whitespace><CONFIG><optional_whitespace>
//
char up[128];
while (fgets(up, sizeof(up), sfile)) {
if (up[0] != '#') {
char *uname = up, *p = up, *cfg = NULL;
kill_whitey(up);
while (*p && !isspace(p[0])) {
p++;
}
// If p is pointing at a NUL, there's nothing after the username.
if (p[0] != '\0') {
p[0] = '\0';
p++;
}
// p now points to the first character after the (now)
// null-terminated username.
while (*p && isspace(*p)) {
p++;
}
// p now points to the first non-whitespace character
// after the above
cfg = p;
if (cfg[0] != '\0') {
// move cfg past the password
while (*cfg && !isspace(cfg[0])) {
cfg++;
}
if (cfg[0] != '\0') {
cfg[0] = '\0';
cfg++;
// Skip whitespace
while (*cfg && isspace(cfg[0])) {
cfg++;
}
}
}
store_pw(new_ut, uname, p, cfg);
}
}
fclose(sfile);
if (settings.verbose) {
fprintf(stderr, "Loaded isasl db from %s\n", filename);
}
return SASL_OK;
}
void sasl_dispose(sasl_conn_t **pconn)
{
free((*pconn)->username);
free((*pconn)->config);
free(*pconn);
*pconn = NULL;
}
static bool isasl_is_fresh(void)
{
bool rv = false;
struct stat st;
const char *filename = get_isasl_filename();
if (filename) {
if (stat(get_isasl_filename(), &st) < 0) {
perror(get_isasl_filename());
} else {
rv = prev_stat.st_mtime == st.st_mtime;
prev_stat = st;
}
}
return rv;
}
static void* check_isasl_db_thread(void* arg)
{
uint32_t sleep_time = *(int*)arg;
if (settings.verbose > 1) {
fprintf(stderr, "isasl checking DB every %us\n", sleep_time);
}
run_sasl_db_thread = true;
bool run = true;
while (run) {
sleep(sleep_time);
if (!isasl_is_fresh()) {
load_user_db();
}
pthread_mutex_lock(&sasl_db_thread_lock);
if (!run_sasl_db_thread) {
run = false;
}
pthread_mutex_unlock(&sasl_db_thread_lock);
}
return NULL;
}
void shutdown_sasl(void)
{
pthread_mutex_lock(&sasl_db_thread_lock);
run_sasl_db_thread = false;
pthread_mutex_unlock(&sasl_db_thread_lock);
pthread_join(sasl_db_thread_tid, NULL);
}
int sasl_server_init(const sasl_callback_t *callbacks,
const char *appname)
{
int rv = load_user_db();
if (rv == SASL_OK) {
static uint32_t sleep_time;
const char *sleep_time_str = getenv("ISASL_DB_CHECK_TIME");
pthread_attr_t attr;
if (pthread_attr_init(&attr) != 0 ||
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED) != 0)
{
perror("Failed to initialize pthread attributes");
exit(EX_OSERR);
}
if (! (sleep_time_str && safe_strtoul(sleep_time_str, &sleep_time))) {
// If we can't find a more frequent sleep time, set it to 60s.
sleep_time = 60;
}
if (get_isasl_filename() != NULL &&
pthread_create(&sasl_db_thread_tid, &attr, check_isasl_db_thread,
&sleep_time) != 0)
{
perror("couldn't create isasl db update thread.");
exit(EX_OSERR);
}
}
return rv;
}
int sasl_server_new(const char *service,
const char *serverFQDN,
const char *user_realm,
const char *iplocalport,
const char *ipremoteport,
const sasl_callback_t *callbacks,
unsigned flags,
sasl_conn_t **pconn)
{
*pconn = calloc(1, sizeof(sasl_conn_t));
return *pconn ? SASL_OK : SASL_NOMEM;
}
int sasl_listmech(sasl_conn_t *conn,
const char *user,
const char *prefix,
const char *sep,
const char *suffix,
const char **result,
unsigned *plen,
int *pcount)
{
// We use this in a very specific way in the codebase. If that ever
// changes, detect it quickly.
assert(strcmp(prefix, "") == 0);
assert(strcmp(sep, " ") == 0);
assert(strcmp(suffix, "") == 0);
*result = "PLAIN";
*plen = strlen(*result);
return SASL_OK;
}
static bool check_up(const char *username, const char *password, char **cfg)
{
pthread_mutex_lock(&uhash_lock);
char *pw = find_pw(username, cfg);
bool rv = pw && (strcmp(password, pw) == 0);
pthread_mutex_unlock(&uhash_lock);
return rv;
}
int sasl_server_start(sasl_conn_t *conn,
const char *mech,
const char *clientin,
unsigned clientinlen,
const char **serverout,
unsigned *serveroutlen)
{
int rv = SASL_FAIL;
*serverout = "";
*serveroutlen = 0;
if(strcmp(mech, "PLAIN") == 0) {
// The clientin string looks like "[authzid]\0username\0password"
while (clientinlen > 0 && clientin[0] != '\0') {
// Skip authzid
clientin++;
clientinlen--;
}
if (clientinlen > 2 && clientinlen < 128 && clientin[0] == '\0') {
const char *username = clientin + 1;
char password[256];
int pwlen = clientinlen - 2 - strlen(username);
assert(pwlen >= 0);
if (pwlen < 256) {
char *cfg = NULL;
password[pwlen] = '\0';
memcpy(password, clientin + 2 + strlen(username), pwlen);
if (check_up(username, password, &cfg)) {
if (conn->username) {
free(conn->username);
conn->username = NULL;
}
if (conn->config) {
free(conn->config);
conn->config = NULL;
}
conn->username = strdup(username);
assert(conn->username);
conn->config = strdup(cfg);
assert(conn->config);
rv = SASL_OK;
}
}
}
}
return rv;
}
int sasl_server_step(sasl_conn_t *conn,
const char *clientin,
unsigned clientinlen,
const char **serverout,
unsigned *serveroutlen)
{
// This is only useful when the above returns SASL_CONTINUE. In this
// implementation, only PLAIN is supported, so it never will.
return SASL_FAIL;
}
int sasl_getprop(sasl_conn_t *conn, int propnum,
const void **pvalue)
{
switch (propnum) {
case SASL_USERNAME:
*pvalue = conn->username;
break;
case ISASL_CONFIG:
*pvalue = conn->config;
break;
default:
return SASL_BADPARAM;
}
return SASL_OK;
}