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