/
netatalk_conf.c
2262 lines (1977 loc) · 76.1 KB
/
netatalk_conf.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) 2012 Frank Lahm <franklahm@gmail.com>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif /* HAVE_CONFIG_H */
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <pwd.h>
#include <grp.h>
#include <utime.h>
#include <errno.h>
#include <string.h>
#include <sys/param.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <inttypes.h>
#include <time.h>
#include <regex.h>
#if HAVE_LOCALE_H
#include <locale.h>
#endif
#if HAVE_LANGINFO_H
#include <langinfo.h>
#endif
#include <atalk/afp.h>
#include <atalk/util.h>
#include <atalk/logger.h>
#include <atalk/ea.h>
#include <atalk/globals.h>
#include <atalk/errchk.h>
#include <atalk/iniparser.h>
#include <atalk/unix.h>
#include <atalk/cnid.h>
#include <atalk/dsi.h>
#include <atalk/uuid.h>
#include <atalk/netatalk_conf.h>
#include <atalk/bstrlib.h>
#include <atalk/bstradd.h>
#define VOLPASSLEN 8
#ifndef UUID_PRINTABLE_STRING_LENGTH
#define UUID_PRINTABLE_STRING_LENGTH 37
#endif
#define IS_VAR(a, b) (strncmp((a), (b), 2) == 0)
/**************************************************************
* Locals
**************************************************************/
static int have_uservol = 0; /* whether there's generic user home share in config ("~" or "~/path", but not "~user") */
static struct vol *Volumes = NULL;
static uint16_t lastvid = 0;
/*
* Get a volumes UUID from the config file.
* If there is none, it is generated and stored there.
*
* Returns pointer to allocated storage on success, NULL on error.
*/
static char *get_vol_uuid(const AFPObj *obj, const char *volname)
{
char *volname_conf;
char buf[1024], uuid[UUID_PRINTABLE_STRING_LENGTH], *p;
FILE *fp;
int result;
long offset;
fp = fopen(obj->options.uuidconf, "r+");
if (fp != NULL) {
/* Lock the file */
result = flock(fileno(fp), LOCK_SH);
if (result != 0) {
LOG(log_error, logtype_afpd, "Can't lock UUID file %s", obj->options.uuidconf);
return NULL;
}
/* scan in the conf file */
while (fgets(buf, sizeof(buf), fp) != NULL) {
p = buf;
while (p && isblank(*p)) {
p++;
}
if (!p || (*p == '#') || (*p == '\n')) {
/* invalid line */
continue;
}
if (*p == '"') {
p++;
volname_conf = strtok( p, "\"" );
if (volname_conf == NULL) {
/* syntax error */
continue;
}
} else {
volname_conf = strtok( p, " \t" );
if (volname_conf == NULL) {
/* syntax error: invalid name */
continue;
}
}
p = strchr(p, '\0');
p++;
if (*p == '\0') {
/* syntax error */
continue;
}
if (strcmp(volname, volname_conf) != 0) {
/* another volume name */
continue;
}
while (p && isblank(*p)) {
p++;
}
if (sscanf(p, "%36s", uuid) == 1 ) {
for (int i=0; uuid[i]; i++) {
uuid[i] = toupper(uuid[i]);
}
LOG(log_debug, logtype_afpd, "get_uuid('%s'): UUID: '%s'", volname, uuid);
fclose(fp);
return strdup(uuid);
}
}
}
if (fp == NULL) {
fp = fopen(obj->options.uuidconf, "a");
if (fp == NULL) {
LOG(log_error, logtype_afpd, "Cannot create or append to %s (%s)",
obj->options.uuidconf, strerror(errno));
return NULL;
}
}
result = flock(fileno(fp), LOCK_EX);
if (result != 0) {
LOG(log_error, logtype_afpd, "Can't lock UUID file %s (%s)",
obj->options.uuidconf, strerror(errno));
fclose(fp);
return NULL;
}
result = fseek(fp, 0L, SEEK_END);
if (result != 0) {
LOG(log_error, logtype_afpd, "Can't fseek to end of UUID file %s (%s)",
obj->options.uuidconf, strerror(errno));
fclose(fp);
return NULL;
}
offset = ftell(fp);
if (offset == 0) {
fprintf(fp, "# DON'T TOUCH NOR COPY THOUGHTLESSLY!\n");
fprintf(fp, "# This file is auto-generated by afpd\n");
fprintf(fp, "# and stores UUIDs for Time Machine volumes.\n\n");
} else {
result = fseek(fp, -1L, SEEK_END);
if (result != 0) {
LOG(log_error, logtype_afpd, "Can't fseek SEEK_END-1 UUID file %s (%s)",
obj->options.uuidconf, strerror(errno));
fclose(fp);
return NULL;
}
if(fgetc(fp) != '\n') {
/* last char is \n? */
fputc('\n', fp);
}
}
/* generate uuid and write to file */
atalk_uuid_t id;
const char *cp;
randombytes((void *)id, 16);
cp = uuid_bin2string(id);
LOG(log_debug, logtype_afpd, "get_uuid('%s'): generated UUID '%s'", volname, cp);
fprintf(fp, "\"%s\"\t%36s\n", volname, cp);
fclose(fp);
return strdup(cp);
}
/*
Check if the underlying filesystem supports EAs.
If not, switch to ea:ad.
As we can't check (requires write access) on ro-volumes, we switch ea:auto
volumes that are options:ro to ea:none.
*/
#define EABUFSZ 4
static int do_check_ea_support(const struct vol *vol)
{
int haseas;
const char *eaname = "org.netatalk.has-Extended-Attributes";
const char *eacontent = "yes";
char buf[EABUFSZ];
if (sys_lgetxattr(vol->v_path, eaname, buf, EABUFSZ) != -1)
return 1;
if (vol->v_flags & AFPVOL_RO) {
LOG(log_debug, logtype_afpd, "read-only volume '%s', can't test for EA support, assuming yes", vol->v_localname);
return 1;
}
become_root();
if ((sys_setxattr(vol->v_path, eaname, eacontent, strlen(eacontent) + 1, 0)) == 0) {
haseas = 1;
} else {
LOG(log_warning, logtype_afpd, "volume \"%s\" does not support Extended Attributes or read-only volume",
vol->v_localname);
haseas = 0;
}
unbecome_root();
return haseas;
}
static void check_ea_support(struct vol *vol)
{
int haseas;
haseas = do_check_ea_support(vol);
if (vol->v_vfs_ea == AFPVOL_EA_AUTO) {
if (haseas)
vol->v_vfs_ea = AFPVOL_EA_SYS;
else
vol->v_vfs_ea = AFPVOL_EA_NONE;
}
if (vol->v_adouble == AD_VERSION_EA) {
if (!haseas)
vol->v_adouble = AD_VERSION2;
}
}
/*!
* Check whether a volume supports ACLs
*
* @param vol (r) volume
*
* @returns 0 if not, 1 if yes
*/
static int check_vol_acl_support(const struct vol *vol)
{
int ret = 0;
#ifdef HAVE_NFSV4_ACLS
ace_t *aces = NULL;
ret = 1;
if (get_nfsv4_acl(vol->v_path, &aces) == -1)
ret = 0;
#endif
#ifdef HAVE_POSIX_ACLS
acl_t acl = NULL;
ret = 1;
if ((acl = acl_get_file(vol->v_path, ACL_TYPE_ACCESS)) == NULL)
ret = 0;
#endif
#ifdef HAVE_NFSV4_ACLS
if (aces) free(aces);
#endif
#ifdef HAVE_POSIX_ACLS
if (acl) acl_free(acl);
#endif /* HAVE_POSIX_ACLS */
LOG(log_debug, logtype_afpd, "Volume \"%s\" ACL support: %s",
vol->v_path, ret ? "yes" : "no");
return ret;
}
/*
* Handle variable substitutions. here's what we understand:
* $b -> basename of path
* $c -> client ip/appletalk address
* $d -> volume pathname on server
* $f -> full name (whatever's in the gecos field)
* $g -> group
* $h -> hostname
* $i -> client ip/appletalk address without port
* $s -> server name (hostname if it doesn't exist)
* $u -> username (guest is usually nobody)
* $v -> volume name or basename if null
* $$ -> $
*
* This get's called from readvolfile with
* path = NULL, volname = NULL for xlating the volumes path
* path = path, volname = NULL for xlating the volumes name
* ... and from volumes options parsing code when xlating eg dbpath with
* path = path, volname = volname
*
* Using this information we can reject xlation of any variable depeninding on a login
* context which is not given in the afp master, where we must evaluate this whole stuff
* too for the Zeroconf announcements.
*/
static char *volxlate(const AFPObj *obj,
char *dest,
size_t destlen,
const char *src,
const struct passwd *pwd,
const char *path,
const char *volname)
{
char *p, *r;
const char *q;
int len;
char *ret;
int xlatevolname = 0;
if (path && !volname)
/* cf above */
xlatevolname = 1;
if (!src) {
return NULL;
}
if (!dest) {
dest = calloc(destlen +1, 1);
}
ret = dest;
if (!ret) {
return NULL;
}
strlcpy(dest, src, destlen +1);
if ((p = strchr(src, '$')) == NULL) /* nothing to do */
return ret;
/* first part of the path. just forward to the next variable. */
len = MIN((size_t)(p - src), destlen);
if (len > 0) {
destlen -= len;
dest += len;
}
while (p && destlen > 0) {
/* now figure out what the variable is */
q = NULL;
if (IS_VAR(p, "$b")) {
if (path) {
if ((q = strrchr(path, '/')) == NULL)
q = path;
else if (*(q + 1) != '\0')
q++;
}
} else if (IS_VAR(p, "$c")) {
if (IS_AFP_SESSION(obj)) {
DSI *dsi = obj->dsi;
len = sprintf(dest, "%s:%u",
getip_string((struct sockaddr *)&dsi->client),
getip_port((struct sockaddr *)&dsi->client));
dest += len;
destlen -= len;
}
} else if (IS_VAR(p, "$d")) {
q = path;
} else if (pwd && IS_VAR(p, "$f")) {
if ((r = strchr(pwd->pw_gecos, ',')))
*r = '\0';
q = pwd->pw_gecos;
} else if (pwd && IS_VAR(p, "$g")) {
struct group *grp = getgrgid(pwd->pw_gid);
if (grp)
q = grp->gr_name;
} else if (IS_VAR(p, "$h")) {
q = obj->options.hostname;
} else if (IS_VAR(p, "$i")) {
DSI *dsi = obj->dsi;
q = getip_string((struct sockaddr *)&dsi->client);
} else if (IS_VAR(p, "$s")) {
q = obj->options.hostname;
} else if (obj->username[0] && IS_VAR(p, "$u")) {
char* sep = NULL;
if ( obj->options.ntseparator && (sep = strchr(obj->username, obj->options.ntseparator[0])) != NULL)
q = sep+1;
else
q = obj->username;
} else if (IS_VAR(p, "$v")) {
if (volname) {
q = volname;
}
else if (path) {
if ((q = strrchr(path, '/')) == NULL)
q = path;
else if (*(q + 1) != '\0')
q++;
}
} else if (IS_VAR(p, "$$")) {
q = "$";
} else
q = p;
/* copy the stuff over. if we don't understand something that we
* should, just skip it over. */
if (q) {
len = MIN(p == q ? 2 : strlen(q), destlen);
strncpy(dest, q, len);
dest += len;
destlen -= len;
}
/* stuff up to next $ */
src = p + 2;
p = strchr(src, '$');
len = p ? MIN((size_t)(p - src), destlen) : destlen;
if (len > 0) {
strncpy(dest, src, len);
dest += len;
destlen -= len;
}
}
return ret;
}
/*!
* check access list
*
* this function wants a string consisting of names seperated by comma
* or space. Names may be quoted within a pair of quotes. Groups are
* denoted by a leading @ symbol.
* Example:
* user1 user2, user3, @group1 @group2, @group3 "user name1", "@group name1"
* A NULL argument allows everybody to have access.
* We return three things:
* -1: no list
* 0: list exists, but name isn't in it
* 1: in list
*/
static int accessvol(const AFPObj *obj, const char *args, const char *name)
{
EC_INIT;
char *names = NULL, *p;
struct group *gr;
if (!args)
EC_EXIT_STATUS(-1);
EC_NULL_LOG( names = strdup(args) );
if ((p = strtok_quote(names, ", ")) == NULL) /* nothing, return okay */
EC_EXIT_STATUS(-1);
while (p) {
if (*p == '@') { /* it's a group */
if ((gr = getgrnam(p + 1)) && gmem(gr->gr_gid, obj->ngroups, obj->groups))
EC_EXIT_STATUS(1);
} else if (strcasecmp(p, name) == 0) /* it's a user name */
EC_EXIT_STATUS(1);
p = strtok_quote(NULL, ", ");
}
EC_CLEANUP:
if (names)
free(names);
EC_EXIT;
}
static int hostaccessvol(const AFPObj *obj, const char *volname, const char *args)
{
int mask_int;
char buf[MAXPATHLEN + 1], *p, *b;
struct sockaddr_storage client;
const DSI *dsi = obj->dsi;
if (!args || !dsi)
return -1;
strlcpy(buf, args, sizeof(buf));
if ((p = strtok_r(buf, ", ", &b)) == NULL) /* nothing, return okay */
return -1;
while (p) {
int ret;
char *ipaddr, *mask_char;
struct addrinfo hints, *ai;
ipaddr = strtok(p, "/");
mask_char = strtok(NULL,"/");
/* Get address from string with getaddrinfo */
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
if ((ret = getaddrinfo(ipaddr, NULL, &hints, &ai)) != 0) {
LOG(log_error, logtype_afpd, "hostaccessvol: getaddrinfo: %s\n", gai_strerror(ret));
continue;
}
/* netmask */
if (mask_char != NULL)
mask_int = atoi(mask_char); /* apply_ip_mask does range checking on it */
else {
if (ai->ai_family == AF_INET) /* IPv4 */
mask_int = 32;
else /* IPv6 */
mask_int = 128;
}
/* Apply mask to addresses */
client = dsi->client;
apply_ip_mask((struct sockaddr *)&client, mask_int);
apply_ip_mask(ai->ai_addr, mask_int);
if (compare_ip((struct sockaddr *)&client, ai->ai_addr) == 0) {
freeaddrinfo(ai);
return 1;
}
/* next address */
freeaddrinfo(ai);
p = strtok_r(NULL, ", ", &b);
}
return 0;
}
/*!
* Get option string from config, use default value if not set
*
* @param conf (r) config handle
* @param vol (r) volume name (must be section name ie wo vars expanded)
* @param opt (r) option
* @param defsec (r) if "option" is not found in "vol", try to find it in section "defsec"
* @param defval (r) if neither "vol" nor "defsec" contain "opt" return "defval"
*
* @returns const option string from "vol" or "defsec", or "defval" if not found
*/
static const char *getoption(const dictionary *conf, const char *vol, const char *opt, const char *defsec, const char *defval)
{
const char *result;
if ((!(result = atalk_iniparser_getstring(conf, vol, opt, NULL))) && (defsec != NULL))
result = atalk_iniparser_getstring(conf, defsec, opt, NULL);
if (result == NULL)
result = defval;
return result;
}
/*!
* Get boolean option from config, use default value if not set
*
* @param conf (r) config handle
* @param vol (r) volume name (must be section name ie wo vars expanded)
* @param opt (r) option
* @param defsec (r) if "option" is not found in "vol", try to find it in section "defsec"
* @param defval (r) if neither "vol" nor "defsec" contain "opt" return "defval"
*
* @returns const option string from "vol" or "defsec", or "defval" if not found
*/
static int getoption_bool(const dictionary *conf, const char *vol, const char *opt, const char *defsec, int defval)
{
int result;
if (((result = atalk_iniparser_getboolean(conf, vol, opt, -1)) == -1) && (defsec != NULL))
result = atalk_iniparser_getboolean(conf, defsec, opt, -1);
if (result == -1)
result = defval;
return result;
}
/*!
* Get boolean option from volume, default section or global - use default value if not set
*
* Order of precedence: volume -> default section -> global -> default value
*
* "vdg" means volume, default section or global
*
* @param conf (r) config handle
* @param vol (r) volume name (must be section name ie wo vars expanded)
* @param opt (r) option
* @param defsec (r) if "option" is not found in "vol", try to find it in section "defsec"
* @param defval (r) if neither "vol" nor "defsec" contain "opt" return "defval"
*
* @returns const option string from "vol" or "defsec", or "defval" if not found
*/
static int vdgoption_bool(const dictionary *conf, const char *vol, const char *opt, const char *defsec, int defval)
{
int result;
result = atalk_iniparser_getboolean(conf, vol, opt, -1);
if ((result == -1) && (defsec != NULL))
result = atalk_iniparser_getboolean(conf, defsec, opt, -1);
if (result == -1)
result = atalk_iniparser_getboolean(conf, INISEC_GLOBAL, opt, defval);
return result;
}
/*!
* Create volume struct
*
* @param obj (r) handle
* @param pwd (r) struct passwd of logged in user, may be NULL in master afpd
* @param section (r) volume name wo variables expanded (exactly as in iniconfig)
* @param name (r) volume name
* @param path_in (r) volume path
* @param preset (r) default preset, may be NULL
* @returns vol on success, NULL on error
*/
static struct vol *creatvol(AFPObj *obj,
const struct passwd *pwd,
const char *section,
const char *name,
const char *path_in,
const char *preset)
{
EC_INIT;
struct vol *volume = NULL;
int i, suffixlen, vlen, tmpvlen, u8mvlen, macvlen;
char tmpname[AFPVOL_U8MNAMELEN+1];
char path[MAXPATHLEN + 1];
ucs2_t u8mtmpname[(AFPVOL_U8MNAMELEN+1)*2], mactmpname[(AFPVOL_MACNAMELEN+1)*2];
char suffix[6]; /* max is #FFFF */
uint16_t flags;
const char *val;
char *p, *q;
bstring dbpath = NULL;
bstring global_path_tmp = NULL;
strlcpy(path, path_in, MAXPATHLEN);
LOG(log_debug, logtype_afpd, "creatvol(volume: '%s', path: \"%s\", preset: '%s'): BEGIN",
name, path, preset ? preset : "-");
if ( name == NULL || *name == '\0' ) {
if ((name = strrchr( path, '/' )) == NULL)
EC_FAIL;
/* if you wish to share /, you need to specify a name. */
if (*++name == '\0')
EC_FAIL;
}
/* Once volumes are loaded, we never change options again, we just delete em when they're removed from afp.conf */
for (struct vol *vol = Volumes; vol; vol = vol->v_next) {
if (STRCMP(name, ==, vol->v_localname) && vol->v_deleted) {
/*
* reloading config, volume still present, nothing else to do,
* we don't change options for volumes once they're loaded
*/
vol->v_deleted = 0;
volume = vol;
EC_EXIT_STATUS(0);
}
if (STRCMP(path, ==, vol->v_path)) {
LOG(log_note, logtype_afpd, "volume \"%s\" path \"%s\" is the same as volumes \"%s\" path",
name, path, vol->v_configname);
EC_EXIT_STATUS(0);
}
/*
* We could check for nested volume paths here, but
* nobody was able to come up with an implementation yet,
* that is simple, fast and correct.
*/
}
/*
* Check allow/deny lists:
* allow -> either no list (-1), or in list (1)
* deny -> either no list (-1), or not in list (0)
*/
if (pwd) {
if (accessvol(obj, getoption(obj->iniconfig, section, "invalid users", preset, NULL), pwd->pw_name) == 1)
goto EC_CLEANUP;
if (accessvol(obj, getoption(obj->iniconfig, section, "valid users", preset, NULL), pwd->pw_name) == 0)
goto EC_CLEANUP;
if (hostaccessvol(obj, section, getoption(obj->iniconfig, section, "hosts deny", preset, NULL)) == 1)
goto EC_CLEANUP;
if (hostaccessvol(obj, section, getoption(obj->iniconfig, section, "hosts allow", preset, NULL)) == 0)
goto EC_CLEANUP;
}
EC_NULL( volume = calloc(1, sizeof(struct vol)) );
EC_NULL( volume->v_configname = strdup(section));
volume->v_vfs_ea = AFPVOL_EA_AUTO;
volume->v_umask = obj->options.umask;
if ((val = getoption(obj->iniconfig, section, "password", preset, NULL)))
EC_NULL( volume->v_password = strdup(val) );
if ((val = getoption(obj->iniconfig, section, "veto files", preset, NULL)))
EC_NULL( volume->v_veto = strdup(val) );
/* vol charset is in [G] and [V] */
if ((val = getoption(obj->iniconfig, section, "vol charset", preset, NULL))) {
if (strcasecmp(val, "UTF-8") == 0) {
val = strdup("UTF8");
}
EC_NULL( volume->v_volcodepage = strdup(val) );
}
else
EC_NULL( volume->v_volcodepage = strdup(obj->options.volcodepage) );
/* mac charset is in [G] and [V] */
if ((val = getoption(obj->iniconfig, section, "mac charset", preset, NULL))) {
if (strncasecmp(val, "MAC", 3) != 0) {
LOG(log_warning, logtype_afpd, "Is '%s' really mac charset? ", val);
}
EC_NULL( volume->v_maccodepage = strdup(val) );
}
else
EC_NULL( volume->v_maccodepage = strdup(obj->options.maccodepage) );
vlen = strlen(name);
strlcpy(tmpname, name, sizeof(tmpname));
for(i = 0; i < vlen; i++)
if(tmpname[i] == '/') tmpname[i] = ':';
if (atalk_iniparser_getboolean(obj->iniconfig, INISEC_GLOBAL, "vol dbnest", 0)) {
EC_NULL( volume->v_dbpath = strdup(path) );
} else {
char *global_path;
val = getoption(obj->iniconfig, section, "vol dbpath", preset, NULL);
if (val == NULL) {
/* check global option */
global_path = atalk_iniparser_getstring(obj->iniconfig,
INISEC_GLOBAL,
"vol dbpath",
NULL);
if (global_path) {
/* check for pre 3.1.1 behaviour without variable */
if (strchr(global_path, '$') == NULL) {
global_path_tmp = bformat("%s/%s/", global_path, tmpname);
val = cfrombstr(global_path_tmp);
} else {
val = global_path;
}
}
}
if (val == NULL) {
EC_NULL( dbpath = bformat("%s/%s/", _PATH_STATEDIR "CNID/", tmpname) );
} else {
EC_NULL( dbpath = bfromcstr(val));
}
EC_NULL( volume->v_dbpath = volxlate(obj, NULL, MAXPATHLEN + 1,
cfrombstr(dbpath), pwd, NULL, tmpname) );
}
if ((val = getoption(obj->iniconfig, section, "cnid scheme", preset, NULL)))
EC_NULL( volume->v_cnidscheme = strdup(val) );
else
volume->v_cnidscheme = strdup(DEFAULT_CNID_SCHEME);
if ((val = getoption(obj->iniconfig, section, "umask", preset, NULL)))
volume->v_umask = (int)strtol(val, NULL, 8);
if ((val = getoption(obj->iniconfig, section, "directory perm", preset, NULL)))
volume->v_dperm = (int)strtol(val, NULL, 8);
if ((val = getoption(obj->iniconfig, section, "file perm", preset, NULL)))
volume->v_fperm = (int)strtol(val, NULL, 8);
if ((val = getoption(obj->iniconfig, section, "vol size limit", preset, NULL)))
volume->v_limitsize = (uint32_t)strtoul(val, NULL, 10);
if ((val = getoption(obj->iniconfig, section, "preexec", preset, NULL)))
EC_NULL( volume->v_preexec = volxlate(obj, NULL, MAXPATHLEN, val, pwd, path, name) );
if ((val = getoption(obj->iniconfig, section, "postexec", preset, NULL)))
EC_NULL( volume->v_postexec = volxlate(obj, NULL, MAXPATHLEN, val, pwd, path, name) );
if ((val = getoption(obj->iniconfig, section, "root preexec", preset, NULL)))
EC_NULL( volume->v_root_preexec = volxlate(obj, NULL, MAXPATHLEN, val, pwd, path, name) );
if ((val = getoption(obj->iniconfig, section, "root postexec", preset, NULL)))
EC_NULL( volume->v_root_postexec = volxlate(obj, NULL, MAXPATHLEN, val, pwd, path, name) );
if ((val = getoption(obj->iniconfig, section, "appledouble", preset, NULL))) {
if (strcmp(val, "v2") == 0)
volume->v_adouble = AD_VERSION2;
else if (strcmp(val, "ea") == 0)
volume->v_adouble = AD_VERSION_EA;
} else {
volume->v_adouble = AD_VERSION;
}
if ((val = getoption(obj->iniconfig, section, "cnid server", preset, NULL))) {
EC_NULL( p = strdup(val) );
volume->v_cnidserver = p;
if ((q = strrchr(val, ':'))) {
*q++ = 0;
volume->v_cnidport = strdup(q);
} else {
volume->v_cnidport = strdup("4700");
}
} else {
volume->v_cnidserver = strdup(obj->options.Cnid_srv);
volume->v_cnidport = strdup(obj->options.Cnid_port);
}
if ((val = getoption(obj->iniconfig, section, "ea", preset, NULL))) {
if (strcasecmp(val, "ad") == 0)
volume->v_vfs_ea = AFPVOL_EA_AD;
else if (strcasecmp(val, "sys") == 0)
volume->v_vfs_ea = AFPVOL_EA_SYS;
else if (strcasecmp(val, "none") == 0)
volume->v_vfs_ea = AFPVOL_EA_NONE;
else if (strcasecmp(val, "samba") == 0) {
volume->v_vfs_ea = AFPVOL_EA_SYS;
volume->v_flags |= AFPVOL_EA_SAMBA;
}
}
if ((val = getoption(obj->iniconfig, section, "casefold", preset, NULL))) {
if (strcasecmp(val, "tolower") == 0)
volume->v_casefold = AFPVOL_UMLOWER;
else if (strcasecmp(val, "toupper") == 0)
volume->v_casefold = AFPVOL_UMUPPER;
else if (strcasecmp(val, "xlatelower") == 0)
volume->v_casefold = AFPVOL_UUPPERMLOWER;
else if (strcasecmp(val, "xlateupper") == 0)
volume->v_casefold = AFPVOL_ULOWERMUPPER;
}
if (getoption_bool(obj->iniconfig, section, "case sensitive", preset, 1))
volume->v_casefold |= AFPVOL_CASESENS;
if (getoption_bool(obj->iniconfig, section, "read only", preset, 0))
volume->v_flags |= AFPVOL_RO;
if (getoption_bool(obj->iniconfig, section, "invisible dots", preset, 0))
volume->v_flags |= AFPVOL_INV_DOTS;
if (!getoption_bool(obj->iniconfig, section, "stat vol", preset, 1))
volume->v_flags |= AFPVOL_NOSTAT;
if (getoption_bool(obj->iniconfig, section, "unix priv", preset, 1))
volume->v_flags |= AFPVOL_UNIX_PRIV;
if (!getoption_bool(obj->iniconfig, section, "cnid dev", preset, 1))
volume->v_flags |= AFPVOL_NODEV;
if (getoption_bool(obj->iniconfig, section, "illegal seq", preset, 0))
volume->v_flags |= AFPVOL_EILSEQ;
if (getoption_bool(obj->iniconfig, section, "time machine", preset, 0))
volume->v_flags |= AFPVOL_TM;
if (getoption_bool(obj->iniconfig, section, "search db", preset, 0))
volume->v_flags |= AFPVOL_SEARCHDB;
if (!getoption_bool(obj->iniconfig, section, "network ids", preset, 1))
volume->v_flags |= AFPVOL_NONETIDS;
#ifdef HAVE_ACLS
if (getoption_bool(obj->iniconfig, section, "acls", preset, 1))
volume->v_flags |= AFPVOL_ACLS;
#endif
if (!getoption_bool(obj->iniconfig, section, "convert appledouble", preset, 1))
volume->v_flags |= AFPVOL_NOV2TOEACONV;
if (getoption_bool(obj->iniconfig, section, "follow symlinks", preset, 0))
volume->v_flags |= AFPVOL_FOLLOWSYM;
if (getoption_bool(obj->iniconfig, section, "spotlight", preset, obj->options.flags & OPTION_SPOTLIGHT_VOL)) {
volume->v_flags |= AFPVOL_SPOTLIGHT;
obj->options.flags |= OPTION_SPOTLIGHT;
}
if (getoption_bool(obj->iniconfig, section, "delete veto files", preset, 0))
volume->v_flags |= AFPVOL_DELVETO;
if (getoption_bool(obj->iniconfig, section, "preexec close", preset, 0))
volume->v_preexec_close = 1;
if (getoption_bool(obj->iniconfig, section, "root preexec close", preset, 0))
volume->v_root_preexec_close = 1;
if (vdgoption_bool(obj->iniconfig, section, "force xattr with sticky bit", preset, 0))
volume->v_flags |= AFPVOL_FORCE_STICKY_XATTR;
if ((val = getoption(obj->iniconfig, section, "ignored attributes", preset, obj->options.ignored_attr))) {
if (strstr(val, "all")) {
volume->v_ignattr |= ATTRBIT_NOWRITE | ATTRBIT_NORENAME | ATTRBIT_NODELETE;
}
if (strstr(val, "nowrite")) {
volume->v_ignattr |= ATTRBIT_NOWRITE;
}
if (strstr(val, "norename")) {
volume->v_ignattr |= ATTRBIT_NORENAME;
}
if (strstr(val, "nodelete")) {
volume->v_ignattr |= ATTRBIT_NODELETE;
}
}
val = getoption(obj->iniconfig, section, "chmod request", preset, NULL);
if (val == NULL) {
val = atalk_iniparser_getstring(obj->iniconfig, INISEC_GLOBAL, "chmod request", "preserve");
}
if (strcasecmp(val, "ignore") == 0) {
volume->v_flags |= AFPVOL_CHMOD_IGNORE;
} else if (strcasecmp(val, "preserve") == 0) {
volume->v_flags |= AFPVOL_CHMOD_PRESERVE_ACL;
} else if (strcasecmp(val, "simple") != 0) {
LOG(log_warning, logtype_afpd, "unknown 'chmod request' setting: '%s', using default", val);
volume->v_flags |= AFPVOL_CHMOD_PRESERVE_ACL;
}
/*
* Handle read-only behaviour. semantics:
* 1) neither the rolist nor the rwlist exist -> rw
* 2) rolist exists -> ro if user is in it.
* 3) rwlist exists -> ro unless user is in it.
* 4) cnid scheme = last -> ro forcibly.
*/
if (pwd) {
if (accessvol(obj, getoption(obj->iniconfig, section, "rolist", preset, NULL), pwd->pw_name) == 1
|| accessvol(obj, getoption(obj->iniconfig, section, "rwlist", preset, NULL), pwd->pw_name) == 0)
volume->v_flags |= AFPVOL_RO;
}
if (0 == strcmp(volume->v_cnidscheme, "last"))
volume->v_flags |= AFPVOL_RO;
if ((volume->v_flags & AFPVOL_NODEV))
volume->v_ad_options |= ADVOL_NODEV;
if ((volume->v_flags & AFPVOL_UNIX_PRIV))
volume->v_ad_options |= ADVOL_UNIXPRIV;
if ((volume->v_flags & AFPVOL_INV_DOTS))
volume->v_ad_options |= ADVOL_INVDOTS;
if ((volume->v_flags & AFPVOL_FOLLOWSYM))
volume->v_ad_options |= ADVOL_FOLLO_SYML;
if ((volume->v_flags & AFPVOL_RO))
volume->v_ad_options |= ADVOL_RO;
if ((volume->v_flags & AFPVOL_FORCE_STICKY_XATTR))
volume->v_ad_options |= ADVOL_FORCE_STICKY_XATTR;
/* Mac to Unix conversion flags*/
if ((volume->v_flags & AFPVOL_EILSEQ))
volume->v_mtou_flags |= CONV__EILSEQ;
if ((volume->v_casefold & AFPVOL_MTOUUPPER))
volume->v_mtou_flags |= CONV_TOUPPER;
else if ((volume->v_casefold & AFPVOL_MTOULOWER))
volume->v_mtou_flags |= CONV_TOLOWER;
/* Unix to Mac conversion flags*/
volume->v_utom_flags = CONV_IGNORE;
if ((volume->v_casefold & AFPVOL_UTOMUPPER))
volume->v_utom_flags |= CONV_TOUPPER;
else if ((volume->v_casefold & AFPVOL_UTOMLOWER))
volume->v_utom_flags |= CONV_TOLOWER;
if ((volume->v_flags & AFPVOL_EILSEQ))
volume->v_utom_flags |= CONV__EILSEQ;
/* suffix for mangling use (lastvid + 1) */
/* because v_vid has not been decided yet. */
if (lastvid == UINT16_MAX) {
LOG(log_error, logtype_default, "vid overflow");
EC_FAIL;
}
suffixlen = snprintf(suffix, sizeof(suffix), "#%X", lastvid + 1 );
if (suffixlen >= sizeof(suffix)) {
LOG(log_error, logtype_default, "vid overflow");
EC_FAIL;
}
/* Unicode Volume Name */
/* Firstly convert name from unixcharset to UTF8-MAC */
flags = CONV_IGNORE;
tmpvlen = convert_charset(obj->options.unixcharset, CH_UTF8_MAC, 0, name, vlen, tmpname, AFPVOL_U8MNAMELEN, &flags);
if (tmpvlen <= 0) {
strcpy(tmpname, "???");
tmpvlen = 3;
}
/* Do we have to mangle ? */
if ( (flags & CONV_REQMANGLE) || (tmpvlen > obj->options.volnamelen)) {
if (tmpvlen + suffixlen > obj->options.volnamelen) {
flags = CONV_FORCE;
tmpvlen = convert_charset(obj->options.unixcharset, CH_UTF8_MAC, 0, name, vlen, tmpname, obj->options.volnamelen - suffixlen, &flags);
tmpname[tmpvlen >= 0 ? tmpvlen : 0] = 0;
}
strcat(tmpname, suffix);
tmpvlen = strlen(tmpname);
}
/* Secondly convert name from UTF8-MAC to UCS2 */
if ( 0 >= ( u8mvlen = convert_string(CH_UTF8_MAC, CH_UCS2, tmpname, tmpvlen, u8mtmpname, AFPVOL_U8MNAMELEN*2)) )
EC_FAIL;