preparing for release of 2.2.3a
[Samba.git] / source / client / smbumount.c
blob983ad44fa0f6619cfdf72877e54640f6d43cdb4b
1 /*
2 * smbumount.c
4 * Copyright (C) 1995-1998 by Volker Lendecke
6 */
8 #include "includes.h"
10 #include <mntent.h>
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)
26 #ifndef O_NOFOLLOW
27 #define O_NOFOLLOW 0400000
28 #endif
30 static void
31 usage(void)
33 printf("usage: smbumount mountpoint\n");
36 static int
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;
44 if (fid == -1) {
45 fprintf(stderr, "Could not open %s: %s\n",
46 mount_point, strerror(errno));
47 return -1;
50 if (ioctl(fid, SMB_IOC_GETMOUNTUID, &mount_uid) != 0) {
51 fprintf(stderr, "%s probably not smb-filesystem\n",
52 mount_point);
53 return -1;
56 if ((getuid() != 0)
57 && (mount_uid != getuid())) {
58 fprintf(stderr, "You are not allowed to umount %s\n",
59 mount_point);
60 return -1;
63 close(fid);
64 return 0;
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. */
72 static char *
73 canonicalize (char *path)
75 char *canonical = malloc (PATH_MAX + 1);
77 if (!canonical) {
78 fprintf(stderr, "Error! Not enough memory!\n");
79 return NULL;
82 if (strlen(path) > PATH_MAX) {
83 fprintf(stderr, "Mount point string too long\n");
84 return NULL;
87 if (path == NULL)
88 return NULL;
90 if (realpath (path, canonical))
91 return canonical;
93 pstrcpy (canonical, path);
94 return canonical;
98 int
99 main(int argc, char *argv[])
101 int fd;
102 char* mount_point;
103 struct mntent *mnt;
104 FILE* mtab;
105 FILE* new_mtab;
107 if (argc != 2) {
108 usage();
109 exit(1);
112 if (geteuid() != 0) {
113 fprintf(stderr, "smbumount must be installed suid root\n");
114 exit(1);
117 mount_point = canonicalize(argv[1]);
119 if (mount_point == NULL)
121 exit(1);
124 if (umount_ok(mount_point) != 0) {
125 exit(1);
128 if (umount(mount_point) != 0) {
129 fprintf(stderr, "Could not umount %s: %s\n",
130 mount_point, strerror(errno));
131 exit(1);
134 if ((fd = open(MOUNTED"~", O_RDWR|O_CREAT|O_EXCL, 0600)) == -1)
136 fprintf(stderr, "Can't get "MOUNTED"~ lock file");
137 return 1;
139 close(fd);
141 if ((mtab = setmntent(MOUNTED, "r")) == NULL) {
142 fprintf(stderr, "Can't open " MOUNTED ": %s\n",
143 strerror(errno));
144 return 1;
147 #define MOUNTED_TMP MOUNTED".tmp"
149 if ((new_mtab = setmntent(MOUNTED_TMP, "w")) == NULL) {
150 fprintf(stderr, "Can't open " MOUNTED_TMP ": %s\n",
151 strerror(errno));
152 endmntent(mtab);
153 return 1;
156 while ((mnt = getmntent(mtab)) != NULL) {
157 if (strcmp(mnt->mnt_dir, mount_point) != 0) {
158 addmntent(new_mtab, mnt);
162 endmntent(mtab);
164 if (fchmod (fileno (new_mtab), S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH) < 0) {
165 fprintf(stderr, "Error changing mode of %s: %s\n",
166 MOUNTED_TMP, strerror(errno));
167 exit(1);
170 endmntent(new_mtab);
172 if (rename(MOUNTED_TMP, MOUNTED) < 0) {
173 fprintf(stderr, "Cannot rename %s to %s: %s\n",
174 MOUNTED, MOUNTED_TMP, strerror(errno));
175 exit(1);
178 if (unlink(MOUNTED"~") == -1)
180 fprintf(stderr, "Can't remove "MOUNTED"~");
181 return 1;
184 return 0;