xref: /openbsd-src/sbin/mount_ntfs/mount_ntfs.c (revision c6821aa4c746ce60a09794faa5a08a9269df0c37)
1*c6821aa4Stb /* $OpenBSD: mount_ntfs.c,v 1.19 2022/08/20 07:03:24 tb Exp $ */
28a2c668cStedu /* $NetBSD: mount_ntfs.c,v 1.9 2003/05/03 15:37:08 christos Exp $ */
38a2c668cStedu 
48a2c668cStedu /*
58a2c668cStedu  * Copyright (c) 1994 Christopher G. Demetriou
68a2c668cStedu  * Copyright (c) 1999 Semen Ustimenko
78a2c668cStedu  * All rights reserved.
88a2c668cStedu  *
98a2c668cStedu  * Redistribution and use in source and binary forms, with or without
108a2c668cStedu  * modification, are permitted provided that the following conditions
118a2c668cStedu  * are met:
128a2c668cStedu  * 1. Redistributions of source code must retain the above copyright
138a2c668cStedu  *    notice, this list of conditions and the following disclaimer.
148a2c668cStedu  * 2. Redistributions in binary form must reproduce the above copyright
158a2c668cStedu  *    notice, this list of conditions and the following disclaimer in the
168a2c668cStedu  *    documentation and/or other materials provided with the distribution.
178a2c668cStedu  * 3. All advertising materials mentioning features or use of this software
188a2c668cStedu  *    must display the following acknowledgement:
198a2c668cStedu  *      This product includes software developed by Christopher G. Demetriou.
208a2c668cStedu  * 4. The name of the author may not be used to endorse or promote products
218a2c668cStedu  *    derived from this software without specific prior written permission
228a2c668cStedu  *
238a2c668cStedu  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
248a2c668cStedu  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
258a2c668cStedu  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
268a2c668cStedu  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
278a2c668cStedu  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
288a2c668cStedu  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
298a2c668cStedu  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
308a2c668cStedu  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
318a2c668cStedu  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
328a2c668cStedu  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
338a2c668cStedu  *
348a2c668cStedu  * Id: mount_ntfs.c,v 1.1.1.1 1999/02/03 03:51:19 semenu Exp
358a2c668cStedu  */
368a2c668cStedu 
37b9fc9a72Sderaadt #include <sys/types.h>
388a2c668cStedu #include <sys/mount.h>
398a2c668cStedu #include <sys/stat.h>
402e4c3f94Stedu 
418a2c668cStedu #include <err.h>
428a2c668cStedu #include <stdio.h>
438a2c668cStedu #include <stdlib.h>
448a2c668cStedu #include <string.h>
458a2c668cStedu #include <unistd.h>
46b9fc9a72Sderaadt #include <limits.h>
4789d600a0Sotto #include <grp.h>
4889d600a0Sotto #include <pwd.h>
498a2c668cStedu 
508a2c668cStedu #include <mntopts.h>
518a2c668cStedu 
528a2c668cStedu static const struct mntopt mopts[] = {
538a2c668cStedu 	MOPT_STDOPTS,
548a2c668cStedu 	{ NULL }
558a2c668cStedu };
568a2c668cStedu 
572e4c3f94Stedu static __dead void usage(void);
5889d600a0Sotto static gid_t a_gid(char *);
5989d600a0Sotto static uid_t a_uid(char *);
602e4c3f94Stedu static mode_t a_mask(char *);
618a2c668cStedu 
628a2c668cStedu int
main(int argc,char * argv[])639fb93924Sderaadt main(int argc, char *argv[])
648a2c668cStedu {
658a2c668cStedu 	struct ntfs_args args;
668a2c668cStedu 	struct stat sb;
678a2c668cStedu 	int c, mntflags, set_gid, set_uid, set_mask;
68b9fc9a72Sderaadt 	char *dev, dir[PATH_MAX];
698a2c668cStedu 
708a2c668cStedu 	mntflags = set_gid = set_uid = set_mask = 0;
712e4c3f94Stedu 	memset(&args, 0, sizeof(args));
728a2c668cStedu 
738a2c668cStedu 	while ((c = getopt(argc, argv, "aiu:g:m:o:")) !=  -1) {
748a2c668cStedu 		switch (c) {
758a2c668cStedu 		case 'u':
7689d600a0Sotto 			args.uid = a_uid(optarg);
778a2c668cStedu 			set_uid = 1;
788a2c668cStedu 			break;
798a2c668cStedu 		case 'g':
8089d600a0Sotto 			args.gid = a_gid(optarg);
818a2c668cStedu 			set_gid = 1;
828a2c668cStedu 			break;
838a2c668cStedu 		case 'm':
8401ceaffdSpedro 			args.mode = a_mask(optarg);
858a2c668cStedu 			set_mask = 1;
868a2c668cStedu 			break;
878a2c668cStedu 		case 'i':
888a2c668cStedu 			args.flag |= NTFS_MFLAG_CASEINS;
898a2c668cStedu 			break;
908a2c668cStedu 		case 'a':
918a2c668cStedu 			args.flag |= NTFS_MFLAG_ALLNAMES;
928a2c668cStedu 			break;
938a2c668cStedu 		case 'o':
948a2c668cStedu 			getmntopts(optarg, mopts, &mntflags);
958a2c668cStedu 			break;
968a2c668cStedu 		default:
978a2c668cStedu 			usage();
988a2c668cStedu 			break;
998a2c668cStedu 		}
1008a2c668cStedu 	}
1018a2c668cStedu 
1028a2c668cStedu 	if (optind + 2 != argc)
1038a2c668cStedu 		usage();
1048a2c668cStedu 
1058a2c668cStedu 	dev = argv[optind];
10607e4edeaStedu 	if (realpath(argv[optind + 1], dir) == NULL)
107b0208e3fSjaredy 		err(1, "realpath %s", argv[optind + 1]);
1088a2c668cStedu 
1098a2c668cStedu 	args.fspec = dev;
1102e4c3f94Stedu 	args.export_info.ex_root = 65534;	/* unchecked anyway on NTFS */
1112e4c3f94Stedu 
1122e4c3f94Stedu 	mntflags |= MNT_RDONLY;
1138a2c668cStedu 	if (mntflags & MNT_RDONLY)
114e117469aStedu 		args.export_info.ex_flags = MNT_EXRDONLY;
1158a2c668cStedu 	else
116e117469aStedu 		args.export_info.ex_flags = 0;
1178a2c668cStedu 	if (!set_gid || !set_uid || !set_mask) {
1188a2c668cStedu 		if (stat(dir, &sb) == -1)
119e7ea3284Smillert 			err(1, "stat %s", dir);
1208a2c668cStedu 
1218a2c668cStedu 		if (!set_uid)
1228a2c668cStedu 			args.uid = sb.st_uid;
1238a2c668cStedu 		if (!set_gid)
1248a2c668cStedu 			args.gid = sb.st_gid;
1258a2c668cStedu 		if (!set_mask)
1268a2c668cStedu 			args.mode = sb.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO);
1278a2c668cStedu 	}
128df69c215Sderaadt 	if (mount(MOUNT_NTFS, dir, mntflags, &args) == -1)
129e7ea3284Smillert 		err(1, "%s on %s", dev, dir);
1308a2c668cStedu 
1318a2c668cStedu 	exit(0);
1328a2c668cStedu }
1338a2c668cStedu 
13489d600a0Sotto gid_t
a_gid(char * s)13589d600a0Sotto a_gid(char *s)
13689d600a0Sotto {
13789d600a0Sotto 	struct group *gr;
13889d600a0Sotto 	const char *errstr;
13989d600a0Sotto 	gid_t gid;
14089d600a0Sotto 
14189d600a0Sotto 	if ((gr = getgrnam(s)) != NULL)
14289d600a0Sotto 		return gr->gr_gid;
14389d600a0Sotto 	gid = strtonum(s, 0, GID_MAX, &errstr);
14489d600a0Sotto 	if (errstr)
14589d600a0Sotto 		errx(1, "group is %s: %s", errstr, s);
14689d600a0Sotto 	return (gid);
14789d600a0Sotto }
14889d600a0Sotto 
14989d600a0Sotto uid_t
a_uid(char * s)15089d600a0Sotto a_uid(char *s)
15189d600a0Sotto {
15289d600a0Sotto 	struct passwd *pw;
15389d600a0Sotto 	const char *errstr;
15489d600a0Sotto 	uid_t uid;
15589d600a0Sotto 
15689d600a0Sotto 	if ((pw = getpwnam(s)) != NULL)
15789d600a0Sotto 		return pw->pw_uid;
15889d600a0Sotto 	uid = strtonum(s, 0, UID_MAX, &errstr);
15989d600a0Sotto 	if (errstr)
16089d600a0Sotto 		errx(1, "user is %s: %s", errstr, s);
16189d600a0Sotto 	return (uid);
16289d600a0Sotto }
16389d600a0Sotto 
16489d600a0Sotto mode_t
a_mask(char * s)16501ceaffdSpedro a_mask(char *s)
16601ceaffdSpedro {
16701ceaffdSpedro 	int done, rv;
16801ceaffdSpedro 	char *ep;
16901ceaffdSpedro 
17001ceaffdSpedro 	done = 0;
17101ceaffdSpedro 	if (*s >= '0' && *s <= '7') {
17201ceaffdSpedro 		done = 1;
17301ceaffdSpedro 		rv = strtol(optarg, &ep, 8);
17401ceaffdSpedro 	}
17501ceaffdSpedro 	if (!done || rv < 0 || *ep)
17601ceaffdSpedro 		errx(1, "invalid file mode: %s", s);
17701ceaffdSpedro 	return (rv);
17801ceaffdSpedro }
17901ceaffdSpedro 
1808a2c668cStedu static void
usage(void)1819fb93924Sderaadt usage(void)
1828a2c668cStedu {
1839fb93924Sderaadt 	fprintf(stderr,
184*c6821aa4Stb 	    "usage: mount_ntfs [-ai] [-g group] [-m mask] [-o options] [-u user]"
185474a64e7Sgrunk 	    " special node\n");
186e7ea3284Smillert 	exit(1);
1878a2c668cStedu }
188