4 * Copyright (C) 1995-1998 by Paal-Kr. Engstad and Volker Lendecke
5 * extensively modified by Tridge
12 #include <sys/utsname.h>
14 #include <asm/types.h>
15 #include <asm/posix_types.h>
16 #include <linux/smb.h>
17 #include <linux/smb_mount.h>
18 #include <asm/unistd.h>
21 /* This may look strange but MS_MGC_VAL is what we are looking for and
22 is what we need from <linux/fs.h> under libc systems and is
23 provided in standard includes on glibc systems. So... We
24 switch on what we need... */
28 static uid_t mount_uid
;
29 static gid_t mount_gid
;
31 static unsigned mount_fmask
;
32 static unsigned mount_dmask
;
33 static int user_mount
;
40 printf("Usage: smbmnt mount-point [options]\n");
41 printf("Version %s\n\n",VERSION
);
42 printf("-s share share name on server\n"
43 "-r mount read-only\n"
44 "-u uid mount as uid\n"
45 "-g gid mount as gid\n"
46 "-f mask permission mask for files\n"
47 "-d mask permission mask for directories\n"
48 "-o options name=value, list of options\n"
49 "-h print this help text\n");
53 parse_args(int argc
, char *argv
[], struct smb_mount_data
*data
, char **share
)
57 while ((opt
= getopt (argc
, argv
, "s:u:g:rf:d:o:")) != EOF
)
66 mount_uid
= strtol(optarg
, NULL
, 0);
71 mount_gid
= strtol(optarg
, NULL
, 0);
78 mount_fmask
= strtol(optarg
, NULL
, 8);
81 mount_dmask
= strtol(optarg
, NULL
, 8);
95 fullpath(const char *p
)
97 char path
[MAXPATHLEN
];
99 if (strlen(p
) > MAXPATHLEN
-1) {
103 if (realpath(p
, path
) == NULL
) {
104 fprintf(stderr
,"Failed to find real path for mount point\n");
110 /* Check whether user is allowed to mount on the specified mount point. If it's
111 OK then we change into that directory - this prevents race conditions */
112 static int mount_ok(char *mount_point
)
116 if (chdir(mount_point
) != 0) {
120 if (sys_stat(".", &st
) != 0) {
124 if (!S_ISDIR(st
.st_mode
)) {
129 if ((getuid() != 0) &&
130 ((getuid() != st
.st_uid
) ||
131 ((st
.st_mode
& S_IRWXU
) != S_IRWXU
))) {
139 /* Tries to mount using the appropriate format. For 2.2 the struct,
140 for 2.4 the ascii version. */
142 do_mount(char *share_name
, unsigned int flags
, struct smb_mount_data
*data
)
146 char *release
, *major
, *minor
;
150 release
= uts
.release
;
151 major
= strsep(&release
, ".");
152 minor
= strsep(&release
, ".");
153 if (major
&& minor
&& atoi(major
) == 2 && atoi(minor
) < 4) {
154 /* < 2.4, assume struct */
155 data1
= (char *) data
;
158 /* >= 2.4, assume ascii but fall back on struct */
160 data2
= (char *) data
;
163 slprintf(opts
, sizeof(opts
)-1,
164 "version=7,uid=%d,gid=%d,file_mode=0%o,dir_mode=0%o,%s",
165 data
->uid
, data
->gid
, data
->file_mode
, data
->dir_mode
,options
);
166 if (mount(share_name
, ".", "smbfs", flags
, data1
) == 0)
168 return mount(share_name
, ".", "smbfs", flags
, data2
);
171 int main(int argc
, char *argv
[])
173 char *mount_point
, *share_name
= NULL
;
177 struct smb_mount_data data
;
180 memset(&data
, 0, sizeof(struct smb_mount_data
));
187 if (argv
[1][0] == '-') {
196 if (geteuid() != 0) {
197 fprintf(stderr
, "smbmnt must be installed suid root for direct user mounts (%d,%d)\n", getuid(), geteuid());
201 mount_uid
= getuid();
202 mount_gid
= getgid();
203 mount_fmask
= umask(0);
205 mount_fmask
= ~mount_fmask
;
207 mount_point
= fullpath(argv
[1]);
212 if (mount_ok(mount_point
) != 0) {
213 fprintf(stderr
, "cannot mount on %s: %s\n",
214 mount_point
, strerror(errno
));
218 data
.version
= SMB_MOUNT_VERSION
;
220 /* getuid() gives us the real uid, who may umount the fs */
221 data
.mounted_uid
= getuid();
223 if (parse_args(argc
, argv
, &data
, &share_name
) != 0) {
228 data
.uid
= mount_uid
;
229 data
.gid
= mount_gid
;
230 data
.file_mode
= (S_IRWXU
|S_IRWXG
|S_IRWXO
) & mount_fmask
;
231 data
.dir_mode
= (S_IRWXU
|S_IRWXG
|S_IRWXO
) & mount_dmask
;
233 if (mount_dmask
== 0) {
234 data
.dir_mode
= data
.file_mode
;
235 if ((data
.dir_mode
& S_IRUSR
) != 0)
236 data
.dir_mode
|= S_IXUSR
;
237 if ((data
.dir_mode
& S_IRGRP
) != 0)
238 data
.dir_mode
|= S_IXGRP
;
239 if ((data
.dir_mode
& S_IROTH
) != 0)
240 data
.dir_mode
|= S_IXOTH
;
245 if (mount_ro
) flags
|= MS_RDONLY
;
247 if (do_mount(share_name
, flags
, &data
) < 0) {
250 fprintf(stderr
, "ERROR: smbfs filesystem not supported by the kernel\n");
253 perror("mount error");
255 fprintf(stderr
, "Please refer to the smbmnt(8) manual page\n");
259 ment
.mnt_fsname
= share_name
? share_name
: "none";
260 ment
.mnt_dir
= mount_point
;
261 ment
.mnt_type
= "smbfs";
266 mount_point
= ment
.mnt_dir
;
268 if (mount_point
== NULL
)
270 fprintf(stderr
, "Mount point too long\n");
274 if ((fd
= open(MOUNTED
"~", O_RDWR
|O_CREAT
|O_EXCL
, 0600)) == -1)
276 fprintf(stderr
, "Can't get "MOUNTED
"~ lock file");
281 if ((mtab
= setmntent(MOUNTED
, "a+")) == NULL
)
283 fprintf(stderr
, "Can't open " MOUNTED
);
287 if (addmntent(mtab
, &ment
) == 1)
289 fprintf(stderr
, "Can't write mount entry");
292 if (fchmod(fileno(mtab
), 0644) == -1)
294 fprintf(stderr
, "Can't set perms on "MOUNTED
);
299 if (unlink(MOUNTED
"~") == -1)
301 fprintf(stderr
, "Can't remove "MOUNTED
"~");