4 * Copyright (C) 1995-1998 by Paal-Kr. Engstad and Volker Lendecke
5 * extensively modified by Tridge
13 #include <asm/types.h>
14 #include <asm/posix_types.h>
15 #include <linux/smb.h>
16 #include <linux/smb_mount.h>
17 #include <asm/unistd.h>
20 /* This may look strange but MS_MGC_VAL is what we are looking for and
21 is what we need from <linux/fs.h> under libc systems and is
22 provided in standard includes on glibc systems. So... We
23 switch on what we need... */
27 static uid_t mount_uid;
28 static gid_t mount_gid;
30 static unsigned mount_fmask;
31 static unsigned mount_dmask;
32 static int user_mount;
38 printf("usage: smbmnt mount-point [options]\n");
39 printf("-s share share name on server\n"
40 "-r mount read-only\n"
41 "-u uid mount as uid\n"
42 "-g gid mount as gid\n"
43 "-f mask permission mask for files\n"
44 "-d mask permission mask for directories\n"
45 "-h print this help text\n");
49 parse_args(int argc, char *argv[], struct smb_mount_data *data, char **share)
53 while ((opt = getopt (argc, argv, "s:u:g:rf:d:")) != EOF)
62 mount_uid = strtol(optarg, NULL, 0);
67 mount_gid = strtol(optarg, NULL, 0);
74 mount_fmask = strtol(optarg, NULL, 8);
77 mount_dmask = strtol(optarg, NULL, 8);
88 fullpath(const char *p)
90 char path[MAXPATHLEN];
92 if (strlen(p) > MAXPATHLEN-1) {
96 if (realpath(p, path) == NULL) {
97 fprintf(stderr,"Failed to find real path for mount point\n");
103 /* Check whether user is allowed to mount on the specified mount point. If it's
104 OK then we change into that directory - this prevents race conditions */
105 static int mount_ok(char *mount_point)
109 if (chdir(mount_point) != 0) {
113 if (sys_stat(".", &st) != 0) {
117 if (!S_ISDIR(st.st_mode)) {
122 if ((getuid() != 0) &&
123 ((getuid() != st.st_uid) ||
124 ((st.st_mode & S_IRWXU) != S_IRWXU))) {
132 int main(int argc, char *argv[])
134 char *mount_point, *share_name = NULL;
138 struct smb_mount_data data;
141 memset(&data, 0, sizeof(struct smb_mount_data));
148 if (argv[1][0] == '-') {
157 if (geteuid() != 0) {
158 fprintf(stderr, "smbmnt must be installed suid root for direct user mounts (%d,%d)\n", getuid(), geteuid());
162 mount_uid = getuid();
163 mount_gid = getgid();
164 mount_fmask = umask(0);
166 mount_fmask = ~mount_fmask;
168 mount_point = fullpath(argv[1]);
173 if (mount_ok(mount_point) != 0) {
174 fprintf(stderr, "cannot mount on %s: %s\n",
175 mount_point, strerror(errno));
179 data.version = SMB_MOUNT_VERSION;
181 /* getuid() gives us the real uid, who may umount the fs */
182 data.mounted_uid = getuid();
184 if (parse_args(argc, argv, &data, &share_name) != 0) {
189 data.uid = mount_uid;
190 data.gid = mount_gid;
191 data.file_mode = (S_IRWXU|S_IRWXG|S_IRWXO) & mount_fmask;
192 data.dir_mode = (S_IRWXU|S_IRWXG|S_IRWXO) & mount_dmask;
194 if (mount_dmask == 0) {
195 data.dir_mode = data.file_mode;
196 if ((data.dir_mode & S_IRUSR) != 0)
197 data.dir_mode |= S_IXUSR;
198 if ((data.dir_mode & S_IRGRP) != 0)
199 data.dir_mode |= S_IXGRP;
200 if ((data.dir_mode & S_IROTH) != 0)
201 data.dir_mode |= S_IXOTH;
206 if (mount_ro) flags |= MS_RDONLY;
208 if (mount(share_name, ".", "smbfs", flags, (char *)&data) < 0)
212 fprintf(stderr, "ERROR: smbfs filesystem not supported by the kernel\n");
215 perror("mount error");
217 fprintf(stderr, "Please refer to the smbmnt(8) manual page\n");
221 ment.mnt_fsname = share_name ? share_name : "none";
222 ment.mnt_dir = mount_point;
223 ment.mnt_type = "smbfs";
228 mount_point = ment.mnt_dir;
230 if (mount_point == NULL)
232 fprintf(stderr, "Mount point too long\n");
236 if ((fd = open(MOUNTED"~", O_RDWR|O_CREAT|O_EXCL, 0600)) == -1)
238 fprintf(stderr, "Can't get "MOUNTED"~ lock file");
243 if ((mtab = setmntent(MOUNTED, "a+")) == NULL)
245 fprintf(stderr, "Can't open " MOUNTED);
249 if (addmntent(mtab, &ment) == 1)
251 fprintf(stderr, "Can't write mount entry");
254 if (fchmod(fileno(mtab), 0644) == -1)
256 fprintf(stderr, "Can't set perms on "MOUNTED);
261 if (unlink(MOUNTED"~") == -1)
263 fprintf(stderr, "Can't remove "MOUNTED"~");