4 * Copyright (C) 1995-1998 by 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 <linux/smb_fs.h>
18 /* This is a (hopefully) temporary hack due to the fact that
19 sizeof( uid_t ) != sizeof( __kernel_uid_t ) under glibc.
20 This may change in the future and smb.h may get fixed in the
21 future. In the mean time, it's ugly hack time - get over it.
23 #undef SMB_IOC_GETMOUNTUID
24 #define SMB_IOC_GETMOUNTUID _IOR('u', 1, __kernel_uid_t)
27 #define O_NOFOLLOW 0400000
33 printf("usage: smbumount mountpoint\n");
37 umount_ok(const char *mount_point
)
39 /* we set O_NOFOLLOW to prevent users playing games with symlinks to
40 umount filesystems they don't own */
41 int fid
= open(mount_point
, O_RDONLY
|O_NOFOLLOW
, 0);
42 __kernel_uid_t mount_uid
;
45 fprintf(stderr
, "Could not open %s: %s\n",
46 mount_point
, strerror(errno
));
50 if (ioctl(fid
, SMB_IOC_GETMOUNTUID
, &mount_uid
) != 0) {
51 fprintf(stderr
, "%s probably not smb-filesystem\n",
57 && (mount_uid
!= getuid())) {
58 fprintf(stderr
, "You are not allowed to umount %s\n",
67 /* Make a canonical pathname from PATH. Returns a freshly malloced string.
68 It is up the *caller* to ensure that the PATH is sensible. i.e.
69 canonicalize ("/dev/fd0/.") returns "/dev/fd0" even though ``/dev/fd0/.''
70 is not a legal pathname for ``/dev/fd0'' Anything we cannot parse
71 we return unmodified. */
73 canonicalize (char *path
)
75 char *canonical
= malloc (PATH_MAX
+ 1);
77 if (strlen(path
) > PATH_MAX
) {
78 fprintf(stderr
, "Mount point string too long\n");
85 if (realpath (path
, canonical
))
88 pstrcpy (canonical
, path
);
94 main(int argc
, char *argv
[])
107 if (geteuid() != 0) {
108 fprintf(stderr
, "smbumount must be installed suid root\n");
112 mount_point
= canonicalize(argv
[1]);
114 if (mount_point
== NULL
)
119 if (umount_ok(mount_point
) != 0) {
123 if (umount(mount_point
) != 0) {
124 fprintf(stderr
, "Could not umount %s: %s\n",
125 mount_point
, strerror(errno
));
129 if ((fd
= open(MOUNTED
"~", O_RDWR
|O_CREAT
|O_EXCL
, 0600)) == -1)
131 fprintf(stderr
, "Can't get "MOUNTED
"~ lock file");
136 if ((mtab
= setmntent(MOUNTED
, "r")) == NULL
) {
137 fprintf(stderr
, "Can't open " MOUNTED
": %s\n",
142 #define MOUNTED_TMP MOUNTED".tmp"
144 if ((new_mtab
= setmntent(MOUNTED_TMP
, "w")) == NULL
) {
145 fprintf(stderr
, "Can't open " MOUNTED_TMP
": %s\n",
151 while ((mnt
= getmntent(mtab
)) != NULL
) {
152 if (strcmp(mnt
->mnt_dir
, mount_point
) != 0) {
153 addmntent(new_mtab
, mnt
);
159 if (fchmod (fileno (new_mtab
), S_IRUSR
|S_IWUSR
|S_IRGRP
|S_IROTH
) < 0) {
160 fprintf(stderr
, "Error changing mode of %s: %s\n",
161 MOUNTED_TMP
, strerror(errno
));
167 if (rename(MOUNTED_TMP
, MOUNTED
) < 0) {
168 fprintf(stderr
, "Cannot rename %s to %s: %s\n",
169 MOUNTED
, MOUNTED_TMP
, strerror(errno
));
173 if (unlink(MOUNTED
"~") == -1)
175 fprintf(stderr
, "Can't remove "MOUNTED
"~");