smb_info.c revision 8c10a8659ac31335ed870a1711c0182623f72fd6
/*
* 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
*/
/*
* Copyright 2008 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
#include <stdarg.h>
#include <unistd.h>
#include <stdlib.h>
#include <time.h>
#include <synch.h>
#include <syslog.h>
#include <string.h>
#include <strings.h>
#include <errno.h>
#include <netdb.h>
#include <resolv.h>
static smb_ntdomain_t smbpdc_cache;
static mutex_t smbpdc_mtx;
static mutex_t seqnum_mtx;
extern int getdomainname(char *, int);
/*
* smb_getdomaininfo
*
* Returns a pointer to the cached domain data. The caller can specify
* whether or not he is prepared to wait if the cache is not yet valid
* and for how long. The specified timeout is in seconds.
*/
{
int err;
if (timeout != 0) {
(void) mutex_lock(&smbpdc_mtx);
while (smbpdc_cache.ipaddr == 0) {
break;
}
(void) mutex_unlock(&smbpdc_mtx);
}
if (smbpdc_cache.ipaddr != 0)
return (&smbpdc_cache);
else
return (0);
}
void
{
char ipstr[16];
sizeof (ipstr));
}
/*
* smb_setdomaininfo
*
* Set the information for the specified domain. If the information is
* non-null, the notification event is raised to wakeup any threads
* blocking on the cache.
*/
void
{
char *p;
/*
* Remove DNS domain name extension
* to avoid confusing NetBIOS.
*/
*p = '\0';
*p = '\0';
(void) mutex_lock(&smbpdc_mtx);
(void) cond_broadcast(&smbpdc_cv);
(void) mutex_unlock(&smbpdc_mtx);
}
}
void
{
if ((kcfg->skc_keepalive != 0) &&
sizeof (kcfg->skc_resource_domain));
1);
sizeof (kcfg->skc_system_comment));
}
/*
* Get the current system NetBIOS name. The hostname is truncated at
* the first `.` or 15 bytes, whichever occurs first, and converted
* to uppercase (by smb_gethostname). Text that appears after the
* first '.' is considered to be part of the NetBIOS scope.
*
* Returns 0 on success, otherwise -1 to indicate an error.
*/
int
{
return (-1);
if (buflen >= NETBIOS_NAME_SZ)
return (0);
}
/*
* Get the current system node name. The returned name is guaranteed
* to be null-terminated (gethostname may not null terminate the name).
* If the hostname has been fully-qualified for some reason, the domain
* part will be removed. If the caller would like the name in upper
* case, it is folded to uppercase.
*
* If gethostname fails, the returned buffer will contain an empty
* string.
*/
int
{
char *p;
return (-1);
*buf = '\0';
return (-1);
}
*p = '\0';
if (upcase)
(void) utf8_strupr(buf);
return (0);
}
/*
* Obtain the fully-qualified name for this machine. If the
* hostname is fully-qualified, accept it. Otherwise, try to
* find an appropriate domain name to append to the hostname.
*/
int
{
char hostname[MAXHOSTNAMELEN];
char domain[MAXHOSTNAMELEN];
hostname[0] = '\0';
domain[0] = '\0';
return (-1);
return (-1);
if (hostname[0] == '\0')
return (-1);
if (domain[0] == '\0') {
return (0);
}
return (0);
}
/*
* smb_resolve_netbiosname
*
* Convert the fully-qualified domain name (i.e. fqdn) to a NETBIOS name.
* Upon success, the NETBIOS name will be returned via buf parameter.
* Returns 0 upon success. Otherwise, returns -1.
*/
int
{
char *p;
if (!buf)
return (-1);
*buf = '\0';
if (!fqdn)
return (-1);
*p = 0;
return (0);
}
/*
* smb_getdomainname
*
* Returns NETBIOS name of the domain if the system is in domain
* mode. Or returns workgroup name if the system is in workgroup
* mode.
*/
int
{
char domain[MAXHOSTNAMELEN];
int rc;
return (-1);
*buf = '\0';
sizeof (domain));
return (-1);
return (0);
}
/*
* smb_getdomainsid
*
* Returns the domain SID if the system is in domain mode.
* Otherwise returns NULL.
*
* Note: Callers are responsible for freeing a returned SID.
*/
smb_getdomainsid(void)
{
char buf[MAXHOSTNAMELEN];
int security_mode;
int rc;
if (security_mode != SMB_SECMODE_DOMAIN)
return (NULL);
*buf = '\0';
return (NULL);
return (NULL);
return (sid);
}
/*
* smb_resolve_fqdn
*
* Converts the NETBIOS name of the domain (i.e. nbt_domain) to a fully
* qualified domain name. The domain from either the domain field or
* search list field of the /etc/resolv.conf will be returned via the
* buf parameter if the first label of the domain matches the given
* NETBIOS name.
*
* Returns -1 upon error. If a match is found, returns 1. Otherwise,
* returns 0.
*/
int
{
struct __res_state res_state;
int i, found = 0;
char *p;
int dlen;
if (!buf)
return (-1);
*buf = '\0';
if (!nbt_domain)
return (-1);
return (-1);
if (*nbt_domain == '\0') {
return (0);
}
return (1);
}
return (1);
}
found = 1;
break;
}
}
return (found);
}
/*
* smb_getfqdomainname
*
* If the domain_name property value is FQDN, it will be returned.
* In domain mode, the domain from either the domain field or
* search list field of the /etc/resolv.conf will be returned via the
* buf parameter if the first label of the domain matches the
* domain_name property. In workgroup mode, it returns the local
* domain.
*
* Returns 0 upon success. Otherwise, returns -1.
*/
int
{
char domain[MAXHOSTNAMELEN];
int rc = 0;
return (-1);
*buf = '\0';
if (smb_config_get_secmode() == SMB_SECMODE_DOMAIN) {
sizeof (domain));
return (-1);
rc = -1;
} else {
}
} else {
rc = -1;
}
return (rc);
}
/*
* smb_set_machine_passwd
*
* This function should be used when setting the machine password property.
* The associated sequence number is incremented.
*/
static int
smb_set_machine_passwd(char *passwd)
{
int rc = -1;
return (-1);
(void) mutex_lock(&seqnum_mtx);
== SMBD_SMF_OK)
rc = 0;
(void) mutex_unlock(&seqnum_mtx);
return (rc);
}
/*
* smb_match_netlogon_seqnum
*
* A sequence number is associated with each machine password property
* update and the netlogon credential chain setup. If the
* sequence numbers don't match, a NETLOGON credential chain
* establishment is required.
*
* Returns 0 if kpasswd_seqnum equals to netlogon_seqnum. Otherwise,
* returns -1.
*/
{
(void) mutex_lock(&seqnum_mtx);
(void) mutex_unlock(&seqnum_mtx);
return (setpasswd_seqnum == netlogon_seqnum);
}
/*
* smb_setdomainprops
*
* This function should be called after joining an AD to
* set all the domain related SMF properties.
*
* The kpasswd_domain property is the AD domain to which the system
* is joined via kclient. If this function is invoked by the SMB
* daemon, fqdn should be set to NULL.
*/
int
{
return (-1);
return (-1);
return (-1);
return (-1);
if (smb_set_machine_passwd(passwd) != 0) {
" machine account password");
return (-1);
}
/*
* If we successfully create a trust account, we mark
* ourselves as a domain member in the environment so
* that we use the SAMLOGON version of the NETLOGON
* PDC location protocol.
*/
return (0);
}
/*
* smb_update_netlogon_seqnum
*
* This function should only be called upon a successful netlogon
* credential chain establishment to set the sequence number of the
* netlogon to match with that of the kpasswd.
*/
void
{
(void) mutex_lock(&seqnum_mtx);
(void) mutex_unlock(&seqnum_mtx);
}
/*
* Temporary fbt for dtrace until user space sdt enabled.
*/
void
smb_tracef(const char *fmt, ...)
{
char buf[128];
}
/*
* Temporary fbt for dtrace until user space sdt enabled.
*/
void
smb_trace(const char *s)
{
}
/*
* smb_tonetbiosname
*
* Creates a NetBIOS name based on the given name and suffix.
* NetBIOS name is 15 capital characters, padded with space if needed
* and the 16th byte is the suffix.
*/
void
{
char tmp_name[NETBIOS_NAME_SZ];
unsigned int cpid;
int len;
len = 0;
cpid = oem_get_smb_cpid();
if (rc > 0)
}
if (len) {
(void) utf8_strupr(tmp_name);
}
}
int
{
int i, cnt;
struct __res_state res_state;
return (0);
return (0);
for (i = 0; i < cnt; i++) {
if (i >= sz)
break;
}
return (i);
}