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