19fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States/*
29fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States * CDDL HEADER START
39fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States *
49fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States * The contents of this file are subject to the terms of the
59fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States * Common Development and Distribution License (the "License").
69fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States * You may not use this file except in compliance with the License.
79fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States *
89fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
99fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States * or http://www.opensolaris.org/os/licensing.
109fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States * See the License for the specific language governing permissions
119fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States * and limitations under the License.
129fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States *
139fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States * When distributing Covered Code, include this CDDL HEADER in each
149fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
159fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States * If applicable, add the following below this CDDL HEADER, with the
169fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States * fields enclosed by brackets "[]" replaced with your own identifying
179fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States * information: Portions Copyright [yyyy] [name of copyright owner]
189fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States *
199fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States * CDDL HEADER END
209fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States */
21148c5f4Alan Wright
229fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States/*
23148c5f4Alan Wright * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
24b3700b0Gordon Ross * Copyright 2014 Nexenta Systems, Inc.  All rights reserved.
259fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States */
269fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States
279fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States#include <strings.h>
289fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States#include <errno.h>
299fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States#include <unistd.h>
309fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States#include <fcntl.h>
319fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States#include <dirent.h>
32148c5f4Alan Wright#include <dlfcn.h>
339fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States#include <pthread.h>
349fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States#include <syslog.h>
359fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States#include <sys/fs_reparse.h>
369fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States#include <uuid/uuid.h>
379fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States
389fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States#include <smbsrv/libsmb.h>
399fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States#include <smbsrv/libmlsvc.h>
40148c5f4Alan Wright#include <smbsrv/smb_dfs.h>
41148c5f4Alan Wright#include <smbsrv/smb_share.h>
429fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States#include <dfs.h>
439fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States
449fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States/*
459fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States * default timeout (TTL) values (in second) for root and link
469fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States */
479fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States#define	DFS_ROOT_TIMEOUT		300
489fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States#define	DFS_LINK_TIMEOUT		1800
499fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States
509fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States/*
519fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States * DFS link data format in reparse point
529fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States *
539fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States * ver:state:prop:timeout:guid:ntarget:cmntlen:comment
549fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States *    [[:tserver:tshare:tstate:pclass:prank]...]
559fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States */
569fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States#define	DFS_LINK_V1			1
579fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States#define	DFS_LINK_HDR_NFIELDS		7	/* # fields in header section */
589fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States#define	DFS_LINK_TRGT_NFIELDS		5	/* # fields for each target */
599fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States
609fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States#define	DFS_ROOT_XATTR			"SUNWdfs.rootinfo"
619fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States
629fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States#define	DFS_INFO_ALL			0
639fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States
64148c5f4Alan Wrightstatic void *dfs_intr_hdl = NULL;
65148c5f4Alan Wright
66148c5f4Alan Wrightstatic struct {
67148c5f4Alan Wright	int (*dfsops_remote_count)(uint32_t *);
68148c5f4Alan Wright} dfs_intr_ops;
69148c5f4Alan Wright
709fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States/*
719fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States * Namespace cache
729fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States *
739fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States * Caches links' UNC and filesystem path where the key is the UNC path.
749fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States */
759fb67eaafshin salek ardakani - Sun Microsystems - Irvine United Statesstatic smb_cache_t dfs_nscache;
769fb67eaafshin salek ardakani - Sun Microsystems - Irvine United Statesstatic char dfs_nbname[NETBIOS_NAME_SZ];
779fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States
789fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States/*
79cb17486joyce mcintosh * The name of cached namespace. This will be the only
80cb17486joyce mcintosh * exported namespace until hosting multiple namespaces
81cb17486joyce mcintosh * is supported
82cb17486joyce mcintosh */
83cb17486joyce mcintoshstatic char dfs_cached_ns[MAXNAMELEN];
84cb17486joyce mcintoshstatic mutex_t dfs_nsmtx;
85cb17486joyce mcintosh
86cb17486joyce mcintosh/*
879fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States * Lock for accessing root information (extended attribute)
889fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States */
899fb67eaafshin salek ardakani - Sun Microsystems - Irvine United Statesstatic rwlock_t dfs_root_rwl;
909fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States
919fb67eaafshin salek ardakani - Sun Microsystems - Irvine United Statesextern uint32_t srvsvc_shr_setdfsroot(smb_share_t *, boolean_t);
929fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States
939fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States/*
949fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States * Namespace functions
959fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States */
969fb67eaafshin salek ardakani - Sun Microsystems - Irvine United Statesstatic boolean_t dfs_namespace_findlink(const char *, char *, char *, size_t);
979fb67eaafshin salek ardakani - Sun Microsystems - Irvine United Statesstatic void *dfs_namespace_cache(void *);
98cb17486joyce mcintoshstatic boolean_t dfs_namespace_iscached(const char *);
999fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States
1009fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States/*
1019fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States * Root functions
1029fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States */
1039fb67eaafshin salek ardakani - Sun Microsystems - Irvine United Statesstatic int dfs_root_add(const char *, dfs_info_t *);
1049fb67eaafshin salek ardakani - Sun Microsystems - Irvine United Statesstatic uint32_t dfs_root_remove(const char *);
1059fb67eaafshin salek ardakani - Sun Microsystems - Irvine United Statesstatic uint32_t dfs_root_encode(dfs_info_t *, char **, size_t *);
1069fb67eaafshin salek ardakani - Sun Microsystems - Irvine United Statesstatic uint32_t dfs_root_decode(dfs_info_t *, char *, size_t, uint32_t);
1079fb67eaafshin salek ardakani - Sun Microsystems - Irvine United Statesstatic uint32_t dfs_root_isvalidstate(uint32_t);
1089fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States
1099fb67eaafshin salek ardakani - Sun Microsystems - Irvine United Statesstatic int dfs_root_xopen(const char *, int);
1109fb67eaafshin salek ardakani - Sun Microsystems - Irvine United Statesstatic void dfs_root_xclose(int);
1119fb67eaafshin salek ardakani - Sun Microsystems - Irvine United Statesstatic uint32_t dfs_root_xwrite(int, dfs_info_t *);
1129fb67eaafshin salek ardakani - Sun Microsystems - Irvine United Statesstatic uint32_t dfs_root_xread(int, dfs_info_t *, uint32_t);
1139fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States
1149fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States/*
1159fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States * Link functions
1169fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States */
1179fb67eaafshin salek ardakani - Sun Microsystems - Irvine United Statesstatic uint32_t dfs_link_encode(dfs_info_t *, char *, size_t);
1189fb67eaafshin salek ardakani - Sun Microsystems - Irvine United Statesstatic uint32_t dfs_link_decode(dfs_info_t *, char *, uint32_t);
1199fb67eaafshin salek ardakani - Sun Microsystems - Irvine United Statesstatic uint32_t dfs_link_commit(const char *, dfs_info_t *);
1209fb67eaafshin salek ardakani - Sun Microsystems - Irvine United Statesstatic boolean_t dfs_link_isvalidstate(uint32_t);
1219fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States
1229fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States/*
1239fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States * Target functions
1249fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States */
1259fb67eaafshin salek ardakani - Sun Microsystems - Irvine United Statesstatic void dfs_target_init(dfs_target_t *, const char *, const char *,
1269fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States    uint32_t);
1279fb67eaafshin salek ardakani - Sun Microsystems - Irvine United Statesstatic int dfs_target_find(dfs_target_t *, uint32_t, const char *,
1289fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States    const char *);
1299fb67eaafshin salek ardakani - Sun Microsystems - Irvine United Statesstatic boolean_t dfs_target_isvalidstate(uint32_t);
1309fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States
1319fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States/*
1329fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States * Cache functions
1339fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States */
1349fb67eaafshin salek ardakani - Sun Microsystems - Irvine United Statesstatic uint32_t dfs_cache_add_byunc(const char *, const char *, uint32_t);
1359fb67eaafshin salek ardakani - Sun Microsystems - Irvine United Statesstatic void dfs_cache_populate(const char *, const char *);
1369fb67eaafshin salek ardakani - Sun Microsystems - Irvine United Statesstatic int dfs_cache_cmp(const void *, const void *);
137cb17486joyce mcintoshstatic void dfs_cache_flush(const char *);
138cb17486joyce mcintoshstatic uint32_t dfs_cache_nscount(void);
1399fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States
1409fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States/*
1419fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States * Utility functions
1429fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States */
1439fb67eaafshin salek ardakani - Sun Microsystems - Irvine United Statesstatic boolean_t dfs_path_isdir(const char *);
1449fb67eaafshin salek ardakani - Sun Microsystems - Irvine United Statesstatic uint32_t dfs_modinfo(uint32_t, dfs_info_t *, dfs_info_t *, uint32_t);
1459fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States
1469fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States/*
1479fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States * DFS module initializationr:
1489fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States *
1499fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States * - creates the namespace cache
1509fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States * - gets system's NetBIOS name
1519fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States */
1529fb67eaafshin salek ardakani - Sun Microsystems - Irvine United Statesvoid
1539fb67eaafshin salek ardakani - Sun Microsystems - Irvine United Statesdfs_init(void)
1549fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States{
1559fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States	smb_cache_create(&dfs_nscache, 0, dfs_cache_cmp, free, bcopy,
1569fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States	    sizeof (dfs_nscnode_t));
1579fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States
158b3700b0Gordon Ross	if (smb_getnetbiosname(dfs_nbname, sizeof (dfs_nbname)) != 0) {
159b3700b0Gordon Ross		syslog(LOG_ERR, "dfs: can't get machine name");
1609fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States		return;
161b3700b0Gordon Ross	}
162148c5f4Alan Wright
163148c5f4Alan Wright	bzero((void *)&dfs_intr_ops, sizeof (dfs_intr_ops));
164148c5f4Alan Wright
165148c5f4Alan Wright	if ((dfs_intr_hdl = smb_dlopen()) == NULL)
166148c5f4Alan Wright		return;
167148c5f4Alan Wright
168148c5f4Alan Wright	if ((dfs_intr_ops.dfsops_remote_count =
169148c5f4Alan Wright	    (int (*)())dlsym(dfs_intr_hdl, "smb_dfs_remote_count")) == NULL) {
170148c5f4Alan Wright		smb_dlclose(dfs_intr_hdl);
171148c5f4Alan Wright		dfs_intr_hdl = NULL;
172148c5f4Alan Wright		bzero((void *)&dfs_intr_ops, sizeof (dfs_intr_ops));
173148c5f4Alan Wright	}
1749fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States}
1759fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States
1769fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States/*
1779fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States * DFS module cleanup:
1789fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States *
1799fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States * - destroys the namespace cache
1809fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States */
1819fb67eaafshin salek ardakani - Sun Microsystems - Irvine United Statesvoid
1829fb67eaafshin salek ardakani - Sun Microsystems - Irvine United Statesdfs_fini(void)
1839fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States{
184148c5f4Alan Wright	smb_dlclose(dfs_intr_hdl);
1859fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States	smb_cache_destroy(&dfs_nscache);
1869fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States}
1879fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States
188