123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223 |
- /* This file is part of Netsukuku
- * (c) Copyright 2005 Andrea Lo Pumo aka AlpT <alpt@freaknet.org>
- *
- * This source code is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License as published
- * by the Free Software Foundation; either version 2 of the License,
- * or (at your option) any later version.
- *
- * This source code is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
- * Please refer to the GNU Public License for more details.
- *
- * You should have received a copy of the GNU Public License along with
- * this source code; if not, write to:
- * Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
- *
- * --
- * andna_cache.c:
- * Functions to manipulate all the andna's caches.
- */
-
- #include "includes.h"
-
- #include "crypto.h"
- #include "andna_cache.h"
- #include "snsd_cache.h"
- #include "common.h"
- #include "hash.h"
-
-
- int net_family;
-
- void
- andna_caches_init(int family)
- {
- net_family = family;
-
- setzero(&lcl_keyring, sizeof(lcl_keyring));
-
- andna_lcl = (lcl_cache *) clist_init(&lcl_counter);
- andna_c = (andna_cache *) clist_init(&andna_c_counter);
- andna_counter_c = (counter_c *) clist_init(&cc_counter);
- andna_rhc = (rh_cache *) clist_init(&rhc_counter);
- }
-
- /*
- * andna_32bit_hash
- *
- * It returns the 32bit hash of the md5 hash of the `hname' string.
- */
- u_int
- andna_32bit_hash(char *hname)
- {
- u_char hashm5[ANDNA_HASH_SZ];
-
- hash_md5((u_char *) hname, strlen(hname), hashm5);
- return fnv_32_buf(hashm5, ANDNA_HASH_SZ, FNV1_32_INIT);
- }
-
- /*
- *
- * * * * Local Cache functions * * *
- *
- */
-
- /*
- * lcl_new_keyring
- *
- * It generates a new keyring.
- */
- void
- lcl_new_keyring(lcl_cache_keyring * keyring)
- {
- setzero(keyring, sizeof(lcl_cache_keyring));
- loginfo("Generating a new ANDNA keyring");
-
- /* Generate the new key pair for the first time */
- keyring->priv_rsa = genrsa(ANDNA_PRIVKEY_BITS, &keyring->pubkey,
- &keyring->pkey_len, &keyring->privkey,
- &keyring->skey_len);
- }
-
- /*
- * lcl_destroy_keyring
- *
- * destroys accurately the keyring ^_^
- */
- void
- lcl_destroy_keyring(lcl_cache_keyring * keyring)
- {
- if (keyring->priv_rsa)
- RSA_free(keyring->priv_rsa);
- if (keyring->pubkey)
- xfree(keyring->pubkey);
- if (keyring->privkey)
- xfree(keyring->privkey);
-
- setzero(keyring, sizeof(lcl_cache_keyring));
- }
-
- /*
- * lcl_cache_new: builds a new lcl_cache generating a new rsa key pair and
- * setting the hostname in the struct
- */
- lcl_cache *
- lcl_cache_new(char *hname)
- {
- lcl_cache *alcl;
-
- alcl = (lcl_cache *) xzalloc(sizeof(lcl_cache));
-
- alcl->hostname = xstrdup(hname);
- alcl->hash = andna_32bit_hash(hname);
-
- return alcl;
- }
-
- void
- lcl_cache_free(lcl_cache * alcl)
- {
- if (alcl->hostname)
- xfree(alcl->hostname);
- alcl->snsd_counter = 0;
- if (alcl->service)
- snsd_service_llist_del(&alcl->service);
- }
-
- void
- lcl_cache_destroy(lcl_cache * head, int *counter)
- {
- lcl_cache *alcl = head, *next;
-
- if (!alcl || !lcl_counter)
- return;
-
- list_safe_for(alcl, next) {
- lcl_cache_free(alcl);
- xfree(alcl);
- }
- *counter = 0;
- }
-
- lcl_cache *
- lcl_cache_find_hname(lcl_cache * alcl, char *hname)
- {
- u_int hash;
-
- if (!alcl || !lcl_counter)
- return 0;
-
- hash = andna_32bit_hash(hname);
- list_for(alcl)
- if (alcl->hash == hash && alcl->hostname &&
- !strncmp(alcl->hostname, hname, ANDNA_MAX_HNAME_LEN))
- return alcl;
- return 0;
- }
-
- lcl_cache *
- lcl_cache_find_hash(lcl_cache * alcl, u_int hash)
- {
- if (!alcl || !lcl_counter)
- return 0;
-
- list_for(alcl)
- if (alcl->hash == hash && alcl->hostname)
- return alcl;
- return 0;
- }
-
- int
- is_lcl_hname_registered(lcl_cache * alcl)
- {
- return alcl->timestamp;
- }
-
- /*
- * lcl_get_registered_hnames
- *
- * It returns a duplicated lcl_cache of `alcl', which contains only
- * hostnames already registered.
- * Note that the structs present in the returned cache are in a different
- * mallocated space, so you should free them.
- */
- lcl_cache *
- lcl_get_registered_hnames(lcl_cache * alcl)
- {
- lcl_cache *lcl;
-
- lcl = list_copy_some(alcl, is_lcl_hname_registered);
- list_for(lcl) {
- lcl->hostname = xstrdup(lcl->hostname);
- lcl->service = snsd_service_llist_copy(lcl->service,
- SNSD_ALL_SERVICE, 0);
- }
-
- return lcl;
- }
-
- /*
- *
- * * * * Andna Cache functions * * *
- *
- */
-
- andna_cache_queue *
- ac_queue_findpubk(andna_cache * ac, char *pubk)
- {
- andna_cache_queue *acq = ac->acq;
-
- if (!acq)
- return 0;
- list_for(acq)
- if (!memcmp(acq->pubkey, pubk, ANDNA_PKEY_LEN))
- return acq;
- return 0;
- }
-
- /*
- * ac_queue_add
- *
- * adds a new entry in the andna cache queue, which is `ac'->acq.
- * The elements in the new `ac'->acq are updated.
- * If an `ac'->acq struct with an `ac'->acq->pubkey equal to `pubkey' already
- * exists, then only the timestamp and the IP will be updated.
- *
- * It returns the pointer to the acq struct. If it isn't possible to add a new
- * entry in the queue, 0 will be returned.
- *
- * Remember to update the acq->timestamp value after this call.
- */
- andna_cache_queue *
- ac_queue_add(andna_cache * ac, char *pubkey)
- {
- andna_cache_queue *acq;
-
- /*
- * This call is not necessary because it's already done by
- * andna_cache_del_expired().
- * * ac_queue_del_expired(ac); * *
- */
-
- if (!(acq = ac_queue_findpubk(ac, pubkey))) {
- if (ac->queue_counter >= ANDNA_MAX_QUEUE || ac->flags & ANDNA_FULL)
- return 0;
-
- acq = xzalloc(sizeof(andna_cache_queue));
- memcpy(acq->pubkey, pubkey, ANDNA_PKEY_LEN);
- clist_append(&ac->acq, 0, &ac->queue_counter, acq);
- }
-
-
- if (ac->queue_counter >= ANDNA_MAX_QUEUE)
- ac->flags |= ANDNA_FULL;
-
- return acq;
- }
-
- void
- ac_queue_del(andna_cache * ac, andna_cache_queue * acq)
- {
-
- acq->snsd_counter = 0;
- if (acq->service)
- snsd_service_llist_del(&acq->service);
- clist_del(&ac->acq, &ac->queue_counter, acq);
- ac->flags &= ~ANDNA_FULL;
- }
-
- /*
- * ac_queue_del_expired: removes the expired entries from the
- * andna_cache_queue `ac'->acq.
- */
- void
- ac_queue_del_expired(andna_cache * ac)
- {
- andna_cache_queue *acq, *next;
- time_t cur_t;
-
- if (!ac || !ac->acq)
- return;
-
- cur_t = time(0);
- acq = ac->acq;
- list_safe_for(acq, next)
- if (cur_t - acq->timestamp > ANDNA_EXPIRATION_TIME)
- ac_queue_del(ac, acq);
- }
-
- /*
- * ac_queue_destroy: destroys an andna_cache_queue
- */
- void
- ac_queue_destroy(andna_cache * ac)
- {
- andna_cache_queue *acq, *next;
-
- if (!ac || !ac->acq)
- return;
-
- acq = ac->acq;
- list_safe_for(acq, next)
- ac_queue_del(ac, acq);
- }
-
- andna_cache *
- andna_cache_findhash(int hash[MAX_IP_INT])
- {
- andna_cache *ac = andna_c;
-
- if (!andna_c_counter)
- return 0;
-
- list_for(ac)
- if (!memcmp(ac->hash, hash, ANDNA_HASH_SZ))
- return ac;
- return 0;
- }
-
- /*
- * andna_cache_gethash
- *
- * It searches an andna_cache entry which has the same hash of `hash'.
- * If it found but this entry is expired, it is deleted from the cache and 0 is
- * returned.
- * If it isn't found 0 is returned, otherwise a pointer to the entry is
- * returned.
- */
- andna_cache *
- andna_cache_gethash(int hash[MAX_IP_INT])
- {
- andna_cache *ac;
-
- ac = andna_cache_findhash(hash);
- if (ac && andna_cache_del_ifexpired(ac))
- return 0;
-
- return ac;
- }
-
- andna_cache *
- andna_cache_addhash(int hash[MAX_IP_INT])
- {
- andna_cache *ac;
-
- andna_cache_del_expired();
-
- if (!(ac = andna_cache_findhash(hash))) {
- ac = xzalloc(sizeof(andna_cache));
- memcpy(ac->hash, hash, ANDNA_HASH_SZ);
-
- clist_add(&andna_c, &andna_c_counter, ac);
- }
-
- return ac;
- }
-
- /*
- * andna_cache_del_ifexpired
- *
- * If `ac' is expired, it deletes it and returns 1; otherwise 0 is returned.
- */
- int
- andna_cache_del_ifexpired(andna_cache * ac)
- {
- ac_queue_del_expired(ac);
-
- if (!ac->queue_counter) {
- clist_del(&andna_c, &andna_c_counter, ac);
- return 1;
- }
-
- return 0;
- }
-
- void
- andna_cache_del_expired(void)
- {
- andna_cache *ac = andna_c, *next;
-
- if (!andna_c_counter)
- return;
-
- list_safe_for(ac, next)
- andna_cache_del_ifexpired(ac);
- }
-
- /*
- * andna_cache_destroy
- *
- * destroys the andna_c llist
- */
- void
- andna_cache_destroy(void)
- {
- andna_cache *ac = andna_c, *next;
-
- if (!andna_c_counter)
- return;
-
- list_safe_for(ac, next) {
- ac_queue_destroy(ac);
- clist_del(&andna_c, &andna_c_counter, ac);
- }
- }
-
-
- /*
- *
- * * * * Counter Cache functions * * *
- *
- */
-
- /*
- * Remeber to update the cch->timestamp value after this call.
- */
- counter_c_hashes *
- cc_hashes_add(counter_c * cc, int hash[MAX_IP_INT])
- {
- counter_c_hashes *cch;
-
- /* The purge is already done in counter_c_del_expired(), so it is not
- * necessary to call it here.
- * * cc_hashes_del_expired(cc); * *
- */
-
- if (!(cch = cc_findhash(cc, hash))) {
- if (cc->hashes >= ANDNA_MAX_HOSTNAMES || cc->flags & ANDNA_FULL)
- return 0;
-
- cch = xzalloc(sizeof(counter_c_hashes));
- memcpy(cch->hash, hash, ANDNA_HASH_SZ);
-
- clist_add(&cc->cch, &cc->hashes, cch);
- }
-
- if (cc->hashes >= ANDNA_MAX_HOSTNAMES)
- cc->flags |= ANDNA_FULL;
-
- return cch;
- }
-
- void
- cc_hashes_del(counter_c * cc, counter_c_hashes * cch)
- {
- clist_del(&cc->cch, &cc->hashes, cch);
- cc->flags &= ~ANDNA_FULL;
- }
-
- void
- cc_hashes_del_expired(counter_c * cc)
- {
- counter_c_hashes *cch, *next;
- time_t cur_t;
-
- if (!cc || !cc->cch || !cc->hashes)
- return;
-
- cur_t = time(0);
- cch = cc->cch;
-
- list_safe_for(cch, next)
- if (cur_t - cch->timestamp > ANDNA_EXPIRATION_TIME)
- cc_hashes_del(cc, cch);
- }
-
- void
- cc_hashes_destroy(counter_c * cc)
- {
- counter_c_hashes *cch, *next;
-
- if (!cc || !cc->cch || !cc->hashes)
- return;
-
- cch = cc->cch;
- list_safe_for(cch, next)
- cc_hashes_del(cc, cch);
- }
-
- counter_c_hashes *
- cc_findhash(counter_c * cc, int hash[MAX_IP_INT])
- {
- counter_c_hashes *cch = cc->cch;
-
- if (!cc->hashes || !cch)
- return 0;
-
- list_for(cch)
- if (!memcmp(cch->hash, hash, ANDNA_HASH_SZ))
- return cch;
- return 0;
- }
-
- counter_c *
- counter_c_findpubk(char *pubk)
- {
- counter_c *cc = andna_counter_c;
-
- if (!cc_counter || !cc)
- return 0;
-
- list_for(cc)
- if (!memcmp(&cc->pubkey, pubk, ANDNA_PKEY_LEN))
- return cc;
- return 0;
- }
-
- counter_c *
- counter_c_add(inet_prefix * rip, char *pubkey)
- {
- counter_c *cc;
-
- counter_c_del_expired();
-
- if (!(cc = counter_c_findpubk(pubkey))) {
- cc = xzalloc(sizeof(counter_c));
-
- memcpy(cc->pubkey, pubkey, ANDNA_PKEY_LEN);
- clist_add(&andna_counter_c, &cc_counter, cc);
- }
-
- return cc;
- }
-
- void
- counter_c_del_expired(void)
- {
- counter_c *cc = andna_counter_c, *next;
-
- if (!cc)
- return;
-
- list_safe_for(cc, next) {
- cc_hashes_del_expired(cc);
- if (!cc->hashes)
- clist_del(&andna_counter_c, &cc_counter, cc);
- }
- }
-
- /*
- * counter_c_destroy
- *
- * destroy the andna_counter_c llist
- */
- void
- counter_c_destroy(void)
- {
- counter_c *cc = andna_counter_c, *next;
-
- if (!cc)
- return;
-
- list_safe_for(cc, next) {
- cc_hashes_destroy(cc);
- clist_del(&andna_counter_c, &cc_counter, cc);
- }
- }
-
- /*
- *
- * * * * Resolved hostnames cache functions * * *
- *
- */
-
- rh_cache *
- rh_cache_new_hash(u_int hash, time_t timestamp)
- {
- rh_cache *rhc;
-
- rhc = xzalloc(sizeof(rh_cache));
- rhc->hash = hash;
- rhc->timestamp = timestamp;
-
- return rhc;
- }
-
- rh_cache *
- rh_cache_new(char *hname, time_t timestamp)
- {
- return rh_cache_new_hash(andna_32bit_hash(hname), timestamp);
- }
-
- /*
- * rh_cache_add_hash
- *
- * It searches a struct in the rh_cache which has the hash value equal to
- * `hash'.
- * If it isn't found a new one is added. In both cases the pointer to the
- * struct will be returned.
- *
- * On error 0 is returned.
- */
- rh_cache *
- rh_cache_add_hash(u_int hash, time_t timestamp)
- {
- rh_cache *rhc;
-
- if (!(rhc = rh_cache_find_hash(hash))) {
- if (rhc_counter >= ANDNA_MAX_HOSTNAMES) {
- /* Delete the expired hnames and see if there's empty
- * space */
- rh_cache_del_expired();
-
- if (rhc_counter >= ANDNA_MAX_HOSTNAMES) {
- /* Delete the oldest struct in cache */
- rhc = (rh_cache *)list_last(andna_rhc);
- clist_del(&andna_rhc, &rhc_counter, rhc);
- }
- }
-
- rhc = rh_cache_new_hash(hash, timestamp);
- clist_add(&andna_rhc, &rhc_counter, rhc);
- }
-
- rhc->timestamp = timestamp;
-
- return rhc;
- }
-
- /*
- * rh_cache_add
- *
- * It searches a struct in the rh_cache which is associated to `hname'.
- * If it isn't found a new one is added. In both cases the pointer to the
- * struct will be returned.
- *
- * On error 0 is returned.
- */
- rh_cache *
- rh_cache_add(char *hname, time_t timestamp)
- {
- return rh_cache_add_hash(andna_32bit_hash(hname), timestamp);
- }
-
- rh_cache *
- rh_cache_find_hash(u_int hash)
- {
- rh_cache *rhc = andna_rhc, *next;
- time_t cur_t;
-
- if (!rhc || !rhc_counter)
- return 0;
-
- cur_t = time(0);
-
- list_safe_for(rhc, next)
- if (rhc->hash == hash) {
- if (cur_t - rhc->timestamp > ANDNA_EXPIRATION_TIME) {
- /* This hostname expired, delete it from the
- * cache */
- rh_cache_del(rhc);
- continue;
- } else
- /* Each time we find a hname in the rh_cache,
- * we move it on top of the llist. */
- andna_rhc = list_moveontop(andna_rhc, rhc);
- return rhc;
- }
- return 0;
- }
-
- rh_cache *
- rh_cache_find_hname(char *hname)
- {
- u_int hash;
-
- hash = andna_32bit_hash(hname);
- return rh_cache_find_hash(hash);
- }
-
- void
- rh_cache_del(rh_cache * rhc)
- {
- rhc->snsd_counter = 0;
- if (rhc->service)
- snsd_service_llist_del(&rhc->service);
-
- clist_del(&andna_rhc, &rhc_counter, rhc);
- }
-
- void
- rh_cache_del_expired(void)
- {
- rh_cache *rhc = andna_rhc, *next;
- time_t cur_t;
-
- if (!rhc || !rhc_counter)
- return;
-
- cur_t = time(0);
-
- list_safe_for(rhc, next)
- if (cur_t - rhc->timestamp > ANDNA_EXPIRATION_TIME)
- rh_cache_del(rhc);
- }
-
- void
- rh_cache_flush(void)
- {
- rh_cache *rhc = andna_rhc, *next;
-
- list_safe_for(rhc, next)
- rh_cache_del(rhc);
- }
-
- /*
- *
- * * * * Pack/Unpack functions * * *
- *
- */
-
- char *
- pack_lcl_keyring(lcl_cache_keyring * keyring, size_t * pack_sz)
- {
- struct lcl_keyring_pkt_hdr key_hdr;
- size_t sz;
- char *pack, *buf;
-
- key_hdr.skey_len = keyring->skey_len;
- key_hdr.pkey_len = keyring->pkey_len;
- sz = LCL_KEYRING_HDR_PACK_SZ(&key_hdr);
-
- pack = buf = xmalloc(sz);
- bufput(&key_hdr, sizeof(struct lcl_keyring_pkt_hdr));
- ints_host_to_network(pack, lcl_keyring_pkt_hdr_iinfo);
-
- bufput(keyring->privkey, keyring->skey_len);
- bufput(keyring->pubkey, keyring->pkey_len);
-
- *pack_sz = sz;
- return pack;
- }
-
- /*
- * unpack_lcl_keyring: unpacks a lcl keyring. On error it returns -1.
- * In `keyring' it restores the packed keys.
- */
- int
- unpack_lcl_keyring(lcl_cache_keyring * keyring, char *pack, size_t pack_sz)
- {
- struct lcl_keyring_pkt_hdr *hdr;
- char *buf;
- u_char *pk;
-
-
- hdr = (struct lcl_keyring_pkt_hdr *) pack;
- ints_network_to_host(hdr, lcl_keyring_pkt_hdr_iinfo);
-
- /*
- * Restore the keyring
- */
- keyring->skey_len = hdr->skey_len;
- keyring->pkey_len = hdr->pkey_len;
- if (keyring->skey_len > ANDNA_SKEY_MAX_LEN) {
- error(ERROR_MSG "Invalid keyring header", ERROR_FUNC);
- return -1;
- }
-
- keyring->privkey = xmalloc(hdr->skey_len);
- keyring->pubkey = xmalloc(hdr->pkey_len);
-
- /* extract the private key */
- buf = pack + sizeof(struct lcl_keyring_pkt_hdr);
- bufget(keyring->privkey, hdr->skey_len);
-
- /* public key */
- bufget(keyring->pubkey, hdr->pkey_len);
-
- pk = keyring->privkey;
- if (!(keyring->priv_rsa = get_rsa_priv((const u_char **) &pk,
- keyring->skey_len))) {
- error(ERROR_MSG "Cannot unpack the priv key from the"
- " lcl_pack: %s", ERROR_POS, ssl_strerr());
- return -1;
- }
-
- return 0;
- }
-
- /*
- * pack_lcl_cache
- *
- * packs the entire local cache linked list that starts with the head
- * `local_cache'. The size of the pack is stored in `pack_sz'.
- * The pointer to the newly allocated pack is returned.
- * Note that the pack is in network byte order.
- */
- char *
- pack_lcl_cache(lcl_cache * local_cache, size_t * pack_sz)
- {
- struct lcl_cache_pkt_hdr lcl_hdr;
- lcl_cache *alcl = local_cache;
- size_t sz = 0, slen;
- char *pack, *buf, *body;
-
- lcl_hdr.tot_caches = 0;
- sz = LCL_CACHE_HDR_PACK_SZ;
-
- /* Calculate the final pack size */
- list_for(alcl) {
- sz += LCL_CACHE_BODY_PACK_SZ(strlen(alcl->hostname) + 1);
- lcl_hdr.tot_caches++;
- }
-
- pack = buf = xmalloc(sz);
- bufput(&lcl_hdr, sizeof(struct lcl_cache_pkt_hdr));
- ints_host_to_network(pack, lcl_cache_pkt_hdr_iinfo);
-
- *pack_sz = 0;
- if (lcl_hdr.tot_caches) {
- alcl = local_cache;
-
- list_for(alcl) {
- body = buf;
-
- bufput(&alcl->hname_updates, sizeof(u_short));
- bufput(&alcl->timestamp, sizeof(time_t));
-
- slen = strlen(alcl->hostname) + 1;
- bufput(alcl->hostname, slen);
-
- ints_host_to_network(body, lcl_cache_pkt_body_iinfo);
- }
- }
-
- *pack_sz = sz;
- return pack;
- }
-
- /*
- * unpack_lcl_cache
- *
- * Unpacks a packed local cache linked list and returns its head.
- * `counter' is set to the number of struct in the llist.
- *
- * On error 0 is returned and `*counter' is set to -1.
- *
- * Note: `pack' is modified during the unpacking.
- */
- lcl_cache *
- unpack_lcl_cache(char *pack, size_t pack_sz, int *counter)
- {
- struct lcl_cache_pkt_hdr *hdr;
- lcl_cache *alcl, *alcl_head = 0;
- char *buf;
- size_t slen, unpacked_sz;
- int i = 0;
-
- hdr = (struct lcl_cache_pkt_hdr *) pack;
- buf = pack + sizeof(struct lcl_cache_pkt_hdr);
- unpacked_sz = sizeof(struct lcl_cache_pkt_hdr);
- ints_network_to_host(hdr, lcl_cache_pkt_hdr_iinfo);
- *counter = 0;
-
- if (hdr->tot_caches > ANDNA_MAX_HOSTNAMES)
- ERROR_FINISH(*counter, -1, finish);
-
- *counter = 0;
- if (hdr->tot_caches) {
- for (i = 0; i < hdr->tot_caches; i++) {
- unpacked_sz += LCL_CACHE_BODY_PACK_SZ(0);
- if (unpacked_sz > pack_sz)
- ERROR_FINISH(*counter, -1, finish);
-
- slen = strlen(buf + sizeof(u_short) + sizeof(time_t)) + 1;
- if (slen > ANDNA_MAX_HNAME_LEN ||
- (unpacked_sz += slen) > pack_sz)
- ERROR_FINISH(*counter, -1, finish);
-
- ints_network_to_host(buf, lcl_cache_pkt_body_iinfo);
-
- alcl = xzalloc(sizeof(lcl_cache));
-
- bufget(&alcl->hname_updates, sizeof(u_short));
- bufget(&alcl->timestamp, sizeof(time_t));
-
- alcl->hostname = xstrdup(buf);
- alcl->hash = andna_32bit_hash(alcl->hostname);
- buf += slen;
-
- clist_add(&alcl_head, counter, alcl);
- }
- }
-
- finish:
- return alcl_head;
- }
-
- /*
- * pack_andna_cache_queue
- *
- * It packs an andna_cache_queue struct. The package is stored in `pack' which
- * has `tot_pack_sz' allocated bytes.
- * `acq' is the struct which will be packed.
- * `pack_type' is equal to ACACHE_PACK_FILE or ACACHE_PACK_PKT, it specify if
- * the package will be stored in a file or will be sent over a network.
- *
- * The number of bytes written in `pack' is returned.
- */
- int
- pack_andna_cache_queue(char *pack, size_t tot_pack_sz,
- andna_cache_queue * acq, int pack_type)
- {
- char *buf = pack;
- u_int t;
- int pack_sz = 0;
-
- if (pack_type == ACACHE_PACK_PKT)
- t = time(0) - acq->timestamp;
- else
- t = acq->timestamp;
-
- bufput(&t, sizeof(uint32_t));
- bufput(&acq->hname_updates, sizeof(u_short));
- bufput(&acq->pubkey, ANDNA_PKEY_LEN);
- bufput(&acq->snsd_counter, sizeof(u_short));
-
- pack_sz += ACQ_BODY_PACK_SZ;
- ints_host_to_network(pack, acq_body_iinfo);
-
- pack_sz += snsd_pack_all_services(buf, tot_pack_sz, acq->service);
-
- return pack_sz;
- }
-
- /*
- * pack_single_andna_cache
- *
- * It packs an andna_cache struct. The package is stored in `pack' which
- * has `tot_pack_sz' allocated bytes.
- * `ac' is the struct which will be packed.
- * `pack_type' is equal to ACACHE_PACK_FILE or ACACHE_PACK_PKT, it specify if
- * the package will be stored in a file or will be sent over a network.
- *
- * The number of bytes written in `pack' is returned.
- */
- int
- pack_single_andna_cache(char *pack, size_t tot_pack_sz,
- andna_cache * ac, int pack_type)
- {
- andna_cache_queue *acq;
- char *buf = pack;
- int pack_sz = 0;
- size_t psz;
-
- bufput(ac->hash, ANDNA_HASH_SZ);
- bufput(&ac->flags, sizeof(char));
- bufput(&ac->queue_counter, sizeof(u_short));
-
- pack_sz += ACACHE_BODY_PACK_SZ;
- ints_host_to_network(pack, andna_cache_body_iinfo);
-
- acq = ac->acq;
- list_for(acq) {
- psz = pack_andna_cache_queue(buf, tot_pack_sz, acq, pack_type);
- buf += psz;
- pack_sz += psz;
- tot_pack_sz -= psz;
- }
-
- return pack_sz;
- }
-
- /*
- * pack_andna_cache
- *
- * It packs the entire andna cache linked list that starts with
- * the head `acache'.
- * The size of the pack is stored in `pack_sz'.
- * `pack_type' specifies if the package will be saved in a file or sent over
- * the net, it is equal to ACACHE_PACK_FILE or to ACACHE_PACK_PKT.
- *
- * The pointer to the newly allocated pack is returned.
- * The pack is written in network order.
- */
- char *
- pack_andna_cache(andna_cache * acache, size_t * pack_sz, int pack_type)
- {
- struct andna_cache_pkt_hdr hdr;
- andna_cache *ac = acache;
- andna_cache_queue *acq;
- char *pack, *buf;
- size_t sz, free_sz, acq_sz, service_sz, psz;
-
- /* Calculate the pack size */
- ac = acache;
- hdr.tot_caches = 0;
- sz = sizeof(struct andna_cache_pkt_hdr);
- list_for(ac) {
- acq = ac->acq;
- acq_sz = 0;
- list_for(acq) {
- service_sz = SNSD_SERVICE_LLIST_PACK_SZ(acq->service);
- acq_sz = ACQ_PACK_SZ(service_sz);
- }
- sz += ACACHE_PACK_SZ(acq_sz);
- hdr.tot_caches++;
- }
-
-
- free_sz = sz;
- buf = pack = xmalloc(sz);
-
- /* Write the header of the package */
- bufput(&hdr, sizeof(struct andna_cache_pkt_hdr));
- free_sz -= sizeof(struct andna_cache_pkt_hdr);
-
- ints_host_to_network(pack, andna_cache_pkt_hdr_iinfo);
-
- if (!hdr.tot_caches)
- goto finish;
-
- /* Pack the rest of the andna_cache */
- ac = acache;
- list_for(ac) {
- psz = pack_single_andna_cache(buf, free_sz, ac, pack_type);
- buf += psz;
- free_sz -= psz;
- }
-
- finish:
- *pack_sz = sz;
- return pack;
- }
-
- /*
- * unpack_acq_llist
- *
- * ac->queue_counter must contain the number of acq structs contained in the
- * package.
- *
- * `*unpacked_sz' is incremented by the number of unpacked bytes.
- *
- * `pack_type' specifies if the package will be saved in a file or sent over
- * the net, it is equal to ACACHE_PACK_FILE or to ACACHE_PACK_PKT.
- */
- andna_cache_queue *
- unpack_acq_llist(char *pack, size_t pack_sz, size_t * unpacked_sz,
- andna_cache * ac, int pack_type)
- {
- andna_cache_queue *acq = 0;
- int e, tmp_counter = 0;
- u_short snsd_counter;
- time_t cur_t;
- char *buf;
-
- cur_t = time(0);
- buf = pack;
- for (e = 0; e < ac->queue_counter; e++) {
- acq = xzalloc(sizeof(andna_cache_queue));
-
- ints_network_to_host(buf, acq_body_iinfo);
-
- bufget(&acq->timestamp, sizeof(uint32_t));
- if (pack_type == ACACHE_PACK_PKT)
- acq->timestamp = cur_t - acq->timestamp;
-
- bufget(&acq->hname_updates, sizeof(u_short));
- bufget(&acq->pubkey, ANDNA_PKEY_LEN);
- bufget(&acq->snsd_counter, sizeof(u_short));
-
- pack_sz -= ACACHE_BODY_PACK_SZ;
- (*unpacked_sz) += ACACHE_BODY_PACK_SZ;
- acq->service = snsd_unpack_all_service(buf, pack_sz, unpacked_sz,
- &snsd_counter);
- if (acq->snsd_counter != snsd_counter) {
- debug(DBG_SOFT, ERROR_MSG "unpack_acq:"
- "snsd_counter (%h) != snsd_counter (%h)",
- ERROR_POS, acq->snsd_counter, snsd_counter);
- xfree(acq);
- list_destroy(ac->acq);
- return 0;
- }
-
- clist_add(&ac->acq, &tmp_counter, acq);
- }
-
- return ac->acq;
- }
-
- /*
- * unpack_andna_cache
- *
- * Unpacks a packed andna cache linked list and returns the
- * its head.
- * `counter' is set to the number of struct in the llist.
- * `pack_type' specifies if the package will be saved in a file or sent over
- * the net, it is equal to ACACHE_PACK_FILE or to ACACHE_PACK_PKT.
- *
- * On error 0 is returned and `*counter' is set to -1.
- * Warning: `pack' will be modified during the unpacking.
- */
- andna_cache *
- unpack_andna_cache(char *pack, size_t pack_sz, int *counter, int pack_type)
- {
- struct andna_cache_pkt_hdr *hdr;
- andna_cache *ac, *ac_head = 0;
- char *buf;
- size_t sz = 0;
- int i, err = 0;
- size_t unpacked_sz = 0;
-
- hdr = (struct andna_cache_pkt_hdr *) pack;
- ints_network_to_host(hdr, andna_cache_pkt_hdr_iinfo);
- *counter = 0;
-
- if (!hdr->tot_caches)
- ERROR_FINISH(err, 1, finish);
-
- buf = pack + sizeof(struct andna_cache_pkt_hdr);
- sz = sizeof(struct andna_cache_pkt_hdr);
-
- for (i = 0; i < hdr->tot_caches; i++) {
- sz += ACACHE_BODY_PACK_SZ;
- if (sz > pack_sz)
- ERROR_FINISH(err, 1, finish); /* overflow */
-
- ac = xzalloc(sizeof(andna_cache));
-
- ints_network_to_host(buf, andna_cache_body_iinfo);
-
- bufget(ac->hash, ANDNA_HASH_SZ);
- bufget(&ac->flags, sizeof(char));
- bufget(&ac->queue_counter, sizeof(u_short));
-
- sz += ACQ_PACK_SZ(0) * ac->queue_counter;
- if (sz > pack_sz)
- ERROR_FINISH(err, 1, finish); /* overflow */
-
- unpacked_sz += ACACHE_BODY_PACK_SZ;
-
- ac->acq =
- unpack_acq_llist(buf, pack_sz - unpacked_sz, &unpacked_sz, ac,
- pack_type);
- clist_add(&ac_head, counter, ac);
- }
-
- finish:
- if (err)
- *counter = -1;
- return ac_head;
- }
-
- /*
- * pack_counter_cache: packs the entire counter cache linked list that starts
- * with the head `counter'. The size of the pack is stored in `pack_sz'.
- * The pointer to the newly allocated pack is returned.
- * The pack will be in network order.
- */
- char *
- pack_counter_cache(counter_c * countercache, size_t * pack_sz)
- {
- struct counter_c_pkt_hdr hdr;
- counter_c *cc = countercache;
- counter_c_hashes *cch;
- char *pack, *buf, *p;
- size_t sz;
- time_t cur_t;
- uint32_t t;
-
- /* Calculate the pack size */
- hdr.tot_caches = 0;
- sz = sizeof(struct counter_c_pkt_hdr);
- list_for(cc) {
- sz += COUNTER_CACHE_PACK_SZ(cc->hashes);
- hdr.tot_caches++;
- }
-
- pack = xmalloc(sz);
- memcpy(pack, &hdr, sizeof(struct counter_c_pkt_hdr));
- ints_host_to_network(pack, counter_c_pkt_hdr_iinfo);
-
- if (hdr.tot_caches) {
- cur_t = time(0);
-
- buf = pack + sizeof(struct counter_c_pkt_hdr);
- cc = countercache;
- list_for(cc) {
- p = buf;
-
- bufput(cc->pubkey, ANDNA_PKEY_LEN);
- bufput(&cc->flags, sizeof(char));
- bufput(&cc->hashes, sizeof(u_short));
-
- ints_host_to_network(p, counter_c_body_iinfo);
-
- cch = cc->cch;
- list_for(cch) {
- p = buf;
-
- t = cur_t - cch->timestamp;
- bufput(&t, sizeof(uint32_t));
-
- bufput(&cch->hname_updates, sizeof(u_short));
- bufput(cch->hash, ANDNA_HASH_SZ);
-
- ints_host_to_network(p, counter_c_hashes_body_iinfo);
- }
- }
- }
-
- *pack_sz = sz;
- return pack;
- }
-
-
- /*
- * unpack_counter_cache
- *
- * Unpacks a packed counter cache linked list and returns the its head.
- * `counter' is set to the number of struct in the llist.
- *
- * On error 0 is returned and `*counter' is set to -1.
- *
- * Note: `pack' will be modified during the unpacking.
- */
- counter_c *
- unpack_counter_cache(char *pack, size_t pack_sz, int *counter)
- {
- struct counter_c_pkt_hdr *hdr;
- counter_c *cc, *cc_head = 0;
- counter_c_hashes *cch;
- char *buf;
- size_t sz;
- int i, e, fake_int = 0;
- time_t cur_t;
-
- hdr = (struct counter_c_pkt_hdr *) pack;
- ints_network_to_host(hdr, counter_c_pkt_hdr_iinfo);
- *counter = 0;
-
- if (hdr->tot_caches) {
- cur_t = time(0);
-
- buf = pack + sizeof(struct counter_c_pkt_hdr);
- sz = sizeof(struct counter_c_pkt_hdr);
-
- for (i = 0; i < hdr->tot_caches; i++) {
- sz += COUNTER_CACHE_BODY_PACK_SZ;
- if (sz > pack_sz)
- /* We don't want to overflow */
- ERROR_FINISH(*counter, -1, finish);
-
- cc = xzalloc(sizeof(counter_c));
-
- ints_network_to_host(buf, counter_c_body_iinfo);
-
- bufget(cc->pubkey, ANDNA_PKEY_LEN);
- bufget(&cc->flags, sizeof(char));
- bufget(&cc->hashes, sizeof(u_short));
-
-
- sz += COUNTER_CACHE_HASHES_PACK_SZ * cc->hashes;
- if (sz > pack_sz)
- /* bleah */
- ERROR_FINISH(*counter, -1, finish);
-
- for (e = 0; e < cc->hashes; e++) {
- cch = xzalloc(sizeof(counter_c_hashes));
-
- ints_network_to_host(buf, counter_c_hashes_body_iinfo);
-
- cch->timestamp = 0;
- bufget(&cch->timestamp, sizeof(uint32_t));
- cch->timestamp = cur_t - cch->timestamp;
-
- bufget(&cch->hname_updates, sizeof(u_short));
- bufget(cch->hash, ANDNA_HASH_SZ);
-
- clist_add(&cc->cch, &fake_int, cch);
- }
-
- clist_add(&cc_head, counter, cc);
- }
- }
- finish:
- return cc_head;
- }
-
-
- /*
- * pack_rh_cache
- *
- * It packs the entire resolved hnames cache linked list that starts
- * with the head `rhcache'. The size of the pack is stored in `pack_sz'.
- * The pointer to the newly allocated pack is returned.
- * The pack will be in network order.
- */
- char *
- pack_rh_cache(rh_cache * rhcache, size_t * pack_sz)
- {
- struct rh_cache_pkt_hdr rh_hdr;
- rh_cache *rhc = rhcache;
- size_t tot_pack_sz = 0, service_sz;
- char *pack, *buf;
-
- rh_hdr.tot_caches = 0;
- tot_pack_sz = sizeof(struct rh_cache_pkt_hdr);
-
- /* Calculate the final pack size */
- list_for(rhc) {
- service_sz = SNSD_SERVICE_LLIST_PACK_SZ(rhc->service);
- tot_pack_sz += RH_CACHE_BODY_PACK_SZ(service_sz);
- rh_hdr.tot_caches++;
- }
- *pack_sz = tot_pack_sz;
-
- buf = pack = xmalloc(tot_pack_sz);
- bufput(&rh_hdr, sizeof(struct rh_cache_pkt_hdr));
- tot_pack_sz -= sizeof(struct rh_cache_pkt_hdr);
-
- ints_host_to_network(pack, rh_cache_pkt_hdr_iinfo);
-
- if (rh_hdr.tot_caches) {
- rhc = rhcache;
-
- list_for(rhc) {
- bufput(&rhc->hash, sizeof(u_int));
- bufput(&rhc->flags, sizeof(char));
- bufput(&rhc->timestamp, sizeof(time_t));
-
- tot_pack_sz -= RH_CACHE_BODY_PACK_SZ(0);
-
- tot_pack_sz -= snsd_pack_all_services(buf, tot_pack_sz,
- rhc->service);
-
- /* host -> network order */
- ints_host_to_network(buf, rh_cache_pkt_body_iinfo);
- }
- }
-
- return pack;
- }
-
- /*
- * unpack_rh_cache
- *
- * Unpacks a packed resolved hnames cache linked list and returns its head.
- * `counter' is set to the number of struct in the llist.
- *
- * On error 0 is returned and `*counter' is set to -1.
- *
- * Note: `pack' will be modified during the unpacking.
- */
- rh_cache *
- unpack_rh_cache(char *pack, size_t pack_sz, int *counter)
- {
- struct rh_cache_pkt_hdr *hdr;
- rh_cache *rhc = 0, *rhc_head = 0;
- char *buf;
- size_t unpacked_sz = 0;
- int i = 0;
-
- hdr = (struct rh_cache_pkt_hdr *) pack;
- ints_network_to_host(hdr, rh_cache_pkt_hdr_iinfo);
- *counter = 0;
-
- if (hdr->tot_caches > ANDNA_MAX_RHC_HNAMES)
- ERROR_FINISH(*counter, -1, finish);
-
- *counter = 0;
- if (hdr->tot_caches) {
- buf = pack + sizeof(struct rh_cache_pkt_hdr);
- unpacked_sz = sizeof(struct rh_cache_pkt_hdr);
-
- for (i = 0; i < hdr->tot_caches; i++) {
- unpacked_sz += RH_CACHE_BODY_PACK_SZ(0);
- if (unpacked_sz > pack_sz)
- ERROR_FINISH(*counter, -1, finish);
-
- ints_network_to_host(buf, rh_cache_pkt_body_iinfo);
-
- rhc = xzalloc(sizeof(rh_cache));
-
- bufget(&rhc->hash, sizeof(u_int));
- bufget(&rhc->flags, sizeof(char));
- bufget(&rhc->timestamp, sizeof(time_t));
-
- rhc->service = snsd_unpack_all_service(buf, pack_sz,
- &unpacked_sz, 0);
-
- clist_add(&rhc_head, counter, rhc);
- }
- }
-
- finish:
- return rhc_head;
- }
-
-
- /*
- *
- * * * * Save/Load functions * * *
- *
- */
-
- /*
- * save_lcl_keyring: saves a local cache keyring in the specified `file'.
- */
- int
- save_lcl_keyring(lcl_cache_keyring * keyring, char *file)
- {
- FILE *fd;
- size_t pack_sz;
- char *pack;
-
- /*Pack! */
- pack = pack_lcl_keyring(keyring, &pack_sz);
- if (!pack_sz || !pack)
- return 0;
-
- if ((fd = fopen(file, "w")) == NULL) {
- error("Cannot save the lcl_keyring in %s: %s", file,
- strerror(errno));
- return -1;
- }
-
- /*Write! */
- fwrite(pack, pack_sz, 1, fd);
-
- xfree(pack);
- fclose(fd);
- return 0;
- }
-
- /*
- * load_lcl_keyring
- *
- * loads from `file' a local cache keyring and restores in it the RSA keys.
- *
- * On error -1 is returned.
- */
- int
- load_lcl_keyring(lcl_cache_keyring * keyring, char *file)
- {
- FILE *fd;
- char *pack = 0;
- size_t pack_sz;
- int ret = 0;
-
- if (!(fd = fopen(file, "r"))) {
- error("Cannot load the lcl_keyring from %s: %s", file,
- strerror(errno));
- return -1;
- }
-
- fseek(fd, 0, SEEK_END);
- pack_sz = ftell(fd);
- rewind(fd);
-
- pack = xmalloc(pack_sz);
- if (!fread(pack, pack_sz, 1, fd))
- ERROR_FINISH(ret, -1, finish);
-
- ret = unpack_lcl_keyring(keyring, pack, pack_sz);
-
- finish:
- if (pack)
- xfree(pack);
- fclose(fd);
-
- if (ret < 0)
- debug(DBG_NORMAL, "Malformed or empty lcl_keyring file. "
- "Aborting load_lcl_keyring().");
- return ret;
- }
-
-
- /*
- * save_lcl_cache: saves a local cache linked list in the specified `file'.
- */
- int
- save_lcl_cache(lcl_cache * lcl, char *file)
- {
- FILE *fd;
- size_t pack_sz;
- char *pack;
-
- /*Pack! */
- pack = pack_lcl_cache(lcl, &pack_sz);
- if (!pack_sz || !pack)
- return 0;
-
- if ((fd = fopen(file, "w")) == NULL) {
- error("Cannot save the lcl_cache in %s: %s", file,
- strerror(errno));
- return -1;
- }
-
- /*Write! */
- fwrite(pack, pack_sz, 1, fd);
-
- xfree(pack);
- fclose(fd);
- return 0;
- }
-
- /*
- * load_lcl_cache: loads from `file' a local cache list and returns the head
- * of the newly allocated llist. In `counter' it is stored the number of
- * structs of the llist.
- * On error 0 is returned.
- */
- lcl_cache *
- load_lcl_cache(char *file, int *counter)
- {
- lcl_cache *lcl = 0;
- FILE *fd;
- char *pack = 0;
- size_t pack_sz;
-
- if (!(fd = fopen(file, "r"))) {
- error("Cannot load the lcl_cache from %s: %s", file,
- strerror(errno));
- return 0;
- }
-
- fseek(fd, 0, SEEK_END);
- pack_sz = ftell(fd);
- rewind(fd);
-
- pack = xmalloc(pack_sz);
- if (!fread(pack, pack_sz, 1, fd))
- goto finish;
-
- lcl = unpack_lcl_cache(pack, pack_sz, counter);
-
- finish:
- if (pack)
- xfree(pack);
- fclose(fd);
- if (!lcl && counter < 0)
- error("Malformed lcl_cache file (%s)"
- "Aborting load_lcl_cache().", file);
- return lcl;
- }
-
-
- /*
- * save_andna_cache: saves an andna cache linked list in the `file' specified
- */
- int
- save_andna_cache(andna_cache * acache, char *file)
- {
- FILE *fd;
- size_t pack_sz;
- char *pack;
-
- /*Pack! */
- pack = pack_andna_cache(acache, &pack_sz, ACACHE_PACK_FILE);
- if (!pack_sz || !pack)
- return 0;
-
- if ((fd = fopen(file, "w")) == NULL) {
- error("Cannot save the andna_cache in %s: %s", file,
- strerror(errno));
- return -1;
- }
-
- /*Write! */
- fwrite(pack, pack_sz, 1, fd);
-
- xfree(pack);
- fclose(fd);
- return 0;
- }
-
- /*
- * load_andna_cache: loads from `file' an andna cache list and returns the head
- * of the newly allocated llist. In `counter' it is stored the number of
- * list's structs.
- * On error 0 is returned.
- */
- andna_cache *
- load_andna_cache(char *file, int *counter)
- {
- andna_cache *acache = 0;
- FILE *fd;
- char *pack = 0;
- size_t pack_sz;
-
- if ((fd = fopen(file, "r")) == NULL) {
- error("Cannot load the andna_cache from %s: %s", file,
- strerror(errno));
- return 0;
- }
-
- fseek(fd, 0, SEEK_END);
- pack_sz = ftell(fd);
- rewind(fd);
-
- pack = xmalloc(pack_sz);
- if (!fread(pack, pack_sz, 1, fd))
- goto finish;
-
- acache = unpack_andna_cache(pack, pack_sz, counter, ACACHE_PACK_FILE);
-
- finish:
- if (pack)
- xfree(pack);
- fclose(fd);
- if (!acache && counter < 0)
- error("Malformed andna_cache file."
- " Aborting load_andna_cache().");
- else if (!acache)
- debug(DBG_NORMAL, "Empty andna_cache file.");
-
- return acache;
- }
-
-
- /*
- * save_counter_c: saves a counter cache linked list in the `file' specified
- */
- int
- save_counter_c(counter_c * countercache, char *file)
- {
- FILE *fd;
- size_t pack_sz;
- char *pack;
-
- /*Pack! */
- pack = pack_counter_cache(countercache, &pack_sz);
- if (!pack_sz || !pack)
- return 0;
-
- if ((fd = fopen(file, "w")) == NULL) {
- error("Cannot save the counter_c in %s: %s", file,
- strerror(errno));
- return -1;
- }
-
- /*Write! */
- fwrite(pack, pack_sz, 1, fd);
-
- xfree(pack);
- fclose(fd);
- return 0;
- }
-
- /*
- * load_counter_c: loads from `file' a counter cache list and returns the head
- * of the newly allocated llist. In `counter' it is stored the number of
- * list's structs.
- * On error 0 is returned.
- */
- counter_c *
- load_counter_c(char *file, int *counter)
- {
- counter_c *countercache = 0;
- FILE *fd;
- char *pack = 0;
- size_t pack_sz;
-
- if ((fd = fopen(file, "r")) == NULL) {
- error("Cannot load the counter_c from %s: %s", file,
- strerror(errno));
- return 0;
- }
-
- fseek(fd, 0, SEEK_END);
- pack_sz = ftell(fd);
- rewind(fd);
-
- pack = xmalloc(pack_sz);
- if (!fread(pack, pack_sz, 1, fd))
- goto finish;
-
- countercache = unpack_counter_cache(pack, pack_sz, counter);
-
- finish:
- if (pack)
- xfree(pack);
- fclose(fd);
- if (!countercache && counter < 0)
- debug(DBG_NORMAL, "Malformed counter_c file (%s). "
- "Aborting load_counter_c().", file);
- return countercache;
- }
-
-
- /*
- * save_rh_cache: saves the resolved hnames cache linked list `rh' in the
- * `file' specified.
- */
- int
- save_rh_cache(rh_cache * rh, char *file)
- {
- FILE *fd = 0;
- size_t pack_sz;
- char *pack;
-
- /*Pack! */
- pack = pack_rh_cache(rh, &pack_sz);
- if (!pack_sz || !pack)
- return 0;
-
- if (!(fd = fopen(file, "w"))) {
- error("Cannot save the rh_cache in %s: %s", file, strerror(errno));
- return -1;
- }
-
- /*Write! */
- fwrite(pack, pack_sz, 1, fd);
-
- xfree(pack);
- fclose(fd);
- return 0;
- }
-
- /*
- * load_rh_cache: loads from `file' a resolved hnames cache list and returns
- * the head of the newly allocated llist. In `counter' it is stored the number
- * of structs of the llist.
- * On error 0 is returned.
- */
- rh_cache *
- load_rh_cache(char *file, int *counter)
- {
- rh_cache *rh = 0;
- FILE *fd;
- char *pack = 0;
- size_t pack_sz;
-
- if ((fd = fopen(file, "r")) == NULL) {
- error("Cannot load the rh_cache from %s: %s", file,
- strerror(errno));
- return 0;
- }
-
- fseek(fd, 0, SEEK_END);
- pack_sz = ftell(fd);
- rewind(fd);
-
- pack = xmalloc(pack_sz);
- if (!fread(pack, pack_sz, 1, fd))
- goto finish;
-
- rh = unpack_rh_cache(pack, pack_sz, counter);
-
- finish:
- if (pack)
- xfree(pack);
- fclose(fd);
- if (!rh && counter < 0)
- error("Malformed rh_cache file (%s). "
- "Aborting load_rh_cache().", file);
- return rh;
- }
-
-
- /*
- * load_hostnames
- *
- * It reads the `file' specified and reads each line in it.
- * The strings read are the hostnames that will be registered in andna.
- * Only ANDNA_MAX_HOSTNAMES lines are read. Each line can be maximum of
- * ANDNA_MAX_HNAME_LEN character long.
- *
- * This function updates automagically the old local cache that is pointed by
- * `*old_alcl_head'. The hostnames that are no more present in the loaded
- * `file' are discarded from the local cache.
- * Since a new local cache is allocated and the old is destroyed, the new
- * pointer to it is written in `*old_alcl_head'.
- *
- * The `old_alcl_counter' is updated too.
- *
- * This function shall be used each time the `file' changes.
- *
- * On error -1 is returned, otherwise 0 shall be the sacred value.
- */
- int
- load_hostnames(char *file, lcl_cache ** old_alcl_head,
- int *old_alcl_counter)
- {
- FILE *fd;
- char buf[ANDNA_MAX_HNAME_LEN + 1];
- size_t slen;
- time_t cur_t, diff;
- int i = 0;
-
- lcl_cache *alcl, *old_alcl, *new_alcl_head = 0;
- int new_alcl_counter = 0;
-
- if ((fd = fopen(file, "r")) == NULL) {
- error("Cannot load any hostnames from %s: %s", file,
- strerror(errno));
- return -1;
- }
-
- cur_t = time(0);
- while (!feof(fd) && i < ANDNA_MAX_HOSTNAMES) {
- setzero(buf, ANDNA_MAX_HNAME_LEN + 1);
- fgets(buf, ANDNA_MAX_HNAME_LEN, fd);
- if (feof(fd))
- break;
-
- if ((*buf) == '#' || (*buf) == '\n' || !(*buf)) {
- /* Strip off the comment lines */
- continue;
- } else {
- slen = strlen(buf);
- if (buf[slen - 1] == '\n') {
- /* Don't include the newline in the string */
- buf[slen - 1] = '\0';
- slen = strlen(buf);
- }
-
- /* Add the hname in the new local cache */
- alcl = lcl_cache_new(buf);
- clist_add(&new_alcl_head, &new_alcl_counter, alcl);
-
- /*
- * If there is an equal entry in the old lcl_cache and
- * it isn't expired, copy the old data in the new
- * struct.
- */
- old_alcl = lcl_cache_find_hname(*old_alcl_head,
- alcl->hostname);
- if (old_alcl) {
- diff = cur_t - old_alcl->timestamp;
- if (diff < ANDNA_EXPIRATION_TIME) {
- alcl->timestamp = old_alcl->timestamp;
- alcl->hname_updates = old_alcl->hname_updates;
- }
- }
- i++;
- }
- }
-
- /* Remove completely the old lcl_cache */
- lcl_cache_destroy(*old_alcl_head, old_alcl_counter);
-
- /* Update the pointers */
- *old_alcl_head = new_alcl_head;
- *old_alcl_counter = new_alcl_counter;
-
- fclose(fd);
- return 0;
- }
-
- /*
- * load_snsd
- *
- * It loads the SNSD records to be registered from the given `file'.
- * In the file there shall be one record per line, up to SNSD_MAX_RECORDS-1#
- * records.
- *
- * Each line has to be written in the following format:
- * hostname:snsd_hostname:service:priority:weight[:pub_key_file]
- * or
- * hostname:snsd_ip:service:priority:weight[:pub_key_file]
- *
- * The old records present in `alcl_head' will be deleted and substituted by
- * the loaded ones.
- *
- * On error -1 is returned.
- * If a syntax error is encountered in the file -2 is returned.
- */
- int
- load_snsd(char *file, lcl_cache * alcl_head)
- {
- #define MAX_SNSD_LINE_SZ (ANDNA_MAX_HNAME_LEN*4)
-
- FILE *fd;
- size_t slen;
- int line = 0, fields, e, service, nodes, ret = 0, err;
- char buf[MAX_SNSD_LINE_SZ + 1], **records;
- u_char proto, abort = 0;
-
- lcl_cache *alcl;
- snsd_service *sns;
- snsd_prio *snp;
- snsd_node *snd, snsd_node;
- inet_prefix ip;
-
- /* Delete all the old snsd records */
- alcl = alcl_head;
- list_for(alcl)
- if (alcl->service)
- snsd_service_llist_del(&alcl->service);
-
- if ((fd = fopen(file, "r")) == NULL) {
- error("Cannot open the snsd_nodes file from %s: %s",
- file, strerror(errno));
- return -1;
- }
-
- line = 1;
- while (!feof(fd) && line <= SNSD_MAX_RECORDS - 1) {
- setzero(buf, MAX_SNSD_LINE_SZ + 1);
- fgets(buf, MAX_SNSD_LINE_SZ, fd);
- if (feof(fd))
- break;
-
- if ((*buf) == '#' || (*buf) == '\n' || !(*buf)) {
- /* Strip off the comment lines */
- line++;
- continue;
- } else {
- slen = strlen(buf);
- if (buf[slen - 1] == '\n') {
- /* Don't include the newline in the string */
- buf[slen - 1] = '\0';
- slen = strlen(buf);
- }
-
- records = split_string(buf, ":", &fields, MAX_SNSD_FIELDS,
- ANDNA_MAX_HNAME_LEN * 2);
- if (fields < MIN_SNSD_FIELDS) {
- error("%s: Syntax error in line %d.\n"
- " The correct syntax is:\n"
- " \thostname:snsd_hostname:service:"
- "priority:weight[:pub_key_file]\n"
- " or\n"
- " \thostname:snsd_ip:service:"
- "priority:weight[:pub_key_file]", file, line);
- ERROR_FINISH(abort, 1, skip_line);
- }
-
- /*
- * hostname
- */
- alcl = lcl_cache_find_hname(alcl_head, records[0]);
- if (!alcl) {
- error("%s: line %d: The hostname \"%s\" doesn't"
- " exist in your local cache.\n"
- " Register it in the `andna_hostnames' file",
- file, line, records[0]);
- ERROR_FINISH(abort, 1, skip_line);
- }
-
- /*
- * snsd record
- */
- if (str_to_inet(records[1], &ip) >= 0) {
- inet_copy_ipdata_raw((u_int*)snsd_node.record, &ip);
- snsd_node.flags = SNSD_NODE_IP;
- } else {
- hash_md5((u_char *) records[1], strlen(records[1]),
- (u_char *) snsd_node.record);
- snsd_node.flags = SNSD_NODE_HNAME;
- }
-
- if (!strncmp(records[0], records[1],
- ANDNA_MAX_HNAME_LEN) && !strcmp(records[2], "0"))
- snsd_node.flags = SNSD_NODE_MAIN_IP | SNSD_NODE_IP;
-
- /***
- * Parse service and protocol
- */
- err = str_to_snsd_service(records[2], &service, &proto);
- if (err == -1)
- error("%s: error in line %d: \"%s\""
- " isn't a valid protocol\n", file, line, records[2]);
- else if (err == -2)
- error("%s: error in line %d: \"%s\""
- " isn't a valid service\n", file, line, records[2]);
- if (err < 0)
- ERROR_FINISH(abort, 1, skip_line);
- /**/
- /* Store service and protocol */
- sns = snsd_add_service(&alcl->service, service, proto);
-
- /* priority */
- snp = snsd_add_prio(&sns->prio, atoi(records[3]));
- nodes = snsd_count_prio_nodes(sns->prio);
- if (nodes >= SNSD_MAX_REC_SERV - 1) {
- error("%s: The maximum number of records for"
- " the service \"%s\" has been reached.\n"
- " The maximum is %d records per service",
- file, service, SNSD_MAX_REC_SERV);
- ERROR_FINISH(abort, 1, skip_line);
- }
-
- /* node and weight */
- snd = snsd_add_node(&snp->node, &alcl->snsd_counter,
- SNSD_MAX_RECORDS - 1, snsd_node.record);
- snd->weight = SNSD_WEIGHT(atoi(records[4]));
- snd->flags |= snsd_node.flags;
-
- /* pub_key_file
- * TODO:
- * if(fields >= 6)
- * snd->pubkey=load_pubkey(records[5])
- */
-
-
- skip_line:
- for (e = 0; e < fields; e++)
- xfree(records[e]);
- if (abort)
- ERROR_FINISH(ret, -2, finish);
- }
- line++;
- }
-
- finish:
- fclose(fd);
- return ret;
- }
-
-
- /*
- *
- * * * * Modify /etc/resolv.conf * * *
- *
- */
-
-
- /*
- * add_resolv_conf: It opens `file' and write in the first line `hname' moving
- * down the previous lines. The old `file' is backupped in `file'.bak.
- * Example: add_resolv_conf("nameserver 127.0.0.1", "/etc/resolv.conf").
- * Use del_resolv_conf to restore `file' with its backup.
- * On error -1 is returned.
- */
- int
- add_resolv_conf(char *hname, char *file)
- {
- FILE *fin = 0, /* `file' */
- *fin_bak = 0, /* `file'.bak */
- *fout = 0, /* The replaced `file' */
- *fout_back = 0; /* The backup of `file' */
-
- char *buf = 0, *p, *file_bk = 0;
- size_t buf_sz;
- int ret = 0;
-
- /*
- * Open and read `file'
- */
-
- if (!(fin = fopen(file, "r"))) {
- error("add_resolv_conf: cannot load %s: %s", file,
- strerror(errno));
- ERROR_FINISH(ret, -1, finish);
- }
-
- /* Prepare the name of the backup file */
- file_bk = xmalloc(strlen(file) + strlen(".bak") + 1);
- *file_bk = 0;
- strcpy(file_bk, file);
- strcat(file_bk, ".bak");
-
- reread_fin:
- fseek(fin, 0, SEEK_END);
- buf_sz = ftell(fin)+1;
- rewind(fin);
-
- buf = xmalloc(buf_sz);
- memset(buf, 0, buf_sz);
- if (!fread(buf, buf_sz, 1, fin)) {
- error("add_resolv_conf: it wasn't possible to read the %s file",
- file);
- ERROR_FINISH(ret, -1, finish);
- }
-
- /*
- * If there is already the `hname' string in the first line, try to
- * read `file'.bak, if it doesn't exist do nothing.
- */
- if (buf_sz - 1 >= strlen(hname) && !strncmp(buf, hname, strlen(hname))) {
- if (fin == fin_bak) {
- /*
- * We've already read `fin_bak', and it has
- * the `hname' string in its first line too. Stop it.
- */
- goto finish;
- }
-
- debug(DBG_NORMAL, "add_resolv_conf: Reading %s instead", file_bk);
- if (!(fin_bak = fopen(file_bk, "r")))
- goto finish;
-
- fclose(fin);
- fin = fin_bak;
-
- goto reread_fin;
- }
-
- /*
- * Backup `file' in `file'.bak
- */
- if (!(fout_back = fopen(file_bk, "w"))) {
- error
- ("add_resolv_conf: cannot create a backup copy of %s in %s: %s",
- file, file_bk, strerror(errno));
- ERROR_FINISH(ret, -1, finish);
- }
- fwrite(buf, buf_sz, 1, fout_back);
-
- /*
- * Delete `file'
- */
- fclose(fin);
- fin = 0;
- unlink(file);
-
- /*
- * Add as a first line `hname' in `file'
- */
- if (!(fout = fopen(file, "w"))) {
- error("add_resolv_conf: cannot reopen %s to overwrite it: %s",
- file, strerror(errno));
- ERROR_FINISH(ret, -1, finish);
- }
- fprintf(fout, "%s\n", hname);
- p = buf;
- while (*p) {
- if (*p != '#')
- fprintf(fout, "#");
- while (*p) {
- fprintf(fout, "%c", *p);
- if (*p == '\n')
- break;
- p++;
- }
- if (!*p)
- break;
- p++;
- }
- /*fwrite(buf, buf_sz, 1, fout); */
-
- finish:
- if (buf)
- xfree(buf);
- if (file_bk)
- xfree(file_bk);
- if (fin)
- fclose(fin);
- if (fout)
- fclose(fout);
- if (fout_back)
- fclose(fout_back);
-
- return ret;
- }
-
- /*
- * del_resolv_conf
- *
- * restores the old `file' modified by add_resolv_conf() by
- * copying `file'.bak over `file'. If the `hname' string is present in
- * `file'.bak it won't be written in `file'.
- * On error it returns -1.
- */
- int
- del_resolv_conf(char *hname, char *file)
- {
- FILE *fin = 0, *fout = 0;
-
- char *buf = 0, *file_bk = 0, tmp_buf[128 + 1];
- size_t buf_sz;
- int ret = 0;
-
- /*
- * Open and read `file'.bak
- */
- file_bk = xmalloc(strlen(file) + strlen(".bak") + 1);
- *file_bk = 0;
- strcpy(file_bk, file);
- strcat(file_bk, ".bak");
- if (!(fin = fopen(file_bk, "r"))) {
- /*error("del_resolv_conf: cannot load %s: %s", file_bk, strerror(errno)); */
- ERROR_FINISH(ret, -1, finish);
- }
-
- fseek(fin, 0, SEEK_END);
- buf_sz = ftell(fin);
- rewind(fin);
-
- if (!buf_sz) {
- /* `file_bk' is empty, delete it */
- unlink(file_bk);
- ERROR_FINISH(ret, -1, finish);
- }
-
- buf = xzalloc(buf_sz);
- while (fgets(tmp_buf, 128, fin)) {
- /* Skip the line which is equal to `hname' */
- if (!strncmp(tmp_buf, hname, strlen(hname)))
- continue;
- strcat(buf, tmp_buf);
- }
-
- /*
- * Delete `file'
- */
- unlink(file);
-
- /*
- * Copy `file'.bak in `file'
- */
-
- if (!(fout = fopen(file, "w"))) {
- error("del_resolv_conf: cannot copy %s in %s: %s", file_bk,
- file, strerror(errno));
- ERROR_FINISH(ret, -1, finish);
- }
- fprintf(fout, "%s", buf);
-
- /*
- * delete `file'.bak
- */
-
- fclose(fin);
- fin = 0;
- unlink(file_bk);
-
- finish:
- if (buf)
- xfree(buf);
- if (file_bk)
- xfree(file_bk);
- if (fin)
- fclose(fin);
- if (fout)
- fclose(fout);
-
- return ret;
- }
|