4 * Copyright (C) 1995-1998 by Paal-Kr. Engstad and Volker Lendecke
12 #include <asm/types.h>
13 #include <asm/posix_types.h>
14 #include <linux/smb.h>
15 #include <linux/smb_mount.h>
16 #include <asm/unistd.h>
19 /* This may look strange but MS_MGC_VAL is what we are looking for and
20 is what we need from <linux/fs.h> under libc systems and is
21 provided in standard includes on glibc systems. So... We
22 switch on what we need... */
26 static char *progname;
32 printf("usage: %s mount-point [options]\n", progname);
33 printf("Try `%s -h' for more information\n", progname);
40 printf("usage: %s mount-point [options]\n", progname);
41 printf("-u uid uid the mounted files get\n"
42 "-g gid gid the mounted files get\n"
43 "-f mode permission the files get (octal notation)\n"
44 "-d mode permission the dirs get (octal notation)\n"
45 "-P pid connection handler's pid\n\n"
46 "-s share share name on server\n\n"
47 "-h print this help text\n");
51 parse_args(int argc, char *argv[], struct smb_mount_data *data, char **share)
57 while ((opt = getopt (argc, argv, "u:g:f:d:s:")) != EOF)
62 if (isdigit(optarg[0]))
64 data->uid = atoi(optarg);
68 pwd = getpwnam(optarg);
71 fprintf(stderr, "Unknown user: %s\n",
75 data->uid = pwd->pw_uid;
79 if (isdigit(optarg[0]))
81 data->gid = atoi(optarg);
85 grp = getgrnam(optarg);
88 fprintf(stderr, "Unknown group: %s\n",
92 data->gid = grp->gr_gid;
96 data->file_mode = strtol(optarg, NULL, 8);
99 data->dir_mode = strtol(optarg, NULL, 8);
113 fullpath(const char *p)
115 char path[MAXPATHLEN];
117 if (strlen(p) > MAXPATHLEN-1)
122 if (realpath(p, path) == NULL)
129 /* Check whether user is allowed to mount on the specified mount point */
131 mount_ok(SMB_STRUCT_STAT *st)
133 if (!S_ISDIR(st->st_mode))
140 && ( (getuid() != st->st_uid)
141 || ((st->st_mode & S_IRWXU) != S_IRWXU)))
151 main(int argc, char *argv[])
153 char *mount_point, *share_name = NULL;
157 struct smb_mount_data data;
163 memset(&data, 0, sizeof(struct smb_mount_data));
166 && (argv[1][0] == '-')
167 && (argv[1][1] == 'h')
168 && (argv[1][2] == '\0'))
174 if (geteuid() != 0) {
175 fprintf(stderr, "%s must be installed suid root\n", progname);
185 mount_point = argv[1];
190 if (sys_stat(mount_point, &st) == -1) {
191 fprintf(stderr, "could not find mount point %s: %s\n",
192 mount_point, strerror(errno));
196 if (mount_ok(&st) != 0) {
197 fprintf(stderr, "cannot mount on %s: %s\n",
198 mount_point, strerror(errno));
202 data.version = SMB_MOUNT_VERSION;
204 /* getuid() gives us the real uid, who may umount the fs */
205 data.mounted_uid = getuid();
211 data.file_mode = (S_IRWXU|S_IRWXG|S_IRWXO) & ~um;
214 if (parse_args(argc, argv, &data, &share_name) != 0) {
219 if (data.dir_mode == 0) {
220 data.dir_mode = data.file_mode;
221 if ((data.dir_mode & S_IRUSR) != 0)
222 data.dir_mode |= S_IXUSR;
223 if ((data.dir_mode & S_IRGRP) != 0)
224 data.dir_mode |= S_IXGRP;
225 if ((data.dir_mode & S_IROTH) != 0)
226 data.dir_mode |= S_IXOTH;
231 if (mount(share_name, mount_point, "smbfs", flags, (char *)&data) < 0)
233 perror("mount error");
234 printf("Please refer to the smbmnt(8) manual page\n");
238 ment.mnt_fsname = share_name ? share_name : "none";
239 ment.mnt_dir = fullpath(mount_point);
240 ment.mnt_type = "smbfs";
245 mount_point = ment.mnt_dir;
247 if (mount_point == NULL)
249 fprintf(stderr, "Mount point too long\n");
253 if ((fd = open(MOUNTED"~", O_RDWR|O_CREAT|O_EXCL, 0600)) == -1)
255 fprintf(stderr, "Can't get "MOUNTED"~ lock file");
260 if ((mtab = setmntent(MOUNTED, "a+")) == NULL)
262 fprintf(stderr, "Can't open " MOUNTED);
266 if (addmntent(mtab, &ment) == 1)
268 fprintf(stderr, "Can't write mount entry");
271 if (fchmod(fileno(mtab), 0644) == -1)
273 fprintf(stderr, "Can't set perms on "MOUNTED);
278 if (unlink(MOUNTED"~") == -1)
280 fprintf(stderr, "Can't remove "MOUNTED"~");