/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil; tab-width: 4 -*-
 * ex: set softtabstop=4 tabstop=8 expandtab shiftwidth=4: *
 * Editor Settings: expandtabs and use 4 spaces for indentation */

/*
 * Copyright Likewise Software
 * 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, see
 * <http://www.gnu.org/licenses/>.
 *
 * LIKEWISE SOFTWARE MAKES THIS SOFTWARE AVAILABLE UNDER OTHER LICENSING
 * TERMS AS WELL.  IF YOU HAVE ENTERED INTO A SEPARATE LICENSE AGREEMENT
 * WITH LIKEWISE SOFTWARE, THEN YOU MAY ELECT TO USE THE SOFTWARE UNDER THE
 * TERMS OF THAT SOFTWARE LICENSE AGREEMENT INSTEAD OF THE TERMS OF THE GNU
 * GENERAL PUBLIC LICENSE, NOTWITHSTANDING THE ABOVE NOTICE.  IF YOU
 * HAVE QUESTIONS, OR WISH TO REQUEST A COPY OF THE ALTERNATE LICENSING
 * TERMS OFFERED BY LIKEWISE SOFTWARE, PLEASE CONTACT LIKEWISE SOFTWARE AT
 * license@likewisesoftware.com
 */

/*
 * Copyright (C) Likewise Software. All rights reserved.
 *
 * Module Name:
 *
 *        samdbdefs.c
 *
 * Abstract:
 *
 *
 *      Likewise SAM Database Provider
 *
 *      Provider macros and definitions
 *
 * Authors: Krishna Ganugapati (krishnag@likewise.com)
 *          Sriram Nambakam (snambakam@likewise.com)
 *          Rafal Szczesniak (rafal@likewise.com)
 *
 */

#ifndef __SAMDBDEFS_H__
#define __SAMDBDEFS_H__

#define SAMDB_DIRECTORY  CACHEDIR "/db"
#define SAMDB_FILE  SAMDB_DIRECTORY "/sam.db"
#define SAMDB_TEMP_FILE  SAMDB_DIRECTORY "/.sam.db"

#define SAM_DB_CONTEXT_POOL_MAX_ENTRIES 10

#define SAM_DB_DEFAULT_ADMINISTRATOR_SHELL   "/bin/sh"
#define SAM_DB_DEFAULT_ADMINISTRATOR_HOMEDIR "/"

#define SAM_DB_DEFAULT_GUEST_SHELL           "/bin/sh"
#define SAM_DB_DEFAULT_GUEST_HOMEDIR         "/tmp"

//
// Loggging macros (use the same as the rest of lsass)
//
#define SAMDB_LOG_ERROR(pszFormat, ...) LSA_LOG_ERROR(pszFormat, ## __VA_ARGS__)
#define SAMDB_LOG_WARNING(pszFormat, ...) LSA_LOG_WARNING(pszFormat, ## __VA_ARGS__)
#define SAMDB_LOG_INFO(pszFormat, ...) LSA_LOG_INFO(pszFormat, ## __VA_ARGS__)
#define SAMDB_LOG_VERBOSE(pszFormat, ...) LSA_LOG_VERBOSE(pszFormat, ## __VA_ARGS__)
#define SAMDB_LOG_DEBUG(pszFormat, ...) LSA_LOG_DEBUG(pszFormat, ## __VA_ARGS__)

//
// Error handling macros
//
#define BAIL_ON_SAMDB_ERROR(error) \
    if ((error)) \
    { \
        SAMDB_LOG_ERROR("Win32 Error code: %u (symbol: %s)", \
                        (error), \
                        LSA_SAFE_LOG_STRING(LwWin32ErrorToName(error))); \
        goto cleanup; \
    }

// 
// Bail on error with error message suppression.
//
// If the given "quiet_condition" evaluates to true then log a message
// at the INFO verbosity level; otherwise, log a message at the
// ERROR verbosity level.
//
#define BAIL_ON_SAMDB_ERROR_QUIET(error, quiet_condition) \
    if ((error)) \
    { \
        if ((quiet_condition)) \
        { \
            SAMDB_LOG_INFO("Win32 Error code: %u (symbol: %s)", \
                           (error), \
                           LSA_SAFE_LOG_STRING(LwWin32ErrorToName(error))); \
        } \
        else \
        { \
            SAMDB_LOG_ERROR("Win32 Error code: %u (symbol: %s)", \
                            (error), \
                            LSA_SAFE_LOG_STRING(LwWin32ErrorToName(error))); \
        } \
        goto cleanup; \
    }


#ifdef BAIL_ON_INVALID_POINTER
#undef BAIL_ON_INVALID_POINTER
#endif

#define BAIL_ON_INVALID_POINTER(pointer, error) \
    if ((pointer) == NULL) \
    { \
        error = ERROR_INVALID_PARAMETER; \
        goto cleanup; \
    }

#define BAIL_ON_SAMDB_SQLITE_ERROR(error, message) \
    if ((error)) \
    { \
        SAMDB_LOG_ERROR("Sqlite3 Error (code: %u): %s", \
                        (error), \
                        LSA_SAFE_LOG_STRING(message)); \
        (error) = ERROR_INTERNAL_ERROR; \
        goto cleanup; \
    }

#define BAIL_ON_SAMDB_SQLITE_ERROR_DB(error, db) \
    BAIL_ON_SAMDB_SQLITE_ERROR((error), sqlite3_errmsg(db))

#define BAIL_ON_SAMDB_SQLITE_ERROR_STMT(error, statement) \
    BAIL_ON_SAMDB_SQLITE_ERROR_DB((error), sqlite3_db_handle(statement))


#define SAMDB_LOCK_MUTEX(bInLock, mutex) \
    if (!bInLock) { \
       int thr_err = pthread_mutex_lock(mutex); \
       if (thr_err) { \
           SAMDB_LOG_ERROR("Failed to lock mutex. Aborting program"); \
           abort(); \
       } \
       bInLock = TRUE; \
    }

#define SAMDB_UNLOCK_MUTEX(bInLock, mutex) \
    if (bInLock) { \
       int thr_err = pthread_mutex_unlock(mutex); \
       if (thr_err) { \
           SAMDB_LOG_ERROR("Failed to unlock mutex. Aborting program"); \
           abort(); \
       } \
       bInLock = FALSE; \
    }

#define SAMDB_SHARED_LOCK_RWLOCK(bInLock, mutex) \
    if (!bInLock) { \
       int thr_err = pthread_rwlock_rdlock(mutex); \
       if (thr_err) { \
           SAMDB_LOG_ERROR("Failed to acquire shared lock on rw mutex. Aborting program"); \
           abort(); \
       } \
       bInLock = TRUE; \
    }

#define SAMDB_EXCLUSIVE_LOCK_RWLOCK(bInLock, mutex) \
    if (!bInLock) { \
       int thr_err = pthread_rwlock_wrlock(mutex); \
       if (thr_err) { \
           SAMDB_LOG_ERROR("Failed to acquire exclusive lock on rw mutex. Aborting program"); \
           abort(); \
       } \
       bInLock = TRUE; \
    }

#define SAMDB_UNLOCK_RWLOCK(bInLock, mutex) \
    if (bInLock) { \
       int thr_err = pthread_rwlock_unlock(mutex); \
       if (thr_err) { \
           SAMDB_LOG_ERROR("Failed to unlock rw mutex. Aborting program"); \
           abort(); \
       } \
       bInLock = FALSE; \
    }

#define SAMDB_SECONDS_IN_HOUR (60 * 60)
#define SAMDB_SECONDS_IN_DAY  (24 * SAMDB_SECONDS_IN_HOUR)

#define SAMDB_MIN_PASSWORD_LENGTH   (6)
#define SAMDB_MIN_PWD_AGE           (0)
#define SAMDB_MAX_PWD_AGE           (30 * SAMDB_SECONDS_IN_DAY)
#define SAMDB_PWD_PROMPT_TIME       (14 * SAMDB_SECONDS_IN_DAY)
#define SAMDB_LOCKOUT_THRESHOLD     (0)
#define SAMDB_LOCKOUT_DURATION      (0)
#define SAMDB_LOCKOUT_WINDOW        (0)

#define SAMBD_100NS_CYCLES_IN_SECOND  (1000*1000*10LL)

#define SAMDB_BUILTIN_DOMAIN_NAME   "BUILTIN"


#endif /* __SAMDBDEFS_H__ */
