1 /*-
2 * Copyright (c) 1999, 2001 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: releng/8.4/sys/fs/nwfs/nwfs_subr.c 187959 2009-01-31 17:36:22Z bz $
33 */
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/lock.h>
38 #include <sys/lockmgr.h>
39 #include <sys/malloc.h>
40 #include <sys/clock.h>
41 #include <sys/time.h>
42
43 #include <netncp/ncp.h>
44 #include <netncp/ncp_conn.h>
45 #include <netncp/ncp_ncp.h>
46 #include <netncp/ncp_subr.h>
47 #include <netncp/ncp_rq.h>
48 #include <netncp/nwerror.h>
49
50 #include <fs/nwfs/nwfs.h>
51 #include <fs/nwfs/nwfs_node.h>
52 #include <fs/nwfs/nwfs_subr.h>
53
54 #define NCP_INFOSZ (sizeof(struct nw_entry_info) - 257)
55
56 MALLOC_DEFINE(M_NWFSDATA, "nwfs_data", "NWFS private data");
57
58 static int
59 ncp_extract_file_info(struct nwmount *nmp, struct ncp_rq *rqp,
60 struct nw_entry_info *target, int withname)
61 {
62 u_int8_t name_len;
63
64 md_get_mem(&rqp->rp, (caddr_t)target, NCP_INFOSZ, MB_MSYSTEM);
65 if (!withname)
66 return 0;
67 md_get_uint8(&rqp->rp, &name_len);
68 target->nameLen = name_len;
69 md_get_mem(&rqp->rp, (caddr_t)target->entryName, name_len, MB_MSYSTEM);
70 target->entryName[name_len] = '\0';
71 ncp_path2unix(target->entryName, target->entryName, name_len, &nmp->m.nls);
72 return 0;
73 }
74
75 int
76 ncp_initsearch(struct vnode *dvp, struct thread *td, struct ucred *cred)
77 {
78 struct nwmount *nmp = VTONWFS(dvp);
79 struct ncp_conn *conn = NWFSTOCONN(nmp);
80 struct nwnode *np = VTONW(dvp);
81 struct ncp_rq *rqp;
82 u_int8_t volnum = nmp->n_volume;
83 u_int32_t dirent = np->n_fid.f_id;
84 int error;
85
86 NCPNDEBUG("vol=%d,dir=%d\n", volnum, dirent);
87 error = ncp_rq_alloc(87, conn, td, cred, &rqp);
88 if (error)
89 return error;
90 mb_put_uint8(&rqp->rq, 2); /* subfunction */
91 mb_put_uint8(&rqp->rq, nmp->name_space);
92 mb_put_uint8(&rqp->rq, 0); /* reserved */
93 ncp_rq_dbase_path(rqp, volnum, dirent, 0, NULL, NULL);
94 rqp->nr_minrplen = sizeof(np->n_seq);
95 error = ncp_request(rqp);
96 if (error)
97 return error;
98 md_get_mem(&rqp->rp, (caddr_t)&np->n_seq, sizeof(np->n_seq), MB_MSYSTEM);
99 ncp_rq_done(rqp);
100 return 0;
101 }
102
103 int
104 ncp_search_for_file_or_subdir(struct nwmount *nmp,
105 struct nw_search_seq *seq,
106 struct nw_entry_info *target,
107 struct thread *td,struct ucred *cred)
108 {
109 struct ncp_conn *conn = NWFSTOCONN(nmp);
110 struct ncp_rq *rqp;
111 int error;
112
113 error = ncp_rq_alloc(87, conn, td, cred, &rqp);
114 if (error)
115 return error;
116 mb_put_uint8(&rqp->rq, 3); /* subfunction */
117 mb_put_uint8(&rqp->rq, nmp->name_space);
118 mb_put_uint8(&rqp->rq, 0); /* data stream */
119 mb_put_uint16le(&rqp->rq, 0xffff); /* Search attribs */
120 mb_put_uint32le(&rqp->rq, IM_ALL); /* return info mask */
121 mb_put_mem(&rqp->rq, (caddr_t)seq, 9, MB_MSYSTEM);
122 mb_put_uint8(&rqp->rq, 2); /* 2 byte pattern */
123 mb_put_uint8(&rqp->rq, 0xff); /* following is a wildcard */
124 mb_put_uint8(&rqp->rq, '*');
125 rqp->nr_minrplen = sizeof(*seq) + 1 + NCP_INFOSZ + 1;
126 error = ncp_request(rqp);
127 if (error)
128 return error;
129 md_get_mem(&rqp->rp, (caddr_t)seq, sizeof(*seq), MB_MSYSTEM);
130 md_get_uint8(&rqp->rp, NULL); /* skip */
131 error = ncp_extract_file_info(nmp, rqp, target, 1);
132 ncp_rq_done(rqp);
133 return error;
134 }
135
136 /*
137 * Returns information for a (one-component) name relative to the specified
138 * directory.
139 */
140 int
141 ncp_obtain_info(struct nwmount *nmp, u_int32_t dirent,
142 int namelen, char *path, struct nw_entry_info *target,
143 struct thread *td,struct ucred *cred)
144 {
145 struct ncp_conn *conn=NWFSTOCONN(nmp);
146 struct ncp_rq *rqp;
147 int error;
148 u_char volnum = nmp->n_volume, ns;
149
150 if (target == NULL) {
151 NCPFATAL("target == NULL\n");
152 return EINVAL;
153 }
154 ns = (path == NULL || path[0] == 0) ? NW_NS_DOS : nmp->name_space;
155 error = ncp_rq_alloc(87, conn, td, cred, &rqp);
156 if (error)
157 return error;
158 mb_put_uint8(&rqp->rq, 6); /* subfunction */
159 mb_put_uint8(&rqp->rq, ns);
160 mb_put_uint8(&rqp->rq, ns); /* DestNameSpace */
161 mb_put_uint16le(&rqp->rq, 0xff); /* get all */
162 mb_put_uint32le(&rqp->rq, IM_ALL);
163 ncp_rq_dbase_path(rqp, volnum, dirent, namelen, path, &nmp->m.nls);
164 error = ncp_request(rqp);
165 if (error)
166 return error;
167 error = ncp_extract_file_info(nmp, rqp, target, path != NULL);
168 ncp_rq_done(rqp);
169 return error;
170 }
171 /*
172 * lookup name pointed by cnp in directory dvp and return file info in np.
173 * May be I should create a little cache, but another way is to minimize
174 * number of calls, on other hand, in multiprocess environment ...
175 */
176 int
177 ncp_lookup(struct vnode *dvp, int len, char *name, struct nw_entry_info *fap,
178 struct thread *td,struct ucred *cred)
179 {
180 struct nwmount *nmp;
181 struct nwnode *dnp;
182 int error;
183
184 if (!dvp || dvp->v_type != VDIR) {
185 nwfs_printf("dvp is NULL or not a directory.\n");
186 return (ENOENT);
187 }
188 dnp = VTONW(dvp);
189 nmp = VTONWFS(dvp);
190
191 if (len == 1 && name[0] == '.') {
192 if (dnp->n_flag & NVOLUME) {
193 error = ncp_obtain_info(nmp, dnp->n_fid.f_id, 0, NULL,
194 fap, td, cred);
195 } else {
196 error = ncp_obtain_info(nmp, dnp->n_fid.f_parent,
197 dnp->n_nmlen, dnp->n_name, fap, td, cred);
198 }
199 return error;
200 } else if (len == 2 && name[0] == '.' && name[1] == '.') {
201 printf("%s: knows NOTHING about '..'\n", __func__);
202 return EIO;
203 } else {
204 error = ncp_obtain_info(nmp, dnp->n_fid.f_id,
205 len, name, fap, td, cred);
206 }
207 return error;
208 }
209
210 static void ConvertToNWfromDWORD(u_int32_t sfd, ncp_fh *fh);
211 static void
212 ConvertToNWfromDWORD(u_int32_t sfd, ncp_fh *fh) {
213 fh->val1 = (fh->val.val32 = sfd);
214 return;
215 }
216
217 /*
218 * If both dir and name are NULL, then in target there's already a looked-up
219 * entry that wants to be opened.
220 */
221 int
222 ncp_open_create_file_or_subdir(struct nwmount *nmp,struct vnode *dvp,int namelen,
223 char *name, int open_create_mode, u_int32_t create_attributes,
224 int desired_acc_rights, struct ncp_open_info *nop,
225 struct thread *td,struct ucred *cred)
226 {
227
228 struct ncp_conn *conn=NWFSTOCONN(nmp);
229 struct ncp_rq *rqp;
230 u_int16_t search_attribs = SA_ALL & (~SA_SUBDIR_FILES);
231 u_int8_t volnum;
232 u_int32_t dirent;
233 int error;
234
235 error = ncp_rq_alloc(87, conn, td, cred, &rqp);
236 if (error)
237 return error;
238 volnum = nmp->n_volume;
239 dirent = VTONW(dvp)->n_fid.f_id;
240 if ((create_attributes & aDIR) != 0) {
241 search_attribs |= SA_SUBDIR_FILES;
242 }
243 mb_put_uint8(&rqp->rq, 1);/* subfunction */
244 mb_put_uint8(&rqp->rq, nmp->name_space);
245 mb_put_uint8(&rqp->rq, open_create_mode);
246 mb_put_uint16le(&rqp->rq, search_attribs);
247 mb_put_uint32le(&rqp->rq, IM_ALL);
248 mb_put_uint32le(&rqp->rq, create_attributes);
249 /*
250 * The desired acc rights seem to be the inherited rights mask for
251 * directories
252 */
253 mb_put_uint16le(&rqp->rq, desired_acc_rights);
254 ncp_rq_dbase_path(rqp, volnum, dirent, namelen, name, &nmp->m.nls);
255 error = ncp_request(rqp);
256 if (error) {
257 if (error == NWE_FILE_NO_CREATE_PRIV)
258 error = EACCES;
259 return error;
260 }
261 md_get_uint32le(&rqp->rp, &nop->origfh);
262 md_get_uint8(&rqp->rp, &nop->action);
263 md_get_uint8(&rqp->rp, NULL); /* skip */
264 error = ncp_extract_file_info(nmp, rqp, &nop->fattr, 1);
265 ncp_rq_done(rqp);
266 ConvertToNWfromDWORD(nop->origfh, &nop->fh);
267 return error;
268 }
269
270 int
271 ncp_close_file(struct ncp_conn *conn, ncp_fh *fh,struct thread *td,struct ucred *cred)
272 {
273 struct ncp_rq *rqp;
274 int error;
275
276 error = ncp_rq_alloc(66, conn, td, cred, &rqp);
277 if (error)
278 return error;
279 mb_put_uint8(&rqp->rq, 0);
280 mb_put_mem(&rqp->rq, (caddr_t)fh, 6, MB_MSYSTEM);
281 error = ncp_request(rqp);
282 if (error)
283 return error;
284 ncp_rq_done(rqp);
285 return error;
286 }
287
288 int
289 ncp_DeleteNSEntry(struct nwmount *nmp, u_int32_t dirent,
290 int namelen,char *name,struct thread *td,struct ucred *cred)
291 {
292 struct ncp_rq *rqp;
293 int error;
294 struct ncp_conn *conn=NWFSTOCONN(nmp);
295
296 error = ncp_rq_alloc(87, conn, td, cred, &rqp);
297 if (error)
298 return error;
299 mb_put_uint8(&rqp->rq, 8); /* subfunction */
300 mb_put_uint8(&rqp->rq, nmp->name_space);
301 mb_put_uint8(&rqp->rq, 0); /* reserved */
302 mb_put_uint16le(&rqp->rq, SA_ALL); /* search attribs: all */
303 ncp_rq_dbase_path(rqp, nmp->n_volume, dirent, namelen, name, &nmp->m.nls);
304 error = ncp_request(rqp);
305 if (!error)
306 ncp_rq_done(rqp);
307 return error;
308 }
309
310 int
311 ncp_nsrename(struct ncp_conn *conn, int volume, int ns, int oldtype,
312 struct ncp_nlstables *nt,
313 nwdirent fdir, char *old_name, int oldlen,
314 nwdirent tdir, char *new_name, int newlen,
315 struct thread *td, struct ucred *cred)
316 {
317 struct ncp_rq *rqp;
318 int error;
319
320 error = ncp_rq_alloc(87, conn, td, cred, &rqp);
321 if (error)
322 return error;
323 mb_put_uint8(&rqp->rq, 4);
324 mb_put_uint8(&rqp->rq, ns);
325 mb_put_uint8(&rqp->rq, 1); /* RRenameToMySelf */
326 mb_put_uint16le(&rqp->rq, oldtype);
327 /* source Handle Path */
328 mb_put_uint8(&rqp->rq, volume);
329 mb_put_mem(&rqp->rq, (c_caddr_t)&fdir, sizeof(fdir), MB_MSYSTEM);
330 mb_put_uint8(&rqp->rq, 1);
331 mb_put_uint8(&rqp->rq, 1); /* 1 source component */
332 /* dest Handle Path */
333 mb_put_uint8(&rqp->rq, volume);
334 mb_put_mem(&rqp->rq, (c_caddr_t)&tdir, sizeof(tdir), MB_MSYSTEM);
335 mb_put_uint8(&rqp->rq, 1);
336 mb_put_uint8(&rqp->rq, 1); /* 1 destination component */
337 ncp_rq_pathstring(rqp, oldlen, old_name, nt);
338 ncp_rq_pathstring(rqp, newlen, new_name, nt);
339 error = ncp_request(rqp);
340 if (!error)
341 ncp_rq_done(rqp);
342 return error;
343 }
344
345 int
346 ncp_modify_file_or_subdir_dos_info(struct nwmount *nmp, struct vnode *vp,
347 u_int32_t info_mask,
348 struct nw_modify_dos_info *info,
349 struct thread *td,struct ucred *cred)
350 {
351 struct nwnode *np=VTONW(vp);
352 struct ncp_rq *rqp;
353 u_int8_t volnum = nmp->n_volume;
354 u_int32_t dirent = np->n_fid.f_id;
355 struct ncp_conn *conn=NWFSTOCONN(nmp);
356 int error;
357
358 error = ncp_rq_alloc(87, conn, td, cred, &rqp);
359 if (error)
360 return error;
361 mb_put_uint8(&rqp->rq, 7); /* subfunction */
362 mb_put_uint8(&rqp->rq, nmp->name_space);
363 mb_put_uint8(&rqp->rq, 0); /* reserved */
364 mb_put_uint16le(&rqp->rq, SA_ALL); /* search attribs: all */
365 mb_put_uint32le(&rqp->rq, info_mask);
366 mb_put_mem(&rqp->rq, (caddr_t)info, sizeof(*info), MB_MSYSTEM);
367 ncp_rq_dbase_path(rqp, volnum, dirent, 0, NULL, NULL);
368 error = ncp_request(rqp);
369 if (!error)
370 ncp_rq_done(rqp);
371 return error;
372 }
373
374 int
375 ncp_setattr(vp, vap, cred, td)
376 struct vnode *vp;
377 struct vattr *vap;
378 struct ucred *cred;
379 struct thread *td;
380 {
381 struct nwmount *nmp=VTONWFS(vp);
382 struct nwnode *np=VTONW(vp);
383 struct ncp_open_info nwn;
384 struct ncp_conn *conn=NWFSTOCONN(nmp);
385 struct nw_modify_dos_info info;
386 struct ncp_rq *rqp;
387 int error = 0, info_mask;
388
389 if (vap->va_size != VNOVAL) {
390 error = ncp_open_create_file_or_subdir(nmp, vp, 0, NULL, OC_MODE_OPEN, 0,
391 AR_WRITE | AR_READ, &nwn,td,cred);
392 if (error)
393 return error;
394 error = ncp_rq_alloc(73, conn, td, cred, &rqp);
395 if (error) {
396 ncp_close_file(conn, &nwn.fh, td, cred);
397 return error;
398 }
399 mb_put_uint8(&rqp->rq, 0);
400 mb_put_mem(&rqp->rq, (caddr_t)&nwn.fh, 6, MB_MSYSTEM);
401 mb_put_uint32be(&rqp->rq, vap->va_size);
402 mb_put_uint16be(&rqp->rq, 0);
403 error = ncp_request(rqp);
404 np->n_vattr.va_size = np->n_size = vap->va_size;
405 if (!error)
406 ncp_rq_done(rqp);
407 ncp_close_file(conn, &nwn.fh, td, cred);
408 if (error)
409 return error;
410 }
411 info_mask = 0;
412 bzero(&info, sizeof(info));
413
414 if (vap->va_mtime.tv_sec != VNOVAL) {
415 info_mask |= (DM_MODIFY_TIME | DM_MODIFY_DATE);
416 ncp_unix2dostime(&vap->va_mtime, nmp->m.tz, &info.modifyDate, &info.modifyTime, NULL);
417 }
418 if (vap->va_atime.tv_sec != VNOVAL) {
419 info_mask |= (DM_LAST_ACCESS_DATE);
420 ncp_unix2dostime(&vap->va_atime, nmp->m.tz, &info.lastAccessDate, NULL, NULL);
421 }
422 if (info_mask) {
423 error = ncp_modify_file_or_subdir_dos_info(nmp, vp, info_mask, &info,td,cred);
424 }
425 return (error);
426 }
427
428 int
429 ncp_get_volume_info_with_number(struct ncp_conn *conn,
430 int n, struct ncp_volume_info *target,
431 struct thread *td,struct ucred *cred)
432 {
433 struct ncp_rq *rqp;
434 u_int32_t tmp32;
435 u_int8_t len;
436 int error;
437
438 error = ncp_rq_alloc_subfn(22, 44, conn, td, cred, &rqp);
439 if (error)
440 return error;
441 mb_put_uint8(&rqp->rq,n);
442 error = ncp_request(rqp);
443 if (error)
444 return error;
445 md_get_uint32le(&rqp->rp, &target->total_blocks);
446 md_get_uint32le(&rqp->rp, &target->free_blocks);
447 md_get_uint32le(&rqp->rp, &target->purgeable_blocks);
448 md_get_uint32le(&rqp->rp, &target->not_yet_purgeable_blocks);
449 md_get_uint32le(&rqp->rp, &target->total_dir_entries);
450 md_get_uint32le(&rqp->rp, &target->available_dir_entries);
451 md_get_uint32le(&rqp->rp, &tmp32);
452 md_get_uint8(&rqp->rp, &target->sectors_per_block);
453 bzero(&target->volume_name, sizeof(target->volume_name));
454 md_get_uint8(&rqp->rp, &len);
455 if (len > NCP_VOLNAME_LEN) {
456 error = ENAMETOOLONG;
457 } else {
458 md_get_mem(&rqp->rp, (caddr_t)&target->volume_name, len, MB_MSYSTEM);
459 }
460 ncp_rq_done(rqp);
461 return error;
462 }
463
464 int
465 ncp_get_namespaces(struct ncp_conn *conn, u_int32_t volume, int *nsf,
466 struct thread *td,struct ucred *cred)
467 {
468 struct ncp_rq *rqp;
469 int error;
470 u_int8_t ns;
471 u_int16_t nscnt;
472
473 error = ncp_rq_alloc(87, conn, td, cred, &rqp);
474 if (error)
475 return error;
476 mb_put_uint8(&rqp->rq, 24); /* Subfunction: Get Loaded Name Spaces */
477 mb_put_uint16le(&rqp->rq, 0); /* reserved */
478 mb_put_uint8(&rqp->rq, volume);
479 error = ncp_request(rqp);
480 if (error)
481 return error;
482 md_get_uint16le(&rqp->rp, &nscnt);
483 *nsf = 0;
484 while (nscnt-- > 0) {
485 md_get_uint8(&rqp->rp, &ns);
486 *nsf |= 1 << ns;
487 }
488 ncp_rq_done(rqp);
489 return error;
490 }
491
492 int
493 ncp_lookup_volume(struct ncp_conn *conn, char *volname,
494 u_char *volNum, u_int32_t *dirEnt,
495 struct thread *td,struct ucred *cred)
496 {
497 struct ncp_rq *rqp;
498 u_int32_t tmp32;
499 int error;
500
501 NCPNDEBUG("looking up vol %s\n", volname);
502 error = ncp_rq_alloc(87, conn, td, cred, &rqp);
503 if (error)
504 return error;
505 mb_put_uint8(&rqp->rq, 22); /* Subfunction: Generate dir handle */
506 mb_put_uint8(&rqp->rq, 0); /* src name space */
507 mb_put_uint8(&rqp->rq, 0); /* dst name space, always zero */
508 mb_put_uint16le(&rqp->rq, 0); /* dstNSIndicator (Jn) */
509
510 mb_put_uint8(&rqp->rq, 0); /* faked volume number */
511 mb_put_uint32be(&rqp->rq, 0); /* faked dir_base */
512 mb_put_uint8(&rqp->rq, 0xff); /* Don't have a dir_base */
513 mb_put_uint8(&rqp->rq, 1); /* 1 path component */
514 ncp_rq_pstring(rqp, volname);
515 error = ncp_request(rqp);
516 if (error)
517 return error;
518 md_get_uint32le(&rqp->rp, &tmp32);
519 md_get_uint32le(&rqp->rp, dirEnt);
520 md_get_uint8(&rqp->rp, volNum);
521 ncp_rq_done(rqp);
522 return error;
523 }
524
525 /*
526 * XXX: I think the timezone in struct nwfs_args is truly bogus, especially
527 * XXX: considering that nwfs_mount(8) picks this up from the kernel in
528 * XXX: the first place. Since I can't test this, I won't attempt to fix it.
529 * XXX: /phk
530 */
531
532 void
533 ncp_unix2dostime(tsp, tzoff, ddp, dtp, dhp)
534 struct timespec *tsp;
535 int tzoff;
536 u_int16_t *ddp;
537 u_int16_t *dtp;
538 u_int8_t *dhp;
539 {
540 struct timespec t;
541
542 t = *tsp;
543
544 t.tv_sec = - tzoff * 60 - utc_offset();
545 timespec2fattime(&t, 1, ddp, dtp, dhp);
546 }
547
548 void
549 ncp_dos2unixtime(dd, dt, dh, tzoff, tsp)
550 u_int dd;
551 u_int dt;
552 u_int dh;
553 int tzoff;
554 struct timespec *tsp;
555 {
556
557 fattime2timespec(dd, dt, dh, 1, tsp);
558 tsp->tv_sec += tzoff * 60 + utc_offset();
559 }
Cache object: 7216047adefed0eaeb1ab4ad8e65dce8
|