Properly destroy the pdb search object
[Samba.git] / source / client / smbmnt.c
blob7f1e1d5003ac05580c34af1f3cf55e4a628bd87f
1 /*
2 * smbmnt.c
4 * Copyright (C) 1995-1998 by Paal-Kr. Engstad and Volker Lendecke
5 * extensively modified by Tridge
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, see <http://www.gnu.org/licenses/>.
22 #define SMBMOUNT_MALLOC 1
24 #include "includes.h"
26 #include <mntent.h>
27 #include <sys/utsname.h>
29 #include <asm/types.h>
30 #include <asm/posix_types.h>
31 #include <linux/smb.h>
32 #include <linux/smb_mount.h>
33 #include <asm/unistd.h>
35 #ifndef MS_MGC_VAL
36 /* This may look strange but MS_MGC_VAL is what we are looking for and
37 is what we need from <linux/fs.h> under libc systems and is
38 provided in standard includes on glibc systems. So... We
39 switch on what we need... */
40 #include <linux/fs.h>
41 #endif
43 static uid_t mount_uid;
44 static gid_t mount_gid;
45 static int mount_ro;
46 static unsigned mount_fmask;
47 static unsigned mount_dmask;
48 static int user_mount;
49 static char *options;
51 static void
52 help(void)
54 printf("\n");
55 printf("Usage: smbmnt mount-point [options]\n");
56 printf("Version %s\n\n",SAMBA_VERSION_STRING);
57 printf("-s share share name on server\n"
58 "-r mount read-only\n"
59 "-u uid mount as uid\n"
60 "-g gid mount as gid\n"
61 "-f mask permission mask for files\n"
62 "-d mask permission mask for directories\n"
63 "-o options name=value, list of options\n"
64 "-h print this help text\n");
67 static int
68 parse_args(int argc, char *argv[], struct smb_mount_data *data, char **share)
70 int opt;
72 while ((opt = getopt (argc, argv, "s:u:g:rf:d:o:")) != EOF)
74 switch (opt)
76 case 's':
77 *share = optarg;
78 break;
79 case 'u':
80 if (!user_mount) {
81 mount_uid = strtol(optarg, NULL, 0);
83 break;
84 case 'g':
85 if (!user_mount) {
86 mount_gid = strtol(optarg, NULL, 0);
88 break;
89 case 'r':
90 mount_ro = 1;
91 break;
92 case 'f':
93 mount_fmask = strtol(optarg, NULL, 8);
94 break;
95 case 'd':
96 mount_dmask = strtol(optarg, NULL, 8);
97 break;
98 case 'o':
99 options = optarg;
100 break;
101 default:
102 return -1;
105 return 0;
109 static char *
110 fullpath(const char *p)
112 char path[PATH_MAX+1];
114 if (strlen(p) > PATH_MAX) {
115 return NULL;
118 if (realpath(p, path) == NULL) {
119 fprintf(stderr,"Failed to find real path for mount point %s: %s\n",
120 p, strerror(errno));
121 exit(1);
123 return strdup(path);
126 /* Check whether user is allowed to mount on the specified mount point. If it's
127 OK then we change into that directory - this prevents race conditions */
128 static int mount_ok(char *mount_point)
130 struct stat st;
132 if (chdir(mount_point) != 0) {
133 return -1;
136 if (stat(".", &st) != 0) {
137 return -1;
140 if (!S_ISDIR(st.st_mode)) {
141 errno = ENOTDIR;
142 return -1;
145 if ((getuid() != 0) &&
146 ((getuid() != st.st_uid) ||
147 ((st.st_mode & S_IRWXU) != S_IRWXU))) {
148 errno = EPERM;
149 return -1;
152 return 0;
155 /* Tries to mount using the appropriate format. For 2.2 the struct,
156 for 2.4 the ascii version. */
157 static int
158 do_mount(char *share_name, unsigned int flags, struct smb_mount_data *data)
160 char *opts;
161 struct utsname uts;
162 char *release, *major, *minor;
163 char *data1, *data2;
164 int ret;
166 if (asprintf(&opts,
167 "version=7,uid=%d,gid=%d,file_mode=0%o,dir_mode=0%o,%s",
168 mount_uid, mount_gid, data->file_mode,
169 data->dir_mode,options) < 0) {
170 return -1;
173 uname(&uts);
174 release = uts.release;
175 major = strtok(release, ".");
176 minor = strtok(NULL, ".");
177 if (major && minor && atoi(major) == 2 && atoi(minor) < 4) {
178 /* < 2.4, assume struct */
179 data1 = (char *) data;
180 data2 = opts;
181 } else {
182 /* >= 2.4, assume ascii but fall back on struct */
183 data1 = opts;
184 data2 = (char *) data;
187 if (mount(share_name, ".", "smbfs", flags, data1) == 0) {
188 SAFE_FREE(opts);
189 return 0;
191 ret = mount(share_name, ".", "smbfs", flags, data2);
192 SAFE_FREE(opts);
193 return ret;
196 int main(int argc, char *argv[])
198 char *mount_point, *share_name = NULL;
199 FILE *mtab;
200 int fd;
201 unsigned int flags;
202 struct smb_mount_data data;
203 struct mntent ment;
205 memset(&data, 0, sizeof(struct smb_mount_data));
207 if (argc < 2) {
208 help();
209 exit(1);
212 if (argv[1][0] == '-') {
213 help();
214 exit(1);
217 if (getuid() != 0) {
218 user_mount = 1;
221 if (geteuid() != 0) {
222 fprintf(stderr, "smbmnt must be installed suid root for direct user mounts (%d,%d)\n", getuid(), geteuid());
223 exit(1);
226 mount_uid = getuid();
227 mount_gid = getgid();
228 mount_fmask = umask(0);
229 umask(mount_fmask);
230 mount_fmask = ~mount_fmask;
232 mount_point = fullpath(argv[1]);
234 argv += 1;
235 argc -= 1;
237 if (mount_ok(mount_point) != 0) {
238 fprintf(stderr, "cannot mount on %s: %s\n",
239 mount_point, strerror(errno));
240 exit(1);
243 data.version = SMB_MOUNT_VERSION;
245 /* getuid() gives us the real uid, who may umount the fs */
246 data.mounted_uid = getuid();
248 if (parse_args(argc, argv, &data, &share_name) != 0) {
249 help();
250 return -1;
253 data.uid = mount_uid; // truncates to 16-bits here!!!
254 data.gid = mount_gid;
255 data.file_mode = (S_IRWXU|S_IRWXG|S_IRWXO) & mount_fmask;
256 data.dir_mode = (S_IRWXU|S_IRWXG|S_IRWXO) & mount_dmask;
258 if (mount_dmask == 0) {
259 data.dir_mode = data.file_mode;
260 if ((data.dir_mode & S_IRUSR) != 0)
261 data.dir_mode |= S_IXUSR;
262 if ((data.dir_mode & S_IRGRP) != 0)
263 data.dir_mode |= S_IXGRP;
264 if ((data.dir_mode & S_IROTH) != 0)
265 data.dir_mode |= S_IXOTH;
268 flags = MS_MGC_VAL | MS_NOSUID | MS_NODEV;
270 if (mount_ro) flags |= MS_RDONLY;
272 if (do_mount(share_name, flags, &data) < 0) {
273 switch (errno) {
274 case ENODEV:
275 fprintf(stderr, "ERROR: smbfs filesystem not supported by the kernel\n");
276 break;
277 default:
278 perror("mount error");
280 fprintf(stderr, "Please refer to the smbmnt(8) manual page\n");
281 return -1;
284 ment.mnt_fsname = share_name ? share_name : (char *)"none";
285 ment.mnt_dir = mount_point;
286 ment.mnt_type = (char *)"smbfs";
287 ment.mnt_opts = (char *)"";
288 ment.mnt_freq = 0;
289 ment.mnt_passno= 0;
291 mount_point = ment.mnt_dir;
293 if (mount_point == NULL)
295 fprintf(stderr, "Mount point too long\n");
296 return -1;
299 if ((fd = open(MOUNTED"~", O_RDWR|O_CREAT|O_EXCL, 0600)) == -1)
301 fprintf(stderr, "Can't get "MOUNTED"~ lock file");
302 return 1;
304 close(fd);
306 if ((mtab = setmntent(MOUNTED, "a+")) == NULL)
308 fprintf(stderr, "Can't open " MOUNTED);
309 return 1;
312 if (addmntent(mtab, &ment) == 1)
314 fprintf(stderr, "Can't write mount entry");
315 return 1;
317 if (fchmod(fileno(mtab), 0644) == -1)
319 fprintf(stderr, "Can't set perms on "MOUNTED);
320 return 1;
322 endmntent(mtab);
324 if (unlink(MOUNTED"~") == -1)
326 fprintf(stderr, "Can't remove "MOUNTED"~");
327 return 1;
330 return 0;