Пример #1
0
/**
 * Parse an Alt-Svc specifier as described in "HTTP Alternative Services"
 * (https://tools.ietf.org/html/draft-ietf-httpbis-alt-svc-04)
 * with the following changes:
 * - do not percent encode token values
 * - do not use quotation marks
 */
h2_alt_svc *h2_alt_svc_parse(const char *s, apr_pool_t *pool) {
    const char *sep = ap_strchr_c(s, '=');
    if (sep) {
        const char *alpn = apr_pstrndup(pool, s, sep - s);
        const char *host = NULL;
        int port = 0;
        s = sep + 1;
        sep = ap_strchr_c(s, ':');  /* mandatory : */
        if (sep) {
            if (sep != s) {    /* optional host */
                host = apr_pstrndup(pool, s, sep - s);
            }
            s = sep + 1;
            if (*s) {          /* must be a port number */
                port = (int)apr_atoi64(s);
                if (port > 0 && port < (0x1 << 16)) {
                    h2_alt_svc *as = apr_pcalloc(pool, sizeof(*as));
                    as->alpn = alpn;
                    as->host = host;
                    as->port = port;
                    return as;
                }
            }
        }
    }
    return NULL;
}
static const char* interpolate_env(request_rec *r, const char *str) {
  /* Interpolate an env str in a configuration string
   * Syntax ${var} --> value_of(var)
   * Method: replace one var, and recurse on remainder of string
   * Nothing clever here, and crap like nested vars may do silly things
   * but we'll at least avoid sending the unwary into a loop
   */
  const char *start;
  const char *end;
  const char *var;
  const char *val;
  const char *firstpart;

  start = ap_strstr(str, "${");
  if (start == NULL) {
    return str;
  }
  end = ap_strchr(start+2, '}');
  if (end == NULL) {
    return str;
  }
  /* OK, this is syntax we want to interpolate.  Is there such a var ? */
  var = apr_pstrndup(r->pool, start+2, end-(start+2));
  val = apr_table_get(r->subprocess_env, var);
  firstpart = apr_pstrndup(r->pool, str, (start-str));

  if (val == NULL) {
    return apr_pstrcat(r->pool, firstpart, interpolate_env(r, end+1), NULL);
  } else {
    return apr_pstrcat(r->pool, firstpart, val,
	interpolate_env(r, end+1), NULL);
  }
}
Пример #3
0
static apr_status_t find_self_exe( const char **exe_name )
{
    apr_status_t rv = APR_SUCCESS;
    char buffer[1024];
#ifdef __MacOS_X__
    uint32_t length = sizeof (buffer);
    if (_NSGetExecutablePath( buffer, &length ) == 0 && buffer[0] == '/') {
        *exe_name = apr_pstrndup( _mp, buffer, length );
    }
    else {
        ERROR( "failed to find exe: %s", strerror(errno) );
        rv = APR_FROM_OS_ERROR( errno );
    }
#else
    ssize_t size = readlink( "/proc/self/exe", buffer, sizeof( buffer ) );
    if( size < 0 ) {
        ERROR( "readlink failed: %s", strerror(errno) );
        rv = APR_FROM_OS_ERROR( errno );
    }
    else {
        *exe_name = apr_pstrndup( _mp, buffer, size );
    }
#endif
    return rv;
}
Пример #4
0
static size_t rcache_curl_header_cb(const void* ptr, size_t size, size_t nmemb, void* _info)
{
    rcache_info* info = _info;
    if (strncmp(ptr, "HTTP/1.", sizeof("HTTP/1.") - 1) == 0) {
        int minor_ver, status;
        if (sscanf(ptr, "HTTP/1.%d %d ", &minor_ver, &status) == 2 && (status != 200 || status != 301 || status != 302) )
            info->r->status = status;
    } else if (strncasecmp(ptr, "content-type:", sizeof("content-type:") - 1) == 0) {
        const char* s = (const char*)ptr + sizeof("content-type:") - 1,
              * e = (const char*)ptr + size * nmemb - 1;
        for (; s <= e; --e)
            if (*e != '\r' && *e != '\n')
                break;
        for (; s <= e; ++s)
            if (*s != ' ' && *s != '\t')
                break;
        if (s <= e)
            info->type = apr_pstrndup(info->r->pool, s, e - s + 1);
    } else if (strncasecmp(ptr, "content-length:", sizeof("content-length:") - 1) == 0) {
        const char* s = (const char*)ptr + sizeof("content-length:") - 1,
              * e = (const char*)ptr + size * nmemb - 1;
        for (; s <= e; --e)
            if (*e != '\r' && *e != '\n')
                break;
        for (; s <= e; ++s)
            if (*s != ' ' && *s != '\t')
                break;
        if (s <= e)
            info->length = atoi( apr_pstrndup(info->r->pool, s, e - s + 1) );
    }
    info->mtime = apr_time_now();
    return nmemb;
}
Пример #5
0
static xmlCharEncoding sniff_encoding(saxctxt* ctx, const char* cbuf, size_t bytes)
{
  request_rec* r = ctx->f->r;
  cdn_conf* cfg = ctx->cfg;
  xmlCharEncoding ret;
  char* p;
  ap_regmatch_t match[2];
  char* buf = (char*)cbuf;
  const char *encoding = 0;

  /* If we've got it in the HTTP headers, there's nothing to do */
  if(r->content_type && (p = ap_strcasestr(r->content_type, "charset=")) && p != NULL) {
    p += 8;
    if((encoding = apr_pstrndup(r->pool, p, strcspn(p, " ;")))) {
      ap_log_error(APLOG_MARK, APLOG_DEBUG, APR_SUCCESS, r->server,
                   "sniff_encoding: found charset %s in Content-Type", encoding);
      ret = xmlParseCharEncoding(encoding);
      if(((ret != XML_CHAR_ENCODING_ERROR) && (ret != XML_CHAR_ENCODING_NONE)))
        return ret;
    }
  }

  /* to sniff, first we look for BOM */
  if(encoding == NULL) {

    if((ret = xmlDetectCharEncoding((const xmlChar*)buf, bytes)) != XML_CHAR_ENCODING_NONE) {
      ap_log_error(APLOG_MARK, APLOG_DEBUG, APR_SUCCESS, r->server,
                   "sniff_encoding: got charset from XML rules");
      return ret;
    }

    /* If none of the above, look for a META-thingey */
    if(ap_regexec(seek_meta_ctype, buf, 1, match, 0) == 0) {
      p = apr_pstrndup(r->pool, buf + match[0].rm_so, match[0].rm_eo - match[0].rm_so);
      if(ap_regexec(seek_charset, p, 2, match, 0) == 0)
        encoding = apr_pstrndup(r->pool, p+match[1].rm_so, match[1].rm_eo - match[1].rm_so);
    }

  }

  /* either it's set to something we found or it's still the default */
  if(encoding) {
    ap_log_error(APLOG_MARK, APLOG_DEBUG, APR_SUCCESS, r->server,
                 "sniff_encoding: got charset %s from HTML META", encoding);
    ret = xmlParseCharEncoding(encoding);
    if(ret != XML_CHAR_ENCODING_ERROR && ret != XML_CHAR_ENCODING_NONE)
      return ret;

    ap_log_error(APLOG_MARK, APLOG_DEBUG, APR_SUCCESS, r->server,
                 "sniff_encoding: charset %s not supported", encoding);
  }

  /* Use configuration default as a last resort */
  ap_log_error(APLOG_MARK, APLOG_DEBUG, APR_SUCCESS, r->server,
               "sniff_encoding: no suitable charset information");
  return (cfg->default_encoding == XML_CHAR_ENCODING_NONE)
    ? XML_CHAR_ENCODING_8859_1 : cfg->default_encoding ;
}
Пример #6
0
apr_status_t h2_request_add_header(h2_request *req, apr_pool_t *pool, 
                                   const char *name, size_t nlen,
                                   const char *value, size_t vlen)
{
    apr_status_t status = APR_SUCCESS;
    
    if (nlen <= 0) {
        return status;
    }
    
    if (name[0] == ':') {
        /* pseudo header, see ch. 8.1.2.3, always should come first */
        if (!apr_is_empty_table(req->headers)) {
            ap_log_perror(APLOG_MARK, APLOG_ERR, 0, pool,
                          APLOGNO(02917) 
                          "h2_request(%d): pseudo header after request start",
                          req->id);
            return APR_EGENERAL;
        }
        
        if (H2_HEADER_METHOD_LEN == nlen
            && !strncmp(H2_HEADER_METHOD, name, nlen)) {
            req->method = apr_pstrndup(pool, value, vlen);
        }
        else if (H2_HEADER_SCHEME_LEN == nlen
                 && !strncmp(H2_HEADER_SCHEME, name, nlen)) {
            req->scheme = apr_pstrndup(pool, value, vlen);
        }
        else if (H2_HEADER_PATH_LEN == nlen
                 && !strncmp(H2_HEADER_PATH, name, nlen)) {
            req->path = apr_pstrndup(pool, value, vlen);
        }
        else if (H2_HEADER_AUTH_LEN == nlen
                 && !strncmp(H2_HEADER_AUTH, name, nlen)) {
            req->authority = apr_pstrndup(pool, value, vlen);
        }
        else {
            char buffer[32];
            memset(buffer, 0, 32);
            strncpy(buffer, name, (nlen > 31)? 31 : nlen);
            ap_log_perror(APLOG_MARK, APLOG_WARNING, 0, pool,
                          APLOGNO(02954) 
                          "h2_request(%d): ignoring unknown pseudo header %s",
                          req->id, buffer);
        }
    }
    else {
        /* non-pseudo header, append to work bucket of stream */
        status = add_h1_header(req, pool, name, nlen, value, vlen);
    }
    
    return status;
}
Пример #7
0
/*--------------------------------------------------------------------------*/
static const char *cmd_advertise_m(cmd_parms *cmd, void *dummy,
                                   const char *arg, const char *opt)
{
    mod_advertise_config *mconf = ap_get_module_config(cmd->server->module_config, &advertise_module);
    if (mconf->ma_advertise_srvs)
        return "Duplicate ServerAdvertise directives are not allowed";

    if (strcasecmp(arg, "Off") == 0)
        mconf->ma_advertise_mode = ma_advertise_off;
    else if (strcasecmp(arg, "On") == 0)
        mconf->ma_advertise_mode = ma_advertise_on;
    else
        return "ServerAdvertise must be Off or On";
    if (opt) {
        const char *p = ap_strstr_c(opt, "://");
        if (p) {
            mconf->ma_advertise_srvm = apr_pstrndup(cmd->pool, opt, p - opt);
            opt = p + 3;
        }
        if (apr_parse_addr_port(&mconf->ma_advertise_srvs,
                                &mconf->ma_advertise_srvi,
                                &mconf->ma_advertise_srvp,
                                opt, cmd->pool) != APR_SUCCESS ||
                                !mconf->ma_advertise_srvs ||
                                !mconf->ma_advertise_srvp)
            return "Invalid ServerAdvertise Address";
    }
    mconf->ma_advertise_server = cmd->server;
    return NULL;
}
Пример #8
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);
}
Пример #9
0
static void cdata_handler(void *userdata, const char *data, int len)
{
    apr_xml_parser *parser = userdata;
    apr_xml_elem *elem;
    apr_text_header *hdr;
    const char *s;

    /* punt once we find an error */
    if (parser->error)
        return;

    elem = parser->cur_elem;
    s = apr_pstrndup(parser->p, data, len);

    if (elem->last_child == NULL) {
        /* no children yet. this cdata follows the start tag */
        hdr = &elem->first_cdata;
    }
    else {
        /* child elements exist. this cdata follows the last child. */
        hdr = &elem->last_child->following_cdata;
    }

    apr_text_append(parser->p, hdr, s);
}
Пример #10
0
size_t _mapcache_curl_header_callback( void *ptr, size_t size, size_t nmemb,  void  *userdata)
{
  char *colonptr;
  struct _header_struct *h = (struct _header_struct*)userdata;
  char *header = apr_pstrndup(h->ctx->pool,ptr,size*nmemb);
  char *endptr = strstr(header,"\r\n");
  if(!endptr) {
    endptr = strstr(header,"\n");
    if(!endptr) {
      /* skip invalid header */
#ifdef DEBUG
      h->ctx->log(h->ctx,MAPCACHE_DEBUG,"received header %s with no trailing \\r\\n",header);
#endif
      return size*nmemb;
    }
  }
  colonptr = strchr(header,':');
  if(colonptr) {
    *colonptr = '\0';
    *endptr = '\0';
    apr_table_setn(h->headers,header,colonptr+2);
  }

  return size*nmemb;
}
Пример #11
0
static apr_dbd_t *dbd_mysql_open(apr_pool_t *pool, const char *params)
{
    static const char *const delims = " \r\n\t;|,";
    const char *ptr;
    int i;
    const char *key;
    size_t klen;
    const char *value;
    size_t vlen;
    struct {
        const char *field;
        const char *value;
    } fields[] = {
        {"host", NULL},
        {"user", NULL},
        {"pass", NULL},
        {"dbname", NULL},
        {"port", NULL},
        {"sock", NULL},
        {NULL, NULL}
    };
    unsigned int port = 0;
    apr_dbd_t *sql = apr_pcalloc(pool, sizeof(apr_dbd_t));
    sql->conn = mysql_init(sql->conn);
    if ( sql->conn == NULL ) {
        return NULL;
    }
    for (ptr = strchr(params, '='); ptr; ptr = strchr(ptr, '=')) {
        for (key = ptr-1; isspace(*key); --key);
        klen = 0;
        while (isalpha(*key)) {
            /* don't parse backwards off the start of the string */
            if (key == params) {
                --key;
                ++klen;
                break;
            }
            --key;
            ++klen;
        }
        ++key;
        for (value = ptr+1; isspace(*value); ++value);
        vlen = strcspn(value, delims);
        for (i=0; fields[i].field != NULL; ++i) {
            if (!strncasecmp(fields[i].field, key, klen)) {
                fields[i].value = apr_pstrndup(pool, value, vlen);
                break;
            }
        }
        ptr = value+vlen;
    }
    if (fields[4].value != NULL) {
        port = atoi(fields[4].value);
    }
    sql->conn = mysql_real_connect(sql->conn, fields[0].value,
                                   fields[1].value, fields[2].value,
                                   fields[3].value, port,
                                   fields[5].value, 0);
    return sql;
}
const char *get_json_callback_param(request_rec *r) {
  char *p, *start_p, *end_p;
  int i;
  const char *callback;
  if (r->args) {
      i = 0;
      p = r->args;
      do {
          int len = strlen(p);
          if (len >= 9 && strncasecmp(p, "callback=", 9) == 0) {
              i = 1;
              break;
          }
          if (len<=0)
              break;
      } 
      while(p++);

      if (i) {
          i = 0;
          start_p = p += 9;
          end_p = r->args + strlen(r->args);
          while (p <= end_p && *p++ != '&') {
              i++;
          }
          return apr_pstrndup(r->connection->pool, start_p, p-start_p-1);
      }
  }
  return callback;
}
const char *get_progress_id(request_rec *r) {
  char *p, *start_p, *end_p;
  int i;
  //try to find progress id in headers
  const char *id  = apr_table_get(r->headers_in, PROGRESS_ID);
  //if not found check args
  if(id == NULL) {
    if (r->args) {
        i = 0;
        p = r->args;
        do {
            int len = strlen(p);
            if (len >= 14 && strncasecmp(p, "X-Progress-ID=", 14) == 0) {
                i = 1;
                break;
            }
            if (len<=0)
                break;
        } 
        while(p++);

        if (i) {
            i = 0;
            start_p = p += 14;
            end_p = r->args + strlen(r->args);
            while (p <= end_p && *p++ != '&') {
                i++;
            }
            return apr_pstrndup(r->connection->pool, start_p, p-start_p-1);
        }
    }
  }
  return id;
}
Пример #14
0
/* count the number of users */
static int get_unique ( void )
{
    apr_pool_t *subpool;
    apr_hash_t *hashuser;
    char *name;
    void *user;
    unsigned int numunique = 0;
    struct utmp *utmpstruct;

    apr_pool_create(&subpool, pool);
    hashuser = apr_hash_make(subpool);
    apr_thread_mutex_lock(mutex);
    setutent();
    while ((utmpstruct = getutent())) {
        if ((utmpstruct->ut_type == USER_PROCESS) &&
           (utmpstruct->ut_name[0] != '\0')) {
            name = apr_pstrndup(subpool, utmpstruct->ut_name, UT_NAMESIZE);
            user = name; /* use key for value, not interested in it anyway */
            apr_hash_set(hashuser, name, APR_HASH_KEY_STRING, user);
        }
    }
    endutent();
    apr_thread_mutex_unlock(mutex);
    numunique = apr_hash_count(hashuser);
    apr_pool_destroy(subpool);

    return numunique;
}
Пример #15
0
char *
svn_fs__next_entry_name(const char **next_p,
                        const char *path,
                        apr_pool_t *pool)
{
  const char *end;

  /* Find the end of the current component.  */
  end = strchr(path, '/');

  if (! end)
    {
      /* The path contains only one component, with no trailing
         slashes. */
      *next_p = 0;
      return apr_pstrdup(pool, path);
    }
  else
    {
      /* There's a slash after the first component.  Skip over an arbitrary
         number of slashes to find the next one. */
      const char *next = end;
      while (*next == '/')
        next++;
      *next_p = next;
      return apr_pstrndup(pool, path, end - path);
    }
}
Пример #16
0
/**
 * find certain header line, return copy of first part (up to first ";")
 * @param p pool to allocate from
 * @param name name of the header
 * @param string input: pointer to pointer string where to find the header;
 *        output: pointer to the ";" or "\n" after the copied value
 * @param end pointer where to stop searching
 * @note string must be NUL-terminated (but the NUL may be after *end)
 * @return copy of the header value or NULL if not found
 */
static char *mbox_mime_get_header(apr_pool_t *p, const char *name,
                                  char **string, const char *end)
{
    char *ptr;
    int namelen = strlen(name);
    for (ptr = *string;
         ptr && *ptr && ptr < end ;
         ptr = ap_strchr(ptr + 1, '\n') + 1)
    {
        int l;
        if (strncasecmp(ptr, name, namelen) != 0)
            continue;
        ptr += namelen;
        if (*ptr != ':')
            continue;
        ptr++;
        while (*ptr == ' ')
            ptr++;
        if (ptr >= end)
            break;
        l = strcspn(ptr, ";\n");
        *string = ptr + l;
        while (apr_isspace(ptr[l]) && l > 0)
            l--;
        return apr_pstrndup(p, ptr, l);
    }
    return NULL;
}
Пример #17
0
static const char * format_location(request_rec * r, const char * url, const char *login) {
	const char * out = "";
	const char * p = url;
	const char * append = NULL;
	while (*p) {
		if (*p == '%') {
			if (*(p + 1) == '%') {
				append = "%";
			} else if (*(p + 1) == 's') {
				append = ap_construct_url(r->pool, r->uri, r);
				if (r->args) {
					append = apr_pstrcat(r->pool, append, "?", r->args, NULL);
				}
			} else if (*(p + 1) == 'u') {
				append = login;
			}
		}
		if (append) {
			char * prefix = "";
			if (p != url) {
				prefix = apr_pstrndup(r->pool, url, p - url);
			}
			out = apr_pstrcat(r->pool, out, prefix, ap_escape_urlencoded(r->pool, append), NULL);
			p++;
			url = p + 1;
			append = NULL;
		}
		p++;
	}
	if (p != url) {
		out = apr_pstrcat(r->pool, out, url, NULL);
	}
	return out;
}
Пример #18
0
/*
 * Hook and mod_cache callback functions
 */
static int create_entity(cache_handle_t *h, request_rec *r, const char *key, apr_off_t len)
{
    disk_cache_conf *conf = ap_get_module_config(r->server->module_config,
                                                 &disk_cache_module);
    cache_object_t *obj;
    disk_cache_object_t *dobj;

    if (conf->cache_root == NULL) {
        return DECLINED;
    }

    /* Allocate and initialize cache_object_t and disk_cache_object_t */
    h->cache_obj = obj = apr_pcalloc(r->pool, sizeof(*obj));
    obj->vobj = dobj = apr_pcalloc(r->pool, sizeof(*dobj));

    obj->key = apr_pstrdup(r->pool, key);

    dobj->name = obj->key;
    dobj->prefix = NULL;
    /* Save the cache root */
    dobj->root = apr_pstrndup(r->pool, conf->cache_root, conf->cache_root_len);
    dobj->root_len = conf->cache_root_len;
    dobj->datafile = data_file(r->pool, conf, dobj, key);
    dobj->hdrsfile = header_file(r->pool, conf, dobj, key);
    dobj->tempfile = apr_pstrcat(r->pool, conf->cache_root, AP_TEMPFILE, NULL);

    return OK;
}
Пример #19
0
/* return each comma separated token, one at a time */
CACHE_DECLARE(const char *)ap_cache_tokstr(apr_pool_t *p, const char *list,
                                           const char **str)
{
    apr_size_t i;
    const char *s;

    s = ap_strchr_c(list, ',');
    if (s != NULL) {
        i = s - list;
        do
            s++;
        while (apr_isspace(*s))
            ; /* noop */
    }
    else
        i = strlen(list);

    while (i > 0 && apr_isspace(list[i - 1]))
        i--;

    *str = s;
    if (i)
        return apr_pstrndup(p, list, i);
    else
        return NULL;
}
Пример #20
0
int main(int argc,char **argv) {
    apr_initialize();
    apr_pool_t *pool;
    apr_pool_create(&pool,NULL);


    apr_status_t st;
    apr_dbm_t *dbm;
    st=apr_dbm_open(&dbm,"friends",APR_DBM_RWCREATE,APR_OS_DEFAULT ,pool);

    apr_datum_t key,value;
    key.dptr="laomeng";
    key.dsize=strlen(key.dptr);
    
    value.dptr="*****@*****.**";
    value.dsize=strlen(value.dptr);
   
    st=apr_dbm_store(dbm,key,value);

    apr_datum_t pvalue;
    st=apr_dbm_fetch(dbm,key,&pvalue);

    char *pstr=apr_pstrndup(pool,pvalue.dptr,pvalue.dsize);
    printf("value => %s\n",pstr);
    
    apr_dbm_close(dbm);

    apr_pool_destroy(pool);
    apr_terminate();
    return 0;
}
Пример #21
0
void
svn_stringbuf_replace(svn_stringbuf_t *str,
                      apr_size_t pos,
                      apr_size_t old_count,
                      const char *bytes,
                      apr_size_t new_count)
{
  if (bytes + new_count > str->data && bytes < str->data + str->blocksize)
    {
      /* special case: BYTES overlaps with this string -> copy the source */
      const char *temp = apr_pstrndup(str->pool, bytes, new_count);
      svn_stringbuf_replace(str, pos, old_count, temp, new_count);
    }
  else
    {
      if (pos > str->len)
        pos = str->len;
      if (pos + old_count > str->len)
        old_count = str->len - pos;

      if (old_count < new_count)
        {
          apr_size_t delta = new_count - old_count;
          svn_stringbuf_ensure(str, str->len + delta);
        }

      if (old_count != new_count)
        memmove(str->data + pos + new_count, str->data + pos + old_count,
                str->len - pos - old_count + 1);

      memcpy(str->data + pos, bytes, new_count);
      str->len += new_count - old_count;
    }
}
Пример #22
0
/*
 * perform compact serialization on a JWT and return the resulting string
 */
char *oidc_jwt_serialize(apr_pool_t *pool, oidc_jwt_t *jwt,
		oidc_jose_error_t *err) {
	cjose_err cjose_err;
	const char *cser = NULL;
	if (strcmp(jwt->header.alg, "none") != 0) {
		if (cjose_jws_export(jwt->cjose_jws, &cser, &cjose_err) == FALSE) {
			oidc_jose_error(err, "cjose_jws_export failed: %s",
					oidc_cjose_e2s(pool, cjose_err));
			return NULL;
		}
	} else {

		char *s_payload = json_dumps(jwt->payload.value.json,
				JSON_PRESERVE_ORDER | JSON_COMPACT);

		char *out = NULL;
		size_t out_len;
		if (cjose_base64url_encode((const uint8_t *)s_payload, strlen(s_payload), &out, &out_len,
				&cjose_err) == FALSE)
			return FALSE;
		cser = apr_pstrndup(pool, out, out_len);
		cjose_get_dealloc()(out);

		free(s_payload);

		cser = apr_psprintf(pool, "eyJhbGciOiJub25lIn0.%s.", cser);
	}
	return apr_pstrdup(pool, cser);
}
Пример #23
0
static char * add_qstring_ignore_token(apr_pool_t *pool, saxctxt *ctx, apr_uri_t *u)
{
  char *where;
  const char *arg = ctx->cfg->ignore_token_arg_relative;

  if(!u->query || !ctx->cfg->ignore_token || !arg)
    return u->query;

  ap_log_error(APLOG_MARK, APLOG_DEBUG, APR_SUCCESS, ctx->f->r->server,
               "add_qstring_ignore_token: adding ignore token %s to %s relative to %s",
               ctx->cfg->ignore_token, u->query, arg);

  /*
   * doing this rather than just a basic strstr for arg makes sure we
   * find an argument name, and not the string within a value
   */
  where = (!strncmp(u->query, arg, strlen(arg))) ? u->query :
    strstr(u->query, apr_pstrcat(pool, "&", arg, 0));

  if(!where) { /* give up, can't find the argument */
    ap_log_error(APLOG_MARK, APLOG_DEBUG, APR_SUCCESS, ctx->f->r->server,
                 "add_qstring_ignore_token: can't find %s", arg);
    return u->query;
  }

  if(ctx->cfg->ignore_token_direction == BEFORE) {
    /* put the ignore_token before arg_relative in the query string */
    if(where == u->query) /* at the beginning of the qstring */
      return apr_pstrcat(pool, ctx->cfg->ignore_token, "&", u->query, 0);
    else {                /* somewhere in the middle */
      char *start = apr_pstrndup(pool, u->query, where - u->query);
      return apr_pstrcat(pool, start, "&", ctx->cfg->ignore_token, where, 0);
    }
  } else {
    /*
     * put the ignore_token after arg_relative in the query string
     * first find the end of the argument/value
     */
    char *end = strchr(where, '&');
    if(end) {
      char *start = apr_pstrndup(pool, u->query, end - u->query);
      return apr_pstrcat(pool, start, "&", ctx->cfg->ignore_token, end, 0);
    } else
      return apr_pstrcat(pool, u->query, "&", ctx->cfg->ignore_token, 0);
  }
}
Пример #24
0
void svn_cstring_split_endline_append (apr_array_header_t *array,
				       const char *input,
				       apr_pool_t *pool) {
  int start = 0, end;
  int len = strlen(input);
  for (end = 0; end < len; end++){
    if (input[end] == '\n') {
      char *sub = apr_pstrndup(pool,(input+start),(end-start));
      *(char **)apr_array_push(array) = sub;
      start = end+1;
    }
  }
  if (start < end) {
    char *sub = apr_pstrndup(pool,(input+start),(len-start));
    *(char **)apr_array_push(array) = sub;
  }
} 	
Пример #25
0
APR_DECLARE(apr_status_t) apr_uid_get(apr_uid_t *uid, apr_gid_t *gid,
                                      const char *username, apr_pool_t *p)
{
#ifdef _WIN32_WCE
    return APR_ENOTIMPL;
#else
    SID_NAME_USE sidtype;
    char anydomain[256];
    char *domain;
    DWORD sidlen = 0;
    DWORD domlen = sizeof(anydomain);
    DWORD rv;
    char *pos;

    if (pos = strchr(username, '/')) {
        domain = apr_pstrndup(p, username, pos - username);
        username = pos + 1;
    }
    else if (pos = strchr(username, '\\')) {
        domain = apr_pstrndup(p, username, pos - username);
        username = pos + 1;
    }
    else {
        domain = NULL;
    }
    /* Get nothing on the first pass ... need to size the sid buffer 
     */
    rv = LookupAccountName(domain, username, domain, &sidlen, 
                           anydomain, &domlen, &sidtype);
    if (sidlen) {
        /* Give it back on the second pass
         */
        *uid = apr_palloc(p, sidlen);
        domlen = sizeof(anydomain);
        rv = LookupAccountName(domain, username, *uid, &sidlen, 
                               anydomain, &domlen, &sidtype);
    }
    if (!sidlen || !rv) {
        return apr_get_os_error();
    }
    /* There doesn't seem to be a simple way to retrieve the primary group sid
     */
    *gid = NULL;
    return APR_SUCCESS;
#endif
}
static void set_body(upload_ctx* ctx, const char* data) {
  const char* cr = strchr(data, '\r') ;
  char* tmp = apr_pstrndup(ctx->pool, data, cr-data) ;
  if ( ctx->val )
    ctx->val = apr_pstrcat(ctx->pool, ctx->val, tmp, NULL) ;
  else
    ctx->val = tmp ;
}
Пример #27
0
static size_t rcache_curl_write_cb(const void* ptr, size_t size, size_t nmemb, void* _info)
{
    rcache_info* info = _info;
    if (nmemb == 0) return 0;
    const char* tmp = apr_pstrndup(info->r->pool, ptr, size * nmemb);
    info->data = apr_pstrcat(info->r->pool, info->data, tmp, NULL);
    return nmemb;
}
Пример #28
0
static const char *mag_cred_store(cmd_parms *parms, void *mconfig,
                                  const char *w)
{
    struct mag_config *cfg = (struct mag_config *)mconfig;
    gss_key_value_element_desc *elements;
    uint32_t count;
    size_t size;
    const char *p;
    char *value;
    char *key;

    p = strchr(w, ':');
    if (!p) {
        ap_log_error(APLOG_MARK, APLOG_ERR|APLOG_NOERRNO, 0, parms->server,
                     "%s [%s]", "Invalid syntax for GssapiCredStore option", w);
        return NULL;
    }

    key = apr_pstrndup(parms->pool, w, (p-w));
    value = apr_pstrdup(parms->pool, p + 1);
    if (!key || !value) {
        ap_log_error(APLOG_MARK, APLOG_ERR|APLOG_NOERRNO, 0, parms->server,
                     "%s", "OOM handling GssapiCredStore option");
        return NULL;
    }

    if (!cfg->cred_store) {
        cfg->cred_store = apr_pcalloc(parms->pool,
                                      sizeof(gss_key_value_set_desc));
        if (!cfg->cred_store) {
            ap_log_error(APLOG_MARK, APLOG_ERR|APLOG_NOERRNO, 0, parms->server,
                         "%s", "OOM handling GssapiCredStore option");
            return NULL;
        }
        size = sizeof(gss_key_value_element_desc) * MAX_CRED_OPTIONS;
        cfg->cred_store->elements = apr_palloc(parms->pool, size);
        if (!cfg->cred_store->elements) {
            ap_log_error(APLOG_MARK, APLOG_ERR|APLOG_NOERRNO, 0, parms->server,
                         "%s", "OOM handling GssapiCredStore option");
        }
    }

    elements = cfg->cred_store->elements;
    count = cfg->cred_store->count;

    if (count >= MAX_CRED_OPTIONS) {
        ap_log_error(APLOG_MARK, APLOG_ERR|APLOG_NOERRNO, 0, parms->server,
                     "Too many GssapiCredStore options (MAX: %d)",
                     MAX_CRED_OPTIONS);
        return NULL;
    }
    cfg->cred_store->count++;

    elements[count].key = key;
    elements[count].value = value;

    return NULL;
}
Пример #29
0
AP_DECLARE(void) ap_add_cgi_vars(request_rec *r)
{
    apr_table_t *e = r->subprocess_env;

    apr_table_setn(e, "GATEWAY_INTERFACE", "CGI/1.1");
    apr_table_setn(e, "SERVER_PROTOCOL", r->protocol);
    apr_table_setn(e, "REQUEST_METHOD", r->method);
    apr_table_setn(e, "QUERY_STRING", r->args ? r->args : "");
    apr_table_setn(e, "REQUEST_URI", original_uri(r));

    /* Note that the code below special-cases scripts run from includes,
     * because it "knows" that the sub_request has been hacked to have the
     * args and path_info of the original request, and not any that may have
     * come with the script URI in the include command.  Ugh.
     */

    if (!strcmp(r->protocol, "INCLUDED")) {
        apr_table_setn(e, "SCRIPT_NAME", r->uri);
        if (r->path_info && *r->path_info) {
            apr_table_setn(e, "PATH_INFO", r->path_info);
        }
    }
    else if (!r->path_info || !*r->path_info) {
        apr_table_setn(e, "SCRIPT_NAME", r->uri);
    }
    else {
        int path_info_start = ap_find_path_info(r->uri, r->path_info);

        apr_table_setn(e, "SCRIPT_NAME",
                      apr_pstrndup(r->pool, r->uri, path_info_start));

        apr_table_setn(e, "PATH_INFO", r->path_info);
    }

    if (r->path_info && r->path_info[0]) {
        /*
         * To get PATH_TRANSLATED, treat PATH_INFO as a URI path.
         * Need to re-escape it for this, since the entire URI was
         * un-escaped before we determined where the PATH_INFO began.
         */
        request_rec *pa_req;

        pa_req = ap_sub_req_lookup_uri(ap_escape_uri(r->pool, r->path_info), r,
                                       NULL);

        if (pa_req->filename) {
            char *pt = apr_pstrcat(r->pool, pa_req->filename, pa_req->path_info,
                                  NULL);
#ifdef WIN32
            /* We need to make this a real Windows path name */
            apr_filepath_merge(&pt, "", pt, APR_FILEPATH_NATIVE, r->pool);
#endif
            apr_table_setn(e, "PATH_TRANSLATED", pt);
        }
        ap_destroy_sub_req(pa_req);
    }
}
Пример #30
0
			int rc_matchByStringsPattern(pool* p, char* regex, char* value, array_header** matches){
				int ret=0;
				regex_t *preg=NULL;
				regmatch_t pmatch[9];
				int i=0;

				memset(pmatch,-1, sizeof(pmatch));

				//match vars
				array_header* nmatches=NULL;
				char** foundPlace=NULL, *found=NULL;
				int chars=0;

				//do regex match
				preg=apr_palloc(p,sizeof(regex_t));

		#ifdef NGX_TCREWRITE_PCRE
				// set custom pcre func for NGX_TCREWRITE_PCRE
				pool *ip = (pool *) iota_pcre_malloc_init(p);
		#endif

				ret=regcomp(preg,regex,0);
				if(ret!=0){
		#ifdef NGX_TCREWRITE_PCRE
					// reset pcre func for NGX_TCREWRITE_PCRE
					iota_pcre_malloc_done(ip);
		#endif
					return -555;
				}
				if(matches!=NULL){
					ret=regexec(preg,value,9,pmatch,0);

					if(pmatch[0].rm_so!=-1){
						nmatches=apr_array_make(p,4,sizeof(char*));
						for(i=0;i<9;i++){
							if(pmatch[i].rm_so!=-1){
								chars=pmatch[i].rm_eo-pmatch[i].rm_so;
								found=apr_pstrndup(p,value+(int)pmatch[i].rm_so,chars);

								foundPlace=(char**)apr_array_push(nmatches);
								*foundPlace=found;
							}
						}

						*matches=nmatches;
					}
				}else{
					ret=regexec(preg,value,0,NULL,0);
				}
				regfree(preg);

		#ifdef NGX_TCREWRITE_PCRE
				// reset pcre func for NGX_TCREWRITE_PCRE
				iota_pcre_malloc_done(ip);
		#endif
				return ret;
			}