-
Notifications
You must be signed in to change notification settings - Fork 84
/
directory.c
2436 lines (2159 loc) · 73.6 KB
/
directory.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 1990,1993 Regents of The University of Michigan.
* All Rights Reserved. See COPYRIGHT.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif /* HAVE_CONFIG_H */
#include <assert.h>
#include <errno.h>
#include <grp.h>
#include <pwd.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/param.h>
#include <sys/stat.h>
#include <utime.h>
#include <atalk/adouble.h>
#include <atalk/afp.h>
#include <atalk/bstradd.h>
#include <atalk/bstrlib.h>
#include <atalk/cnid.h>
#include <atalk/errchk.h>
#include <atalk/fce_api.h>
#include <atalk/globals.h>
#include <atalk/logger.h>
#include <atalk/netatalk_conf.h>
#include <atalk/unix.h>
#include <atalk/util.h>
#include <atalk/uuid.h>
#include <atalk/vfs.h>
#include "desktop.h"
#include "dircache.h"
#include "directory.h"
#include "file.h"
#include "filedir.h"
#include "fork.h"
#include "hash.h"
#include "mangle.h"
#include "unix.h"
#include "volume.h"
/*
* FIXMEs, loose ends after the dircache rewrite:
* o merge dircache_search_by_name and dir_add ??
* o case-insensitivity is gone from cname
*/
/*******************************************************************************************
* Globals
******************************************************************************************/
int afp_errno;
/* As long as directory.c hasn't got its own init call, this get initialized in dircache_init */
struct dir rootParent = {
NULL, NULL, NULL, NULL, /* path, d_m_name, d_u_name, d_m_name_ucs2 */
NULL, 0, 0, /* qidx_node, ctime, d_flags */
0, 0, 0, 0 /* pdid, did, offcnt, d_vid */
};
struct dir *curdir = &rootParent;
struct path Cur_Path = {
0,
"", /* mac name */
".", /* unix name */
0, /* id */
NULL,/* struct dir * */
0, /* stat is not set */
0, /* errno */
{0} /* struct stat */
};
/*
* dir_remove queues struct dirs to be freed here. We can't just delete them immeidately
* e.g. in dircache_search_by_id, because a caller somewhere up the stack might be
* referencing it.
* So instead:
* - we mark it as invalid by setting d_did to CNID_INVALID (ie 0)
* - queue it in "invalid_dircache_entries" queue
* - which is finally freed at the end of every AFP func in afp_dsi.c.
*/
q_t *invalid_dircache_entries;
/*******************************************************************************************
* Locals
******************************************************************************************/
/* -------------------------
appledouble mkdir afp error code.
*/
static int netatalk_mkdir(const struct vol *vol, const char *name)
{
int ret;
struct stat st;
if (vol->v_flags & AFPVOL_UNIX_PRIV) {
if (lstat(".", &st) < 0)
return AFPERR_MISC;
int mode = (DIRBITS & (~S_ISGID & st.st_mode)) | (0777 & ~vol->v_umask);
LOG(log_maxdebug, logtype_afpd, "netatalk_mkdir(\"%s\") {parent mode: %04o, vol umask: %04o}",
name, st.st_mode, vol->v_umask);
ret = mkdir(name, mode);
} else {
ret = ad_mkdir(name, DIRBITS | 0777);
}
if (ret < 0) {
switch ( errno ) {
case ENOENT :
return( AFPERR_NOOBJ );
case EROFS :
return( AFPERR_VLOCK );
case EPERM:
case EACCES :
return( AFPERR_ACCESS );
case EEXIST :
return( AFPERR_EXIST );
case ENOSPC :
case EDQUOT :
return( AFPERR_DFULL );
default :
return( AFPERR_PARAM );
}
}
return AFP_OK;
}
/* ------------------- */
static int deletedir(const struct vol *vol, int dirfd, char *dir)
{
char path[MAXPATHLEN + 1];
DIR *dp;
struct dirent *de;
struct stat st;
size_t len;
int err = AFP_OK;
size_t remain;
if ((len = strlen(dir)) +2 > sizeof(path))
return AFPERR_PARAM;
/* already gone */
if ((dp = opendirat(dirfd, dir)) == NULL)
return AFP_OK;
snprintf(path, MAXPATHLEN, "%s/", dir);
len++;
remain = strlen(path) -len -1;
while ((de = readdir(dp)) && err == AFP_OK) {
/* skip this and previous directory */
if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
continue;
if (strlen(de->d_name) > remain) {
err = AFPERR_PARAM;
break;
}
strcpy(path + len, de->d_name);
if (ostatat(dirfd, path, &st, vol_syml_opt(vol))) {
continue;
}
if (S_ISDIR(st.st_mode)) {
err = deletedir(vol, dirfd, path);
} else {
err = netatalk_unlinkat(dirfd, path);
}
}
closedir(dp);
/* okay. the directory is empty. delete it. note: we already got rid
of .AppleDouble. */
if (err == AFP_OK) {
err = netatalk_rmdir(dirfd, dir);
}
return err;
}
/* do a recursive copy. */
static int copydir(struct vol *vol, struct dir *ddir, int dirfd, char *src, char *dst)
{
char spath[MAXPATHLEN + 1], dpath[MAXPATHLEN + 1];
DIR *dp;
struct dirent *de;
struct stat st;
struct utimbuf ut;
size_t slen, dlen;
size_t srem, drem;
int err;
/* doesn't exist or the path is too long. */
if (((slen = strlen(src)) > sizeof(spath) - 2) ||
((dlen = strlen(dst)) > sizeof(dpath) - 2) ||
((dp = opendirat(dirfd, src)) == NULL))
return AFPERR_PARAM;
/* try to create the destination directory */
if (AFP_OK != (err = netatalk_mkdir(vol, dst)) ) {
closedir(dp);
return err;
}
/* set things up to copy */
snprintf(spath, MAXPATHLEN, "%s/", src);
slen++;
srem = strlen(spath) - slen -1;
snprintf(dpath, MAXPATHLEN, "%s/", dst);
dlen++;
drem = strlen(dpath) - dlen -1;
err = AFP_OK;
while ((de = readdir(dp))) {
/* skip this and previous directory */
if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
continue;
if (strlen(de->d_name) > srem) {
err = AFPERR_PARAM;
break;
}
strcpy(spath + slen, de->d_name);
if (ostatat(dirfd, spath, &st, vol_syml_opt(vol)) == 0) {
if (strlen(de->d_name) > drem) {
err = AFPERR_PARAM;
break;
}
strcpy(dpath + dlen, de->d_name);
if (S_ISDIR(st.st_mode)) {
if (AFP_OK != (err = copydir(vol, ddir, dirfd, spath, dpath)))
goto copydir_done;
} else if (AFP_OK != (err = copyfile(vol, vol, ddir, dirfd, spath, dpath, NULL, NULL))) {
goto copydir_done;
} else {
/* keep the same time stamp. */
ut.actime = ut.modtime = st.st_mtime;
utime(dpath, &ut);
}
}
}
/* keep the same time stamp. */
if (ostatat(dirfd, src, &st, vol_syml_opt(vol)) == 0) {
ut.actime = ut.modtime = st.st_mtime;
utime(dst, &ut);
}
copydir_done:
closedir(dp);
return err;
}
/* ---------------------
* is our cached offspring count valid?
*/
static int diroffcnt(struct dir *dir, struct stat *st)
{
return st->st_ctime == dir->d_ctime;
}
/* --------------------- */
static int invisible_dots(const struct vol *vol, const char *name)
{
return vol_inv_dots(vol) && *name == '.' && strcmp(name, ".") && strcmp(name, "..");
}
/* ------------------ */
static int set_dir_errors(struct path *path, const char *where, int err)
{
switch ( err ) {
case EPERM :
case EACCES :
return AFPERR_ACCESS;
case EROFS :
return AFPERR_VLOCK;
}
LOG(log_error, logtype_afpd, "setdirparam(%s): %s: %s", fullpathname(path->u_name), where, strerror(err) );
return AFPERR_PARAM;
}
/*!
* @brief Convert name in client encoding to server encoding
*
* Convert ret->m_name to ret->u_name from client encoding to server encoding.
* This only gets called from cname().
*
* @returns 0 on success, -1 on error
*
* @note If the passed ret->m_name is mangled, we'll demangle it
*/
static int cname_mtouname(const struct vol *vol, struct dir *dir, struct path *ret, int toUTF8)
{
static char temp[ MAXPATHLEN + 1];
char *t;
cnid_t fileid = 0;
if (vol->v_obj->afp_version >= 30) {
if (toUTF8) {
if (dir->d_did == DIRDID_ROOT_PARENT) {
/*
* With uft8 volume name is utf8-mac, but requested path may be a mangled longname. See #2611981.
* So we compare it with the longname from the current volume and if they match
* we overwrite the requested path with the utf8 volume name so that the following
* strcmp can match.
*/
ucs2_to_charset(vol->v_maccharset, vol->v_macname, temp, AFPVOL_MACNAMELEN + 1);
if (strcasecmp(ret->m_name, temp) == 0)
ucs2_to_charset(CH_UTF8_MAC, vol->v_u8mname, ret->m_name, AFPVOL_U8MNAMELEN);
} else {
/* toUTF8 */
if (mtoUTF8(vol, ret->m_name, strlen(ret->m_name), temp, MAXPATHLEN) == (size_t)-1) {
afp_errno = AFPERR_PARAM;
return -1;
}
strcpy(ret->m_name, temp);
}
}
/* check for OS X mangled filename :( */
t = demangle_osx(vol, ret->m_name, dir->d_did, &fileid);
if (curdir == NULL) {
/* demangle_osx() calls dirlookup() which might have clobbered curdir */
movecwd(vol, dir);
}
LOG(log_maxdebug, logtype_afpd, "cname_mtouname('%s',did:%u) {demangled:'%s', fileid:%u}",
ret->m_name, ntohl(dir->d_did), t, ntohl(fileid));
if (t != ret->m_name) {
ret->u_name = t;
/* duplicate work but we can't reuse all convert_char we did in demangle_osx
* flags weren't the same
*/
if ( (t = utompath(vol, ret->u_name, fileid, utf8_encoding(vol->v_obj))) ) {
/* at last got our view of mac name */
strcpy(ret->m_name, t);
}
}
} /* afp_version >= 30 */
/* If we haven't got it by now, get it */
if (ret->u_name == NULL) {
if ((ret->u_name = mtoupath(vol, ret->m_name, dir->d_did, utf8_encoding(vol->v_obj))) == NULL) {
afp_errno = AFPERR_PARAM;
return -1;
}
}
return 0;
}
/*!
* @brief Build struct path from struct dir
*
* The final movecwd in cname failed, possibly with EPERM or ENOENT. We:
* 1. move cwd into parent dir (we're often already there, but not always)
* 2. set struct path to the dirname
* 3. in case of
* AFPERR_ACCESS: the dir is there, we just can't chdir into it
* AFPERR_NOOBJ: the dir was there when we stated it in cname, so we have a race
* 4. indicate there's no dir for this path
* 5. remove the dir
*/
static struct path *path_from_dir(struct vol *vol, struct dir *dir, struct path *ret)
{
if (dir->d_did == DIRDID_ROOT_PARENT || dir->d_did == DIRDID_ROOT)
return NULL;
switch (afp_errno) {
case AFPERR_ACCESS:
if (movecwd( vol, dirlookup(vol, dir->d_pdid)) < 0 ) /* 1 */
return NULL;
memcpy(ret->m_name, cfrombstr(dir->d_m_name), blength(dir->d_m_name) + 1); /* 3 */
if (dir->d_m_name == dir->d_u_name) {
ret->u_name = ret->m_name;
} else {
ret->u_name = ret->m_name + blength(dir->d_m_name) + 1;
memcpy(ret->u_name, cfrombstr(dir->d_u_name), blength(dir->d_u_name) + 1);
}
ret->d_dir = dir;
LOG(log_debug, logtype_afpd, "cname('%s') {path-from-dir: AFPERR_ACCESS. curdir:'%s', path:'%s'}",
cfrombstr(dir->d_fullpath),
cfrombstr(curdir->d_fullpath),
ret->u_name);
return ret;
case AFPERR_NOOBJ:
if (movecwd(vol, dirlookup(vol, dir->d_pdid)) < 0 ) /* 1 */
return NULL;
memcpy(ret->m_name, cfrombstr(dir->d_m_name), blength(dir->d_m_name) + 1);
if (dir->d_m_name == dir->d_u_name) {
ret->u_name = ret->m_name;
} else {
ret->u_name = ret->m_name + blength(dir->d_m_name) + 1;
memcpy(ret->u_name, cfrombstr(dir->d_u_name), blength(dir->d_u_name) + 1);
}
ret->d_dir = NULL; /* 4 */
dir_remove(vol, dir); /* 5 */
return ret;
default:
return NULL;
}
/* DEADC0DE: never get here */
return NULL;
}
/*********************************************************************************************
* Interface
********************************************************************************************/
int get_afp_errno(const int param)
{
if (afp_errno != AFPERR_DID1)
return afp_errno;
return param;
}
/*!
* @brief Resolve a DID
*
* Resolve a DID, allocate a struct dir for it
* 1. Check for special CNIDs 0 (invalid), 1 and 2.
* 2a. Check if the DID is in the cache.
* 2b. Check if it's really a dir because we cache files too.
* 3. If it's not in the cache resolve it via the database.
* 4. Build complete server-side path to the dir.
* 5. Check if it exists and is a directory.
* 6. Create the struct dir and populate it.
* 7. Add it to the cache.
*
* @param vol (r) pointer to struct vol
* @param did (r) DID to resolve
*
* @returns pointer to struct dir
*/
struct dir *dirlookup(const struct vol *vol, cnid_t did)
{
static char buffer[12 + MAXPATHLEN + 1];
struct stat st;
struct dir *ret = NULL, *pdir;
bstring fullpath = NULL;
char *upath = NULL, *mpath;
cnid_t cnid, pdid;
size_t maxpath;
int buflen = 12 + MAXPATHLEN + 1;
int utf8;
int err = 0;
LOG(log_debug, logtype_afpd, "dirlookup(did: %u): START", ntohl(did));
/* check for did 0, 1 and 2 */
if (did == 0 || vol == NULL) { /* 1 */
afp_errno = AFPERR_PARAM;
ret = NULL;
goto exit;
} else if (did == DIRDID_ROOT_PARENT) {
rootParent.d_vid = vol->v_vid;
ret = &rootParent;
goto exit;
} else if (did == DIRDID_ROOT) {
ret = vol->v_root;
goto exit;
}
/* Search the cache */
if ((ret = dircache_search_by_did(vol, did)) != NULL) { /* 2a */
if (ret->d_flags & DIRF_ISFILE) { /* 2b */
afp_errno = AFPERR_BADTYPE;
ret = NULL;
goto exit;
}
if (lstat(cfrombstr(ret->d_fullpath), &st) != 0) {
LOG(log_debug, logtype_afpd, "dirlookup(did: %u, path: \"%s\"): lstat: %s",
ntohl(did), cfrombstr(ret->d_fullpath), strerror(errno));
switch (errno) {
case ENOENT:
case ENOTDIR:
/* It's not there anymore, so remove it */
LOG(log_debug, logtype_afpd, "dirlookup(did: %u): calling dir_remove", ntohl(did));
dir_remove(vol, ret);
afp_errno = AFPERR_NOOBJ;
ret = NULL;
goto exit;
default:
ret = ret;
goto exit;
}
/* DEADC0DE */
ret = NULL;
goto exit;
}
ret = ret;
goto exit;
}
utf8 = utf8_encoding(vol->v_obj);
maxpath = (utf8) ? MAXPATHLEN - 7 : 255;
/* Get it from the database */
cnid = did;
LOG(log_debug, logtype_afpd, "dirlookup(did: %u): querying CNID database", ntohl(did));
AFP_CNID_START("cnid_resolve");
upath = cnid_resolve(vol->v_cdb, &cnid, buffer, buflen);
AFP_CNID_DONE();
if (upath == NULL) {
afp_errno = AFPERR_NOOBJ;
err = 1;
goto exit;
}
if ((upath = strdup(upath)) == NULL) { /* 3 */
afp_errno = AFPERR_NOOBJ;
err = 1;
goto exit;
}
pdid = cnid;
/*
* Recurse up the tree, terminates in dirlookup when either
* - DIRDID_ROOT is hit
* - a cached entry is found
*/
LOG(log_debug, logtype_afpd, "dirlookup(did: %u): recursion for did: %u",
ntohl(did), ntohl(pdid));
if ((pdir = dirlookup(vol, pdid)) == NULL) {
err = 1;
goto exit;
}
/* build the fullpath */
if ((fullpath = bstrcpy(pdir->d_fullpath)) == NULL
|| bconchar(fullpath, '/') != BSTR_OK
|| bcatcstr(fullpath, upath) != BSTR_OK) {
err = 1;
goto exit;
}
/* stat it and check if it's a dir */
LOG(log_debug, logtype_afpd, "dirlookup(did: %u): stating \"%s\"",
ntohl(did), cfrombstr(fullpath));
if (ostat(cfrombstr(fullpath), &st, vol_syml_opt(vol)) != 0) { /* 5a */
switch (errno) {
case ENOENT:
afp_errno = AFPERR_NOOBJ;
err = 1;
goto exit;
case EPERM:
afp_errno = AFPERR_ACCESS;
err = 1;
goto exit;
default:
afp_errno = AFPERR_MISC;
err = 1;
goto exit;
}
} else {
if ( ! S_ISDIR(st.st_mode)) { /* 5b */
afp_errno = AFPERR_BADTYPE;
err = 1;
goto exit;
}
}
/* Get macname from unix name */
if ( (mpath = utompath(vol, upath, did, utf8)) == NULL ) {
afp_errno = AFPERR_NOOBJ;
err = 1;
goto exit;
}
/* Create struct dir */
if ((ret = dir_new(mpath, upath, vol, pdid, did, fullpath, &st)) == NULL) { /* 6 */
LOG(log_error, logtype_afpd, "dirlookup(did: %u) {%s, %s}: %s", ntohl(did), mpath, upath, strerror(errno));
err = 1;
goto exit;
}
/* Add it to the cache only if it's a dir */
if (dircache_add(vol, ret) != 0) { /* 7 */
err = 1;
goto exit;
}
exit:
if (upath) free(upath);
if (err) {
LOG(log_debug, logtype_afpd, "dirlookup(did: %u) {exit_error: %s}",
ntohl(did), AfpErr2name(afp_errno));
if (fullpath)
bdestroy(fullpath);
if (ret) {
dir_free(ret);
ret = NULL;
}
}
if (ret)
LOG(log_debug, logtype_afpd, "dirlookup(did: %u): RESULT: pdid: %u, path: \"%s\"",
ntohl(ret->d_did), ntohl(ret->d_pdid), cfrombstr(ret->d_fullpath));
return ret;
}
/*!
* @brief Construct struct dir
*
* Construct struct dir from parameters.
*
* @param m_name (r) directory name in UTF8-dec
* @param u_name (r) directory name in server side encoding
* @param vol (r) pointer to struct vol
* @param pdid (r) Parent CNID
* @param did (r) CNID
* @param path (r) Full unix path to object
* @param st (r) struct stat of object
*
* @returns pointer to new struct dir or NULL on error
*
* @note Most of the time mac name and unix name are the same.
*/
struct dir *dir_new(const char *m_name,
const char *u_name,
const struct vol *vol,
cnid_t pdid,
cnid_t did,
bstring path,
struct stat *st)
{
struct dir *dir;
dir = (struct dir *) calloc(1, sizeof( struct dir ));
if (!dir)
return NULL;
if ((dir->d_m_name = bfromcstr(m_name)) == NULL) {
free(dir);
return NULL;
}
if (convert_string_allocate( (utf8_encoding(vol->v_obj)) ? CH_UTF8_MAC : vol->v_maccharset,
CH_UCS2,
m_name,
-1, (char **)&dir->d_m_name_ucs2) == (size_t)-1 ) {
LOG(log_error, logtype_afpd, "dir_new(did: %u) {%s, %s}: couldn't set UCS2 name", ntohl(did), m_name, u_name);
dir->d_m_name_ucs2 = NULL;
}
if (m_name == u_name || !strcmp(m_name, u_name)) {
dir->d_u_name = dir->d_m_name;
}
else if ((dir->d_u_name = bfromcstr(u_name)) == NULL) {
bdestroy(dir->d_m_name);
free(dir);
return NULL;
}
dir->d_did = did;
dir->d_pdid = pdid;
dir->d_vid = vol->v_vid;
dir->d_fullpath = path;
dir->dcache_ctime = st->st_ctime;
dir->dcache_ino = st->st_ino;
if (!S_ISDIR(st->st_mode))
dir->d_flags = DIRF_ISFILE;
dir->d_rights_cache = 0xffffffff;
return dir;
}
/*!
* @brief Free a struct dir and all its members
*
* @param (rw) pointer to struct dir
*/
void dir_free(struct dir *dir)
{
if (dir->d_u_name != dir->d_m_name) {
bdestroy(dir->d_u_name);
}
if (dir->d_m_name_ucs2)
free(dir->d_m_name_ucs2);
bdestroy(dir->d_m_name);
bdestroy(dir->d_fullpath);
free(dir);
}
/*!
* @brief Create struct dir from struct path
*
* Create a new struct dir from struct path. Then add it to the cache.
*
* 1. Open adouble file, get CNID from it.
* 2. Search the database, hinting with the CNID from (1).
* 3. Build fullpath and create struct dir.
* 4. Add it to the cache.
*
* @param vol (r) pointer to struct vol, possibly modified in callee
* @param dir (r) pointer to parrent directory
* @param path (rw) pointer to struct path with valid path->u_name
* @param len (r) strlen of path->u_name
*
* @returns Pointer to new struct dir or NULL on error.
*
* @note Function also assigns path->m_name from path->u_name.
*/
struct dir *dir_add(struct vol *vol, const struct dir *dir, struct path *path, int len)
{
int err = 0;
struct dir *cdir = NULL;
cnid_t id;
struct adouble ad;
struct adouble *adp = NULL;
bstring fullpath = NULL;
AFP_ASSERT(vol);
AFP_ASSERT(dir);
AFP_ASSERT(path);
AFP_ASSERT(len > 0);
if ((cdir = dircache_search_by_name(vol, dir, path->u_name, strlen(path->u_name))) != NULL) {
/* there's a stray entry in the dircache */
LOG(log_debug, logtype_afpd, "dir_add(did:%u,'%s/%s'): {stray cache entry: did:%u,'%s', removing}",
ntohl(dir->d_did), cfrombstr(dir->d_fullpath), path->u_name,
ntohl(cdir->d_did), cfrombstr(dir->d_fullpath));
if (dir_remove(vol, cdir) != 0) {
dircache_dump();
AFP_PANIC("dir_add");
}
}
/* get_id needs adp for reading CNID from adouble file */
ad_init(&ad, vol);
if ((ad_open(&ad, path->u_name, ADFLAGS_HF | ADFLAGS_DIR | ADFLAGS_RDONLY)) == 0) /* 1 */
adp = &ad;
/* Get CNID */
if ((id = get_id(vol, adp, &path->st, dir->d_did, path->u_name, len)) == CNID_INVALID) { /* 2 */
err = 1;
goto exit;
}
if (adp)
ad_close(adp, ADFLAGS_HF);
/* Get macname from unixname */
if (path->m_name == NULL) {
if ((path->m_name = utompath(vol, path->u_name, id, utf8_encoding(vol->v_obj))) == NULL) {
LOG(log_error, logtype_afpd, "dir_add(\"%s\"): can't assign macname", path->u_name);
err = 2;
goto exit;
}
}
/* Build fullpath */
if ( ((fullpath = bstrcpy(dir->d_fullpath)) == NULL) /* 3 */
|| (bconchar(fullpath, '/') != BSTR_OK)
|| (bcatcstr(fullpath, path->u_name)) != BSTR_OK) {
LOG(log_error, logtype_afpd, "dir_add: fullpath: %s", strerror(errno) );
err = 3;
goto exit;
}
/* Allocate and initialize struct dir */
if ((cdir = dir_new(path->m_name,
path->u_name,
vol,
dir->d_did,
id,
fullpath,
&path->st)) == NULL) { /* 3 */
err = 4;
goto exit;
}
if ((dircache_add(vol, cdir)) != 0) { /* 4 */
LOG(log_error, logtype_afpd, "dir_add: fatal dircache error: %s", cfrombstr(fullpath));
exit(EXITERR_SYS);
}
exit:
if (err != 0) {
LOG(log_debug, logtype_afpd, "dir_add('%s/%s'): error: %u",
cfrombstr(dir->d_u_name), path->u_name, err);
if (adp)
ad_close(adp, ADFLAGS_HF);
if (!cdir && fullpath)
bdestroy(fullpath);
if (cdir)
dir_free(cdir);
cdir = NULL;
} else {
/* no error */
LOG(log_debug, logtype_afpd, "dir_add(did:%u,'%s/%s'): {cached: %u,'%s'}",
ntohl(dir->d_did), cfrombstr(dir->d_fullpath), path->u_name,
ntohl(cdir->d_did), cfrombstr(cdir->d_fullpath));
}
return(cdir);
}
/*!
* Free the queue with invalid struct dirs
*
* This gets called at the end of every AFP func.
*/
void dir_free_invalid_q(void)
{
struct dir *dir;
while ((dir = (struct dir *)dequeue(invalid_dircache_entries)))
dir_free(dir);
}
/*!
* @brief Remove a dir from a cache and queue it for freeing
*
* 1. Check if the dir is locked or has opened forks
* 2. Remove it from the cache
* 3. Queue it for removal
* 4. If it's a request to remove curdir, mark curdir as invalid
* 5. Mark it as invalid
*
* @param (r) pointer to struct vol
* @param (rw) pointer to struct dir
*/
int dir_remove(const struct vol *vol, struct dir *dir)
{
AFP_ASSERT(vol);
AFP_ASSERT(dir);
if (dir->d_did == DIRDID_ROOT_PARENT || dir->d_did == DIRDID_ROOT)
return 0;
LOG(log_debug, logtype_afpd, "dir_remove(did:%u,'%s'): {removing}",
ntohl(dir->d_did), cfrombstr(dir->d_u_name));
dircache_remove(vol, dir, DIRCACHE | DIDNAME_INDEX | QUEUE_INDEX); /* 2 */
enqueue(invalid_dircache_entries, dir); /* 3 */
if (curdir == dir) /* 4 */
curdir = NULL;
dir->d_did = CNID_INVALID; /* 5 */
return 0;
}
/*!
* @brief Resolve a catalog node name path
*
* 1. Evaluate path type
* 2. Move to start dir, if we can't, it might be e.g. because of EACCES, build
* path from dirname, so e.g. getdirparams has sth it can chew on. curdir
* is dir parent then. All this is done in path_from_dir().
* 3. Parse next cnode name in path, cases:
* 4. single "\0" -> do nothing
* 5. two or more consecutive "\0" -> chdir("..") one or more times
* 6. cnode name -> copy it to path.m_name
* 7. Get unix name from mac name
* 8. Special handling of request with did 1
* 9. stat the cnode name
* 10. If it's not there, it's probably an afp_createfile|dir,
* return with curdir = dir parent, struct path = dirname
* 11. If it's there and it's a file, it must should be the last element of the requested
* path. Return with curdir = cnode name parent dir, struct path = filename
* 12. Treat symlinks like files, don't follow them
* 13. If it's a dir:
* 14. Search the dircache for it
* 15. If it's not in the cache, create a struct dir for it and add it to the cache
* 16. chdir into the dir and
* 17. set m_name to the mac equivalent of "."
* 18. goto 3
*/
struct path *cname(struct vol *vol, struct dir *dir, char **cpath)
{
static char path[ MAXPATHLEN + 1];
static struct path ret;
struct dir *cdir;
char *data, *p;
int len;
uint32_t hint;
uint16_t len16;
int size = 0;
int toUTF8 = 0;
LOG(log_maxdebug, logtype_afpd, "cname('%s'): {start}", cfrombstr(dir->d_fullpath));
data = *cpath;
afp_errno = AFPERR_NOOBJ;
memset(&ret, 0, sizeof(ret));
switch (ret.m_type = *data) { /* 1 */
case 2:
data++;
len = (unsigned char) *data++;
size = 2;
if (vol->v_obj->afp_version >= 30) {
ret.m_type = 3;
toUTF8 = 1;
}
break;
case 3:
if (vol->v_obj->afp_version >= 30) {
data++;
memcpy(&hint, data, sizeof(hint));
hint = ntohl(hint);
data += sizeof(hint);
memcpy(&len16, data, sizeof(len16));
len = ntohs(len16);
data += 2;
size = 7;
break;
}
/* else it's an error */
default:
afp_errno = AFPERR_PARAM;
return( NULL );
}
*cpath += len + size;
path[0] = 0;
ret.m_name = path;
if (movecwd(vol, dir) < 0 ) {
LOG(log_debug, logtype_afpd, "cname(did:%u): failed to chdir to '%s'",
ntohl(dir->d_did), cfrombstr(dir->d_fullpath));
if (len == 0)
return path_from_dir(vol, dir, &ret);
else
return NULL;
}
while (len) { /* 3 */
if (*data == 0) { /* 4 or 5 */
data++;
len--;
while (len > 0 && *data == 0) { /* 5 */
/* chdir to parrent dir */
if ((dir = dirlookup(vol, dir->d_pdid)) == NULL)
return NULL;
if (movecwd( vol, dir ) < 0 ) {
dir_remove(vol, dir);
return NULL;
}
data++;
len--;
}
continue;
}
/* 6*/
for ( p = path; *data != 0 && len > 0; len-- ) {
*p++ = *data++;
if (p > &path[UTF8FILELEN_EARLY]) { /* FIXME safeguard, limit of early Mac OS X */
afp_errno = AFPERR_PARAM;
return NULL;
}
}
*p = 0; /* Terminate string */
ret.u_name = NULL;
if (cname_mtouname(vol, dir, &ret, toUTF8) != 0) { /* 7 */
LOG(log_error, logtype_afpd, "cname('%s'): error from cname_mtouname", path);
return NULL;
}
LOG(log_maxdebug, logtype_afpd, "cname('%s'): {node: '%s}", cfrombstr(dir->d_fullpath), ret.u_name);
/* Prevent access to our special folders like .AppleDouble */
if (check_name(vol, ret.u_name)) {
/* the name is illegal */
LOG(log_info, logtype_afpd, "cname: illegal path: '%s'", ret.u_name);
afp_errno = AFPERR_PARAM;
if (vol->v_obj->options.flags & OPTION_VETOMSG) {
bstring message = bformat("Attempt to access vetoed file or directory \"%s\" in directory \"%s\"",
ret.u_name, bdata(dir->d_u_name));
if (setmessage(bdata(message)) == 0)