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
|
/*
* ProFTPD - mod_auth_otp database storage
* Copyright (c) 2015-2017 TJ Saunders
*
* 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., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA.
*
* As a special exemption, TJ Saunders and other respective copyright holders
* give permission to link this program with OpenSSL, and distribute the
* resulting executable, without including the source code for OpenSSL in the
* source distribution.
*/
#include "mod_auth_otp.h"
#include "mod_sql.h"
#include "base32.h"
#include "db.h"
#define AUTH_OTP_SQL_VALUE_BUFSZ 32
/* Max number of attempts for lock requests */
#define AUTH_OTP_MAX_LOCK_ATTEMPTS 10
static const char *trace_channel = "auth_otp";
static char *db_get_name(pool *p, const char *name) {
cmdtable *cmdtab;
cmd_rec *cmd;
modret_t *res;
/* Find the cmdtable for the sql_escapestr command. */
cmdtab = pr_stash_get_symbol2(PR_SYM_HOOK, "sql_escapestr", NULL, NULL, NULL);
if (cmdtab == NULL) {
pr_log_writefile(auth_otp_logfd, MOD_AUTH_OTP_VERSION,
"error: unable to find SQL hook symbol 'sql_escapestr'");
return pstrdup(p, name);
}
if (strlen(name) == 0) {
return pstrdup(p, "");
}
cmd = pr_cmd_alloc(p, 1, pr_str_strip(p, (char *) name));
/* Call the handler. */
res = pr_module_call(cmdtab->m, cmdtab->handler, cmd);
/* Check the results. */
if (MODRET_ISDECLINED(res) ||
MODRET_ISERROR(res)) {
pr_log_writefile(auth_otp_logfd, MOD_AUTH_OTP_VERSION,
"error executing 'sql_escapestring'");
return pstrdup(p, name);
}
return res->data;
}
int auth_otp_db_close(struct auth_otp_db *dbh) {
if (dbh->db_lockfd > 0) {
(void) close(dbh->db_lockfd);
dbh->db_lockfd = -1;
}
destroy_pool(dbh->pool);
return 0;
}
struct auth_otp_db *auth_otp_db_open(pool *p, const char *tabinfo) {
struct auth_otp_db *dbh = NULL;
pool *db_pool = NULL, *tmp_pool = NULL;
char *ptr, *ptr2, *named_query, *select_query = NULL, *update_query = NULL;
config_rec *c;
/* The tabinfo should look like:
* "/<select-named-query>/<update-named-query>"
*
* Parse the named queries out of the string, and store them in the db
* handle.
*/
ptr = strchr(tabinfo, '/');
if (ptr == NULL) {
pr_log_writefile(auth_otp_logfd, MOD_AUTH_OTP_VERSION,
"error: badly formatted table info '%s'", tabinfo);
errno = EINVAL;
return NULL;
}
ptr2 = strchr(ptr + 1, '/');
if (ptr2 == NULL) {
pr_log_writefile(auth_otp_logfd, MOD_AUTH_OTP_VERSION,
"error: badly formatted table info '%s'", tabinfo);
errno = EINVAL;
return NULL;
}
db_pool = make_sub_pool(p);
pr_pool_tag(db_pool, "Auth OTP Table Pool");
dbh = pcalloc(db_pool, sizeof(struct auth_otp_db));
dbh->pool = db_pool;
tmp_pool = make_sub_pool(p);
*ptr2 = '\0';
select_query = pstrdup(dbh->pool, ptr + 1);
/* Verify that the named query has indeed been defined. This is based on how
* mod_sql creates its config_rec names.
*/
named_query = pstrcat(tmp_pool, "SQLNamedQuery_", select_query, NULL);
c = find_config(main_server->conf, CONF_PARAM, named_query, FALSE);
if (c == NULL) {
pr_log_writefile(auth_otp_logfd, MOD_AUTH_OTP_VERSION,
"error: unable to resolve SQLNamedQuery name '%s'", select_query);
destroy_pool(tmp_pool);
errno = EINVAL;
return NULL;
}
*ptr = *ptr2 = '/';
ptr = ptr2;
ptr2 = strchr(ptr + 1, '/');
if (ptr2 != NULL) {
*ptr2 = '\0';
}
update_query = pstrdup(dbh->pool, ptr + 1);
if (ptr2 != NULL) {
*ptr2 = '/';
}
named_query = pstrcat(tmp_pool, "SQLNamedQuery_", update_query, NULL);
c = find_config(main_server->conf, CONF_PARAM, named_query, FALSE);
if (c == NULL) {
pr_log_writefile(auth_otp_logfd, MOD_AUTH_OTP_VERSION,
"error: unable to resolve SQLNamedQuery name '%s'", update_query);
destroy_pool(tmp_pool);
errno = EINVAL;
return NULL;
}
destroy_pool(tmp_pool);
dbh->select_query = select_query;
dbh->update_query = update_query;
/* Prepare the lock structure. */
dbh->db_lock.l_whence = SEEK_CUR;
dbh->db_lock.l_start = 0;
dbh->db_lock.l_len = 0;
return dbh;
}
int auth_otp_db_get_user_info(pool *p, struct auth_otp_db *dbh,
const char *user, const unsigned char **secret, size_t *secret_len,
unsigned long *counter) {
int res;
pool *tmp_pool = NULL;
cmdtable *sql_cmdtab = NULL;
cmd_rec *sql_cmd = NULL;
modret_t *sql_res = NULL;
array_header *sql_data = NULL;
const char *select_query = NULL;
char *encoded, **values = NULL;
size_t encoded_len;
unsigned int nvalues = 0;
if (dbh == NULL ||
user == NULL ||
secret == NULL ||
secret_len == NULL) {
errno = EINVAL;
return -1;
}
/* Allocate a temporary pool for the duration of this lookup. */
tmp_pool = make_sub_pool(p);
/* Find the cmdtable for the sql_lookup command. */
sql_cmdtab = pr_stash_get_symbol2(PR_SYM_HOOK, "sql_lookup", NULL, NULL,
NULL);
if (sql_cmdtab == NULL) {
pr_log_writefile(auth_otp_logfd, MOD_AUTH_OTP_VERSION,
"error: unable to find SQL hook symbol 'sql_lookup'");
destroy_pool(tmp_pool);
errno = EPERM;
return -1;
}
/* Prepare the SELECT query. */
select_query = dbh->select_query;
sql_cmd = pr_cmd_alloc(tmp_pool, 3, "sql_lookup", select_query,
db_get_name(tmp_pool, user));
/* Call the handler. */
sql_res = pr_module_call(sql_cmdtab->m, sql_cmdtab->handler, sql_cmd);
/* Check the results. */
if (sql_res == NULL ||
MODRET_ISERROR(sql_res)) {
pr_log_writefile(auth_otp_logfd, MOD_AUTH_OTP_VERSION,
"error processing SQLNamedQuery '%s'", select_query);
destroy_pool(tmp_pool);
errno = EPERM;
return -1;
}
sql_data = (array_header *) sql_res->data;
/* The expected number of items in the result set depends on whether we
* want/need the HOTP counter. If not, then it's only 1 (for the secret),
* otherwise 2 (secret and current counter).
*/
nvalues = (counter ? 2 : 1);
if (sql_data->nelts < nvalues) {
if (sql_data->nelts > 0) {
pr_log_writefile(auth_otp_logfd, MOD_AUTH_OTP_VERSION,
"error: SQLNamedQuery '%s' returned incorrect number of values (%d)",
select_query, sql_data->nelts);
}
destroy_pool(tmp_pool);
errno = (sql_data->nelts == 0) ? ENOENT : EINVAL;
return -1;
}
values = sql_data->elts;
/* Don't forget to base32-decode the value from the database. */
encoded = values[0];
encoded_len = strlen(encoded);
res = auth_otp_base32_decode(p, (const unsigned char *) encoded, encoded_len,
secret, secret_len);
if (res < 0) {
(void) pr_log_writefile(auth_otp_logfd, MOD_AUTH_OTP_VERSION,
"error base32-decoding value from database: %s", strerror(errno));
errno = EPERM;
return -1;
}
pr_memscrub(values[0], *secret_len);
if (counter != NULL) {
*counter = (unsigned long) atol(values[1]);
}
destroy_pool(tmp_pool);
return 0;
}
int auth_otp_db_have_user_info(pool *p, struct auth_otp_db *dbh,
const char *user) {
int res, xerrno = 0;
const unsigned char *secret = NULL;
size_t secret_len = 0;
res = auth_otp_db_get_user_info(p, dbh, user, &secret, &secret_len, NULL);
xerrno = errno;
if (res == 0) {
pr_memscrub((void *) secret, secret_len);
}
errno = xerrno;
return res;
}
int auth_otp_db_update_counter(struct auth_otp_db *dbh, const char *user,
unsigned long counter) {
pool *tmp_pool = NULL;
cmdtable *sql_cmdtab = NULL;
cmd_rec *sql_cmd = NULL;
modret_t *sql_res = NULL;
const char *update_query = NULL;
char *counter_str = NULL;
size_t counter_len = 0;
if (dbh == NULL ||
user == NULL) {
errno = EINVAL;
return -1;
}
/* Allocate a temporary pool for the duration of this change. */
tmp_pool = make_sub_pool(dbh->pool);
/* Find the cmdtable for the sql_change command. */
sql_cmdtab = pr_stash_get_symbol2(PR_SYM_HOOK, "sql_change", NULL, NULL,
NULL);
if (sql_cmdtab == NULL) {
pr_log_writefile(auth_otp_logfd, MOD_AUTH_OTP_VERSION,
"error: unable to find SQL hook symbol 'sql_change'");
destroy_pool(tmp_pool);
return -1;
}
update_query = dbh->update_query;
counter_len = AUTH_OTP_SQL_VALUE_BUFSZ * sizeof(char);
counter_str = pcalloc(tmp_pool, counter_len);
pr_snprintf(counter_str, counter_len-1, "%lu", counter);
sql_cmd = pr_cmd_alloc(tmp_pool, 4, "sql_change", update_query,
db_get_name(tmp_pool, user), counter_str);
/* Call the handler. */
sql_res = pr_module_call(sql_cmdtab->m, sql_cmdtab->handler, sql_cmd);
/* Check the results. */
if (sql_res == NULL ||
MODRET_ISERROR(sql_res)) {
pr_log_writefile(auth_otp_logfd, MOD_AUTH_OTP_VERSION,
"error processing SQLNamedQuery '%s'", update_query);
destroy_pool(tmp_pool);
errno = EPERM;
return -1;
}
destroy_pool(tmp_pool);
return 0;
}
/* Locking routines */
static const char *get_lock_type(struct flock *lock) {
const char *lock_type;
switch (lock->l_type) {
case F_RDLCK:
lock_type = "read-lock";
break;
case F_WRLCK:
lock_type = "write-lock";
break;
case F_UNLCK:
lock_type = "unlock";
break;
default:
lock_type = "[unknown]";
}
return lock_type;
}
static int do_lock(int fd, struct flock *lock) {
unsigned int nattempts = 1;
const char *lock_type;
lock_type = get_lock_type(lock);
pr_trace_msg(trace_channel, 9,
"attempt #%u to %s AuthOTPTableLock fd %d", nattempts, lock_type, fd);
while (fcntl(fd, F_SETLK, lock) < 0) {
int xerrno = errno;
if (xerrno == EINTR) {
pr_signals_handle();
continue;
}
pr_trace_msg(trace_channel, 3,
"%s (attempt #%u) of AuthOTPTableLock fd %d failed: %s", lock_type,
nattempts, fd, strerror(xerrno));
if (xerrno == EACCES) {
struct flock locker;
/* Get the PID of the process blocking this lock. */
if (fcntl(fd, F_GETLK, &locker) == 0) {
pr_trace_msg(trace_channel, 3, "process ID %lu has blocking %s lock on "
"AuthOTPTableLock fd %d", (unsigned long) locker.l_pid,
get_lock_type(&locker), fd);
}
}
if (xerrno == EAGAIN ||
xerrno == EACCES) {
/* Treat this as an interrupted call, call pr_signals_handle() (which
* will delay for a few msecs because of EINTR), and try again.
* After MAX_LOCK_ATTEMPTS attempts, give up altogether.
*/
nattempts++;
if (nattempts <= AUTH_OTP_MAX_LOCK_ATTEMPTS) {
errno = EINTR;
pr_signals_handle();
errno = 0;
pr_trace_msg(trace_channel, 9,
"attempt #%u to %s AuthOTPTableLock fd %d", nattempts, lock_type, fd);
continue;
}
pr_trace_msg(trace_channel, 9, "unable to acquire %s on "
"AuthOTPTableLock fd %d after %u attempts: %s", lock_type, fd,
nattempts, strerror(xerrno));
}
errno = xerrno;
return -1;
}
pr_trace_msg(trace_channel, 9,
"%s of AuthOTPTableLock fd %d successful after %u %s", lock_type, fd,
nattempts, nattempts != 1 ? "attempts" : "attempt");
return 0;
}
int auth_otp_db_rlock(struct auth_otp_db *dbh) {
int res = 0;
if (dbh->db_lockfd > 0) {
dbh->db_lock.l_type = F_RDLCK;
res = do_lock(dbh->db_lockfd, &dbh->db_lock);
}
return res;
}
int auth_otp_db_wlock(struct auth_otp_db *dbh) {
int res = 0;
if (dbh->db_lockfd > 0) {
dbh->db_lock.l_type = F_WRLCK;
res = do_lock(dbh->db_lockfd, &dbh->db_lock);
}
return res;
}
int auth_otp_db_unlock(struct auth_otp_db *dbh) {
int res = 0;
if (dbh->db_lockfd > 0) {
dbh->db_lock.l_type = F_UNLCK;
res = do_lock(dbh->db_lockfd, &dbh->db_lock);
}
return res;
}
|