]> gitweb.michael.orlitzky.com - apply-default-acl.git/blobdiff - src/libadacl.c
Add various NULL pointer checks for good measure.
[apply-default-acl.git] / src / libadacl.c
index 08b13b82395941b90956129499de8a3450177ace..5402a32be0ac3ca03a535aa40bf049ef1eabca3f 100644 (file)
 #include "libadacl.h"
 
 
+/* Even though most other library functions reliably return -1 for
+ * error, it feels a little wrong to re-use the ACL_ERROR constant.
+ */
+#define CLOSE_ERROR -1
+#define OPEN_ERROR -1
+#define SNPRINTF_ERROR -1
+#define STAT_ERROR -1
+
+
 /**
  * @brief The recursive portion of the @c safe_open function, used to
  *   open a file descriptor in a symlink-safe way when combined with
  *   the @c O_NOFOLLOW flag.
  *
+ * The @c O_PATH flag is not used because we want to fail upon
+ * encountering any symlinks.
+ *
  * @param at_fd
  *   A file descriptor relative to which @c pathname will be opened.
  *
  * @param pathname
  *   The path to the file/directory/whatever whose descriptor you want.
  *
+ * @param flags
+ *   File status flags to be passed to @c openat.
+ *
  * @return a file descriptor for @c pathname if everything goes well,
  *   and @c OPEN_ERROR if not.
  */
 int safe_open_ex(int at_fd, char* pathname, int flags) {
-  if (pathname != NULL && strlen(pathname) == 0) {
+  if (pathname == NULL) {
+    errno = EINVAL;
+    perror("safe_open_ex (args)");
+    return OPEN_ERROR;
+  }
+
+  if (strlen(pathname) == 0) {
     /* Oops, went one level to deep with nothing to do. */
     return at_fd;
   }
@@ -97,6 +118,9 @@ int safe_open_ex(int at_fd, char* pathname, int flags) {
  * @param pathname
  *   The path to the file/directory/whatever whose descriptor you want.
  *
+ * @param flags
+ *   File status flags to be passed to @c openat.
+ *
  * @return a file descriptor for @c pathname if everything goes well,
  *   and @c OPEN_ERROR if not.
  */
@@ -145,6 +169,11 @@ int safe_open(const char* pathname, int flags) {
   }
 
   int fd = open("/", flags);
+  if (fd == OPEN_ERROR) {
+    perror("safe_open (open)");
+    return OPEN_ERROR;
+  }
+
   if (strcmp(abspath, "/") == 0) {
     return fd;
   }
@@ -185,6 +214,11 @@ int safe_open(const char* pathname, int flags) {
  *
  */
 int acl_set_entry(acl_t* aclp, acl_entry_t entry) {
+  if (aclp == NULL || entry == NULL) {
+    errno = EINVAL;
+    perror("acl_set_entry (args)");
+    return ACL_ERROR;
+  }
 
   acl_tag_t entry_tag;
   if (acl_get_tag_type(entry, &entry_tag) == ACL_ERROR) {
@@ -333,6 +367,11 @@ int acl_entry_count(acl_t acl) {
  *   - @c ACL_ERROR - Unexpected library error
  */
 int acl_is_minimal(acl_t acl) {
+  if (acl == NULL) {
+    errno = EINVAL;
+    perror("acl_is_minimal (args)");
+    return ACL_ERROR;
+  }
 
   int ec = acl_entry_count(acl);
 
@@ -363,6 +402,11 @@ int acl_is_minimal(acl_t acl) {
  *   - @c ACL_ERROR - Unexpected library error.
  */
 int acl_execute_masked(acl_t acl) {
+  if (acl == NULL) {
+    errno = EINVAL;
+    perror("acl_execute_masked (args)");
+    return ACL_ERROR;
+  }
 
   acl_entry_t entry;
   int ge_result = acl_get_entry(acl, ACL_FIRST_ENTRY, &entry);
@@ -426,6 +470,12 @@ int acl_execute_masked(acl_t acl) {
  *   - @c ACL_ERROR - Unexpected library error.
  */
 int any_can_execute(int fd, const struct stat* sp) {
+  if (sp == NULL) {
+    errno = EINVAL;
+    perror("any_can_execute (args)");
+    return ACL_ERROR;
+  }
+
   acl_t acl = acl_get_fd(fd);
 
   if (acl == (acl_t)NULL) {
@@ -523,8 +573,7 @@ int any_can_execute(int fd, const struct stat* sp) {
  *   - @c ACL_ERROR - Unexpected library error.
  */
 int assign_default_acl(const char* path, acl_t acl) {
-
-  if (path == NULL) {
+  if (path == NULL || acl == NULL) {
     errno = EINVAL;
     perror("assign_default_acl (args)");
     return ACL_ERROR;
@@ -600,8 +649,7 @@ int wipe_acls(int fd) {
  *
  * @return
  *   - @c ACL_SUCCESS - The parent default ACL was inherited successfully.
- *   - @c ACL_FAILURE - The target path is not a regular file/directory,
- *     or the parent of @c path is not a directory.
+ *   - @c ACL_FAILURE - If symlinks or hard links are encountered.
  *   - @c ACL_ERROR - Unexpected library error.
  */
 int apply_default_acl_ex(const char* path,
@@ -851,7 +899,7 @@ int apply_default_acl_ex(const char* path,
  *
  * @return
  *   - @c ACL_SUCCESS - The parent default ACL was inherited successfully.
- *   - @c ACL_FAILURE - The target path is not a regular file/directory,
+ *   - @c ACL_FAILURE - If symlinks or hard links are encountered.
  *     or the parent of @c path is not a directory.
  *   - @c ACL_ERROR - Unexpected library error.
  */