(chdir_no_follow): Move declaration of local,
[platform/upstream/coreutils.git] / lib / chdir-safer.c
1 /* much like chdir(2), but safer
2
3    Copyright (C) 2005 Free Software Foundation, Inc.
4
5    This program is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; either version 2, or (at your option)
8    any later version.
9
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14
15    You should have received a copy of the GNU General Public License
16    along with this program; if not, write to the Free Software Foundation,
17    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
18
19 /* written by Jim Meyering */
20
21 #ifdef HAVE_CONFIG_H
22 # include <config.h>
23 #endif
24
25 #include "chdir-safer.h"
26
27 #include <stdbool.h>
28 #include <fcntl.h>
29 #include <errno.h>
30 #include <unistd.h>
31 #include <sys/types.h>
32 #include <sys/stat.h>
33
34 #ifndef O_DIRECTORY
35 # define O_DIRECTORY 0
36 #endif
37
38 #ifndef O_NOFOLLOW
39 # define O_NOFOLLOW 0
40 #endif
41
42 #define SAME_INODE(Stat_buf_1, Stat_buf_2) \
43   ((Stat_buf_1).st_ino == (Stat_buf_2).st_ino \
44    && (Stat_buf_1).st_dev == (Stat_buf_2).st_dev)
45
46 /* Like chdir, but fail if DIR is a symbolic link to a directory (or
47    similar funny business), or if DIR is neither readable nor
48    writable.  This avoids a minor race condition between when a
49    directory is created or statted and when the process chdirs into it.
50
51    On some systems, a writable yet unreadable directory cannot be
52    opened via open with O_WRONLY.  For example, on Linux 2.6, the
53    open syscall fails with EISDIR.  */
54 int
55 chdir_no_follow (char const *dir)
56 {
57   int result = 0;
58   int saved_errno;
59   int open_flags = O_DIRECTORY | O_NOCTTY | O_NOFOLLOW | O_NONBLOCK;
60   int fd = open (dir, O_RDONLY | open_flags);
61   if (fd < 0)
62     {
63       if (errno == EACCES)
64         fd = open (dir, O_WRONLY | open_flags);
65       if (fd < 0)
66         return fd;
67     }
68
69   /* If open follows symlinks, lstat DIR and fstat FD to ensure that
70      they are the same file; if they are different files, set errno to
71      ELOOP (the same value that open uses for symlinks with
72      O_NOFOLLOW) so the caller can report a failure.  */
73   if (! O_NOFOLLOW)
74     {
75       struct stat sb1;
76       result = lstat (dir, &sb1);
77       if (result == 0)
78         {
79           struct stat sb2;
80           result = fstat (fd, &sb2);
81           if (result == 0 && ! SAME_INODE (sb1, sb2))
82             {
83               errno = ELOOP;
84               result = -1;
85             }
86         }
87     }
88
89   if (result == 0)
90     result = fchdir (fd);
91
92   saved_errno = errno;
93   close (fd);
94   errno = saved_errno;
95   return result;
96 }