/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
*/
#include <stdio.h>
#include <strings.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <synch.h>
#include <pwd.h>
#include <grp.h>
#include <assert.h>
#include <syslog.h>
#include <pthread.h>
/*
* Domain cache states
*/
#define SMB_DCACHE_STATE_NONE 0
/*
* Cache lock modes
*/
#define SMB_DCACHE_RDLOCK 0
typedef struct smb_domain_cache {
static uint32_t smb_domain_add_local(void);
static void smb_dcache_create(void);
static void smb_dcache_destroy(void);
static uint32_t smb_dcache_lock(int);
static void smb_dcache_unlock(void);
static void smb_dcache_remove(smb_domain_t *);
static void smb_dcache_setdc(char *);
static void smb_dcache_getdc(char *, size_t);
static boolean_t smb_dcache_wait(void);
static uint32_t smb_dcache_updating(void);
static void smb_dcache_ready(void);
static void smb_dcache_error(uint32_t, const char *, ...);
/*
* Domain Join (In progress?)
*/
/*
* DC failover
*/
typedef enum smb_dc_state {
typedef struct smb_dc_report_t {
static smb_dc_state_t smb_dc_get_state(void);
{
(void) rw_rdlock(&smb_djoin_lock);
(void) rw_unlock(&smb_djoin_lock);
return (in_progress);
}
void
smb_domainjoin_start(void)
{
(void) rw_wrlock(&smb_djoin_lock);
(void) rw_unlock(&smb_djoin_lock);
}
void
smb_domainjoin_end(void)
{
(void) rw_wrlock(&smb_djoin_lock);
(void) rw_unlock(&smb_djoin_lock);
}
void
smb_dc_info_init(void)
{
}
/*
* Threads that detect a DC failure should call this function to report a
* failed DC to DC fail-over thread. DC state is set to SMB_DC_STATE_FAILED.
*
* If the join operation is in progress, this function is treated as a no-op
* because the reported failure with the currently selected DC is expected.
* Hence, DC failover is not necessary.
*/
void
{
if (smb_domainjoin_inprogress())
return;
(void) mutex_lock(&dc_failure_mtx);
sizeof (reported_dc.r_domain));
sizeof (reported_dc.r_srv));
(void) cond_broadcast(&dc_failure_cv);
(void) mutex_unlock(&dc_failure_mtx);
}
/*
* The caller will block until a DC failure occurs.
*/
void
smb_dc_await_failure(void)
{
(void) mutex_lock(&dc_failure_mtx);
(void) mutex_unlock(&dc_failure_mtx);
}
/*
* DC reselection is required if the selected DC has been reported with a
* recent failure. If the join operation is in progress, DC reselection
* shouldn't be performed. If either the current AD domain or selected DC
* cannot be obtained from SMF repository, DC reselection will not be performed.
*/
smb_dc_need_reselection(void)
{
if (smb_domainjoin_inprogress())
return (B_FALSE);
return (B_FALSE);
return (B_FALSE);
return (reselect);
}
/*
* DC fail-over thread will ignore a DC failure report if the reported DC isn't
* the selected DC. DC state will be set to SMB_DC_STATE_READY and the reported
* DC name will be cleared since no failure is against the current selected DC.
*/
void
smb_dc_ignore_failure(void)
{
(void) mutex_lock(&dc_reselect_mtx);
(void) cond_broadcast(&dc_reselect_cv);
(void) mutex_unlock(&dc_reselect_mtx);
}
/*
* DC state is set to SMB_DC_STATE_UPDATING.
* DC fail-over thread calls this function to inform reporter threads that
* fail-over operation is in progress. Any DC failure reports that come in
* during this time will be ignored.
*/
void
smb_dc_reselecting(void)
{
}
/*
* DC fail-over thread calls this function to awake all reporter threads that
* are blocked on DC reselection. If DC fail-over succeeds, the DC state will
* be set to SMB_DC_STATE_READY. Otherwise, the DC state will be set to
* SMB_DC_STATE_UPDATE_FAILED.
*/
void
{
(void) mutex_lock(&dc_reselect_mtx);
if (success) {
} else {
" failed. %s\\%s remains selected.",
}
(void) cond_broadcast(&dc_reselect_cv);
(void) mutex_unlock(&dc_reselect_mtx);
}
/*
* Caller will block until a DC fail-over has completed or cond_reltimedwait()
* times out.
*
* If the join operation is in progress, this function doesn't need to be
* blocked on DC reselection as that will be skipped by DC failover service.
*/
void
smb_dc_await_reselection(void)
{
if (smb_domainjoin_inprogress())
return;
state = smb_dc_get_state();
if ((state != SMB_DC_STATE_READY) &&
(state != SMB_DC_STATE_UPDATE_FAILED)) {
(void) mutex_lock(&dc_reselect_mtx);
pthread_self());
"timed out", pthread_self());
(void) mutex_unlock(&dc_reselect_mtx);
}
}
/*
* domain cache one time initialization. This function should
* only be called during service startup.
*
* Returns 0 on success and an error code on failure.
*/
void
smb_domain_init(void)
{
if ((rc = smb_domain_add_local()) != 0) {
return;
}
if (rc != SMB_DOMAIN_SUCCESS) {
return;
}
if (rc == SMB_DOMAIN_NOMACHINE_SID) {
return;
}
}
/*
* Destroys the cache upon service termination
*/
void
smb_domain_fini(void)
{
}
/*
* Add a domain structure to domain cache. There is no checking
* for duplicates.
*/
static uint32_t
{
return (SMB_DOMAIN_INVALID_ARG);
}
return (res);
}
/*
* Lookup a domain by its name. The passed name is the NETBIOS or fully
* qualified DNS name or non-qualified DNS name.
*
* If the requested domain is found and given 'di' pointer is not NULL
* it'll be filled with the domain information and B_TRUE is returned.
* If the caller only needs to check a domain existence it can pass
* NULL for 'di' and just check the return value.
*
* If the domain is not in the cache B_FALSE is returned.
*/
{
char *p;
return (B_FALSE);
return (B_FALSE);
while (dcnode) {
if (found) {
if (di)
break;
}
*p = '\0';
0) == 0);
*p = '.';
if (found) {
if (di)
break;
}
}
}
return (found);
}
/*
* Lookup a domain by its SID.
*
* If the requested domain is found and given 'di' pointer is not NULL
* it'll be filled with the domain information and B_TRUE is returned.
* If the caller only needs to check a domain existence it can pass
* NULL for 'di' and just check the return value.
*
* If the domain is not in the cache B_FALSE is returned.
*/
{
return (B_FALSE);
return (B_FALSE);
while (dcnode) {
if (found) {
if (di)
break;
}
}
return (found);
}
/*
* Lookup a domain by its type.
*
* If the requested domain is found and given 'di' pointer is not NULL
* it'll be filled with the domain information and B_TRUE is returned.
* If the caller only needs to check a domain existence it can pass
* NULL for 'di' and just check the return value.
*
* If the domain is not in the cache B_FALSE is returned.
*/
{
return (B_FALSE);
while (dcnode) {
if (di)
break;
}
}
return (found);
}
/*
* Returns primary domain information plus the name of
* the selected domain controller.
*/
{
if (!success)
return (B_FALSE);
}
{
if (*buf == '\0')
return (*buf != '\0');
}
{
return (smb_config_setdc(dc) == 0);
}
/*
* Transfer the cache to updating state.
* In this state any request for reading the cache would
* be blocked until the update is finished.
*/
smb_domain_start_update(void)
{
return (smb_dcache_updating());
}
/*
* Transfer the cache from updating to ready state.
*/
void
smb_domain_end_update(void)
{
}
/*
* Updates the cache with given information for the primary
* domain, possible trusted domains and the selected domain
* controller.
*
* Before adding the new entries existing entries of type
* primary and trusted will be removed from cache.
*/
void
{
int i;
return;
while (dcnode) {
} else {
}
}
}
}
void
{
return;
}
void
{
return;
}
void
{
return;
}
/*
* from the domain cache. Also return the SMB_CI_DC_SELECTED property
* if the primary domain exists in the cache.
*/
int
{
return (NT_STATUS_INTERNAL_ERROR);
while (domain) {
case SMB_DOMAIN_PRIMARY:
/* LINTED: E_CASE_FALLTHRU */
case SMB_DOMAIN_TRUSTED:
case SMB_DOMAIN_UNTRUSTED:
case SMB_DOMAIN_LOCAL:
return (NT_STATUS_NO_MEMORY);
}
break;
default:
break;
}
}
if (found_primary)
sizeof (domains_info->d_dc_name));
return (NT_STATUS_SUCCESS);
}
/*
* Free the memory allocated for the domain list and destroy the list.
*/
void
{
}
}
/*
* Add an entry for the local domain to the domain cache
*/
static uint32_t
smb_domain_add_local(void)
{
char *lsidstr;
return (SMB_DOMAIN_NOMACHINE_SID);
return (SMB_DOMAIN_NOMACHINE_SID);
}
*ad_domain = '\0';
return (SMB_DOMAIN_SUCCESS);
}
/*
* Add an entry for the primary domain to the domain cache
*/
static uint32_t
{
int rc;
if (secmode != SMB_SECMODE_DOMAIN)
return (SMB_DOMAIN_SUCCESS);
if (rc != SMBD_SMF_OK)
return (SMB_DOMAIN_NODOMAIN_SID);
return (SMB_DOMAIN_NODOMAIN_NAME);
return (SMB_DOMAIN_SUCCESS);
}
/*
* Initialize the domain cache.
* This function does not populate the cache.
*/
static void
smb_dcache_create(void)
{
return;
}
smb_dcache.dc_nops = 0;
}
/*
* Removes and frees all the cache entries
*/
static void
smb_dcache_flush(void)
{
}
/*
* Destroys the cache.
*/
static void
smb_dcache_destroy(void)
{
while (smb_dcache.dc_nops > 0)
&smb_dcache.dc_mtx);
}
}
/*
* Lock the cache with the specified mode.
* If the cache is in updating state and a read lock is
* requested, the lock won't be granted until either the
* update is finished or SMB_DCACHE_UPDATE_WAIT has passed.
*
* Whenever a lock is granted, the number of inflight cache
* operations is incremented.
*/
static uint32_t
{
switch (smb_dcache.dc_state) {
case SMB_DCACHE_STATE_NONE:
return (SMB_DOMAIN_INTERNAL_ERR);
if (mode == SMB_DCACHE_RDLOCK) {
/*
* Read operations should wait until the update
* is completed.
*/
if (!smb_dcache_wait()) {
return (SMB_DOMAIN_INTERNAL_ERR);
}
}
default:
break;
}
/*
* Lock has to be taken outside the mutex otherwise
* there could be a deadlock
*/
if (mode == SMB_DCACHE_RDLOCK)
else
return (SMB_DOMAIN_SUCCESS);
}
/*
* Decrement the number of inflight operations and then unlock.
*/
static void
smb_dcache_unlock(void)
{
}
/*
* This function MUST be called with smb_dcache.dc_cache_lck held for writing
* (i.e. smb_dcache_lock(SMB_DCACHE_WRLOCK)).
*/
static uint32_t
{
return (SMB_DOMAIN_NO_MEMORY);
return (SMB_DOMAIN_NO_MEMORY);
}
return (SMB_DOMAIN_SUCCESS);
}
/*
* This function MUST be called with smb_dcache.dc_cache_lck held for writing
* (i.e. smb_dcache_lock(SMB_DCACHE_WRLOCK)).
*/
static void
{
}
static void
{
}
static void
{
}
/*
* Waits for SMB_DCACHE_UPDATE_WAIT seconds if cache is in
* UPDATING state. Upon wake up returns true if cache is
* ready to be used, otherwise it returns false.
*
* This function MUST be called with smb_dcache.dc_mtx held.
*/
static boolean_t
smb_dcache_wait(void)
{
int err;
break;
}
}
/*
* Transfers the cache into READ_DELAY state, this will ensure
* any read access to the cache during domain join will be
* stalled until the update is finished. This is to ensure
* that new domain information is used by the operations.
*
* If another thread is already updating the cache, other
* callers will wait until cache is no longer in UPDATING
* state. The return code is decided based on the new
* state of the cache.
*/
{
switch (smb_dcache.dc_state) {
case SMB_DCACHE_STATE_READY:
break;
break;
&smb_dcache.dc_mtx);
} else {
}
break;
case SMB_DCACHE_STATE_NONE:
default:
break;
}
return (rc);
}
/*
* Transfer the cache from read_delay to ready state.
*/
void
{
}
/*
* Transfers the cache into UPDATING state, this will ensure
* any read access to the cache will be stalled until the
* update is finished. This is to avoid providing incomplete,
* inconsistent or stale information.
*
* If another thread is already updating the cache, other
* callers will wait until cache is no longer in UPDATING
* state. The return code is decided based on the new
* state of the cache.
*/
static uint32_t
smb_dcache_updating(void)
{
switch (smb_dcache.dc_state) {
case SMB_DCACHE_STATE_READY:
break;
break;
&smb_dcache.dc_mtx);
} else {
}
break;
case SMB_DCACHE_STATE_NONE:
default:
break;
}
return (rc);
}
/*
* Transfers the cache from READ_DELAY / UPDATING to READY state.
*
* Nothing will happen if the cache is no longer available
* or it is being destroyed.
*/
static void
smb_dcache_ready(void)
{
switch (smb_dcache.dc_state) {
break;
case SMB_DCACHE_STATE_NONE:
break;
default:
assert(0);
}
}
static void
{
static struct {
char *errtxt;
} errtab[] = {
{ SMB_DOMAIN_SUCCESS, "success" },
{ SMB_DOMAIN_NOMACHINE_SID, "no machine SID" },
{ SMB_DOMAIN_NODOMAIN_SID, "no domain SID" },
{ SMB_DOMAIN_NODOMAIN_NAME, "no domain name" },
{ SMB_DOMAIN_INTERNAL_ERR, "internal error" },
{ SMB_DOMAIN_INVALID_ARG, "invalid parameter" },
{ SMB_DOMAIN_NO_MEMORY, "no memory" },
{ SMB_DOMAIN_NO_CACHE, "no cache" }
};
int i;
for (i = 0; i < tablesz; ++i) {
break;
}
}
}
static smb_dc_state_t
smb_dc_get_state(void)
{
return (state);
}