static ne_buffer *acl_body(ne_acl_entry *right, int count) { ne_buffer *body = ne_buffer_create(); int m; ne_buffer_zappend(body, "<?xml version=\"1.0\" encoding=\"utf-8\"?>" EOL "<acl xmlns='DAV:'>" EOL); for (m = 0; m < count; m++) { const char *type; type = (right[m].type == ne_acl_grant ? "grant" : "deny"); ne_buffer_concat(body, "<ace>" EOL "<principal>", NULL); switch (right[m].apply) { case ne_acl_all: ne_buffer_zappend(body, "<all/>" EOL); break; case ne_acl_property: ne_buffer_concat(body, "<property><", right[m].principal, "/></property>" EOL, NULL); break; case ne_acl_href: ne_buffer_concat(body, "<href>", right[m].principal, "</href>" EOL, NULL); break; } ne_buffer_concat(body, "</principal>" EOL "<", type, ">" EOL, NULL); if (right[m].read == 0) ne_buffer_concat(body, "<privilege>" "<read/>" "</privilege>" EOL, NULL); if (right[m].read_acl == 0) ne_buffer_concat(body, "<privilege>" "<read-acl/>" "</privilege>" EOL, NULL); if (right[m].write == 0) ne_buffer_concat(body, "<privilege>" "<write/>" "</privilege>" EOL, NULL); if (right[m].write_acl == 0) ne_buffer_concat(body, "<privilege>" "<write-acl/>" "</privilege>" EOL, NULL); if (right[m].read_cuprivset == 0) ne_buffer_concat(body, "<privilege>" "<read-current-user-privilege-set/>" "</privilege>" EOL, NULL); ne_buffer_concat(body, "</", type, ">" EOL, NULL); ne_buffer_zappend(body, "</ace>" EOL); } ne_buffer_zappend(body, "</acl>" EOL); return body; }
int s3_get_bucket(S3 *s3,const char *bucket, const char *prefix,const char *marker,int max_keys,const char *delimiter, const S3KeyInfoCallback *key_info_cb) { ne_request *req; int err, retry; ne_buffer *params; if(!s3) return -1; if(!bucket) return -1; params = ne_buffer_create(); if(prefix) ne_buffer_concat(params,"prefix=",prefix,"/&",NULL); if(marker) ne_buffer_concat(params,"marker=",marker,"&",NULL); if(max_keys >= 0) { char mk[16]; snprintf(mk,16,"%d",max_keys); mk[15] = 0; ne_buffer_concat(params,"max_keys=",max_keys,"&",NULL); } if(delimiter) ne_buffer_concat(params,"delimiter=",delimiter,"&",NULL); s3_begin_session(s3); req = s3_new_request(s3,"GET",bucket,NULL,params->data,NULL); ne_buffer_destroy(params); // send to server do { err = ne_begin_request(req); if(err != NE_OK) err = -EIO; else { if(ne_get_status(req)->code != 200) { s3_handle_error_response(s3,req); err = -EACCES; } else { s3->key_info.key_info_cb = key_info_cb; s3_parse_xml_response(s3, req, s3_xml_key_info_startelm, s3_xml_key_info_cdata, s3_xml_key_info_endelm, &s3->key_info ); } retry = ne_end_request(req); } } while(retry == NE_RETRY); ne_request_destroy(req); s3_end_session(s3); return err; }
static int buf_concat(void) { ne_buffer *s = ne_buffer_create(); ON(s == NULL); ne_buffer_concat(s, "a", "b", "c", "d", "e", "f", "g", NULL); ONCMP(s->data, "abcdefg"); ON(ne_buffer_size(s) != 7); ne_buffer_destroy(s); return OK; }
static int buf_concat3(void) { ne_buffer *s = ne_buffer_create(); ON(s == NULL); ne_buffer_zappend(s, "foobar"); ne_buffer_concat(s, "norman", NULL); ONCMP(s->data, "foobarnorman"); ON(ne_buffer_size(s) != 12); ne_buffer_destroy(s); return OK; }
/* * this hook is called just before a request has been sent. * Here it is used to set the session cookie if available. */ static void pre_send_hook(ne_request *req, void *userdata, ne_buffer *header) { (void) userdata; if( !req ) return; if(dav_session.session_key) { ne_buffer_concat(header, "Cookie: ", dav_session.session_key, "\r\n", NULL); } }
static int buf_concat2(void) { #define RES "alphabetagammadeltaepsilonetatheta" ne_buffer *s = ne_buffer_create(); ON(s == NULL); ne_buffer_concat(s, "alpha", "beta", "gamma", "delta", "epsilon", "eta", "theta", NULL); ONCMP(s->data, RES); ON(ne_buffer_size(s) != strlen(RES)); ne_buffer_destroy(s); return OK; }
char *ne_uri_unparse(const ne_uri *uri) { ne_buffer *buf = ne_buffer_create(); if (uri->scheme) { ne_buffer_concat(buf, uri->scheme, ":", NULL); } if (uri->host) { ne_buffer_czappend(buf, "//"); if (uri->userinfo) { ne_buffer_concat(buf, uri->userinfo, "@", NULL); } ne_buffer_zappend(buf, uri->host); if (uri->port > 0 && (!uri->scheme || ne_uri_defaultport(uri->scheme) != uri->port)) { char str[20]; ne_snprintf(str, 20, ":%d", uri->port); ne_buffer_zappend(buf, str); } } ne_buffer_zappend(buf, uri->path); if (uri->query) { ne_buffer_concat(buf, "?", uri->query, NULL); } if (uri->fragment) { ne_buffer_concat(buf, "#", uri->fragment, NULL); } return ne_buffer_finish(buf); }
static void lk_pre_send(ne_request *r, void *userdata, ne_buffer *req) { struct lh_req_cookie *lrc = ne_get_request_private(r, HOOK_ID); if (lrc->submit != NULL) { struct lock_list *item; /* Add in the If header */ ne_buffer_czappend(req, "If:"); for (item = lrc->submit; item != NULL; item = item->next) { char *uri = ne_uri_unparse(&item->lock->uri); ne_buffer_concat(req, " <", uri, "> (<", item->lock->token, ">)", NULL); ne_free(uri); } ne_buffer_czappend(req, "\r\n"); } }
static void ah_pre_send(ne_request *r, void *cookie, ne_buffer *request) { auth_session *sess = cookie; struct auth_request *req = ne_get_request_private(r, sess->spec->id); if (!sess->can_handle || !req) { NE_DEBUG(NE_DBG_HTTPAUTH, "Not handling session.\n"); } else { char *value; NE_DEBUG(NE_DBG_HTTPAUTH, "Handling auth session.\n"); req->will_handle = 1; switch(sess->scheme) { case auth_scheme_basic: value = request_basic(sess); break; case auth_scheme_digest: value = request_digest(sess, req); break; #ifdef HAVE_GSSAPI case auth_scheme_gssapi: value = request_gssapi(sess); break; #endif #ifdef HAVE_SSPI case auth_scheme_sspi_negotiate: case auth_scheme_sspi_ntlm: value = request_sspi(sess); break; #endif default: value = NULL; break; } if (value != NULL) { ne_buffer_concat(request, sess->spec->req_hdr, ": ", value, NULL); ne_free(value); } } }
/* Just before sending the request: let them add headers if they want */ static void pre_send(ne_request *req, void *session, ne_buffer *request) { ne_cookie_cache *cache = session; ne_cookie *cook; if (cache->cookies == NULL) { return; } ne_buffer_zappend(request, "Cookie: "); for (cook = cache->cookies; cook != NULL; cook=cook->next) { ne_buffer_concat(request, cook->name, "=", cook->value, NULL); if (cook->next != NULL) { ne_buffer_zappend(request, "; "); } } ne_buffer_zappend(request, "\r\n"); }
/* Return Digest authentication credentials header value for the given * session. */ static char *request_digest(auth_session *sess, struct auth_request *req) { struct ne_md5_ctx a2, rdig; unsigned char a2_md5[16], rdig_md5[16]; char a2_md5_ascii[33], rdig_md5_ascii[33]; char nc_value[9] = {0}; const char *qop_value = "auth"; /* qop-value */ ne_buffer *ret; /* Increase the nonce-count */ if (sess->qop != auth_qop_none) { sess->nonce_count++; ne_snprintf(nc_value, 9, "%08x", sess->nonce_count); NE_DEBUG(NE_DBG_HTTPAUTH, "Nonce count is %u, nc is [%s]\n", sess->nonce_count, nc_value); } /* Calculate H(A2). */ ne_md5_init_ctx(&a2); ne_md5_process_bytes(req->method, strlen(req->method), &a2); ne_md5_process_bytes(":", 1, &a2); ne_md5_process_bytes(req->uri, strlen(req->uri), &a2); ne_md5_finish_ctx(&a2, a2_md5); ne_md5_to_ascii(a2_md5, a2_md5_ascii); NE_DEBUG(NE_DBG_HTTPAUTH, "H(A2): %s\n", a2_md5_ascii); NE_DEBUG(NE_DBG_HTTPAUTH, "Calculating Request-Digest.\n"); /* Now, calculation of the Request-Digest. * The first section is the regardless of qop value * H(A1) ":" unq(nonce-value) ":" */ ne_md5_init_ctx(&rdig); /* Use the calculated H(A1) */ ne_md5_process_bytes(sess->h_a1, 32, &rdig); ne_md5_process_bytes(":", 1, &rdig); ne_md5_process_bytes(sess->nonce, strlen(sess->nonce), &rdig); ne_md5_process_bytes(":", 1, &rdig); if (sess->qop != auth_qop_none) { /* Add on: * nc-value ":" unq(cnonce-value) ":" unq(qop-value) ":" */ NE_DEBUG(NE_DBG_HTTPAUTH, "Have qop directive, digesting: [%s:%s:%s]\n", nc_value, sess->cnonce, qop_value); ne_md5_process_bytes(nc_value, 8, &rdig); ne_md5_process_bytes(":", 1, &rdig); ne_md5_process_bytes(sess->cnonce, strlen(sess->cnonce), &rdig); ne_md5_process_bytes(":", 1, &rdig); /* Store a copy of this structure (see note below) */ sess->stored_rdig = rdig; ne_md5_process_bytes(qop_value, strlen(qop_value), &rdig); ne_md5_process_bytes(":", 1, &rdig); } else { /* Store a copy of this structure... we do this because the * calculation of the rspauth= field in the Auth-Info header * is the same as this digest, up to this point. */ sess->stored_rdig = rdig; } /* And finally, H(A2) */ ne_md5_process_bytes(a2_md5_ascii, 32, &rdig); ne_md5_finish_ctx(&rdig, rdig_md5); ne_md5_to_ascii(rdig_md5, rdig_md5_ascii); ret = ne_buffer_create(); ne_buffer_concat(ret, "Digest username=\"", sess->username, "\", " "realm=\"", sess->realm, "\", " "nonce=\"", sess->nonce, "\", " "uri=\"", req->uri, "\", " "response=\"", rdig_md5_ascii, "\", " "algorithm=\"", sess->alg == auth_alg_md5 ? "MD5" : "MD5-sess", "\"", NULL); if (sess->opaque != NULL) { ne_buffer_concat(ret, ", opaque=\"", sess->opaque, "\"", NULL); } if (sess->qop != auth_qop_none) { /* Add in cnonce and nc-value fields */ ne_buffer_concat(ret, ", cnonce=\"", sess->cnonce, "\", " "nc=", nc_value, ", " "qop=\"", qop_value, "\"", NULL); } ne_buffer_zappend(ret, "\r\n"); NE_DEBUG(NE_DBG_HTTPAUTH, "Digest request header is %s\n", ret->data); return ne_buffer_finish(ret); }
static ne_buffer *acl_body(const ne_acl_entry *right, int count) { ne_buffer *body = ne_buffer_create(); int m; ne_buffer_zappend(body, "<?xml version=\"1.0\" encoding=\"utf-8\"?>" EOL "<acl xmlns='DAV:'>" EOL); for (m = 0; m < count; m++) { const char *type; type = (right[m].type == ne_acl_grant ? "grant" : "deny"); ne_buffer_concat(body, "<ace>" EOL "<principal>", NULL); switch (right[m].target) { case ne_acl_all: ne_buffer_czappend(body, "<all/>" EOL); break; case ne_acl_authenticated: ne_buffer_czappend(body, "<authenticated/>" EOL); break; case ne_acl_unauthenticated: ne_buffer_czappend(body, "<unauthenticated/>" EOL); break; case ne_acl_self: ne_buffer_czappend(body, "<self/>" EOL); break; case ne_acl_property: ne_buffer_concat(body, "<property><", right[m].tname, "/></property>" EOL, NULL); break; case ne_acl_href: ne_buffer_concat(body, "<href>", right[m].tname, "</href>" EOL, NULL); break; } ne_buffer_concat(body, "</principal>" EOL "<", type, ">" EOL, NULL); if ((right[m].privileges & NE_ACL_READ) == NE_ACL_READ) ne_buffer_concat(body, "<privilege>" "<read/>" "</privilege>" EOL, NULL); if ((right[m].privileges & NE_ACL_WRITE) == NE_ACL_WRITE) ne_buffer_concat(body, "<privilege>" "<write/>" "</privilege>" EOL, NULL); if ((right[m].privileges & NE_ACL_WRITE_PROPERTIES) == NE_ACL_WRITE_PROPERTIES) ne_buffer_concat(body, "<privilege>" "<write-properties/>" "</privilege>" EOL, NULL); if ((right[m].privileges & NE_ACL_WRITE_CONTENT) == NE_ACL_WRITE_CONTENT) ne_buffer_concat(body, "<privilege>" "<write-content/>" "</privilege>" EOL, NULL); if ((right[m].privileges & NE_ACL_UNLOCK) == NE_ACL_UNLOCK) ne_buffer_concat(body, "<privilege>" "<unlock/>" "</privilege>" EOL, NULL); if ((right[m].privileges & NE_ACL_READ_ACL) == NE_ACL_READ_ACL) ne_buffer_concat(body, "<privilege>" "<read-acl/>" "</privilege>" EOL, NULL); if ((right[m].privileges & NE_ACL_READ_CUPRIVSET) == NE_ACL_READ_CUPRIVSET) ne_buffer_concat(body, "<privilege>" "<read-current-user-privileges-set/>" "</privilege>" EOL, NULL); if ((right[m].privileges & NE_ACL_WRITE_ACL) == NE_ACL_WRITE_ACL) ne_buffer_concat(body, "<privilege>" "<write-acl/>" "</privilege>" EOL, NULL); if ((right[m].privileges & NE_ACL_BIND) == NE_ACL_BIND) ne_buffer_concat(body, "<privilege>" "<bind/>" "</privilege>" EOL, NULL); if ((right[m].privileges & NE_ACL_UNBIND) == NE_ACL_UNBIND) ne_buffer_concat(body, "<privilege>" "<unbind/>" "</privilege>" EOL, NULL); if ((right[m].privileges & NE_ACL_ALL) == NE_ACL_ALL) ne_buffer_concat(body, "<privilege>" "<all/>" "</privilege>" EOL, NULL); ne_buffer_concat(body, "</", type, ">" EOL, NULL); ne_buffer_czappend(body, "</ace>" EOL); } ne_buffer_czappend(body, "</acl>" EOL); return body; }
static ne_request *s3_new_request(const S3 *s3,const char *method,const char *bucket,const char *key,const char *params,const char *content_type) { ne_buffer *date, *signing_string, *request_str; ne_request *req; char *sig, *p; time_t t; if(!s3) return NULL; if(!method) return NULL; if(!bucket) return NULL; if(!s3->session) return NULL; // create some string buffers date = ne_buffer_create(); signing_string = ne_buffer_create(); request_str = ne_buffer_create(); // get the time t = time(NULL); ne_buffer_zappend(date,asctime(gmtime(&t))); if(date->data[date->used - 2] == '\n') date->data[date->used - 2] = 0; ne_buffer_altered(date); // create request if(key) ne_buffer_concat(request_str,"/",bucket,"/",key,NULL); else ne_buffer_concat(request_str,"/",bucket,NULL); if(params && params[0] != 0) { ne_buffer_zappend(request_str,"?"); ne_buffer_zappend(request_str,params); } req = ne_request_create(s3->session,method,request_str->data); // Add date header ne_add_request_header(req,"Date",date->data); // Add content-type header if(content_type) ne_add_request_header(req,"Content-Type",content_type); else content_type = ""; // construct signing string p = strrchr(request_str->data,'?'); if(p) { *p = 0; ne_buffer_altered(request_str); } ne_buffer_concat(signing_string,method,"\n\n",content_type,"\n",date->data,"\n",request_str->data,NULL); // sign the string sig = s3_sign_string(s3,signing_string->data); // construct signed header ne_print_request_header(req,"Authorization","AWS %s:%s",s3->access_id,sig); ne_buffer_destroy(date); ne_buffer_destroy(signing_string); ne_buffer_destroy(request_str); free(sig); return req; }
int ne_lock(ne_session *sess, struct ne_lock *lock) { ne_request *req = ne_request_create(sess, "LOCK", lock->uri.path); ne_buffer *body = ne_buffer_create(); ne_xml_parser *parser = ne_xml_create(); int ret, parse_failed; struct lock_ctx ctx; memset(&ctx, 0, sizeof ctx); ctx.cdata = ne_buffer_create(); ctx.req = req; ne_xml_push_handler(parser, lk_startelm, lk_cdata, lk_endelm, &ctx); /* Create the body */ ne_buffer_concat(body, "<?xml version=\"1.0\" encoding=\"utf-8\"?>" EOL "<lockinfo xmlns='DAV:'>" EOL " <lockscope>", lock->scope==ne_lockscope_exclusive? "<exclusive/>":"<shared/>", "</lockscope>" EOL "<locktype><write/></locktype>", NULL); if (lock->owner) { ne_buffer_concat(body, "<owner>", lock->owner, "</owner>" EOL, NULL); } ne_buffer_zappend(body, "</lockinfo>" EOL); ne_set_request_body_buffer(req, body->data, ne_buffer_size(body)); /* ne_add_request_header(req, "Content-Type", NE_XML_MEDIA_TYPE); */ /* Just to test whether sever accepts both text/xml and application/xml */ ne_add_request_header(req, "Content-Type", "text/xml"); ne_add_depth_header(req, lock->depth); add_timeout_header(req, lock->timeout); /* TODO: * By 2518, we need this only if we are creating a lock-null resource. * Since we don't KNOW whether the lock we're given is a lock-null * or not, we cover our bases. */ ne_lock_using_parent(req, lock->uri.path); /* This one is clearer from 2518 sec 8.10.4. */ ne_lock_using_resource(req, lock->uri.path, lock->depth); ret = ne_xml_dispatch_request(req, parser); ne_buffer_destroy(body); ne_buffer_destroy(ctx.cdata); parse_failed = ne_xml_failed(parser); if (ret == NE_OK && ne_get_status(req)->klass == 2) { if (ctx.token == NULL) { ret = NE_ERROR; ne_set_error(sess, _("No Lock-Token header given")); } else if (parse_failed) { ret = NE_ERROR; ne_set_error(sess, "%s", ne_xml_get_error(parser)); } else if (ne_get_status(req)->code == 207) { ret = NE_ERROR; /* TODO: set the error string appropriately */ } else if (ctx.found) { /* it worked: copy over real lock details if given. */ if (lock->token) ne_free(lock->token); lock->token = ctx.token; ctx.token = NULL; if (ctx.active.timeout != NE_TIMEOUT_INVALID) lock->timeout = ctx.active.timeout; lock->scope = ctx.active.scope; lock->type = ctx.active.type; if (ctx.active.depth >= 0) lock->depth = ctx.active.depth; if (ctx.active.owner) { if (lock->owner) ne_free(lock->owner); lock->owner = ctx.active.owner; ctx.active.owner = NULL; } } else { ret = NE_ERROR; ne_set_error(sess, _("Response missing activelock for %s"), ctx.token); } } else if (ret == NE_OK /* && status != 2xx */) { ret = NE_ERROR; } ne_lock_free(&ctx.active); if (ctx.token) ne_free(ctx.token); ne_request_destroy(req); ne_xml_destroy(parser); return ret; }