Ejemplo n.º 1
0
int lua_apr_file_attrs_set(lua_State *L)
{
  apr_fileattrs_t attributes, valid;
  const char *path, *key;
  apr_status_t status;

  path = luaL_checkstring(L, 1);
  luaL_checktype(L, 2, LUA_TTABLE);

  attributes = valid = 0;
  lua_pushnil(L);
  while (lua_next(L, 2)) {
    key = lua_tostring(L, -2);
    if (strcmp(key, "readonly") == 0) {
      valid |= APR_FILE_ATTR_READONLY;
      if (lua_toboolean(L, -1))
        attributes |= APR_FILE_ATTR_READONLY;
    } else if (strcmp(key, "hidden") == 0) {
      valid |= APR_FILE_ATTR_HIDDEN;
      if (lua_toboolean(L, -1))
        attributes |= APR_FILE_ATTR_HIDDEN;
    } else if (strcmp(key, "executable") == 0) {
      valid |= APR_FILE_ATTR_EXECUTABLE;
      if (lua_toboolean(L, -1))
        attributes |= APR_FILE_ATTR_EXECUTABLE;
    } else {
      luaL_argerror(L, 2, lua_pushfstring(L, "invalid key " LUA_QS, key));
    }
    lua_pop(L, 1);
  }
  status = apr_file_attrs_set(path, attributes, valid, to_pool(L));

  return push_status(L, status);
}
Ejemplo n.º 2
0
int lua_apr_stat(lua_State *L)
{
  const char *path, *name, *dir;
  apr_pool_t *memory_pool;
  lua_apr_stat_context context = { 0 };
  apr_status_t status;

  memory_pool = to_pool(L);
  path = luaL_checkstring(L, 1);
  name = apr_filepath_name_get(path);
  dir = apr_pstrndup(memory_pool, path, name - path);
  context.firstarg = 2;
  context.lastarg = lua_gettop(L);
  check_stat_request(L, &context);
  status = apr_stat(&context.info, path, context.wanted, memory_pool);
  if (status != APR_SUCCESS && !APR_STATUS_IS_INCOMPLETE(status))
    return push_error_status(L, status);

  /* XXX apr_stat() doesn't fill in finfo.name (tested on Linux) */
  if (!(context.info.valid & APR_FINFO_NAME)) {
    context.info.valid |= APR_FINFO_NAME;
    context.info.name = name;
  }

  return push_stat_results(L, &context, dir);
}
Ejemplo n.º 3
0
int lua_apr_host_to_addr(lua_State *L)
{
  apr_sockaddr_t *address;
  apr_pool_t *pool;
  const char *host;
  char *ip_address;
  apr_status_t status;
  int family;

  pool = to_pool(L);
  host = luaL_checkstring(L, 1);
  family = family_check(L, 2);

  status = apr_sockaddr_info_get(&address, host, family, SOCK_STREAM, 0, pool);
  if (status != APR_SUCCESS)
    return push_error_status(L, status);

  lua_settop(L, 0);

  do {
    status = apr_sockaddr_ip_get(&ip_address, address);
    if (status != APR_SUCCESS)
      return push_error_status(L, status);
    lua_pushstring(L, ip_address);
    address = address->next;
  } while (address != NULL);

  return lua_gettop(L);
}
Ejemplo n.º 4
0
int lua_apr_ldap(lua_State *L)
{
  lua_apr_ldap_object *object;
  apr_ldap_err_t *error = NULL;
  apr_pool_t *memory_pool;
  apr_status_t status;
  int portno, secure = APR_LDAP_NONE;
  const char *url, *hostname;
  apr_uri_t info;

  lua_settop(L, 2);
  memory_pool = to_pool(L);
  url = luaL_optstring(L, 1, "ldap://127.0.0.1");
  if (lua_toboolean(L, 2))
    secure = APR_LDAP_STARTTLS;

  /* Get and parse the LDAP URL. */
  status = apr_uri_parse(memory_pool, url, &info);
  if (status != APR_SUCCESS)
    return push_error_status(L, status);

  /* Get the host name and port number of the LDAP server. */
  hostname = (info.hostname != NULL) ? info.hostname : "127.0.0.1";
  portno = (info.port_str != NULL) ? info.port : APR_URI_LDAP_DEFAULT_PORT;

  /* Use a secure connection? */
  if (info.scheme != NULL && strcmp(info.scheme, "ldaps") == 0)
    secure = APR_LDAP_SSL;

  /* Create the userdata object and memory pool. */
  object = new_object(L, &lua_apr_ldap_type);
  status = apr_pool_create(&object->pool, NULL);
  if (status != APR_SUCCESS)
    return push_error_status(L, status);

  /* Automatically call apr_ldap_ssl_init() as needed because this
   * stuff is so low level it doesn't make sense to expose it to Lua. */
  if (secure != APR_LDAP_NONE && !ldap_ssl_inited) {
    if (ldap_pool == NULL) {
      /* Create a private memory pool for SSL and rebind support. */
      status = apr_pool_create(&ldap_pool, NULL);
      if (status != APR_SUCCESS)
        return push_error_status(L, status);
    }
    status = apr_ldap_ssl_init(ldap_pool, NULL, 0, &error);
    if (status != APR_SUCCESS)
      return push_error_status(L, status);
    ldap_ssl_inited = 1;
  }

  /* Open the LDAP connection. */
  status = apr_ldap_init(object->pool, &object->ldap, hostname, portno, secure, &error);
  if (status != APR_SUCCESS)
    return push_ldap_error(L, status, error);

  return 1;
}
Ejemplo n.º 5
0
int lua_apr_file_remove(lua_State *L)
{
  apr_status_t status;
  const char *path;

  path = luaL_checkstring(L, 1);
  status = apr_file_remove(path, to_pool(L));

  return push_status(L, status);
}
Ejemplo n.º 6
0
int lua_apr_shm_remove(lua_State *L)
{
  apr_status_t status;
  const char *filename;

  filename = luaL_checkstring(L, 1);
  status = apr_shm_remove(filename, to_pool(L));

  return push_status(L, status);
}
Ejemplo n.º 7
0
int lua_apr_file_rename(lua_State *L)
{
  const char *source, *target;
  apr_status_t status;

  source = luaL_checkstring(L, 1);
  target = luaL_checkstring(L, 2);
  status = apr_file_rename(source, target, to_pool(L));

  return push_status(L, status);
}
Ejemplo n.º 8
0
int lua_apr_file_mtime_set(lua_State *L)
{
  apr_status_t status;
  const char *path;
  apr_time_t mtime;

  path = luaL_checkstring(L, 1);
  mtime = time_check(L, 2);
  status = apr_file_mtime_set(path, mtime, to_pool(L));

  return push_status(L, status);
}
Ejemplo n.º 9
0
int lua_apr_dir_remove(lua_State *L)
{
  apr_status_t status;
  apr_pool_t *memory_pool;
  const char *filepath;

  memory_pool = to_pool(L);
  filepath = luaL_checkstring(L, 1);
  status = apr_dir_remove(filepath, memory_pool);

  return push_status(L, status);
}
Ejemplo n.º 10
0
int lua_apr_env_delete(lua_State *L)
{
  apr_pool_t *memory_pool;
  apr_status_t status;
  const char *name;

  memory_pool = to_pool(L);
  name = luaL_checkstring(L, 1);
  status = apr_env_delete(name, memory_pool);

  return push_status(L, status);
}
Ejemplo n.º 11
0
int lua_apr_env_set(lua_State *L)
{
  const char *name, *value;
  apr_pool_t *memory_pool;
  apr_status_t status;

  memory_pool = to_pool(L);
  name = luaL_checkstring(L, 1);
  value = luaL_checkstring(L, 2);
  status  = apr_env_set(name, value, memory_pool);

  return push_status(L, status);
}
Ejemplo n.º 12
0
int lua_apr_file_append(lua_State *L)
{
  const char *source, *target;
  apr_fileperms_t permissions;
  apr_status_t status;

  source = luaL_checkstring(L, 1);
  target = luaL_checkstring(L, 2);
  permissions = check_permissions(L, 3, 1);
  status = apr_file_append(source, target, permissions, to_pool(L));

  return push_status(L, status);
}
Ejemplo n.º 13
0
int lua_apr_hostname_get(lua_State *L)
{
  char hostname[APRMAXHOSTLEN + 1];
  apr_status_t status;
  apr_pool_t *pool;

  pool = to_pool(L);
  status = apr_gethostname(hostname, count(hostname), pool);
  if (status != APR_SUCCESS)
    return push_error_status(L, status);
  lua_pushstring(L, hostname);

  return 1;
}
Ejemplo n.º 14
0
int lua_apr_dir_make_recursive(lua_State *L)
{
  apr_status_t status;
  apr_pool_t *memory_pool;
  const char *filepath;
  apr_fileperms_t permissions;

  memory_pool = to_pool(L);
  filepath = luaL_checkstring(L, 1);
  permissions = check_permissions(L, 2, 0);
  status = apr_dir_make_recursive(filepath, permissions, memory_pool);

  return push_status(L, status);
}
Ejemplo n.º 15
0
int lua_apr_ldap_info(lua_State *L)
{
  apr_pool_t *memory_pool;
  apr_ldap_err_t *result;
  int status;

  memory_pool = to_pool(L);
  status = apr_ldap_info(memory_pool, &result);
  if (status != APR_SUCCESS)
    return push_error_status(L, status);

  lua_pushstring(L, result->reason);
  return 1;
}
Ejemplo n.º 16
0
int lua_apr_temp_dir_get(lua_State *L)
{
  apr_pool_t* memory_pool;
  const char *filepath;
  apr_status_t status;

  memory_pool = to_pool(L);
  status = apr_temp_dir_get(&filepath, memory_pool);
  if (status != APR_SUCCESS) {
    return push_error_status(L, status);
  } else {
    lua_pushstring(L, filepath);
    return 1;
  }
}
Ejemplo n.º 17
0
int lua_apr_base64_decode(lua_State *L)
{
  size_t plain_len, coded_len;
  apr_pool_t *memory_pool;
  const char *coded;
  char *plain;

  memory_pool = to_pool(L);
  coded = luaL_checklstring(L, 1, &coded_len);
  plain_len = apr_base64_decode_len(coded);
  plain = apr_palloc(memory_pool, plain_len);
  if (plain == NULL)
    return push_error_memory(L);
  plain_len = apr_base64_decode(plain, coded);
  if (plain_len > 0 && plain[plain_len - 1] == '\0')
    plain_len--;
  lua_pushlstring(L, plain, plain_len);
  return 1;
}
Ejemplo n.º 18
0
int lua_apr_env_get(lua_State *L)
{
  apr_pool_t *memory_pool;
  apr_status_t status;
  const char *name;
  char *value;

  memory_pool = to_pool(L);
  name = luaL_checkstring(L, 1);
  status = apr_env_get(&value, name, memory_pool);
  if (APR_STATUS_IS_ENOENT(status)) {
    return 0;
  } else if (status != APR_SUCCESS) {
    return push_error_status(L, status);
  } else {
    lua_pushstring(L, value);
    return 1;
  }
}
Ejemplo n.º 19
0
int lua_apr_addr_to_host(lua_State *L)
{
  apr_sockaddr_t *address;
  apr_pool_t *pool;
  const char *ip_address;
  char *host;
  apr_status_t status;
  int family;

  pool = to_pool(L);
  ip_address = luaL_checkstring(L, 1);
  family = family_check(L, 2);
  status = apr_sockaddr_info_get(&address, ip_address, family, SOCK_STREAM, 0, pool);
  if (status == APR_SUCCESS)
    status = apr_getnameinfo(&host, address, 0);
  if (status != APR_SUCCESS)
    return push_error_status(L, status);
  lua_pushstring(L, host);

  return 1;
}
Ejemplo n.º 20
0
 void recycle(void* buf)
 {
     if (!to_pool(buf)) free(buf);
 }
Ejemplo n.º 21
0
int lua_apr_ldap_url_parse(lua_State*L)
{
  apr_ldap_url_desc_t *ludpp;
  apr_pool_t *memory_pool;
  apr_ldap_err_t *error = NULL;
  const char *url;
  int status, i;
  char *attr, *ext;

  memory_pool = to_pool(L);
  url = luaL_checkstring(L, 1);
  status = apr_ldap_url_parse_ext(memory_pool, url, &ludpp, &error);
  if (status != APR_LDAP_URL_SUCCESS) {
    push_ldap_error(L, status, error);
    lua_pop(L, 1);
    switch (status) {
      case APR_LDAP_URL_ERR_MEM:          lua_pushliteral(L, "MEM");          return 3;
      case APR_LDAP_URL_ERR_PARAM:        lua_pushliteral(L, "PARAM");        return 3;
      case APR_LDAP_URL_ERR_BADSCHEME:    lua_pushliteral(L, "BADSCHEME");    return 3;
      case APR_LDAP_URL_ERR_BADENCLOSURE: lua_pushliteral(L, "BADENCLOSURE"); return 3;
      case APR_LDAP_URL_ERR_BADURL:       lua_pushliteral(L, "BADURL");       return 3;
      case APR_LDAP_URL_ERR_BADHOST:      lua_pushliteral(L, "BADHOST");      return 3;
      case APR_LDAP_URL_ERR_BADATTRS:     lua_pushliteral(L, "BADATTRS");     return 3;
      case APR_LDAP_URL_ERR_BADSCOPE:     lua_pushliteral(L, "BADSCOPE");     return 3;
      case APR_LDAP_URL_ERR_BADFILTER:    lua_pushliteral(L, "BADFILTER");    return 3;
      case APR_LDAP_URL_ERR_BADEXTS:      lua_pushliteral(L, "BADEXTS");      return 3;
      default:                                                                return 2;
    }
  }

  lua_newtable(L);

  lua_pushstring(L, ludpp->lud_scheme);
  lua_setfield(L, -2, "scheme");

  lua_pushstring(L, ludpp->lud_host);
  lua_setfield(L, -2, "host");

  lua_pushinteger(L, ludpp->lud_port);
  lua_setfield(L, -2, "port");

  if (ludpp->lud_scope == LDAP_SCOPE_BASE)
    lua_pushliteral(L, "base");
  else if (ludpp->lud_scope == LDAP_SCOPE_ONELEVEL)
    lua_pushliteral(L, "one");
  else
    lua_pushliteral(L, "sub");
  lua_setfield(L, -2, "scope");

  lua_pushstring(L, ludpp->lud_filter);
  lua_setfield(L, -2, "filter");

  lua_pushstring(L, ludpp->lud_dn);
  lua_setfield(L, -2, "dn");

  lua_pushinteger(L, ludpp->lud_crit_exts);
  lua_setfield(L, -2, "crit_exts");

  if (ludpp->lud_attrs != NULL) {
    i = 0;
    lua_newtable(L);
    while ((attr = ludpp->lud_attrs[i++]) != NULL) {
      lua_pushinteger(L, i + 1);
      lua_pushstring(L, attr);
      lua_settable(L, -3);
    }
    lua_setfield(L, -2, "attrs");
  }

  if (ludpp->lud_exts != NULL) {
    i = 0;
    lua_newtable(L);
    while ((ext = ludpp->lud_exts[i++]) != NULL) {
      lua_pushinteger(L, i + 1);
      lua_pushstring(L, ext);
      lua_settable(L, -3);
    }
    lua_setfield(L, -2, "exts");
  }

  return 1;
}