Add mount.cifs as a wrapper for mount.cifs.
[Samba.git] / source / client / smbmnt.c
blob3d7de1b3c07928e8d88d8bac7f49a0ad9448269a
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;
165 char *saveptr = NULL;
167 if (asprintf(&opts,
168 "version=7,uid=%d,gid=%d,file_mode=0%o,dir_mode=0%o,%s",
169 mount_uid, mount_gid, data->file_mode,
170 data->dir_mode,options) < 0) {
171 return -1;
174 uname(&uts);
175 release = uts.release;
176 major = strtok_r(release, ".", &saveptr);
177 minor = strtok_r(NULL, ".", &saveptr);
178 if (major && minor && atoi(major) == 2 && atoi(minor) < 4) {
179 /* < 2.4, assume struct */
180 data1 = (char *) data;
181 data2 = opts;
182 } else {
183 /* >= 2.4, assume ascii but fall back on struct */
184 data1 = opts;
185 data2 = (char *) data;
188 if (mount(share_name, ".", "smbfs", flags, data1) == 0) {
189 SAFE_FREE(opts);
190 return 0;
192 ret = mount(share_name, ".", "smbfs", flags, data2);
193 SAFE_FREE(opts);
194 return ret;
197 int main(int argc, char *argv[])
199 char *mount_point, *share_name = NULL;
200 FILE *mtab;
201 int fd;
202 unsigned int flags;
203 struct smb_mount_data data;
204 struct mntent ment;
206 memset(&data, 0, sizeof(struct smb_mount_data));
208 if (argc < 2) {
209 help();
210 exit(1);
213 if (argv[1][0] == '-') {
214 help();
215 exit(1);
218 if (getuid() != 0) {
219 user_mount = 1;
222 if (geteuid() != 0) {
223 fprintf(stderr, "smbmnt must be installed suid root for direct user mounts (%d,%d)\n", getuid(), geteuid());
224 exit(1);
227 mount_uid = getuid();
228 mount_gid = getgid();
229 mount_fmask = umask(0);
230 umask(mount_fmask);
231 mount_fmask = ~mount_fmask;
233 mount_point = fullpath(argv[1]);
235 argv += 1;
236 argc -= 1;
238 if (mount_ok(mount_point) != 0) {
239 fprintf(stderr, "cannot mount on %s: %s\n",
240 mount_point, strerror(errno));
241 exit(1);
244 data.version = SMB_MOUNT_VERSION;
246 /* getuid() gives us the real uid, who may umount the fs */
247 data.mounted_uid = getuid();
249 if (parse_args(argc, argv, &data, &share_name) != 0) {
250 help();
251 return -1;
254 data.uid = mount_uid; /* truncates to 16-bits here!!! */
255 data.gid = mount_gid;
256 data.file_mode = (S_IRWXU|S_IRWXG|S_IRWXO) & mount_fmask;
257 data.dir_mode = (S_IRWXU|S_IRWXG|S_IRWXO) & mount_dmask;
259 if (mount_dmask == 0) {
260 data.dir_mode = data.file_mode;
261 if ((data.dir_mode & S_IRUSR) != 0)
262 data.dir_mode |= S_IXUSR;
263 if ((data.dir_mode & S_IRGRP) != 0)
264 data.dir_mode |= S_IXGRP;
265 if ((data.dir_mode & S_IROTH) != 0)
266 data.dir_mode |= S_IXOTH;
269 flags = MS_MGC_VAL | MS_NOSUID | MS_NODEV;
271 if (mount_ro) flags |= MS_RDONLY;
273 if (do_mount(share_name, flags, &data) < 0) {
274 switch (errno) {
275 case ENODEV:
276 fprintf(stderr, "ERROR: smbfs filesystem not supported by the kernel\n");
277 break;
278 default:
279 perror("mount error");
281 fprintf(stderr, "Please refer to the smbmnt(8) manual page\n");
282 return -1;
285 ment.mnt_fsname = share_name ? share_name : (char *)"none";
286 ment.mnt_dir = mount_point;
287 ment.mnt_type = (char *)"smbfs";
288 ment.mnt_opts = (char *)"";
289 ment.mnt_freq = 0;
290 ment.mnt_passno= 0;
292 mount_point = ment.mnt_dir;
294 if (mount_point == NULL)
296 fprintf(stderr, "Mount point too long\n");
297 return -1;
300 if ((fd = open(MOUNTED"~", O_RDWR|O_CREAT|O_EXCL, 0600)) == -1)
302 fprintf(stderr, "Can't get "MOUNTED"~ lock file");
303 return 1;
305 close(fd);
307 if ((mtab = setmntent(MOUNTED, "a+")) == NULL)
309 fprintf(stderr, "Can't open " MOUNTED);
310 return 1;
313 if (addmntent(mtab, &ment) == 1)
315 fprintf(stderr, "Can't write mount entry");
316 return 1;
318 if (fchmod(fileno(mtab), 0644) == -1)
320 fprintf(stderr, "Can't set perms on "MOUNTED);
321 return 1;
323 endmntent(mtab);
325 if (unlink(MOUNTED"~") == -1)
327 fprintf(stderr, "Can't remove "MOUNTED"~");
328 return 1;
331 return 0;