4 * Copyright (C) 1995-1998 by Paal-Kr. Engstad and Volker Lendecke
5 * extensively modified by Tridge
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, see <http://www.gnu.org/licenses/>.
22 #define SMBMOUNT_MALLOC 1
27 #include <sys/utsname.h>
29 #include <asm/types.h>
30 #include <asm/posix_types.h>
31 #include <linux/smb.h>
32 #include <linux/smb_mount.h>
33 #include <asm/unistd.h>
36 /* This may look strange but MS_MGC_VAL is what we are looking for and
37 is what we need from <linux/fs.h> under libc systems and is
38 provided in standard includes on glibc systems. So... We
39 switch on what we need... */
43 static uid_t mount_uid
;
44 static gid_t mount_gid
;
46 static unsigned mount_fmask
;
47 static unsigned mount_dmask
;
48 static int user_mount
;
55 printf("Usage: smbmnt mount-point [options]\n");
56 printf("Version %s\n\n",SAMBA_VERSION_STRING
);
57 printf("-s share share name on server\n"
58 "-r mount read-only\n"
59 "-u uid mount as uid\n"
60 "-g gid mount as gid\n"
61 "-f mask permission mask for files\n"
62 "-d mask permission mask for directories\n"
63 "-o options name=value, list of options\n"
64 "-h print this help text\n");
68 parse_args(int argc
, char *argv
[], struct smb_mount_data
*data
, char **share
)
72 while ((opt
= getopt (argc
, argv
, "s:u:g:rf:d:o:")) != EOF
)
81 mount_uid
= strtol(optarg
, NULL
, 0);
86 mount_gid
= strtol(optarg
, NULL
, 0);
93 mount_fmask
= strtol(optarg
, NULL
, 8);
96 mount_dmask
= strtol(optarg
, NULL
, 8);
110 fullpath(const char *p
)
112 char path
[PATH_MAX
+1];
114 if (strlen(p
) > PATH_MAX
) {
118 if (realpath(p
, path
) == NULL
) {
119 fprintf(stderr
,"Failed to find real path for mount point %s: %s\n",
126 /* Check whether user is allowed to mount on the specified mount point. If it's
127 OK then we change into that directory - this prevents race conditions */
128 static int mount_ok(char *mount_point
)
132 if (chdir(mount_point
) != 0) {
136 if (stat(".", &st
) != 0) {
140 if (!S_ISDIR(st
.st_mode
)) {
145 if ((getuid() != 0) &&
146 ((getuid() != st
.st_uid
) ||
147 ((st
.st_mode
& S_IRWXU
) != S_IRWXU
))) {
155 /* Tries to mount using the appropriate format. For 2.2 the struct,
156 for 2.4 the ascii version. */
158 do_mount(char *share_name
, unsigned int flags
, struct smb_mount_data
*data
)
162 char *release
, *major
, *minor
;
168 "version=7,uid=%d,gid=%d,file_mode=0%o,dir_mode=0%o,%s",
169 mount_uid
, mount_gid
, data
->file_mode
,
170 data
->dir_mode
,options
) < 0) {
175 release
= uts
.release
;
176 major
= strtok_r(release
, ".", &saveptr
);
177 minor
= strtok_r(NULL
, ".", &saveptr
);
178 if (major
&& minor
&& atoi(major
) == 2 && atoi(minor
) < 4) {
179 /* < 2.4, assume struct */
180 data1
= (char *) data
;
183 /* >= 2.4, assume ascii but fall back on struct */
185 data2
= (char *) data
;
188 if (mount(share_name
, ".", "smbfs", flags
, data1
) == 0) {
192 ret
= mount(share_name
, ".", "smbfs", flags
, data2
);
197 int main(int argc
, char *argv
[])
199 char *mount_point
, *share_name
= NULL
;
203 struct smb_mount_data data
;
206 memset(&data
, 0, sizeof(struct smb_mount_data
));
213 if (argv
[1][0] == '-') {
222 if (geteuid() != 0) {
223 fprintf(stderr
, "smbmnt must be installed suid root for direct user mounts (%d,%d)\n", getuid(), geteuid());
227 mount_uid
= getuid();
228 mount_gid
= getgid();
229 mount_fmask
= umask(0);
231 mount_fmask
= ~mount_fmask
;
233 mount_point
= fullpath(argv
[1]);
238 if (mount_ok(mount_point
) != 0) {
239 fprintf(stderr
, "cannot mount on %s: %s\n",
240 mount_point
, strerror(errno
));
244 data
.version
= SMB_MOUNT_VERSION
;
246 /* getuid() gives us the real uid, who may umount the fs */
247 data
.mounted_uid
= getuid();
249 if (parse_args(argc
, argv
, &data
, &share_name
) != 0) {
254 data
.uid
= mount_uid
; // truncates to 16-bits here!!!
255 data
.gid
= mount_gid
;
256 data
.file_mode
= (S_IRWXU
|S_IRWXG
|S_IRWXO
) & mount_fmask
;
257 data
.dir_mode
= (S_IRWXU
|S_IRWXG
|S_IRWXO
) & mount_dmask
;
259 if (mount_dmask
== 0) {
260 data
.dir_mode
= data
.file_mode
;
261 if ((data
.dir_mode
& S_IRUSR
) != 0)
262 data
.dir_mode
|= S_IXUSR
;
263 if ((data
.dir_mode
& S_IRGRP
) != 0)
264 data
.dir_mode
|= S_IXGRP
;
265 if ((data
.dir_mode
& S_IROTH
) != 0)
266 data
.dir_mode
|= S_IXOTH
;
269 flags
= MS_MGC_VAL
| MS_NOSUID
| MS_NODEV
;
271 if (mount_ro
) flags
|= MS_RDONLY
;
273 if (do_mount(share_name
, flags
, &data
) < 0) {
276 fprintf(stderr
, "ERROR: smbfs filesystem not supported by the kernel\n");
279 perror("mount error");
281 fprintf(stderr
, "Please refer to the smbmnt(8) manual page\n");
285 ment
.mnt_fsname
= share_name
? share_name
: (char *)"none";
286 ment
.mnt_dir
= mount_point
;
287 ment
.mnt_type
= (char *)"smbfs";
288 ment
.mnt_opts
= (char *)"";
292 mount_point
= ment
.mnt_dir
;
294 if (mount_point
== NULL
)
296 fprintf(stderr
, "Mount point too long\n");
300 if ((fd
= open(MOUNTED
"~", O_RDWR
|O_CREAT
|O_EXCL
, 0600)) == -1)
302 fprintf(stderr
, "Can't get "MOUNTED
"~ lock file");
307 if ((mtab
= setmntent(MOUNTED
, "a+")) == NULL
)
309 fprintf(stderr
, "Can't open " MOUNTED
);
313 if (addmntent(mtab
, &ment
) == 1)
315 fprintf(stderr
, "Can't write mount entry");
318 if (fchmod(fileno(mtab
), 0644) == -1)
320 fprintf(stderr
, "Can't set perms on "MOUNTED
);
325 if (unlink(MOUNTED
"~") == -1)
327 fprintf(stderr
, "Can't remove "MOUNTED
"~");