summaryrefslogtreecommitdiff
path: root/src/tools/files.c
blob: 62fd00368c4e62715802d3c6c353985897569e3d (plain)
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
/*
    Authors:
        Jakub Hrozek <jhrozek@redhat.com>

    Copyright (C) 2009 Red Hat

    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 3 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.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

/*
 * This file incorporates work covered by the following copyright and
 * permission notice:
 *
 * Copyright (c) 1991 - 1994, Julianne Frances Haugh
 * Copyright (c) 1996 - 2001, Marek Michałkiewicz
 * Copyright (c) 2003 - 2006, Tomasz Kłoczko
 * Copyright (c) 2007 - 2008, Nicolas François
 *
 * All rights reserved.
 *
 * 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 name of the copyright holders or contributors may not be used to
 *    endorse or promote products derived from this software without
 *    specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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 COPYRIGHT
 * HOLDERS 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.
 */

#include "config.h"

#include <sys/stat.h>
#include <sys/types.h>
#include <sys/time.h>
#include <dirent.h>
#include <fcntl.h>
#include <errno.h>
#include <talloc.h>

#include "util/util.h"
#include "tools/tools_util.h"

struct copy_ctx {
    const char *src_orig;
    const char *dst_orig;
    dev_t       src_dev;
    uid_t       uid;
    gid_t       gid;
};

static int open_cloexec(const char *pathname, int flags, int *ret)
{
    int fd;
    int oflags;

    oflags = flags;
#ifdef O_CLOEXEC
    oflags |= O_CLOEXEC;
#endif

    errno = 0;
    fd = open(pathname, oflags);
    if (fd == -1) {
        if (ret) {
            *ret = errno;
        }
        return -1;
    }

#ifndef O_CLOEXEC
    int v;

    v = fcntl(fd, F_GETFD, 0);
    /* we ignore an error, it's not fatal and there is nothing we
     * can do about it anyways */
    (void)fcntl(fd, F_SETFD, v | FD_CLOEXEC);
#endif

    return fd;
}

static int openat_cloexec(int dir_fd, const char *pathname, int flags, int *ret)
{
    int fd;
    int oflags;

    oflags = flags;
#ifdef O_CLOEXEC
    oflags |= O_CLOEXEC;
#endif

    errno = 0;
    fd = openat(dir_fd, pathname, oflags);
    if (fd == -1) {
        if (ret) {
            *ret = errno;
        }
        return -1;
    }

#ifndef O_CLOEXEC
    int v;

    v = fcntl(fd, F_GETFD, 0);
    /* we ignore an error, it's not fatal and there is nothing we
     * can do about it anyways */
    (void)fcntl(fd, F_SETFD, v | FD_CLOEXEC);
#endif

    return fd;
}

static int sss_timeat_set(int dir_fd, const char *path,
                          const struct stat *statp,
                          int flags)
{
    int ret;

#ifdef HAVE_UTIMENSAT
    struct timespec timebuf[2];

    timebuf[0] = statp->st_atim;
    timebuf[1] = statp->st_mtim;

    ret = utimensat(dir_fd, path, timebuf, flags);
#else
    struct timeval tv[2];

    tv[0].tv_sec  = statp->st_atime;
    tv[0].tv_usec = 0;
    tv[1].tv_sec = statp->st_mtime;
    tv[1].tv_usec = 0;

    ret = futimesat(dir_fd, path, tv);
#endif
    if (ret == -1) {
        return errno;
    }

    return EOK;
}

static int sss_futime_set(int fd, const struct stat *statp)
{
    int ret;

#ifdef HAVE_FUTIMENS
    struct timespec timebuf[2];

    timebuf[0] = statp->st_atim;
    timebuf[1] = statp->st_mtim;
    ret = futimens(fd, timebuf);
#else
    struct timeval tv[2];

    tv[0].tv_sec  = statp->st_atime;
    tv[0].tv_usec = 0;
    tv[1].tv_sec = statp->st_mtime;
    tv[1].tv_usec = 0;

    ret = futimes(fd, tv);
#endif
    if (ret == -1) {
        return errno;
    }

    return EOK;
}

/* wrapper in order not to create a temporary context in
 * every iteration */
static int remove_tree_with_ctx(TALLOC_CTX *mem_ctx,
                                int parent_fd,
                                const char *dir_name,
                                dev_t parent_dev);

int remove_tree(const char *root)
{
    TALLOC_CTX *tmp_ctx = NULL;
    int ret;

    tmp_ctx = talloc_new(NULL);
    if (!tmp_ctx) {
        return ENOMEM;
    }

    ret = remove_tree_with_ctx(tmp_ctx, AT_FDCWD, root, 0);
    talloc_free(tmp_ctx);
    return ret;
}

/*
 * The context is not freed in case of error
 * because this is a recursive function, will be freed when we
 * reach the top level remove_tree() again
 */
static int remove_tree_with_ctx(TALLOC_CTX *mem_ctx,
                                int parent_fd,
                                const char *dir_name,
                                dev_t parent_dev)
{
    struct dirent *result;
    struct stat statres;
    DIR *rootdir = NULL;
    int ret, err;
    int dir_fd;

    dir_fd = openat_cloexec(parent_fd, dir_name,
                            O_RDONLY | O_DIRECTORY | O_NOFOLLOW, &ret);
    if (dir_fd == -1) {
        ret = errno;
        DEBUG(SSSDBG_CRIT_FAILURE, ("Cannot open %s: [%d]: %s\n",
              dir_name, ret, strerror(ret)));
        return ret;
    }

    rootdir = fdopendir(dir_fd);
    if (rootdir == NULL) {
        ret = errno;
        DEBUG(SSSDBG_CRIT_FAILURE,
              ("Cannot open directory: [%d][%s]\n", ret, strerror(ret)));
        close(dir_fd);
        goto fail;
    }

    while ((result = readdir(rootdir)) != NULL) {
        if (strcmp(result->d_name, ".") == 0 ||
            strcmp(result->d_name, "..") == 0) {
            continue;
        }

        ret = fstatat(dir_fd, result->d_name,
                      &statres, AT_SYMLINK_NOFOLLOW);
        if (ret != 0) {
            ret = errno;
            DEBUG(SSSDBG_CRIT_FAILURE,
                  ("stat failed: [%d][%s]\n", ret, strerror(ret)));
            goto fail;
        }

        if (S_ISDIR(statres.st_mode)) {
            /* if directory, recursively descend, but check if on the same FS */
            if (parent_dev && parent_dev != statres.st_dev) {
                DEBUG(SSSDBG_CRIT_FAILURE,
                      ("Directory %s is on different filesystem, "
                       "will not follow\n"));
                ret = EFAULT;
                goto fail;
            }

            ret = remove_tree_with_ctx(mem_ctx, dir_fd, result->d_name, statres.st_dev);
            if (ret != EOK) {
                DEBUG(SSSDBG_CRIT_FAILURE,
                      ("Removing subdirectory failed: [%d][%s]\n",
                       ret, strerror(ret)));
                goto fail;
            }
        } else {
            ret = unlinkat(dir_fd, result->d_name, 0);
            if (ret != 0) {
                ret = errno;
                DEBUG(SSSDBG_CRIT_FAILURE,
                        ("Removing file failed: [%d][%s]\n", ret, strerror(ret)));
                goto fail;
            }
        }
    }

    ret = closedir(rootdir);
    rootdir = NULL;
    if (ret != 0) {
        ret = errno;
        goto fail;
    }

    ret = unlinkat(parent_fd, dir_name, AT_REMOVEDIR);
    if (ret == -1) {
        ret = errno;
    }

    ret = EOK;
fail:
    if (rootdir) {  /* clean up on abnormal exit but retain return code */
        err = closedir(rootdir);
        if (err) {
            DEBUG(SSSDBG_CRIT_FAILURE, ("closedir failed, bad dirp?\n"));
        }
    }
    return ret;
}

static char *talloc_readlinkat(TALLOC_CTX *mem_ctx, int dir_fd,
                               const char *filename)
{
    size_t size = 1024;
    ssize_t nchars;
    char *buffer;
    char *new_buffer;

    buffer = talloc_array(mem_ctx, char, size);
    if (!buffer) {
        return NULL;
    }

    while (1) {
        nchars = readlinkat(dir_fd, filename, buffer, size);
        if (nchars < 0) {
            talloc_free(buffer);
            return NULL;
        }

        if ((size_t) nchars < size) {
            /* The buffer was large enough */
            break;
        }

        /* Try again with a bigger buffer */
        size *= 2;
        new_buffer = talloc_realloc(mem_ctx, buffer, char, size);
        if (!new_buffer) {
            talloc_free(buffer);
            return NULL;
        }
        buffer = new_buffer;
    }

    /* readlink does not nul-terminate */
    buffer[nchars] = '\0';
    return buffer;
}

static int
copy_symlink(int src_dir_fd,
             int dst_dir_fd,
             const char *file_name,
             const char *full_path,
             const struct stat *statp,
             uid_t uid, gid_t gid)
{
    char *buf;
    errno_t ret;

    buf = talloc_readlinkat(NULL, src_dir_fd, file_name);
    if (!buf) {
        return ENOMEM;
    }

    ret = selinux_file_context(full_path);
    if (ret != 0) {
        DEBUG(SSSDBG_MINOR_FAILURE,
              ("Failed to set SELinux context for [%s]\n", full_path));
        /* Not fatal */
    }

    ret = symlinkat(buf, dst_dir_fd, file_name);
    talloc_free(buf);
    if (ret == -1) {
        ret = errno;
        if (ret == EEXIST) {
            DEBUG(SSSDBG_MINOR_FAILURE,
                  ("symlink pointing to already exists at '%s'\n", full_path));
            return EOK;
        }

        DEBUG(SSSDBG_CRIT_FAILURE, ("symlinkat failed: %s\n", strerror(ret)));
        return ret;
    }

    ret = fchownat(dst_dir_fd, file_name,
                   uid, gid, AT_SYMLINK_NOFOLLOW);
    if (ret == -1) {
        ret = errno;
        DEBUG(SSSDBG_CRIT_FAILURE,
             ("fchownat failed: %s\n", strerror(ret)));
        return ret;
    }

    ret = sss_timeat_set(dst_dir_fd, file_name, statp,
                         AT_SYMLINK_NOFOLLOW);
    if (ret != EOK) {
        DEBUG(SSSDBG_MINOR_FAILURE, ("utimensat failed [%d]: %s\n",
              ret, strerror(ret)));
        /* Do not fail */
    }

    return EOK;
}

/* Create a special file named file_name under a directory with file
 * descriptor dst_dir_fd. full_path is used for both setting SELinux
 * context and logging. The node is owned by uid/gid and its mode
 * and device number is read from statp.
 */
static int copy_special(int dst_dir_fd,
                        const char *file_name,
                        const char *full_path,
                        const struct stat *statp,
                        uid_t uid, gid_t gid)
{
    int ret;

    ret = selinux_file_context(full_path);
    if (ret != 0) {
        DEBUG(SSSDBG_MINOR_FAILURE,
              ("Failed to set SELinux context for [%s]\n", full_path));
        /* Not fatal */
    }

    ret = mknodat(dst_dir_fd, file_name, statp->st_mode & ~07777,
                  statp->st_rdev);
    if (ret != 0) {
        ret = errno;
        DEBUG(SSSDBG_OP_FAILURE,
              ("Cannot mknod special file '%s': [%d][%s].\n",
              full_path, ret, strerror(ret)));
        return ret;
    }

    ret = fchownat(dst_dir_fd, file_name, uid, gid, 0);
    if (ret != 0) {
        ret = errno;
        DEBUG(SSSDBG_CRIT_FAILURE,
              ("fchownat failed for '%s': [%d][%s]\n",
              full_path, ret, strerror(ret)));
        return ret;
    }

    ret = fchmodat(dst_dir_fd, file_name, statp->st_mode & 07777, 0);
    if (ret != 0) {
        ret = errno;
        DEBUG(SSSDBG_CRIT_FAILURE,
              ("fchmodat failed for '%s': [%d][%s]\n",
              full_path, ret, strerror(ret)));
        return ret;
    }

    ret = sss_timeat_set(dst_dir_fd, file_name, statp, 0);
    if (ret == -1) {
        ret = errno;
        DEBUG(SSSDBG_MINOR_FAILURE,
              ("utimensat failed for '%s': [%d][%s]\n",
              full_path, ret, strerror(ret)));
        /* Do not fail, this shouldn't be fatal */
    }

    return EOK;
}

/* Copy bytes from input file descriptor ifd into file named
 * dst_named under directory with dest_dir_fd. Own the new file
 * by uid/gid
 */
static int
copy_file(int ifd,
          int dest_dir_fd,
          const char *file_name,
          const char *full_path,
          const struct stat *statp,
          uid_t uid, gid_t gid)
{
    int ofd = -1;
    errno_t ret;
    char buf[1024];
    ssize_t cnt, written;

    ret = selinux_file_context(full_path);
    if (ret != 0) {
        DEBUG(SSSDBG_MINOR_FAILURE,
              ("Failed to set SELinux context for [%s]\n", full_path));
        /* Not fatal */
    }

    /* Start with absolutely restrictive permissions */
    ofd = openat(dest_dir_fd, file_name,
                 O_EXCL | O_CREAT | O_WRONLY | O_NOFOLLOW,
                 0);
    if (ofd < 0 && errno != EEXIST) {
        ret = errno;
        DEBUG(SSSDBG_OP_FAILURE,
               ("Cannot open() destination file '%s': [%d][%s].\n",
               full_path, ret, strerror(ret)));
        goto done;
    }

    while ((cnt = sss_atomic_read_s(ifd, buf, sizeof(buf))) != 0) {
        if (cnt == -1) {
            ret = errno;
            DEBUG(SSSDBG_CRIT_FAILURE,
                  ("Cannot read() from source file: [%d][%s].\n",
                   ret, strerror(ret)));
            goto done;
        }

        errno = 0;
        written = sss_atomic_write_s(ofd, buf, cnt);
        if (written == -1) {
            ret = errno;
            DEBUG(SSSDBG_CRIT_FAILURE,
                  ("Cannot write() to destination file: [%d][%s].\n",
                   ret, strerror(ret)));
            goto done;
        }

        if (written != cnt) {
            DEBUG(SSSDBG_CRIT_FAILURE,
                  ("Wrote %d bytes, expected %d\n", written, cnt));
            goto done;
        }
    }

    /* Set the ownership; permissions are still
     * restrictive. */
    ret = fchown(ofd, uid, gid);
    if (ret == -1 && errno != EPERM) {
        ret = errno;
        DEBUG(SSSDBG_OP_FAILURE,
              ("Error changing owner of '%s': %s\n",
              full_path, strerror(ret)));
        goto done;
    }

    /* Set the desired mode. */
    ret = fchmod(ofd, statp->st_mode);
    if (ret == -1) {
        ret = errno;
        DEBUG(SSSDBG_OP_FAILURE, ("Error changing owner of '%s': %s\n",
              full_path, strerror(ret)));
              goto done;
    }

    ret = sss_futime_set(ofd, statp);
    if (ret != EOK) {
        DEBUG(SSSDBG_MINOR_FAILURE, ("sss_futime_set failed [%d]: %s\n",
              ret, strerror(ret)));
        /* Do not fail */
    }

    close(ofd);
    ofd = -1;
    ret = EOK;

done:
    if (ofd != -1) close(ofd);
    return ret;
}

static errno_t
copy_dir(struct copy_ctx *cctx,
         int src_dir_fd, const char *src_dir_path,
         int dest_parent_fd, const char *dest_dir_name,
         const char *dest_dir_path,
         mode_t mode,
         const struct stat *src_dir_stat);

static errno_t
copy_entry(struct copy_ctx *cctx,
           int src_dir_fd,
           const char *src_dir_path,
           int dest_dir_fd,
           const char *dest_dir_path,
           const char *ent_name)
{
    char *src_ent_path = NULL;
    char *dest_ent_path = NULL;
    int ifd = -1;
    errno_t ret;
    struct stat st;

    /* Build the path of the source file or directory and its
     * corresponding member in the new tree. */
    src_ent_path = talloc_asprintf(cctx, "%s/%s", src_dir_path, ent_name);
    dest_ent_path = talloc_asprintf(cctx, "%s/%s", dest_dir_path, ent_name);
    if (!src_ent_path || !dest_ent_path) {
        ret = ENOMEM;
        goto done;
    }

    /* Open the input entry first, then we can fstat() it and be
     * certain that it is still the same file.  O_NONBLOCK protects
     * us against FIFOs and perhaps side-effects of the open() of a
     * device file if there ever was one here, and doesn't matter
     * for regular files or directories. */
    ifd = openat_cloexec(src_dir_fd, ent_name,
                         O_RDONLY | O_NOFOLLOW | O_NONBLOCK, &ret);
    if (ifd == -1 && ret != ELOOP) {
        /* openat error */
        DEBUG(SSSDBG_CRIT_FAILURE, ("openat failed on '%s': %s\n",
              src_ent_path, strerror(ret)));
        goto done;
    } else if (ifd == -1 && ret == ELOOP) {
        /* Should be a symlink.. */
        ret = fstatat(src_dir_fd, ent_name, &st, AT_SYMLINK_NOFOLLOW);
        if (ret == -1) {
            ret = errno;
            DEBUG(SSSDBG_CRIT_FAILURE, ("fstatat failed on '%s': %s\n",
                  src_ent_path, strerror(ret)));
            goto done;
        }

        /* Handle symlinks */
        ret = copy_symlink(src_dir_fd, dest_dir_fd, ent_name,
                           dest_ent_path, &st, cctx->uid, cctx->gid);
        if (ret != EOK) {
            DEBUG(SSSDBG_OP_FAILURE, ("Cannot copy '%s' to '%s'\n",
                  src_ent_path, dest_ent_path));
        }
        goto done;
    }

    ret = fstat(ifd, &st);
    if (ret != 0) {
        ret = errno;
        DEBUG(SSSDBG_CRIT_FAILURE,
                ("couldn't stat '%s': %s", src_ent_path, strerror(ret)));
        goto done;
    }

    if (S_ISDIR(st.st_mode)) {
        /* If it's a directory, descend into it. */
        ret = copy_dir(cctx, ifd, src_ent_path,
                       dest_dir_fd, ent_name,
                       dest_ent_path, st.st_mode & 07777,
                       &st);
        if (ret != EOK) {
            DEBUG(SSSDBG_OP_FAILURE,
                    ("Could recursively copy '%s' to '%s': %s\n",
                    src_ent_path, dest_dir_fd, strerror(ret)));
            goto done;
        }
    } else if (S_ISREG(st.st_mode)) {
        /* Copy a regular file */
        ret = copy_file(ifd, dest_dir_fd, ent_name, dest_ent_path,
                        &st, cctx->uid, cctx->gid);
        if (ret) {
            DEBUG(SSSDBG_OP_FAILURE, ("Cannot copy '%s' to '%s'\n",
                    src_ent_path, dest_ent_path));
            goto done;
        }
    } else {
        /* Copy a special file */
        ret = copy_special(dest_dir_fd, ent_name, dest_ent_path,
                           &st, cctx->uid, cctx->gid);
        if (ret) {
            DEBUG(SSSDBG_OP_FAILURE, ("Cannot copy '%s' to '%s'\n",
                  src_ent_path, dest_ent_path));
            goto done;
        }
    }

    ret = EOK;
done:
    talloc_free(src_ent_path);
    talloc_free(dest_ent_path);
    if (ifd != -1) close(ifd);
    return ret;
}

static errno_t
copy_dir(struct copy_ctx *cctx,
         int src_dir_fd, const char *src_dir_path,
         int dest_parent_fd, const char *dest_dir_name,
         const char *dest_dir_path,
         mode_t mode,
         const struct stat *src_dir_stat)
{
    errno_t ret;
    int dest_dir_fd = -1;
    DIR *dir = NULL;
    struct dirent *ent;

    if (!dest_dir_path) {
        return EINVAL;
    }

    dir = fdopendir(src_dir_fd);
    if (dir == NULL) {
        ret = errno;
        DEBUG(SSSDBG_CRIT_FAILURE,
              ("Error reading '%s': %s", src_dir_path, strerror(ret)));
        goto done;
    }

    /* Create the directory.  It starts owned by us (presumbaly root), with
     * fairly restrictive permissions that still allow us to use the
     * directory.
     * */
    errno = 0;
    ret = mkdirat(dest_parent_fd, dest_dir_name, S_IRWXU);
    if (ret == -1 && errno != EEXIST) {
        ret = errno;
        DEBUG(SSSDBG_CRIT_FAILURE,
              ("Error reading '%s': %s", dest_dir_path, strerror(ret)));
        goto done;
    }

    dest_dir_fd = openat_cloexec(dest_parent_fd, dest_dir_name,
                                 O_RDONLY | O_DIRECTORY | O_NOFOLLOW, &ret);
    if (dest_dir_fd == -1) {
        ret = errno;
        DEBUG(SSSDBG_CRIT_FAILURE,
              ("Error opening '%s': %s", dest_dir_path, strerror(ret)));
        goto done;
    }

    while ((ent = readdir(dir)) != NULL) {
        /* Iterate through each item in the directory. */
        /* Skip over self and parent hard links. */
        if (strcmp(ent->d_name, ".") == 0 ||
            strcmp(ent->d_name, "..") == 0) {
            continue;
        }

        ret = copy_entry(cctx,
                         src_dir_fd, src_dir_path,
                         dest_dir_fd, dest_dir_path,
                         ent->d_name);
        if (ret != EOK) {
            DEBUG(SSSDBG_OP_FAILURE, ("Could not copy [%s] to [%s]\n",
                  src_dir_path, dest_dir_path));
            goto done;
        }
    }

    /* Set the ownership on the directory.  Permissions are still
     * fairly restrictive. */
    ret = fchown(dest_dir_fd, cctx->uid, cctx->gid);
    if (ret == -1 && errno != EPERM) {
        ret = errno;
        DEBUG(SSSDBG_OP_FAILURE,
              ("Error changing owner of '%s': %s",
              dest_dir_path, strerror(ret)));
        goto done;
    }

    /* Set the desired mode. Do this explicitly to preserve S_ISGID and
     * other bits. Do this after chown, because chown is permitted to
     * reset these bits. */
    ret = fchmod(dest_dir_fd, mode);
    if (ret == -1) {
        DEBUG(SSSDBG_OP_FAILURE,
              ("Error setting mode of '%s': %s",
              dest_dir_path, strerror(ret)));
        goto done;
    }

    sss_futime_set(dest_dir_fd, src_dir_stat);
    if (ret != EOK) {
        DEBUG(SSSDBG_MINOR_FAILURE, ("sss_futime_set failed [%d]: %s\n",
              ret, strerror(ret)));
        /* Do not fail */
    }

    ret = EOK;
done:
    if (dir) closedir(dir);

    if (dest_dir_fd != -1) {
        close(dest_dir_fd);
    }
    return ret;
}

/* NOTE:
 * For several reasons, including the fact that we copy even special files
 * (pipes, etc) from the skeleton directory, the skeldir needs to be trusted
 */
int copy_tree(const char *src_root, const char *dst_root,
              mode_t mode_root, uid_t uid, gid_t gid)
{
    int ret = EOK;
    struct copy_ctx *cctx = NULL;
    int fd = -1;
    struct stat s_src;

    fd = open_cloexec(src_root, O_RDONLY | O_DIRECTORY, &ret);
    if (fd == -1) {
        goto fail;
    }

    ret = fstat(fd, &s_src);
    if (ret == -1) {
        ret = errno;
        goto fail;
    }

    cctx = talloc_zero(NULL, struct copy_ctx);
    if (!cctx) {
        ret = ENOMEM;
        goto fail;
    }

    cctx->src_orig = src_root;
    cctx->dst_orig = dst_root;
    cctx->src_dev  = s_src.st_dev;
    cctx->uid      = uid;
    cctx->gid      = gid;

    ret = copy_dir(cctx, fd, src_root, AT_FDCWD,
                   dst_root, dst_root, mode_root, &s_src);
    if (ret != EOK) {
        DEBUG(SSSDBG_CRIT_FAILURE,
              ("copy_dir failed: [%d][%s]\n", ret, strerror(ret)));
        goto fail;
    }

fail:
    if (fd != -1) close(fd);
    reset_selinux_file_context();
    talloc_free(cctx);
    return ret;
}