1 /*
2 * This file and its contents are supplied under the terms of the
3 * Common Development and Distribution License ("CDDL"), version 1.0.
4 * You may only use this file in accordance with the terms of version
5 * 1.0 of the CDDL.
6 *
7 * A full copy of the text of the CDDL should have accompanied this
8 * source. A copy of the CDDL is also available via the Internet at
9 * http://www.illumos.org/license/CDDL.
10 */
11
12 /*
13 * Copyright 2014-2021 Tintri by DDN, Inc. All rights reserved.
14 */
15
16 /*
17 * Replace the smb_shr_load() function in libmlsvc, because
18 * fksmbd doesn't want the real shares known by libshare,
19 * instead preferring its own (fake) list of shares.
20 */
21
22 #include <sys/types.h>
23
24
25 #include <errno.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <strings.h>
29 #include <syslog.h>
30 #include <libshare.h>
31 #include <unistd.h>
32 #include <note.h>
33
34 #include <smbsrv/libsmb.h>
35 #include <smbsrv/libsmbns.h>
36 #include <smbsrv/libmlsvc.h>
37 #include <smbsrv/smb_share.h>
38 #include <smbsrv/smb.h>
39
40 #define SMB_SHRF_ENCRYPT 0x8000 // SMB2_SHAREFLAG_ENCRYPT_DATA?
41
42 static void
new_share(char * name,char * path,char * comment,int flags)43 new_share(char *name, char *path, char *comment, int flags)
44 {
45 smb_share_t si;
46
47 bzero(&si, sizeof (si));
48 (void) strlcpy(si.shr_name, name, MAXNAMELEN);
49 (void) strlcpy(si.shr_path, path, MAXPATHLEN);
50 (void) strlcpy(si.shr_cmnt, comment, SMB_SHARE_CMNT_MAX);
51 if (flags & SMB_SHRF_ENCRYPT) {
52 flags &= ~SMB_SHRF_ENCRYPT;
53 si.shr_encrypt = SMB_CONFIG_REQUIRED;
54 }
55 si.shr_flags = flags;
56
57
58 if (smb_shr_add(&si) != 0) {
59 syslog(LOG_ERR, "failed to add test share: %s",
60 si.shr_name);
61 }
62 }
63
64 /*
65 * This function loads a list of shares from a text file, where
66 * each line of the file contains:
67 * name path comment
68 *
69 * This is only for fksmbd, for testing.
70 */
71 void
shr_load_file(char * shr_file)72 shr_load_file(char *shr_file)
73 {
74 char linebuf[1024];
75 FILE *fp;
76 char *p;
77 char *name, *path, *comment;
78
79 fp = fopen(shr_file, "r");
80 if (fp == NULL) {
81 perror(shr_file);
82 return;
83 }
84
85 while ((p = fgets(linebuf, sizeof (linebuf), fp)) != NULL) {
86
87 name = p;
88 p = strpbrk(p, " \t\n");
89 if (p == NULL)
90 continue;
91 *p++ = '\0';
92
93 path = p;
94 p = strpbrk(p, " \t\n");
95 if (p == NULL)
96 comment = "";
97 else {
98 *p++ = '\0';
99
100 comment = p;
101 p = strchr(p, '\n');
102 if (p != NULL)
103 *p++ = '\0';
104 }
105 new_share(name, path, comment, 0);
106 }
107 (void) fclose(fp);
108 }
109
110 /*ARGSUSED*/
111 void *
smb_shr_load(void * args)112 smb_shr_load(void *args)
113 {
114 char *shr_file;
115 _NOTE(ARGUNUSED(args))
116
117 /*
118 * Not loading the real shares in fksmbd because that
119 * tries to enable the network/smb/server service.
120 * Also, we won't generally have access to everything
121 * in the real shares, because fksmbd runs (only) with
122 * the credentials of the user who runs it.
123 */
124 new_share("test", "/var/smb/test", "fksmbd test share",
125 SMB_SHRF_GUEST_OK);
126 new_share("testca", "/var/smb/test", "fksmbd test CA share",
127 SMB_SHRF_CA);
128 new_share("teste", "/var/smb/test", "fksmbd test encrypted share",
129 SMB_SHRF_ENCRYPT);
130
131 /* Allow creating lots of shares for testing. */
132 shr_file = getenv("FKSMBD_SHARE_FILE");
133 if (shr_file != NULL)
134 shr_load_file(shr_file);
135
136 return (NULL);
137 }
138
139 void
smb_shr_load_execinfo()140 smb_shr_load_execinfo()
141 {
142 }
143
144 void
smb_shr_unload()145 smb_shr_unload()
146 {
147 }
148