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 * $FreeBSD$ 34 */ 35 #include <sys/param.h> 36 #include <sys/stat.h> 37 #include <sys/errno.h> 38 #include <sys/linker.h> 39 #include <sys/mount.h> 40 41 #include <stdio.h> 42 #include <string.h> 43 #include <pwd.h> 44 #include <grp.h> 45 #include <unistd.h> 46 #include <ctype.h> 47 #include <stdlib.h> 48 #include <err.h> 49 #include <sysexits.h> 50 51 #include <cflib.h> 52 53 #include <netsmb/smb.h> 54 #include <netsmb/smb_conn.h> 55 #include <netsmb/smb_lib.h> 56 57 #include <fs/smbfs/smbfs.h> 58 59 #include "mntopts.h" 60 61 static char mount_point[MAXPATHLEN + 1]; 62 static void usage(void); 63 64 static struct mntopt mopts[] = { 65 MOPT_STDOPTS, 66 MOPT_END 67 }; 68 69 static char smbfs_vfsname[] = "smbfs"; 70 71 int 72 main(int argc, char *argv[]) 73 { 74 struct iovec *iov; 75 unsigned int iovlen; 76 struct smb_ctx sctx, *ctx = &sctx; 77 struct stat st; 78 #ifdef APPLE 79 extern void dropsuid(); 80 extern int loadsmbvfs(); 81 #else 82 struct xvfsconf vfc; 83 #endif 84 char *next, *p, *val; 85 int opt, error, mntflags, caseopt, fd; 86 uid_t uid; 87 gid_t gid; 88 mode_t dir_mode, file_mode; 89 char errmsg[255] = { 0 }; 90 91 iov = NULL; 92 iovlen = 0; 93 fd = 0; 94 uid = (uid_t)-1; 95 gid = (gid_t)-1; 96 caseopt = 0; 97 file_mode = 0; 98 dir_mode = 0; 99 100 #ifdef APPLE 101 dropsuid(); 102 #endif 103 if (argc == 2) { 104 if (strcmp(argv[1], "-h") == 0) { 105 usage(); 106 } 107 } 108 if (argc < 3) 109 usage(); 110 111 #ifdef APPLE 112 error = loadsmbvfs(); 113 #else 114 error = getvfsbyname(smbfs_vfsname, &vfc); 115 if (error) { 116 if (kldload(smbfs_vfsname) < 0) 117 err(EX_OSERR, "kldload(%s)", smbfs_vfsname); 118 error = getvfsbyname(smbfs_vfsname, &vfc); 119 } 120 #endif 121 if (error) 122 errx(EX_OSERR, "SMB filesystem is not available"); 123 124 if (smb_lib_init() != 0) 125 exit(1); 126 127 mntflags = error = 0; 128 129 caseopt = SMB_CS_NONE; 130 131 if (smb_ctx_init(ctx, argc, argv, SMBL_SHARE, SMBL_SHARE, SMB_ST_DISK) != 0) 132 exit(1); 133 if (smb_ctx_readrc(ctx) != 0) 134 exit(1); 135 if (smb_rc) 136 rc_close(smb_rc); 137 138 while ((opt = getopt(argc, argv, STDPARAM_OPT"c:d:f:g:l:n:o:u:w:")) != -1) { 139 switch (opt) { 140 case STDPARAM_ARGS: 141 error = smb_ctx_opt(ctx, opt, optarg); 142 if (error) 143 exit(1); 144 break; 145 case 'u': { 146 struct passwd *pwd; 147 148 pwd = isdigit(optarg[0]) ? 149 getpwuid(atoi(optarg)) : getpwnam(optarg); 150 if (pwd == NULL) 151 errx(EX_NOUSER, "unknown user '%s'", optarg); 152 uid = pwd->pw_uid; 153 break; 154 } 155 case 'g': { 156 struct group *grp; 157 158 grp = isdigit(optarg[0]) ? 159 getgrgid(atoi(optarg)) : getgrnam(optarg); 160 if (grp == NULL) 161 errx(EX_NOUSER, "unknown group '%s'", optarg); 162 gid = grp->gr_gid; 163 break; 164 } 165 case 'd': 166 errno = 0; 167 dir_mode = strtol(optarg, &next, 8); 168 if (errno || *next != 0) 169 errx(EX_DATAERR, "invalid value for directory mode"); 170 break; 171 case 'f': 172 errno = 0; 173 file_mode = strtol(optarg, &next, 8); 174 if (errno || *next != 0) 175 errx(EX_DATAERR, "invalid value for file mode"); 176 break; 177 case '?': 178 usage(); 179 /*NOTREACHED*/ 180 case 'n': { 181 char *inp, *nsp; 182 183 nsp = inp = optarg; 184 while ((nsp = strsep(&inp, ",;:")) != NULL) { 185 if (strcasecmp(nsp, "LONG") == 0) { 186 build_iovec(&iov, &iovlen, 187 "nolong", NULL, 0); 188 } else { 189 errx(EX_DATAERR, 190 "unknown suboption '%s'", nsp); 191 } 192 } 193 break; 194 }; 195 case 'o': 196 getmntopts(optarg, mopts, &mntflags, 0); 197 p = strchr(optarg, '='); 198 val = NULL; 199 if (p != NULL) { 200 *p = '\0'; 201 val = p + 1; 202 } 203 build_iovec(&iov, &iovlen, optarg, val, (size_t)-1); 204 break; 205 case 'c': 206 switch (optarg[0]) { 207 case 'l': 208 caseopt |= SMB_CS_LOWER; 209 break; 210 case 'u': 211 caseopt |= SMB_CS_UPPER; 212 break; 213 default: 214 errx(EX_DATAERR, "invalid suboption '%c' for -c", 215 optarg[0]); 216 } 217 break; 218 default: 219 usage(); 220 } 221 } 222 223 if (optind == argc - 2) 224 optind++; 225 226 if (optind != argc - 1) 227 usage(); 228 realpath(argv[optind], mount_point); 229 230 if (stat(mount_point, &st) == -1) 231 err(EX_OSERR, "could not find mount point %s", mount_point); 232 if (!S_ISDIR(st.st_mode)) { 233 errno = ENOTDIR; 234 err(EX_OSERR, "can't mount on %s", mount_point); 235 } 236 /* 237 if (smb_getextattr(mount_point, &einfo) == 0) 238 errx(EX_OSERR, "can't mount on %s twice", mount_point); 239 */ 240 if (uid == (uid_t)-1) 241 uid = st.st_uid; 242 if (gid == (gid_t)-1) 243 gid = st.st_gid; 244 if (file_mode == 0 ) 245 file_mode = st.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO); 246 if (dir_mode == 0) { 247 dir_mode = file_mode; 248 if (dir_mode & S_IRUSR) 249 dir_mode |= S_IXUSR; 250 if (dir_mode & S_IRGRP) 251 dir_mode |= S_IXGRP; 252 if (dir_mode & S_IROTH) 253 dir_mode |= S_IXOTH; 254 } 255 /* 256 * For now, let connection be private for this mount 257 */ 258 ctx->ct_ssn.ioc_opt |= SMBVOPT_PRIVATE; 259 ctx->ct_ssn.ioc_owner = ctx->ct_sh.ioc_owner = 0; /* root */ 260 ctx->ct_ssn.ioc_group = ctx->ct_sh.ioc_group = gid; 261 opt = 0; 262 if (dir_mode & S_IXGRP) 263 opt |= SMBM_EXECGRP; 264 if (dir_mode & S_IXOTH) 265 opt |= SMBM_EXECOTH; 266 ctx->ct_ssn.ioc_rights |= opt; 267 ctx->ct_sh.ioc_rights |= opt; 268 error = smb_ctx_resolve(ctx); 269 if (error) 270 exit(1); 271 error = smb_ctx_lookup(ctx, SMBL_SHARE, SMBLK_CREATE); 272 if (error) { 273 exit(1); 274 } 275 276 fd = ctx->ct_fd; 277 278 build_iovec(&iov, &iovlen, "fstype", strdup("smbfs"), -1); 279 build_iovec(&iov, &iovlen, "fspath", mount_point, -1); 280 build_iovec_argf(&iov, &iovlen, "fd", "%d", fd); 281 build_iovec(&iov, &iovlen, "mountpoint", mount_point, -1); 282 build_iovec_argf(&iov, &iovlen, "uid", "%d", uid); 283 build_iovec_argf(&iov, &iovlen, "gid", "%d", gid); 284 build_iovec_argf(&iov, &iovlen, "file_mode", "%d", file_mode); 285 build_iovec_argf(&iov, &iovlen, "dir_mode", "%d", dir_mode); 286 build_iovec_argf(&iov, &iovlen, "caseopt", "%d", caseopt); 287 build_iovec(&iov, &iovlen, "errmsg", errmsg, sizeof errmsg); 288 289 error = nmount(iov, iovlen, mntflags); 290 smb_ctx_done(ctx); 291 if (error) { 292 smb_error("mount error: %s %s", error, mount_point, errmsg); 293 exit(1); 294 } 295 return 0; 296 } 297 298 static void 299 usage(void) 300 { 301 fprintf(stderr, "%s\n%s\n%s\n%s\n", 302 "usage: mount_smbfs [-E cs1:cs2] [-I host] [-L locale] [-M crights:srights]", 303 " [-N] [-O cowner:cgroup/sowner:sgroup] [-R retrycount]", 304 " [-T timeout] [-W workgroup] [-c case] [-d mode] [-f mode]", 305 " [-g gid] [-n opt] [-u uid] [-U username] //user@server/share node"); 306 307 exit (1); 308 } 309