int setup_dir_permissions()

in storm-core/src/native/worker-launcher/impl/worker-launcher.c [534:621]


int setup_dir_permissions(const char* local_dir, int user_writable, boolean setgid_on_dir) {
  //This is the same as
  //> chmod g+rwX -R $local_dir
  //> chmod g+s -R $local_dir
  //> if [ $user_writable ]; then chmod u+w;  else u-w; fi
  //> chown -no-dereference -R $user:$supervisor-group $local_dir
  int exit_code = 0;
  uid_t euser = geteuid();

  if (local_dir == NULL) {
    fprintf(ERRORFILE, "ERROR: Path is null in setup_dir_permissions\n");
    exit_code = UNABLE_TO_BUILD_PATH; // may be malloc failed
  } else {
    char *(paths[]) = {strndup(local_dir, PATH_MAX), 0};
    if (paths[0] == NULL) {
      fprintf(ERRORFILE, "ERROR: Malloc failed in setup_dir_permissions\n");
      return -1;
    }
    // check to make sure the directory exists
    if (access(local_dir, F_OK) != 0) {
      if (errno == ENOENT) {
        fprintf(ERRORFILE, "ERROR: Path does not exist %s in setup_dir_permissions\n", local_dir);
        free(paths[0]);
        paths[0] = NULL;
        return UNABLE_TO_BUILD_PATH;
      }
    }
    FTS* tree = fts_open(paths, FTS_PHYSICAL | FTS_XDEV, NULL);
    FTSENT* entry = NULL;
    int ret = 0;

    if (tree == NULL) {
      fprintf(ERRORFILE,
              "ERROR: Cannot open file traversal structure for the path %s:%s in setup_dir_permissions\n",
              local_dir, strerror(errno));
      free(paths[0]);
      paths[0] = NULL;
      return -1;
    }

    if (seteuid(0) != 0) {
      fprintf(ERRORFILE, "ERROR: Could not become root in setup_dir_permissions\n");
      return -1;
    }

    while (((entry = fts_read(tree)) != NULL) && exit_code == 0) {
      switch (entry->fts_info) {

        case FTS_DP:        // A directory being visited in post-order
        case FTS_DOT:       // A dot directory
        case FTS_SL:        // A symbolic link
        case FTS_SLNONE:    // A broken symbolic link
          //NOOP
          fprintf(LOGFILE, "NOOP: %s\n", entry->fts_path);
          break;
        case FTS_D:         // A directory in pre-order
        case FTS_F:         // A regular file
          if (setup_permissions(entry, euser, user_writable, setgid_on_dir) != 0) {
            exit_code = -1;
          }
          break;
        case FTS_DEFAULT:   // Unknown type of file
        case FTS_DNR:       // Unreadable directory
        case FTS_NS:        // A file with no stat(2) information
        case FTS_DC:        // A directory that causes a cycle
        case FTS_NSOK:      // No stat information requested
        case FTS_ERR:       // Error return
        default:
          fprintf(ERRORFILE, "ERROR: Unexpected...\n");
          exit_code = -1;
          break;
      }
    }
    ret = fts_close(tree);
    if (exit_code == 0 && ret != 0) {
      fprintf(ERRORFILE, "ERROR: Error in fts_close while setting up %s\n", local_dir);
      exit_code = -1;
    }
    free(paths[0]);
    paths[0] = NULL;

    if (seteuid(euser) != 0) {
      fprintf(ERRORFILE, "ERROR: Could not switch euid back to %d\n", euser);
      return -1;
    }
  }
  return exit_code;
}