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