1 | /*
|
---|
2 | * smbumount.c
|
---|
3 | *
|
---|
4 | * Copyright (C) 1995-1998 by Volker Lendecke
|
---|
5 | *
|
---|
6 | */
|
---|
7 |
|
---|
8 | #define SMBMOUNT_MALLOC 1
|
---|
9 |
|
---|
10 | #include "includes.h"
|
---|
11 |
|
---|
12 | #include <mntent.h>
|
---|
13 |
|
---|
14 | #include <asm/types.h>
|
---|
15 | #include <asm/posix_types.h>
|
---|
16 | #include <linux/smb.h>
|
---|
17 | #include <linux/smb_mount.h>
|
---|
18 | #include <linux/smb_fs.h>
|
---|
19 |
|
---|
20 | /* This is a (hopefully) temporary hack due to the fact that
|
---|
21 | sizeof( uid_t ) != sizeof( __kernel_uid_t ) under glibc.
|
---|
22 | This may change in the future and smb.h may get fixed in the
|
---|
23 | future. In the mean time, it's ugly hack time - get over it.
|
---|
24 | */
|
---|
25 | #undef SMB_IOC_GETMOUNTUID
|
---|
26 | #define SMB_IOC_GETMOUNTUID _IOR('u', 1, __kernel_uid_t)
|
---|
27 |
|
---|
28 | #ifndef O_NOFOLLOW
|
---|
29 | #define O_NOFOLLOW 0400000
|
---|
30 | #endif
|
---|
31 |
|
---|
32 | static void
|
---|
33 | usage(void)
|
---|
34 | {
|
---|
35 | printf("usage: smbumount mountpoint\n\n");
|
---|
36 | printf("Please be aware that smbfs is deprecated in favor of "
|
---|
37 | "cifs\n");
|
---|
38 | }
|
---|
39 |
|
---|
40 | static int
|
---|
41 | umount_ok(const char *mount_point)
|
---|
42 | {
|
---|
43 | /* we set O_NOFOLLOW to prevent users playing games with symlinks to
|
---|
44 | umount filesystems they don't own */
|
---|
45 | int fid = open(mount_point, O_RDONLY|O_NOFOLLOW, 0);
|
---|
46 | __kernel_uid32_t mount_uid;
|
---|
47 |
|
---|
48 | if (fid == -1) {
|
---|
49 | fprintf(stderr, "Could not open %s: %s\n",
|
---|
50 | mount_point, strerror(errno));
|
---|
51 | return -1;
|
---|
52 | }
|
---|
53 |
|
---|
54 | if (ioctl(fid, SMB_IOC_GETMOUNTUID32, &mount_uid) != 0) {
|
---|
55 | __kernel_uid_t mount_uid16;
|
---|
56 | if (ioctl(fid, SMB_IOC_GETMOUNTUID, &mount_uid16) != 0) {
|
---|
57 | fprintf(stderr, "%s probably not smb-filesystem\n",
|
---|
58 | mount_point);
|
---|
59 | return -1;
|
---|
60 | }
|
---|
61 | mount_uid = mount_uid16;
|
---|
62 | }
|
---|
63 |
|
---|
64 | if ((getuid() != 0)
|
---|
65 | && (mount_uid != getuid())) {
|
---|
66 | fprintf(stderr, "You are not allowed to umount %s\n",
|
---|
67 | mount_point);
|
---|
68 | return -1;
|
---|
69 | }
|
---|
70 |
|
---|
71 | close(fid);
|
---|
72 | return 0;
|
---|
73 | }
|
---|
74 |
|
---|
75 | /* Make a canonical pathname from PATH. Returns a freshly malloced string.
|
---|
76 | It is up the *caller* to ensure that the PATH is sensible. i.e.
|
---|
77 | canonicalize ("/dev/fd0/.") returns "/dev/fd0" even though ``/dev/fd0/.''
|
---|
78 | is not a legal pathname for ``/dev/fd0'' Anything we cannot parse
|
---|
79 | we return unmodified. */
|
---|
80 | static char *
|
---|
81 | canonicalize (char *path)
|
---|
82 | {
|
---|
83 | char *canonical = malloc (PATH_MAX + 1);
|
---|
84 |
|
---|
85 | if (!canonical) {
|
---|
86 | fprintf(stderr, "Error! Not enough memory!\n");
|
---|
87 | return NULL;
|
---|
88 | }
|
---|
89 |
|
---|
90 | if (strlen(path) > PATH_MAX) {
|
---|
91 | fprintf(stderr, "Mount point string too long\n");
|
---|
92 | return NULL;
|
---|
93 | }
|
---|
94 |
|
---|
95 | if (path == NULL)
|
---|
96 | return NULL;
|
---|
97 |
|
---|
98 | if (realpath (path, canonical))
|
---|
99 | return canonical;
|
---|
100 |
|
---|
101 | strncpy (canonical, path, PATH_MAX);
|
---|
102 | canonical[PATH_MAX] = '\0';
|
---|
103 | return canonical;
|
---|
104 | }
|
---|
105 |
|
---|
106 |
|
---|
107 | int
|
---|
108 | main(int argc, char *argv[])
|
---|
109 | {
|
---|
110 | int fd;
|
---|
111 | char* mount_point;
|
---|
112 | struct mntent *mnt;
|
---|
113 | FILE* mtab;
|
---|
114 | FILE* new_mtab;
|
---|
115 |
|
---|
116 | if (argc != 2) {
|
---|
117 | usage();
|
---|
118 | exit(1);
|
---|
119 | }
|
---|
120 |
|
---|
121 | if (geteuid() != 0) {
|
---|
122 | fprintf(stderr, "smbumount must be installed suid root\n");
|
---|
123 | exit(1);
|
---|
124 | }
|
---|
125 |
|
---|
126 | mount_point = canonicalize(argv[1]);
|
---|
127 |
|
---|
128 | if (mount_point == NULL)
|
---|
129 | {
|
---|
130 | exit(1);
|
---|
131 | }
|
---|
132 |
|
---|
133 | if (umount_ok(mount_point) != 0) {
|
---|
134 | exit(1);
|
---|
135 | }
|
---|
136 |
|
---|
137 | if (umount(mount_point) != 0) {
|
---|
138 | fprintf(stderr, "Could not umount %s: %s\n",
|
---|
139 | mount_point, strerror(errno));
|
---|
140 | exit(1);
|
---|
141 | }
|
---|
142 |
|
---|
143 | if ((fd = open(MOUNTED"~", O_RDWR|O_CREAT|O_EXCL, 0600)) == -1)
|
---|
144 | {
|
---|
145 | fprintf(stderr, "Can't get "MOUNTED"~ lock file");
|
---|
146 | return 1;
|
---|
147 | }
|
---|
148 | close(fd);
|
---|
149 |
|
---|
150 | if ((mtab = setmntent(MOUNTED, "r")) == NULL) {
|
---|
151 | fprintf(stderr, "Can't open " MOUNTED ": %s\n",
|
---|
152 | strerror(errno));
|
---|
153 | return 1;
|
---|
154 | }
|
---|
155 |
|
---|
156 | #define MOUNTED_TMP MOUNTED".tmp"
|
---|
157 |
|
---|
158 | if ((new_mtab = setmntent(MOUNTED_TMP, "w")) == NULL) {
|
---|
159 | fprintf(stderr, "Can't open " MOUNTED_TMP ": %s\n",
|
---|
160 | strerror(errno));
|
---|
161 | endmntent(mtab);
|
---|
162 | return 1;
|
---|
163 | }
|
---|
164 |
|
---|
165 | while ((mnt = getmntent(mtab)) != NULL) {
|
---|
166 | if (strcmp(mnt->mnt_dir, mount_point) != 0) {
|
---|
167 | addmntent(new_mtab, mnt);
|
---|
168 | }
|
---|
169 | }
|
---|
170 |
|
---|
171 | endmntent(mtab);
|
---|
172 |
|
---|
173 | if (fchmod (fileno (new_mtab), S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH) < 0) {
|
---|
174 | fprintf(stderr, "Error changing mode of %s: %s\n",
|
---|
175 | MOUNTED_TMP, strerror(errno));
|
---|
176 | exit(1);
|
---|
177 | }
|
---|
178 |
|
---|
179 | endmntent(new_mtab);
|
---|
180 |
|
---|
181 | if (rename(MOUNTED_TMP, MOUNTED) < 0) {
|
---|
182 | fprintf(stderr, "Cannot rename %s to %s: %s\n",
|
---|
183 | MOUNTED, MOUNTED_TMP, strerror(errno));
|
---|
184 | exit(1);
|
---|
185 | }
|
---|
186 |
|
---|
187 | if (unlink(MOUNTED"~") == -1)
|
---|
188 | {
|
---|
189 | fprintf(stderr, "Can't remove "MOUNTED"~");
|
---|
190 | return 1;
|
---|
191 | }
|
---|
192 |
|
---|
193 | return 0;
|
---|
194 | }
|
---|