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