pam_mysql.c 114 KB
Newer Older
1 2 3 4
/*
 * PAM module for MySQL
 *
 * Copyright (C) 1998-2005 Gunay Arslan and the contributors.
5
 * Copyright (C) 2015-2016 Nigel Cunningham and contributors.
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
 * All rights reserved.
 * 
 * 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.
 *
 * Original Version written by: Gunay ARSLAN <arslan@gunes.medyatext.com.tr>
 * This version by: James O'Kane <jo2y@midnightlinux.com>
 * Modifications by Steve Brown, <steve@electronic.co.uk>
Nigel Cunningham's avatar
Nigel Cunningham committed
25 26
 *      B.J. Black, <bj@taos.com>
 *      Kyle Smith, <kyle@13th-floor.org>
27 28 29 30 31 32 33 34
 * Patch integration by Moriyoshi Koizumi, <moriyoshi@at.wakwak.com>,
 * based on the patches by the following contributors:
 *        Paul Bryan (check_acct_mgmt service support)
 *        Kev Green (Documentation, UNIX socket option)
 *        Kees Cook (Misc. clean-ups and more)
 *        Fredrik Rambris (RPM spec file)
 *        Peter E. Stokke (chauthtok service support)
 *        Sergey Matveychuk (OpenPAM support)
35 36
 * Package unmaintained for some years; now taken care of by Nigel Cunningham
 *   https://github.com/NigelCunningham/pam-MySQL
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
 */

#define _GNU_SOURCE

#define PAM_SM_AUTH
#define PAM_SM_ACCOUNT
#define PAM_SM_SESSION
#define PAM_SM_PASSWORD

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

/* {{{ includes */

#include <stdio.h>
#include <stdlib.h>

#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif

#ifdef HAVE_SYSLOG_H
#include <syslog.h>
#endif

#ifdef HAVE_STDARG_H
#include <stdarg.h>
#endif

#ifdef HAVE_ALLOCA_H
#include <alloca.h>
#endif

#ifdef HAVE_STRING_H
#include <string.h>
#endif

#ifdef HAVE_STRINGS_H
#include <strings.h>
#endif

#ifdef HAVE_SYS_SOCKET_H
#include <sys/socket.h>
#endif

83
#include <time.h>
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

#ifdef HAVE_ERRNO_H
#include <errno.h>
#endif

#ifdef HAVE_FCNTL_H
#include <fcntl.h>
#endif

#ifdef HAVE_SYS_STAT_H
#include <sys/stat.h>
#endif

#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif

#ifdef HAVE_SYS_PARAM_H
#include <sys/param.h>
#endif

#ifdef HAVE_NETINET_IN_H
#include <netinet/in.h>
#endif

#ifdef HAVE_ARPA_INET_H
#include <arpa/inet.h>
#endif

#ifdef HAVE_NETDB_H
#include <netdb.h>
#endif

#ifdef HAVE_CRYPT_H
#include <crypt.h>
#endif

#ifndef HAVE_OPENSSL
#ifdef HAVE_MD5_H
#include <md5.h>
#endif

#if defined(HAVE_SASL_MD5_H) && (defined(HAVE_CYRUS_SASL_V1) || defined(HAVE_CYRUS_SASL_V2))
#define USE_SASL_MD5
#include <md5global.h>
#include <md5.h>
#endif
#endif

#ifdef HAVE_OPENSSL
#include <openssl/md5.h>
#include <openssl/sha.h>
#endif

#ifdef HAVE_MYSQL_H
#include <mysql.h>
#endif

/*
 * here, we make definitions for the externally accessible functions
 * in this file (these definitions are required for static modules
 * but strongly encouraged generally) they are used to instruct the
 * modules include file to define their prototypes.
 */

#define PAM_SM_AUTH
#define PAM_SM_ACCOUNT
#define PAM_SM_SESSION
#define PAM_SM_PASSWORD

154 155
#include <security/pam_appl.h>
#include <security/pam_modules.h>
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170

/* }}} */

#ifndef PAM_EXTERN
#define PAM_EXTERN
#endif

#ifndef LOG_AUTHPRIV
#define LOG_AUTHPRIV LOG_AUTH
#endif

#ifdef LINUX_PAM_CONST_BUG
#define PAM_AUTHTOK_RECOVERY_ERR PAM_AUTHTOK_RECOVER_ERR
#endif

171
#ifndef HAVE_MAKE_SCRAMBLED_PASSWORD
172 173 174 175
#include "crypto.h"
#include "crypto-sha1.h"

// Implementation from commit 2db6b50c7b7c638104bd9639994f0574e8f4813c in Pure-ftp source.
176
void make_scrambled_password(char scrambled_password[42], const char password[255])
177 178 179 180 181 182 183 184 185 186
{
	SHA1_CTX      ctx;
	unsigned char h0[20], h1[20];

	SHA1Init(&ctx);
	SHA1Update(&ctx, password, strlen(password));
	SHA1Final(h0, &ctx);
	SHA1Init(&ctx);
	SHA1Update(&ctx, h0, sizeof h0);
# ifdef HAVE_EXPLICIT_BZERO
187
    explicit_bzero(h0, strlen(password));
188 189 190 191
# else
    volatile unsigned char *pnt_ = (volatile unsigned char *) h0;
    size_t                     i = (size_t) 0U;

192
    while (i < strlen(password)) {
193 194 195 196 197 198 199 200 201 202 203
        pnt_[i++] = 0U;
    }
# endif

	SHA1Final(h1, &ctx);
	*scrambled_password = '*';
	hexify(scrambled_password + 1U, h1,
			42, sizeof h1);
}
#endif

204 205 206 207 208 209 210 211 212
#define PAM_MODULE_NAME  "pam_mysql"
#define PAM_MYSQL_LOG_PREFIX PAM_MODULE_NAME " - "
#define PLEASE_ENTER_PASSWORD "Password:"
#define PLEASE_ENTER_OLD_PASSWORD "(Current) Password:"
#define PLEASE_ENTER_NEW_PASSWORD "(New) Password:"
#define PLEASE_REENTER_NEW_PASSWORD "Retype (New) Password:"

/* {{{ consts  */
enum _pam_mysql_err_t {
Nigel Cunningham's avatar
Nigel Cunningham committed
213 214 215 216 217 218 219 220 221 222 223 224
  PAM_MYSQL_ERR_SUCCESS = 0,
  PAM_MYSQL_ERR_UNKNOWN = -1,
  PAM_MYSQL_ERR_NO_ENTRY = 1,
  PAM_MYSQL_ERR_ALLOC = 2,
  PAM_MYSQL_ERR_INVAL = 3,
  PAM_MYSQL_ERR_BUSY = 4,
  PAM_MYSQL_ERR_DB = 5,
  PAM_MYSQL_ERR_MISMATCH = 6,
  PAM_MYSQL_ERR_IO = 7,
  PAM_MYSQL_ERR_SYNTAX = 8,
  PAM_MYSQL_ERR_EOF = 9,
  PAM_MYSQL_ERR_NOTIMPL = 10
225 226 227
};

enum _pam_mysql_config_token_t {
Nigel Cunningham's avatar
Nigel Cunningham committed
228 229 230 231 232 233
  PAM_MYSQL_CONFIG_TOKEN_EQUAL = 0,
  PAM_MYSQL_CONFIG_TOKEN_NEWLINE,
  PAM_MYSQL_CONFIG_TOKEN_STRING,
  PAM_MYSQL_CONFIG_TOKEN_SEMICOLON,
  PAM_MYSQL_CONFIG_TOKEN_COMMENT,
  PAM_MYSQL_CONFIG_TOKEN__LAST
234 235 236 237 238 239
};

#define PAM_MYSQL_USER_STAT_EXPIRED         0x0001
#define PAM_MYSQL_USER_STAT_AUTHTOK_EXPIRED 0x0002
#define PAM_MYSQL_USER_STAT_NULL_PASSWD     0x0004

Nigel Cunningham's avatar
Nigel Cunningham committed
240 241
#define PAM_MYSQL_CAP_CHAUTHTOK_SELF    0x0001
#define PAM_MYSQL_CAP_CHAUTHTOK_OTHERS    0x0002
242
/* }}} */
Nigel Cunningham's avatar
Nigel Cunningham committed
243

244 245 246
/* {{{ typedefs */
/* {{{ typedef struct pam_mysql_ctx_t */
typedef struct _pam_mysql_ctx_t {
Nigel Cunningham's avatar
Nigel Cunningham committed
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
  MYSQL *mysql_hdl;
  char *host;
  char *where;
  char *db;
  char *user;
  char *passwd;
  char *table;
  char *update_table;
  char *usercolumn;
  char *passwdcolumn;
  char *statcolumn;
  int crypt_type;
  int use_323_passwd;
  int md5;
  int sqllog;
  int verbose;
  int use_first_pass;
  int try_first_pass;
  int disconnect_every_op;
  char *logtable;
  char *logmsgcolumn;
  char *logpidcolumn;
  char *logusercolumn;
  char *loghostcolumn;
  char *logrhostcolumn;
  char *logtimecolumn;
  char *config_file;
  char *my_host_info;
275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
} pam_mysql_ctx_t; /*Max length for most MySQL fields is 16 */
/* }}} */

/* {{{ typedef enum pam_mysql_err_t */
typedef enum _pam_mysql_err_t pam_mysql_err_t;
/* }}} */

/* {{{ typedef enum pam_mysql_config_token_t */
typedef enum _pam_mysql_config_token_t pam_mysql_config_token_t;
/* }}} */

/* {{{ typedef (func) pam_mysql_option_getter_t */
typedef int(*pam_mysql_option_getter_t)(void *val, const char **pretval, int *to_release);
/* }}} */

/* {{{ typedef (func) pam_mysql_option_setter_t */
typedef int(*pam_mysql_option_setter_t)(void *val, const char *newval_str);
/* }}} */

/* {{{ typedef struct pam_mysql_option_accessor_t */
typedef struct _pam_mysql_option_accessor_t {
Nigel Cunningham's avatar
Nigel Cunningham committed
296 297
  pam_mysql_option_getter_t get_op;
  pam_mysql_option_setter_t set_op;
298 299 300 301 302
} pam_mysql_option_accessor_t;
/* }}} */

/* {{{ typedef struct pam_mysql_option_t */
typedef struct _pam_mysql_option_t {
Nigel Cunningham's avatar
Nigel Cunningham committed
303 304 305 306
  const char *name;
  size_t name_len;
  size_t offset;
  pam_mysql_option_accessor_t *accessor;
307 308 309 310 311
} pam_mysql_option_t;
/* }}} */

/* {{{ typedef struct pam_mysql_str_t */
typedef struct _pam_mysql_str_t {
Nigel Cunningham's avatar
Nigel Cunningham committed
312 313 314 315
  char *p;
  size_t len;
  size_t alloc_size;
  int mangle;
316 317 318 319 320 321 322
} pam_mysql_str_t;
/* }}} */

/* {{{ typedef (func) pam_mysql_handle_entry_fn_t */
struct _pam_mysql_entry_handler_t;

typedef pam_mysql_err_t (*pam_mysql_handle_entry_fn_t)(
Nigel Cunningham's avatar
Nigel Cunningham committed
323 324
    struct _pam_mysql_entry_handler_t *, int, const char *, size_t,
    const char *, size_t);
325 326 327 328
/* }}} */

/* {{{ typedef struct pam_mysql_entry_handler_t */
typedef struct _pam_mysql_entry_handler_t {
Nigel Cunningham's avatar
Nigel Cunningham committed
329 330 331
  pam_mysql_ctx_t *ctx;
  pam_mysql_handle_entry_fn_t handle_entry_fn;
  pam_mysql_option_t *options;
332 333 334 335 336
} pam_mysql_entry_handler_t;
/* }}} */

/* {{{ typedef struct pam_mysql_config_parser_t */
typedef struct _pam_mysql_config_parser_t {
Nigel Cunningham's avatar
Nigel Cunningham committed
337 338
  pam_mysql_ctx_t *ctx;
  pam_mysql_entry_handler_t *hdlr;
339 340 341 342 343
} pam_mysql_config_parser_t;
/* }}} */

/* {{{ typedef struct pam_mysql_stream_t */
typedef struct _pam_mysql_stream_t {
Nigel Cunningham's avatar
Nigel Cunningham committed
344 345 346 347 348 349 350 351
  int fd;
  unsigned char buf[2][2048];
  unsigned char *buf_start;
  unsigned char *buf_ptr;
  unsigned char *buf_end;
  unsigned char *pushback;
  size_t buf_in_use;
  int eof;
352 353 354 355 356
} pam_mysql_stream_t;
/* }}} */

/* {{{ typedef struct _pam_mysql_config_scanner_t */
typedef struct _pam_mysql_config_scanner_t {
Nigel Cunningham's avatar
Nigel Cunningham committed
357 358 359 360 361
  pam_mysql_ctx_t *ctx;
  pam_mysql_str_t image;
  pam_mysql_config_token_t token;
  pam_mysql_stream_t *stream;
  int state;
362 363 364 365 366 367 368
} pam_mysql_config_scanner_t;
/* }}} */
/* }}} */

/* {{{ prototypes */
/* {{{ General PAM Prototypes */
PAM_EXTERN int pam_sm_authenticate(pam_handle_t *pamh,
Nigel Cunningham's avatar
Nigel Cunningham committed
369
    int flags, int argc, const char **argv);
370
PAM_EXTERN int pam_sm_chauthtok(pam_handle_t *pamh, int flags, int argc,
Nigel Cunningham's avatar
Nigel Cunningham committed
371
    const char **argv);
372
PAM_EXTERN int pam_sm_acct_mgmt(pam_handle_t *pamh, int flags, int argc,
Nigel Cunningham's avatar
Nigel Cunningham committed
373
    const char **argv);
374
PAM_EXTERN int pam_sm_setcred(pam_handle_t *pamh, int flags, int argc,
Nigel Cunningham's avatar
Nigel Cunningham committed
375
    const char **argv);
376
PAM_EXTERN int pam_sm_open_session(pam_handle_t *pamh, int flags, int argc,
Nigel Cunningham's avatar
Nigel Cunningham committed
377
    const char **argv);
378
PAM_EXTERN int pam_sm_close_session(pam_handle_t *pamh, int flags, int argc,
Nigel Cunningham's avatar
Nigel Cunningham committed
379
    const char **argv);
380 381 382 383 384 385 386 387 388 389 390 391 392 393 394
/* }}} */

/* {{{ static prototypes */ 
static void pam_mysql_cleanup_hdlr(pam_handle_t *pamh, void * voiddata, int status);

/* {{{ pam_mysql methods */
static pam_mysql_err_t pam_mysql_retrieve_ctx(pam_mysql_ctx_t **pretval, pam_handle_t *pamh);

static pam_mysql_err_t pam_mysql_init_ctx(pam_mysql_ctx_t *);
static void pam_mysql_destroy_ctx(pam_mysql_ctx_t *);
static void pam_mysql_saltify(pam_mysql_ctx_t *, char *salt, const char *salter );
static pam_mysql_err_t pam_mysql_parse_args(pam_mysql_ctx_t *, int argc, const char **argv);
static pam_mysql_err_t pam_mysql_open_db(pam_mysql_ctx_t *);
static void pam_mysql_close_db(pam_mysql_ctx_t *);
static pam_mysql_err_t pam_mysql_check_passwd(pam_mysql_ctx_t *ctx,
Nigel Cunningham's avatar
Nigel Cunningham committed
395
    const char *user, const char *passwd, int null_inhibited);
396
static pam_mysql_err_t pam_mysql_update_passwd(pam_mysql_ctx_t *,
Nigel Cunningham's avatar
Nigel Cunningham committed
397
    const char *user, const char *new_passwd);
398
static pam_mysql_err_t pam_mysql_query_user_stat(pam_mysql_ctx_t *,
Nigel Cunningham's avatar
Nigel Cunningham committed
399
    int *pretval, const char *user);
400
static pam_mysql_err_t pam_mysql_query_user_caps(pam_mysql_ctx_t *,
Nigel Cunningham's avatar
Nigel Cunningham committed
401
    int *pretval, const char *user);
402
static pam_mysql_err_t pam_mysql_sql_log(pam_mysql_ctx_t *, const char *msg,
Nigel Cunningham's avatar
Nigel Cunningham committed
403
    const char *user, const char *host);
404
static pam_mysql_err_t pam_mysql_get_host_info(pam_mysql_ctx_t *,
Nigel Cunningham's avatar
Nigel Cunningham committed
405
    const char **pretval);
406 407 408 409 410 411 412 413 414 415 416 417 418
/* }}} */

static size_t strnncpy(char *dest, size_t dest_size, const char *src, size_t src_len);
static void *xcalloc(size_t nmemb, size_t size);
static char *xstrdup(const char *ptr);
static void xfree(void *ptr);
static void xfree_overwrite(char *ptr);
/* }}} */
/* }}} */

/* {{{ strnncpy */
static size_t strnncpy(char *dest, size_t dest_size, const char *src, size_t src_len)
{
Nigel Cunningham's avatar
Nigel Cunningham committed
419 420 421
  size_t cpy_len;
  dest_size--;
  cpy_len = (dest_size < src_len ? dest_size: src_len);
422

Nigel Cunningham's avatar
Nigel Cunningham committed
423
  memcpy(dest, src, cpy_len);
424

Nigel Cunningham's avatar
Nigel Cunningham committed
425
  dest[cpy_len] = '\0';
426

Nigel Cunningham's avatar
Nigel Cunningham committed
427
  return cpy_len;
428 429 430 431 432 433
}
/* }}} */

/* {{{ xcalloc */
static void *xcalloc(size_t nmemb, size_t size)
{
Nigel Cunningham's avatar
Nigel Cunningham committed
434 435
  void *retval;
  double v = ((double)size) * (int)(nmemb & (((size_t)-1) >> 1));
436

Nigel Cunningham's avatar
Nigel Cunningham committed
437 438 439
  if (v != nmemb * size) {
    return NULL;
  }
440

Nigel Cunningham's avatar
Nigel Cunningham committed
441
  retval = calloc(nmemb, size);
442

Nigel Cunningham's avatar
Nigel Cunningham committed
443
  return retval;
444 445 446 447 448 449
}
/* }}} */

/* {{{ xrealloc */
static void *xrealloc(void *ptr, size_t nmemb, size_t size)
{
Nigel Cunningham's avatar
Nigel Cunningham committed
450 451
  void *retval;
  size_t total = nmemb * size;
452

Nigel Cunningham's avatar
Nigel Cunningham committed
453 454 455
  if (((double)size) * (int)(nmemb & (((size_t)-1) >> 1)) != total) {
    return NULL;
  }
456

Nigel Cunningham's avatar
Nigel Cunningham committed
457
  retval = realloc(ptr, total);
458

Nigel Cunningham's avatar
Nigel Cunningham committed
459
  return retval;
460 461 462 463 464 465
}
/* }}} */

/* {{{ xstrdup */
static char *xstrdup(const char *ptr)
{
Nigel Cunningham's avatar
Nigel Cunningham committed
466 467
  size_t len = strlen(ptr) + sizeof(char);
  char *retval = xcalloc(sizeof(char), len);
468

Nigel Cunningham's avatar
Nigel Cunningham committed
469 470 471
  if (retval == NULL) {
    return NULL;
  }
472

Nigel Cunningham's avatar
Nigel Cunningham committed
473
  memcpy(retval, ptr, len);
474

Nigel Cunningham's avatar
Nigel Cunningham committed
475
  return retval;
476 477 478 479 480 481
}
/* }}} */

/* {{{ xfree */
static void xfree(void *ptr)
{
Nigel Cunningham's avatar
Nigel Cunningham committed
482 483 484
  if (ptr != NULL) {
    free(ptr);
  }
485 486 487 488 489 490
}
/* }}} */

/* {{{ xfree_overwrite */
static void xfree_overwrite(char *ptr)
{
Nigel Cunningham's avatar
Nigel Cunningham committed
491 492 493 494 495 496 497
  if (ptr != NULL) {
    char *p;
    for (p = ptr; *p != '\0'; p++) {
      *p = '\0';
    }
    free(ptr);
  }
498 499 500 501 502
}
/* }}} */

/* {{{ memspn */
static void *memspn(void *buf, size_t buf_len, const unsigned char *delims,
Nigel Cunningham's avatar
Nigel Cunningham committed
503
    size_t ndelims)
504
{
Nigel Cunningham's avatar
Nigel Cunningham committed
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
  unsigned char *buf_end = ((unsigned char *)buf) + buf_len;
  unsigned char *p;

  switch (ndelims) {
    case 0:
      return buf_end;

    case 1: {
        unsigned char c = delims[0];

        for (p = (unsigned char *)buf; p < buf_end; p++) {
          if (*p != c) {
            return (void *)p;
          }
        }
      } break;

    case 2: {
        unsigned char c1 = delims[0], c2 = delims[1];

        for (p = (unsigned char *)buf; p < buf_end; p++) {
          if (*p != c1 && *p != c2) {
            return (void *)p;
          }
        }
      } break;

    default: {
         const unsigned char *delims_end = delims + ndelims;
         unsigned char and_mask = ~0, or_mask = 0;
         const unsigned char *q;

         for (q = delims; q < delims_end; q++) {
           and_mask &= *q;
           or_mask |= *q;
         }

         for (p = (unsigned char *)buf; p < buf_end; p++) {
           if ((*p & and_mask) == and_mask && (*p & or_mask) != 0) {
             for (q = delims; *p != *q; q++) {
               if (q >= delims_end) {
                 return (void *)p;
               }
             }
           }
         }
       } break;
  }

  return NULL;
555 556 557 558 559
}
/* }}} */

/* {{{ memcspn */
static void *memcspn(void *buf, size_t buf_len, const unsigned char *delims,
Nigel Cunningham's avatar
Nigel Cunningham committed
560
    size_t ndelims)
561
{
Nigel Cunningham's avatar
Nigel Cunningham committed
562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580
  if (ndelims == 1) {
    return memchr(buf, delims[0], buf_len);
  } else {
    unsigned char *buf_end = ((unsigned char *)buf) + buf_len;
    const unsigned char *delims_end = delims + ndelims;
    unsigned char *p;

    for (p = (unsigned char *)buf; p < buf_end; p++) {
      const unsigned char *q;

      for (q = delims; q < delims_end; q++) {
        if (*p == *q) {
          return (void *)p;
        }
      }
    }

    return NULL;
  }
581 582 583 584 585 586 587 588 589 590 591 592 593 594
}
/* }}} */

/* {{{ pam_mysql_md5_data
 * 
 * AFAIK, only FreeBSD has MD5Data() defined in md5.h
 * better MD5 support will appear in 0.5
 */
#ifdef HAVE_MD5DATA
#define HAVE_PAM_MYSQL_MD5_DATA
#define pam_mysql_md5_data MD5Data
#elif defined(HAVE_OPENSSL) || (defined(HAVE_SASL_MD5_H) && defined(USE_SASL_MD5)) || (!defined(HAVE_OPENSSL) && defined(HAVE_SOLARIS_MD5))
#if defined(USE_SASL_MD5)
static unsigned char *MD5(const unsigned char *d, unsigned int n,
Nigel Cunningham's avatar
Nigel Cunningham committed
595
    unsigned char *md)
596
{
Nigel Cunningham's avatar
Nigel Cunningham committed
597
  MD5_CTX ctx;
598

Nigel Cunningham's avatar
Nigel Cunningham committed
599
  _sasl_MD5Init(&ctx);
600

Nigel Cunningham's avatar
Nigel Cunningham committed
601
  _sasl_MD5Update(&ctx, (unsigned char *)d, n);
602

Nigel Cunningham's avatar
Nigel Cunningham committed
603
  _sasl_MD5Final(md, &ctx);
604

Nigel Cunningham's avatar
Nigel Cunningham committed
605
  return md;
606 607 608 609 610 611 612
}
#elif defined(USE_SOLARIS_MD5)
#define MD5(d, n, md) md5_calc(d, md, n)
#endif
#define HAVE_PAM_MYSQL_MD5_DATA
static char *pam_mysql_md5_data(const unsigned char *d, unsigned int sz, char *md)
{
Nigel Cunningham's avatar
Nigel Cunningham committed
613 614
  size_t i, j;
  unsigned char buf[16];
615

Nigel Cunningham's avatar
Nigel Cunningham committed
616 617 618 619 620
  if (md == NULL) {
    if ((md = xcalloc(32 + 1, sizeof(char))) == NULL) {
      return NULL;
    }
  }
621

Nigel Cunningham's avatar
Nigel Cunningham committed
622
  MD5(d, (unsigned long)sz, buf);
623

Nigel Cunningham's avatar
Nigel Cunningham committed
624 625 626 627 628
  for (i = 0, j = 0; i < 16; i++, j += 2) {
    md[j + 0] = "0123456789abcdef"[(int)(buf[i] >> 4)];
    md[j + 1] = "0123456789abcdef"[(int)(buf[i] & 0x0f)];
  }
  md[j] = '\0';
629

Nigel Cunningham's avatar
Nigel Cunningham committed
630
  return md;
631 632 633 634 635 636 637 638 639
}
#endif
/* }}} */

/* {{{ pam_mysql_sha1_data */
#if defined(HAVE_OPENSSL)
#define HAVE_PAM_MYSQL_SHA1_DATA
static char *pam_mysql_sha1_data(const unsigned char *d, unsigned int sz, char *md)
{
Nigel Cunningham's avatar
Nigel Cunningham committed
640 641
  size_t i, j;
  unsigned char buf[20];
642

Nigel Cunningham's avatar
Nigel Cunningham committed
643 644 645 646 647
  if (md == NULL) {
    if ((md = xcalloc(40 + 1, sizeof(char))) == NULL) {
      return NULL;
    }
  }
648

Nigel Cunningham's avatar
Nigel Cunningham committed
649
  SHA1(d, (unsigned long)sz, buf);
650

Nigel Cunningham's avatar
Nigel Cunningham committed
651 652 653 654 655
  for (i = 0, j = 0; i < 20; i++, j += 2) {
    md[j + 0] = "0123456789abcdef"[(int)(buf[i] >> 4)];
    md[j + 1] = "0123456789abcdef"[(int)(buf[i] & 0x0f)];
  }
  md[j] = '\0';
656

Nigel Cunningham's avatar
Nigel Cunningham committed
657
  return md;
658
}
659 660 661

static char *pam_mysql_sha512_data(const unsigned char *d, unsigned int sz, char *md)
{
Nigel Cunningham's avatar
Nigel Cunningham committed
662 663
  size_t i, j;
  unsigned char buf[64];
664

Nigel Cunningham's avatar
Nigel Cunningham committed
665 666 667 668 669
  if (md == NULL) {
    if ((md = xcalloc(128 + 1, sizeof(char))) == NULL) {
      return NULL;
    }
  }
670

Nigel Cunningham's avatar
Nigel Cunningham committed
671
  SHA512(d, (unsigned long)sz, buf);
672

Nigel Cunningham's avatar
Nigel Cunningham committed
673 674 675 676 677
  for (i = 0, j = 0; i < 64; i++, j += 2) {
    md[j + 0] = "0123456789abcdef"[(int)(buf[i] >> 4)];
    md[j + 1] = "0123456789abcdef"[(int)(buf[i] & 0x0f)];
  }
  md[j] = '\0';
678

Nigel Cunningham's avatar
Nigel Cunningham committed
679
  return md;
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
}
#endif
/* }}} */

#if defined(HAVE_PAM_MYSQL_SHA1_DATA) && defined(HAVE_PAM_MYSQL_MD5_DATA)
#define HAVE_PAM_MYSQL_DRUPAL7
/**
 * The standard log2 number of iterations for password stretching. This should
 * increase by 1 every Drupal version in order to counteract increases in the
 * speed and power of computers available to crack the hashes.
 */
#define DRUPAL_HASH_COUNT 14

/**
 * The minimum allowed log2 number of iterations for password stretching.
 */
#define DRUPAL_MIN_HASH_COUNT 7

/**
 * The maximum allowed log2 number of iterations for password stretching.
 */
#define DRUPAL_MAX_HASH_COUNT 30

/**
 * The expected (and maximum) number of characters in a hashed password.
 */
#define DRUPAL_HASH_LENGTH 55

/**
 * Returns a string for mapping an int to the corresponding base 64 character.
 */
static char * _password_itoa64(void)
{
Nigel Cunningham's avatar
Nigel Cunningham committed
713
  return "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
714 715 716 717 718 719 720
}

/**
 * Parse the log2 iteration count from a stored hash or setting string.
 */
static int d7_password_get_count_log2(char *setting)
{
Nigel Cunningham's avatar
Nigel Cunningham committed
721 722 723 724 725 726 727 728
  char *itoa64 = _password_itoa64();
  int i;

  for (i = 0; i < strlen(itoa64); i++) {
    if (itoa64[i] == setting[3])
      return i;
  }
  return -1;
729 730 731
}

static char * _password_base64_encode(unsigned char *input, int count, char *output) {
Nigel Cunningham's avatar
Nigel Cunningham committed
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
  int i = 0, off = 0;
  char *itoa64 = _password_itoa64();
  unsigned long value;

  do {
    value = (unsigned long) input[i++];
    output[off++] = itoa64[value & 0x3f];
    if (i < count) {
      value |= (((unsigned long) input[i]) << 8);
    }
    output[off++] = itoa64[(value >> 6) & 0x3f];
    if (i++ >= count) {
      break;
    }
    if (i < count) {
      value |= (((unsigned long) input[i]) << 16);
    }
    output[off++] = itoa64[(value >> 12) & 0x3f];
    if (i++ >= count) {
      break;
    }
    output[off++] = itoa64[(value >> 18) & 0x3f];
  } while (i < count);

  output[off] = 0;

  return output;
759 760 761 762 763
}

/* Strings may be binary and contain \0, so we can't use strlen */
static char *d7_hash(int use_md5, char *string1, int len1, char *string2, int len2)
{
Nigel Cunningham's avatar
Nigel Cunningham committed
764 765
  int len = len1 + len2;
  char *combined = xcalloc(len, sizeof(char)), *output = xcalloc(129, sizeof(char));
766

Nigel Cunningham's avatar
Nigel Cunningham committed
767 768 769 770
  if (!combined) {
    syslog(LOG_AUTHPRIV | LOG_ERR, PAM_MYSQL_LOG_PREFIX "hash: Failed to allocate memory for combined value.");
    return NULL;
  }
771

Nigel Cunningham's avatar
Nigel Cunningham committed
772 773
  memcpy(combined, string1, len1);
  memcpy(combined + len1, string2, len2);
774

Nigel Cunningham's avatar
Nigel Cunningham committed
775 776 777 778
  if (use_md5)
    MD5(combined, (unsigned long)len, output);
  else
    SHA512(combined, (unsigned long)len, output);
779

Nigel Cunningham's avatar
Nigel Cunningham committed
780 781
  xfree(combined);
  return output;
782 783 784 785
}

// The first 12 characters of an existing hash are its setting string.
static char * d7_password_crypt(int use_md5, char *password, char *setting) {
Nigel Cunningham's avatar
Nigel Cunningham committed
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
  char salt[9], *old, *new, *final;
  int expected, count, count_log2 = d7_password_get_count_log2(setting);
  int len;

  // Hashes may be imported from elsewhere, so we allow != DRUPAL_HASH_COUNT
  if (count_log2 < DRUPAL_MIN_HASH_COUNT || count_log2 > DRUPAL_MAX_HASH_COUNT) {
    syslog(LOG_AUTHPRIV | LOG_ERR, PAM_MYSQL_LOG_PREFIX "_password_crypt: count_log2 outside of range.");
    return NULL;
  }

  strncpy(salt, &setting[4], 8);
  salt[8] = '\0';
  if (strlen(salt) != 8) {
    syslog(LOG_AUTHPRIV | LOG_ERR, PAM_MYSQL_LOG_PREFIX "_password_crypt: Salt length is not 8.");
    return NULL;
  }

  // Convert the base 2 logarithm into an integer.
  count = 1 << count_log2;

  old = d7_hash(use_md5, salt , 8, password, strlen(password));

  do {
    new = d7_hash(use_md5, old , use_md5 ? 16 : 64, password, strlen(password));
    xfree(old);
    if (!new)
      return NULL;
    old = new;
  } while (--count);

  len = use_md5 ? 16 : 64;

  new = xcalloc(129, sizeof(char));
  memcpy(new, setting, 12);
  _password_base64_encode(old, len, &new[12]);
  xfree(old);
  // _password_base64_encode() of a 16 byte MD5 will always be 22 characters.
  // _password_base64_encode() of a 64 byte sha512 will always be 86 characters.
  expected = 12 + ((8 * len + 5) / 6);
  if (strlen(new) != expected) {
    syslog(LOG_AUTHPRIV | LOG_ERR, PAM_MYSQL_LOG_PREFIX "_password_crypt: Hash length not as expected.");
    xfree(new);
    return NULL;
  }

  final = xcalloc(DRUPAL_HASH_LENGTH + 1, sizeof(char));
  strncpy(final, new, DRUPAL_HASH_LENGTH);
  xfree(new);
  return final;
835 836 837 838
}

static char *pam_mysql_drupal7_data(const unsigned char *pwd, unsigned int sz, char *md, char *db_pwd)
{
Nigel Cunningham's avatar
Nigel Cunningham committed
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
  char *stored_hash = db_pwd, *pwd_ptr = (char *) pwd, *hashed;
  int match = 0, offset = 0;

  // Algorithm taken from user_check_password in includes/password.c in D7.0.
  if (db_pwd[0] == 'U' && db_pwd[1] == '$') {
    // This may be an updated password from user_update_7000(). Such hashes
    // have 'U' added as the first character and need an extra md5().
    stored_hash = &db_pwd[1];
    offset++;
    pwd_ptr = pam_mysql_md5_data(pwd, (unsigned long)sz, md);
  } else
    stored_hash = &db_pwd[0];

  if (stored_hash[0] == '$' && stored_hash[2] == '$') {
    switch (stored_hash[1]) {
      case 'S':
        match = 1;
        hashed = d7_password_crypt(0, pwd_ptr, stored_hash);
        break;
      case 'H':
        // phpBB3 uses "$H$" for the same thing as "$P$".
      case 'P':
        match = 1;
        hashed = d7_password_crypt(1, pwd_ptr, stored_hash);
        break;
    }
  }

  if (!match || !hashed) {
    md[0] = db_pwd[0] + 1;
    return NULL;
  }
  memcpy(md, hashed, strlen(hashed));
  xfree(hashed);
873 874

  return md;
875
}
876 877 878 879 880 881 882
#endif
/* }}} */

/* {{{ option handlers */
/* {{{ pam_mysql_string_opt_getter */
static pam_mysql_err_t pam_mysql_string_opt_getter(void *val, const char **pretval, int *to_release)
{
Nigel Cunningham's avatar
Nigel Cunningham committed
883 884
  *pretval = *(char **)val;
  *to_release = 0;
885

Nigel Cunningham's avatar
Nigel Cunningham committed
886
  return PAM_MYSQL_ERR_SUCCESS;
887 888 889 890 891 892
}
/* }}} */

/* {{{ pam_mysql_string_opt_setter */
static pam_mysql_err_t pam_mysql_string_opt_setter(void *val, const char *newval_str)
{
Nigel Cunningham's avatar
Nigel Cunningham committed
893 894 895
  if (*(char **)val != NULL) {
    xfree(*(char **)val);
  }
896

Nigel Cunningham's avatar
Nigel Cunningham committed
897 898 899 900
  if (NULL == (*(char **)val = xstrdup(newval_str))) {
    syslog(LOG_AUTHPRIV | LOG_CRIT, PAM_MYSQL_LOG_PREFIX "allocation failure at " __FILE__ ":%d", __LINE__);
    return PAM_MYSQL_ERR_ALLOC;
  }
901

Nigel Cunningham's avatar
Nigel Cunningham committed
902
  return PAM_MYSQL_ERR_SUCCESS;
903 904 905 906 907 908 909
}
/* }}} */

/* {{{ pam_mysql_boolean_opt_getter
 */
static pam_mysql_err_t pam_mysql_boolean_opt_getter(void *val, const char **pretval, int *to_release)
{
Nigel Cunningham's avatar
Nigel Cunningham committed
910 911
  *pretval = (*(int *)val ? "true": "false");
  *to_release = 0;
912

Nigel Cunningham's avatar
Nigel Cunningham committed
913
  return PAM_MYSQL_ERR_SUCCESS;
914 915 916 917 918 919
}
/* }}} */

/* {{{ pam_mysql_boolean_opt_setter */
static pam_mysql_err_t pam_mysql_boolean_opt_setter(void *val, const char *newval_str)
{
Nigel Cunningham's avatar
Nigel Cunningham committed
920 921 922 923 924 925
  *(int *)val = (strcmp(newval_str, "0") != 0 &&
      strcasecmp(newval_str, "N") != 0 &&
      strcasecmp(newval_str, "false") != 0 &&
      strcasecmp(newval_str, "no") != 0);

  return PAM_MYSQL_ERR_SUCCESS;
926 927 928 929 930 931
}
/* }}} */

/* {{{ pam_mysql_crypt_opt_getter */
static pam_mysql_err_t pam_mysql_crypt_opt_getter(void *val, const char **pretval, int *to_release)
{
Nigel Cunningham's avatar
Nigel Cunningham committed
932 933 934 935
  switch (*(int *)val) {
    case 0:
      *pretval = "plain";
      break;
936

Nigel Cunningham's avatar
Nigel Cunningham committed
937 938 939
    case 1:
      *pretval = "Y";
      break;
940

Nigel Cunningham's avatar
Nigel Cunningham committed
941 942 943
    case 2:
      *pretval = "mysql";
      break;
944

Nigel Cunningham's avatar
Nigel Cunningham committed
945 946 947
    case 3:
      *pretval = "md5";
      break;
948

Nigel Cunningham's avatar
Nigel Cunningham committed
949 950 951
    case 4:
      *pretval = "sha1";
      break;
952

Nigel Cunningham's avatar
Nigel Cunningham committed
953 954 955
    case 5:
      *pretval = "drupal7";
      break;
956

Nigel Cunningham's avatar
Nigel Cunningham committed
957 958 959
    case 6:
      *pretval = "joomla15";
      break;
960

Nigel Cunningham's avatar
Nigel Cunningham committed
961 962 963
    default:
      *pretval = NULL;
  }
964

Nigel Cunningham's avatar
Nigel Cunningham committed
965
  *to_release = 0;
966

Nigel Cunningham's avatar
Nigel Cunningham committed
967
  return PAM_MYSQL_ERR_SUCCESS;
968 969 970 971 972 973
}
/* }}} */

/* {{{ pam_mysql_crypt_opt_setter */
static pam_mysql_err_t pam_mysql_crypt_opt_setter(void *val, const char *newval_str)
{
Nigel Cunningham's avatar
Nigel Cunningham committed
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 1001 1002 1003 1004 1005 1006 1007 1008 1009
  if (strcmp(newval_str, "0") == 0 || strcasecmp(newval_str, "plain") == 0) {
    *(int *)val = 0;
    return PAM_MYSQL_ERR_SUCCESS;
  }

  if (strcmp(newval_str, "1") == 0 || strcasecmp(newval_str, "Y") == 0) {
    *(int *)val = 1;
    return PAM_MYSQL_ERR_SUCCESS;
  }

  if (strcmp(newval_str, "2") == 0 || strcasecmp(newval_str, "mysql") == 0) {
    *(int *)val = 2;
    return PAM_MYSQL_ERR_SUCCESS;
  }
  if (strcmp(newval_str, "3") == 0 || strcasecmp(newval_str, "md5") == 0) {
    *(int *)val = 3;
    return PAM_MYSQL_ERR_SUCCESS;
  }
  if (strcmp(newval_str, "4") == 0 || strcasecmp(newval_str, "sha1") == 0) {
    *(int *)val = 4;
    return PAM_MYSQL_ERR_SUCCESS;
  }

  if (strcmp(newval_str, "5") == 0 || strcasecmp(newval_str, "drupal7") == 0) {
    *(int *)val = 5;
    return PAM_MYSQL_ERR_SUCCESS;
  }

  if (strcmp(newval_str, "6") == 0 || strcasecmp(newval_str, "joomla15") == 0) {
    *(int *)val = 6;
    return PAM_MYSQL_ERR_SUCCESS;
  }

  *(int *)val = 0;

  return PAM_MYSQL_ERR_INVAL;
1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020
}
/* }}} */
/* }}} */

/* {{{ option definitions */
#define PAM_MYSQL_OFFSETOF(type, x) ((size_t)&((type *)0)->x)

#define PAM_MYSQL_DEF_OPTION(name, accr) PAM_MYSQL_DEF_OPTION2(name, name, accr)


#define PAM_MYSQL_DEF_OPTION2(name, sname, accr) \
Nigel Cunningham's avatar
Nigel Cunningham committed
1021
{ #name, sizeof(#name) - 1, PAM_MYSQL_OFFSETOF(pam_mysql_ctx_t, sname), accr }
1022 1023

static pam_mysql_option_accessor_t pam_mysql_string_opt_accr = {
Nigel Cunningham's avatar
Nigel Cunningham committed
1024 1025
  pam_mysql_string_opt_getter,
  pam_mysql_string_opt_setter
1026 1027 1028
};

static pam_mysql_option_accessor_t pam_mysql_boolean_opt_accr = {
Nigel Cunningham's avatar
Nigel Cunningham committed
1029 1030
  pam_mysql_boolean_opt_getter,
  pam_mysql_boolean_opt_setter
1031 1032 1033
};

static pam_mysql_option_accessor_t pam_mysql_crypt_opt_accr = {
Nigel Cunningham's avatar
Nigel Cunningham committed
1034 1035
  pam_mysql_crypt_opt_getter,
  pam_mysql_crypt_opt_setter
1036 1037 1038
};

static pam_mysql_option_t options[] = {
Nigel Cunningham's avatar
Nigel Cunningham committed
1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066
  PAM_MYSQL_DEF_OPTION(host, &pam_mysql_string_opt_accr),
  PAM_MYSQL_DEF_OPTION(where, &pam_mysql_string_opt_accr),
  PAM_MYSQL_DEF_OPTION(db, &pam_mysql_string_opt_accr),
  PAM_MYSQL_DEF_OPTION(user, &pam_mysql_string_opt_accr),
  PAM_MYSQL_DEF_OPTION(passwd, &pam_mysql_string_opt_accr),
  PAM_MYSQL_DEF_OPTION(table, &pam_mysql_string_opt_accr),
  PAM_MYSQL_DEF_OPTION(update_table, &pam_mysql_string_opt_accr),
  PAM_MYSQL_DEF_OPTION(usercolumn, &pam_mysql_string_opt_accr),
  PAM_MYSQL_DEF_OPTION(passwdcolumn, &pam_mysql_string_opt_accr),
  PAM_MYSQL_DEF_OPTION(statcolumn, &pam_mysql_string_opt_accr),
  PAM_MYSQL_DEF_OPTION2(crypt, crypt_type, &pam_mysql_crypt_opt_accr),
  PAM_MYSQL_DEF_OPTION(md5, &pam_mysql_boolean_opt_accr),
  PAM_MYSQL_DEF_OPTION(sqllog, &pam_mysql_boolean_opt_accr),
  PAM_MYSQL_DEF_OPTION(verbose, &pam_mysql_boolean_opt_accr),
  PAM_MYSQL_DEF_OPTION(logtable, &pam_mysql_string_opt_accr),
  PAM_MYSQL_DEF_OPTION(logmsgcolumn, &pam_mysql_string_opt_accr),
  PAM_MYSQL_DEF_OPTION(logpidcolumn, &pam_mysql_string_opt_accr),
  PAM_MYSQL_DEF_OPTION(logusercolumn, &pam_mysql_string_opt_accr),
  PAM_MYSQL_DEF_OPTION(loghostcolumn, &pam_mysql_string_opt_accr),
  PAM_MYSQL_DEF_OPTION(logrhostcolumn, &pam_mysql_string_opt_accr),
  PAM_MYSQL_DEF_OPTION(logtimecolumn, &pam_mysql_string_opt_accr),
  PAM_MYSQL_DEF_OPTION(config_file, &pam_mysql_string_opt_accr),
  PAM_MYSQL_DEF_OPTION(use_323_passwd, &pam_mysql_boolean_opt_accr),
  PAM_MYSQL_DEF_OPTION(use_first_pass, &pam_mysql_boolean_opt_accr),
  PAM_MYSQL_DEF_OPTION(try_first_pass, &pam_mysql_boolean_opt_accr),
  PAM_MYSQL_DEF_OPTION(disconnect_every_op, &pam_mysql_boolean_opt_accr),
  PAM_MYSQL_DEF_OPTION2(debug, verbose, &pam_mysql_boolean_opt_accr),
  { NULL, 0, 0, NULL }
1067 1068 1069 1070 1071 1072 1073
};
/* }}} */

/* {{{ string functions */
/* {{{ pam_mysql_str_init() */
static pam_mysql_err_t pam_mysql_str_init(pam_mysql_str_t *str, int mangle)
{
Nigel Cunningham's avatar
Nigel Cunningham committed
1074 1075 1076 1077
  str->p = "";
  str->len = 0;
  str->alloc_size = 0;
  str->mangle = mangle;
1078

Nigel Cunningham's avatar
Nigel Cunningham committed
1079
  return PAM_MYSQL_ERR_SUCCESS;
1080 1081 1082 1083 1084 1085
}
/* }}} */

/* {{{ pam_mysql_str_destroy() */
static void pam_mysql_str_destroy(pam_mysql_str_t *str)
{
Nigel Cunningham's avatar
Nigel Cunningham committed
1086 1087 1088 1089 1090 1091
  if (str->alloc_size > 0) {
    if (str->mangle) {
      memset(str->p, 0, str->len);
    }
    xfree(str->p);
  }
1092 1093 1094 1095 1096 1097
}
/* }}} */

/* {{{ pam_mysql_str_reserve() */
static pam_mysql_err_t pam_mysql_str_reserve(pam_mysql_str_t *str, size_t len)
{
Nigel Cunningham's avatar
Nigel Cunningham committed
1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152
  size_t len_req;

  {
    len_req = str->len + len;
    if (len_req < str->len) {
      syslog(LOG_AUTHPRIV | LOG_CRIT, PAM_MYSQL_LOG_PREFIX "integer overflow at " __FILE__ ":%d", __LINE__);
      return PAM_MYSQL_ERR_INVAL;
    }

    len_req += sizeof(char); // space for a string terminator
  }

  if (len_req >= str->alloc_size) {
    size_t cv = 0;
    size_t new_size = (str->alloc_size == 0 ? 1: str->alloc_size);
    char *new_buf;

    do {
      new_size *= 2;
      if (cv > new_size) {
        syslog(LOG_AUTHPRIV | LOG_CRIT, PAM_MYSQL_LOG_PREFIX "allocation failure at " __FILE__ ":%d", __LINE__);
        return PAM_MYSQL_ERR_ALLOC;    
      }
      cv = new_size;
    } while (new_size < len_req);

    if (str->mangle) {
      if (NULL == (new_buf = xcalloc(new_size, sizeof(char)))) {
        syslog(LOG_AUTHPRIV | LOG_CRIT, PAM_MYSQL_LOG_PREFIX "allocation failure at " __FILE__ ":%d", __LINE__);
        return PAM_MYSQL_ERR_ALLOC;    
      }

      memcpy(new_buf, str->p, str->len);
      memset(str->p, 0, str->len);
      if (str->alloc_size > 0) {
        xfree(str->p);
      }
    } else {
      if (str->alloc_size == 0) {
        if (NULL == (new_buf = xcalloc(new_size, sizeof(char)))) {
          syslog(LOG_AUTHPRIV | LOG_CRIT, PAM_MYSQL_LOG_PREFIX "allocation failure at " __FILE__ ":%d", __LINE__);
          return PAM_MYSQL_ERR_ALLOC;    
        }
      } else {
        if (NULL == (new_buf = xrealloc(str->p, new_size, sizeof(char)))) {
          syslog(LOG_AUTHPRIV | LOG_CRIT, PAM_MYSQL_LOG_PREFIX "allocation failure at " __FILE__ ":%d", __LINE__);
          return PAM_MYSQL_ERR_ALLOC;    
        }
      }
    }
    str->p = new_buf;
    str->alloc_size = new_size;
  }

  return PAM_MYSQL_ERR_SUCCESS;
1153 1154 1155 1156 1157
}
/* }}} */

/* {{{ pam_mysql_str_append() */
static pam_mysql_err_t pam_mysql_str_append(pam_mysql_str_t *str,
Nigel Cunningham's avatar
Nigel Cunningham committed
1158
    const char *s, size_t len)
1159
{
Nigel Cunningham's avatar
Nigel Cunningham committed
1160
  pam_mysql_err_t err;
1161

Nigel Cunningham's avatar
Nigel Cunningham committed
1162 1163 1164
  if ((err = pam_mysql_str_reserve(str, len))) {
    return err;
  }
1165

Nigel Cunningham's avatar
Nigel Cunningham committed
1166 1167 1168
  memcpy(str->p + str->len, s, len);
  str->len += len;
  str->p[str->len] = '\0';
1169

Nigel Cunningham's avatar
Nigel Cunningham committed
1170
  return PAM_MYSQL_ERR_SUCCESS;
1171 1172 1173 1174 1175 1176
}
/* }}} */

/* {{{ pam_mysql_str_append_char() */
static pam_mysql_err_t pam_mysql_str_append_char(pam_mysql_str_t *str, char c)
{
Nigel Cunningham's avatar
Nigel Cunningham committed
1177
  return pam_mysql_str_append(str, &c, sizeof(c));
1178 1179 1180 1181 1182 1183
}
/* }}} */

/* {{{ pam_mysql_str_truncate() */
static pam_mysql_err_t pam_mysql_str_truncate(pam_mysql_str_t *str, size_t len)
{
Nigel Cunningham's avatar
Nigel Cunningham committed
1184 1185 1186
  if (len > str->len) {
    return PAM_MYSQL_ERR_INVAL;
  }
1187

Nigel Cunningham's avatar
Nigel Cunningham committed
1188
  str->len = len;
1189

Nigel Cunningham's avatar
Nigel Cunningham committed
1190 1191 1192
  if (str->alloc_size != 0) {
    str->p[len] = '\0';
  }
1193

Nigel Cunningham's avatar
Nigel Cunningham committed
1194
  return PAM_MYSQL_ERR_SUCCESS;
1195 1196 1197 1198 1199 1200 1201
}
/* }}} */
/* }}} */

/* {{{ stream functions */
/* {{{ pam_mysql_stream_open */
static pam_mysql_err_t pam_mysql_stream_open(pam_mysql_stream_t *stream,
Nigel Cunningham's avatar
Nigel Cunningham committed
1202
    pam_mysql_ctx_t *ctx, const char *file)
1203
{
Nigel Cunningham's avatar
Nigel Cunningham committed
1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219
  stream->buf_end = stream->buf_start = stream->buf_ptr = stream->buf[0];
  stream->pushback = NULL;
  stream->buf_in_use = 0;
  stream->eof = 0;

  if ((stream->fd = open(file, O_RDONLY)) == -1) {
    if (ctx->verbose) {
      switch (errno) {
        case EACCES:
        case EPERM:
          syslog(LOG_AUTHPRIV | LOG_ERR, PAM_MYSQL_LOG_PREFIX "access to %s not permitted", file);
          break;

        case EISDIR:
          syslog(LOG_AUTHPRIV | LOG_ERR, PAM_MYSQL_LOG_PREFIX "%s is directory", file);
          break;
1220

1221
#if HAVE_DECL_ELOOP
Nigel Cunningham's avatar
Nigel Cunningham committed
1222 1223 1224
        case ELOOP:
          syslog(LOG_AUTHPRIV | LOG_ERR, PAM_MYSQL_LOG_PREFIX "%s refers to an inresolvable symbolic link", file);
          break;
1225 1226
#endif

Nigel Cunningham's avatar
Nigel Cunningham committed
1227 1228 1229
        case EMFILE:
          syslog(LOG_AUTHPRIV | LOG_ERR, PAM_MYSQL_LOG_PREFIX "too many opened files");
          break;
1230

Nigel Cunningham's avatar
Nigel Cunningham committed
1231 1232 1233
        case ENFILE:
          syslog(LOG_AUTHPRIV | LOG_ERR, PAM_MYSQL_LOG_PREFIX "too many opened files within this system");
          break;
1234

Nigel Cunningham's avatar
Nigel Cunningham committed
1235 1236 1237
        case ENOENT:
          syslog(LOG_AUTHPRIV | LOG_ERR, PAM_MYSQL_LOG_PREFIX "%s does not exist", file);
          break;
1238

Nigel Cunningham's avatar
Nigel Cunningham committed
1239 1240 1241
        case ENOMEM:
          syslog(LOG_AUTHPRIV | LOG_ERR, PAM_MYSQL_LOG_PREFIX "kernel resource exhausted");
          break;
1242

1243
#if HAVE_DECL_EOVERFLOW
Nigel Cunningham's avatar
Nigel Cunningham committed
1244 1245 1246
        case EOVERFLOW:
          syslog(LOG_AUTHPRIV | LOG_ERR, PAM_MYSQL_LOG_PREFIX "%s is too big", file);
          break;
1247 1248
#endif

Nigel Cunningham's avatar
Nigel Cunningham committed
1249 1250 1251 1252 1253
        default:
          syslog(LOG_AUTHPRIV | LOG_ERR, PAM_MYSQL_LOG_PREFIX "unknown error while opening %s", file);
          break;
      }
    }
1254

Nigel Cunningham's avatar
Nigel Cunningham committed
1255 1256
    return PAM_MYSQL_ERR_IO;
  }
1257

Nigel Cunningham's avatar
Nigel Cunningham committed
1258
  return PAM_MYSQL_ERR_SUCCESS;
1259 1260 1261 1262 1263 1264
}
/* }}} */

/* {{{ pam_mysql_stream_close */
static void pam_mysql_stream_close(pam_mysql_stream_t *stream)
{
Nigel Cunningham's avatar
Nigel Cunningham committed
1265 1266 1267
  if (stream->fd != -1) {
    close(stream->fd);
  }
1268 1269 1270 1271 1272
}
/* }}} */

/* {{{ pam_mysql_stream_getc */
static pam_mysql_err_t pam_mysql_stream_getc(pam_mysql_stream_t *stream,
Nigel Cunningham's avatar
Nigel Cunningham committed
1273
    int *retval)
1274
{
Nigel Cunningham's avatar
Nigel Cunningham committed
1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306
  if (stream->buf_ptr >= stream->buf_end) {
    ssize_t new_buf_len;
    unsigned char *new_buf = stream->buf[1 - stream->buf_in_use];

    if (stream->pushback != NULL) {
      stream->buf_end = stream->pushback;
      stream->pushback = NULL;
    } else {
      if (stream->eof) {
        return PAM_MYSQL_ERR_EOF;
      }

      if ((new_buf_len = read(stream->fd, new_buf, sizeof(stream->buf[0]))) == -1) {
        syslog(LOG_AUTHPRIV | LOG_ERR, PAM_MYSQL_LOG_PREFIX "I/O error");
        return PAM_MYSQL_ERR_IO;
      }

      if (new_buf_len == 0) {
        stream->eof = 1;
        return PAM_MYSQL_ERR_EOF;
      }

      stream->buf_end = new_buf + new_buf_len;
    }

    stream->buf_start = stream->buf_ptr = new_buf;
    stream->buf_in_use = 1 - stream->buf_in_use;
  }

  *retval = *(stream->buf_ptr++);

  return PAM_MYSQL_ERR_SUCCESS;
1307 1308 1309 1310 1311 1312
}
/* }}} */

/* {{{ pam_mysql_stream_ungetc */
static pam_mysql_err_t pam_mysql_stream_ungetc(pam_mysql_stream_t *stream, int c)
{
Nigel Cunningham's avatar
Nigel Cunningham committed
1313 1314 1315 1316
  if (stream->buf_ptr == stream->buf_start) {
    if (stream->pushback != NULL) {
      return PAM_MYSQL_ERR_IO;
    }
1317

Nigel Cunningham's avatar
Nigel Cunningham committed
1318 1319 1320 1321 1322
    stream->pushback = stream->buf_end;
    stream->buf_in_use = 1 - stream->buf_in_use;
    stream->buf_start = stream->buf[stream->buf_in_use];
    stream->buf_ptr = stream->buf_start + sizeof(stream->buf[0]);
  }
1323

Nigel Cunningham's avatar
Nigel Cunningham committed
1324
  *(--stream->buf_ptr) = c;
1325

Nigel Cunningham's avatar
Nigel Cunningham committed
1326
  return PAM_MYSQL_ERR_SUCCESS;
1327 1328 1329 1330 1331
}
/* }}} */

/* {{{ pam_mysql_stream_skip_spn */
static pam_mysql_err_t pam_mysql_stream_skip_spn(pam_mysql_stream_t *stream,
Nigel Cunningham's avatar
Nigel Cunningham committed
1332
    const char *accept_cset, size_t naccepts)
1333
{
Nigel Cunningham's avatar
Nigel Cunningham committed
1334
  unsigned char *p;
1335

Nigel Cunningham's avatar
Nigel Cunningham committed
1336 1337 1338
  if (stream->eof) {
    return PAM_MYSQL_ERR_EOF;
  }
1339

Nigel Cunningham's avatar
Nigel Cunningham committed
1340 1341 1342 1343
  if ((p = (unsigned char *)memspn(stream->buf_ptr,
          stream->buf_end - stream->buf_ptr, (const unsigned char *)accept_cset,
          naccepts)) != NULL) {
    stream->buf_ptr = p;
1344

Nigel Cunningham's avatar
Nigel Cunningham committed
1345 1346
    return PAM_MYSQL_ERR_SUCCESS;
  }
1347

Nigel Cunningham's avatar
Nigel Cunningham committed
1348 1349 1350 1351 1352
  if (stream->pushback != NULL) {
    stream->buf_in_use = 1 - stream->buf_in_use;
    stream->buf_start = stream->buf_ptr = stream->buf[stream->buf_in_use];
    stream->buf_end = stream->pushback;
    stream->pushback = NULL;
1353

Nigel Cunningham's avatar
Nigel Cunningham committed
1354 1355 1356 1357
    if ((p = (unsigned char *)memspn(stream->buf_ptr,
            stream->buf_end - stream->buf_ptr, (const unsigned char *)accept_cset,
            naccepts)) != NULL) {
      stream->buf_ptr = p;
1358

Nigel Cunningham's avatar
Nigel Cunningham committed
1359 1360 1361
      return PAM_MYSQL_ERR_SUCCESS;
    }
  }
1362

Nigel Cunningham's avatar
Nigel Cunningham committed
1363 1364
  for (;;) {
    ssize_t new_buf_len;
1365

Nigel Cunningham's avatar
Nigel Cunningham committed
1366 1367 1368 1369
    if ((new_buf_len = read(stream->fd, stream->buf_start, sizeof(stream->buf[0]))) == -1) {
      syslog(LOG_AUTHPRIV | LOG_ERR, PAM_MYSQL_LOG_PREFIX "I/O error");
      return PAM_MYSQL_ERR_IO;
    }
1370

Nigel Cunningham's avatar
Nigel Cunningham committed
1371 1372 1373 1374
    if (new_buf_len == 0) {
      stream->eof = 1;
      return PAM_MYSQL_ERR_EOF;
    }
1375

Nigel Cunningham's avatar
Nigel Cunningham committed
1376
    stream->buf_end = stream->buf_start + new_buf_len;
1377

Nigel Cunningham's avatar
Nigel Cunningham committed
1378 1379 1380
    if ((p = (unsigned char *)memspn(stream->buf_start, new_buf_len,
            (const unsigned char *)accept_cset, naccepts)) != NULL) {
      stream->buf_ptr = p;
1381

Nigel Cunningham's avatar
Nigel Cunningham committed
1382 1383 1384
      break;
    }
  }
1385

Nigel Cunningham's avatar
Nigel Cunningham committed
1386
  return PAM_MYSQL_ERR_SUCCESS;
1387 1388 1389 1390 1391
}
/* }}} */

/* {{{ pam_mysql_stream_read_cspn */
static pam_mysql_err_t pam_mysql_stream_read_cspn(pam_mysql_stream_t *stream,
Nigel Cunningham's avatar
Nigel Cunningham committed
1392 1393
    pam_mysql_str_t *append_to, int *found_delim, const char *delims,
    size_t ndelims)
1394
{
Nigel Cunningham's avatar
Nigel Cunningham committed
1395 1396 1397 1398
  pam_mysql_err_t err;
  unsigned char *p;
  ssize_t len;
  size_t rem_len;
1399

Nigel Cunningham's avatar
Nigel Cunningham committed
1400 1401 1402
  if (stream->eof) {
    return PAM_MYSQL_ERR_EOF;
  }
1403

Nigel Cunningham's avatar
Nigel Cunningham committed
1404 1405 1406 1407 1408 1409 1410
  if ((p = (unsigned char *)memcspn(stream->buf_ptr,
          stream->buf_end - stream->buf_ptr, (const unsigned char *)delims,
          ndelims)) != NULL) {
    if ((err = pam_mysql_str_append(append_to, (char *)stream->buf_ptr,
            p - stream->buf_ptr))) {
      return err;
    }
1411

Nigel Cunningham's avatar
Nigel Cunningham committed
1412 1413
    *found_delim = *p;
    stream->buf_ptr = p;
1414

Nigel Cunningham's avatar
Nigel Cunningham committed
1415 1416
    return PAM_MYSQL_ERR_SUCCESS;
  }
1417

Nigel Cunningham's avatar
Nigel Cunningham committed
1418 1419 1420 1421
  if ((err = pam_mysql_str_append(append_to, (char *)stream->buf_ptr,
          stream->buf_end - stream->buf_ptr))) {
    return err;
  }
1422

Nigel Cunningham's avatar
Nigel Cunningham committed
1423 1424 1425 1426 1427
  if (stream->pushback != NULL) {
    stream->buf_in_use = 1 - stream->buf_in_use;
    stream->buf_start = stream->buf_ptr = stream->buf[stream->buf_in_use];
    stream->buf_end = stream->pushback;
    stream->pushback = NULL;
1428

Nigel Cunningham's avatar
Nigel Cunningham committed
1429 1430 1431 1432 1433 1434 1435
    if ((p = (unsigned char *)memcspn(stream->buf_ptr,
            stream->buf_end - stream->buf_ptr,
            (const unsigned char *)delims, ndelims)) != NULL) {
      if ((err = pam_mysql_str_append(append_to, (char *)stream->buf_ptr,
              p - stream->buf_ptr))) {
        return err;
      }
1436

Nigel Cunningham's avatar
Nigel Cunningham committed
1437 1438
      *found_delim = *p;
      stream->buf_ptr = p;
1439

Nigel Cunningham's avatar
Nigel Cunningham committed
1440 1441
      return PAM_MYSQL_ERR_SUCCESS;
    }
1442

Nigel Cunningham's avatar
Nigel Cunningham committed
1443 1444 1445 1446 1447
    if ((err = pam_mysql_str_append(append_to, (char *)stream->buf_ptr,
            stream->buf_end - stream->buf_ptr))) {
      return err;
    }
  }
1448

Nigel Cunningham's avatar
Nigel Cunningham committed
1449
  rem_len = 0;
1450

Nigel Cunningham's avatar
Nigel Cunningham committed
1451 1452
  for (;;) {
    unsigned char *block;
1453

Nigel Cunningham's avatar
Nigel Cunningham committed
1454 1455 1456 1457
    if ((err = pam_mysql_str_reserve(append_to,
            sizeof(stream->buf[0]) - rem_len))) {
      return err;
    }
1458

Nigel Cunningham's avatar
Nigel Cunningham committed
1459
    block = (unsigned char*)append_to->p + append_to->len;
1460

Nigel Cunningham's avatar
Nigel Cunningham committed
1461 1462 1463 1464
    if ((len = read(stream->fd, block, sizeof(stream->buf[0]))) == -1) {
      syslog(LOG_AUTHPRIV | LOG_ERR, PAM_MYSQL_LOG_PREFIX "I/O error");
      return PAM_MYSQL_ERR_IO;
    }
1465

Nigel Cunningham's avatar
Nigel Cunningham committed
1466 1467 1468 1469
    if (len == 0) {
      stream->eof = 1;
      return PAM_MYSQL_ERR_EOF;
    }
1470

Nigel Cunningham's avatar
Nigel Cunningham committed
1471 1472 1473
    if ((p = (unsigned char *)memcspn(block, len,
            (const unsigned char *)delims, ndelims)) != NULL) {
      size_t new_buf_len;
1474

Nigel Cunningham's avatar
Nigel Cunningham committed
1475 1476
      append_to->len += p - block;
      new_buf_len = len - (p - block);
1477

Nigel Cunningham's avatar
Nigel Cunningham committed
1478
      memcpy(stream->buf_start, p, new_buf_len);
1479

Nigel Cunningham's avatar
Nigel Cunningham committed
1480 1481
      stream->buf_ptr = stream->buf_start;
      stream->buf_end = stream->buf_start + new_buf_len;
1482

Nigel Cunningham's avatar
Nigel Cunningham committed
1483 1484
      break;
    }
1485

Nigel Cunningham's avatar
Nigel Cunningham committed
1486 1487 1488
    append_to->len += len;
    rem_len = sizeof(stream->buf[0]) - len;
  }
1489

Nigel Cunningham's avatar
Nigel Cunningham committed
1490 1491
  *found_delim = *p;
  append_to->p[append_to->len] = '\0';
1492

Nigel Cunningham's avatar
Nigel Cunningham committed
1493
  return PAM_MYSQL_ERR_SUCCESS;
1494 1495 1496 1497 1498 1499
}
/* }}} */
/* }}} */

/* {{{ config file scanner / parser */
static const char * pam_mysql_config_token_name[] = {
Nigel Cunningham's avatar
Nigel Cunningham committed
1500 1501 1502 1503 1504 1505
  "=",
  "<NEWLINE>",
  "<STRING_LITERAL>",
  ";",
  "<COMMENT>",
  NULL
1506 1507 1508 1509
};

/* {{{ pam_mysql_config_scanner_init */
static pam_mysql_err_t pam_mysql_config_scanner_init(
Nigel Cunningham's avatar
Nigel Cunningham committed
1510 1511
    pam_mysql_config_scanner_t *scanner, pam_mysql_ctx_t *ctx,
    pam_mysql_stream_t *stream)
1512
{
Nigel Cunningham's avatar
Nigel Cunningham committed
1513
  pam_mysql_err_t err;
1514

Nigel Cunningham's avatar
Nigel Cunningham committed
1515 1516 1517
  if ((err = pam_mysql_str_init(&scanner->image, 1))) {
    return err;
  }
1518

Nigel Cunningham's avatar
Nigel Cunningham committed
1519 1520 1521
  scanner->ctx = ctx;
  scanner->stream = stream;
  scanner->state = 0;
1522

Nigel Cunningham's avatar
Nigel Cunningham committed
1523
  return PAM_MYSQL_ERR_SUCCESS;
1524 1525 1526 1527 1528
}
/* }}} */

/* {{{ pam_mysql_config_scanner_destroy */
static void pam_mysql_config_scanner_destroy(
Nigel Cunningham's avatar
Nigel Cunningham committed
1529
    pam_mysql_config_scanner_t *scanner)
1530
{
Nigel Cunningham's avatar
Nigel Cunningham committed
1531
  pam_mysql_str_destroy(&scanner->image);
1532 1533 1534 1535 1536
}
/* }}} */

/* {{{ pam_mysql_config_scanner_next_token */
static pam_mysql_err_t pam_mysql_config_scanner_next_token(
Nigel Cunningham's avatar
Nigel Cunningham committed
1537
    pam_mysql_config_scanner_t *scanner)
1538
{
Nigel Cunningham's avatar
Nigel Cunningham committed
1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709
  pam_mysql_err_t err;
  int c;

  switch (scanner->state) {
    case 0:
      if ((err = pam_mysql_str_truncate(&scanner->image, 0))) {
        return err;
      }

      if ((err = pam_mysql_stream_getc(scanner->stream, &c))) {
        return err;
      }

      if (c == '#') {
        if ((err = pam_mysql_str_append_char(&scanner->image, c))) {
          return err;
        }

        if ((err = pam_mysql_stream_read_cspn(scanner->stream,
                &scanner->image, &c, "\n\r", sizeof("\n\r") - 1))) {
          return err;
        }

        scanner->token = PAM_MYSQL_CONFIG_TOKEN_COMMENT;

        if ((err = pam_mysql_stream_getc(scanner->stream, &c))) {
          return err;
        }

        if (c == '\r') {
          if ((err = pam_mysql_stream_getc(scanner->stream, &c))) {
            return err;
          }

          if (c != '\n') {
            if ((err = pam_mysql_stream_ungetc(scanner->stream, c))) {
              return err;
            }
          }
        }
        break;
      } else {
        if ((err = pam_mysql_stream_ungetc(scanner->stream, c))) {
          return err;
        }
      }

    case 1:
      if ((err = pam_mysql_stream_skip_spn(scanner->stream,
              " \t", sizeof(" \t") - 1))) {
        return err;
      }

      if ((err = pam_mysql_str_truncate(&scanner->image, 0))) {
        return err;
      }

      if ((err = pam_mysql_stream_getc(scanner->stream, &c))) {
        return err;
      }

      if ((err = pam_mysql_str_append_char(&scanner->image, c))) {
        return err;
      }

      switch (scanner->image.p[0]) {
        case '=':
          scanner->token = PAM_MYSQL_CONFIG_TOKEN_EQUAL;
          scanner->state = 2;
          break;

        case ';':
          scanner->token = PAM_MYSQL_CONFIG_TOKEN_SEMICOLON;
          scanner->state = 1;
          break;

        default:
          if ((err = pam_mysql_stream_read_cspn(scanner->stream,
                  &scanner->image, &c, "=; \t\n\r", sizeof("=; \t\n\r") - 1))) {
            return err;
          }
          scanner->token = PAM_MYSQL_CONFIG_TOKEN_STRING;
          scanner->state = 1;
          break;

        case '\r':
          if ((err = pam_mysql_stream_getc(scanner->stream, &c))) {
            return err;
          }

          if (c != '\n') {
            if ((err = pam_mysql_stream_ungetc(scanner->stream, c))) {
              return err;
            }
          } else {
            if ((err = pam_mysql_str_append_char(&scanner->image, c))) {
              return err;
            }
          }

          scanner->token = PAM_MYSQL_CONFIG_TOKEN_NEWLINE;
          scanner->state = 0;
          break;

        case '\n':
          scanner->token = PAM_MYSQL_CONFIG_TOKEN_NEWLINE;
          scanner->state = 0;
          break;
      }
      break;

    case 2:
      if ((err = pam_mysql_stream_skip_spn(scanner->stream,
              " \t", sizeof(" \t") - 1))) {
        return err;
      }

      if ((err = pam_mysql_str_truncate(&scanner->image, 0))) {
        return err;
      }

      if ((err = pam_mysql_stream_getc(scanner->stream, &c))) {
        return err;
      }

      if ((err = pam_mysql_str_append_char(&scanner->image, c))) {
        return err;
      }

      switch (scanner->image.p[0]) {
        case ';':
          scanner->token = PAM_MYSQL_CONFIG_TOKEN_SEMICOLON;
          scanner->state = 1;
          break;

        default:
          if ((err = pam_mysql_stream_read_cspn(scanner->stream,
                  &scanner->image, &c, ";\n\r", sizeof(";\n\r") - 1))) {
            return err;
          }
          scanner->token = PAM_MYSQL_CONFIG_TOKEN_STRING;
          break;

        case '\r':
          if ((err = pam_mysql_stream_getc(scanner->stream, &c))) {
            return err;
          }

          if (c != '\n') {
            if ((err = pam_mysql_stream_ungetc(scanner->stream, c))) {
              return err;
            }
          } else {
            if ((err = pam_mysql_str_append_char(&scanner->image, c))) {
              return err;
            }
          }

          scanner->token = PAM_MYSQL_CONFIG_TOKEN_NEWLINE;
          scanner->state = 0;
          break;

        case '\n':
          scanner->token = PAM_MYSQL_CONFIG_TOKEN_NEWLINE;
          scanner->state = 0;
          break;
      }
      break;
  }

  return PAM_MYSQL_ERR_SUCCESS;
1710 1711 1712 1713 1714
}
/* }}} */

/* {{{ pam_mysql_config_parser_init */
static pam_mysql_err_t pam_mysql_config_parser_init(
Nigel Cunningham's avatar
Nigel Cunningham committed
1715 1716
    pam_mysql_config_parser_t *parser, pam_mysql_ctx_t *ctx,
    pam_mysql_entry_handler_t *hdlr)
1717
{
Nigel Cunningham's avatar
Nigel Cunningham committed
1718 1719
  parser->ctx = ctx;
  parser->hdlr = hdlr;
1720

Nigel Cunningham's avatar
Nigel Cunningham committed
1721
  return PAM_MYSQL_ERR_SUCCESS;
1722 1723 1724 1725 1726 1727
}
/* }}} */

/* {{{ pam_mysql_config_parser_destroy */
static void pam_mysql_config_parser_destroy(pam_mysql_config_parser_t *parser)
{
Nigel Cunningham's avatar
Nigel Cunningham committed
1728
  /* do nothing */
1729 1730 1731 1732 1733
}
/* }}} */

/* {{{ pam_mysql_config_parser_parse */
static pam_mysql_err_t pam_mysql_config_parser_parse(
Nigel Cunningham's avatar
Nigel Cunningham committed
1734
    pam_mysql_config_parser_t *parser, pam_mysql_stream_t *stream)
1735
{
Nigel Cunningham's avatar
Nigel Cunningham committed
1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846