/* -*- 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:
 *
 *        samdbtable.h
 *
 * Abstract:
 *
 *
 *      Likewise SAM Database Provider
 *
 *      Database Schema
 *
 * Authors: Krishna Ganugapati (krishnag@likewisesoftware.com)
 *          Sriram Nambakam (snambakam@likewise.com)
 *          Rafal Szczesniak (rafal@likewise.com)
 *
 */

#ifndef __SAM_DB_TABLE_H__
#define __SAM_DB_TABLE_H__

#define SAM_DB_SCHEMA_VERSION 4

#if !defined(SAM_DB_UID_RID_OFFSET)
#define SAM_DB_UID_RID_OFFSET     (1000)
#endif
#if !defined(SAM_DB_GID_RID_OFFSET)
#define SAM_DB_GID_RID_OFFSET     SAM_DB_UID_RID_OFFSET
#endif

#define SAM_DB_UID_FROM_RID(rid)  (SAM_DB_UID_RID_OFFSET + (rid))
#define SAM_DB_GID_FROM_RID(rid)  (SAM_DB_GID_RID_OFFSET + (rid))

#define SAM_DB_ID_FROM_RID_OFFSET (LW_MAX(SAM_DB_UID_RID_OFFSET, SAM_DB_GID_RID_OFFSET))

#define SAM_DB_MIN_RID            (1000)
#define SAM_DB_MAX_RID            (0xffffffff -                                 \
                                   (SAM_DB_MIN_RID + SAM_DB_ID_FROM_RID_OFFSET) \
                                   - 1)
#define SAM_DB_MIN_UID            SAM_DB_UID_FROM_RID(SAM_DB_MIN_RID)
#define SAM_DB_MAX_UID            SAM_DB_UID_FROM_RID(SAM_DB_MAX_RID)
#define SAM_DB_MIN_GID            SAM_DB_GID_FROM_RID(SAM_DB_MIN_RID)
#define SAM_DB_MAX_GID            SAM_DB_GID_FROM_RID(SAM_DB_MAX_RID)

#define SAM_DB_CONFIG_TABLE              "samdbconfig"
#define SAM_DB_OBJECTS_TABLE             "samdbobjects"
#define SAM_DB_MEMBERS_TABLE             "samdbmembers"

#define SAM_DB_COL_RECORD_ID             "ObjectRecordId"
#define SAM_DB_COL_GROUP_RECORD_ID       "GroupRecordId"
#define SAM_DB_COL_MEMBER_RECORD_ID      "MemberRecordId"
#define SAM_DB_COL_OBJECT_SID            "ObjectSID"
#define SAM_DB_COL_SECURITY_DESCRIPTOR   "SecurityDescriptor"
#define SAM_DB_COL_OBJECT_CLASS          "ObjectClass"
#define SAM_DB_COL_LIKEWISE_FLAGS        "LikewiseFlags"
#define SAM_DB_COL_DOMAIN                "Domain"
#define SAM_DB_COL_SAM_ACCOUNT_NAME      "SamAccountName"
#define SAM_DB_COL_USER_PRINCIPAL_NAME   "UserPrincipalName"
#define SAM_DB_COL_DESCRIPTION           "Description"
#define SAM_DB_COL_COMMENT               "Comment"
#define SAM_DB_COL_UID                   "UID"
#define SAM_DB_COL_PASSWORD              "Password"
#define SAM_DB_COL_ACCOUNT_FLAGS         "AccountFlags"
#define SAM_DB_COL_GECOS                 "Gecos"
#define SAM_DB_COL_HOME_DIR              "Homedir"
#define SAM_DB_COL_HOME_DRIVE            "Homedrive"
#define SAM_DB_COL_LOGON_SCRIPT          "LogonScript"
#define SAM_DB_COL_PROFILE_PATH          "ProfilePath"
#define SAM_DB_COL_WORKSTATIONS          "Workstations"
#define SAM_DB_COL_SHELL                 "LoginShell"
#define SAM_DB_COL_UNIX_HOME_DIR         "UnixHomedir"
#define SAM_DB_COL_PASSWORD_LAST_SET     "PasswordLastSet"
#define SAM_DB_COL_FULL_NAME             "FullName"
#define SAM_DB_COL_PARAMETERS            "Parameters"
#define SAM_DB_COL_ACCOUNT_EXPIRY        "AccountExpiry"
#define SAM_DB_COL_LM_HASH               "LMHash"
#define SAM_DB_COL_NT_HASH               "NTHash"
#define SAM_DB_COL_PRIMARY_GROUP         "PrimaryGroup"
#define SAM_DB_COL_GID                   "GID"
#define SAM_DB_COL_COUNTRY_CODE          "CountryCode"
#define SAM_DB_COL_CODE_PAGE             "CodePage"
#define SAM_DB_COL_MAX_PWD_AGE           "MaxPwdAge"
#define SAM_DB_COL_MIN_PWD_AGE           "MinPwdAge"
#define SAM_DB_COL_PWD_PROMPT_TIME       "PwdPromptTime"
#define SAM_DB_COL_LAST_LOGON            "LastLogon"
#define SAM_DB_COL_LAST_LOGOFF           "LastLogoff"
#define SAM_DB_COL_LOCKOUT_TIME          "LockoutTime"
#define SAM_DB_COL_LOGON_COUNT           "LogonCount"
#define SAM_DB_COL_BAD_PASSWORD_COUNT    "BadPwdCount"
#define SAM_DB_COL_LOGON_HOURS           "LogonHours"
#define SAM_DB_COL_MIN_PWD_LENGTH        "MinPwdLength"
#define SAM_DB_COL_PWD_HISTORY_LENGTH    "PwdHistoryLength"
#define SAM_DB_COL_PWD_PROPERTIES        "PwdProperties"
#define SAM_DB_COL_SEQUENCE_NUMBER       "SequenceNumber"
#define SAM_DB_COL_LOCKOUT_DURATION      "LockoutDuration"
#define SAM_DB_COL_LOCKOUT_WINDOW        "LockoutWindow"
#define SAM_DB_COL_LOCKOUT_THRESHOLD     "LockoutThreshold"
#define SAM_DB_COL_CREATED_TIME          "CreatedTime"

#define SAMDB_QUERY_CREATE_CONFIG_TABLE  \
    "CREATE TABLE " SAM_DB_CONFIG_TABLE " (\n"                                 \
                 "UIDCounter INTEGER,\n"                                       \
                 "GIDCounter INTEGER,\n"                                       \
                 "RIDCounter INTEGER,\n"                                       \
                 "Version    INTEGER\n"                                        \
                 ");\n"

#define SAMDB_QUERY_CREATE_OBJECTS_TABLE  \
    "CREATE TABLE %s (\n"                                                      \
                 SAM_DB_COL_RECORD_ID " INTEGER PRIMARY KEY AUTOINCREMENT,\n"  \
                 SAM_DB_COL_OBJECT_SID            " TEXT COLLATE NOCASE,\n"    \
                 SAM_DB_COL_SECURITY_DESCRIPTOR   " BLOB,\n"                   \
                 SAM_DB_COL_OBJECT_CLASS          " INTEGER,\n"                \
                 SAM_DB_COL_LIKEWISE_FLAGS        " INTEGER,\n"                \
                 SAM_DB_COL_DOMAIN                " TEXT COLLATE NOCASE,\n"    \
                 SAM_DB_COL_SAM_ACCOUNT_NAME      " TEXT COLLATE NOCASE,\n"    \
                 SAM_DB_COL_USER_PRINCIPAL_NAME   " TEXT COLLATE NOCASE,\n"    \
                 SAM_DB_COL_DESCRIPTION           " TEXT,\n"                   \
                 SAM_DB_COL_COMMENT               " TEXT,\n"                   \
                 SAM_DB_COL_UID                   " INTEGER,\n"                \
                 SAM_DB_COL_PASSWORD              " TEXT,\n"                   \
                 SAM_DB_COL_ACCOUNT_FLAGS         " INTEGER,\n"                \
                 SAM_DB_COL_GECOS                 " TEXT,\n"                   \
                 SAM_DB_COL_HOME_DIR              " TEXT,\n"                   \
                 SAM_DB_COL_HOME_DRIVE            " TEXT,\n"                   \
                 SAM_DB_COL_LOGON_SCRIPT          " TEXT,\n"                   \
                 SAM_DB_COL_PROFILE_PATH          " TEXT,\n"                   \
                 SAM_DB_COL_WORKSTATIONS          " TEXT,\n"                   \
                 SAM_DB_COL_PARAMETERS            " TEXT,\n"                   \
                 SAM_DB_COL_SHELL                 " TEXT,\n"                   \
                 SAM_DB_COL_UNIX_HOME_DIR         " TEXT,\n"                   \
                 SAM_DB_COL_PASSWORD_LAST_SET     " INTEGER,\n"                \
                 SAM_DB_COL_FULL_NAME             " TEXT,\n"                   \
                 SAM_DB_COL_ACCOUNT_EXPIRY        " INTEGER,\n"                \
                 SAM_DB_COL_LM_HASH               " BLOB,\n"                   \
                 SAM_DB_COL_NT_HASH               " BLOB,\n"                   \
                 SAM_DB_COL_PRIMARY_GROUP         " INTEGER,\n"                \
                 SAM_DB_COL_GID                   " INTEGER,\n"                \
                 SAM_DB_COL_COUNTRY_CODE          " INTEGER,\n"                \
                 SAM_DB_COL_CODE_PAGE             " INTEGER,\n"                \
                 SAM_DB_COL_MAX_PWD_AGE           " INTEGER,\n"                \
                 SAM_DB_COL_MIN_PWD_AGE           " INTEGER,\n"                \
                 SAM_DB_COL_PWD_PROMPT_TIME       " INTEGER,\n"                \
                 SAM_DB_COL_LAST_LOGON            " INTEGER,\n"                \
                 SAM_DB_COL_LAST_LOGOFF           " INTEGER,\n"                \
                 SAM_DB_COL_LOCKOUT_TIME          " INTEGER,\n"                \
                 SAM_DB_COL_LOGON_COUNT           " INTEGER,\n"                \
                 SAM_DB_COL_BAD_PASSWORD_COUNT    " INTEGER,\n"                \
                 SAM_DB_COL_LOGON_HOURS           " BLOB,\n"                   \
                 SAM_DB_COL_MIN_PWD_LENGTH        " INTEGER,\n"                \
                 SAM_DB_COL_PWD_HISTORY_LENGTH    " INTEGER,\n"                \
                 SAM_DB_COL_PWD_PROPERTIES        " INTEGER,\n"                \
                 SAM_DB_COL_SEQUENCE_NUMBER       " INTEGER,\n"                \
                 SAM_DB_COL_LOCKOUT_DURATION      " INTEGER,\n"                \
                 SAM_DB_COL_LOCKOUT_WINDOW        " INTEGER,\n"                \
                 SAM_DB_COL_LOCKOUT_THRESHOLD     " INTEGER,\n"                \
                 SAM_DB_COL_CREATED_TIME " DATE DEFAULT (DATETIME('now')),\n"  \
     "UNIQUE(" SAM_DB_COL_OBJECT_SID "),\n"                                    \
     "CHECK(" SAM_DB_COL_OBJECT_CLASS      " == 1 \n"                          \
            " OR " SAM_DB_COL_OBJECT_CLASS " == 2 \n"                          \
            " OR " SAM_DB_COL_OBJECT_CLASS " == 3 \n"                          \
            " OR " SAM_DB_COL_OBJECT_CLASS " == 4)\n"                          \
                 ");\n"

#define SAMDB_QUERY_CREATE_MEMBERS_TABLE \
    "CREATE TABLE %s (\n"                                                      \
                 SAM_DB_COL_GROUP_RECORD_ID       " INTEGER,\n"                \
                 SAM_DB_COL_MEMBER_RECORD_ID      " INTEGER,\n"                \
                 SAM_DB_COL_CREATED_TIME " DATE DEFAULT (DATETIME('now')),\n"  \
  "UNIQUE(" SAM_DB_COL_GROUP_RECORD_ID ", " SAM_DB_COL_MEMBER_RECORD_ID "),\n" \
  "FOREIGN KEY (" SAM_DB_COL_GROUP_RECORD_ID ") \n"                            \
  "    REFERENCES " SAM_DB_OBJECTS_TABLE " (" SAM_DB_COL_RECORD_ID "),\n"      \
                 "FOREIGN KEY (" SAM_DB_COL_MEMBER_RECORD_ID ") \n"            \
       "    REFERENCES " SAM_DB_OBJECTS_TABLE " (" SAM_DB_COL_RECORD_ID ")\n"  \
                 ");\n"

typedef enum
{
    SAMDB_OBJECT_CLASS_UNKNOWN = 0,
    SAMDB_OBJECT_CLASS_DOMAIN,
    SAMDB_OBJECT_CLASS_USER,
    SAMDB_OBJECT_CLASS_LOCAL_GROUP,
    SAMDB_OBJECT_CLASS_FOREIGN_SECURITY_PRINCIPAL,
    SAMDB_OBJECT_CLASS_SENTINEL
} SAMDB_OBJECT_CLASS, *PSAMDB_OBJECT_CLASS;


#define SAMDB_ATTR_RECORD_ID \
    {'O','b','j','e','c','t','R','e','c','o','r','d','I','d',0}
#define SAMDB_ATTR_GROUP_RECORD_ID \
    {'G','r','o','u','p','R','e','c','o','r','d','I','d',0}
#define SAMDB_ATTR_MEMBER_RECORD_ID \
    {'M','e','m','b','e','r','R','e','c','o','r','d','I','d',0}
#define SAMDB_ATTR_OBJECT_SID \
    {'O','b','j','e','c','t','S','I','D',0}
#define SAMDB_ATTR_SECURITY_DESCRIPTOR \
    {'S','e','c','u','r','i','t','y','D','e','s','c','r','i','p','t','o','r',0}
#define SAMDB_ATTR_OBJECT_CLASS \
    {'O','b','j','e','c','t','C','l','a','s','s',0}
#define SAMDB_ATTR_LIKEWISE_FLAGS \
    {'L','i','k','e','w','i','s','e','F','l','a','g','s',0}
#define SAMDB_ATTR_DOMAIN \
    {'D','o','m','a','i','n',0}
#define SAMDB_ATTR_SAM_ACCOUNT_NAME \
    {'S','a','m','A','c','c','o','u','n','t','N','a','m','e',0}
#define SAMDB_ATTR_USER_PRINCIPAL_NAME \
    {'U','s','e','r','P','r','i','n','c','i','p','a','l','N','a','m','e',0}
#define SAMDB_ATTR_DESCRIPTION \
    {'D','e','s','c','r','i','p','t','i','o','n',0}
#define SAMDB_ATTR_COMMENT \
    {'C','o','m','m','e','n','t',0}
#define SAMDB_ATTR_UID \
    {'U','I','D',0}
#define SAMDB_ATTR_PASSWORD \
    {'P','a','s','s','w','o','r','d',0}
#define SAMDB_ATTR_ACCOUNT_FLAGS \
    {'A','c','c','o','u','n','t','F','l','a','g','s',0}
#define SAMDB_ATTR_GECOS \
    {'G','e','c','o','s',0}
#define SAMDB_ATTR_HOME_DIR \
    {'H','o','m','e','d','i','r',0}
#define SAMDB_ATTR_HOME_DRIVE \
    {'H','o','m','e','d','r','i','v','e',0}
#define SAMDB_ATTR_LOGON_SCRIPT \
    {'L','o','g','o','n','S','c','r','i','p','t',0}
#define SAMDB_ATTR_PROFILE_PATH \
    {'P','r','o','f','i','l','e','P','a','t','h',0}
#define SAMDB_ATTR_WORKSTATIONS \
    {'W','o','r','k','s','t','a','t','i','o','n','s',0}
#define SAMDB_ATTR_PARAMETERS \
    {'P','a','r','a','m','e','t','e','r','s',0}
#define SAMDB_ATTR_SHELL \
    {'L','o','g','i','n','S','h','e','l','l',0}
#define SAMDB_ATTR_UNIX_HOME_DIR \
    {'U','n','i','x','H','o','m','e','d','i','r',0}
#define SAMDB_ATTR_PASSWORD_LAST_SET \
    {'P','a','s','s','w','o','r','d','L','a','s','t','S','e','t',0}
#define SAMDB_ATTR_FULL_NAME \
    {'F','u','l','l','N','a','m','e',0}
#define SAMDB_ATTR_ACCOUNT_EXPIRY \
    {'A','c','c','o','u','n','t','E','x','p','i','r','y',0}
#define SAMDB_ATTR_LM_HASH \
    {'L','M','H','a','s','h',0}
#define SAMDB_ATTR_NT_HASH \
    {'N','T','H','a','s','h',0}
#define SAMDB_ATTR_PRIMARY_GROUP \
    {'P','r','i','m','a','r','y','G','r','o','u','p',0}
#define SAMDB_ATTR_GID \
    {'G','I','D',0}
#define SAMDB_ATTR_COUNTRY_CODE \
    {'C','o','u','n','t','r','y','C','o','d','e',0}
#define SAMDB_ATTR_CODE_PAGE \
    {'C','o','d','e','P','a','g','e',0}
#define SAMDB_ATTR_MAX_PWD_AGE \
    {'M','a','x','P','w','d','A','g','e',0}
#define SAMDB_ATTR_MIN_PWD_AGE \
    {'M','i','n','P','w','d','A','g','e',0}
#define SAMDB_ATTR_PWD_PROMPT_TIME \
    {'P','w','d','P','r','o','m','p','t','T','i','m','e',0}
#define SAMDB_ATTR_LAST_LOGON \
    {'L','a','s','t','L','o','g','o','n',0}
#define SAMDB_ATTR_LAST_LOGOFF \
    {'L','a','s','t','L','o','g','o','f','f',0}
#define SAMDB_ATTR_LOCKOUT_TIME \
    {'L','o','c','k','o','u','t','T','i','m','e',0}
#define SAMDB_ATTR_LOGON_COUNT \
    {'L','o','g','o','n','C','o','u','n','t',0}
#define SAMDB_ATTR_BAD_PASSWORD_COUNT \
    {'B','a','d','P','w','d','C','o','u','n','t',0}
#define SAMDB_ATTR_LOGON_HOURS \
    {'L','o','g','o','n','H','o','u','r','s',0}
#define SAMDB_ATTR_MIN_PWD_LENGTH \
    {'M','i','n','P','w','d','L','e','n','g','t','h',0}
#define SAMDB_ATTR_PWD_HISTORY_LENGTH \
    {'P','w','d','H','i','s','t','o','r','y','L','e','n','g','t','h',0}
#define SAMDB_ATTR_PWD_PROPERTIES \
    {'P','w','d','P','r','o','p','e','r','t','i','e','s',0}
#define SAMDB_ATTR_SEQUENCE_NUMBER \
    {'S','e','q','u','e','n','c','e','N','u','m','b','e','r',0}
#define SAMDB_ATTR_LOCKOUT_DURATION \
    {'L','o','c','k','o','u','t','D','u','r','a','t','i','o','n',0}
#define SAMDB_ATTR_LOCKOUT_WINDOW \
    {'L','o','c','k','o','u','t','W','i','n','d','o','w',0}
#define SAMDB_ATTR_LOCKOUT_THRESHOLD \
    {'L','o','c','k','o','u','t','T','h','r','e','s','h','o','l','d',0}
#define SAMDB_ATTR_CREATED_TIME \
    {'C','r','e','a','t','e','d','T','i','m','e',0}
#define SAMDB_ATTR_MEMBERS \
    {'M','e','m','b','e','r','s',0}


#define SAMDB_ATTR_NAME_MAX_LEN    (32)
#define SAMDB_COL_NAME_MAX_LEN     (32)


#endif /* __SAM_DB_TABLE_H__ */
