2 * Copyright (c) 2000-2001, Boris Popov
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
32 * $Id: mount_smbfs.c,v 1.28.44.2 2005/06/02 00:55:41 lindak Exp $
36 * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
37 * Use is subject to license terms.
39 * Copyright 2012 Nexenta Systems, Inc. All rights reserved.
55 #include <priv_utils.h>
57 #include <sys/types.h>
59 #include <sys/errno.h>
60 #include <sys/mount.h>
61 #include <sys/mntent.h>
62 #include <sys/mnttab.h>
64 #include <sys/fs/smbfs_mount.h>
66 /* This needs to know ctx->ct_dev_fd, etc. */
67 #include <netsmb/smb_lib.h>
72 static char mount_point
[MAXPATHLEN
+ 1];
73 static void usage(void);
74 static int setsubopt(smb_ctx_t
*, struct smbfs_args
*, char *);
76 const char * const optlist
[] = {
78 /* Generic VFS options. */
89 #define OPT_NODEVICES 5
93 #define OPT_NOSETUID 7
101 #define OPT_NOXATTR 11
104 /* Sort of generic (from NFS) */
107 #define OPT_ACTIMEO 13
109 #define OPT_ACREGMIN 14
111 #define OPT_ACREGMAX 15
113 #define OPT_ACDIRMIN 16
115 #define OPT_ACDIRMAX 17
118 /* smbfs-specifis options */
119 #define OPT_DOMAIN 18
127 #define OPT_DIRPERMS 22
129 #define OPT_FILEPERMS 23
131 #define OPT_NOPROMPT 24
141 static int Oflg
= 0; /* Overlay mounts */
142 static int qflg
= 0; /* quiet - don't print warnings on bad options */
143 static int noprompt
= 0; /* don't prompt for password */
145 /* Note: smbfs uses _both_ kinds of options. */
146 static int mntflags
= MS_DATA
| MS_OPTIONSTR
;
148 #define EX_OK 0 /* normal */
149 #define EX_OPT 1 /* bad options, usage, etc */
150 #define EX_MNT 2 /* mount point problems, etc */
151 #define RET_ERR 3 /* later errors */
153 #define SERVICE "svc:/network/smb/client:default"
155 struct smbfs_args mdata
;
159 * Initialize this with "rw" just to have something there,
160 * so we don't have to decide whether to add a comma when
161 * we strcat another option. Note the "rw" may be changed
162 * to an "ro" by option processing.
164 char optbuf
[MAX_MNTOPT_STR
] = "rw";
165 char special
[MAXPATHLEN
];
168 main(int argc
, char *argv
[])
170 struct smb_ctx
*ctx
= NULL
;
172 int opt
, error
, err2
;
173 static char *fstype
= MNTTYPE_SMBFS
;
176 (void) setlocale(LC_ALL
, "");
177 #if !defined(TEXT_DOMAIN)
178 #define TEXT_DOMAIN "SYS_TEST"
180 (void) textdomain(TEXT_DOMAIN
);
183 * Normal users are allowed to run "mount -F smbfs ..."
184 * to mount on a directory they own. To allow that, this
185 * program is installed setuid root, and it adds SYS_MOUNT
186 * privilege here (if needed), and then restores the user's
187 * normal privileges. When root runs this, it's a no-op.
189 if (__init_suid_priv(0, PRIV_SYS_MOUNT
, (char *)NULL
) < 0) {
190 (void) fprintf(stderr
,
191 gettext("Insufficient privileges, "
192 "%s must be set-uid root\n"), argv
[0]);
197 if (strcmp(argv
[1], "-h") == 0) {
199 } else if (strcmp(argv
[1], "-v") == 0) {
200 errx(EX_OK
, gettext("version %d.%d.%d"),
201 SMBFS_VERSION
/ 100000,
202 (SMBFS_VERSION
% 10000) / 1000,
203 (SMBFS_VERSION
% 1000) / 100);
209 /* Debugging support. */
210 if ((env
= getenv("SMBFS_DEBUG")) != NULL
) {
211 smb_debug
= atoi(env
);
216 error
= smb_lib_init();
220 mnt
.mnt_mntopts
= optbuf
;
222 bzero(&mdata
, sizeof (mdata
));
223 mdata
.version
= SMBFS_VERSION
; /* smbfs mount version */
224 mdata
.uid
= (uid_t
)-1;
225 mdata
.gid
= (gid_t
)-1;
227 error
= smb_ctx_alloc(&ctx
);
232 * Parse the UNC path so we have the server (etc.)
233 * that we need during rcfile+sharectl parsing.
237 error
= smb_ctx_parseunc(ctx
, argv
[argc
- 2],
238 SMBL_SHARE
, SMBL_SHARE
, USE_DISKDEV
, NULL
);
242 error
= smb_ctx_readrc(ctx
);
246 while ((opt
= getopt(argc
, argv
, "ro:Oq")) != -1) {
257 mntflags
|= MS_RDONLY
;
261 char *nextopt
, *comma
, *sopt
;
264 for (sopt
= optarg
; sopt
!= NULL
; sopt
= nextopt
) {
265 comma
= strchr(sopt
, ',');
271 ret
= setsubopt(ctx
, &mdata
, sopt
);
274 /* undo changes to optarg */
288 mntflags
|= MS_OVERLAY
;
290 if (mntflags
& MS_RDONLY
) {
292 /* convert "rw"->"ro" */
293 if (p
= strstr(optbuf
, "rw")) {
294 if (*(p
+2) == ',' || *(p
+2) == '\0')
299 if (optind
+ 2 != argc
)
302 (void) snprintf(special
, sizeof (special
), "//%s/%s",
303 ctx
->ct_fullserver
, ctx
->ct_origshare
);
305 mnt
.mnt_special
= special
;
306 mnt
.mnt_mountp
= argv
[optind
+1];
308 if ((realpath(argv
[optind
+1], mount_point
) == NULL
) ||
309 (stat(mount_point
, &st
) == -1)) {
310 err(EX_MNT
, gettext("could not find mount point %s"),
313 if (!S_ISDIR(st
.st_mode
)) {
315 err(EX_MNT
, gettext("can't mount on %s"), mount_point
);
319 * Fill in mdata defaults.
321 if (mdata
.uid
== (uid_t
)-1)
322 mdata
.uid
= getuid();
323 if (mdata
.gid
== (gid_t
)-1)
324 mdata
.gid
= getgid();
325 if (mdata
.file_mode
== 0)
326 mdata
.file_mode
= S_IRWXU
;
327 if (mdata
.dir_mode
== 0) {
328 mdata
.dir_mode
= mdata
.file_mode
;
329 if (mdata
.dir_mode
& S_IRUSR
)
330 mdata
.dir_mode
|= S_IXUSR
;
331 if (mdata
.dir_mode
& S_IRGRP
)
332 mdata
.dir_mode
|= S_IXGRP
;
333 if (mdata
.dir_mode
& S_IROTH
)
334 mdata
.dir_mode
|= S_IXOTH
;
337 ctx
->ct_ssn
.ssn_owner
= SMBM_ANY_OWNER
;
339 ctx
->ct_flags
|= SMBCF_NOPWD
;
342 * Resolve the server address,
343 * setup derived defaults.
345 error
= smb_ctx_resolve(ctx
);
350 * Have server, share, etc. from above:
351 * smb_ctx_scan_argv, option settings.
352 * Get the session and tree.
355 error
= smb_ctx_get_ssn(ctx
);
356 if (error
== EAUTH
&& noprompt
== 0) {
357 err2
= smb_get_authentication(ctx
);
362 smb_error(gettext("//%s: login failed"),
363 error
, ctx
->ct_fullserver
);
367 error
= smb_ctx_get_tree(ctx
);
369 smb_error(gettext("//%s/%s: tree connect failed"),
370 error
, ctx
->ct_fullserver
, ctx
->ct_origshare
);
375 * Have tree connection, now mount it.
377 mdata
.devfd
= ctx
->ct_dev_fd
;
379 /* Need sys_mount privilege for the mount call. */
380 (void) __priv_bracket(PRIV_ON
);
381 err2
= mount(mnt
.mnt_special
, mnt
.mnt_mountp
,
382 mntflags
, fstype
, &mdata
, sizeof (mdata
),
383 mnt
.mnt_mntopts
, MAX_MNTOPT_STR
);
384 (void) __priv_bracket(PRIV_OFF
);
387 if (errno
!= ENOENT
) {
388 err(EX_MNT
, gettext("mount_smbfs: %s"),
392 if (stat(mnt
.mnt_mountp
, &sb
) < 0 &&
394 err(EX_MNT
, gettext("mount_smbfs: %s"),
397 err(EX_MNT
, gettext("mount_smbfs: %s"),
406 #define bad(val) (val == NULL || !isdigit(*val))
409 setsubopt(smb_ctx_t
*ctx
, struct smbfs_args
*mdatap
, char *subopt
)
411 char *equals
, *optarg
;
419 equals
= strchr(subopt
, '=');
426 for (index
= 0; optlist
[index
] != NULL
; index
++) {
427 if (strcmp(subopt
, optlist
[index
]) == 0)
432 * Note: if the option was unknown, index will
433 * point to the NULL at the end of optlist[],
434 * and we'll take the switch default.
452 * These options are handled via the
453 * generic option string mechanism.
454 * None of these take an optarg.
458 (void) strlcat(optbuf
, ",", sizeof (optbuf
));
459 if (strlcat(optbuf
, subopt
, sizeof (optbuf
)) >=
462 warnx(gettext("option string too long"));
468 * OPT_RO, OPT_RW, are actually generic too,
469 * but we use the mntflags for these, and
470 * then update the options string later.
473 mntflags
|= MS_RDONLY
;
476 mntflags
&= ~MS_RDONLY
;
480 * NFS-derived options for attribute cache
481 * handling (disable, set min/max timeouts)
484 mdatap
->flags
|= SMBFS_MF_NOAC
;
489 val
= strtol(optarg
, &p
, 10);
490 if (errno
|| *p
!= 0)
492 mdatap
->acdirmin
= mdatap
->acregmin
= val
;
493 mdatap
->acdirmax
= mdatap
->acregmax
= val
;
494 mdatap
->flags
|= SMBFS_MF_ACDIRMAX
;
495 mdatap
->flags
|= SMBFS_MF_ACREGMAX
;
496 mdatap
->flags
|= SMBFS_MF_ACDIRMIN
;
497 mdatap
->flags
|= SMBFS_MF_ACREGMIN
;
502 val
= strtol(optarg
, &p
, 10);
503 if (errno
|| *p
!= 0)
505 mdatap
->acregmin
= val
;
506 mdatap
->flags
|= SMBFS_MF_ACREGMIN
;
511 val
= strtol(optarg
, &p
, 10);
512 if (errno
|| *p
!= 0)
514 mdatap
->acregmax
= val
;
515 mdatap
->flags
|= SMBFS_MF_ACREGMAX
;
520 val
= strtol(optarg
, &p
, 10);
521 if (errno
|| *p
!= 0)
523 mdatap
->acdirmin
= val
;
524 mdatap
->flags
|= SMBFS_MF_ACDIRMIN
;
529 val
= strtol(optarg
, &p
, 10);
530 if (errno
|| *p
!= 0)
532 mdatap
->acdirmax
= val
;
533 mdatap
->flags
|= SMBFS_MF_ACDIRMAX
;
537 * SMBFS-specific options. Some of these
538 * don't go through the mount system call,
539 * but just set libsmbfs options.
542 if (smb_ctx_setdomain(ctx
, optarg
, B_TRUE
) != 0)
547 if (smb_ctx_setuser(ctx
, optarg
, B_TRUE
) != 0)
552 pwd
= isdigit(optarg
[0]) ?
553 getpwuid(atoi(optarg
)) : getpwnam(optarg
);
556 warnx(gettext("unknown user '%s'"), optarg
);
559 mdatap
->uid
= pwd
->pw_uid
;
564 grp
= isdigit(optarg
[0]) ?
565 getgrgid(atoi(optarg
)) : getgrnam(optarg
);
568 warnx(gettext("unknown group '%s'"), optarg
);
571 mdatap
->gid
= grp
->gr_gid
;
577 val
= strtol(optarg
, &p
, 8);
578 if (errno
|| *p
!= 0)
580 mdatap
->dir_mode
= val
;
585 val
= strtol(optarg
, &p
, 8);
586 if (errno
|| *p
!= 0)
588 mdatap
->file_mode
= val
;
598 warnx(gettext("unknown option %s"), subopt
);
604 warnx(gettext("invalid value for %s"), subopt
);
609 /* Undo changes made to subopt */
619 (void) fprintf(stderr
, "%s\n",
620 gettext("usage: mount -F smbfs [-Orq] [-o option[,option]]"
621 " //[workgroup;][user[:password]@]server[/share] path"));