1 /*
2 * Copyright (c) 1996 Regents of The University of Michigan.
3 * All Rights Reserved. See COPYRIGHT.
4 */
5
6 #ifdef HAVE_CONFIG_H
7 #include "config.h"
8 #endif /* HAVE_CONFIG_H */
9
10 #include <stdio.h>
11 #include <stdlib.h>
12 #ifdef HAVE_UNISTD_H
13 #include <unistd.h>
14 #endif
15 #include <string.h>
16 #include <sys/stat.h> /* works around a bug */
17 #include <sys/param.h>
18 #include <errno.h>
19
20 #include <atalk/logger.h>
21 #include <atalk/util.h>
22 #include <atalk/bstrlib.h>
23 #include <atalk/bstradd.h>
24 #include <atalk/globals.h>
25 #include <atalk/fce_api.h>
26 #include <atalk/ea.h>
27
28 #include "volume.h"
29 #include "directory.h"
30 #include "fork.h"
31 #include "desktop.h"
32
33 /* we need to have a hashed list of oforks (by dev inode) */
34 #define OFORK_HASHSIZE 64
35 static struct ofork *ofork_table[OFORK_HASHSIZE]; /* forks hashed by dev/inode */
36 static struct ofork **oforks = NULL; /* point to allocated table of open forks pointers */
37 static int nforks = 0;
38 static u_short lastrefnum = 0;
39
40
41 /* OR some of each character for the hash*/
hashfn(const struct file_key * key)42 static unsigned long hashfn(const struct file_key *key)
43 {
44 return key->inode & (OFORK_HASHSIZE - 1);
45 }
46
of_hash(struct ofork * of)47 static void of_hash(struct ofork *of)
48 {
49 struct ofork **table;
50
51 table = &ofork_table[hashfn(&of->key)];
52 if ((of->next = *table) != NULL)
53 (*table)->prevp = &of->next;
54 *table = of;
55 of->prevp = table;
56 }
57
of_unhash(struct ofork * of)58 static void of_unhash(struct ofork *of)
59 {
60 if (of->prevp) {
61 if (of->next)
62 of->next->prevp = of->prevp;
63 *(of->prevp) = of->next;
64 }
65 }
66
of_flush(const struct vol * vol)67 int of_flush(const struct vol *vol)
68 {
69 int refnum;
70
71 if (!oforks)
72 return 0;
73
74 for ( refnum = 0; refnum < nforks; refnum++ ) {
75 if (oforks[ refnum ] != NULL && (oforks[refnum]->of_vol == vol) &&
76 flushfork( oforks[ refnum ] ) < 0 ) {
77 LOG(log_error, logtype_afpd, "of_flush: %s", strerror(errno) );
78 }
79 }
80 return( 0 );
81 }
82
of_rename(const struct vol * vol,struct ofork * s_of,struct dir * olddir,const char * oldpath _U_,struct dir * newdir,const char * newpath)83 int of_rename(const struct vol *vol,
84 struct ofork *s_of,
85 struct dir *olddir, const char *oldpath _U_,
86 struct dir *newdir, const char *newpath)
87 {
88 struct ofork *of, *next;
89 int done = 0;
90
91 if (!s_of)
92 return AFP_OK;
93
94 next = ofork_table[hashfn(&s_of->key)];
95 while ((of = next)) {
96 next = next->next; /* so we can unhash and still be all right. */
97
98 if (vol == of->of_vol
99 && olddir->d_did == of->of_did
100 && s_of->key.dev == of->key.dev
101 && s_of->key.inode == of->key.inode ) {
102 if (!done) {
103 free(of_name(of));
104 if ((of_name(of) = strdup(newpath)) == NULL)
105 return AFPERR_MISC;
106 done = 1;
107 }
108 if (newdir != olddir)
109 of->of_did = newdir->d_did;
110 }
111 }
112
113 return AFP_OK;
114 }
115
116 #define min(a,b) ((a)<(b)?(a):(b))
117
118 struct ofork *
of_alloc(struct vol * vol,struct dir * dir,char * path,uint16_t * ofrefnum,const int eid,struct adouble * ad,struct stat * st)119 of_alloc(struct vol *vol,
120 struct dir *dir,
121 char *path,
122 uint16_t *ofrefnum,
123 const int eid,
124 struct adouble *ad,
125 struct stat *st)
126 {
127 struct ofork *of;
128 uint16_t refnum, of_refnum;
129
130 int i;
131
132 if (!oforks) {
133 nforks = getdtablesize() - 10;
134 /* protect against insane ulimit -n */
135 nforks = min(nforks, 0xffff);
136 oforks = (struct ofork **) calloc(nforks, sizeof(struct ofork *));
137 if (!oforks)
138 return NULL;
139 }
140
141 for ( refnum = ++lastrefnum, i = 0; i < nforks; i++, refnum++ ) {
142 /* cf AFP3.0.pdf, File fork page 40 */
143 if (!refnum)
144 refnum++;
145 if ( oforks[ refnum % nforks ] == NULL ) {
146 break;
147 }
148 }
149 /* grr, Apple and their 'uniquely identifies'
150 the next line is a protection against
151 of_alloc()
152 refnum % nforks = 3
153 lastrefnum = 3
154 oforks[3] != NULL
155 refnum = 4
156 oforks[4] == NULL
157 return 4
158
159 close(oforks[4])
160
161 of_alloc()
162 refnum % nforks = 4
163 ...
164 return 4
165 same if lastrefnum++ rather than ++lastrefnum.
166 */
167 lastrefnum = refnum;
168 if ( i == nforks ) {
169 LOG(log_error, logtype_afpd, "of_alloc: maximum number of forks exceeded.");
170 return( NULL );
171 }
172
173 of_refnum = refnum % nforks;
174 if (( oforks[ of_refnum ] =
175 (struct ofork *)malloc( sizeof( struct ofork ))) == NULL ) {
176 LOG(log_error, logtype_afpd, "of_alloc: malloc: %s", strerror(errno) );
177 return NULL;
178 }
179 of = oforks[of_refnum];
180
181 /* see if we need to allocate space for the adouble struct */
182 if (!ad) {
183 ad = malloc( sizeof( struct adouble ) );
184 if (!ad) {
185 LOG(log_error, logtype_afpd, "of_alloc: malloc: %s", strerror(errno) );
186 free(of);
187 oforks[ of_refnum ] = NULL;
188 return NULL;
189 }
190
191 /* initialize to zero. This is important to ensure that
192 ad_open really does reinitialize the structure. */
193 ad_init(ad, vol);
194 if ((ad->ad_name = strdup(path)) == NULL) {
195 LOG(log_error, logtype_afpd, "of_alloc: malloc: %s", strerror(errno) );
196 free(ad);
197 free(of);
198 oforks[ of_refnum ] = NULL;
199 return NULL;
200 }
201 } else {
202 /* Increase the refcount on this struct adouble. This is
203 decremented again in oforc_dealloc. */
204 ad_ref(ad);
205 }
206
207 of->of_ad = ad;
208 of->of_vol = vol;
209 of->of_did = dir->d_did;
210
211 *ofrefnum = refnum;
212 of->of_refnum = refnum;
213 of->key.dev = st->st_dev;
214 of->key.inode = st->st_ino;
215 if (eid == ADEID_DFORK)
216 of->of_flags = AFPFORK_DATA;
217 else
218 of->of_flags = AFPFORK_RSRC;
219
220 of_hash(of);
221 return( of );
222 }
223
of_find(const uint16_t ofrefnum)224 struct ofork *of_find(const uint16_t ofrefnum )
225 {
226 if (!oforks || !nforks)
227 return NULL;
228
229 return( oforks[ ofrefnum % nforks ] );
230 }
231
232 /* -------------------------- */
of_stat(const struct vol * vol,struct path * path)233 int of_stat(const struct vol *vol, struct path *path)
234 {
235 int ret;
236
237 path->st_errno = 0;
238 path->st_valid = 1;
239
240 if ((ret = ostat(path->u_name, &path->st, vol_syml_opt(vol))) < 0) {
241 LOG(log_debug, logtype_afpd, "of_stat('%s/%s': %s)",
242 cfrombstr(curdir->d_fullpath), path->u_name, strerror(errno));
243 path->st_errno = errno;
244 }
245
246 return ret;
247 }
248
249
250 #ifdef HAVE_ATFUNCS
of_fstatat(int dirfd,struct path * path)251 int of_fstatat(int dirfd, struct path *path)
252 {
253 int ret;
254
255 path->st_errno = 0;
256 path->st_valid = 1;
257
258 if ((ret = fstatat(dirfd, path->u_name, &path->st, AT_SYMLINK_NOFOLLOW)) < 0)
259 path->st_errno = errno;
260
261 return ret;
262 }
263 #endif /* HAVE_ATFUNCS */
264
265 /* --------------------------
266 stat the current directory.
267 stat(".") works even if "." is deleted thus
268 we have to stat ../name because we want to know if it's there
269 */
of_statdir(struct vol * vol,struct path * path)270 int of_statdir(struct vol *vol, struct path *path)
271 {
272 static char pathname[ MAXPATHLEN + 1] = "../";
273 int ret;
274 size_t len;
275 struct dir *dir;
276
277 if (*path->m_name) {
278 /* not curdir */
279 return of_stat(vol, path);
280 }
281 path->st_errno = 0;
282 path->st_valid = 1;
283 /* FIXME, what about: we don't have r-x perm anymore ? */
284 len = blength(path->d_dir->d_u_name);
285 if (len > (MAXPATHLEN - 3))
286 len = MAXPATHLEN - 3;
287 strncpy(pathname + 3, cfrombstr(path->d_dir->d_u_name), len + 1);
288
289 LOG(log_debug, logtype_afpd, "of_statdir: stating: '%s'", pathname);
290
291 if (!(ret = ostat(pathname, &path->st, vol_syml_opt(vol))))
292 return 0;
293
294 path->st_errno = errno;
295
296 /* hmm, can't stat curdir anymore */
297 if (errno == EACCES && (dir = dirlookup(vol, curdir->d_pdid))) {
298 if (movecwd(vol, dir))
299 return -1;
300 path->st_errno = 0;
301
302 if ((ret = ostat(cfrombstr(path->d_dir->d_u_name), &path->st, vol_syml_opt(vol))) < 0)
303 path->st_errno = errno;
304 }
305
306 return ret;
307 }
308
309 /* -------------------------- */
of_findname(const struct vol * vol,struct path * path)310 struct ofork *of_findname(const struct vol *vol, struct path *path)
311 {
312 struct ofork *of;
313 struct file_key key;
314
315 if (!path->st_valid) {
316 of_stat(vol, path);
317 }
318
319 if (path->st_errno)
320 return NULL;
321
322 key.dev = path->st.st_dev;
323 key.inode = path->st.st_ino;
324
325 for (of = ofork_table[hashfn(&key)]; of; of = of->next) {
326 if (key.dev == of->key.dev && key.inode == of->key.inode ) {
327 return of;
328 }
329 }
330
331 return NULL;
332 }
333
334 /*!
335 * @brief Search for open fork by dirfd/name
336 *
337 * Function call of_fstatat with dirfd and path and uses dev and ino
338 * to search the open fork table.
339 *
340 * @param dirfd (r) directory fd
341 * @param path (rw) pointer to struct path
342 */
343 #ifdef HAVE_ATFUNCS
of_findnameat(int dirfd,struct path * path)344 struct ofork *of_findnameat(int dirfd, struct path *path)
345 {
346 struct ofork *of;
347 struct file_key key;
348
349 if ( ! path->st_valid) {
350 of_fstatat(dirfd, path);
351 }
352
353 if (path->st_errno)
354 return NULL;
355
356 key.dev = path->st.st_dev;
357 key.inode = path->st.st_ino;
358
359 for (of = ofork_table[hashfn(&key)]; of; of = of->next) {
360 if (key.dev == of->key.dev && key.inode == of->key.inode ) {
361 return of;
362 }
363 }
364
365 return NULL;
366 }
367 #endif
368
of_dealloc(struct ofork * of)369 void of_dealloc(struct ofork *of)
370 {
371 if (!oforks)
372 return;
373
374 of_unhash(of);
375 oforks[ of->of_refnum % nforks ] = NULL;
376
377 /* decrease refcount */
378 of->of_ad->ad_refcount--;
379
380 if ( of->of_ad->ad_refcount <= 0) {
381 free( of->of_ad->ad_name );
382 free( of->of_ad);
383 }
384
385 free( of );
386 }
387
388 /* --------------------------- */
of_closefork(const AFPObj * obj,struct ofork * ofork)389 int of_closefork(const AFPObj *obj, struct ofork *ofork)
390 {
391 struct timeval tv;
392 int adflags = 0;
393 int ret;
394 struct dir *dir;
395 bstring forkpath = NULL;
396
397 adflags = 0;
398 if (ofork->of_flags & AFPFORK_DATA)
399 adflags |= ADFLAGS_DF;
400 if (ofork->of_flags & AFPFORK_META)
401 adflags |= ADFLAGS_HF;
402 if (ofork->of_flags & AFPFORK_RSRC) {
403 adflags |= ADFLAGS_RF;
404 /* Only set the rfork's length if we're closing the rfork. */
405 ad_refresh(NULL, ofork->of_ad );
406 if ((ofork->of_flags & AFPFORK_DIRTY) && !gettimeofday(&tv, NULL)) {
407 ad_setdate(ofork->of_ad, AD_DATE_MODIFY | AD_DATE_UNIX,tv.tv_sec);
408 ad_flush( ofork->of_ad );
409 }
410 }
411
412 dir = dirlookup(ofork->of_vol, ofork->of_did);
413 if (dir == NULL) {
414 LOG(log_debug, logtype_afpd, "dirlookup failed for %ju", (uintmax_t)ofork->of_did);
415 }
416
417 if (dir) {
418 forkpath = bformat("%s/%s", bdata(dir->d_fullpath), of_name(ofork));
419 }
420
421 /* Somone has used write_fork, we assume file was changed, register it to file change event api */
422 if ((ofork->of_flags & AFPFORK_MODIFIED) && (forkpath)) {
423 fce_register(obj, FCE_FILE_MODIFY, bdata(forkpath), NULL);
424 }
425
426 ad_unlock(ofork->of_ad, ofork->of_refnum, ofork->of_flags & AFPFORK_ERROR ? 0 : 1);
427
428 #ifdef HAVE_FSHARE_T
429 if (obj->options.flags & OPTION_SHARE_RESERV) {
430 fshare_t shmd;
431 shmd.f_id = ofork->of_refnum;
432 if (AD_DATA_OPEN(ofork->of_ad))
433 fcntl(ad_data_fileno(ofork->of_ad), F_UNSHARE, &shmd);
434 if (AD_RSRC_OPEN(ofork->of_ad))
435 fcntl(ad_reso_fileno(ofork->of_ad), F_UNSHARE, &shmd);
436 }
437 #endif
438
439 ret = 0;
440
441 /*
442 * Check for 0 byte size resource forks, delete them.
443 * Here's the deal:
444 * (1) the size must be 0
445 * (2) the fork must refer to a resource fork
446 * (3) the refcount must be 1 which means this fork has the last
447 * reference to the adouble struct and the subsequent
448 * ad_close() will close the assoiciated fd.
449 * (4) nobody else has the resource fork open
450 *
451 * We only do this for ._ AppleDouble resource forks, not for
452 * xattr resource forks, because the test-suite then fails several
453 * tests on Solaris, the reason for that still needs to be
454 * determined.
455 */
456 if ((ofork->of_ad->ad_rlen == 0)
457 && (ofork->of_flags & AFPFORK_RSRC)
458 && (ofork->of_ad->ad_rfp->adf_refcount == 1)
459 && (ad_openforks(ofork->of_ad, ATTRBIT_DOPEN) == 0)) {
460
461 #ifndef HAVE_EAFD
462 (void)unlink(ofork->of_ad->ad_ops->ad_path(
463 mtoupath(ofork->of_vol,
464 of_name(ofork),
465 ofork->of_did,
466 utf8_encoding(obj)),
467 0));
468 #endif
469 }
470
471 if ( ad_close( ofork->of_ad, adflags | ADFLAGS_SETSHRMD) < 0 ) {
472 ret = -1;
473 }
474
475 of_dealloc(ofork);
476
477 if (forkpath)
478 bdestroy(forkpath);
479
480 return ret;
481 }
482
483 /* ----------------------
484
485 */
of_ad(const struct vol * vol,struct path * path,struct adouble * ad)486 struct adouble *of_ad(const struct vol *vol, struct path *path, struct adouble *ad)
487 {
488 struct ofork *of;
489 struct adouble *adp;
490
491 if ((of = of_findname(vol, path))) {
492 adp = of->of_ad;
493 } else {
494 ad_init(ad, vol);
495 adp = ad;
496 }
497 return adp;
498 }
499
500 /* ----------------------
501 close all forks for a volume
502 */
of_closevol(const AFPObj * obj,const struct vol * vol)503 void of_closevol(const AFPObj *obj, const struct vol *vol)
504 {
505 int refnum;
506
507 if (!oforks)
508 return;
509
510 for ( refnum = 0; refnum < nforks; refnum++ ) {
511 if (oforks[ refnum ] != NULL && oforks[refnum]->of_vol == vol) {
512 if (of_closefork(obj, oforks[ refnum ]) < 0 ) {
513 LOG(log_error, logtype_afpd, "of_closevol: %s", strerror(errno) );
514 }
515 }
516 }
517 return;
518 }
519
520 /* ----------------------
521 close all forks for a volume
522 */
of_close_all_forks(const AFPObj * obj)523 void of_close_all_forks(const AFPObj *obj)
524 {
525 int refnum;
526
527 if (!oforks)
528 return;
529
530 for ( refnum = 0; refnum < nforks; refnum++ ) {
531 if (oforks[ refnum ] != NULL) {
532 if (of_closefork(obj, oforks[ refnum ]) < 0 ) {
533 LOG(log_error, logtype_afpd, "of_close_all_forks: %s", strerror(errno) );
534 }
535 }
536 }
537 return;
538 }
539
540