4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright (c) 2011,2012 Turbo Fredriksson <turbo@bayour.com>, based on nfs.c
27 * This is an addition to the zfs device driver to add, modify and remove SMB
28 * shares using the 'net share' command that comes with Samba.
31 * Make sure that samba listens to 'localhost' (127.0.0.1) and that the options
32 * 'usershare max shares' and 'usershare owner only' have been rewied/set
33 * accordingly (see zfs(8) for information).
35 * Once configuration in samba have been done, test that this
36 * works with the following three commands (in this case, my ZFS
37 * filesystem is called 'share/Test1'):
39 * (root)# net -U root -S 127.0.0.1 usershare add Test1 /share/Test1 \
40 * "Comment: /share/Test1" "Everyone:F"
41 * (root)# net usershare list | grep -i test
42 * (root)# net -U root -S 127.0.0.1 usershare delete Test1
44 * The first command will create a user share that gives everyone full access.
45 * To limit the access below that, use normal UNIX commands (chmod, chown etc).
56 #include <sys/types.h>
60 #include "libshare_impl.h"
63 static boolean_t
smb_available(void);
65 static sa_fstype_t
*smb_fstype
;
68 * Retrieve the list of SMB shares.
71 smb_retrieve_shares(void)
74 char file_path
[PATH_MAX
], line
[512], *token
, *key
, *value
;
75 char *dup_value
= NULL
, *path
= NULL
, *comment
= NULL
, *name
= NULL
;
76 char *guest_ok
= NULL
;
78 FILE *share_file_fp
= NULL
;
79 struct dirent
*directory
;
81 smb_share_t
*shares
, *new_shares
= NULL
;
83 /* opendir(), stat() */
84 shares_dir
= opendir(SHARE_DIR
);
85 if (shares_dir
== NULL
)
86 return (SA_SYSTEM_ERR
);
88 /* Go through the directory, looking for shares */
89 while ((directory
= readdir(shares_dir
))) {
90 if (directory
->d_name
[0] == '.')
93 snprintf(file_path
, sizeof (file_path
),
94 "%s/%s", SHARE_DIR
, directory
->d_name
);
96 if (stat(file_path
, &eStat
) == -1) {
101 if (!S_ISREG(eStat
.st_mode
))
104 if ((share_file_fp
= fopen(file_path
, "r")) == NULL
) {
109 name
= strdup(directory
->d_name
);
115 while (fgets(line
, sizeof (line
), share_file_fp
)) {
119 /* Trim trailing new-line character(s). */
120 while (line
[strlen(line
) - 1] == '\r' ||
121 line
[strlen(line
) - 1] == '\n')
122 line
[strlen(line
) - 1] = '\0';
124 /* Split the line in two, separated by '=' */
125 token
= strchr(line
, '=');
133 dup_value
= strdup(value
);
134 if (dup_value
== NULL
) {
139 if (strcmp(key
, "path") == 0) {
142 } else if (strcmp(key
, "comment") == 0) {
145 } else if (strcmp(key
, "guest_ok") == 0) {
147 guest_ok
= dup_value
;
153 if (path
== NULL
|| comment
== NULL
|| guest_ok
== NULL
)
154 continue; /* Incomplete share definition */
156 shares
= (smb_share_t
*)
157 malloc(sizeof (smb_share_t
));
158 if (shares
== NULL
) {
163 (void) strlcpy(shares
->name
, name
,
164 sizeof (shares
->name
));
166 (void) strlcpy(shares
->path
, path
,
167 sizeof (shares
->path
));
169 (void) strlcpy(shares
->comment
, comment
,
170 sizeof (shares
->comment
));
172 shares
->guest_ok
= atoi(guest_ok
);
174 shares
->next
= new_shares
;
188 if (share_file_fp
!= NULL
) {
189 fclose(share_file_fp
);
190 share_file_fp
= NULL
;
203 closedir(shares_dir
);
205 smb_shares
= new_shares
;
211 * Used internally by smb_enable_share to enable sharing for a single host.
214 smb_enable_share_one(const char *sharename
, const char *sharepath
)
216 char *argv
[10], *pos
;
217 char name
[SMB_NAME_MAX
], comment
[SMB_COMMENT_MAX
];
220 /* Support ZFS share name regexp '[[:alnum:]_-.: ]' */
221 strncpy(name
, sharename
, sizeof (name
));
222 name
[sizeof (name
)-1] = '\0';
225 while (*pos
!= '\0') {
238 * CMD: net -S NET_CMD_ARG_HOST usershare add Test1 /share/Test1 \
239 * "Comment" "Everyone:F"
241 snprintf(comment
, sizeof (comment
), "Comment: %s", sharepath
);
243 argv
[0] = NET_CMD_PATH
;
244 argv
[1] = (char *)"-S";
245 argv
[2] = NET_CMD_ARG_HOST
;
246 argv
[3] = (char *)"usershare";
247 argv
[4] = (char *)"add";
248 argv
[5] = (char *)name
;
249 argv
[6] = (char *)sharepath
;
250 argv
[7] = (char *)comment
;
251 argv
[8] = "Everyone:F";
254 rc
= libzfs_run_process(argv
[0], argv
, 0);
256 return (SA_SYSTEM_ERR
);
258 /* Reload the share file */
259 (void) smb_retrieve_shares();
265 * Enables SMB sharing for the specified share.
268 smb_enable_share(sa_share_impl_t impl_share
)
272 if (!smb_available())
273 return (SA_SYSTEM_ERR
);
275 shareopts
= FSINFO(impl_share
, smb_fstype
)->shareopts
;
276 if (shareopts
== NULL
) /* on/off */
277 return (SA_SYSTEM_ERR
);
279 if (strcmp(shareopts
, "off") == 0)
282 /* Magic: Enable (i.e., 'create new') share */
283 return (smb_enable_share_one(impl_share
->dataset
,
284 impl_share
->sharepath
));
288 * Used internally by smb_disable_share to disable sharing for a single host.
291 smb_disable_share_one(const char *sharename
)
296 /* CMD: net -S NET_CMD_ARG_HOST usershare delete Test1 */
297 argv
[0] = NET_CMD_PATH
;
298 argv
[1] = (char *)"-S";
299 argv
[2] = NET_CMD_ARG_HOST
;
300 argv
[3] = (char *)"usershare";
301 argv
[4] = (char *)"delete";
302 argv
[5] = strdup(sharename
);
305 rc
= libzfs_run_process(argv
[0], argv
, 0);
307 return (SA_SYSTEM_ERR
);
313 * Disables SMB sharing for the specified share.
316 smb_disable_share(sa_share_impl_t impl_share
)
318 smb_share_t
*shares
= smb_shares
;
320 if (!smb_available()) {
322 * The share can't possibly be active, so nothing
323 * needs to be done to disable it.
328 while (shares
!= NULL
) {
329 if (strcmp(impl_share
->sharepath
, shares
->path
) == 0)
330 return (smb_disable_share_one(shares
->name
));
332 shares
= shares
->next
;
339 * Checks whether the specified SMB share options are syntactically correct.
342 smb_validate_shareopts(const char *shareopts
)
344 /* TODO: Accept 'name' and sec/acl (?) */
345 if ((strcmp(shareopts
, "off") == 0) || (strcmp(shareopts
, "on") == 0))
348 return (SA_SYNTAX_ERR
);
352 * Checks whether a share is currently active.
355 smb_is_share_active(sa_share_impl_t impl_share
)
357 smb_share_t
*iter
= smb_shares
;
359 if (!smb_available())
362 /* Retrieve the list of (possible) active shares */
363 smb_retrieve_shares();
365 while (iter
!= NULL
) {
366 if (strcmp(impl_share
->sharepath
, iter
->path
) == 0)
376 * Called to update a share's options. A share's options might be out of
377 * date if the share was loaded from disk and the "sharesmb" dataset
378 * property has changed in the meantime. This function also takes care
379 * of re-enabling the share if necessary.
382 smb_update_shareopts(sa_share_impl_t impl_share
, const char *resource
,
383 const char *shareopts
)
386 boolean_t needs_reshare
= B_FALSE
;
390 return (SA_SYSTEM_ERR
);
392 FSINFO(impl_share
, smb_fstype
)->active
=
393 smb_is_share_active(impl_share
);
395 old_shareopts
= FSINFO(impl_share
, smb_fstype
)->shareopts
;
397 if (FSINFO(impl_share
, smb_fstype
)->active
&& old_shareopts
!= NULL
&&
398 strcmp(old_shareopts
, shareopts
) != 0) {
399 needs_reshare
= B_TRUE
;
400 smb_disable_share(impl_share
);
403 shareopts_dup
= strdup(shareopts
);
405 if (shareopts_dup
== NULL
)
406 return (SA_NO_MEMORY
);
408 if (old_shareopts
!= NULL
)
411 FSINFO(impl_share
, smb_fstype
)->shareopts
= shareopts_dup
;
414 smb_enable_share(impl_share
);
420 * Clears a share's SMB options. Used by libshare to
421 * clean up shares that are about to be free()'d.
424 smb_clear_shareopts(sa_share_impl_t impl_share
)
426 free(FSINFO(impl_share
, smb_fstype
)->shareopts
);
427 FSINFO(impl_share
, smb_fstype
)->shareopts
= NULL
;
430 static const sa_share_ops_t smb_shareops
= {
431 .enable_share
= smb_enable_share
,
432 .disable_share
= smb_disable_share
,
434 .validate_shareopts
= smb_validate_shareopts
,
435 .update_shareopts
= smb_update_shareopts
,
436 .clear_shareopts
= smb_clear_shareopts
,
440 * Provides a convenient wrapper for determining SMB availability
447 if (lstat(SHARE_DIR
, &statbuf
) != 0 ||
448 !S_ISDIR(statbuf
.st_mode
))
451 if (access(NET_CMD_PATH
, F_OK
) != 0)
458 * Initializes the SMB functionality of libshare.
461 libshare_smb_init(void)
463 smb_fstype
= register_fstype("smb", &smb_shareops
);