13db3f65cSamw /*
23db3f65cSamw * CDDL HEADER START
33db3f65cSamw *
43db3f65cSamw * The contents of this file are subject to the terms of the
53db3f65cSamw * Common Development and Distribution License (the "License").
63db3f65cSamw * You may not use this file except in compliance with the License.
73db3f65cSamw *
83db3f65cSamw * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
93db3f65cSamw * or http://www.opensolaris.org/os/licensing.
103db3f65cSamw * See the License for the specific language governing permissions
113db3f65cSamw * and limitations under the License.
123db3f65cSamw *
133db3f65cSamw * When distributing Covered Code, include this CDDL HEADER in each
143db3f65cSamw * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
153db3f65cSamw * If applicable, add the following below this CDDL HEADER, with the
163db3f65cSamw * fields enclosed by brackets "[]" replaced with your own identifying
173db3f65cSamw * information: Portions Copyright [yyyy] [name of copyright owner]
183db3f65cSamw *
193db3f65cSamw * CDDL HEADER END
20*9b241b4eSYuri Pankov *
21c5866007SKeyur Desai * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
22*9b241b4eSYuri Pankov * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
233db3f65cSamw */
243db3f65cSamw
253db3f65cSamw /*
26b89a8333Snatalie li - Sun Microsystems - Irvine United States * SMB/CIFS share cache implementation.
273db3f65cSamw */
283db3f65cSamw
293db3f65cSamw #include <errno.h>
303db3f65cSamw #include <synch.h>
313db3f65cSamw #include <stdlib.h>
323db3f65cSamw #include <strings.h>
333db3f65cSamw #include <syslog.h>
343db3f65cSamw #include <thread.h>
353db3f65cSamw #include <pthread.h>
36c8ec8eeaSjose borrego #include <assert.h>
37b89a8333Snatalie li - Sun Microsystems - Irvine United States #include <libshare.h>
38743a77edSAlan Wright #include <libzfs.h>
3929bd2886SAlan Wright #include <priv_utils.h>
4029bd2886SAlan Wright #include <sys/types.h>
4129bd2886SAlan Wright #include <sys/wait.h>
4229bd2886SAlan Wright #include <unistd.h>
4329bd2886SAlan Wright #include <pwd.h>
4429bd2886SAlan Wright #include <signal.h>
45c5866007SKeyur Desai #include <dirent.h>
46cb174861Sjoyce mcintosh #include <dlfcn.h>
473db3f65cSamw
483db3f65cSamw #include <smbsrv/libsmb.h>
493db3f65cSamw #include <smbsrv/libsmbns.h>
508d96b23eSAlan Wright #include <smbsrv/libmlsvc.h>
513db3f65cSamw #include <smbsrv/smb_share.h>
52bbf6f00cSJordan Brown #include <smbsrv/smb.h>
53e3f2c991SKeyur Desai #include <mlsvc.h>
549fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States #include <dfs.h>
553db3f65cSamw
56b89a8333Snatalie li - Sun Microsystems - Irvine United States #define SMB_SHR_ERROR_THRESHOLD 3
578d7e4166Sjose borrego #define SMB_SHR_CSC_BUFSZ 64
588d7e4166Sjose borrego
59c5866007SKeyur Desai typedef struct smb_transient {
60c5866007SKeyur Desai char *name;
61c5866007SKeyur Desai char *cmnt;
62c5866007SKeyur Desai char *path;
63c5866007SKeyur Desai char drive;
64c5866007SKeyur Desai boolean_t check;
65c5866007SKeyur Desai } smb_transient_t;
66c5866007SKeyur Desai
67c5866007SKeyur Desai static smb_transient_t tshare[] = {
68c5866007SKeyur Desai { "IPC$", "Remote IPC", NULL, '\0', B_FALSE },
69c5866007SKeyur Desai { "c$", "Default Share", SMB_CVOL, 'C', B_FALSE },
70c5866007SKeyur Desai { "vss$", "VSS", SMB_VSS, 'V', B_TRUE }
71c5866007SKeyur Desai };
72c5866007SKeyur Desai
7329bd2886SAlan Wright static struct {
7429bd2886SAlan Wright char *value;
7529bd2886SAlan Wright uint32_t flag;
7629bd2886SAlan Wright } cscopt[] = {
7729bd2886SAlan Wright { "disabled", SMB_SHRF_CSC_DISABLED },
7829bd2886SAlan Wright { "manual", SMB_SHRF_CSC_MANUAL },
7929bd2886SAlan Wright { "auto", SMB_SHRF_CSC_AUTO },
8029bd2886SAlan Wright { "vdo", SMB_SHRF_CSC_VDO }
8129bd2886SAlan Wright };
8229bd2886SAlan Wright
83c8ec8eeaSjose borrego /*
84c8ec8eeaSjose borrego * Cache functions and vars
85c8ec8eeaSjose borrego */
86b89a8333Snatalie li - Sun Microsystems - Irvine United States #define SMB_SHR_HTAB_SZ 1024
873db3f65cSamw
88b89a8333Snatalie li - Sun Microsystems - Irvine United States /*
89b89a8333Snatalie li - Sun Microsystems - Irvine United States * Cache handle
90b89a8333Snatalie li - Sun Microsystems - Irvine United States *
91b89a8333Snatalie li - Sun Microsystems - Irvine United States * Shares cache is a hash table.
92b89a8333Snatalie li - Sun Microsystems - Irvine United States *
93b89a8333Snatalie li - Sun Microsystems - Irvine United States * sc_cache pointer to hash table handle
94b89a8333Snatalie li - Sun Microsystems - Irvine United States * sc_cache_lck synchronize cache read/write accesses
95b89a8333Snatalie li - Sun Microsystems - Irvine United States * sc_state cache state machine values
96b89a8333Snatalie li - Sun Microsystems - Irvine United States * sc_nops number of inflight/pending cache operations
97b89a8333Snatalie li - Sun Microsystems - Irvine United States * sc_mtx protects handle fields
98b89a8333Snatalie li - Sun Microsystems - Irvine United States */
99b89a8333Snatalie li - Sun Microsystems - Irvine United States typedef struct smb_shr_cache {
100b89a8333Snatalie li - Sun Microsystems - Irvine United States HT_HANDLE *sc_cache;
101b89a8333Snatalie li - Sun Microsystems - Irvine United States rwlock_t sc_cache_lck;
102b89a8333Snatalie li - Sun Microsystems - Irvine United States mutex_t sc_mtx;
103b89a8333Snatalie li - Sun Microsystems - Irvine United States cond_t sc_cv;
104b89a8333Snatalie li - Sun Microsystems - Irvine United States uint32_t sc_state;
105b89a8333Snatalie li - Sun Microsystems - Irvine United States uint32_t sc_nops;
106b89a8333Snatalie li - Sun Microsystems - Irvine United States } smb_shr_cache_t;
107b89a8333Snatalie li - Sun Microsystems - Irvine United States
108b89a8333Snatalie li - Sun Microsystems - Irvine United States /*
109b89a8333Snatalie li - Sun Microsystems - Irvine United States * Cache states
110b89a8333Snatalie li - Sun Microsystems - Irvine United States */
111b89a8333Snatalie li - Sun Microsystems - Irvine United States #define SMB_SHR_CACHE_STATE_NONE 0
112b89a8333Snatalie li - Sun Microsystems - Irvine United States #define SMB_SHR_CACHE_STATE_CREATED 1
113b89a8333Snatalie li - Sun Microsystems - Irvine United States #define SMB_SHR_CACHE_STATE_DESTROYING 2
114b89a8333Snatalie li - Sun Microsystems - Irvine United States
115b89a8333Snatalie li - Sun Microsystems - Irvine United States /*
116b89a8333Snatalie li - Sun Microsystems - Irvine United States * Cache lock modes
117b89a8333Snatalie li - Sun Microsystems - Irvine United States */
118b89a8333Snatalie li - Sun Microsystems - Irvine United States #define SMB_SHR_CACHE_RDLOCK 0
119b89a8333Snatalie li - Sun Microsystems - Irvine United States #define SMB_SHR_CACHE_WRLOCK 1
120b89a8333Snatalie li - Sun Microsystems - Irvine United States
121b89a8333Snatalie li - Sun Microsystems - Irvine United States static smb_shr_cache_t smb_shr_cache;
1223db3f65cSamw
1233db3f65cSamw static uint32_t smb_shr_cache_create(void);
124c8ec8eeaSjose borrego static void smb_shr_cache_destroy(void);
125b89a8333Snatalie li - Sun Microsystems - Irvine United States static uint32_t smb_shr_cache_lock(int);
126b89a8333Snatalie li - Sun Microsystems - Irvine United States static void smb_shr_cache_unlock(void);
127b89a8333Snatalie li - Sun Microsystems - Irvine United States static int smb_shr_cache_count(void);
128b89a8333Snatalie li - Sun Microsystems - Irvine United States static smb_share_t *smb_shr_cache_iterate(smb_shriter_t *);
129b89a8333Snatalie li - Sun Microsystems - Irvine United States
130b89a8333Snatalie li - Sun Microsystems - Irvine United States static smb_share_t *smb_shr_cache_findent(char *);
131c8ec8eeaSjose borrego static uint32_t smb_shr_cache_addent(smb_share_t *);
132c8ec8eeaSjose borrego static void smb_shr_cache_delent(char *);
133c8ec8eeaSjose borrego static void smb_shr_cache_freent(HT_ITEM *);
1343db3f65cSamw
135c5866007SKeyur Desai static boolean_t smb_shr_is_empty(const char *);
136c5866007SKeyur Desai static boolean_t smb_shr_is_dot_or_dotdot(const char *);
137c5866007SKeyur Desai
1383db3f65cSamw /*
139c8ec8eeaSjose borrego * sharemgr functions
140c8ec8eeaSjose borrego */
141b89a8333Snatalie li - Sun Microsystems - Irvine United States static void smb_shr_sa_loadgrp(sa_group_t);
142b89a8333Snatalie li - Sun Microsystems - Irvine United States static uint32_t smb_shr_sa_load(sa_share_t, sa_resource_t);
1438d7e4166Sjose borrego static uint32_t smb_shr_sa_loadbyname(char *);
144b89a8333Snatalie li - Sun Microsystems - Irvine United States static uint32_t smb_shr_sa_get(sa_share_t, sa_resource_t, smb_share_t *);
145c8ec8eeaSjose borrego
146c8ec8eeaSjose borrego /*
147743a77edSAlan Wright * .ZFS management functions
148743a77edSAlan Wright */
149743a77edSAlan Wright static void smb_shr_zfs_add(smb_share_t *);
150743a77edSAlan Wright static void smb_shr_zfs_remove(smb_share_t *);
151743a77edSAlan Wright static void smb_shr_zfs_rename(smb_share_t *, smb_share_t *);
152743a77edSAlan Wright
153743a77edSAlan Wright /*
154c8ec8eeaSjose borrego * share publishing
155c8ec8eeaSjose borrego */
156c8ec8eeaSjose borrego #define SMB_SHR_PUBLISH 0
157c8ec8eeaSjose borrego #define SMB_SHR_UNPUBLISH 1
158c8ec8eeaSjose borrego
159c8ec8eeaSjose borrego typedef struct smb_shr_pitem {
160c8ec8eeaSjose borrego list_node_t spi_lnd;
161c8ec8eeaSjose borrego char spi_name[MAXNAMELEN];
162c8ec8eeaSjose borrego char spi_container[MAXPATHLEN];
163c8ec8eeaSjose borrego char spi_op;
164c8ec8eeaSjose borrego } smb_shr_pitem_t;
165c8ec8eeaSjose borrego
166c8ec8eeaSjose borrego /*
167c8ec8eeaSjose borrego * publish queue states
168c8ec8eeaSjose borrego */
169c8ec8eeaSjose borrego #define SMB_SHR_PQS_NOQUEUE 0
170c8ec8eeaSjose borrego #define SMB_SHR_PQS_READY 1 /* the queue is ready */
171c8ec8eeaSjose borrego #define SMB_SHR_PQS_PUBLISHING 2 /* publisher thread is running */
172c8ec8eeaSjose borrego #define SMB_SHR_PQS_STOPPING 3
173c8ec8eeaSjose borrego
174c8ec8eeaSjose borrego /*
175c8ec8eeaSjose borrego * share publishing queue
176c8ec8eeaSjose borrego */
177c8ec8eeaSjose borrego typedef struct smb_shr_pqueue {
178c8ec8eeaSjose borrego list_t spq_list;
179c8ec8eeaSjose borrego mutex_t spq_mtx;
180c8ec8eeaSjose borrego cond_t spq_cv;
181c8ec8eeaSjose borrego uint32_t spq_state;
182c8ec8eeaSjose borrego } smb_shr_pqueue_t;
183c8ec8eeaSjose borrego
184c8ec8eeaSjose borrego static smb_shr_pqueue_t ad_queue;
185c8ec8eeaSjose borrego
186c8ec8eeaSjose borrego static int smb_shr_publisher_start(void);
187c8ec8eeaSjose borrego static void smb_shr_publisher_stop(void);
188c8ec8eeaSjose borrego static void smb_shr_publisher_send(smb_ads_handle_t *, list_t *, const char *);
189b89a8333Snatalie li - Sun Microsystems - Irvine United States static void smb_shr_publisher_queue(const char *, const char *, char);
190c8ec8eeaSjose borrego static void *smb_shr_publisher(void *);
191b89a8333Snatalie li - Sun Microsystems - Irvine United States static void smb_shr_publisher_flush(list_t *);
192b89a8333Snatalie li - Sun Microsystems - Irvine United States static void smb_shr_publish(const char *, const char *);
193b89a8333Snatalie li - Sun Microsystems - Irvine United States static void smb_shr_unpublish(const char *, const char *);
194c8ec8eeaSjose borrego
195c8ec8eeaSjose borrego /*
196b89a8333Snatalie li - Sun Microsystems - Irvine United States * Utility/helper functions
197b89a8333Snatalie li - Sun Microsystems - Irvine United States */
1988d7e4166Sjose borrego static uint32_t smb_shr_lookup(char *, smb_share_t *);
1999fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States static uint32_t smb_shr_add_transient(char *, char *, char *);
20029bd2886SAlan Wright static int smb_shr_enable_all_privs(void);
201148c5f43SAlan Wright static int smb_shr_expand_subs(char **, smb_share_t *, smb_shr_execinfo_t *);
20229bd2886SAlan Wright static char **smb_shr_tokenize_cmd(char *);
20329bd2886SAlan Wright static void smb_shr_sig_abnormal_term(int);
20429bd2886SAlan Wright static void smb_shr_sig_child(int);
205148c5f43SAlan Wright static int smb_shr_encode(smb_share_t *, nvlist_t **);
20689dc44ceSjose borrego
20789dc44ceSjose borrego /*
20889dc44ceSjose borrego * libshare handle and synchronization
20989dc44ceSjose borrego */
21089dc44ceSjose borrego typedef struct smb_sa_handle {
21189dc44ceSjose borrego sa_handle_t sa_handle;
21289dc44ceSjose borrego mutex_t sa_mtx;
21389dc44ceSjose borrego boolean_t sa_in_service;
21489dc44ceSjose borrego } smb_sa_handle_t;
21589dc44ceSjose borrego
21689dc44ceSjose borrego static smb_sa_handle_t smb_sa_handle;
21789dc44ceSjose borrego
21829bd2886SAlan Wright static char smb_shr_exec_map[MAXPATHLEN];
21929bd2886SAlan Wright static char smb_shr_exec_unmap[MAXPATHLEN];
22029bd2886SAlan Wright static mutex_t smb_shr_exec_mtx;
22129bd2886SAlan Wright
222b89a8333Snatalie li - Sun Microsystems - Irvine United States /*
223e3f2c991SKeyur Desai * Semaphore held during temporary, process-wide changes
224e3f2c991SKeyur Desai * such as process privileges. It is a seamaphore and
225e3f2c991SKeyur Desai * not a mutex so a child of fork can reset it.
226e3f2c991SKeyur Desai */
227e3f2c991SKeyur Desai static sema_t smb_proc_sem = DEFAULTSEMA;
228e3f2c991SKeyur Desai
229e3f2c991SKeyur Desai /*
2308d7e4166Sjose borrego * Creates and initializes the cache and starts the publisher
2318d7e4166Sjose borrego * thread.
2323db3f65cSamw */
2333db3f65cSamw int
smb_shr_start(void)2343db3f65cSamw smb_shr_start(void)
2353db3f65cSamw {
236c5866007SKeyur Desai smb_transient_t *ts;
237c5866007SKeyur Desai uint32_t nerr;
2389fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States int i;
2399fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States
24089dc44ceSjose borrego (void) mutex_lock(&smb_sa_handle.sa_mtx);
24189dc44ceSjose borrego smb_sa_handle.sa_in_service = B_TRUE;
24289dc44ceSjose borrego (void) mutex_unlock(&smb_sa_handle.sa_mtx);
24389dc44ceSjose borrego
244b89a8333Snatalie li - Sun Microsystems - Irvine United States if (smb_shr_cache_create() != NERR_Success)
245b89a8333Snatalie li - Sun Microsystems - Irvine United States return (ENOMEM);
246b89a8333Snatalie li - Sun Microsystems - Irvine United States
2479fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States for (i = 0; i < sizeof (tshare)/sizeof (tshare[0]); ++i) {
248c5866007SKeyur Desai ts = &tshare[i];
249c5866007SKeyur Desai
250c5866007SKeyur Desai if (ts->check && smb_shr_is_empty(ts->path))
251c5866007SKeyur Desai continue;
252c5866007SKeyur Desai
253c5866007SKeyur Desai nerr = smb_shr_add_transient(ts->name, ts->cmnt, ts->path);
254c5866007SKeyur Desai if (nerr != NERR_Success)
255b89a8333Snatalie li - Sun Microsystems - Irvine United States return (ENOMEM);
2569fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States }
257b89a8333Snatalie li - Sun Microsystems - Irvine United States
2588d7e4166Sjose borrego return (smb_shr_publisher_start());
2593db3f65cSamw }
2603db3f65cSamw
2613db3f65cSamw void
smb_shr_stop(void)2623db3f65cSamw smb_shr_stop(void)
2633db3f65cSamw {
264c8ec8eeaSjose borrego smb_shr_cache_destroy();
265c8ec8eeaSjose borrego smb_shr_publisher_stop();
26689dc44ceSjose borrego
26789dc44ceSjose borrego (void) mutex_lock(&smb_sa_handle.sa_mtx);
26889dc44ceSjose borrego smb_sa_handle.sa_in_service = B_FALSE;
26989dc44ceSjose borrego
27089dc44ceSjose borrego if (smb_sa_handle.sa_handle != NULL) {
27189dc44ceSjose borrego sa_fini(smb_sa_handle.sa_handle);
27289dc44ceSjose borrego smb_sa_handle.sa_handle = NULL;
27389dc44ceSjose borrego }
27489dc44ceSjose borrego
27589dc44ceSjose borrego (void) mutex_unlock(&smb_sa_handle.sa_mtx);
27689dc44ceSjose borrego }
27789dc44ceSjose borrego
27889dc44ceSjose borrego /*
27989dc44ceSjose borrego * Get a handle and exclusive access to the libshare API.
28089dc44ceSjose borrego */
28189dc44ceSjose borrego sa_handle_t
smb_shr_sa_enter(void)28289dc44ceSjose borrego smb_shr_sa_enter(void)
28389dc44ceSjose borrego {
28489dc44ceSjose borrego (void) mutex_lock(&smb_sa_handle.sa_mtx);
28589dc44ceSjose borrego if (!smb_sa_handle.sa_in_service) {
28689dc44ceSjose borrego (void) mutex_unlock(&smb_sa_handle.sa_mtx);
28789dc44ceSjose borrego return (NULL);
28889dc44ceSjose borrego }
28989dc44ceSjose borrego
29089dc44ceSjose borrego if (smb_sa_handle.sa_handle == NULL) {
29189dc44ceSjose borrego smb_sa_handle.sa_handle = sa_init(SA_INIT_SHARE_API);
29289dc44ceSjose borrego if (smb_sa_handle.sa_handle == NULL) {
29389dc44ceSjose borrego syslog(LOG_ERR, "share: failed to get libshare handle");
29489dc44ceSjose borrego (void) mutex_unlock(&smb_sa_handle.sa_mtx);
29589dc44ceSjose borrego return (NULL);
29689dc44ceSjose borrego }
29789dc44ceSjose borrego }
29889dc44ceSjose borrego
29989dc44ceSjose borrego return (smb_sa_handle.sa_handle);
30089dc44ceSjose borrego }
30189dc44ceSjose borrego
30289dc44ceSjose borrego /*
30389dc44ceSjose borrego * Release exclusive access to the libshare API.
30489dc44ceSjose borrego */
30589dc44ceSjose borrego void
smb_shr_sa_exit(void)30689dc44ceSjose borrego smb_shr_sa_exit(void)
30789dc44ceSjose borrego {
30889dc44ceSjose borrego (void) mutex_unlock(&smb_sa_handle.sa_mtx);
3093db3f65cSamw }
3103db3f65cSamw
3113db3f65cSamw /*
312c8ec8eeaSjose borrego * Return the total number of shares
3133db3f65cSamw */
3143db3f65cSamw int
smb_shr_count(void)3153db3f65cSamw smb_shr_count(void)
3163db3f65cSamw {
317b89a8333Snatalie li - Sun Microsystems - Irvine United States int n_shares = 0;
3183db3f65cSamw
319b89a8333Snatalie li - Sun Microsystems - Irvine United States if (smb_shr_cache_lock(SMB_SHR_CACHE_RDLOCK) == NERR_Success) {
320b89a8333Snatalie li - Sun Microsystems - Irvine United States n_shares = smb_shr_cache_count();
321b89a8333Snatalie li - Sun Microsystems - Irvine United States smb_shr_cache_unlock();
322b89a8333Snatalie li - Sun Microsystems - Irvine United States }
3233db3f65cSamw
3243db3f65cSamw return (n_shares);
3253db3f65cSamw }
3263db3f65cSamw
3273db3f65cSamw /*
3283db3f65cSamw * smb_shr_iterinit
3293db3f65cSamw *
330c8ec8eeaSjose borrego * Initialize given iterator for traversing hash table.
3313db3f65cSamw */
3323db3f65cSamw void
smb_shr_iterinit(smb_shriter_t * shi)333c8ec8eeaSjose borrego smb_shr_iterinit(smb_shriter_t *shi)
3343db3f65cSamw {
3353db3f65cSamw bzero(shi, sizeof (smb_shriter_t));
336c8ec8eeaSjose borrego shi->si_first = B_TRUE;
3373db3f65cSamw }
3383db3f65cSamw
3393db3f65cSamw /*
3403db3f65cSamw * smb_shr_iterate
3413db3f65cSamw *
3423db3f65cSamw * Iterate on the shares in the hash table. The iterator must be initialized
3433db3f65cSamw * before the first iteration. On subsequent calls, the iterator must be
3443db3f65cSamw * passed unchanged.
3453db3f65cSamw *
3463db3f65cSamw * Returns NULL on failure or when all shares are visited, otherwise
3473db3f65cSamw * returns information of visited share.
3483db3f65cSamw */
3493db3f65cSamw smb_share_t *
smb_shr_iterate(smb_shriter_t * shi)3503db3f65cSamw smb_shr_iterate(smb_shriter_t *shi)
3513db3f65cSamw {
352c8ec8eeaSjose borrego smb_share_t *share = NULL;
353b89a8333Snatalie li - Sun Microsystems - Irvine United States smb_share_t *cached_si;
3543db3f65cSamw
355b89a8333Snatalie li - Sun Microsystems - Irvine United States if (shi == NULL)
3563db3f65cSamw return (NULL);
3573db3f65cSamw
358b89a8333Snatalie li - Sun Microsystems - Irvine United States if (smb_shr_cache_lock(SMB_SHR_CACHE_RDLOCK) == NERR_Success) {
359b89a8333Snatalie li - Sun Microsystems - Irvine United States if ((cached_si = smb_shr_cache_iterate(shi)) != NULL) {
360c8ec8eeaSjose borrego share = &shi->si_share;
361b89a8333Snatalie li - Sun Microsystems - Irvine United States bcopy(cached_si, share, sizeof (smb_share_t));
3623db3f65cSamw }
363b89a8333Snatalie li - Sun Microsystems - Irvine United States smb_shr_cache_unlock();
364b89a8333Snatalie li - Sun Microsystems - Irvine United States }
3653db3f65cSamw
366c8ec8eeaSjose borrego return (share);
3673db3f65cSamw }
3683db3f65cSamw
3693db3f65cSamw /*
370b89a8333Snatalie li - Sun Microsystems - Irvine United States * Adds the given share to cache, publishes the share in ADS
371b89a8333Snatalie li - Sun Microsystems - Irvine United States * if it has an AD container, calls kernel to take a hold on
372b89a8333Snatalie li - Sun Microsystems - Irvine United States * the shared file system. If it can't take a hold on the
373b89a8333Snatalie li - Sun Microsystems - Irvine United States * shared file system, it's either because shared directory
374b89a8333Snatalie li - Sun Microsystems - Irvine United States * does not exist or some other error has occurred, in any
375b89a8333Snatalie li - Sun Microsystems - Irvine United States * case the share is removed from the cache.
3763db3f65cSamw *
377b89a8333Snatalie li - Sun Microsystems - Irvine United States * If the specified share is an autohome share which already
378b89a8333Snatalie li - Sun Microsystems - Irvine United States * exists in the cache, just increments the reference count.
3793db3f65cSamw */
3803db3f65cSamw uint32_t
smb_shr_add(smb_share_t * si)381b89a8333Snatalie li - Sun Microsystems - Irvine United States smb_shr_add(smb_share_t *si)
3823db3f65cSamw {
383b89a8333Snatalie li - Sun Microsystems - Irvine United States smb_share_t *cached_si;
384148c5f43SAlan Wright nvlist_t *shrlist;
385b89a8333Snatalie li - Sun Microsystems - Irvine United States uint32_t status;
386c8ec8eeaSjose borrego int rc;
3873db3f65cSamw
388c8ec8eeaSjose borrego assert(si != NULL);
3893db3f65cSamw
390fe1c642dSBill Krier if (smb_name_validate_share(si->shr_name) != ERROR_SUCCESS)
391c8ec8eeaSjose borrego return (ERROR_INVALID_NAME);
3923db3f65cSamw
393b89a8333Snatalie li - Sun Microsystems - Irvine United States if (smb_shr_cache_lock(SMB_SHR_CACHE_WRLOCK) != NERR_Success)
394b89a8333Snatalie li - Sun Microsystems - Irvine United States return (NERR_InternalError);
395c8ec8eeaSjose borrego
396b89a8333Snatalie li - Sun Microsystems - Irvine United States cached_si = smb_shr_cache_findent(si->shr_name);
397b89a8333Snatalie li - Sun Microsystems - Irvine United States if (cached_si) {
398b89a8333Snatalie li - Sun Microsystems - Irvine United States if (si->shr_flags & SMB_SHRF_AUTOHOME) {
399b89a8333Snatalie li - Sun Microsystems - Irvine United States cached_si->shr_refcnt++;
400b89a8333Snatalie li - Sun Microsystems - Irvine United States status = NERR_Success;
401b89a8333Snatalie li - Sun Microsystems - Irvine United States } else {
402b89a8333Snatalie li - Sun Microsystems - Irvine United States status = NERR_DuplicateShare;
403b89a8333Snatalie li - Sun Microsystems - Irvine United States }
404b89a8333Snatalie li - Sun Microsystems - Irvine United States smb_shr_cache_unlock();
405c8ec8eeaSjose borrego return (status);
406c8ec8eeaSjose borrego }
407b89a8333Snatalie li - Sun Microsystems - Irvine United States
408b89a8333Snatalie li - Sun Microsystems - Irvine United States if ((status = smb_shr_cache_addent(si)) != NERR_Success) {
409b89a8333Snatalie li - Sun Microsystems - Irvine United States smb_shr_cache_unlock();
410b89a8333Snatalie li - Sun Microsystems - Irvine United States return (status);
411c8ec8eeaSjose borrego }
412c8ec8eeaSjose borrego
413b89a8333Snatalie li - Sun Microsystems - Irvine United States /* don't hold the lock across door call */
414b89a8333Snatalie li - Sun Microsystems - Irvine United States smb_shr_cache_unlock();
415b89a8333Snatalie li - Sun Microsystems - Irvine United States
416148c5f43SAlan Wright if ((rc = smb_shr_encode(si, &shrlist)) == 0) {
417148c5f43SAlan Wright /* send the share to kernel */
418148c5f43SAlan Wright rc = smb_kmod_share(shrlist);
419148c5f43SAlan Wright nvlist_free(shrlist);
420c8ec8eeaSjose borrego
421c8ec8eeaSjose borrego if (rc == 0) {
422b89a8333Snatalie li - Sun Microsystems - Irvine United States smb_shr_publish(si->shr_name, si->shr_container);
423743a77edSAlan Wright
424743a77edSAlan Wright /* If path is ZFS, add the .zfs/shares/<share> entry. */
425743a77edSAlan Wright smb_shr_zfs_add(si);
426743a77edSAlan Wright
427cb174861Sjoyce mcintosh if ((si->shr_flags & SMB_SHRF_DFSROOT) != 0)
428cb174861Sjoyce mcintosh dfs_namespace_load(si->shr_name);
429cb174861Sjoyce mcintosh
430b89a8333Snatalie li - Sun Microsystems - Irvine United States return (NERR_Success);
431c8ec8eeaSjose borrego }
432148c5f43SAlan Wright }
433c8ec8eeaSjose borrego
434b89a8333Snatalie li - Sun Microsystems - Irvine United States if (smb_shr_cache_lock(SMB_SHR_CACHE_WRLOCK) == NERR_Success) {
435c8ec8eeaSjose borrego smb_shr_cache_delent(si->shr_name);
436b89a8333Snatalie li - Sun Microsystems - Irvine United States smb_shr_cache_unlock();
437b89a8333Snatalie li - Sun Microsystems - Irvine United States }
438c8ec8eeaSjose borrego
439c8ec8eeaSjose borrego /*
440c8ec8eeaSjose borrego * rc == ENOENT means the shared directory doesn't exist
441c8ec8eeaSjose borrego */
442c8ec8eeaSjose borrego return ((rc == ENOENT) ? NERR_UnknownDevDir : NERR_InternalError);
4433db3f65cSamw }
4443db3f65cSamw
4453db3f65cSamw /*
446b89a8333Snatalie li - Sun Microsystems - Irvine United States * Removes the specified share from cache, removes it from AD
447b89a8333Snatalie li - Sun Microsystems - Irvine United States * if it has an AD container, and calls the kernel to release
448b89a8333Snatalie li - Sun Microsystems - Irvine United States * the hold on the shared file system.
4493db3f65cSamw *
450b89a8333Snatalie li - Sun Microsystems - Irvine United States * If this is an autohome share then decrement the reference
451b89a8333Snatalie li - Sun Microsystems - Irvine United States * count. If it reaches 0 then it proceeds with removing steps.
4523db3f65cSamw */
4533db3f65cSamw uint32_t
smb_shr_remove(char * sharename)454b89a8333Snatalie li - Sun Microsystems - Irvine United States smb_shr_remove(char *sharename)
4553db3f65cSamw {
456b89a8333Snatalie li - Sun Microsystems - Irvine United States smb_share_t *si;
457b89a8333Snatalie li - Sun Microsystems - Irvine United States char container[MAXPATHLEN];
4589fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States boolean_t dfsroot;
459148c5f43SAlan Wright nvlist_t *shrlist;
4603db3f65cSamw
461c8ec8eeaSjose borrego assert(sharename != NULL);
4623db3f65cSamw
463fe1c642dSBill Krier if (smb_name_validate_share(sharename) != ERROR_SUCCESS)
464b89a8333Snatalie li - Sun Microsystems - Irvine United States return (ERROR_INVALID_NAME);
4653db3f65cSamw
466b89a8333Snatalie li - Sun Microsystems - Irvine United States if (smb_shr_cache_lock(SMB_SHR_CACHE_WRLOCK) != NERR_Success)
4673db3f65cSamw return (NERR_InternalError);
468b89a8333Snatalie li - Sun Microsystems - Irvine United States
469b89a8333Snatalie li - Sun Microsystems - Irvine United States if ((si = smb_shr_cache_findent(sharename)) == NULL) {
470b89a8333Snatalie li - Sun Microsystems - Irvine United States smb_shr_cache_unlock();
471b89a8333Snatalie li - Sun Microsystems - Irvine United States return (NERR_NetNameNotFound);
4723db3f65cSamw }
4733db3f65cSamw
474b89a8333Snatalie li - Sun Microsystems - Irvine United States if (si->shr_type & STYPE_IPC) {
475b89a8333Snatalie li - Sun Microsystems - Irvine United States /* IPC$ share cannot be removed */
476b89a8333Snatalie li - Sun Microsystems - Irvine United States smb_shr_cache_unlock();
477b89a8333Snatalie li - Sun Microsystems - Irvine United States return (ERROR_ACCESS_DENIED);
478b89a8333Snatalie li - Sun Microsystems - Irvine United States }
479b89a8333Snatalie li - Sun Microsystems - Irvine United States
480b89a8333Snatalie li - Sun Microsystems - Irvine United States if (si->shr_flags & SMB_SHRF_AUTOHOME) {
481b89a8333Snatalie li - Sun Microsystems - Irvine United States if ((--si->shr_refcnt) > 0) {
482b89a8333Snatalie li - Sun Microsystems - Irvine United States smb_shr_cache_unlock();
483b89a8333Snatalie li - Sun Microsystems - Irvine United States return (NERR_Success);
484b89a8333Snatalie li - Sun Microsystems - Irvine United States }
485b89a8333Snatalie li - Sun Microsystems - Irvine United States }
486b89a8333Snatalie li - Sun Microsystems - Irvine United States
487743a77edSAlan Wright /*
488743a77edSAlan Wright * If path is ZFS, remove the .zfs/shares/<share> entry. Need
489743a77edSAlan Wright * to remove before cleanup of cache occurs.
490743a77edSAlan Wright */
491743a77edSAlan Wright smb_shr_zfs_remove(si);
492148c5f43SAlan Wright (void) smb_shr_encode(si, &shrlist);
493743a77edSAlan Wright
494b89a8333Snatalie li - Sun Microsystems - Irvine United States (void) strlcpy(container, si->shr_container, sizeof (container));
4959fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States dfsroot = ((si->shr_flags & SMB_SHRF_DFSROOT) != 0);
496b89a8333Snatalie li - Sun Microsystems - Irvine United States smb_shr_cache_delent(sharename);
497b89a8333Snatalie li - Sun Microsystems - Irvine United States smb_shr_cache_unlock();
498b89a8333Snatalie li - Sun Microsystems - Irvine United States
499b89a8333Snatalie li - Sun Microsystems - Irvine United States smb_shr_unpublish(sharename, container);
500b89a8333Snatalie li - Sun Microsystems - Irvine United States
501b89a8333Snatalie li - Sun Microsystems - Irvine United States /* call kernel to release the hold on the shared file system */
502148c5f43SAlan Wright if (shrlist != NULL) {
503148c5f43SAlan Wright (void) smb_kmod_unshare(shrlist);
504148c5f43SAlan Wright nvlist_free(shrlist);
505148c5f43SAlan Wright }
5063db3f65cSamw
5079fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States if (dfsroot)
5089fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States dfs_namespace_unload(sharename);
5099fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States
5103db3f65cSamw return (NERR_Success);
5113db3f65cSamw }
5123db3f65cSamw
5133db3f65cSamw /*
5143db3f65cSamw * Rename a share. Check that the current name exists and the new name
5153db3f65cSamw * doesn't exist. The rename is performed by deleting the current share
5163db3f65cSamw * definition and creating a new share with the new name.
5173db3f65cSamw */
5183db3f65cSamw uint32_t
smb_shr_rename(char * from_name,char * to_name)519c8ec8eeaSjose borrego smb_shr_rename(char *from_name, char *to_name)
5203db3f65cSamw {
521b89a8333Snatalie li - Sun Microsystems - Irvine United States smb_share_t *from_si;
522b89a8333Snatalie li - Sun Microsystems - Irvine United States smb_share_t to_si;
523c8ec8eeaSjose borrego uint32_t status;
524148c5f43SAlan Wright nvlist_t *shrlist;
5253db3f65cSamw
526c8ec8eeaSjose borrego assert((from_name != NULL) && (to_name != NULL));
5273db3f65cSamw
528fe1c642dSBill Krier if (smb_name_validate_share(from_name) != ERROR_SUCCESS ||
529fe1c642dSBill Krier smb_name_validate_share(to_name) != ERROR_SUCCESS)
530c8ec8eeaSjose borrego return (ERROR_INVALID_NAME);
5313db3f65cSamw
532b89a8333Snatalie li - Sun Microsystems - Irvine United States if (smb_shr_cache_lock(SMB_SHR_CACHE_WRLOCK) != NERR_Success)
533b89a8333Snatalie li - Sun Microsystems - Irvine United States return (NERR_InternalError);
534b89a8333Snatalie li - Sun Microsystems - Irvine United States
535b89a8333Snatalie li - Sun Microsystems - Irvine United States if ((from_si = smb_shr_cache_findent(from_name)) == NULL) {
536b89a8333Snatalie li - Sun Microsystems - Irvine United States smb_shr_cache_unlock();
5373db3f65cSamw return (NERR_NetNameNotFound);
538b89a8333Snatalie li - Sun Microsystems - Irvine United States }
5393db3f65cSamw
540b89a8333Snatalie li - Sun Microsystems - Irvine United States if (from_si->shr_type & STYPE_IPC) {
541b89a8333Snatalie li - Sun Microsystems - Irvine United States /* IPC$ share cannot be renamed */
542b89a8333Snatalie li - Sun Microsystems - Irvine United States smb_shr_cache_unlock();
543c8ec8eeaSjose borrego return (ERROR_ACCESS_DENIED);
544b89a8333Snatalie li - Sun Microsystems - Irvine United States }
5453db3f65cSamw
546b89a8333Snatalie li - Sun Microsystems - Irvine United States if (smb_shr_cache_findent(to_name) != NULL) {
547b89a8333Snatalie li - Sun Microsystems - Irvine United States smb_shr_cache_unlock();
548b89a8333Snatalie li - Sun Microsystems - Irvine United States return (NERR_DuplicateShare);
549b89a8333Snatalie li - Sun Microsystems - Irvine United States }
550b89a8333Snatalie li - Sun Microsystems - Irvine United States
551b89a8333Snatalie li - Sun Microsystems - Irvine United States bcopy(from_si, &to_si, sizeof (smb_share_t));
552b89a8333Snatalie li - Sun Microsystems - Irvine United States (void) strlcpy(to_si.shr_name, to_name, sizeof (to_si.shr_name));
553b89a8333Snatalie li - Sun Microsystems - Irvine United States
554cb174861Sjoyce mcintosh
555743a77edSAlan Wright /* If path is ZFS, rename the .zfs/shares/<share> entry. */
556743a77edSAlan Wright smb_shr_zfs_rename(from_si, &to_si);
557743a77edSAlan Wright
558b89a8333Snatalie li - Sun Microsystems - Irvine United States if ((status = smb_shr_cache_addent(&to_si)) != NERR_Success) {
559b89a8333Snatalie li - Sun Microsystems - Irvine United States smb_shr_cache_unlock();
560c8ec8eeaSjose borrego return (status);
561b89a8333Snatalie li - Sun Microsystems - Irvine United States }
562c8ec8eeaSjose borrego
563c8ec8eeaSjose borrego smb_shr_cache_delent(from_name);
564b89a8333Snatalie li - Sun Microsystems - Irvine United States smb_shr_cache_unlock();
565b89a8333Snatalie li - Sun Microsystems - Irvine United States
566148c5f43SAlan Wright if (smb_shr_encode(from_si, &shrlist) == 0) {
567148c5f43SAlan Wright (void) smb_kmod_unshare(shrlist);
568148c5f43SAlan Wright nvlist_free(shrlist);
569148c5f43SAlan Wright
570148c5f43SAlan Wright if (smb_shr_encode(&to_si, &shrlist) == 0) {
571148c5f43SAlan Wright (void) smb_kmod_share(shrlist);
572148c5f43SAlan Wright nvlist_free(shrlist);
573148c5f43SAlan Wright }
574148c5f43SAlan Wright }
575148c5f43SAlan Wright
576b89a8333Snatalie li - Sun Microsystems - Irvine United States smb_shr_unpublish(from_name, to_si.shr_container);
577b89a8333Snatalie li - Sun Microsystems - Irvine United States smb_shr_publish(to_name, to_si.shr_container);
578c8ec8eeaSjose borrego
579c8ec8eeaSjose borrego return (NERR_Success);
5803db3f65cSamw }
5813db3f65cSamw
5823db3f65cSamw /*
583c8ec8eeaSjose borrego * Load the information for the specified share into the supplied share
584c8ec8eeaSjose borrego * info structure.
5858d7e4166Sjose borrego *
5868d7e4166Sjose borrego * First looks up the cache to see if the specified share exists, if there
5878d7e4166Sjose borrego * is a miss then it looks up sharemgr.
588c8ec8eeaSjose borrego */
589c8ec8eeaSjose borrego uint32_t
smb_shr_get(char * sharename,smb_share_t * si)590c8ec8eeaSjose borrego smb_shr_get(char *sharename, smb_share_t *si)
591c8ec8eeaSjose borrego {
5928d7e4166Sjose borrego uint32_t status;
593c8ec8eeaSjose borrego
594b89a8333Snatalie li - Sun Microsystems - Irvine United States if (sharename == NULL || *sharename == '\0')
595c8ec8eeaSjose borrego return (NERR_NetNameNotFound);
596b89a8333Snatalie li - Sun Microsystems - Irvine United States
5978d7e4166Sjose borrego if ((status = smb_shr_lookup(sharename, si)) == NERR_Success)
5988d7e4166Sjose borrego return (status);
599c8ec8eeaSjose borrego
6008d7e4166Sjose borrego if ((status = smb_shr_sa_loadbyname(sharename)) == NERR_Success)
6018d7e4166Sjose borrego status = smb_shr_lookup(sharename, si);
602c8ec8eeaSjose borrego
603b89a8333Snatalie li - Sun Microsystems - Irvine United States return (status);
604c8ec8eeaSjose borrego }
605c8ec8eeaSjose borrego
606c8ec8eeaSjose borrego /*
607c8ec8eeaSjose borrego * Modifies an existing share. Properties that can be modified are:
608c8ec8eeaSjose borrego *
609c8ec8eeaSjose borrego * o comment
610c8ec8eeaSjose borrego * o AD container
611b89a8333Snatalie li - Sun Microsystems - Irvine United States * o host access
612e3f2c991SKeyur Desai * o abe
613c8ec8eeaSjose borrego */
614c8ec8eeaSjose borrego uint32_t
smb_shr_modify(smb_share_t * new_si)615b89a8333Snatalie li - Sun Microsystems - Irvine United States smb_shr_modify(smb_share_t *new_si)
616c8ec8eeaSjose borrego {
617c8ec8eeaSjose borrego smb_share_t *si;
618b89a8333Snatalie li - Sun Microsystems - Irvine United States boolean_t adc_changed = B_FALSE;
619b89a8333Snatalie li - Sun Microsystems - Irvine United States char old_container[MAXPATHLEN];
6209fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States uint32_t access, flag;
621148c5f43SAlan Wright nvlist_t *shrlist;
622c8ec8eeaSjose borrego
623b89a8333Snatalie li - Sun Microsystems - Irvine United States assert(new_si != NULL);
624c8ec8eeaSjose borrego
625b89a8333Snatalie li - Sun Microsystems - Irvine United States if (smb_shr_cache_lock(SMB_SHR_CACHE_WRLOCK) != NERR_Success)
626b89a8333Snatalie li - Sun Microsystems - Irvine United States return (NERR_InternalError);
627c8ec8eeaSjose borrego
628b89a8333Snatalie li - Sun Microsystems - Irvine United States if ((si = smb_shr_cache_findent(new_si->shr_name)) == NULL) {
629b89a8333Snatalie li - Sun Microsystems - Irvine United States smb_shr_cache_unlock();
630b89a8333Snatalie li - Sun Microsystems - Irvine United States return (NERR_NetNameNotFound);
631c8ec8eeaSjose borrego }
632c8ec8eeaSjose borrego
633b89a8333Snatalie li - Sun Microsystems - Irvine United States if (si->shr_type & STYPE_IPC) {
634b89a8333Snatalie li - Sun Microsystems - Irvine United States /* IPC$ share cannot be modified */
635b89a8333Snatalie li - Sun Microsystems - Irvine United States smb_shr_cache_unlock();
636b89a8333Snatalie li - Sun Microsystems - Irvine United States return (ERROR_ACCESS_DENIED);
637c8ec8eeaSjose borrego }
638c8ec8eeaSjose borrego
63989dc44ceSjose borrego (void) strlcpy(si->shr_cmnt, new_si->shr_cmnt, sizeof (si->shr_cmnt));
640b89a8333Snatalie li - Sun Microsystems - Irvine United States
641b89a8333Snatalie li - Sun Microsystems - Irvine United States adc_changed = (strcmp(new_si->shr_container, si->shr_container) != 0);
642b89a8333Snatalie li - Sun Microsystems - Irvine United States if (adc_changed) {
643b89a8333Snatalie li - Sun Microsystems - Irvine United States /* save current container - needed for unpublishing */
644b89a8333Snatalie li - Sun Microsystems - Irvine United States (void) strlcpy(old_container, si->shr_container,
645b89a8333Snatalie li - Sun Microsystems - Irvine United States sizeof (old_container));
646b89a8333Snatalie li - Sun Microsystems - Irvine United States (void) strlcpy(si->shr_container, new_si->shr_container,
647b89a8333Snatalie li - Sun Microsystems - Irvine United States sizeof (si->shr_container));
648b89a8333Snatalie li - Sun Microsystems - Irvine United States }
649b89a8333Snatalie li - Sun Microsystems - Irvine United States
6509fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States flag = (new_si->shr_flags & SMB_SHRF_ABE);
651e3f2c991SKeyur Desai si->shr_flags &= ~SMB_SHRF_ABE;
6529fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States si->shr_flags |= flag;
653e3f2c991SKeyur Desai
6549fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States flag = (new_si->shr_flags & SMB_SHRF_CATIA);
6558b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States si->shr_flags &= ~SMB_SHRF_CATIA;
6569fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States si->shr_flags |= flag;
6578b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States
6589fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States flag = (new_si->shr_flags & SMB_SHRF_GUEST_OK);
65929bd2886SAlan Wright si->shr_flags &= ~SMB_SHRF_GUEST_OK;
6609fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States si->shr_flags |= flag;
6619fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States
6629fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States flag = (new_si->shr_flags & SMB_SHRF_DFSROOT);
6639fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States si->shr_flags &= ~SMB_SHRF_DFSROOT;
6649fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States si->shr_flags |= flag;
6659fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States
6669fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States flag = (new_si->shr_flags & SMB_SHRF_CSC_MASK);
6679fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States si->shr_flags &= ~SMB_SHRF_CSC_MASK;
6689fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States si->shr_flags |= flag;
6698d7e4166Sjose borrego
670b89a8333Snatalie li - Sun Microsystems - Irvine United States access = (new_si->shr_flags & SMB_SHRF_ACC_ALL);
67189dc44ceSjose borrego si->shr_flags &= ~SMB_SHRF_ACC_ALL;
672b89a8333Snatalie li - Sun Microsystems - Irvine United States si->shr_flags |= access;
673b89a8333Snatalie li - Sun Microsystems - Irvine United States
674b89a8333Snatalie li - Sun Microsystems - Irvine United States if (access & SMB_SHRF_ACC_NONE)
675b89a8333Snatalie li - Sun Microsystems - Irvine United States (void) strlcpy(si->shr_access_none, new_si->shr_access_none,
676b89a8333Snatalie li - Sun Microsystems - Irvine United States sizeof (si->shr_access_none));
677b89a8333Snatalie li - Sun Microsystems - Irvine United States
678b89a8333Snatalie li - Sun Microsystems - Irvine United States if (access & SMB_SHRF_ACC_RO)
679b89a8333Snatalie li - Sun Microsystems - Irvine United States (void) strlcpy(si->shr_access_ro, new_si->shr_access_ro,
680b89a8333Snatalie li - Sun Microsystems - Irvine United States sizeof (si->shr_access_ro));
681b89a8333Snatalie li - Sun Microsystems - Irvine United States
682b89a8333Snatalie li - Sun Microsystems - Irvine United States if (access & SMB_SHRF_ACC_RW)
683b89a8333Snatalie li - Sun Microsystems - Irvine United States (void) strlcpy(si->shr_access_rw, new_si->shr_access_rw,
684b89a8333Snatalie li - Sun Microsystems - Irvine United States sizeof (si->shr_access_rw));
685b89a8333Snatalie li - Sun Microsystems - Irvine United States
686b89a8333Snatalie li - Sun Microsystems - Irvine United States smb_shr_cache_unlock();
687b89a8333Snatalie li - Sun Microsystems - Irvine United States
688148c5f43SAlan Wright if (smb_shr_encode(si, &shrlist) == 0) {
689148c5f43SAlan Wright (void) smb_kmod_unshare(shrlist);
690148c5f43SAlan Wright nvlist_free(shrlist);
691148c5f43SAlan Wright
692148c5f43SAlan Wright if (smb_shr_encode(new_si, &shrlist) == 0) {
693148c5f43SAlan Wright (void) smb_kmod_share(shrlist);
694148c5f43SAlan Wright nvlist_free(shrlist);
695148c5f43SAlan Wright }
696148c5f43SAlan Wright }
697148c5f43SAlan Wright
698b89a8333Snatalie li - Sun Microsystems - Irvine United States if (adc_changed) {
699b89a8333Snatalie li - Sun Microsystems - Irvine United States smb_shr_unpublish(new_si->shr_name, old_container);
700b89a8333Snatalie li - Sun Microsystems - Irvine United States smb_shr_publish(new_si->shr_name, new_si->shr_container);
701b89a8333Snatalie li - Sun Microsystems - Irvine United States }
702b89a8333Snatalie li - Sun Microsystems - Irvine United States
703b89a8333Snatalie li - Sun Microsystems - Irvine United States return (NERR_Success);
704b89a8333Snatalie li - Sun Microsystems - Irvine United States }
705c8ec8eeaSjose borrego
706c8ec8eeaSjose borrego /*
7073db3f65cSamw * smb_shr_exists
7083db3f65cSamw *
709c8ec8eeaSjose borrego * Returns B_TRUE if the share exists. Otherwise returns B_FALSE
7103db3f65cSamw */
711c8ec8eeaSjose borrego boolean_t
smb_shr_exists(char * sharename)712c8ec8eeaSjose borrego smb_shr_exists(char *sharename)
7133db3f65cSamw {
714b89a8333Snatalie li - Sun Microsystems - Irvine United States boolean_t exists = B_FALSE;
7153db3f65cSamw
716c8ec8eeaSjose borrego if (sharename == NULL || *sharename == '\0')
717c8ec8eeaSjose borrego return (B_FALSE);
718c8ec8eeaSjose borrego
719b89a8333Snatalie li - Sun Microsystems - Irvine United States if (smb_shr_cache_lock(SMB_SHR_CACHE_RDLOCK) == NERR_Success) {
720b89a8333Snatalie li - Sun Microsystems - Irvine United States exists = (smb_shr_cache_findent(sharename) != NULL);
721b89a8333Snatalie li - Sun Microsystems - Irvine United States smb_shr_cache_unlock();
722b89a8333Snatalie li - Sun Microsystems - Irvine United States }
723c8ec8eeaSjose borrego
724c8ec8eeaSjose borrego return (exists);
7253db3f65cSamw }
7263db3f65cSamw
7273db3f65cSamw /*
728b89a8333Snatalie li - Sun Microsystems - Irvine United States * If the shared directory does not begin with a /, one will be
729b89a8333Snatalie li - Sun Microsystems - Irvine United States * inserted as a prefix. If ipaddr is not zero, then also return
730b89a8333Snatalie li - Sun Microsystems - Irvine United States * information about access based on the host level access lists, if
731b89a8333Snatalie li - Sun Microsystems - Irvine United States * present. Also return access check if there is an IP address and
732b89a8333Snatalie li - Sun Microsystems - Irvine United States * shr_accflags.
733b89a8333Snatalie li - Sun Microsystems - Irvine United States *
734b89a8333Snatalie li - Sun Microsystems - Irvine United States * The value of smb_chk_hostaccess is checked for an access match.
735b89a8333Snatalie li - Sun Microsystems - Irvine United States * -1 is wildcard match
736b89a8333Snatalie li - Sun Microsystems - Irvine United States * 0 is no match
737b89a8333Snatalie li - Sun Microsystems - Irvine United States * 1 is match
738b89a8333Snatalie li - Sun Microsystems - Irvine United States *
739b89a8333Snatalie li - Sun Microsystems - Irvine United States * Precedence is none is checked first followed by ro then rw if
740b89a8333Snatalie li - Sun Microsystems - Irvine United States * needed. If x is wildcard (< 0) then check to see if the other
741b89a8333Snatalie li - Sun Microsystems - Irvine United States * values are a match. If a match, that wins.
742b89a8333Snatalie li - Sun Microsystems - Irvine United States */
743148c5f43SAlan Wright uint32_t
smb_shr_hostaccess(smb_inaddr_t * ipaddr,char * none_list,char * ro_list,char * rw_list,uint32_t flag)744148c5f43SAlan Wright smb_shr_hostaccess(smb_inaddr_t *ipaddr, char *none_list, char *ro_list,
745148c5f43SAlan Wright char *rw_list, uint32_t flag)
746b89a8333Snatalie li - Sun Microsystems - Irvine United States {
747148c5f43SAlan Wright uint32_t acc = SMB_SHRF_ACC_NONE;
748148c5f43SAlan Wright int none = 0;
749148c5f43SAlan Wright int ro = 0;
750148c5f43SAlan Wright int rw = 0;
751b89a8333Snatalie li - Sun Microsystems - Irvine United States
752148c5f43SAlan Wright if (!smb_inet_iszero(ipaddr)) {
753148c5f43SAlan Wright if ((flag & SMB_SHRF_ACC_NONE) != 0)
754148c5f43SAlan Wright none = smb_chk_hostaccess(ipaddr, none_list);
755148c5f43SAlan Wright if ((flag & SMB_SHRF_ACC_RO) != 0)
756148c5f43SAlan Wright ro = smb_chk_hostaccess(ipaddr, ro_list);
757148c5f43SAlan Wright if ((flag & SMB_SHRF_ACC_RW) != 0)
758148c5f43SAlan Wright rw = smb_chk_hostaccess(ipaddr, rw_list);
759148c5f43SAlan Wright
760b89a8333Snatalie li - Sun Microsystems - Irvine United States /* make first pass to get basic value */
761b89a8333Snatalie li - Sun Microsystems - Irvine United States if (none != 0)
762b89a8333Snatalie li - Sun Microsystems - Irvine United States acc = SMB_SHRF_ACC_NONE;
763b89a8333Snatalie li - Sun Microsystems - Irvine United States else if (ro != 0)
764b89a8333Snatalie li - Sun Microsystems - Irvine United States acc = SMB_SHRF_ACC_RO;
765b89a8333Snatalie li - Sun Microsystems - Irvine United States else if (rw != 0)
766b89a8333Snatalie li - Sun Microsystems - Irvine United States acc = SMB_SHRF_ACC_RW;
767b89a8333Snatalie li - Sun Microsystems - Irvine United States
768b89a8333Snatalie li - Sun Microsystems - Irvine United States /* make second pass to handle '*' case */
769b89a8333Snatalie li - Sun Microsystems - Irvine United States if (none < 0) {
770b89a8333Snatalie li - Sun Microsystems - Irvine United States acc = SMB_SHRF_ACC_NONE;
771b89a8333Snatalie li - Sun Microsystems - Irvine United States if (ro > 0)
772b89a8333Snatalie li - Sun Microsystems - Irvine United States acc = SMB_SHRF_ACC_RO;
773b89a8333Snatalie li - Sun Microsystems - Irvine United States else if (rw > 0)
774b89a8333Snatalie li - Sun Microsystems - Irvine United States acc = SMB_SHRF_ACC_RW;
775b89a8333Snatalie li - Sun Microsystems - Irvine United States } else if (ro < 0) {
776b89a8333Snatalie li - Sun Microsystems - Irvine United States acc = SMB_SHRF_ACC_RO;
777b89a8333Snatalie li - Sun Microsystems - Irvine United States if (none > 0)
778b89a8333Snatalie li - Sun Microsystems - Irvine United States acc = SMB_SHRF_ACC_NONE;
779b89a8333Snatalie li - Sun Microsystems - Irvine United States else if (rw > 0)
780b89a8333Snatalie li - Sun Microsystems - Irvine United States acc = SMB_SHRF_ACC_RW;
781b89a8333Snatalie li - Sun Microsystems - Irvine United States } else if (rw < 0) {
782b89a8333Snatalie li - Sun Microsystems - Irvine United States acc = SMB_SHRF_ACC_RW;
783b89a8333Snatalie li - Sun Microsystems - Irvine United States if (none > 0)
784b89a8333Snatalie li - Sun Microsystems - Irvine United States acc = SMB_SHRF_ACC_NONE;
785b89a8333Snatalie li - Sun Microsystems - Irvine United States else if (ro > 0)
786b89a8333Snatalie li - Sun Microsystems - Irvine United States acc = SMB_SHRF_ACC_RO;
787b89a8333Snatalie li - Sun Microsystems - Irvine United States }
788b89a8333Snatalie li - Sun Microsystems - Irvine United States }
789148c5f43SAlan Wright
790148c5f43SAlan Wright return (acc);
791b89a8333Snatalie li - Sun Microsystems - Irvine United States }
792b89a8333Snatalie li - Sun Microsystems - Irvine United States
793b89a8333Snatalie li - Sun Microsystems - Irvine United States /*
7943db3f65cSamw * smb_shr_is_special
7953db3f65cSamw *
796c8ec8eeaSjose borrego * Special share reserved for interprocess communication (IPC$) or
797c8ec8eeaSjose borrego * remote administration of the server (ADMIN$). Can also refer to
798c8ec8eeaSjose borrego * administrative shares such as C$, D$, E$, and so forth.
7993db3f65cSamw */
8003db3f65cSamw int
smb_shr_is_special(char * sharename)801c8ec8eeaSjose borrego smb_shr_is_special(char *sharename)
8023db3f65cSamw {
8033db3f65cSamw int len;
8043db3f65cSamw
805c8ec8eeaSjose borrego if (sharename == NULL)
8063db3f65cSamw return (0);
8073db3f65cSamw
808c8ec8eeaSjose borrego if ((len = strlen(sharename)) == 0)
8093db3f65cSamw return (0);
8103db3f65cSamw
811c8ec8eeaSjose borrego if (sharename[len - 1] == '$')
8123db3f65cSamw return (STYPE_SPECIAL);
813c8ec8eeaSjose borrego
8143db3f65cSamw return (0);
8153db3f65cSamw }
8163db3f65cSamw
8173db3f65cSamw /*
8183db3f65cSamw * smb_shr_is_restricted
8193db3f65cSamw *
8203db3f65cSamw * Check whether or not there is a restriction on a share. Restricted
8213db3f65cSamw * shares are generally STYPE_SPECIAL, for example, IPC$. All the
822c8ec8eeaSjose borrego * administration share names are restricted: C$, D$ etc. Returns B_TRUE
823c8ec8eeaSjose borrego * if the share is restricted. Otherwise B_FALSE is returned to indicate
8243db3f65cSamw * that there are no restrictions.
8253db3f65cSamw */
826c8ec8eeaSjose borrego boolean_t
smb_shr_is_restricted(char * sharename)827c8ec8eeaSjose borrego smb_shr_is_restricted(char *sharename)
8283db3f65cSamw {
8293db3f65cSamw static char *restricted[] = {
8303db3f65cSamw "IPC$"
8313db3f65cSamw };
8323db3f65cSamw
8333db3f65cSamw int i;
8343db3f65cSamw
835c8ec8eeaSjose borrego if (sharename == NULL)
836c8ec8eeaSjose borrego return (B_FALSE);
837c8ec8eeaSjose borrego
8383db3f65cSamw for (i = 0; i < sizeof (restricted)/sizeof (restricted[0]); i++) {
839bbf6f00cSJordan Brown if (smb_strcasecmp(restricted[i], sharename, 0) == 0)
840c8ec8eeaSjose borrego return (B_TRUE);
8413db3f65cSamw }
8423db3f65cSamw
843c8ec8eeaSjose borrego return (smb_shr_is_admin(sharename));
8443db3f65cSamw }
8453db3f65cSamw
8463db3f65cSamw /*
8473db3f65cSamw * smb_shr_is_admin
8483db3f65cSamw *
8493db3f65cSamw * Check whether or not access to the share should be restricted to
8503db3f65cSamw * administrators. This is a bit of a hack because what we're doing
8513db3f65cSamw * is checking for the default admin shares: C$, D$ etc.. There are
8523db3f65cSamw * other shares that have restrictions: see smb_shr_is_restricted().
8533db3f65cSamw *
854c8ec8eeaSjose borrego * Returns B_TRUE if the shares is an admin share. Otherwise B_FALSE
855c8ec8eeaSjose borrego * is returned to indicate that there are no restrictions.
8563db3f65cSamw */
857c8ec8eeaSjose borrego boolean_t
smb_shr_is_admin(char * sharename)858c8ec8eeaSjose borrego smb_shr_is_admin(char *sharename)
8593db3f65cSamw {
860c8ec8eeaSjose borrego if (sharename == NULL)
861c8ec8eeaSjose borrego return (B_FALSE);
8623db3f65cSamw
863c8ec8eeaSjose borrego if (strlen(sharename) == 2 &&
864bbf6f00cSJordan Brown smb_isalpha(sharename[0]) && sharename[1] == '$') {
865c8ec8eeaSjose borrego return (B_TRUE);
8663db3f65cSamw }
8673db3f65cSamw
868c8ec8eeaSjose borrego return (B_FALSE);
8693db3f65cSamw }
8703db3f65cSamw
871c5866007SKeyur Desai char
smb_shr_drive_letter(const char * path)872c5866007SKeyur Desai smb_shr_drive_letter(const char *path)
873c5866007SKeyur Desai {
874c5866007SKeyur Desai smb_transient_t *ts;
875c5866007SKeyur Desai int i;
876c5866007SKeyur Desai
877c5866007SKeyur Desai if (path == NULL)
878c5866007SKeyur Desai return ('\0');
879c5866007SKeyur Desai
880c5866007SKeyur Desai for (i = 0; i < sizeof (tshare)/sizeof (tshare[0]); ++i) {
881c5866007SKeyur Desai ts = &tshare[i];
882c5866007SKeyur Desai
883c5866007SKeyur Desai if (ts->path == NULL)
884c5866007SKeyur Desai continue;
885c5866007SKeyur Desai
886c5866007SKeyur Desai if (strcasecmp(ts->path, path) == 0)
887c5866007SKeyur Desai return (ts->drive);
888c5866007SKeyur Desai }
889c5866007SKeyur Desai
890c5866007SKeyur Desai return ('\0');
891c5866007SKeyur Desai }
892c5866007SKeyur Desai
893c5866007SKeyur Desai /*
894c5866007SKeyur Desai * Returns true if the specified directory is empty,
895c5866007SKeyur Desai * otherwise returns false.
896c5866007SKeyur Desai */
897c5866007SKeyur Desai static boolean_t
smb_shr_is_empty(const char * path)898c5866007SKeyur Desai smb_shr_is_empty(const char *path)
899c5866007SKeyur Desai {
900c5866007SKeyur Desai DIR *dirp;
901c5866007SKeyur Desai struct dirent *dp;
902c5866007SKeyur Desai
903c5866007SKeyur Desai if (path == NULL)
904c5866007SKeyur Desai return (B_TRUE);
905c5866007SKeyur Desai
906c5866007SKeyur Desai if ((dirp = opendir(path)) == NULL)
907c5866007SKeyur Desai return (B_TRUE);
908c5866007SKeyur Desai
909c5866007SKeyur Desai while ((dp = readdir(dirp)) != NULL) {
910c5866007SKeyur Desai if (!smb_shr_is_dot_or_dotdot(dp->d_name))
911c5866007SKeyur Desai return (B_FALSE);
912c5866007SKeyur Desai }
913c5866007SKeyur Desai
914c5866007SKeyur Desai (void) closedir(dirp);
915c5866007SKeyur Desai return (B_TRUE);
916c5866007SKeyur Desai }
917c5866007SKeyur Desai
918c5866007SKeyur Desai /*
919c5866007SKeyur Desai * Returns true if name is "." or "..", otherwise returns false.
920c5866007SKeyur Desai */
921c5866007SKeyur Desai static boolean_t
smb_shr_is_dot_or_dotdot(const char * name)922c5866007SKeyur Desai smb_shr_is_dot_or_dotdot(const char *name)
923c5866007SKeyur Desai {
924c5866007SKeyur Desai if (*name != '.')
925c5866007SKeyur Desai return (B_FALSE);
926c5866007SKeyur Desai
927c5866007SKeyur Desai if ((name[1] == '\0') || (name[1] == '.' && name[2] == '\0'))
928c5866007SKeyur Desai return (B_TRUE);
929c5866007SKeyur Desai
930c5866007SKeyur Desai return (B_FALSE);
931c5866007SKeyur Desai }
932c5866007SKeyur Desai
9333db3f65cSamw /*
9343db3f65cSamw * smb_shr_get_realpath
9353db3f65cSamw *
936b89a8333Snatalie li - Sun Microsystems - Irvine United States * Derive the real path for a share from the path provided by a client.
937b89a8333Snatalie li - Sun Microsystems - Irvine United States * For instance, the real path of C:\ may be /cvol or the real path of
938b89a8333Snatalie li - Sun Microsystems - Irvine United States * F:\home may be /vol1/home.
9393db3f65cSamw *
940b89a8333Snatalie li - Sun Microsystems - Irvine United States * clntpath - path provided by the Windows client is in the
9413db3f65cSamw * format of <drive letter>:\<dir>
9423db3f65cSamw * realpath - path that will be stored as the directory field of
9433db3f65cSamw * the smb_share_t structure of the share.
944b89a8333Snatalie li - Sun Microsystems - Irvine United States * maxlen - maximum length of the realpath buffer
9453db3f65cSamw *
9463db3f65cSamw * Return LAN Manager network error code.
9473db3f65cSamw */
9483db3f65cSamw uint32_t
smb_shr_get_realpath(const char * clntpath,char * realpath,int maxlen)949b89a8333Snatalie li - Sun Microsystems - Irvine United States smb_shr_get_realpath(const char *clntpath, char *realpath, int maxlen)
9503db3f65cSamw {
951b89a8333Snatalie li - Sun Microsystems - Irvine United States const char *p;
952b89a8333Snatalie li - Sun Microsystems - Irvine United States int len;
9533db3f65cSamw
954b89a8333Snatalie li - Sun Microsystems - Irvine United States if ((p = strchr(clntpath, ':')) != NULL)
955b89a8333Snatalie li - Sun Microsystems - Irvine United States ++p;
956b89a8333Snatalie li - Sun Microsystems - Irvine United States else
957b89a8333Snatalie li - Sun Microsystems - Irvine United States p = clntpath;
958c8ec8eeaSjose borrego
959b89a8333Snatalie li - Sun Microsystems - Irvine United States (void) strlcpy(realpath, p, maxlen);
960b89a8333Snatalie li - Sun Microsystems - Irvine United States (void) strcanon(realpath, "/\\");
961b89a8333Snatalie li - Sun Microsystems - Irvine United States (void) strsubst(realpath, '\\', '/');
962c8ec8eeaSjose borrego
963b89a8333Snatalie li - Sun Microsystems - Irvine United States len = strlen(realpath);
964b89a8333Snatalie li - Sun Microsystems - Irvine United States if ((len > 1) && (realpath[len - 1] == '/'))
965b89a8333Snatalie li - Sun Microsystems - Irvine United States realpath[len - 1] = '\0';
966c8ec8eeaSjose borrego
967c8ec8eeaSjose borrego return (NERR_Success);
968c8ec8eeaSjose borrego }
969c8ec8eeaSjose borrego
970b89a8333Snatalie li - Sun Microsystems - Irvine United States void
smb_shr_list(int offset,smb_shrlist_t * list)971b89a8333Snatalie li - Sun Microsystems - Irvine United States smb_shr_list(int offset, smb_shrlist_t *list)
972c8ec8eeaSjose borrego {
973b89a8333Snatalie li - Sun Microsystems - Irvine United States smb_shriter_t iterator;
974b89a8333Snatalie li - Sun Microsystems - Irvine United States smb_share_t *si;
975b89a8333Snatalie li - Sun Microsystems - Irvine United States int n = 0;
976c8ec8eeaSjose borrego
977b89a8333Snatalie li - Sun Microsystems - Irvine United States bzero(list, sizeof (smb_shrlist_t));
978b89a8333Snatalie li - Sun Microsystems - Irvine United States smb_shr_iterinit(&iterator);
979c8ec8eeaSjose borrego
980b89a8333Snatalie li - Sun Microsystems - Irvine United States while ((si = smb_shr_iterate(&iterator)) != NULL) {
981b89a8333Snatalie li - Sun Microsystems - Irvine United States if (--offset > 0)
982c8ec8eeaSjose borrego continue;
983c8ec8eeaSjose borrego
984b89a8333Snatalie li - Sun Microsystems - Irvine United States if ((si->shr_flags & SMB_SHRF_TRANS) &&
985b89a8333Snatalie li - Sun Microsystems - Irvine United States ((si->shr_type & STYPE_IPC) == 0)) {
986b89a8333Snatalie li - Sun Microsystems - Irvine United States bcopy(si, &list->sl_shares[n], sizeof (smb_share_t));
987b89a8333Snatalie li - Sun Microsystems - Irvine United States if (++n == LMSHARES_PER_REQUEST)
988b89a8333Snatalie li - Sun Microsystems - Irvine United States break;
989b89a8333Snatalie li - Sun Microsystems - Irvine United States }
990c8ec8eeaSjose borrego }
991c8ec8eeaSjose borrego
992b89a8333Snatalie li - Sun Microsystems - Irvine United States list->sl_cnt = n;
993c8ec8eeaSjose borrego }
994c8ec8eeaSjose borrego
995c8ec8eeaSjose borrego /*
99629bd2886SAlan Wright * Executes the map/unmap command associated with a share.
99729bd2886SAlan Wright *
99829bd2886SAlan Wright * Returns 0 on success. Otherwise non-zero for errors.
99929bd2886SAlan Wright */
100029bd2886SAlan Wright int
smb_shr_exec(smb_shr_execinfo_t * subs)1001148c5f43SAlan Wright smb_shr_exec(smb_shr_execinfo_t *subs)
100229bd2886SAlan Wright {
100329bd2886SAlan Wright char cmd[MAXPATHLEN], **cmd_tokens, *path, *ptr;
100429bd2886SAlan Wright pid_t child_pid;
100529bd2886SAlan Wright int child_status;
100629bd2886SAlan Wright struct sigaction pact, cact;
100729bd2886SAlan Wright smb_share_t si;
100829bd2886SAlan Wright
1009148c5f43SAlan Wright if (smb_shr_get(subs->e_sharename, &si) != 0)
101029bd2886SAlan Wright return (-1);
101129bd2886SAlan Wright
101229bd2886SAlan Wright *cmd = '\0';
101329bd2886SAlan Wright
101429bd2886SAlan Wright (void) mutex_lock(&smb_shr_exec_mtx);
101529bd2886SAlan Wright
1016148c5f43SAlan Wright switch (subs->e_type) {
1017148c5f43SAlan Wright case SMB_EXEC_MAP:
101829bd2886SAlan Wright (void) strlcpy(cmd, smb_shr_exec_map, sizeof (cmd));
101929bd2886SAlan Wright break;
1020148c5f43SAlan Wright case SMB_EXEC_UNMAP:
102129bd2886SAlan Wright (void) strlcpy(cmd, smb_shr_exec_unmap, sizeof (cmd));
102229bd2886SAlan Wright break;
102329bd2886SAlan Wright default:
102429bd2886SAlan Wright (void) mutex_unlock(&smb_shr_exec_mtx);
102529bd2886SAlan Wright return (-1);
102629bd2886SAlan Wright }
102729bd2886SAlan Wright
102829bd2886SAlan Wright (void) mutex_unlock(&smb_shr_exec_mtx);
102929bd2886SAlan Wright
103029bd2886SAlan Wright if (*cmd == '\0')
103129bd2886SAlan Wright return (0);
103229bd2886SAlan Wright
1033e3f2c991SKeyur Desai if (smb_proc_takesem() != 0)
1034e3f2c991SKeyur Desai return (-1);
1035e3f2c991SKeyur Desai
103629bd2886SAlan Wright pact.sa_handler = smb_shr_sig_child;
103729bd2886SAlan Wright pact.sa_flags = 0;
103829bd2886SAlan Wright (void) sigemptyset(&pact.sa_mask);
103929bd2886SAlan Wright sigaction(SIGCHLD, &pact, NULL);
104029bd2886SAlan Wright
104129bd2886SAlan Wright (void) priv_set(PRIV_ON, PRIV_EFFECTIVE, PRIV_PROC_FORK, NULL);
104229bd2886SAlan Wright
104329bd2886SAlan Wright if ((child_pid = fork()) == -1) {
104429bd2886SAlan Wright (void) priv_set(PRIV_OFF, PRIV_EFFECTIVE, PRIV_PROC_FORK, NULL);
1045e3f2c991SKeyur Desai smb_proc_givesem();
104629bd2886SAlan Wright return (-1);
104729bd2886SAlan Wright }
104829bd2886SAlan Wright
104929bd2886SAlan Wright if (child_pid == 0) {
105029bd2886SAlan Wright
105129bd2886SAlan Wright /* child process */
105229bd2886SAlan Wright
105329bd2886SAlan Wright cact.sa_handler = smb_shr_sig_abnormal_term;
105429bd2886SAlan Wright cact.sa_flags = 0;
105529bd2886SAlan Wright (void) sigemptyset(&cact.sa_mask);
105629bd2886SAlan Wright sigaction(SIGTERM, &cact, NULL);
105729bd2886SAlan Wright sigaction(SIGABRT, &cact, NULL);
105829bd2886SAlan Wright sigaction(SIGSEGV, &cact, NULL);
105929bd2886SAlan Wright
106029bd2886SAlan Wright if (priv_set(PRIV_ON, PRIV_EFFECTIVE, PRIV_PROC_EXEC,
106129bd2886SAlan Wright PRIV_FILE_DAC_EXECUTE, NULL))
106229bd2886SAlan Wright _exit(-1);
106329bd2886SAlan Wright
106429bd2886SAlan Wright if (smb_shr_enable_all_privs())
106529bd2886SAlan Wright _exit(-1);
106629bd2886SAlan Wright
1067e3f2c991SKeyur Desai smb_proc_initsem();
1068e3f2c991SKeyur Desai
106929bd2886SAlan Wright (void) trim_whitespace(cmd);
107029bd2886SAlan Wright (void) strcanon(cmd, " ");
107129bd2886SAlan Wright
107229bd2886SAlan Wright if ((cmd_tokens = smb_shr_tokenize_cmd(cmd)) != NULL) {
107329bd2886SAlan Wright
107429bd2886SAlan Wright if (smb_shr_expand_subs(cmd_tokens, &si, subs) != 0) {
107529bd2886SAlan Wright free(cmd_tokens[0]);
107629bd2886SAlan Wright free(cmd_tokens);
107729bd2886SAlan Wright _exit(-1);
107829bd2886SAlan Wright }
107929bd2886SAlan Wright
108029bd2886SAlan Wright ptr = cmd;
108129bd2886SAlan Wright path = strsep(&ptr, " ");
108229bd2886SAlan Wright
108329bd2886SAlan Wright (void) execv(path, cmd_tokens);
108429bd2886SAlan Wright }
108529bd2886SAlan Wright
108629bd2886SAlan Wright _exit(-1);
108729bd2886SAlan Wright }
108829bd2886SAlan Wright
1089e3f2c991SKeyur Desai (void) priv_set(PRIV_OFF, PRIV_EFFECTIVE, PRIV_PROC_FORK, NULL);
1090e3f2c991SKeyur Desai smb_proc_givesem();
1091e3f2c991SKeyur Desai
109229bd2886SAlan Wright /* parent process */
109329bd2886SAlan Wright
109429bd2886SAlan Wright while (waitpid(child_pid, &child_status, 0) < 0) {
109529bd2886SAlan Wright if (errno != EINTR)
109629bd2886SAlan Wright break;
109729bd2886SAlan Wright
109829bd2886SAlan Wright /* continue if waitpid got interrupted by a signal */
109929bd2886SAlan Wright errno = 0;
110029bd2886SAlan Wright continue;
110129bd2886SAlan Wright }
110229bd2886SAlan Wright
110329bd2886SAlan Wright if (WIFEXITED(child_status))
110429bd2886SAlan Wright return (WEXITSTATUS(child_status));
110529bd2886SAlan Wright
110629bd2886SAlan Wright return (child_status);
110729bd2886SAlan Wright }
110829bd2886SAlan Wright
110929bd2886SAlan Wright /*
1110e3f2c991SKeyur Desai * Locking for process-wide settings (i.e. privileges)
1111e3f2c991SKeyur Desai */
1112e3f2c991SKeyur Desai void
smb_proc_initsem(void)1113e3f2c991SKeyur Desai smb_proc_initsem(void)
1114e3f2c991SKeyur Desai {
1115e3f2c991SKeyur Desai (void) sema_init(&smb_proc_sem, 1, USYNC_THREAD, NULL);
1116e3f2c991SKeyur Desai }
1117e3f2c991SKeyur Desai
1118e3f2c991SKeyur Desai int
smb_proc_takesem(void)1119e3f2c991SKeyur Desai smb_proc_takesem(void)
1120e3f2c991SKeyur Desai {
1121e3f2c991SKeyur Desai return (sema_wait(&smb_proc_sem));
1122e3f2c991SKeyur Desai }
1123e3f2c991SKeyur Desai
1124e3f2c991SKeyur Desai void
smb_proc_givesem(void)1125e3f2c991SKeyur Desai smb_proc_givesem(void)
1126e3f2c991SKeyur Desai {
1127e3f2c991SKeyur Desai (void) sema_post(&smb_proc_sem);
1128e3f2c991SKeyur Desai }
1129e3f2c991SKeyur Desai
1130e3f2c991SKeyur Desai /*
1131b89a8333Snatalie li - Sun Microsystems - Irvine United States * ============================================
1132b89a8333Snatalie li - Sun Microsystems - Irvine United States * Private helper/utility functions
1133b89a8333Snatalie li - Sun Microsystems - Irvine United States * ============================================
1134c8ec8eeaSjose borrego */
1135c8ec8eeaSjose borrego
1136c8ec8eeaSjose borrego /*
11378d7e4166Sjose borrego * Looks up the given share in the cache and return
11388d7e4166Sjose borrego * the info in 'si'
11398d7e4166Sjose borrego */
11408d7e4166Sjose borrego static uint32_t
smb_shr_lookup(char * sharename,smb_share_t * si)11418d7e4166Sjose borrego smb_shr_lookup(char *sharename, smb_share_t *si)
11428d7e4166Sjose borrego {
11438d7e4166Sjose borrego smb_share_t *cached_si;
11448d7e4166Sjose borrego uint32_t status = NERR_NetNameNotFound;
11458d7e4166Sjose borrego
11468d7e4166Sjose borrego if (sharename == NULL || *sharename == '\0')
11478d7e4166Sjose borrego return (NERR_NetNameNotFound);
11488d7e4166Sjose borrego if (smb_shr_cache_lock(SMB_SHR_CACHE_RDLOCK) == NERR_Success) {
11498d7e4166Sjose borrego cached_si = smb_shr_cache_findent(sharename);
11508d7e4166Sjose borrego if (cached_si != NULL) {
11518d7e4166Sjose borrego bcopy(cached_si, si, sizeof (smb_share_t));
11528d7e4166Sjose borrego status = NERR_Success;
11538d7e4166Sjose borrego }
11548d7e4166Sjose borrego
11558d7e4166Sjose borrego smb_shr_cache_unlock();
11568d7e4166Sjose borrego }
11578d7e4166Sjose borrego return (status);
11588d7e4166Sjose borrego }
11598d7e4166Sjose borrego
11608d7e4166Sjose borrego /*
11619fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States * Add IPC$ or Admin shares to the cache upon startup.
1162c8ec8eeaSjose borrego */
1163c8ec8eeaSjose borrego static uint32_t
smb_shr_add_transient(char * name,char * cmnt,char * path)11649fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States smb_shr_add_transient(char *name, char *cmnt, char *path)
1165c8ec8eeaSjose borrego {
11669fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States smb_share_t trans;
1167b89a8333Snatalie li - Sun Microsystems - Irvine United States uint32_t status = NERR_InternalError;
1168c8ec8eeaSjose borrego
11699fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States if (name == NULL)
11709fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States return (status);
11719fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States
11729fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States bzero(&trans, sizeof (smb_share_t));
11739fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States (void) strlcpy(trans.shr_name, name, MAXNAMELEN);
11749fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States if (cmnt)
11759fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States (void) strlcpy(trans.shr_cmnt, cmnt, SMB_SHARE_CMNT_MAX);
11769fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States
11779fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States if (path)
11789fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States (void) strlcpy(trans.shr_path, path, MAXPATHLEN);
11799fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States
11809fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States if (strcasecmp(name, "IPC$") == 0)
11819fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States trans.shr_type = STYPE_IPC;
11829fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States
11839fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States trans.shr_flags = SMB_SHRF_TRANS;
1184c8ec8eeaSjose borrego
1185b89a8333Snatalie li - Sun Microsystems - Irvine United States if (smb_shr_cache_lock(SMB_SHR_CACHE_WRLOCK) == NERR_Success) {
11869fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States status = smb_shr_cache_addent(&trans);
1187b89a8333Snatalie li - Sun Microsystems - Irvine United States smb_shr_cache_unlock();
1188c8ec8eeaSjose borrego }
1189c8ec8eeaSjose borrego
1190c8ec8eeaSjose borrego return (status);
1191c8ec8eeaSjose borrego }
1192c8ec8eeaSjose borrego
1193c8ec8eeaSjose borrego /*
1194c8ec8eeaSjose borrego * ============================================
1195b89a8333Snatalie li - Sun Microsystems - Irvine United States * Cache management functions
1196b89a8333Snatalie li - Sun Microsystems - Irvine United States *
1197b89a8333Snatalie li - Sun Microsystems - Irvine United States * All cache functions are private
1198c8ec8eeaSjose borrego * ============================================
1199c8ec8eeaSjose borrego */
1200c8ec8eeaSjose borrego
1201c8ec8eeaSjose borrego /*
1202b89a8333Snatalie li - Sun Microsystems - Irvine United States * Create the share cache (hash table).
1203c8ec8eeaSjose borrego */
1204c8ec8eeaSjose borrego static uint32_t
smb_shr_cache_create(void)1205b89a8333Snatalie li - Sun Microsystems - Irvine United States smb_shr_cache_create(void)
1206b89a8333Snatalie li - Sun Microsystems - Irvine United States {
1207b89a8333Snatalie li - Sun Microsystems - Irvine United States uint32_t status = NERR_Success;
1208b89a8333Snatalie li - Sun Microsystems - Irvine United States
1209b89a8333Snatalie li - Sun Microsystems - Irvine United States (void) mutex_lock(&smb_shr_cache.sc_mtx);
1210b89a8333Snatalie li - Sun Microsystems - Irvine United States switch (smb_shr_cache.sc_state) {
1211b89a8333Snatalie li - Sun Microsystems - Irvine United States case SMB_SHR_CACHE_STATE_NONE:
1212b89a8333Snatalie li - Sun Microsystems - Irvine United States smb_shr_cache.sc_cache = ht_create_table(SMB_SHR_HTAB_SZ,
1213b89a8333Snatalie li - Sun Microsystems - Irvine United States MAXNAMELEN, 0);
1214b89a8333Snatalie li - Sun Microsystems - Irvine United States if (smb_shr_cache.sc_cache == NULL) {
1215b89a8333Snatalie li - Sun Microsystems - Irvine United States status = NERR_InternalError;
1216b89a8333Snatalie li - Sun Microsystems - Irvine United States break;
1217b89a8333Snatalie li - Sun Microsystems - Irvine United States }
1218b89a8333Snatalie li - Sun Microsystems - Irvine United States
1219b89a8333Snatalie li - Sun Microsystems - Irvine United States (void) ht_register_callback(smb_shr_cache.sc_cache,
1220b89a8333Snatalie li - Sun Microsystems - Irvine United States smb_shr_cache_freent);
1221b89a8333Snatalie li - Sun Microsystems - Irvine United States smb_shr_cache.sc_nops = 0;
1222b89a8333Snatalie li - Sun Microsystems - Irvine United States smb_shr_cache.sc_state = SMB_SHR_CACHE_STATE_CREATED;
1223b89a8333Snatalie li - Sun Microsystems - Irvine United States break;
1224b89a8333Snatalie li - Sun Microsystems - Irvine United States
1225b89a8333Snatalie li - Sun Microsystems - Irvine United States default:
1226b89a8333Snatalie li - Sun Microsystems - Irvine United States assert(0);
1227b89a8333Snatalie li - Sun Microsystems - Irvine United States status = NERR_InternalError;
1228b89a8333Snatalie li - Sun Microsystems - Irvine United States break;
1229b89a8333Snatalie li - Sun Microsystems - Irvine United States }
1230b89a8333Snatalie li - Sun Microsystems - Irvine United States (void) mutex_unlock(&smb_shr_cache.sc_mtx);
1231b89a8333Snatalie li - Sun Microsystems - Irvine United States
1232b89a8333Snatalie li - Sun Microsystems - Irvine United States return (status);
1233b89a8333Snatalie li - Sun Microsystems - Irvine United States }
1234b89a8333Snatalie li - Sun Microsystems - Irvine United States
1235b89a8333Snatalie li - Sun Microsystems - Irvine United States /*
1236b89a8333Snatalie li - Sun Microsystems - Irvine United States * Destroy the share cache (hash table).
1237b89a8333Snatalie li - Sun Microsystems - Irvine United States * Wait for inflight/pending operations to finish or abort before
1238b89a8333Snatalie li - Sun Microsystems - Irvine United States * destroying the cache.
1239b89a8333Snatalie li - Sun Microsystems - Irvine United States */
1240b89a8333Snatalie li - Sun Microsystems - Irvine United States static void
smb_shr_cache_destroy(void)1241b89a8333Snatalie li - Sun Microsystems - Irvine United States smb_shr_cache_destroy(void)
1242b89a8333Snatalie li - Sun Microsystems - Irvine United States {
1243b89a8333Snatalie li - Sun Microsystems - Irvine United States (void) mutex_lock(&smb_shr_cache.sc_mtx);
1244b89a8333Snatalie li - Sun Microsystems - Irvine United States if (smb_shr_cache.sc_state == SMB_SHR_CACHE_STATE_CREATED) {
1245b89a8333Snatalie li - Sun Microsystems - Irvine United States smb_shr_cache.sc_state = SMB_SHR_CACHE_STATE_DESTROYING;
1246b89a8333Snatalie li - Sun Microsystems - Irvine United States while (smb_shr_cache.sc_nops > 0)
1247b89a8333Snatalie li - Sun Microsystems - Irvine United States (void) cond_wait(&smb_shr_cache.sc_cv,
1248b89a8333Snatalie li - Sun Microsystems - Irvine United States &smb_shr_cache.sc_mtx);
1249b89a8333Snatalie li - Sun Microsystems - Irvine United States
1250b89a8333Snatalie li - Sun Microsystems - Irvine United States smb_shr_cache.sc_cache = NULL;
1251b89a8333Snatalie li - Sun Microsystems - Irvine United States smb_shr_cache.sc_state = SMB_SHR_CACHE_STATE_NONE;
1252b89a8333Snatalie li - Sun Microsystems - Irvine United States }
1253b89a8333Snatalie li - Sun Microsystems - Irvine United States (void) mutex_unlock(&smb_shr_cache.sc_mtx);
1254b89a8333Snatalie li - Sun Microsystems - Irvine United States }
1255b89a8333Snatalie li - Sun Microsystems - Irvine United States
1256b89a8333Snatalie li - Sun Microsystems - Irvine United States /*
1257b89a8333Snatalie li - Sun Microsystems - Irvine United States * If the cache is in "created" state, lock the cache for read
1258b89a8333Snatalie li - Sun Microsystems - Irvine United States * or read/write based on the specified mode.
1259b89a8333Snatalie li - Sun Microsystems - Irvine United States *
1260b89a8333Snatalie li - Sun Microsystems - Irvine United States * Whenever a lock is granted, the number of inflight cache
1261b89a8333Snatalie li - Sun Microsystems - Irvine United States * operations is incremented.
1262b89a8333Snatalie li - Sun Microsystems - Irvine United States */
1263b89a8333Snatalie li - Sun Microsystems - Irvine United States static uint32_t
smb_shr_cache_lock(int mode)1264b89a8333Snatalie li - Sun Microsystems - Irvine United States smb_shr_cache_lock(int mode)
1265b89a8333Snatalie li - Sun Microsystems - Irvine United States {
1266b89a8333Snatalie li - Sun Microsystems - Irvine United States (void) mutex_lock(&smb_shr_cache.sc_mtx);
12678d7e4166Sjose borrego if (smb_shr_cache.sc_state != SMB_SHR_CACHE_STATE_CREATED) {
1268b89a8333Snatalie li - Sun Microsystems - Irvine United States (void) mutex_unlock(&smb_shr_cache.sc_mtx);
1269b89a8333Snatalie li - Sun Microsystems - Irvine United States return (NERR_InternalError);
1270b89a8333Snatalie li - Sun Microsystems - Irvine United States }
12718d7e4166Sjose borrego smb_shr_cache.sc_nops++;
1272b89a8333Snatalie li - Sun Microsystems - Irvine United States (void) mutex_unlock(&smb_shr_cache.sc_mtx);
1273b89a8333Snatalie li - Sun Microsystems - Irvine United States
1274b89a8333Snatalie li - Sun Microsystems - Irvine United States /*
1275b89a8333Snatalie li - Sun Microsystems - Irvine United States * Lock has to be taken outside the mutex otherwise
1276b89a8333Snatalie li - Sun Microsystems - Irvine United States * there could be a deadlock
1277b89a8333Snatalie li - Sun Microsystems - Irvine United States */
1278b89a8333Snatalie li - Sun Microsystems - Irvine United States if (mode == SMB_SHR_CACHE_RDLOCK)
1279b89a8333Snatalie li - Sun Microsystems - Irvine United States (void) rw_rdlock(&smb_shr_cache.sc_cache_lck);
1280b89a8333Snatalie li - Sun Microsystems - Irvine United States else
1281b89a8333Snatalie li - Sun Microsystems - Irvine United States (void) rw_wrlock(&smb_shr_cache.sc_cache_lck);
1282b89a8333Snatalie li - Sun Microsystems - Irvine United States
1283b89a8333Snatalie li - Sun Microsystems - Irvine United States return (NERR_Success);
1284b89a8333Snatalie li - Sun Microsystems - Irvine United States }
1285b89a8333Snatalie li - Sun Microsystems - Irvine United States
1286b89a8333Snatalie li - Sun Microsystems - Irvine United States /*
1287b89a8333Snatalie li - Sun Microsystems - Irvine United States * Decrement the number of inflight operations and then unlock.
1288b89a8333Snatalie li - Sun Microsystems - Irvine United States */
1289b89a8333Snatalie li - Sun Microsystems - Irvine United States static void
smb_shr_cache_unlock(void)1290b89a8333Snatalie li - Sun Microsystems - Irvine United States smb_shr_cache_unlock(void)
1291b89a8333Snatalie li - Sun Microsystems - Irvine United States {
1292b89a8333Snatalie li - Sun Microsystems - Irvine United States (void) mutex_lock(&smb_shr_cache.sc_mtx);
1293b89a8333Snatalie li - Sun Microsystems - Irvine United States assert(smb_shr_cache.sc_nops > 0);
1294b89a8333Snatalie li - Sun Microsystems - Irvine United States smb_shr_cache.sc_nops--;
1295b89a8333Snatalie li - Sun Microsystems - Irvine United States (void) cond_broadcast(&smb_shr_cache.sc_cv);
1296b89a8333Snatalie li - Sun Microsystems - Irvine United States (void) mutex_unlock(&smb_shr_cache.sc_mtx);
1297b89a8333Snatalie li - Sun Microsystems - Irvine United States
1298b89a8333Snatalie li - Sun Microsystems - Irvine United States (void) rw_unlock(&smb_shr_cache.sc_cache_lck);
1299b89a8333Snatalie li - Sun Microsystems - Irvine United States }
1300b89a8333Snatalie li - Sun Microsystems - Irvine United States
1301b89a8333Snatalie li - Sun Microsystems - Irvine United States /*
1302b89a8333Snatalie li - Sun Microsystems - Irvine United States * Return the total number of shares
1303b89a8333Snatalie li - Sun Microsystems - Irvine United States */
1304b89a8333Snatalie li - Sun Microsystems - Irvine United States static int
smb_shr_cache_count(void)1305b89a8333Snatalie li - Sun Microsystems - Irvine United States smb_shr_cache_count(void)
1306b89a8333Snatalie li - Sun Microsystems - Irvine United States {
1307b89a8333Snatalie li - Sun Microsystems - Irvine United States return (ht_get_total_items(smb_shr_cache.sc_cache));
1308b89a8333Snatalie li - Sun Microsystems - Irvine United States }
1309b89a8333Snatalie li - Sun Microsystems - Irvine United States
1310b89a8333Snatalie li - Sun Microsystems - Irvine United States /*
1311b89a8333Snatalie li - Sun Microsystems - Irvine United States * looks up the given share name in the cache and if it
1312b89a8333Snatalie li - Sun Microsystems - Irvine United States * finds a match returns a pointer to the cached entry.
1313b89a8333Snatalie li - Sun Microsystems - Irvine United States * Note that since a pointer is returned this function
1314b89a8333Snatalie li - Sun Microsystems - Irvine United States * MUST be protected by smb_shr_cache_lock/unlock pair
1315b89a8333Snatalie li - Sun Microsystems - Irvine United States */
1316b89a8333Snatalie li - Sun Microsystems - Irvine United States static smb_share_t *
smb_shr_cache_findent(char * sharename)1317b89a8333Snatalie li - Sun Microsystems - Irvine United States smb_shr_cache_findent(char *sharename)
1318b89a8333Snatalie li - Sun Microsystems - Irvine United States {
1319b89a8333Snatalie li - Sun Microsystems - Irvine United States HT_ITEM *item;
1320b89a8333Snatalie li - Sun Microsystems - Irvine United States
1321bbf6f00cSJordan Brown (void) smb_strlwr(sharename);
1322b89a8333Snatalie li - Sun Microsystems - Irvine United States item = ht_find_item(smb_shr_cache.sc_cache, sharename);
1323b89a8333Snatalie li - Sun Microsystems - Irvine United States if (item && item->hi_data)
1324b89a8333Snatalie li - Sun Microsystems - Irvine United States return ((smb_share_t *)item->hi_data);
1325b89a8333Snatalie li - Sun Microsystems - Irvine United States
1326b89a8333Snatalie li - Sun Microsystems - Irvine United States return (NULL);
1327b89a8333Snatalie li - Sun Microsystems - Irvine United States }
1328b89a8333Snatalie li - Sun Microsystems - Irvine United States
1329b89a8333Snatalie li - Sun Microsystems - Irvine United States /*
1330b89a8333Snatalie li - Sun Microsystems - Irvine United States * Return a pointer to the first/next entry in
1331b89a8333Snatalie li - Sun Microsystems - Irvine United States * the cache based on the given iterator.
1332b89a8333Snatalie li - Sun Microsystems - Irvine United States *
1333b89a8333Snatalie li - Sun Microsystems - Irvine United States * Calls to this function MUST be protected by
1334b89a8333Snatalie li - Sun Microsystems - Irvine United States * smb_shr_cache_lock/unlock.
1335b89a8333Snatalie li - Sun Microsystems - Irvine United States */
1336b89a8333Snatalie li - Sun Microsystems - Irvine United States static smb_share_t *
smb_shr_cache_iterate(smb_shriter_t * shi)1337b89a8333Snatalie li - Sun Microsystems - Irvine United States smb_shr_cache_iterate(smb_shriter_t *shi)
1338b89a8333Snatalie li - Sun Microsystems - Irvine United States {
1339b89a8333Snatalie li - Sun Microsystems - Irvine United States HT_ITEM *item;
1340b89a8333Snatalie li - Sun Microsystems - Irvine United States
1341b89a8333Snatalie li - Sun Microsystems - Irvine United States if (shi->si_first) {
1342b89a8333Snatalie li - Sun Microsystems - Irvine United States item = ht_findfirst(smb_shr_cache.sc_cache, &shi->si_hashiter);
1343b89a8333Snatalie li - Sun Microsystems - Irvine United States shi->si_first = B_FALSE;
1344b89a8333Snatalie li - Sun Microsystems - Irvine United States } else {
1345b89a8333Snatalie li - Sun Microsystems - Irvine United States item = ht_findnext(&shi->si_hashiter);
1346b89a8333Snatalie li - Sun Microsystems - Irvine United States }
1347b89a8333Snatalie li - Sun Microsystems - Irvine United States
1348b89a8333Snatalie li - Sun Microsystems - Irvine United States if (item && item->hi_data)
1349b89a8333Snatalie li - Sun Microsystems - Irvine United States return ((smb_share_t *)item->hi_data);
1350b89a8333Snatalie li - Sun Microsystems - Irvine United States
1351b89a8333Snatalie li - Sun Microsystems - Irvine United States return (NULL);
1352b89a8333Snatalie li - Sun Microsystems - Irvine United States }
1353b89a8333Snatalie li - Sun Microsystems - Irvine United States
1354b89a8333Snatalie li - Sun Microsystems - Irvine United States /*
1355b89a8333Snatalie li - Sun Microsystems - Irvine United States * Add the specified share to the cache. Memory needs to be allocated
1356b89a8333Snatalie li - Sun Microsystems - Irvine United States * for the cache entry and the passed information is copied to the
1357b89a8333Snatalie li - Sun Microsystems - Irvine United States * allocated space.
1358b89a8333Snatalie li - Sun Microsystems - Irvine United States */
1359b89a8333Snatalie li - Sun Microsystems - Irvine United States static uint32_t
smb_shr_cache_addent(smb_share_t * si)1360b89a8333Snatalie li - Sun Microsystems - Irvine United States smb_shr_cache_addent(smb_share_t *si)
1361b89a8333Snatalie li - Sun Microsystems - Irvine United States {
1362b89a8333Snatalie li - Sun Microsystems - Irvine United States smb_share_t *cache_ent;
1363b89a8333Snatalie li - Sun Microsystems - Irvine United States uint32_t status = NERR_Success;
1364b89a8333Snatalie li - Sun Microsystems - Irvine United States
1365b89a8333Snatalie li - Sun Microsystems - Irvine United States if ((cache_ent = malloc(sizeof (smb_share_t))) == NULL)
1366b89a8333Snatalie li - Sun Microsystems - Irvine United States return (ERROR_NOT_ENOUGH_MEMORY);
1367b89a8333Snatalie li - Sun Microsystems - Irvine United States
1368148c5f43SAlan Wright (void) smb_strlwr(si->shr_name);
1369b89a8333Snatalie li - Sun Microsystems - Irvine United States
1370cb174861Sjoyce mcintosh if (((si->shr_type & STYPE_PRINTQ) == 0) &&
1371cb174861Sjoyce mcintosh (si->shr_type & STYPE_IPC) == 0)
1372148c5f43SAlan Wright si->shr_type = STYPE_DISKTREE;
1373cb174861Sjoyce mcintosh
1374148c5f43SAlan Wright si->shr_type |= smb_shr_is_special(cache_ent->shr_name);
1375b89a8333Snatalie li - Sun Microsystems - Irvine United States
1376b89a8333Snatalie li - Sun Microsystems - Irvine United States if (smb_shr_is_admin(cache_ent->shr_name))
1377148c5f43SAlan Wright si->shr_flags |= SMB_SHRF_ADMIN;
1378148c5f43SAlan Wright
1379148c5f43SAlan Wright bcopy(si, cache_ent, sizeof (smb_share_t));
1380b89a8333Snatalie li - Sun Microsystems - Irvine United States
1381b89a8333Snatalie li - Sun Microsystems - Irvine United States if (si->shr_flags & SMB_SHRF_AUTOHOME)
1382b89a8333Snatalie li - Sun Microsystems - Irvine United States cache_ent->shr_refcnt = 1;
1383b89a8333Snatalie li - Sun Microsystems - Irvine United States
1384b89a8333Snatalie li - Sun Microsystems - Irvine United States if (ht_add_item(smb_shr_cache.sc_cache, cache_ent->shr_name, cache_ent)
1385b89a8333Snatalie li - Sun Microsystems - Irvine United States == NULL) {
1386b89a8333Snatalie li - Sun Microsystems - Irvine United States syslog(LOG_DEBUG, "share: %s: cache update failed",
1387b89a8333Snatalie li - Sun Microsystems - Irvine United States cache_ent->shr_name);
1388b89a8333Snatalie li - Sun Microsystems - Irvine United States free(cache_ent);
1389b89a8333Snatalie li - Sun Microsystems - Irvine United States status = NERR_InternalError;
1390b89a8333Snatalie li - Sun Microsystems - Irvine United States }
1391b89a8333Snatalie li - Sun Microsystems - Irvine United States
1392b89a8333Snatalie li - Sun Microsystems - Irvine United States return (status);
1393b89a8333Snatalie li - Sun Microsystems - Irvine United States }
1394b89a8333Snatalie li - Sun Microsystems - Irvine United States
1395b89a8333Snatalie li - Sun Microsystems - Irvine United States /*
1396b89a8333Snatalie li - Sun Microsystems - Irvine United States * Delete the specified share from the cache.
1397b89a8333Snatalie li - Sun Microsystems - Irvine United States */
1398b89a8333Snatalie li - Sun Microsystems - Irvine United States static void
smb_shr_cache_delent(char * sharename)1399b89a8333Snatalie li - Sun Microsystems - Irvine United States smb_shr_cache_delent(char *sharename)
1400b89a8333Snatalie li - Sun Microsystems - Irvine United States {
1401bbf6f00cSJordan Brown (void) smb_strlwr(sharename);
1402b89a8333Snatalie li - Sun Microsystems - Irvine United States (void) ht_remove_item(smb_shr_cache.sc_cache, sharename);
1403b89a8333Snatalie li - Sun Microsystems - Irvine United States }
1404b89a8333Snatalie li - Sun Microsystems - Irvine United States
1405b89a8333Snatalie li - Sun Microsystems - Irvine United States /*
1406b89a8333Snatalie li - Sun Microsystems - Irvine United States * Call back to free the given cache entry.
1407b89a8333Snatalie li - Sun Microsystems - Irvine United States */
1408b89a8333Snatalie li - Sun Microsystems - Irvine United States static void
smb_shr_cache_freent(HT_ITEM * item)1409b89a8333Snatalie li - Sun Microsystems - Irvine United States smb_shr_cache_freent(HT_ITEM *item)
1410b89a8333Snatalie li - Sun Microsystems - Irvine United States {
1411b89a8333Snatalie li - Sun Microsystems - Irvine United States if (item && item->hi_data)
1412b89a8333Snatalie li - Sun Microsystems - Irvine United States free(item->hi_data);
1413b89a8333Snatalie li - Sun Microsystems - Irvine United States }
1414b89a8333Snatalie li - Sun Microsystems - Irvine United States
1415b89a8333Snatalie li - Sun Microsystems - Irvine United States /*
1416b89a8333Snatalie li - Sun Microsystems - Irvine United States * ============================================
1417b89a8333Snatalie li - Sun Microsystems - Irvine United States * Interfaces to sharemgr
1418b89a8333Snatalie li - Sun Microsystems - Irvine United States *
1419b89a8333Snatalie li - Sun Microsystems - Irvine United States * All functions in this section are private
1420b89a8333Snatalie li - Sun Microsystems - Irvine United States * ============================================
1421b89a8333Snatalie li - Sun Microsystems - Irvine United States */
1422b89a8333Snatalie li - Sun Microsystems - Irvine United States
1423b89a8333Snatalie li - Sun Microsystems - Irvine United States /*
1424b89a8333Snatalie li - Sun Microsystems - Irvine United States * Load shares from sharemgr
1425b89a8333Snatalie li - Sun Microsystems - Irvine United States */
1426b89a8333Snatalie li - Sun Microsystems - Irvine United States /*ARGSUSED*/
1427fd9ee8b5Sjoyce mcintosh void *
smb_shr_load(void * args)1428fd9ee8b5Sjoyce mcintosh smb_shr_load(void *args)
1429c8ec8eeaSjose borrego {
1430c8ec8eeaSjose borrego sa_handle_t handle;
1431b89a8333Snatalie li - Sun Microsystems - Irvine United States sa_group_t group, subgroup;
1432b89a8333Snatalie li - Sun Microsystems - Irvine United States char *gstate;
1433b89a8333Snatalie li - Sun Microsystems - Irvine United States boolean_t gdisabled;
1434fd9ee8b5Sjoyce mcintosh
1435fd9ee8b5Sjoyce mcintosh (void) mutex_lock(&smb_shr_exec_mtx);
1436fd9ee8b5Sjoyce mcintosh (void) smb_config_get_execinfo(smb_shr_exec_map, smb_shr_exec_unmap,
1437fd9ee8b5Sjoyce mcintosh MAXPATHLEN);
1438fd9ee8b5Sjoyce mcintosh (void) mutex_unlock(&smb_shr_exec_mtx);
1439b89a8333Snatalie li - Sun Microsystems - Irvine United States
1440cb174861Sjoyce mcintosh if ((handle = smb_shr_sa_enter()) == NULL) {
1441fd9ee8b5Sjoyce mcintosh syslog(LOG_ERR, "smb_shr_load: load failed");
1442b89a8333Snatalie li - Sun Microsystems - Irvine United States return (NULL);
1443cb174861Sjoyce mcintosh }
1444b89a8333Snatalie li - Sun Microsystems - Irvine United States
1445b89a8333Snatalie li - Sun Microsystems - Irvine United States for (group = sa_get_group(handle, NULL);
1446b89a8333Snatalie li - Sun Microsystems - Irvine United States group != NULL; group = sa_get_next_group(group)) {
1447b89a8333Snatalie li - Sun Microsystems - Irvine United States gstate = sa_get_group_attr(group, "state");
1448b89a8333Snatalie li - Sun Microsystems - Irvine United States if (gstate == NULL)
1449b89a8333Snatalie li - Sun Microsystems - Irvine United States continue;
1450b89a8333Snatalie li - Sun Microsystems - Irvine United States
1451b89a8333Snatalie li - Sun Microsystems - Irvine United States gdisabled = (strcasecmp(gstate, "disabled") == 0);
1452b89a8333Snatalie li - Sun Microsystems - Irvine United States sa_free_attr_string(gstate);
1453b89a8333Snatalie li - Sun Microsystems - Irvine United States if (gdisabled)
1454b89a8333Snatalie li - Sun Microsystems - Irvine United States continue;
1455b89a8333Snatalie li - Sun Microsystems - Irvine United States
1456b89a8333Snatalie li - Sun Microsystems - Irvine United States smb_shr_sa_loadgrp(group);
1457b89a8333Snatalie li - Sun Microsystems - Irvine United States
1458b89a8333Snatalie li - Sun Microsystems - Irvine United States for (subgroup = sa_get_sub_group(group);
1459b89a8333Snatalie li - Sun Microsystems - Irvine United States subgroup != NULL;
1460b89a8333Snatalie li - Sun Microsystems - Irvine United States subgroup = sa_get_next_group(subgroup)) {
1461b89a8333Snatalie li - Sun Microsystems - Irvine United States smb_shr_sa_loadgrp(subgroup);
1462b89a8333Snatalie li - Sun Microsystems - Irvine United States }
1463b89a8333Snatalie li - Sun Microsystems - Irvine United States
1464b89a8333Snatalie li - Sun Microsystems - Irvine United States }
146589dc44ceSjose borrego smb_shr_sa_exit();
1466b89a8333Snatalie li - Sun Microsystems - Irvine United States return (NULL);
1467b89a8333Snatalie li - Sun Microsystems - Irvine United States }
1468b89a8333Snatalie li - Sun Microsystems - Irvine United States
1469b89a8333Snatalie li - Sun Microsystems - Irvine United States /*
1470b89a8333Snatalie li - Sun Microsystems - Irvine United States * Load the shares contained in the specified group.
1471b89a8333Snatalie li - Sun Microsystems - Irvine United States *
1472b89a8333Snatalie li - Sun Microsystems - Irvine United States * Don't process groups on which the smb protocol is disabled.
1473b89a8333Snatalie li - Sun Microsystems - Irvine United States * The top level ZFS group won't have the smb protocol enabled
1474b89a8333Snatalie li - Sun Microsystems - Irvine United States * but sub-groups will.
1475b89a8333Snatalie li - Sun Microsystems - Irvine United States *
1476b89a8333Snatalie li - Sun Microsystems - Irvine United States * We will tolerate a limited number of errors and then give
1477b89a8333Snatalie li - Sun Microsystems - Irvine United States * up on the current group. A typical error might be that the
1478b89a8333Snatalie li - Sun Microsystems - Irvine United States * shared directory no longer exists.
1479b89a8333Snatalie li - Sun Microsystems - Irvine United States */
1480b89a8333Snatalie li - Sun Microsystems - Irvine United States static void
smb_shr_sa_loadgrp(sa_group_t group)1481b89a8333Snatalie li - Sun Microsystems - Irvine United States smb_shr_sa_loadgrp(sa_group_t group)
1482b89a8333Snatalie li - Sun Microsystems - Irvine United States {
1483c8ec8eeaSjose borrego sa_share_t share;
1484c8ec8eeaSjose borrego sa_resource_t resource;
1485b89a8333Snatalie li - Sun Microsystems - Irvine United States int error_count = 0;
1486c8ec8eeaSjose borrego
1487b89a8333Snatalie li - Sun Microsystems - Irvine United States if (sa_get_optionset(group, SMB_PROTOCOL_NAME) == NULL)
1488b89a8333Snatalie li - Sun Microsystems - Irvine United States return;
1489c8ec8eeaSjose borrego
1490b89a8333Snatalie li - Sun Microsystems - Irvine United States for (share = sa_get_share(group, NULL);
1491b89a8333Snatalie li - Sun Microsystems - Irvine United States share != NULL;
1492b89a8333Snatalie li - Sun Microsystems - Irvine United States share = sa_get_next_share(share)) {
1493b89a8333Snatalie li - Sun Microsystems - Irvine United States for (resource = sa_get_share_resource(share, NULL);
1494b89a8333Snatalie li - Sun Microsystems - Irvine United States resource != NULL;
1495b89a8333Snatalie li - Sun Microsystems - Irvine United States resource = sa_get_next_resource(resource)) {
1496b89a8333Snatalie li - Sun Microsystems - Irvine United States if (smb_shr_sa_load(share, resource))
1497b89a8333Snatalie li - Sun Microsystems - Irvine United States ++error_count;
1498b89a8333Snatalie li - Sun Microsystems - Irvine United States
1499b89a8333Snatalie li - Sun Microsystems - Irvine United States if (error_count > SMB_SHR_ERROR_THRESHOLD)
1500b89a8333Snatalie li - Sun Microsystems - Irvine United States break;
1501c8ec8eeaSjose borrego }
1502c8ec8eeaSjose borrego
1503b89a8333Snatalie li - Sun Microsystems - Irvine United States if (error_count > SMB_SHR_ERROR_THRESHOLD)
1504b89a8333Snatalie li - Sun Microsystems - Irvine United States break;
1505c8ec8eeaSjose borrego }
1506c8ec8eeaSjose borrego }
1507c8ec8eeaSjose borrego
1508b89a8333Snatalie li - Sun Microsystems - Irvine United States /*
1509b89a8333Snatalie li - Sun Microsystems - Irvine United States * Load a share definition from sharemgr and add it to the cache.
15108d7e4166Sjose borrego * If the share is already in the cache then it doesn't do anything.
15118d7e4166Sjose borrego *
15128d7e4166Sjose borrego * This function does not report duplicate shares as error since
15138d7e4166Sjose borrego * a share might have been added by smb_shr_get() while load is
15148d7e4166Sjose borrego * in progress.
1515b89a8333Snatalie li - Sun Microsystems - Irvine United States */
1516c8ec8eeaSjose borrego static uint32_t
smb_shr_sa_load(sa_share_t share,sa_resource_t resource)1517b89a8333Snatalie li - Sun Microsystems - Irvine United States smb_shr_sa_load(sa_share_t share, sa_resource_t resource)
1518b89a8333Snatalie li - Sun Microsystems - Irvine United States {
1519b89a8333Snatalie li - Sun Microsystems - Irvine United States smb_share_t si;
15208d7e4166Sjose borrego char *sharename;
1521b89a8333Snatalie li - Sun Microsystems - Irvine United States uint32_t status;
15228d7e4166Sjose borrego boolean_t loaded;
15238d7e4166Sjose borrego
15248d7e4166Sjose borrego if ((sharename = sa_get_resource_attr(resource, "name")) == NULL)
15258d7e4166Sjose borrego return (NERR_InternalError);
15268d7e4166Sjose borrego
15278d7e4166Sjose borrego loaded = smb_shr_exists(sharename);
15288d7e4166Sjose borrego sa_free_attr_string(sharename);
15298d7e4166Sjose borrego
15308d7e4166Sjose borrego if (loaded)
15318d7e4166Sjose borrego return (NERR_Success);
1532b89a8333Snatalie li - Sun Microsystems - Irvine United States
1533b89a8333Snatalie li - Sun Microsystems - Irvine United States if ((status = smb_shr_sa_get(share, resource, &si)) != NERR_Success) {
1534b89a8333Snatalie li - Sun Microsystems - Irvine United States syslog(LOG_DEBUG, "share: failed to load %s (%d)",
1535b89a8333Snatalie li - Sun Microsystems - Irvine United States si.shr_name, status);
1536b89a8333Snatalie li - Sun Microsystems - Irvine United States return (status);
1537b89a8333Snatalie li - Sun Microsystems - Irvine United States }
1538b89a8333Snatalie li - Sun Microsystems - Irvine United States
15398d7e4166Sjose borrego status = smb_shr_add(&si);
15408d7e4166Sjose borrego if ((status != NERR_Success) && (status != NERR_DuplicateShare)) {
1541b89a8333Snatalie li - Sun Microsystems - Irvine United States syslog(LOG_DEBUG, "share: failed to cache %s (%d)",
1542b89a8333Snatalie li - Sun Microsystems - Irvine United States si.shr_name, status);
1543b89a8333Snatalie li - Sun Microsystems - Irvine United States return (status);
1544b89a8333Snatalie li - Sun Microsystems - Irvine United States }
1545b89a8333Snatalie li - Sun Microsystems - Irvine United States
1546b89a8333Snatalie li - Sun Microsystems - Irvine United States return (NERR_Success);
1547b89a8333Snatalie li - Sun Microsystems - Irvine United States }
1548b89a8333Snatalie li - Sun Microsystems - Irvine United States
15499fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States static char *
smb_shr_sa_getprop(sa_optionset_t opts,char * propname)15509fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States smb_shr_sa_getprop(sa_optionset_t opts, char *propname)
15519fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States {
15529fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States sa_property_t prop;
15539fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States char *val = NULL;
15549fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States
15559fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States prop = sa_get_property(opts, propname);
15569fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States if (prop != NULL)
15579fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States val = sa_get_property_attr(prop, "value");
15589fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States
15599fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States return (val);
15609fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States }
15619fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States
1562b89a8333Snatalie li - Sun Microsystems - Irvine United States /*
1563b89a8333Snatalie li - Sun Microsystems - Irvine United States * Read the specified share information from sharemgr and return
1564b89a8333Snatalie li - Sun Microsystems - Irvine United States * it in the given smb_share_t structure.
1565b89a8333Snatalie li - Sun Microsystems - Irvine United States *
1566b89a8333Snatalie li - Sun Microsystems - Irvine United States * Shares read from sharemgr are marked as permanent/persistent.
1567b89a8333Snatalie li - Sun Microsystems - Irvine United States */
1568b89a8333Snatalie li - Sun Microsystems - Irvine United States static uint32_t
smb_shr_sa_get(sa_share_t share,sa_resource_t resource,smb_share_t * si)1569b89a8333Snatalie li - Sun Microsystems - Irvine United States smb_shr_sa_get(sa_share_t share, sa_resource_t resource, smb_share_t *si)
1570c8ec8eeaSjose borrego {
1571c8ec8eeaSjose borrego sa_optionset_t opts;
1572c8ec8eeaSjose borrego char *val = NULL;
1573c8ec8eeaSjose borrego char *path;
1574c8ec8eeaSjose borrego char *rname;
1575c8ec8eeaSjose borrego
1576c8ec8eeaSjose borrego if ((path = sa_get_share_attr(share, "path")) == NULL)
1577c8ec8eeaSjose borrego return (NERR_InternalError);
1578c8ec8eeaSjose borrego
1579c8ec8eeaSjose borrego if ((rname = sa_get_resource_attr(resource, "name")) == NULL) {
1580c8ec8eeaSjose borrego sa_free_attr_string(path);
1581c8ec8eeaSjose borrego return (NERR_InternalError);
1582c8ec8eeaSjose borrego }
1583c8ec8eeaSjose borrego
1584c8ec8eeaSjose borrego bzero(si, sizeof (smb_share_t));
1585c8ec8eeaSjose borrego si->shr_flags = SMB_SHRF_PERM;
1586c8ec8eeaSjose borrego
1587c8ec8eeaSjose borrego (void) strlcpy(si->shr_path, path, sizeof (si->shr_path));
1588c8ec8eeaSjose borrego (void) strlcpy(si->shr_name, rname, sizeof (si->shr_name));
1589c8ec8eeaSjose borrego sa_free_attr_string(path);
1590c8ec8eeaSjose borrego sa_free_attr_string(rname);
1591c8ec8eeaSjose borrego
1592c8ec8eeaSjose borrego val = sa_get_resource_description(resource);
1593c8ec8eeaSjose borrego if (val == NULL)
1594c8ec8eeaSjose borrego val = sa_get_share_description(share);
1595c8ec8eeaSjose borrego
1596c8ec8eeaSjose borrego if (val != NULL) {
1597c8ec8eeaSjose borrego (void) strlcpy(si->shr_cmnt, val, sizeof (si->shr_cmnt));
1598c8ec8eeaSjose borrego sa_free_share_description(val);
1599c8ec8eeaSjose borrego }
1600c8ec8eeaSjose borrego
1601c8ec8eeaSjose borrego opts = sa_get_derived_optionset(resource, SMB_PROTOCOL_NAME, 1);
1602c8ec8eeaSjose borrego if (opts == NULL)
1603c8ec8eeaSjose borrego return (NERR_Success);
1604c8ec8eeaSjose borrego
16059fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States val = smb_shr_sa_getprop(opts, SHOPT_AD_CONTAINER);
16069fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States if (val != NULL) {
1607c8ec8eeaSjose borrego (void) strlcpy(si->shr_container, val,
1608c8ec8eeaSjose borrego sizeof (si->shr_container));
1609c8ec8eeaSjose borrego free(val);
1610c8ec8eeaSjose borrego }
1611b89a8333Snatalie li - Sun Microsystems - Irvine United States
16129fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States val = smb_shr_sa_getprop(opts, SHOPT_CATIA);
16139fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States if (val != NULL) {
16149fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States smb_shr_sa_setflag(val, si, SMB_SHRF_CATIA);
16158b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States free(val);
16168b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States }
16178b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States
16189fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States val = smb_shr_sa_getprop(opts, SHOPT_ABE);
16199fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States if (val != NULL) {
16209fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States smb_shr_sa_setflag(val, si, SMB_SHRF_ABE);
1621e3f2c991SKeyur Desai free(val);
1622e3f2c991SKeyur Desai }
16239fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States
16249fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States val = smb_shr_sa_getprop(opts, SHOPT_GUEST);
16259fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States if (val != NULL) {
16269fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States smb_shr_sa_setflag(val, si, SMB_SHRF_GUEST_OK);
16279fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States free(val);
1628e3f2c991SKeyur Desai }
1629e3f2c991SKeyur Desai
16309fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States val = smb_shr_sa_getprop(opts, SHOPT_DFSROOT);
16319fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States if (val != NULL) {
16329fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States smb_shr_sa_setflag(val, si, SMB_SHRF_DFSROOT);
16339fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States free(val);
16349fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States }
16359fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States
16369fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States val = smb_shr_sa_getprop(opts, SHOPT_CSC);
16379fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States if (val != NULL) {
16388d7e4166Sjose borrego smb_shr_sa_csc_option(val, si);
16398d7e4166Sjose borrego free(val);
16408d7e4166Sjose borrego }
16418d7e4166Sjose borrego
16429fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States val = smb_shr_sa_getprop(opts, SHOPT_NONE);
16439fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States if (val != NULL) {
1644b89a8333Snatalie li - Sun Microsystems - Irvine United States (void) strlcpy(si->shr_access_none, val,
1645b89a8333Snatalie li - Sun Microsystems - Irvine United States sizeof (si->shr_access_none));
1646b89a8333Snatalie li - Sun Microsystems - Irvine United States free(val);
1647b89a8333Snatalie li - Sun Microsystems - Irvine United States si->shr_flags |= SMB_SHRF_ACC_NONE;
1648b89a8333Snatalie li - Sun Microsystems - Irvine United States }
1649b89a8333Snatalie li - Sun Microsystems - Irvine United States
16509fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States val = smb_shr_sa_getprop(opts, SHOPT_RO);
16519fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States if (val != NULL) {
1652b89a8333Snatalie li - Sun Microsystems - Irvine United States (void) strlcpy(si->shr_access_ro, val,
1653b89a8333Snatalie li - Sun Microsystems - Irvine United States sizeof (si->shr_access_ro));
1654b89a8333Snatalie li - Sun Microsystems - Irvine United States free(val);
1655b89a8333Snatalie li - Sun Microsystems - Irvine United States si->shr_flags |= SMB_SHRF_ACC_RO;
1656b89a8333Snatalie li - Sun Microsystems - Irvine United States }
1657b89a8333Snatalie li - Sun Microsystems - Irvine United States
16589fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States val = smb_shr_sa_getprop(opts, SHOPT_RW);
16599fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States if (val != NULL) {
1660b89a8333Snatalie li - Sun Microsystems - Irvine United States (void) strlcpy(si->shr_access_rw, val,
1661b89a8333Snatalie li - Sun Microsystems - Irvine United States sizeof (si->shr_access_rw));
1662b89a8333Snatalie li - Sun Microsystems - Irvine United States free(val);
1663b89a8333Snatalie li - Sun Microsystems - Irvine United States si->shr_flags |= SMB_SHRF_ACC_RW;
1664b89a8333Snatalie li - Sun Microsystems - Irvine United States }
1665b89a8333Snatalie li - Sun Microsystems - Irvine United States
1666c8ec8eeaSjose borrego sa_free_derived_optionset(opts);
1667c8ec8eeaSjose borrego return (NERR_Success);
1668c8ec8eeaSjose borrego }
1669c8ec8eeaSjose borrego
1670c8ec8eeaSjose borrego /*
16718d7e4166Sjose borrego * Map a client-side caching (CSC) option to the appropriate share
16728d7e4166Sjose borrego * flag. Only one option is allowed; an error will be logged if
16738d7e4166Sjose borrego * multiple options have been specified. We don't need to do anything
16748d7e4166Sjose borrego * about multiple values here because the SRVSVC will not recognize
16758d7e4166Sjose borrego * a value containing multiple flags and will return the default value.
16768d7e4166Sjose borrego *
16778d7e4166Sjose borrego * If the option value is not recognized, it will be ignored: invalid
16788d7e4166Sjose borrego * values will typically be caught and rejected by sharemgr.
16798d7e4166Sjose borrego */
168089dc44ceSjose borrego void
smb_shr_sa_csc_option(const char * value,smb_share_t * si)16818d7e4166Sjose borrego smb_shr_sa_csc_option(const char *value, smb_share_t *si)
16828d7e4166Sjose borrego {
16838d7e4166Sjose borrego int i;
16848d7e4166Sjose borrego
16858d7e4166Sjose borrego for (i = 0; i < (sizeof (cscopt) / sizeof (cscopt[0])); ++i) {
16868d7e4166Sjose borrego if (strcasecmp(value, cscopt[i].value) == 0) {
16878d7e4166Sjose borrego si->shr_flags |= cscopt[i].flag;
16888d7e4166Sjose borrego break;
16898d7e4166Sjose borrego }
16908d7e4166Sjose borrego }
16918d7e4166Sjose borrego
16928d7e4166Sjose borrego switch (si->shr_flags & SMB_SHRF_CSC_MASK) {
16938d7e4166Sjose borrego case 0:
16948d7e4166Sjose borrego case SMB_SHRF_CSC_DISABLED:
16958d7e4166Sjose borrego case SMB_SHRF_CSC_MANUAL:
16968d7e4166Sjose borrego case SMB_SHRF_CSC_AUTO:
16978d7e4166Sjose borrego case SMB_SHRF_CSC_VDO:
16988d7e4166Sjose borrego break;
16998d7e4166Sjose borrego
17008d7e4166Sjose borrego default:
170189dc44ceSjose borrego syslog(LOG_INFO, "csc option conflict: 0x%08x",
170289dc44ceSjose borrego si->shr_flags & SMB_SHRF_CSC_MASK);
17038d7e4166Sjose borrego break;
17048d7e4166Sjose borrego }
17058d7e4166Sjose borrego }
17068d7e4166Sjose borrego
17078d7e4166Sjose borrego /*
170829bd2886SAlan Wright * Return the option name for the first CSC flag (there should be only
170929bd2886SAlan Wright * one) encountered in the share flags.
171029bd2886SAlan Wright */
171129bd2886SAlan Wright char *
smb_shr_sa_csc_name(const smb_share_t * si)171229bd2886SAlan Wright smb_shr_sa_csc_name(const smb_share_t *si)
171329bd2886SAlan Wright {
171429bd2886SAlan Wright int i;
171529bd2886SAlan Wright
171629bd2886SAlan Wright for (i = 0; i < (sizeof (cscopt) / sizeof (cscopt[0])); ++i) {
171729bd2886SAlan Wright if (si->shr_flags & cscopt[i].flag)
171829bd2886SAlan Wright return (cscopt[i].value);
171929bd2886SAlan Wright }
172029bd2886SAlan Wright
172129bd2886SAlan Wright return (NULL);
172229bd2886SAlan Wright }
172329bd2886SAlan Wright
172429bd2886SAlan Wright /*
17259fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States * Takes the value of a boolean share property and set/clear the
17269fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States * specified flag based on the property's value.
17278b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States */
17288b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States void
smb_shr_sa_setflag(const char * value,smb_share_t * si,uint32_t flag)17299fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States smb_shr_sa_setflag(const char *value, smb_share_t *si, uint32_t flag)
17308b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States {
17319fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States if ((strcasecmp(value, "true") == 0) || (strcmp(value, "1") == 0))
17329fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States si->shr_flags |= flag;
17339fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States else
17349fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States si->shr_flags &= ~flag;
173529bd2886SAlan Wright }
173629bd2886SAlan Wright
173729bd2886SAlan Wright /*
17388d7e4166Sjose borrego * looks up sharemgr for the given share (resource) and loads
17398d7e4166Sjose borrego * the definition into cache if lookup is successful
17408d7e4166Sjose borrego */
17418d7e4166Sjose borrego static uint32_t
smb_shr_sa_loadbyname(char * sharename)17428d7e4166Sjose borrego smb_shr_sa_loadbyname(char *sharename)
17438d7e4166Sjose borrego {
17448d7e4166Sjose borrego sa_handle_t handle;
17458d7e4166Sjose borrego sa_share_t share;
17468d7e4166Sjose borrego sa_resource_t resource;
17478d7e4166Sjose borrego uint32_t status;
17488d7e4166Sjose borrego
174989dc44ceSjose borrego if ((handle = smb_shr_sa_enter()) == NULL)
17508d7e4166Sjose borrego return (NERR_InternalError);
17518d7e4166Sjose borrego
17528d7e4166Sjose borrego resource = sa_find_resource(handle, sharename);
17538d7e4166Sjose borrego if (resource == NULL) {
175489dc44ceSjose borrego smb_shr_sa_exit();
17558d7e4166Sjose borrego return (NERR_NetNameNotFound);
17568d7e4166Sjose borrego }
17578d7e4166Sjose borrego
17588d7e4166Sjose borrego share = sa_get_resource_parent(resource);
17598d7e4166Sjose borrego if (share == NULL) {
176089dc44ceSjose borrego smb_shr_sa_exit();
17618d7e4166Sjose borrego return (NERR_InternalError);
17628d7e4166Sjose borrego }
17638d7e4166Sjose borrego
17648d7e4166Sjose borrego status = smb_shr_sa_load(share, resource);
17658d7e4166Sjose borrego
176689dc44ceSjose borrego smb_shr_sa_exit();
17678d7e4166Sjose borrego return (status);
17688d7e4166Sjose borrego }
17698d7e4166Sjose borrego
17708d7e4166Sjose borrego /*
1771c8ec8eeaSjose borrego * ============================================
1772c8ec8eeaSjose borrego * Share publishing functions
1773b89a8333Snatalie li - Sun Microsystems - Irvine United States *
1774b89a8333Snatalie li - Sun Microsystems - Irvine United States * All the functions are private
1775c8ec8eeaSjose borrego * ============================================
1776c8ec8eeaSjose borrego */
1777c8ec8eeaSjose borrego
1778b89a8333Snatalie li - Sun Microsystems - Irvine United States static void
smb_shr_publish(const char * sharename,const char * container)1779b89a8333Snatalie li - Sun Microsystems - Irvine United States smb_shr_publish(const char *sharename, const char *container)
1780b89a8333Snatalie li - Sun Microsystems - Irvine United States {
1781b89a8333Snatalie li - Sun Microsystems - Irvine United States smb_shr_publisher_queue(sharename, container, SMB_SHR_PUBLISH);
1782b89a8333Snatalie li - Sun Microsystems - Irvine United States }
1783b89a8333Snatalie li - Sun Microsystems - Irvine United States
1784b89a8333Snatalie li - Sun Microsystems - Irvine United States static void
smb_shr_unpublish(const char * sharename,const char * container)1785b89a8333Snatalie li - Sun Microsystems - Irvine United States smb_shr_unpublish(const char *sharename, const char *container)
1786b89a8333Snatalie li - Sun Microsystems - Irvine United States {
1787b89a8333Snatalie li - Sun Microsystems - Irvine United States smb_shr_publisher_queue(sharename, container, SMB_SHR_UNPUBLISH);
1788b89a8333Snatalie li - Sun Microsystems - Irvine United States }
1789b89a8333Snatalie li - Sun Microsystems - Irvine United States
1790c8ec8eeaSjose borrego /*
1791b89a8333Snatalie li - Sun Microsystems - Irvine United States * In domain mode, put a share on the publisher queue.
1792b89a8333Snatalie li - Sun Microsystems - Irvine United States * This is a no-op if the smb service is in Workgroup mode.
1793c8ec8eeaSjose borrego */
1794c8ec8eeaSjose borrego static void
smb_shr_publisher_queue(const char * sharename,const char * container,char op)1795b89a8333Snatalie li - Sun Microsystems - Irvine United States smb_shr_publisher_queue(const char *sharename, const char *container, char op)
1796c8ec8eeaSjose borrego {
1797c8ec8eeaSjose borrego smb_shr_pitem_t *item = NULL;
1798c8ec8eeaSjose borrego
1799c8ec8eeaSjose borrego if (container == NULL || *container == '\0')
1800c8ec8eeaSjose borrego return;
1801c8ec8eeaSjose borrego
1802b89a8333Snatalie li - Sun Microsystems - Irvine United States if (smb_config_get_secmode() != SMB_SECMODE_DOMAIN)
1803b89a8333Snatalie li - Sun Microsystems - Irvine United States return;
1804b89a8333Snatalie li - Sun Microsystems - Irvine United States
1805c8ec8eeaSjose borrego (void) mutex_lock(&ad_queue.spq_mtx);
1806c8ec8eeaSjose borrego switch (ad_queue.spq_state) {
1807c8ec8eeaSjose borrego case SMB_SHR_PQS_READY:
1808c8ec8eeaSjose borrego case SMB_SHR_PQS_PUBLISHING:
1809c8ec8eeaSjose borrego break;
1810c8ec8eeaSjose borrego default:
1811c8ec8eeaSjose borrego (void) mutex_unlock(&ad_queue.spq_mtx);
1812c8ec8eeaSjose borrego return;
1813c8ec8eeaSjose borrego }
1814c8ec8eeaSjose borrego (void) mutex_unlock(&ad_queue.spq_mtx);
1815c8ec8eeaSjose borrego
1816b89a8333Snatalie li - Sun Microsystems - Irvine United States if ((item = malloc(sizeof (smb_shr_pitem_t))) == NULL)
1817c8ec8eeaSjose borrego return;
1818c8ec8eeaSjose borrego
1819c8ec8eeaSjose borrego item->spi_op = op;
1820c8ec8eeaSjose borrego (void) strlcpy(item->spi_name, sharename, sizeof (item->spi_name));
1821c8ec8eeaSjose borrego (void) strlcpy(item->spi_container, container,
1822c8ec8eeaSjose borrego sizeof (item->spi_container));
1823c8ec8eeaSjose borrego
1824c8ec8eeaSjose borrego (void) mutex_lock(&ad_queue.spq_mtx);
1825c8ec8eeaSjose borrego list_insert_tail(&ad_queue.spq_list, item);
1826c8ec8eeaSjose borrego (void) cond_signal(&ad_queue.spq_cv);
1827c8ec8eeaSjose borrego (void) mutex_unlock(&ad_queue.spq_mtx);
1828c8ec8eeaSjose borrego }
1829c8ec8eeaSjose borrego
1830b89a8333Snatalie li - Sun Microsystems - Irvine United States /*
1831b89a8333Snatalie li - Sun Microsystems - Irvine United States * Publishing won't be activated if the smb service is running in
1832b89a8333Snatalie li - Sun Microsystems - Irvine United States * Workgroup mode.
1833b89a8333Snatalie li - Sun Microsystems - Irvine United States */
1834c8ec8eeaSjose borrego static int
smb_shr_publisher_start(void)1835c8ec8eeaSjose borrego smb_shr_publisher_start(void)
1836c8ec8eeaSjose borrego {
1837b89a8333Snatalie li - Sun Microsystems - Irvine United States pthread_t publish_thr;
1838c8ec8eeaSjose borrego pthread_attr_t tattr;
1839c8ec8eeaSjose borrego int rc;
1840c8ec8eeaSjose borrego
1841b89a8333Snatalie li - Sun Microsystems - Irvine United States if (smb_config_get_secmode() != SMB_SECMODE_DOMAIN)
1842b89a8333Snatalie li - Sun Microsystems - Irvine United States return (0);
1843b89a8333Snatalie li - Sun Microsystems - Irvine United States
1844c8ec8eeaSjose borrego (void) mutex_lock(&ad_queue.spq_mtx);
1845c8ec8eeaSjose borrego if (ad_queue.spq_state != SMB_SHR_PQS_NOQUEUE) {
1846c8ec8eeaSjose borrego (void) mutex_unlock(&ad_queue.spq_mtx);
1847c8ec8eeaSjose borrego errno = EINVAL;
1848c8ec8eeaSjose borrego return (-1);
1849c8ec8eeaSjose borrego }
1850c8ec8eeaSjose borrego
1851c8ec8eeaSjose borrego list_create(&ad_queue.spq_list, sizeof (smb_shr_pitem_t),
1852c8ec8eeaSjose borrego offsetof(smb_shr_pitem_t, spi_lnd));
1853c8ec8eeaSjose borrego ad_queue.spq_state = SMB_SHR_PQS_READY;
1854c8ec8eeaSjose borrego (void) mutex_unlock(&ad_queue.spq_mtx);
1855c8ec8eeaSjose borrego
1856c8ec8eeaSjose borrego (void) pthread_attr_init(&tattr);
1857c8ec8eeaSjose borrego (void) pthread_attr_setdetachstate(&tattr, PTHREAD_CREATE_DETACHED);
1858b89a8333Snatalie li - Sun Microsystems - Irvine United States rc = pthread_create(&publish_thr, &tattr, smb_shr_publisher, 0);
1859c8ec8eeaSjose borrego (void) pthread_attr_destroy(&tattr);
1860c8ec8eeaSjose borrego
1861c8ec8eeaSjose borrego return (rc);
1862c8ec8eeaSjose borrego }
1863c8ec8eeaSjose borrego
1864c8ec8eeaSjose borrego static void
smb_shr_publisher_stop(void)1865c8ec8eeaSjose borrego smb_shr_publisher_stop(void)
1866c8ec8eeaSjose borrego {
1867b89a8333Snatalie li - Sun Microsystems - Irvine United States if (smb_config_get_secmode() != SMB_SECMODE_DOMAIN)
1868b89a8333Snatalie li - Sun Microsystems - Irvine United States return;
1869b89a8333Snatalie li - Sun Microsystems - Irvine United States
1870c8ec8eeaSjose borrego (void) mutex_lock(&ad_queue.spq_mtx);
1871c8ec8eeaSjose borrego switch (ad_queue.spq_state) {
1872c8ec8eeaSjose borrego case SMB_SHR_PQS_READY:
1873c8ec8eeaSjose borrego case SMB_SHR_PQS_PUBLISHING:
1874c8ec8eeaSjose borrego ad_queue.spq_state = SMB_SHR_PQS_STOPPING;
1875c8ec8eeaSjose borrego (void) cond_signal(&ad_queue.spq_cv);
1876c8ec8eeaSjose borrego break;
1877c8ec8eeaSjose borrego default:
1878c8ec8eeaSjose borrego break;
1879c8ec8eeaSjose borrego }
1880c8ec8eeaSjose borrego (void) mutex_unlock(&ad_queue.spq_mtx);
1881c8ec8eeaSjose borrego }
1882c8ec8eeaSjose borrego
1883c8ec8eeaSjose borrego /*
1884b89a8333Snatalie li - Sun Microsystems - Irvine United States * This is the publisher daemon thread. While running, the thread waits
1885b89a8333Snatalie li - Sun Microsystems - Irvine United States * on a conditional variable until notified that a share needs to be
1886b89a8333Snatalie li - Sun Microsystems - Irvine United States * [un]published or that the thread should be terminated.
1887b89a8333Snatalie li - Sun Microsystems - Irvine United States *
1888b89a8333Snatalie li - Sun Microsystems - Irvine United States * Entries may remain in the outgoing queue if the Active Directory
1889b89a8333Snatalie li - Sun Microsystems - Irvine United States * service is inaccessible, in which case the thread wakes up every 60
1890b89a8333Snatalie li - Sun Microsystems - Irvine United States * seconds to retry.
1891c8ec8eeaSjose borrego */
1892c8ec8eeaSjose borrego /*ARGSUSED*/
1893c8ec8eeaSjose borrego static void *
smb_shr_publisher(void * arg)1894c8ec8eeaSjose borrego smb_shr_publisher(void *arg)
1895c8ec8eeaSjose borrego {
1896c8ec8eeaSjose borrego smb_ads_handle_t *ah;
1897c8ec8eeaSjose borrego smb_shr_pitem_t *shr;
1898c8ec8eeaSjose borrego list_t publist;
1899b89a8333Snatalie li - Sun Microsystems - Irvine United States timestruc_t pubretry;
1900c8ec8eeaSjose borrego char hostname[MAXHOSTNAMELEN];
1901c8ec8eeaSjose borrego
1902c8ec8eeaSjose borrego (void) mutex_lock(&ad_queue.spq_mtx);
1903b89a8333Snatalie li - Sun Microsystems - Irvine United States if (ad_queue.spq_state != SMB_SHR_PQS_READY) {
1904c8ec8eeaSjose borrego (void) mutex_unlock(&ad_queue.spq_mtx);
1905c8ec8eeaSjose borrego return (NULL);
1906c8ec8eeaSjose borrego }
1907b89a8333Snatalie li - Sun Microsystems - Irvine United States ad_queue.spq_state = SMB_SHR_PQS_PUBLISHING;
1908c8ec8eeaSjose borrego (void) mutex_unlock(&ad_queue.spq_mtx);
1909c8ec8eeaSjose borrego
19109fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States (void) smb_gethostname(hostname, MAXHOSTNAMELEN,
19119fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States SMB_CASE_PRESERVE);
1912b89a8333Snatalie li - Sun Microsystems - Irvine United States
1913c8ec8eeaSjose borrego list_create(&publist, sizeof (smb_shr_pitem_t),
1914c8ec8eeaSjose borrego offsetof(smb_shr_pitem_t, spi_lnd));
1915c8ec8eeaSjose borrego
1916c8ec8eeaSjose borrego for (;;) {
1917c8ec8eeaSjose borrego (void) mutex_lock(&ad_queue.spq_mtx);
1918b89a8333Snatalie li - Sun Microsystems - Irvine United States
1919b89a8333Snatalie li - Sun Microsystems - Irvine United States while (list_is_empty(&ad_queue.spq_list) &&
1920b89a8333Snatalie li - Sun Microsystems - Irvine United States (ad_queue.spq_state == SMB_SHR_PQS_PUBLISHING)) {
1921b89a8333Snatalie li - Sun Microsystems - Irvine United States if (list_is_empty(&publist)) {
1922b89a8333Snatalie li - Sun Microsystems - Irvine United States (void) cond_wait(&ad_queue.spq_cv,
1923b89a8333Snatalie li - Sun Microsystems - Irvine United States &ad_queue.spq_mtx);
1924b89a8333Snatalie li - Sun Microsystems - Irvine United States } else {
1925b89a8333Snatalie li - Sun Microsystems - Irvine United States pubretry.tv_sec = 60;
1926b89a8333Snatalie li - Sun Microsystems - Irvine United States pubretry.tv_nsec = 0;
1927b89a8333Snatalie li - Sun Microsystems - Irvine United States (void) cond_reltimedwait(&ad_queue.spq_cv,
1928b89a8333Snatalie li - Sun Microsystems - Irvine United States &ad_queue.spq_mtx, &pubretry);
1929b89a8333Snatalie li - Sun Microsystems - Irvine United States break;
1930b89a8333Snatalie li - Sun Microsystems - Irvine United States }
1931b89a8333Snatalie li - Sun Microsystems - Irvine United States }
1932c8ec8eeaSjose borrego
1933c8ec8eeaSjose borrego if (ad_queue.spq_state != SMB_SHR_PQS_PUBLISHING) {
1934c8ec8eeaSjose borrego (void) mutex_unlock(&ad_queue.spq_mtx);
1935c8ec8eeaSjose borrego break;
1936c8ec8eeaSjose borrego }
1937c8ec8eeaSjose borrego
1938c8ec8eeaSjose borrego /*
1939b89a8333Snatalie li - Sun Microsystems - Irvine United States * Transfer queued items to the local list so that
1940b89a8333Snatalie li - Sun Microsystems - Irvine United States * the mutex can be released.
1941c8ec8eeaSjose borrego */
1942c8ec8eeaSjose borrego while ((shr = list_head(&ad_queue.spq_list)) != NULL) {
1943c8ec8eeaSjose borrego list_remove(&ad_queue.spq_list, shr);
1944c8ec8eeaSjose borrego list_insert_tail(&publist, shr);
1945c8ec8eeaSjose borrego }
1946b89a8333Snatalie li - Sun Microsystems - Irvine United States
1947c8ec8eeaSjose borrego (void) mutex_unlock(&ad_queue.spq_mtx);
1948c8ec8eeaSjose borrego
1949b89a8333Snatalie li - Sun Microsystems - Irvine United States if ((ah = smb_ads_open()) != NULL) {
1950c8ec8eeaSjose borrego smb_shr_publisher_send(ah, &publist, hostname);
1951c8ec8eeaSjose borrego smb_ads_close(ah);
1952c8ec8eeaSjose borrego }
1953c8ec8eeaSjose borrego }
1954b89a8333Snatalie li - Sun Microsystems - Irvine United States
1955b89a8333Snatalie li - Sun Microsystems - Irvine United States (void) mutex_lock(&ad_queue.spq_mtx);
1956b89a8333Snatalie li - Sun Microsystems - Irvine United States smb_shr_publisher_flush(&ad_queue.spq_list);
1957c8ec8eeaSjose borrego list_destroy(&ad_queue.spq_list);
1958c8ec8eeaSjose borrego ad_queue.spq_state = SMB_SHR_PQS_NOQUEUE;
1959c8ec8eeaSjose borrego (void) mutex_unlock(&ad_queue.spq_mtx);
1960c8ec8eeaSjose borrego
1961b89a8333Snatalie li - Sun Microsystems - Irvine United States smb_shr_publisher_flush(&publist);
1962c8ec8eeaSjose borrego list_destroy(&publist);
1963c8ec8eeaSjose borrego return (NULL);
1964c8ec8eeaSjose borrego }
1965c8ec8eeaSjose borrego
1966c8ec8eeaSjose borrego /*
1967b89a8333Snatalie li - Sun Microsystems - Irvine United States * Remove items from the specified queue and [un]publish them.
1968c8ec8eeaSjose borrego */
1969c8ec8eeaSjose borrego static void
smb_shr_publisher_send(smb_ads_handle_t * ah,list_t * publist,const char * host)1970c8ec8eeaSjose borrego smb_shr_publisher_send(smb_ads_handle_t *ah, list_t *publist, const char *host)
1971c8ec8eeaSjose borrego {
1972c8ec8eeaSjose borrego smb_shr_pitem_t *shr;
1973c8ec8eeaSjose borrego
1974c8ec8eeaSjose borrego while ((shr = list_head(publist)) != NULL) {
1975b89a8333Snatalie li - Sun Microsystems - Irvine United States (void) mutex_lock(&ad_queue.spq_mtx);
1976b89a8333Snatalie li - Sun Microsystems - Irvine United States if (ad_queue.spq_state != SMB_SHR_PQS_PUBLISHING) {
1977b89a8333Snatalie li - Sun Microsystems - Irvine United States (void) mutex_unlock(&ad_queue.spq_mtx);
1978b89a8333Snatalie li - Sun Microsystems - Irvine United States return;
1979b89a8333Snatalie li - Sun Microsystems - Irvine United States }
1980b89a8333Snatalie li - Sun Microsystems - Irvine United States (void) mutex_unlock(&ad_queue.spq_mtx);
1981b89a8333Snatalie li - Sun Microsystems - Irvine United States
1982c8ec8eeaSjose borrego list_remove(publist, shr);
1983c8ec8eeaSjose borrego
1984c8ec8eeaSjose borrego if (shr->spi_op == SMB_SHR_PUBLISH)
1985c8ec8eeaSjose borrego (void) smb_ads_publish_share(ah, shr->spi_name,
1986c8ec8eeaSjose borrego NULL, shr->spi_container, host);
1987c8ec8eeaSjose borrego else
1988c8ec8eeaSjose borrego (void) smb_ads_remove_share(ah, shr->spi_name,
1989c8ec8eeaSjose borrego NULL, shr->spi_container, host);
1990b89a8333Snatalie li - Sun Microsystems - Irvine United States
1991b89a8333Snatalie li - Sun Microsystems - Irvine United States free(shr);
1992c8ec8eeaSjose borrego }
1993b89a8333Snatalie li - Sun Microsystems - Irvine United States }
1994b89a8333Snatalie li - Sun Microsystems - Irvine United States
1995b89a8333Snatalie li - Sun Microsystems - Irvine United States /*
1996b89a8333Snatalie li - Sun Microsystems - Irvine United States * Flush all remaining items from the specified list/queue.
1997b89a8333Snatalie li - Sun Microsystems - Irvine United States */
1998b89a8333Snatalie li - Sun Microsystems - Irvine United States static void
smb_shr_publisher_flush(list_t * lst)1999b89a8333Snatalie li - Sun Microsystems - Irvine United States smb_shr_publisher_flush(list_t *lst)
2000b89a8333Snatalie li - Sun Microsystems - Irvine United States {
2001b89a8333Snatalie li - Sun Microsystems - Irvine United States smb_shr_pitem_t *shr;
2002b89a8333Snatalie li - Sun Microsystems - Irvine United States
2003b89a8333Snatalie li - Sun Microsystems - Irvine United States while ((shr = list_head(lst)) != NULL) {
2004b89a8333Snatalie li - Sun Microsystems - Irvine United States list_remove(lst, shr);
2005c8ec8eeaSjose borrego free(shr);
2006c8ec8eeaSjose borrego }
2007c8ec8eeaSjose borrego }
2008743a77edSAlan Wright
2009743a77edSAlan Wright /*
20106d57f833SAlan Wright * If the share path refers to a ZFS file system, add the
20119fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States * .zfs/shares/<share> object and call smb_quota_add_fs()
20129fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States * to initialize quota support for the share.
2013743a77edSAlan Wright */
2014743a77edSAlan Wright static void
smb_shr_zfs_add(smb_share_t * si)2015743a77edSAlan Wright smb_shr_zfs_add(smb_share_t *si)
2016743a77edSAlan Wright {
20176d57f833SAlan Wright libzfs_handle_t *libhd;
20186d57f833SAlan Wright zfs_handle_t *zfshd;
20196d57f833SAlan Wright int ret;
20209fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States char buf[MAXPATHLEN]; /* dataset or mountpoint */
2021743a77edSAlan Wright
20229fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States if (smb_getdataset(si->shr_path, buf, MAXPATHLEN) != 0)
20236d57f833SAlan Wright return;
20246d57f833SAlan Wright
20256d57f833SAlan Wright if ((libhd = libzfs_init()) == NULL)
20266d57f833SAlan Wright return;
20276d57f833SAlan Wright
20289fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States if ((zfshd = zfs_open(libhd, buf, ZFS_TYPE_FILESYSTEM)) == NULL) {
20296d57f833SAlan Wright libzfs_fini(libhd);
20306d57f833SAlan Wright return;
2031743a77edSAlan Wright }
20326d57f833SAlan Wright
20336d57f833SAlan Wright errno = 0;
20349fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States ret = zfs_smb_acl_add(libhd, buf, si->shr_path, si->shr_name);
20356d57f833SAlan Wright if (ret != 0 && errno != EAGAIN && errno != EEXIST)
20366d57f833SAlan Wright syslog(LOG_INFO, "share: failed to add ACL object: %s: %s\n",
20376d57f833SAlan Wright si->shr_name, strerror(errno));
20386d57f833SAlan Wright
20399fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States if (zfs_prop_get(zfshd, ZFS_PROP_MOUNTPOINT, buf, MAXPATHLEN,
20409fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States NULL, NULL, 0, B_FALSE) == 0) {
20419fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States smb_quota_add_fs(buf);
20429fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States }
20439fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States
20449fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States
20456d57f833SAlan Wright zfs_close(zfshd);
20466d57f833SAlan Wright libzfs_fini(libhd);
2047743a77edSAlan Wright }
2048743a77edSAlan Wright
2049743a77edSAlan Wright /*
20506d57f833SAlan Wright * If the share path refers to a ZFS file system, remove the
20519fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States * .zfs/shares/<share> object, and call smb_quota_remove_fs()
20529fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States * to end quota support for the share.
2053743a77edSAlan Wright */
2054743a77edSAlan Wright static void
smb_shr_zfs_remove(smb_share_t * si)2055743a77edSAlan Wright smb_shr_zfs_remove(smb_share_t *si)
2056743a77edSAlan Wright {
20576d57f833SAlan Wright libzfs_handle_t *libhd;
20586d57f833SAlan Wright zfs_handle_t *zfshd;
20596d57f833SAlan Wright int ret;
20609fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States char buf[MAXPATHLEN]; /* dataset or mountpoint */
2061743a77edSAlan Wright
20629fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States if (smb_getdataset(si->shr_path, buf, MAXPATHLEN) != 0)
20636d57f833SAlan Wright return;
20646d57f833SAlan Wright
20656d57f833SAlan Wright if ((libhd = libzfs_init()) == NULL)
20666d57f833SAlan Wright return;
20676d57f833SAlan Wright
20689fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States if ((zfshd = zfs_open(libhd, buf, ZFS_TYPE_FILESYSTEM)) == NULL) {
20696d57f833SAlan Wright libzfs_fini(libhd);
20706d57f833SAlan Wright return;
2071743a77edSAlan Wright }
20726d57f833SAlan Wright
20736d57f833SAlan Wright errno = 0;
20749fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States ret = zfs_smb_acl_remove(libhd, buf, si->shr_path, si->shr_name);
20756d57f833SAlan Wright if (ret != 0 && errno != EAGAIN)
20766d57f833SAlan Wright syslog(LOG_INFO, "share: failed to remove ACL object: %s: %s\n",
20776d57f833SAlan Wright si->shr_name, strerror(errno));
20786d57f833SAlan Wright
20799fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States if (zfs_prop_get(zfshd, ZFS_PROP_MOUNTPOINT, buf, MAXPATHLEN,
20809fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States NULL, NULL, 0, B_FALSE) == 0) {
20819fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States smb_quota_remove_fs(buf);
20829fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States }
20839fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States
20846d57f833SAlan Wright zfs_close(zfshd);
20856d57f833SAlan Wright libzfs_fini(libhd);
2086743a77edSAlan Wright }
2087743a77edSAlan Wright
2088743a77edSAlan Wright /*
20896d57f833SAlan Wright * If the share path refers to a ZFS file system, rename the
2090743a77edSAlan Wright * .zfs/shares/<share> object.
2091743a77edSAlan Wright */
2092743a77edSAlan Wright static void
smb_shr_zfs_rename(smb_share_t * from,smb_share_t * to)2093743a77edSAlan Wright smb_shr_zfs_rename(smb_share_t *from, smb_share_t *to)
2094743a77edSAlan Wright {
20956d57f833SAlan Wright libzfs_handle_t *libhd;
20966d57f833SAlan Wright zfs_handle_t *zfshd;
20976d57f833SAlan Wright int ret;
2098743a77edSAlan Wright char dataset[MAXPATHLEN];
2099743a77edSAlan Wright
21006d57f833SAlan Wright if (smb_getdataset(from->shr_path, dataset, MAXPATHLEN) != 0)
21016d57f833SAlan Wright return;
21026d57f833SAlan Wright
21036d57f833SAlan Wright if ((libhd = libzfs_init()) == NULL)
21046d57f833SAlan Wright return;
21056d57f833SAlan Wright
21066d57f833SAlan Wright if ((zfshd = zfs_open(libhd, dataset, ZFS_TYPE_FILESYSTEM)) == NULL) {
21076d57f833SAlan Wright libzfs_fini(libhd);
21086d57f833SAlan Wright return;
2109743a77edSAlan Wright }
21106d57f833SAlan Wright
21116d57f833SAlan Wright errno = 0;
21126d57f833SAlan Wright ret = zfs_smb_acl_rename(libhd, dataset, from->shr_path,
21136d57f833SAlan Wright from->shr_name, to->shr_name);
21146d57f833SAlan Wright if (ret != 0 && errno != EAGAIN)
21156d57f833SAlan Wright syslog(LOG_INFO, "share: failed to rename ACL object: %s: %s\n",
21166d57f833SAlan Wright from->shr_name, strerror(errno));
21176d57f833SAlan Wright
21186d57f833SAlan Wright zfs_close(zfshd);
21196d57f833SAlan Wright libzfs_fini(libhd);
2120743a77edSAlan Wright }
212129bd2886SAlan Wright
212229bd2886SAlan Wright /*
212329bd2886SAlan Wright * Enable all privileges in the inheritable set to execute command.
212429bd2886SAlan Wright */
212529bd2886SAlan Wright static int
smb_shr_enable_all_privs(void)212629bd2886SAlan Wright smb_shr_enable_all_privs(void)
212729bd2886SAlan Wright {
212829bd2886SAlan Wright priv_set_t *pset;
212929bd2886SAlan Wright
213029bd2886SAlan Wright pset = priv_allocset();
213129bd2886SAlan Wright if (pset == NULL)
213229bd2886SAlan Wright return (-1);
213329bd2886SAlan Wright
213429bd2886SAlan Wright if (getppriv(PRIV_LIMIT, pset)) {
213529bd2886SAlan Wright priv_freeset(pset);
213629bd2886SAlan Wright return (-1);
213729bd2886SAlan Wright }
213829bd2886SAlan Wright
213929bd2886SAlan Wright if (setppriv(PRIV_ON, PRIV_INHERITABLE, pset)) {
214029bd2886SAlan Wright priv_freeset(pset);
214129bd2886SAlan Wright return (-1);
214229bd2886SAlan Wright }
214329bd2886SAlan Wright
214429bd2886SAlan Wright priv_freeset(pset);
214529bd2886SAlan Wright return (0);
214629bd2886SAlan Wright }
214729bd2886SAlan Wright
214829bd2886SAlan Wright /*
214929bd2886SAlan Wright * Tokenizes the command string and returns the list of tokens in an array.
215029bd2886SAlan Wright *
215129bd2886SAlan Wright * Returns NULL if there are no tokens.
215229bd2886SAlan Wright */
215329bd2886SAlan Wright static char **
smb_shr_tokenize_cmd(char * cmdstr)215429bd2886SAlan Wright smb_shr_tokenize_cmd(char *cmdstr)
215529bd2886SAlan Wright {
215629bd2886SAlan Wright char *cmd, *buf, *bp, *value;
215729bd2886SAlan Wright char **argv, **ap;
215829bd2886SAlan Wright int argc, i;
215929bd2886SAlan Wright
216029bd2886SAlan Wright if (cmdstr == NULL || *cmdstr == '\0')
216129bd2886SAlan Wright return (NULL);
216229bd2886SAlan Wright
216329bd2886SAlan Wright if ((buf = malloc(MAXPATHLEN)) == NULL)
216429bd2886SAlan Wright return (NULL);
216529bd2886SAlan Wright
216629bd2886SAlan Wright (void) strlcpy(buf, cmdstr, MAXPATHLEN);
216729bd2886SAlan Wright
216829bd2886SAlan Wright for (argc = 2, bp = cmdstr; *bp != '\0'; ++bp)
216929bd2886SAlan Wright if (*bp == ' ')
217029bd2886SAlan Wright ++argc;
217129bd2886SAlan Wright
217229bd2886SAlan Wright if ((argv = calloc(argc, sizeof (char *))) == NULL) {
217329bd2886SAlan Wright free(buf);
217429bd2886SAlan Wright return (NULL);
217529bd2886SAlan Wright }
217629bd2886SAlan Wright
217729bd2886SAlan Wright ap = argv;
217829bd2886SAlan Wright for (bp = buf, i = 0; i < argc; ++i) {
217929bd2886SAlan Wright do {
218029bd2886SAlan Wright if ((value = strsep(&bp, " ")) == NULL)
218129bd2886SAlan Wright break;
218229bd2886SAlan Wright } while (*value == '\0');
218329bd2886SAlan Wright
218429bd2886SAlan Wright if (value == NULL)
218529bd2886SAlan Wright break;
218629bd2886SAlan Wright
218729bd2886SAlan Wright *ap++ = value;
218829bd2886SAlan Wright }
218929bd2886SAlan Wright
219029bd2886SAlan Wright /* get the filename of the command from the path */
219129bd2886SAlan Wright if ((cmd = strrchr(argv[0], '/')) != NULL)
219229bd2886SAlan Wright (void) strlcpy(argv[0], ++cmd, strlen(argv[0]));
219329bd2886SAlan Wright
219429bd2886SAlan Wright return (argv);
219529bd2886SAlan Wright }
219629bd2886SAlan Wright
219729bd2886SAlan Wright /*
219829bd2886SAlan Wright * Expands the command string for the following substitution tokens:
219929bd2886SAlan Wright *
220029bd2886SAlan Wright * %U - Windows username
220129bd2886SAlan Wright * %D - Name of the domain or workgroup of %U
220229bd2886SAlan Wright * %h - The server hostname
220329bd2886SAlan Wright * %M - The client hostname
220429bd2886SAlan Wright * %L - The server NetBIOS name
220529bd2886SAlan Wright * %m - The client NetBIOS name. This option is only valid for NetBIOS
220629bd2886SAlan Wright * connections (port 139).
220729bd2886SAlan Wright * %I - The IP address of the client machine
220829bd2886SAlan Wright * %i - The local IP address to which the client is connected
220929bd2886SAlan Wright * %S - The name of the share
221029bd2886SAlan Wright * %P - The root directory of the share
221129bd2886SAlan Wright * %u - The UID of the Unix user
221229bd2886SAlan Wright *
221329bd2886SAlan Wright * Returns 0 on success. Otherwise -1.
221429bd2886SAlan Wright */
221529bd2886SAlan Wright static int
smb_shr_expand_subs(char ** cmd_toks,smb_share_t * si,smb_shr_execinfo_t * subs)2216148c5f43SAlan Wright smb_shr_expand_subs(char **cmd_toks, smb_share_t *si, smb_shr_execinfo_t *subs)
221729bd2886SAlan Wright {
221829bd2886SAlan Wright char *fmt, *sub_chr, *ptr;
221929bd2886SAlan Wright boolean_t unknown;
222029bd2886SAlan Wright char hostname[MAXHOSTNAMELEN];
222129bd2886SAlan Wright char ip_str[INET6_ADDRSTRLEN];
222229bd2886SAlan Wright char name[SMB_PI_MAX_HOST];
2223bbf6f00cSJordan Brown smb_wchar_t wbuf[SMB_PI_MAX_HOST];
222429bd2886SAlan Wright int i;
222529bd2886SAlan Wright
222629bd2886SAlan Wright if (cmd_toks == NULL || *cmd_toks == NULL)
222729bd2886SAlan Wright return (-1);
222829bd2886SAlan Wright
222929bd2886SAlan Wright for (i = 1; cmd_toks[i]; i++) {
223029bd2886SAlan Wright fmt = cmd_toks[i];
223129bd2886SAlan Wright if (*fmt == '%') {
223229bd2886SAlan Wright sub_chr = fmt + 1;
223329bd2886SAlan Wright unknown = B_FALSE;
223429bd2886SAlan Wright
223529bd2886SAlan Wright switch (*sub_chr) {
223629bd2886SAlan Wright case 'U':
223729bd2886SAlan Wright ptr = strdup(subs->e_winname);
223829bd2886SAlan Wright break;
223929bd2886SAlan Wright case 'D':
224029bd2886SAlan Wright ptr = strdup(subs->e_userdom);
224129bd2886SAlan Wright break;
224229bd2886SAlan Wright case 'h':
224329bd2886SAlan Wright if (gethostname(hostname, MAXHOSTNAMELEN) != 0)
224429bd2886SAlan Wright unknown = B_TRUE;
224529bd2886SAlan Wright else
224629bd2886SAlan Wright ptr = strdup(hostname);
224729bd2886SAlan Wright break;
224829bd2886SAlan Wright case 'M':
224929bd2886SAlan Wright if (smb_getnameinfo(&subs->e_cli_ipaddr,
225029bd2886SAlan Wright hostname, sizeof (hostname), 0) != 0)
225129bd2886SAlan Wright unknown = B_TRUE;
225229bd2886SAlan Wright else
225329bd2886SAlan Wright ptr = strdup(hostname);
225429bd2886SAlan Wright break;
225529bd2886SAlan Wright case 'L':
225629bd2886SAlan Wright if (smb_getnetbiosname(hostname,
225729bd2886SAlan Wright NETBIOS_NAME_SZ) != 0)
225829bd2886SAlan Wright unknown = B_TRUE;
225929bd2886SAlan Wright else
226029bd2886SAlan Wright ptr = strdup(hostname);
226129bd2886SAlan Wright break;
226229bd2886SAlan Wright case 'm':
226329bd2886SAlan Wright if (*subs->e_cli_netbiosname == '\0')
226429bd2886SAlan Wright unknown = B_TRUE;
226529bd2886SAlan Wright else {
2266bbf6f00cSJordan Brown (void) smb_mbstowcs(wbuf,
226729bd2886SAlan Wright subs->e_cli_netbiosname,
226829bd2886SAlan Wright SMB_PI_MAX_HOST - 1);
226929bd2886SAlan Wright
2270bbf6f00cSJordan Brown if (ucstooem(name, wbuf,
2271bbf6f00cSJordan Brown SMB_PI_MAX_HOST, OEM_CPG_850) == 0)
227229bd2886SAlan Wright (void) strlcpy(name,
227329bd2886SAlan Wright subs->e_cli_netbiosname,
227429bd2886SAlan Wright SMB_PI_MAX_HOST);
227529bd2886SAlan Wright
227629bd2886SAlan Wright ptr = strdup(name);
227729bd2886SAlan Wright }
227829bd2886SAlan Wright break;
227929bd2886SAlan Wright case 'I':
228029bd2886SAlan Wright if (smb_inet_ntop(&subs->e_cli_ipaddr, ip_str,
228129bd2886SAlan Wright SMB_IPSTRLEN(subs->e_cli_ipaddr.a_family))
228229bd2886SAlan Wright != NULL)
228329bd2886SAlan Wright ptr = strdup(ip_str);
228429bd2886SAlan Wright else
228529bd2886SAlan Wright unknown = B_TRUE;
228629bd2886SAlan Wright break;
228729bd2886SAlan Wright case 'i':
228829bd2886SAlan Wright if (smb_inet_ntop(&subs->e_srv_ipaddr, ip_str,
228929bd2886SAlan Wright SMB_IPSTRLEN(subs->e_srv_ipaddr.a_family))
229029bd2886SAlan Wright != NULL)
229129bd2886SAlan Wright ptr = strdup(ip_str);
229229bd2886SAlan Wright else
229329bd2886SAlan Wright unknown = B_TRUE;
229429bd2886SAlan Wright break;
229529bd2886SAlan Wright case 'S':
229629bd2886SAlan Wright ptr = strdup(si->shr_name);
229729bd2886SAlan Wright break;
229829bd2886SAlan Wright case 'P':
229929bd2886SAlan Wright ptr = strdup(si->shr_path);
230029bd2886SAlan Wright break;
230129bd2886SAlan Wright case 'u':
230229bd2886SAlan Wright (void) snprintf(name, sizeof (name), "%u",
230329bd2886SAlan Wright subs->e_uid);
230429bd2886SAlan Wright ptr = strdup(name);
230529bd2886SAlan Wright break;
230629bd2886SAlan Wright default:
230729bd2886SAlan Wright /* unknown sub char */
230829bd2886SAlan Wright unknown = B_TRUE;
230929bd2886SAlan Wright break;
231029bd2886SAlan Wright }
231129bd2886SAlan Wright
231229bd2886SAlan Wright if (unknown)
231329bd2886SAlan Wright ptr = strdup("");
231429bd2886SAlan Wright
231529bd2886SAlan Wright } else /* first char of cmd's arg is not '%' char */
231629bd2886SAlan Wright ptr = strdup("");
231729bd2886SAlan Wright
231829bd2886SAlan Wright cmd_toks[i] = ptr;
231929bd2886SAlan Wright
232029bd2886SAlan Wright if (ptr == NULL) {
232129bd2886SAlan Wright for (i = 1; cmd_toks[i]; i++)
232229bd2886SAlan Wright free(cmd_toks[i]);
232329bd2886SAlan Wright
232429bd2886SAlan Wright return (-1);
232529bd2886SAlan Wright }
232629bd2886SAlan Wright }
232729bd2886SAlan Wright
232829bd2886SAlan Wright return (0);
232929bd2886SAlan Wright }
233029bd2886SAlan Wright
233129bd2886SAlan Wright /*ARGSUSED*/
233229bd2886SAlan Wright static void
smb_shr_sig_abnormal_term(int sig_val)233329bd2886SAlan Wright smb_shr_sig_abnormal_term(int sig_val)
233429bd2886SAlan Wright {
233529bd2886SAlan Wright /*
233629bd2886SAlan Wright * Calling _exit() prevents parent process from getting SIGTERM/SIGINT
233729bd2886SAlan Wright * signal.
233829bd2886SAlan Wright */
233929bd2886SAlan Wright _exit(-1);
234029bd2886SAlan Wright }
234129bd2886SAlan Wright
234229bd2886SAlan Wright /*ARGSUSED*/
234329bd2886SAlan Wright static void
smb_shr_sig_child(int sig_val)234429bd2886SAlan Wright smb_shr_sig_child(int sig_val)
234529bd2886SAlan Wright {
234629bd2886SAlan Wright /*
234729bd2886SAlan Wright * Catch the signal and allow the exit status of the child process
234829bd2886SAlan Wright * to be available for reaping.
234929bd2886SAlan Wright */
235029bd2886SAlan Wright }
235129bd2886SAlan Wright
235229bd2886SAlan Wright /*
2353148c5f43SAlan Wright * This is a temporary function which converts the given smb_share_t
2354148c5f43SAlan Wright * structure to the nvlist format that will be provided by libsharev2
235529bd2886SAlan Wright */
2356148c5f43SAlan Wright static int
smb_shr_encode(smb_share_t * si,nvlist_t ** nvlist)2357148c5f43SAlan Wright smb_shr_encode(smb_share_t *si, nvlist_t **nvlist)
235829bd2886SAlan Wright {
2359148c5f43SAlan Wright nvlist_t *list;
2360148c5f43SAlan Wright nvlist_t *share;
2361148c5f43SAlan Wright nvlist_t *smb;
2362148c5f43SAlan Wright char *csc;
2363148c5f43SAlan Wright int rc = 0;
236429bd2886SAlan Wright
2365148c5f43SAlan Wright *nvlist = NULL;
236629bd2886SAlan Wright
2367148c5f43SAlan Wright if ((rc = nvlist_alloc(&list, NV_UNIQUE_NAME, 0)) != 0)
2368148c5f43SAlan Wright return (rc);
236929bd2886SAlan Wright
2370148c5f43SAlan Wright if ((rc = nvlist_alloc(&share, NV_UNIQUE_NAME, 0)) != 0) {
2371148c5f43SAlan Wright nvlist_free(list);
2372148c5f43SAlan Wright return (rc);
237329bd2886SAlan Wright }
237429bd2886SAlan Wright
2375148c5f43SAlan Wright if ((rc = nvlist_alloc(&smb, NV_UNIQUE_NAME, 0)) != 0) {
2376148c5f43SAlan Wright nvlist_free(share);
2377148c5f43SAlan Wright nvlist_free(list);
2378148c5f43SAlan Wright return (rc);
2379148c5f43SAlan Wright }
2380148c5f43SAlan Wright
2381148c5f43SAlan Wright /* global share properties */
2382148c5f43SAlan Wright rc |= nvlist_add_string(share, "name", si->shr_name);
2383148c5f43SAlan Wright rc |= nvlist_add_string(share, "path", si->shr_path);
2384148c5f43SAlan Wright rc |= nvlist_add_string(share, "desc", si->shr_cmnt);
2385148c5f43SAlan Wright
2386148c5f43SAlan Wright /* smb protocol properties */
2387148c5f43SAlan Wright rc = nvlist_add_string(smb, SHOPT_AD_CONTAINER, si->shr_container);
2388148c5f43SAlan Wright if ((si->shr_flags & SMB_SHRF_ACC_NONE) != 0)
2389148c5f43SAlan Wright rc |= nvlist_add_string(smb, SHOPT_NONE, si->shr_access_none);
2390148c5f43SAlan Wright if ((si->shr_flags & SMB_SHRF_ACC_RO) != 0)
2391148c5f43SAlan Wright rc |= nvlist_add_string(smb, SHOPT_RO, si->shr_access_ro);
2392148c5f43SAlan Wright if ((si->shr_flags & SMB_SHRF_ACC_RW) != 0)
2393148c5f43SAlan Wright rc |= nvlist_add_string(smb, SHOPT_RW, si->shr_access_rw);
2394148c5f43SAlan Wright
2395148c5f43SAlan Wright if ((si->shr_flags & SMB_SHRF_ABE) != 0)
2396148c5f43SAlan Wright rc |= nvlist_add_string(smb, SHOPT_ABE, "true");
2397148c5f43SAlan Wright if ((si->shr_flags & SMB_SHRF_CATIA) != 0)
2398148c5f43SAlan Wright rc |= nvlist_add_string(smb, SHOPT_CATIA, "true");
2399148c5f43SAlan Wright if ((si->shr_flags & SMB_SHRF_GUEST_OK) != 0)
2400148c5f43SAlan Wright rc |= nvlist_add_string(smb, SHOPT_GUEST, "true");
2401148c5f43SAlan Wright if ((si->shr_flags & SMB_SHRF_DFSROOT) != 0)
2402148c5f43SAlan Wright rc |= nvlist_add_string(smb, SHOPT_DFSROOT, "true");
2403148c5f43SAlan Wright
2404148c5f43SAlan Wright if ((si->shr_flags & SMB_SHRF_AUTOHOME) != 0) {
2405148c5f43SAlan Wright rc |= nvlist_add_string(smb, "Autohome", "true");
2406148c5f43SAlan Wright rc |= nvlist_add_uint32(smb, "uid", si->shr_uid);
2407148c5f43SAlan Wright rc |= nvlist_add_uint32(smb, "gid", si->shr_gid);
2408148c5f43SAlan Wright }
2409148c5f43SAlan Wright
2410148c5f43SAlan Wright if ((csc = smb_shr_sa_csc_name(si)) != NULL)
2411148c5f43SAlan Wright rc |= nvlist_add_string(smb, SHOPT_CSC, csc);
2412148c5f43SAlan Wright
2413cb174861Sjoyce mcintosh rc |= nvlist_add_uint32(smb, "type", si->shr_type);
2414cb174861Sjoyce mcintosh
2415148c5f43SAlan Wright rc |= nvlist_add_nvlist(share, "smb", smb);
2416148c5f43SAlan Wright rc |= nvlist_add_nvlist(list, si->shr_name, share);
2417148c5f43SAlan Wright
2418148c5f43SAlan Wright nvlist_free(share);
2419148c5f43SAlan Wright nvlist_free(smb);
2420148c5f43SAlan Wright
2421148c5f43SAlan Wright if (rc != 0)
2422148c5f43SAlan Wright nvlist_free(list);
2423148c5f43SAlan Wright else
2424148c5f43SAlan Wright *nvlist = list;
2425148c5f43SAlan Wright
2426148c5f43SAlan Wright return (rc);
242729bd2886SAlan Wright }
2428