4 * Copyright (C) 1995-1998 by Paal-Kr. Engstad and Volker Lendecke
13 #include <sys/socket.h>
14 #include <sys/param.h>
15 #include <netinet/in.h>
18 #include <sys/types.h>
19 /* #include <sys/wait.h> */ /* generates a warning here */
20 extern pid_t waitpid(pid_t, int *, int);
21 #include <sys/errno.h>
27 #include <sys/mount.h>
31 #include <linux/smb.h>
32 #include <linux/smb_mount.h>
34 #include <asm/unistd.h>
36 static char *progname;
42 printf("usage: %s mount-point [options]\n", progname);
43 printf("Try `%s -h' for more information\n", progname);
50 printf("usage: %s mount-point [options]\n", progname);
51 printf("-u uid uid the mounted files get\n"
52 "-g gid gid the mounted files get\n"
53 "-f mode permission the files get (octal notation)\n"
54 "-d mode permission the dirs get (octal notation)\n"
55 "-P pid connection handler's pid\n\n"
56 "-s share share name on server\n\n"
57 "-h print this help text\n");
61 parse_args(int argc, char *argv[], struct smb_mount_data *data, char **share)
67 while ((opt = getopt (argc, argv, "u:g:f:d:s:")) != EOF)
72 if (isdigit(optarg[0]))
74 data->uid = atoi(optarg);
78 pwd = getpwnam(optarg);
81 fprintf(stderr, "Unknown user: %s\n",
85 data->uid = pwd->pw_uid;
89 if (isdigit(optarg[0]))
91 data->gid = atoi(optarg);
95 grp = getgrnam(optarg);
98 fprintf(stderr, "Unknown group: %s\n",
102 data->gid = grp->gr_gid;
106 data->file_mode = strtol(optarg, NULL, 8);
109 data->dir_mode = strtol(optarg, NULL, 8);
123 fullpath(const char *p)
125 char path[MAXPATHLEN];
127 if (strlen(p) > MAXPATHLEN-1)
132 if (realpath(p, path) == NULL)
139 /* Check whether user is allowed to mount on the specified mount point */
141 mount_ok(SMB_STRUCT_STAT *st)
143 if (!S_ISDIR(st->st_mode))
150 && ( (getuid() != st->st_uid)
151 || ((st->st_mode & S_IRWXU) != S_IRWXU)))
161 main(int argc, char *argv[])
163 char *mount_point, *share_name = NULL;
167 struct smb_mount_data data;
173 memset(&data, 0, sizeof(struct smb_mount_data));
176 && (argv[1][0] == '-')
177 && (argv[1][1] == 'h')
178 && (argv[1][2] == '\0'))
184 if (geteuid() != 0) {
185 fprintf(stderr, "%s must be installed suid root\n", progname);
195 mount_point = argv[1];
200 if (sys_stat(mount_point, &st) == -1) {
201 fprintf(stderr, "could not find mount point %s: %s\n",
202 mount_point, strerror(errno));
206 if (mount_ok(&st) != 0) {
207 fprintf(stderr, "cannot mount on %s: %s\n",
208 mount_point, strerror(errno));
212 data.version = SMB_MOUNT_VERSION;
214 /* getuid() gives us the real uid, who may umount the fs */
215 data.mounted_uid = getuid();
221 data.file_mode = (S_IRWXU|S_IRWXG|S_IRWXO) & ~um;
224 if (parse_args(argc, argv, &data, &share_name) != 0) {
229 if (data.dir_mode == 0) {
230 data.dir_mode = data.file_mode;
231 if ((data.dir_mode & S_IRUSR) != 0)
232 data.dir_mode |= S_IXUSR;
233 if ((data.dir_mode & S_IRGRP) != 0)
234 data.dir_mode |= S_IXGRP;
235 if ((data.dir_mode & S_IROTH) != 0)
236 data.dir_mode |= S_IXOTH;
241 if (mount(share_name, mount_point, "smbfs", flags, (char *)&data) < 0)
243 perror("mount error");
244 printf("Please refer to the smbmnt(8) manual page\n");
248 ment.mnt_fsname = share_name ? share_name : "none";
249 ment.mnt_dir = fullpath(mount_point);
250 ment.mnt_type = "smbfs";
255 mount_point = ment.mnt_dir;
257 if (mount_point == NULL)
259 fprintf(stderr, "Mount point too long\n");
263 if ((fd = open(MOUNTED"~", O_RDWR|O_CREAT|O_EXCL, 0600)) == -1)
265 fprintf(stderr, "Can't get "MOUNTED"~ lock file");
270 if ((mtab = setmntent(MOUNTED, "a+")) == NULL)
272 fprintf(stderr, "Can't open " MOUNTED);
276 if (addmntent(mtab, &ment) == 1)
278 fprintf(stderr, "Can't write mount entry");
281 if (fchmod(fileno(mtab), 0644) == -1)
283 fprintf(stderr, "Can't set perms on "MOUNTED);
288 if (unlink(MOUNTED"~") == -1)
290 fprintf(stderr, "Can't remove "MOUNTED"~");