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