registry responds with both domain and host cookies; registry uses snprintfz, strncpyz
Costa Tsaousis (ktsaou) committed
May 14, 2016 at 18:49 UTC
f1647aa7167a8235fed3dff56fe2c741410d075a
3 files changed
+30
-26
src/registry.c
+15
-22
@@ -340,8 +340,7 @@ static inline URL *registry_url_allocate_nolock(const char *url, size_t urllen)
340
341
// a simple strcpy() should do the job
342
// but I prefer to be safe, since the caller specified urllen
343
- strncpy(u->url, url, urllen);
344
- u->url[urllen] = '\0';
343
+ strncpyz(u->url, url, urllen);
344
345
u->len = urllen;
346
u->links = 0;
@@ -424,8 +423,7 @@ static inline MACHINE *registry_machine_allocate(const char *machine_guid, time_
423
MACHINE *m = malloc(sizeof(MACHINE));
424
if(!m) fatal("Registry: cannot allocate memory for new machine '%s'", machine_guid);
425
427
- strncpy(m->guid, machine_guid, 36);
428
- m->guid[36] = '\0';
426
+ strncpyz(m->guid, machine_guid, 36);
427
428
debug(D_REGISTRY, "Registry: registry_machine_allocate('%s'): creating dictionary of urls", machine_guid);
429
m->urls = dictionary_create(DICTIONARY_FLAGS);
@@ -488,8 +486,7 @@ static inline PERSON_URL *registry_person_url_allocate(PERSON *p, MACHINE *m, UR
486
487
// a simple strcpy() should do the job
488
// but I prefer to be safe, since the caller specified urllen
491
- strncpy(pu->name, name, namelen);
492
- pu->name[namelen] = '\0';
489
+ strncpyz(pu->name, name, namelen);
490
491
pu->machine = m;
492
pu->first_t = pu->last_t = when;
@@ -552,10 +549,8 @@ static inline PERSON *registry_person_allocate(const char *person_guid, time_t w
549
info("Registry: generated person guid '%s' found in the registry. Retrying...", p->guid);
550
}
551
}
555
- else {
556
- strncpy(p->guid, person_guid, 36);
557
- p->guid[36] = '\0';
558
- }
552
+ else
553
+ strncpyz(p->guid, person_guid, 36);
554
555
debug(D_REGISTRY, "Registry: registry_person_allocate('%s'): creating dictionary of urls", p->guid);
556
p->urls = dictionary_create(DICTIONARY_FLAGS);
@@ -1022,12 +1017,10 @@ static inline void registry_set_person_cookie(struct web_client *w, PERSON *p) {
1017
struct tm etmbuf, *etm = gmtime_r(&et, &etmbuf);
1018
strftime(edate, sizeof(edate), "%a, %d %b %Y %H:%M:%S %Z", etm);
1019
1025
- if(registry.registry_domain && registry.registry_domain[0])
1026
- snprintf(w->cookie, COOKIE_MAX, NETDATA_REGISTRY_COOKIE_NAME "=%s; Domain=%s; Expires=%s", p->guid, registry.registry_domain, edate);
1027
- else
1028
- snprintf(w->cookie, COOKIE_MAX, NETDATA_REGISTRY_COOKIE_NAME "=%s; Expires=%s", p->guid, edate);
1020
+ snprintfz(w->cookie1, COOKIE_MAX, NETDATA_REGISTRY_COOKIE_NAME "=%s; Expires=%s", p->guid, edate);
1021
1030
- w->cookie[COOKIE_MAX] = '\0';
1022
+ if(registry.registry_domain && registry.registry_domain[0])
1023
+ snprintfz(w->cookie2, COOKIE_MAX, NETDATA_REGISTRY_COOKIE_NAME "=%s; Domain=%s; Expires=%s", p->guid, registry.registry_domain, edate);
1024
}
1025
1026
static inline void registry_json_header(struct web_client *w, const char *action, const char *status) {
@@ -1399,8 +1392,8 @@ int registry_save(void) {
1392
char tmp_filename[FILENAME_MAX + 1];
1393
char old_filename[FILENAME_MAX + 1];
1394
1402
- snprintf(old_filename, FILENAME_MAX, "%s.old", registry.db_filename);
1403
- snprintf(tmp_filename, FILENAME_MAX, "%s.tmp", registry.db_filename);
1395
+ snprintfz(old_filename, FILENAME_MAX, "%s.old", registry.db_filename);
1396
+ snprintfz(tmp_filename, FILENAME_MAX, "%s.tmp", registry.db_filename);
1397
1398
debug(D_REGISTRY, "Registry: Creating file '%s'", tmp_filename);
1399
FILE *fp = fopen(tmp_filename, "w");
@@ -1643,14 +1636,14 @@ int registry_init(void) {
1636
}
1637
1638
// filenames
1646
- snprintf(filename, FILENAME_MAX, "%s/netdata.public.unique.id", registry.pathname);
1639
+ snprintfz(filename, FILENAME_MAX, "%s/netdata.public.unique.id", registry.pathname);
1640
registry.machine_guid_filename = config_get("registry", "netdata unique id file", filename);
1641
registry_get_this_machine_guid();
1642
1650
- snprintf(filename, FILENAME_MAX, "%s/registry.db", registry.pathname);
1643
+ snprintfz(filename, FILENAME_MAX, "%s/registry.db", registry.pathname);
1644
registry.db_filename = config_get("registry", "registry db file", filename);
1645
1653
- snprintf(filename, FILENAME_MAX, "%s/registry-log.db", registry.pathname);
1646
+ snprintfz(filename, FILENAME_MAX, "%s/registry-log.db", registry.pathname);
1647
registry.log_filename = config_get("registry", "registry log file", filename);
1648
1649
// configuration options
@@ -1883,7 +1876,7 @@ int test1(int argc, char **argv) {
1876
uuid_unparse(uuid, machines_guids[m]);
1877
1878
char buf[FILENAME_MAX + 1];
1886
- snprintf(buf, FILENAME_MAX, "http://%u.netdata.rocks/", m+1);
1879
+ snprintfz(buf, FILENAME_MAX, "http://%u.netdata.rocks/", m+1);
1880
machines_urls[m] = strdup(buf);
1881
1882
// fprintf(stderr, "\tmachine %u: '%s', url: '%s'\n", m + 1, machines_guids[m], machines_urls[m]);
@@ -1970,7 +1963,7 @@ int test1(int argc, char **argv) {
1963
char *url = machines_urls[tm];
1964
char buf[FILENAME_MAX + 1];
1965
if (random() % 10000 == 1234) {
1973
- snprintf(buf, FILENAME_MAX, "http://random.%ld.netdata.rocks/", random());
1966
+ snprintfz(buf, FILENAME_MAX, "http://random.%ld.netdata.rocks/", random());
1967
url = buf;
1968
}
1969
else if (random() % 1000 == 123)
src/web_client.c
+13
-3
@@ -175,7 +175,8 @@ void web_client_reset(struct web_client *w)
175
}
176
177
w->last_url[0] = '\0';
178
- w->cookie[0] = '\0';
178
+ w->cookie1[0] = '\0';
179
+ w->cookie2[0] = '\0';
180
w->origin[0] = '*';
181
w->origin[1] = '\0';
182
@@ -801,6 +802,9 @@ int web_client_api_request_v1_registry(struct web_client *w, char *url)
802
803
debug(D_WEB_CLIENT, "%llu: API v1 registry with URL '%s'", w->id, url);
804
805
+ // FIXME
806
+ // The browser may send multiple cookies with our id
807
+
808
char *cookie = strstr(w->response.data->buffer, " " NETDATA_REGISTRY_COOKIE_NAME "=");
809
if(cookie)
810
strncpyz(person_guid, &cookie[sizeof(NETDATA_REGISTRY_COOKIE_NAME) + 1], 36);
@@ -1655,10 +1659,16 @@ void web_client_process(struct web_client *w) {
1659
, date
1660
);
1661
1658
- if(w->cookie[0]) {
1662
+ if(w->cookie1[0]) {
1663
+ buffer_sprintf(w->response.header_output,
1664
+ "Set-Cookie: %s\r\n",
1665
+ w->cookie1);
1666
+ }
1667
+
1668
+ if(w->cookie2[0]) {
1669
buffer_sprintf(w->response.header_output,
1670
"Set-Cookie: %s\r\n",
1661
- w->cookie);
1671
+ w->cookie2);
1672
}
1673
1674
if(w->mode == WEB_CLIENT_MODE_OPTIONS) {
src/web_client.h
+2
-1
@@ -61,7 +61,8 @@ struct web_client {
61
62
struct timeval tv_in, tv_ready;
63
64
- char cookie[COOKIE_MAX+1];
64
+ char cookie1[COOKIE_MAX+1];
65
+ char cookie2[COOKIE_MAX+1];
66
char origin[ORIGIN_MAX+1];
67
68
int mode;