static int __cdb_findnext(Dictionary *dic, void *key, unsigned int keylen) { #if defined(USE_TINYCDB) if (dic->first) { cdb_findinit(&dic->cdb_find, &dic->cdb, key, keylen); dic->first = 0; } return cdb_findnext(&dic->cdb_find); #else return cdb_findnext(&dic->cdb, key, keylen); #endif }
int main(int argc, char* argv[]) { int fd; static struct cdb c; errmsg_iam("cdbget"); if(argc < 3) die(1, "usage: cdbget data.cdb key"); fd = open(argv[1], O_RDONLY | O_BINARY); if(fd == -1) diesys(1, "open"); cdb_init(&c, fd); if(cdb_find(&c, argv[2], str_len(argv[2])) > 0) { do { char* x = malloc(cdb_datalen(&c)); if(!x) die(1, "out of memory"); if(cdb_read(&c, x, cdb_datalen(&c), cdb_datapos(&c)) == -1) diesys(1, "cdb_read"); buffer_put(buffer_1, x, cdb_datalen(&c)); buffer_put(buffer_1, "\n", 1); free(x); } while(cdb_findnext(&c, argv[2], str_len(argv[2])) > 0); } buffer_flush(buffer_1); }
static int do_regex_checks(struct _info *info, char *rr_type, char *key, char *rr_rdata) { char *pattern; unsigned keylen = strlen(key), patlen, vpos; int nchecks = 0; if (cdb_findinit(&info->cdbf, &info->cdb, key, keylen) <= 0) { return (-1); } while (cdb_findnext(&info->cdbf) > 0) { int m; vpos = cdb_datapos(&info->cdb); patlen = cdb_datalen(&info->cdb); pattern = malloc(patlen + 1); cdb_read(&info->cdb, pattern, patlen, vpos); pattern[patlen] = '\0'; if (*pattern == '@') { /* * Handle special function for data check */ if (!strcmp(pattern, "@IP")) { m = func_ip(info, rr_type, rr_rdata); if (m) { ++nchecks; } else { return (0); } } else if (!strcmp(pattern, "@SOA")) { m = func_soa(info, rr_type, rr_rdata); if (m) { ++nchecks; } else { return (0); } } } else { m = match(pattern, rr_rdata); // fprintf(stderr, "*** match %s ", m ? "TRUE " : "false"); // fprintf(stderr, "%s\n", pattern); if (m) { ++nchecks; // FIXME: can return now } else { sprintf(info->reason, "%s [%s] fails regexp [%s]", rr_type, rr_rdata, pattern); } } free(pattern); } return (nchecks); }
int cdb_getnext(struct cdb* cdb, const str* key, str* data) { if (cdb == 0 || key == 0 || data == 0) return -1; if (!cdb_findnext(cdb, key->s, key->len)) return 0; if (!str_ready(data, cdb->dlen)) return -1; if (cdb_read(cdb, (unsigned char*)data->s, cdb->dlen, cdb->dpos) == -1) return -1; data->len = cdb->dlen; data->s[data->len] = 0; return 1; }
SDB_VISIBLE int sdb_exists (Sdb* s, const char *key) { char ch; SdbKv *kv; int klen = strlen (key); ut32 pos, hash = sdb_hash (key, klen); kv = (SdbKv*)ht_lookup (s->ht, hash); if (kv) return (*kv->value)? 1: 0; if (s->fd == -1) return 0; cdb_findstart (&s->db); if (cdb_findnext (&s->db, hash, key, klen)) { pos = cdb_datapos (&s->db); cdb_read (&s->db, &ch, 1, pos); return ch != 0; } return 0; }
SDB_VISIBLE char *sdb_get (Sdb* s, const char *key, ut32 *cas) { char *buf; ut32 hash, pos, len, keylen; SdbKv *kv; ut64 now = 0LL; if (cas) *cas = 0; if (!s || !key) return NULL; keylen = strlen (key)+1; hash = sdb_hash (key, keylen); /* search in memory */ kv = (SdbKv*)ht_lookup (s->ht, hash); if (kv) { if (*kv->value) { if (kv->expire) { if (!now) now = sdb_now (); if (now > kv->expire) { sdb_remove (s, key, 0); return NULL; } } if (cas) *cas = kv->cas; return strdup (kv->value); // XXX too many mallocs } return NULL; } /* search in disk */ if (s->fd == -1) return NULL; cdb_findstart (&s->db); if (!cdb_findnext (&s->db, hash, key, keylen)) return NULL; len = cdb_datalen (&s->db); if (len == 0) return NULL; pos = cdb_datapos (&s->db); if (!(buf = malloc (len+1))) // XXX too many mallocs return NULL; cdb_read (&s->db, buf, len, pos); buf[len] = 0; return buf; }
static int find(char *d,int flagwild) { int r; char ch; struct tai cutoff; char ttd[8]; char ttlstr[4]; char recordloc[2]; double newttl; for (;;) { r = cdb_findnext(&c,d,dns_domain_length(d)); if (r <= 0) return r; dlen = cdb_datalen(&c); if (dlen > sizeof data) return -1; if (cdb_read(&c,data,dlen,cdb_datapos(&c)) == -1) return -1; dpos = dns_packet_copy(data,dlen,0,type,2); if (!dpos) return -1; dpos = dns_packet_copy(data,dlen,dpos,&ch,1); if (!dpos) return -1; if ((ch == '=' + 1) || (ch == '*' + 1)) { --ch; dpos = dns_packet_copy(data,dlen,dpos,recordloc,2); if (!dpos) return -1; if (byte_diff(recordloc,2,clientloc)) continue; } if (flagwild != (ch == '*')) continue; dpos = dns_packet_copy(data,dlen,dpos,ttlstr,4); if (!dpos) return -1; uint32_unpack_big(ttlstr,&ttl); dpos = dns_packet_copy(data,dlen,dpos,ttd,8); if (!dpos) return -1; if (byte_diff(ttd,8,"\0\0\0\0\0\0\0\0")) { tai_unpack(ttd,&cutoff); if (ttl == 0) { if (tai_less(&cutoff,&now)) continue; tai_sub(&cutoff,&cutoff,&now); newttl = tai_approx(&cutoff); if (newttl <= 2.0) newttl = 2.0; if (newttl >= 3600.0) newttl = 3600.0; ttl = newttl; } else if (!tai_less(&cutoff,&now)) continue; } return 1; } }
VALUE mCDB_Reader_each_for_key(VALUE self,VALUE key) { struct cdb db; VALUE value; struct cdb_find find; StringValue(key); int fd = open_cdb_fd(self); size_t vlen; cdb_init(&db,fd); cdb_findinit(&find,&db,RSTRING_PTR(key),RSTRING_LEN(key)); while(cdb_findnext(&find) > 0) { vlen = cdb_datalen(&db); value = rb_str_buf_new(vlen); cdb_read(&db,RSTRING_PTR(value),vlen,cdb_datapos(&db)); rb_str_set_len(value,vlen); rb_yield(value); } close(fd); return Qnil; }
int checks(char *key, char *buf) { struct cdb cdb; struct cdb_find cdbf; int fd; char *data; unsigned keylen = strlen(key), datalen, vpos; if (((fd = open(DB, O_RDONLY)) == -1) || (cdb_init(&cdb, fd) != 0)) { printf("Can't open database: %s\n", strerror(errno)); return (-1); } if (cdb_findinit(&cdbf, &cdb, key, keylen) <= 0) { return (-1); } while (cdb_findnext(&cdbf) > 0) { char *pattern, *subject; int m; vpos = cdb_datapos(&cdb); datalen = cdb_datalen(&cdb); data = malloc(datalen + 1); cdb_read(&cdb, data, datalen, vpos); data[datalen] = '\0'; pattern = data; subject = buf; m = match(pattern, subject); printf("*** match %s ", m ? "TRUE " : "false"); printf("%s\n", data); free(data); } cdb_free(&cdb); close(fd); return (0); }
main(int argc,char **argv) { char *key; int r; uint32 pos; uint32 len; unsigned long u = 0; if (!*argv) die_usage(); if (!*++argv) die_usage(); key = *argv; if (*++argv) { scan_ulong(*argv,&u); } cdb_init(&c,0); cdb_findstart(&c); for (;;) { r = cdb_findnext(&c,key,str_len(key)); if (r == -1) die_read(); if (!r) _exit(100); if (!u) break; --u; } pos = cdb_datapos(&c); len = cdb_datalen(&c); while (len > 0) { r = sizeof buf; if (r > len) r = len; if (cdb_read(&c,buf,r,pos) == -1) die_read(); if (buffer_put(buffer_1small,buf,r) == -1) die_write(); pos += r; len -= r; } if (buffer_flush(buffer_1small) == -1) die_write(); _exit(0); }
SDB_VISIBLE const char *sdb_getc (Sdb* s, const char *key, ut32 *cas) { ut32 hash, pos, len, keylen; SdbKv *kv; ut64 now = 0LL; if (cas) *cas = 0; if (!s||!key) return NULL; keylen = strlen (key)+1; hash = sdb_hash (key, keylen); /* search in memory */ kv = (SdbKv*)ht_lookup (s->ht, hash); if (kv) { if (*kv->value) { if (kv->expire) { if (!now) now = sdb_now (); if (now > kv->expire) { sdb_remove (s, key, 0); return NULL; } } if (cas) *cas = kv->cas; return kv->value; } return NULL; } /* search in disk */ if (s->fd == -1) return NULL; cdb_findstart (&s->db); if (!cdb_findnext (&s->db, hash, key, keylen)) return NULL; len = cdb_datalen (&s->db); if (len == 0) return NULL; pos = cdb_datapos (&s->db); return s->db.map+pos; }
static PyObject * cdbo_getall(CdbObject *self, PyObject *args) { PyObject * list, * data; char * key; unsigned int klen; int r, err; if (!PyArg_ParseTuple(args, "s#:getall", &key, &klen)) return NULL; list = PyList_New(0); if (list == NULL) return NULL; cdb_findstart(&self->c); while ((r = cdb_findnext(&self->c, key, klen))) { if (r == -1) { Py_DECREF(list); return CDBerr; } data = CDBO_CURDATA(self); if (data == NULL) { Py_DECREF(list); return NULL; } err = PyList_Append(list, data); Py_DECREF(data); if (err != 0) { Py_DECREF(list); return NULL; } } return list; }
int cdbb_read_nentry(struct cdbb *a, char *k, size_t ks, struct nentry *n) { int err, dlen; unsigned char *buf; array_catb(&n->k, k, ks); err = cdb_find(a->r, (unsigned char *)k, ks); if (err <= 0) return err; err = 0; /* num */ do { dlen = cdb_datalen(a->r); buf = alloca(dlen); if (cdb_read(a->r, buf, dlen, cdb_datapos(a->r)) < 0) return -2; array_catb(&n->e, (char *)buf, dlen); err++; } while (cdb_findnext(a->r, (unsigned char *)k, ks) > 0); return err; }
void doaxfr(char id[2]) { char key[512]; uint32 klen; char num[4]; uint32 eod; uint32 pos; int r; axfrcheck(zone); find_client_loc(clientloc, ip); tai_now(&now); cdb_init(&c,fdcdb); cdb_findstart(&c); for (;;) { r = cdb_findnext(&c,zone,zonelen); if (r == -1) die_cdbread(); if (!r) die_outside(); dlen = cdb_datalen(&c); if (dlen > sizeof data) die_cdbformat(); if (cdb_read(&c,data,dlen,cdb_datapos(&c)) == -1) die_cdbformat(); if (build(&soa,zone,1,id)) break; } cdb_free(&c); print(soa.s,soa.len); seek_begin(fdcdb); buffer_init(&bcdb,buffer_unixread,fdcdb,bcdbspace,sizeof bcdbspace); pos = 0; get(num,4); pos += 4; uint32_unpack(num,&eod); while (pos < 2048) { get(num,4); pos += 4; } while (pos < eod) { if (eod - pos < 8) die_cdbformat(); get(num,4); pos += 4; uint32_unpack(num,&klen); get(num,4); pos += 4; uint32_unpack(num,&dlen); if (eod - pos < klen) die_cdbformat(); pos += klen; if (eod - pos < dlen) die_cdbformat(); pos += dlen; if (klen > sizeof key) die_cdbformat(); get(key,klen); if (dlen > sizeof data) die_cdbformat(); get(data,dlen); if ((klen > 1) && (key[0] == 0)) continue; /* location */ if (klen < 1) die_cdbformat(); if (dns_packet_getname(key,klen,0,&q) != klen) die_cdbformat(); if (!dns_domain_suffix(q,zone)) continue; if (!build(&message,q,0,id)) continue; print(message.s,message.len); } print(soa.s,soa.len); }
/// Traverses the roadmap file, attempting to find build-avoidance /// opportunities for the current command. Any files eligible for /// recycling are compared to the current state and downloaded /// as required. /// @param[in] ca the CA being shopped for /// @param[in] cmdkey the roadmap index to the command /// @param[in] getfiles boolean - really get new files? /// @return an enum indicating what happened shop_e shop(ca_o ca, CCS cmdkey, int getfiles) { shop_e rc; shopping_state_s shop_state, *ssp = &shop_state; struct cdb_find cdbf; CCS line; char cmdix[64]; if (!ShopCDB) { return SHOP_OFF; } // Expect the worst, hope for the best ... rc = SHOP_NOMATCH; memset(ssp, 0, sizeof(*ssp)); ssp->getfiles = getfiles; ssp->cdbp = ShopCDB; ssp->ca = ca; ssp->ptx_dict = _shop_ptx_init(); ssp->ignore_path_re = re_init_prop__(P_SHOP_IGNORE_PATH_RE); // First, grab the set of PTXes and store them away. if (cdb_findinit(&cdbf, ssp->cdbp, PTX_PREFIX, strlen(PTX_PREFIX)) >= 0) { while (cdb_findnext(&cdbf) > 0) { unsigned len; char xn[64]; CS id; len = cdb_datalen(ssp->cdbp); cdb_read(ssp->cdbp, xn, len, cdb_datapos(ssp->cdbp)); xn[len] = '\0'; if ((id = strchr(xn, '='))) { *id++ = '\0'; _shop_ptx_insert(ssp, xn, id); } else { putil_int("bad PTX line in roadmap: %s", xn); } } } else { putil_die("cdb_findinit (%s)", PTX_PREFIX); } // This is basically a debugging mode where the cmd key is // provided directly. We then look up the cmdline from that, // and rejoin the main logic. if (cmdkey) { if ((line = _shop_find_cmdline(ssp, cmdkey))) { ca_set_line(ssp->ca, line); putil_free(line); } else { putil_int("no line found for cmd key '%s'", cmdkey); rc = SHOP_ERR; } } line = ca_get_line(ssp->ca); // Tell CDB what command line we're looking for. if (cdb_findinit(&cdbf, ssp->cdbp, line, strlen(line)) < 0) { putil_die("cdb_findinit (%s)", line); } // It's possible there would be more than one instance of a given // command line so we look for the full match in a loop. In most // cases this loop will be traversed only once. for (cmdix[0] = '\0'; rc != SHOP_RECYCLED && cdb_findnext(&cdbf) > 0;) { unsigned len; len = cdb_datalen(ssp->cdbp); cdb_read(ssp->cdbp, cmdix, len, cdb_datapos(ssp->cdbp)); cmdix[len] = '\0'; rc = _shop_for_cmd(ssp, cmdix); } // If rc equals SHOP_RECYCLED we've matched a command from a given PTX. // All we need to do is determine all the target files which // go with it and process them. if (rc == SHOP_RECYCLED) { vb_printf(VB_SHOP, "WINNER is %s (%s)", ssp->winner, ssp->wix); if (_shop_collect_targets(ssp, ssp->wincmd) == SHOP_RECYCLED) { if (_shop_process_targets(ssp)) { rc = SHOP_ERR; } else { // Mark the passed-in CA as recycled. ca_set_recycled(ssp->ca, ssp->winner); } } } // All the rest is cleanup ... ca_clear_pa(ssp->ca); _shop_ptx_destroy(ssp->ptx_dict); return rc; }
static shop_e _shop_collect_targets(shopping_state_s *ssp, CCS cmdix) { char key[64]; struct cdb_find cdbf_tgt; unsigned vlen; shop_e rc; rc = SHOP_RECYCLED; vb_printf(VB_SHOP, "COLLECTING: [%s]", cmdix); // First we collect all targets of the winning command in the // "real" CA object .... snprintf(key, charlen(key), ">%s", cmdix); if (cdb_findinit(&cdbf_tgt, ssp->cdbp, key, strlen(key)) < 0) { putil_die("cdb_findinit: %s (%s)", key, ca_get_line(ssp->ca)); } while (cdb_findnext(&cdbf_tgt) > 0) { CS buf, pskeys, csv, ptxes, ixstr; CCS pskey; vlen = cdb_datalen(ssp->cdbp); buf = (CS)alloca(vlen + 1); cdb_read(ssp->cdbp, buf, vlen, cdb_datapos(ssp->cdbp)); buf[vlen] = '\0'; if (!(ptxes = strstr(buf, FS1))) { putil_int("bad format in roadmap: %s", buf); rc = SHOP_ERR; continue; } *ptxes++ = '\0'; pskeys = buf; // Make sure to skip all targets not associated with the winning PTX. for (ixstr = util_strsep(&ptxes, FS2); ixstr; ixstr = util_strsep(&ptxes, FS2)) { if (!strcmp(ixstr, ssp->wix)) { ps_o tgt_ps; pa_o dummy_pa; for (pskey = util_strsep(&pskeys, FS2); pskey && rc == SHOP_RECYCLED; pskey = util_strsep(&pskeys, FS2)) { if (cdb_find(ssp->cdbp, pskey, strlen(pskey)) <= 0) { putil_int("bad key in roadmap: %s", pskey); continue; } vlen = cdb_datalen(ssp->cdbp); csv = (CS)alloca(vlen + 1); cdb_read(ssp->cdbp, csv, vlen, cdb_datapos(ssp->cdbp)); csv[vlen] = '\0'; vb_printf(VB_SHOP, "COLLECTED [%s] %s", pskey, csv); tgt_ps = ps_newFromCSVString(csv); // Unfortunately, as noted elsewhere, a CA object // doesn't hold PS objects, it holds PA objects. // So we need a dummy PA to wrap the target PS in. dummy_pa = pa_new(); pa_set_ps(dummy_pa, tgt_ps); if (ps_is_link(tgt_ps)) { pa_set_op(dummy_pa, OP_LINK); } else if (ps_is_symlink(tgt_ps)) { pa_set_op(dummy_pa, OP_SYMLINK); } else if (ps_is_unlink(tgt_ps)) { pa_set_op(dummy_pa, OP_UNLINK); } else { pa_set_op(dummy_pa, OP_CREAT); } pa_set_call(dummy_pa, "dummy"); pa_set_uploadable(dummy_pa, 1); ca_record_pa(ssp->ca, dummy_pa); } // Once we've reached the winner, we're done. break; } } } return rc; }
static void _shop_compare_prereqs(shopping_state_s *ssp, CCS cmdix) { char key[64]; struct cdb_find cdbf_prq; unsigned vlen; snprintf(key, charlen(key), "<%s", cmdix); if (cdb_findinit(&cdbf_prq, ssp->cdbp, key, strlen(key)) < 0) { putil_die("cdb_findinit: %s (%s)", key, ca_get_line(ssp->ca)); } while (cdb_findnext(&cdbf_prq) > 0 && _shop_ptx_count(ssp)) { CCS pskey; CS pskeys, prqline, ptxes1, ptxbuf, ixstr; int ptxesleft; // Read a prereq line from the roadmap. vlen = cdb_datalen(ssp->cdbp); prqline = (CS)alloca(vlen + 1); cdb_read(ssp->cdbp, prqline, vlen, cdb_datapos(ssp->cdbp)); prqline[vlen] = '\0'; // Split the line into a list of path states and a list of ptxes. if (!(ptxes1 = strstr(prqline, FS1))) { putil_int("bad format in roadmap: %s", prqline); break; } *ptxes1++ = '\0'; pskeys = prqline; // Here we make a temporary copy of the ptx list and take a // quick run through it. If none of the listed ptxes remain // eligible, we can skip evaluating this set of path states. // On the other hand, if we do go on to evaluate any of these // ptxes, mark them as having been evaluated. In order to // be a winner you must not only survive the war but also // show evidence of having fought. No one gets a medal for // hiding in a foxhole, so to speak. ptxbuf = (CS)alloca(strlen(ptxes1) + CHARSIZE); strcpy(ptxbuf, ptxes1); for (ptxesleft = 0, ixstr = util_strsep(&ptxbuf, FS2); ixstr; ixstr = util_strsep(&ptxbuf, FS2)) { if (_shop_ptx_contains(ssp, ixstr)) { ptxesleft = 1; _shop_ptx_mark_as_seen(ssp, ixstr); } } if (!ptxesleft) { continue; } /* * Evaluate individual path states. The format allows for * pathstate keys to be enumerated, like S1+S2+S3+S4, or * for ranges in the form "S1-4". */ for (pskey = util_strsep(&pskeys, FS2); pskey && _shop_ptx_count(ssp); pskey = util_strsep(&pskeys, FS2)) { CS end; if ((end = strchr(pskey, '-'))) { uint64_t i, first, last; char nkey[64], *p; // The following relies on the fact that Integer.toString // is guaranteed to use lower-case characters. *end++ = '\0'; for (p = nkey; ISALPHA(*pskey) && ISUPPER(*pskey); ) { *p++ = *pskey++; } first = strtol(pskey, NULL, RMAP_RADIX); last = strtol(end, NULL, RMAP_RADIX); for (i = first; i <= last; i++) { util_format_to_radix(RMAP_RADIX, p, charlen(nkey), i); _shop_cmp_pathstate(ssp, nkey, ptxes1); } } else { _shop_cmp_pathstate(ssp, pskey, ptxes1); } } } ps_destroy(CurrentPS); CurrentPS = NULL; }
int cdb_find(struct cdb *c,char *key,unsigned int len) { cdb_findstart(c); return cdb_findnext(c,key,len); }
void doaxfr (char id[2]) { int r = 0; char num[4]; char key[512]; uint32 klen = 0; uint32 eod = 0, pos = 0; axfrcheck (zone); tai_now (&now); cdb_init (&c, fdcdb); byte_zero (clientloc, 2); key[0] = 0; key[1] = '%'; byte_copy (key + 2, 4, ip); r = cdb_find (&c, key, 6); if (!r) r = cdb_find (&c, key, 5); if (!r) r = cdb_find (&c, key, 4); if (!r) r = cdb_find (&c, key, 3); if (!r) r = cdb_find (&c, key, 2); if (r == -1) errx (-1, "could not read from file `data.cdb'"); if (r && (cdb_datalen (&c) == 2)) if (cdb_read (&c, clientloc, 2, cdb_datapos (&c)) == -1) err (-1, "could not read from file `data.cdb'"); cdb_findstart (&c); for (;;) { r = cdb_findnext (&c, zone, zonelen); if (r == -1) errx (-1, "could not read from file `data.cdb'"); if (!r) errx (-1, "could not find information in `data.cdb'"); dlen = cdb_datalen (&c); if (dlen > sizeof data) errx (-1, "could not read from file `data.cdb': format error"); if (cdb_read (&c, data, dlen, cdb_datapos (&c)) == -1) errx (-1, "could not read from file `data.cdb': format error"); if (build (&soa, zone, 1, id)) break; } cdb_free (&c); print (soa.s, soa.len); seek_begin (fdcdb); buffer_init (&bcdb, buffer_unixread, fdcdb, bcdbspace, sizeof (bcdbspace)); pos = 0; get (num, 4); pos += 4; uint32_unpack (num, &eod); while (pos < 2048) { get (num, 4); pos += 4; } while (pos < eod) { if (eod - pos < 8) errx (-1, "could not read from file `data.cdb': format error"); get (num, 4); pos += 4; uint32_unpack (num, &klen); get (num,4); pos += 4; uint32_unpack (num, &dlen); if (eod - pos < klen) errx (-1, "could not read from file `data.cdb': format error"); pos += klen; if (eod - pos < dlen) errx (-1, "could not read from file `data.cdb': format error"); pos += dlen; if (klen > sizeof key) errx (-1, "could not read from file `data.cdb': format error"); get (key, klen); if (dlen > sizeof data) errx (-1, "could not read from file `data.cdb': format error"); get (data, dlen); if ((klen > 1) && (key[0] == 0)) continue; /* location */ if (klen < 1) errx (-1, "could not read from file `data.cdb': format error"); if (dns_packet_getname (key, klen, 0, &q) != klen) errx (-1, "could not read from file `data.cdb': format error"); if (!dns_domain_suffix (q, zone)) continue; if (!build (&message, q, 0, id)) continue; print (message.s, message.len); } print (soa.s, soa.len); }
int cdb_find(struct cdb *c,char *key,off_t len) { cdb_findstart(c); return cdb_findnext(c,key,len); }