1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 /*
27  * SMB/CIFS share cache implementation.
28  */
29 
30 #include <errno.h>
31 #include <synch.h>
32 #include <stdlib.h>
33 #include <strings.h>
34 #include <syslog.h>
35 #include <thread.h>
36 #include <pthread.h>
37 #include <assert.h>
38 #include <libshare.h>
39 #include <libzfs.h>
40 
41 #include <smbsrv/libsmb.h>
42 #include <smbsrv/libsmbns.h>
43 #include <smbsrv/libmlsvc.h>
44 
45 #include <smbsrv/lm.h>
46 #include <smbsrv/smb_share.h>
47 #include <smbsrv/cifs.h>
48 #include <smbsrv/nterror.h>
49 
50 #define	SMB_SHR_ERROR_THRESHOLD		3
51 
52 #define	SMB_SHR_CSC_BUFSZ		64
53 
54 /*
55  * Cache functions and vars
56  */
57 #define	SMB_SHR_HTAB_SZ			1024
58 
59 /*
60  * Cache handle
61  *
62  * Shares cache is a hash table.
63  *
64  * sc_cache		pointer to hash table handle
65  * sc_cache_lck		synchronize cache read/write accesses
66  * sc_state		cache state machine values
67  * sc_nops		number of inflight/pending cache operations
68  * sc_mtx		protects handle fields
69  */
70 typedef struct smb_shr_cache {
71 	HT_HANDLE	*sc_cache;
72 	rwlock_t	sc_cache_lck;
73 	mutex_t		sc_mtx;
74 	cond_t		sc_cv;
75 	uint32_t	sc_state;
76 	uint32_t	sc_nops;
77 } smb_shr_cache_t;
78 
79 /*
80  * Cache states
81  */
82 #define	SMB_SHR_CACHE_STATE_NONE	0
83 #define	SMB_SHR_CACHE_STATE_CREATED	1
84 #define	SMB_SHR_CACHE_STATE_DESTROYING	2
85 
86 /*
87  * Cache lock modes
88  */
89 #define	SMB_SHR_CACHE_RDLOCK	0
90 #define	SMB_SHR_CACHE_WRLOCK	1
91 
92 static smb_shr_cache_t smb_shr_cache;
93 
94 static uint32_t smb_shr_cache_create(void);
95 static void smb_shr_cache_destroy(void);
96 static uint32_t smb_shr_cache_lock(int);
97 static void smb_shr_cache_unlock(void);
98 static int smb_shr_cache_count(void);
99 static smb_share_t *smb_shr_cache_iterate(smb_shriter_t *);
100 
101 static smb_share_t *smb_shr_cache_findent(char *);
102 static uint32_t smb_shr_cache_addent(smb_share_t *);
103 static void smb_shr_cache_delent(char *);
104 static void smb_shr_cache_freent(HT_ITEM *);
105 
106 /*
107  * sharemgr functions
108  */
109 static void *smb_shr_sa_loadall(void *);
110 static void smb_shr_sa_loadgrp(sa_group_t);
111 static uint32_t smb_shr_sa_load(sa_share_t, sa_resource_t);
112 static uint32_t smb_shr_sa_loadbyname(char *);
113 static uint32_t smb_shr_sa_get(sa_share_t, sa_resource_t, smb_share_t *);
114 
115 /*
116  * .ZFS management functions
117  */
118 static void smb_shr_zfs_add(smb_share_t *);
119 static void smb_shr_zfs_remove(smb_share_t *);
120 static void smb_shr_zfs_rename(smb_share_t *, smb_share_t *);
121 
122 /*
123  * share publishing
124  */
125 #define	SMB_SHR_PUBLISH		0
126 #define	SMB_SHR_UNPUBLISH	1
127 
128 typedef struct smb_shr_pitem {
129 	list_node_t	spi_lnd;
130 	char		spi_name[MAXNAMELEN];
131 	char		spi_container[MAXPATHLEN];
132 	char		spi_op;
133 } smb_shr_pitem_t;
134 
135 /*
136  * publish queue states
137  */
138 #define	SMB_SHR_PQS_NOQUEUE	0
139 #define	SMB_SHR_PQS_READY	1	/* the queue is ready */
140 #define	SMB_SHR_PQS_PUBLISHING	2	/* publisher thread is running */
141 #define	SMB_SHR_PQS_STOPPING	3
142 
143 /*
144  * share publishing queue
145  */
146 typedef struct smb_shr_pqueue {
147 	list_t		spq_list;
148 	mutex_t		spq_mtx;
149 	cond_t		spq_cv;
150 	uint32_t	spq_state;
151 } smb_shr_pqueue_t;
152 
153 static smb_shr_pqueue_t ad_queue;
154 
155 static int smb_shr_publisher_start(void);
156 static void smb_shr_publisher_stop(void);
157 static void smb_shr_publisher_send(smb_ads_handle_t *, list_t *, const char *);
158 static void smb_shr_publisher_queue(const char *, const char *, char);
159 static void *smb_shr_publisher(void *);
160 static void smb_shr_publisher_flush(list_t *);
161 static void smb_shr_publish(const char *, const char *);
162 static void smb_shr_unpublish(const char *, const char *);
163 
164 /*
165  * Utility/helper functions
166  */
167 static uint32_t smb_shr_lookup(char *, smb_share_t *);
168 static uint32_t smb_shr_addipc(void);
169 static void smb_shr_set_oemname(smb_share_t *);
170 
171 
172 /*
173  * libshare handle and synchronization
174  */
175 typedef struct smb_sa_handle {
176 	sa_handle_t	sa_handle;
177 	mutex_t		sa_mtx;
178 	boolean_t	sa_in_service;
179 } smb_sa_handle_t;
180 
181 static smb_sa_handle_t smb_sa_handle;
182 
183 /*
184  * Creates and initializes the cache and starts the publisher
185  * thread.
186  */
187 int
188 smb_shr_start(void)
189 {
190 	(void) mutex_lock(&smb_sa_handle.sa_mtx);
191 	smb_sa_handle.sa_in_service = B_TRUE;
192 	(void) mutex_unlock(&smb_sa_handle.sa_mtx);
193 
194 	if (smb_shr_cache_create() != NERR_Success)
195 		return (ENOMEM);
196 
197 	if (smb_shr_addipc() != NERR_Success)
198 		return (ENOMEM);
199 
200 	return (smb_shr_publisher_start());
201 }
202 
203 void
204 smb_shr_stop(void)
205 {
206 	smb_shr_cache_destroy();
207 	smb_shr_publisher_stop();
208 
209 	(void) mutex_lock(&smb_sa_handle.sa_mtx);
210 	smb_sa_handle.sa_in_service = B_FALSE;
211 
212 	if (smb_sa_handle.sa_handle != NULL) {
213 		sa_fini(smb_sa_handle.sa_handle);
214 		smb_sa_handle.sa_handle = NULL;
215 	}
216 
217 	(void) mutex_unlock(&smb_sa_handle.sa_mtx);
218 }
219 
220 /*
221  * Get a handle and exclusive access to the libshare API.
222  */
223 sa_handle_t
224 smb_shr_sa_enter(void)
225 {
226 	(void) mutex_lock(&smb_sa_handle.sa_mtx);
227 	if (!smb_sa_handle.sa_in_service) {
228 		(void) mutex_unlock(&smb_sa_handle.sa_mtx);
229 		return (NULL);
230 	}
231 
232 	if (smb_sa_handle.sa_handle == NULL) {
233 		smb_sa_handle.sa_handle = sa_init(SA_INIT_SHARE_API);
234 		if (smb_sa_handle.sa_handle == NULL) {
235 			syslog(LOG_ERR, "share: failed to get libshare handle");
236 			(void) mutex_unlock(&smb_sa_handle.sa_mtx);
237 			return (NULL);
238 		}
239 	}
240 
241 	return (smb_sa_handle.sa_handle);
242 }
243 
244 /*
245  * Release exclusive access to the libshare API.
246  */
247 void
248 smb_shr_sa_exit(void)
249 {
250 	(void) mutex_unlock(&smb_sa_handle.sa_mtx);
251 }
252 
253 /*
254  * Launches a thread to populate the share cache by share information
255  * stored in sharemgr
256  */
257 int
258 smb_shr_load(void)
259 {
260 	pthread_t load_thr;
261 	pthread_attr_t tattr;
262 	int rc;
263 
264 	(void) pthread_attr_init(&tattr);
265 	(void) pthread_attr_setdetachstate(&tattr, PTHREAD_CREATE_DETACHED);
266 	rc = pthread_create(&load_thr, &tattr, smb_shr_sa_loadall, 0);
267 	(void) pthread_attr_destroy(&tattr);
268 
269 	return (rc);
270 }
271 
272 /*
273  * Return the total number of shares
274  */
275 int
276 smb_shr_count(void)
277 {
278 	int n_shares = 0;
279 
280 	if (smb_shr_cache_lock(SMB_SHR_CACHE_RDLOCK) == NERR_Success) {
281 		n_shares = smb_shr_cache_count();
282 		smb_shr_cache_unlock();
283 	}
284 
285 	return (n_shares);
286 }
287 
288 /*
289  * smb_shr_iterinit
290  *
291  * Initialize given iterator for traversing hash table.
292  */
293 void
294 smb_shr_iterinit(smb_shriter_t *shi)
295 {
296 	bzero(shi, sizeof (smb_shriter_t));
297 	shi->si_first = B_TRUE;
298 }
299 
300 /*
301  * smb_shr_iterate
302  *
303  * Iterate on the shares in the hash table. The iterator must be initialized
304  * before the first iteration. On subsequent calls, the iterator must be
305  * passed unchanged.
306  *
307  * Returns NULL on failure or when all shares are visited, otherwise
308  * returns information of visited share.
309  */
310 smb_share_t *
311 smb_shr_iterate(smb_shriter_t *shi)
312 {
313 	smb_share_t *share = NULL;
314 	smb_share_t *cached_si;
315 
316 	if (shi == NULL)
317 		return (NULL);
318 
319 	if (smb_shr_cache_lock(SMB_SHR_CACHE_RDLOCK) == NERR_Success) {
320 		if ((cached_si = smb_shr_cache_iterate(shi)) != NULL) {
321 			share = &shi->si_share;
322 			bcopy(cached_si, share, sizeof (smb_share_t));
323 		}
324 		smb_shr_cache_unlock();
325 	}
326 
327 	return (share);
328 }
329 
330 /*
331  * Adds the given share to cache, publishes the share in ADS
332  * if it has an AD container, calls kernel to take a hold on
333  * the shared file system. If it can't take a hold on the
334  * shared file system, it's either because shared directory
335  * does not exist or some other error has occurred, in any
336  * case the share is removed from the cache.
337  *
338  * If the specified share is an autohome share which already
339  * exists in the cache, just increments the reference count.
340  */
341 uint32_t
342 smb_shr_add(smb_share_t *si)
343 {
344 	smb_share_t *cached_si;
345 	uint32_t status;
346 	int rc;
347 
348 	assert(si != NULL);
349 
350 	if (!smb_shr_chkname(si->shr_name))
351 		return (ERROR_INVALID_NAME);
352 
353 	if (smb_shr_cache_lock(SMB_SHR_CACHE_WRLOCK) != NERR_Success)
354 		return (NERR_InternalError);
355 
356 	cached_si = smb_shr_cache_findent(si->shr_name);
357 	if (cached_si) {
358 		if (si->shr_flags & SMB_SHRF_AUTOHOME) {
359 			cached_si->shr_refcnt++;
360 			status = NERR_Success;
361 		} else {
362 			status = NERR_DuplicateShare;
363 		}
364 		smb_shr_cache_unlock();
365 		return (status);
366 	}
367 
368 	if ((status = smb_shr_cache_addent(si)) != NERR_Success) {
369 		smb_shr_cache_unlock();
370 		return (status);
371 	}
372 
373 	/* don't hold the lock across door call */
374 	smb_shr_cache_unlock();
375 
376 	/* call kernel to take a hold on the shared file system */
377 	rc = mlsvc_set_share(SMB_SHROP_ADD, si->shr_path, si->shr_name);
378 
379 	if (rc == 0) {
380 		smb_shr_publish(si->shr_name, si->shr_container);
381 
382 		/* If path is ZFS, add the .zfs/shares/<share> entry. */
383 		smb_shr_zfs_add(si);
384 
385 		return (NERR_Success);
386 	}
387 
388 	if (smb_shr_cache_lock(SMB_SHR_CACHE_WRLOCK) == NERR_Success) {
389 		smb_shr_cache_delent(si->shr_name);
390 		smb_shr_cache_unlock();
391 	}
392 
393 	/*
394 	 * rc == ENOENT means the shared directory doesn't exist
395 	 */
396 	return ((rc == ENOENT) ? NERR_UnknownDevDir : NERR_InternalError);
397 }
398 
399 /*
400  * Removes the specified share from cache, removes it from AD
401  * if it has an AD container, and calls the kernel to release
402  * the hold on the shared file system.
403  *
404  * If this is an autohome share then decrement the reference
405  * count. If it reaches 0 then it proceeds with removing steps.
406  */
407 uint32_t
408 smb_shr_remove(char *sharename)
409 {
410 	smb_share_t *si;
411 	char path[MAXPATHLEN];
412 	char container[MAXPATHLEN];
413 
414 	assert(sharename != NULL);
415 
416 	if (!smb_shr_chkname(sharename))
417 		return (ERROR_INVALID_NAME);
418 
419 	if (smb_shr_cache_lock(SMB_SHR_CACHE_WRLOCK) != NERR_Success)
420 		return (NERR_InternalError);
421 
422 	if ((si = smb_shr_cache_findent(sharename)) == NULL) {
423 		smb_shr_cache_unlock();
424 		return (NERR_NetNameNotFound);
425 	}
426 
427 	if (si->shr_type & STYPE_IPC) {
428 		/* IPC$ share cannot be removed */
429 		smb_shr_cache_unlock();
430 		return (ERROR_ACCESS_DENIED);
431 	}
432 
433 	if (si->shr_flags & SMB_SHRF_AUTOHOME) {
434 		if ((--si->shr_refcnt) > 0) {
435 			smb_shr_cache_unlock();
436 			return (NERR_Success);
437 		}
438 	}
439 
440 	/*
441 	 * If path is ZFS, remove the .zfs/shares/<share> entry.  Need
442 	 * to remove before cleanup of cache occurs.
443 	 */
444 	smb_shr_zfs_remove(si);
445 
446 	(void) strlcpy(path, si->shr_path, sizeof (path));
447 	(void) strlcpy(container, si->shr_container, sizeof (container));
448 	smb_shr_cache_delent(sharename);
449 	smb_shr_cache_unlock();
450 
451 	smb_shr_unpublish(sharename, container);
452 
453 	/* call kernel to release the hold on the shared file system */
454 	(void) mlsvc_set_share(SMB_SHROP_DELETE, path, sharename);
455 
456 	return (NERR_Success);
457 }
458 
459 /*
460  * Rename a share. Check that the current name exists and the new name
461  * doesn't exist. The rename is performed by deleting the current share
462  * definition and creating a new share with the new name.
463  */
464 uint32_t
465 smb_shr_rename(char *from_name, char *to_name)
466 {
467 	smb_share_t *from_si;
468 	smb_share_t to_si;
469 	uint32_t status;
470 
471 	assert((from_name != NULL) && (to_name != NULL));
472 
473 	if (!smb_shr_chkname(from_name) || !smb_shr_chkname(to_name))
474 		return (ERROR_INVALID_NAME);
475 
476 	if (smb_shr_cache_lock(SMB_SHR_CACHE_WRLOCK) != NERR_Success)
477 		return (NERR_InternalError);
478 
479 	if ((from_si = smb_shr_cache_findent(from_name)) == NULL) {
480 		smb_shr_cache_unlock();
481 		return (NERR_NetNameNotFound);
482 	}
483 
484 	if (from_si->shr_type & STYPE_IPC) {
485 		/* IPC$ share cannot be renamed */
486 		smb_shr_cache_unlock();
487 		return (ERROR_ACCESS_DENIED);
488 	}
489 
490 	if (smb_shr_cache_findent(to_name) != NULL) {
491 		smb_shr_cache_unlock();
492 		return (NERR_DuplicateShare);
493 	}
494 
495 	bcopy(from_si, &to_si, sizeof (smb_share_t));
496 	(void) strlcpy(to_si.shr_name, to_name, sizeof (to_si.shr_name));
497 
498 	/* If path is ZFS, rename the .zfs/shares/<share> entry. */
499 	smb_shr_zfs_rename(from_si, &to_si);
500 
501 	if ((status = smb_shr_cache_addent(&to_si)) != NERR_Success) {
502 		smb_shr_cache_unlock();
503 		return (status);
504 	}
505 
506 	smb_shr_cache_delent(from_name);
507 	smb_shr_cache_unlock();
508 
509 	smb_shr_unpublish(from_name, to_si.shr_container);
510 	smb_shr_publish(to_name, to_si.shr_container);
511 
512 	return (NERR_Success);
513 }
514 
515 /*
516  * Load the information for the specified share into the supplied share
517  * info structure.
518  *
519  * First looks up the cache to see if the specified share exists, if there
520  * is a miss then it looks up sharemgr.
521  */
522 uint32_t
523 smb_shr_get(char *sharename, smb_share_t *si)
524 {
525 	uint32_t status;
526 
527 	if (sharename == NULL || *sharename == '\0')
528 		return (NERR_NetNameNotFound);
529 
530 	if ((status = smb_shr_lookup(sharename, si)) == NERR_Success)
531 		return (status);
532 
533 	if ((status = smb_shr_sa_loadbyname(sharename)) == NERR_Success)
534 		status = smb_shr_lookup(sharename, si);
535 
536 	return (status);
537 }
538 
539 /*
540  * Modifies an existing share. Properties that can be modified are:
541  *
542  *   o comment
543  *   o AD container
544  *   o host access
545  */
546 uint32_t
547 smb_shr_modify(smb_share_t *new_si)
548 {
549 	smb_share_t *si;
550 	boolean_t adc_changed = B_FALSE;
551 	char old_container[MAXPATHLEN];
552 	uint32_t cscopt;
553 	uint32_t access;
554 
555 	assert(new_si != NULL);
556 
557 	if (smb_shr_cache_lock(SMB_SHR_CACHE_WRLOCK) != NERR_Success)
558 		return (NERR_InternalError);
559 
560 	if ((si = smb_shr_cache_findent(new_si->shr_name)) == NULL) {
561 		smb_shr_cache_unlock();
562 		return (NERR_NetNameNotFound);
563 	}
564 
565 	if (si->shr_type & STYPE_IPC) {
566 		/* IPC$ share cannot be modified */
567 		smb_shr_cache_unlock();
568 		return (ERROR_ACCESS_DENIED);
569 	}
570 
571 	(void) strlcpy(si->shr_cmnt, new_si->shr_cmnt, sizeof (si->shr_cmnt));
572 
573 	adc_changed = (strcmp(new_si->shr_container, si->shr_container) != 0);
574 	if (adc_changed) {
575 		/* save current container - needed for unpublishing */
576 		(void) strlcpy(old_container, si->shr_container,
577 		    sizeof (old_container));
578 		(void) strlcpy(si->shr_container, new_si->shr_container,
579 		    sizeof (si->shr_container));
580 	}
581 
582 	cscopt = (new_si->shr_flags & SMB_SHRF_CSC_MASK);
583 	si->shr_flags &= ~SMB_SHRF_CSC_MASK;
584 	si->shr_flags |= cscopt;
585 
586 	access = (new_si->shr_flags & SMB_SHRF_ACC_ALL);
587 	si->shr_flags &= ~SMB_SHRF_ACC_ALL;
588 	si->shr_flags |= access;
589 
590 	if (access & SMB_SHRF_ACC_NONE)
591 		(void) strlcpy(si->shr_access_none, new_si->shr_access_none,
592 		    sizeof (si->shr_access_none));
593 
594 	if (access & SMB_SHRF_ACC_RO)
595 		(void) strlcpy(si->shr_access_ro, new_si->shr_access_ro,
596 		    sizeof (si->shr_access_ro));
597 
598 	if (access & SMB_SHRF_ACC_RW)
599 		(void) strlcpy(si->shr_access_rw, new_si->shr_access_rw,
600 		    sizeof (si->shr_access_rw));
601 
602 	smb_shr_cache_unlock();
603 
604 	if (adc_changed) {
605 		smb_shr_unpublish(new_si->shr_name, old_container);
606 		smb_shr_publish(new_si->shr_name, new_si->shr_container);
607 	}
608 
609 	return (NERR_Success);
610 }
611 
612 /*
613  * smb_shr_exists
614  *
615  * Returns B_TRUE if the share exists. Otherwise returns B_FALSE
616  */
617 boolean_t
618 smb_shr_exists(char *sharename)
619 {
620 	boolean_t exists = B_FALSE;
621 
622 	if (sharename == NULL || *sharename == '\0')
623 		return (B_FALSE);
624 
625 	if (smb_shr_cache_lock(SMB_SHR_CACHE_RDLOCK) == NERR_Success) {
626 		exists = (smb_shr_cache_findent(sharename) != NULL);
627 		smb_shr_cache_unlock();
628 	}
629 
630 	return (exists);
631 }
632 
633 /*
634  * If the shared directory does not begin with a /, one will be
635  * inserted as a prefix. If ipaddr is not zero, then also return
636  * information about access based on the host level access lists, if
637  * present. Also return access check if there is an IP address and
638  * shr_accflags.
639  *
640  * The value of smb_chk_hostaccess is checked for an access match.
641  * -1 is wildcard match
642  * 0 is no match
643  * 1 is match
644  *
645  * Precedence is none is checked first followed by ro then rw if
646  * needed.  If x is wildcard (< 0) then check to see if the other
647  * values are a match. If a match, that wins.
648  *
649  * ipv6 is wide open for now, see smb_chk_hostaccess
650  */
651 void
652 smb_shr_hostaccess(smb_share_t *si, smb_inaddr_t *ipaddr)
653 {
654 	int acc = SMB_SHRF_ACC_OPEN;
655 
656 	/*
657 	 * Check to see if there area any share level access
658 	 * restrictions.
659 	 */
660 	if ((!smb_inet_iszero(ipaddr)) &&
661 	    (si->shr_flags & SMB_SHRF_ACC_ALL) != 0) {
662 		int none = SMB_SHRF_ACC_OPEN;
663 		int rw = SMB_SHRF_ACC_OPEN;
664 		int ro = SMB_SHRF_ACC_OPEN;
665 
666 		if (si->shr_flags & SMB_SHRF_ACC_NONE)
667 			none = smb_chk_hostaccess(ipaddr, si->shr_access_none);
668 		if (si->shr_flags & SMB_SHRF_ACC_RW)
669 			rw = smb_chk_hostaccess(ipaddr, si->shr_access_rw);
670 		if (si->shr_flags & SMB_SHRF_ACC_RO)
671 			ro = smb_chk_hostaccess(ipaddr, si->shr_access_ro);
672 		/* make first pass to get basic value */
673 		if (none != 0)
674 			acc = SMB_SHRF_ACC_NONE;
675 		else if (ro != 0)
676 			acc = SMB_SHRF_ACC_RO;
677 		else if (rw != 0)
678 			acc = SMB_SHRF_ACC_RW;
679 
680 		/* make second pass to handle '*' case */
681 		if (none < 0) {
682 			acc = SMB_SHRF_ACC_NONE;
683 			if (ro > 0)
684 				acc = SMB_SHRF_ACC_RO;
685 			else if (rw > 0)
686 				acc = SMB_SHRF_ACC_RW;
687 		} else if (ro < 0) {
688 			acc = SMB_SHRF_ACC_RO;
689 			if (none > 0)
690 				acc = SMB_SHRF_ACC_NONE;
691 			else if (rw > 0)
692 				acc = SMB_SHRF_ACC_RW;
693 		} else if (rw < 0) {
694 			acc = SMB_SHRF_ACC_RW;
695 			if (none > 0)
696 				acc = SMB_SHRF_ACC_NONE;
697 			else if (ro > 0)
698 				acc = SMB_SHRF_ACC_RO;
699 		}
700 	}
701 	si->shr_access_value = acc;	/* return access here */
702 }
703 
704 /*
705  * smb_shr_is_special
706  *
707  * Special share reserved for interprocess communication (IPC$) or
708  * remote administration of the server (ADMIN$). Can also refer to
709  * administrative shares such as C$, D$, E$, and so forth.
710  */
711 int
712 smb_shr_is_special(char *sharename)
713 {
714 	int len;
715 
716 	if (sharename == NULL)
717 		return (0);
718 
719 	if ((len = strlen(sharename)) == 0)
720 		return (0);
721 
722 	if (sharename[len - 1] == '$')
723 		return (STYPE_SPECIAL);
724 
725 	return (0);
726 }
727 
728 /*
729  * smb_shr_is_restricted
730  *
731  * Check whether or not there is a restriction on a share. Restricted
732  * shares are generally STYPE_SPECIAL, for example, IPC$. All the
733  * administration share names are restricted: C$, D$ etc. Returns B_TRUE
734  * if the share is restricted. Otherwise B_FALSE is returned to indicate
735  * that there are no restrictions.
736  */
737 boolean_t
738 smb_shr_is_restricted(char *sharename)
739 {
740 	static char *restricted[] = {
741 		"IPC$"
742 	};
743 
744 	int i;
745 
746 	if (sharename == NULL)
747 		return (B_FALSE);
748 
749 	for (i = 0; i < sizeof (restricted)/sizeof (restricted[0]); i++) {
750 		if (utf8_strcasecmp(restricted[i], sharename) == 0)
751 			return (B_TRUE);
752 	}
753 
754 	return (smb_shr_is_admin(sharename));
755 }
756 
757 /*
758  * smb_shr_is_admin
759  *
760  * Check whether or not access to the share should be restricted to
761  * administrators. This is a bit of a hack because what we're doing
762  * is checking for the default admin shares: C$, D$ etc.. There are
763  * other shares that have restrictions: see smb_shr_is_restricted().
764  *
765  * Returns B_TRUE if the shares is an admin share. Otherwise B_FALSE
766  * is returned to indicate that there are no restrictions.
767  */
768 boolean_t
769 smb_shr_is_admin(char *sharename)
770 {
771 	if (sharename == NULL)
772 		return (B_FALSE);
773 
774 	if (strlen(sharename) == 2 &&
775 	    mts_isalpha(sharename[0]) && sharename[1] == '$') {
776 		return (B_TRUE);
777 	}
778 
779 	return (B_FALSE);
780 }
781 
782 /*
783  * smb_shr_chkname
784  *
785  * Check for invalid characters in a share name.  The list of invalid
786  * characters includes control characters and the following:
787  *
788  * " / \ [ ] : | < > + ; , ? * =
789  */
790 boolean_t
791 smb_shr_chkname(char *sharename)
792 {
793 	char *invalid = "\"/\\[]:|<>+;,?*=";
794 	char *cp;
795 
796 	if (sharename == NULL)
797 		return (B_FALSE);
798 
799 	if (strpbrk(sharename, invalid))
800 		return (B_FALSE);
801 
802 	for (cp = sharename; *cp != '\0'; cp++) {
803 		if (iscntrl(*cp))
804 			return (B_FALSE);
805 	}
806 
807 	return (B_TRUE);
808 }
809 
810 /*
811  * smb_shr_get_realpath
812  *
813  * Derive the real path for a share from the path provided by a client.
814  * For instance, the real path of C:\ may be /cvol or the real path of
815  * F:\home may be /vol1/home.
816  *
817  * clntpath - path provided by the Windows client is in the
818  *            format of <drive letter>:\<dir>
819  * realpath - path that will be stored as the directory field of
820  *            the smb_share_t structure of the share.
821  * maxlen   - maximum length of the realpath buffer
822  *
823  * Return LAN Manager network error code.
824  */
825 uint32_t
826 smb_shr_get_realpath(const char *clntpath, char *realpath, int maxlen)
827 {
828 	const char *p;
829 	int len;
830 
831 	if ((p = strchr(clntpath, ':')) != NULL)
832 		++p;
833 	else
834 		p = clntpath;
835 
836 	(void) strlcpy(realpath, p, maxlen);
837 	(void) strcanon(realpath, "/\\");
838 	(void) strsubst(realpath, '\\', '/');
839 
840 	len = strlen(realpath);
841 	if ((len > 1) && (realpath[len - 1] == '/'))
842 		realpath[len - 1] = '\0';
843 
844 	return (NERR_Success);
845 }
846 
847 void
848 smb_shr_list(int offset, smb_shrlist_t *list)
849 {
850 	smb_shriter_t iterator;
851 	smb_share_t *si;
852 	int n = 0;
853 
854 	bzero(list, sizeof (smb_shrlist_t));
855 	smb_shr_iterinit(&iterator);
856 
857 	while ((si = smb_shr_iterate(&iterator)) != NULL) {
858 		if (--offset > 0)
859 			continue;
860 
861 		if ((si->shr_flags & SMB_SHRF_TRANS) &&
862 		    ((si->shr_type & STYPE_IPC) == 0)) {
863 			bcopy(si, &list->sl_shares[n], sizeof (smb_share_t));
864 			if (++n == LMSHARES_PER_REQUEST)
865 				break;
866 		}
867 	}
868 
869 	list->sl_cnt = n;
870 }
871 
872 /*
873  * ============================================
874  * Private helper/utility functions
875  * ============================================
876  */
877 
878 /*
879  * Looks up the given share in the cache and return
880  * the info in 'si'
881  */
882 static uint32_t
883 smb_shr_lookup(char *sharename, smb_share_t *si)
884 {
885 	smb_share_t *cached_si;
886 	uint32_t status = NERR_NetNameNotFound;
887 
888 	if (sharename == NULL || *sharename == '\0')
889 		return (NERR_NetNameNotFound);
890 	if (smb_shr_cache_lock(SMB_SHR_CACHE_RDLOCK) == NERR_Success) {
891 		cached_si = smb_shr_cache_findent(sharename);
892 		if (cached_si != NULL) {
893 			bcopy(cached_si, si, sizeof (smb_share_t));
894 			status = NERR_Success;
895 		}
896 
897 		smb_shr_cache_unlock();
898 	}
899 	return (status);
900 }
901 
902 /*
903  * Add IPC$ to the cache upon startup.
904  */
905 static uint32_t
906 smb_shr_addipc(void)
907 {
908 	smb_share_t ipc;
909 	uint32_t status = NERR_InternalError;
910 
911 	bzero(&ipc, sizeof (smb_share_t));
912 	(void) strcpy(ipc.shr_name, "IPC$");
913 	(void) strcpy(ipc.shr_cmnt, "Remote IPC");
914 	ipc.shr_flags = SMB_SHRF_TRANS;
915 	ipc.shr_type = STYPE_IPC;
916 
917 	if (smb_shr_cache_lock(SMB_SHR_CACHE_WRLOCK) == NERR_Success) {
918 		status = smb_shr_cache_addent(&ipc);
919 		smb_shr_cache_unlock();
920 	}
921 
922 	return (status);
923 }
924 
925 /*
926  * smb_shr_set_oemname
927  *
928  * Generate the OEM name for the specified share.  If the name is
929  * shorter than 13 bytes the oemname will be saved in si->shr_oemname.
930  * Otherwise si->shr_oemname will be empty and SMB_SHRF_LONGNAME will
931  * be set in si->shr_flags.
932  */
933 static void
934 smb_shr_set_oemname(smb_share_t *si)
935 {
936 	unsigned int cpid = oem_get_smb_cpid();
937 	mts_wchar_t *unibuf;
938 	char *oem_name;
939 	int length;
940 
941 	length = strlen(si->shr_name) + 1;
942 
943 	oem_name = malloc(length);
944 	unibuf = malloc(length * sizeof (mts_wchar_t));
945 	if ((oem_name == NULL) || (unibuf == NULL)) {
946 		free(oem_name);
947 		free(unibuf);
948 		return;
949 	}
950 
951 	(void) mts_mbstowcs(unibuf, si->shr_name, length);
952 
953 	if (unicodestooems(oem_name, unibuf, length, cpid) == 0)
954 		(void) strcpy(oem_name, si->shr_name);
955 
956 	free(unibuf);
957 
958 	if (strlen(oem_name) + 1 > SMB_SHARE_OEMNAME_MAX) {
959 		si->shr_flags |= SMB_SHRF_LONGNAME;
960 		*si->shr_oemname = '\0';
961 	} else {
962 		si->shr_flags &= ~SMB_SHRF_LONGNAME;
963 		(void) strlcpy(si->shr_oemname, oem_name,
964 		    SMB_SHARE_OEMNAME_MAX);
965 	}
966 
967 	free(oem_name);
968 }
969 
970 /*
971  * ============================================
972  * Cache management functions
973  *
974  * All cache functions are private
975  * ============================================
976  */
977 
978 /*
979  * Create the share cache (hash table).
980  */
981 static uint32_t
982 smb_shr_cache_create(void)
983 {
984 	uint32_t status = NERR_Success;
985 
986 	(void) mutex_lock(&smb_shr_cache.sc_mtx);
987 	switch (smb_shr_cache.sc_state) {
988 	case SMB_SHR_CACHE_STATE_NONE:
989 		smb_shr_cache.sc_cache = ht_create_table(SMB_SHR_HTAB_SZ,
990 		    MAXNAMELEN, 0);
991 		if (smb_shr_cache.sc_cache == NULL) {
992 			status = NERR_InternalError;
993 			break;
994 		}
995 
996 		(void) ht_register_callback(smb_shr_cache.sc_cache,
997 		    smb_shr_cache_freent);
998 		smb_shr_cache.sc_nops = 0;
999 		smb_shr_cache.sc_state = SMB_SHR_CACHE_STATE_CREATED;
1000 		break;
1001 
1002 	default:
1003 		assert(0);
1004 		status = NERR_InternalError;
1005 		break;
1006 	}
1007 	(void) mutex_unlock(&smb_shr_cache.sc_mtx);
1008 
1009 	return (status);
1010 }
1011 
1012 /*
1013  * Destroy the share cache (hash table).
1014  * Wait for inflight/pending operations to finish or abort before
1015  * destroying the cache.
1016  */
1017 static void
1018 smb_shr_cache_destroy(void)
1019 {
1020 	(void) mutex_lock(&smb_shr_cache.sc_mtx);
1021 	if (smb_shr_cache.sc_state == SMB_SHR_CACHE_STATE_CREATED) {
1022 		smb_shr_cache.sc_state = SMB_SHR_CACHE_STATE_DESTROYING;
1023 		while (smb_shr_cache.sc_nops > 0)
1024 			(void) cond_wait(&smb_shr_cache.sc_cv,
1025 			    &smb_shr_cache.sc_mtx);
1026 
1027 		smb_shr_cache.sc_cache = NULL;
1028 		smb_shr_cache.sc_state = SMB_SHR_CACHE_STATE_NONE;
1029 	}
1030 	(void) mutex_unlock(&smb_shr_cache.sc_mtx);
1031 }
1032 
1033 /*
1034  * If the cache is in "created" state, lock the cache for read
1035  * or read/write based on the specified mode.
1036  *
1037  * Whenever a lock is granted, the number of inflight cache
1038  * operations is incremented.
1039  */
1040 static uint32_t
1041 smb_shr_cache_lock(int mode)
1042 {
1043 	(void) mutex_lock(&smb_shr_cache.sc_mtx);
1044 	if (smb_shr_cache.sc_state != SMB_SHR_CACHE_STATE_CREATED) {
1045 		(void) mutex_unlock(&smb_shr_cache.sc_mtx);
1046 		return (NERR_InternalError);
1047 	}
1048 	smb_shr_cache.sc_nops++;
1049 	(void) mutex_unlock(&smb_shr_cache.sc_mtx);
1050 
1051 	/*
1052 	 * Lock has to be taken outside the mutex otherwise
1053 	 * there could be a deadlock
1054 	 */
1055 	if (mode == SMB_SHR_CACHE_RDLOCK)
1056 		(void) rw_rdlock(&smb_shr_cache.sc_cache_lck);
1057 	else
1058 		(void) rw_wrlock(&smb_shr_cache.sc_cache_lck);
1059 
1060 	return (NERR_Success);
1061 }
1062 
1063 /*
1064  * Decrement the number of inflight operations and then unlock.
1065  */
1066 static void
1067 smb_shr_cache_unlock(void)
1068 {
1069 	(void) mutex_lock(&smb_shr_cache.sc_mtx);
1070 	assert(smb_shr_cache.sc_nops > 0);
1071 	smb_shr_cache.sc_nops--;
1072 	(void) cond_broadcast(&smb_shr_cache.sc_cv);
1073 	(void) mutex_unlock(&smb_shr_cache.sc_mtx);
1074 
1075 	(void) rw_unlock(&smb_shr_cache.sc_cache_lck);
1076 }
1077 
1078 /*
1079  * Return the total number of shares
1080  */
1081 static int
1082 smb_shr_cache_count(void)
1083 {
1084 	return (ht_get_total_items(smb_shr_cache.sc_cache));
1085 }
1086 
1087 /*
1088  * looks up the given share name in the cache and if it
1089  * finds a match returns a pointer to the cached entry.
1090  * Note that since a pointer is returned this function
1091  * MUST be protected by smb_shr_cache_lock/unlock pair
1092  */
1093 static smb_share_t *
1094 smb_shr_cache_findent(char *sharename)
1095 {
1096 	HT_ITEM *item;
1097 
1098 	(void) utf8_strlwr(sharename);
1099 	item = ht_find_item(smb_shr_cache.sc_cache, sharename);
1100 	if (item && item->hi_data)
1101 		return ((smb_share_t *)item->hi_data);
1102 
1103 	return (NULL);
1104 }
1105 
1106 /*
1107  * Return a pointer to the first/next entry in
1108  * the cache based on the given iterator.
1109  *
1110  * Calls to this function MUST be protected by
1111  * smb_shr_cache_lock/unlock.
1112  */
1113 static smb_share_t *
1114 smb_shr_cache_iterate(smb_shriter_t *shi)
1115 {
1116 	HT_ITEM *item;
1117 
1118 	if (shi->si_first) {
1119 		item = ht_findfirst(smb_shr_cache.sc_cache, &shi->si_hashiter);
1120 		shi->si_first = B_FALSE;
1121 	} else {
1122 		item = ht_findnext(&shi->si_hashiter);
1123 	}
1124 
1125 	if (item && item->hi_data)
1126 		return ((smb_share_t *)item->hi_data);
1127 
1128 	return (NULL);
1129 }
1130 
1131 /*
1132  * Add the specified share to the cache.  Memory needs to be allocated
1133  * for the cache entry and the passed information is copied to the
1134  * allocated space.
1135  */
1136 static uint32_t
1137 smb_shr_cache_addent(smb_share_t *si)
1138 {
1139 	smb_share_t *cache_ent;
1140 	uint32_t status = NERR_Success;
1141 
1142 	if ((cache_ent = malloc(sizeof (smb_share_t))) == NULL)
1143 		return (ERROR_NOT_ENOUGH_MEMORY);
1144 
1145 	bcopy(si, cache_ent, sizeof (smb_share_t));
1146 
1147 	(void) utf8_strlwr(cache_ent->shr_name);
1148 	smb_shr_set_oemname(cache_ent);
1149 
1150 	if ((si->shr_type & STYPE_IPC) == 0)
1151 		cache_ent->shr_type = STYPE_DISKTREE;
1152 	cache_ent->shr_type |= smb_shr_is_special(cache_ent->shr_name);
1153 
1154 	if (smb_shr_is_admin(cache_ent->shr_name))
1155 		cache_ent->shr_flags |= SMB_SHRF_ADMIN;
1156 
1157 	if (si->shr_flags & SMB_SHRF_AUTOHOME)
1158 		cache_ent->shr_refcnt = 1;
1159 
1160 	if (ht_add_item(smb_shr_cache.sc_cache, cache_ent->shr_name, cache_ent)
1161 	    == NULL) {
1162 		syslog(LOG_DEBUG, "share: %s: cache update failed",
1163 		    cache_ent->shr_name);
1164 		free(cache_ent);
1165 		status = NERR_InternalError;
1166 	}
1167 
1168 	return (status);
1169 }
1170 
1171 /*
1172  * Delete the specified share from the cache.
1173  */
1174 static void
1175 smb_shr_cache_delent(char *sharename)
1176 {
1177 	(void) utf8_strlwr(sharename);
1178 	(void) ht_remove_item(smb_shr_cache.sc_cache, sharename);
1179 }
1180 
1181 /*
1182  * Call back to free the given cache entry.
1183  */
1184 static void
1185 smb_shr_cache_freent(HT_ITEM *item)
1186 {
1187 	if (item && item->hi_data)
1188 		free(item->hi_data);
1189 }
1190 
1191 /*
1192  * ============================================
1193  * Interfaces to sharemgr
1194  *
1195  * All functions in this section are private
1196  * ============================================
1197  */
1198 
1199 /*
1200  * Load shares from sharemgr
1201  */
1202 /*ARGSUSED*/
1203 static void *
1204 smb_shr_sa_loadall(void *args)
1205 {
1206 	sa_handle_t handle;
1207 	sa_group_t group, subgroup;
1208 	char *gstate;
1209 	boolean_t gdisabled;
1210 
1211 	if ((handle = smb_shr_sa_enter()) == NULL)
1212 		return (NULL);
1213 
1214 	for (group = sa_get_group(handle, NULL);
1215 	    group != NULL; group = sa_get_next_group(group)) {
1216 		gstate = sa_get_group_attr(group, "state");
1217 		if (gstate == NULL)
1218 			continue;
1219 
1220 		gdisabled = (strcasecmp(gstate, "disabled") == 0);
1221 		sa_free_attr_string(gstate);
1222 		if (gdisabled)
1223 			continue;
1224 
1225 		smb_shr_sa_loadgrp(group);
1226 
1227 		for (subgroup = sa_get_sub_group(group);
1228 		    subgroup != NULL;
1229 		    subgroup = sa_get_next_group(subgroup)) {
1230 			smb_shr_sa_loadgrp(subgroup);
1231 		}
1232 
1233 	}
1234 
1235 	smb_shr_sa_exit();
1236 	return (NULL);
1237 }
1238 
1239 /*
1240  * Load the shares contained in the specified group.
1241  *
1242  * Don't process groups on which the smb protocol is disabled.
1243  * The top level ZFS group won't have the smb protocol enabled
1244  * but sub-groups will.
1245  *
1246  * We will tolerate a limited number of errors and then give
1247  * up on the current group.  A typical error might be that the
1248  * shared directory no longer exists.
1249  */
1250 static void
1251 smb_shr_sa_loadgrp(sa_group_t group)
1252 {
1253 	sa_share_t share;
1254 	sa_resource_t resource;
1255 	int error_count = 0;
1256 
1257 	if (sa_get_optionset(group, SMB_PROTOCOL_NAME) == NULL)
1258 		return;
1259 
1260 	for (share = sa_get_share(group, NULL);
1261 	    share != NULL;
1262 	    share = sa_get_next_share(share)) {
1263 		for (resource = sa_get_share_resource(share, NULL);
1264 		    resource != NULL;
1265 		    resource = sa_get_next_resource(resource)) {
1266 			if (smb_shr_sa_load(share, resource))
1267 				++error_count;
1268 
1269 			if (error_count > SMB_SHR_ERROR_THRESHOLD)
1270 				break;
1271 		}
1272 
1273 		if (error_count > SMB_SHR_ERROR_THRESHOLD)
1274 			break;
1275 	}
1276 }
1277 
1278 /*
1279  * Load a share definition from sharemgr and add it to the cache.
1280  * If the share is already in the cache then it doesn't do anything.
1281  *
1282  * This function does not report duplicate shares as error since
1283  * a share might have been added by smb_shr_get() while load is
1284  * in progress.
1285  */
1286 static uint32_t
1287 smb_shr_sa_load(sa_share_t share, sa_resource_t resource)
1288 {
1289 	smb_share_t si;
1290 	char *sharename;
1291 	uint32_t status;
1292 	boolean_t loaded;
1293 
1294 	if ((sharename = sa_get_resource_attr(resource, "name")) == NULL)
1295 		return (NERR_InternalError);
1296 
1297 	loaded = smb_shr_exists(sharename);
1298 	sa_free_attr_string(sharename);
1299 
1300 	if (loaded)
1301 		return (NERR_Success);
1302 
1303 	if ((status = smb_shr_sa_get(share, resource, &si)) != NERR_Success) {
1304 		syslog(LOG_DEBUG, "share: failed to load %s (%d)",
1305 		    si.shr_name, status);
1306 		return (status);
1307 	}
1308 
1309 	status = smb_shr_add(&si);
1310 	if ((status != NERR_Success) && (status != NERR_DuplicateShare)) {
1311 		syslog(LOG_DEBUG, "share: failed to cache %s (%d)",
1312 		    si.shr_name, status);
1313 		return (status);
1314 	}
1315 
1316 	return (NERR_Success);
1317 }
1318 
1319 /*
1320  * Read the specified share information from sharemgr and return
1321  * it in the given smb_share_t structure.
1322  *
1323  * Shares read from sharemgr are marked as permanent/persistent.
1324  */
1325 static uint32_t
1326 smb_shr_sa_get(sa_share_t share, sa_resource_t resource, smb_share_t *si)
1327 {
1328 	sa_property_t prop;
1329 	sa_optionset_t opts;
1330 	char *val = NULL;
1331 	char *path;
1332 	char *rname;
1333 
1334 	if ((path = sa_get_share_attr(share, "path")) == NULL)
1335 		return (NERR_InternalError);
1336 
1337 	if ((rname = sa_get_resource_attr(resource, "name")) == NULL) {
1338 		sa_free_attr_string(path);
1339 		return (NERR_InternalError);
1340 	}
1341 
1342 	bzero(si, sizeof (smb_share_t));
1343 	si->shr_flags = SMB_SHRF_PERM;
1344 
1345 	(void) strlcpy(si->shr_path, path, sizeof (si->shr_path));
1346 	(void) strlcpy(si->shr_name, rname, sizeof (si->shr_name));
1347 	sa_free_attr_string(path);
1348 	sa_free_attr_string(rname);
1349 
1350 	val = sa_get_resource_description(resource);
1351 	if (val == NULL)
1352 		val = sa_get_share_description(share);
1353 
1354 	if (val != NULL) {
1355 		(void) strlcpy(si->shr_cmnt, val, sizeof (si->shr_cmnt));
1356 		sa_free_share_description(val);
1357 	}
1358 
1359 	opts = sa_get_derived_optionset(resource, SMB_PROTOCOL_NAME, 1);
1360 	if (opts == NULL)
1361 		return (NERR_Success);
1362 
1363 	prop = (sa_property_t)sa_get_property(opts, SHOPT_AD_CONTAINER);
1364 	if (prop != NULL) {
1365 		if ((val = sa_get_property_attr(prop, "value")) != NULL) {
1366 			(void) strlcpy(si->shr_container, val,
1367 			    sizeof (si->shr_container));
1368 			free(val);
1369 		}
1370 	}
1371 
1372 	prop = (sa_property_t)sa_get_property(opts, SHOPT_CSC);
1373 	if (prop != NULL) {
1374 		if ((val = sa_get_property_attr(prop, "value")) != NULL) {
1375 			smb_shr_sa_csc_option(val, si);
1376 			free(val);
1377 		}
1378 	}
1379 
1380 	prop = (sa_property_t)sa_get_property(opts, SHOPT_NONE);
1381 	if (prop != NULL) {
1382 		if ((val = sa_get_property_attr(prop, "value")) != NULL) {
1383 			(void) strlcpy(si->shr_access_none, val,
1384 			    sizeof (si->shr_access_none));
1385 			free(val);
1386 			si->shr_flags |= SMB_SHRF_ACC_NONE;
1387 		}
1388 	}
1389 
1390 	prop = (sa_property_t)sa_get_property(opts, SHOPT_RO);
1391 	if (prop != NULL) {
1392 		if ((val = sa_get_property_attr(prop, "value")) != NULL) {
1393 			(void) strlcpy(si->shr_access_ro, val,
1394 			    sizeof (si->shr_access_ro));
1395 			free(val);
1396 			si->shr_flags |= SMB_SHRF_ACC_RO;
1397 		}
1398 	}
1399 
1400 	prop = (sa_property_t)sa_get_property(opts, SHOPT_RW);
1401 	if (prop != NULL) {
1402 		if ((val = sa_get_property_attr(prop, "value")) != NULL) {
1403 			(void) strlcpy(si->shr_access_rw, val,
1404 			    sizeof (si->shr_access_rw));
1405 			free(val);
1406 			si->shr_flags |= SMB_SHRF_ACC_RW;
1407 		}
1408 	}
1409 
1410 	sa_free_derived_optionset(opts);
1411 	return (NERR_Success);
1412 }
1413 
1414 /*
1415  * Map a client-side caching (CSC) option to the appropriate share
1416  * flag.  Only one option is allowed; an error will be logged if
1417  * multiple options have been specified.  We don't need to do anything
1418  * about multiple values here because the SRVSVC will not recognize
1419  * a value containing multiple flags and will return the default value.
1420  *
1421  * If the option value is not recognized, it will be ignored: invalid
1422  * values will typically be caught and rejected by sharemgr.
1423  */
1424 void
1425 smb_shr_sa_csc_option(const char *value, smb_share_t *si)
1426 {
1427 	struct {
1428 		char *value;
1429 		uint32_t flag;
1430 	} cscopt[] = {
1431 		{ "disabled",	SMB_SHRF_CSC_DISABLED },
1432 		{ "manual",	SMB_SHRF_CSC_MANUAL },
1433 		{ "auto",	SMB_SHRF_CSC_AUTO },
1434 		{ "vdo",	SMB_SHRF_CSC_VDO }
1435 	};
1436 
1437 	int i;
1438 
1439 	for (i = 0; i < (sizeof (cscopt) / sizeof (cscopt[0])); ++i) {
1440 		if (strcasecmp(value, cscopt[i].value) == 0) {
1441 			si->shr_flags |= cscopt[i].flag;
1442 			break;
1443 		}
1444 	}
1445 
1446 	switch (si->shr_flags & SMB_SHRF_CSC_MASK) {
1447 	case 0:
1448 	case SMB_SHRF_CSC_DISABLED:
1449 	case SMB_SHRF_CSC_MANUAL:
1450 	case SMB_SHRF_CSC_AUTO:
1451 	case SMB_SHRF_CSC_VDO:
1452 		break;
1453 
1454 	default:
1455 		syslog(LOG_INFO, "csc option conflict: 0x%08x",
1456 		    si->shr_flags & SMB_SHRF_CSC_MASK);
1457 		break;
1458 	}
1459 }
1460 
1461 /*
1462  * looks up sharemgr for the given share (resource) and loads
1463  * the definition into cache if lookup is successful
1464  */
1465 static uint32_t
1466 smb_shr_sa_loadbyname(char *sharename)
1467 {
1468 	sa_handle_t handle;
1469 	sa_share_t share;
1470 	sa_resource_t resource;
1471 	uint32_t status;
1472 
1473 	if ((handle = smb_shr_sa_enter()) == NULL)
1474 		return (NERR_InternalError);
1475 
1476 	resource = sa_find_resource(handle, sharename);
1477 	if (resource == NULL) {
1478 		smb_shr_sa_exit();
1479 		return (NERR_NetNameNotFound);
1480 	}
1481 
1482 	share = sa_get_resource_parent(resource);
1483 	if (share == NULL) {
1484 		smb_shr_sa_exit();
1485 		return (NERR_InternalError);
1486 	}
1487 
1488 	status = smb_shr_sa_load(share, resource);
1489 
1490 	smb_shr_sa_exit();
1491 	return (status);
1492 }
1493 
1494 /*
1495  * ============================================
1496  * Share publishing functions
1497  *
1498  * All the functions are private
1499  * ============================================
1500  */
1501 
1502 static void
1503 smb_shr_publish(const char *sharename, const char *container)
1504 {
1505 	smb_shr_publisher_queue(sharename, container, SMB_SHR_PUBLISH);
1506 }
1507 
1508 static void
1509 smb_shr_unpublish(const char *sharename, const char *container)
1510 {
1511 	smb_shr_publisher_queue(sharename, container, SMB_SHR_UNPUBLISH);
1512 }
1513 
1514 /*
1515  * In domain mode, put a share on the publisher queue.
1516  * This is a no-op if the smb service is in Workgroup mode.
1517  */
1518 static void
1519 smb_shr_publisher_queue(const char *sharename, const char *container, char op)
1520 {
1521 	smb_shr_pitem_t *item = NULL;
1522 
1523 	if (container == NULL || *container == '\0')
1524 		return;
1525 
1526 	if (smb_config_get_secmode() != SMB_SECMODE_DOMAIN)
1527 		return;
1528 
1529 	(void) mutex_lock(&ad_queue.spq_mtx);
1530 	switch (ad_queue.spq_state) {
1531 	case SMB_SHR_PQS_READY:
1532 	case SMB_SHR_PQS_PUBLISHING:
1533 		break;
1534 	default:
1535 		(void) mutex_unlock(&ad_queue.spq_mtx);
1536 		return;
1537 	}
1538 	(void) mutex_unlock(&ad_queue.spq_mtx);
1539 
1540 	if ((item = malloc(sizeof (smb_shr_pitem_t))) == NULL)
1541 		return;
1542 
1543 	item->spi_op = op;
1544 	(void) strlcpy(item->spi_name, sharename, sizeof (item->spi_name));
1545 	(void) strlcpy(item->spi_container, container,
1546 	    sizeof (item->spi_container));
1547 
1548 	(void) mutex_lock(&ad_queue.spq_mtx);
1549 	list_insert_tail(&ad_queue.spq_list, item);
1550 	(void) cond_signal(&ad_queue.spq_cv);
1551 	(void) mutex_unlock(&ad_queue.spq_mtx);
1552 }
1553 
1554 /*
1555  * Publishing won't be activated if the smb service is running in
1556  * Workgroup mode.
1557  */
1558 static int
1559 smb_shr_publisher_start(void)
1560 {
1561 	pthread_t publish_thr;
1562 	pthread_attr_t tattr;
1563 	int rc;
1564 
1565 	if (smb_config_get_secmode() != SMB_SECMODE_DOMAIN)
1566 		return (0);
1567 
1568 	(void) mutex_lock(&ad_queue.spq_mtx);
1569 	if (ad_queue.spq_state != SMB_SHR_PQS_NOQUEUE) {
1570 		(void) mutex_unlock(&ad_queue.spq_mtx);
1571 		errno = EINVAL;
1572 		return (-1);
1573 	}
1574 
1575 	list_create(&ad_queue.spq_list, sizeof (smb_shr_pitem_t),
1576 	    offsetof(smb_shr_pitem_t, spi_lnd));
1577 	ad_queue.spq_state = SMB_SHR_PQS_READY;
1578 	(void) mutex_unlock(&ad_queue.spq_mtx);
1579 
1580 	(void) pthread_attr_init(&tattr);
1581 	(void) pthread_attr_setdetachstate(&tattr, PTHREAD_CREATE_DETACHED);
1582 	rc = pthread_create(&publish_thr, &tattr, smb_shr_publisher, 0);
1583 	(void) pthread_attr_destroy(&tattr);
1584 
1585 	return (rc);
1586 }
1587 
1588 static void
1589 smb_shr_publisher_stop(void)
1590 {
1591 	if (smb_config_get_secmode() != SMB_SECMODE_DOMAIN)
1592 		return;
1593 
1594 	(void) mutex_lock(&ad_queue.spq_mtx);
1595 	switch (ad_queue.spq_state) {
1596 	case SMB_SHR_PQS_READY:
1597 	case SMB_SHR_PQS_PUBLISHING:
1598 		ad_queue.spq_state = SMB_SHR_PQS_STOPPING;
1599 		(void) cond_signal(&ad_queue.spq_cv);
1600 		break;
1601 	default:
1602 		break;
1603 	}
1604 	(void) mutex_unlock(&ad_queue.spq_mtx);
1605 }
1606 
1607 /*
1608  * This is the publisher daemon thread.  While running, the thread waits
1609  * on a conditional variable until notified that a share needs to be
1610  * [un]published or that the thread should be terminated.
1611  *
1612  * Entries may remain in the outgoing queue if the Active Directory
1613  * service is inaccessible, in which case the thread wakes up every 60
1614  * seconds to retry.
1615  */
1616 /*ARGSUSED*/
1617 static void *
1618 smb_shr_publisher(void *arg)
1619 {
1620 	smb_ads_handle_t *ah;
1621 	smb_shr_pitem_t *shr;
1622 	list_t publist;
1623 	timestruc_t pubretry;
1624 	char hostname[MAXHOSTNAMELEN];
1625 
1626 	(void) mutex_lock(&ad_queue.spq_mtx);
1627 	if (ad_queue.spq_state != SMB_SHR_PQS_READY) {
1628 		(void) mutex_unlock(&ad_queue.spq_mtx);
1629 		return (NULL);
1630 	}
1631 	ad_queue.spq_state = SMB_SHR_PQS_PUBLISHING;
1632 	(void) mutex_unlock(&ad_queue.spq_mtx);
1633 
1634 	(void) smb_gethostname(hostname, MAXHOSTNAMELEN, 0);
1635 
1636 	list_create(&publist, sizeof (smb_shr_pitem_t),
1637 	    offsetof(smb_shr_pitem_t, spi_lnd));
1638 
1639 	for (;;) {
1640 		(void) mutex_lock(&ad_queue.spq_mtx);
1641 
1642 		while (list_is_empty(&ad_queue.spq_list) &&
1643 		    (ad_queue.spq_state == SMB_SHR_PQS_PUBLISHING)) {
1644 			if (list_is_empty(&publist)) {
1645 				(void) cond_wait(&ad_queue.spq_cv,
1646 				    &ad_queue.spq_mtx);
1647 			} else {
1648 				pubretry.tv_sec = 60;
1649 				pubretry.tv_nsec = 0;
1650 				(void) cond_reltimedwait(&ad_queue.spq_cv,
1651 				    &ad_queue.spq_mtx, &pubretry);
1652 				break;
1653 			}
1654 		}
1655 
1656 		if (ad_queue.spq_state != SMB_SHR_PQS_PUBLISHING) {
1657 			(void) mutex_unlock(&ad_queue.spq_mtx);
1658 			break;
1659 		}
1660 
1661 		/*
1662 		 * Transfer queued items to the local list so that
1663 		 * the mutex can be released.
1664 		 */
1665 		while ((shr = list_head(&ad_queue.spq_list)) != NULL) {
1666 			list_remove(&ad_queue.spq_list, shr);
1667 			list_insert_tail(&publist, shr);
1668 		}
1669 
1670 		(void) mutex_unlock(&ad_queue.spq_mtx);
1671 
1672 		if ((ah = smb_ads_open()) != NULL) {
1673 			smb_shr_publisher_send(ah, &publist, hostname);
1674 			smb_ads_close(ah);
1675 		}
1676 	}
1677 
1678 	(void) mutex_lock(&ad_queue.spq_mtx);
1679 	smb_shr_publisher_flush(&ad_queue.spq_list);
1680 	list_destroy(&ad_queue.spq_list);
1681 	ad_queue.spq_state = SMB_SHR_PQS_NOQUEUE;
1682 	(void) mutex_unlock(&ad_queue.spq_mtx);
1683 
1684 	smb_shr_publisher_flush(&publist);
1685 	list_destroy(&publist);
1686 	return (NULL);
1687 }
1688 
1689 /*
1690  * Remove items from the specified queue and [un]publish them.
1691  */
1692 static void
1693 smb_shr_publisher_send(smb_ads_handle_t *ah, list_t *publist, const char *host)
1694 {
1695 	smb_shr_pitem_t *shr;
1696 
1697 	while ((shr = list_head(publist)) != NULL) {
1698 		(void) mutex_lock(&ad_queue.spq_mtx);
1699 		if (ad_queue.spq_state != SMB_SHR_PQS_PUBLISHING) {
1700 			(void) mutex_unlock(&ad_queue.spq_mtx);
1701 			return;
1702 		}
1703 		(void) mutex_unlock(&ad_queue.spq_mtx);
1704 
1705 		list_remove(publist, shr);
1706 
1707 		if (shr->spi_op == SMB_SHR_PUBLISH)
1708 			(void) smb_ads_publish_share(ah, shr->spi_name,
1709 			    NULL, shr->spi_container, host);
1710 		else
1711 			(void) smb_ads_remove_share(ah, shr->spi_name,
1712 			    NULL, shr->spi_container, host);
1713 
1714 		free(shr);
1715 	}
1716 }
1717 
1718 /*
1719  * Flush all remaining items from the specified list/queue.
1720  */
1721 static void
1722 smb_shr_publisher_flush(list_t *lst)
1723 {
1724 	smb_shr_pitem_t *shr;
1725 
1726 	while ((shr = list_head(lst)) != NULL) {
1727 		list_remove(lst, shr);
1728 		free(shr);
1729 	}
1730 }
1731 
1732 /*
1733  * If the share path refers to a ZFS file system, add the
1734  * .zfs/shares/<share> object.
1735  */
1736 
1737 static void
1738 smb_shr_zfs_add(smb_share_t *si)
1739 {
1740 	libzfs_handle_t *libhd;
1741 	zfs_handle_t *zfshd;
1742 	int ret;
1743 	char dataset[MAXPATHLEN];
1744 
1745 	if (smb_getdataset(si->shr_path, dataset, MAXPATHLEN) != 0)
1746 		return;
1747 
1748 	if ((libhd = libzfs_init()) == NULL)
1749 		return;
1750 
1751 	if ((zfshd = zfs_open(libhd, dataset, ZFS_TYPE_FILESYSTEM)) == NULL) {
1752 		libzfs_fini(libhd);
1753 		return;
1754 	}
1755 
1756 	errno = 0;
1757 	ret = zfs_smb_acl_add(libhd, dataset, si->shr_path, si->shr_name);
1758 	if (ret != 0 && errno != EAGAIN && errno != EEXIST)
1759 		syslog(LOG_INFO, "share: failed to add ACL object: %s: %s\n",
1760 		    si->shr_name, strerror(errno));
1761 
1762 	zfs_close(zfshd);
1763 	libzfs_fini(libhd);
1764 }
1765 
1766 /*
1767  * If the share path refers to a ZFS file system, remove the
1768  * .zfs/shares/<share> object.
1769  */
1770 
1771 static void
1772 smb_shr_zfs_remove(smb_share_t *si)
1773 {
1774 	libzfs_handle_t *libhd;
1775 	zfs_handle_t *zfshd;
1776 	int ret;
1777 	char dataset[MAXPATHLEN];
1778 
1779 	if (smb_getdataset(si->shr_path, dataset, MAXPATHLEN) != 0)
1780 		return;
1781 
1782 	if ((libhd = libzfs_init()) == NULL)
1783 		return;
1784 
1785 	if ((zfshd = zfs_open(libhd, dataset, ZFS_TYPE_FILESYSTEM)) == NULL) {
1786 		libzfs_fini(libhd);
1787 		return;
1788 	}
1789 
1790 	errno = 0;
1791 	ret = zfs_smb_acl_remove(libhd, dataset, si->shr_path, si->shr_name);
1792 	if (ret != 0 && errno != EAGAIN)
1793 		syslog(LOG_INFO, "share: failed to remove ACL object: %s: %s\n",
1794 		    si->shr_name, strerror(errno));
1795 
1796 	zfs_close(zfshd);
1797 	libzfs_fini(libhd);
1798 }
1799 
1800 /*
1801  * If the share path refers to a ZFS file system, rename the
1802  * .zfs/shares/<share> object.
1803  */
1804 
1805 static void
1806 smb_shr_zfs_rename(smb_share_t *from, smb_share_t *to)
1807 {
1808 	libzfs_handle_t *libhd;
1809 	zfs_handle_t *zfshd;
1810 	int ret;
1811 	char dataset[MAXPATHLEN];
1812 
1813 	if (smb_getdataset(from->shr_path, dataset, MAXPATHLEN) != 0)
1814 		return;
1815 
1816 	if ((libhd = libzfs_init()) == NULL)
1817 		return;
1818 
1819 	if ((zfshd = zfs_open(libhd, dataset, ZFS_TYPE_FILESYSTEM)) == NULL) {
1820 		libzfs_fini(libhd);
1821 		return;
1822 	}
1823 
1824 	errno = 0;
1825 	ret = zfs_smb_acl_rename(libhd, dataset, from->shr_path,
1826 	    from->shr_name, to->shr_name);
1827 	if (ret != 0 && errno != EAGAIN)
1828 		syslog(LOG_INFO, "share: failed to rename ACL object: %s: %s\n",
1829 		    from->shr_name, strerror(errno));
1830 
1831 	zfs_close(zfshd);
1832 	libzfs_fini(libhd);
1833 }
1834