]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - usr.sbin/mount_nwfs/mount_nwfs.c
Copy stable/8 to releng/8.1 in preparation for 8.1-RC1.
[FreeBSD/releng/8.1.git] / usr.sbin / mount_nwfs / mount_nwfs.c
1 /*
2  * Copyright (c) 1999, 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  * $FreeBSD$
33  */
34 #include <sys/param.h>
35 #include <sys/stat.h>
36 #include <sys/errno.h>
37 #include <sys/mount.h>
38 #include <sys/sysctl.h>
39 #include <machine/cpu.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 #include <time.h>
51
52 #include <netncp/ncp_lib.h>
53 #include <netncp/ncp_rcfile.h>
54 #include <fs/nwfs/nwfs_mount.h>
55 #include "mntopts.h"
56
57 #define NWFS_VFSNAME    "nwfs"
58
59 static char mount_point[MAXPATHLEN + 1];
60 static void usage(void);
61 static int parsercfile(struct ncp_conn_loginfo *li, struct nwfs_args *mdata);
62
63 static struct mntopt mopts[] = {
64         MOPT_STDOPTS,
65         { NULL }
66 };
67
68 static int 
69 parsercfile(struct ncp_conn_loginfo *li, struct nwfs_args *mdata) {
70         return 0;
71 }
72
73 int
74 main(int argc, char *argv[]) {
75         NWCONN_HANDLE connHandle;
76         struct nwfs_args mdata;
77         struct ncp_conn_loginfo li;
78         struct stat st;
79         struct nw_entry_info einfo;
80         struct tm *tm;
81         time_t ltime;
82         int opt, error, mntflags, nlsopt, wall_clock;
83         size_t len;
84         char *p, *p1, tmp[1024];
85         u_char *pv;
86
87         if (argc < 2)
88                 usage();
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", NWFS_VERSION / 100000,
94                             (NWFS_VERSION % 10000) / 1000,
95                             (NWFS_VERSION % 1000) / 100);
96                 }
97         }
98
99         if(ncp_initlib()) exit(1);
100
101         mntflags = error = 0;
102         bzero(&mdata,sizeof(mdata));
103         mdata.uid = mdata.gid = -1;
104         nlsopt = 0;
105
106         if (ncp_li_init(&li, argc, argv)) return 1;
107         /*
108          * A little bit weird, but I should figure out which server/user to use
109          * _before_ reading .rc file
110          */
111         if (argc >= 3 && argv[argc-1][0] != '-' && argv[argc-2][0] != '-' &&
112             argv[argc-2][0] == '/') {
113                 p = argv[argc-2];
114                 error = 1;
115                 do {
116                         if (*p++ != '/') break;
117                         p1 = tmp;
118                         while (*p != ':' && *p != 0) *p1++ = *p++;
119                         if (*p++ == 0) break;
120                         *p1 = 0;
121                         if (ncp_li_setserver(&li, tmp)) break;
122                         p1 = tmp;
123                         while (*p != '/' && *p != 0) *p1++ = *p++;
124                         if (*p++ == 0) break;
125                         *p1 = 0;
126                         if (ncp_li_setuser(&li, tmp)) break;
127                         p1 = tmp;
128                         while (*p != '/' && *p != 0) *p1++ = *p++;
129                         *p1 = 0;
130                         if (strlen(tmp) > NCP_VOLNAME_LEN) {
131                                 warnx("volume name too long: %s", tmp);
132                                 break;
133                         }
134                         ncp_str_upper(strcpy(mdata.mounted_vol,tmp));
135                         if (*p == '/')
136                                 p++;
137                         p1 = mdata.root_path + 2;
138                         pv = mdata.root_path + 1;
139                         for(;*p;) {
140                                 *pv = 0;
141                                 while (*p != '/' && *p) {
142                                         *p1++ = *p++;
143                                         (*pv)++;
144                                 }
145                                 if (*pv) {
146                                         ncp_nls_mem_u2n(pv + 1, pv + 1, *pv);
147                                         pv += (*pv) + 1;
148                                         mdata.root_path[0]++;
149                                 }
150                                 if (*p++ == 0) break;
151                                 p1++;
152                         }
153                         error = 0;
154                 } while(0);
155                 if (error)
156                         errx(EX_DATAERR, 
157                             "an error occurred while parsing '%s'",
158                             argv[argc - 2]);
159         }
160         if (ncp_li_readrc(&li)) return 1;
161         if (ncp_rc) {
162                 parsercfile(&li,&mdata);
163                 rc_close(ncp_rc);
164         }
165         while ((opt = getopt(argc, argv, STDPARAM_OPT"V:c:d:f:g:l:n:o:u:w:")) != -1) {
166                 switch (opt) {
167                     case STDPARAM_ARGS:
168                         if (ncp_li_arg(&li, opt, optarg)) {     
169                                 return 1;
170                         }
171                         break;
172                     case 'V':
173                         if (strlen(optarg) > NCP_VOLNAME_LEN)
174                                 errx(EX_DATAERR, "volume too long: %s", optarg);
175                         ncp_str_upper(strcpy(mdata.mounted_vol,optarg));
176                         break;
177                     case 'u': {
178                         struct passwd *pwd;
179
180                         pwd = isdigit(optarg[0]) ?
181                             getpwuid(atoi(optarg)) : getpwnam(optarg);
182                         if (pwd == NULL)
183                                 errx(EX_NOUSER, "unknown user '%s'", optarg);
184                         mdata.uid = pwd->pw_uid;
185                         break;
186                     }
187                     case 'g': {
188                         struct group *grp;
189
190                         grp = isdigit(optarg[0]) ?
191                             getgrgid(atoi(optarg)) : getgrnam(optarg);
192                         if (grp == NULL)
193                                 errx(EX_NOUSER, "unknown group '%s'", optarg);
194                         mdata.gid = grp->gr_gid;
195                         break;
196                     }
197                     case 'd':
198                         errno = 0;
199                         mdata.dir_mode = strtol(optarg, &p, 8);
200                         if (errno || *p != 0)
201                                 errx(EX_DATAERR, "invalid value for directory mode");
202                         break;
203                     case 'f':
204                         errno = 0;
205                         mdata.file_mode = strtol(optarg, &p, 8);
206                         if (errno || *p != 0)
207                                 errx(EX_DATAERR, "invalid value for file mode");
208                         break;
209                     case '?':
210                         usage();
211                         /*NOTREACHED*/
212                     case 'n': {
213                         char *inp, *nsp;
214
215                         nsp = inp = optarg;
216                         while ((nsp = strsep(&inp, ",;:")) != NULL) {
217                                 if (strcasecmp(nsp, "OS2") == 0)
218                                         mdata.flags |= NWFS_MOUNT_NO_OS2;
219                                 else if (strcasecmp(nsp, "LONG") == 0)
220                                         mdata.flags |= NWFS_MOUNT_NO_LONG;
221                                 else if (strcasecmp(nsp, "NFS") == 0)
222                                         mdata.flags |= NWFS_MOUNT_NO_NFS;
223                                 else
224                                         errx(EX_DATAERR, "unknown namespace '%s'", nsp);
225                         }
226                         break;
227                     };
228                     case 'l':
229                         if (ncp_nls_setlocale(optarg) != 0) return 1;
230                         mdata.flags |= NWFS_MOUNT_HAVE_NLS;
231                         break;
232                     case 'o':
233                         getmntopts(optarg, mopts, &mntflags, 0);
234                         break;
235                     case 'c':
236                         switch (optarg[0]) {
237                             case 'l':
238                                 nlsopt |= NWHP_LOWER;
239                                 break;
240                             case 'u':
241                                 nlsopt |= NWHP_UPPER;
242                                 break;
243                             case 'n':
244                                 nlsopt |= NWHP_LOWER | NWHP_UPPER;
245                                 break;
246                             case 'L':
247                                 nlsopt |= NWHP_LOWER | NWHP_NOSTRICT;
248                                 break;
249                             case 'U':
250                                 nlsopt |= NWHP_UPPER | NWHP_NOSTRICT;
251                                 break;
252                             default:
253                                 errx(EX_DATAERR, "invalid suboption '%c' for -c",
254                                     optarg[0]);
255                         }
256                         break;
257                     case 'w':
258                         if (ncp_nls_setrecodebyname(optarg) != 0)
259                                 return 1;
260                         mdata.flags |= NWFS_MOUNT_HAVE_NLS;
261                         break;
262                     default:
263                         usage();
264                 }
265         }
266
267         if (optind == argc - 2) {
268                 optind++;
269         } else if (mdata.mounted_vol[0] == 0)
270                 errx(EX_USAGE, "volume name should be specified");
271         
272         if (optind != argc - 1)
273                 usage();
274         realpath(argv[optind], mount_point);
275
276         if (stat(mount_point, &st) == -1)
277                 err(EX_OSERR, "could not find mount point %s", mount_point);
278         if (!S_ISDIR(st.st_mode)) {
279                 errno = ENOTDIR;
280                 err(EX_OSERR, "can't mount on %s", mount_point);
281         }
282         if (ncp_geteinfo(mount_point, &einfo) == 0)
283                 errx(EX_OSERR, "can't mount on %s twice", mount_point);
284
285         if (mdata.uid == -1) {
286                 mdata.uid = st.st_uid;
287         }
288         if (mdata.gid == -1) {
289                 mdata.gid = st.st_gid;
290         }
291         if (mdata.file_mode == 0 ) {
292                 mdata.file_mode = st.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO);
293         }
294         if (mdata.dir_mode == 0) {
295                 mdata.dir_mode = mdata.file_mode;
296                 if ((mdata.dir_mode & S_IRUSR) != 0)
297                         mdata.dir_mode |= S_IXUSR;
298                 if ((mdata.dir_mode & S_IRGRP) != 0)
299                         mdata.dir_mode |= S_IXGRP;
300                 if ((mdata.dir_mode & S_IROTH) != 0)
301                         mdata.dir_mode |= S_IXOTH;
302         }
303         if (li.access_mode == 0) {
304                 li.access_mode = mdata.dir_mode;
305         }
306 /*      if (mdata.flags & NWFS_MOUNT_HAVE_NLS) {*/
307                 mdata.nls = ncp_nls;
308 /*      }*/
309         mdata.nls.opt = nlsopt;
310
311         len = sizeof(wall_clock);
312         if (sysctlbyname("machdep.wall_cmos_clock", &wall_clock, &len, NULL, 0) == -1)
313                 err(EX_OSERR, "get wall_clock");
314         if (wall_clock == 0) {
315                 time(&ltime);
316                 tm = localtime(&ltime);
317                 mdata.tz = -(tm->tm_gmtoff / 60);
318         }
319
320         error = ncp_li_check(&li);
321         if (error)
322                 return 1;
323         li.opt |= NCP_OPT_WDOG;
324         /* well, now we can try to login, or use already established connection */
325         error = ncp_li_login(&li, &connHandle);
326         if (error) {
327                 ncp_error("cannot login to server %s", error, li.server);
328                 exit(1);
329         }
330         error = ncp_conn2ref(connHandle, &mdata.connRef);
331         if (error) {
332                 ncp_error("could not convert handle to reference", error);
333                 ncp_disconnect(connHandle);
334                 exit(1);
335         }
336         strcpy(mdata.mount_point,mount_point);
337         mdata.version = NWFS_VERSION;
338         error = mount(NWFS_VFSNAME, mdata.mount_point, mntflags, (void*)&mdata);
339         if (error) {
340                 ncp_error("mount error: %s", error, mdata.mount_point);
341                 ncp_disconnect(connHandle);
342                 exit(1);
343         }
344         /*
345          * I'm leave along my handle, but kernel should keep own ...
346          */
347         ncp_disconnect(connHandle);
348         /* we are done ?, impossible ... */
349         return 0;
350 }
351
352 static void
353 usage(void)
354 {
355         fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n",
356         "usage: mount_nwfs [-Chv] -S server -U user [-connection options]",
357         "                  -V volume [-M mode] [-c case] [-d mode] [-f mode]",
358         "                  [-g gid] [-l locale] [-n os2] [-u uid] [-w scheme]",
359         "                  node",
360         "       mount_nwfs [-options] /server:user/volume[/path] node");
361
362         exit (1);
363 }