]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/smbfs/mount_smbfs/mount_smbfs.c
merge fix for boot-time hang on centos' xen
[FreeBSD/FreeBSD.git] / contrib / smbfs / mount_smbfs / mount_smbfs.c
1 /*
2  * Copyright (c) 2000-2002, Boris Popov
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *    This product includes software developed by Boris Popov.
16  * 4. Neither the name of the author nor the names of any co-contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  *
32  * $Id: mount_smbfs.c,v 1.17 2002/04/10 04:17:51 bp Exp $
33  * $FreeBSD$
34  */
35 #include <sys/param.h>
36 #include <sys/stat.h>
37 #include <sys/errno.h>
38 #include <sys/linker.h>
39 #include <sys/mount.h>
40
41 #include <stdio.h>
42 #include <string.h>
43 #include <pwd.h>
44 #include <grp.h>
45 #include <unistd.h>
46 #include <ctype.h>
47 #include <stdlib.h>
48 #include <err.h>
49 #include <sysexits.h>
50
51 #include <cflib.h>
52
53 #include <netsmb/smb.h>
54 #include <netsmb/smb_conn.h>
55 #include <netsmb/smb_lib.h>
56
57 #include <fs/smbfs/smbfs.h>
58
59 #include "mntopts.h"
60
61 static char mount_point[MAXPATHLEN + 1];
62 static void usage(void);
63
64 static struct mntopt mopts[] = {
65         MOPT_STDOPTS,
66         { NULL, 0, 0, 0 }
67 };
68
69
70 int
71 main(int argc, char *argv[])
72 {
73         struct smb_ctx sctx, *ctx = &sctx;
74         struct smbfs_args mdata;
75         struct stat st;
76 #ifdef APPLE
77         extern void dropsuid();
78         extern int loadsmbvfs();
79 #else
80         struct xvfsconf vfc;
81 #endif
82         char *next;
83         int opt, error, mntflags, caseopt;
84
85
86 #ifdef APPLE
87         dropsuid();
88 #endif
89         if (argc == 2) {
90                 if (strcmp(argv[1], "-h") == 0) {
91                         usage();
92                 } else if (strcmp(argv[1], "-v") == 0) {
93                         errx(EX_OK, "version %d.%d.%d", SMBFS_VERSION / 100000,
94                             (SMBFS_VERSION % 10000) / 1000,
95                             (SMBFS_VERSION % 1000) / 100);
96                 }
97         }
98         if (argc < 3)
99                 usage();
100
101 #ifdef APPLE
102         error = loadsmbvfs();
103 #else
104         error = getvfsbyname(SMBFS_VFSNAME, &vfc);
105         if (error) {
106                 if (kldload(SMBFS_VFSNAME) < 0)
107                         err(EX_OSERR, "kldload("SMBFS_VFSNAME")");
108                 error = getvfsbyname(SMBFS_VFSNAME, &vfc);
109         }
110 #endif
111         if (error)
112                 errx(EX_OSERR, "SMB filesystem is not available");
113
114         if (smb_lib_init() != 0)
115                 exit(1);
116
117         mntflags = error = 0;
118         bzero(&mdata, sizeof(mdata));
119         mdata.uid = mdata.gid = -1;
120         caseopt = SMB_CS_NONE;
121
122         if (smb_ctx_init(ctx, argc, argv, SMBL_SHARE, SMBL_SHARE, SMB_ST_DISK) != 0)
123                 exit(1);
124         if (smb_ctx_readrc(ctx) != 0)
125                 exit(1);
126         if (smb_rc)
127                 rc_close(smb_rc);
128
129         while ((opt = getopt(argc, argv, STDPARAM_OPT"c:d:f:g:l:n:o:u:w:")) != -1) {
130                 switch (opt) {
131                     case STDPARAM_ARGS:
132                         error = smb_ctx_opt(ctx, opt, optarg);
133                         if (error)
134                                 exit(1);
135                         break;
136                     case 'u': {
137                         struct passwd *pwd;
138
139                         pwd = isdigit(optarg[0]) ?
140                             getpwuid(atoi(optarg)) : getpwnam(optarg);
141                         if (pwd == NULL)
142                                 errx(EX_NOUSER, "unknown user '%s'", optarg);
143                         mdata.uid = pwd->pw_uid;
144                         break;
145                     }
146                     case 'g': {
147                         struct group *grp;
148
149                         grp = isdigit(optarg[0]) ?
150                             getgrgid(atoi(optarg)) : getgrnam(optarg);
151                         if (grp == NULL)
152                                 errx(EX_NOUSER, "unknown group '%s'", optarg);
153                         mdata.gid = grp->gr_gid;
154                         break;
155                     }
156                     case 'd':
157                         errno = 0;
158                         mdata.dir_mode = strtol(optarg, &next, 8);
159                         if (errno || *next != 0)
160                                 errx(EX_DATAERR, "invalid value for directory mode");
161                         break;
162                     case 'f':
163                         errno = 0;
164                         mdata.file_mode = strtol(optarg, &next, 8);
165                         if (errno || *next != 0)
166                                 errx(EX_DATAERR, "invalid value for file mode");
167                         break;
168                     case '?':
169                         usage();
170                         /*NOTREACHED*/
171                     case 'n': {
172                         char *inp, *nsp;
173
174                         nsp = inp = optarg;
175                         while ((nsp = strsep(&inp, ",;:")) != NULL) {
176                                 if (strcasecmp(nsp, "LONG") == 0)
177                                         mdata.flags |= SMBFS_MOUNT_NO_LONG;
178                                 else
179                                         errx(EX_DATAERR, "unknown suboption '%s'", nsp);
180                         }
181                         break;
182                     };
183                     case 'o':
184                         getmntopts(optarg, mopts, &mntflags, 0);
185                         break;
186                     case 'c':
187                         switch (optarg[0]) {
188                             case 'l':
189                                 caseopt |= SMB_CS_LOWER;
190                                 break;
191                             case 'u':
192                                 caseopt |= SMB_CS_UPPER;
193                                 break;
194                             default:
195                                 errx(EX_DATAERR, "invalid suboption '%c' for -c",
196                                     optarg[0]);
197                         }
198                         break;
199                     default:
200                         usage();
201                 }
202         }
203
204         if (optind == argc - 2)
205                 optind++;
206         
207         if (optind != argc - 1)
208                 usage();
209         realpath(argv[optind], mount_point);
210
211         if (stat(mount_point, &st) == -1)
212                 err(EX_OSERR, "could not find mount point %s", mount_point);
213         if (!S_ISDIR(st.st_mode)) {
214                 errno = ENOTDIR;
215                 err(EX_OSERR, "can't mount on %s", mount_point);
216         }
217 /*
218         if (smb_getextattr(mount_point, &einfo) == 0)
219                 errx(EX_OSERR, "can't mount on %s twice", mount_point);
220 */
221         if (mdata.uid == (uid_t)-1)
222                 mdata.uid = st.st_uid;
223         if (mdata.gid == (gid_t)-1)
224                 mdata.gid = st.st_gid;
225         if (mdata.file_mode == 0 )
226                 mdata.file_mode = st.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO);
227         if (mdata.dir_mode == 0) {
228                 mdata.dir_mode = mdata.file_mode;
229                 if (mdata.dir_mode & S_IRUSR)
230                         mdata.dir_mode |= S_IXUSR;
231                 if (mdata.dir_mode & S_IRGRP)
232                         mdata.dir_mode |= S_IXGRP;
233                 if (mdata.dir_mode & S_IROTH)
234                         mdata.dir_mode |= S_IXOTH;
235         }
236         /*
237          * For now, let connection be private for this mount
238          */
239         ctx->ct_ssn.ioc_opt |= SMBVOPT_PRIVATE;
240         ctx->ct_ssn.ioc_owner = ctx->ct_sh.ioc_owner = 0; /* root */
241         ctx->ct_ssn.ioc_group = ctx->ct_sh.ioc_group = mdata.gid;
242         opt = 0;
243         if (mdata.dir_mode & S_IXGRP)
244                 opt |= SMBM_EXECGRP;
245         if (mdata.dir_mode & S_IXOTH)
246                 opt |= SMBM_EXECOTH;
247         ctx->ct_ssn.ioc_rights |= opt;
248         ctx->ct_sh.ioc_rights |= opt;
249         error = smb_ctx_resolve(ctx);
250         if (error)
251                 exit(1);
252         error = smb_ctx_lookup(ctx, SMBL_SHARE, SMBLK_CREATE);
253         if (error) {
254                 exit(1);
255         }
256         strcpy(mdata.mount_point,mount_point);
257         mdata.version = SMBFS_VERSION;
258         mdata.dev = ctx->ct_fd;
259         mdata.caseopt = caseopt;
260         error = mount(SMBFS_VFSNAME, mdata.mount_point, mntflags, (void*)&mdata);
261         smb_ctx_done(ctx);
262         if (error) {
263                 smb_error("mount error: %s", error, mdata.mount_point);
264                 exit(1);
265         }
266         return 0;
267 }
268
269 static void
270 usage(void)
271 {
272         fprintf(stderr, "%s\n%s\n%s\n%s\n",
273         "usage: mount_smbfs [-E cs1:cs2] [-I host] [-L locale] [-M crights:srights]",
274         "                   [-N] [-O cowner:cgroup/sowner:sgroup] [-R retrycount]",
275         "                   [-T timeout] [-W workgroup] [-c case] [-d mode] [-f mode]",
276         "                   [-g gid] [-n opt] [-u uid] //user@server/share node");
277
278         exit (1);
279 }