first pass at updating head branch to be to be the same as the SAMBA_2_0 branch
[kai/samba.git] / source / client / smbmnt.c
1 /*
2  *  smbmnt.c
3  *
4  *  Copyright (C) 1995-1998 by Paal-Kr. Engstad and Volker Lendecke
5  *  extensively modified by Tridge
6  *
7  */
8
9 #include "includes.h"
10
11 #include <mntent.h>
12
13 #include <asm/types.h>
14 #include <asm/posix_types.h>
15 #include <linux/smb.h>
16 #include <linux/smb_mount.h>
17 #include <asm/unistd.h>
18
19 #ifndef MS_MGC_VAL
20 /* This may look strange but MS_MGC_VAL is what we are looking for and
21         is what we need from <linux/fs.h> under libc systems and is
22         provided in standard includes on glibc systems.  So...  We
23         switch on what we need...  */
24 #include <linux/fs.h>
25 #endif
26
27 static uid_t mount_uid;
28 static gid_t mount_gid;
29 static int mount_ro;
30 static unsigned mount_fmask;
31 static unsigned mount_dmask;
32 static int user_mount;
33
34 static void
35 help(void)
36 {
37         printf("\n");
38         printf("usage: smbmnt mount-point [options]\n");
39         printf("-s share       share name on server\n"
40                "-r             mount read-only\n"
41                "-u uid         mount as uid\n"
42                "-g gid         mount as gid\n"
43                "-f mask        permission mask for files\n"
44                "-d mask        permission mask for directories\n"
45                "-h             print this help text\n");
46 }
47
48 static int
49 parse_args(int argc, char *argv[], struct smb_mount_data *data, char **share)
50 {
51         int opt;
52
53         while ((opt = getopt (argc, argv, "s:u:g:rf:d:")) != EOF)
54         {
55                 switch (opt)
56                 {
57                 case 's':
58                         *share = optarg;
59                         break;
60                 case 'u':
61                         if (!user_mount) {
62                                 mount_uid = strtol(optarg, NULL, 0);
63                         }
64                         break;
65                 case 'g':
66                         if (!user_mount) {
67                                 mount_gid = strtol(optarg, NULL, 0);
68                         }
69                         break;
70                 case 'r':
71                         mount_ro = 1;
72                         break;
73                 case 'f':
74                         mount_fmask = strtol(optarg, NULL, 8);
75                         break;
76                 case 'd':
77                         mount_dmask = strtol(optarg, NULL, 8);
78                         break;
79                 default:
80                         return -1;
81                 }
82         }
83         return 0;
84         
85 }
86
87 static char *
88 fullpath(const char *p)
89 {
90         char path[MAXPATHLEN];
91
92         if (strlen(p) > MAXPATHLEN-1) {
93                 return NULL;
94         }
95
96         if (realpath(p, path) == NULL) {
97                 fprintf(stderr,"Failed to find real path for mount point\n");
98                 exit(1);
99         }
100         return strdup(path);
101 }
102
103 /* Check whether user is allowed to mount on the specified mount point. If it's
104    OK then we change into that directory - this prevents race conditions */
105 static int mount_ok(char *mount_point)
106 {
107         SMB_STRUCT_STAT st;
108
109         if (chdir(mount_point) != 0) {
110                 return -1;
111         }
112
113         if (sys_stat(".", &st) != 0) {
114                 return -1;
115         }
116
117         if (!S_ISDIR(st.st_mode)) {
118                 errno = ENOTDIR;
119                 return -1;
120         }
121
122         if ((getuid() != 0) && 
123             ((getuid() != st.st_uid) || 
124              ((st.st_mode & S_IRWXU) != S_IRWXU))) {
125                 errno = EPERM;
126                 return -1;
127         }
128
129         return 0;
130 }
131
132  int main(int argc, char *argv[])
133 {
134         char *mount_point, *share_name = NULL;
135         FILE *mtab;
136         int fd;
137         unsigned int flags;
138         struct smb_mount_data data;
139         struct mntent ment;
140
141         memset(&data, 0, sizeof(struct smb_mount_data));
142
143         if (argc < 2) {
144                 help();
145                 exit(1);
146         }
147
148         if (argv[1][0] == '-') {
149                 help();
150                 exit(1);
151         }
152
153         if (getuid() != 0) {
154                 user_mount = 1;
155         }
156
157         if (geteuid() != 0) {
158                 fprintf(stderr, "smbmnt must be installed suid root for direct user mounts (%d,%d)\n", getuid(), geteuid());
159                 exit(1);
160         }
161
162         mount_uid = getuid();
163         mount_gid = getgid();
164         mount_fmask = umask(0);
165         umask(mount_fmask);
166         mount_fmask = ~mount_fmask;
167
168         mount_point = fullpath(argv[1]);
169
170         argv += 1;
171         argc -= 1;
172
173         if (mount_ok(mount_point) != 0) {
174                 fprintf(stderr, "cannot mount on %s: %s\n",
175                         mount_point, strerror(errno));
176                 exit(1);
177         }
178
179         data.version = SMB_MOUNT_VERSION;
180
181         /* getuid() gives us the real uid, who may umount the fs */
182         data.mounted_uid = getuid();
183
184         if (parse_args(argc, argv, &data, &share_name) != 0) {
185                 help();
186                 return -1;
187         }
188
189         data.uid = mount_uid;
190         data.gid = mount_gid;
191         data.file_mode = (S_IRWXU|S_IRWXG|S_IRWXO) & mount_fmask;
192         data.dir_mode  = (S_IRWXU|S_IRWXG|S_IRWXO) & mount_dmask;
193
194         if (mount_dmask == 0) {
195                 data.dir_mode = data.file_mode;
196                 if ((data.dir_mode & S_IRUSR) != 0)
197                         data.dir_mode |= S_IXUSR;
198                 if ((data.dir_mode & S_IRGRP) != 0)
199                         data.dir_mode |= S_IXGRP;
200                 if ((data.dir_mode & S_IROTH) != 0)
201                         data.dir_mode |= S_IXOTH;
202         }
203
204         flags = MS_MGC_VAL;
205
206         if (mount_ro) flags |= MS_RDONLY;
207
208         if (mount(share_name, ".", "smbfs", flags, (char *)&data) < 0)
209         {
210                 switch (errno) {
211                 case ENODEV:
212                         fprintf(stderr, "ERROR: smbfs filesystem not supported by the kernel\n");
213                         break;
214                 default:
215                         perror("mount error");
216                 }
217                 fprintf(stderr, "Please refer to the smbmnt(8) manual page\n");
218                 return -1;
219         }
220
221         ment.mnt_fsname = share_name ? share_name : "none";
222         ment.mnt_dir = mount_point;
223         ment.mnt_type = "smbfs";
224         ment.mnt_opts = "";
225         ment.mnt_freq = 0;
226         ment.mnt_passno= 0;
227
228         mount_point = ment.mnt_dir;
229
230         if (mount_point == NULL)
231         {
232                 fprintf(stderr, "Mount point too long\n");
233                 return -1;
234         }
235         
236         if ((fd = open(MOUNTED"~", O_RDWR|O_CREAT|O_EXCL, 0600)) == -1)
237         {
238                 fprintf(stderr, "Can't get "MOUNTED"~ lock file");
239                 return 1;
240         }
241         close(fd);
242         
243         if ((mtab = setmntent(MOUNTED, "a+")) == NULL)
244         {
245                 fprintf(stderr, "Can't open " MOUNTED);
246                 return 1;
247         }
248
249         if (addmntent(mtab, &ment) == 1)
250         {
251                 fprintf(stderr, "Can't write mount entry");
252                 return 1;
253         }
254         if (fchmod(fileno(mtab), 0644) == -1)
255         {
256                 fprintf(stderr, "Can't set perms on "MOUNTED);
257                 return 1;
258         }
259         endmntent(mtab);
260
261         if (unlink(MOUNTED"~") == -1)
262         {
263                 fprintf(stderr, "Can't remove "MOUNTED"~");
264                 return 1;
265         }
266
267         return 0;
268 }