1 /*
2  * Copyright (c) 2000-2002, Boris Popov
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *    This product includes software developed by Boris Popov.
16  * 4. Neither the name of the author nor the names of any co-contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  *
32  * $Id: mount_smbfs.c,v 1.17 2002/04/10 04:17:51 bp Exp $
33  */
34 #include <sys/param.h>
35 #include <sys/stat.h>
36 #include <sys/errno.h>
37 #include <sys/mount.h>
38 
39 #include <stdio.h>
40 #include <string.h>
41 #include <pwd.h>
42 #include <grp.h>
43 #include <unistd.h>
44 #include <ctype.h>
45 #include <stdlib.h>
46 #include <err.h>
47 #include <sysexits.h>
48 
49 #include <cflib.h>
50 
51 #include <netsmb/smb.h>
52 #include <netsmb/smb_conn.h>
53 #include <netsmb/smb_lib.h>
54 
55 #include <vfs/smbfs/smbfs.h>
56 
57 #include <mntopts.h>
58 
59 static char mount_point[MAXPATHLEN + 1];
60 static void usage(void) __dead2;
61 
62 static struct mntopt mopts[] = {
63 	MOPT_STDOPTS,
64 	MOPT_NULL
65 };
66 
67 
68 int
main(int argc,char * argv[])69 main(int argc, char *argv[])
70 {
71 	struct smb_ctx sctx, *ctx = &sctx;
72 	struct smbfs_args mdata;
73 	struct stat st;
74 #ifdef APPLE
75 	extern void dropsuid();
76 	extern int loadsmbvfs();
77 #else
78 	struct vfsconf vfc;
79 #endif /* APPLE */
80 	char *next;
81 	int opt, error, mntflags, caseopt;
82 
83 
84 #ifdef APPLE
85 	dropsuid();
86 #endif /* APPLE */
87 	if (argc == 2) {
88 		if (strcmp(argv[1], "-h") == 0) {
89 			usage();
90 		} else if (strcmp(argv[1], "-v") == 0) {
91 			errx(EX_OK, "version %d.%d.%d", SMBFS_VERSION / 100000,
92 			    (SMBFS_VERSION % 10000) / 1000,
93 			    (SMBFS_VERSION % 1000) / 100);
94 		}
95 	}
96 	if (argc < 3)
97 		usage();
98 
99 #ifdef APPLE
100 	error = loadsmbvfs();
101 #else
102 	error = getvfsbyname(SMBFS_VFSNAME, &vfc);
103 	if (error && vfsisloadable(SMBFS_VFSNAME)) {
104 		if(vfsload(SMBFS_VFSNAME))
105 			err(EX_OSERR, "vfsload("SMBFS_VFSNAME")");
106 		endvfsent();
107 		error = getvfsbyname(SMBFS_VFSNAME, &vfc);
108 	}
109 #endif /* APPLE */
110 	if (error)
111 		errx(EX_OSERR, "SMB filesystem is not available");
112 
113 	if (smb_lib_init() != 0)
114 		exit(1);
115 
116 	mntflags = error = 0;
117 	bzero(&mdata, sizeof(mdata));
118 	mdata.uid = mdata.gid = -1;
119 	caseopt = SMB_CS_NONE;
120 
121 	if (smb_ctx_init(ctx, argc, argv, SMBL_SHARE, SMBL_SHARE, SMB_ST_DISK) != 0)
122 		exit(1);
123 	if (smb_ctx_readrc(ctx) != 0)
124 		exit(1);
125 	if (smb_rc)
126 		rc_close(smb_rc);
127 
128 	while ((opt = getopt(argc, argv, STDPARAM_OPT"c:d:f:g:l:n:o:u:w:")) != -1) {
129 		switch (opt) {
130 		    case STDPARAM_ARGS:
131 			error = smb_ctx_opt(ctx, opt, optarg);
132 			if (error)
133 				exit(1);
134 			break;
135 		    case 'u': {
136 			struct passwd *pwd;
137 
138 			pwd = isdigit(optarg[0]) ?
139 			    getpwuid(atoi(optarg)) : getpwnam(optarg);
140 			if (pwd == NULL)
141 				errx(EX_NOUSER, "unknown user '%s'", optarg);
142 			mdata.uid = pwd->pw_uid;
143 			break;
144 		    }
145 		    case 'g': {
146 			struct group *grp;
147 
148 			grp = isdigit(optarg[0]) ?
149 			    getgrgid(atoi(optarg)) : getgrnam(optarg);
150 			if (grp == NULL)
151 				errx(EX_NOUSER, "unknown group '%s'", optarg);
152 			mdata.gid = grp->gr_gid;
153 			break;
154 		    }
155 		    case 'd':
156 			errno = 0;
157 			mdata.dir_mode = strtol(optarg, &next, 8);
158 			if (errno || *next != 0)
159 				errx(EX_DATAERR, "invalid value for directory mode");
160 			break;
161 		    case 'f':
162 			errno = 0;
163 			mdata.file_mode = strtol(optarg, &next, 8);
164 			if (errno || *next != 0)
165 				errx(EX_DATAERR, "invalid value for file mode");
166 			break;
167 		    case '?':
168 			usage();
169 			/*NOTREACHED*/
170 		    case 'n': {
171 			char *inp, *nsp;
172 
173 			nsp = inp = optarg;
174 			while ((nsp = strsep(&inp, ",;:")) != NULL) {
175 				if (strcasecmp(nsp, "LONG") == 0)
176 					mdata.flags |= SMBFS_MOUNT_NO_LONG;
177 				else
178 					errx(EX_DATAERR, "unknown suboption '%s'", nsp);
179 			}
180 			break;
181 		    };
182 		    case 'o':
183 			getmntopts(optarg, mopts, &mntflags, 0);
184 			break;
185 		    case 'c':
186 			switch (optarg[0]) {
187 			    case 'l':
188 				caseopt |= SMB_CS_LOWER;
189 				break;
190 			    case 'u':
191 				caseopt |= SMB_CS_UPPER;
192 				break;
193 			    default:
194 		    		errx(EX_DATAERR, "invalid suboption '%c' for -c",
195 				    optarg[0]);
196 			}
197 			break;
198 		    default:
199 			usage();
200 		}
201 	}
202 
203 	if (optind == argc - 2)
204 		optind++;
205 
206 	if (optind != argc - 1)
207 		usage();
208 	realpath(argv[optind], mount_point);
209 
210 	if (stat(mount_point, &st) == -1)
211 		err(EX_OSERR, "could not find mount point %s", mount_point);
212 	if (!S_ISDIR(st.st_mode)) {
213 		errno = ENOTDIR;
214 		err(EX_OSERR, "can't mount on %s", mount_point);
215 	}
216 /*
217 	if (smb_getextattr(mount_point, &einfo) == 0)
218 		errx(EX_OSERR, "can't mount on %s twice", mount_point);
219 */
220 	if (mdata.uid == (uid_t)-1)
221 		mdata.uid = st.st_uid;
222 	if (mdata.gid == (gid_t)-1)
223 		mdata.gid = st.st_gid;
224 	if (mdata.file_mode == 0 )
225 		mdata.file_mode = st.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO);
226 	if (mdata.dir_mode == 0) {
227 		mdata.dir_mode = mdata.file_mode;
228 		if (mdata.dir_mode & S_IRUSR)
229 			mdata.dir_mode |= S_IXUSR;
230 		if (mdata.dir_mode & S_IRGRP)
231 			mdata.dir_mode |= S_IXGRP;
232 		if (mdata.dir_mode & S_IROTH)
233 			mdata.dir_mode |= S_IXOTH;
234 	}
235 	/*
236 	 * For now, let connection be private for this mount
237 	 */
238 	ctx->ct_ssn.ioc_opt |= SMBVOPT_PRIVATE;
239 	ctx->ct_ssn.ioc_owner = ctx->ct_sh.ioc_owner = 0; /* root */
240 	ctx->ct_ssn.ioc_group = ctx->ct_sh.ioc_group = mdata.gid;
241 	opt = 0;
242 	if (mdata.dir_mode & S_IXGRP)
243 		opt |= SMBM_EXECGRP;
244 	if (mdata.dir_mode & S_IXOTH)
245 		opt |= SMBM_EXECOTH;
246 	ctx->ct_ssn.ioc_rights |= opt;
247 	ctx->ct_sh.ioc_rights |= opt;
248 	error = smb_ctx_resolve(ctx);
249 	if (error)
250 		exit(1);
251 	error = smb_ctx_lookup(ctx, SMBL_SHARE, SMBLK_CREATE);
252 	if (error) {
253 		exit(1);
254 	}
255 	strcpy(mdata.mount_point,mount_point);
256 	mdata.version = SMBFS_VERSION;
257 	mdata.dev = ctx->ct_fd;
258 	mdata.caseopt = caseopt;
259 	error = mount(SMBFS_VFSNAME, mdata.mount_point, mntflags, (void*)&mdata);
260 	smb_ctx_done(ctx);
261 	if (error) {
262 		smb_error("mount error: %s", error, mdata.mount_point);
263 		exit(1);
264 	}
265 	return 0;
266 }
267 
268 static void
usage(void)269 usage(void)
270 {
271 	fprintf(stderr, "%s\n%s\n%s\n%s\n",
272 	"usage: mount_smbfs [-E cs1:cs2] [-I host] [-L locale] [-M crights:srights]",
273 	"                   [-N] [-O cowner:cgroup/sowner:sgroup] [-R retrycount]",
274 	"                   [-T timeout] [-W workgroup] [-c case] [-d mode] [-f mode]",
275 	"                   [-g gid] [-n opt] [-u uid] //user@server/share node");
276 
277 	exit (1);
278 }
279