blocxx
PosixPathSecurity.cpp
Go to the documentation of this file.
1 /*******************************************************************************
2 * Copyright (C) 2005, Vintela, Inc. All rights reserved.
3 * Copyright (C) 2006, Novell, Inc. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * * Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * * Neither the name of
14 * Vintela, Inc.,
15 * nor Novell, Inc.,
16 * nor the names of its contributors or employees may be used to
17 * endorse or promote products derived from this software without
18 * specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 *******************************************************************************/
32 
37 #include "blocxx/BLOCXX_config.h"
38 #include "blocxx/PathSecurity.hpp"
39 
40 namespace BLOCXX_NAMESPACE
41 {
42 
43  inline bool group_ok(::gid_t gid)
44  {
45 #ifdef BLOCXX_SOLARIS
46  return gid == 0 /* root */ || gid == 3 /* sys */;
47 #elif defined(BLOCXX_DARWIN)
48  return gid == 0 /* root */ || gid == 80 /* admin */;
49 #else
50  return gid == 0 /* root */;
51 #endif
52  }
53 
54  inline bool check_grp_oth(struct stat const & x)
55  {
56  ::mode_t badmsk = group_ok(x.st_gid) ? S_IWOTH : (S_IWGRP | S_IWOTH);
57  return !(x.st_mode & badmsk);
58  }
59 
60  EFileStatusReturn file_ok(struct stat const & x, ::uid_t uid, bool full_path)
61  {
62  // Note: originally this disallowed multiple hard links to a file,
63  // but that restriction is not necessary, as the permissions for a
64  // file are associated with its inode, and not with its directory
65  // entries. Note also that it's not a problem if someone does an
66  // unlink of an alternate path to the file, as this just removes
67  // the alternate directory entry -- the file itself is not actually
68  // deleted until there are no hard links at all to it.
70  if (x.st_uid == 0 ||
71 #if defined(BLOCXX_HPUX) || defined(BLOCXX_AIX)
72  // on HP-UX & AIX, many system dirs & files are owned by the bin user, which has a uid of 2.
73  x.st_uid == 2 ||
74 #endif
75  x.st_uid == uid)
76  {
77  if (!(S_ISLNK(x.st_mode) || check_grp_oth(x) || S_ISDIR(x.st_mode) && !full_path && (x.st_mode & S_ISVTX)))
78  {
79  retval = E_FILE_BAD_OTHER;
80  }
81  }
82  else
83  {
84  retval = E_FILE_BAD_OWNER;
85  }
86  return retval;
87  }
88 
89  EFileStatusReturn getFileStatus(struct stat const & x, uid_t uid, bool is_full_path, const String& path)
90  {
91  return file_ok(x, uid, is_full_path);
92  }
93 
94  bool isPathAbsolute(String const & path)
95  {
96  return path.startsWith("/") ? true : false;
97  }
98 
99 } // end namespace BLOCXX_NAMESPACE