/* -*- 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    2004-2009
 * 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:
 *
 *        shareapi.h
 *
 * Abstract:
 *
 *        Likewise Input Output (LWIO) - SRV
 *
 *        Share API
 *
 * Authors: Sriram Nambakam (snambakam@likewisesoftware.com)
 *
 */

#ifndef __SHAREAPI_H__
#define __SHAREAPI_H__

#include <smbwire.h>
#include <lw/list.h>
#include <elements_types.h>

#define LWIO_SRV_FILE_SYSTEM_PREFIX_A "C:\\"
#define LWIO_SRV_FILE_SYSTEM_PREFIX_W { 'C', ':', '\\', 0 }

#define LWIO_SRV_PIPE_SYSTEM_ROOT_A   "\\npfs"
#define LWIO_SRV_PIPE_SYSTEM_ROOT_W   { '\\', 'n', 'p', 'f', 's', 0 }

#define LWIO_SRV_SHARE_STRING_ID_IPC_A "IPC"
#define LWIO_SRV_SHARE_STRING_ID_IPC_W {'I','P','C',0}

#define LWIO_SRV_SHARE_STRING_ID_COMM_A "COMM"
#define LWIO_SRV_SHARE_STRING_ID_COMM_W {'C','O','M','M',0}

#define LWIO_SRV_SHARE_STRING_ID_PRINTER_A "LPT1:"
#define LWIO_SRV_SHARE_STRING_ID_PRINTER_W {'L','P','T','1',':',0}

#define LWIO_SRV_SHARE_STRING_ID_DISK_A "A:"
#define LWIO_SRV_SHARE_STRING_ID_DISK_W {'A',':',0}

#define LWIO_SRV_SHARE_STRING_ID_HOME_SHARE_W SRV_SHARE_REPOSITORY_SPECIAL_SHARE_HOME_W

typedef struct _SRV_SHARE_INFO
{
    LONG RefCount;

    pthread_rwlock_t Mutex;
    pthread_rwlock_t* pMutex;

    PWSTR pwszName;
    PWSTR pwszPath;
    PWSTR pwszComment;

    PSECURITY_DESCRIPTOR_RELATIVE pSecDesc;
    ULONG SecDescLen;

    PSECURITY_DESCRIPTOR_ABSOLUTE pAbsSecDesc;

    SHARE_STYPE Service;
    SHARE_INFO_FLAGS Flags;

    BOOLEAN DeleteInProgress;

    LW_LIST_LINKS TreeList;
    LW_LIST_LINKS Tree2List;

} SRV_SHARE_INFO, *PSRV_SHARE_INFO;

typedef struct _SRV_SHARE_INFO_REGISTRATION
{
    LW_LIST_LINKS Link;
    LW_LIST_LINKS RundownLink;

} SRV_SHARE_INFO_REGISTRATION, *PSRV_SHARE_INFO_REGISTRATION;


typedef struct SRV_SHARES_HANDLE_DATA *SRV_SHARES_HANDLE, **PSRV_SHARES_HANDLE;


NTSTATUS
SrvShareMapIdToServiceStringW(
    IN SHARE_STYPE Service,
    OUT PWSTR* ppwszService
    );

NTSTATUS
SrvShareMapIdToServiceStringA(
    IN SHARE_STYPE Service,
    OUT PSTR* ppszService
    );

NTSTATUS
SrvShareMapServiceStringToIdA(
    IN PCSTR pszService,
    IN OUT SHARE_STYPE* pService
    );

NTSTATUS
SrvShareMapServiceStringToIdW(
    IN PWSTR pwszService,
    OUT SHARE_STYPE* pService
    );

NTSTATUS
SrvShareMapFromWindowsPath(
    IN PWSTR pwszClientWin32Path,
    OUT PWSTR* ppwszIoMgrPath
    );

NTSTATUS
SrvShareMapToWindowsPath(
    IN PWSTR pwszIoMgrPath,
    OUT PWSTR* ppwszClientWin32Path
    );

NTSTATUS
SrvGetShareName(
    IN PWSTR pwszPath,
    OUT PWSTR* ppwszSharename
    );

NTSTATUS
SrvGetMaximalShareAccessMask(
    PSRV_SHARE_INFO pShareInfo,
    ACCESS_MASK* pMask
    );

NTSTATUS
SrvGetGuestShareAccessMask(
    PSRV_SHARE_INFO pShareInfo,
    ACCESS_MASK* pMask
    );

NTSTATUS
SrvGetCscFlags(
    PSRV_SHARE_INFO pShareInfo,
    SMB_CSC_FLAGS* pCscFlags
    );

VOID
SrvShareFreeSecurity(
    IN PSRV_SHARE_INFO pShareInfo
    );

NTSTATUS
SrvShareAccessCheck(
    IN PSRV_SHARE_INFO pShareInfo,
    IN PACCESS_TOKEN pToken,
    IN ACCESS_MASK DesiredAccess,
    IN PGENERIC_MAPPING pGenericMap,
    OUT PACCESS_MASK pGrantedAccess
    );

NTSTATUS
SrvShareSetSecurity(
    IN PSRV_SHARE_INFO pShareInfo,
    IN PSECURITY_DESCRIPTOR_RELATIVE pIncRelSecDesc,
    IN ULONG IncRelSecDescLen
    );

NTSTATUS
SrvShareSetDefaultSecurity(
    PSRV_SHARE_INFO pShareInfo
    );

NTSTATUS
SrvShareInit(
    IN PSTR pszFileSystemRoot,
    OUT PSRV_SHARES_HANDLE pHandle
    );

NTSTATUS
SrvShareReloadConfiguration(
    IN SRV_SHARES_HANDLE Handle
    );

NTSTATUS
SrvShareFindByName(
    IN SRV_SHARES_HANDLE Handle,
    IN PWSTR pwszShareName,
    IN OPTIONAL PCWSTR pDomain,
    IN OPTIONAL PCWSTR pUser,
    OUT PSRV_SHARE_INFO* ppShareInfo
    );

NTSTATUS
SrvShareGetName(
    IN PSRV_SHARE_INFO pInfo,
    IN OPTIONAL PCWSTR pDomain,
    IN OPTIONAL PCWSTR pUser,
    OUT PWSTR* ppName
    );

NTSTATUS
SrvShareGetPath(
    IN PSRV_SHARE_INFO pInfo,
    IN OPTIONAL PCWSTR pDomain,
    IN OPTIONAL PCWSTR pUser,
    OUT PWSTR* ppPath
    );

NTSTATUS
SrvShareAdd(
    IN SRV_SHARES_HANDLE Handle,
    IN PWSTR pwszShareName,
    IN PWSTR pwszPath,
    IN PWSTR pwszComment,
    IN PBYTE pSecDesc,
    IN ULONG SecDescLen,
    IN SHARE_STYPE ShareType,
    IN SHARE_INFO_FLAGS Flags
    );

NTSTATUS
SrvShareUpdate(
    IN SRV_SHARES_HANDLE Handle,
    IN PSRV_SHARE_INFO pShareInfo
    );

NTSTATUS
SrvShareDelete(
    IN SRV_SHARES_HANDLE Handle,
    IN PWSTR pwszShareName
    );

NTSTATUS
SrvShareEnum(
    IN SRV_SHARES_HANDLE Handle,
    IN PWSTR pDomain,
    IN PWSTR pUser,
    OUT PSRV_SHARE_INFO** pppShareInfo,
    IN OUT PULONG pNumEntries
    );

NTSTATUS
SrvShareDuplicateInfo(
    IN PSRV_SHARE_INFO pShareInfo,
    OUT PSRV_SHARE_INFO* ppShareInfo
    );

PSRV_SHARE_INFO
SrvShareAcquireInfo(
    IN PSRV_SHARE_INFO pShareInfo
    );

VOID
SrvShareReleaseInfo(
    IN PSRV_SHARE_INFO pShareInfo
    );

VOID
SrvShareShutdown(
    IN OUT PSRV_SHARES_HANDLE pHandle
    );

VOID
SrvShareInfoRegistrationInit(
    IN OUT PSRV_SHARE_INFO_REGISTRATION pRegistration
    );

NTSTATUS
SrvShareInfoTreeRegister(
    IN PSRV_SHARE_INFO pShareInfo,
    IN PLWIO_SRV_TREE pTree
    );

NTSTATUS
SrvShareInfoTree2Register(
    IN PSRV_SHARE_INFO pShareInfo,
    IN PLWIO_SRV_TREE_2 pTree
    );

BOOLEAN
SrvShareInfoTreeIsRegistered(
    IN PSRV_SHARE_INFO pShareInfo,
    IN PLWIO_SRV_TREE pTree
    );

BOOLEAN
SrvShareInfoTree2IsRegistered(
    IN PSRV_SHARE_INFO pShareInfo,
    IN PLWIO_SRV_TREE_2 pTree
    );

VOID
SrvShareInfoTreeUnregister(
    IN PSRV_SHARE_INFO pShareInfo,
    IN PLWIO_SRV_TREE pTree
    );

VOID
SrvShareInfoTree2Unregister(
    IN PSRV_SHARE_INFO pShareInfo,
    IN PLWIO_SRV_TREE_2 pTree
    );

#endif /* __SHAREAPI_H__ */

