-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.c
1680 lines (1395 loc) · 34.8 KB
/
util.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
#include "util.h"
extern int verbose;
const int CMDCLOSED = 0x0000,
CMDSTART = 0x0001,
CMDSTOP = 0x0002,
CMDREADY = 0x0004,
CMDROTCLEANUP = 0x0008,
CMDROTINCLUDE = 0x000c,
CMDCUST = 0x0010; /* Followed by a custom integer. */
static struct snapinterval *snapshotinterval(const struct snapshot *);
struct snapinterval *
snaps_alloc_snapinterval(char *name, int count, time_t lifetime)
{
struct snapinterval *si;
si = malloc(sizeof(struct snapinterval));
if (si == NULL)
err(1, "%s: malloc", __func__);
si->name = strdup(name);
if (si->name == NULL)
err(1, "%s: strdup", __func__);
si->count = count;
si->lifetime = lifetime;
return si;
}
void
snaps_free_snapinterval(struct snapinterval **si)
{
if (*si == NULL)
return;
free((*si)->name);
(*si)->name = NULL;
(*si)->count = 0;
(*si)->lifetime = 0;
free(*si);
*si = NULL;
}
/* Add a new snapinterval while ensuring order by lifetime. */
struct snapinterval **
snaps_add_snapinterval(struct snapinterval **siv, struct snapinterval *si)
{
int i;
/* find first empty slot */
for (i = 0; siv != NULL && siv[i] != NULL; i++)
;
/* ensure enough space */
siv = reallocarray(siv, i + 2, sizeof(si));
if (siv == NULL)
err(1, "%s: reallocarray", __func__);
/* terminate new vector */
siv[i+1] = NULL;
/* don't just append, ensure order from short to long lifetime */
for ( ; i > 0 && si->lifetime < siv[i-1]->lifetime; i--)
siv[i] = siv[i-1];
siv[i] = si;
return siv;
}
/* Free all intervals in siv and siv itself. */
void
snaps_clear_snapintervalv(struct snapinterval ***siv)
{
int n;
if (*siv == NULL)
return;
for (n = 0; (*siv)[n] != NULL; n++)
snaps_free_snapinterval(&(*siv)[n]);
free(*siv);
*siv = NULL;
}
struct endpoint *
snaps_alloc_endpoint(char *ruser, char *hostname, char *rpath, char *root,
int createroot, gid_t shared, uid_t uid, gid_t gid,
struct snapinterval **snapshots)
{
char *pathcomp;
struct endpoint *ep;
ep = malloc(sizeof(struct endpoint));
if (ep == NULL)
err(1, "%s: malloc", __func__);
ep->ruser = strdup(ruser);
if (ep->ruser == NULL)
err(1, "%s: strdup", __func__);
ep->hostname = strdup(hostname);
if (ep->hostname == NULL)
err(1, "%s: strdup", __func__);
ep->rpath = strdup(rpath);
if (ep->rpath == NULL)
err(1, "%s: strdup", __func__);
ep->root = strdup(root);
if (ep->root == NULL)
err(1, "%s: strdup", __func__);
ep->createroot = createroot;
ep->shared = shared;
ep->uid = uid;
ep->gid = gid;
/* Base local path on the root, hostname and remote path. */
if (asprintf(&pathcomp, "%s/%s", ep->hostname, ep->rpath) <= 0)
err(1, "%s: asprintf", __func__);
if (normalize_pathcomp(pathcomp) != 0)
errx(1, "%s could not normalize path component: %s", __func__,
pathcomp);
/* set path by prepending the root */
if (asprintf(&ep->path, "%s/%s", ep->root, pathcomp) <= 0)
err(1, "%s: asprintf", __func__);
free(pathcomp);
pathcomp = NULL;
ep->pathfd = -1;
ep->snapshots = snapshots;
ep->rsyncbin = NULL;
ep->rsyncargv = NULL;
ep->rsyncexit = NULL;
ep->postexec = NULL;
ep->rotfd = -1;
ep->rotpid = -1;
ep->synfd = -1;
ep->synpid = -1;
ep->poxfd = -1;
ep->poxpid = -1;
return ep;
}
void
snaps_free_endpoint(struct endpoint **ep)
{
if (*ep == NULL)
return;
free((*ep)->ruser);
(*ep)->ruser = NULL;
free((*ep)->hostname);
(*ep)->hostname = NULL;
free((*ep)->rpath);
(*ep)->rpath = NULL;
free((*ep)->root);
(*ep)->root = NULL;
free((*ep)->path);
(*ep)->path = NULL;
(*ep)->createroot = 0;
(*ep)->shared = -1;
(*ep)->uid = -1;
(*ep)->gid = -1;
free((*ep)->rsyncbin);
(*ep)->rsyncbin = NULL;
clrstrv(&(*ep)->rsyncargv); /* rsyncargv is set to null */
clrintv(&(*ep)->rsyncexit); /* rsyncexit is set to null */
free((*ep)->postexec);
(*ep)->postexec = NULL;
/* close fd to the path */
if (close((*ep)->pathfd) == -1 && errno != EBADF)
err(1, "close pathfd");
(*ep)->pathfd = -1;
/* close fd to the rotator */
if (close((*ep)->rotfd) == -1 && errno != EBADF)
err(1, "close rotfd");
(*ep)->rotfd = -1;
/* close fd to the syncer */
if (close((*ep)->synfd) == -1 && errno != EBADF)
err(1, "close synfd");
(*ep)->synfd = -1;
/* close fd to postexec */
if (close((*ep)->poxfd) == -1 && errno != EBADF)
err(1, "close poxfd");
(*ep)->poxfd = -1;
(*ep)->rotpid = -1;
(*ep)->synpid = -1;
(*ep)->poxpid = -1;
snaps_clear_snapintervalv(&(*ep)->snapshots);
free(*ep);
*ep = NULL;
}
void
snaps_endpoint_setopts(struct endpoint *ep, char *rsyncbin, char **rsyncargv,
int **rsyncexit, char *postexec)
{
if (ep == NULL)
return;
free(ep->rsyncbin);
ep->rsyncbin = NULL;
clrstrv(&ep->rsyncargv); /* rsyncargv is set to null */
clrintv(&ep->rsyncexit); /* rsyncexit is set to null */
free(ep->postexec);
ep->postexec = NULL;
if (rsyncbin != NULL) {
ep->rsyncbin = strdup(rsyncbin);
if (ep->rsyncbin == NULL)
err(1, "%s: strdup", __func__);
}
/* NULL is a valid value to the dup functions. exits on error */
ep->rsyncargv = dupstrv(rsyncargv);
ep->rsyncexit = dupintv(rsyncexit);
if (postexec != NULL) {
ep->postexec = strdup(postexec);
if (ep->postexec == NULL)
err(1, "%s: strdup", __func__);
}
}
struct endpoint **
snaps_add_endpoint(struct endpoint **epv, struct endpoint *ep)
{
int i;
if (ep == NULL)
return epv;
/* find first empty slot */
for (i = 0; epv != NULL && epv[i] != NULL; i++)
;
/* ensure enough space */
epv = reallocarray(epv, i + 2, sizeof(ep));
if (epv == NULL)
err(1, "%s: reallocarray", __func__);
epv[i] = ep;
/* terminate vector */
epv[i+1] = NULL;
return epv;
}
/*
* Ensure ep is freed and no longer part of epv.
*/
struct endpoint **
snaps_rm_endpoint(struct endpoint **epv, struct endpoint *ep)
{
int n;
if (epv == NULL)
return epv;
/* find ep */
for (n = 0; epv[n] != NULL && epv[n] != ep; n++)
;
if (epv[n] == NULL)
return epv;
/* free and compact */
snaps_free_endpoint(&epv[n]);
do {
epv[n] = epv[n + 1];
n++;
} while (epv[n] != NULL);
/* resize */
epv = reallocarray(epv, n, sizeof(ep));
if (epv == NULL)
err(1, "%s: reallocarray", __func__);
return epv;
}
/* Open endpoint root dir for relative path reference. */
void
snaps_endpoint_openrootfd(struct endpoint *ep)
{
/* (re)open fd to new path */
if (close(ep->pathfd) == -1 && errno != EBADF)
err(1, "%s: close pathfd", __func__);
ep->pathfd = open(ep->path, O_RDONLY | O_DIRECTORY | O_CLOEXEC |
O_NOFOLLOW);
if (ep->pathfd == -1)
err(1, "%s: open pathfd %s", __func__, ep->path);
}
/*
* Set a new path for the endpoint and (re)open a file descriptor to it.
*/
void
snaps_endpoint_chpath(struct endpoint *ep, const char *npath)
{
if (ep == NULL)
return;
free(ep->path);
ep->path = strdup(npath);
if (ep->path == NULL)
err(1, "%s: strdup", __func__);
snaps_endpoint_openrootfd(ep);
}
/*
* Keep one endpoint and remove all the others.
*
* Returns a new pointer to a possibly relocated epv, exits if memory allocation
* fails.
*/
struct endpoint **
snaps_keep_one_endpoint(struct endpoint **epv, struct endpoint *ep)
{
int n;
if (epv == NULL)
return epv;
/* free everything except ep */
for (n = 0; epv[n] != NULL; n++)
if (epv[n] != ep)
snaps_free_endpoint(&epv[n]);
/* ensure ep is the first item */
epv[0] = ep;
epv[1] = NULL;
/* resize */
epv = reallocarray(epv, 2, sizeof(ep));
if (epv == NULL)
err(1, "%s: reallocarray", __func__);
return epv;
}
/*
* Make sure a directory exists. Creates directories recursively if needed but
* only if all ancestors are owned by the superuser and none of them is writable
* by either the group or others.
*
* If updmod is not nul, it will be updated to contain whether or not the mode
* or owner are updated.
*
* If the path already exists but is not a directory return -1 and set errno to
* ENOTDIR.
*
* Returns the number of created directories on success, or -1 on failure with
* errno set to indicate the error.
*/
int
secureensuredir(const char *p, mode_t mode, gid_t gid, int *updmod)
{
struct stat st;
int created = 0, done, trusted, i;
char path[PATH_MAX], *slash;
mode_t cmode;
/* The file access permission bits. */
const mode_t pbits = S_ISUID | S_ISGID | S_ISVTX | S_IRWXU | S_IRWXG |
S_IRWXO;
/* Only permission bits should be passed. */
if ((mode & ~pbits) != 0) {
errno = EINVAL;
return -1;
}
/* Requests for a writable "group" or "other" won't be serviced. */
if ((mode & (S_IWGRP | S_IWOTH)) != 0) {
errno = EINVAL;
return -1;
}
/*
* First check if the existing part can be trusted.
*/
if (trustedpath(p, S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH, gid, &trusted,
NULL) != 0)
err(1, "%s: trustedpath", __func__);
if (!trusted) {
errno = EPERM;
return -1;
}
/* Ensure an absolute path to work with. */
path[0] = '\0';
if (p[0] != '/') {
if (getcwd(path, sizeof(path)) == NULL)
err(1, "%s: getcwd", __func__);
if (path[0] != '/') /* See getcwd(2) on Linux. */
errx(1, "are you in a chroot and forgot to chdir(2)?");
/* ensure termination with a '/' */
i = strrchr(path, '\0') - path;
if (path[i - 1] != '/') {
if (i + 1 >= sizeof(path)) {
errno = ENAMETOOLONG;
return -1;
}
path[i++] = '/';
path[i] = '\0';
}
}
if (strlcat(path, p, sizeof(path)) >= sizeof(path)) {
errno = ENAMETOOLONG;
return -1;
}
slash = path;
for (;;) {
slash += strspn(slash, "/");
slash += strcspn(slash, "/");
done = (*slash == '\0');
*slash = '\0';
if (stat(path, &st) == -1) {
if (errno == ENOENT) {
if (mkdir(path, mode) == -1)
err(1, "mkdir: %s", path);
created++;
} else {
/* errno is set by stat */
return -1;
}
} else {
/*
* The path exists. Check type and trust trustedpath to
* have checked ownership and permissions.
*/
if (!S_ISDIR(st.st_mode)) {
errno = ENOTDIR;
return -1;
}
}
if (done)
break;
*slash = '/';
}
if (updmod != NULL)
*updmod = 0;
/*
* Ensure the requested permissions.
*/
if (stat(path, &st) == -1)
err(1, "%s: stat", __func__);
cmode = st.st_mode & pbits;
if (cmode != mode) {
if (chmod(path, mode) == -1)
return -1;
if (updmod != NULL)
*updmod = 1;
}
/*
* Ensure the requested group owner.
*/
if (gid != -1) {
if (st.st_gid != gid) {
if (chown(path, -1, gid) == -1)
return -1;
if (updmod != NULL)
*updmod = 1;
}
}
return created;
}
/*
* Find out if all existing components of a path are owned by the superuser and
* are not writable by the group or others. The final component, if it exists,
* must have a mode that is a subset of the given relax mode. Furthermore, if a
* group id is provided, than the final component must be owned by this group.
*
* Supported relax bits: S_IRGRP, S_IXGRP, S_IROTH, S_IXOTH.
*
* Return 0 on success or -1 on failure with errno set. If the path can be
* trusted than "trusted" is set to 1, otherwise to 0. If the path is trusted
* and "exists" is not null, then it will be set to 1 if all components exist in
* the file-system or 0 if some part does not exist.
*/
int
trustedpath(const char *p, mode_t relax, gid_t gid, int *trusted, int *ex)
{
struct stat st;
int eop, symlinks, exists;
size_t i;
ssize_t slen;
mode_t mode;
char symlink[PATH_MAX], path[PATH_MAX], *slash;
if (p == NULL) {
errno = EINVAL;
return -1;
}
/* Check if only supported relax bits are passed. */
if ((relax & (S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH)) != relax) {
errno = EINVAL;
return -1;
}
if (trusted == NULL) {
errno = EINVAL;
return -1;
}
if (p[0] == '\0') {
errno = ENOENT;
return -1;
}
/* Special check the root. */
if (lstat("/", &st) == -1)
err(1, "%s: lstat", __func__);
if (!S_ISDIR(st.st_mode)) {
/* XXX can this ever happen? */
errno = ENOTDIR;
return -1;
}
*trusted = 0;
if (st.st_uid != 0)
return 0;
if (st.st_mode & (S_IWGRP | S_IWOTH))
return 0;
exists = 1;
/* Ensure an absolute path to work with. */
path[0] = '\0';
if (p[0] != '/') {
if (getcwd(path, sizeof(path)) == NULL)
err(1, "%s: getcwd", __func__);
if (path[0] != '/') /* See getcwd(2) on Linux. */
errx(1, "are you in a chroot and forgot to chdir(2)?");
/* ensure termination with a '/' */
i = strrchr(path, '\0') - path;
if (path[i - 1] != '/') {
if (i + 1 >= sizeof(path)) {
errno = ENAMETOOLONG;
return -1;
}
path[i++] = '/';
path[i] = '\0';
}
}
if (strlcat(path, p, sizeof(path)) >= sizeof(path)) {
errno = ENAMETOOLONG;
return -1;
}
symlinks = 0;
slash = path;
for (;;) {
/* Move to end of the next component. */
slash += strspn(slash, "/");
slash += strcspn(slash, "/");
eop = (*slash == '\0');
*slash = '\0';
if (lstat(path, &st) == -1) {
if (errno == ENOENT) {
/*
* We made it to a non-existing component, we're
* good.
*/
exists = 0;
break;
} else {
err(1, "%s: lstat \"%s\" of \"%s\"",
__func__, path, p);
}
}
if (st.st_uid != 0)
return 0;
if (st.st_mode & (S_IWGRP | S_IWOTH))
return 0;
if (S_ISLNK(st.st_mode)) {
if (++symlinks > SYMLOOP_MAX) {
errno = ELOOP;
return -1;
}
slen = readlink(path, symlink, sizeof(symlink));
if (slen == -1) {
return -1;
} else if (slen == 0) {
errno = EINVAL;
return -1;
} else if (slen == sizeof(symlink)) {
errno = ENAMETOOLONG;
return -1;
}
symlink[slen] = '\0';
/* If there is anything left, append it to symlink. */
if (!eop) {
if (symlink[slen - 1] != '/') {
if (slen + 1 >= sizeof(symlink)) {
errno = ENAMETOOLONG;
return -1;
}
symlink[slen++] = '/';
symlink[slen] = '\0';
}
if (strlcat(symlink, slash + 1, sizeof(symlink))
>= sizeof(symlink)) {
errno = ENAMETOOLONG;
return -1;
}
}
/*
* If the symlink target is absolute, replace all
* preceding components with the target, else replace
* only the last component. Make sure slash keeps
* pointing to the last processed slash.
*/
if (symlink[0] == '/') {
/* point to new symlink root */
path[0] = '\0';
slash = path;
} else {
/* strip the component after the last '/' */
slash = strrchr(path, '/');
*(slash + 1) = '\0';
}
if (strlcat(path, symlink, sizeof(path))
>= sizeof(path)) {
errno = ENAMETOOLONG;
return -1;
}
eop = 0;
}
/* If this was the last part, we're good. */
if (eop)
break;
*slash = '/';
}
/*
* Do some stricter checks on the final component if it exists.
*
* Check if the permission bits of the final component are a subset of
* the given relax mode. If a group id is given, make sure these match
* as well.
*/
if (exists) {
/*
* Get all file access permission bits except the owner bits and
* the saved-text bit. It is already verified that the owner is
* the superuser and the presence of the saved-text bit would
* only impose extra restrictions so it's safe to ignore.
*/
mode = st.st_mode & (S_ISUID | S_ISGID | S_IRWXG | S_IRWXO);
if ((mode & ~relax) != 0)
return 0;
if (gid != -1)
if (st.st_gid != gid)
return 0;
}
*trusted = 1;
if (ex != NULL)
*ex = exists;
return 0;
}
/*
* Normalize path.
*
* Remove extraneous slashes, "." and ".." components, turn a relative path into
* an absolute path and ensure termination with a "/". Note that apart from
* calling getcwd on a relative path no checks are done within the file-system
* if any of the components exist or are of a certain type.
*
* Return the normalized path on success, NULL on error and set errno. The
* returned variable should be free(3)'d if not passed by the caller.
*
* Based on realpath(2) that ships with OpenBSD.
*/
/* $OpenBSD: realpath.c,v 1.22 2017/12/24 01:50:50 millert Exp $ */
/*
* Copyright (c) 2003 Constantin S. Svintsoff <[email protected]>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The names of the authors may not be used to endorse or promote
* products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/*
* Returns (resolved) on success, or (NULL) on failure, in which case the path
* which caused trouble is left in (resolved).
*/
char *
normalize_path(const char *path, char *resolved, int withslash)
{
const char *p;
char *q;
size_t left_len, resolved_len, next_token_len;
int mem_allocated;
char left[PATH_MAX], next_token[PATH_MAX];
if (path == NULL) {
errno = EINVAL;
return (NULL);
}
if (path[0] == '\0') {
errno = ENOENT;
return (NULL);
}
if (resolved == NULL) {
resolved = malloc(PATH_MAX);
if (resolved == NULL)
return (NULL);
mem_allocated = 1;
} else
mem_allocated = 0;
if (path[0] == '/') {
resolved[0] = '/';
resolved[1] = '\0';
if (path[1] == '\0')
return (resolved);
resolved_len = 1;
left_len = strlcpy(left, path + 1, sizeof(left));
} else {
if (getcwd(resolved, PATH_MAX) == NULL) {
if (mem_allocated)
free(resolved);
else
strlcpy(resolved, ".", PATH_MAX);
return (NULL);
}
resolved_len = strlen(resolved);
left_len = strlcpy(left, path, sizeof(left));
}
if (left_len >= sizeof(left)) {
errno = ENAMETOOLONG;
goto err;
}
/*
* Iterate over path components in `left'.
*/
while (left_len != 0) {
/*
* Extract the next path component and adjust `left'
* and its length.
*/
p = strchr(left, '/');
next_token_len = p ? (size_t) (p - left) : left_len;
memcpy(next_token, left, next_token_len);
next_token[next_token_len] = '\0';
if (p != NULL) {
left_len -= next_token_len + 1;
memmove(left, p + 1, left_len + 1);
} else {
left[0] = '\0';
left_len = 0;
}
if (resolved[resolved_len - 1] != '/') {
if (resolved_len + 1 >= PATH_MAX) {
errno = ENAMETOOLONG;
goto err;
}
resolved[resolved_len++] = '/';
resolved[resolved_len] = '\0';
}
if (next_token[0] == '\0')
continue;
else if (strcmp(next_token, ".") == 0)
continue;
else if (strcmp(next_token, "..") == 0) {
/*
* Strip the last path component except when we have
* single "/"
*/
if (resolved_len > 1) {
resolved[resolved_len - 1] = '\0';
q = strrchr(resolved, '/') + 1;
*q = '\0';
resolved_len = q - resolved;
}
continue;
}
/*
* Append the next path component.
*/
resolved_len = strlcat(resolved, next_token, PATH_MAX);
if (resolved_len >= PATH_MAX) {
errno = ENAMETOOLONG;
goto err;
}
}
if (withslash) {
/* Ensure termination with '/'. */
if (resolved[resolved_len - 1] != '/') {
if (resolved_len + 1 >= PATH_MAX) {
errno = ENAMETOOLONG;
goto err;
}
resolved[resolved_len++] = '/';
resolved[resolved_len] = '\0';
}
} else {
/*
* Remove trailing slash except when the resolved pathname
* is a single "/".
*/
if (resolved_len > 1 && resolved[resolved_len - 1] == '/')
resolved[resolved_len - 1] = '\0';
}
return (resolved);
err:
if (mem_allocated)
free(resolved);
return (NULL);
}
/*
* Check if an endpoint with the given identifier exists in the endpoint vector.
*
* Return a pointer to the endpoint if found, NULL otherwise.
*/
struct endpoint *
snaps_find_endpoint(struct endpoint **epv, const char *id)
{
while (epv && *epv) {
if (strcmp(snaps_endpoint_id(*epv), id) == 0)
return *epv;
epv++;
}
return NULL;
}
/*
* Return a unique identifier for this endpoint.
*/
char *
snaps_endpoint_id(struct endpoint *ep)
{
return ep->path;
}
/*
* Check if path is within root.
*
* If 'issubdir' is not nul, set it to 1 if path is a subdir of root, 0
* otherwise.
*
* Return 1 if path equals or is within root, 0 otherwise.
*/
int
inroot(const char *root, const char *path, int *issubdir)
{
char p1[PATH_MAX], p2[PATH_MAX];
/* init */
if (issubdir != NULL)
*issubdir = 0;
if (root == NULL || path == NULL)
return 0;
if (root[0] == '\0' || path[0] == '\0')
return 0;
if (normalize_path(root, p1, 1) == NULL)
err(1, "%s: normalize_path: %s", __func__, root);
if (normalize_path(path, p2, 1) == NULL)
err(1, "%s: normalize_path: %s", __func__, path);
if (strncmp(p1, p2, strlen(p1)) == 0) {
if (issubdir != NULL && strlen(p2) > strlen(p1))
*issubdir = 1;
return 1;
}
return 0;
}
/*
* Turns the string path into one pathname component by replacing each slash
* with an underscore.
*
* Further, it collapses multiple slashes into one and removes a trailing slash, if any.
*
* 1. make sure the length is at least 1
* 2. make sure the length does not exceed NAME_MAX
* 3. make sure it's not ".." or ".", it's ok to start with one of those
* 4. collapse multiple slashes into one.
* 5. remove trailing slash if any, unless only one slash is there
* 6. replace any slash with an underscore
*
* Return 0 on success, -1 on error.
*/
int
normalize_pathcomp(char *path)
{
size_t len, i, j;