xref: /titanic_51/usr/src/cmd/smbsrv/fksmbd/fksmbd_shr.c (revision aab83bb83be7342f6cfccaed8d5fe0b2f404855d)
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 Nexenta Systems, 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 static void
41 new_share(char *name, char *path, char *comment, int flags)
42 {
43 	smb_share_t si;
44 
45 	bzero(&si, sizeof (si));
46 	(void) strlcpy(si.shr_name, name, MAXNAMELEN);
47 	(void) strlcpy(si.shr_path, path, MAXPATHLEN);
48 	(void) strlcpy(si.shr_cmnt, comment, SMB_SHARE_CMNT_MAX);
49 	si.shr_flags = flags;
50 	if (smb_shr_add(&si) != 0) {
51 		syslog(LOG_ERR, "failed to add test share: %s",
52 		    si.shr_name);
53 	}
54 }
55 
56 /*
57  * This function loads a list of shares from a text file, where
58  * each line of the file contains:
59  * name path comment
60  *
61  * This is only for fksmbd, for testing.
62  */
63 void
64 shr_load_file(char *shr_file)
65 {
66 	char linebuf[1024];
67 	FILE *fp;
68 	char *p;
69 	char *name, *path, *comment;
70 
71 	fp = fopen(shr_file, "r");
72 	if (fp == NULL) {
73 		perror(shr_file);
74 		return;
75 	}
76 
77 	while ((p = fgets(linebuf, sizeof (linebuf), fp)) != NULL) {
78 
79 		name = p;
80 		p = strpbrk(p, " \t\n");
81 		if (p == NULL)
82 			continue;
83 		*p++ = '\0';
84 
85 		path = p;
86 		p = strpbrk(p, " \t\n");
87 		if (p == NULL)
88 			comment = "";
89 		else {
90 			*p++ = '\0';
91 
92 			comment = p;
93 			p = strchr(p, '\n');
94 			if (p != NULL)
95 				*p++ = '\0';
96 		}
97 		new_share(name, path, comment, 0);
98 	}
99 	(void) fclose(fp);
100 }
101 
102 /*ARGSUSED*/
103 void *
104 smb_shr_load(void *args)
105 {
106 	char *shr_file;
107 	_NOTE(ARGUNUSED(args))
108 
109 	/*
110 	 * Not loading the real shares in fksmbd because that
111 	 * tries to enable the network/smb/server service.
112 	 * Also, we won't generally have access to everything
113 	 * in the real shares, because fksmbd runs (only) with
114 	 * the credentials of the user who runs it.
115 	 */
116 	new_share("test", "/var/smb/test", "fksmbd test share",
117 	    SMB_SHRF_GUEST_OK);
118 
119 	/* Allow creating lots of shares for testing. */
120 	shr_file = getenv("FKSMBD_SHARE_FILE");
121 	if (shr_file != NULL)
122 		shr_load_file(shr_file);
123 
124 	return (NULL);
125 }
126