You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

andna_cache.c 46KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154
  1. /* This file is part of Netsukuku
  2. * (c) Copyright 2005 Andrea Lo Pumo aka AlpT <alpt@freaknet.org>
  3. *
  4. * This source code is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as published
  6. * by the Free Software Foundation; either version 2 of the License,
  7. * or (at your option) any later version.
  8. *
  9. * This source code is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  12. * Please refer to the GNU Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Public License along with
  15. * this source code; if not, write to:
  16. * Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. *
  18. * --
  19. * andna_cache.c:
  20. * Functions to manipulate all the andna's caches.
  21. */
  22. #include "includes.h"
  23. #include "crypto.h"
  24. #include "andna_cache.h"
  25. #include "snsd_cache.h"
  26. #include "common.h"
  27. #include "hash.h"
  28. int net_family;
  29. void andna_caches_init(int family)
  30. {
  31. net_family = family;
  32. setzero(&lcl_keyring, sizeof(lcl_keyring));
  33. andna_lcl=(lcl_cache *)clist_init(&lcl_counter);
  34. andna_c=(andna_cache *)clist_init(&andna_c_counter);
  35. andna_counter_c=(counter_c *)clist_init(&cc_counter);
  36. andna_rhc=(rh_cache *)clist_init(&rhc_counter);
  37. }
  38. /*
  39. * andna_32bit_hash
  40. *
  41. * It returns the 32bit hash of the md5 hash of the `hname' string.
  42. */
  43. u_int andna_32bit_hash(char *hname)
  44. {
  45. u_char hashm5[ANDNA_HASH_SZ];
  46. hash_md5((u_char*)hname, strlen(hname), hashm5);
  47. return fnv_32_buf(hashm5, ANDNA_HASH_SZ, FNV1_32_INIT);
  48. }
  49. /*
  50. *
  51. * * * * Local Cache functions * * *
  52. *
  53. */
  54. /*
  55. * lcl_new_keyring
  56. *
  57. * It generates a new keyring.
  58. */
  59. void lcl_new_keyring(lcl_cache_keyring *keyring)
  60. {
  61. setzero(keyring, sizeof(lcl_cache_keyring));
  62. loginfo("Generating a new ANDNA keyring");
  63. /* Generate the new key pair for the first time */
  64. keyring->priv_rsa = genrsa(ANDNA_PRIVKEY_BITS, &keyring->pubkey,
  65. &keyring->pkey_len, &keyring->privkey, &keyring->skey_len);
  66. }
  67. /*
  68. * lcl_destroy_keyring
  69. *
  70. * destroys accurately the keyring ^_^
  71. */
  72. void lcl_destroy_keyring(lcl_cache_keyring *keyring)
  73. {
  74. if(keyring->priv_rsa)
  75. RSA_free(keyring->priv_rsa);
  76. if(keyring->pubkey)
  77. xfree(keyring->pubkey);
  78. if(keyring->privkey)
  79. xfree(keyring->privkey);
  80. setzero(keyring, sizeof(lcl_cache_keyring));
  81. }
  82. /*
  83. * lcl_cache_new: builds a new lcl_cache generating a new rsa key pair and
  84. * setting the hostname in the struct
  85. */
  86. lcl_cache *lcl_cache_new(char *hname)
  87. {
  88. lcl_cache *alcl;
  89. alcl=(lcl_cache *)xzalloc(sizeof(lcl_cache));
  90. alcl->hostname = xstrdup(hname);
  91. alcl->hash = andna_32bit_hash(hname);
  92. return alcl;
  93. }
  94. void lcl_cache_free(lcl_cache *alcl)
  95. {
  96. if(alcl->hostname)
  97. xfree(alcl->hostname);
  98. alcl->snsd_counter=0;
  99. if(alcl->service)
  100. snsd_service_llist_del(&alcl->service);
  101. }
  102. void lcl_cache_destroy(lcl_cache *head, int *counter)
  103. {
  104. lcl_cache *alcl=head, *next;
  105. if(!alcl || !lcl_counter)
  106. return;
  107. list_safe_for(alcl, next) {
  108. lcl_cache_free(alcl);
  109. xfree(alcl);
  110. }
  111. *counter=0;
  112. }
  113. lcl_cache *lcl_cache_find_hname(lcl_cache *alcl, char *hname)
  114. {
  115. u_int hash;
  116. if(!alcl || !lcl_counter)
  117. return 0;
  118. hash = andna_32bit_hash(hname);
  119. list_for(alcl)
  120. if(alcl->hash == hash && alcl->hostname &&
  121. !strncmp(alcl->hostname, hname, ANDNA_MAX_HNAME_LEN))
  122. return alcl;
  123. return 0;
  124. }
  125. lcl_cache *lcl_cache_find_hash(lcl_cache *alcl, u_int hash)
  126. {
  127. if(!alcl || !lcl_counter)
  128. return 0;
  129. list_for(alcl)
  130. if(alcl->hash == hash && alcl->hostname)
  131. return alcl;
  132. return 0;
  133. }
  134. int is_lcl_hname_registered(lcl_cache *alcl)
  135. {
  136. return alcl->timestamp;
  137. }
  138. /*
  139. * lcl_get_registered_hnames
  140. *
  141. * It returns a duplicated lcl_cache of `alcl', which contains only
  142. * hostnames already registered.
  143. * Note that the structs present in the returned cache are in a different
  144. * mallocated space, so you should free them.
  145. */
  146. lcl_cache *lcl_get_registered_hnames(lcl_cache *alcl)
  147. {
  148. lcl_cache *lcl;
  149. lcl=list_copy_some(alcl, is_lcl_hname_registered);
  150. list_for(lcl) {
  151. lcl->hostname=xstrdup(lcl->hostname);
  152. lcl->service=snsd_service_llist_copy(lcl->service,
  153. SNSD_ALL_SERVICE, 0);
  154. }
  155. return lcl;
  156. }
  157. /*
  158. *
  159. * * * * Andna Cache functions * * *
  160. *
  161. */
  162. andna_cache_queue *ac_queue_findpubk(andna_cache *ac, char *pubk)
  163. {
  164. andna_cache_queue *acq=ac->acq;
  165. if(!acq)
  166. return 0;
  167. list_for(acq)
  168. if(!memcmp(acq->pubkey, pubk, ANDNA_PKEY_LEN))
  169. return acq;
  170. return 0;
  171. }
  172. /*
  173. * ac_queue_add
  174. *
  175. * adds a new entry in the andna cache queue, which is `ac'->acq.
  176. * The elements in the new `ac'->acq are updated.
  177. * If an `ac'->acq struct with an `ac'->acq->pubkey equal to `pubkey' already
  178. * exists, then only the timestamp and the IP will be updated.
  179. *
  180. * It returns the pointer to the acq struct. If it isn't possible to add a new
  181. * entry in the queue, 0 will be returned.
  182. *
  183. * Remember to update the acq->timestamp value after this call.
  184. */
  185. andna_cache_queue *ac_queue_add(andna_cache *ac, char *pubkey)
  186. {
  187. andna_cache_queue *acq;
  188. /*
  189. * This call is not necessary because it's already done by
  190. * andna_cache_del_expired().
  191. * * ac_queue_del_expired(ac); * *
  192. */
  193. if(!(acq=ac_queue_findpubk(ac, pubkey))) {
  194. if(ac->queue_counter >= ANDNA_MAX_QUEUE || ac->flags & ANDNA_FULL)
  195. return 0;
  196. acq=xzalloc(sizeof(andna_cache_queue));
  197. memcpy(acq->pubkey, pubkey, ANDNA_PKEY_LEN);
  198. clist_append(&ac->acq, 0, &ac->queue_counter, acq);
  199. }
  200. if(ac->queue_counter >= ANDNA_MAX_QUEUE)
  201. ac->flags|=ANDNA_FULL;
  202. return acq;
  203. }
  204. void ac_queue_del(andna_cache *ac, andna_cache_queue *acq)
  205. {
  206. acq->snsd_counter=0;
  207. if(acq->service)
  208. snsd_service_llist_del(&acq->service);
  209. clist_del(&ac->acq, &ac->queue_counter, acq);
  210. ac->flags&=~ANDNA_FULL;
  211. }
  212. /*
  213. * ac_queue_del_expired: removes the expired entries from the
  214. * andna_cache_queue `ac'->acq.
  215. */
  216. void ac_queue_del_expired(andna_cache *ac)
  217. {
  218. andna_cache_queue *acq, *next;
  219. time_t cur_t;
  220. if(!ac || !ac->acq)
  221. return;
  222. cur_t=time(0);
  223. acq=ac->acq;
  224. list_safe_for(acq, next)
  225. if(cur_t - acq->timestamp > ANDNA_EXPIRATION_TIME)
  226. ac_queue_del(ac, acq);
  227. }
  228. /*
  229. * ac_queue_destroy: destroys an andna_cache_queue
  230. */
  231. void ac_queue_destroy(andna_cache *ac)
  232. {
  233. andna_cache_queue *acq, *next;
  234. if(!ac || !ac->acq)
  235. return;
  236. acq=ac->acq;
  237. list_safe_for(acq, next)
  238. ac_queue_del(ac, acq);
  239. }
  240. andna_cache *andna_cache_findhash(int hash[MAX_IP_INT])
  241. {
  242. andna_cache *ac=andna_c;
  243. if(!andna_c_counter)
  244. return 0;
  245. list_for(ac)
  246. if(!memcmp(ac->hash, hash, ANDNA_HASH_SZ))
  247. return ac;
  248. return 0;
  249. }
  250. /*
  251. * andna_cache_gethash
  252. *
  253. * It searches an andna_cache entry which has the same hash of `hash'.
  254. * If it found but this entry is expired, it is deleted from the cache and 0 is
  255. * returned.
  256. * If it isn't found 0 is returned, otherwise a pointer to the entry is
  257. * returned.
  258. */
  259. andna_cache *andna_cache_gethash(int hash[MAX_IP_INT])
  260. {
  261. andna_cache *ac;
  262. ac=andna_cache_findhash(hash);
  263. if(ac && andna_cache_del_ifexpired(ac))
  264. return 0;
  265. return ac;
  266. }
  267. andna_cache *andna_cache_addhash(int hash[MAX_IP_INT])
  268. {
  269. andna_cache *ac;
  270. andna_cache_del_expired();
  271. if(!(ac=andna_cache_findhash(hash))) {
  272. ac=xzalloc(sizeof(andna_cache));
  273. memcpy(ac->hash, hash, ANDNA_HASH_SZ);
  274. clist_add(&andna_c, &andna_c_counter, ac);
  275. }
  276. return ac;
  277. }
  278. /*
  279. * andna_cache_del_ifexpired
  280. *
  281. * If `ac' is expired, it deletes it and returns 1; otherwise 0 is returned.
  282. */
  283. int andna_cache_del_ifexpired(andna_cache *ac)
  284. {
  285. ac_queue_del_expired(ac);
  286. if(!ac->queue_counter) {
  287. clist_del(&andna_c, &andna_c_counter, ac);
  288. return 1;
  289. }
  290. return 0;
  291. }
  292. void andna_cache_del_expired(void)
  293. {
  294. andna_cache *ac=andna_c, *next;
  295. if(!andna_c_counter)
  296. return;
  297. list_safe_for(ac, next)
  298. andna_cache_del_ifexpired(ac);
  299. }
  300. /*
  301. * andna_cache_destroy
  302. *
  303. * destroys the andna_c llist
  304. */
  305. void andna_cache_destroy(void)
  306. {
  307. andna_cache *ac=andna_c, *next;
  308. if(!andna_c_counter)
  309. return;
  310. list_safe_for(ac, next) {
  311. ac_queue_destroy(ac);
  312. clist_del(&andna_c, &andna_c_counter, ac);
  313. }
  314. }
  315. /*
  316. *
  317. * * * * Counter Cache functions * * *
  318. *
  319. */
  320. /*
  321. * Remeber to update the cch->timestamp value after this call.
  322. */
  323. counter_c_hashes *cc_hashes_add(counter_c *cc, int hash[MAX_IP_INT])
  324. {
  325. counter_c_hashes *cch;
  326. /* The purge is already done in counter_c_del_expired(), so it is not
  327. * necessary to call it here.
  328. * * cc_hashes_del_expired(cc); * *
  329. */
  330. if(!(cch=cc_findhash(cc, hash))) {
  331. if(cc->hashes >= ANDNA_MAX_HOSTNAMES || cc->flags & ANDNA_FULL)
  332. return 0;
  333. cch=xzalloc(sizeof(counter_c_hashes));
  334. memcpy(cch->hash, hash, ANDNA_HASH_SZ);
  335. clist_add(&cc->cch, &cc->hashes, cch);
  336. }
  337. if(cc->hashes >= ANDNA_MAX_HOSTNAMES)
  338. cc->flags|=ANDNA_FULL;
  339. return cch;
  340. }
  341. void cc_hashes_del(counter_c *cc, counter_c_hashes *cch)
  342. {
  343. clist_del(&cc->cch, &cc->hashes, cch);
  344. cc->flags&=~ANDNA_FULL;
  345. }
  346. void cc_hashes_del_expired(counter_c *cc)
  347. {
  348. counter_c_hashes *cch, *next;
  349. time_t cur_t;
  350. if(!cc || !cc->cch || !cc->hashes)
  351. return;
  352. cur_t=time(0);
  353. cch=cc->cch;
  354. list_safe_for(cch, next)
  355. if(cur_t - cch->timestamp > ANDNA_EXPIRATION_TIME)
  356. cc_hashes_del(cc, cch);
  357. }
  358. void cc_hashes_destroy(counter_c *cc)
  359. {
  360. counter_c_hashes *cch, *next;
  361. if(!cc || !cc->cch || !cc->hashes)
  362. return;
  363. cch=cc->cch;
  364. list_safe_for(cch, next)
  365. cc_hashes_del(cc, cch);
  366. }
  367. counter_c_hashes *cc_findhash(counter_c *cc, int hash[MAX_IP_INT])
  368. {
  369. counter_c_hashes *cch=cc->cch;
  370. if(!cc->hashes || !cch)
  371. return 0;
  372. list_for(cch)
  373. if(!memcmp(cch->hash, hash, ANDNA_HASH_SZ))
  374. return cch;
  375. return 0;
  376. }
  377. counter_c *counter_c_findpubk(char *pubk)
  378. {
  379. counter_c *cc=andna_counter_c;
  380. if(!cc_counter || !cc)
  381. return 0;
  382. list_for(cc)
  383. if(!memcmp(&cc->pubkey, pubk, ANDNA_PKEY_LEN))
  384. return cc;
  385. return 0;
  386. }
  387. counter_c *counter_c_add(inet_prefix *rip, char *pubkey)
  388. {
  389. counter_c *cc;
  390. counter_c_del_expired();
  391. if(!(cc=counter_c_findpubk(pubkey))) {
  392. cc=xzalloc(sizeof(counter_c));
  393. memcpy(cc->pubkey, pubkey, ANDNA_PKEY_LEN);
  394. clist_add(&andna_counter_c, &cc_counter, cc);
  395. }
  396. return cc;
  397. }
  398. void counter_c_del_expired(void)
  399. {
  400. counter_c *cc=andna_counter_c, *next;
  401. if(!cc)
  402. return;
  403. list_safe_for(cc, next) {
  404. cc_hashes_del_expired(cc);
  405. if(!cc->hashes)
  406. clist_del(&andna_counter_c, &cc_counter, cc);
  407. }
  408. }
  409. /*
  410. * counter_c_destroy
  411. *
  412. * destroy the andna_counter_c llist
  413. */
  414. void counter_c_destroy(void)
  415. {
  416. counter_c *cc=andna_counter_c, *next;
  417. if(!cc)
  418. return;
  419. list_safe_for(cc, next) {
  420. cc_hashes_destroy(cc);
  421. clist_del(&andna_counter_c, &cc_counter, cc);
  422. }
  423. }
  424. /*
  425. *
  426. * * * * Resolved hostnames cache functions * * *
  427. *
  428. */
  429. rh_cache *rh_cache_new_hash(u_int hash, time_t timestamp)
  430. {
  431. rh_cache *rhc;
  432. rhc=xzalloc(sizeof(rh_cache));
  433. rhc->hash=hash;
  434. rhc->timestamp=timestamp;
  435. return rhc;
  436. }
  437. rh_cache *rh_cache_new(char *hname, time_t timestamp)
  438. {
  439. return rh_cache_new_hash(andna_32bit_hash(hname), timestamp);
  440. }
  441. /*
  442. * rh_cache_add_hash
  443. *
  444. * It searches a struct in the rh_cache which has the hash value equal to
  445. * `hash'.
  446. * If it isn't found a new one is added. In both cases the pointer to the
  447. * struct will be returned.
  448. *
  449. * On error 0 is returned.
  450. */
  451. rh_cache *rh_cache_add_hash(u_int hash, time_t timestamp)
  452. {
  453. rh_cache *rhc;
  454. if(!(rhc=rh_cache_find_hash(hash))) {
  455. if(rhc_counter >= ANDNA_MAX_HOSTNAMES) {
  456. /* Delete the expired hnames and see if there's empty
  457. * space */
  458. rh_cache_del_expired();
  459. if(rhc_counter >= ANDNA_MAX_HOSTNAMES) {
  460. /* Delete the oldest struct in cache */
  461. rhc=list_last(andna_rhc);
  462. clist_del(&andna_rhc, &rhc_counter, rhc);
  463. }
  464. }
  465. rhc=rh_cache_new_hash(hash, timestamp);
  466. clist_add(&andna_rhc, &rhc_counter, rhc);
  467. }
  468. rhc->timestamp=timestamp;
  469. return rhc;
  470. }
  471. /*
  472. * rh_cache_add
  473. *
  474. * It searches a struct in the rh_cache which is associated to `hname'.
  475. * If it isn't found a new one is added. In both cases the pointer to the
  476. * struct will be returned.
  477. *
  478. * On error 0 is returned.
  479. */
  480. rh_cache *rh_cache_add(char *hname, time_t timestamp)
  481. {
  482. return rh_cache_add_hash(andna_32bit_hash(hname), timestamp);
  483. }
  484. rh_cache *rh_cache_find_hash(u_int hash)
  485. {
  486. rh_cache *rhc=andna_rhc, *next;
  487. time_t cur_t;
  488. if(!rhc || !rhc_counter)
  489. return 0;
  490. cur_t=time(0);
  491. list_safe_for(rhc, next)
  492. if(rhc->hash == hash) {
  493. if(cur_t - rhc->timestamp > ANDNA_EXPIRATION_TIME) {
  494. /* This hostname expired, delete it from the
  495. * cache */
  496. rh_cache_del(rhc);
  497. continue;
  498. } else
  499. /* Each time we find a hname in the rh_cache,
  500. * we move it on top of the llist. */
  501. andna_rhc=list_moveontop(andna_rhc, rhc);
  502. return rhc;
  503. }
  504. return 0;
  505. }
  506. rh_cache *rh_cache_find_hname(char *hname)
  507. {
  508. u_int hash;
  509. hash=andna_32bit_hash(hname);
  510. return rh_cache_find_hash(hash);
  511. }
  512. void rh_cache_del(rh_cache *rhc)
  513. {
  514. rhc->snsd_counter=0;
  515. if(rhc->service)
  516. snsd_service_llist_del(&rhc->service);
  517. clist_del(&andna_rhc, &rhc_counter, rhc);
  518. }
  519. void rh_cache_del_expired(void)
  520. {
  521. rh_cache *rhc=andna_rhc, *next;
  522. time_t cur_t;
  523. if(!rhc || !rhc_counter)
  524. return;
  525. cur_t=time(0);
  526. list_safe_for(rhc, next)
  527. if(cur_t - rhc->timestamp > ANDNA_EXPIRATION_TIME)
  528. rh_cache_del(rhc);
  529. }
  530. void rh_cache_flush(void)
  531. {
  532. rh_cache *rhc=andna_rhc, *next;
  533. list_safe_for(rhc, next)
  534. rh_cache_del(rhc);
  535. }
  536. /*
  537. *
  538. * * * * Pack/Unpack functions * * *
  539. *
  540. */
  541. char *pack_lcl_keyring(lcl_cache_keyring *keyring, size_t *pack_sz)
  542. {
  543. struct lcl_keyring_pkt_hdr key_hdr;
  544. size_t sz;
  545. char *pack, *buf;
  546. key_hdr.skey_len=keyring->skey_len;
  547. key_hdr.pkey_len=keyring->pkey_len;
  548. sz=LCL_KEYRING_HDR_PACK_SZ(&key_hdr);
  549. pack=buf=xmalloc(sz);
  550. bufput(&key_hdr, sizeof(struct lcl_keyring_pkt_hdr));
  551. ints_host_to_network(pack, lcl_keyring_pkt_hdr_iinfo);
  552. bufput(keyring->privkey, keyring->skey_len);
  553. bufput(keyring->pubkey, keyring->pkey_len);
  554. *pack_sz=sz;
  555. return pack;
  556. }
  557. /*
  558. * unpack_lcl_keyring: unpacks a lcl keyring. On error it returns -1.
  559. * In `keyring' it restores the packed keys.
  560. */
  561. int unpack_lcl_keyring(lcl_cache_keyring *keyring, char *pack, size_t pack_sz)
  562. {
  563. struct lcl_keyring_pkt_hdr *hdr;
  564. char *buf;
  565. u_char *pk;
  566. hdr=(struct lcl_keyring_pkt_hdr *)pack;
  567. ints_network_to_host(hdr, lcl_keyring_pkt_hdr_iinfo);
  568. /*
  569. * Restore the keyring
  570. */
  571. keyring->skey_len=hdr->skey_len;
  572. keyring->pkey_len=hdr->pkey_len;
  573. if(keyring->skey_len > ANDNA_SKEY_MAX_LEN) {
  574. error(ERROR_MSG "Invalid keyring header", ERROR_FUNC);
  575. return -1;
  576. }
  577. keyring->privkey=xmalloc(hdr->skey_len);
  578. keyring->pubkey=xmalloc(hdr->pkey_len);
  579. /* extract the private key */
  580. buf=pack+sizeof(struct lcl_keyring_pkt_hdr);
  581. bufget(keyring->privkey, hdr->skey_len);
  582. /* public key */
  583. bufget(keyring->pubkey, hdr->pkey_len);
  584. pk=keyring->privkey;
  585. if(!(keyring->priv_rsa=get_rsa_priv((const u_char **)&pk,
  586. keyring->skey_len))) {
  587. error(ERROR_MSG "Cannot unpack the priv key from the"
  588. " lcl_pack: %s", ERROR_POS, ssl_strerr());
  589. return -1;
  590. }
  591. return 0;
  592. }
  593. /*
  594. * pack_lcl_cache
  595. *
  596. * packs the entire local cache linked list that starts with the head
  597. * `local_cache'. The size of the pack is stored in `pack_sz'.
  598. * The pointer to the newly allocated pack is returned.
  599. * Note that the pack is in network byte order.
  600. */
  601. char *pack_lcl_cache(lcl_cache *local_cache, size_t *pack_sz)
  602. {
  603. struct lcl_cache_pkt_hdr lcl_hdr;
  604. lcl_cache *alcl=local_cache;
  605. size_t sz=0, slen;
  606. char *pack, *buf, *body;
  607. lcl_hdr.tot_caches=0;
  608. sz=LCL_CACHE_HDR_PACK_SZ;
  609. /* Calculate the final pack size */
  610. list_for(alcl) {
  611. sz+=LCL_CACHE_BODY_PACK_SZ(strlen(alcl->hostname)+1);
  612. lcl_hdr.tot_caches++;
  613. }
  614. pack=buf=xmalloc(sz);
  615. bufput(&lcl_hdr, sizeof(struct lcl_cache_pkt_hdr));
  616. ints_host_to_network(pack, lcl_cache_pkt_hdr_iinfo);
  617. *pack_sz=0;
  618. if(lcl_hdr.tot_caches) {
  619. alcl=local_cache;
  620. list_for(alcl) {
  621. body=buf;
  622. bufput(&alcl->hname_updates, sizeof(u_short));
  623. bufput(&alcl->timestamp, sizeof(time_t));
  624. slen=strlen(alcl->hostname)+1;
  625. bufput(alcl->hostname, slen);
  626. ints_host_to_network(body, lcl_cache_pkt_body_iinfo);
  627. }
  628. }
  629. *pack_sz=sz;
  630. return pack;
  631. }
  632. /*
  633. * unpack_lcl_cache
  634. *
  635. * Unpacks a packed local cache linked list and returns its head.
  636. * `counter' is set to the number of struct in the llist.
  637. *
  638. * On error 0 is returned and `*counter' is set to -1.
  639. *
  640. * Note: `pack' is modified during the unpacking.
  641. */
  642. lcl_cache *unpack_lcl_cache(char *pack, size_t pack_sz, int *counter)
  643. {
  644. struct lcl_cache_pkt_hdr *hdr;
  645. lcl_cache *alcl, *alcl_head=0;
  646. char *buf;
  647. size_t slen, unpacked_sz;
  648. int i=0;
  649. hdr=(struct lcl_cache_pkt_hdr *)pack;
  650. buf=pack+sizeof(struct lcl_cache_pkt_hdr);
  651. unpacked_sz=sizeof(struct lcl_cache_pkt_hdr);
  652. ints_network_to_host(hdr, lcl_cache_pkt_hdr_iinfo);
  653. *counter=0;
  654. if(hdr->tot_caches > ANDNA_MAX_HOSTNAMES)
  655. ERROR_FINISH(*counter, -1, finish);
  656. *counter=0;
  657. if(hdr->tot_caches) {
  658. for(i=0; i<hdr->tot_caches; i++) {
  659. unpacked_sz+=LCL_CACHE_BODY_PACK_SZ(0);
  660. if(unpacked_sz > pack_sz)
  661. ERROR_FINISH(*counter, -1, finish);
  662. slen=strlen(buf+sizeof(u_short)+sizeof(time_t))+1;
  663. if(slen > ANDNA_MAX_HNAME_LEN ||
  664. (unpacked_sz+=slen) > pack_sz)
  665. ERROR_FINISH(*counter, -1, finish);
  666. ints_network_to_host(buf, lcl_cache_pkt_body_iinfo);
  667. alcl=xzalloc(sizeof(lcl_cache));
  668. bufget(&alcl->hname_updates, sizeof(u_short));
  669. bufget(&alcl->timestamp, sizeof(time_t));
  670. alcl->hostname=xstrdup(buf);
  671. alcl->hash=andna_32bit_hash(alcl->hostname);
  672. buf+=slen;
  673. clist_add(&alcl_head, counter, alcl);
  674. }
  675. }
  676. finish:
  677. return alcl_head;
  678. }
  679. /*
  680. * pack_andna_cache_queue
  681. *
  682. * It packs an andna_cache_queue struct. The package is stored in `pack' which
  683. * has `tot_pack_sz' allocated bytes.
  684. * `acq' is the struct which will be packed.
  685. * `pack_type' is equal to ACACHE_PACK_FILE or ACACHE_PACK_PKT, it specify if
  686. * the package will be stored in a file or will be sent over a network.
  687. *
  688. * The number of bytes written in `pack' is returned.
  689. */
  690. int pack_andna_cache_queue(char *pack, size_t tot_pack_sz,
  691. andna_cache_queue *acq, int pack_type)
  692. {
  693. char *buf=pack;
  694. u_int t;
  695. int pack_sz=0;
  696. if(pack_type == ACACHE_PACK_PKT)
  697. t = time(0) - acq->timestamp;
  698. else
  699. t = acq->timestamp;
  700. bufput(&t, sizeof(uint32_t));
  701. bufput(&acq->hname_updates, sizeof(u_short));
  702. bufput(&acq->pubkey, ANDNA_PKEY_LEN);
  703. bufput(&acq->snsd_counter, sizeof(u_short));
  704. pack_sz+=ACQ_BODY_PACK_SZ;
  705. ints_host_to_network(pack, acq_body_iinfo);
  706. pack_sz+=snsd_pack_all_services(buf, tot_pack_sz, acq->service);
  707. return pack_sz;
  708. }
  709. /*
  710. * pack_single_andna_cache
  711. *
  712. * It packs an andna_cache struct. The package is stored in `pack' which
  713. * has `tot_pack_sz' allocated bytes.
  714. * `ac' is the struct which will be packed.
  715. * `pack_type' is equal to ACACHE_PACK_FILE or ACACHE_PACK_PKT, it specify if
  716. * the package will be stored in a file or will be sent over a network.
  717. *
  718. * The number of bytes written in `pack' is returned.
  719. */
  720. int pack_single_andna_cache(char *pack, size_t tot_pack_sz,
  721. andna_cache *ac, int pack_type)
  722. {
  723. andna_cache_queue *acq;
  724. char *buf=pack;
  725. int pack_sz=0;
  726. size_t psz;
  727. bufput(ac->hash, ANDNA_HASH_SZ);
  728. bufput(&ac->flags, sizeof(char));
  729. bufput(&ac->queue_counter, sizeof(u_short));
  730. pack_sz+=ACACHE_BODY_PACK_SZ;
  731. ints_host_to_network(pack, andna_cache_body_iinfo);
  732. acq=ac->acq;
  733. list_for(acq) {
  734. psz=pack_andna_cache_queue(buf, tot_pack_sz, acq, pack_type);
  735. buf+=psz;
  736. pack_sz+=psz;
  737. tot_pack_sz-=psz;
  738. }
  739. return pack_sz;
  740. }
  741. /*
  742. * pack_andna_cache
  743. *
  744. * It packs the entire andna cache linked list that starts with
  745. * the head `acache'.
  746. * The size of the pack is stored in `pack_sz'.
  747. * `pack_type' specifies if the package will be saved in a file or sent over
  748. * the net, it is equal to ACACHE_PACK_FILE or to ACACHE_PACK_PKT.
  749. *
  750. * The pointer to the newly allocated pack is returned.
  751. * The pack is written in network order.
  752. */
  753. char *pack_andna_cache(andna_cache *acache, size_t *pack_sz, int pack_type)
  754. {
  755. struct andna_cache_pkt_hdr hdr;
  756. andna_cache *ac=acache;
  757. andna_cache_queue *acq;
  758. char *pack, *buf;
  759. size_t sz, free_sz, acq_sz, service_sz, psz;
  760. /* Calculate the pack size */
  761. ac=acache;
  762. hdr.tot_caches=0;
  763. sz=sizeof(struct andna_cache_pkt_hdr);
  764. list_for(ac) {
  765. acq=ac->acq;
  766. acq_sz=0;
  767. list_for(acq) {
  768. service_sz = SNSD_SERVICE_LLIST_PACK_SZ(acq->service);
  769. acq_sz = ACQ_PACK_SZ(service_sz);
  770. }
  771. sz+=ACACHE_PACK_SZ(acq_sz);
  772. hdr.tot_caches++;
  773. }
  774. free_sz=sz;
  775. buf=pack=xmalloc(sz);
  776. /* Write the header of the package */
  777. bufput(&hdr, sizeof(struct andna_cache_pkt_hdr));
  778. free_sz-=sizeof(struct andna_cache_pkt_hdr);
  779. ints_host_to_network(pack, andna_cache_pkt_hdr_iinfo);
  780. if(!hdr.tot_caches)
  781. goto finish;
  782. /* Pack the rest of the andna_cache */
  783. ac=acache;
  784. list_for(ac) {
  785. psz=pack_single_andna_cache(buf, free_sz, ac, pack_type);
  786. buf+=psz;
  787. free_sz-=psz;
  788. }
  789. finish:
  790. *pack_sz=sz;
  791. return pack;
  792. }
  793. /*
  794. * unpack_acq_llist
  795. *
  796. * ac->queue_counter must contain the number of acq structs contained in the
  797. * package.
  798. *
  799. * `*unpacked_sz' is incremented by the number of unpacked bytes.
  800. *
  801. * `pack_type' specifies if the package will be saved in a file or sent over
  802. * the net, it is equal to ACACHE_PACK_FILE or to ACACHE_PACK_PKT.
  803. */
  804. andna_cache_queue *
  805. unpack_acq_llist(char *pack, size_t pack_sz, size_t *unpacked_sz,
  806. andna_cache *ac, int pack_type)
  807. {
  808. andna_cache_queue *acq=0;
  809. int e, tmp_counter=0;
  810. u_short snsd_counter;
  811. time_t cur_t;
  812. char *buf;
  813. cur_t=time(0);
  814. buf=pack;
  815. for(e=0; e < ac->queue_counter; e++) {
  816. acq=xzalloc(sizeof(andna_cache_queue));
  817. ints_network_to_host(buf, acq_body_iinfo);
  818. bufget(&acq->timestamp, sizeof(uint32_t));
  819. if(pack_type == ACACHE_PACK_PKT)
  820. acq->timestamp = cur_t - acq->timestamp;
  821. bufget(&acq->hname_updates, sizeof(u_short));
  822. bufget(&acq->pubkey, ANDNA_PKEY_LEN);
  823. bufget(&acq->snsd_counter, sizeof(u_short));
  824. pack_sz-=ACACHE_BODY_PACK_SZ;
  825. (*unpacked_sz)+=ACACHE_BODY_PACK_SZ;
  826. acq->service=snsd_unpack_all_service(buf, pack_sz, unpacked_sz,
  827. &snsd_counter);
  828. if(acq->snsd_counter != snsd_counter) {
  829. debug(DBG_SOFT, ERROR_MSG "unpack_acq:"
  830. "snsd_counter (%h) != snsd_counter (%h)",
  831. ERROR_POS, acq->snsd_counter,
  832. snsd_counter);
  833. xfree(acq);
  834. list_destroy(ac->acq);
  835. return 0;
  836. }
  837. clist_add(&ac->acq, &tmp_counter, acq);
  838. }
  839. return ac->acq;
  840. }
  841. /*
  842. * unpack_andna_cache
  843. *
  844. * Unpacks a packed andna cache linked list and returns the
  845. * its head.
  846. * `counter' is set to the number of struct in the llist.
  847. * `pack_type' specifies if the package will be saved in a file or sent over
  848. * the net, it is equal to ACACHE_PACK_FILE or to ACACHE_PACK_PKT.
  849. *
  850. * On error 0 is returned and `*counter' is set to -1.
  851. * Warning: `pack' will be modified during the unpacking.
  852. */
  853. andna_cache *unpack_andna_cache(char *pack, size_t pack_sz, int *counter,
  854. int pack_type)
  855. {
  856. struct andna_cache_pkt_hdr *hdr;
  857. andna_cache *ac, *ac_head=0;
  858. char *buf;
  859. size_t sz=0;
  860. int i, err=0;
  861. size_t unpacked_sz=0;
  862. hdr=(struct andna_cache_pkt_hdr *)pack;
  863. ints_network_to_host(hdr, andna_cache_pkt_hdr_iinfo);
  864. *counter=0;
  865. if(!hdr->tot_caches)
  866. ERROR_FINISH(err, 1, finish);
  867. buf=pack + sizeof(struct andna_cache_pkt_hdr);
  868. sz=sizeof(struct andna_cache_pkt_hdr);
  869. for(i=0; i<hdr->tot_caches; i++) {
  870. sz+=ACACHE_BODY_PACK_SZ;
  871. if(sz > pack_sz)
  872. ERROR_FINISH(err, 1, finish); /* overflow */
  873. ac=xzalloc(sizeof(andna_cache));
  874. ints_network_to_host(buf, andna_cache_body_iinfo);
  875. bufget(ac->hash, ANDNA_HASH_SZ);
  876. bufget(&ac->flags, sizeof(char));
  877. bufget(&ac->queue_counter, sizeof(u_short));
  878. sz+=ACQ_PACK_SZ(0)*ac->queue_counter;
  879. if(sz > pack_sz)
  880. ERROR_FINISH(err, 1, finish); /* overflow */
  881. unpacked_sz+=ACACHE_BODY_PACK_SZ;
  882. ac->acq=unpack_acq_llist(buf, pack_sz-unpacked_sz, &unpacked_sz,
  883. ac, pack_type);
  884. clist_add(&ac_head, counter, ac);
  885. }
  886. finish:
  887. if(err)
  888. *counter=-1;
  889. return ac_head;
  890. }
  891. /*
  892. * pack_counter_cache: packs the entire counter cache linked list that starts
  893. * with the head `counter'. The size of the pack is stored in `pack_sz'.
  894. * The pointer to the newly allocated pack is returned.
  895. * The pack will be in network order.
  896. */
  897. char *pack_counter_cache(counter_c *countercache, size_t *pack_sz)
  898. {
  899. struct counter_c_pkt_hdr hdr;
  900. counter_c *cc=countercache;
  901. counter_c_hashes *cch;
  902. char *pack, *buf, *p;
  903. size_t sz;
  904. time_t cur_t;
  905. uint32_t t;
  906. /* Calculate the pack size */
  907. hdr.tot_caches=0;
  908. sz=sizeof(struct counter_c_pkt_hdr);
  909. list_for(cc) {
  910. sz+=COUNTER_CACHE_PACK_SZ(cc->hashes);
  911. hdr.tot_caches++;
  912. }
  913. pack=xmalloc(sz);
  914. memcpy(pack, &hdr, sizeof(struct counter_c_pkt_hdr));
  915. ints_host_to_network(pack, counter_c_pkt_hdr_iinfo);
  916. if(hdr.tot_caches) {
  917. cur_t=time(0);
  918. buf=pack + sizeof(struct counter_c_pkt_hdr);
  919. cc=countercache;
  920. list_for(cc) {
  921. p=buf;
  922. bufput(cc->pubkey, ANDNA_PKEY_LEN);
  923. bufput(&cc->flags, sizeof(char));
  924. bufput(&cc->hashes, sizeof(u_short));
  925. ints_host_to_network(p, counter_c_body_iinfo);
  926. cch=cc->cch;
  927. list_for(cch) {
  928. p=buf;
  929. t = cur_t - cch->timestamp;
  930. bufput(&t, sizeof(uint32_t));
  931. bufput(&cch->hname_updates, sizeof(u_short));
  932. bufput(cch->hash, ANDNA_HASH_SZ);
  933. ints_host_to_network(p, counter_c_hashes_body_iinfo);
  934. }
  935. }
  936. }
  937. *pack_sz=sz;
  938. return pack;
  939. }
  940. /*
  941. * unpack_counter_cache
  942. *
  943. * Unpacks a packed counter cache linked list and returns the its head.
  944. * `counter' is set to the number of struct in the llist.
  945. *
  946. * On error 0 is returned and `*counter' is set to -1.
  947. *
  948. * Note: `pack' will be modified during the unpacking.
  949. */
  950. counter_c *unpack_counter_cache(char *pack, size_t pack_sz, int *counter)
  951. {
  952. struct counter_c_pkt_hdr *hdr;
  953. counter_c *cc, *cc_head=0;
  954. counter_c_hashes *cch;
  955. char *buf;
  956. size_t sz;
  957. int i, e, fake_int=0;
  958. time_t cur_t;
  959. hdr=(struct counter_c_pkt_hdr *)pack;
  960. ints_network_to_host(hdr, counter_c_pkt_hdr_iinfo);
  961. *counter=0;
  962. if(hdr->tot_caches) {
  963. cur_t = time(0);
  964. buf=pack + sizeof(struct counter_c_pkt_hdr);
  965. sz=sizeof(struct counter_c_pkt_hdr);
  966. for(i=0; i<hdr->tot_caches; i++) {
  967. sz+=COUNTER_CACHE_BODY_PACK_SZ;
  968. if(sz > pack_sz)
  969. /* We don't want to overflow */
  970. ERROR_FINISH(*counter, -1, finish);
  971. cc=xzalloc(sizeof(counter_c));
  972. ints_network_to_host(buf, counter_c_body_iinfo);
  973. bufget(cc->pubkey, ANDNA_PKEY_LEN);
  974. bufget(&cc->flags, sizeof(char));
  975. bufget(&cc->hashes, sizeof(u_short));
  976. sz+=COUNTER_CACHE_HASHES_PACK_SZ * cc->hashes;
  977. if(sz > pack_sz)
  978. /* bleah */
  979. ERROR_FINISH(*counter, -1, finish);
  980. for(e=0; e < cc->hashes; e++) {
  981. cch=xzalloc(sizeof(counter_c_hashes));
  982. ints_network_to_host(buf, counter_c_hashes_body_iinfo);
  983. cch->timestamp=0;
  984. bufget(&cch->timestamp, sizeof(uint32_t));
  985. cch->timestamp = cur_t - cch->timestamp;
  986. bufget(&cch->hname_updates, sizeof(u_short));
  987. bufget(cch->hash, ANDNA_HASH_SZ);
  988. clist_add(&cc->cch, &fake_int, cch);
  989. }
  990. clist_add(&cc_head, counter, cc);
  991. }
  992. }
  993. finish:
  994. return cc_head;
  995. }
  996. /*
  997. * pack_rh_cache
  998. *
  999. * It packs the entire resolved hnames cache linked list that starts
  1000. * with the head `rhcache'. The size of the pack is stored in `pack_sz'.
  1001. * The pointer to the newly allocated pack is returned.
  1002. * The pack will be in network order.
  1003. */
  1004. char *pack_rh_cache(rh_cache *rhcache, size_t *pack_sz)
  1005. {
  1006. struct rh_cache_pkt_hdr rh_hdr;
  1007. rh_cache *rhc=rhcache;
  1008. size_t tot_pack_sz=0, service_sz;
  1009. char *pack, *buf, *body;
  1010. rh_hdr.tot_caches=0;
  1011. tot_pack_sz=sizeof(struct rh_cache_pkt_hdr);
  1012. /* Calculate the final pack size */
  1013. list_for(rhc) {
  1014. service_sz=SNSD_SERVICE_LLIST_PACK_SZ(rhc->service);
  1015. tot_pack_sz+=RH_CACHE_BODY_PACK_SZ(service_sz);
  1016. rh_hdr.tot_caches++;
  1017. }
  1018. *pack_sz=tot_pack_sz;
  1019. buf=pack=xmalloc(tot_pack_sz);
  1020. bufput(&rh_hdr, sizeof(struct rh_cache_pkt_hdr));
  1021. tot_pack_sz-=sizeof(struct rh_cache_pkt_hdr);
  1022. ints_host_to_network(pack, rh_cache_pkt_hdr_iinfo);
  1023. if(rh_hdr.tot_caches) {
  1024. rhc=rhcache;
  1025. list_for(rhc) {
  1026. body=buf;
  1027. bufput(&rhc->hash, sizeof(u_int));
  1028. bufput(&rhc->flags, sizeof(char));
  1029. bufput(&rhc->timestamp, sizeof(time_t));
  1030. tot_pack_sz-=RH_CACHE_BODY_PACK_SZ(0);
  1031. tot_pack_sz-=snsd_pack_all_services(buf, tot_pack_sz,
  1032. rhc->service);
  1033. /* host -> network order */
  1034. ints_host_to_network(buf, rh_cache_pkt_body_iinfo);
  1035. }
  1036. }
  1037. return pack;
  1038. }
  1039. /*
  1040. * unpack_rh_cache
  1041. *
  1042. * Unpacks a packed resolved hnames cache linked list and returns its head.
  1043. * `counter' is set to the number of struct in the llist.
  1044. *
  1045. * On error 0 is returned and `*counter' is set to -1.
  1046. *
  1047. * Note: `pack' will be modified during the unpacking.
  1048. */
  1049. rh_cache *unpack_rh_cache(char *pack, size_t pack_sz, int *counter)
  1050. {
  1051. struct rh_cache_pkt_hdr *hdr;
  1052. rh_cache *rhc=0, *rhc_head=0;
  1053. char *buf;
  1054. size_t unpacked_sz=0;
  1055. int i=0;
  1056. hdr=(struct rh_cache_pkt_hdr *)pack;
  1057. ints_network_to_host(hdr, rh_cache_pkt_hdr_iinfo);
  1058. *counter=0;
  1059. if(hdr->tot_caches > ANDNA_MAX_RHC_HNAMES)
  1060. ERROR_FINISH(*counter, -1, finish);
  1061. *counter=0;
  1062. if(hdr->tot_caches) {
  1063. buf=pack + sizeof(struct rh_cache_pkt_hdr);
  1064. unpacked_sz=sizeof(struct rh_cache_pkt_hdr);
  1065. for(i=0; i<hdr->tot_caches; i++) {
  1066. unpacked_sz+=RH_CACHE_BODY_PACK_SZ(0);
  1067. if(unpacked_sz > pack_sz)
  1068. ERROR_FINISH(*counter, -1, finish);
  1069. ints_network_to_host(buf, rh_cache_pkt_body_iinfo);
  1070. rhc=xzalloc(sizeof(rh_cache));
  1071. bufget(&rhc->hash, sizeof(u_int));
  1072. bufget(&rhc->flags, sizeof(char));
  1073. bufget(&rhc->timestamp, sizeof(time_t));
  1074. rhc->service=snsd_unpack_all_service(buf, pack_sz,
  1075. &unpacked_sz, 0);
  1076. clist_add(&rhc_head, counter, rhc);
  1077. }
  1078. }
  1079. finish:
  1080. return rhc_head;
  1081. }
  1082. /*
  1083. *
  1084. * * * * Save/Load functions * * *
  1085. *
  1086. */
  1087. /*
  1088. * save_lcl_keyring: saves a local cache keyring in the specified `file'.
  1089. */
  1090. int save_lcl_keyring(lcl_cache_keyring *keyring, char *file)
  1091. {
  1092. FILE *fd;
  1093. size_t pack_sz;
  1094. char *pack;
  1095. /*Pack!*/
  1096. pack=pack_lcl_keyring(keyring, &pack_sz);
  1097. if(!pack_sz || !pack)
  1098. return 0;
  1099. if((fd=fopen(file, "w"))==NULL) {
  1100. error("Cannot save the lcl_keyring in %s: %s", file,
  1101. strerror(errno));
  1102. return -1;
  1103. }
  1104. /*Write!*/
  1105. fwrite(pack, pack_sz, 1, fd);
  1106. xfree(pack);
  1107. fclose(fd);
  1108. return 0;
  1109. }
  1110. /*
  1111. * load_lcl_keyring
  1112. *
  1113. * loads from `file' a local cache keyring and restores in it the RSA keys.
  1114. *
  1115. * On error -1 is returned.
  1116. */
  1117. int load_lcl_keyring(lcl_cache_keyring *keyring, char *file)
  1118. {
  1119. FILE *fd;
  1120. char *pack=0;
  1121. size_t pack_sz;
  1122. int ret=0;
  1123. if(!(fd=fopen(file, "r"))) {
  1124. error("Cannot load the lcl_keyring from %s: %s", file,
  1125. strerror(errno));
  1126. return -1;
  1127. }
  1128. fseek(fd, 0, SEEK_END);
  1129. pack_sz=ftell(fd);
  1130. rewind(fd);
  1131. pack=xmalloc(pack_sz);
  1132. if(!fread(pack, pack_sz, 1, fd))
  1133. ERROR_FINISH(ret, -1, finish);
  1134. ret=unpack_lcl_keyring(keyring, pack, pack_sz);
  1135. finish:
  1136. if(pack)
  1137. xfree(pack);
  1138. fclose(fd);
  1139. if(ret < 0)
  1140. debug(DBG_NORMAL, "Malformed or empty lcl_keyring file. "
  1141. "Aborting load_lcl_keyring().");
  1142. return ret;
  1143. }
  1144. /*
  1145. * save_lcl_cache: saves a local cache linked list in the specified `file'.
  1146. */
  1147. int save_lcl_cache(lcl_cache *lcl, char *file)
  1148. {
  1149. FILE *fd;
  1150. size_t pack_sz;
  1151. char *pack;
  1152. /*Pack!*/
  1153. pack=pack_lcl_cache(lcl, &pack_sz);
  1154. if(!pack_sz || !pack)
  1155. return 0;
  1156. if((fd=fopen(file, "w"))==NULL) {
  1157. error("Cannot save the lcl_cache in %s: %s", file, strerror(errno));
  1158. return -1;
  1159. }
  1160. /*Write!*/
  1161. fwrite(pack, pack_sz, 1, fd);
  1162. xfree(pack);
  1163. fclose(fd);
  1164. return 0;
  1165. }
  1166. /*
  1167. * load_lcl_cache: loads from `file' a local cache list and returns the head
  1168. * of the newly allocated llist. In `counter' it is stored the number of
  1169. * structs of the llist.
  1170. * On error 0 is returned.
  1171. */
  1172. lcl_cache *load_lcl_cache(char *file, int *counter)
  1173. {
  1174. lcl_cache *lcl=0;
  1175. FILE *fd;
  1176. char *pack=0;
  1177. size_t pack_sz;
  1178. if(!(fd=fopen(file, "r"))) {
  1179. error("Cannot load the lcl_cache from %s: %s", file, strerror(errno));
  1180. return 0;
  1181. }
  1182. fseek(fd, 0, SEEK_END);
  1183. pack_sz=ftell(fd);
  1184. rewind(fd);
  1185. pack=xmalloc(pack_sz);
  1186. if(!fread(pack, pack_sz, 1, fd))
  1187. goto finish;
  1188. lcl=unpack_lcl_cache(pack, pack_sz, counter);
  1189. finish:
  1190. if(pack)
  1191. xfree(pack);
  1192. fclose(fd);
  1193. if(!lcl && counter < 0)
  1194. error("Malformed lcl_cache file (%s)"
  1195. "Aborting load_lcl_cache().", file);
  1196. return lcl;
  1197. }
  1198. /*
  1199. * save_andna_cache: saves an andna cache linked list in the `file' specified
  1200. */
  1201. int save_andna_cache(andna_cache *acache, char *file)
  1202. {
  1203. FILE *fd;
  1204. size_t pack_sz;
  1205. char *pack;
  1206. /*Pack!*/
  1207. pack=pack_andna_cache(acache, &pack_sz, ACACHE_PACK_FILE);
  1208. if(!pack_sz || !pack)
  1209. return 0;
  1210. if((fd=fopen(file, "w"))==NULL) {
  1211. error("Cannot save the andna_cache in %s: %s", file, strerror(errno));
  1212. return -1;
  1213. }
  1214. /*Write!*/
  1215. fwrite(pack, pack_sz, 1, fd);
  1216. xfree(pack);
  1217. fclose(fd);
  1218. return 0;
  1219. }
  1220. /*
  1221. * load_andna_cache: loads from `file' an andna cache list and returns the head
  1222. * of the newly allocated llist. In `counter' it is stored the number of
  1223. * list's structs.
  1224. * On error 0 is returned.
  1225. */
  1226. andna_cache *load_andna_cache(char *file, int *counter)
  1227. {
  1228. andna_cache *acache=0;
  1229. FILE *fd;
  1230. char *pack=0;
  1231. size_t pack_sz;
  1232. if((fd=fopen(file, "r"))==NULL) {
  1233. error("Cannot load the andna_cache from %s: %s", file, strerror(errno));
  1234. return 0;
  1235. }
  1236. fseek(fd, 0, SEEK_END);
  1237. pack_sz=ftell(fd);
  1238. rewind(fd);
  1239. pack=xmalloc(pack_sz);
  1240. if(!fread(pack, pack_sz, 1, fd))
  1241. goto finish;
  1242. acache=unpack_andna_cache(pack, pack_sz, counter, ACACHE_PACK_FILE);
  1243. finish:
  1244. if(pack)
  1245. xfree(pack);
  1246. fclose(fd);
  1247. if(!acache && counter < 0)
  1248. error("Malformed andna_cache file."
  1249. " Aborting load_andna_cache().");
  1250. else if(!acache)
  1251. debug(DBG_NORMAL, "Empty andna_cache file.");
  1252. return acache;
  1253. }
  1254. /*
  1255. * save_counter_c: saves a counter cache linked list in the `file' specified
  1256. */
  1257. int save_counter_c(counter_c *countercache, char *file)
  1258. {
  1259. FILE *fd;
  1260. size_t pack_sz;
  1261. char *pack;
  1262. /*Pack!*/
  1263. pack=pack_counter_cache(countercache, &pack_sz);
  1264. if(!pack_sz || !pack)
  1265. return 0;
  1266. if((fd=fopen(file, "w"))==NULL) {
  1267. error("Cannot save the counter_c in %s: %s", file, strerror(errno));
  1268. return -1;
  1269. }
  1270. /*Write!*/
  1271. fwrite(pack, pack_sz, 1, fd);
  1272. xfree(pack);
  1273. fclose(fd);
  1274. return 0;
  1275. }
  1276. /*
  1277. * load_counter_c: loads from `file' a counter cache list and returns the head
  1278. * of the newly allocated llist. In `counter' it is stored the number of
  1279. * list's structs.
  1280. * On error 0 is returned.
  1281. */
  1282. counter_c *load_counter_c(char *file, int *counter)
  1283. {
  1284. counter_c *countercache=0;
  1285. FILE *fd;
  1286. char *pack=0;
  1287. size_t pack_sz;
  1288. if((fd=fopen(file, "r"))==NULL) {
  1289. error("Cannot load the counter_c from %s: %s", file, strerror(errno));
  1290. return 0;
  1291. }
  1292. fseek(fd, 0, SEEK_END);
  1293. pack_sz=ftell(fd);
  1294. rewind(fd);
  1295. pack=xmalloc(pack_sz);
  1296. if(!fread(pack, pack_sz, 1, fd))
  1297. goto finish;
  1298. countercache=unpack_counter_cache(pack, pack_sz, counter);
  1299. finish:
  1300. if(pack)
  1301. xfree(pack);
  1302. fclose(fd);
  1303. if(!countercache && counter < 0)
  1304. debug(DBG_NORMAL, "Malformed counter_c file (%s). "
  1305. "Aborting load_counter_c().", file);
  1306. return countercache;
  1307. }
  1308. /*
  1309. * save_rh_cache: saves the resolved hnames cache linked list `rh' in the
  1310. * `file' specified.
  1311. */
  1312. int save_rh_cache(rh_cache *rh, char *file)
  1313. {
  1314. FILE *fd=0;
  1315. size_t pack_sz;
  1316. char *pack;
  1317. /*Pack!*/
  1318. pack=pack_rh_cache(rh, &pack_sz);
  1319. if(!pack_sz || !pack)
  1320. return 0;
  1321. if(!(fd=fopen(file, "w"))) {
  1322. error("Cannot save the rh_cache in %s: %s",
  1323. file, strerror(errno));
  1324. return -1;
  1325. }
  1326. /*Write!*/
  1327. fwrite(pack, pack_sz, 1, fd);
  1328. xfree(pack);
  1329. fclose(fd);
  1330. return 0;
  1331. }
  1332. /*
  1333. * load_rh_cache: loads from `file' a resolved hnames cache list and returns
  1334. * the head of the newly allocated llist. In `counter' it is stored the number
  1335. * of structs of the llist.
  1336. * On error 0 is returned.
  1337. */
  1338. rh_cache *load_rh_cache(char *file, int *counter)
  1339. {
  1340. rh_cache *rh=0;
  1341. FILE *fd;
  1342. char *pack=0;
  1343. size_t pack_sz;
  1344. if((fd=fopen(file, "r"))==NULL) {
  1345. error("Cannot load the rh_cache from %s: %s", file, strerror(errno));
  1346. return 0;
  1347. }
  1348. fseek(fd, 0, SEEK_END);
  1349. pack_sz=ftell(fd);
  1350. rewind(fd);
  1351. pack=xmalloc(pack_sz);
  1352. if(!fread(pack, pack_sz, 1, fd))
  1353. goto finish;
  1354. rh=unpack_rh_cache(pack, pack_sz, counter);
  1355. finish:
  1356. if(pack)
  1357. xfree(pack);
  1358. fclose(fd);
  1359. if(!rh && counter < 0)
  1360. error("Malformed rh_cache file (%s). "
  1361. "Aborting load_rh_cache().", file);
  1362. return rh;
  1363. }
  1364. /*
  1365. * load_hostnames
  1366. *
  1367. * It reads the `file' specified and reads each line in it.
  1368. * The strings read are the hostnames that will be registered in andna.
  1369. * Only ANDNA_MAX_HOSTNAMES lines are read. Each line can be maximum of
  1370. * ANDNA_MAX_HNAME_LEN character long.
  1371. *
  1372. * This function updates automagically the old local cache that is pointed by
  1373. * `*old_alcl_head'. The hostnames that are no more present in the loaded
  1374. * `file' are discarded from the local cache.
  1375. * Since a new local cache is allocated and the old is destroyed, the new
  1376. * pointer to it is written in `*old_alcl_head'.
  1377. *
  1378. * The `old_alcl_counter' is updated too.
  1379. *
  1380. * This function shall be used each time the `file' changes.
  1381. *
  1382. * On error -1 is returned, otherwise 0 shall be the sacred value.
  1383. */
  1384. int load_hostnames(char *file, lcl_cache **old_alcl_head, int *old_alcl_counter)
  1385. {
  1386. FILE *fd;
  1387. char buf[ANDNA_MAX_HNAME_LEN+1];
  1388. size_t slen;
  1389. time_t cur_t, diff;
  1390. int i=0;
  1391. lcl_cache *alcl, *old_alcl, *new_alcl_head=0;
  1392. int new_alcl_counter=0;
  1393. if((fd=fopen(file, "r"))==NULL) {
  1394. error("Cannot load any hostnames from %s: %s", file, strerror(errno));
  1395. return -1;
  1396. }
  1397. cur_t=time(0);
  1398. while(!feof(fd) && i < ANDNA_MAX_HOSTNAMES) {
  1399. setzero(buf, ANDNA_MAX_HNAME_LEN+1);
  1400. fgets(buf, ANDNA_MAX_HNAME_LEN, fd);
  1401. if(feof(fd))
  1402. break;
  1403. if((*buf)=='#' || (*buf)=='\n' || !(*buf)) {
  1404. /* Strip off the comment lines */
  1405. continue;
  1406. } else {
  1407. slen=strlen(buf);
  1408. if(buf[slen-1] == '\n') {
  1409. /* Don't include the newline in the string */
  1410. buf[slen-1]='\0';
  1411. slen=strlen(buf);
  1412. }
  1413. /* Add the hname in the new local cache */
  1414. alcl = lcl_cache_new(buf);
  1415. clist_add(&new_alcl_head, &new_alcl_counter, alcl);
  1416. /*
  1417. * If there is an equal entry in the old lcl_cache and
  1418. * it isn't expired, copy the old data in the new
  1419. * struct.
  1420. */
  1421. old_alcl = lcl_cache_find_hname(*old_alcl_head,
  1422. alcl->hostname);
  1423. if(old_alcl) {
  1424. diff=cur_t - old_alcl->timestamp;
  1425. if(diff < ANDNA_EXPIRATION_TIME) {
  1426. alcl->timestamp=old_alcl->timestamp;
  1427. alcl->hname_updates=old_alcl->hname_updates;
  1428. }
  1429. }
  1430. i++;
  1431. }
  1432. }
  1433. /* Remove completely the old lcl_cache */
  1434. lcl_cache_destroy(*old_alcl_head, old_alcl_counter);
  1435. /* Update the pointers */
  1436. *old_alcl_head=new_alcl_head;
  1437. *old_alcl_counter=new_alcl_counter;
  1438. fclose(fd);
  1439. return 0;
  1440. }
  1441. /*
  1442. * load_snsd
  1443. *
  1444. * It loads the SNSD records to be registered from the given `file'.
  1445. * In the file there shall be one record per line, up to SNSD_MAX_RECORDS-1#
  1446. * records.
  1447. *
  1448. * Each line has to be written in the following format:
  1449. * hostname:snsd_hostname:service:priority:weight[:pub_key_file]
  1450. * or
  1451. * hostname:snsd_ip:service:priority:weight[:pub_key_file]
  1452. *
  1453. * The old records present in `alcl_head' will be deleted and substituted by
  1454. * the loaded ones.
  1455. *
  1456. * On error -1 is returned.
  1457. * If a syntax error is encountered in the file -2 is returned.
  1458. */
  1459. int load_snsd(char *file, lcl_cache *alcl_head)
  1460. {
  1461. #define MAX_SNSD_LINE_SZ (ANDNA_MAX_HNAME_LEN*4)
  1462. FILE *fd;
  1463. size_t slen;
  1464. int line=0, fields, e, service, nodes, ret=0, err;
  1465. char buf[MAX_SNSD_LINE_SZ+1], **records;
  1466. u_char proto, abort=0;
  1467. lcl_cache *alcl;
  1468. snsd_service *sns;
  1469. snsd_prio *snp;
  1470. snsd_node *snd, snsd_node;
  1471. inet_prefix ip;
  1472. /* Delete all the old snsd records */
  1473. alcl=alcl_head;
  1474. list_for(alcl)
  1475. if(alcl->service)
  1476. snsd_service_llist_del(&alcl->service);
  1477. if((fd=fopen(file, "r"))==NULL) {
  1478. error("Cannot open the snsd_nodes file from %s: %s",
  1479. file, strerror(errno));
  1480. return -1;
  1481. }
  1482. line=1;
  1483. while(!feof(fd) && line <= SNSD_MAX_RECORDS-1) {
  1484. setzero(buf, MAX_SNSD_LINE_SZ+1);
  1485. fgets(buf, MAX_SNSD_LINE_SZ, fd);
  1486. if(feof(fd))
  1487. break;
  1488. if((*buf)=='#' || (*buf)=='\n' || !(*buf)) {
  1489. /* Strip off the comment lines */
  1490. line++;
  1491. continue;
  1492. } else {
  1493. slen=strlen(buf);
  1494. if(buf[slen-1] == '\n') {
  1495. /* Don't include the newline in the string */
  1496. buf[slen-1]='\0';
  1497. slen=strlen(buf);
  1498. }
  1499. records=split_string(buf, ":", &fields, MAX_SNSD_FIELDS,
  1500. ANDNA_MAX_HNAME_LEN*2);
  1501. if(fields < MIN_SNSD_FIELDS) {
  1502. error("%s: Syntax error in line %d.\n"
  1503. " The correct syntax is:\n"
  1504. " \thostname:snsd_hostname:service:"
  1505. "priority:weight[:pub_key_file]\n"
  1506. " or\n"
  1507. " \thostname:snsd_ip:service:"
  1508. "priority:weight[:pub_key_file]",
  1509. file, line);
  1510. ERROR_FINISH(abort, 1, skip_line);
  1511. }
  1512. /*
  1513. * hostname
  1514. */
  1515. alcl=lcl_cache_find_hname(alcl_head, records[0]);
  1516. if(!alcl) {
  1517. error("%s: line %d: The hostname \"%s\" doesn't"
  1518. " exist in your local cache.\n"
  1519. " Register it in the `andna_hostnames' file",
  1520. file, line, records[0]);
  1521. ERROR_FINISH(abort, 1, skip_line);
  1522. }
  1523. /*
  1524. * snsd record
  1525. */
  1526. if(str_to_inet(records[1], &ip) >= 0) {
  1527. inet_copy_ipdata_raw(snsd_node.record, &ip);
  1528. snsd_node.flags=SNSD_NODE_IP;
  1529. } else {
  1530. hash_md5((u_char*)records[1], strlen(records[1]),
  1531. (u_char *)snsd_node.record);
  1532. snsd_node.flags=SNSD_NODE_HNAME;
  1533. }
  1534. if(!strncmp(records[0], records[1],
  1535. ANDNA_MAX_HNAME_LEN) &&
  1536. !strcmp(records[2], "0"))
  1537. snsd_node.flags=SNSD_NODE_MAIN_IP | SNSD_NODE_IP;
  1538. /***
  1539. * Parse service and protocol
  1540. */
  1541. err=str_to_snsd_service(records[2], &service, &proto);
  1542. if(err == -1)
  1543. error("%s: error in line %d: \"%s\""
  1544. " isn't a valid protocol\n",
  1545. file, line, records[2]);
  1546. else if(err == -2)
  1547. error("%s: error in line %d: \"%s\""
  1548. " isn't a valid service\n",
  1549. file, line, records[2]);
  1550. if(err < 0)
  1551. ERROR_FINISH(abort, 1, skip_line);
  1552. /**/
  1553. /* Store service and protocol */
  1554. sns=snsd_add_service(&alcl->service, service, proto);
  1555. /* priority */
  1556. snp=snsd_add_prio(&sns->prio, atoi(records[3]));
  1557. nodes=snsd_count_prio_nodes(sns->prio);
  1558. if(nodes >= SNSD_MAX_REC_SERV-1) {
  1559. error("%s: The maximum number of records for"
  1560. " the service \"%s\" has been reached.\n"
  1561. " The maximum is %d records per service",
  1562. file, service, SNSD_MAX_REC_SERV);
  1563. ERROR_FINISH(abort, 1, skip_line);
  1564. }
  1565. /* node and weight */
  1566. snd=snsd_add_node(&snp->node, &alcl->snsd_counter,
  1567. SNSD_MAX_RECORDS-1, snsd_node.record);
  1568. snd->weight=SNSD_WEIGHT(atoi(records[4]));
  1569. snd->flags|=snsd_node.flags;
  1570. /* pub_key_file
  1571. * TODO:
  1572. * if(fields >= 6)
  1573. * snd->pubkey=load_pubkey(records[5])
  1574. */
  1575. skip_line:
  1576. for(e=0; e<fields; e++)
  1577. xfree(records[e]);
  1578. if(abort)
  1579. ERROR_FINISH(ret, -2, finish);
  1580. }
  1581. line++;
  1582. }
  1583. finish:
  1584. fclose(fd);
  1585. return ret;
  1586. }
  1587. /*
  1588. *
  1589. * * * * Modify /etc/resolv.conf * * *
  1590. *
  1591. */
  1592. /*
  1593. * add_resolv_conf: It opens `file' and write in the first line `hname' moving
  1594. * down the previous lines. The old `file' is backupped in `file'.bak.
  1595. * Example: add_resolv_conf("nameserver 127.0.0.1", "/etc/resolv.conf").
  1596. * Use del_resolv_conf to restore `file' with its backup.
  1597. * On error -1 is returned.
  1598. */
  1599. int add_resolv_conf(char *hname, char *file)
  1600. {
  1601. FILE *fin=0, /* `file' */
  1602. *fin_bak=0, /* `file'.bak */
  1603. *fout=0, /* The replaced `file' */
  1604. *fout_back=0; /* The backup of `file' */
  1605. char *buf=0, *p, *file_bk=0;
  1606. size_t buf_sz;
  1607. int ret=0;
  1608. /*
  1609. * Open and read `file'
  1610. */
  1611. if(!(fin=fopen(file, "r"))) {
  1612. error("add_resolv_conf: cannot load %s: %s", file, strerror(errno));
  1613. ERROR_FINISH(ret, -1, finish);
  1614. }
  1615. /* Prepare the name of the backup file */
  1616. file_bk=xmalloc(strlen(file) + strlen(".bak") + 1);
  1617. *file_bk=0;
  1618. strcpy(file_bk, file);
  1619. strcat(file_bk, ".bak");
  1620. reread_fin:
  1621. fseek(fin, 0, SEEK_END);
  1622. buf_sz=ftell(fin);
  1623. rewind(fin);
  1624. buf=xmalloc(buf_sz);
  1625. if(!fread(buf, buf_sz, 1, fin)) {
  1626. error("add_resolv_conf: it wasn't possible to read the %s file",
  1627. file);
  1628. ERROR_FINISH(ret, -1, finish);
  1629. }
  1630. /*
  1631. * If there is already the `hname' string in the first line, try to
  1632. * read `file'.bak, if it doesn't exist do nothing.
  1633. */
  1634. if(buf_sz-1 >= strlen(hname) && !strncmp(buf, hname, strlen(hname))) {
  1635. if(fin == fin_bak) {
  1636. /*
  1637. * We've already read `fin_bak', and it has
  1638. * the `hname' string in its first line too. Stop it.
  1639. */
  1640. goto finish;
  1641. }
  1642. debug(DBG_NORMAL, "add_resolv_conf: Reading %s instead",
  1643. file_bk);
  1644. if(!(fin_bak=fopen(file_bk, "r")))
  1645. goto finish;
  1646. fclose(fin);
  1647. fin=fin_bak;
  1648. goto reread_fin;
  1649. }
  1650. /*
  1651. * Backup `file' in `file'.bak
  1652. */
  1653. if(!(fout_back=fopen(file_bk, "w"))) {
  1654. error("add_resolv_conf: cannot create a backup copy of %s in %s: %s", file,
  1655. file_bk, strerror(errno));
  1656. ERROR_FINISH(ret, -1, finish);
  1657. }
  1658. fwrite(buf, buf_sz, 1, fout_back);
  1659. /*
  1660. * Delete `file'
  1661. */
  1662. fclose(fin);
  1663. fin=0;
  1664. unlink(file);
  1665. /*
  1666. * Add as a first line `hname' in `file'
  1667. */
  1668. if(!(fout=fopen(file, "w"))) {
  1669. error("add_resolv_conf: cannot reopen %s to overwrite it: %s", file,
  1670. strerror(errno));
  1671. ERROR_FINISH(ret, -1, finish);
  1672. }
  1673. fprintf(fout, "%s\n", hname);
  1674. p=buf;
  1675. while(*p) {
  1676. if(*p != '#')
  1677. fprintf(fout, "#");
  1678. while(*p) {
  1679. fprintf(fout, "%c", *p);
  1680. if(*p == '\n')
  1681. break;
  1682. p++;
  1683. }
  1684. if(!*p)
  1685. break;
  1686. p++;
  1687. }
  1688. /*fwrite(buf, buf_sz, 1, fout);*/
  1689. finish:
  1690. if(buf)
  1691. xfree(buf);
  1692. if(file_bk)
  1693. xfree(file_bk);
  1694. if(fin)
  1695. fclose(fin);
  1696. if(fout)
  1697. fclose(fout);
  1698. if(fout_back)
  1699. fclose(fout_back);
  1700. return ret;
  1701. }
  1702. /*
  1703. * del_resolv_conf
  1704. *
  1705. * restores the old `file' modified by add_resolv_conf() by
  1706. * copying `file'.bak over `file'. If the `hname' string is present in
  1707. * `file'.bak it won't be written in `file'.
  1708. * On error it returns -1.
  1709. */
  1710. int del_resolv_conf(char *hname, char *file)
  1711. {
  1712. FILE *fin=0, *fout=0;
  1713. char *buf=0, *file_bk=0, tmp_buf[128+1];
  1714. size_t buf_sz;
  1715. int ret=0;
  1716. /*
  1717. * Open and read `file'.bak
  1718. */
  1719. file_bk=xmalloc(strlen(file) + strlen(".bak") + 1);
  1720. *file_bk=0;
  1721. strcpy(file_bk, file);
  1722. strcat(file_bk, ".bak");
  1723. if(!(fin=fopen(file_bk, "r"))) {
  1724. /*error("del_resolv_conf: cannot load %s: %s", file_bk, strerror(errno));*/
  1725. ERROR_FINISH(ret, -1, finish);
  1726. }
  1727. fseek(fin, 0, SEEK_END);
  1728. buf_sz=ftell(fin);
  1729. rewind(fin);
  1730. if(!buf_sz) {
  1731. /* `file_bk' is empty, delete it */
  1732. unlink(file_bk);
  1733. ERROR_FINISH(ret, -1, finish);
  1734. }
  1735. buf=xzalloc(buf_sz);
  1736. while(fgets(tmp_buf, 128, fin)) {
  1737. /* Skip the line which is equal to `hname' */
  1738. if(!strncmp(tmp_buf, hname, strlen(hname)))
  1739. continue;
  1740. strcat(buf, tmp_buf);
  1741. }
  1742. /*
  1743. * Delete `file'
  1744. */
  1745. unlink(file);
  1746. /*
  1747. * Copy `file'.bak in `file'
  1748. */
  1749. if(!(fout=fopen(file, "w"))) {
  1750. error("del_resolv_conf: cannot copy %s in %s: %s", file_bk,
  1751. file, strerror(errno));
  1752. ERROR_FINISH(ret, -1, finish);
  1753. }
  1754. fprintf(fout, "%s", buf);
  1755. /*
  1756. * delete `file'.bak
  1757. */
  1758. fclose(fin);
  1759. fin=0;
  1760. unlink(file_bk);
  1761. finish:
  1762. if(buf)
  1763. xfree(buf);
  1764. if(file_bk)
  1765. xfree(file_bk);
  1766. if(fin)
  1767. fclose(fin);
  1768. if(fout)
  1769. fclose(fout);
  1770. return ret;
  1771. }